super.c 35 KB

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