super.c 26 KB

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