super.c 53 KB

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