super.c 19 KB

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