super.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737
  1. /*
  2. * fs/f2fs/super.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/statfs.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/kthread.h>
  18. #include <linux/parser.h>
  19. #include <linux/mount.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/random.h>
  23. #include <linux/exportfs.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/f2fs_fs.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/quota.h>
  29. #include "f2fs.h"
  30. #include "node.h"
  31. #include "segment.h"
  32. #include "xattr.h"
  33. #include "gc.h"
  34. #include "trace.h"
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/f2fs.h>
  37. static struct kmem_cache *f2fs_inode_cachep;
  38. #ifdef CONFIG_F2FS_FAULT_INJECTION
  39. char *fault_name[FAULT_MAX] = {
  40. [FAULT_KMALLOC] = "kmalloc",
  41. [FAULT_PAGE_ALLOC] = "page alloc",
  42. [FAULT_ALLOC_NID] = "alloc nid",
  43. [FAULT_ORPHAN] = "orphan",
  44. [FAULT_BLOCK] = "no more block",
  45. [FAULT_DIR_DEPTH] = "too big dir depth",
  46. [FAULT_EVICT_INODE] = "evict_inode fail",
  47. [FAULT_TRUNCATE] = "truncate fail",
  48. [FAULT_IO] = "IO error",
  49. [FAULT_CHECKPOINT] = "checkpoint error",
  50. };
  51. static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
  52. unsigned int rate)
  53. {
  54. struct f2fs_fault_info *ffi = &sbi->fault_info;
  55. if (rate) {
  56. atomic_set(&ffi->inject_ops, 0);
  57. ffi->inject_rate = rate;
  58. ffi->inject_type = (1 << FAULT_MAX) - 1;
  59. } else {
  60. memset(ffi, 0, sizeof(struct f2fs_fault_info));
  61. }
  62. }
  63. #endif
  64. /* f2fs-wide shrinker description */
  65. static struct shrinker f2fs_shrinker_info = {
  66. .scan_objects = f2fs_shrink_scan,
  67. .count_objects = f2fs_shrink_count,
  68. .seeks = DEFAULT_SEEKS,
  69. };
  70. enum {
  71. Opt_gc_background,
  72. Opt_disable_roll_forward,
  73. Opt_norecovery,
  74. Opt_discard,
  75. Opt_nodiscard,
  76. Opt_noheap,
  77. Opt_heap,
  78. Opt_user_xattr,
  79. Opt_nouser_xattr,
  80. Opt_acl,
  81. Opt_noacl,
  82. Opt_active_logs,
  83. Opt_disable_ext_identify,
  84. Opt_inline_xattr,
  85. Opt_noinline_xattr,
  86. Opt_inline_data,
  87. Opt_inline_dentry,
  88. Opt_noinline_dentry,
  89. Opt_flush_merge,
  90. Opt_noflush_merge,
  91. Opt_nobarrier,
  92. Opt_fastboot,
  93. Opt_extent_cache,
  94. Opt_noextent_cache,
  95. Opt_noinline_data,
  96. Opt_data_flush,
  97. Opt_mode,
  98. Opt_io_size_bits,
  99. Opt_fault_injection,
  100. Opt_lazytime,
  101. Opt_nolazytime,
  102. Opt_quota,
  103. Opt_noquota,
  104. Opt_usrquota,
  105. Opt_grpquota,
  106. Opt_prjquota,
  107. Opt_usrjquota,
  108. Opt_grpjquota,
  109. Opt_prjjquota,
  110. Opt_offusrjquota,
  111. Opt_offgrpjquota,
  112. Opt_offprjjquota,
  113. Opt_jqfmt_vfsold,
  114. Opt_jqfmt_vfsv0,
  115. Opt_jqfmt_vfsv1,
  116. Opt_err,
  117. };
  118. static match_table_t f2fs_tokens = {
  119. {Opt_gc_background, "background_gc=%s"},
  120. {Opt_disable_roll_forward, "disable_roll_forward"},
  121. {Opt_norecovery, "norecovery"},
  122. {Opt_discard, "discard"},
  123. {Opt_nodiscard, "nodiscard"},
  124. {Opt_noheap, "no_heap"},
  125. {Opt_heap, "heap"},
  126. {Opt_user_xattr, "user_xattr"},
  127. {Opt_nouser_xattr, "nouser_xattr"},
  128. {Opt_acl, "acl"},
  129. {Opt_noacl, "noacl"},
  130. {Opt_active_logs, "active_logs=%u"},
  131. {Opt_disable_ext_identify, "disable_ext_identify"},
  132. {Opt_inline_xattr, "inline_xattr"},
  133. {Opt_noinline_xattr, "noinline_xattr"},
  134. {Opt_inline_data, "inline_data"},
  135. {Opt_inline_dentry, "inline_dentry"},
  136. {Opt_noinline_dentry, "noinline_dentry"},
  137. {Opt_flush_merge, "flush_merge"},
  138. {Opt_noflush_merge, "noflush_merge"},
  139. {Opt_nobarrier, "nobarrier"},
  140. {Opt_fastboot, "fastboot"},
  141. {Opt_extent_cache, "extent_cache"},
  142. {Opt_noextent_cache, "noextent_cache"},
  143. {Opt_noinline_data, "noinline_data"},
  144. {Opt_data_flush, "data_flush"},
  145. {Opt_mode, "mode=%s"},
  146. {Opt_io_size_bits, "io_bits=%u"},
  147. {Opt_fault_injection, "fault_injection=%u"},
  148. {Opt_lazytime, "lazytime"},
  149. {Opt_nolazytime, "nolazytime"},
  150. {Opt_quota, "quota"},
  151. {Opt_noquota, "noquota"},
  152. {Opt_usrquota, "usrquota"},
  153. {Opt_grpquota, "grpquota"},
  154. {Opt_prjquota, "prjquota"},
  155. {Opt_usrjquota, "usrjquota=%s"},
  156. {Opt_grpjquota, "grpjquota=%s"},
  157. {Opt_prjjquota, "prjjquota=%s"},
  158. {Opt_offusrjquota, "usrjquota="},
  159. {Opt_offgrpjquota, "grpjquota="},
  160. {Opt_offprjjquota, "prjjquota="},
  161. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  162. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  163. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  164. {Opt_err, NULL},
  165. };
  166. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  167. {
  168. struct va_format vaf;
  169. va_list args;
  170. va_start(args, fmt);
  171. vaf.fmt = fmt;
  172. vaf.va = &args;
  173. printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  174. va_end(args);
  175. }
  176. static void init_once(void *foo)
  177. {
  178. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  179. inode_init_once(&fi->vfs_inode);
  180. }
  181. #ifdef CONFIG_QUOTA
  182. static const char * const quotatypes[] = INITQFNAMES;
  183. #define QTYPE2NAME(t) (quotatypes[t])
  184. static int f2fs_set_qf_name(struct super_block *sb, int qtype,
  185. substring_t *args)
  186. {
  187. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  188. char *qname;
  189. int ret = -EINVAL;
  190. if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) {
  191. f2fs_msg(sb, KERN_ERR,
  192. "Cannot change journaled "
  193. "quota options when quota turned on");
  194. return -EINVAL;
  195. }
  196. qname = match_strdup(args);
  197. if (!qname) {
  198. f2fs_msg(sb, KERN_ERR,
  199. "Not enough memory for storing quotafile name");
  200. return -EINVAL;
  201. }
  202. if (sbi->s_qf_names[qtype]) {
  203. if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
  204. ret = 0;
  205. else
  206. f2fs_msg(sb, KERN_ERR,
  207. "%s quota file already specified",
  208. QTYPE2NAME(qtype));
  209. goto errout;
  210. }
  211. if (strchr(qname, '/')) {
  212. f2fs_msg(sb, KERN_ERR,
  213. "quotafile must be on filesystem root");
  214. goto errout;
  215. }
  216. sbi->s_qf_names[qtype] = qname;
  217. set_opt(sbi, QUOTA);
  218. return 0;
  219. errout:
  220. kfree(qname);
  221. return ret;
  222. }
  223. static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
  224. {
  225. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  226. if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) {
  227. f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  228. " when quota turned on");
  229. return -EINVAL;
  230. }
  231. kfree(sbi->s_qf_names[qtype]);
  232. sbi->s_qf_names[qtype] = NULL;
  233. return 0;
  234. }
  235. static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
  236. {
  237. /*
  238. * We do the test below only for project quotas. 'usrquota' and
  239. * 'grpquota' mount options are allowed even without quota feature
  240. * to support legacy quotas in quota files.
  241. */
  242. if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
  243. f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
  244. "Cannot enable project quota enforcement.");
  245. return -1;
  246. }
  247. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] ||
  248. sbi->s_qf_names[PRJQUOTA]) {
  249. if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  250. clear_opt(sbi, USRQUOTA);
  251. if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  252. clear_opt(sbi, GRPQUOTA);
  253. if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA])
  254. clear_opt(sbi, PRJQUOTA);
  255. if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
  256. test_opt(sbi, PRJQUOTA)) {
  257. f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
  258. "format mixing");
  259. return -1;
  260. }
  261. if (!sbi->s_jquota_fmt) {
  262. f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
  263. "not specified");
  264. return -1;
  265. }
  266. }
  267. return 0;
  268. }
  269. #endif
  270. static int parse_options(struct super_block *sb, char *options)
  271. {
  272. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  273. struct request_queue *q;
  274. substring_t args[MAX_OPT_ARGS];
  275. char *p, *name;
  276. int arg = 0;
  277. #ifdef CONFIG_QUOTA
  278. int ret;
  279. #endif
  280. if (!options)
  281. return 0;
  282. while ((p = strsep(&options, ",")) != NULL) {
  283. int token;
  284. if (!*p)
  285. continue;
  286. /*
  287. * Initialize args struct so we know whether arg was
  288. * found; some options take optional arguments.
  289. */
  290. args[0].to = args[0].from = NULL;
  291. token = match_token(p, f2fs_tokens, args);
  292. switch (token) {
  293. case Opt_gc_background:
  294. name = match_strdup(&args[0]);
  295. if (!name)
  296. return -ENOMEM;
  297. if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
  298. set_opt(sbi, BG_GC);
  299. clear_opt(sbi, FORCE_FG_GC);
  300. } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
  301. clear_opt(sbi, BG_GC);
  302. clear_opt(sbi, FORCE_FG_GC);
  303. } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
  304. set_opt(sbi, BG_GC);
  305. set_opt(sbi, FORCE_FG_GC);
  306. } else {
  307. kfree(name);
  308. return -EINVAL;
  309. }
  310. kfree(name);
  311. break;
  312. case Opt_disable_roll_forward:
  313. set_opt(sbi, DISABLE_ROLL_FORWARD);
  314. break;
  315. case Opt_norecovery:
  316. /* this option mounts f2fs with ro */
  317. set_opt(sbi, DISABLE_ROLL_FORWARD);
  318. if (!f2fs_readonly(sb))
  319. return -EINVAL;
  320. break;
  321. case Opt_discard:
  322. q = bdev_get_queue(sb->s_bdev);
  323. if (blk_queue_discard(q)) {
  324. set_opt(sbi, DISCARD);
  325. } else if (!f2fs_sb_mounted_blkzoned(sb)) {
  326. f2fs_msg(sb, KERN_WARNING,
  327. "mounting with \"discard\" option, but "
  328. "the device does not support discard");
  329. }
  330. break;
  331. case Opt_nodiscard:
  332. if (f2fs_sb_mounted_blkzoned(sb)) {
  333. f2fs_msg(sb, KERN_WARNING,
  334. "discard is required for zoned block devices");
  335. return -EINVAL;
  336. }
  337. clear_opt(sbi, DISCARD);
  338. break;
  339. case Opt_noheap:
  340. set_opt(sbi, NOHEAP);
  341. break;
  342. case Opt_heap:
  343. clear_opt(sbi, NOHEAP);
  344. break;
  345. #ifdef CONFIG_F2FS_FS_XATTR
  346. case Opt_user_xattr:
  347. set_opt(sbi, XATTR_USER);
  348. break;
  349. case Opt_nouser_xattr:
  350. clear_opt(sbi, XATTR_USER);
  351. break;
  352. case Opt_inline_xattr:
  353. set_opt(sbi, INLINE_XATTR);
  354. break;
  355. case Opt_noinline_xattr:
  356. clear_opt(sbi, INLINE_XATTR);
  357. break;
  358. #else
  359. case Opt_user_xattr:
  360. f2fs_msg(sb, KERN_INFO,
  361. "user_xattr options not supported");
  362. break;
  363. case Opt_nouser_xattr:
  364. f2fs_msg(sb, KERN_INFO,
  365. "nouser_xattr options not supported");
  366. break;
  367. case Opt_inline_xattr:
  368. f2fs_msg(sb, KERN_INFO,
  369. "inline_xattr options not supported");
  370. break;
  371. case Opt_noinline_xattr:
  372. f2fs_msg(sb, KERN_INFO,
  373. "noinline_xattr options not supported");
  374. break;
  375. #endif
  376. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  377. case Opt_acl:
  378. set_opt(sbi, POSIX_ACL);
  379. break;
  380. case Opt_noacl:
  381. clear_opt(sbi, POSIX_ACL);
  382. break;
  383. #else
  384. case Opt_acl:
  385. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  386. break;
  387. case Opt_noacl:
  388. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  389. break;
  390. #endif
  391. case Opt_active_logs:
  392. if (args->from && match_int(args, &arg))
  393. return -EINVAL;
  394. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  395. return -EINVAL;
  396. sbi->active_logs = arg;
  397. break;
  398. case Opt_disable_ext_identify:
  399. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  400. break;
  401. case Opt_inline_data:
  402. set_opt(sbi, INLINE_DATA);
  403. break;
  404. case Opt_inline_dentry:
  405. set_opt(sbi, INLINE_DENTRY);
  406. break;
  407. case Opt_noinline_dentry:
  408. clear_opt(sbi, INLINE_DENTRY);
  409. break;
  410. case Opt_flush_merge:
  411. set_opt(sbi, FLUSH_MERGE);
  412. break;
  413. case Opt_noflush_merge:
  414. clear_opt(sbi, FLUSH_MERGE);
  415. break;
  416. case Opt_nobarrier:
  417. set_opt(sbi, NOBARRIER);
  418. break;
  419. case Opt_fastboot:
  420. set_opt(sbi, FASTBOOT);
  421. break;
  422. case Opt_extent_cache:
  423. set_opt(sbi, EXTENT_CACHE);
  424. break;
  425. case Opt_noextent_cache:
  426. clear_opt(sbi, EXTENT_CACHE);
  427. break;
  428. case Opt_noinline_data:
  429. clear_opt(sbi, INLINE_DATA);
  430. break;
  431. case Opt_data_flush:
  432. set_opt(sbi, DATA_FLUSH);
  433. break;
  434. case Opt_mode:
  435. name = match_strdup(&args[0]);
  436. if (!name)
  437. return -ENOMEM;
  438. if (strlen(name) == 8 &&
  439. !strncmp(name, "adaptive", 8)) {
  440. if (f2fs_sb_mounted_blkzoned(sb)) {
  441. f2fs_msg(sb, KERN_WARNING,
  442. "adaptive mode is not allowed with "
  443. "zoned block device feature");
  444. kfree(name);
  445. return -EINVAL;
  446. }
  447. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  448. } else if (strlen(name) == 3 &&
  449. !strncmp(name, "lfs", 3)) {
  450. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  451. } else {
  452. kfree(name);
  453. return -EINVAL;
  454. }
  455. kfree(name);
  456. break;
  457. case Opt_io_size_bits:
  458. if (args->from && match_int(args, &arg))
  459. return -EINVAL;
  460. if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
  461. f2fs_msg(sb, KERN_WARNING,
  462. "Not support %d, larger than %d",
  463. 1 << arg, BIO_MAX_PAGES);
  464. return -EINVAL;
  465. }
  466. sbi->write_io_size_bits = arg;
  467. break;
  468. case Opt_fault_injection:
  469. if (args->from && match_int(args, &arg))
  470. return -EINVAL;
  471. #ifdef CONFIG_F2FS_FAULT_INJECTION
  472. f2fs_build_fault_attr(sbi, arg);
  473. set_opt(sbi, FAULT_INJECTION);
  474. #else
  475. f2fs_msg(sb, KERN_INFO,
  476. "FAULT_INJECTION was not selected");
  477. #endif
  478. break;
  479. case Opt_lazytime:
  480. sb->s_flags |= MS_LAZYTIME;
  481. break;
  482. case Opt_nolazytime:
  483. sb->s_flags &= ~MS_LAZYTIME;
  484. break;
  485. #ifdef CONFIG_QUOTA
  486. case Opt_quota:
  487. case Opt_usrquota:
  488. set_opt(sbi, USRQUOTA);
  489. break;
  490. case Opt_grpquota:
  491. set_opt(sbi, GRPQUOTA);
  492. break;
  493. case Opt_prjquota:
  494. set_opt(sbi, PRJQUOTA);
  495. break;
  496. case Opt_usrjquota:
  497. ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
  498. if (ret)
  499. return ret;
  500. break;
  501. case Opt_grpjquota:
  502. ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
  503. if (ret)
  504. return ret;
  505. break;
  506. case Opt_prjjquota:
  507. ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
  508. if (ret)
  509. return ret;
  510. break;
  511. case Opt_offusrjquota:
  512. ret = f2fs_clear_qf_name(sb, USRQUOTA);
  513. if (ret)
  514. return ret;
  515. break;
  516. case Opt_offgrpjquota:
  517. ret = f2fs_clear_qf_name(sb, GRPQUOTA);
  518. if (ret)
  519. return ret;
  520. break;
  521. case Opt_offprjjquota:
  522. ret = f2fs_clear_qf_name(sb, PRJQUOTA);
  523. if (ret)
  524. return ret;
  525. break;
  526. case Opt_jqfmt_vfsold:
  527. sbi->s_jquota_fmt = QFMT_VFS_OLD;
  528. break;
  529. case Opt_jqfmt_vfsv0:
  530. sbi->s_jquota_fmt = QFMT_VFS_V0;
  531. break;
  532. case Opt_jqfmt_vfsv1:
  533. sbi->s_jquota_fmt = QFMT_VFS_V1;
  534. break;
  535. case Opt_noquota:
  536. clear_opt(sbi, QUOTA);
  537. clear_opt(sbi, USRQUOTA);
  538. clear_opt(sbi, GRPQUOTA);
  539. clear_opt(sbi, PRJQUOTA);
  540. break;
  541. #else
  542. case Opt_quota:
  543. case Opt_usrquota:
  544. case Opt_grpquota:
  545. case Opt_prjquota:
  546. case Opt_usrjquota:
  547. case Opt_grpjquota:
  548. case Opt_prjjquota:
  549. case Opt_offusrjquota:
  550. case Opt_offgrpjquota:
  551. case Opt_offprjjquota:
  552. case Opt_jqfmt_vfsold:
  553. case Opt_jqfmt_vfsv0:
  554. case Opt_jqfmt_vfsv1:
  555. case Opt_noquota:
  556. f2fs_msg(sb, KERN_INFO,
  557. "quota operations not supported");
  558. break;
  559. #endif
  560. default:
  561. f2fs_msg(sb, KERN_ERR,
  562. "Unrecognized mount option \"%s\" or missing value",
  563. p);
  564. return -EINVAL;
  565. }
  566. }
  567. #ifdef CONFIG_QUOTA
  568. if (f2fs_check_quota_options(sbi))
  569. return -EINVAL;
  570. #endif
  571. if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
  572. f2fs_msg(sb, KERN_ERR,
  573. "Should set mode=lfs with %uKB-sized IO",
  574. F2FS_IO_SIZE_KB(sbi));
  575. return -EINVAL;
  576. }
  577. return 0;
  578. }
  579. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  580. {
  581. struct f2fs_inode_info *fi;
  582. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  583. if (!fi)
  584. return NULL;
  585. init_once((void *) fi);
  586. /* Initialize f2fs-specific inode info */
  587. fi->vfs_inode.i_version = 1;
  588. atomic_set(&fi->dirty_pages, 0);
  589. fi->i_current_depth = 1;
  590. fi->i_advise = 0;
  591. init_rwsem(&fi->i_sem);
  592. INIT_LIST_HEAD(&fi->dirty_list);
  593. INIT_LIST_HEAD(&fi->gdirty_list);
  594. INIT_LIST_HEAD(&fi->inmem_pages);
  595. mutex_init(&fi->inmem_lock);
  596. init_rwsem(&fi->dio_rwsem[READ]);
  597. init_rwsem(&fi->dio_rwsem[WRITE]);
  598. init_rwsem(&fi->i_mmap_sem);
  599. init_rwsem(&fi->i_xattr_sem);
  600. #ifdef CONFIG_QUOTA
  601. memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
  602. fi->i_reserved_quota = 0;
  603. #endif
  604. /* Will be used by directory only */
  605. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  606. return &fi->vfs_inode;
  607. }
  608. static int f2fs_drop_inode(struct inode *inode)
  609. {
  610. int ret;
  611. /*
  612. * This is to avoid a deadlock condition like below.
  613. * writeback_single_inode(inode)
  614. * - f2fs_write_data_page
  615. * - f2fs_gc -> iput -> evict
  616. * - inode_wait_for_writeback(inode)
  617. */
  618. if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
  619. if (!inode->i_nlink && !is_bad_inode(inode)) {
  620. /* to avoid evict_inode call simultaneously */
  621. atomic_inc(&inode->i_count);
  622. spin_unlock(&inode->i_lock);
  623. /* some remained atomic pages should discarded */
  624. if (f2fs_is_atomic_file(inode))
  625. drop_inmem_pages(inode);
  626. /* should remain fi->extent_tree for writepage */
  627. f2fs_destroy_extent_node(inode);
  628. sb_start_intwrite(inode->i_sb);
  629. f2fs_i_size_write(inode, 0);
  630. if (F2FS_HAS_BLOCKS(inode))
  631. f2fs_truncate(inode);
  632. sb_end_intwrite(inode->i_sb);
  633. fscrypt_put_encryption_info(inode, NULL);
  634. spin_lock(&inode->i_lock);
  635. atomic_dec(&inode->i_count);
  636. }
  637. trace_f2fs_drop_inode(inode, 0);
  638. return 0;
  639. }
  640. ret = generic_drop_inode(inode);
  641. trace_f2fs_drop_inode(inode, ret);
  642. return ret;
  643. }
  644. int f2fs_inode_dirtied(struct inode *inode, bool sync)
  645. {
  646. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  647. int ret = 0;
  648. spin_lock(&sbi->inode_lock[DIRTY_META]);
  649. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  650. ret = 1;
  651. } else {
  652. set_inode_flag(inode, FI_DIRTY_INODE);
  653. stat_inc_dirty_inode(sbi, DIRTY_META);
  654. }
  655. if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
  656. list_add_tail(&F2FS_I(inode)->gdirty_list,
  657. &sbi->inode_list[DIRTY_META]);
  658. inc_page_count(sbi, F2FS_DIRTY_IMETA);
  659. }
  660. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  661. return ret;
  662. }
  663. void f2fs_inode_synced(struct inode *inode)
  664. {
  665. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  666. spin_lock(&sbi->inode_lock[DIRTY_META]);
  667. if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  668. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  669. return;
  670. }
  671. if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
  672. list_del_init(&F2FS_I(inode)->gdirty_list);
  673. dec_page_count(sbi, F2FS_DIRTY_IMETA);
  674. }
  675. clear_inode_flag(inode, FI_DIRTY_INODE);
  676. clear_inode_flag(inode, FI_AUTO_RECOVER);
  677. stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
  678. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  679. }
  680. /*
  681. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  682. *
  683. * We should call set_dirty_inode to write the dirty inode through write_inode.
  684. */
  685. static void f2fs_dirty_inode(struct inode *inode, int flags)
  686. {
  687. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  688. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  689. inode->i_ino == F2FS_META_INO(sbi))
  690. return;
  691. if (flags == I_DIRTY_TIME)
  692. return;
  693. if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
  694. clear_inode_flag(inode, FI_AUTO_RECOVER);
  695. f2fs_inode_dirtied(inode, false);
  696. }
  697. static void f2fs_i_callback(struct rcu_head *head)
  698. {
  699. struct inode *inode = container_of(head, struct inode, i_rcu);
  700. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  701. }
  702. static void f2fs_destroy_inode(struct inode *inode)
  703. {
  704. call_rcu(&inode->i_rcu, f2fs_i_callback);
  705. }
  706. static void destroy_percpu_info(struct f2fs_sb_info *sbi)
  707. {
  708. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  709. percpu_counter_destroy(&sbi->total_valid_inode_count);
  710. }
  711. static void destroy_device_list(struct f2fs_sb_info *sbi)
  712. {
  713. int i;
  714. for (i = 0; i < sbi->s_ndevs; i++) {
  715. blkdev_put(FDEV(i).bdev, FMODE_EXCL);
  716. #ifdef CONFIG_BLK_DEV_ZONED
  717. kfree(FDEV(i).blkz_type);
  718. #endif
  719. }
  720. kfree(sbi->devs);
  721. }
  722. static void f2fs_put_super(struct super_block *sb)
  723. {
  724. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  725. int i;
  726. f2fs_quota_off_umount(sb);
  727. /* prevent remaining shrinker jobs */
  728. mutex_lock(&sbi->umount_mutex);
  729. /*
  730. * We don't need to do checkpoint when superblock is clean.
  731. * But, the previous checkpoint was not done by umount, it needs to do
  732. * clean checkpoint again.
  733. */
  734. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  735. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  736. struct cp_control cpc = {
  737. .reason = CP_UMOUNT,
  738. };
  739. write_checkpoint(sbi, &cpc);
  740. }
  741. /* be sure to wait for any on-going discard commands */
  742. f2fs_wait_discard_bios(sbi);
  743. if (f2fs_discard_en(sbi) && !sbi->discard_blks) {
  744. struct cp_control cpc = {
  745. .reason = CP_UMOUNT | CP_TRIMMED,
  746. };
  747. write_checkpoint(sbi, &cpc);
  748. }
  749. /* write_checkpoint can update stat informaion */
  750. f2fs_destroy_stats(sbi);
  751. /*
  752. * normally superblock is clean, so we need to release this.
  753. * In addition, EIO will skip do checkpoint, we need this as well.
  754. */
  755. release_ino_entry(sbi, true);
  756. f2fs_leave_shrinker(sbi);
  757. mutex_unlock(&sbi->umount_mutex);
  758. /* our cp_error case, we can wait for any writeback page */
  759. f2fs_flush_merged_writes(sbi);
  760. iput(sbi->node_inode);
  761. iput(sbi->meta_inode);
  762. /* destroy f2fs internal modules */
  763. destroy_node_manager(sbi);
  764. destroy_segment_manager(sbi);
  765. kfree(sbi->ckpt);
  766. f2fs_unregister_sysfs(sbi);
  767. sb->s_fs_info = NULL;
  768. if (sbi->s_chksum_driver)
  769. crypto_free_shash(sbi->s_chksum_driver);
  770. kfree(sbi->raw_super);
  771. destroy_device_list(sbi);
  772. mempool_destroy(sbi->write_io_dummy);
  773. #ifdef CONFIG_QUOTA
  774. for (i = 0; i < MAXQUOTAS; i++)
  775. kfree(sbi->s_qf_names[i]);
  776. #endif
  777. destroy_percpu_info(sbi);
  778. for (i = 0; i < NR_PAGE_TYPE; i++)
  779. kfree(sbi->write_io[i]);
  780. kfree(sbi);
  781. }
  782. int f2fs_sync_fs(struct super_block *sb, int sync)
  783. {
  784. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  785. int err = 0;
  786. trace_f2fs_sync_fs(sb, sync);
  787. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  788. return -EAGAIN;
  789. if (sync) {
  790. struct cp_control cpc;
  791. cpc.reason = __get_cp_reason(sbi);
  792. mutex_lock(&sbi->gc_mutex);
  793. err = write_checkpoint(sbi, &cpc);
  794. mutex_unlock(&sbi->gc_mutex);
  795. }
  796. f2fs_trace_ios(NULL, 1);
  797. return err;
  798. }
  799. static int f2fs_freeze(struct super_block *sb)
  800. {
  801. if (f2fs_readonly(sb))
  802. return 0;
  803. /* IO error happened before */
  804. if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
  805. return -EIO;
  806. /* must be clean, since sync_filesystem() was already called */
  807. if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
  808. return -EINVAL;
  809. return 0;
  810. }
  811. static int f2fs_unfreeze(struct super_block *sb)
  812. {
  813. return 0;
  814. }
  815. #ifdef CONFIG_QUOTA
  816. static int f2fs_statfs_project(struct super_block *sb,
  817. kprojid_t projid, struct kstatfs *buf)
  818. {
  819. struct kqid qid;
  820. struct dquot *dquot;
  821. u64 limit;
  822. u64 curblock;
  823. qid = make_kqid_projid(projid);
  824. dquot = dqget(sb, qid);
  825. if (IS_ERR(dquot))
  826. return PTR_ERR(dquot);
  827. spin_lock(&dq_data_lock);
  828. limit = (dquot->dq_dqb.dqb_bsoftlimit ?
  829. dquot->dq_dqb.dqb_bsoftlimit :
  830. dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
  831. if (limit && buf->f_blocks > limit) {
  832. curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
  833. buf->f_blocks = limit;
  834. buf->f_bfree = buf->f_bavail =
  835. (buf->f_blocks > curblock) ?
  836. (buf->f_blocks - curblock) : 0;
  837. }
  838. limit = dquot->dq_dqb.dqb_isoftlimit ?
  839. dquot->dq_dqb.dqb_isoftlimit :
  840. dquot->dq_dqb.dqb_ihardlimit;
  841. if (limit && buf->f_files > limit) {
  842. buf->f_files = limit;
  843. buf->f_ffree =
  844. (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
  845. (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
  846. }
  847. spin_unlock(&dq_data_lock);
  848. dqput(dquot);
  849. return 0;
  850. }
  851. #endif
  852. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  853. {
  854. struct super_block *sb = dentry->d_sb;
  855. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  856. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  857. block_t total_count, user_block_count, start_count, ovp_count;
  858. u64 avail_node_count;
  859. total_count = le64_to_cpu(sbi->raw_super->block_count);
  860. user_block_count = sbi->user_block_count;
  861. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  862. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  863. buf->f_type = F2FS_SUPER_MAGIC;
  864. buf->f_bsize = sbi->blocksize;
  865. buf->f_blocks = total_count - start_count;
  866. buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
  867. buf->f_bavail = user_block_count - valid_user_blocks(sbi) -
  868. sbi->reserved_blocks;
  869. avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  870. if (avail_node_count > user_block_count) {
  871. buf->f_files = user_block_count;
  872. buf->f_ffree = buf->f_bavail;
  873. } else {
  874. buf->f_files = avail_node_count;
  875. buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
  876. buf->f_bavail);
  877. }
  878. buf->f_namelen = F2FS_NAME_LEN;
  879. buf->f_fsid.val[0] = (u32)id;
  880. buf->f_fsid.val[1] = (u32)(id >> 32);
  881. #ifdef CONFIG_QUOTA
  882. if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
  883. sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
  884. f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
  885. }
  886. #endif
  887. return 0;
  888. }
  889. static inline void f2fs_show_quota_options(struct seq_file *seq,
  890. struct super_block *sb)
  891. {
  892. #ifdef CONFIG_QUOTA
  893. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  894. if (sbi->s_jquota_fmt) {
  895. char *fmtname = "";
  896. switch (sbi->s_jquota_fmt) {
  897. case QFMT_VFS_OLD:
  898. fmtname = "vfsold";
  899. break;
  900. case QFMT_VFS_V0:
  901. fmtname = "vfsv0";
  902. break;
  903. case QFMT_VFS_V1:
  904. fmtname = "vfsv1";
  905. break;
  906. }
  907. seq_printf(seq, ",jqfmt=%s", fmtname);
  908. }
  909. if (sbi->s_qf_names[USRQUOTA])
  910. seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
  911. if (sbi->s_qf_names[GRPQUOTA])
  912. seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
  913. if (sbi->s_qf_names[PRJQUOTA])
  914. seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]);
  915. #endif
  916. }
  917. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  918. {
  919. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  920. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
  921. if (test_opt(sbi, FORCE_FG_GC))
  922. seq_printf(seq, ",background_gc=%s", "sync");
  923. else
  924. seq_printf(seq, ",background_gc=%s", "on");
  925. } else {
  926. seq_printf(seq, ",background_gc=%s", "off");
  927. }
  928. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  929. seq_puts(seq, ",disable_roll_forward");
  930. if (test_opt(sbi, DISCARD))
  931. seq_puts(seq, ",discard");
  932. if (test_opt(sbi, NOHEAP))
  933. seq_puts(seq, ",no_heap");
  934. else
  935. seq_puts(seq, ",heap");
  936. #ifdef CONFIG_F2FS_FS_XATTR
  937. if (test_opt(sbi, XATTR_USER))
  938. seq_puts(seq, ",user_xattr");
  939. else
  940. seq_puts(seq, ",nouser_xattr");
  941. if (test_opt(sbi, INLINE_XATTR))
  942. seq_puts(seq, ",inline_xattr");
  943. else
  944. seq_puts(seq, ",noinline_xattr");
  945. #endif
  946. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  947. if (test_opt(sbi, POSIX_ACL))
  948. seq_puts(seq, ",acl");
  949. else
  950. seq_puts(seq, ",noacl");
  951. #endif
  952. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  953. seq_puts(seq, ",disable_ext_identify");
  954. if (test_opt(sbi, INLINE_DATA))
  955. seq_puts(seq, ",inline_data");
  956. else
  957. seq_puts(seq, ",noinline_data");
  958. if (test_opt(sbi, INLINE_DENTRY))
  959. seq_puts(seq, ",inline_dentry");
  960. else
  961. seq_puts(seq, ",noinline_dentry");
  962. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  963. seq_puts(seq, ",flush_merge");
  964. if (test_opt(sbi, NOBARRIER))
  965. seq_puts(seq, ",nobarrier");
  966. if (test_opt(sbi, FASTBOOT))
  967. seq_puts(seq, ",fastboot");
  968. if (test_opt(sbi, EXTENT_CACHE))
  969. seq_puts(seq, ",extent_cache");
  970. else
  971. seq_puts(seq, ",noextent_cache");
  972. if (test_opt(sbi, DATA_FLUSH))
  973. seq_puts(seq, ",data_flush");
  974. seq_puts(seq, ",mode=");
  975. if (test_opt(sbi, ADAPTIVE))
  976. seq_puts(seq, "adaptive");
  977. else if (test_opt(sbi, LFS))
  978. seq_puts(seq, "lfs");
  979. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  980. if (F2FS_IO_SIZE_BITS(sbi))
  981. seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
  982. #ifdef CONFIG_F2FS_FAULT_INJECTION
  983. if (test_opt(sbi, FAULT_INJECTION))
  984. seq_printf(seq, ",fault_injection=%u",
  985. sbi->fault_info.inject_rate);
  986. #endif
  987. #ifdef CONFIG_QUOTA
  988. if (test_opt(sbi, QUOTA))
  989. seq_puts(seq, ",quota");
  990. if (test_opt(sbi, USRQUOTA))
  991. seq_puts(seq, ",usrquota");
  992. if (test_opt(sbi, GRPQUOTA))
  993. seq_puts(seq, ",grpquota");
  994. if (test_opt(sbi, PRJQUOTA))
  995. seq_puts(seq, ",prjquota");
  996. #endif
  997. f2fs_show_quota_options(seq, sbi->sb);
  998. return 0;
  999. }
  1000. static void default_options(struct f2fs_sb_info *sbi)
  1001. {
  1002. /* init some FS parameters */
  1003. sbi->active_logs = NR_CURSEG_TYPE;
  1004. set_opt(sbi, BG_GC);
  1005. set_opt(sbi, INLINE_XATTR);
  1006. set_opt(sbi, INLINE_DATA);
  1007. set_opt(sbi, INLINE_DENTRY);
  1008. set_opt(sbi, EXTENT_CACHE);
  1009. set_opt(sbi, NOHEAP);
  1010. sbi->sb->s_flags |= MS_LAZYTIME;
  1011. set_opt(sbi, FLUSH_MERGE);
  1012. if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
  1013. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  1014. set_opt(sbi, DISCARD);
  1015. } else {
  1016. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  1017. }
  1018. #ifdef CONFIG_F2FS_FS_XATTR
  1019. set_opt(sbi, XATTR_USER);
  1020. #endif
  1021. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  1022. set_opt(sbi, POSIX_ACL);
  1023. #endif
  1024. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1025. f2fs_build_fault_attr(sbi, 0);
  1026. #endif
  1027. }
  1028. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  1029. {
  1030. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1031. struct f2fs_mount_info org_mount_opt;
  1032. unsigned long old_sb_flags;
  1033. int err, active_logs;
  1034. bool need_restart_gc = false;
  1035. bool need_stop_gc = false;
  1036. bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
  1037. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1038. struct f2fs_fault_info ffi = sbi->fault_info;
  1039. #endif
  1040. #ifdef CONFIG_QUOTA
  1041. int s_jquota_fmt;
  1042. char *s_qf_names[MAXQUOTAS];
  1043. int i, j;
  1044. #endif
  1045. /*
  1046. * Save the old mount options in case we
  1047. * need to restore them.
  1048. */
  1049. org_mount_opt = sbi->mount_opt;
  1050. old_sb_flags = sb->s_flags;
  1051. active_logs = sbi->active_logs;
  1052. #ifdef CONFIG_QUOTA
  1053. s_jquota_fmt = sbi->s_jquota_fmt;
  1054. for (i = 0; i < MAXQUOTAS; i++) {
  1055. if (sbi->s_qf_names[i]) {
  1056. s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
  1057. GFP_KERNEL);
  1058. if (!s_qf_names[i]) {
  1059. for (j = 0; j < i; j++)
  1060. kfree(s_qf_names[j]);
  1061. return -ENOMEM;
  1062. }
  1063. } else {
  1064. s_qf_names[i] = NULL;
  1065. }
  1066. }
  1067. #endif
  1068. /* recover superblocks we couldn't write due to previous RO mount */
  1069. if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  1070. err = f2fs_commit_super(sbi, false);
  1071. f2fs_msg(sb, KERN_INFO,
  1072. "Try to recover all the superblocks, ret: %d", err);
  1073. if (!err)
  1074. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1075. }
  1076. default_options(sbi);
  1077. /* parse mount options */
  1078. err = parse_options(sb, data);
  1079. if (err)
  1080. goto restore_opts;
  1081. /*
  1082. * Previous and new state of filesystem is RO,
  1083. * so skip checking GC and FLUSH_MERGE conditions.
  1084. */
  1085. if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
  1086. goto skip;
  1087. if (!f2fs_readonly(sb) && (*flags & MS_RDONLY)) {
  1088. err = dquot_suspend(sb, -1);
  1089. if (err < 0)
  1090. goto restore_opts;
  1091. } else {
  1092. /* dquot_resume needs RW */
  1093. sb->s_flags &= ~MS_RDONLY;
  1094. dquot_resume(sb, -1);
  1095. }
  1096. /* disallow enable/disable extent_cache dynamically */
  1097. if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
  1098. err = -EINVAL;
  1099. f2fs_msg(sbi->sb, KERN_WARNING,
  1100. "switch extent_cache option is not allowed");
  1101. goto restore_opts;
  1102. }
  1103. /*
  1104. * We stop the GC thread if FS is mounted as RO
  1105. * or if background_gc = off is passed in mount
  1106. * option. Also sync the filesystem.
  1107. */
  1108. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  1109. if (sbi->gc_thread) {
  1110. stop_gc_thread(sbi);
  1111. need_restart_gc = true;
  1112. }
  1113. } else if (!sbi->gc_thread) {
  1114. err = start_gc_thread(sbi);
  1115. if (err)
  1116. goto restore_opts;
  1117. need_stop_gc = true;
  1118. }
  1119. if (*flags & MS_RDONLY) {
  1120. writeback_inodes_sb(sb, WB_REASON_SYNC);
  1121. sync_inodes_sb(sb);
  1122. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1123. set_sbi_flag(sbi, SBI_IS_CLOSE);
  1124. f2fs_sync_fs(sb, 1);
  1125. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  1126. }
  1127. /*
  1128. * We stop issue flush thread if FS is mounted as RO
  1129. * or if flush_merge is not passed in mount option.
  1130. */
  1131. if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  1132. clear_opt(sbi, FLUSH_MERGE);
  1133. destroy_flush_cmd_control(sbi, false);
  1134. } else {
  1135. err = create_flush_cmd_control(sbi);
  1136. if (err)
  1137. goto restore_gc;
  1138. }
  1139. skip:
  1140. #ifdef CONFIG_QUOTA
  1141. /* Release old quota file names */
  1142. for (i = 0; i < MAXQUOTAS; i++)
  1143. kfree(s_qf_names[i]);
  1144. #endif
  1145. /* Update the POSIXACL Flag */
  1146. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1147. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  1148. return 0;
  1149. restore_gc:
  1150. if (need_restart_gc) {
  1151. if (start_gc_thread(sbi))
  1152. f2fs_msg(sbi->sb, KERN_WARNING,
  1153. "background gc thread has stopped");
  1154. } else if (need_stop_gc) {
  1155. stop_gc_thread(sbi);
  1156. }
  1157. restore_opts:
  1158. #ifdef CONFIG_QUOTA
  1159. sbi->s_jquota_fmt = s_jquota_fmt;
  1160. for (i = 0; i < MAXQUOTAS; i++) {
  1161. kfree(sbi->s_qf_names[i]);
  1162. sbi->s_qf_names[i] = s_qf_names[i];
  1163. }
  1164. #endif
  1165. sbi->mount_opt = org_mount_opt;
  1166. sbi->active_logs = active_logs;
  1167. sb->s_flags = old_sb_flags;
  1168. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1169. sbi->fault_info = ffi;
  1170. #endif
  1171. return err;
  1172. }
  1173. #ifdef CONFIG_QUOTA
  1174. /* Read data from quotafile */
  1175. static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
  1176. size_t len, loff_t off)
  1177. {
  1178. struct inode *inode = sb_dqopt(sb)->files[type];
  1179. struct address_space *mapping = inode->i_mapping;
  1180. block_t blkidx = F2FS_BYTES_TO_BLK(off);
  1181. int offset = off & (sb->s_blocksize - 1);
  1182. int tocopy;
  1183. size_t toread;
  1184. loff_t i_size = i_size_read(inode);
  1185. struct page *page;
  1186. char *kaddr;
  1187. if (off > i_size)
  1188. return 0;
  1189. if (off + len > i_size)
  1190. len = i_size - off;
  1191. toread = len;
  1192. while (toread > 0) {
  1193. tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
  1194. repeat:
  1195. page = read_mapping_page(mapping, blkidx, NULL);
  1196. if (IS_ERR(page))
  1197. return PTR_ERR(page);
  1198. lock_page(page);
  1199. if (unlikely(page->mapping != mapping)) {
  1200. f2fs_put_page(page, 1);
  1201. goto repeat;
  1202. }
  1203. if (unlikely(!PageUptodate(page))) {
  1204. f2fs_put_page(page, 1);
  1205. return -EIO;
  1206. }
  1207. kaddr = kmap_atomic(page);
  1208. memcpy(data, kaddr + offset, tocopy);
  1209. kunmap_atomic(kaddr);
  1210. f2fs_put_page(page, 1);
  1211. offset = 0;
  1212. toread -= tocopy;
  1213. data += tocopy;
  1214. blkidx++;
  1215. }
  1216. return len;
  1217. }
  1218. /* Write to quotafile */
  1219. static ssize_t f2fs_quota_write(struct super_block *sb, int type,
  1220. const char *data, size_t len, loff_t off)
  1221. {
  1222. struct inode *inode = sb_dqopt(sb)->files[type];
  1223. struct address_space *mapping = inode->i_mapping;
  1224. const struct address_space_operations *a_ops = mapping->a_ops;
  1225. int offset = off & (sb->s_blocksize - 1);
  1226. size_t towrite = len;
  1227. struct page *page;
  1228. char *kaddr;
  1229. int err = 0;
  1230. int tocopy;
  1231. while (towrite > 0) {
  1232. tocopy = min_t(unsigned long, sb->s_blocksize - offset,
  1233. towrite);
  1234. err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
  1235. &page, NULL);
  1236. if (unlikely(err))
  1237. break;
  1238. kaddr = kmap_atomic(page);
  1239. memcpy(kaddr + offset, data, tocopy);
  1240. kunmap_atomic(kaddr);
  1241. flush_dcache_page(page);
  1242. a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
  1243. page, NULL);
  1244. offset = 0;
  1245. towrite -= tocopy;
  1246. off += tocopy;
  1247. data += tocopy;
  1248. cond_resched();
  1249. }
  1250. if (len == towrite)
  1251. return 0;
  1252. inode->i_version++;
  1253. inode->i_mtime = inode->i_ctime = current_time(inode);
  1254. f2fs_mark_inode_dirty_sync(inode, false);
  1255. return len - towrite;
  1256. }
  1257. static struct dquot **f2fs_get_dquots(struct inode *inode)
  1258. {
  1259. return F2FS_I(inode)->i_dquot;
  1260. }
  1261. static qsize_t *f2fs_get_reserved_space(struct inode *inode)
  1262. {
  1263. return &F2FS_I(inode)->i_reserved_quota;
  1264. }
  1265. static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
  1266. {
  1267. return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type],
  1268. sbi->s_jquota_fmt, type);
  1269. }
  1270. void f2fs_enable_quota_files(struct f2fs_sb_info *sbi)
  1271. {
  1272. int i, ret;
  1273. for (i = 0; i < MAXQUOTAS; i++) {
  1274. if (sbi->s_qf_names[i]) {
  1275. ret = f2fs_quota_on_mount(sbi, i);
  1276. if (ret < 0)
  1277. f2fs_msg(sbi->sb, KERN_ERR,
  1278. "Cannot turn on journaled "
  1279. "quota: error %d", ret);
  1280. }
  1281. }
  1282. }
  1283. static int f2fs_quota_sync(struct super_block *sb, int type)
  1284. {
  1285. struct quota_info *dqopt = sb_dqopt(sb);
  1286. int cnt;
  1287. int ret;
  1288. ret = dquot_writeback_dquots(sb, type);
  1289. if (ret)
  1290. return ret;
  1291. /*
  1292. * Now when everything is written we can discard the pagecache so
  1293. * that userspace sees the changes.
  1294. */
  1295. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1296. if (type != -1 && cnt != type)
  1297. continue;
  1298. if (!sb_has_quota_active(sb, cnt))
  1299. continue;
  1300. ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
  1301. if (ret)
  1302. return ret;
  1303. inode_lock(dqopt->files[cnt]);
  1304. truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
  1305. inode_unlock(dqopt->files[cnt]);
  1306. }
  1307. return 0;
  1308. }
  1309. static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
  1310. const struct path *path)
  1311. {
  1312. struct inode *inode;
  1313. int err;
  1314. err = f2fs_quota_sync(sb, type);
  1315. if (err)
  1316. return err;
  1317. err = dquot_quota_on(sb, type, format_id, path);
  1318. if (err)
  1319. return err;
  1320. inode = d_inode(path->dentry);
  1321. inode_lock(inode);
  1322. F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
  1323. inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
  1324. S_NOATIME | S_IMMUTABLE);
  1325. inode_unlock(inode);
  1326. f2fs_mark_inode_dirty_sync(inode, false);
  1327. return 0;
  1328. }
  1329. static int f2fs_quota_off(struct super_block *sb, int type)
  1330. {
  1331. struct inode *inode = sb_dqopt(sb)->files[type];
  1332. int err;
  1333. if (!inode || !igrab(inode))
  1334. return dquot_quota_off(sb, type);
  1335. f2fs_quota_sync(sb, type);
  1336. err = dquot_quota_off(sb, type);
  1337. if (err)
  1338. goto out_put;
  1339. inode_lock(inode);
  1340. F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
  1341. inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
  1342. inode_unlock(inode);
  1343. f2fs_mark_inode_dirty_sync(inode, false);
  1344. out_put:
  1345. iput(inode);
  1346. return err;
  1347. }
  1348. void f2fs_quota_off_umount(struct super_block *sb)
  1349. {
  1350. int type;
  1351. for (type = 0; type < MAXQUOTAS; type++)
  1352. f2fs_quota_off(sb, type);
  1353. }
  1354. int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
  1355. {
  1356. *projid = F2FS_I(inode)->i_projid;
  1357. return 0;
  1358. }
  1359. static const struct dquot_operations f2fs_quota_operations = {
  1360. .get_reserved_space = f2fs_get_reserved_space,
  1361. .write_dquot = dquot_commit,
  1362. .acquire_dquot = dquot_acquire,
  1363. .release_dquot = dquot_release,
  1364. .mark_dirty = dquot_mark_dquot_dirty,
  1365. .write_info = dquot_commit_info,
  1366. .alloc_dquot = dquot_alloc,
  1367. .destroy_dquot = dquot_destroy,
  1368. .get_projid = f2fs_get_projid,
  1369. .get_next_id = dquot_get_next_id,
  1370. };
  1371. static const struct quotactl_ops f2fs_quotactl_ops = {
  1372. .quota_on = f2fs_quota_on,
  1373. .quota_off = f2fs_quota_off,
  1374. .quota_sync = f2fs_quota_sync,
  1375. .get_state = dquot_get_state,
  1376. .set_info = dquot_set_dqinfo,
  1377. .get_dqblk = dquot_get_dqblk,
  1378. .set_dqblk = dquot_set_dqblk,
  1379. .get_nextdqblk = dquot_get_next_dqblk,
  1380. };
  1381. #else
  1382. void f2fs_quota_off_umount(struct super_block *sb)
  1383. {
  1384. }
  1385. #endif
  1386. static const struct super_operations f2fs_sops = {
  1387. .alloc_inode = f2fs_alloc_inode,
  1388. .drop_inode = f2fs_drop_inode,
  1389. .destroy_inode = f2fs_destroy_inode,
  1390. .write_inode = f2fs_write_inode,
  1391. .dirty_inode = f2fs_dirty_inode,
  1392. .show_options = f2fs_show_options,
  1393. #ifdef CONFIG_QUOTA
  1394. .quota_read = f2fs_quota_read,
  1395. .quota_write = f2fs_quota_write,
  1396. .get_dquots = f2fs_get_dquots,
  1397. #endif
  1398. .evict_inode = f2fs_evict_inode,
  1399. .put_super = f2fs_put_super,
  1400. .sync_fs = f2fs_sync_fs,
  1401. .freeze_fs = f2fs_freeze,
  1402. .unfreeze_fs = f2fs_unfreeze,
  1403. .statfs = f2fs_statfs,
  1404. .remount_fs = f2fs_remount,
  1405. };
  1406. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1407. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  1408. {
  1409. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1410. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1411. ctx, len, NULL);
  1412. }
  1413. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  1414. void *fs_data)
  1415. {
  1416. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1417. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1418. ctx, len, fs_data, XATTR_CREATE);
  1419. }
  1420. static unsigned f2fs_max_namelen(struct inode *inode)
  1421. {
  1422. return S_ISLNK(inode->i_mode) ?
  1423. inode->i_sb->s_blocksize : F2FS_NAME_LEN;
  1424. }
  1425. static const struct fscrypt_operations f2fs_cryptops = {
  1426. .key_prefix = "f2fs:",
  1427. .get_context = f2fs_get_context,
  1428. .set_context = f2fs_set_context,
  1429. .is_encrypted = f2fs_encrypted_inode,
  1430. .empty_dir = f2fs_empty_dir,
  1431. .max_namelen = f2fs_max_namelen,
  1432. };
  1433. #else
  1434. static const struct fscrypt_operations f2fs_cryptops = {
  1435. .is_encrypted = f2fs_encrypted_inode,
  1436. };
  1437. #endif
  1438. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  1439. u64 ino, u32 generation)
  1440. {
  1441. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1442. struct inode *inode;
  1443. if (check_nid_range(sbi, ino))
  1444. return ERR_PTR(-ESTALE);
  1445. /*
  1446. * f2fs_iget isn't quite right if the inode is currently unallocated!
  1447. * However f2fs_iget currently does appropriate checks to handle stale
  1448. * inodes so everything is OK.
  1449. */
  1450. inode = f2fs_iget(sb, ino);
  1451. if (IS_ERR(inode))
  1452. return ERR_CAST(inode);
  1453. if (unlikely(generation && inode->i_generation != generation)) {
  1454. /* we didn't find the right inode.. */
  1455. iput(inode);
  1456. return ERR_PTR(-ESTALE);
  1457. }
  1458. return inode;
  1459. }
  1460. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1461. int fh_len, int fh_type)
  1462. {
  1463. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1464. f2fs_nfs_get_inode);
  1465. }
  1466. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1467. int fh_len, int fh_type)
  1468. {
  1469. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1470. f2fs_nfs_get_inode);
  1471. }
  1472. static const struct export_operations f2fs_export_ops = {
  1473. .fh_to_dentry = f2fs_fh_to_dentry,
  1474. .fh_to_parent = f2fs_fh_to_parent,
  1475. .get_parent = f2fs_get_parent,
  1476. };
  1477. static loff_t max_file_blocks(void)
  1478. {
  1479. loff_t result = 0;
  1480. loff_t leaf_count = ADDRS_PER_BLOCK;
  1481. /*
  1482. * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
  1483. * F2FS_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
  1484. * space in inode.i_addr, it will be more safe to reassign
  1485. * result as zero.
  1486. */
  1487. /* two direct node blocks */
  1488. result += (leaf_count * 2);
  1489. /* two indirect node blocks */
  1490. leaf_count *= NIDS_PER_BLOCK;
  1491. result += (leaf_count * 2);
  1492. /* one double indirect node block */
  1493. leaf_count *= NIDS_PER_BLOCK;
  1494. result += leaf_count;
  1495. return result;
  1496. }
  1497. static int __f2fs_commit_super(struct buffer_head *bh,
  1498. struct f2fs_super_block *super)
  1499. {
  1500. lock_buffer(bh);
  1501. if (super)
  1502. memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
  1503. set_buffer_uptodate(bh);
  1504. set_buffer_dirty(bh);
  1505. unlock_buffer(bh);
  1506. /* it's rare case, we can do fua all the time */
  1507. return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  1508. }
  1509. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  1510. struct buffer_head *bh)
  1511. {
  1512. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1513. (bh->b_data + F2FS_SUPER_OFFSET);
  1514. struct super_block *sb = sbi->sb;
  1515. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1516. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  1517. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  1518. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  1519. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1520. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1521. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  1522. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  1523. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  1524. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  1525. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  1526. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  1527. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1528. u64 main_end_blkaddr = main_blkaddr +
  1529. (segment_count_main << log_blocks_per_seg);
  1530. u64 seg_end_blkaddr = segment0_blkaddr +
  1531. (segment_count << log_blocks_per_seg);
  1532. if (segment0_blkaddr != cp_blkaddr) {
  1533. f2fs_msg(sb, KERN_INFO,
  1534. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  1535. segment0_blkaddr, cp_blkaddr);
  1536. return true;
  1537. }
  1538. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  1539. sit_blkaddr) {
  1540. f2fs_msg(sb, KERN_INFO,
  1541. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  1542. cp_blkaddr, sit_blkaddr,
  1543. segment_count_ckpt << log_blocks_per_seg);
  1544. return true;
  1545. }
  1546. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  1547. nat_blkaddr) {
  1548. f2fs_msg(sb, KERN_INFO,
  1549. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  1550. sit_blkaddr, nat_blkaddr,
  1551. segment_count_sit << log_blocks_per_seg);
  1552. return true;
  1553. }
  1554. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  1555. ssa_blkaddr) {
  1556. f2fs_msg(sb, KERN_INFO,
  1557. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  1558. nat_blkaddr, ssa_blkaddr,
  1559. segment_count_nat << log_blocks_per_seg);
  1560. return true;
  1561. }
  1562. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  1563. main_blkaddr) {
  1564. f2fs_msg(sb, KERN_INFO,
  1565. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  1566. ssa_blkaddr, main_blkaddr,
  1567. segment_count_ssa << log_blocks_per_seg);
  1568. return true;
  1569. }
  1570. if (main_end_blkaddr > seg_end_blkaddr) {
  1571. f2fs_msg(sb, KERN_INFO,
  1572. "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
  1573. main_blkaddr,
  1574. segment0_blkaddr +
  1575. (segment_count << log_blocks_per_seg),
  1576. segment_count_main << log_blocks_per_seg);
  1577. return true;
  1578. } else if (main_end_blkaddr < seg_end_blkaddr) {
  1579. int err = 0;
  1580. char *res;
  1581. /* fix in-memory information all the time */
  1582. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  1583. segment0_blkaddr) >> log_blocks_per_seg);
  1584. if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
  1585. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1586. res = "internally";
  1587. } else {
  1588. err = __f2fs_commit_super(bh, NULL);
  1589. res = err ? "failed" : "done";
  1590. }
  1591. f2fs_msg(sb, KERN_INFO,
  1592. "Fix alignment : %s, start(%u) end(%u) block(%u)",
  1593. res, main_blkaddr,
  1594. segment0_blkaddr +
  1595. (segment_count << log_blocks_per_seg),
  1596. segment_count_main << log_blocks_per_seg);
  1597. if (err)
  1598. return true;
  1599. }
  1600. return false;
  1601. }
  1602. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  1603. struct buffer_head *bh)
  1604. {
  1605. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1606. (bh->b_data + F2FS_SUPER_OFFSET);
  1607. struct super_block *sb = sbi->sb;
  1608. unsigned int blocksize;
  1609. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  1610. f2fs_msg(sb, KERN_INFO,
  1611. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  1612. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  1613. return 1;
  1614. }
  1615. /* Currently, support only 4KB page cache size */
  1616. if (F2FS_BLKSIZE != PAGE_SIZE) {
  1617. f2fs_msg(sb, KERN_INFO,
  1618. "Invalid page_cache_size (%lu), supports only 4KB\n",
  1619. PAGE_SIZE);
  1620. return 1;
  1621. }
  1622. /* Currently, support only 4KB block size */
  1623. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  1624. if (blocksize != F2FS_BLKSIZE) {
  1625. f2fs_msg(sb, KERN_INFO,
  1626. "Invalid blocksize (%u), supports only 4KB\n",
  1627. blocksize);
  1628. return 1;
  1629. }
  1630. /* check log blocks per segment */
  1631. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  1632. f2fs_msg(sb, KERN_INFO,
  1633. "Invalid log blocks per segment (%u)\n",
  1634. le32_to_cpu(raw_super->log_blocks_per_seg));
  1635. return 1;
  1636. }
  1637. /* Currently, support 512/1024/2048/4096 bytes sector size */
  1638. if (le32_to_cpu(raw_super->log_sectorsize) >
  1639. F2FS_MAX_LOG_SECTOR_SIZE ||
  1640. le32_to_cpu(raw_super->log_sectorsize) <
  1641. F2FS_MIN_LOG_SECTOR_SIZE) {
  1642. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  1643. le32_to_cpu(raw_super->log_sectorsize));
  1644. return 1;
  1645. }
  1646. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  1647. le32_to_cpu(raw_super->log_sectorsize) !=
  1648. F2FS_MAX_LOG_SECTOR_SIZE) {
  1649. f2fs_msg(sb, KERN_INFO,
  1650. "Invalid log sectors per block(%u) log sectorsize(%u)",
  1651. le32_to_cpu(raw_super->log_sectors_per_block),
  1652. le32_to_cpu(raw_super->log_sectorsize));
  1653. return 1;
  1654. }
  1655. /* check reserved ino info */
  1656. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  1657. le32_to_cpu(raw_super->meta_ino) != 2 ||
  1658. le32_to_cpu(raw_super->root_ino) != 3) {
  1659. f2fs_msg(sb, KERN_INFO,
  1660. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  1661. le32_to_cpu(raw_super->node_ino),
  1662. le32_to_cpu(raw_super->meta_ino),
  1663. le32_to_cpu(raw_super->root_ino));
  1664. return 1;
  1665. }
  1666. if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
  1667. f2fs_msg(sb, KERN_INFO,
  1668. "Invalid segment count (%u)",
  1669. le32_to_cpu(raw_super->segment_count));
  1670. return 1;
  1671. }
  1672. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  1673. if (sanity_check_area_boundary(sbi, bh))
  1674. return 1;
  1675. return 0;
  1676. }
  1677. int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  1678. {
  1679. unsigned int total, fsmeta;
  1680. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1681. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1682. unsigned int ovp_segments, reserved_segments;
  1683. unsigned int main_segs, blocks_per_seg;
  1684. int i;
  1685. total = le32_to_cpu(raw_super->segment_count);
  1686. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  1687. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  1688. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  1689. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  1690. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  1691. if (unlikely(fsmeta >= total))
  1692. return 1;
  1693. ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1694. reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1695. if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
  1696. ovp_segments == 0 || reserved_segments == 0)) {
  1697. f2fs_msg(sbi->sb, KERN_ERR,
  1698. "Wrong layout: check mkfs.f2fs version");
  1699. return 1;
  1700. }
  1701. main_segs = le32_to_cpu(raw_super->segment_count_main);
  1702. blocks_per_seg = sbi->blocks_per_seg;
  1703. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  1704. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  1705. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
  1706. return 1;
  1707. }
  1708. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  1709. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  1710. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
  1711. return 1;
  1712. }
  1713. if (unlikely(f2fs_cp_error(sbi))) {
  1714. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  1715. return 1;
  1716. }
  1717. return 0;
  1718. }
  1719. static void init_sb_info(struct f2fs_sb_info *sbi)
  1720. {
  1721. struct f2fs_super_block *raw_super = sbi->raw_super;
  1722. int i, j;
  1723. sbi->log_sectors_per_block =
  1724. le32_to_cpu(raw_super->log_sectors_per_block);
  1725. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  1726. sbi->blocksize = 1 << sbi->log_blocksize;
  1727. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1728. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  1729. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  1730. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  1731. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  1732. sbi->total_node_count =
  1733. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  1734. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  1735. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  1736. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  1737. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  1738. sbi->cur_victim_sec = NULL_SECNO;
  1739. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  1740. sbi->dir_level = DEF_DIR_LEVEL;
  1741. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  1742. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  1743. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  1744. for (i = 0; i < NR_COUNT_TYPE; i++)
  1745. atomic_set(&sbi->nr_pages[i], 0);
  1746. atomic_set(&sbi->wb_sync_req, 0);
  1747. INIT_LIST_HEAD(&sbi->s_list);
  1748. mutex_init(&sbi->umount_mutex);
  1749. for (i = 0; i < NR_PAGE_TYPE - 1; i++)
  1750. for (j = HOT; j < NR_TEMP_TYPE; j++)
  1751. mutex_init(&sbi->wio_mutex[i][j]);
  1752. spin_lock_init(&sbi->cp_lock);
  1753. }
  1754. static int init_percpu_info(struct f2fs_sb_info *sbi)
  1755. {
  1756. int err;
  1757. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
  1758. if (err)
  1759. return err;
  1760. return percpu_counter_init(&sbi->total_valid_inode_count, 0,
  1761. GFP_KERNEL);
  1762. }
  1763. #ifdef CONFIG_BLK_DEV_ZONED
  1764. static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
  1765. {
  1766. struct block_device *bdev = FDEV(devi).bdev;
  1767. sector_t nr_sectors = bdev->bd_part->nr_sects;
  1768. sector_t sector = 0;
  1769. struct blk_zone *zones;
  1770. unsigned int i, nr_zones;
  1771. unsigned int n = 0;
  1772. int err = -EIO;
  1773. if (!f2fs_sb_mounted_blkzoned(sbi->sb))
  1774. return 0;
  1775. if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
  1776. SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
  1777. return -EINVAL;
  1778. sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
  1779. if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
  1780. __ilog2_u32(sbi->blocks_per_blkz))
  1781. return -EINVAL;
  1782. sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
  1783. FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
  1784. sbi->log_blocks_per_blkz;
  1785. if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
  1786. FDEV(devi).nr_blkz++;
  1787. FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
  1788. if (!FDEV(devi).blkz_type)
  1789. return -ENOMEM;
  1790. #define F2FS_REPORT_NR_ZONES 4096
  1791. zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
  1792. GFP_KERNEL);
  1793. if (!zones)
  1794. return -ENOMEM;
  1795. /* Get block zones type */
  1796. while (zones && sector < nr_sectors) {
  1797. nr_zones = F2FS_REPORT_NR_ZONES;
  1798. err = blkdev_report_zones(bdev, sector,
  1799. zones, &nr_zones,
  1800. GFP_KERNEL);
  1801. if (err)
  1802. break;
  1803. if (!nr_zones) {
  1804. err = -EIO;
  1805. break;
  1806. }
  1807. for (i = 0; i < nr_zones; i++) {
  1808. FDEV(devi).blkz_type[n] = zones[i].type;
  1809. sector += zones[i].len;
  1810. n++;
  1811. }
  1812. }
  1813. kfree(zones);
  1814. return err;
  1815. }
  1816. #endif
  1817. /*
  1818. * Read f2fs raw super block.
  1819. * Because we have two copies of super block, so read both of them
  1820. * to get the first valid one. If any one of them is broken, we pass
  1821. * them recovery flag back to the caller.
  1822. */
  1823. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  1824. struct f2fs_super_block **raw_super,
  1825. int *valid_super_block, int *recovery)
  1826. {
  1827. struct super_block *sb = sbi->sb;
  1828. int block;
  1829. struct buffer_head *bh;
  1830. struct f2fs_super_block *super;
  1831. int err = 0;
  1832. super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
  1833. if (!super)
  1834. return -ENOMEM;
  1835. for (block = 0; block < 2; block++) {
  1836. bh = sb_bread(sb, block);
  1837. if (!bh) {
  1838. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  1839. block + 1);
  1840. err = -EIO;
  1841. continue;
  1842. }
  1843. /* sanity checking of raw super */
  1844. if (sanity_check_raw_super(sbi, bh)) {
  1845. f2fs_msg(sb, KERN_ERR,
  1846. "Can't find valid F2FS filesystem in %dth superblock",
  1847. block + 1);
  1848. err = -EINVAL;
  1849. brelse(bh);
  1850. continue;
  1851. }
  1852. if (!*raw_super) {
  1853. memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
  1854. sizeof(*super));
  1855. *valid_super_block = block;
  1856. *raw_super = super;
  1857. }
  1858. brelse(bh);
  1859. }
  1860. /* Fail to read any one of the superblocks*/
  1861. if (err < 0)
  1862. *recovery = 1;
  1863. /* No valid superblock */
  1864. if (!*raw_super)
  1865. kfree(super);
  1866. else
  1867. err = 0;
  1868. return err;
  1869. }
  1870. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  1871. {
  1872. struct buffer_head *bh;
  1873. int err;
  1874. if ((recover && f2fs_readonly(sbi->sb)) ||
  1875. bdev_read_only(sbi->sb->s_bdev)) {
  1876. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1877. return -EROFS;
  1878. }
  1879. /* write back-up superblock first */
  1880. bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
  1881. if (!bh)
  1882. return -EIO;
  1883. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1884. brelse(bh);
  1885. /* if we are in recovery path, skip writing valid superblock */
  1886. if (recover || err)
  1887. return err;
  1888. /* write current valid superblock */
  1889. bh = sb_getblk(sbi->sb, sbi->valid_super_block);
  1890. if (!bh)
  1891. return -EIO;
  1892. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1893. brelse(bh);
  1894. return err;
  1895. }
  1896. static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
  1897. {
  1898. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1899. unsigned int max_devices = MAX_DEVICES;
  1900. int i;
  1901. /* Initialize single device information */
  1902. if (!RDEV(0).path[0]) {
  1903. if (!bdev_is_zoned(sbi->sb->s_bdev))
  1904. return 0;
  1905. max_devices = 1;
  1906. }
  1907. /*
  1908. * Initialize multiple devices information, or single
  1909. * zoned block device information.
  1910. */
  1911. sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
  1912. GFP_KERNEL);
  1913. if (!sbi->devs)
  1914. return -ENOMEM;
  1915. for (i = 0; i < max_devices; i++) {
  1916. if (i > 0 && !RDEV(i).path[0])
  1917. break;
  1918. if (max_devices == 1) {
  1919. /* Single zoned block device mount */
  1920. FDEV(0).bdev =
  1921. blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
  1922. sbi->sb->s_mode, sbi->sb->s_type);
  1923. } else {
  1924. /* Multi-device mount */
  1925. memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
  1926. FDEV(i).total_segments =
  1927. le32_to_cpu(RDEV(i).total_segments);
  1928. if (i == 0) {
  1929. FDEV(i).start_blk = 0;
  1930. FDEV(i).end_blk = FDEV(i).start_blk +
  1931. (FDEV(i).total_segments <<
  1932. sbi->log_blocks_per_seg) - 1 +
  1933. le32_to_cpu(raw_super->segment0_blkaddr);
  1934. } else {
  1935. FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
  1936. FDEV(i).end_blk = FDEV(i).start_blk +
  1937. (FDEV(i).total_segments <<
  1938. sbi->log_blocks_per_seg) - 1;
  1939. }
  1940. FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
  1941. sbi->sb->s_mode, sbi->sb->s_type);
  1942. }
  1943. if (IS_ERR(FDEV(i).bdev))
  1944. return PTR_ERR(FDEV(i).bdev);
  1945. /* to release errored devices */
  1946. sbi->s_ndevs = i + 1;
  1947. #ifdef CONFIG_BLK_DEV_ZONED
  1948. if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
  1949. !f2fs_sb_mounted_blkzoned(sbi->sb)) {
  1950. f2fs_msg(sbi->sb, KERN_ERR,
  1951. "Zoned block device feature not enabled\n");
  1952. return -EINVAL;
  1953. }
  1954. if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
  1955. if (init_blkz_info(sbi, i)) {
  1956. f2fs_msg(sbi->sb, KERN_ERR,
  1957. "Failed to initialize F2FS blkzone information");
  1958. return -EINVAL;
  1959. }
  1960. if (max_devices == 1)
  1961. break;
  1962. f2fs_msg(sbi->sb, KERN_INFO,
  1963. "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
  1964. i, FDEV(i).path,
  1965. FDEV(i).total_segments,
  1966. FDEV(i).start_blk, FDEV(i).end_blk,
  1967. bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
  1968. "Host-aware" : "Host-managed");
  1969. continue;
  1970. }
  1971. #endif
  1972. f2fs_msg(sbi->sb, KERN_INFO,
  1973. "Mount Device [%2d]: %20s, %8u, %8x - %8x",
  1974. i, FDEV(i).path,
  1975. FDEV(i).total_segments,
  1976. FDEV(i).start_blk, FDEV(i).end_blk);
  1977. }
  1978. f2fs_msg(sbi->sb, KERN_INFO,
  1979. "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
  1980. return 0;
  1981. }
  1982. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  1983. {
  1984. struct f2fs_sb_info *sbi;
  1985. struct f2fs_super_block *raw_super;
  1986. struct inode *root;
  1987. int err;
  1988. bool retry = true, need_fsck = false;
  1989. char *options = NULL;
  1990. int recovery, i, valid_super_block;
  1991. struct curseg_info *seg_i;
  1992. try_onemore:
  1993. err = -EINVAL;
  1994. raw_super = NULL;
  1995. valid_super_block = -1;
  1996. recovery = 0;
  1997. /* allocate memory for f2fs-specific super block info */
  1998. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  1999. if (!sbi)
  2000. return -ENOMEM;
  2001. sbi->sb = sb;
  2002. /* Load the checksum driver */
  2003. sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
  2004. if (IS_ERR(sbi->s_chksum_driver)) {
  2005. f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
  2006. err = PTR_ERR(sbi->s_chksum_driver);
  2007. sbi->s_chksum_driver = NULL;
  2008. goto free_sbi;
  2009. }
  2010. /* set a block size */
  2011. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  2012. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  2013. goto free_sbi;
  2014. }
  2015. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  2016. &recovery);
  2017. if (err)
  2018. goto free_sbi;
  2019. sb->s_fs_info = sbi;
  2020. sbi->raw_super = raw_super;
  2021. /* precompute checksum seed for metadata */
  2022. if (f2fs_sb_has_inode_chksum(sb))
  2023. sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
  2024. sizeof(raw_super->uuid));
  2025. /*
  2026. * The BLKZONED feature indicates that the drive was formatted with
  2027. * zone alignment optimization. This is optional for host-aware
  2028. * devices, but mandatory for host-managed zoned block devices.
  2029. */
  2030. #ifndef CONFIG_BLK_DEV_ZONED
  2031. if (f2fs_sb_mounted_blkzoned(sb)) {
  2032. f2fs_msg(sb, KERN_ERR,
  2033. "Zoned block device support is not enabled\n");
  2034. err = -EOPNOTSUPP;
  2035. goto free_sb_buf;
  2036. }
  2037. #endif
  2038. default_options(sbi);
  2039. /* parse mount options */
  2040. options = kstrdup((const char *)data, GFP_KERNEL);
  2041. if (data && !options) {
  2042. err = -ENOMEM;
  2043. goto free_sb_buf;
  2044. }
  2045. err = parse_options(sb, options);
  2046. if (err)
  2047. goto free_options;
  2048. sbi->max_file_blocks = max_file_blocks();
  2049. sb->s_maxbytes = sbi->max_file_blocks <<
  2050. le32_to_cpu(raw_super->log_blocksize);
  2051. sb->s_max_links = F2FS_LINK_MAX;
  2052. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  2053. #ifdef CONFIG_QUOTA
  2054. sb->dq_op = &f2fs_quota_operations;
  2055. sb->s_qcop = &f2fs_quotactl_ops;
  2056. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  2057. #endif
  2058. sb->s_op = &f2fs_sops;
  2059. sb->s_cop = &f2fs_cryptops;
  2060. sb->s_xattr = f2fs_xattr_handlers;
  2061. sb->s_export_op = &f2fs_export_ops;
  2062. sb->s_magic = F2FS_SUPER_MAGIC;
  2063. sb->s_time_gran = 1;
  2064. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2065. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  2066. memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  2067. /* init f2fs-specific super block info */
  2068. sbi->valid_super_block = valid_super_block;
  2069. mutex_init(&sbi->gc_mutex);
  2070. mutex_init(&sbi->cp_mutex);
  2071. init_rwsem(&sbi->node_write);
  2072. init_rwsem(&sbi->node_change);
  2073. /* disallow all the data/node/meta page writes */
  2074. set_sbi_flag(sbi, SBI_POR_DOING);
  2075. spin_lock_init(&sbi->stat_lock);
  2076. /* init iostat info */
  2077. spin_lock_init(&sbi->iostat_lock);
  2078. sbi->iostat_enable = false;
  2079. for (i = 0; i < NR_PAGE_TYPE; i++) {
  2080. int n = (i == META) ? 1: NR_TEMP_TYPE;
  2081. int j;
  2082. sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
  2083. GFP_KERNEL);
  2084. if (!sbi->write_io[i]) {
  2085. err = -ENOMEM;
  2086. goto free_options;
  2087. }
  2088. for (j = HOT; j < n; j++) {
  2089. init_rwsem(&sbi->write_io[i][j].io_rwsem);
  2090. sbi->write_io[i][j].sbi = sbi;
  2091. sbi->write_io[i][j].bio = NULL;
  2092. spin_lock_init(&sbi->write_io[i][j].io_lock);
  2093. INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
  2094. }
  2095. }
  2096. init_rwsem(&sbi->cp_rwsem);
  2097. init_waitqueue_head(&sbi->cp_wait);
  2098. init_sb_info(sbi);
  2099. err = init_percpu_info(sbi);
  2100. if (err)
  2101. goto free_options;
  2102. if (F2FS_IO_SIZE(sbi) > 1) {
  2103. sbi->write_io_dummy =
  2104. mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
  2105. if (!sbi->write_io_dummy) {
  2106. err = -ENOMEM;
  2107. goto free_options;
  2108. }
  2109. }
  2110. /* get an inode for meta space */
  2111. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  2112. if (IS_ERR(sbi->meta_inode)) {
  2113. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  2114. err = PTR_ERR(sbi->meta_inode);
  2115. goto free_io_dummy;
  2116. }
  2117. err = get_valid_checkpoint(sbi);
  2118. if (err) {
  2119. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  2120. goto free_meta_inode;
  2121. }
  2122. /* Initialize device list */
  2123. err = f2fs_scan_devices(sbi);
  2124. if (err) {
  2125. f2fs_msg(sb, KERN_ERR, "Failed to find devices");
  2126. goto free_devices;
  2127. }
  2128. sbi->total_valid_node_count =
  2129. le32_to_cpu(sbi->ckpt->valid_node_count);
  2130. percpu_counter_set(&sbi->total_valid_inode_count,
  2131. le32_to_cpu(sbi->ckpt->valid_inode_count));
  2132. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  2133. sbi->total_valid_block_count =
  2134. le64_to_cpu(sbi->ckpt->valid_block_count);
  2135. sbi->last_valid_block_count = sbi->total_valid_block_count;
  2136. sbi->reserved_blocks = 0;
  2137. for (i = 0; i < NR_INODE_TYPE; i++) {
  2138. INIT_LIST_HEAD(&sbi->inode_list[i]);
  2139. spin_lock_init(&sbi->inode_lock[i]);
  2140. }
  2141. init_extent_cache_info(sbi);
  2142. init_ino_entry_info(sbi);
  2143. /* setup f2fs internal modules */
  2144. err = build_segment_manager(sbi);
  2145. if (err) {
  2146. f2fs_msg(sb, KERN_ERR,
  2147. "Failed to initialize F2FS segment manager");
  2148. goto free_sm;
  2149. }
  2150. err = build_node_manager(sbi);
  2151. if (err) {
  2152. f2fs_msg(sb, KERN_ERR,
  2153. "Failed to initialize F2FS node manager");
  2154. goto free_nm;
  2155. }
  2156. /* For write statistics */
  2157. if (sb->s_bdev->bd_part)
  2158. sbi->sectors_written_start =
  2159. (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2160. /* Read accumulated write IO statistics if exists */
  2161. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  2162. if (__exist_node_summaries(sbi))
  2163. sbi->kbytes_written =
  2164. le64_to_cpu(seg_i->journal->info.kbytes_written);
  2165. build_gc_manager(sbi);
  2166. /* get an inode for node space */
  2167. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  2168. if (IS_ERR(sbi->node_inode)) {
  2169. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  2170. err = PTR_ERR(sbi->node_inode);
  2171. goto free_nm;
  2172. }
  2173. f2fs_join_shrinker(sbi);
  2174. err = f2fs_build_stats(sbi);
  2175. if (err)
  2176. goto free_nm;
  2177. /* read root inode and dentry */
  2178. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  2179. if (IS_ERR(root)) {
  2180. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  2181. err = PTR_ERR(root);
  2182. goto free_node_inode;
  2183. }
  2184. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  2185. iput(root);
  2186. err = -EINVAL;
  2187. goto free_node_inode;
  2188. }
  2189. sb->s_root = d_make_root(root); /* allocate root dentry */
  2190. if (!sb->s_root) {
  2191. err = -ENOMEM;
  2192. goto free_root_inode;
  2193. }
  2194. err = f2fs_register_sysfs(sbi);
  2195. if (err)
  2196. goto free_root_inode;
  2197. /* if there are nt orphan nodes free them */
  2198. err = recover_orphan_inodes(sbi);
  2199. if (err)
  2200. goto free_sysfs;
  2201. /* recover fsynced data */
  2202. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  2203. /*
  2204. * mount should be failed, when device has readonly mode, and
  2205. * previous checkpoint was not done by clean system shutdown.
  2206. */
  2207. if (bdev_read_only(sb->s_bdev) &&
  2208. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  2209. err = -EROFS;
  2210. goto free_meta;
  2211. }
  2212. if (need_fsck)
  2213. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2214. if (!retry)
  2215. goto skip_recovery;
  2216. err = recover_fsync_data(sbi, false);
  2217. if (err < 0) {
  2218. need_fsck = true;
  2219. f2fs_msg(sb, KERN_ERR,
  2220. "Cannot recover all fsync data errno=%d", err);
  2221. goto free_meta;
  2222. }
  2223. } else {
  2224. err = recover_fsync_data(sbi, true);
  2225. if (!f2fs_readonly(sb) && err > 0) {
  2226. err = -EINVAL;
  2227. f2fs_msg(sb, KERN_ERR,
  2228. "Need to recover fsync data");
  2229. goto free_sysfs;
  2230. }
  2231. }
  2232. skip_recovery:
  2233. /* recover_fsync_data() cleared this already */
  2234. clear_sbi_flag(sbi, SBI_POR_DOING);
  2235. /*
  2236. * If filesystem is not mounted as read-only then
  2237. * do start the gc_thread.
  2238. */
  2239. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  2240. /* After POR, we can run background GC thread.*/
  2241. err = start_gc_thread(sbi);
  2242. if (err)
  2243. goto free_meta;
  2244. }
  2245. kfree(options);
  2246. /* recover broken superblock */
  2247. if (recovery) {
  2248. err = f2fs_commit_super(sbi, true);
  2249. f2fs_msg(sb, KERN_INFO,
  2250. "Try to recover %dth superblock, ret: %d",
  2251. sbi->valid_super_block ? 1 : 2, err);
  2252. }
  2253. f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
  2254. cur_cp_version(F2FS_CKPT(sbi)));
  2255. f2fs_update_time(sbi, CP_TIME);
  2256. f2fs_update_time(sbi, REQ_TIME);
  2257. return 0;
  2258. free_meta:
  2259. f2fs_sync_inode_meta(sbi);
  2260. /*
  2261. * Some dirty meta pages can be produced by recover_orphan_inodes()
  2262. * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
  2263. * followed by write_checkpoint() through f2fs_write_node_pages(), which
  2264. * falls into an infinite loop in sync_meta_pages().
  2265. */
  2266. truncate_inode_pages_final(META_MAPPING(sbi));
  2267. free_sysfs:
  2268. f2fs_unregister_sysfs(sbi);
  2269. free_root_inode:
  2270. dput(sb->s_root);
  2271. sb->s_root = NULL;
  2272. free_node_inode:
  2273. truncate_inode_pages_final(NODE_MAPPING(sbi));
  2274. mutex_lock(&sbi->umount_mutex);
  2275. release_ino_entry(sbi, true);
  2276. f2fs_leave_shrinker(sbi);
  2277. iput(sbi->node_inode);
  2278. mutex_unlock(&sbi->umount_mutex);
  2279. f2fs_destroy_stats(sbi);
  2280. free_nm:
  2281. destroy_node_manager(sbi);
  2282. free_sm:
  2283. destroy_segment_manager(sbi);
  2284. free_devices:
  2285. destroy_device_list(sbi);
  2286. kfree(sbi->ckpt);
  2287. free_meta_inode:
  2288. make_bad_inode(sbi->meta_inode);
  2289. iput(sbi->meta_inode);
  2290. free_io_dummy:
  2291. mempool_destroy(sbi->write_io_dummy);
  2292. free_options:
  2293. for (i = 0; i < NR_PAGE_TYPE; i++)
  2294. kfree(sbi->write_io[i]);
  2295. destroy_percpu_info(sbi);
  2296. #ifdef CONFIG_QUOTA
  2297. for (i = 0; i < MAXQUOTAS; i++)
  2298. kfree(sbi->s_qf_names[i]);
  2299. #endif
  2300. kfree(options);
  2301. free_sb_buf:
  2302. kfree(raw_super);
  2303. free_sbi:
  2304. if (sbi->s_chksum_driver)
  2305. crypto_free_shash(sbi->s_chksum_driver);
  2306. kfree(sbi);
  2307. /* give only one another chance */
  2308. if (retry) {
  2309. retry = false;
  2310. shrink_dcache_sb(sb);
  2311. goto try_onemore;
  2312. }
  2313. return err;
  2314. }
  2315. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  2316. const char *dev_name, void *data)
  2317. {
  2318. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  2319. }
  2320. static void kill_f2fs_super(struct super_block *sb)
  2321. {
  2322. if (sb->s_root) {
  2323. set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
  2324. stop_gc_thread(F2FS_SB(sb));
  2325. stop_discard_thread(F2FS_SB(sb));
  2326. }
  2327. kill_block_super(sb);
  2328. }
  2329. static struct file_system_type f2fs_fs_type = {
  2330. .owner = THIS_MODULE,
  2331. .name = "f2fs",
  2332. .mount = f2fs_mount,
  2333. .kill_sb = kill_f2fs_super,
  2334. .fs_flags = FS_REQUIRES_DEV,
  2335. };
  2336. MODULE_ALIAS_FS("f2fs");
  2337. static int __init init_inodecache(void)
  2338. {
  2339. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  2340. sizeof(struct f2fs_inode_info), 0,
  2341. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  2342. if (!f2fs_inode_cachep)
  2343. return -ENOMEM;
  2344. return 0;
  2345. }
  2346. static void destroy_inodecache(void)
  2347. {
  2348. /*
  2349. * Make sure all delayed rcu free inodes are flushed before we
  2350. * destroy cache.
  2351. */
  2352. rcu_barrier();
  2353. kmem_cache_destroy(f2fs_inode_cachep);
  2354. }
  2355. static int __init init_f2fs_fs(void)
  2356. {
  2357. int err;
  2358. f2fs_build_trace_ios();
  2359. err = init_inodecache();
  2360. if (err)
  2361. goto fail;
  2362. err = create_node_manager_caches();
  2363. if (err)
  2364. goto free_inodecache;
  2365. err = create_segment_manager_caches();
  2366. if (err)
  2367. goto free_node_manager_caches;
  2368. err = create_checkpoint_caches();
  2369. if (err)
  2370. goto free_segment_manager_caches;
  2371. err = create_extent_cache();
  2372. if (err)
  2373. goto free_checkpoint_caches;
  2374. err = f2fs_init_sysfs();
  2375. if (err)
  2376. goto free_extent_cache;
  2377. err = register_shrinker(&f2fs_shrinker_info);
  2378. if (err)
  2379. goto free_sysfs;
  2380. err = register_filesystem(&f2fs_fs_type);
  2381. if (err)
  2382. goto free_shrinker;
  2383. err = f2fs_create_root_stats();
  2384. if (err)
  2385. goto free_filesystem;
  2386. return 0;
  2387. free_filesystem:
  2388. unregister_filesystem(&f2fs_fs_type);
  2389. free_shrinker:
  2390. unregister_shrinker(&f2fs_shrinker_info);
  2391. free_sysfs:
  2392. f2fs_exit_sysfs();
  2393. free_extent_cache:
  2394. destroy_extent_cache();
  2395. free_checkpoint_caches:
  2396. destroy_checkpoint_caches();
  2397. free_segment_manager_caches:
  2398. destroy_segment_manager_caches();
  2399. free_node_manager_caches:
  2400. destroy_node_manager_caches();
  2401. free_inodecache:
  2402. destroy_inodecache();
  2403. fail:
  2404. return err;
  2405. }
  2406. static void __exit exit_f2fs_fs(void)
  2407. {
  2408. f2fs_destroy_root_stats();
  2409. unregister_filesystem(&f2fs_fs_type);
  2410. unregister_shrinker(&f2fs_shrinker_info);
  2411. f2fs_exit_sysfs();
  2412. destroy_extent_cache();
  2413. destroy_checkpoint_caches();
  2414. destroy_segment_manager_caches();
  2415. destroy_node_manager_caches();
  2416. destroy_inodecache();
  2417. f2fs_destroy_trace_ios();
  2418. }
  2419. module_init(init_f2fs_fs)
  2420. module_exit(exit_f2fs_fs)
  2421. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  2422. MODULE_DESCRIPTION("Flash Friendly File System");
  2423. MODULE_LICENSE("GPL");