super.c 26 KB

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