super.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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/proc_fs.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/kthread.h>
  19. #include <linux/parser.h>
  20. #include <linux/mount.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/random.h>
  23. #include <linux/exportfs.h>
  24. #include <linux/f2fs_fs.h>
  25. #include "f2fs.h"
  26. #include "node.h"
  27. #include "xattr.h"
  28. static struct kmem_cache *f2fs_inode_cachep;
  29. enum {
  30. Opt_gc_background_off,
  31. Opt_disable_roll_forward,
  32. Opt_discard,
  33. Opt_noheap,
  34. Opt_nouser_xattr,
  35. Opt_noacl,
  36. Opt_active_logs,
  37. Opt_disable_ext_identify,
  38. Opt_err,
  39. };
  40. static match_table_t f2fs_tokens = {
  41. {Opt_gc_background_off, "background_gc_off"},
  42. {Opt_disable_roll_forward, "disable_roll_forward"},
  43. {Opt_discard, "discard"},
  44. {Opt_noheap, "no_heap"},
  45. {Opt_nouser_xattr, "nouser_xattr"},
  46. {Opt_noacl, "noacl"},
  47. {Opt_active_logs, "active_logs=%u"},
  48. {Opt_disable_ext_identify, "disable_ext_identify"},
  49. {Opt_err, NULL},
  50. };
  51. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  52. {
  53. struct va_format vaf;
  54. va_list args;
  55. va_start(args, fmt);
  56. vaf.fmt = fmt;
  57. vaf.va = &args;
  58. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  59. va_end(args);
  60. }
  61. static void init_once(void *foo)
  62. {
  63. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  64. inode_init_once(&fi->vfs_inode);
  65. }
  66. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  67. {
  68. struct f2fs_inode_info *fi;
  69. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
  70. if (!fi)
  71. return NULL;
  72. init_once((void *) fi);
  73. /* Initilize f2fs-specific inode info */
  74. fi->vfs_inode.i_version = 1;
  75. atomic_set(&fi->dirty_dents, 0);
  76. fi->i_current_depth = 1;
  77. fi->i_advise = 0;
  78. rwlock_init(&fi->ext.ext_lock);
  79. set_inode_flag(fi, FI_NEW_INODE);
  80. return &fi->vfs_inode;
  81. }
  82. static void f2fs_i_callback(struct rcu_head *head)
  83. {
  84. struct inode *inode = container_of(head, struct inode, i_rcu);
  85. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  86. }
  87. static void f2fs_destroy_inode(struct inode *inode)
  88. {
  89. call_rcu(&inode->i_rcu, f2fs_i_callback);
  90. }
  91. static void f2fs_put_super(struct super_block *sb)
  92. {
  93. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  94. f2fs_destroy_stats(sbi);
  95. stop_gc_thread(sbi);
  96. write_checkpoint(sbi, false, true);
  97. iput(sbi->node_inode);
  98. iput(sbi->meta_inode);
  99. /* destroy f2fs internal modules */
  100. destroy_node_manager(sbi);
  101. destroy_segment_manager(sbi);
  102. kfree(sbi->ckpt);
  103. sb->s_fs_info = NULL;
  104. brelse(sbi->raw_super_buf);
  105. kfree(sbi);
  106. }
  107. int f2fs_sync_fs(struct super_block *sb, int sync)
  108. {
  109. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  110. if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
  111. return 0;
  112. if (sync)
  113. write_checkpoint(sbi, false, false);
  114. else
  115. f2fs_balance_fs(sbi);
  116. return 0;
  117. }
  118. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  119. {
  120. struct super_block *sb = dentry->d_sb;
  121. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  122. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  123. block_t total_count, user_block_count, start_count, ovp_count;
  124. total_count = le64_to_cpu(sbi->raw_super->block_count);
  125. user_block_count = sbi->user_block_count;
  126. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  127. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  128. buf->f_type = F2FS_SUPER_MAGIC;
  129. buf->f_bsize = sbi->blocksize;
  130. buf->f_blocks = total_count - start_count;
  131. buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
  132. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  133. buf->f_files = sbi->total_node_count;
  134. buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
  135. buf->f_namelen = F2FS_MAX_NAME_LEN;
  136. buf->f_fsid.val[0] = (u32)id;
  137. buf->f_fsid.val[1] = (u32)(id >> 32);
  138. return 0;
  139. }
  140. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  141. {
  142. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  143. if (test_opt(sbi, BG_GC))
  144. seq_puts(seq, ",background_gc_on");
  145. else
  146. seq_puts(seq, ",background_gc_off");
  147. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  148. seq_puts(seq, ",disable_roll_forward");
  149. if (test_opt(sbi, DISCARD))
  150. seq_puts(seq, ",discard");
  151. if (test_opt(sbi, NOHEAP))
  152. seq_puts(seq, ",no_heap_alloc");
  153. #ifdef CONFIG_F2FS_FS_XATTR
  154. if (test_opt(sbi, XATTR_USER))
  155. seq_puts(seq, ",user_xattr");
  156. else
  157. seq_puts(seq, ",nouser_xattr");
  158. #endif
  159. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  160. if (test_opt(sbi, POSIX_ACL))
  161. seq_puts(seq, ",acl");
  162. else
  163. seq_puts(seq, ",noacl");
  164. #endif
  165. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  166. seq_puts(seq, ",disable_ext_indentify");
  167. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  168. return 0;
  169. }
  170. static struct super_operations f2fs_sops = {
  171. .alloc_inode = f2fs_alloc_inode,
  172. .destroy_inode = f2fs_destroy_inode,
  173. .write_inode = f2fs_write_inode,
  174. .show_options = f2fs_show_options,
  175. .evict_inode = f2fs_evict_inode,
  176. .put_super = f2fs_put_super,
  177. .sync_fs = f2fs_sync_fs,
  178. .statfs = f2fs_statfs,
  179. };
  180. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  181. u64 ino, u32 generation)
  182. {
  183. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  184. struct inode *inode;
  185. if (ino < F2FS_ROOT_INO(sbi))
  186. return ERR_PTR(-ESTALE);
  187. /*
  188. * f2fs_iget isn't quite right if the inode is currently unallocated!
  189. * However f2fs_iget currently does appropriate checks to handle stale
  190. * inodes so everything is OK.
  191. */
  192. inode = f2fs_iget(sb, ino);
  193. if (IS_ERR(inode))
  194. return ERR_CAST(inode);
  195. if (generation && inode->i_generation != generation) {
  196. /* we didn't find the right inode.. */
  197. iput(inode);
  198. return ERR_PTR(-ESTALE);
  199. }
  200. return inode;
  201. }
  202. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  203. int fh_len, int fh_type)
  204. {
  205. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  206. f2fs_nfs_get_inode);
  207. }
  208. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  209. int fh_len, int fh_type)
  210. {
  211. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  212. f2fs_nfs_get_inode);
  213. }
  214. static const struct export_operations f2fs_export_ops = {
  215. .fh_to_dentry = f2fs_fh_to_dentry,
  216. .fh_to_parent = f2fs_fh_to_parent,
  217. .get_parent = f2fs_get_parent,
  218. };
  219. static int parse_options(struct super_block *sb, struct f2fs_sb_info *sbi,
  220. char *options)
  221. {
  222. substring_t args[MAX_OPT_ARGS];
  223. char *p;
  224. int arg = 0;
  225. if (!options)
  226. return 0;
  227. while ((p = strsep(&options, ",")) != NULL) {
  228. int token;
  229. if (!*p)
  230. continue;
  231. /*
  232. * Initialize args struct so we know whether arg was
  233. * found; some options take optional arguments.
  234. */
  235. args[0].to = args[0].from = NULL;
  236. token = match_token(p, f2fs_tokens, args);
  237. switch (token) {
  238. case Opt_gc_background_off:
  239. clear_opt(sbi, BG_GC);
  240. break;
  241. case Opt_disable_roll_forward:
  242. set_opt(sbi, DISABLE_ROLL_FORWARD);
  243. break;
  244. case Opt_discard:
  245. set_opt(sbi, DISCARD);
  246. break;
  247. case Opt_noheap:
  248. set_opt(sbi, NOHEAP);
  249. break;
  250. #ifdef CONFIG_F2FS_FS_XATTR
  251. case Opt_nouser_xattr:
  252. clear_opt(sbi, XATTR_USER);
  253. break;
  254. #else
  255. case Opt_nouser_xattr:
  256. f2fs_msg(sb, KERN_INFO,
  257. "nouser_xattr options not supported");
  258. break;
  259. #endif
  260. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  261. case Opt_noacl:
  262. clear_opt(sbi, POSIX_ACL);
  263. break;
  264. #else
  265. case Opt_noacl:
  266. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  267. break;
  268. #endif
  269. case Opt_active_logs:
  270. if (args->from && match_int(args, &arg))
  271. return -EINVAL;
  272. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  273. return -EINVAL;
  274. sbi->active_logs = arg;
  275. break;
  276. case Opt_disable_ext_identify:
  277. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  278. break;
  279. default:
  280. f2fs_msg(sb, KERN_ERR,
  281. "Unrecognized mount option \"%s\" or missing value",
  282. p);
  283. return -EINVAL;
  284. }
  285. }
  286. return 0;
  287. }
  288. static loff_t max_file_size(unsigned bits)
  289. {
  290. loff_t result = ADDRS_PER_INODE;
  291. loff_t leaf_count = ADDRS_PER_BLOCK;
  292. /* two direct node blocks */
  293. result += (leaf_count * 2);
  294. /* two indirect node blocks */
  295. leaf_count *= NIDS_PER_BLOCK;
  296. result += (leaf_count * 2);
  297. /* one double indirect node block */
  298. leaf_count *= NIDS_PER_BLOCK;
  299. result += leaf_count;
  300. result <<= bits;
  301. return result;
  302. }
  303. static int sanity_check_raw_super(struct super_block *sb,
  304. struct f2fs_super_block *raw_super)
  305. {
  306. unsigned int blocksize;
  307. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  308. f2fs_msg(sb, KERN_INFO,
  309. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  310. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  311. return 1;
  312. }
  313. /* Currently, support only 4KB block size */
  314. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  315. if (blocksize != PAGE_CACHE_SIZE) {
  316. f2fs_msg(sb, KERN_INFO,
  317. "Invalid blocksize (%u), supports only 4KB\n",
  318. blocksize);
  319. return 1;
  320. }
  321. if (le32_to_cpu(raw_super->log_sectorsize) !=
  322. F2FS_LOG_SECTOR_SIZE) {
  323. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
  324. return 1;
  325. }
  326. if (le32_to_cpu(raw_super->log_sectors_per_block) !=
  327. F2FS_LOG_SECTORS_PER_BLOCK) {
  328. f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static int sanity_check_ckpt(struct f2fs_super_block *raw_super,
  334. struct f2fs_checkpoint *ckpt)
  335. {
  336. unsigned int total, fsmeta;
  337. total = le32_to_cpu(raw_super->segment_count);
  338. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  339. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  340. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  341. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  342. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  343. if (fsmeta >= total)
  344. return 1;
  345. return 0;
  346. }
  347. static void init_sb_info(struct f2fs_sb_info *sbi)
  348. {
  349. struct f2fs_super_block *raw_super = sbi->raw_super;
  350. int i;
  351. sbi->log_sectors_per_block =
  352. le32_to_cpu(raw_super->log_sectors_per_block);
  353. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  354. sbi->blocksize = 1 << sbi->log_blocksize;
  355. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  356. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  357. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  358. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  359. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  360. sbi->total_node_count =
  361. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  362. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  363. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  364. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  365. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  366. for (i = 0; i < NR_COUNT_TYPE; i++)
  367. atomic_set(&sbi->nr_pages[i], 0);
  368. }
  369. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  370. {
  371. struct f2fs_sb_info *sbi;
  372. struct f2fs_super_block *raw_super;
  373. struct buffer_head *raw_super_buf;
  374. struct inode *root;
  375. long err = -EINVAL;
  376. int i;
  377. /* allocate memory for f2fs-specific super block info */
  378. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  379. if (!sbi)
  380. return -ENOMEM;
  381. /* set a block size */
  382. if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
  383. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  384. goto free_sbi;
  385. }
  386. /* read f2fs raw super block */
  387. raw_super_buf = sb_bread(sb, 0);
  388. if (!raw_super_buf) {
  389. err = -EIO;
  390. f2fs_msg(sb, KERN_ERR, "unable to read superblock");
  391. goto free_sbi;
  392. }
  393. raw_super = (struct f2fs_super_block *)
  394. ((char *)raw_super_buf->b_data + F2FS_SUPER_OFFSET);
  395. /* init some FS parameters */
  396. sbi->active_logs = NR_CURSEG_TYPE;
  397. set_opt(sbi, BG_GC);
  398. #ifdef CONFIG_F2FS_FS_XATTR
  399. set_opt(sbi, XATTR_USER);
  400. #endif
  401. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  402. set_opt(sbi, POSIX_ACL);
  403. #endif
  404. /* parse mount options */
  405. if (parse_options(sb, sbi, (char *)data))
  406. goto free_sb_buf;
  407. /* sanity checking of raw super */
  408. if (sanity_check_raw_super(sb, raw_super)) {
  409. f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem");
  410. goto free_sb_buf;
  411. }
  412. sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
  413. sb->s_max_links = F2FS_LINK_MAX;
  414. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  415. sb->s_op = &f2fs_sops;
  416. sb->s_xattr = f2fs_xattr_handlers;
  417. sb->s_export_op = &f2fs_export_ops;
  418. sb->s_magic = F2FS_SUPER_MAGIC;
  419. sb->s_fs_info = sbi;
  420. sb->s_time_gran = 1;
  421. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  422. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  423. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  424. /* init f2fs-specific super block info */
  425. sbi->sb = sb;
  426. sbi->raw_super = raw_super;
  427. sbi->raw_super_buf = raw_super_buf;
  428. mutex_init(&sbi->gc_mutex);
  429. mutex_init(&sbi->write_inode);
  430. mutex_init(&sbi->writepages);
  431. mutex_init(&sbi->cp_mutex);
  432. for (i = 0; i < NR_LOCK_TYPE; i++)
  433. mutex_init(&sbi->fs_lock[i]);
  434. sbi->por_doing = 0;
  435. spin_lock_init(&sbi->stat_lock);
  436. init_rwsem(&sbi->bio_sem);
  437. init_sb_info(sbi);
  438. /* get an inode for meta space */
  439. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  440. if (IS_ERR(sbi->meta_inode)) {
  441. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  442. err = PTR_ERR(sbi->meta_inode);
  443. goto free_sb_buf;
  444. }
  445. err = get_valid_checkpoint(sbi);
  446. if (err) {
  447. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  448. goto free_meta_inode;
  449. }
  450. /* sanity checking of checkpoint */
  451. err = -EINVAL;
  452. if (sanity_check_ckpt(raw_super, sbi->ckpt)) {
  453. f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
  454. goto free_cp;
  455. }
  456. sbi->total_valid_node_count =
  457. le32_to_cpu(sbi->ckpt->valid_node_count);
  458. sbi->total_valid_inode_count =
  459. le32_to_cpu(sbi->ckpt->valid_inode_count);
  460. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  461. sbi->total_valid_block_count =
  462. le64_to_cpu(sbi->ckpt->valid_block_count);
  463. sbi->last_valid_block_count = sbi->total_valid_block_count;
  464. sbi->alloc_valid_block_count = 0;
  465. INIT_LIST_HEAD(&sbi->dir_inode_list);
  466. spin_lock_init(&sbi->dir_inode_lock);
  467. init_orphan_info(sbi);
  468. /* setup f2fs internal modules */
  469. err = build_segment_manager(sbi);
  470. if (err) {
  471. f2fs_msg(sb, KERN_ERR,
  472. "Failed to initialize F2FS segment manager");
  473. goto free_sm;
  474. }
  475. err = build_node_manager(sbi);
  476. if (err) {
  477. f2fs_msg(sb, KERN_ERR,
  478. "Failed to initialize F2FS node manager");
  479. goto free_nm;
  480. }
  481. build_gc_manager(sbi);
  482. /* get an inode for node space */
  483. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  484. if (IS_ERR(sbi->node_inode)) {
  485. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  486. err = PTR_ERR(sbi->node_inode);
  487. goto free_nm;
  488. }
  489. /* if there are nt orphan nodes free them */
  490. err = -EINVAL;
  491. if (recover_orphan_inodes(sbi))
  492. goto free_node_inode;
  493. /* read root inode and dentry */
  494. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  495. if (IS_ERR(root)) {
  496. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  497. err = PTR_ERR(root);
  498. goto free_node_inode;
  499. }
  500. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
  501. goto free_root_inode;
  502. sb->s_root = d_make_root(root); /* allocate root dentry */
  503. if (!sb->s_root) {
  504. err = -ENOMEM;
  505. goto free_root_inode;
  506. }
  507. /* recover fsynced data */
  508. if (!test_opt(sbi, DISABLE_ROLL_FORWARD))
  509. recover_fsync_data(sbi);
  510. /* After POR, we can run background GC thread */
  511. err = start_gc_thread(sbi);
  512. if (err)
  513. goto fail;
  514. err = f2fs_build_stats(sbi);
  515. if (err)
  516. goto fail;
  517. return 0;
  518. fail:
  519. stop_gc_thread(sbi);
  520. free_root_inode:
  521. dput(sb->s_root);
  522. sb->s_root = NULL;
  523. free_node_inode:
  524. iput(sbi->node_inode);
  525. free_nm:
  526. destroy_node_manager(sbi);
  527. free_sm:
  528. destroy_segment_manager(sbi);
  529. free_cp:
  530. kfree(sbi->ckpt);
  531. free_meta_inode:
  532. make_bad_inode(sbi->meta_inode);
  533. iput(sbi->meta_inode);
  534. free_sb_buf:
  535. brelse(raw_super_buf);
  536. free_sbi:
  537. kfree(sbi);
  538. return err;
  539. }
  540. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  541. const char *dev_name, void *data)
  542. {
  543. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  544. }
  545. static struct file_system_type f2fs_fs_type = {
  546. .owner = THIS_MODULE,
  547. .name = "f2fs",
  548. .mount = f2fs_mount,
  549. .kill_sb = kill_block_super,
  550. .fs_flags = FS_REQUIRES_DEV,
  551. };
  552. static int __init init_inodecache(void)
  553. {
  554. f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
  555. sizeof(struct f2fs_inode_info), NULL);
  556. if (f2fs_inode_cachep == NULL)
  557. return -ENOMEM;
  558. return 0;
  559. }
  560. static void destroy_inodecache(void)
  561. {
  562. /*
  563. * Make sure all delayed rcu free inodes are flushed before we
  564. * destroy cache.
  565. */
  566. rcu_barrier();
  567. kmem_cache_destroy(f2fs_inode_cachep);
  568. }
  569. static int __init init_f2fs_fs(void)
  570. {
  571. int err;
  572. err = init_inodecache();
  573. if (err)
  574. goto fail;
  575. err = create_node_manager_caches();
  576. if (err)
  577. goto fail;
  578. err = create_gc_caches();
  579. if (err)
  580. goto fail;
  581. err = create_checkpoint_caches();
  582. if (err)
  583. goto fail;
  584. err = register_filesystem(&f2fs_fs_type);
  585. if (err)
  586. goto fail;
  587. f2fs_create_root_stats();
  588. fail:
  589. return err;
  590. }
  591. static void __exit exit_f2fs_fs(void)
  592. {
  593. f2fs_destroy_root_stats();
  594. unregister_filesystem(&f2fs_fs_type);
  595. destroy_checkpoint_caches();
  596. destroy_gc_caches();
  597. destroy_node_manager_caches();
  598. destroy_inodecache();
  599. }
  600. module_init(init_f2fs_fs)
  601. module_exit(exit_f2fs_fs)
  602. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  603. MODULE_DESCRIPTION("Flash Friendly File System");
  604. MODULE_LICENSE("GPL");