super.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376
  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. /* unregister procfs/sysfs entries in advance to avoid race case */
  939. f2fs_unregister_sysfs(sbi);
  940. f2fs_quota_off_umount(sb);
  941. /* prevent remaining shrinker jobs */
  942. mutex_lock(&sbi->umount_mutex);
  943. /*
  944. * We don't need to do checkpoint when superblock is clean.
  945. * But, the previous checkpoint was not done by umount, it needs to do
  946. * clean checkpoint again.
  947. */
  948. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  949. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  950. struct cp_control cpc = {
  951. .reason = CP_UMOUNT,
  952. };
  953. f2fs_write_checkpoint(sbi, &cpc);
  954. }
  955. /* be sure to wait for any on-going discard commands */
  956. dropped = f2fs_wait_discard_bios(sbi);
  957. if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
  958. !sbi->discard_blks && !dropped) {
  959. struct cp_control cpc = {
  960. .reason = CP_UMOUNT | CP_TRIMMED,
  961. };
  962. f2fs_write_checkpoint(sbi, &cpc);
  963. }
  964. /*
  965. * normally superblock is clean, so we need to release this.
  966. * In addition, EIO will skip do checkpoint, we need this as well.
  967. */
  968. f2fs_release_ino_entry(sbi, true);
  969. f2fs_leave_shrinker(sbi);
  970. mutex_unlock(&sbi->umount_mutex);
  971. /* our cp_error case, we can wait for any writeback page */
  972. f2fs_flush_merged_writes(sbi);
  973. f2fs_wait_on_all_pages_writeback(sbi);
  974. f2fs_bug_on(sbi, sbi->fsync_node_num);
  975. iput(sbi->node_inode);
  976. sbi->node_inode = NULL;
  977. iput(sbi->meta_inode);
  978. sbi->meta_inode = NULL;
  979. /*
  980. * iput() can update stat information, if f2fs_write_checkpoint()
  981. * above failed with error.
  982. */
  983. f2fs_destroy_stats(sbi);
  984. /* destroy f2fs internal modules */
  985. f2fs_destroy_node_manager(sbi);
  986. f2fs_destroy_segment_manager(sbi);
  987. kfree(sbi->ckpt);
  988. sb->s_fs_info = NULL;
  989. if (sbi->s_chksum_driver)
  990. crypto_free_shash(sbi->s_chksum_driver);
  991. kfree(sbi->raw_super);
  992. destroy_device_list(sbi);
  993. mempool_destroy(sbi->write_io_dummy);
  994. #ifdef CONFIG_QUOTA
  995. for (i = 0; i < MAXQUOTAS; i++)
  996. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  997. #endif
  998. destroy_percpu_info(sbi);
  999. for (i = 0; i < NR_PAGE_TYPE; i++)
  1000. kfree(sbi->write_io[i]);
  1001. kfree(sbi);
  1002. }
  1003. int f2fs_sync_fs(struct super_block *sb, int sync)
  1004. {
  1005. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1006. int err = 0;
  1007. if (unlikely(f2fs_cp_error(sbi)))
  1008. return 0;
  1009. trace_f2fs_sync_fs(sb, sync);
  1010. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1011. return -EAGAIN;
  1012. if (sync) {
  1013. struct cp_control cpc;
  1014. cpc.reason = __get_cp_reason(sbi);
  1015. mutex_lock(&sbi->gc_mutex);
  1016. err = f2fs_write_checkpoint(sbi, &cpc);
  1017. mutex_unlock(&sbi->gc_mutex);
  1018. }
  1019. f2fs_trace_ios(NULL, 1);
  1020. return err;
  1021. }
  1022. static int f2fs_freeze(struct super_block *sb)
  1023. {
  1024. if (f2fs_readonly(sb))
  1025. return 0;
  1026. /* IO error happened before */
  1027. if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
  1028. return -EIO;
  1029. /* must be clean, since sync_filesystem() was already called */
  1030. if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
  1031. return -EINVAL;
  1032. return 0;
  1033. }
  1034. static int f2fs_unfreeze(struct super_block *sb)
  1035. {
  1036. return 0;
  1037. }
  1038. #ifdef CONFIG_QUOTA
  1039. static int f2fs_statfs_project(struct super_block *sb,
  1040. kprojid_t projid, struct kstatfs *buf)
  1041. {
  1042. struct kqid qid;
  1043. struct dquot *dquot;
  1044. u64 limit;
  1045. u64 curblock;
  1046. qid = make_kqid_projid(projid);
  1047. dquot = dqget(sb, qid);
  1048. if (IS_ERR(dquot))
  1049. return PTR_ERR(dquot);
  1050. spin_lock(&dquot->dq_dqb_lock);
  1051. limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
  1052. dquot->dq_dqb.dqb_bhardlimit);
  1053. if (limit)
  1054. limit >>= sb->s_blocksize_bits;
  1055. if (limit && buf->f_blocks > limit) {
  1056. curblock = (dquot->dq_dqb.dqb_curspace +
  1057. dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
  1058. buf->f_blocks = limit;
  1059. buf->f_bfree = buf->f_bavail =
  1060. (buf->f_blocks > curblock) ?
  1061. (buf->f_blocks - curblock) : 0;
  1062. }
  1063. limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
  1064. dquot->dq_dqb.dqb_ihardlimit);
  1065. if (limit && buf->f_files > limit) {
  1066. buf->f_files = limit;
  1067. buf->f_ffree =
  1068. (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
  1069. (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
  1070. }
  1071. spin_unlock(&dquot->dq_dqb_lock);
  1072. dqput(dquot);
  1073. return 0;
  1074. }
  1075. #endif
  1076. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1077. {
  1078. struct super_block *sb = dentry->d_sb;
  1079. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1080. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1081. block_t total_count, user_block_count, start_count;
  1082. u64 avail_node_count;
  1083. total_count = le64_to_cpu(sbi->raw_super->block_count);
  1084. user_block_count = sbi->user_block_count;
  1085. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  1086. buf->f_type = F2FS_SUPER_MAGIC;
  1087. buf->f_bsize = sbi->blocksize;
  1088. buf->f_blocks = total_count - start_count;
  1089. buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
  1090. sbi->current_reserved_blocks;
  1091. if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
  1092. buf->f_bavail = buf->f_bfree -
  1093. F2FS_OPTION(sbi).root_reserved_blocks;
  1094. else
  1095. buf->f_bavail = 0;
  1096. avail_node_count = sbi->total_node_count - sbi->nquota_files -
  1097. F2FS_RESERVED_NODE_NUM;
  1098. if (avail_node_count > user_block_count) {
  1099. buf->f_files = user_block_count;
  1100. buf->f_ffree = buf->f_bavail;
  1101. } else {
  1102. buf->f_files = avail_node_count;
  1103. buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
  1104. buf->f_bavail);
  1105. }
  1106. buf->f_namelen = F2FS_NAME_LEN;
  1107. buf->f_fsid.val[0] = (u32)id;
  1108. buf->f_fsid.val[1] = (u32)(id >> 32);
  1109. #ifdef CONFIG_QUOTA
  1110. if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
  1111. sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
  1112. f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
  1113. }
  1114. #endif
  1115. return 0;
  1116. }
  1117. static inline void f2fs_show_quota_options(struct seq_file *seq,
  1118. struct super_block *sb)
  1119. {
  1120. #ifdef CONFIG_QUOTA
  1121. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1122. if (F2FS_OPTION(sbi).s_jquota_fmt) {
  1123. char *fmtname = "";
  1124. switch (F2FS_OPTION(sbi).s_jquota_fmt) {
  1125. case QFMT_VFS_OLD:
  1126. fmtname = "vfsold";
  1127. break;
  1128. case QFMT_VFS_V0:
  1129. fmtname = "vfsv0";
  1130. break;
  1131. case QFMT_VFS_V1:
  1132. fmtname = "vfsv1";
  1133. break;
  1134. }
  1135. seq_printf(seq, ",jqfmt=%s", fmtname);
  1136. }
  1137. if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
  1138. seq_show_option(seq, "usrjquota",
  1139. F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
  1140. if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
  1141. seq_show_option(seq, "grpjquota",
  1142. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
  1143. if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
  1144. seq_show_option(seq, "prjjquota",
  1145. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
  1146. #endif
  1147. }
  1148. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  1149. {
  1150. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  1151. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
  1152. if (test_opt(sbi, FORCE_FG_GC))
  1153. seq_printf(seq, ",background_gc=%s", "sync");
  1154. else
  1155. seq_printf(seq, ",background_gc=%s", "on");
  1156. } else {
  1157. seq_printf(seq, ",background_gc=%s", "off");
  1158. }
  1159. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  1160. seq_puts(seq, ",disable_roll_forward");
  1161. if (test_opt(sbi, DISCARD))
  1162. seq_puts(seq, ",discard");
  1163. if (test_opt(sbi, NOHEAP))
  1164. seq_puts(seq, ",no_heap");
  1165. else
  1166. seq_puts(seq, ",heap");
  1167. #ifdef CONFIG_F2FS_FS_XATTR
  1168. if (test_opt(sbi, XATTR_USER))
  1169. seq_puts(seq, ",user_xattr");
  1170. else
  1171. seq_puts(seq, ",nouser_xattr");
  1172. if (test_opt(sbi, INLINE_XATTR))
  1173. seq_puts(seq, ",inline_xattr");
  1174. else
  1175. seq_puts(seq, ",noinline_xattr");
  1176. if (test_opt(sbi, INLINE_XATTR_SIZE))
  1177. seq_printf(seq, ",inline_xattr_size=%u",
  1178. F2FS_OPTION(sbi).inline_xattr_size);
  1179. #endif
  1180. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  1181. if (test_opt(sbi, POSIX_ACL))
  1182. seq_puts(seq, ",acl");
  1183. else
  1184. seq_puts(seq, ",noacl");
  1185. #endif
  1186. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  1187. seq_puts(seq, ",disable_ext_identify");
  1188. if (test_opt(sbi, INLINE_DATA))
  1189. seq_puts(seq, ",inline_data");
  1190. else
  1191. seq_puts(seq, ",noinline_data");
  1192. if (test_opt(sbi, INLINE_DENTRY))
  1193. seq_puts(seq, ",inline_dentry");
  1194. else
  1195. seq_puts(seq, ",noinline_dentry");
  1196. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  1197. seq_puts(seq, ",flush_merge");
  1198. if (test_opt(sbi, NOBARRIER))
  1199. seq_puts(seq, ",nobarrier");
  1200. if (test_opt(sbi, FASTBOOT))
  1201. seq_puts(seq, ",fastboot");
  1202. if (test_opt(sbi, EXTENT_CACHE))
  1203. seq_puts(seq, ",extent_cache");
  1204. else
  1205. seq_puts(seq, ",noextent_cache");
  1206. if (test_opt(sbi, DATA_FLUSH))
  1207. seq_puts(seq, ",data_flush");
  1208. seq_puts(seq, ",mode=");
  1209. if (test_opt(sbi, ADAPTIVE))
  1210. seq_puts(seq, "adaptive");
  1211. else if (test_opt(sbi, LFS))
  1212. seq_puts(seq, "lfs");
  1213. seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
  1214. if (test_opt(sbi, RESERVE_ROOT))
  1215. seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
  1216. F2FS_OPTION(sbi).root_reserved_blocks,
  1217. from_kuid_munged(&init_user_ns,
  1218. F2FS_OPTION(sbi).s_resuid),
  1219. from_kgid_munged(&init_user_ns,
  1220. F2FS_OPTION(sbi).s_resgid));
  1221. if (F2FS_IO_SIZE_BITS(sbi))
  1222. seq_printf(seq, ",io_bits=%u",
  1223. F2FS_OPTION(sbi).write_io_size_bits);
  1224. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1225. if (test_opt(sbi, FAULT_INJECTION)) {
  1226. seq_printf(seq, ",fault_injection=%u",
  1227. F2FS_OPTION(sbi).fault_info.inject_rate);
  1228. seq_printf(seq, ",fault_type=%u",
  1229. F2FS_OPTION(sbi).fault_info.inject_type);
  1230. }
  1231. #endif
  1232. #ifdef CONFIG_QUOTA
  1233. if (test_opt(sbi, QUOTA))
  1234. seq_puts(seq, ",quota");
  1235. if (test_opt(sbi, USRQUOTA))
  1236. seq_puts(seq, ",usrquota");
  1237. if (test_opt(sbi, GRPQUOTA))
  1238. seq_puts(seq, ",grpquota");
  1239. if (test_opt(sbi, PRJQUOTA))
  1240. seq_puts(seq, ",prjquota");
  1241. #endif
  1242. f2fs_show_quota_options(seq, sbi->sb);
  1243. if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
  1244. seq_printf(seq, ",whint_mode=%s", "user-based");
  1245. else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
  1246. seq_printf(seq, ",whint_mode=%s", "fs-based");
  1247. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1248. if (F2FS_OPTION(sbi).test_dummy_encryption)
  1249. seq_puts(seq, ",test_dummy_encryption");
  1250. #endif
  1251. if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
  1252. seq_printf(seq, ",alloc_mode=%s", "default");
  1253. else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
  1254. seq_printf(seq, ",alloc_mode=%s", "reuse");
  1255. if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
  1256. seq_printf(seq, ",fsync_mode=%s", "posix");
  1257. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
  1258. seq_printf(seq, ",fsync_mode=%s", "strict");
  1259. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
  1260. seq_printf(seq, ",fsync_mode=%s", "nobarrier");
  1261. return 0;
  1262. }
  1263. static void default_options(struct f2fs_sb_info *sbi)
  1264. {
  1265. /* init some FS parameters */
  1266. F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
  1267. F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
  1268. F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
  1269. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
  1270. F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
  1271. F2FS_OPTION(sbi).test_dummy_encryption = false;
  1272. F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
  1273. F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
  1274. set_opt(sbi, BG_GC);
  1275. set_opt(sbi, INLINE_XATTR);
  1276. set_opt(sbi, INLINE_DATA);
  1277. set_opt(sbi, INLINE_DENTRY);
  1278. set_opt(sbi, EXTENT_CACHE);
  1279. set_opt(sbi, NOHEAP);
  1280. sbi->sb->s_flags |= SB_LAZYTIME;
  1281. set_opt(sbi, FLUSH_MERGE);
  1282. set_opt(sbi, DISCARD);
  1283. if (f2fs_sb_has_blkzoned(sbi->sb))
  1284. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  1285. else
  1286. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  1287. #ifdef CONFIG_F2FS_FS_XATTR
  1288. set_opt(sbi, XATTR_USER);
  1289. #endif
  1290. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  1291. set_opt(sbi, POSIX_ACL);
  1292. #endif
  1293. f2fs_build_fault_attr(sbi, 0, 0);
  1294. }
  1295. #ifdef CONFIG_QUOTA
  1296. static int f2fs_enable_quotas(struct super_block *sb);
  1297. #endif
  1298. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  1299. {
  1300. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1301. struct f2fs_mount_info org_mount_opt;
  1302. unsigned long old_sb_flags;
  1303. int err;
  1304. bool need_restart_gc = false;
  1305. bool need_stop_gc = false;
  1306. bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
  1307. #ifdef CONFIG_QUOTA
  1308. int i, j;
  1309. #endif
  1310. /*
  1311. * Save the old mount options in case we
  1312. * need to restore them.
  1313. */
  1314. org_mount_opt = sbi->mount_opt;
  1315. old_sb_flags = sb->s_flags;
  1316. #ifdef CONFIG_QUOTA
  1317. org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
  1318. for (i = 0; i < MAXQUOTAS; i++) {
  1319. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  1320. org_mount_opt.s_qf_names[i] =
  1321. kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
  1322. GFP_KERNEL);
  1323. if (!org_mount_opt.s_qf_names[i]) {
  1324. for (j = 0; j < i; j++)
  1325. kfree(org_mount_opt.s_qf_names[j]);
  1326. return -ENOMEM;
  1327. }
  1328. } else {
  1329. org_mount_opt.s_qf_names[i] = NULL;
  1330. }
  1331. }
  1332. #endif
  1333. /* recover superblocks we couldn't write due to previous RO mount */
  1334. if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  1335. err = f2fs_commit_super(sbi, false);
  1336. f2fs_msg(sb, KERN_INFO,
  1337. "Try to recover all the superblocks, ret: %d", err);
  1338. if (!err)
  1339. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1340. }
  1341. default_options(sbi);
  1342. /* parse mount options */
  1343. err = parse_options(sb, data);
  1344. if (err)
  1345. goto restore_opts;
  1346. /*
  1347. * Previous and new state of filesystem is RO,
  1348. * so skip checking GC and FLUSH_MERGE conditions.
  1349. */
  1350. if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
  1351. goto skip;
  1352. #ifdef CONFIG_QUOTA
  1353. if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
  1354. err = dquot_suspend(sb, -1);
  1355. if (err < 0)
  1356. goto restore_opts;
  1357. } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
  1358. /* dquot_resume needs RW */
  1359. sb->s_flags &= ~SB_RDONLY;
  1360. if (sb_any_quota_suspended(sb)) {
  1361. dquot_resume(sb, -1);
  1362. } else if (f2fs_sb_has_quota_ino(sb)) {
  1363. err = f2fs_enable_quotas(sb);
  1364. if (err)
  1365. goto restore_opts;
  1366. }
  1367. }
  1368. #endif
  1369. /* disallow enable/disable extent_cache dynamically */
  1370. if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
  1371. err = -EINVAL;
  1372. f2fs_msg(sbi->sb, KERN_WARNING,
  1373. "switch extent_cache option is not allowed");
  1374. goto restore_opts;
  1375. }
  1376. /*
  1377. * We stop the GC thread if FS is mounted as RO
  1378. * or if background_gc = off is passed in mount
  1379. * option. Also sync the filesystem.
  1380. */
  1381. if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
  1382. if (sbi->gc_thread) {
  1383. f2fs_stop_gc_thread(sbi);
  1384. need_restart_gc = true;
  1385. }
  1386. } else if (!sbi->gc_thread) {
  1387. err = f2fs_start_gc_thread(sbi);
  1388. if (err)
  1389. goto restore_opts;
  1390. need_stop_gc = true;
  1391. }
  1392. if (*flags & SB_RDONLY ||
  1393. F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
  1394. writeback_inodes_sb(sb, WB_REASON_SYNC);
  1395. sync_inodes_sb(sb);
  1396. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1397. set_sbi_flag(sbi, SBI_IS_CLOSE);
  1398. f2fs_sync_fs(sb, 1);
  1399. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  1400. }
  1401. /*
  1402. * We stop issue flush thread if FS is mounted as RO
  1403. * or if flush_merge is not passed in mount option.
  1404. */
  1405. if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  1406. clear_opt(sbi, FLUSH_MERGE);
  1407. f2fs_destroy_flush_cmd_control(sbi, false);
  1408. } else {
  1409. err = f2fs_create_flush_cmd_control(sbi);
  1410. if (err)
  1411. goto restore_gc;
  1412. }
  1413. skip:
  1414. #ifdef CONFIG_QUOTA
  1415. /* Release old quota file names */
  1416. for (i = 0; i < MAXQUOTAS; i++)
  1417. kfree(org_mount_opt.s_qf_names[i]);
  1418. #endif
  1419. /* Update the POSIXACL Flag */
  1420. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  1421. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  1422. limit_reserve_root(sbi);
  1423. *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
  1424. return 0;
  1425. restore_gc:
  1426. if (need_restart_gc) {
  1427. if (f2fs_start_gc_thread(sbi))
  1428. f2fs_msg(sbi->sb, KERN_WARNING,
  1429. "background gc thread has stopped");
  1430. } else if (need_stop_gc) {
  1431. f2fs_stop_gc_thread(sbi);
  1432. }
  1433. restore_opts:
  1434. #ifdef CONFIG_QUOTA
  1435. F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
  1436. for (i = 0; i < MAXQUOTAS; i++) {
  1437. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  1438. F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
  1439. }
  1440. #endif
  1441. sbi->mount_opt = org_mount_opt;
  1442. sb->s_flags = old_sb_flags;
  1443. return err;
  1444. }
  1445. #ifdef CONFIG_QUOTA
  1446. /* Read data from quotafile */
  1447. static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
  1448. size_t len, loff_t off)
  1449. {
  1450. struct inode *inode = sb_dqopt(sb)->files[type];
  1451. struct address_space *mapping = inode->i_mapping;
  1452. block_t blkidx = F2FS_BYTES_TO_BLK(off);
  1453. int offset = off & (sb->s_blocksize - 1);
  1454. int tocopy;
  1455. size_t toread;
  1456. loff_t i_size = i_size_read(inode);
  1457. struct page *page;
  1458. char *kaddr;
  1459. if (off > i_size)
  1460. return 0;
  1461. if (off + len > i_size)
  1462. len = i_size - off;
  1463. toread = len;
  1464. while (toread > 0) {
  1465. tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
  1466. repeat:
  1467. page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
  1468. if (IS_ERR(page)) {
  1469. if (PTR_ERR(page) == -ENOMEM) {
  1470. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1471. goto repeat;
  1472. }
  1473. return PTR_ERR(page);
  1474. }
  1475. lock_page(page);
  1476. if (unlikely(page->mapping != mapping)) {
  1477. f2fs_put_page(page, 1);
  1478. goto repeat;
  1479. }
  1480. if (unlikely(!PageUptodate(page))) {
  1481. f2fs_put_page(page, 1);
  1482. return -EIO;
  1483. }
  1484. kaddr = kmap_atomic(page);
  1485. memcpy(data, kaddr + offset, tocopy);
  1486. kunmap_atomic(kaddr);
  1487. f2fs_put_page(page, 1);
  1488. offset = 0;
  1489. toread -= tocopy;
  1490. data += tocopy;
  1491. blkidx++;
  1492. }
  1493. return len;
  1494. }
  1495. /* Write to quotafile */
  1496. static ssize_t f2fs_quota_write(struct super_block *sb, int type,
  1497. const char *data, size_t len, loff_t off)
  1498. {
  1499. struct inode *inode = sb_dqopt(sb)->files[type];
  1500. struct address_space *mapping = inode->i_mapping;
  1501. const struct address_space_operations *a_ops = mapping->a_ops;
  1502. int offset = off & (sb->s_blocksize - 1);
  1503. size_t towrite = len;
  1504. struct page *page;
  1505. void *fsdata = NULL;
  1506. char *kaddr;
  1507. int err = 0;
  1508. int tocopy;
  1509. while (towrite > 0) {
  1510. tocopy = min_t(unsigned long, sb->s_blocksize - offset,
  1511. towrite);
  1512. retry:
  1513. err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
  1514. &page, &fsdata);
  1515. if (unlikely(err)) {
  1516. if (err == -ENOMEM) {
  1517. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1518. goto retry;
  1519. }
  1520. break;
  1521. }
  1522. kaddr = kmap_atomic(page);
  1523. memcpy(kaddr + offset, data, tocopy);
  1524. kunmap_atomic(kaddr);
  1525. flush_dcache_page(page);
  1526. a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
  1527. page, fsdata);
  1528. offset = 0;
  1529. towrite -= tocopy;
  1530. off += tocopy;
  1531. data += tocopy;
  1532. cond_resched();
  1533. }
  1534. if (len == towrite)
  1535. return err;
  1536. inode->i_mtime = inode->i_ctime = current_time(inode);
  1537. f2fs_mark_inode_dirty_sync(inode, false);
  1538. return len - towrite;
  1539. }
  1540. static struct dquot **f2fs_get_dquots(struct inode *inode)
  1541. {
  1542. return F2FS_I(inode)->i_dquot;
  1543. }
  1544. static qsize_t *f2fs_get_reserved_space(struct inode *inode)
  1545. {
  1546. return &F2FS_I(inode)->i_reserved_quota;
  1547. }
  1548. static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
  1549. {
  1550. return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
  1551. F2FS_OPTION(sbi).s_jquota_fmt, type);
  1552. }
  1553. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
  1554. {
  1555. int enabled = 0;
  1556. int i, err;
  1557. if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
  1558. err = f2fs_enable_quotas(sbi->sb);
  1559. if (err) {
  1560. f2fs_msg(sbi->sb, KERN_ERR,
  1561. "Cannot turn on quota_ino: %d", err);
  1562. return 0;
  1563. }
  1564. return 1;
  1565. }
  1566. for (i = 0; i < MAXQUOTAS; i++) {
  1567. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  1568. err = f2fs_quota_on_mount(sbi, i);
  1569. if (!err) {
  1570. enabled = 1;
  1571. continue;
  1572. }
  1573. f2fs_msg(sbi->sb, KERN_ERR,
  1574. "Cannot turn on quotas: %d on %d", err, i);
  1575. }
  1576. }
  1577. return enabled;
  1578. }
  1579. static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
  1580. unsigned int flags)
  1581. {
  1582. struct inode *qf_inode;
  1583. unsigned long qf_inum;
  1584. int err;
  1585. BUG_ON(!f2fs_sb_has_quota_ino(sb));
  1586. qf_inum = f2fs_qf_ino(sb, type);
  1587. if (!qf_inum)
  1588. return -EPERM;
  1589. qf_inode = f2fs_iget(sb, qf_inum);
  1590. if (IS_ERR(qf_inode)) {
  1591. f2fs_msg(sb, KERN_ERR,
  1592. "Bad quota inode %u:%lu", type, qf_inum);
  1593. return PTR_ERR(qf_inode);
  1594. }
  1595. /* Don't account quota for quota files to avoid recursion */
  1596. qf_inode->i_flags |= S_NOQUOTA;
  1597. err = dquot_enable(qf_inode, type, format_id, flags);
  1598. iput(qf_inode);
  1599. return err;
  1600. }
  1601. static int f2fs_enable_quotas(struct super_block *sb)
  1602. {
  1603. int type, err = 0;
  1604. unsigned long qf_inum;
  1605. bool quota_mopt[MAXQUOTAS] = {
  1606. test_opt(F2FS_SB(sb), USRQUOTA),
  1607. test_opt(F2FS_SB(sb), GRPQUOTA),
  1608. test_opt(F2FS_SB(sb), PRJQUOTA),
  1609. };
  1610. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
  1611. for (type = 0; type < MAXQUOTAS; type++) {
  1612. qf_inum = f2fs_qf_ino(sb, type);
  1613. if (qf_inum) {
  1614. err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
  1615. DQUOT_USAGE_ENABLED |
  1616. (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
  1617. if (err) {
  1618. f2fs_msg(sb, KERN_ERR,
  1619. "Failed to enable quota tracking "
  1620. "(type=%d, err=%d). Please run "
  1621. "fsck to fix.", type, err);
  1622. for (type--; type >= 0; type--)
  1623. dquot_quota_off(sb, type);
  1624. return err;
  1625. }
  1626. }
  1627. }
  1628. return 0;
  1629. }
  1630. static int f2fs_quota_sync(struct super_block *sb, int type)
  1631. {
  1632. struct quota_info *dqopt = sb_dqopt(sb);
  1633. int cnt;
  1634. int ret;
  1635. ret = dquot_writeback_dquots(sb, type);
  1636. if (ret)
  1637. return ret;
  1638. /*
  1639. * Now when everything is written we can discard the pagecache so
  1640. * that userspace sees the changes.
  1641. */
  1642. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1643. if (type != -1 && cnt != type)
  1644. continue;
  1645. if (!sb_has_quota_active(sb, cnt))
  1646. continue;
  1647. ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
  1648. if (ret)
  1649. return ret;
  1650. inode_lock(dqopt->files[cnt]);
  1651. truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
  1652. inode_unlock(dqopt->files[cnt]);
  1653. }
  1654. return 0;
  1655. }
  1656. static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
  1657. const struct path *path)
  1658. {
  1659. struct inode *inode;
  1660. int err;
  1661. err = f2fs_quota_sync(sb, type);
  1662. if (err)
  1663. return err;
  1664. err = dquot_quota_on(sb, type, format_id, path);
  1665. if (err)
  1666. return err;
  1667. inode = d_inode(path->dentry);
  1668. inode_lock(inode);
  1669. F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
  1670. f2fs_set_inode_flags(inode);
  1671. inode_unlock(inode);
  1672. f2fs_mark_inode_dirty_sync(inode, false);
  1673. return 0;
  1674. }
  1675. static int f2fs_quota_off(struct super_block *sb, int type)
  1676. {
  1677. struct inode *inode = sb_dqopt(sb)->files[type];
  1678. int err;
  1679. if (!inode || !igrab(inode))
  1680. return dquot_quota_off(sb, type);
  1681. err = f2fs_quota_sync(sb, type);
  1682. if (err)
  1683. goto out_put;
  1684. err = dquot_quota_off(sb, type);
  1685. if (err || f2fs_sb_has_quota_ino(sb))
  1686. goto out_put;
  1687. inode_lock(inode);
  1688. F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
  1689. f2fs_set_inode_flags(inode);
  1690. inode_unlock(inode);
  1691. f2fs_mark_inode_dirty_sync(inode, false);
  1692. out_put:
  1693. iput(inode);
  1694. return err;
  1695. }
  1696. void f2fs_quota_off_umount(struct super_block *sb)
  1697. {
  1698. int type;
  1699. int err;
  1700. for (type = 0; type < MAXQUOTAS; type++) {
  1701. err = f2fs_quota_off(sb, type);
  1702. if (err) {
  1703. int ret = dquot_quota_off(sb, type);
  1704. f2fs_msg(sb, KERN_ERR,
  1705. "Fail to turn off disk quota "
  1706. "(type: %d, err: %d, ret:%d), Please "
  1707. "run fsck to fix it.", type, err, ret);
  1708. set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
  1709. }
  1710. }
  1711. /*
  1712. * In case of checkpoint=disable, we must flush quota blocks.
  1713. * This can cause NULL exception for node_inode in end_io, since
  1714. * put_super already dropped it.
  1715. */
  1716. sync_filesystem(sb);
  1717. }
  1718. static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
  1719. {
  1720. struct quota_info *dqopt = sb_dqopt(sb);
  1721. int type;
  1722. for (type = 0; type < MAXQUOTAS; type++) {
  1723. if (!dqopt->files[type])
  1724. continue;
  1725. f2fs_inode_synced(dqopt->files[type]);
  1726. }
  1727. }
  1728. static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
  1729. {
  1730. *projid = F2FS_I(inode)->i_projid;
  1731. return 0;
  1732. }
  1733. static const struct dquot_operations f2fs_quota_operations = {
  1734. .get_reserved_space = f2fs_get_reserved_space,
  1735. .write_dquot = dquot_commit,
  1736. .acquire_dquot = dquot_acquire,
  1737. .release_dquot = dquot_release,
  1738. .mark_dirty = dquot_mark_dquot_dirty,
  1739. .write_info = dquot_commit_info,
  1740. .alloc_dquot = dquot_alloc,
  1741. .destroy_dquot = dquot_destroy,
  1742. .get_projid = f2fs_get_projid,
  1743. .get_next_id = dquot_get_next_id,
  1744. };
  1745. static const struct quotactl_ops f2fs_quotactl_ops = {
  1746. .quota_on = f2fs_quota_on,
  1747. .quota_off = f2fs_quota_off,
  1748. .quota_sync = f2fs_quota_sync,
  1749. .get_state = dquot_get_state,
  1750. .set_info = dquot_set_dqinfo,
  1751. .get_dqblk = dquot_get_dqblk,
  1752. .set_dqblk = dquot_set_dqblk,
  1753. .get_nextdqblk = dquot_get_next_dqblk,
  1754. };
  1755. #else
  1756. void f2fs_quota_off_umount(struct super_block *sb)
  1757. {
  1758. }
  1759. #endif
  1760. static const struct super_operations f2fs_sops = {
  1761. .alloc_inode = f2fs_alloc_inode,
  1762. .drop_inode = f2fs_drop_inode,
  1763. .destroy_inode = f2fs_destroy_inode,
  1764. .write_inode = f2fs_write_inode,
  1765. .dirty_inode = f2fs_dirty_inode,
  1766. .show_options = f2fs_show_options,
  1767. #ifdef CONFIG_QUOTA
  1768. .quota_read = f2fs_quota_read,
  1769. .quota_write = f2fs_quota_write,
  1770. .get_dquots = f2fs_get_dquots,
  1771. #endif
  1772. .evict_inode = f2fs_evict_inode,
  1773. .put_super = f2fs_put_super,
  1774. .sync_fs = f2fs_sync_fs,
  1775. .freeze_fs = f2fs_freeze,
  1776. .unfreeze_fs = f2fs_unfreeze,
  1777. .statfs = f2fs_statfs,
  1778. .remount_fs = f2fs_remount,
  1779. };
  1780. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1781. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  1782. {
  1783. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1784. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1785. ctx, len, NULL);
  1786. }
  1787. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  1788. void *fs_data)
  1789. {
  1790. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1791. /*
  1792. * Encrypting the root directory is not allowed because fsck
  1793. * expects lost+found directory to exist and remain unencrypted
  1794. * if LOST_FOUND feature is enabled.
  1795. *
  1796. */
  1797. if (f2fs_sb_has_lost_found(sbi->sb) &&
  1798. inode->i_ino == F2FS_ROOT_INO(sbi))
  1799. return -EPERM;
  1800. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1801. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1802. ctx, len, fs_data, XATTR_CREATE);
  1803. }
  1804. static bool f2fs_dummy_context(struct inode *inode)
  1805. {
  1806. return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
  1807. }
  1808. static const struct fscrypt_operations f2fs_cryptops = {
  1809. .key_prefix = "f2fs:",
  1810. .get_context = f2fs_get_context,
  1811. .set_context = f2fs_set_context,
  1812. .dummy_context = f2fs_dummy_context,
  1813. .empty_dir = f2fs_empty_dir,
  1814. .max_namelen = F2FS_NAME_LEN,
  1815. };
  1816. #endif
  1817. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  1818. u64 ino, u32 generation)
  1819. {
  1820. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1821. struct inode *inode;
  1822. if (f2fs_check_nid_range(sbi, ino))
  1823. return ERR_PTR(-ESTALE);
  1824. /*
  1825. * f2fs_iget isn't quite right if the inode is currently unallocated!
  1826. * However f2fs_iget currently does appropriate checks to handle stale
  1827. * inodes so everything is OK.
  1828. */
  1829. inode = f2fs_iget(sb, ino);
  1830. if (IS_ERR(inode))
  1831. return ERR_CAST(inode);
  1832. if (unlikely(generation && inode->i_generation != generation)) {
  1833. /* we didn't find the right inode.. */
  1834. iput(inode);
  1835. return ERR_PTR(-ESTALE);
  1836. }
  1837. return inode;
  1838. }
  1839. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1840. int fh_len, int fh_type)
  1841. {
  1842. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1843. f2fs_nfs_get_inode);
  1844. }
  1845. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1846. int fh_len, int fh_type)
  1847. {
  1848. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1849. f2fs_nfs_get_inode);
  1850. }
  1851. static const struct export_operations f2fs_export_ops = {
  1852. .fh_to_dentry = f2fs_fh_to_dentry,
  1853. .fh_to_parent = f2fs_fh_to_parent,
  1854. .get_parent = f2fs_get_parent,
  1855. };
  1856. static loff_t max_file_blocks(void)
  1857. {
  1858. loff_t result = 0;
  1859. loff_t leaf_count = ADDRS_PER_BLOCK;
  1860. /*
  1861. * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
  1862. * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
  1863. * space in inode.i_addr, it will be more safe to reassign
  1864. * result as zero.
  1865. */
  1866. /* two direct node blocks */
  1867. result += (leaf_count * 2);
  1868. /* two indirect node blocks */
  1869. leaf_count *= NIDS_PER_BLOCK;
  1870. result += (leaf_count * 2);
  1871. /* one double indirect node block */
  1872. leaf_count *= NIDS_PER_BLOCK;
  1873. result += leaf_count;
  1874. return result;
  1875. }
  1876. static int __f2fs_commit_super(struct buffer_head *bh,
  1877. struct f2fs_super_block *super)
  1878. {
  1879. lock_buffer(bh);
  1880. if (super)
  1881. memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
  1882. set_buffer_dirty(bh);
  1883. unlock_buffer(bh);
  1884. /* it's rare case, we can do fua all the time */
  1885. return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
  1886. }
  1887. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  1888. struct buffer_head *bh)
  1889. {
  1890. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1891. (bh->b_data + F2FS_SUPER_OFFSET);
  1892. struct super_block *sb = sbi->sb;
  1893. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1894. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  1895. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  1896. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  1897. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1898. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1899. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  1900. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  1901. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  1902. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  1903. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  1904. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  1905. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1906. u64 main_end_blkaddr = main_blkaddr +
  1907. (segment_count_main << log_blocks_per_seg);
  1908. u64 seg_end_blkaddr = segment0_blkaddr +
  1909. (segment_count << log_blocks_per_seg);
  1910. if (segment0_blkaddr != cp_blkaddr) {
  1911. f2fs_msg(sb, KERN_INFO,
  1912. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  1913. segment0_blkaddr, cp_blkaddr);
  1914. return true;
  1915. }
  1916. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  1917. sit_blkaddr) {
  1918. f2fs_msg(sb, KERN_INFO,
  1919. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  1920. cp_blkaddr, sit_blkaddr,
  1921. segment_count_ckpt << log_blocks_per_seg);
  1922. return true;
  1923. }
  1924. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  1925. nat_blkaddr) {
  1926. f2fs_msg(sb, KERN_INFO,
  1927. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  1928. sit_blkaddr, nat_blkaddr,
  1929. segment_count_sit << log_blocks_per_seg);
  1930. return true;
  1931. }
  1932. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  1933. ssa_blkaddr) {
  1934. f2fs_msg(sb, KERN_INFO,
  1935. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  1936. nat_blkaddr, ssa_blkaddr,
  1937. segment_count_nat << log_blocks_per_seg);
  1938. return true;
  1939. }
  1940. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  1941. main_blkaddr) {
  1942. f2fs_msg(sb, KERN_INFO,
  1943. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  1944. ssa_blkaddr, main_blkaddr,
  1945. segment_count_ssa << log_blocks_per_seg);
  1946. return true;
  1947. }
  1948. if (main_end_blkaddr > seg_end_blkaddr) {
  1949. f2fs_msg(sb, KERN_INFO,
  1950. "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
  1951. main_blkaddr,
  1952. segment0_blkaddr +
  1953. (segment_count << log_blocks_per_seg),
  1954. segment_count_main << log_blocks_per_seg);
  1955. return true;
  1956. } else if (main_end_blkaddr < seg_end_blkaddr) {
  1957. int err = 0;
  1958. char *res;
  1959. /* fix in-memory information all the time */
  1960. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  1961. segment0_blkaddr) >> log_blocks_per_seg);
  1962. if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
  1963. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1964. res = "internally";
  1965. } else {
  1966. err = __f2fs_commit_super(bh, NULL);
  1967. res = err ? "failed" : "done";
  1968. }
  1969. f2fs_msg(sb, KERN_INFO,
  1970. "Fix alignment : %s, start(%u) end(%u) block(%u)",
  1971. res, main_blkaddr,
  1972. segment0_blkaddr +
  1973. (segment_count << log_blocks_per_seg),
  1974. segment_count_main << log_blocks_per_seg);
  1975. if (err)
  1976. return true;
  1977. }
  1978. return false;
  1979. }
  1980. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  1981. struct buffer_head *bh)
  1982. {
  1983. block_t segment_count, segs_per_sec, secs_per_zone;
  1984. block_t total_sections, blocks_per_seg;
  1985. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1986. (bh->b_data + F2FS_SUPER_OFFSET);
  1987. struct super_block *sb = sbi->sb;
  1988. unsigned int blocksize;
  1989. if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
  1990. f2fs_msg(sb, KERN_INFO,
  1991. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  1992. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  1993. return -EINVAL;
  1994. }
  1995. /* Currently, support only 4KB page cache size */
  1996. if (F2FS_BLKSIZE != PAGE_SIZE) {
  1997. f2fs_msg(sb, KERN_INFO,
  1998. "Invalid page_cache_size (%lu), supports only 4KB\n",
  1999. PAGE_SIZE);
  2000. return -EFSCORRUPTED;
  2001. }
  2002. /* Currently, support only 4KB block size */
  2003. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  2004. if (blocksize != F2FS_BLKSIZE) {
  2005. f2fs_msg(sb, KERN_INFO,
  2006. "Invalid blocksize (%u), supports only 4KB\n",
  2007. blocksize);
  2008. return -EFSCORRUPTED;
  2009. }
  2010. /* check log blocks per segment */
  2011. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  2012. f2fs_msg(sb, KERN_INFO,
  2013. "Invalid log blocks per segment (%u)\n",
  2014. le32_to_cpu(raw_super->log_blocks_per_seg));
  2015. return -EFSCORRUPTED;
  2016. }
  2017. /* Currently, support 512/1024/2048/4096 bytes sector size */
  2018. if (le32_to_cpu(raw_super->log_sectorsize) >
  2019. F2FS_MAX_LOG_SECTOR_SIZE ||
  2020. le32_to_cpu(raw_super->log_sectorsize) <
  2021. F2FS_MIN_LOG_SECTOR_SIZE) {
  2022. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  2023. le32_to_cpu(raw_super->log_sectorsize));
  2024. return -EFSCORRUPTED;
  2025. }
  2026. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  2027. le32_to_cpu(raw_super->log_sectorsize) !=
  2028. F2FS_MAX_LOG_SECTOR_SIZE) {
  2029. f2fs_msg(sb, KERN_INFO,
  2030. "Invalid log sectors per block(%u) log sectorsize(%u)",
  2031. le32_to_cpu(raw_super->log_sectors_per_block),
  2032. le32_to_cpu(raw_super->log_sectorsize));
  2033. return -EFSCORRUPTED;
  2034. }
  2035. segment_count = le32_to_cpu(raw_super->segment_count);
  2036. segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  2037. secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  2038. total_sections = le32_to_cpu(raw_super->section_count);
  2039. /* blocks_per_seg should be 512, given the above check */
  2040. blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
  2041. if (segment_count > F2FS_MAX_SEGMENT ||
  2042. segment_count < F2FS_MIN_SEGMENTS) {
  2043. f2fs_msg(sb, KERN_INFO,
  2044. "Invalid segment count (%u)",
  2045. segment_count);
  2046. return -EFSCORRUPTED;
  2047. }
  2048. if (total_sections > segment_count ||
  2049. total_sections < F2FS_MIN_SEGMENTS ||
  2050. segs_per_sec > segment_count || !segs_per_sec) {
  2051. f2fs_msg(sb, KERN_INFO,
  2052. "Invalid segment/section count (%u, %u x %u)",
  2053. segment_count, total_sections, segs_per_sec);
  2054. return -EFSCORRUPTED;
  2055. }
  2056. if ((segment_count / segs_per_sec) < total_sections) {
  2057. f2fs_msg(sb, KERN_INFO,
  2058. "Small segment_count (%u < %u * %u)",
  2059. segment_count, segs_per_sec, total_sections);
  2060. return -EFSCORRUPTED;
  2061. }
  2062. if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
  2063. f2fs_msg(sb, KERN_INFO,
  2064. "Wrong segment_count / block_count (%u > %llu)",
  2065. segment_count, le64_to_cpu(raw_super->block_count));
  2066. return -EFSCORRUPTED;
  2067. }
  2068. if (secs_per_zone > total_sections || !secs_per_zone) {
  2069. f2fs_msg(sb, KERN_INFO,
  2070. "Wrong secs_per_zone / total_sections (%u, %u)",
  2071. secs_per_zone, total_sections);
  2072. return -EFSCORRUPTED;
  2073. }
  2074. if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
  2075. raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
  2076. (le32_to_cpu(raw_super->extension_count) +
  2077. raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
  2078. f2fs_msg(sb, KERN_INFO,
  2079. "Corrupted extension count (%u + %u > %u)",
  2080. le32_to_cpu(raw_super->extension_count),
  2081. raw_super->hot_ext_count,
  2082. F2FS_MAX_EXTENSION);
  2083. return -EFSCORRUPTED;
  2084. }
  2085. if (le32_to_cpu(raw_super->cp_payload) >
  2086. (blocks_per_seg - F2FS_CP_PACKS)) {
  2087. f2fs_msg(sb, KERN_INFO,
  2088. "Insane cp_payload (%u > %u)",
  2089. le32_to_cpu(raw_super->cp_payload),
  2090. blocks_per_seg - F2FS_CP_PACKS);
  2091. return -EFSCORRUPTED;
  2092. }
  2093. /* check reserved ino info */
  2094. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  2095. le32_to_cpu(raw_super->meta_ino) != 2 ||
  2096. le32_to_cpu(raw_super->root_ino) != 3) {
  2097. f2fs_msg(sb, KERN_INFO,
  2098. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  2099. le32_to_cpu(raw_super->node_ino),
  2100. le32_to_cpu(raw_super->meta_ino),
  2101. le32_to_cpu(raw_super->root_ino));
  2102. return -EFSCORRUPTED;
  2103. }
  2104. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  2105. if (sanity_check_area_boundary(sbi, bh))
  2106. return -EFSCORRUPTED;
  2107. return 0;
  2108. }
  2109. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
  2110. {
  2111. unsigned int total, fsmeta;
  2112. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2113. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2114. unsigned int ovp_segments, reserved_segments;
  2115. unsigned int main_segs, blocks_per_seg;
  2116. unsigned int sit_segs, nat_segs;
  2117. unsigned int sit_bitmap_size, nat_bitmap_size;
  2118. unsigned int log_blocks_per_seg;
  2119. unsigned int segment_count_main;
  2120. unsigned int cp_pack_start_sum, cp_payload;
  2121. block_t user_block_count;
  2122. int i, j;
  2123. total = le32_to_cpu(raw_super->segment_count);
  2124. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  2125. sit_segs = le32_to_cpu(raw_super->segment_count_sit);
  2126. fsmeta += sit_segs;
  2127. nat_segs = le32_to_cpu(raw_super->segment_count_nat);
  2128. fsmeta += nat_segs;
  2129. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  2130. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  2131. if (unlikely(fsmeta >= total))
  2132. return 1;
  2133. ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  2134. reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  2135. if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
  2136. ovp_segments == 0 || reserved_segments == 0)) {
  2137. f2fs_msg(sbi->sb, KERN_ERR,
  2138. "Wrong layout: check mkfs.f2fs version");
  2139. return 1;
  2140. }
  2141. user_block_count = le64_to_cpu(ckpt->user_block_count);
  2142. segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  2143. log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  2144. if (!user_block_count || user_block_count >=
  2145. segment_count_main << log_blocks_per_seg) {
  2146. f2fs_msg(sbi->sb, KERN_ERR,
  2147. "Wrong user_block_count: %u", user_block_count);
  2148. return 1;
  2149. }
  2150. main_segs = le32_to_cpu(raw_super->segment_count_main);
  2151. blocks_per_seg = sbi->blocks_per_seg;
  2152. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  2153. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  2154. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
  2155. return 1;
  2156. for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
  2157. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  2158. le32_to_cpu(ckpt->cur_node_segno[j])) {
  2159. f2fs_msg(sbi->sb, KERN_ERR,
  2160. "Node segment (%u, %u) has the same "
  2161. "segno: %u", i, j,
  2162. le32_to_cpu(ckpt->cur_node_segno[i]));
  2163. return 1;
  2164. }
  2165. }
  2166. }
  2167. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  2168. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  2169. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
  2170. return 1;
  2171. for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
  2172. if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
  2173. le32_to_cpu(ckpt->cur_data_segno[j])) {
  2174. f2fs_msg(sbi->sb, KERN_ERR,
  2175. "Data segment (%u, %u) has the same "
  2176. "segno: %u", i, j,
  2177. le32_to_cpu(ckpt->cur_data_segno[i]));
  2178. return 1;
  2179. }
  2180. }
  2181. }
  2182. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  2183. for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
  2184. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  2185. le32_to_cpu(ckpt->cur_data_segno[j])) {
  2186. f2fs_msg(sbi->sb, KERN_ERR,
  2187. "Node segment (%u) and Data segment (%u)"
  2188. " has the same segno: %u", i, j,
  2189. le32_to_cpu(ckpt->cur_node_segno[i]));
  2190. return 1;
  2191. }
  2192. }
  2193. }
  2194. sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  2195. nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  2196. if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
  2197. nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
  2198. f2fs_msg(sbi->sb, KERN_ERR,
  2199. "Wrong bitmap size: sit: %u, nat:%u",
  2200. sit_bitmap_size, nat_bitmap_size);
  2201. return 1;
  2202. }
  2203. cp_pack_start_sum = __start_sum_addr(sbi);
  2204. cp_payload = __cp_payload(sbi);
  2205. if (cp_pack_start_sum < cp_payload + 1 ||
  2206. cp_pack_start_sum > blocks_per_seg - 1 -
  2207. NR_CURSEG_TYPE) {
  2208. f2fs_msg(sbi->sb, KERN_ERR,
  2209. "Wrong cp_pack_start_sum: %u",
  2210. cp_pack_start_sum);
  2211. return 1;
  2212. }
  2213. if (unlikely(f2fs_cp_error(sbi))) {
  2214. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  2215. return 1;
  2216. }
  2217. return 0;
  2218. }
  2219. static void init_sb_info(struct f2fs_sb_info *sbi)
  2220. {
  2221. struct f2fs_super_block *raw_super = sbi->raw_super;
  2222. int i, j;
  2223. sbi->log_sectors_per_block =
  2224. le32_to_cpu(raw_super->log_sectors_per_block);
  2225. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  2226. sbi->blocksize = 1 << sbi->log_blocksize;
  2227. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  2228. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  2229. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  2230. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  2231. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  2232. sbi->total_node_count =
  2233. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  2234. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  2235. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  2236. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  2237. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  2238. sbi->cur_victim_sec = NULL_SECNO;
  2239. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  2240. sbi->dir_level = DEF_DIR_LEVEL;
  2241. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  2242. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  2243. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  2244. for (i = 0; i < NR_COUNT_TYPE; i++)
  2245. atomic_set(&sbi->nr_pages[i], 0);
  2246. for (i = 0; i < META; i++)
  2247. atomic_set(&sbi->wb_sync_req[i], 0);
  2248. INIT_LIST_HEAD(&sbi->s_list);
  2249. mutex_init(&sbi->umount_mutex);
  2250. for (i = 0; i < NR_PAGE_TYPE - 1; i++)
  2251. for (j = HOT; j < NR_TEMP_TYPE; j++)
  2252. mutex_init(&sbi->wio_mutex[i][j]);
  2253. init_rwsem(&sbi->io_order_lock);
  2254. spin_lock_init(&sbi->cp_lock);
  2255. sbi->dirty_device = 0;
  2256. spin_lock_init(&sbi->dev_lock);
  2257. init_rwsem(&sbi->sb_lock);
  2258. }
  2259. static int init_percpu_info(struct f2fs_sb_info *sbi)
  2260. {
  2261. int err;
  2262. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
  2263. if (err)
  2264. return err;
  2265. err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
  2266. GFP_KERNEL);
  2267. if (err)
  2268. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  2269. return err;
  2270. }
  2271. #ifdef CONFIG_BLK_DEV_ZONED
  2272. static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
  2273. {
  2274. struct block_device *bdev = FDEV(devi).bdev;
  2275. sector_t nr_sectors = bdev->bd_part->nr_sects;
  2276. sector_t sector = 0;
  2277. struct blk_zone *zones;
  2278. unsigned int i, nr_zones;
  2279. unsigned int n = 0;
  2280. int err = -EIO;
  2281. if (!f2fs_sb_has_blkzoned(sbi->sb))
  2282. return 0;
  2283. if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
  2284. SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
  2285. return -EINVAL;
  2286. sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
  2287. if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
  2288. __ilog2_u32(sbi->blocks_per_blkz))
  2289. return -EINVAL;
  2290. sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
  2291. FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
  2292. sbi->log_blocks_per_blkz;
  2293. if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
  2294. FDEV(devi).nr_blkz++;
  2295. FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
  2296. GFP_KERNEL);
  2297. if (!FDEV(devi).blkz_type)
  2298. return -ENOMEM;
  2299. #define F2FS_REPORT_NR_ZONES 4096
  2300. zones = f2fs_kzalloc(sbi,
  2301. array_size(F2FS_REPORT_NR_ZONES,
  2302. sizeof(struct blk_zone)),
  2303. GFP_KERNEL);
  2304. if (!zones)
  2305. return -ENOMEM;
  2306. /* Get block zones type */
  2307. while (zones && sector < nr_sectors) {
  2308. nr_zones = F2FS_REPORT_NR_ZONES;
  2309. err = blkdev_report_zones(bdev, sector,
  2310. zones, &nr_zones,
  2311. GFP_KERNEL);
  2312. if (err)
  2313. break;
  2314. if (!nr_zones) {
  2315. err = -EIO;
  2316. break;
  2317. }
  2318. for (i = 0; i < nr_zones; i++) {
  2319. FDEV(devi).blkz_type[n] = zones[i].type;
  2320. sector += zones[i].len;
  2321. n++;
  2322. }
  2323. }
  2324. kfree(zones);
  2325. return err;
  2326. }
  2327. #endif
  2328. /*
  2329. * Read f2fs raw super block.
  2330. * Because we have two copies of super block, so read both of them
  2331. * to get the first valid one. If any one of them is broken, we pass
  2332. * them recovery flag back to the caller.
  2333. */
  2334. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  2335. struct f2fs_super_block **raw_super,
  2336. int *valid_super_block, int *recovery)
  2337. {
  2338. struct super_block *sb = sbi->sb;
  2339. int block;
  2340. struct buffer_head *bh;
  2341. struct f2fs_super_block *super;
  2342. int err = 0;
  2343. super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
  2344. if (!super)
  2345. return -ENOMEM;
  2346. for (block = 0; block < 2; block++) {
  2347. bh = sb_bread(sb, block);
  2348. if (!bh) {
  2349. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  2350. block + 1);
  2351. err = -EIO;
  2352. continue;
  2353. }
  2354. /* sanity checking of raw super */
  2355. err = sanity_check_raw_super(sbi, bh);
  2356. if (err) {
  2357. f2fs_msg(sb, KERN_ERR,
  2358. "Can't find valid F2FS filesystem in %dth superblock",
  2359. block + 1);
  2360. brelse(bh);
  2361. continue;
  2362. }
  2363. if (!*raw_super) {
  2364. memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
  2365. sizeof(*super));
  2366. *valid_super_block = block;
  2367. *raw_super = super;
  2368. }
  2369. brelse(bh);
  2370. }
  2371. /* Fail to read any one of the superblocks*/
  2372. if (err < 0)
  2373. *recovery = 1;
  2374. /* No valid superblock */
  2375. if (!*raw_super)
  2376. kfree(super);
  2377. else
  2378. err = 0;
  2379. return err;
  2380. }
  2381. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  2382. {
  2383. struct buffer_head *bh;
  2384. int err;
  2385. if ((recover && f2fs_readonly(sbi->sb)) ||
  2386. bdev_read_only(sbi->sb->s_bdev)) {
  2387. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  2388. return -EROFS;
  2389. }
  2390. /* write back-up superblock first */
  2391. bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
  2392. if (!bh)
  2393. return -EIO;
  2394. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  2395. brelse(bh);
  2396. /* if we are in recovery path, skip writing valid superblock */
  2397. if (recover || err)
  2398. return err;
  2399. /* write current valid superblock */
  2400. bh = sb_bread(sbi->sb, sbi->valid_super_block);
  2401. if (!bh)
  2402. return -EIO;
  2403. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  2404. brelse(bh);
  2405. return err;
  2406. }
  2407. static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
  2408. {
  2409. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2410. unsigned int max_devices = MAX_DEVICES;
  2411. int i;
  2412. /* Initialize single device information */
  2413. if (!RDEV(0).path[0]) {
  2414. if (!bdev_is_zoned(sbi->sb->s_bdev))
  2415. return 0;
  2416. max_devices = 1;
  2417. }
  2418. /*
  2419. * Initialize multiple devices information, or single
  2420. * zoned block device information.
  2421. */
  2422. sbi->devs = f2fs_kzalloc(sbi,
  2423. array_size(max_devices,
  2424. sizeof(struct f2fs_dev_info)),
  2425. GFP_KERNEL);
  2426. if (!sbi->devs)
  2427. return -ENOMEM;
  2428. for (i = 0; i < max_devices; i++) {
  2429. if (i > 0 && !RDEV(i).path[0])
  2430. break;
  2431. if (max_devices == 1) {
  2432. /* Single zoned block device mount */
  2433. FDEV(0).bdev =
  2434. blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
  2435. sbi->sb->s_mode, sbi->sb->s_type);
  2436. } else {
  2437. /* Multi-device mount */
  2438. memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
  2439. FDEV(i).total_segments =
  2440. le32_to_cpu(RDEV(i).total_segments);
  2441. if (i == 0) {
  2442. FDEV(i).start_blk = 0;
  2443. FDEV(i).end_blk = FDEV(i).start_blk +
  2444. (FDEV(i).total_segments <<
  2445. sbi->log_blocks_per_seg) - 1 +
  2446. le32_to_cpu(raw_super->segment0_blkaddr);
  2447. } else {
  2448. FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
  2449. FDEV(i).end_blk = FDEV(i).start_blk +
  2450. (FDEV(i).total_segments <<
  2451. sbi->log_blocks_per_seg) - 1;
  2452. }
  2453. FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
  2454. sbi->sb->s_mode, sbi->sb->s_type);
  2455. }
  2456. if (IS_ERR(FDEV(i).bdev))
  2457. return PTR_ERR(FDEV(i).bdev);
  2458. /* to release errored devices */
  2459. sbi->s_ndevs = i + 1;
  2460. #ifdef CONFIG_BLK_DEV_ZONED
  2461. if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
  2462. !f2fs_sb_has_blkzoned(sbi->sb)) {
  2463. f2fs_msg(sbi->sb, KERN_ERR,
  2464. "Zoned block device feature not enabled\n");
  2465. return -EINVAL;
  2466. }
  2467. if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
  2468. if (init_blkz_info(sbi, i)) {
  2469. f2fs_msg(sbi->sb, KERN_ERR,
  2470. "Failed to initialize F2FS blkzone information");
  2471. return -EINVAL;
  2472. }
  2473. if (max_devices == 1)
  2474. break;
  2475. f2fs_msg(sbi->sb, KERN_INFO,
  2476. "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
  2477. i, FDEV(i).path,
  2478. FDEV(i).total_segments,
  2479. FDEV(i).start_blk, FDEV(i).end_blk,
  2480. bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
  2481. "Host-aware" : "Host-managed");
  2482. continue;
  2483. }
  2484. #endif
  2485. f2fs_msg(sbi->sb, KERN_INFO,
  2486. "Mount Device [%2d]: %20s, %8u, %8x - %8x",
  2487. i, FDEV(i).path,
  2488. FDEV(i).total_segments,
  2489. FDEV(i).start_blk, FDEV(i).end_blk);
  2490. }
  2491. f2fs_msg(sbi->sb, KERN_INFO,
  2492. "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
  2493. return 0;
  2494. }
  2495. static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
  2496. {
  2497. struct f2fs_sm_info *sm_i = SM_I(sbi);
  2498. /* adjust parameters according to the volume size */
  2499. if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
  2500. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
  2501. sm_i->dcc_info->discard_granularity = 1;
  2502. sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
  2503. }
  2504. sbi->readdir_ra = 1;
  2505. }
  2506. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  2507. {
  2508. struct f2fs_sb_info *sbi;
  2509. struct f2fs_super_block *raw_super;
  2510. struct inode *root;
  2511. int err;
  2512. bool retry = true, need_fsck = false;
  2513. char *options = NULL;
  2514. int recovery, i, valid_super_block;
  2515. struct curseg_info *seg_i;
  2516. try_onemore:
  2517. err = -EINVAL;
  2518. raw_super = NULL;
  2519. valid_super_block = -1;
  2520. recovery = 0;
  2521. /* allocate memory for f2fs-specific super block info */
  2522. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  2523. if (!sbi)
  2524. return -ENOMEM;
  2525. sbi->sb = sb;
  2526. /* Load the checksum driver */
  2527. sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
  2528. if (IS_ERR(sbi->s_chksum_driver)) {
  2529. f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
  2530. err = PTR_ERR(sbi->s_chksum_driver);
  2531. sbi->s_chksum_driver = NULL;
  2532. goto free_sbi;
  2533. }
  2534. /* set a block size */
  2535. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  2536. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  2537. goto free_sbi;
  2538. }
  2539. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  2540. &recovery);
  2541. if (err)
  2542. goto free_sbi;
  2543. sb->s_fs_info = sbi;
  2544. sbi->raw_super = raw_super;
  2545. /* precompute checksum seed for metadata */
  2546. if (f2fs_sb_has_inode_chksum(sb))
  2547. sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
  2548. sizeof(raw_super->uuid));
  2549. /*
  2550. * The BLKZONED feature indicates that the drive was formatted with
  2551. * zone alignment optimization. This is optional for host-aware
  2552. * devices, but mandatory for host-managed zoned block devices.
  2553. */
  2554. #ifndef CONFIG_BLK_DEV_ZONED
  2555. if (f2fs_sb_has_blkzoned(sb)) {
  2556. f2fs_msg(sb, KERN_ERR,
  2557. "Zoned block device support is not enabled\n");
  2558. err = -EOPNOTSUPP;
  2559. goto free_sb_buf;
  2560. }
  2561. #endif
  2562. default_options(sbi);
  2563. /* parse mount options */
  2564. options = kstrdup((const char *)data, GFP_KERNEL);
  2565. if (data && !options) {
  2566. err = -ENOMEM;
  2567. goto free_sb_buf;
  2568. }
  2569. err = parse_options(sb, options);
  2570. if (err)
  2571. goto free_options;
  2572. sbi->max_file_blocks = max_file_blocks();
  2573. sb->s_maxbytes = sbi->max_file_blocks <<
  2574. le32_to_cpu(raw_super->log_blocksize);
  2575. sb->s_max_links = F2FS_LINK_MAX;
  2576. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  2577. #ifdef CONFIG_QUOTA
  2578. sb->dq_op = &f2fs_quota_operations;
  2579. if (f2fs_sb_has_quota_ino(sb))
  2580. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  2581. else
  2582. sb->s_qcop = &f2fs_quotactl_ops;
  2583. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  2584. if (f2fs_sb_has_quota_ino(sbi->sb)) {
  2585. for (i = 0; i < MAXQUOTAS; i++) {
  2586. if (f2fs_qf_ino(sbi->sb, i))
  2587. sbi->nquota_files++;
  2588. }
  2589. }
  2590. #endif
  2591. sb->s_op = &f2fs_sops;
  2592. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2593. sb->s_cop = &f2fs_cryptops;
  2594. #endif
  2595. sb->s_xattr = f2fs_xattr_handlers;
  2596. sb->s_export_op = &f2fs_export_ops;
  2597. sb->s_magic = F2FS_SUPER_MAGIC;
  2598. sb->s_time_gran = 1;
  2599. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  2600. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  2601. memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  2602. sb->s_iflags |= SB_I_CGROUPWB;
  2603. /* init f2fs-specific super block info */
  2604. sbi->valid_super_block = valid_super_block;
  2605. mutex_init(&sbi->gc_mutex);
  2606. mutex_init(&sbi->writepages);
  2607. mutex_init(&sbi->cp_mutex);
  2608. init_rwsem(&sbi->node_write);
  2609. init_rwsem(&sbi->node_change);
  2610. /* disallow all the data/node/meta page writes */
  2611. set_sbi_flag(sbi, SBI_POR_DOING);
  2612. spin_lock_init(&sbi->stat_lock);
  2613. /* init iostat info */
  2614. spin_lock_init(&sbi->iostat_lock);
  2615. sbi->iostat_enable = false;
  2616. for (i = 0; i < NR_PAGE_TYPE; i++) {
  2617. int n = (i == META) ? 1: NR_TEMP_TYPE;
  2618. int j;
  2619. sbi->write_io[i] =
  2620. f2fs_kmalloc(sbi,
  2621. array_size(n,
  2622. sizeof(struct f2fs_bio_info)),
  2623. GFP_KERNEL);
  2624. if (!sbi->write_io[i]) {
  2625. err = -ENOMEM;
  2626. goto free_bio_info;
  2627. }
  2628. for (j = HOT; j < n; j++) {
  2629. init_rwsem(&sbi->write_io[i][j].io_rwsem);
  2630. sbi->write_io[i][j].sbi = sbi;
  2631. sbi->write_io[i][j].bio = NULL;
  2632. spin_lock_init(&sbi->write_io[i][j].io_lock);
  2633. INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
  2634. }
  2635. }
  2636. init_rwsem(&sbi->cp_rwsem);
  2637. init_waitqueue_head(&sbi->cp_wait);
  2638. init_sb_info(sbi);
  2639. err = init_percpu_info(sbi);
  2640. if (err)
  2641. goto free_bio_info;
  2642. if (F2FS_IO_SIZE(sbi) > 1) {
  2643. sbi->write_io_dummy =
  2644. mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
  2645. if (!sbi->write_io_dummy) {
  2646. err = -ENOMEM;
  2647. goto free_percpu;
  2648. }
  2649. }
  2650. /* get an inode for meta space */
  2651. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  2652. if (IS_ERR(sbi->meta_inode)) {
  2653. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  2654. err = PTR_ERR(sbi->meta_inode);
  2655. goto free_io_dummy;
  2656. }
  2657. err = f2fs_get_valid_checkpoint(sbi);
  2658. if (err) {
  2659. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  2660. goto free_meta_inode;
  2661. }
  2662. /* Initialize device list */
  2663. err = f2fs_scan_devices(sbi);
  2664. if (err) {
  2665. f2fs_msg(sb, KERN_ERR, "Failed to find devices");
  2666. goto free_devices;
  2667. }
  2668. sbi->total_valid_node_count =
  2669. le32_to_cpu(sbi->ckpt->valid_node_count);
  2670. percpu_counter_set(&sbi->total_valid_inode_count,
  2671. le32_to_cpu(sbi->ckpt->valid_inode_count));
  2672. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  2673. sbi->total_valid_block_count =
  2674. le64_to_cpu(sbi->ckpt->valid_block_count);
  2675. sbi->last_valid_block_count = sbi->total_valid_block_count;
  2676. sbi->reserved_blocks = 0;
  2677. sbi->current_reserved_blocks = 0;
  2678. limit_reserve_root(sbi);
  2679. for (i = 0; i < NR_INODE_TYPE; i++) {
  2680. INIT_LIST_HEAD(&sbi->inode_list[i]);
  2681. spin_lock_init(&sbi->inode_lock[i]);
  2682. }
  2683. f2fs_init_extent_cache_info(sbi);
  2684. f2fs_init_ino_entry_info(sbi);
  2685. f2fs_init_fsync_node_info(sbi);
  2686. /* setup f2fs internal modules */
  2687. err = f2fs_build_segment_manager(sbi);
  2688. if (err) {
  2689. f2fs_msg(sb, KERN_ERR,
  2690. "Failed to initialize F2FS segment manager");
  2691. goto free_sm;
  2692. }
  2693. err = f2fs_build_node_manager(sbi);
  2694. if (err) {
  2695. f2fs_msg(sb, KERN_ERR,
  2696. "Failed to initialize F2FS node manager");
  2697. goto free_nm;
  2698. }
  2699. /* For write statistics */
  2700. if (sb->s_bdev->bd_part)
  2701. sbi->sectors_written_start =
  2702. (u64)part_stat_read(sb->s_bdev->bd_part,
  2703. sectors[STAT_WRITE]);
  2704. /* Read accumulated write IO statistics if exists */
  2705. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  2706. if (__exist_node_summaries(sbi))
  2707. sbi->kbytes_written =
  2708. le64_to_cpu(seg_i->journal->info.kbytes_written);
  2709. f2fs_build_gc_manager(sbi);
  2710. err = f2fs_build_stats(sbi);
  2711. if (err)
  2712. goto free_nm;
  2713. /* get an inode for node space */
  2714. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  2715. if (IS_ERR(sbi->node_inode)) {
  2716. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  2717. err = PTR_ERR(sbi->node_inode);
  2718. goto free_stats;
  2719. }
  2720. /* read root inode and dentry */
  2721. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  2722. if (IS_ERR(root)) {
  2723. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  2724. err = PTR_ERR(root);
  2725. goto free_node_inode;
  2726. }
  2727. if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
  2728. !root->i_size || !root->i_nlink) {
  2729. iput(root);
  2730. err = -EINVAL;
  2731. goto free_node_inode;
  2732. }
  2733. sb->s_root = d_make_root(root); /* allocate root dentry */
  2734. if (!sb->s_root) {
  2735. err = -ENOMEM;
  2736. goto free_root_inode;
  2737. }
  2738. err = f2fs_register_sysfs(sbi);
  2739. if (err)
  2740. goto free_root_inode;
  2741. #ifdef CONFIG_QUOTA
  2742. /* Enable quota usage during mount */
  2743. if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
  2744. err = f2fs_enable_quotas(sb);
  2745. if (err) {
  2746. f2fs_msg(sb, KERN_ERR,
  2747. "Cannot turn on quotas: error %d", err);
  2748. goto free_sysfs;
  2749. }
  2750. }
  2751. #endif
  2752. /* if there are nt orphan nodes free them */
  2753. err = f2fs_recover_orphan_inodes(sbi);
  2754. if (err)
  2755. goto free_meta;
  2756. /* recover fsynced data */
  2757. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  2758. /*
  2759. * mount should be failed, when device has readonly mode, and
  2760. * previous checkpoint was not done by clean system shutdown.
  2761. */
  2762. if (bdev_read_only(sb->s_bdev) &&
  2763. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  2764. err = -EROFS;
  2765. goto free_meta;
  2766. }
  2767. if (need_fsck)
  2768. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2769. if (!retry)
  2770. goto skip_recovery;
  2771. err = f2fs_recover_fsync_data(sbi, false);
  2772. if (err < 0) {
  2773. need_fsck = true;
  2774. f2fs_msg(sb, KERN_ERR,
  2775. "Cannot recover all fsync data errno=%d", err);
  2776. goto free_meta;
  2777. }
  2778. } else {
  2779. err = f2fs_recover_fsync_data(sbi, true);
  2780. if (!f2fs_readonly(sb) && err > 0) {
  2781. err = -EINVAL;
  2782. f2fs_msg(sb, KERN_ERR,
  2783. "Need to recover fsync data");
  2784. goto free_meta;
  2785. }
  2786. }
  2787. skip_recovery:
  2788. /* f2fs_recover_fsync_data() cleared this already */
  2789. clear_sbi_flag(sbi, SBI_POR_DOING);
  2790. /*
  2791. * If filesystem is not mounted as read-only then
  2792. * do start the gc_thread.
  2793. */
  2794. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  2795. /* After POR, we can run background GC thread.*/
  2796. err = f2fs_start_gc_thread(sbi);
  2797. if (err)
  2798. goto free_meta;
  2799. }
  2800. kfree(options);
  2801. /* recover broken superblock */
  2802. if (recovery) {
  2803. err = f2fs_commit_super(sbi, true);
  2804. f2fs_msg(sb, KERN_INFO,
  2805. "Try to recover %dth superblock, ret: %d",
  2806. sbi->valid_super_block ? 1 : 2, err);
  2807. }
  2808. f2fs_join_shrinker(sbi);
  2809. f2fs_tuning_parameters(sbi);
  2810. f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
  2811. cur_cp_version(F2FS_CKPT(sbi)));
  2812. f2fs_update_time(sbi, CP_TIME);
  2813. f2fs_update_time(sbi, REQ_TIME);
  2814. return 0;
  2815. free_meta:
  2816. #ifdef CONFIG_QUOTA
  2817. f2fs_truncate_quota_inode_pages(sb);
  2818. if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
  2819. f2fs_quota_off_umount(sbi->sb);
  2820. #endif
  2821. /*
  2822. * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
  2823. * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
  2824. * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
  2825. * falls into an infinite loop in f2fs_sync_meta_pages().
  2826. */
  2827. truncate_inode_pages_final(META_MAPPING(sbi));
  2828. #ifdef CONFIG_QUOTA
  2829. free_sysfs:
  2830. #endif
  2831. f2fs_unregister_sysfs(sbi);
  2832. free_root_inode:
  2833. dput(sb->s_root);
  2834. sb->s_root = NULL;
  2835. free_node_inode:
  2836. f2fs_release_ino_entry(sbi, true);
  2837. truncate_inode_pages_final(NODE_MAPPING(sbi));
  2838. iput(sbi->node_inode);
  2839. sbi->node_inode = NULL;
  2840. free_stats:
  2841. f2fs_destroy_stats(sbi);
  2842. free_nm:
  2843. f2fs_destroy_node_manager(sbi);
  2844. free_sm:
  2845. f2fs_destroy_segment_manager(sbi);
  2846. free_devices:
  2847. destroy_device_list(sbi);
  2848. kfree(sbi->ckpt);
  2849. free_meta_inode:
  2850. make_bad_inode(sbi->meta_inode);
  2851. iput(sbi->meta_inode);
  2852. sbi->meta_inode = NULL;
  2853. free_io_dummy:
  2854. mempool_destroy(sbi->write_io_dummy);
  2855. free_percpu:
  2856. destroy_percpu_info(sbi);
  2857. free_bio_info:
  2858. for (i = 0; i < NR_PAGE_TYPE; i++)
  2859. kfree(sbi->write_io[i]);
  2860. free_options:
  2861. #ifdef CONFIG_QUOTA
  2862. for (i = 0; i < MAXQUOTAS; i++)
  2863. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  2864. #endif
  2865. kfree(options);
  2866. free_sb_buf:
  2867. kfree(raw_super);
  2868. free_sbi:
  2869. if (sbi->s_chksum_driver)
  2870. crypto_free_shash(sbi->s_chksum_driver);
  2871. kfree(sbi);
  2872. /* give only one another chance */
  2873. if (retry) {
  2874. retry = false;
  2875. shrink_dcache_sb(sb);
  2876. goto try_onemore;
  2877. }
  2878. return err;
  2879. }
  2880. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  2881. const char *dev_name, void *data)
  2882. {
  2883. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  2884. }
  2885. static void kill_f2fs_super(struct super_block *sb)
  2886. {
  2887. if (sb->s_root) {
  2888. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2889. set_sbi_flag(sbi, SBI_IS_CLOSE);
  2890. f2fs_stop_gc_thread(sbi);
  2891. f2fs_stop_discard_thread(sbi);
  2892. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  2893. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  2894. struct cp_control cpc = {
  2895. .reason = CP_UMOUNT,
  2896. };
  2897. f2fs_write_checkpoint(sbi, &cpc);
  2898. }
  2899. if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
  2900. sb->s_flags &= ~SB_RDONLY;
  2901. }
  2902. kill_block_super(sb);
  2903. }
  2904. static struct file_system_type f2fs_fs_type = {
  2905. .owner = THIS_MODULE,
  2906. .name = "f2fs",
  2907. .mount = f2fs_mount,
  2908. .kill_sb = kill_f2fs_super,
  2909. .fs_flags = FS_REQUIRES_DEV,
  2910. };
  2911. MODULE_ALIAS_FS("f2fs");
  2912. static int __init init_inodecache(void)
  2913. {
  2914. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  2915. sizeof(struct f2fs_inode_info), 0,
  2916. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  2917. if (!f2fs_inode_cachep)
  2918. return -ENOMEM;
  2919. return 0;
  2920. }
  2921. static void destroy_inodecache(void)
  2922. {
  2923. /*
  2924. * Make sure all delayed rcu free inodes are flushed before we
  2925. * destroy cache.
  2926. */
  2927. rcu_barrier();
  2928. kmem_cache_destroy(f2fs_inode_cachep);
  2929. }
  2930. static int __init init_f2fs_fs(void)
  2931. {
  2932. int err;
  2933. if (PAGE_SIZE != F2FS_BLKSIZE) {
  2934. printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
  2935. PAGE_SIZE, F2FS_BLKSIZE);
  2936. return -EINVAL;
  2937. }
  2938. f2fs_build_trace_ios();
  2939. err = init_inodecache();
  2940. if (err)
  2941. goto fail;
  2942. err = f2fs_create_node_manager_caches();
  2943. if (err)
  2944. goto free_inodecache;
  2945. err = f2fs_create_segment_manager_caches();
  2946. if (err)
  2947. goto free_node_manager_caches;
  2948. err = f2fs_create_checkpoint_caches();
  2949. if (err)
  2950. goto free_segment_manager_caches;
  2951. err = f2fs_create_extent_cache();
  2952. if (err)
  2953. goto free_checkpoint_caches;
  2954. err = f2fs_init_sysfs();
  2955. if (err)
  2956. goto free_extent_cache;
  2957. err = register_shrinker(&f2fs_shrinker_info);
  2958. if (err)
  2959. goto free_sysfs;
  2960. err = register_filesystem(&f2fs_fs_type);
  2961. if (err)
  2962. goto free_shrinker;
  2963. err = f2fs_create_root_stats();
  2964. if (err)
  2965. goto free_filesystem;
  2966. err = f2fs_init_post_read_processing();
  2967. if (err)
  2968. goto free_root_stats;
  2969. return 0;
  2970. free_root_stats:
  2971. f2fs_destroy_root_stats();
  2972. free_filesystem:
  2973. unregister_filesystem(&f2fs_fs_type);
  2974. free_shrinker:
  2975. unregister_shrinker(&f2fs_shrinker_info);
  2976. free_sysfs:
  2977. f2fs_exit_sysfs();
  2978. free_extent_cache:
  2979. f2fs_destroy_extent_cache();
  2980. free_checkpoint_caches:
  2981. f2fs_destroy_checkpoint_caches();
  2982. free_segment_manager_caches:
  2983. f2fs_destroy_segment_manager_caches();
  2984. free_node_manager_caches:
  2985. f2fs_destroy_node_manager_caches();
  2986. free_inodecache:
  2987. destroy_inodecache();
  2988. fail:
  2989. return err;
  2990. }
  2991. static void __exit exit_f2fs_fs(void)
  2992. {
  2993. f2fs_destroy_post_read_processing();
  2994. f2fs_destroy_root_stats();
  2995. unregister_filesystem(&f2fs_fs_type);
  2996. unregister_shrinker(&f2fs_shrinker_info);
  2997. f2fs_exit_sysfs();
  2998. f2fs_destroy_extent_cache();
  2999. f2fs_destroy_checkpoint_caches();
  3000. f2fs_destroy_segment_manager_caches();
  3001. f2fs_destroy_node_manager_caches();
  3002. destroy_inodecache();
  3003. f2fs_destroy_trace_ios();
  3004. }
  3005. module_init(init_f2fs_fs)
  3006. module_exit(exit_f2fs_fs)
  3007. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  3008. MODULE_DESCRIPTION("Flash Friendly File System");
  3009. MODULE_LICENSE("GPL");