super.c 18 KB

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