super.c 41 KB

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