super.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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_nouser_xattr,
  43. Opt_noacl,
  44. Opt_active_logs,
  45. Opt_disable_ext_identify,
  46. Opt_err,
  47. };
  48. static match_table_t f2fs_tokens = {
  49. {Opt_gc_background, "background_gc=%s"},
  50. {Opt_disable_roll_forward, "disable_roll_forward"},
  51. {Opt_discard, "discard"},
  52. {Opt_noheap, "no_heap"},
  53. {Opt_nouser_xattr, "nouser_xattr"},
  54. {Opt_noacl, "noacl"},
  55. {Opt_active_logs, "active_logs=%u"},
  56. {Opt_disable_ext_identify, "disable_ext_identify"},
  57. {Opt_err, NULL},
  58. };
  59. /* Sysfs support for f2fs */
  60. struct f2fs_attr {
  61. struct attribute attr;
  62. ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
  63. ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
  64. const char *, size_t);
  65. int offset;
  66. };
  67. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  68. struct f2fs_sb_info *sbi, char *buf)
  69. {
  70. struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
  71. unsigned int *ui;
  72. if (!gc_kth)
  73. return -EINVAL;
  74. ui = (unsigned int *)(((char *)gc_kth) + a->offset);
  75. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  76. }
  77. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  78. struct f2fs_sb_info *sbi,
  79. const char *buf, size_t count)
  80. {
  81. struct f2fs_gc_kthread *gc_kth = sbi->gc_thread;
  82. unsigned long t;
  83. unsigned int *ui;
  84. ssize_t ret;
  85. if (!gc_kth)
  86. return -EINVAL;
  87. ui = (unsigned int *)(((char *)gc_kth) + a->offset);
  88. ret = kstrtoul(skip_spaces(buf), 0, &t);
  89. if (ret < 0)
  90. return ret;
  91. *ui = t;
  92. return count;
  93. }
  94. static ssize_t f2fs_attr_show(struct kobject *kobj,
  95. struct attribute *attr, char *buf)
  96. {
  97. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  98. s_kobj);
  99. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  100. return a->show ? a->show(a, sbi, buf) : 0;
  101. }
  102. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  103. const char *buf, size_t len)
  104. {
  105. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  106. s_kobj);
  107. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  108. return a->store ? a->store(a, sbi, buf, len) : 0;
  109. }
  110. static void f2fs_sb_release(struct kobject *kobj)
  111. {
  112. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  113. s_kobj);
  114. complete(&sbi->s_kobj_unregister);
  115. }
  116. #define F2FS_ATTR_OFFSET(_name, _mode, _show, _store, _elname) \
  117. static struct f2fs_attr f2fs_attr_##_name = { \
  118. .attr = {.name = __stringify(_name), .mode = _mode }, \
  119. .show = _show, \
  120. .store = _store, \
  121. .offset = offsetof(struct f2fs_gc_kthread, _elname), \
  122. }
  123. #define F2FS_RW_ATTR(name, elname) \
  124. F2FS_ATTR_OFFSET(name, 0644, f2fs_sbi_show, f2fs_sbi_store, elname)
  125. F2FS_RW_ATTR(gc_min_sleep_time, min_sleep_time);
  126. F2FS_RW_ATTR(gc_max_sleep_time, max_sleep_time);
  127. F2FS_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
  128. F2FS_RW_ATTR(gc_idle, gc_idle);
  129. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  130. static struct attribute *f2fs_attrs[] = {
  131. ATTR_LIST(gc_min_sleep_time),
  132. ATTR_LIST(gc_max_sleep_time),
  133. ATTR_LIST(gc_no_gc_sleep_time),
  134. ATTR_LIST(gc_idle),
  135. NULL,
  136. };
  137. static const struct sysfs_ops f2fs_attr_ops = {
  138. .show = f2fs_attr_show,
  139. .store = f2fs_attr_store,
  140. };
  141. static struct kobj_type f2fs_ktype = {
  142. .default_attrs = f2fs_attrs,
  143. .sysfs_ops = &f2fs_attr_ops,
  144. .release = f2fs_sb_release,
  145. };
  146. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  147. {
  148. struct va_format vaf;
  149. va_list args;
  150. va_start(args, fmt);
  151. vaf.fmt = fmt;
  152. vaf.va = &args;
  153. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  154. va_end(args);
  155. }
  156. static void init_once(void *foo)
  157. {
  158. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  159. inode_init_once(&fi->vfs_inode);
  160. }
  161. static int parse_options(struct super_block *sb, char *options)
  162. {
  163. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  164. substring_t args[MAX_OPT_ARGS];
  165. char *p, *name;
  166. int arg = 0;
  167. if (!options)
  168. return 0;
  169. while ((p = strsep(&options, ",")) != NULL) {
  170. int token;
  171. if (!*p)
  172. continue;
  173. /*
  174. * Initialize args struct so we know whether arg was
  175. * found; some options take optional arguments.
  176. */
  177. args[0].to = args[0].from = NULL;
  178. token = match_token(p, f2fs_tokens, args);
  179. switch (token) {
  180. case Opt_gc_background:
  181. name = match_strdup(&args[0]);
  182. if (!name)
  183. return -ENOMEM;
  184. if (!strncmp(name, "on", 2))
  185. set_opt(sbi, BG_GC);
  186. else if (!strncmp(name, "off", 3))
  187. clear_opt(sbi, BG_GC);
  188. else {
  189. kfree(name);
  190. return -EINVAL;
  191. }
  192. kfree(name);
  193. break;
  194. case Opt_disable_roll_forward:
  195. set_opt(sbi, DISABLE_ROLL_FORWARD);
  196. break;
  197. case Opt_discard:
  198. set_opt(sbi, DISCARD);
  199. break;
  200. case Opt_noheap:
  201. set_opt(sbi, NOHEAP);
  202. break;
  203. #ifdef CONFIG_F2FS_FS_XATTR
  204. case Opt_nouser_xattr:
  205. clear_opt(sbi, XATTR_USER);
  206. break;
  207. #else
  208. case Opt_nouser_xattr:
  209. f2fs_msg(sb, KERN_INFO,
  210. "nouser_xattr options not supported");
  211. break;
  212. #endif
  213. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  214. case Opt_noacl:
  215. clear_opt(sbi, POSIX_ACL);
  216. break;
  217. #else
  218. case Opt_noacl:
  219. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  220. break;
  221. #endif
  222. case Opt_active_logs:
  223. if (args->from && match_int(args, &arg))
  224. return -EINVAL;
  225. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  226. return -EINVAL;
  227. sbi->active_logs = arg;
  228. break;
  229. case Opt_disable_ext_identify:
  230. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  231. break;
  232. default:
  233. f2fs_msg(sb, KERN_ERR,
  234. "Unrecognized mount option \"%s\" or missing value",
  235. p);
  236. return -EINVAL;
  237. }
  238. }
  239. return 0;
  240. }
  241. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  242. {
  243. struct f2fs_inode_info *fi;
  244. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_NOFS | __GFP_ZERO);
  245. if (!fi)
  246. return NULL;
  247. init_once((void *) fi);
  248. /* Initialize f2fs-specific inode info */
  249. fi->vfs_inode.i_version = 1;
  250. atomic_set(&fi->dirty_dents, 0);
  251. fi->i_current_depth = 1;
  252. fi->i_advise = 0;
  253. rwlock_init(&fi->ext.ext_lock);
  254. set_inode_flag(fi, FI_NEW_INODE);
  255. return &fi->vfs_inode;
  256. }
  257. static int f2fs_drop_inode(struct inode *inode)
  258. {
  259. /*
  260. * This is to avoid a deadlock condition like below.
  261. * writeback_single_inode(inode)
  262. * - f2fs_write_data_page
  263. * - f2fs_gc -> iput -> evict
  264. * - inode_wait_for_writeback(inode)
  265. */
  266. if (!inode_unhashed(inode) && inode->i_state & I_SYNC)
  267. return 0;
  268. return generic_drop_inode(inode);
  269. }
  270. /*
  271. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  272. *
  273. * We should call set_dirty_inode to write the dirty inode through write_inode.
  274. */
  275. static void f2fs_dirty_inode(struct inode *inode, int flags)
  276. {
  277. set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
  278. return;
  279. }
  280. static void f2fs_i_callback(struct rcu_head *head)
  281. {
  282. struct inode *inode = container_of(head, struct inode, i_rcu);
  283. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  284. }
  285. static void f2fs_destroy_inode(struct inode *inode)
  286. {
  287. call_rcu(&inode->i_rcu, f2fs_i_callback);
  288. }
  289. static void f2fs_put_super(struct super_block *sb)
  290. {
  291. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  292. if (sbi->s_proc) {
  293. remove_proc_entry("segment_info", sbi->s_proc);
  294. remove_proc_entry(sb->s_id, f2fs_proc_root);
  295. }
  296. kobject_del(&sbi->s_kobj);
  297. f2fs_destroy_stats(sbi);
  298. stop_gc_thread(sbi);
  299. write_checkpoint(sbi, true);
  300. iput(sbi->node_inode);
  301. iput(sbi->meta_inode);
  302. /* destroy f2fs internal modules */
  303. destroy_node_manager(sbi);
  304. destroy_segment_manager(sbi);
  305. kfree(sbi->ckpt);
  306. kobject_put(&sbi->s_kobj);
  307. wait_for_completion(&sbi->s_kobj_unregister);
  308. sb->s_fs_info = NULL;
  309. brelse(sbi->raw_super_buf);
  310. kfree(sbi);
  311. }
  312. int f2fs_sync_fs(struct super_block *sb, int sync)
  313. {
  314. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  315. trace_f2fs_sync_fs(sb, sync);
  316. if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
  317. return 0;
  318. if (sync) {
  319. mutex_lock(&sbi->gc_mutex);
  320. write_checkpoint(sbi, false);
  321. mutex_unlock(&sbi->gc_mutex);
  322. } else {
  323. f2fs_balance_fs(sbi);
  324. }
  325. return 0;
  326. }
  327. static int f2fs_freeze(struct super_block *sb)
  328. {
  329. int err;
  330. if (f2fs_readonly(sb))
  331. return 0;
  332. err = f2fs_sync_fs(sb, 1);
  333. return err;
  334. }
  335. static int f2fs_unfreeze(struct super_block *sb)
  336. {
  337. return 0;
  338. }
  339. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  340. {
  341. struct super_block *sb = dentry->d_sb;
  342. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  343. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  344. block_t total_count, user_block_count, start_count, ovp_count;
  345. total_count = le64_to_cpu(sbi->raw_super->block_count);
  346. user_block_count = sbi->user_block_count;
  347. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  348. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  349. buf->f_type = F2FS_SUPER_MAGIC;
  350. buf->f_bsize = sbi->blocksize;
  351. buf->f_blocks = total_count - start_count;
  352. buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
  353. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  354. buf->f_files = sbi->total_node_count;
  355. buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
  356. buf->f_namelen = F2FS_NAME_LEN;
  357. buf->f_fsid.val[0] = (u32)id;
  358. buf->f_fsid.val[1] = (u32)(id >> 32);
  359. return 0;
  360. }
  361. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  362. {
  363. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  364. if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
  365. seq_printf(seq, ",background_gc=%s", "on");
  366. else
  367. seq_printf(seq, ",background_gc=%s", "off");
  368. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  369. seq_puts(seq, ",disable_roll_forward");
  370. if (test_opt(sbi, DISCARD))
  371. seq_puts(seq, ",discard");
  372. if (test_opt(sbi, NOHEAP))
  373. seq_puts(seq, ",no_heap_alloc");
  374. #ifdef CONFIG_F2FS_FS_XATTR
  375. if (test_opt(sbi, XATTR_USER))
  376. seq_puts(seq, ",user_xattr");
  377. else
  378. seq_puts(seq, ",nouser_xattr");
  379. #endif
  380. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  381. if (test_opt(sbi, POSIX_ACL))
  382. seq_puts(seq, ",acl");
  383. else
  384. seq_puts(seq, ",noacl");
  385. #endif
  386. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  387. seq_puts(seq, ",disable_ext_identify");
  388. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  389. return 0;
  390. }
  391. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  392. {
  393. struct super_block *sb = seq->private;
  394. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  395. unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
  396. int i;
  397. for (i = 0; i < total_segs; i++) {
  398. seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
  399. if (i != 0 && (i % 10) == 0)
  400. seq_puts(seq, "\n");
  401. else
  402. seq_puts(seq, " ");
  403. }
  404. return 0;
  405. }
  406. static int segment_info_open_fs(struct inode *inode, struct file *file)
  407. {
  408. return single_open(file, segment_info_seq_show, PDE_DATA(inode));
  409. }
  410. static const struct file_operations f2fs_seq_segment_info_fops = {
  411. .owner = THIS_MODULE,
  412. .open = segment_info_open_fs,
  413. .read = seq_read,
  414. .llseek = seq_lseek,
  415. .release = single_release,
  416. };
  417. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  418. {
  419. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  420. struct f2fs_mount_info org_mount_opt;
  421. int err, active_logs;
  422. /*
  423. * Save the old mount options in case we
  424. * need to restore them.
  425. */
  426. org_mount_opt = sbi->mount_opt;
  427. active_logs = sbi->active_logs;
  428. /* parse mount options */
  429. err = parse_options(sb, data);
  430. if (err)
  431. goto restore_opts;
  432. /*
  433. * Previous and new state of filesystem is RO,
  434. * so no point in checking GC conditions.
  435. */
  436. if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
  437. goto skip;
  438. /*
  439. * We stop the GC thread if FS is mounted as RO
  440. * or if background_gc = off is passed in mount
  441. * option. Also sync the filesystem.
  442. */
  443. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  444. if (sbi->gc_thread) {
  445. stop_gc_thread(sbi);
  446. f2fs_sync_fs(sb, 1);
  447. }
  448. } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
  449. err = start_gc_thread(sbi);
  450. if (err)
  451. goto restore_opts;
  452. }
  453. skip:
  454. /* Update the POSIXACL Flag */
  455. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  456. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  457. return 0;
  458. restore_opts:
  459. sbi->mount_opt = org_mount_opt;
  460. sbi->active_logs = active_logs;
  461. return err;
  462. }
  463. static struct super_operations f2fs_sops = {
  464. .alloc_inode = f2fs_alloc_inode,
  465. .drop_inode = f2fs_drop_inode,
  466. .destroy_inode = f2fs_destroy_inode,
  467. .write_inode = f2fs_write_inode,
  468. .dirty_inode = f2fs_dirty_inode,
  469. .show_options = f2fs_show_options,
  470. .evict_inode = f2fs_evict_inode,
  471. .put_super = f2fs_put_super,
  472. .sync_fs = f2fs_sync_fs,
  473. .freeze_fs = f2fs_freeze,
  474. .unfreeze_fs = f2fs_unfreeze,
  475. .statfs = f2fs_statfs,
  476. .remount_fs = f2fs_remount,
  477. };
  478. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  479. u64 ino, u32 generation)
  480. {
  481. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  482. struct inode *inode;
  483. if (ino < F2FS_ROOT_INO(sbi))
  484. return ERR_PTR(-ESTALE);
  485. /*
  486. * f2fs_iget isn't quite right if the inode is currently unallocated!
  487. * However f2fs_iget currently does appropriate checks to handle stale
  488. * inodes so everything is OK.
  489. */
  490. inode = f2fs_iget(sb, ino);
  491. if (IS_ERR(inode))
  492. return ERR_CAST(inode);
  493. if (generation && inode->i_generation != generation) {
  494. /* we didn't find the right inode.. */
  495. iput(inode);
  496. return ERR_PTR(-ESTALE);
  497. }
  498. return inode;
  499. }
  500. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  501. int fh_len, int fh_type)
  502. {
  503. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  504. f2fs_nfs_get_inode);
  505. }
  506. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  507. int fh_len, int fh_type)
  508. {
  509. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  510. f2fs_nfs_get_inode);
  511. }
  512. static const struct export_operations f2fs_export_ops = {
  513. .fh_to_dentry = f2fs_fh_to_dentry,
  514. .fh_to_parent = f2fs_fh_to_parent,
  515. .get_parent = f2fs_get_parent,
  516. };
  517. static loff_t max_file_size(unsigned bits)
  518. {
  519. loff_t result = ADDRS_PER_INODE;
  520. loff_t leaf_count = ADDRS_PER_BLOCK;
  521. /* two direct node blocks */
  522. result += (leaf_count * 2);
  523. /* two indirect node blocks */
  524. leaf_count *= NIDS_PER_BLOCK;
  525. result += (leaf_count * 2);
  526. /* one double indirect node block */
  527. leaf_count *= NIDS_PER_BLOCK;
  528. result += leaf_count;
  529. result <<= bits;
  530. return result;
  531. }
  532. static int sanity_check_raw_super(struct super_block *sb,
  533. struct f2fs_super_block *raw_super)
  534. {
  535. unsigned int blocksize;
  536. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  537. f2fs_msg(sb, KERN_INFO,
  538. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  539. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  540. return 1;
  541. }
  542. /* Currently, support only 4KB page cache size */
  543. if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
  544. f2fs_msg(sb, KERN_INFO,
  545. "Invalid page_cache_size (%lu), supports only 4KB\n",
  546. PAGE_CACHE_SIZE);
  547. return 1;
  548. }
  549. /* Currently, support only 4KB block size */
  550. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  551. if (blocksize != F2FS_BLKSIZE) {
  552. f2fs_msg(sb, KERN_INFO,
  553. "Invalid blocksize (%u), supports only 4KB\n",
  554. blocksize);
  555. return 1;
  556. }
  557. if (le32_to_cpu(raw_super->log_sectorsize) !=
  558. F2FS_LOG_SECTOR_SIZE) {
  559. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
  560. return 1;
  561. }
  562. if (le32_to_cpu(raw_super->log_sectors_per_block) !=
  563. F2FS_LOG_SECTORS_PER_BLOCK) {
  564. f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  570. {
  571. unsigned int total, fsmeta;
  572. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  573. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  574. total = le32_to_cpu(raw_super->segment_count);
  575. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  576. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  577. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  578. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  579. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  580. if (fsmeta >= total)
  581. return 1;
  582. if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
  583. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  584. return 1;
  585. }
  586. return 0;
  587. }
  588. static void init_sb_info(struct f2fs_sb_info *sbi)
  589. {
  590. struct f2fs_super_block *raw_super = sbi->raw_super;
  591. int i;
  592. sbi->log_sectors_per_block =
  593. le32_to_cpu(raw_super->log_sectors_per_block);
  594. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  595. sbi->blocksize = 1 << sbi->log_blocksize;
  596. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  597. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  598. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  599. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  600. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  601. sbi->total_node_count =
  602. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  603. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  604. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  605. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  606. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  607. sbi->cur_victim_sec = NULL_SECNO;
  608. for (i = 0; i < NR_COUNT_TYPE; i++)
  609. atomic_set(&sbi->nr_pages[i], 0);
  610. }
  611. static int validate_superblock(struct super_block *sb,
  612. struct f2fs_super_block **raw_super,
  613. struct buffer_head **raw_super_buf, sector_t block)
  614. {
  615. const char *super = (block == 0 ? "first" : "second");
  616. /* read f2fs raw super block */
  617. *raw_super_buf = sb_bread(sb, block);
  618. if (!*raw_super_buf) {
  619. f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
  620. super);
  621. return -EIO;
  622. }
  623. *raw_super = (struct f2fs_super_block *)
  624. ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
  625. /* sanity checking of raw super */
  626. if (!sanity_check_raw_super(sb, *raw_super))
  627. return 0;
  628. f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
  629. "in %s superblock", super);
  630. return -EINVAL;
  631. }
  632. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  633. {
  634. struct f2fs_sb_info *sbi;
  635. struct f2fs_super_block *raw_super;
  636. struct buffer_head *raw_super_buf;
  637. struct inode *root;
  638. long err = -EINVAL;
  639. int i;
  640. /* allocate memory for f2fs-specific super block info */
  641. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  642. if (!sbi)
  643. return -ENOMEM;
  644. /* set a block size */
  645. if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
  646. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  647. goto free_sbi;
  648. }
  649. err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
  650. if (err) {
  651. brelse(raw_super_buf);
  652. /* check secondary superblock when primary failed */
  653. err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
  654. if (err)
  655. goto free_sb_buf;
  656. }
  657. sb->s_fs_info = sbi;
  658. /* init some FS parameters */
  659. sbi->active_logs = NR_CURSEG_TYPE;
  660. set_opt(sbi, BG_GC);
  661. #ifdef CONFIG_F2FS_FS_XATTR
  662. set_opt(sbi, XATTR_USER);
  663. #endif
  664. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  665. set_opt(sbi, POSIX_ACL);
  666. #endif
  667. /* parse mount options */
  668. err = parse_options(sb, (char *)data);
  669. if (err)
  670. goto free_sb_buf;
  671. sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
  672. sb->s_max_links = F2FS_LINK_MAX;
  673. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  674. sb->s_op = &f2fs_sops;
  675. sb->s_xattr = f2fs_xattr_handlers;
  676. sb->s_export_op = &f2fs_export_ops;
  677. sb->s_magic = F2FS_SUPER_MAGIC;
  678. sb->s_time_gran = 1;
  679. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  680. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  681. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  682. /* init f2fs-specific super block info */
  683. sbi->sb = sb;
  684. sbi->raw_super = raw_super;
  685. sbi->raw_super_buf = raw_super_buf;
  686. mutex_init(&sbi->gc_mutex);
  687. mutex_init(&sbi->writepages);
  688. mutex_init(&sbi->cp_mutex);
  689. for (i = 0; i < NR_GLOBAL_LOCKS; i++)
  690. mutex_init(&sbi->fs_lock[i]);
  691. mutex_init(&sbi->node_write);
  692. sbi->por_doing = 0;
  693. spin_lock_init(&sbi->stat_lock);
  694. init_rwsem(&sbi->bio_sem);
  695. init_sb_info(sbi);
  696. /* get an inode for meta space */
  697. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  698. if (IS_ERR(sbi->meta_inode)) {
  699. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  700. err = PTR_ERR(sbi->meta_inode);
  701. goto free_sb_buf;
  702. }
  703. err = get_valid_checkpoint(sbi);
  704. if (err) {
  705. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  706. goto free_meta_inode;
  707. }
  708. /* sanity checking of checkpoint */
  709. err = -EINVAL;
  710. if (sanity_check_ckpt(sbi)) {
  711. f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
  712. goto free_cp;
  713. }
  714. sbi->total_valid_node_count =
  715. le32_to_cpu(sbi->ckpt->valid_node_count);
  716. sbi->total_valid_inode_count =
  717. le32_to_cpu(sbi->ckpt->valid_inode_count);
  718. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  719. sbi->total_valid_block_count =
  720. le64_to_cpu(sbi->ckpt->valid_block_count);
  721. sbi->last_valid_block_count = sbi->total_valid_block_count;
  722. sbi->alloc_valid_block_count = 0;
  723. INIT_LIST_HEAD(&sbi->dir_inode_list);
  724. spin_lock_init(&sbi->dir_inode_lock);
  725. init_orphan_info(sbi);
  726. /* setup f2fs internal modules */
  727. err = build_segment_manager(sbi);
  728. if (err) {
  729. f2fs_msg(sb, KERN_ERR,
  730. "Failed to initialize F2FS segment manager");
  731. goto free_sm;
  732. }
  733. err = build_node_manager(sbi);
  734. if (err) {
  735. f2fs_msg(sb, KERN_ERR,
  736. "Failed to initialize F2FS node manager");
  737. goto free_nm;
  738. }
  739. build_gc_manager(sbi);
  740. /* get an inode for node space */
  741. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  742. if (IS_ERR(sbi->node_inode)) {
  743. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  744. err = PTR_ERR(sbi->node_inode);
  745. goto free_nm;
  746. }
  747. /* if there are nt orphan nodes free them */
  748. err = -EINVAL;
  749. if (recover_orphan_inodes(sbi))
  750. goto free_node_inode;
  751. /* read root inode and dentry */
  752. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  753. if (IS_ERR(root)) {
  754. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  755. err = PTR_ERR(root);
  756. goto free_node_inode;
  757. }
  758. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
  759. goto free_root_inode;
  760. sb->s_root = d_make_root(root); /* allocate root dentry */
  761. if (!sb->s_root) {
  762. err = -ENOMEM;
  763. goto free_root_inode;
  764. }
  765. /* recover fsynced data */
  766. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  767. err = recover_fsync_data(sbi);
  768. if (err)
  769. f2fs_msg(sb, KERN_ERR,
  770. "Cannot recover all fsync data errno=%ld", err);
  771. }
  772. /*
  773. * If filesystem is not mounted as read-only then
  774. * do start the gc_thread.
  775. */
  776. if (!(sb->s_flags & MS_RDONLY)) {
  777. /* After POR, we can run background GC thread.*/
  778. err = start_gc_thread(sbi);
  779. if (err)
  780. goto fail;
  781. }
  782. err = f2fs_build_stats(sbi);
  783. if (err)
  784. goto fail;
  785. if (f2fs_proc_root)
  786. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  787. if (sbi->s_proc)
  788. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  789. &f2fs_seq_segment_info_fops, sb);
  790. if (test_opt(sbi, DISCARD)) {
  791. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  792. if (!blk_queue_discard(q))
  793. f2fs_msg(sb, KERN_WARNING,
  794. "mounting with \"discard\" option, but "
  795. "the device does not support discard");
  796. }
  797. sbi->s_kobj.kset = f2fs_kset;
  798. init_completion(&sbi->s_kobj_unregister);
  799. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  800. "%s", sb->s_id);
  801. if (err)
  802. goto fail;
  803. return 0;
  804. fail:
  805. stop_gc_thread(sbi);
  806. free_root_inode:
  807. dput(sb->s_root);
  808. sb->s_root = NULL;
  809. free_node_inode:
  810. iput(sbi->node_inode);
  811. free_nm:
  812. destroy_node_manager(sbi);
  813. free_sm:
  814. destroy_segment_manager(sbi);
  815. free_cp:
  816. kfree(sbi->ckpt);
  817. free_meta_inode:
  818. make_bad_inode(sbi->meta_inode);
  819. iput(sbi->meta_inode);
  820. free_sb_buf:
  821. brelse(raw_super_buf);
  822. free_sbi:
  823. kfree(sbi);
  824. return err;
  825. }
  826. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  827. const char *dev_name, void *data)
  828. {
  829. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  830. }
  831. static struct file_system_type f2fs_fs_type = {
  832. .owner = THIS_MODULE,
  833. .name = "f2fs",
  834. .mount = f2fs_mount,
  835. .kill_sb = kill_block_super,
  836. .fs_flags = FS_REQUIRES_DEV,
  837. };
  838. MODULE_ALIAS_FS("f2fs");
  839. static int __init init_inodecache(void)
  840. {
  841. f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
  842. sizeof(struct f2fs_inode_info), NULL);
  843. if (f2fs_inode_cachep == NULL)
  844. return -ENOMEM;
  845. return 0;
  846. }
  847. static void destroy_inodecache(void)
  848. {
  849. /*
  850. * Make sure all delayed rcu free inodes are flushed before we
  851. * destroy cache.
  852. */
  853. rcu_barrier();
  854. kmem_cache_destroy(f2fs_inode_cachep);
  855. }
  856. static int __init init_f2fs_fs(void)
  857. {
  858. int err;
  859. err = init_inodecache();
  860. if (err)
  861. goto fail;
  862. err = create_node_manager_caches();
  863. if (err)
  864. goto fail;
  865. err = create_gc_caches();
  866. if (err)
  867. goto fail;
  868. err = create_checkpoint_caches();
  869. if (err)
  870. goto fail;
  871. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  872. if (!f2fs_kset)
  873. goto fail;
  874. err = register_filesystem(&f2fs_fs_type);
  875. if (err)
  876. goto fail;
  877. f2fs_create_root_stats();
  878. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  879. fail:
  880. return err;
  881. }
  882. static void __exit exit_f2fs_fs(void)
  883. {
  884. remove_proc_entry("fs/f2fs", NULL);
  885. f2fs_destroy_root_stats();
  886. unregister_filesystem(&f2fs_fs_type);
  887. destroy_checkpoint_caches();
  888. destroy_gc_caches();
  889. destroy_node_manager_caches();
  890. destroy_inodecache();
  891. kset_unregister(f2fs_kset);
  892. }
  893. module_init(init_f2fs_fs)
  894. module_exit(exit_f2fs_fs)
  895. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  896. MODULE_DESCRIPTION("Flash Friendly File System");
  897. MODULE_LICENSE("GPL");