super.c 36 KB

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