super.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375
  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 *f2fs_fault_name[FAULT_MAX] = {
  40. [FAULT_KMALLOC] = "kmalloc",
  41. [FAULT_KVMALLOC] = "kvmalloc",
  42. [FAULT_PAGE_ALLOC] = "page alloc",
  43. [FAULT_PAGE_GET] = "page get",
  44. [FAULT_ALLOC_BIO] = "alloc bio",
  45. [FAULT_ALLOC_NID] = "alloc nid",
  46. [FAULT_ORPHAN] = "orphan",
  47. [FAULT_BLOCK] = "no more block",
  48. [FAULT_DIR_DEPTH] = "too big dir depth",
  49. [FAULT_EVICT_INODE] = "evict_inode fail",
  50. [FAULT_TRUNCATE] = "truncate fail",
  51. [FAULT_IO] = "IO error",
  52. [FAULT_CHECKPOINT] = "checkpoint error",
  53. [FAULT_DISCARD] = "discard error",
  54. };
  55. void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
  56. unsigned int type)
  57. {
  58. struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
  59. if (rate) {
  60. atomic_set(&ffi->inject_ops, 0);
  61. ffi->inject_rate = rate;
  62. }
  63. if (type)
  64. ffi->inject_type = type;
  65. if (!rate && !type)
  66. memset(ffi, 0, sizeof(struct f2fs_fault_info));
  67. }
  68. #endif
  69. /* f2fs-wide shrinker description */
  70. static struct shrinker f2fs_shrinker_info = {
  71. .scan_objects = f2fs_shrink_scan,
  72. .count_objects = f2fs_shrink_count,
  73. .seeks = DEFAULT_SEEKS,
  74. };
  75. enum {
  76. Opt_gc_background,
  77. Opt_disable_roll_forward,
  78. Opt_norecovery,
  79. Opt_discard,
  80. Opt_nodiscard,
  81. Opt_noheap,
  82. Opt_heap,
  83. Opt_user_xattr,
  84. Opt_nouser_xattr,
  85. Opt_acl,
  86. Opt_noacl,
  87. Opt_active_logs,
  88. Opt_disable_ext_identify,
  89. Opt_inline_xattr,
  90. Opt_noinline_xattr,
  91. Opt_inline_xattr_size,
  92. Opt_inline_data,
  93. Opt_inline_dentry,
  94. Opt_noinline_dentry,
  95. Opt_flush_merge,
  96. Opt_noflush_merge,
  97. Opt_nobarrier,
  98. Opt_fastboot,
  99. Opt_extent_cache,
  100. Opt_noextent_cache,
  101. Opt_noinline_data,
  102. Opt_data_flush,
  103. Opt_reserve_root,
  104. Opt_resgid,
  105. Opt_resuid,
  106. Opt_mode,
  107. Opt_io_size_bits,
  108. Opt_fault_injection,
  109. Opt_fault_type,
  110. Opt_lazytime,
  111. Opt_nolazytime,
  112. Opt_quota,
  113. Opt_noquota,
  114. Opt_usrquota,
  115. Opt_grpquota,
  116. Opt_prjquota,
  117. Opt_usrjquota,
  118. Opt_grpjquota,
  119. Opt_prjjquota,
  120. Opt_offusrjquota,
  121. Opt_offgrpjquota,
  122. Opt_offprjjquota,
  123. Opt_jqfmt_vfsold,
  124. Opt_jqfmt_vfsv0,
  125. Opt_jqfmt_vfsv1,
  126. Opt_whint,
  127. Opt_alloc,
  128. Opt_fsync,
  129. Opt_test_dummy_encryption,
  130. Opt_err,
  131. };
  132. static match_table_t f2fs_tokens = {
  133. {Opt_gc_background, "background_gc=%s"},
  134. {Opt_disable_roll_forward, "disable_roll_forward"},
  135. {Opt_norecovery, "norecovery"},
  136. {Opt_discard, "discard"},
  137. {Opt_nodiscard, "nodiscard"},
  138. {Opt_noheap, "no_heap"},
  139. {Opt_heap, "heap"},
  140. {Opt_user_xattr, "user_xattr"},
  141. {Opt_nouser_xattr, "nouser_xattr"},
  142. {Opt_acl, "acl"},
  143. {Opt_noacl, "noacl"},
  144. {Opt_active_logs, "active_logs=%u"},
  145. {Opt_disable_ext_identify, "disable_ext_identify"},
  146. {Opt_inline_xattr, "inline_xattr"},
  147. {Opt_noinline_xattr, "noinline_xattr"},
  148. {Opt_inline_xattr_size, "inline_xattr_size=%u"},
  149. {Opt_inline_data, "inline_data"},
  150. {Opt_inline_dentry, "inline_dentry"},
  151. {Opt_noinline_dentry, "noinline_dentry"},
  152. {Opt_flush_merge, "flush_merge"},
  153. {Opt_noflush_merge, "noflush_merge"},
  154. {Opt_nobarrier, "nobarrier"},
  155. {Opt_fastboot, "fastboot"},
  156. {Opt_extent_cache, "extent_cache"},
  157. {Opt_noextent_cache, "noextent_cache"},
  158. {Opt_noinline_data, "noinline_data"},
  159. {Opt_data_flush, "data_flush"},
  160. {Opt_reserve_root, "reserve_root=%u"},
  161. {Opt_resgid, "resgid=%u"},
  162. {Opt_resuid, "resuid=%u"},
  163. {Opt_mode, "mode=%s"},
  164. {Opt_io_size_bits, "io_bits=%u"},
  165. {Opt_fault_injection, "fault_injection=%u"},
  166. {Opt_fault_type, "fault_type=%u"},
  167. {Opt_lazytime, "lazytime"},
  168. {Opt_nolazytime, "nolazytime"},
  169. {Opt_quota, "quota"},
  170. {Opt_noquota, "noquota"},
  171. {Opt_usrquota, "usrquota"},
  172. {Opt_grpquota, "grpquota"},
  173. {Opt_prjquota, "prjquota"},
  174. {Opt_usrjquota, "usrjquota=%s"},
  175. {Opt_grpjquota, "grpjquota=%s"},
  176. {Opt_prjjquota, "prjjquota=%s"},
  177. {Opt_offusrjquota, "usrjquota="},
  178. {Opt_offgrpjquota, "grpjquota="},
  179. {Opt_offprjjquota, "prjjquota="},
  180. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  181. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  182. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  183. {Opt_whint, "whint_mode=%s"},
  184. {Opt_alloc, "alloc_mode=%s"},
  185. {Opt_fsync, "fsync_mode=%s"},
  186. {Opt_test_dummy_encryption, "test_dummy_encryption"},
  187. {Opt_err, NULL},
  188. };
  189. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  190. {
  191. struct va_format vaf;
  192. va_list args;
  193. va_start(args, fmt);
  194. vaf.fmt = fmt;
  195. vaf.va = &args;
  196. printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  197. va_end(args);
  198. }
  199. static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
  200. {
  201. block_t limit = (sbi->user_block_count << 1) / 1000;
  202. /* limit is 0.2% */
  203. if (test_opt(sbi, RESERVE_ROOT) &&
  204. F2FS_OPTION(sbi).root_reserved_blocks > limit) {
  205. F2FS_OPTION(sbi).root_reserved_blocks = limit;
  206. f2fs_msg(sbi->sb, KERN_INFO,
  207. "Reduce reserved blocks for root = %u",
  208. F2FS_OPTION(sbi).root_reserved_blocks);
  209. }
  210. if (!test_opt(sbi, RESERVE_ROOT) &&
  211. (!uid_eq(F2FS_OPTION(sbi).s_resuid,
  212. make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
  213. !gid_eq(F2FS_OPTION(sbi).s_resgid,
  214. make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
  215. f2fs_msg(sbi->sb, KERN_INFO,
  216. "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
  217. from_kuid_munged(&init_user_ns,
  218. F2FS_OPTION(sbi).s_resuid),
  219. from_kgid_munged(&init_user_ns,
  220. F2FS_OPTION(sbi).s_resgid));
  221. }
  222. static void init_once(void *foo)
  223. {
  224. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  225. inode_init_once(&fi->vfs_inode);
  226. }
  227. #ifdef CONFIG_QUOTA
  228. static const char * const quotatypes[] = INITQFNAMES;
  229. #define QTYPE2NAME(t) (quotatypes[t])
  230. static int f2fs_set_qf_name(struct super_block *sb, int qtype,
  231. substring_t *args)
  232. {
  233. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  234. char *qname;
  235. int ret = -EINVAL;
  236. if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
  237. f2fs_msg(sb, KERN_ERR,
  238. "Cannot change journaled "
  239. "quota options when quota turned on");
  240. return -EINVAL;
  241. }
  242. if (f2fs_sb_has_quota_ino(sb)) {
  243. f2fs_msg(sb, KERN_INFO,
  244. "QUOTA feature is enabled, so ignore qf_name");
  245. return 0;
  246. }
  247. qname = match_strdup(args);
  248. if (!qname) {
  249. f2fs_msg(sb, KERN_ERR,
  250. "Not enough memory for storing quotafile name");
  251. return -EINVAL;
  252. }
  253. if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
  254. if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
  255. ret = 0;
  256. else
  257. f2fs_msg(sb, KERN_ERR,
  258. "%s quota file already specified",
  259. QTYPE2NAME(qtype));
  260. goto errout;
  261. }
  262. if (strchr(qname, '/')) {
  263. f2fs_msg(sb, KERN_ERR,
  264. "quotafile must be on filesystem root");
  265. goto errout;
  266. }
  267. F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
  268. set_opt(sbi, QUOTA);
  269. return 0;
  270. errout:
  271. kfree(qname);
  272. return ret;
  273. }
  274. static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
  275. {
  276. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  277. if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
  278. f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  279. " when quota turned on");
  280. return -EINVAL;
  281. }
  282. kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
  283. F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
  284. return 0;
  285. }
  286. static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
  287. {
  288. /*
  289. * We do the test below only for project quotas. 'usrquota' and
  290. * 'grpquota' mount options are allowed even without quota feature
  291. * to support legacy quotas in quota files.
  292. */
  293. if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
  294. f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
  295. "Cannot enable project quota enforcement.");
  296. return -1;
  297. }
  298. if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
  299. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
  300. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
  301. if (test_opt(sbi, USRQUOTA) &&
  302. F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
  303. clear_opt(sbi, USRQUOTA);
  304. if (test_opt(sbi, GRPQUOTA) &&
  305. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
  306. clear_opt(sbi, GRPQUOTA);
  307. if (test_opt(sbi, PRJQUOTA) &&
  308. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
  309. clear_opt(sbi, PRJQUOTA);
  310. if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
  311. test_opt(sbi, PRJQUOTA)) {
  312. f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
  313. "format mixing");
  314. return -1;
  315. }
  316. if (!F2FS_OPTION(sbi).s_jquota_fmt) {
  317. f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
  318. "not specified");
  319. return -1;
  320. }
  321. }
  322. if (f2fs_sb_has_quota_ino(sbi->sb) && F2FS_OPTION(sbi).s_jquota_fmt) {
  323. f2fs_msg(sbi->sb, KERN_INFO,
  324. "QUOTA feature is enabled, so ignore jquota_fmt");
  325. F2FS_OPTION(sbi).s_jquota_fmt = 0;
  326. }
  327. return 0;
  328. }
  329. #endif
  330. static int parse_options(struct super_block *sb, char *options)
  331. {
  332. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  333. substring_t args[MAX_OPT_ARGS];
  334. char *p, *name;
  335. int arg = 0;
  336. kuid_t uid;
  337. kgid_t gid;
  338. #ifdef CONFIG_QUOTA
  339. int ret;
  340. #endif
  341. if (!options)
  342. return 0;
  343. while ((p = strsep(&options, ",")) != NULL) {
  344. int token;
  345. if (!*p)
  346. continue;
  347. /*
  348. * Initialize args struct so we know whether arg was
  349. * found; some options take optional arguments.
  350. */
  351. args[0].to = args[0].from = NULL;
  352. token = match_token(p, f2fs_tokens, args);
  353. switch (token) {
  354. case Opt_gc_background:
  355. name = match_strdup(&args[0]);
  356. if (!name)
  357. return -ENOMEM;
  358. if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
  359. set_opt(sbi, BG_GC);
  360. clear_opt(sbi, FORCE_FG_GC);
  361. } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
  362. clear_opt(sbi, BG_GC);
  363. clear_opt(sbi, FORCE_FG_GC);
  364. } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
  365. set_opt(sbi, BG_GC);
  366. set_opt(sbi, FORCE_FG_GC);
  367. } else {
  368. kfree(name);
  369. return -EINVAL;
  370. }
  371. kfree(name);
  372. break;
  373. case Opt_disable_roll_forward:
  374. set_opt(sbi, DISABLE_ROLL_FORWARD);
  375. break;
  376. case Opt_norecovery:
  377. /* this option mounts f2fs with ro */
  378. set_opt(sbi, DISABLE_ROLL_FORWARD);
  379. if (!f2fs_readonly(sb))
  380. return -EINVAL;
  381. break;
  382. case Opt_discard:
  383. set_opt(sbi, DISCARD);
  384. break;
  385. case Opt_nodiscard:
  386. if (f2fs_sb_has_blkzoned(sb)) {
  387. f2fs_msg(sb, KERN_WARNING,
  388. "discard is required for zoned block devices");
  389. return -EINVAL;
  390. }
  391. clear_opt(sbi, DISCARD);
  392. break;
  393. case Opt_noheap:
  394. set_opt(sbi, NOHEAP);
  395. break;
  396. case Opt_heap:
  397. clear_opt(sbi, NOHEAP);
  398. break;
  399. #ifdef CONFIG_F2FS_FS_XATTR
  400. case Opt_user_xattr:
  401. set_opt(sbi, XATTR_USER);
  402. break;
  403. case Opt_nouser_xattr:
  404. clear_opt(sbi, XATTR_USER);
  405. break;
  406. case Opt_inline_xattr:
  407. set_opt(sbi, INLINE_XATTR);
  408. break;
  409. case Opt_noinline_xattr:
  410. clear_opt(sbi, INLINE_XATTR);
  411. break;
  412. case Opt_inline_xattr_size:
  413. if (args->from && match_int(args, &arg))
  414. return -EINVAL;
  415. set_opt(sbi, INLINE_XATTR_SIZE);
  416. F2FS_OPTION(sbi).inline_xattr_size = arg;
  417. break;
  418. #else
  419. case Opt_user_xattr:
  420. f2fs_msg(sb, KERN_INFO,
  421. "user_xattr options not supported");
  422. break;
  423. case Opt_nouser_xattr:
  424. f2fs_msg(sb, KERN_INFO,
  425. "nouser_xattr options not supported");
  426. break;
  427. case Opt_inline_xattr:
  428. f2fs_msg(sb, KERN_INFO,
  429. "inline_xattr options not supported");
  430. break;
  431. case Opt_noinline_xattr:
  432. f2fs_msg(sb, KERN_INFO,
  433. "noinline_xattr options not supported");
  434. break;
  435. #endif
  436. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  437. case Opt_acl:
  438. set_opt(sbi, POSIX_ACL);
  439. break;
  440. case Opt_noacl:
  441. clear_opt(sbi, POSIX_ACL);
  442. break;
  443. #else
  444. case Opt_acl:
  445. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  446. break;
  447. case Opt_noacl:
  448. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  449. break;
  450. #endif
  451. case Opt_active_logs:
  452. if (args->from && match_int(args, &arg))
  453. return -EINVAL;
  454. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  455. return -EINVAL;
  456. F2FS_OPTION(sbi).active_logs = arg;
  457. break;
  458. case Opt_disable_ext_identify:
  459. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  460. break;
  461. case Opt_inline_data:
  462. set_opt(sbi, INLINE_DATA);
  463. break;
  464. case Opt_inline_dentry:
  465. set_opt(sbi, INLINE_DENTRY);
  466. break;
  467. case Opt_noinline_dentry:
  468. clear_opt(sbi, INLINE_DENTRY);
  469. break;
  470. case Opt_flush_merge:
  471. set_opt(sbi, FLUSH_MERGE);
  472. break;
  473. case Opt_noflush_merge:
  474. clear_opt(sbi, FLUSH_MERGE);
  475. break;
  476. case Opt_nobarrier:
  477. set_opt(sbi, NOBARRIER);
  478. break;
  479. case Opt_fastboot:
  480. set_opt(sbi, FASTBOOT);
  481. break;
  482. case Opt_extent_cache:
  483. set_opt(sbi, EXTENT_CACHE);
  484. break;
  485. case Opt_noextent_cache:
  486. clear_opt(sbi, EXTENT_CACHE);
  487. break;
  488. case Opt_noinline_data:
  489. clear_opt(sbi, INLINE_DATA);
  490. break;
  491. case Opt_data_flush:
  492. set_opt(sbi, DATA_FLUSH);
  493. break;
  494. case Opt_reserve_root:
  495. if (args->from && match_int(args, &arg))
  496. return -EINVAL;
  497. if (test_opt(sbi, RESERVE_ROOT)) {
  498. f2fs_msg(sb, KERN_INFO,
  499. "Preserve previous reserve_root=%u",
  500. F2FS_OPTION(sbi).root_reserved_blocks);
  501. } else {
  502. F2FS_OPTION(sbi).root_reserved_blocks = arg;
  503. set_opt(sbi, RESERVE_ROOT);
  504. }
  505. break;
  506. case Opt_resuid:
  507. if (args->from && match_int(args, &arg))
  508. return -EINVAL;
  509. uid = make_kuid(current_user_ns(), arg);
  510. if (!uid_valid(uid)) {
  511. f2fs_msg(sb, KERN_ERR,
  512. "Invalid uid value %d", arg);
  513. return -EINVAL;
  514. }
  515. F2FS_OPTION(sbi).s_resuid = uid;
  516. break;
  517. case Opt_resgid:
  518. if (args->from && match_int(args, &arg))
  519. return -EINVAL;
  520. gid = make_kgid(current_user_ns(), arg);
  521. if (!gid_valid(gid)) {
  522. f2fs_msg(sb, KERN_ERR,
  523. "Invalid gid value %d", arg);
  524. return -EINVAL;
  525. }
  526. F2FS_OPTION(sbi).s_resgid = gid;
  527. break;
  528. case Opt_mode:
  529. name = match_strdup(&args[0]);
  530. if (!name)
  531. return -ENOMEM;
  532. if (strlen(name) == 8 &&
  533. !strncmp(name, "adaptive", 8)) {
  534. if (f2fs_sb_has_blkzoned(sb)) {
  535. f2fs_msg(sb, KERN_WARNING,
  536. "adaptive mode is not allowed with "
  537. "zoned block device feature");
  538. kfree(name);
  539. return -EINVAL;
  540. }
  541. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  542. } else if (strlen(name) == 3 &&
  543. !strncmp(name, "lfs", 3)) {
  544. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  545. } else {
  546. kfree(name);
  547. return -EINVAL;
  548. }
  549. kfree(name);
  550. break;
  551. case Opt_io_size_bits:
  552. if (args->from && match_int(args, &arg))
  553. return -EINVAL;
  554. if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
  555. f2fs_msg(sb, KERN_WARNING,
  556. "Not support %d, larger than %d",
  557. 1 << arg, BIO_MAX_PAGES);
  558. return -EINVAL;
  559. }
  560. F2FS_OPTION(sbi).write_io_size_bits = arg;
  561. break;
  562. case Opt_fault_injection:
  563. if (args->from && match_int(args, &arg))
  564. return -EINVAL;
  565. #ifdef CONFIG_F2FS_FAULT_INJECTION
  566. f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
  567. set_opt(sbi, FAULT_INJECTION);
  568. #else
  569. f2fs_msg(sb, KERN_INFO,
  570. "FAULT_INJECTION was not selected");
  571. #endif
  572. break;
  573. case Opt_fault_type:
  574. if (args->from && match_int(args, &arg))
  575. return -EINVAL;
  576. #ifdef CONFIG_F2FS_FAULT_INJECTION
  577. f2fs_build_fault_attr(sbi, 0, arg);
  578. set_opt(sbi, FAULT_INJECTION);
  579. #else
  580. f2fs_msg(sb, KERN_INFO,
  581. "FAULT_INJECTION was not selected");
  582. #endif
  583. break;
  584. case Opt_lazytime:
  585. sb->s_flags |= SB_LAZYTIME;
  586. break;
  587. case Opt_nolazytime:
  588. sb->s_flags &= ~SB_LAZYTIME;
  589. break;
  590. #ifdef CONFIG_QUOTA
  591. case Opt_quota:
  592. case Opt_usrquota:
  593. set_opt(sbi, USRQUOTA);
  594. break;
  595. case Opt_grpquota:
  596. set_opt(sbi, GRPQUOTA);
  597. break;
  598. case Opt_prjquota:
  599. set_opt(sbi, PRJQUOTA);
  600. break;
  601. case Opt_usrjquota:
  602. ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
  603. if (ret)
  604. return ret;
  605. break;
  606. case Opt_grpjquota:
  607. ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
  608. if (ret)
  609. return ret;
  610. break;
  611. case Opt_prjjquota:
  612. ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
  613. if (ret)
  614. return ret;
  615. break;
  616. case Opt_offusrjquota:
  617. ret = f2fs_clear_qf_name(sb, USRQUOTA);
  618. if (ret)
  619. return ret;
  620. break;
  621. case Opt_offgrpjquota:
  622. ret = f2fs_clear_qf_name(sb, GRPQUOTA);
  623. if (ret)
  624. return ret;
  625. break;
  626. case Opt_offprjjquota:
  627. ret = f2fs_clear_qf_name(sb, PRJQUOTA);
  628. if (ret)
  629. return ret;
  630. break;
  631. case Opt_jqfmt_vfsold:
  632. F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
  633. break;
  634. case Opt_jqfmt_vfsv0:
  635. F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
  636. break;
  637. case Opt_jqfmt_vfsv1:
  638. F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
  639. break;
  640. case Opt_noquota:
  641. clear_opt(sbi, QUOTA);
  642. clear_opt(sbi, USRQUOTA);
  643. clear_opt(sbi, GRPQUOTA);
  644. clear_opt(sbi, PRJQUOTA);
  645. break;
  646. #else
  647. case Opt_quota:
  648. case Opt_usrquota:
  649. case Opt_grpquota:
  650. case Opt_prjquota:
  651. case Opt_usrjquota:
  652. case Opt_grpjquota:
  653. case Opt_prjjquota:
  654. case Opt_offusrjquota:
  655. case Opt_offgrpjquota:
  656. case Opt_offprjjquota:
  657. case Opt_jqfmt_vfsold:
  658. case Opt_jqfmt_vfsv0:
  659. case Opt_jqfmt_vfsv1:
  660. case Opt_noquota:
  661. f2fs_msg(sb, KERN_INFO,
  662. "quota operations not supported");
  663. break;
  664. #endif
  665. case Opt_whint:
  666. name = match_strdup(&args[0]);
  667. if (!name)
  668. return -ENOMEM;
  669. if (strlen(name) == 10 &&
  670. !strncmp(name, "user-based", 10)) {
  671. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
  672. } else if (strlen(name) == 3 &&
  673. !strncmp(name, "off", 3)) {
  674. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
  675. } else if (strlen(name) == 8 &&
  676. !strncmp(name, "fs-based", 8)) {
  677. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
  678. } else {
  679. kfree(name);
  680. return -EINVAL;
  681. }
  682. kfree(name);
  683. break;
  684. case Opt_alloc:
  685. name = match_strdup(&args[0]);
  686. if (!name)
  687. return -ENOMEM;
  688. if (strlen(name) == 7 &&
  689. !strncmp(name, "default", 7)) {
  690. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
  691. } else if (strlen(name) == 5 &&
  692. !strncmp(name, "reuse", 5)) {
  693. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
  694. } else {
  695. kfree(name);
  696. return -EINVAL;
  697. }
  698. kfree(name);
  699. break;
  700. case Opt_fsync:
  701. name = match_strdup(&args[0]);
  702. if (!name)
  703. return -ENOMEM;
  704. if (strlen(name) == 5 &&
  705. !strncmp(name, "posix", 5)) {
  706. F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
  707. } else if (strlen(name) == 6 &&
  708. !strncmp(name, "strict", 6)) {
  709. F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
  710. } else if (strlen(name) == 9 &&
  711. !strncmp(name, "nobarrier", 9)) {
  712. F2FS_OPTION(sbi).fsync_mode =
  713. FSYNC_MODE_NOBARRIER;
  714. } else {
  715. kfree(name);
  716. return -EINVAL;
  717. }
  718. kfree(name);
  719. break;
  720. case Opt_test_dummy_encryption:
  721. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  722. if (!f2fs_sb_has_encrypt(sb)) {
  723. f2fs_msg(sb, KERN_ERR, "Encrypt feature is off");
  724. return -EINVAL;
  725. }
  726. F2FS_OPTION(sbi).test_dummy_encryption = true;
  727. f2fs_msg(sb, KERN_INFO,
  728. "Test dummy encryption mode enabled");
  729. #else
  730. f2fs_msg(sb, KERN_INFO,
  731. "Test dummy encryption mount option ignored");
  732. #endif
  733. break;
  734. default:
  735. f2fs_msg(sb, KERN_ERR,
  736. "Unrecognized mount option \"%s\" or missing value",
  737. p);
  738. return -EINVAL;
  739. }
  740. }
  741. #ifdef CONFIG_QUOTA
  742. if (f2fs_check_quota_options(sbi))
  743. return -EINVAL;
  744. #else
  745. if (f2fs_sb_has_quota_ino(sbi->sb) && !f2fs_readonly(sbi->sb)) {
  746. f2fs_msg(sbi->sb, KERN_INFO,
  747. "Filesystem with quota feature cannot be mounted RDWR "
  748. "without CONFIG_QUOTA");
  749. return -EINVAL;
  750. }
  751. if (f2fs_sb_has_project_quota(sbi->sb) && !f2fs_readonly(sbi->sb)) {
  752. f2fs_msg(sb, KERN_ERR,
  753. "Filesystem with project quota feature cannot be "
  754. "mounted RDWR without CONFIG_QUOTA");
  755. return -EINVAL;
  756. }
  757. #endif
  758. if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
  759. f2fs_msg(sb, KERN_ERR,
  760. "Should set mode=lfs with %uKB-sized IO",
  761. F2FS_IO_SIZE_KB(sbi));
  762. return -EINVAL;
  763. }
  764. if (test_opt(sbi, INLINE_XATTR_SIZE)) {
  765. if (!f2fs_sb_has_extra_attr(sb) ||
  766. !f2fs_sb_has_flexible_inline_xattr(sb)) {
  767. f2fs_msg(sb, KERN_ERR,
  768. "extra_attr or flexible_inline_xattr "
  769. "feature is off");
  770. return -EINVAL;
  771. }
  772. if (!test_opt(sbi, INLINE_XATTR)) {
  773. f2fs_msg(sb, KERN_ERR,
  774. "inline_xattr_size option should be "
  775. "set with inline_xattr option");
  776. return -EINVAL;
  777. }
  778. if (F2FS_OPTION(sbi).inline_xattr_size <
  779. sizeof(struct f2fs_xattr_header) / sizeof(__le32) ||
  780. F2FS_OPTION(sbi).inline_xattr_size >
  781. DEF_ADDRS_PER_INODE -
  782. F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -
  783. DEF_INLINE_RESERVED_SIZE -
  784. MIN_INLINE_DENTRY_SIZE / sizeof(__le32)) {
  785. f2fs_msg(sb, KERN_ERR,
  786. "inline xattr size is out of range");
  787. return -EINVAL;
  788. }
  789. }
  790. /* Not pass down write hints if the number of active logs is lesser
  791. * than NR_CURSEG_TYPE.
  792. */
  793. if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
  794. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
  795. return 0;
  796. }
  797. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  798. {
  799. struct f2fs_inode_info *fi;
  800. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  801. if (!fi)
  802. return NULL;
  803. init_once((void *) fi);
  804. /* Initialize f2fs-specific inode info */
  805. atomic_set(&fi->dirty_pages, 0);
  806. init_rwsem(&fi->i_sem);
  807. INIT_LIST_HEAD(&fi->dirty_list);
  808. INIT_LIST_HEAD(&fi->gdirty_list);
  809. INIT_LIST_HEAD(&fi->inmem_ilist);
  810. INIT_LIST_HEAD(&fi->inmem_pages);
  811. mutex_init(&fi->inmem_lock);
  812. init_rwsem(&fi->i_gc_rwsem[READ]);
  813. init_rwsem(&fi->i_gc_rwsem[WRITE]);
  814. init_rwsem(&fi->i_mmap_sem);
  815. init_rwsem(&fi->i_xattr_sem);
  816. /* Will be used by directory only */
  817. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  818. return &fi->vfs_inode;
  819. }
  820. static int f2fs_drop_inode(struct inode *inode)
  821. {
  822. int ret;
  823. /*
  824. * This is to avoid a deadlock condition like below.
  825. * writeback_single_inode(inode)
  826. * - f2fs_write_data_page
  827. * - f2fs_gc -> iput -> evict
  828. * - inode_wait_for_writeback(inode)
  829. */
  830. if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
  831. if (!inode->i_nlink && !is_bad_inode(inode)) {
  832. /* to avoid evict_inode call simultaneously */
  833. atomic_inc(&inode->i_count);
  834. spin_unlock(&inode->i_lock);
  835. /* some remained atomic pages should discarded */
  836. if (f2fs_is_atomic_file(inode))
  837. f2fs_drop_inmem_pages(inode);
  838. /* should remain fi->extent_tree for writepage */
  839. f2fs_destroy_extent_node(inode);
  840. sb_start_intwrite(inode->i_sb);
  841. f2fs_i_size_write(inode, 0);
  842. if (F2FS_HAS_BLOCKS(inode))
  843. f2fs_truncate(inode);
  844. sb_end_intwrite(inode->i_sb);
  845. spin_lock(&inode->i_lock);
  846. atomic_dec(&inode->i_count);
  847. }
  848. trace_f2fs_drop_inode(inode, 0);
  849. return 0;
  850. }
  851. ret = generic_drop_inode(inode);
  852. trace_f2fs_drop_inode(inode, ret);
  853. return ret;
  854. }
  855. int f2fs_inode_dirtied(struct inode *inode, bool sync)
  856. {
  857. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  858. int ret = 0;
  859. spin_lock(&sbi->inode_lock[DIRTY_META]);
  860. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  861. ret = 1;
  862. } else {
  863. set_inode_flag(inode, FI_DIRTY_INODE);
  864. stat_inc_dirty_inode(sbi, DIRTY_META);
  865. }
  866. if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
  867. list_add_tail(&F2FS_I(inode)->gdirty_list,
  868. &sbi->inode_list[DIRTY_META]);
  869. inc_page_count(sbi, F2FS_DIRTY_IMETA);
  870. }
  871. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  872. return ret;
  873. }
  874. void f2fs_inode_synced(struct inode *inode)
  875. {
  876. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  877. spin_lock(&sbi->inode_lock[DIRTY_META]);
  878. if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  879. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  880. return;
  881. }
  882. if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
  883. list_del_init(&F2FS_I(inode)->gdirty_list);
  884. dec_page_count(sbi, F2FS_DIRTY_IMETA);
  885. }
  886. clear_inode_flag(inode, FI_DIRTY_INODE);
  887. clear_inode_flag(inode, FI_AUTO_RECOVER);
  888. stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
  889. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  890. }
  891. /*
  892. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  893. *
  894. * We should call set_dirty_inode to write the dirty inode through write_inode.
  895. */
  896. static void f2fs_dirty_inode(struct inode *inode, int flags)
  897. {
  898. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  899. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  900. inode->i_ino == F2FS_META_INO(sbi))
  901. return;
  902. if (flags == I_DIRTY_TIME)
  903. return;
  904. if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
  905. clear_inode_flag(inode, FI_AUTO_RECOVER);
  906. f2fs_inode_dirtied(inode, false);
  907. }
  908. static void f2fs_i_callback(struct rcu_head *head)
  909. {
  910. struct inode *inode = container_of(head, struct inode, i_rcu);
  911. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  912. }
  913. static void f2fs_destroy_inode(struct inode *inode)
  914. {
  915. call_rcu(&inode->i_rcu, f2fs_i_callback);
  916. }
  917. static void destroy_percpu_info(struct f2fs_sb_info *sbi)
  918. {
  919. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  920. percpu_counter_destroy(&sbi->total_valid_inode_count);
  921. }
  922. static void destroy_device_list(struct f2fs_sb_info *sbi)
  923. {
  924. int i;
  925. for (i = 0; i < sbi->s_ndevs; i++) {
  926. blkdev_put(FDEV(i).bdev, FMODE_EXCL);
  927. #ifdef CONFIG_BLK_DEV_ZONED
  928. kfree(FDEV(i).blkz_type);
  929. #endif
  930. }
  931. kfree(sbi->devs);
  932. }
  933. static void f2fs_put_super(struct super_block *sb)
  934. {
  935. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  936. int i;
  937. bool dropped;
  938. f2fs_quota_off_umount(sb);
  939. /* prevent remaining shrinker jobs */
  940. mutex_lock(&sbi->umount_mutex);
  941. /*
  942. * We don't need to do checkpoint when superblock is clean.
  943. * But, the previous checkpoint was not done by umount, it needs to do
  944. * clean checkpoint again.
  945. */
  946. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  947. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  948. struct cp_control cpc = {
  949. .reason = CP_UMOUNT,
  950. };
  951. f2fs_write_checkpoint(sbi, &cpc);
  952. }
  953. /* be sure to wait for any on-going discard commands */
  954. dropped = f2fs_wait_discard_bios(sbi);
  955. if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
  956. !sbi->discard_blks && !dropped) {
  957. struct cp_control cpc = {
  958. .reason = CP_UMOUNT | CP_TRIMMED,
  959. };
  960. f2fs_write_checkpoint(sbi, &cpc);
  961. }
  962. /*
  963. * normally superblock is clean, so we need to release this.
  964. * In addition, EIO will skip do checkpoint, we need this as well.
  965. */
  966. f2fs_release_ino_entry(sbi, true);
  967. f2fs_leave_shrinker(sbi);
  968. mutex_unlock(&sbi->umount_mutex);
  969. /* our cp_error case, we can wait for any writeback page */
  970. f2fs_flush_merged_writes(sbi);
  971. f2fs_wait_on_all_pages_writeback(sbi);
  972. f2fs_bug_on(sbi, sbi->fsync_node_num);
  973. iput(sbi->node_inode);
  974. sbi->node_inode = NULL;
  975. iput(sbi->meta_inode);
  976. sbi->meta_inode = NULL;
  977. /*
  978. * iput() can update stat information, if f2fs_write_checkpoint()
  979. * above failed with error.
  980. */
  981. f2fs_destroy_stats(sbi);
  982. /* destroy f2fs internal modules */
  983. f2fs_destroy_node_manager(sbi);
  984. f2fs_destroy_segment_manager(sbi);
  985. kfree(sbi->ckpt);
  986. f2fs_unregister_sysfs(sbi);
  987. sb->s_fs_info = NULL;
  988. if (sbi->s_chksum_driver)
  989. crypto_free_shash(sbi->s_chksum_driver);
  990. kfree(sbi->raw_super);
  991. destroy_device_list(sbi);
  992. mempool_destroy(sbi->write_io_dummy);
  993. #ifdef CONFIG_QUOTA
  994. for (i = 0; i < MAXQUOTAS; i++)
  995. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  996. #endif
  997. destroy_percpu_info(sbi);
  998. for (i = 0; i < NR_PAGE_TYPE; i++)
  999. kfree(sbi->write_io[i]);
  1000. kfree(sbi);
  1001. }
  1002. int f2fs_sync_fs(struct super_block *sb, int sync)
  1003. {
  1004. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1005. int err = 0;
  1006. if (unlikely(f2fs_cp_error(sbi)))
  1007. return 0;
  1008. trace_f2fs_sync_fs(sb, sync);
  1009. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1010. return -EAGAIN;
  1011. if (sync) {
  1012. struct cp_control cpc;
  1013. cpc.reason = __get_cp_reason(sbi);
  1014. mutex_lock(&sbi->gc_mutex);
  1015. err = f2fs_write_checkpoint(sbi, &cpc);
  1016. mutex_unlock(&sbi->gc_mutex);
  1017. }
  1018. f2fs_trace_ios(NULL, 1);
  1019. return err;
  1020. }
  1021. static int f2fs_freeze(struct super_block *sb)
  1022. {
  1023. if (f2fs_readonly(sb))
  1024. return 0;
  1025. /* IO error happened before */
  1026. if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
  1027. return -EIO;
  1028. /* must be clean, since sync_filesystem() was already called */
  1029. if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
  1030. return -EINVAL;
  1031. return 0;
  1032. }
  1033. static int f2fs_unfreeze(struct super_block *sb)
  1034. {
  1035. return 0;
  1036. }
  1037. #ifdef CONFIG_QUOTA
  1038. static int f2fs_statfs_project(struct super_block *sb,
  1039. kprojid_t projid, struct kstatfs *buf)
  1040. {
  1041. struct kqid qid;
  1042. struct dquot *dquot;
  1043. u64 limit;
  1044. u64 curblock;
  1045. qid = make_kqid_projid(projid);
  1046. dquot = dqget(sb, qid);
  1047. if (IS_ERR(dquot))
  1048. return PTR_ERR(dquot);
  1049. spin_lock(&dquot->dq_dqb_lock);
  1050. limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
  1051. dquot->dq_dqb.dqb_bhardlimit);
  1052. if (limit)
  1053. limit >>= sb->s_blocksize_bits;
  1054. if (limit && buf->f_blocks > limit) {
  1055. curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
  1056. buf->f_blocks = limit;
  1057. buf->f_bfree = buf->f_bavail =
  1058. (buf->f_blocks > curblock) ?
  1059. (buf->f_blocks - curblock) : 0;
  1060. }
  1061. limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
  1062. dquot->dq_dqb.dqb_ihardlimit);
  1063. if (limit && buf->f_files > limit) {
  1064. buf->f_files = limit;
  1065. buf->f_ffree =
  1066. (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
  1067. (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
  1068. }
  1069. spin_unlock(&dquot->dq_dqb_lock);
  1070. dqput(dquot);
  1071. return 0;
  1072. }
  1073. #endif
  1074. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1075. {
  1076. struct super_block *sb = dentry->d_sb;
  1077. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1078. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1079. block_t total_count, user_block_count, start_count;
  1080. u64 avail_node_count;
  1081. total_count = le64_to_cpu(sbi->raw_super->block_count);
  1082. user_block_count = sbi->user_block_count;
  1083. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  1084. buf->f_type = F2FS_SUPER_MAGIC;
  1085. buf->f_bsize = sbi->blocksize;
  1086. buf->f_blocks = total_count - start_count;
  1087. buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
  1088. sbi->current_reserved_blocks;
  1089. if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
  1090. buf->f_bavail = buf->f_bfree -
  1091. F2FS_OPTION(sbi).root_reserved_blocks;
  1092. else
  1093. buf->f_bavail = 0;
  1094. avail_node_count = sbi->total_node_count - sbi->nquota_files -
  1095. F2FS_RESERVED_NODE_NUM;
  1096. if (avail_node_count > user_block_count) {
  1097. buf->f_files = user_block_count;
  1098. buf->f_ffree = buf->f_bavail;
  1099. } else {
  1100. buf->f_files = avail_node_count;
  1101. buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
  1102. buf->f_bavail);
  1103. }
  1104. buf->f_namelen = F2FS_NAME_LEN;
  1105. buf->f_fsid.val[0] = (u32)id;
  1106. buf->f_fsid.val[1] = (u32)(id >> 32);
  1107. #ifdef CONFIG_QUOTA
  1108. if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
  1109. sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
  1110. f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
  1111. }
  1112. #endif
  1113. return 0;
  1114. }
  1115. static inline void f2fs_show_quota_options(struct seq_file *seq,
  1116. struct super_block *sb)
  1117. {
  1118. #ifdef CONFIG_QUOTA
  1119. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1120. if (F2FS_OPTION(sbi).s_jquota_fmt) {
  1121. char *fmtname = "";
  1122. switch (F2FS_OPTION(sbi).s_jquota_fmt) {
  1123. case QFMT_VFS_OLD:
  1124. fmtname = "vfsold";
  1125. break;
  1126. case QFMT_VFS_V0:
  1127. fmtname = "vfsv0";
  1128. break;
  1129. case QFMT_VFS_V1:
  1130. fmtname = "vfsv1";
  1131. break;
  1132. }
  1133. seq_printf(seq, ",jqfmt=%s", fmtname);
  1134. }
  1135. if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
  1136. seq_show_option(seq, "usrjquota",
  1137. F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
  1138. if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
  1139. seq_show_option(seq, "grpjquota",
  1140. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
  1141. if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
  1142. seq_show_option(seq, "prjjquota",
  1143. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
  1144. #endif
  1145. }
  1146. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  1147. {
  1148. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  1149. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
  1150. if (test_opt(sbi, FORCE_FG_GC))
  1151. seq_printf(seq, ",background_gc=%s", "sync");
  1152. else
  1153. seq_printf(seq, ",background_gc=%s", "on");
  1154. } else {
  1155. seq_printf(seq, ",background_gc=%s", "off");
  1156. }
  1157. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  1158. seq_puts(seq, ",disable_roll_forward");
  1159. if (test_opt(sbi, DISCARD))
  1160. seq_puts(seq, ",discard");
  1161. if (test_opt(sbi, NOHEAP))
  1162. seq_puts(seq, ",no_heap");
  1163. else
  1164. seq_puts(seq, ",heap");
  1165. #ifdef CONFIG_F2FS_FS_XATTR
  1166. if (test_opt(sbi, XATTR_USER))
  1167. seq_puts(seq, ",user_xattr");
  1168. else
  1169. seq_puts(seq, ",nouser_xattr");
  1170. if (test_opt(sbi, INLINE_XATTR))
  1171. seq_puts(seq, ",inline_xattr");
  1172. else
  1173. seq_puts(seq, ",noinline_xattr");
  1174. if (test_opt(sbi, INLINE_XATTR_SIZE))
  1175. seq_printf(seq, ",inline_xattr_size=%u",
  1176. F2FS_OPTION(sbi).inline_xattr_size);
  1177. #endif
  1178. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  1179. if (test_opt(sbi, POSIX_ACL))
  1180. seq_puts(seq, ",acl");
  1181. else
  1182. seq_puts(seq, ",noacl");
  1183. #endif
  1184. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  1185. seq_puts(seq, ",disable_ext_identify");
  1186. if (test_opt(sbi, INLINE_DATA))
  1187. seq_puts(seq, ",inline_data");
  1188. else
  1189. seq_puts(seq, ",noinline_data");
  1190. if (test_opt(sbi, INLINE_DENTRY))
  1191. seq_puts(seq, ",inline_dentry");
  1192. else
  1193. seq_puts(seq, ",noinline_dentry");
  1194. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  1195. seq_puts(seq, ",flush_merge");
  1196. if (test_opt(sbi, NOBARRIER))
  1197. seq_puts(seq, ",nobarrier");
  1198. if (test_opt(sbi, FASTBOOT))
  1199. seq_puts(seq, ",fastboot");
  1200. if (test_opt(sbi, EXTENT_CACHE))
  1201. seq_puts(seq, ",extent_cache");
  1202. else
  1203. seq_puts(seq, ",noextent_cache");
  1204. if (test_opt(sbi, DATA_FLUSH))
  1205. seq_puts(seq, ",data_flush");
  1206. seq_puts(seq, ",mode=");
  1207. if (test_opt(sbi, ADAPTIVE))
  1208. seq_puts(seq, "adaptive");
  1209. else if (test_opt(sbi, LFS))
  1210. seq_puts(seq, "lfs");
  1211. seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
  1212. if (test_opt(sbi, RESERVE_ROOT))
  1213. seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
  1214. F2FS_OPTION(sbi).root_reserved_blocks,
  1215. from_kuid_munged(&init_user_ns,
  1216. F2FS_OPTION(sbi).s_resuid),
  1217. from_kgid_munged(&init_user_ns,
  1218. F2FS_OPTION(sbi).s_resgid));
  1219. if (F2FS_IO_SIZE_BITS(sbi))
  1220. seq_printf(seq, ",io_bits=%u",
  1221. F2FS_OPTION(sbi).write_io_size_bits);
  1222. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1223. if (test_opt(sbi, FAULT_INJECTION)) {
  1224. seq_printf(seq, ",fault_injection=%u",
  1225. F2FS_OPTION(sbi).fault_info.inject_rate);
  1226. seq_printf(seq, ",fault_type=%u",
  1227. F2FS_OPTION(sbi).fault_info.inject_type);
  1228. }
  1229. #endif
  1230. #ifdef CONFIG_QUOTA
  1231. if (test_opt(sbi, QUOTA))
  1232. seq_puts(seq, ",quota");
  1233. if (test_opt(sbi, USRQUOTA))
  1234. seq_puts(seq, ",usrquota");
  1235. if (test_opt(sbi, GRPQUOTA))
  1236. seq_puts(seq, ",grpquota");
  1237. if (test_opt(sbi, PRJQUOTA))
  1238. seq_puts(seq, ",prjquota");
  1239. #endif
  1240. f2fs_show_quota_options(seq, sbi->sb);
  1241. if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
  1242. seq_printf(seq, ",whint_mode=%s", "user-based");
  1243. else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
  1244. seq_printf(seq, ",whint_mode=%s", "fs-based");
  1245. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1246. if (F2FS_OPTION(sbi).test_dummy_encryption)
  1247. seq_puts(seq, ",test_dummy_encryption");
  1248. #endif
  1249. if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
  1250. seq_printf(seq, ",alloc_mode=%s", "default");
  1251. else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
  1252. seq_printf(seq, ",alloc_mode=%s", "reuse");
  1253. if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
  1254. seq_printf(seq, ",fsync_mode=%s", "posix");
  1255. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
  1256. seq_printf(seq, ",fsync_mode=%s", "strict");
  1257. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
  1258. seq_printf(seq, ",fsync_mode=%s", "nobarrier");
  1259. return 0;
  1260. }
  1261. static void default_options(struct f2fs_sb_info *sbi)
  1262. {
  1263. /* init some FS parameters */
  1264. F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
  1265. F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
  1266. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
  1267. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
  1268. F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
  1269. F2FS_OPTION(sbi).test_dummy_encryption = false;
  1270. F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
  1271. F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
  1272. set_opt(sbi, BG_GC);
  1273. set_opt(sbi, INLINE_XATTR);
  1274. set_opt(sbi, INLINE_DATA);
  1275. set_opt(sbi, INLINE_DENTRY);
  1276. set_opt(sbi, EXTENT_CACHE);
  1277. set_opt(sbi, NOHEAP);
  1278. sbi->sb->s_flags |= SB_LAZYTIME;
  1279. set_opt(sbi, FLUSH_MERGE);
  1280. set_opt(sbi, DISCARD);
  1281. if (f2fs_sb_has_blkzoned(sbi->sb))
  1282. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  1283. else
  1284. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  1285. #ifdef CONFIG_F2FS_FS_XATTR
  1286. set_opt(sbi, XATTR_USER);
  1287. #endif
  1288. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  1289. set_opt(sbi, POSIX_ACL);
  1290. #endif
  1291. f2fs_build_fault_attr(sbi, 0, 0);
  1292. }
  1293. #ifdef CONFIG_QUOTA
  1294. static int f2fs_enable_quotas(struct super_block *sb);
  1295. #endif
  1296. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  1297. {
  1298. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1299. struct f2fs_mount_info org_mount_opt;
  1300. unsigned long old_sb_flags;
  1301. int err;
  1302. bool need_restart_gc = false;
  1303. bool need_stop_gc = false;
  1304. bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
  1305. #ifdef CONFIG_QUOTA
  1306. int i, j;
  1307. #endif
  1308. /*
  1309. * Save the old mount options in case we
  1310. * need to restore them.
  1311. */
  1312. org_mount_opt = sbi->mount_opt;
  1313. old_sb_flags = sb->s_flags;
  1314. #ifdef CONFIG_QUOTA
  1315. org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
  1316. for (i = 0; i < MAXQUOTAS; i++) {
  1317. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  1318. org_mount_opt.s_qf_names[i] =
  1319. kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
  1320. GFP_KERNEL);
  1321. if (!org_mount_opt.s_qf_names[i]) {
  1322. for (j = 0; j < i; j++)
  1323. kfree(org_mount_opt.s_qf_names[j]);
  1324. return -ENOMEM;
  1325. }
  1326. } else {
  1327. org_mount_opt.s_qf_names[i] = NULL;
  1328. }
  1329. }
  1330. #endif
  1331. /* recover superblocks we couldn't write due to previous RO mount */
  1332. if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  1333. err = f2fs_commit_super(sbi, false);
  1334. f2fs_msg(sb, KERN_INFO,
  1335. "Try to recover all the superblocks, ret: %d", err);
  1336. if (!err)
  1337. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1338. }
  1339. default_options(sbi);
  1340. /* parse mount options */
  1341. err = parse_options(sb, data);
  1342. if (err)
  1343. goto restore_opts;
  1344. /*
  1345. * Previous and new state of filesystem is RO,
  1346. * so skip checking GC and FLUSH_MERGE conditions.
  1347. */
  1348. if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
  1349. goto skip;
  1350. #ifdef CONFIG_QUOTA
  1351. if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
  1352. err = dquot_suspend(sb, -1);
  1353. if (err < 0)
  1354. goto restore_opts;
  1355. } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
  1356. /* dquot_resume needs RW */
  1357. sb->s_flags &= ~SB_RDONLY;
  1358. if (sb_any_quota_suspended(sb)) {
  1359. dquot_resume(sb, -1);
  1360. } else if (f2fs_sb_has_quota_ino(sb)) {
  1361. err = f2fs_enable_quotas(sb);
  1362. if (err)
  1363. goto restore_opts;
  1364. }
  1365. }
  1366. #endif
  1367. /* disallow enable/disable extent_cache dynamically */
  1368. if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
  1369. err = -EINVAL;
  1370. f2fs_msg(sbi->sb, KERN_WARNING,
  1371. "switch extent_cache option is not allowed");
  1372. goto restore_opts;
  1373. }
  1374. /*
  1375. * We stop the GC thread if FS is mounted as RO
  1376. * or if background_gc = off is passed in mount
  1377. * option. Also sync the filesystem.
  1378. */
  1379. if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
  1380. if (sbi->gc_thread) {
  1381. f2fs_stop_gc_thread(sbi);
  1382. need_restart_gc = true;
  1383. }
  1384. } else if (!sbi->gc_thread) {
  1385. err = f2fs_start_gc_thread(sbi);
  1386. if (err)
  1387. goto restore_opts;
  1388. need_stop_gc = true;
  1389. }
  1390. if (*flags & SB_RDONLY ||
  1391. F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
  1392. writeback_inodes_sb(sb, WB_REASON_SYNC);
  1393. sync_inodes_sb(sb);
  1394. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1395. set_sbi_flag(sbi, SBI_IS_CLOSE);
  1396. f2fs_sync_fs(sb, 1);
  1397. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  1398. }
  1399. /*
  1400. * We stop issue flush thread if FS is mounted as RO
  1401. * or if flush_merge is not passed in mount option.
  1402. */
  1403. if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  1404. clear_opt(sbi, FLUSH_MERGE);
  1405. f2fs_destroy_flush_cmd_control(sbi, false);
  1406. } else {
  1407. err = f2fs_create_flush_cmd_control(sbi);
  1408. if (err)
  1409. goto restore_gc;
  1410. }
  1411. skip:
  1412. #ifdef CONFIG_QUOTA
  1413. /* Release old quota file names */
  1414. for (i = 0; i < MAXQUOTAS; i++)
  1415. kfree(org_mount_opt.s_qf_names[i]);
  1416. #endif
  1417. /* Update the POSIXACL Flag */
  1418. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  1419. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  1420. limit_reserve_root(sbi);
  1421. *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
  1422. return 0;
  1423. restore_gc:
  1424. if (need_restart_gc) {
  1425. if (f2fs_start_gc_thread(sbi))
  1426. f2fs_msg(sbi->sb, KERN_WARNING,
  1427. "background gc thread has stopped");
  1428. } else if (need_stop_gc) {
  1429. f2fs_stop_gc_thread(sbi);
  1430. }
  1431. restore_opts:
  1432. #ifdef CONFIG_QUOTA
  1433. F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
  1434. for (i = 0; i < MAXQUOTAS; i++) {
  1435. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  1436. F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
  1437. }
  1438. #endif
  1439. sbi->mount_opt = org_mount_opt;
  1440. sb->s_flags = old_sb_flags;
  1441. return err;
  1442. }
  1443. #ifdef CONFIG_QUOTA
  1444. /* Read data from quotafile */
  1445. static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
  1446. size_t len, loff_t off)
  1447. {
  1448. struct inode *inode = sb_dqopt(sb)->files[type];
  1449. struct address_space *mapping = inode->i_mapping;
  1450. block_t blkidx = F2FS_BYTES_TO_BLK(off);
  1451. int offset = off & (sb->s_blocksize - 1);
  1452. int tocopy;
  1453. size_t toread;
  1454. loff_t i_size = i_size_read(inode);
  1455. struct page *page;
  1456. char *kaddr;
  1457. if (off > i_size)
  1458. return 0;
  1459. if (off + len > i_size)
  1460. len = i_size - off;
  1461. toread = len;
  1462. while (toread > 0) {
  1463. tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
  1464. repeat:
  1465. page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
  1466. if (IS_ERR(page)) {
  1467. if (PTR_ERR(page) == -ENOMEM) {
  1468. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1469. goto repeat;
  1470. }
  1471. return PTR_ERR(page);
  1472. }
  1473. lock_page(page);
  1474. if (unlikely(page->mapping != mapping)) {
  1475. f2fs_put_page(page, 1);
  1476. goto repeat;
  1477. }
  1478. if (unlikely(!PageUptodate(page))) {
  1479. f2fs_put_page(page, 1);
  1480. return -EIO;
  1481. }
  1482. kaddr = kmap_atomic(page);
  1483. memcpy(data, kaddr + offset, tocopy);
  1484. kunmap_atomic(kaddr);
  1485. f2fs_put_page(page, 1);
  1486. offset = 0;
  1487. toread -= tocopy;
  1488. data += tocopy;
  1489. blkidx++;
  1490. }
  1491. return len;
  1492. }
  1493. /* Write to quotafile */
  1494. static ssize_t f2fs_quota_write(struct super_block *sb, int type,
  1495. const char *data, size_t len, loff_t off)
  1496. {
  1497. struct inode *inode = sb_dqopt(sb)->files[type];
  1498. struct address_space *mapping = inode->i_mapping;
  1499. const struct address_space_operations *a_ops = mapping->a_ops;
  1500. int offset = off & (sb->s_blocksize - 1);
  1501. size_t towrite = len;
  1502. struct page *page;
  1503. char *kaddr;
  1504. int err = 0;
  1505. int tocopy;
  1506. while (towrite > 0) {
  1507. tocopy = min_t(unsigned long, sb->s_blocksize - offset,
  1508. towrite);
  1509. retry:
  1510. err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
  1511. &page, NULL);
  1512. if (unlikely(err)) {
  1513. if (err == -ENOMEM) {
  1514. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1515. goto retry;
  1516. }
  1517. break;
  1518. }
  1519. kaddr = kmap_atomic(page);
  1520. memcpy(kaddr + offset, data, tocopy);
  1521. kunmap_atomic(kaddr);
  1522. flush_dcache_page(page);
  1523. a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
  1524. page, NULL);
  1525. offset = 0;
  1526. towrite -= tocopy;
  1527. off += tocopy;
  1528. data += tocopy;
  1529. cond_resched();
  1530. }
  1531. if (len == towrite)
  1532. return err;
  1533. inode->i_mtime = inode->i_ctime = current_time(inode);
  1534. f2fs_mark_inode_dirty_sync(inode, false);
  1535. return len - towrite;
  1536. }
  1537. static struct dquot **f2fs_get_dquots(struct inode *inode)
  1538. {
  1539. return F2FS_I(inode)->i_dquot;
  1540. }
  1541. static qsize_t *f2fs_get_reserved_space(struct inode *inode)
  1542. {
  1543. return &F2FS_I(inode)->i_reserved_quota;
  1544. }
  1545. static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
  1546. {
  1547. return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
  1548. F2FS_OPTION(sbi).s_jquota_fmt, type);
  1549. }
  1550. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
  1551. {
  1552. int enabled = 0;
  1553. int i, err;
  1554. if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
  1555. err = f2fs_enable_quotas(sbi->sb);
  1556. if (err) {
  1557. f2fs_msg(sbi->sb, KERN_ERR,
  1558. "Cannot turn on quota_ino: %d", err);
  1559. return 0;
  1560. }
  1561. return 1;
  1562. }
  1563. for (i = 0; i < MAXQUOTAS; i++) {
  1564. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  1565. err = f2fs_quota_on_mount(sbi, i);
  1566. if (!err) {
  1567. enabled = 1;
  1568. continue;
  1569. }
  1570. f2fs_msg(sbi->sb, KERN_ERR,
  1571. "Cannot turn on quotas: %d on %d", err, i);
  1572. }
  1573. }
  1574. return enabled;
  1575. }
  1576. static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
  1577. unsigned int flags)
  1578. {
  1579. struct inode *qf_inode;
  1580. unsigned long qf_inum;
  1581. int err;
  1582. BUG_ON(!f2fs_sb_has_quota_ino(sb));
  1583. qf_inum = f2fs_qf_ino(sb, type);
  1584. if (!qf_inum)
  1585. return -EPERM;
  1586. qf_inode = f2fs_iget(sb, qf_inum);
  1587. if (IS_ERR(qf_inode)) {
  1588. f2fs_msg(sb, KERN_ERR,
  1589. "Bad quota inode %u:%lu", type, qf_inum);
  1590. return PTR_ERR(qf_inode);
  1591. }
  1592. /* Don't account quota for quota files to avoid recursion */
  1593. qf_inode->i_flags |= S_NOQUOTA;
  1594. err = dquot_enable(qf_inode, type, format_id, flags);
  1595. iput(qf_inode);
  1596. return err;
  1597. }
  1598. static int f2fs_enable_quotas(struct super_block *sb)
  1599. {
  1600. int type, err = 0;
  1601. unsigned long qf_inum;
  1602. bool quota_mopt[MAXQUOTAS] = {
  1603. test_opt(F2FS_SB(sb), USRQUOTA),
  1604. test_opt(F2FS_SB(sb), GRPQUOTA),
  1605. test_opt(F2FS_SB(sb), PRJQUOTA),
  1606. };
  1607. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
  1608. for (type = 0; type < MAXQUOTAS; type++) {
  1609. qf_inum = f2fs_qf_ino(sb, type);
  1610. if (qf_inum) {
  1611. err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
  1612. DQUOT_USAGE_ENABLED |
  1613. (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
  1614. if (err) {
  1615. f2fs_msg(sb, KERN_ERR,
  1616. "Failed to enable quota tracking "
  1617. "(type=%d, err=%d). Please run "
  1618. "fsck to fix.", type, err);
  1619. for (type--; type >= 0; type--)
  1620. dquot_quota_off(sb, type);
  1621. return err;
  1622. }
  1623. }
  1624. }
  1625. return 0;
  1626. }
  1627. static int f2fs_quota_sync(struct super_block *sb, int type)
  1628. {
  1629. struct quota_info *dqopt = sb_dqopt(sb);
  1630. int cnt;
  1631. int ret;
  1632. ret = dquot_writeback_dquots(sb, type);
  1633. if (ret)
  1634. return ret;
  1635. /*
  1636. * Now when everything is written we can discard the pagecache so
  1637. * that userspace sees the changes.
  1638. */
  1639. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1640. if (type != -1 && cnt != type)
  1641. continue;
  1642. if (!sb_has_quota_active(sb, cnt))
  1643. continue;
  1644. ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
  1645. if (ret)
  1646. return ret;
  1647. inode_lock(dqopt->files[cnt]);
  1648. truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
  1649. inode_unlock(dqopt->files[cnt]);
  1650. }
  1651. return 0;
  1652. }
  1653. static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
  1654. const struct path *path)
  1655. {
  1656. struct inode *inode;
  1657. int err;
  1658. err = f2fs_quota_sync(sb, type);
  1659. if (err)
  1660. return err;
  1661. err = dquot_quota_on(sb, type, format_id, path);
  1662. if (err)
  1663. return err;
  1664. inode = d_inode(path->dentry);
  1665. inode_lock(inode);
  1666. F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
  1667. f2fs_set_inode_flags(inode);
  1668. inode_unlock(inode);
  1669. f2fs_mark_inode_dirty_sync(inode, false);
  1670. return 0;
  1671. }
  1672. static int f2fs_quota_off(struct super_block *sb, int type)
  1673. {
  1674. struct inode *inode = sb_dqopt(sb)->files[type];
  1675. int err;
  1676. if (!inode || !igrab(inode))
  1677. return dquot_quota_off(sb, type);
  1678. err = f2fs_quota_sync(sb, type);
  1679. if (err)
  1680. goto out_put;
  1681. err = dquot_quota_off(sb, type);
  1682. if (err || f2fs_sb_has_quota_ino(sb))
  1683. goto out_put;
  1684. inode_lock(inode);
  1685. F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
  1686. f2fs_set_inode_flags(inode);
  1687. inode_unlock(inode);
  1688. f2fs_mark_inode_dirty_sync(inode, false);
  1689. out_put:
  1690. iput(inode);
  1691. return err;
  1692. }
  1693. void f2fs_quota_off_umount(struct super_block *sb)
  1694. {
  1695. int type;
  1696. int err;
  1697. for (type = 0; type < MAXQUOTAS; type++) {
  1698. err = f2fs_quota_off(sb, type);
  1699. if (err) {
  1700. int ret = dquot_quota_off(sb, type);
  1701. f2fs_msg(sb, KERN_ERR,
  1702. "Fail to turn off disk quota "
  1703. "(type: %d, err: %d, ret:%d), Please "
  1704. "run fsck to fix it.", type, err, ret);
  1705. set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
  1706. }
  1707. }
  1708. /*
  1709. * In case of checkpoint=disable, we must flush quota blocks.
  1710. * This can cause NULL exception for node_inode in end_io, since
  1711. * put_super already dropped it.
  1712. */
  1713. sync_filesystem(sb);
  1714. }
  1715. static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
  1716. {
  1717. struct quota_info *dqopt = sb_dqopt(sb);
  1718. int type;
  1719. for (type = 0; type < MAXQUOTAS; type++) {
  1720. if (!dqopt->files[type])
  1721. continue;
  1722. f2fs_inode_synced(dqopt->files[type]);
  1723. }
  1724. }
  1725. static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
  1726. {
  1727. *projid = F2FS_I(inode)->i_projid;
  1728. return 0;
  1729. }
  1730. static const struct dquot_operations f2fs_quota_operations = {
  1731. .get_reserved_space = f2fs_get_reserved_space,
  1732. .write_dquot = dquot_commit,
  1733. .acquire_dquot = dquot_acquire,
  1734. .release_dquot = dquot_release,
  1735. .mark_dirty = dquot_mark_dquot_dirty,
  1736. .write_info = dquot_commit_info,
  1737. .alloc_dquot = dquot_alloc,
  1738. .destroy_dquot = dquot_destroy,
  1739. .get_projid = f2fs_get_projid,
  1740. .get_next_id = dquot_get_next_id,
  1741. };
  1742. static const struct quotactl_ops f2fs_quotactl_ops = {
  1743. .quota_on = f2fs_quota_on,
  1744. .quota_off = f2fs_quota_off,
  1745. .quota_sync = f2fs_quota_sync,
  1746. .get_state = dquot_get_state,
  1747. .set_info = dquot_set_dqinfo,
  1748. .get_dqblk = dquot_get_dqblk,
  1749. .set_dqblk = dquot_set_dqblk,
  1750. .get_nextdqblk = dquot_get_next_dqblk,
  1751. };
  1752. #else
  1753. void f2fs_quota_off_umount(struct super_block *sb)
  1754. {
  1755. }
  1756. #endif
  1757. static const struct super_operations f2fs_sops = {
  1758. .alloc_inode = f2fs_alloc_inode,
  1759. .drop_inode = f2fs_drop_inode,
  1760. .destroy_inode = f2fs_destroy_inode,
  1761. .write_inode = f2fs_write_inode,
  1762. .dirty_inode = f2fs_dirty_inode,
  1763. .show_options = f2fs_show_options,
  1764. #ifdef CONFIG_QUOTA
  1765. .quota_read = f2fs_quota_read,
  1766. .quota_write = f2fs_quota_write,
  1767. .get_dquots = f2fs_get_dquots,
  1768. #endif
  1769. .evict_inode = f2fs_evict_inode,
  1770. .put_super = f2fs_put_super,
  1771. .sync_fs = f2fs_sync_fs,
  1772. .freeze_fs = f2fs_freeze,
  1773. .unfreeze_fs = f2fs_unfreeze,
  1774. .statfs = f2fs_statfs,
  1775. .remount_fs = f2fs_remount,
  1776. };
  1777. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1778. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  1779. {
  1780. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1781. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1782. ctx, len, NULL);
  1783. }
  1784. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  1785. void *fs_data)
  1786. {
  1787. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1788. /*
  1789. * Encrypting the root directory is not allowed because fsck
  1790. * expects lost+found directory to exist and remain unencrypted
  1791. * if LOST_FOUND feature is enabled.
  1792. *
  1793. */
  1794. if (f2fs_sb_has_lost_found(sbi->sb) &&
  1795. inode->i_ino == F2FS_ROOT_INO(sbi))
  1796. return -EPERM;
  1797. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1798. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1799. ctx, len, fs_data, XATTR_CREATE);
  1800. }
  1801. static bool f2fs_dummy_context(struct inode *inode)
  1802. {
  1803. return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
  1804. }
  1805. static const struct fscrypt_operations f2fs_cryptops = {
  1806. .key_prefix = "f2fs:",
  1807. .get_context = f2fs_get_context,
  1808. .set_context = f2fs_set_context,
  1809. .dummy_context = f2fs_dummy_context,
  1810. .empty_dir = f2fs_empty_dir,
  1811. .max_namelen = F2FS_NAME_LEN,
  1812. };
  1813. #endif
  1814. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  1815. u64 ino, u32 generation)
  1816. {
  1817. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1818. struct inode *inode;
  1819. if (f2fs_check_nid_range(sbi, ino))
  1820. return ERR_PTR(-ESTALE);
  1821. /*
  1822. * f2fs_iget isn't quite right if the inode is currently unallocated!
  1823. * However f2fs_iget currently does appropriate checks to handle stale
  1824. * inodes so everything is OK.
  1825. */
  1826. inode = f2fs_iget(sb, ino);
  1827. if (IS_ERR(inode))
  1828. return ERR_CAST(inode);
  1829. if (unlikely(generation && inode->i_generation != generation)) {
  1830. /* we didn't find the right inode.. */
  1831. iput(inode);
  1832. return ERR_PTR(-ESTALE);
  1833. }
  1834. return inode;
  1835. }
  1836. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1837. int fh_len, int fh_type)
  1838. {
  1839. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1840. f2fs_nfs_get_inode);
  1841. }
  1842. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1843. int fh_len, int fh_type)
  1844. {
  1845. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1846. f2fs_nfs_get_inode);
  1847. }
  1848. static const struct export_operations f2fs_export_ops = {
  1849. .fh_to_dentry = f2fs_fh_to_dentry,
  1850. .fh_to_parent = f2fs_fh_to_parent,
  1851. .get_parent = f2fs_get_parent,
  1852. };
  1853. static loff_t max_file_blocks(void)
  1854. {
  1855. loff_t result = 0;
  1856. loff_t leaf_count = ADDRS_PER_BLOCK;
  1857. /*
  1858. * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
  1859. * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
  1860. * space in inode.i_addr, it will be more safe to reassign
  1861. * result as zero.
  1862. */
  1863. /* two direct node blocks */
  1864. result += (leaf_count * 2);
  1865. /* two indirect node blocks */
  1866. leaf_count *= NIDS_PER_BLOCK;
  1867. result += (leaf_count * 2);
  1868. /* one double indirect node block */
  1869. leaf_count *= NIDS_PER_BLOCK;
  1870. result += leaf_count;
  1871. return result;
  1872. }
  1873. static int __f2fs_commit_super(struct buffer_head *bh,
  1874. struct f2fs_super_block *super)
  1875. {
  1876. lock_buffer(bh);
  1877. if (super)
  1878. memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
  1879. set_buffer_dirty(bh);
  1880. unlock_buffer(bh);
  1881. /* it's rare case, we can do fua all the time */
  1882. return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  1883. }
  1884. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  1885. struct buffer_head *bh)
  1886. {
  1887. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1888. (bh->b_data + F2FS_SUPER_OFFSET);
  1889. struct super_block *sb = sbi->sb;
  1890. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1891. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  1892. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  1893. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  1894. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1895. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1896. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  1897. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  1898. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  1899. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  1900. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  1901. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  1902. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1903. u64 main_end_blkaddr = main_blkaddr +
  1904. (segment_count_main << log_blocks_per_seg);
  1905. u64 seg_end_blkaddr = segment0_blkaddr +
  1906. (segment_count << log_blocks_per_seg);
  1907. if (segment0_blkaddr != cp_blkaddr) {
  1908. f2fs_msg(sb, KERN_INFO,
  1909. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  1910. segment0_blkaddr, cp_blkaddr);
  1911. return true;
  1912. }
  1913. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  1914. sit_blkaddr) {
  1915. f2fs_msg(sb, KERN_INFO,
  1916. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  1917. cp_blkaddr, sit_blkaddr,
  1918. segment_count_ckpt << log_blocks_per_seg);
  1919. return true;
  1920. }
  1921. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  1922. nat_blkaddr) {
  1923. f2fs_msg(sb, KERN_INFO,
  1924. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  1925. sit_blkaddr, nat_blkaddr,
  1926. segment_count_sit << log_blocks_per_seg);
  1927. return true;
  1928. }
  1929. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  1930. ssa_blkaddr) {
  1931. f2fs_msg(sb, KERN_INFO,
  1932. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  1933. nat_blkaddr, ssa_blkaddr,
  1934. segment_count_nat << log_blocks_per_seg);
  1935. return true;
  1936. }
  1937. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  1938. main_blkaddr) {
  1939. f2fs_msg(sb, KERN_INFO,
  1940. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  1941. ssa_blkaddr, main_blkaddr,
  1942. segment_count_ssa << log_blocks_per_seg);
  1943. return true;
  1944. }
  1945. if (main_end_blkaddr > seg_end_blkaddr) {
  1946. f2fs_msg(sb, KERN_INFO,
  1947. "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
  1948. main_blkaddr,
  1949. segment0_blkaddr +
  1950. (segment_count << log_blocks_per_seg),
  1951. segment_count_main << log_blocks_per_seg);
  1952. return true;
  1953. } else if (main_end_blkaddr < seg_end_blkaddr) {
  1954. int err = 0;
  1955. char *res;
  1956. /* fix in-memory information all the time */
  1957. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  1958. segment0_blkaddr) >> log_blocks_per_seg);
  1959. if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
  1960. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1961. res = "internally";
  1962. } else {
  1963. err = __f2fs_commit_super(bh, NULL);
  1964. res = err ? "failed" : "done";
  1965. }
  1966. f2fs_msg(sb, KERN_INFO,
  1967. "Fix alignment : %s, start(%u) end(%u) block(%u)",
  1968. res, main_blkaddr,
  1969. segment0_blkaddr +
  1970. (segment_count << log_blocks_per_seg),
  1971. segment_count_main << log_blocks_per_seg);
  1972. if (err)
  1973. return true;
  1974. }
  1975. return false;
  1976. }
  1977. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  1978. struct buffer_head *bh)
  1979. {
  1980. block_t segment_count, segs_per_sec, secs_per_zone;
  1981. block_t total_sections, blocks_per_seg;
  1982. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1983. (bh->b_data + F2FS_SUPER_OFFSET);
  1984. struct super_block *sb = sbi->sb;
  1985. unsigned int blocksize;
  1986. if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
  1987. f2fs_msg(sb, KERN_INFO,
  1988. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  1989. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  1990. return -EINVAL;
  1991. }
  1992. /* Currently, support only 4KB page cache size */
  1993. if (F2FS_BLKSIZE != PAGE_SIZE) {
  1994. f2fs_msg(sb, KERN_INFO,
  1995. "Invalid page_cache_size (%lu), supports only 4KB\n",
  1996. PAGE_SIZE);
  1997. return -EFSCORRUPTED;
  1998. }
  1999. /* Currently, support only 4KB block size */
  2000. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  2001. if (blocksize != F2FS_BLKSIZE) {
  2002. f2fs_msg(sb, KERN_INFO,
  2003. "Invalid blocksize (%u), supports only 4KB\n",
  2004. blocksize);
  2005. return -EFSCORRUPTED;
  2006. }
  2007. /* check log blocks per segment */
  2008. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  2009. f2fs_msg(sb, KERN_INFO,
  2010. "Invalid log blocks per segment (%u)\n",
  2011. le32_to_cpu(raw_super->log_blocks_per_seg));
  2012. return -EFSCORRUPTED;
  2013. }
  2014. /* Currently, support 512/1024/2048/4096 bytes sector size */
  2015. if (le32_to_cpu(raw_super->log_sectorsize) >
  2016. F2FS_MAX_LOG_SECTOR_SIZE ||
  2017. le32_to_cpu(raw_super->log_sectorsize) <
  2018. F2FS_MIN_LOG_SECTOR_SIZE) {
  2019. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  2020. le32_to_cpu(raw_super->log_sectorsize));
  2021. return -EFSCORRUPTED;
  2022. }
  2023. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  2024. le32_to_cpu(raw_super->log_sectorsize) !=
  2025. F2FS_MAX_LOG_SECTOR_SIZE) {
  2026. f2fs_msg(sb, KERN_INFO,
  2027. "Invalid log sectors per block(%u) log sectorsize(%u)",
  2028. le32_to_cpu(raw_super->log_sectors_per_block),
  2029. le32_to_cpu(raw_super->log_sectorsize));
  2030. return -EFSCORRUPTED;
  2031. }
  2032. segment_count = le32_to_cpu(raw_super->segment_count);
  2033. segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  2034. secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  2035. total_sections = le32_to_cpu(raw_super->section_count);
  2036. /* blocks_per_seg should be 512, given the above check */
  2037. blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
  2038. if (segment_count > F2FS_MAX_SEGMENT ||
  2039. segment_count < F2FS_MIN_SEGMENTS) {
  2040. f2fs_msg(sb, KERN_INFO,
  2041. "Invalid segment count (%u)",
  2042. segment_count);
  2043. return -EFSCORRUPTED;
  2044. }
  2045. if (total_sections > segment_count ||
  2046. total_sections < F2FS_MIN_SEGMENTS ||
  2047. segs_per_sec > segment_count || !segs_per_sec) {
  2048. f2fs_msg(sb, KERN_INFO,
  2049. "Invalid segment/section count (%u, %u x %u)",
  2050. segment_count, total_sections, segs_per_sec);
  2051. return -EFSCORRUPTED;
  2052. }
  2053. if ((segment_count / segs_per_sec) < total_sections) {
  2054. f2fs_msg(sb, KERN_INFO,
  2055. "Small segment_count (%u < %u * %u)",
  2056. segment_count, segs_per_sec, total_sections);
  2057. return -EFSCORRUPTED;
  2058. }
  2059. if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
  2060. f2fs_msg(sb, KERN_INFO,
  2061. "Wrong segment_count / block_count (%u > %llu)",
  2062. segment_count, le64_to_cpu(raw_super->block_count));
  2063. return -EFSCORRUPTED;
  2064. }
  2065. if (secs_per_zone > total_sections || !secs_per_zone) {
  2066. f2fs_msg(sb, KERN_INFO,
  2067. "Wrong secs_per_zone / total_sections (%u, %u)",
  2068. secs_per_zone, total_sections);
  2069. return -EFSCORRUPTED;
  2070. }
  2071. if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
  2072. raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
  2073. (le32_to_cpu(raw_super->extension_count) +
  2074. raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
  2075. f2fs_msg(sb, KERN_INFO,
  2076. "Corrupted extension count (%u + %u > %u)",
  2077. le32_to_cpu(raw_super->extension_count),
  2078. raw_super->hot_ext_count,
  2079. F2FS_MAX_EXTENSION);
  2080. return -EFSCORRUPTED;
  2081. }
  2082. if (le32_to_cpu(raw_super->cp_payload) >
  2083. (blocks_per_seg - F2FS_CP_PACKS)) {
  2084. f2fs_msg(sb, KERN_INFO,
  2085. "Insane cp_payload (%u > %u)",
  2086. le32_to_cpu(raw_super->cp_payload),
  2087. blocks_per_seg - F2FS_CP_PACKS);
  2088. return -EFSCORRUPTED;
  2089. }
  2090. /* check reserved ino info */
  2091. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  2092. le32_to_cpu(raw_super->meta_ino) != 2 ||
  2093. le32_to_cpu(raw_super->root_ino) != 3) {
  2094. f2fs_msg(sb, KERN_INFO,
  2095. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  2096. le32_to_cpu(raw_super->node_ino),
  2097. le32_to_cpu(raw_super->meta_ino),
  2098. le32_to_cpu(raw_super->root_ino));
  2099. return -EFSCORRUPTED;
  2100. }
  2101. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  2102. if (sanity_check_area_boundary(sbi, bh))
  2103. return -EFSCORRUPTED;
  2104. return 0;
  2105. }
  2106. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
  2107. {
  2108. unsigned int total, fsmeta;
  2109. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2110. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2111. unsigned int ovp_segments, reserved_segments;
  2112. unsigned int main_segs, blocks_per_seg;
  2113. unsigned int sit_segs, nat_segs;
  2114. unsigned int sit_bitmap_size, nat_bitmap_size;
  2115. unsigned int log_blocks_per_seg;
  2116. unsigned int segment_count_main;
  2117. unsigned int cp_pack_start_sum, cp_payload;
  2118. block_t user_block_count;
  2119. int i, j;
  2120. total = le32_to_cpu(raw_super->segment_count);
  2121. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  2122. sit_segs = le32_to_cpu(raw_super->segment_count_sit);
  2123. fsmeta += sit_segs;
  2124. nat_segs = le32_to_cpu(raw_super->segment_count_nat);
  2125. fsmeta += nat_segs;
  2126. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  2127. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  2128. if (unlikely(fsmeta >= total))
  2129. return 1;
  2130. ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  2131. reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  2132. if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
  2133. ovp_segments == 0 || reserved_segments == 0)) {
  2134. f2fs_msg(sbi->sb, KERN_ERR,
  2135. "Wrong layout: check mkfs.f2fs version");
  2136. return 1;
  2137. }
  2138. user_block_count = le64_to_cpu(ckpt->user_block_count);
  2139. segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  2140. log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  2141. if (!user_block_count || user_block_count >=
  2142. segment_count_main << log_blocks_per_seg) {
  2143. f2fs_msg(sbi->sb, KERN_ERR,
  2144. "Wrong user_block_count: %u", user_block_count);
  2145. return 1;
  2146. }
  2147. main_segs = le32_to_cpu(raw_super->segment_count_main);
  2148. blocks_per_seg = sbi->blocks_per_seg;
  2149. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  2150. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  2151. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
  2152. return 1;
  2153. for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
  2154. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  2155. le32_to_cpu(ckpt->cur_node_segno[j])) {
  2156. f2fs_msg(sbi->sb, KERN_ERR,
  2157. "Node segment (%u, %u) has the same "
  2158. "segno: %u", i, j,
  2159. le32_to_cpu(ckpt->cur_node_segno[i]));
  2160. return 1;
  2161. }
  2162. }
  2163. }
  2164. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  2165. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  2166. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
  2167. return 1;
  2168. for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
  2169. if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
  2170. le32_to_cpu(ckpt->cur_data_segno[j])) {
  2171. f2fs_msg(sbi->sb, KERN_ERR,
  2172. "Data segment (%u, %u) has the same "
  2173. "segno: %u", i, j,
  2174. le32_to_cpu(ckpt->cur_data_segno[i]));
  2175. return 1;
  2176. }
  2177. }
  2178. }
  2179. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  2180. for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
  2181. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  2182. le32_to_cpu(ckpt->cur_data_segno[j])) {
  2183. f2fs_msg(sbi->sb, KERN_ERR,
  2184. "Node segment (%u) and Data segment (%u)"
  2185. " has the same segno: %u", i, j,
  2186. le32_to_cpu(ckpt->cur_node_segno[i]));
  2187. return 1;
  2188. }
  2189. }
  2190. }
  2191. sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  2192. nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  2193. if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
  2194. nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
  2195. f2fs_msg(sbi->sb, KERN_ERR,
  2196. "Wrong bitmap size: sit: %u, nat:%u",
  2197. sit_bitmap_size, nat_bitmap_size);
  2198. return 1;
  2199. }
  2200. cp_pack_start_sum = __start_sum_addr(sbi);
  2201. cp_payload = __cp_payload(sbi);
  2202. if (cp_pack_start_sum < cp_payload + 1 ||
  2203. cp_pack_start_sum > blocks_per_seg - 1 -
  2204. NR_CURSEG_TYPE) {
  2205. f2fs_msg(sbi->sb, KERN_ERR,
  2206. "Wrong cp_pack_start_sum: %u",
  2207. cp_pack_start_sum);
  2208. return 1;
  2209. }
  2210. if (unlikely(f2fs_cp_error(sbi))) {
  2211. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  2212. return 1;
  2213. }
  2214. return 0;
  2215. }
  2216. static void init_sb_info(struct f2fs_sb_info *sbi)
  2217. {
  2218. struct f2fs_super_block *raw_super = sbi->raw_super;
  2219. int i, j;
  2220. sbi->log_sectors_per_block =
  2221. le32_to_cpu(raw_super->log_sectors_per_block);
  2222. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  2223. sbi->blocksize = 1 << sbi->log_blocksize;
  2224. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  2225. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  2226. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  2227. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  2228. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  2229. sbi->total_node_count =
  2230. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  2231. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  2232. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  2233. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  2234. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  2235. sbi->cur_victim_sec = NULL_SECNO;
  2236. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  2237. sbi->dir_level = DEF_DIR_LEVEL;
  2238. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  2239. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  2240. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  2241. for (i = 0; i < NR_COUNT_TYPE; i++)
  2242. atomic_set(&sbi->nr_pages[i], 0);
  2243. for (i = 0; i < META; i++)
  2244. atomic_set(&sbi->wb_sync_req[i], 0);
  2245. INIT_LIST_HEAD(&sbi->s_list);
  2246. mutex_init(&sbi->umount_mutex);
  2247. for (i = 0; i < NR_PAGE_TYPE - 1; i++)
  2248. for (j = HOT; j < NR_TEMP_TYPE; j++)
  2249. mutex_init(&sbi->wio_mutex[i][j]);
  2250. init_rwsem(&sbi->io_order_lock);
  2251. spin_lock_init(&sbi->cp_lock);
  2252. sbi->dirty_device = 0;
  2253. spin_lock_init(&sbi->dev_lock);
  2254. init_rwsem(&sbi->sb_lock);
  2255. }
  2256. static int init_percpu_info(struct f2fs_sb_info *sbi)
  2257. {
  2258. int err;
  2259. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
  2260. if (err)
  2261. return err;
  2262. err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
  2263. GFP_KERNEL);
  2264. if (err)
  2265. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  2266. return err;
  2267. }
  2268. #ifdef CONFIG_BLK_DEV_ZONED
  2269. static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
  2270. {
  2271. struct block_device *bdev = FDEV(devi).bdev;
  2272. sector_t nr_sectors = bdev->bd_part->nr_sects;
  2273. sector_t sector = 0;
  2274. struct blk_zone *zones;
  2275. unsigned int i, nr_zones;
  2276. unsigned int n = 0;
  2277. int err = -EIO;
  2278. if (!f2fs_sb_has_blkzoned(sbi->sb))
  2279. return 0;
  2280. if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
  2281. SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
  2282. return -EINVAL;
  2283. sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
  2284. if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
  2285. __ilog2_u32(sbi->blocks_per_blkz))
  2286. return -EINVAL;
  2287. sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
  2288. FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
  2289. sbi->log_blocks_per_blkz;
  2290. if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
  2291. FDEV(devi).nr_blkz++;
  2292. FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
  2293. GFP_KERNEL);
  2294. if (!FDEV(devi).blkz_type)
  2295. return -ENOMEM;
  2296. #define F2FS_REPORT_NR_ZONES 4096
  2297. zones = f2fs_kzalloc(sbi,
  2298. array_size(F2FS_REPORT_NR_ZONES,
  2299. sizeof(struct blk_zone)),
  2300. GFP_KERNEL);
  2301. if (!zones)
  2302. return -ENOMEM;
  2303. /* Get block zones type */
  2304. while (zones && sector < nr_sectors) {
  2305. nr_zones = F2FS_REPORT_NR_ZONES;
  2306. err = blkdev_report_zones(bdev, sector,
  2307. zones, &nr_zones,
  2308. GFP_KERNEL);
  2309. if (err)
  2310. break;
  2311. if (!nr_zones) {
  2312. err = -EIO;
  2313. break;
  2314. }
  2315. for (i = 0; i < nr_zones; i++) {
  2316. FDEV(devi).blkz_type[n] = zones[i].type;
  2317. sector += zones[i].len;
  2318. n++;
  2319. }
  2320. }
  2321. kfree(zones);
  2322. return err;
  2323. }
  2324. #endif
  2325. /*
  2326. * Read f2fs raw super block.
  2327. * Because we have two copies of super block, so read both of them
  2328. * to get the first valid one. If any one of them is broken, we pass
  2329. * them recovery flag back to the caller.
  2330. */
  2331. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  2332. struct f2fs_super_block **raw_super,
  2333. int *valid_super_block, int *recovery)
  2334. {
  2335. struct super_block *sb = sbi->sb;
  2336. int block;
  2337. struct buffer_head *bh;
  2338. struct f2fs_super_block *super;
  2339. int err = 0;
  2340. super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
  2341. if (!super)
  2342. return -ENOMEM;
  2343. for (block = 0; block < 2; block++) {
  2344. bh = sb_bread(sb, block);
  2345. if (!bh) {
  2346. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  2347. block + 1);
  2348. err = -EIO;
  2349. continue;
  2350. }
  2351. /* sanity checking of raw super */
  2352. err = sanity_check_raw_super(sbi, bh);
  2353. if (err) {
  2354. f2fs_msg(sb, KERN_ERR,
  2355. "Can't find valid F2FS filesystem in %dth superblock",
  2356. block + 1);
  2357. brelse(bh);
  2358. continue;
  2359. }
  2360. if (!*raw_super) {
  2361. memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
  2362. sizeof(*super));
  2363. *valid_super_block = block;
  2364. *raw_super = super;
  2365. }
  2366. brelse(bh);
  2367. }
  2368. /* Fail to read any one of the superblocks*/
  2369. if (err < 0)
  2370. *recovery = 1;
  2371. /* No valid superblock */
  2372. if (!*raw_super)
  2373. kfree(super);
  2374. else
  2375. err = 0;
  2376. return err;
  2377. }
  2378. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  2379. {
  2380. struct buffer_head *bh;
  2381. int err;
  2382. if ((recover && f2fs_readonly(sbi->sb)) ||
  2383. bdev_read_only(sbi->sb->s_bdev)) {
  2384. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  2385. return -EROFS;
  2386. }
  2387. /* write back-up superblock first */
  2388. bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
  2389. if (!bh)
  2390. return -EIO;
  2391. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  2392. brelse(bh);
  2393. /* if we are in recovery path, skip writing valid superblock */
  2394. if (recover || err)
  2395. return err;
  2396. /* write current valid superblock */
  2397. bh = sb_bread(sbi->sb, sbi->valid_super_block);
  2398. if (!bh)
  2399. return -EIO;
  2400. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  2401. brelse(bh);
  2402. return err;
  2403. }
  2404. static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
  2405. {
  2406. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2407. unsigned int max_devices = MAX_DEVICES;
  2408. int i;
  2409. /* Initialize single device information */
  2410. if (!RDEV(0).path[0]) {
  2411. if (!bdev_is_zoned(sbi->sb->s_bdev))
  2412. return 0;
  2413. max_devices = 1;
  2414. }
  2415. /*
  2416. * Initialize multiple devices information, or single
  2417. * zoned block device information.
  2418. */
  2419. sbi->devs = f2fs_kzalloc(sbi,
  2420. array_size(max_devices,
  2421. sizeof(struct f2fs_dev_info)),
  2422. GFP_KERNEL);
  2423. if (!sbi->devs)
  2424. return -ENOMEM;
  2425. for (i = 0; i < max_devices; i++) {
  2426. if (i > 0 && !RDEV(i).path[0])
  2427. break;
  2428. if (max_devices == 1) {
  2429. /* Single zoned block device mount */
  2430. FDEV(0).bdev =
  2431. blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
  2432. sbi->sb->s_mode, sbi->sb->s_type);
  2433. } else {
  2434. /* Multi-device mount */
  2435. memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
  2436. FDEV(i).total_segments =
  2437. le32_to_cpu(RDEV(i).total_segments);
  2438. if (i == 0) {
  2439. FDEV(i).start_blk = 0;
  2440. FDEV(i).end_blk = FDEV(i).start_blk +
  2441. (FDEV(i).total_segments <<
  2442. sbi->log_blocks_per_seg) - 1 +
  2443. le32_to_cpu(raw_super->segment0_blkaddr);
  2444. } else {
  2445. FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
  2446. FDEV(i).end_blk = FDEV(i).start_blk +
  2447. (FDEV(i).total_segments <<
  2448. sbi->log_blocks_per_seg) - 1;
  2449. }
  2450. FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
  2451. sbi->sb->s_mode, sbi->sb->s_type);
  2452. }
  2453. if (IS_ERR(FDEV(i).bdev))
  2454. return PTR_ERR(FDEV(i).bdev);
  2455. /* to release errored devices */
  2456. sbi->s_ndevs = i + 1;
  2457. #ifdef CONFIG_BLK_DEV_ZONED
  2458. if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
  2459. !f2fs_sb_has_blkzoned(sbi->sb)) {
  2460. f2fs_msg(sbi->sb, KERN_ERR,
  2461. "Zoned block device feature not enabled\n");
  2462. return -EINVAL;
  2463. }
  2464. if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
  2465. if (init_blkz_info(sbi, i)) {
  2466. f2fs_msg(sbi->sb, KERN_ERR,
  2467. "Failed to initialize F2FS blkzone information");
  2468. return -EINVAL;
  2469. }
  2470. if (max_devices == 1)
  2471. break;
  2472. f2fs_msg(sbi->sb, KERN_INFO,
  2473. "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
  2474. i, FDEV(i).path,
  2475. FDEV(i).total_segments,
  2476. FDEV(i).start_blk, FDEV(i).end_blk,
  2477. bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
  2478. "Host-aware" : "Host-managed");
  2479. continue;
  2480. }
  2481. #endif
  2482. f2fs_msg(sbi->sb, KERN_INFO,
  2483. "Mount Device [%2d]: %20s, %8u, %8x - %8x",
  2484. i, FDEV(i).path,
  2485. FDEV(i).total_segments,
  2486. FDEV(i).start_blk, FDEV(i).end_blk);
  2487. }
  2488. f2fs_msg(sbi->sb, KERN_INFO,
  2489. "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
  2490. return 0;
  2491. }
  2492. static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
  2493. {
  2494. struct f2fs_sm_info *sm_i = SM_I(sbi);
  2495. /* adjust parameters according to the volume size */
  2496. if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
  2497. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
  2498. sm_i->dcc_info->discard_granularity = 1;
  2499. sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
  2500. }
  2501. sbi->readdir_ra = 1;
  2502. }
  2503. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  2504. {
  2505. struct f2fs_sb_info *sbi;
  2506. struct f2fs_super_block *raw_super;
  2507. struct inode *root;
  2508. int err;
  2509. bool retry = true, need_fsck = false;
  2510. char *options = NULL;
  2511. int recovery, i, valid_super_block;
  2512. struct curseg_info *seg_i;
  2513. try_onemore:
  2514. err = -EINVAL;
  2515. raw_super = NULL;
  2516. valid_super_block = -1;
  2517. recovery = 0;
  2518. /* allocate memory for f2fs-specific super block info */
  2519. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  2520. if (!sbi)
  2521. return -ENOMEM;
  2522. sbi->sb = sb;
  2523. /* Load the checksum driver */
  2524. sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
  2525. if (IS_ERR(sbi->s_chksum_driver)) {
  2526. f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
  2527. err = PTR_ERR(sbi->s_chksum_driver);
  2528. sbi->s_chksum_driver = NULL;
  2529. goto free_sbi;
  2530. }
  2531. /* set a block size */
  2532. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  2533. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  2534. goto free_sbi;
  2535. }
  2536. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  2537. &recovery);
  2538. if (err)
  2539. goto free_sbi;
  2540. sb->s_fs_info = sbi;
  2541. sbi->raw_super = raw_super;
  2542. /* precompute checksum seed for metadata */
  2543. if (f2fs_sb_has_inode_chksum(sb))
  2544. sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
  2545. sizeof(raw_super->uuid));
  2546. /*
  2547. * The BLKZONED feature indicates that the drive was formatted with
  2548. * zone alignment optimization. This is optional for host-aware
  2549. * devices, but mandatory for host-managed zoned block devices.
  2550. */
  2551. #ifndef CONFIG_BLK_DEV_ZONED
  2552. if (f2fs_sb_has_blkzoned(sb)) {
  2553. f2fs_msg(sb, KERN_ERR,
  2554. "Zoned block device support is not enabled\n");
  2555. err = -EOPNOTSUPP;
  2556. goto free_sb_buf;
  2557. }
  2558. #endif
  2559. default_options(sbi);
  2560. /* parse mount options */
  2561. options = kstrdup((const char *)data, GFP_KERNEL);
  2562. if (data && !options) {
  2563. err = -ENOMEM;
  2564. goto free_sb_buf;
  2565. }
  2566. err = parse_options(sb, options);
  2567. if (err)
  2568. goto free_options;
  2569. sbi->max_file_blocks = max_file_blocks();
  2570. sb->s_maxbytes = sbi->max_file_blocks <<
  2571. le32_to_cpu(raw_super->log_blocksize);
  2572. sb->s_max_links = F2FS_LINK_MAX;
  2573. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  2574. #ifdef CONFIG_QUOTA
  2575. sb->dq_op = &f2fs_quota_operations;
  2576. if (f2fs_sb_has_quota_ino(sb))
  2577. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  2578. else
  2579. sb->s_qcop = &f2fs_quotactl_ops;
  2580. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  2581. if (f2fs_sb_has_quota_ino(sbi->sb)) {
  2582. for (i = 0; i < MAXQUOTAS; i++) {
  2583. if (f2fs_qf_ino(sbi->sb, i))
  2584. sbi->nquota_files++;
  2585. }
  2586. }
  2587. #endif
  2588. sb->s_op = &f2fs_sops;
  2589. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2590. sb->s_cop = &f2fs_cryptops;
  2591. #endif
  2592. sb->s_xattr = f2fs_xattr_handlers;
  2593. sb->s_export_op = &f2fs_export_ops;
  2594. sb->s_magic = F2FS_SUPER_MAGIC;
  2595. sb->s_time_gran = 1;
  2596. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  2597. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  2598. memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  2599. sb->s_iflags |= SB_I_CGROUPWB;
  2600. /* init f2fs-specific super block info */
  2601. sbi->valid_super_block = valid_super_block;
  2602. mutex_init(&sbi->gc_mutex);
  2603. mutex_init(&sbi->writepages);
  2604. mutex_init(&sbi->cp_mutex);
  2605. init_rwsem(&sbi->node_write);
  2606. init_rwsem(&sbi->node_change);
  2607. /* disallow all the data/node/meta page writes */
  2608. set_sbi_flag(sbi, SBI_POR_DOING);
  2609. spin_lock_init(&sbi->stat_lock);
  2610. /* init iostat info */
  2611. spin_lock_init(&sbi->iostat_lock);
  2612. sbi->iostat_enable = false;
  2613. for (i = 0; i < NR_PAGE_TYPE; i++) {
  2614. int n = (i == META) ? 1: NR_TEMP_TYPE;
  2615. int j;
  2616. sbi->write_io[i] =
  2617. f2fs_kmalloc(sbi,
  2618. array_size(n,
  2619. sizeof(struct f2fs_bio_info)),
  2620. GFP_KERNEL);
  2621. if (!sbi->write_io[i]) {
  2622. err = -ENOMEM;
  2623. goto free_bio_info;
  2624. }
  2625. for (j = HOT; j < n; j++) {
  2626. init_rwsem(&sbi->write_io[i][j].io_rwsem);
  2627. sbi->write_io[i][j].sbi = sbi;
  2628. sbi->write_io[i][j].bio = NULL;
  2629. spin_lock_init(&sbi->write_io[i][j].io_lock);
  2630. INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
  2631. }
  2632. }
  2633. init_rwsem(&sbi->cp_rwsem);
  2634. init_waitqueue_head(&sbi->cp_wait);
  2635. init_sb_info(sbi);
  2636. err = init_percpu_info(sbi);
  2637. if (err)
  2638. goto free_bio_info;
  2639. if (F2FS_IO_SIZE(sbi) > 1) {
  2640. sbi->write_io_dummy =
  2641. mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
  2642. if (!sbi->write_io_dummy) {
  2643. err = -ENOMEM;
  2644. goto free_percpu;
  2645. }
  2646. }
  2647. /* get an inode for meta space */
  2648. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  2649. if (IS_ERR(sbi->meta_inode)) {
  2650. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  2651. err = PTR_ERR(sbi->meta_inode);
  2652. goto free_io_dummy;
  2653. }
  2654. err = f2fs_get_valid_checkpoint(sbi);
  2655. if (err) {
  2656. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  2657. goto free_meta_inode;
  2658. }
  2659. /* Initialize device list */
  2660. err = f2fs_scan_devices(sbi);
  2661. if (err) {
  2662. f2fs_msg(sb, KERN_ERR, "Failed to find devices");
  2663. goto free_devices;
  2664. }
  2665. sbi->total_valid_node_count =
  2666. le32_to_cpu(sbi->ckpt->valid_node_count);
  2667. percpu_counter_set(&sbi->total_valid_inode_count,
  2668. le32_to_cpu(sbi->ckpt->valid_inode_count));
  2669. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  2670. sbi->total_valid_block_count =
  2671. le64_to_cpu(sbi->ckpt->valid_block_count);
  2672. sbi->last_valid_block_count = sbi->total_valid_block_count;
  2673. sbi->reserved_blocks = 0;
  2674. sbi->current_reserved_blocks = 0;
  2675. limit_reserve_root(sbi);
  2676. for (i = 0; i < NR_INODE_TYPE; i++) {
  2677. INIT_LIST_HEAD(&sbi->inode_list[i]);
  2678. spin_lock_init(&sbi->inode_lock[i]);
  2679. }
  2680. f2fs_init_extent_cache_info(sbi);
  2681. f2fs_init_ino_entry_info(sbi);
  2682. f2fs_init_fsync_node_info(sbi);
  2683. /* setup f2fs internal modules */
  2684. err = f2fs_build_segment_manager(sbi);
  2685. if (err) {
  2686. f2fs_msg(sb, KERN_ERR,
  2687. "Failed to initialize F2FS segment manager");
  2688. goto free_sm;
  2689. }
  2690. err = f2fs_build_node_manager(sbi);
  2691. if (err) {
  2692. f2fs_msg(sb, KERN_ERR,
  2693. "Failed to initialize F2FS node manager");
  2694. goto free_nm;
  2695. }
  2696. /* For write statistics */
  2697. if (sb->s_bdev->bd_part)
  2698. sbi->sectors_written_start =
  2699. (u64)part_stat_read(sb->s_bdev->bd_part,
  2700. sectors[STAT_WRITE]);
  2701. /* Read accumulated write IO statistics if exists */
  2702. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  2703. if (__exist_node_summaries(sbi))
  2704. sbi->kbytes_written =
  2705. le64_to_cpu(seg_i->journal->info.kbytes_written);
  2706. f2fs_build_gc_manager(sbi);
  2707. err = f2fs_build_stats(sbi);
  2708. if (err)
  2709. goto free_nm;
  2710. /* get an inode for node space */
  2711. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  2712. if (IS_ERR(sbi->node_inode)) {
  2713. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  2714. err = PTR_ERR(sbi->node_inode);
  2715. goto free_stats;
  2716. }
  2717. /* read root inode and dentry */
  2718. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  2719. if (IS_ERR(root)) {
  2720. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  2721. err = PTR_ERR(root);
  2722. goto free_node_inode;
  2723. }
  2724. if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
  2725. !root->i_size || !root->i_nlink) {
  2726. iput(root);
  2727. err = -EINVAL;
  2728. goto free_node_inode;
  2729. }
  2730. sb->s_root = d_make_root(root); /* allocate root dentry */
  2731. if (!sb->s_root) {
  2732. err = -ENOMEM;
  2733. goto free_root_inode;
  2734. }
  2735. err = f2fs_register_sysfs(sbi);
  2736. if (err)
  2737. goto free_root_inode;
  2738. #ifdef CONFIG_QUOTA
  2739. /* Enable quota usage during mount */
  2740. if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
  2741. err = f2fs_enable_quotas(sb);
  2742. if (err) {
  2743. f2fs_msg(sb, KERN_ERR,
  2744. "Cannot turn on quotas: error %d", err);
  2745. goto free_sysfs;
  2746. }
  2747. }
  2748. #endif
  2749. /* if there are nt orphan nodes free them */
  2750. err = f2fs_recover_orphan_inodes(sbi);
  2751. if (err)
  2752. goto free_meta;
  2753. /* recover fsynced data */
  2754. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  2755. /*
  2756. * mount should be failed, when device has readonly mode, and
  2757. * previous checkpoint was not done by clean system shutdown.
  2758. */
  2759. if (bdev_read_only(sb->s_bdev) &&
  2760. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  2761. err = -EROFS;
  2762. goto free_meta;
  2763. }
  2764. if (need_fsck)
  2765. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2766. if (!retry)
  2767. goto skip_recovery;
  2768. err = f2fs_recover_fsync_data(sbi, false);
  2769. if (err < 0) {
  2770. need_fsck = true;
  2771. f2fs_msg(sb, KERN_ERR,
  2772. "Cannot recover all fsync data errno=%d", err);
  2773. goto free_meta;
  2774. }
  2775. } else {
  2776. err = f2fs_recover_fsync_data(sbi, true);
  2777. if (!f2fs_readonly(sb) && err > 0) {
  2778. err = -EINVAL;
  2779. f2fs_msg(sb, KERN_ERR,
  2780. "Need to recover fsync data");
  2781. goto free_meta;
  2782. }
  2783. }
  2784. skip_recovery:
  2785. /* f2fs_recover_fsync_data() cleared this already */
  2786. clear_sbi_flag(sbi, SBI_POR_DOING);
  2787. /*
  2788. * If filesystem is not mounted as read-only then
  2789. * do start the gc_thread.
  2790. */
  2791. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  2792. /* After POR, we can run background GC thread.*/
  2793. err = f2fs_start_gc_thread(sbi);
  2794. if (err)
  2795. goto free_meta;
  2796. }
  2797. kfree(options);
  2798. /* recover broken superblock */
  2799. if (recovery) {
  2800. err = f2fs_commit_super(sbi, true);
  2801. f2fs_msg(sb, KERN_INFO,
  2802. "Try to recover %dth superblock, ret: %d",
  2803. sbi->valid_super_block ? 1 : 2, err);
  2804. }
  2805. f2fs_join_shrinker(sbi);
  2806. f2fs_tuning_parameters(sbi);
  2807. f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
  2808. cur_cp_version(F2FS_CKPT(sbi)));
  2809. f2fs_update_time(sbi, CP_TIME);
  2810. f2fs_update_time(sbi, REQ_TIME);
  2811. return 0;
  2812. free_meta:
  2813. #ifdef CONFIG_QUOTA
  2814. f2fs_truncate_quota_inode_pages(sb);
  2815. if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
  2816. f2fs_quota_off_umount(sbi->sb);
  2817. #endif
  2818. /*
  2819. * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
  2820. * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
  2821. * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
  2822. * falls into an infinite loop in f2fs_sync_meta_pages().
  2823. */
  2824. truncate_inode_pages_final(META_MAPPING(sbi));
  2825. #ifdef CONFIG_QUOTA
  2826. free_sysfs:
  2827. #endif
  2828. f2fs_unregister_sysfs(sbi);
  2829. free_root_inode:
  2830. dput(sb->s_root);
  2831. sb->s_root = NULL;
  2832. free_node_inode:
  2833. f2fs_release_ino_entry(sbi, true);
  2834. truncate_inode_pages_final(NODE_MAPPING(sbi));
  2835. iput(sbi->node_inode);
  2836. sbi->node_inode = NULL;
  2837. free_stats:
  2838. f2fs_destroy_stats(sbi);
  2839. free_nm:
  2840. f2fs_destroy_node_manager(sbi);
  2841. free_sm:
  2842. f2fs_destroy_segment_manager(sbi);
  2843. free_devices:
  2844. destroy_device_list(sbi);
  2845. kfree(sbi->ckpt);
  2846. free_meta_inode:
  2847. make_bad_inode(sbi->meta_inode);
  2848. iput(sbi->meta_inode);
  2849. sbi->meta_inode = NULL;
  2850. free_io_dummy:
  2851. mempool_destroy(sbi->write_io_dummy);
  2852. free_percpu:
  2853. destroy_percpu_info(sbi);
  2854. free_bio_info:
  2855. for (i = 0; i < NR_PAGE_TYPE; i++)
  2856. kfree(sbi->write_io[i]);
  2857. free_options:
  2858. #ifdef CONFIG_QUOTA
  2859. for (i = 0; i < MAXQUOTAS; i++)
  2860. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  2861. #endif
  2862. kfree(options);
  2863. free_sb_buf:
  2864. kfree(raw_super);
  2865. free_sbi:
  2866. if (sbi->s_chksum_driver)
  2867. crypto_free_shash(sbi->s_chksum_driver);
  2868. kfree(sbi);
  2869. /* give only one another chance */
  2870. if (retry) {
  2871. retry = false;
  2872. shrink_dcache_sb(sb);
  2873. goto try_onemore;
  2874. }
  2875. return err;
  2876. }
  2877. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  2878. const char *dev_name, void *data)
  2879. {
  2880. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  2881. }
  2882. static void kill_f2fs_super(struct super_block *sb)
  2883. {
  2884. if (sb->s_root) {
  2885. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2886. set_sbi_flag(sbi, SBI_IS_CLOSE);
  2887. f2fs_stop_gc_thread(sbi);
  2888. f2fs_stop_discard_thread(sbi);
  2889. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  2890. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  2891. struct cp_control cpc = {
  2892. .reason = CP_UMOUNT,
  2893. };
  2894. f2fs_write_checkpoint(sbi, &cpc);
  2895. }
  2896. if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
  2897. sb->s_flags &= ~SB_RDONLY;
  2898. }
  2899. kill_block_super(sb);
  2900. }
  2901. static struct file_system_type f2fs_fs_type = {
  2902. .owner = THIS_MODULE,
  2903. .name = "f2fs",
  2904. .mount = f2fs_mount,
  2905. .kill_sb = kill_f2fs_super,
  2906. .fs_flags = FS_REQUIRES_DEV,
  2907. };
  2908. MODULE_ALIAS_FS("f2fs");
  2909. static int __init init_inodecache(void)
  2910. {
  2911. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  2912. sizeof(struct f2fs_inode_info), 0,
  2913. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  2914. if (!f2fs_inode_cachep)
  2915. return -ENOMEM;
  2916. return 0;
  2917. }
  2918. static void destroy_inodecache(void)
  2919. {
  2920. /*
  2921. * Make sure all delayed rcu free inodes are flushed before we
  2922. * destroy cache.
  2923. */
  2924. rcu_barrier();
  2925. kmem_cache_destroy(f2fs_inode_cachep);
  2926. }
  2927. static int __init init_f2fs_fs(void)
  2928. {
  2929. int err;
  2930. if (PAGE_SIZE != F2FS_BLKSIZE) {
  2931. printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
  2932. PAGE_SIZE, F2FS_BLKSIZE);
  2933. return -EINVAL;
  2934. }
  2935. f2fs_build_trace_ios();
  2936. err = init_inodecache();
  2937. if (err)
  2938. goto fail;
  2939. err = f2fs_create_node_manager_caches();
  2940. if (err)
  2941. goto free_inodecache;
  2942. err = f2fs_create_segment_manager_caches();
  2943. if (err)
  2944. goto free_node_manager_caches;
  2945. err = f2fs_create_checkpoint_caches();
  2946. if (err)
  2947. goto free_segment_manager_caches;
  2948. err = f2fs_create_extent_cache();
  2949. if (err)
  2950. goto free_checkpoint_caches;
  2951. err = f2fs_init_sysfs();
  2952. if (err)
  2953. goto free_extent_cache;
  2954. err = register_shrinker(&f2fs_shrinker_info);
  2955. if (err)
  2956. goto free_sysfs;
  2957. err = register_filesystem(&f2fs_fs_type);
  2958. if (err)
  2959. goto free_shrinker;
  2960. err = f2fs_create_root_stats();
  2961. if (err)
  2962. goto free_filesystem;
  2963. err = f2fs_init_post_read_processing();
  2964. if (err)
  2965. goto free_root_stats;
  2966. return 0;
  2967. free_root_stats:
  2968. f2fs_destroy_root_stats();
  2969. free_filesystem:
  2970. unregister_filesystem(&f2fs_fs_type);
  2971. free_shrinker:
  2972. unregister_shrinker(&f2fs_shrinker_info);
  2973. free_sysfs:
  2974. f2fs_exit_sysfs();
  2975. free_extent_cache:
  2976. f2fs_destroy_extent_cache();
  2977. free_checkpoint_caches:
  2978. f2fs_destroy_checkpoint_caches();
  2979. free_segment_manager_caches:
  2980. f2fs_destroy_segment_manager_caches();
  2981. free_node_manager_caches:
  2982. f2fs_destroy_node_manager_caches();
  2983. free_inodecache:
  2984. destroy_inodecache();
  2985. fail:
  2986. return err;
  2987. }
  2988. static void __exit exit_f2fs_fs(void)
  2989. {
  2990. f2fs_destroy_post_read_processing();
  2991. f2fs_destroy_root_stats();
  2992. unregister_filesystem(&f2fs_fs_type);
  2993. unregister_shrinker(&f2fs_shrinker_info);
  2994. f2fs_exit_sysfs();
  2995. f2fs_destroy_extent_cache();
  2996. f2fs_destroy_checkpoint_caches();
  2997. f2fs_destroy_segment_manager_caches();
  2998. f2fs_destroy_node_manager_caches();
  2999. destroy_inodecache();
  3000. f2fs_destroy_trace_ios();
  3001. }
  3002. module_init(init_f2fs_fs)
  3003. module_exit(exit_f2fs_fs)
  3004. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  3005. MODULE_DESCRIPTION("Flash Friendly File System");
  3006. MODULE_LICENSE("GPL");
  3007. MODULE_SOFTDEP("pre: crypto-crc32c");