super.c 46 KB

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