super.c 32 KB

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