super.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. write_checkpoint(sbi, true);
  327. iput(sbi->node_inode);
  328. iput(sbi->meta_inode);
  329. /* destroy f2fs internal modules */
  330. destroy_node_manager(sbi);
  331. destroy_segment_manager(sbi);
  332. kfree(sbi->ckpt);
  333. kobject_put(&sbi->s_kobj);
  334. wait_for_completion(&sbi->s_kobj_unregister);
  335. sb->s_fs_info = NULL;
  336. brelse(sbi->raw_super_buf);
  337. kfree(sbi);
  338. }
  339. int f2fs_sync_fs(struct super_block *sb, int sync)
  340. {
  341. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  342. trace_f2fs_sync_fs(sb, sync);
  343. if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
  344. return 0;
  345. if (sync) {
  346. mutex_lock(&sbi->gc_mutex);
  347. write_checkpoint(sbi, false);
  348. mutex_unlock(&sbi->gc_mutex);
  349. } else {
  350. f2fs_balance_fs(sbi);
  351. }
  352. return 0;
  353. }
  354. static int f2fs_freeze(struct super_block *sb)
  355. {
  356. int err;
  357. if (f2fs_readonly(sb))
  358. return 0;
  359. err = f2fs_sync_fs(sb, 1);
  360. return err;
  361. }
  362. static int f2fs_unfreeze(struct super_block *sb)
  363. {
  364. return 0;
  365. }
  366. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  367. {
  368. struct super_block *sb = dentry->d_sb;
  369. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  370. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  371. block_t total_count, user_block_count, start_count, ovp_count;
  372. total_count = le64_to_cpu(sbi->raw_super->block_count);
  373. user_block_count = sbi->user_block_count;
  374. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  375. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  376. buf->f_type = F2FS_SUPER_MAGIC;
  377. buf->f_bsize = sbi->blocksize;
  378. buf->f_blocks = total_count - start_count;
  379. buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
  380. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  381. buf->f_files = sbi->total_node_count;
  382. buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
  383. buf->f_namelen = F2FS_NAME_LEN;
  384. buf->f_fsid.val[0] = (u32)id;
  385. buf->f_fsid.val[1] = (u32)(id >> 32);
  386. return 0;
  387. }
  388. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  389. {
  390. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  391. if (!(root->d_sb->s_flags & MS_RDONLY) && test_opt(sbi, BG_GC))
  392. seq_printf(seq, ",background_gc=%s", "on");
  393. else
  394. seq_printf(seq, ",background_gc=%s", "off");
  395. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  396. seq_puts(seq, ",disable_roll_forward");
  397. if (test_opt(sbi, DISCARD))
  398. seq_puts(seq, ",discard");
  399. if (test_opt(sbi, NOHEAP))
  400. seq_puts(seq, ",no_heap_alloc");
  401. #ifdef CONFIG_F2FS_FS_XATTR
  402. if (test_opt(sbi, XATTR_USER))
  403. seq_puts(seq, ",user_xattr");
  404. else
  405. seq_puts(seq, ",nouser_xattr");
  406. if (test_opt(sbi, INLINE_XATTR))
  407. seq_puts(seq, ",inline_xattr");
  408. #endif
  409. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  410. if (test_opt(sbi, POSIX_ACL))
  411. seq_puts(seq, ",acl");
  412. else
  413. seq_puts(seq, ",noacl");
  414. #endif
  415. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  416. seq_puts(seq, ",disable_ext_identify");
  417. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  418. return 0;
  419. }
  420. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  421. {
  422. struct super_block *sb = seq->private;
  423. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  424. unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
  425. int i;
  426. for (i = 0; i < total_segs; i++) {
  427. seq_printf(seq, "%u", get_valid_blocks(sbi, i, 1));
  428. if (i != 0 && (i % 10) == 0)
  429. seq_puts(seq, "\n");
  430. else
  431. seq_puts(seq, " ");
  432. }
  433. return 0;
  434. }
  435. static int segment_info_open_fs(struct inode *inode, struct file *file)
  436. {
  437. return single_open(file, segment_info_seq_show, PDE_DATA(inode));
  438. }
  439. static const struct file_operations f2fs_seq_segment_info_fops = {
  440. .owner = THIS_MODULE,
  441. .open = segment_info_open_fs,
  442. .read = seq_read,
  443. .llseek = seq_lseek,
  444. .release = single_release,
  445. };
  446. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  447. {
  448. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  449. struct f2fs_mount_info org_mount_opt;
  450. int err, active_logs;
  451. /*
  452. * Save the old mount options in case we
  453. * need to restore them.
  454. */
  455. org_mount_opt = sbi->mount_opt;
  456. active_logs = sbi->active_logs;
  457. /* parse mount options */
  458. err = parse_options(sb, data);
  459. if (err)
  460. goto restore_opts;
  461. /*
  462. * Previous and new state of filesystem is RO,
  463. * so no point in checking GC conditions.
  464. */
  465. if ((sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
  466. goto skip;
  467. /*
  468. * We stop the GC thread if FS is mounted as RO
  469. * or if background_gc = off is passed in mount
  470. * option. Also sync the filesystem.
  471. */
  472. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  473. if (sbi->gc_thread) {
  474. stop_gc_thread(sbi);
  475. f2fs_sync_fs(sb, 1);
  476. }
  477. } else if (test_opt(sbi, BG_GC) && !sbi->gc_thread) {
  478. err = start_gc_thread(sbi);
  479. if (err)
  480. goto restore_opts;
  481. }
  482. skip:
  483. /* Update the POSIXACL Flag */
  484. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  485. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  486. return 0;
  487. restore_opts:
  488. sbi->mount_opt = org_mount_opt;
  489. sbi->active_logs = active_logs;
  490. return err;
  491. }
  492. static struct super_operations f2fs_sops = {
  493. .alloc_inode = f2fs_alloc_inode,
  494. .drop_inode = f2fs_drop_inode,
  495. .destroy_inode = f2fs_destroy_inode,
  496. .write_inode = f2fs_write_inode,
  497. .dirty_inode = f2fs_dirty_inode,
  498. .show_options = f2fs_show_options,
  499. .evict_inode = f2fs_evict_inode,
  500. .put_super = f2fs_put_super,
  501. .sync_fs = f2fs_sync_fs,
  502. .freeze_fs = f2fs_freeze,
  503. .unfreeze_fs = f2fs_unfreeze,
  504. .statfs = f2fs_statfs,
  505. .remount_fs = f2fs_remount,
  506. };
  507. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  508. u64 ino, u32 generation)
  509. {
  510. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  511. struct inode *inode;
  512. if (ino < F2FS_ROOT_INO(sbi))
  513. return ERR_PTR(-ESTALE);
  514. /*
  515. * f2fs_iget isn't quite right if the inode is currently unallocated!
  516. * However f2fs_iget currently does appropriate checks to handle stale
  517. * inodes so everything is OK.
  518. */
  519. inode = f2fs_iget(sb, ino);
  520. if (IS_ERR(inode))
  521. return ERR_CAST(inode);
  522. if (generation && inode->i_generation != generation) {
  523. /* we didn't find the right inode.. */
  524. iput(inode);
  525. return ERR_PTR(-ESTALE);
  526. }
  527. return inode;
  528. }
  529. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  530. int fh_len, int fh_type)
  531. {
  532. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  533. f2fs_nfs_get_inode);
  534. }
  535. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  536. int fh_len, int fh_type)
  537. {
  538. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  539. f2fs_nfs_get_inode);
  540. }
  541. static const struct export_operations f2fs_export_ops = {
  542. .fh_to_dentry = f2fs_fh_to_dentry,
  543. .fh_to_parent = f2fs_fh_to_parent,
  544. .get_parent = f2fs_get_parent,
  545. };
  546. static loff_t max_file_size(unsigned bits)
  547. {
  548. loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
  549. loff_t leaf_count = ADDRS_PER_BLOCK;
  550. /* two direct node blocks */
  551. result += (leaf_count * 2);
  552. /* two indirect node blocks */
  553. leaf_count *= NIDS_PER_BLOCK;
  554. result += (leaf_count * 2);
  555. /* one double indirect node block */
  556. leaf_count *= NIDS_PER_BLOCK;
  557. result += leaf_count;
  558. result <<= bits;
  559. return result;
  560. }
  561. static int sanity_check_raw_super(struct super_block *sb,
  562. struct f2fs_super_block *raw_super)
  563. {
  564. unsigned int blocksize;
  565. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  566. f2fs_msg(sb, KERN_INFO,
  567. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  568. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  569. return 1;
  570. }
  571. /* Currently, support only 4KB page cache size */
  572. if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
  573. f2fs_msg(sb, KERN_INFO,
  574. "Invalid page_cache_size (%lu), supports only 4KB\n",
  575. PAGE_CACHE_SIZE);
  576. return 1;
  577. }
  578. /* Currently, support only 4KB block size */
  579. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  580. if (blocksize != F2FS_BLKSIZE) {
  581. f2fs_msg(sb, KERN_INFO,
  582. "Invalid blocksize (%u), supports only 4KB\n",
  583. blocksize);
  584. return 1;
  585. }
  586. if (le32_to_cpu(raw_super->log_sectorsize) !=
  587. F2FS_LOG_SECTOR_SIZE) {
  588. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize");
  589. return 1;
  590. }
  591. if (le32_to_cpu(raw_super->log_sectors_per_block) !=
  592. F2FS_LOG_SECTORS_PER_BLOCK) {
  593. f2fs_msg(sb, KERN_INFO, "Invalid log sectors per block");
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  599. {
  600. unsigned int total, fsmeta;
  601. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  602. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  603. total = le32_to_cpu(raw_super->segment_count);
  604. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  605. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  606. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  607. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  608. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  609. if (fsmeta >= total)
  610. return 1;
  611. if (is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
  612. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  613. return 1;
  614. }
  615. return 0;
  616. }
  617. static void init_sb_info(struct f2fs_sb_info *sbi)
  618. {
  619. struct f2fs_super_block *raw_super = sbi->raw_super;
  620. int i;
  621. sbi->log_sectors_per_block =
  622. le32_to_cpu(raw_super->log_sectors_per_block);
  623. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  624. sbi->blocksize = 1 << sbi->log_blocksize;
  625. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  626. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  627. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  628. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  629. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  630. sbi->total_node_count =
  631. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  632. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  633. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  634. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  635. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  636. sbi->cur_victim_sec = NULL_SECNO;
  637. for (i = 0; i < NR_COUNT_TYPE; i++)
  638. atomic_set(&sbi->nr_pages[i], 0);
  639. }
  640. static int validate_superblock(struct super_block *sb,
  641. struct f2fs_super_block **raw_super,
  642. struct buffer_head **raw_super_buf, sector_t block)
  643. {
  644. const char *super = (block == 0 ? "first" : "second");
  645. /* read f2fs raw super block */
  646. *raw_super_buf = sb_bread(sb, block);
  647. if (!*raw_super_buf) {
  648. f2fs_msg(sb, KERN_ERR, "unable to read %s superblock",
  649. super);
  650. return -EIO;
  651. }
  652. *raw_super = (struct f2fs_super_block *)
  653. ((char *)(*raw_super_buf)->b_data + F2FS_SUPER_OFFSET);
  654. /* sanity checking of raw super */
  655. if (!sanity_check_raw_super(sb, *raw_super))
  656. return 0;
  657. f2fs_msg(sb, KERN_ERR, "Can't find a valid F2FS filesystem "
  658. "in %s superblock", super);
  659. return -EINVAL;
  660. }
  661. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  662. {
  663. struct f2fs_sb_info *sbi;
  664. struct f2fs_super_block *raw_super;
  665. struct buffer_head *raw_super_buf;
  666. struct inode *root;
  667. long err = -EINVAL;
  668. /* allocate memory for f2fs-specific super block info */
  669. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  670. if (!sbi)
  671. return -ENOMEM;
  672. /* set a block size */
  673. if (!sb_set_blocksize(sb, F2FS_BLKSIZE)) {
  674. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  675. goto free_sbi;
  676. }
  677. err = validate_superblock(sb, &raw_super, &raw_super_buf, 0);
  678. if (err) {
  679. brelse(raw_super_buf);
  680. /* check secondary superblock when primary failed */
  681. err = validate_superblock(sb, &raw_super, &raw_super_buf, 1);
  682. if (err)
  683. goto free_sb_buf;
  684. }
  685. sb->s_fs_info = sbi;
  686. /* init some FS parameters */
  687. sbi->active_logs = NR_CURSEG_TYPE;
  688. set_opt(sbi, BG_GC);
  689. #ifdef CONFIG_F2FS_FS_XATTR
  690. set_opt(sbi, XATTR_USER);
  691. #endif
  692. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  693. set_opt(sbi, POSIX_ACL);
  694. #endif
  695. /* parse mount options */
  696. err = parse_options(sb, (char *)data);
  697. if (err)
  698. goto free_sb_buf;
  699. sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
  700. sb->s_max_links = F2FS_LINK_MAX;
  701. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  702. sb->s_op = &f2fs_sops;
  703. sb->s_xattr = f2fs_xattr_handlers;
  704. sb->s_export_op = &f2fs_export_ops;
  705. sb->s_magic = F2FS_SUPER_MAGIC;
  706. sb->s_time_gran = 1;
  707. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  708. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  709. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  710. /* init f2fs-specific super block info */
  711. sbi->sb = sb;
  712. sbi->raw_super = raw_super;
  713. sbi->raw_super_buf = raw_super_buf;
  714. mutex_init(&sbi->gc_mutex);
  715. mutex_init(&sbi->writepages);
  716. mutex_init(&sbi->cp_mutex);
  717. mutex_init(&sbi->node_write);
  718. sbi->por_doing = 0;
  719. spin_lock_init(&sbi->stat_lock);
  720. init_rwsem(&sbi->bio_sem);
  721. init_rwsem(&sbi->cp_rwsem);
  722. init_waitqueue_head(&sbi->cp_wait);
  723. init_sb_info(sbi);
  724. /* get an inode for meta space */
  725. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  726. if (IS_ERR(sbi->meta_inode)) {
  727. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  728. err = PTR_ERR(sbi->meta_inode);
  729. goto free_sb_buf;
  730. }
  731. err = get_valid_checkpoint(sbi);
  732. if (err) {
  733. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  734. goto free_meta_inode;
  735. }
  736. /* sanity checking of checkpoint */
  737. err = -EINVAL;
  738. if (sanity_check_ckpt(sbi)) {
  739. f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
  740. goto free_cp;
  741. }
  742. sbi->total_valid_node_count =
  743. le32_to_cpu(sbi->ckpt->valid_node_count);
  744. sbi->total_valid_inode_count =
  745. le32_to_cpu(sbi->ckpt->valid_inode_count);
  746. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  747. sbi->total_valid_block_count =
  748. le64_to_cpu(sbi->ckpt->valid_block_count);
  749. sbi->last_valid_block_count = sbi->total_valid_block_count;
  750. sbi->alloc_valid_block_count = 0;
  751. INIT_LIST_HEAD(&sbi->dir_inode_list);
  752. spin_lock_init(&sbi->dir_inode_lock);
  753. init_orphan_info(sbi);
  754. /* setup f2fs internal modules */
  755. err = build_segment_manager(sbi);
  756. if (err) {
  757. f2fs_msg(sb, KERN_ERR,
  758. "Failed to initialize F2FS segment manager");
  759. goto free_sm;
  760. }
  761. err = build_node_manager(sbi);
  762. if (err) {
  763. f2fs_msg(sb, KERN_ERR,
  764. "Failed to initialize F2FS node manager");
  765. goto free_nm;
  766. }
  767. build_gc_manager(sbi);
  768. /* get an inode for node space */
  769. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  770. if (IS_ERR(sbi->node_inode)) {
  771. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  772. err = PTR_ERR(sbi->node_inode);
  773. goto free_nm;
  774. }
  775. /* if there are nt orphan nodes free them */
  776. err = -EINVAL;
  777. if (recover_orphan_inodes(sbi))
  778. goto free_node_inode;
  779. /* read root inode and dentry */
  780. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  781. if (IS_ERR(root)) {
  782. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  783. err = PTR_ERR(root);
  784. goto free_node_inode;
  785. }
  786. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size)
  787. goto free_root_inode;
  788. sb->s_root = d_make_root(root); /* allocate root dentry */
  789. if (!sb->s_root) {
  790. err = -ENOMEM;
  791. goto free_root_inode;
  792. }
  793. /* recover fsynced data */
  794. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  795. err = recover_fsync_data(sbi);
  796. if (err)
  797. f2fs_msg(sb, KERN_ERR,
  798. "Cannot recover all fsync data errno=%ld", err);
  799. }
  800. /*
  801. * If filesystem is not mounted as read-only then
  802. * do start the gc_thread.
  803. */
  804. if (!(sb->s_flags & MS_RDONLY)) {
  805. /* After POR, we can run background GC thread.*/
  806. err = start_gc_thread(sbi);
  807. if (err)
  808. goto fail;
  809. }
  810. err = f2fs_build_stats(sbi);
  811. if (err)
  812. goto fail;
  813. if (f2fs_proc_root)
  814. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  815. if (sbi->s_proc)
  816. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  817. &f2fs_seq_segment_info_fops, sb);
  818. if (test_opt(sbi, DISCARD)) {
  819. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  820. if (!blk_queue_discard(q))
  821. f2fs_msg(sb, KERN_WARNING,
  822. "mounting with \"discard\" option, but "
  823. "the device does not support discard");
  824. }
  825. sbi->s_kobj.kset = f2fs_kset;
  826. init_completion(&sbi->s_kobj_unregister);
  827. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  828. "%s", sb->s_id);
  829. if (err)
  830. goto fail;
  831. return 0;
  832. fail:
  833. stop_gc_thread(sbi);
  834. free_root_inode:
  835. dput(sb->s_root);
  836. sb->s_root = NULL;
  837. free_node_inode:
  838. iput(sbi->node_inode);
  839. free_nm:
  840. destroy_node_manager(sbi);
  841. free_sm:
  842. destroy_segment_manager(sbi);
  843. free_cp:
  844. kfree(sbi->ckpt);
  845. free_meta_inode:
  846. make_bad_inode(sbi->meta_inode);
  847. iput(sbi->meta_inode);
  848. free_sb_buf:
  849. brelse(raw_super_buf);
  850. free_sbi:
  851. kfree(sbi);
  852. return err;
  853. }
  854. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  855. const char *dev_name, void *data)
  856. {
  857. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  858. }
  859. static struct file_system_type f2fs_fs_type = {
  860. .owner = THIS_MODULE,
  861. .name = "f2fs",
  862. .mount = f2fs_mount,
  863. .kill_sb = kill_block_super,
  864. .fs_flags = FS_REQUIRES_DEV,
  865. };
  866. MODULE_ALIAS_FS("f2fs");
  867. static int __init init_inodecache(void)
  868. {
  869. f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
  870. sizeof(struct f2fs_inode_info), NULL);
  871. if (f2fs_inode_cachep == NULL)
  872. return -ENOMEM;
  873. return 0;
  874. }
  875. static void destroy_inodecache(void)
  876. {
  877. /*
  878. * Make sure all delayed rcu free inodes are flushed before we
  879. * destroy cache.
  880. */
  881. rcu_barrier();
  882. kmem_cache_destroy(f2fs_inode_cachep);
  883. }
  884. static int __init init_f2fs_fs(void)
  885. {
  886. int err;
  887. err = init_inodecache();
  888. if (err)
  889. goto fail;
  890. err = create_node_manager_caches();
  891. if (err)
  892. goto free_inodecache;
  893. err = create_gc_caches();
  894. if (err)
  895. goto free_node_manager_caches;
  896. err = create_checkpoint_caches();
  897. if (err)
  898. goto free_gc_caches;
  899. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  900. if (!f2fs_kset) {
  901. err = -ENOMEM;
  902. goto free_checkpoint_caches;
  903. }
  904. err = register_filesystem(&f2fs_fs_type);
  905. if (err)
  906. goto free_kset;
  907. f2fs_create_root_stats();
  908. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  909. return 0;
  910. free_kset:
  911. kset_unregister(f2fs_kset);
  912. free_checkpoint_caches:
  913. destroy_checkpoint_caches();
  914. free_gc_caches:
  915. destroy_gc_caches();
  916. free_node_manager_caches:
  917. destroy_node_manager_caches();
  918. free_inodecache:
  919. destroy_inodecache();
  920. fail:
  921. return err;
  922. }
  923. static void __exit exit_f2fs_fs(void)
  924. {
  925. remove_proc_entry("fs/f2fs", NULL);
  926. f2fs_destroy_root_stats();
  927. unregister_filesystem(&f2fs_fs_type);
  928. destroy_checkpoint_caches();
  929. destroy_gc_caches();
  930. destroy_node_manager_caches();
  931. destroy_inodecache();
  932. kset_unregister(f2fs_kset);
  933. }
  934. module_init(init_f2fs_fs)
  935. module_exit(exit_f2fs_fs)
  936. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  937. MODULE_DESCRIPTION("Flash Friendly File System");
  938. MODULE_LICENSE("GPL");