super.c 26 KB

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