super.c 50 KB

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