super.c 81 KB

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