super.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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/f2fs_fs.h>
  26. #include <linux/sysfs.h>
  27. #include "f2fs.h"
  28. #include "node.h"
  29. #include "segment.h"
  30. #include "xattr.h"
  31. #include "gc.h"
  32. #include "trace.h"
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/f2fs.h>
  35. static struct proc_dir_entry *f2fs_proc_root;
  36. static struct kmem_cache *f2fs_inode_cachep;
  37. static struct kset *f2fs_kset;
  38. #ifdef CONFIG_F2FS_FAULT_INJECTION
  39. char *fault_name[FAULT_MAX] = {
  40. [FAULT_KMALLOC] = "kmalloc",
  41. [FAULT_PAGE_ALLOC] = "page alloc",
  42. [FAULT_ALLOC_NID] = "alloc nid",
  43. [FAULT_ORPHAN] = "orphan",
  44. [FAULT_BLOCK] = "no more block",
  45. [FAULT_DIR_DEPTH] = "too big dir depth",
  46. [FAULT_EVICT_INODE] = "evict_inode fail",
  47. [FAULT_IO] = "IO error",
  48. [FAULT_CHECKPOINT] = "checkpoint error",
  49. };
  50. static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
  51. unsigned int rate)
  52. {
  53. struct f2fs_fault_info *ffi = &sbi->fault_info;
  54. if (rate) {
  55. atomic_set(&ffi->inject_ops, 0);
  56. ffi->inject_rate = rate;
  57. ffi->inject_type = (1 << FAULT_MAX) - 1;
  58. } else {
  59. memset(ffi, 0, sizeof(struct f2fs_fault_info));
  60. }
  61. }
  62. #endif
  63. /* f2fs-wide shrinker description */
  64. static struct shrinker f2fs_shrinker_info = {
  65. .shrink = f2fs_shrink_scan,
  66. .seeks = DEFAULT_SEEKS,
  67. };
  68. enum {
  69. Opt_gc_background,
  70. Opt_disable_roll_forward,
  71. Opt_norecovery,
  72. Opt_discard,
  73. Opt_nodiscard,
  74. Opt_noheap,
  75. Opt_user_xattr,
  76. Opt_nouser_xattr,
  77. Opt_acl,
  78. Opt_noacl,
  79. Opt_active_logs,
  80. Opt_disable_ext_identify,
  81. Opt_inline_xattr,
  82. Opt_inline_data,
  83. Opt_inline_dentry,
  84. Opt_noinline_dentry,
  85. Opt_flush_merge,
  86. Opt_noflush_merge,
  87. Opt_nobarrier,
  88. Opt_fastboot,
  89. Opt_extent_cache,
  90. Opt_noextent_cache,
  91. Opt_noinline_data,
  92. Opt_data_flush,
  93. Opt_mode,
  94. Opt_fault_injection,
  95. Opt_err,
  96. };
  97. static match_table_t f2fs_tokens = {
  98. {Opt_gc_background, "background_gc=%s"},
  99. {Opt_disable_roll_forward, "disable_roll_forward"},
  100. {Opt_norecovery, "norecovery"},
  101. {Opt_discard, "discard"},
  102. {Opt_nodiscard, "nodiscard"},
  103. {Opt_noheap, "no_heap"},
  104. {Opt_user_xattr, "user_xattr"},
  105. {Opt_nouser_xattr, "nouser_xattr"},
  106. {Opt_acl, "acl"},
  107. {Opt_noacl, "noacl"},
  108. {Opt_active_logs, "active_logs=%u"},
  109. {Opt_disable_ext_identify, "disable_ext_identify"},
  110. {Opt_inline_xattr, "inline_xattr"},
  111. {Opt_inline_data, "inline_data"},
  112. {Opt_inline_dentry, "inline_dentry"},
  113. {Opt_noinline_dentry, "noinline_dentry"},
  114. {Opt_flush_merge, "flush_merge"},
  115. {Opt_noflush_merge, "noflush_merge"},
  116. {Opt_nobarrier, "nobarrier"},
  117. {Opt_fastboot, "fastboot"},
  118. {Opt_extent_cache, "extent_cache"},
  119. {Opt_noextent_cache, "noextent_cache"},
  120. {Opt_noinline_data, "noinline_data"},
  121. {Opt_data_flush, "data_flush"},
  122. {Opt_mode, "mode=%s"},
  123. {Opt_fault_injection, "fault_injection=%u"},
  124. {Opt_err, NULL},
  125. };
  126. /* Sysfs support for f2fs */
  127. enum {
  128. GC_THREAD, /* struct f2fs_gc_thread */
  129. SM_INFO, /* struct f2fs_sm_info */
  130. NM_INFO, /* struct f2fs_nm_info */
  131. F2FS_SBI, /* struct f2fs_sb_info */
  132. #ifdef CONFIG_F2FS_FAULT_INJECTION
  133. FAULT_INFO_RATE, /* struct f2fs_fault_info */
  134. FAULT_INFO_TYPE, /* struct f2fs_fault_info */
  135. #endif
  136. };
  137. struct f2fs_attr {
  138. struct attribute attr;
  139. ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
  140. ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
  141. const char *, size_t);
  142. int struct_type;
  143. int offset;
  144. };
  145. static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  146. {
  147. if (struct_type == GC_THREAD)
  148. return (unsigned char *)sbi->gc_thread;
  149. else if (struct_type == SM_INFO)
  150. return (unsigned char *)SM_I(sbi);
  151. else if (struct_type == NM_INFO)
  152. return (unsigned char *)NM_I(sbi);
  153. else if (struct_type == F2FS_SBI)
  154. return (unsigned char *)sbi;
  155. #ifdef CONFIG_F2FS_FAULT_INJECTION
  156. else if (struct_type == FAULT_INFO_RATE ||
  157. struct_type == FAULT_INFO_TYPE)
  158. return (unsigned char *)&sbi->fault_info;
  159. #endif
  160. return NULL;
  161. }
  162. static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
  163. struct f2fs_sb_info *sbi, char *buf)
  164. {
  165. struct super_block *sb = sbi->sb;
  166. if (!sb->s_bdev->bd_part)
  167. return snprintf(buf, PAGE_SIZE, "0\n");
  168. return snprintf(buf, PAGE_SIZE, "%llu\n",
  169. (unsigned long long)(sbi->kbytes_written +
  170. BD_PART_WRITTEN(sbi)));
  171. }
  172. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  173. struct f2fs_sb_info *sbi, char *buf)
  174. {
  175. unsigned char *ptr = NULL;
  176. unsigned int *ui;
  177. ptr = __struct_ptr(sbi, a->struct_type);
  178. if (!ptr)
  179. return -EINVAL;
  180. ui = (unsigned int *)(ptr + a->offset);
  181. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  182. }
  183. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  184. struct f2fs_sb_info *sbi,
  185. const char *buf, size_t count)
  186. {
  187. unsigned char *ptr;
  188. unsigned long t;
  189. unsigned int *ui;
  190. ssize_t ret;
  191. ptr = __struct_ptr(sbi, a->struct_type);
  192. if (!ptr)
  193. return -EINVAL;
  194. ui = (unsigned int *)(ptr + a->offset);
  195. ret = kstrtoul(skip_spaces(buf), 0, &t);
  196. if (ret < 0)
  197. return ret;
  198. #ifdef CONFIG_F2FS_FAULT_INJECTION
  199. if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
  200. return -EINVAL;
  201. #endif
  202. *ui = t;
  203. return count;
  204. }
  205. static ssize_t f2fs_attr_show(struct kobject *kobj,
  206. struct attribute *attr, char *buf)
  207. {
  208. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  209. s_kobj);
  210. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  211. return a->show ? a->show(a, sbi, buf) : 0;
  212. }
  213. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  214. const char *buf, size_t len)
  215. {
  216. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  217. s_kobj);
  218. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  219. return a->store ? a->store(a, sbi, buf, len) : 0;
  220. }
  221. static void f2fs_sb_release(struct kobject *kobj)
  222. {
  223. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  224. s_kobj);
  225. complete(&sbi->s_kobj_unregister);
  226. }
  227. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  228. static struct f2fs_attr f2fs_attr_##_name = { \
  229. .attr = {.name = __stringify(_name), .mode = _mode }, \
  230. .show = _show, \
  231. .store = _store, \
  232. .struct_type = _struct_type, \
  233. .offset = _offset \
  234. }
  235. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  236. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  237. f2fs_sbi_show, f2fs_sbi_store, \
  238. offsetof(struct struct_name, elname))
  239. #define F2FS_GENERAL_RO_ATTR(name) \
  240. static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  241. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
  242. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
  243. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
  244. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
  245. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
  246. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, max_small_discards, max_discards);
  247. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
  248. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
  249. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
  250. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
  251. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
  252. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
  253. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
  254. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
  255. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
  256. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
  257. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
  258. #ifdef CONFIG_F2FS_FAULT_INJECTION
  259. F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
  260. F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
  261. #endif
  262. F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
  263. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  264. static struct attribute *f2fs_attrs[] = {
  265. ATTR_LIST(gc_min_sleep_time),
  266. ATTR_LIST(gc_max_sleep_time),
  267. ATTR_LIST(gc_no_gc_sleep_time),
  268. ATTR_LIST(gc_idle),
  269. ATTR_LIST(reclaim_segments),
  270. ATTR_LIST(max_small_discards),
  271. ATTR_LIST(batched_trim_sections),
  272. ATTR_LIST(ipu_policy),
  273. ATTR_LIST(min_ipu_util),
  274. ATTR_LIST(min_fsync_blocks),
  275. ATTR_LIST(max_victim_search),
  276. ATTR_LIST(dir_level),
  277. ATTR_LIST(ram_thresh),
  278. ATTR_LIST(ra_nid_pages),
  279. ATTR_LIST(dirty_nats_ratio),
  280. ATTR_LIST(cp_interval),
  281. ATTR_LIST(idle_interval),
  282. #ifdef CONFIG_F2FS_FAULT_INJECTION
  283. ATTR_LIST(inject_rate),
  284. ATTR_LIST(inject_type),
  285. #endif
  286. ATTR_LIST(lifetime_write_kbytes),
  287. NULL,
  288. };
  289. static const struct sysfs_ops f2fs_attr_ops = {
  290. .show = f2fs_attr_show,
  291. .store = f2fs_attr_store,
  292. };
  293. static struct kobj_type f2fs_ktype = {
  294. .default_attrs = f2fs_attrs,
  295. .sysfs_ops = &f2fs_attr_ops,
  296. .release = f2fs_sb_release,
  297. };
  298. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  299. {
  300. struct va_format vaf;
  301. va_list args;
  302. va_start(args, fmt);
  303. vaf.fmt = fmt;
  304. vaf.va = &args;
  305. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  306. va_end(args);
  307. }
  308. static void init_once(void *foo)
  309. {
  310. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  311. inode_init_once(&fi->vfs_inode);
  312. }
  313. static int parse_options(struct super_block *sb, char *options)
  314. {
  315. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  316. struct request_queue *q;
  317. substring_t args[MAX_OPT_ARGS];
  318. char *p, *name;
  319. int arg = 0;
  320. if (!options)
  321. return 0;
  322. while ((p = strsep(&options, ",")) != NULL) {
  323. int token;
  324. if (!*p)
  325. continue;
  326. /*
  327. * Initialize args struct so we know whether arg was
  328. * found; some options take optional arguments.
  329. */
  330. args[0].to = args[0].from = NULL;
  331. token = match_token(p, f2fs_tokens, args);
  332. switch (token) {
  333. case Opt_gc_background:
  334. name = match_strdup(&args[0]);
  335. if (!name)
  336. return -ENOMEM;
  337. if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
  338. set_opt(sbi, BG_GC);
  339. clear_opt(sbi, FORCE_FG_GC);
  340. } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
  341. clear_opt(sbi, BG_GC);
  342. clear_opt(sbi, FORCE_FG_GC);
  343. } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
  344. set_opt(sbi, BG_GC);
  345. set_opt(sbi, FORCE_FG_GC);
  346. } else {
  347. kfree(name);
  348. return -EINVAL;
  349. }
  350. kfree(name);
  351. break;
  352. case Opt_disable_roll_forward:
  353. set_opt(sbi, DISABLE_ROLL_FORWARD);
  354. break;
  355. case Opt_norecovery:
  356. /* this option mounts f2fs with ro */
  357. set_opt(sbi, DISABLE_ROLL_FORWARD);
  358. if (!f2fs_readonly(sb))
  359. return -EINVAL;
  360. break;
  361. case Opt_discard:
  362. q = bdev_get_queue(sb->s_bdev);
  363. if (blk_queue_discard(q)) {
  364. set_opt(sbi, DISCARD);
  365. } else if (!f2fs_sb_mounted_blkzoned(sb)) {
  366. f2fs_msg(sb, KERN_WARNING,
  367. "mounting with \"discard\" option, but "
  368. "the device does not support discard");
  369. }
  370. break;
  371. case Opt_nodiscard:
  372. if (f2fs_sb_mounted_blkzoned(sb)) {
  373. f2fs_msg(sb, KERN_WARNING,
  374. "discard is required for zoned block devices");
  375. return -EINVAL;
  376. }
  377. clear_opt(sbi, DISCARD);
  378. break;
  379. case Opt_noheap:
  380. set_opt(sbi, NOHEAP);
  381. break;
  382. #ifdef CONFIG_F2FS_FS_XATTR
  383. case Opt_user_xattr:
  384. set_opt(sbi, XATTR_USER);
  385. break;
  386. case Opt_nouser_xattr:
  387. clear_opt(sbi, XATTR_USER);
  388. break;
  389. case Opt_inline_xattr:
  390. set_opt(sbi, INLINE_XATTR);
  391. break;
  392. #else
  393. case Opt_user_xattr:
  394. f2fs_msg(sb, KERN_INFO,
  395. "user_xattr options not supported");
  396. break;
  397. case Opt_nouser_xattr:
  398. f2fs_msg(sb, KERN_INFO,
  399. "nouser_xattr options not supported");
  400. break;
  401. case Opt_inline_xattr:
  402. f2fs_msg(sb, KERN_INFO,
  403. "inline_xattr options not supported");
  404. break;
  405. #endif
  406. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  407. case Opt_acl:
  408. set_opt(sbi, POSIX_ACL);
  409. break;
  410. case Opt_noacl:
  411. clear_opt(sbi, POSIX_ACL);
  412. break;
  413. #else
  414. case Opt_acl:
  415. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  416. break;
  417. case Opt_noacl:
  418. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  419. break;
  420. #endif
  421. case Opt_active_logs:
  422. if (args->from && match_int(args, &arg))
  423. return -EINVAL;
  424. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  425. return -EINVAL;
  426. sbi->active_logs = arg;
  427. break;
  428. case Opt_disable_ext_identify:
  429. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  430. break;
  431. case Opt_inline_data:
  432. set_opt(sbi, INLINE_DATA);
  433. break;
  434. case Opt_inline_dentry:
  435. set_opt(sbi, INLINE_DENTRY);
  436. break;
  437. case Opt_noinline_dentry:
  438. clear_opt(sbi, INLINE_DENTRY);
  439. break;
  440. case Opt_flush_merge:
  441. set_opt(sbi, FLUSH_MERGE);
  442. break;
  443. case Opt_noflush_merge:
  444. clear_opt(sbi, FLUSH_MERGE);
  445. break;
  446. case Opt_nobarrier:
  447. set_opt(sbi, NOBARRIER);
  448. break;
  449. case Opt_fastboot:
  450. set_opt(sbi, FASTBOOT);
  451. break;
  452. case Opt_extent_cache:
  453. set_opt(sbi, EXTENT_CACHE);
  454. break;
  455. case Opt_noextent_cache:
  456. clear_opt(sbi, EXTENT_CACHE);
  457. break;
  458. case Opt_noinline_data:
  459. clear_opt(sbi, INLINE_DATA);
  460. break;
  461. case Opt_data_flush:
  462. set_opt(sbi, DATA_FLUSH);
  463. break;
  464. case Opt_mode:
  465. name = match_strdup(&args[0]);
  466. if (!name)
  467. return -ENOMEM;
  468. if (strlen(name) == 8 &&
  469. !strncmp(name, "adaptive", 8)) {
  470. if (f2fs_sb_mounted_blkzoned(sb)) {
  471. f2fs_msg(sb, KERN_WARNING,
  472. "adaptive mode is not allowed with "
  473. "zoned block device feature");
  474. kfree(name);
  475. return -EINVAL;
  476. }
  477. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  478. } else if (strlen(name) == 3 &&
  479. !strncmp(name, "lfs", 3)) {
  480. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  481. } else {
  482. kfree(name);
  483. return -EINVAL;
  484. }
  485. kfree(name);
  486. break;
  487. case Opt_fault_injection:
  488. if (args->from && match_int(args, &arg))
  489. return -EINVAL;
  490. #ifdef CONFIG_F2FS_FAULT_INJECTION
  491. f2fs_build_fault_attr(sbi, arg);
  492. #else
  493. f2fs_msg(sb, KERN_INFO,
  494. "FAULT_INJECTION was not selected");
  495. #endif
  496. break;
  497. default:
  498. f2fs_msg(sb, KERN_ERR,
  499. "Unrecognized mount option \"%s\" or missing value",
  500. p);
  501. return -EINVAL;
  502. }
  503. }
  504. return 0;
  505. }
  506. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  507. {
  508. struct f2fs_inode_info *fi;
  509. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  510. if (!fi)
  511. return NULL;
  512. init_once((void *) fi);
  513. if (percpu_counter_init(&fi->dirty_pages, 0)) {
  514. kmem_cache_free(f2fs_inode_cachep, fi);
  515. return NULL;
  516. }
  517. /* Initialize f2fs-specific inode info */
  518. fi->vfs_inode.i_version = 1;
  519. fi->i_current_depth = 1;
  520. fi->i_advise = 0;
  521. init_rwsem(&fi->i_sem);
  522. INIT_LIST_HEAD(&fi->dirty_list);
  523. INIT_LIST_HEAD(&fi->gdirty_list);
  524. INIT_LIST_HEAD(&fi->inmem_pages);
  525. mutex_init(&fi->inmem_lock);
  526. init_rwsem(&fi->dio_rwsem[READ]);
  527. init_rwsem(&fi->dio_rwsem[WRITE]);
  528. /* Will be used by directory only */
  529. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  530. return &fi->vfs_inode;
  531. }
  532. static int f2fs_drop_inode(struct inode *inode)
  533. {
  534. /*
  535. * This is to avoid a deadlock condition like below.
  536. * writeback_single_inode(inode)
  537. * - f2fs_write_data_page
  538. * - f2fs_gc -> iput -> evict
  539. * - inode_wait_for_writeback(inode)
  540. */
  541. if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
  542. if (!inode->i_nlink && !is_bad_inode(inode)) {
  543. /* to avoid evict_inode call simultaneously */
  544. atomic_inc(&inode->i_count);
  545. spin_unlock(&inode->i_lock);
  546. /* some remained atomic pages should discarded */
  547. if (f2fs_is_atomic_file(inode))
  548. drop_inmem_pages(inode);
  549. /* should remain fi->extent_tree for writepage */
  550. f2fs_destroy_extent_node(inode);
  551. f2fs_i_size_write(inode, 0);
  552. if (F2FS_HAS_BLOCKS(inode))
  553. f2fs_truncate(inode);
  554. fscrypt_put_encryption_info(inode, NULL);
  555. spin_lock(&inode->i_lock);
  556. atomic_dec(&inode->i_count);
  557. }
  558. return 0;
  559. }
  560. return generic_drop_inode(inode);
  561. }
  562. int f2fs_inode_dirtied(struct inode *inode, bool sync)
  563. {
  564. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  565. int ret = 0;
  566. spin_lock(&sbi->inode_lock[DIRTY_META]);
  567. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  568. ret = 1;
  569. } else {
  570. set_inode_flag(inode, FI_DIRTY_INODE);
  571. stat_inc_dirty_inode(sbi, DIRTY_META);
  572. }
  573. if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
  574. list_add_tail(&F2FS_I(inode)->gdirty_list,
  575. &sbi->inode_list[DIRTY_META]);
  576. inc_page_count(sbi, F2FS_DIRTY_IMETA);
  577. }
  578. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  579. return ret;
  580. }
  581. void f2fs_inode_synced(struct inode *inode)
  582. {
  583. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  584. spin_lock(&sbi->inode_lock[DIRTY_META]);
  585. if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  586. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  587. return;
  588. }
  589. if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
  590. list_del_init(&F2FS_I(inode)->gdirty_list);
  591. dec_page_count(sbi, F2FS_DIRTY_IMETA);
  592. }
  593. clear_inode_flag(inode, FI_DIRTY_INODE);
  594. clear_inode_flag(inode, FI_AUTO_RECOVER);
  595. stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
  596. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  597. }
  598. /*
  599. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  600. *
  601. * We should call set_dirty_inode to write the dirty inode through write_inode.
  602. */
  603. static void f2fs_dirty_inode(struct inode *inode, int flags)
  604. {
  605. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  606. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  607. inode->i_ino == F2FS_META_INO(sbi))
  608. return;
  609. if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
  610. clear_inode_flag(inode, FI_AUTO_RECOVER);
  611. f2fs_inode_dirtied(inode, false);
  612. }
  613. static void f2fs_i_callback(struct rcu_head *head)
  614. {
  615. struct inode *inode = container_of(head, struct inode, i_rcu);
  616. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  617. }
  618. static void f2fs_destroy_inode(struct inode *inode)
  619. {
  620. percpu_counter_destroy(&F2FS_I(inode)->dirty_pages);
  621. call_rcu(&inode->i_rcu, f2fs_i_callback);
  622. }
  623. static void destroy_percpu_info(struct f2fs_sb_info *sbi)
  624. {
  625. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  626. percpu_counter_destroy(&sbi->total_valid_inode_count);
  627. }
  628. static void f2fs_put_super(struct super_block *sb)
  629. {
  630. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  631. if (sbi->s_proc) {
  632. remove_proc_entry("segment_info", sbi->s_proc);
  633. remove_proc_entry("segment_bits", sbi->s_proc);
  634. remove_proc_entry(sb->s_id, f2fs_proc_root);
  635. }
  636. kobject_del(&sbi->s_kobj);
  637. stop_gc_thread(sbi);
  638. /* prevent remaining shrinker jobs */
  639. mutex_lock(&sbi->umount_mutex);
  640. /*
  641. * We don't need to do checkpoint when superblock is clean.
  642. * But, the previous checkpoint was not done by umount, it needs to do
  643. * clean checkpoint again.
  644. */
  645. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  646. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  647. struct cp_control cpc = {
  648. .reason = CP_UMOUNT,
  649. };
  650. write_checkpoint(sbi, &cpc);
  651. }
  652. /* write_checkpoint can update stat informaion */
  653. f2fs_destroy_stats(sbi);
  654. /*
  655. * normally superblock is clean, so we need to release this.
  656. * In addition, EIO will skip do checkpoint, we need this as well.
  657. */
  658. release_ino_entry(sbi, true);
  659. f2fs_leave_shrinker(sbi);
  660. mutex_unlock(&sbi->umount_mutex);
  661. /* our cp_error case, we can wait for any writeback page */
  662. f2fs_flush_merged_bios(sbi);
  663. iput(sbi->node_inode);
  664. iput(sbi->meta_inode);
  665. /* destroy f2fs internal modules */
  666. destroy_node_manager(sbi);
  667. destroy_segment_manager(sbi);
  668. kfree(sbi->ckpt);
  669. kobject_put(&sbi->s_kobj);
  670. wait_for_completion(&sbi->s_kobj_unregister);
  671. sb->s_fs_info = NULL;
  672. kfree(sbi->raw_super);
  673. destroy_percpu_info(sbi);
  674. kfree(sbi);
  675. }
  676. int f2fs_sync_fs(struct super_block *sb, int sync)
  677. {
  678. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  679. int err = 0;
  680. trace_f2fs_sync_fs(sb, sync);
  681. if (sync) {
  682. struct cp_control cpc;
  683. cpc.reason = __get_cp_reason(sbi);
  684. mutex_lock(&sbi->gc_mutex);
  685. err = write_checkpoint(sbi, &cpc);
  686. mutex_unlock(&sbi->gc_mutex);
  687. }
  688. f2fs_trace_ios(NULL, 1);
  689. return err;
  690. }
  691. static int f2fs_freeze(struct super_block *sb)
  692. {
  693. if (f2fs_readonly(sb))
  694. return 0;
  695. /* IO error happened before */
  696. if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
  697. return -EIO;
  698. /* must be clean, since sync_filesystem() was already called */
  699. if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
  700. return -EINVAL;
  701. return 0;
  702. }
  703. static int f2fs_unfreeze(struct super_block *sb)
  704. {
  705. return 0;
  706. }
  707. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  708. {
  709. struct super_block *sb = dentry->d_sb;
  710. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  711. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  712. block_t total_count, user_block_count, start_count, ovp_count;
  713. total_count = le64_to_cpu(sbi->raw_super->block_count);
  714. user_block_count = sbi->user_block_count;
  715. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  716. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  717. buf->f_type = F2FS_SUPER_MAGIC;
  718. buf->f_bsize = sbi->blocksize;
  719. buf->f_blocks = total_count - start_count;
  720. buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
  721. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  722. buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  723. buf->f_ffree = min(buf->f_files - valid_node_count(sbi),
  724. buf->f_bavail);
  725. buf->f_namelen = F2FS_NAME_LEN;
  726. buf->f_fsid.val[0] = (u32)id;
  727. buf->f_fsid.val[1] = (u32)(id >> 32);
  728. return 0;
  729. }
  730. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  731. {
  732. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  733. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
  734. if (test_opt(sbi, FORCE_FG_GC))
  735. seq_printf(seq, ",background_gc=%s", "sync");
  736. else
  737. seq_printf(seq, ",background_gc=%s", "on");
  738. } else {
  739. seq_printf(seq, ",background_gc=%s", "off");
  740. }
  741. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  742. seq_puts(seq, ",disable_roll_forward");
  743. if (test_opt(sbi, DISCARD))
  744. seq_puts(seq, ",discard");
  745. if (test_opt(sbi, NOHEAP))
  746. seq_puts(seq, ",no_heap_alloc");
  747. #ifdef CONFIG_F2FS_FS_XATTR
  748. if (test_opt(sbi, XATTR_USER))
  749. seq_puts(seq, ",user_xattr");
  750. else
  751. seq_puts(seq, ",nouser_xattr");
  752. if (test_opt(sbi, INLINE_XATTR))
  753. seq_puts(seq, ",inline_xattr");
  754. #endif
  755. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  756. if (test_opt(sbi, POSIX_ACL))
  757. seq_puts(seq, ",acl");
  758. else
  759. seq_puts(seq, ",noacl");
  760. #endif
  761. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  762. seq_puts(seq, ",disable_ext_identify");
  763. if (test_opt(sbi, INLINE_DATA))
  764. seq_puts(seq, ",inline_data");
  765. else
  766. seq_puts(seq, ",noinline_data");
  767. if (test_opt(sbi, INLINE_DENTRY))
  768. seq_puts(seq, ",inline_dentry");
  769. else
  770. seq_puts(seq, ",noinline_dentry");
  771. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  772. seq_puts(seq, ",flush_merge");
  773. if (test_opt(sbi, NOBARRIER))
  774. seq_puts(seq, ",nobarrier");
  775. if (test_opt(sbi, FASTBOOT))
  776. seq_puts(seq, ",fastboot");
  777. if (test_opt(sbi, EXTENT_CACHE))
  778. seq_puts(seq, ",extent_cache");
  779. else
  780. seq_puts(seq, ",noextent_cache");
  781. if (test_opt(sbi, DATA_FLUSH))
  782. seq_puts(seq, ",data_flush");
  783. seq_puts(seq, ",mode=");
  784. if (test_opt(sbi, ADAPTIVE))
  785. seq_puts(seq, "adaptive");
  786. else if (test_opt(sbi, LFS))
  787. seq_puts(seq, "lfs");
  788. return 0;
  789. }
  790. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  791. {
  792. struct super_block *sb = seq->private;
  793. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  794. unsigned int total_segs =
  795. le32_to_cpu(sbi->raw_super->segment_count_main);
  796. int i;
  797. seq_puts(seq, "format: segment_type|valid_blocks\n"
  798. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  799. for (i = 0; i < total_segs; i++) {
  800. struct seg_entry *se = get_seg_entry(sbi, i);
  801. if ((i % 10) == 0)
  802. seq_printf(seq, "%-10d", i);
  803. seq_printf(seq, "%d|%-3u", se->type,
  804. get_valid_blocks(sbi, i, 1));
  805. if ((i % 10) == 9 || i == (total_segs - 1))
  806. seq_putc(seq, '\n');
  807. else
  808. seq_putc(seq, ' ');
  809. }
  810. return 0;
  811. }
  812. static int segment_bits_seq_show(struct seq_file *seq, void *offset)
  813. {
  814. struct super_block *sb = seq->private;
  815. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  816. unsigned int total_segs =
  817. le32_to_cpu(sbi->raw_super->segment_count_main);
  818. int i, j;
  819. seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
  820. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  821. for (i = 0; i < total_segs; i++) {
  822. struct seg_entry *se = get_seg_entry(sbi, i);
  823. seq_printf(seq, "%-10d", i);
  824. seq_printf(seq, "%d|%-3u|", se->type,
  825. get_valid_blocks(sbi, i, 1));
  826. for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
  827. seq_printf(seq, " %.2x", se->cur_valid_map[j]);
  828. seq_putc(seq, '\n');
  829. }
  830. return 0;
  831. }
  832. #define F2FS_PROC_FILE_DEF(_name) \
  833. static int _name##_open_fs(struct inode *inode, struct file *file) \
  834. { \
  835. return single_open(file, _name##_seq_show, PDE(inode)->data); \
  836. } \
  837. \
  838. static const struct file_operations f2fs_seq_##_name##_fops = { \
  839. .owner = THIS_MODULE, \
  840. .open = _name##_open_fs, \
  841. .read = seq_read, \
  842. .llseek = seq_lseek, \
  843. .release = single_release, \
  844. };
  845. F2FS_PROC_FILE_DEF(segment_info);
  846. F2FS_PROC_FILE_DEF(segment_bits);
  847. static void default_options(struct f2fs_sb_info *sbi)
  848. {
  849. /* init some FS parameters */
  850. sbi->active_logs = NR_CURSEG_TYPE;
  851. set_opt(sbi, BG_GC);
  852. set_opt(sbi, INLINE_DATA);
  853. set_opt(sbi, INLINE_DENTRY);
  854. set_opt(sbi, EXTENT_CACHE);
  855. set_opt(sbi, FLUSH_MERGE);
  856. if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
  857. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  858. set_opt(sbi, DISCARD);
  859. } else {
  860. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  861. }
  862. #ifdef CONFIG_F2FS_FS_XATTR
  863. set_opt(sbi, XATTR_USER);
  864. #endif
  865. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  866. set_opt(sbi, POSIX_ACL);
  867. #endif
  868. #ifdef CONFIG_F2FS_FAULT_INJECTION
  869. f2fs_build_fault_attr(sbi, 0);
  870. #endif
  871. }
  872. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  873. {
  874. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  875. struct f2fs_mount_info org_mount_opt;
  876. int err, active_logs;
  877. bool need_restart_gc = false;
  878. bool need_stop_gc = false;
  879. bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
  880. #ifdef CONFIG_F2FS_FAULT_INJECTION
  881. struct f2fs_fault_info ffi = sbi->fault_info;
  882. #endif
  883. sync_filesystem(sb);
  884. /*
  885. * Save the old mount options in case we
  886. * need to restore them.
  887. */
  888. org_mount_opt = sbi->mount_opt;
  889. active_logs = sbi->active_logs;
  890. /* recover superblocks we couldn't write due to previous RO mount */
  891. if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  892. err = f2fs_commit_super(sbi, false);
  893. f2fs_msg(sb, KERN_INFO,
  894. "Try to recover all the superblocks, ret: %d", err);
  895. if (!err)
  896. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  897. }
  898. sbi->mount_opt.opt = 0;
  899. default_options(sbi);
  900. /* parse mount options */
  901. err = parse_options(sb, data);
  902. if (err)
  903. goto restore_opts;
  904. /*
  905. * Previous and new state of filesystem is RO,
  906. * so skip checking GC and FLUSH_MERGE conditions.
  907. */
  908. if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
  909. goto skip;
  910. /* disallow enable/disable extent_cache dynamically */
  911. if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
  912. err = -EINVAL;
  913. f2fs_msg(sbi->sb, KERN_WARNING,
  914. "switch extent_cache option is not allowed");
  915. goto restore_opts;
  916. }
  917. /*
  918. * We stop the GC thread if FS is mounted as RO
  919. * or if background_gc = off is passed in mount
  920. * option. Also sync the filesystem.
  921. */
  922. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  923. if (sbi->gc_thread) {
  924. stop_gc_thread(sbi);
  925. need_restart_gc = true;
  926. }
  927. } else if (!sbi->gc_thread) {
  928. err = start_gc_thread(sbi);
  929. if (err)
  930. goto restore_opts;
  931. need_stop_gc = true;
  932. }
  933. if (*flags & MS_RDONLY) {
  934. writeback_inodes_sb(sb, WB_REASON_SYNC);
  935. sync_inodes_sb(sb);
  936. set_sbi_flag(sbi, SBI_IS_DIRTY);
  937. set_sbi_flag(sbi, SBI_IS_CLOSE);
  938. f2fs_sync_fs(sb, 1);
  939. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  940. }
  941. /*
  942. * We stop issue flush thread if FS is mounted as RO
  943. * or if flush_merge is not passed in mount option.
  944. */
  945. if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  946. clear_opt(sbi, FLUSH_MERGE);
  947. destroy_flush_cmd_control(sbi, false);
  948. } else {
  949. err = create_flush_cmd_control(sbi);
  950. if (err)
  951. goto restore_gc;
  952. }
  953. skip:
  954. /* Update the POSIXACL Flag */
  955. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  956. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  957. return 0;
  958. restore_gc:
  959. if (need_restart_gc) {
  960. if (start_gc_thread(sbi))
  961. f2fs_msg(sbi->sb, KERN_WARNING,
  962. "background gc thread has stopped");
  963. } else if (need_stop_gc) {
  964. stop_gc_thread(sbi);
  965. }
  966. restore_opts:
  967. sbi->mount_opt = org_mount_opt;
  968. sbi->active_logs = active_logs;
  969. #ifdef CONFIG_F2FS_FAULT_INJECTION
  970. sbi->fault_info = ffi;
  971. #endif
  972. return err;
  973. }
  974. static struct super_operations f2fs_sops = {
  975. .alloc_inode = f2fs_alloc_inode,
  976. .drop_inode = f2fs_drop_inode,
  977. .destroy_inode = f2fs_destroy_inode,
  978. .write_inode = f2fs_write_inode,
  979. .dirty_inode = f2fs_dirty_inode,
  980. .show_options = f2fs_show_options,
  981. .evict_inode = f2fs_evict_inode,
  982. .put_super = f2fs_put_super,
  983. .sync_fs = f2fs_sync_fs,
  984. .freeze_fs = f2fs_freeze,
  985. .unfreeze_fs = f2fs_unfreeze,
  986. .statfs = f2fs_statfs,
  987. .remount_fs = f2fs_remount,
  988. };
  989. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  990. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  991. {
  992. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  993. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  994. ctx, len, NULL);
  995. }
  996. static int f2fs_key_prefix(struct inode *inode, u8 **key)
  997. {
  998. *key = F2FS_I_SB(inode)->key_prefix;
  999. return F2FS_I_SB(inode)->key_prefix_size;
  1000. }
  1001. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  1002. void *fs_data)
  1003. {
  1004. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  1005. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  1006. ctx, len, fs_data, XATTR_CREATE);
  1007. }
  1008. static unsigned f2fs_max_namelen(struct inode *inode)
  1009. {
  1010. return S_ISLNK(inode->i_mode) ?
  1011. inode->i_sb->s_blocksize : F2FS_NAME_LEN;
  1012. }
  1013. static struct fscrypt_operations f2fs_cryptops = {
  1014. .get_context = f2fs_get_context,
  1015. .key_prefix = f2fs_key_prefix,
  1016. .set_context = f2fs_set_context,
  1017. .is_encrypted = f2fs_encrypted_inode,
  1018. .empty_dir = f2fs_empty_dir,
  1019. .max_namelen = f2fs_max_namelen,
  1020. };
  1021. #else
  1022. static struct fscrypt_operations f2fs_cryptops = {
  1023. .is_encrypted = f2fs_encrypted_inode,
  1024. };
  1025. #endif
  1026. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  1027. u64 ino, u32 generation)
  1028. {
  1029. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1030. struct inode *inode;
  1031. if (check_nid_range(sbi, ino))
  1032. return ERR_PTR(-ESTALE);
  1033. /*
  1034. * f2fs_iget isn't quite right if the inode is currently unallocated!
  1035. * However f2fs_iget currently does appropriate checks to handle stale
  1036. * inodes so everything is OK.
  1037. */
  1038. inode = f2fs_iget(sb, ino);
  1039. if (IS_ERR(inode))
  1040. return ERR_CAST(inode);
  1041. if (unlikely(generation && inode->i_generation != generation)) {
  1042. /* we didn't find the right inode.. */
  1043. iput(inode);
  1044. return ERR_PTR(-ESTALE);
  1045. }
  1046. return inode;
  1047. }
  1048. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1049. int fh_len, int fh_type)
  1050. {
  1051. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1052. f2fs_nfs_get_inode);
  1053. }
  1054. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1055. int fh_len, int fh_type)
  1056. {
  1057. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1058. f2fs_nfs_get_inode);
  1059. }
  1060. static const struct export_operations f2fs_export_ops = {
  1061. .fh_to_dentry = f2fs_fh_to_dentry,
  1062. .fh_to_parent = f2fs_fh_to_parent,
  1063. .get_parent = f2fs_get_parent,
  1064. };
  1065. static loff_t max_file_blocks(void)
  1066. {
  1067. loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
  1068. loff_t leaf_count = ADDRS_PER_BLOCK;
  1069. /* two direct node blocks */
  1070. result += (leaf_count * 2);
  1071. /* two indirect node blocks */
  1072. leaf_count *= NIDS_PER_BLOCK;
  1073. result += (leaf_count * 2);
  1074. /* one double indirect node block */
  1075. leaf_count *= NIDS_PER_BLOCK;
  1076. result += leaf_count;
  1077. return result;
  1078. }
  1079. static int __f2fs_commit_super(struct buffer_head *bh,
  1080. struct f2fs_super_block *super)
  1081. {
  1082. lock_buffer(bh);
  1083. if (super)
  1084. memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
  1085. set_buffer_uptodate(bh);
  1086. set_buffer_dirty(bh);
  1087. unlock_buffer(bh);
  1088. /* it's rare case, we can do fua all the time */
  1089. return __sync_dirty_buffer(bh, WRITE_FLUSH_FUA);
  1090. }
  1091. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  1092. struct buffer_head *bh)
  1093. {
  1094. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1095. (bh->b_data + F2FS_SUPER_OFFSET);
  1096. struct super_block *sb = sbi->sb;
  1097. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1098. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  1099. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  1100. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  1101. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1102. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1103. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  1104. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  1105. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  1106. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  1107. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  1108. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  1109. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1110. u64 main_end_blkaddr = main_blkaddr +
  1111. (segment_count_main << log_blocks_per_seg);
  1112. u64 seg_end_blkaddr = segment0_blkaddr +
  1113. (segment_count << log_blocks_per_seg);
  1114. if (segment0_blkaddr != cp_blkaddr) {
  1115. f2fs_msg(sb, KERN_INFO,
  1116. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  1117. segment0_blkaddr, cp_blkaddr);
  1118. return true;
  1119. }
  1120. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  1121. sit_blkaddr) {
  1122. f2fs_msg(sb, KERN_INFO,
  1123. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  1124. cp_blkaddr, sit_blkaddr,
  1125. segment_count_ckpt << log_blocks_per_seg);
  1126. return true;
  1127. }
  1128. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  1129. nat_blkaddr) {
  1130. f2fs_msg(sb, KERN_INFO,
  1131. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  1132. sit_blkaddr, nat_blkaddr,
  1133. segment_count_sit << log_blocks_per_seg);
  1134. return true;
  1135. }
  1136. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  1137. ssa_blkaddr) {
  1138. f2fs_msg(sb, KERN_INFO,
  1139. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  1140. nat_blkaddr, ssa_blkaddr,
  1141. segment_count_nat << log_blocks_per_seg);
  1142. return true;
  1143. }
  1144. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  1145. main_blkaddr) {
  1146. f2fs_msg(sb, KERN_INFO,
  1147. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  1148. ssa_blkaddr, main_blkaddr,
  1149. segment_count_ssa << log_blocks_per_seg);
  1150. return true;
  1151. }
  1152. if (main_end_blkaddr > seg_end_blkaddr) {
  1153. f2fs_msg(sb, KERN_INFO,
  1154. "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
  1155. main_blkaddr,
  1156. segment0_blkaddr +
  1157. (segment_count << log_blocks_per_seg),
  1158. segment_count_main << log_blocks_per_seg);
  1159. return true;
  1160. } else if (main_end_blkaddr < seg_end_blkaddr) {
  1161. int err = 0;
  1162. char *res;
  1163. /* fix in-memory information all the time */
  1164. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  1165. segment0_blkaddr) >> log_blocks_per_seg);
  1166. if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
  1167. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1168. res = "internally";
  1169. } else {
  1170. err = __f2fs_commit_super(bh, NULL);
  1171. res = err ? "failed" : "done";
  1172. }
  1173. f2fs_msg(sb, KERN_INFO,
  1174. "Fix alignment : %s, start(%u) end(%u) block(%u)",
  1175. res, main_blkaddr,
  1176. segment0_blkaddr +
  1177. (segment_count << log_blocks_per_seg),
  1178. segment_count_main << log_blocks_per_seg);
  1179. if (err)
  1180. return true;
  1181. }
  1182. return false;
  1183. }
  1184. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  1185. struct buffer_head *bh)
  1186. {
  1187. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1188. (bh->b_data + F2FS_SUPER_OFFSET);
  1189. struct super_block *sb = sbi->sb;
  1190. unsigned int blocksize;
  1191. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  1192. f2fs_msg(sb, KERN_INFO,
  1193. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  1194. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  1195. return 1;
  1196. }
  1197. /* Currently, support only 4KB page cache size */
  1198. if (F2FS_BLKSIZE != PAGE_SIZE) {
  1199. f2fs_msg(sb, KERN_INFO,
  1200. "Invalid page_cache_size (%lu), supports only 4KB\n",
  1201. PAGE_SIZE);
  1202. return 1;
  1203. }
  1204. /* Currently, support only 4KB block size */
  1205. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  1206. if (blocksize != F2FS_BLKSIZE) {
  1207. f2fs_msg(sb, KERN_INFO,
  1208. "Invalid blocksize (%u), supports only 4KB\n",
  1209. blocksize);
  1210. return 1;
  1211. }
  1212. /* check log blocks per segment */
  1213. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  1214. f2fs_msg(sb, KERN_INFO,
  1215. "Invalid log blocks per segment (%u)\n",
  1216. le32_to_cpu(raw_super->log_blocks_per_seg));
  1217. return 1;
  1218. }
  1219. /* Currently, support 512/1024/2048/4096 bytes sector size */
  1220. if (le32_to_cpu(raw_super->log_sectorsize) >
  1221. F2FS_MAX_LOG_SECTOR_SIZE ||
  1222. le32_to_cpu(raw_super->log_sectorsize) <
  1223. F2FS_MIN_LOG_SECTOR_SIZE) {
  1224. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  1225. le32_to_cpu(raw_super->log_sectorsize));
  1226. return 1;
  1227. }
  1228. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  1229. le32_to_cpu(raw_super->log_sectorsize) !=
  1230. F2FS_MAX_LOG_SECTOR_SIZE) {
  1231. f2fs_msg(sb, KERN_INFO,
  1232. "Invalid log sectors per block(%u) log sectorsize(%u)",
  1233. le32_to_cpu(raw_super->log_sectors_per_block),
  1234. le32_to_cpu(raw_super->log_sectorsize));
  1235. return 1;
  1236. }
  1237. /* check reserved ino info */
  1238. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  1239. le32_to_cpu(raw_super->meta_ino) != 2 ||
  1240. le32_to_cpu(raw_super->root_ino) != 3) {
  1241. f2fs_msg(sb, KERN_INFO,
  1242. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  1243. le32_to_cpu(raw_super->node_ino),
  1244. le32_to_cpu(raw_super->meta_ino),
  1245. le32_to_cpu(raw_super->root_ino));
  1246. return 1;
  1247. }
  1248. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  1249. if (sanity_check_area_boundary(sbi, bh))
  1250. return 1;
  1251. if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
  1252. f2fs_msg(sb, KERN_INFO,
  1253. "Invalid segment count (%u)",
  1254. le32_to_cpu(raw_super->segment_count));
  1255. return 1;
  1256. }
  1257. return 0;
  1258. }
  1259. int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  1260. {
  1261. unsigned int total, fsmeta;
  1262. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1263. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1264. unsigned int main_segs, blocks_per_seg;
  1265. unsigned int cp_pack_start_sum, cp_payload;
  1266. unsigned int ovp_segments, reserved_segments;
  1267. int i;
  1268. total = le32_to_cpu(raw_super->segment_count);
  1269. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  1270. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  1271. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  1272. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  1273. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  1274. if (unlikely(fsmeta >= total))
  1275. return 1;
  1276. ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1277. reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1278. if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
  1279. ovp_segments == 0 || reserved_segments == 0)) {
  1280. f2fs_msg(sbi->sb, KERN_ERR,
  1281. "Wrong layout: check mkfs.f2fs version");
  1282. return 1;
  1283. }
  1284. main_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
  1285. blocks_per_seg = sbi->blocks_per_seg;
  1286. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  1287. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  1288. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg) {
  1289. return 1;
  1290. }
  1291. }
  1292. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  1293. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  1294. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) {
  1295. return 1;
  1296. }
  1297. }
  1298. cp_pack_start_sum = __start_sum_addr(sbi);
  1299. cp_payload = __cp_payload(sbi);
  1300. if (cp_pack_start_sum < cp_payload + 1 ||
  1301. cp_pack_start_sum > blocks_per_seg - 1 -
  1302. NR_CURSEG_TYPE) {
  1303. f2fs_msg(sbi->sb, KERN_ERR,
  1304. "Wrong cp_pack_start_sum: %u",
  1305. cp_pack_start_sum);
  1306. return 1;
  1307. }
  1308. if (unlikely(f2fs_cp_error(sbi))) {
  1309. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  1310. return 1;
  1311. }
  1312. return 0;
  1313. }
  1314. static void init_sb_info(struct f2fs_sb_info *sbi)
  1315. {
  1316. struct f2fs_super_block *raw_super = sbi->raw_super;
  1317. int i;
  1318. sbi->log_sectors_per_block =
  1319. le32_to_cpu(raw_super->log_sectors_per_block);
  1320. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  1321. sbi->blocksize = 1 << sbi->log_blocksize;
  1322. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1323. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  1324. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  1325. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  1326. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  1327. sbi->total_node_count =
  1328. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  1329. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  1330. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  1331. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  1332. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  1333. sbi->cur_victim_sec = NULL_SECNO;
  1334. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  1335. sbi->dir_level = DEF_DIR_LEVEL;
  1336. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  1337. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  1338. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  1339. for (i = 0; i < NR_COUNT_TYPE; i++)
  1340. atomic_set(&sbi->nr_pages[i], 0);
  1341. INIT_LIST_HEAD(&sbi->s_list);
  1342. mutex_init(&sbi->umount_mutex);
  1343. mutex_init(&sbi->wio_mutex[NODE]);
  1344. mutex_init(&sbi->wio_mutex[DATA]);
  1345. spin_lock_init(&sbi->cp_lock);
  1346. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1347. memcpy(sbi->key_prefix, F2FS_KEY_DESC_PREFIX,
  1348. F2FS_KEY_DESC_PREFIX_SIZE);
  1349. sbi->key_prefix_size = F2FS_KEY_DESC_PREFIX_SIZE;
  1350. #endif
  1351. }
  1352. static int init_percpu_info(struct f2fs_sb_info *sbi)
  1353. {
  1354. int err;
  1355. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0);
  1356. if (err)
  1357. return err;
  1358. return percpu_counter_init(&sbi->total_valid_inode_count, 0);
  1359. }
  1360. #ifdef CONFIG_BLK_DEV_ZONED
  1361. static int init_blkz_info(struct f2fs_sb_info *sbi)
  1362. {
  1363. struct block_device *bdev = sbi->sb->s_bdev;
  1364. sector_t nr_sectors = bdev->bd_part->nr_sects;
  1365. sector_t sector = 0;
  1366. struct blk_zone *zones;
  1367. unsigned int i, nr_zones;
  1368. unsigned int n = 0;
  1369. int err = -EIO;
  1370. if (!f2fs_sb_mounted_blkzoned(sbi->sb))
  1371. return 0;
  1372. sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_size(bdev));
  1373. sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
  1374. sbi->nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
  1375. sbi->log_blocks_per_blkz;
  1376. if (nr_sectors & (bdev_zone_size(bdev) - 1))
  1377. sbi->nr_blkz++;
  1378. sbi->blkz_type = kmalloc(sbi->nr_blkz, GFP_KERNEL);
  1379. if (!sbi->blkz_type)
  1380. return -ENOMEM;
  1381. #define F2FS_REPORT_NR_ZONES 4096
  1382. zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
  1383. GFP_KERNEL);
  1384. if (!zones)
  1385. return -ENOMEM;
  1386. /* Get block zones type */
  1387. while (zones && sector < nr_sectors) {
  1388. nr_zones = F2FS_REPORT_NR_ZONES;
  1389. err = blkdev_report_zones(bdev, sector,
  1390. zones, &nr_zones,
  1391. GFP_KERNEL);
  1392. if (err)
  1393. break;
  1394. if (!nr_zones) {
  1395. err = -EIO;
  1396. break;
  1397. }
  1398. for (i = 0; i < nr_zones; i++) {
  1399. sbi->blkz_type[n] = zones[i].type;
  1400. sector += zones[i].len;
  1401. n++;
  1402. }
  1403. }
  1404. kfree(zones);
  1405. return err;
  1406. }
  1407. #endif
  1408. /*
  1409. * Read f2fs raw super block.
  1410. * Because we have two copies of super block, so read both of them
  1411. * to get the first valid one. If any one of them is broken, we pass
  1412. * them recovery flag back to the caller.
  1413. */
  1414. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  1415. struct f2fs_super_block **raw_super,
  1416. int *valid_super_block, int *recovery)
  1417. {
  1418. struct super_block *sb = sbi->sb;
  1419. int block;
  1420. struct buffer_head *bh;
  1421. struct f2fs_super_block *super;
  1422. int err = 0;
  1423. super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
  1424. if (!super)
  1425. return -ENOMEM;
  1426. for (block = 0; block < 2; block++) {
  1427. bh = sb_bread(sb, block);
  1428. if (!bh) {
  1429. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  1430. block + 1);
  1431. err = -EIO;
  1432. continue;
  1433. }
  1434. /* sanity checking of raw super */
  1435. if (sanity_check_raw_super(sbi, bh)) {
  1436. f2fs_msg(sb, KERN_ERR,
  1437. "Can't find valid F2FS filesystem in %dth superblock",
  1438. block + 1);
  1439. err = -EINVAL;
  1440. brelse(bh);
  1441. continue;
  1442. }
  1443. if (!*raw_super) {
  1444. memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
  1445. sizeof(*super));
  1446. *valid_super_block = block;
  1447. *raw_super = super;
  1448. }
  1449. brelse(bh);
  1450. }
  1451. /* Fail to read any one of the superblocks*/
  1452. if (err < 0)
  1453. *recovery = 1;
  1454. /* No valid superblock */
  1455. if (!*raw_super)
  1456. kfree(super);
  1457. else
  1458. err = 0;
  1459. return err;
  1460. }
  1461. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  1462. {
  1463. struct buffer_head *bh;
  1464. int err;
  1465. if ((recover && f2fs_readonly(sbi->sb)) ||
  1466. bdev_read_only(sbi->sb->s_bdev)) {
  1467. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1468. return -EROFS;
  1469. }
  1470. /* write back-up superblock first */
  1471. bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
  1472. if (!bh)
  1473. return -EIO;
  1474. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1475. brelse(bh);
  1476. /* if we are in recovery path, skip writing valid superblock */
  1477. if (recover || err)
  1478. return err;
  1479. /* write current valid superblock */
  1480. bh = sb_getblk(sbi->sb, sbi->valid_super_block);
  1481. if (!bh)
  1482. return -EIO;
  1483. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1484. brelse(bh);
  1485. return err;
  1486. }
  1487. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  1488. {
  1489. struct f2fs_sb_info *sbi;
  1490. struct f2fs_super_block *raw_super;
  1491. struct inode *root;
  1492. int err;
  1493. bool retry = true, need_fsck = false;
  1494. char *options = NULL;
  1495. int recovery, i, valid_super_block;
  1496. struct curseg_info *seg_i;
  1497. try_onemore:
  1498. err = -EINVAL;
  1499. raw_super = NULL;
  1500. valid_super_block = -1;
  1501. recovery = 0;
  1502. /* allocate memory for f2fs-specific super block info */
  1503. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  1504. if (!sbi)
  1505. return -ENOMEM;
  1506. sbi->sb = sb;
  1507. /* set a block size */
  1508. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  1509. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  1510. goto free_sbi;
  1511. }
  1512. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  1513. &recovery);
  1514. if (err)
  1515. goto free_sbi;
  1516. sb->s_fs_info = sbi;
  1517. sbi->raw_super = raw_super;
  1518. /*
  1519. * The BLKZONED feature indicates that the drive was formatted with
  1520. * zone alignment optimization. This is optional for host-aware
  1521. * devices, but mandatory for host-managed zoned block devices.
  1522. */
  1523. #ifndef CONFIG_BLK_DEV_ZONED
  1524. if (f2fs_sb_mounted_blkzoned(sb)) {
  1525. f2fs_msg(sb, KERN_ERR,
  1526. "Zoned block device support is not enabled\n");
  1527. goto free_sb_buf;
  1528. }
  1529. #else
  1530. if (bdev_zoned_model(sb->s_bdev) == BLK_ZONED_HM &&
  1531. !f2fs_sb_mounted_blkzoned(sb)) {
  1532. f2fs_msg(sb, KERN_ERR,
  1533. "Zoned block device feature not enabled\n");
  1534. goto free_sb_buf;
  1535. }
  1536. #endif
  1537. default_options(sbi);
  1538. /* parse mount options */
  1539. options = kstrdup((const char *)data, GFP_KERNEL);
  1540. if (data && !options) {
  1541. err = -ENOMEM;
  1542. goto free_sb_buf;
  1543. }
  1544. err = parse_options(sb, options);
  1545. if (err)
  1546. goto free_options;
  1547. sbi->max_file_blocks = max_file_blocks();
  1548. sb->s_maxbytes = sbi->max_file_blocks <<
  1549. le32_to_cpu(raw_super->log_blocksize);
  1550. sb->s_max_links = F2FS_LINK_MAX;
  1551. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1552. sb->s_op = &f2fs_sops;
  1553. sb->s_cop = &f2fs_cryptops;
  1554. sb->s_xattr = f2fs_xattr_handlers;
  1555. sb->s_export_op = &f2fs_export_ops;
  1556. sb->s_magic = F2FS_SUPER_MAGIC;
  1557. sb->s_time_gran = 1;
  1558. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1559. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  1560. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  1561. /* init f2fs-specific super block info */
  1562. sbi->valid_super_block = valid_super_block;
  1563. mutex_init(&sbi->gc_mutex);
  1564. mutex_init(&sbi->cp_mutex);
  1565. init_rwsem(&sbi->node_write);
  1566. /* disallow all the data/node/meta page writes */
  1567. set_sbi_flag(sbi, SBI_POR_DOING);
  1568. spin_lock_init(&sbi->stat_lock);
  1569. init_rwsem(&sbi->read_io.io_rwsem);
  1570. sbi->read_io.sbi = sbi;
  1571. sbi->read_io.bio = NULL;
  1572. for (i = 0; i < NR_PAGE_TYPE; i++) {
  1573. init_rwsem(&sbi->write_io[i].io_rwsem);
  1574. sbi->write_io[i].sbi = sbi;
  1575. sbi->write_io[i].bio = NULL;
  1576. }
  1577. init_rwsem(&sbi->cp_rwsem);
  1578. init_waitqueue_head(&sbi->cp_wait);
  1579. init_sb_info(sbi);
  1580. err = init_percpu_info(sbi);
  1581. if (err)
  1582. goto free_options;
  1583. /* get an inode for meta space */
  1584. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  1585. if (IS_ERR(sbi->meta_inode)) {
  1586. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  1587. err = PTR_ERR(sbi->meta_inode);
  1588. goto free_options;
  1589. }
  1590. err = get_valid_checkpoint(sbi);
  1591. if (err) {
  1592. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  1593. goto free_meta_inode;
  1594. }
  1595. sbi->total_valid_node_count =
  1596. le32_to_cpu(sbi->ckpt->valid_node_count);
  1597. percpu_counter_set(&sbi->total_valid_inode_count,
  1598. le32_to_cpu(sbi->ckpt->valid_inode_count));
  1599. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  1600. sbi->total_valid_block_count =
  1601. le64_to_cpu(sbi->ckpt->valid_block_count);
  1602. sbi->last_valid_block_count = sbi->total_valid_block_count;
  1603. for (i = 0; i < NR_INODE_TYPE; i++) {
  1604. INIT_LIST_HEAD(&sbi->inode_list[i]);
  1605. spin_lock_init(&sbi->inode_lock[i]);
  1606. }
  1607. init_extent_cache_info(sbi);
  1608. init_ino_entry_info(sbi);
  1609. #ifdef CONFIG_BLK_DEV_ZONED
  1610. err = init_blkz_info(sbi);
  1611. if (err) {
  1612. f2fs_msg(sb, KERN_ERR,
  1613. "Failed to initialize F2FS blkzone information");
  1614. goto free_blkz;
  1615. }
  1616. #endif
  1617. /* setup f2fs internal modules */
  1618. err = build_segment_manager(sbi);
  1619. if (err) {
  1620. f2fs_msg(sb, KERN_ERR,
  1621. "Failed to initialize F2FS segment manager");
  1622. goto free_sm;
  1623. }
  1624. err = build_node_manager(sbi);
  1625. if (err) {
  1626. f2fs_msg(sb, KERN_ERR,
  1627. "Failed to initialize F2FS node manager");
  1628. goto free_nm;
  1629. }
  1630. /* For write statistics */
  1631. if (sb->s_bdev->bd_part)
  1632. sbi->sectors_written_start =
  1633. (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  1634. /* Read accumulated write IO statistics if exists */
  1635. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  1636. if (__exist_node_summaries(sbi))
  1637. sbi->kbytes_written =
  1638. le64_to_cpu(seg_i->journal->info.kbytes_written);
  1639. build_gc_manager(sbi);
  1640. /* get an inode for node space */
  1641. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  1642. if (IS_ERR(sbi->node_inode)) {
  1643. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  1644. err = PTR_ERR(sbi->node_inode);
  1645. goto free_nm;
  1646. }
  1647. f2fs_join_shrinker(sbi);
  1648. /* if there are nt orphan nodes free them */
  1649. err = recover_orphan_inodes(sbi);
  1650. if (err)
  1651. goto free_node_inode;
  1652. /* read root inode and dentry */
  1653. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  1654. if (IS_ERR(root)) {
  1655. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  1656. err = PTR_ERR(root);
  1657. goto free_node_inode;
  1658. }
  1659. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1660. iput(root);
  1661. err = -EINVAL;
  1662. goto free_node_inode;
  1663. }
  1664. sb->s_root = d_make_root(root); /* allocate root dentry */
  1665. if (!sb->s_root) {
  1666. err = -ENOMEM;
  1667. goto free_root_inode;
  1668. }
  1669. err = f2fs_build_stats(sbi);
  1670. if (err)
  1671. goto free_root_inode;
  1672. if (f2fs_proc_root)
  1673. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  1674. if (sbi->s_proc) {
  1675. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  1676. &f2fs_seq_segment_info_fops, sb);
  1677. proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
  1678. &f2fs_seq_segment_bits_fops, sb);
  1679. }
  1680. sbi->s_kobj.kset = f2fs_kset;
  1681. init_completion(&sbi->s_kobj_unregister);
  1682. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  1683. "%s", sb->s_id);
  1684. if (err)
  1685. goto free_proc;
  1686. /* recover fsynced data */
  1687. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  1688. /*
  1689. * mount should be failed, when device has readonly mode, and
  1690. * previous checkpoint was not done by clean system shutdown.
  1691. */
  1692. if (bdev_read_only(sb->s_bdev) &&
  1693. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  1694. err = -EROFS;
  1695. goto free_kobj;
  1696. }
  1697. if (need_fsck)
  1698. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1699. if (!retry)
  1700. goto skip_recovery;
  1701. err = recover_fsync_data(sbi, false);
  1702. if (err < 0) {
  1703. need_fsck = true;
  1704. f2fs_msg(sb, KERN_ERR,
  1705. "Cannot recover all fsync data errno=%d", err);
  1706. goto free_kobj;
  1707. }
  1708. } else {
  1709. err = recover_fsync_data(sbi, true);
  1710. if (!f2fs_readonly(sb) && err > 0) {
  1711. err = -EINVAL;
  1712. f2fs_msg(sb, KERN_ERR,
  1713. "Need to recover fsync data");
  1714. goto free_kobj;
  1715. }
  1716. }
  1717. skip_recovery:
  1718. /* recover_fsync_data() cleared this already */
  1719. clear_sbi_flag(sbi, SBI_POR_DOING);
  1720. /*
  1721. * If filesystem is not mounted as read-only then
  1722. * do start the gc_thread.
  1723. */
  1724. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  1725. /* After POR, we can run background GC thread.*/
  1726. err = start_gc_thread(sbi);
  1727. if (err)
  1728. goto free_kobj;
  1729. }
  1730. kfree(options);
  1731. /* recover broken superblock */
  1732. if (recovery) {
  1733. err = f2fs_commit_super(sbi, true);
  1734. f2fs_msg(sb, KERN_INFO,
  1735. "Try to recover %dth superblock, ret: %d",
  1736. sbi->valid_super_block ? 1 : 2, err);
  1737. }
  1738. f2fs_update_time(sbi, CP_TIME);
  1739. f2fs_update_time(sbi, REQ_TIME);
  1740. return 0;
  1741. free_kobj:
  1742. f2fs_sync_inode_meta(sbi);
  1743. kobject_del(&sbi->s_kobj);
  1744. kobject_put(&sbi->s_kobj);
  1745. wait_for_completion(&sbi->s_kobj_unregister);
  1746. free_proc:
  1747. if (sbi->s_proc) {
  1748. remove_proc_entry("segment_info", sbi->s_proc);
  1749. remove_proc_entry("segment_bits", sbi->s_proc);
  1750. remove_proc_entry(sb->s_id, f2fs_proc_root);
  1751. }
  1752. f2fs_destroy_stats(sbi);
  1753. free_root_inode:
  1754. dput(sb->s_root);
  1755. sb->s_root = NULL;
  1756. free_node_inode:
  1757. truncate_inode_pages(NODE_MAPPING(sbi), 0);
  1758. mutex_lock(&sbi->umount_mutex);
  1759. release_ino_entry(sbi, true);
  1760. f2fs_leave_shrinker(sbi);
  1761. /*
  1762. * Some dirty meta pages can be produced by recover_orphan_inodes()
  1763. * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
  1764. * followed by write_checkpoint() through f2fs_write_node_pages(), which
  1765. * falls into an infinite loop in sync_meta_pages().
  1766. */
  1767. truncate_inode_pages(META_MAPPING(sbi), 0);
  1768. iput(sbi->node_inode);
  1769. mutex_unlock(&sbi->umount_mutex);
  1770. free_nm:
  1771. destroy_node_manager(sbi);
  1772. free_sm:
  1773. destroy_segment_manager(sbi);
  1774. #ifdef CONFIG_BLK_DEV_ZONED
  1775. free_blkz:
  1776. kfree(sbi->blkz_type);
  1777. #endif
  1778. kfree(sbi->ckpt);
  1779. free_meta_inode:
  1780. make_bad_inode(sbi->meta_inode);
  1781. iput(sbi->meta_inode);
  1782. free_options:
  1783. destroy_percpu_info(sbi);
  1784. kfree(options);
  1785. free_sb_buf:
  1786. kfree(raw_super);
  1787. free_sbi:
  1788. kfree(sbi);
  1789. /* give only one another chance */
  1790. if (retry) {
  1791. retry = false;
  1792. shrink_dcache_sb(sb);
  1793. goto try_onemore;
  1794. }
  1795. return err;
  1796. }
  1797. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  1798. const char *dev_name, void *data)
  1799. {
  1800. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  1801. }
  1802. static void kill_f2fs_super(struct super_block *sb)
  1803. {
  1804. if (sb->s_root)
  1805. set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
  1806. kill_block_super(sb);
  1807. }
  1808. static struct file_system_type f2fs_fs_type = {
  1809. .owner = THIS_MODULE,
  1810. .name = "f2fs",
  1811. .mount = f2fs_mount,
  1812. .kill_sb = kill_f2fs_super,
  1813. .fs_flags = FS_REQUIRES_DEV,
  1814. };
  1815. MODULE_ALIAS_FS("f2fs");
  1816. static int __init init_inodecache(void)
  1817. {
  1818. f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
  1819. sizeof(struct f2fs_inode_info));
  1820. if (!f2fs_inode_cachep)
  1821. return -ENOMEM;
  1822. return 0;
  1823. }
  1824. static void destroy_inodecache(void)
  1825. {
  1826. /*
  1827. * Make sure all delayed rcu free inodes are flushed before we
  1828. * destroy cache.
  1829. */
  1830. rcu_barrier();
  1831. kmem_cache_destroy(f2fs_inode_cachep);
  1832. }
  1833. static int __init init_f2fs_fs(void)
  1834. {
  1835. int err;
  1836. f2fs_build_trace_ios();
  1837. err = init_inodecache();
  1838. if (err)
  1839. goto fail;
  1840. err = create_node_manager_caches();
  1841. if (err)
  1842. goto free_inodecache;
  1843. err = create_segment_manager_caches();
  1844. if (err)
  1845. goto free_node_manager_caches;
  1846. err = create_checkpoint_caches();
  1847. if (err)
  1848. goto free_segment_manager_caches;
  1849. err = create_extent_cache();
  1850. if (err)
  1851. goto free_checkpoint_caches;
  1852. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  1853. if (!f2fs_kset) {
  1854. err = -ENOMEM;
  1855. goto free_extent_cache;
  1856. }
  1857. register_shrinker(&f2fs_shrinker_info);
  1858. err = register_filesystem(&f2fs_fs_type);
  1859. if (err)
  1860. goto free_shrinker;
  1861. err = f2fs_create_root_stats();
  1862. if (err)
  1863. goto free_filesystem;
  1864. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  1865. return 0;
  1866. free_filesystem:
  1867. unregister_filesystem(&f2fs_fs_type);
  1868. free_shrinker:
  1869. unregister_shrinker(&f2fs_shrinker_info);
  1870. kset_unregister(f2fs_kset);
  1871. free_extent_cache:
  1872. destroy_extent_cache();
  1873. free_checkpoint_caches:
  1874. destroy_checkpoint_caches();
  1875. free_segment_manager_caches:
  1876. destroy_segment_manager_caches();
  1877. free_node_manager_caches:
  1878. destroy_node_manager_caches();
  1879. free_inodecache:
  1880. destroy_inodecache();
  1881. fail:
  1882. return err;
  1883. }
  1884. static void __exit exit_f2fs_fs(void)
  1885. {
  1886. remove_proc_entry("fs/f2fs", NULL);
  1887. f2fs_destroy_root_stats();
  1888. unregister_filesystem(&f2fs_fs_type);
  1889. unregister_shrinker(&f2fs_shrinker_info);
  1890. kset_unregister(f2fs_kset);
  1891. destroy_extent_cache();
  1892. destroy_checkpoint_caches();
  1893. destroy_segment_manager_caches();
  1894. destroy_node_manager_caches();
  1895. destroy_inodecache();
  1896. f2fs_destroy_trace_ios();
  1897. }
  1898. module_init(init_f2fs_fs)
  1899. module_exit(exit_f2fs_fs)
  1900. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  1901. MODULE_DESCRIPTION("Flash Friendly File System");
  1902. MODULE_LICENSE("GPL");