super.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. #define CREATE_TRACE_POINTS
  33. #include <trace/events/f2fs.h>
  34. static struct proc_dir_entry *f2fs_proc_root;
  35. static struct kmem_cache *f2fs_inode_cachep;
  36. static struct kset *f2fs_kset;
  37. enum {
  38. Opt_gc_background,
  39. Opt_disable_roll_forward,
  40. Opt_discard,
  41. Opt_noheap,
  42. Opt_user_xattr,
  43. Opt_nouser_xattr,
  44. Opt_acl,
  45. Opt_noacl,
  46. Opt_active_logs,
  47. Opt_disable_ext_identify,
  48. Opt_inline_xattr,
  49. Opt_inline_data,
  50. Opt_flush_merge,
  51. Opt_nobarrier,
  52. Opt_err,
  53. };
  54. static match_table_t f2fs_tokens = {
  55. {Opt_gc_background, "background_gc=%s"},
  56. {Opt_disable_roll_forward, "disable_roll_forward"},
  57. {Opt_discard, "discard"},
  58. {Opt_noheap, "no_heap"},
  59. {Opt_user_xattr, "user_xattr"},
  60. {Opt_nouser_xattr, "nouser_xattr"},
  61. {Opt_acl, "acl"},
  62. {Opt_noacl, "noacl"},
  63. {Opt_active_logs, "active_logs=%u"},
  64. {Opt_disable_ext_identify, "disable_ext_identify"},
  65. {Opt_inline_xattr, "inline_xattr"},
  66. {Opt_inline_data, "inline_data"},
  67. {Opt_flush_merge, "flush_merge"},
  68. {Opt_nobarrier, "nobarrier"},
  69. {Opt_err, NULL},
  70. };
  71. /* Sysfs support for f2fs */
  72. enum {
  73. GC_THREAD, /* struct f2fs_gc_thread */
  74. SM_INFO, /* struct f2fs_sm_info */
  75. NM_INFO, /* struct f2fs_nm_info */
  76. F2FS_SBI, /* struct f2fs_sb_info */
  77. };
  78. struct f2fs_attr {
  79. struct attribute attr;
  80. ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
  81. ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
  82. const char *, size_t);
  83. int struct_type;
  84. int offset;
  85. };
  86. static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  87. {
  88. if (struct_type == GC_THREAD)
  89. return (unsigned char *)sbi->gc_thread;
  90. else if (struct_type == SM_INFO)
  91. return (unsigned char *)SM_I(sbi);
  92. else if (struct_type == NM_INFO)
  93. return (unsigned char *)NM_I(sbi);
  94. else if (struct_type == F2FS_SBI)
  95. return (unsigned char *)sbi;
  96. return NULL;
  97. }
  98. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  99. struct f2fs_sb_info *sbi, char *buf)
  100. {
  101. unsigned char *ptr = NULL;
  102. unsigned int *ui;
  103. ptr = __struct_ptr(sbi, a->struct_type);
  104. if (!ptr)
  105. return -EINVAL;
  106. ui = (unsigned int *)(ptr + a->offset);
  107. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  108. }
  109. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  110. struct f2fs_sb_info *sbi,
  111. const char *buf, size_t count)
  112. {
  113. unsigned char *ptr;
  114. unsigned long t;
  115. unsigned int *ui;
  116. ssize_t ret;
  117. ptr = __struct_ptr(sbi, a->struct_type);
  118. if (!ptr)
  119. return -EINVAL;
  120. ui = (unsigned int *)(ptr + a->offset);
  121. ret = kstrtoul(skip_spaces(buf), 0, &t);
  122. if (ret < 0)
  123. return ret;
  124. *ui = t;
  125. return count;
  126. }
  127. static ssize_t f2fs_attr_show(struct kobject *kobj,
  128. struct attribute *attr, char *buf)
  129. {
  130. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  131. s_kobj);
  132. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  133. return a->show ? a->show(a, sbi, buf) : 0;
  134. }
  135. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  136. const char *buf, size_t len)
  137. {
  138. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  139. s_kobj);
  140. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  141. return a->store ? a->store(a, sbi, buf, len) : 0;
  142. }
  143. static void f2fs_sb_release(struct kobject *kobj)
  144. {
  145. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  146. s_kobj);
  147. complete(&sbi->s_kobj_unregister);
  148. }
  149. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  150. static struct f2fs_attr f2fs_attr_##_name = { \
  151. .attr = {.name = __stringify(_name), .mode = _mode }, \
  152. .show = _show, \
  153. .store = _store, \
  154. .struct_type = _struct_type, \
  155. .offset = _offset \
  156. }
  157. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  158. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  159. f2fs_sbi_show, f2fs_sbi_store, \
  160. offsetof(struct struct_name, elname))
  161. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
  162. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
  163. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
  164. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
  165. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
  166. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, max_small_discards, max_discards);
  167. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
  168. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
  169. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
  170. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
  171. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
  172. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
  173. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  174. static struct attribute *f2fs_attrs[] = {
  175. ATTR_LIST(gc_min_sleep_time),
  176. ATTR_LIST(gc_max_sleep_time),
  177. ATTR_LIST(gc_no_gc_sleep_time),
  178. ATTR_LIST(gc_idle),
  179. ATTR_LIST(reclaim_segments),
  180. ATTR_LIST(max_small_discards),
  181. ATTR_LIST(ipu_policy),
  182. ATTR_LIST(min_ipu_util),
  183. ATTR_LIST(min_fsync_blocks),
  184. ATTR_LIST(max_victim_search),
  185. ATTR_LIST(dir_level),
  186. ATTR_LIST(ram_thresh),
  187. NULL,
  188. };
  189. static const struct sysfs_ops f2fs_attr_ops = {
  190. .show = f2fs_attr_show,
  191. .store = f2fs_attr_store,
  192. };
  193. static struct kobj_type f2fs_ktype = {
  194. .default_attrs = f2fs_attrs,
  195. .sysfs_ops = &f2fs_attr_ops,
  196. .release = f2fs_sb_release,
  197. };
  198. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  199. {
  200. struct va_format vaf;
  201. va_list args;
  202. va_start(args, fmt);
  203. vaf.fmt = fmt;
  204. vaf.va = &args;
  205. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  206. va_end(args);
  207. }
  208. static void init_once(void *foo)
  209. {
  210. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  211. inode_init_once(&fi->vfs_inode);
  212. }
  213. static int parse_options(struct super_block *sb, char *options)
  214. {
  215. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  216. substring_t args[MAX_OPT_ARGS];
  217. char *p, *name;
  218. int arg = 0;
  219. if (!options)
  220. return 0;
  221. while ((p = strsep(&options, ",")) != NULL) {
  222. int token;
  223. if (!*p)
  224. continue;
  225. /*
  226. * Initialize args struct so we know whether arg was
  227. * found; some options take optional arguments.
  228. */
  229. args[0].to = args[0].from = NULL;
  230. token = match_token(p, f2fs_tokens, args);
  231. switch (token) {
  232. case Opt_gc_background:
  233. name = match_strdup(&args[0]);
  234. if (!name)
  235. return -ENOMEM;
  236. if (strlen(name) == 2 && !strncmp(name, "on", 2))
  237. set_opt(sbi, BG_GC);
  238. else if (strlen(name) == 3 && !strncmp(name, "off", 3))
  239. clear_opt(sbi, BG_GC);
  240. else {
  241. kfree(name);
  242. return -EINVAL;
  243. }
  244. kfree(name);
  245. break;
  246. case Opt_disable_roll_forward:
  247. set_opt(sbi, DISABLE_ROLL_FORWARD);
  248. break;
  249. case Opt_discard:
  250. set_opt(sbi, DISCARD);
  251. break;
  252. case Opt_noheap:
  253. set_opt(sbi, NOHEAP);
  254. break;
  255. #ifdef CONFIG_F2FS_FS_XATTR
  256. case Opt_user_xattr:
  257. set_opt(sbi, XATTR_USER);
  258. break;
  259. case Opt_nouser_xattr:
  260. clear_opt(sbi, XATTR_USER);
  261. break;
  262. case Opt_inline_xattr:
  263. set_opt(sbi, INLINE_XATTR);
  264. break;
  265. #else
  266. case Opt_user_xattr:
  267. f2fs_msg(sb, KERN_INFO,
  268. "user_xattr options not supported");
  269. break;
  270. case Opt_nouser_xattr:
  271. f2fs_msg(sb, KERN_INFO,
  272. "nouser_xattr options not supported");
  273. break;
  274. case Opt_inline_xattr:
  275. f2fs_msg(sb, KERN_INFO,
  276. "inline_xattr options not supported");
  277. break;
  278. #endif
  279. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  280. case Opt_acl:
  281. set_opt(sbi, POSIX_ACL);
  282. break;
  283. case Opt_noacl:
  284. clear_opt(sbi, POSIX_ACL);
  285. break;
  286. #else
  287. case Opt_acl:
  288. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  289. break;
  290. case Opt_noacl:
  291. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  292. break;
  293. #endif
  294. case Opt_active_logs:
  295. if (args->from && match_int(args, &arg))
  296. return -EINVAL;
  297. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  298. return -EINVAL;
  299. sbi->active_logs = arg;
  300. break;
  301. case Opt_disable_ext_identify:
  302. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  303. break;
  304. case Opt_inline_data:
  305. set_opt(sbi, INLINE_DATA);
  306. break;
  307. case Opt_flush_merge:
  308. set_opt(sbi, FLUSH_MERGE);
  309. break;
  310. case Opt_nobarrier:
  311. set_opt(sbi, NOBARRIER);
  312. break;
  313. default:
  314. f2fs_msg(sb, KERN_ERR,
  315. "Unrecognized mount option \"%s\" or missing value",
  316. p);
  317. return -EINVAL;
  318. }
  319. }
  320. return 0;
  321. }
  322. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  323. {
  324. struct f2fs_inode_info *fi;
  325. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  326. if (!fi)
  327. return NULL;
  328. init_once((void *) fi);
  329. /* Initialize f2fs-specific inode info */
  330. fi->vfs_inode.i_version = 1;
  331. atomic_set(&fi->dirty_pages, 0);
  332. fi->i_current_depth = 1;
  333. fi->i_advise = 0;
  334. rwlock_init(&fi->ext.ext_lock);
  335. init_rwsem(&fi->i_sem);
  336. INIT_LIST_HEAD(&fi->inmem_pages);
  337. mutex_init(&fi->inmem_lock);
  338. set_inode_flag(fi, FI_NEW_INODE);
  339. if (test_opt(F2FS_SB(sb), INLINE_XATTR))
  340. set_inode_flag(fi, FI_INLINE_XATTR);
  341. /* Will be used by directory only */
  342. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  343. return &fi->vfs_inode;
  344. }
  345. static int f2fs_drop_inode(struct inode *inode)
  346. {
  347. /*
  348. * This is to avoid a deadlock condition like below.
  349. * writeback_single_inode(inode)
  350. * - f2fs_write_data_page
  351. * - f2fs_gc -> iput -> evict
  352. * - inode_wait_for_writeback(inode)
  353. */
  354. if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
  355. return 0;
  356. return generic_drop_inode(inode);
  357. }
  358. /*
  359. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  360. *
  361. * We should call set_dirty_inode to write the dirty inode through write_inode.
  362. */
  363. static void f2fs_dirty_inode(struct inode *inode, int flags)
  364. {
  365. set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
  366. }
  367. static void f2fs_i_callback(struct rcu_head *head)
  368. {
  369. struct inode *inode = container_of(head, struct inode, i_rcu);
  370. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  371. }
  372. static void f2fs_destroy_inode(struct inode *inode)
  373. {
  374. call_rcu(&inode->i_rcu, f2fs_i_callback);
  375. }
  376. static void f2fs_put_super(struct super_block *sb)
  377. {
  378. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  379. if (sbi->s_proc) {
  380. remove_proc_entry("segment_info", sbi->s_proc);
  381. remove_proc_entry(sb->s_id, f2fs_proc_root);
  382. }
  383. kobject_del(&sbi->s_kobj);
  384. f2fs_destroy_stats(sbi);
  385. stop_gc_thread(sbi);
  386. /* We don't need to do checkpoint when it's clean */
  387. if (sbi->s_dirty) {
  388. struct cp_control cpc = {
  389. .reason = CP_UMOUNT,
  390. };
  391. write_checkpoint(sbi, &cpc);
  392. }
  393. /*
  394. * normally superblock is clean, so we need to release this.
  395. * In addition, EIO will skip do checkpoint, we need this as well.
  396. */
  397. release_dirty_inode(sbi);
  398. release_discard_addrs(sbi);
  399. iput(sbi->node_inode);
  400. iput(sbi->meta_inode);
  401. /* destroy f2fs internal modules */
  402. destroy_node_manager(sbi);
  403. destroy_segment_manager(sbi);
  404. kfree(sbi->ckpt);
  405. kobject_put(&sbi->s_kobj);
  406. wait_for_completion(&sbi->s_kobj_unregister);
  407. sb->s_fs_info = NULL;
  408. brelse(sbi->raw_super_buf);
  409. kfree(sbi);
  410. }
  411. int f2fs_sync_fs(struct super_block *sb, int sync)
  412. {
  413. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  414. trace_f2fs_sync_fs(sb, sync);
  415. if (sync) {
  416. struct cp_control cpc = {
  417. .reason = CP_SYNC,
  418. };
  419. mutex_lock(&sbi->gc_mutex);
  420. write_checkpoint(sbi, &cpc);
  421. mutex_unlock(&sbi->gc_mutex);
  422. } else {
  423. f2fs_balance_fs(sbi);
  424. }
  425. return 0;
  426. }
  427. static int f2fs_freeze(struct super_block *sb)
  428. {
  429. int err;
  430. if (f2fs_readonly(sb))
  431. return 0;
  432. err = f2fs_sync_fs(sb, 1);
  433. return err;
  434. }
  435. static int f2fs_unfreeze(struct super_block *sb)
  436. {
  437. return 0;
  438. }
  439. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  440. {
  441. struct super_block *sb = dentry->d_sb;
  442. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  443. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  444. block_t total_count, user_block_count, start_count, ovp_count;
  445. total_count = le64_to_cpu(sbi->raw_super->block_count);
  446. user_block_count = sbi->user_block_count;
  447. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  448. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  449. buf->f_type = F2FS_SUPER_MAGIC;
  450. buf->f_bsize = sbi->blocksize;
  451. buf->f_blocks = total_count - start_count;
  452. buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
  453. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  454. buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  455. buf->f_ffree = buf->f_files - valid_inode_count(sbi);
  456. buf->f_namelen = F2FS_NAME_LEN;
  457. buf->f_fsid.val[0] = (u32)id;
  458. buf->f_fsid.val[1] = (u32)(id >> 32);
  459. return 0;
  460. }
  461. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  462. {
  463. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  464. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC))
  465. seq_printf(seq, ",background_gc=%s", "on");
  466. else
  467. seq_printf(seq, ",background_gc=%s", "off");
  468. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  469. seq_puts(seq, ",disable_roll_forward");
  470. if (test_opt(sbi, DISCARD))
  471. seq_puts(seq, ",discard");
  472. if (test_opt(sbi, NOHEAP))
  473. seq_puts(seq, ",no_heap_alloc");
  474. #ifdef CONFIG_F2FS_FS_XATTR
  475. if (test_opt(sbi, XATTR_USER))
  476. seq_puts(seq, ",user_xattr");
  477. else
  478. seq_puts(seq, ",nouser_xattr");
  479. if (test_opt(sbi, INLINE_XATTR))
  480. seq_puts(seq, ",inline_xattr");
  481. #endif
  482. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  483. if (test_opt(sbi, POSIX_ACL))
  484. seq_puts(seq, ",acl");
  485. else
  486. seq_puts(seq, ",noacl");
  487. #endif
  488. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  489. seq_puts(seq, ",disable_ext_identify");
  490. if (test_opt(sbi, INLINE_DATA))
  491. seq_puts(seq, ",inline_data");
  492. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  493. seq_puts(seq, ",flush_merge");
  494. if (test_opt(sbi, NOBARRIER))
  495. seq_puts(seq, ",nobarrier");
  496. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  497. return 0;
  498. }
  499. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  500. {
  501. struct super_block *sb = seq->private;
  502. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  503. unsigned int total_segs =
  504. le32_to_cpu(sbi->raw_super->segment_count_main);
  505. int i;
  506. seq_puts(seq, "format: segment_type|valid_blocks\n"
  507. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  508. for (i = 0; i < total_segs; i++) {
  509. struct seg_entry *se = get_seg_entry(sbi, i);
  510. if ((i % 10) == 0)
  511. seq_printf(seq, "%-5d", i);
  512. seq_printf(seq, "%d|%-3u", se->type,
  513. get_valid_blocks(sbi, i, 1));
  514. if ((i % 10) == 9 || i == (total_segs - 1))
  515. seq_putc(seq, '\n');
  516. else
  517. seq_putc(seq, ' ');
  518. }
  519. return 0;
  520. }
  521. static int segment_info_open_fs(struct inode *inode, struct file *file)
  522. {
  523. return single_open(file, segment_info_seq_show, PDE_DATA(inode));
  524. }
  525. static const struct file_operations f2fs_seq_segment_info_fops = {
  526. .owner = THIS_MODULE,
  527. .open = segment_info_open_fs,
  528. .read = seq_read,
  529. .llseek = seq_lseek,
  530. .release = single_release,
  531. };
  532. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  533. {
  534. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  535. struct f2fs_mount_info org_mount_opt;
  536. int err, active_logs;
  537. bool need_restart_gc = false;
  538. bool need_stop_gc = false;
  539. sync_filesystem(sb);
  540. /*
  541. * Save the old mount options in case we
  542. * need to restore them.
  543. */
  544. org_mount_opt = sbi->mount_opt;
  545. active_logs = sbi->active_logs;
  546. sbi->mount_opt.opt = 0;
  547. sbi->active_logs = NR_CURSEG_TYPE;
  548. /* parse mount options */
  549. err = parse_options(sb, data);
  550. if (err)
  551. goto restore_opts;
  552. /*
  553. * Previous and new state of filesystem is RO,
  554. * so skip checking GC and FLUSH_MERGE conditions.
  555. */
  556. if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
  557. goto skip;
  558. /*
  559. * We stop the GC thread if FS is mounted as RO
  560. * or if background_gc = off is passed in mount
  561. * option. Also sync the filesystem.
  562. */
  563. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  564. if (sbi->gc_thread) {
  565. stop_gc_thread(sbi);
  566. f2fs_sync_fs(sb, 1);
  567. need_restart_gc = true;
  568. }
  569. } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
  570. err = start_gc_thread(sbi);
  571. if (err)
  572. goto restore_opts;
  573. need_stop_gc = true;
  574. }
  575. /*
  576. * We stop issue flush thread if FS is mounted as RO
  577. * or if flush_merge is not passed in mount option.
  578. */
  579. if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  580. destroy_flush_cmd_control(sbi);
  581. } else if (test_opt(sbi, FLUSH_MERGE) && !SM_I(sbi)->cmd_control_info) {
  582. err = create_flush_cmd_control(sbi);
  583. if (err)
  584. goto restore_gc;
  585. }
  586. skip:
  587. /* Update the POSIXACL Flag */
  588. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  589. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  590. return 0;
  591. restore_gc:
  592. if (need_restart_gc) {
  593. if (start_gc_thread(sbi))
  594. f2fs_msg(sbi->sb, KERN_WARNING,
  595. "background gc thread has stopped");
  596. } else if (need_stop_gc) {
  597. stop_gc_thread(sbi);
  598. }
  599. restore_opts:
  600. sbi->mount_opt = org_mount_opt;
  601. sbi->active_logs = active_logs;
  602. return err;
  603. }
  604. static struct super_operations f2fs_sops = {
  605. .alloc_inode = f2fs_alloc_inode,
  606. .drop_inode = f2fs_drop_inode,
  607. .destroy_inode = f2fs_destroy_inode,
  608. .write_inode = f2fs_write_inode,
  609. .dirty_inode = f2fs_dirty_inode,
  610. .show_options = f2fs_show_options,
  611. .evict_inode = f2fs_evict_inode,
  612. .put_super = f2fs_put_super,
  613. .sync_fs = f2fs_sync_fs,
  614. .freeze_fs = f2fs_freeze,
  615. .unfreeze_fs = f2fs_unfreeze,
  616. .statfs = f2fs_statfs,
  617. .remount_fs = f2fs_remount,
  618. };
  619. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  620. u64 ino, u32 generation)
  621. {
  622. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  623. struct inode *inode;
  624. if (check_nid_range(sbi, ino))
  625. return ERR_PTR(-ESTALE);
  626. /*
  627. * f2fs_iget isn't quite right if the inode is currently unallocated!
  628. * However f2fs_iget currently does appropriate checks to handle stale
  629. * inodes so everything is OK.
  630. */
  631. inode = f2fs_iget(sb, ino);
  632. if (IS_ERR(inode))
  633. return ERR_CAST(inode);
  634. if (unlikely(generation && inode->i_generation != generation)) {
  635. /* we didn't find the right inode.. */
  636. iput(inode);
  637. return ERR_PTR(-ESTALE);
  638. }
  639. return inode;
  640. }
  641. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  642. int fh_len, int fh_type)
  643. {
  644. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  645. f2fs_nfs_get_inode);
  646. }
  647. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  648. int fh_len, int fh_type)
  649. {
  650. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  651. f2fs_nfs_get_inode);
  652. }
  653. static const struct export_operations f2fs_export_ops = {
  654. .fh_to_dentry = f2fs_fh_to_dentry,
  655. .fh_to_parent = f2fs_fh_to_parent,
  656. .get_parent = f2fs_get_parent,
  657. };
  658. static loff_t max_file_size(unsigned bits)
  659. {
  660. loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
  661. loff_t leaf_count = ADDRS_PER_BLOCK;
  662. /* two direct node blocks */
  663. result += (leaf_count * 2);
  664. /* two indirect node blocks */
  665. leaf_count *= NIDS_PER_BLOCK;
  666. result += (leaf_count * 2);
  667. /* one double indirect node block */
  668. leaf_count *= NIDS_PER_BLOCK;
  669. result += leaf_count;
  670. result <<= bits;
  671. return result;
  672. }
  673. static int sanity_check_raw_super(struct super_block *sb,
  674. struct f2fs_super_block *raw_super)
  675. {
  676. unsigned int blocksize;
  677. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  678. f2fs_msg(sb, KERN_INFO,
  679. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  680. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  681. return 1;
  682. }
  683. /* Currently, support only 4KB page cache size */
  684. if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
  685. f2fs_msg(sb, KERN_INFO,
  686. "Invalid page_cache_size (%lu), supports only 4KB\n",
  687. PAGE_CACHE_SIZE);
  688. return 1;
  689. }
  690. /* Currently, support only 4KB block size */
  691. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  692. if (blocksize != F2FS_BLKSIZE) {
  693. f2fs_msg(sb, KERN_INFO,
  694. "Invalid blocksize (%u), supports only 4KB\n",
  695. blocksize);
  696. return 1;
  697. }
  698. /* Currently, support 512/1024/2048/4096 bytes sector size */
  699. if (le32_to_cpu(raw_super->log_sectorsize) >
  700. F2FS_MAX_LOG_SECTOR_SIZE ||
  701. le32_to_cpu(raw_super->log_sectorsize) <
  702. F2FS_MIN_LOG_SECTOR_SIZE) {
  703. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  704. le32_to_cpu(raw_super->log_sectorsize));
  705. return 1;
  706. }
  707. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  708. le32_to_cpu(raw_super->log_sectorsize) !=
  709. F2FS_MAX_LOG_SECTOR_SIZE) {
  710. f2fs_msg(sb, KERN_INFO,
  711. "Invalid log sectors per block(%u) log sectorsize(%u)",
  712. le32_to_cpu(raw_super->log_sectors_per_block),
  713. le32_to_cpu(raw_super->log_sectorsize));
  714. return 1;
  715. }
  716. return 0;
  717. }
  718. static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  719. {
  720. unsigned int total, fsmeta;
  721. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  722. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  723. total = le32_to_cpu(raw_super->segment_count);
  724. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  725. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  726. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  727. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  728. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  729. if (unlikely(fsmeta >= total))
  730. return 1;
  731. if (unlikely(f2fs_cp_error(sbi))) {
  732. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  733. return 1;
  734. }
  735. return 0;
  736. }
  737. static void init_sb_info(struct f2fs_sb_info *sbi)
  738. {
  739. struct f2fs_super_block *raw_super = sbi->raw_super;
  740. int i;
  741. sbi->log_sectors_per_block =
  742. le32_to_cpu(raw_super->log_sectors_per_block);
  743. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  744. sbi->blocksize = 1 << sbi->log_blocksize;
  745. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  746. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  747. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  748. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  749. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  750. sbi->total_node_count =
  751. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  752. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  753. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  754. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  755. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  756. sbi->cur_victim_sec = NULL_SECNO;
  757. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  758. for (i = 0; i < NR_COUNT_TYPE; i++)
  759. atomic_set(&sbi->nr_pages[i], 0);
  760. sbi->dir_level = DEF_DIR_LEVEL;
  761. sbi->need_fsck = false;
  762. }
  763. /*
  764. * Read f2fs raw super block.
  765. * Because we have two copies of super block, so read the first one at first,
  766. * if the first one is invalid, move to read the second one.
  767. */
  768. static int read_raw_super_block(struct super_block *sb,
  769. struct f2fs_super_block **raw_super,
  770. struct buffer_head **raw_super_buf)
  771. {
  772. int block = 0;
  773. retry:
  774. *raw_super_buf = sb_bread(sb, block);
  775. if (!*raw_super_buf) {
  776. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  777. block + 1);
  778. if (block == 0) {
  779. block++;
  780. goto retry;
  781. } else {
  782. return -EIO;
  783. }
  784. }
  785. *raw_super = (struct f2fs_super_block *)
  786. ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
  787. /* sanity checking of raw super */
  788. if (sanity_check_raw_super(sb, *raw_super)) {
  789. brelse(*raw_super_buf);
  790. f2fs_msg(sb, KERN_ERR,
  791. "Can't find valid F2FS filesystem in %dth superblock",
  792. block + 1);
  793. if (block == 0) {
  794. block++;
  795. goto retry;
  796. } else {
  797. return -EINVAL;
  798. }
  799. }
  800. return 0;
  801. }
  802. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  803. {
  804. struct f2fs_sb_info *sbi;
  805. struct f2fs_super_block *raw_super;
  806. struct buffer_head *raw_super_buf;
  807. struct inode *root;
  808. long err = -EINVAL;
  809. bool retry = true;
  810. int i;
  811. try_onemore:
  812. /* allocate memory for f2fs-specific super block info */
  813. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  814. if (!sbi)
  815. return -ENOMEM;
  816. /* set a block size */
  817. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  818. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  819. goto free_sbi;
  820. }
  821. err = read_raw_super_block(sb, &raw_super, &raw_super_buf);
  822. if (err)
  823. goto free_sbi;
  824. sb->s_fs_info = sbi;
  825. /* init some FS parameters */
  826. sbi->active_logs = NR_CURSEG_TYPE;
  827. set_opt(sbi, BG_GC);
  828. #ifdef CONFIG_F2FS_FS_XATTR
  829. set_opt(sbi, XATTR_USER);
  830. #endif
  831. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  832. set_opt(sbi, POSIX_ACL);
  833. #endif
  834. /* parse mount options */
  835. err = parse_options(sb, (char *)data);
  836. if (err)
  837. goto free_sb_buf;
  838. sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
  839. sb->s_max_links = F2FS_LINK_MAX;
  840. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  841. sb->s_op = &f2fs_sops;
  842. sb->s_xattr = f2fs_xattr_handlers;
  843. sb->s_export_op = &f2fs_export_ops;
  844. sb->s_magic = F2FS_SUPER_MAGIC;
  845. sb->s_time_gran = 1;
  846. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  847. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  848. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  849. /* init f2fs-specific super block info */
  850. sbi->sb = sb;
  851. sbi->raw_super = raw_super;
  852. sbi->raw_super_buf = raw_super_buf;
  853. mutex_init(&sbi->gc_mutex);
  854. mutex_init(&sbi->writepages);
  855. mutex_init(&sbi->cp_mutex);
  856. init_rwsem(&sbi->node_write);
  857. sbi->por_doing = false;
  858. spin_lock_init(&sbi->stat_lock);
  859. init_rwsem(&sbi->read_io.io_rwsem);
  860. sbi->read_io.sbi = sbi;
  861. sbi->read_io.bio = NULL;
  862. for (i = 0; i < NR_PAGE_TYPE; i++) {
  863. init_rwsem(&sbi->write_io[i].io_rwsem);
  864. sbi->write_io[i].sbi = sbi;
  865. sbi->write_io[i].bio = NULL;
  866. }
  867. init_rwsem(&sbi->cp_rwsem);
  868. init_waitqueue_head(&sbi->cp_wait);
  869. init_sb_info(sbi);
  870. /* get an inode for meta space */
  871. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  872. if (IS_ERR(sbi->meta_inode)) {
  873. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  874. err = PTR_ERR(sbi->meta_inode);
  875. goto free_sb_buf;
  876. }
  877. err = get_valid_checkpoint(sbi);
  878. if (err) {
  879. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  880. goto free_meta_inode;
  881. }
  882. /* sanity checking of checkpoint */
  883. err = -EINVAL;
  884. if (sanity_check_ckpt(sbi)) {
  885. f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
  886. goto free_cp;
  887. }
  888. sbi->total_valid_node_count =
  889. le32_to_cpu(sbi->ckpt->valid_node_count);
  890. sbi->total_valid_inode_count =
  891. le32_to_cpu(sbi->ckpt->valid_inode_count);
  892. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  893. sbi->total_valid_block_count =
  894. le64_to_cpu(sbi->ckpt->valid_block_count);
  895. sbi->last_valid_block_count = sbi->total_valid_block_count;
  896. sbi->alloc_valid_block_count = 0;
  897. INIT_LIST_HEAD(&sbi->dir_inode_list);
  898. spin_lock_init(&sbi->dir_inode_lock);
  899. init_ino_entry_info(sbi);
  900. /* setup f2fs internal modules */
  901. err = build_segment_manager(sbi);
  902. if (err) {
  903. f2fs_msg(sb, KERN_ERR,
  904. "Failed to initialize F2FS segment manager");
  905. goto free_sm;
  906. }
  907. err = build_node_manager(sbi);
  908. if (err) {
  909. f2fs_msg(sb, KERN_ERR,
  910. "Failed to initialize F2FS node manager");
  911. goto free_nm;
  912. }
  913. build_gc_manager(sbi);
  914. /* get an inode for node space */
  915. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  916. if (IS_ERR(sbi->node_inode)) {
  917. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  918. err = PTR_ERR(sbi->node_inode);
  919. goto free_nm;
  920. }
  921. /* if there are nt orphan nodes free them */
  922. recover_orphan_inodes(sbi);
  923. /* read root inode and dentry */
  924. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  925. if (IS_ERR(root)) {
  926. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  927. err = PTR_ERR(root);
  928. goto free_node_inode;
  929. }
  930. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  931. iput(root);
  932. err = -EINVAL;
  933. goto free_node_inode;
  934. }
  935. sb->s_root = d_make_root(root); /* allocate root dentry */
  936. if (!sb->s_root) {
  937. err = -ENOMEM;
  938. goto free_root_inode;
  939. }
  940. err = f2fs_build_stats(sbi);
  941. if (err)
  942. goto free_root_inode;
  943. if (f2fs_proc_root)
  944. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  945. if (sbi->s_proc)
  946. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  947. &f2fs_seq_segment_info_fops, sb);
  948. if (test_opt(sbi, DISCARD)) {
  949. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  950. if (!blk_queue_discard(q))
  951. f2fs_msg(sb, KERN_WARNING,
  952. "mounting with \"discard\" option, but "
  953. "the device does not support discard");
  954. }
  955. sbi->s_kobj.kset = f2fs_kset;
  956. init_completion(&sbi->s_kobj_unregister);
  957. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  958. "%s", sb->s_id);
  959. if (err)
  960. goto free_proc;
  961. if (!retry)
  962. sbi->need_fsck = true;
  963. /* recover fsynced data */
  964. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  965. err = recover_fsync_data(sbi);
  966. if (err) {
  967. f2fs_msg(sb, KERN_ERR,
  968. "Cannot recover all fsync data errno=%ld", err);
  969. goto free_kobj;
  970. }
  971. }
  972. /*
  973. * If filesystem is not mounted as read-only then
  974. * do start the gc_thread.
  975. */
  976. if (!f2fs_readonly(sb)) {
  977. /* After POR, we can run background GC thread.*/
  978. err = start_gc_thread(sbi);
  979. if (err)
  980. goto free_kobj;
  981. }
  982. return 0;
  983. free_kobj:
  984. kobject_del(&sbi->s_kobj);
  985. free_proc:
  986. if (sbi->s_proc) {
  987. remove_proc_entry("segment_info", sbi->s_proc);
  988. remove_proc_entry(sb->s_id, f2fs_proc_root);
  989. }
  990. f2fs_destroy_stats(sbi);
  991. free_root_inode:
  992. dput(sb->s_root);
  993. sb->s_root = NULL;
  994. free_node_inode:
  995. iput(sbi->node_inode);
  996. free_nm:
  997. destroy_node_manager(sbi);
  998. free_sm:
  999. destroy_segment_manager(sbi);
  1000. free_cp:
  1001. kfree(sbi->ckpt);
  1002. free_meta_inode:
  1003. make_bad_inode(sbi->meta_inode);
  1004. iput(sbi->meta_inode);
  1005. free_sb_buf:
  1006. brelse(raw_super_buf);
  1007. free_sbi:
  1008. kfree(sbi);
  1009. /* give only one another chance */
  1010. if (retry) {
  1011. retry = 0;
  1012. shrink_dcache_sb(sb);
  1013. goto try_onemore;
  1014. }
  1015. return err;
  1016. }
  1017. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  1018. const char *dev_name, void *data)
  1019. {
  1020. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  1021. }
  1022. static struct file_system_type f2fs_fs_type = {
  1023. .owner = THIS_MODULE,
  1024. .name = "f2fs",
  1025. .mount = f2fs_mount,
  1026. .kill_sb = kill_block_super,
  1027. .fs_flags = FS_REQUIRES_DEV,
  1028. };
  1029. MODULE_ALIAS_FS("f2fs");
  1030. static int __init init_inodecache(void)
  1031. {
  1032. f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
  1033. sizeof(struct f2fs_inode_info));
  1034. if (!f2fs_inode_cachep)
  1035. return -ENOMEM;
  1036. return 0;
  1037. }
  1038. static void destroy_inodecache(void)
  1039. {
  1040. /*
  1041. * Make sure all delayed rcu free inodes are flushed before we
  1042. * destroy cache.
  1043. */
  1044. rcu_barrier();
  1045. kmem_cache_destroy(f2fs_inode_cachep);
  1046. }
  1047. static int __init init_f2fs_fs(void)
  1048. {
  1049. int err;
  1050. err = init_inodecache();
  1051. if (err)
  1052. goto fail;
  1053. err = create_node_manager_caches();
  1054. if (err)
  1055. goto free_inodecache;
  1056. err = create_segment_manager_caches();
  1057. if (err)
  1058. goto free_node_manager_caches;
  1059. err = create_gc_caches();
  1060. if (err)
  1061. goto free_segment_manager_caches;
  1062. err = create_checkpoint_caches();
  1063. if (err)
  1064. goto free_gc_caches;
  1065. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  1066. if (!f2fs_kset) {
  1067. err = -ENOMEM;
  1068. goto free_checkpoint_caches;
  1069. }
  1070. err = register_filesystem(&f2fs_fs_type);
  1071. if (err)
  1072. goto free_kset;
  1073. f2fs_create_root_stats();
  1074. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  1075. return 0;
  1076. free_kset:
  1077. kset_unregister(f2fs_kset);
  1078. free_checkpoint_caches:
  1079. destroy_checkpoint_caches();
  1080. free_gc_caches:
  1081. destroy_gc_caches();
  1082. free_segment_manager_caches:
  1083. destroy_segment_manager_caches();
  1084. free_node_manager_caches:
  1085. destroy_node_manager_caches();
  1086. free_inodecache:
  1087. destroy_inodecache();
  1088. fail:
  1089. return err;
  1090. }
  1091. static void __exit exit_f2fs_fs(void)
  1092. {
  1093. remove_proc_entry("fs/f2fs", NULL);
  1094. f2fs_destroy_root_stats();
  1095. unregister_filesystem(&f2fs_fs_type);
  1096. destroy_checkpoint_caches();
  1097. destroy_gc_caches();
  1098. destroy_segment_manager_caches();
  1099. destroy_node_manager_caches();
  1100. destroy_inodecache();
  1101. kset_unregister(f2fs_kset);
  1102. }
  1103. module_init(init_f2fs_fs)
  1104. module_exit(exit_f2fs_fs)
  1105. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  1106. MODULE_DESCRIPTION("Flash Friendly File System");
  1107. MODULE_LICENSE("GPL");