super.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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, interval_time[CP_TIME]);
  197. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
  198. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  199. static struct attribute *f2fs_attrs[] = {
  200. ATTR_LIST(gc_min_sleep_time),
  201. ATTR_LIST(gc_max_sleep_time),
  202. ATTR_LIST(gc_no_gc_sleep_time),
  203. ATTR_LIST(gc_idle),
  204. ATTR_LIST(reclaim_segments),
  205. ATTR_LIST(max_small_discards),
  206. ATTR_LIST(batched_trim_sections),
  207. ATTR_LIST(ipu_policy),
  208. ATTR_LIST(min_ipu_util),
  209. ATTR_LIST(min_fsync_blocks),
  210. ATTR_LIST(max_victim_search),
  211. ATTR_LIST(dir_level),
  212. ATTR_LIST(ram_thresh),
  213. ATTR_LIST(ra_nid_pages),
  214. ATTR_LIST(cp_interval),
  215. ATTR_LIST(idle_interval),
  216. NULL,
  217. };
  218. static const struct sysfs_ops f2fs_attr_ops = {
  219. .show = f2fs_attr_show,
  220. .store = f2fs_attr_store,
  221. };
  222. static struct kobj_type f2fs_ktype = {
  223. .default_attrs = f2fs_attrs,
  224. .sysfs_ops = &f2fs_attr_ops,
  225. .release = f2fs_sb_release,
  226. };
  227. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  228. {
  229. struct va_format vaf;
  230. va_list args;
  231. va_start(args, fmt);
  232. vaf.fmt = fmt;
  233. vaf.va = &args;
  234. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  235. va_end(args);
  236. }
  237. static void init_once(void *foo)
  238. {
  239. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  240. inode_init_once(&fi->vfs_inode);
  241. }
  242. static int parse_options(struct super_block *sb, char *options)
  243. {
  244. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  245. struct request_queue *q;
  246. substring_t args[MAX_OPT_ARGS];
  247. char *p, *name;
  248. int arg = 0;
  249. if (!options)
  250. return 0;
  251. while ((p = strsep(&options, ",")) != NULL) {
  252. int token;
  253. if (!*p)
  254. continue;
  255. /*
  256. * Initialize args struct so we know whether arg was
  257. * found; some options take optional arguments.
  258. */
  259. args[0].to = args[0].from = NULL;
  260. token = match_token(p, f2fs_tokens, args);
  261. switch (token) {
  262. case Opt_gc_background:
  263. name = match_strdup(&args[0]);
  264. if (!name)
  265. return -ENOMEM;
  266. if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
  267. set_opt(sbi, BG_GC);
  268. clear_opt(sbi, FORCE_FG_GC);
  269. } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
  270. clear_opt(sbi, BG_GC);
  271. clear_opt(sbi, FORCE_FG_GC);
  272. } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
  273. set_opt(sbi, BG_GC);
  274. set_opt(sbi, FORCE_FG_GC);
  275. } else {
  276. kfree(name);
  277. return -EINVAL;
  278. }
  279. kfree(name);
  280. break;
  281. case Opt_disable_roll_forward:
  282. set_opt(sbi, DISABLE_ROLL_FORWARD);
  283. break;
  284. case Opt_norecovery:
  285. /* this option mounts f2fs with ro */
  286. set_opt(sbi, DISABLE_ROLL_FORWARD);
  287. if (!f2fs_readonly(sb))
  288. return -EINVAL;
  289. break;
  290. case Opt_discard:
  291. q = bdev_get_queue(sb->s_bdev);
  292. if (blk_queue_discard(q)) {
  293. set_opt(sbi, DISCARD);
  294. } else {
  295. f2fs_msg(sb, KERN_WARNING,
  296. "mounting with \"discard\" option, but "
  297. "the device does not support discard");
  298. }
  299. break;
  300. case Opt_noheap:
  301. set_opt(sbi, NOHEAP);
  302. break;
  303. #ifdef CONFIG_F2FS_FS_XATTR
  304. case Opt_user_xattr:
  305. set_opt(sbi, XATTR_USER);
  306. break;
  307. case Opt_nouser_xattr:
  308. clear_opt(sbi, XATTR_USER);
  309. break;
  310. case Opt_inline_xattr:
  311. set_opt(sbi, INLINE_XATTR);
  312. break;
  313. #else
  314. case Opt_user_xattr:
  315. f2fs_msg(sb, KERN_INFO,
  316. "user_xattr options not supported");
  317. break;
  318. case Opt_nouser_xattr:
  319. f2fs_msg(sb, KERN_INFO,
  320. "nouser_xattr options not supported");
  321. break;
  322. case Opt_inline_xattr:
  323. f2fs_msg(sb, KERN_INFO,
  324. "inline_xattr options not supported");
  325. break;
  326. #endif
  327. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  328. case Opt_acl:
  329. set_opt(sbi, POSIX_ACL);
  330. break;
  331. case Opt_noacl:
  332. clear_opt(sbi, POSIX_ACL);
  333. break;
  334. #else
  335. case Opt_acl:
  336. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  337. break;
  338. case Opt_noacl:
  339. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  340. break;
  341. #endif
  342. case Opt_active_logs:
  343. if (args->from && match_int(args, &arg))
  344. return -EINVAL;
  345. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  346. return -EINVAL;
  347. sbi->active_logs = arg;
  348. break;
  349. case Opt_disable_ext_identify:
  350. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  351. break;
  352. case Opt_inline_data:
  353. set_opt(sbi, INLINE_DATA);
  354. break;
  355. case Opt_inline_dentry:
  356. set_opt(sbi, INLINE_DENTRY);
  357. break;
  358. case Opt_flush_merge:
  359. set_opt(sbi, FLUSH_MERGE);
  360. break;
  361. case Opt_nobarrier:
  362. set_opt(sbi, NOBARRIER);
  363. break;
  364. case Opt_fastboot:
  365. set_opt(sbi, FASTBOOT);
  366. break;
  367. case Opt_extent_cache:
  368. set_opt(sbi, EXTENT_CACHE);
  369. break;
  370. case Opt_noextent_cache:
  371. clear_opt(sbi, EXTENT_CACHE);
  372. break;
  373. case Opt_noinline_data:
  374. clear_opt(sbi, INLINE_DATA);
  375. break;
  376. case Opt_data_flush:
  377. set_opt(sbi, DATA_FLUSH);
  378. break;
  379. default:
  380. f2fs_msg(sb, KERN_ERR,
  381. "Unrecognized mount option \"%s\" or missing value",
  382. p);
  383. return -EINVAL;
  384. }
  385. }
  386. return 0;
  387. }
  388. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  389. {
  390. struct f2fs_inode_info *fi;
  391. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  392. if (!fi)
  393. return NULL;
  394. init_once((void *) fi);
  395. /* Initialize f2fs-specific inode info */
  396. fi->vfs_inode.i_version = 1;
  397. atomic_set(&fi->dirty_pages, 0);
  398. fi->i_current_depth = 1;
  399. fi->i_advise = 0;
  400. init_rwsem(&fi->i_sem);
  401. INIT_LIST_HEAD(&fi->dirty_list);
  402. INIT_LIST_HEAD(&fi->inmem_pages);
  403. mutex_init(&fi->inmem_lock);
  404. set_inode_flag(fi, FI_NEW_INODE);
  405. if (test_opt(F2FS_SB(sb), INLINE_XATTR))
  406. set_inode_flag(fi, FI_INLINE_XATTR);
  407. /* Will be used by directory only */
  408. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  409. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  410. fi->i_crypt_info = NULL;
  411. #endif
  412. return &fi->vfs_inode;
  413. }
  414. static int f2fs_drop_inode(struct inode *inode)
  415. {
  416. /*
  417. * This is to avoid a deadlock condition like below.
  418. * writeback_single_inode(inode)
  419. * - f2fs_write_data_page
  420. * - f2fs_gc -> iput -> evict
  421. * - inode_wait_for_writeback(inode)
  422. */
  423. if (!inode_unhashed(inode) && inode->i_state & I_SYNC) {
  424. if (!inode->i_nlink && !is_bad_inode(inode)) {
  425. /* to avoid evict_inode call simultaneously */
  426. atomic_inc(&inode->i_count);
  427. spin_unlock(&inode->i_lock);
  428. /* some remained atomic pages should discarded */
  429. if (f2fs_is_atomic_file(inode))
  430. commit_inmem_pages(inode, true);
  431. /* should remain fi->extent_tree for writepage */
  432. f2fs_destroy_extent_node(inode);
  433. sb_start_intwrite(inode->i_sb);
  434. i_size_write(inode, 0);
  435. if (F2FS_HAS_BLOCKS(inode))
  436. f2fs_truncate(inode, true);
  437. sb_end_intwrite(inode->i_sb);
  438. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  439. if (F2FS_I(inode)->i_crypt_info)
  440. f2fs_free_encryption_info(inode,
  441. F2FS_I(inode)->i_crypt_info);
  442. #endif
  443. spin_lock(&inode->i_lock);
  444. atomic_dec(&inode->i_count);
  445. }
  446. return 0;
  447. }
  448. return generic_drop_inode(inode);
  449. }
  450. /*
  451. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  452. *
  453. * We should call set_dirty_inode to write the dirty inode through write_inode.
  454. */
  455. static void f2fs_dirty_inode(struct inode *inode, int flags)
  456. {
  457. set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
  458. }
  459. static void f2fs_i_callback(struct rcu_head *head)
  460. {
  461. struct inode *inode = container_of(head, struct inode, i_rcu);
  462. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  463. }
  464. static void f2fs_destroy_inode(struct inode *inode)
  465. {
  466. call_rcu(&inode->i_rcu, f2fs_i_callback);
  467. }
  468. static void f2fs_put_super(struct super_block *sb)
  469. {
  470. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  471. if (sbi->s_proc) {
  472. remove_proc_entry("segment_info", sbi->s_proc);
  473. remove_proc_entry(sb->s_id, f2fs_proc_root);
  474. }
  475. kobject_del(&sbi->s_kobj);
  476. stop_gc_thread(sbi);
  477. /* prevent remaining shrinker jobs */
  478. mutex_lock(&sbi->umount_mutex);
  479. /*
  480. * We don't need to do checkpoint when superblock is clean.
  481. * But, the previous checkpoint was not done by umount, it needs to do
  482. * clean checkpoint again.
  483. */
  484. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  485. !is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
  486. struct cp_control cpc = {
  487. .reason = CP_UMOUNT,
  488. };
  489. write_checkpoint(sbi, &cpc);
  490. }
  491. /* write_checkpoint can update stat informaion */
  492. f2fs_destroy_stats(sbi);
  493. /*
  494. * normally superblock is clean, so we need to release this.
  495. * In addition, EIO will skip do checkpoint, we need this as well.
  496. */
  497. release_ino_entry(sbi);
  498. release_discard_addrs(sbi);
  499. f2fs_leave_shrinker(sbi);
  500. mutex_unlock(&sbi->umount_mutex);
  501. iput(sbi->node_inode);
  502. iput(sbi->meta_inode);
  503. /* destroy f2fs internal modules */
  504. destroy_node_manager(sbi);
  505. destroy_segment_manager(sbi);
  506. kfree(sbi->ckpt);
  507. kobject_put(&sbi->s_kobj);
  508. wait_for_completion(&sbi->s_kobj_unregister);
  509. sb->s_fs_info = NULL;
  510. kfree(sbi->raw_super);
  511. kfree(sbi);
  512. }
  513. int f2fs_sync_fs(struct super_block *sb, int sync)
  514. {
  515. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  516. int err = 0;
  517. trace_f2fs_sync_fs(sb, sync);
  518. if (sync) {
  519. struct cp_control cpc;
  520. cpc.reason = __get_cp_reason(sbi);
  521. mutex_lock(&sbi->gc_mutex);
  522. err = write_checkpoint(sbi, &cpc);
  523. mutex_unlock(&sbi->gc_mutex);
  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. static loff_t max_file_blocks(void)
  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. return result;
  810. }
  811. static inline bool sanity_check_area_boundary(struct super_block *sb,
  812. struct f2fs_super_block *raw_super)
  813. {
  814. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  815. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  816. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  817. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  818. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  819. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  820. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  821. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  822. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  823. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  824. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  825. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  826. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  827. if (segment0_blkaddr != cp_blkaddr) {
  828. f2fs_msg(sb, KERN_INFO,
  829. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  830. segment0_blkaddr, cp_blkaddr);
  831. return true;
  832. }
  833. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  834. sit_blkaddr) {
  835. f2fs_msg(sb, KERN_INFO,
  836. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  837. cp_blkaddr, sit_blkaddr,
  838. segment_count_ckpt << log_blocks_per_seg);
  839. return true;
  840. }
  841. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  842. nat_blkaddr) {
  843. f2fs_msg(sb, KERN_INFO,
  844. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  845. sit_blkaddr, nat_blkaddr,
  846. segment_count_sit << log_blocks_per_seg);
  847. return true;
  848. }
  849. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  850. ssa_blkaddr) {
  851. f2fs_msg(sb, KERN_INFO,
  852. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  853. nat_blkaddr, ssa_blkaddr,
  854. segment_count_nat << log_blocks_per_seg);
  855. return true;
  856. }
  857. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  858. main_blkaddr) {
  859. f2fs_msg(sb, KERN_INFO,
  860. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  861. ssa_blkaddr, main_blkaddr,
  862. segment_count_ssa << log_blocks_per_seg);
  863. return true;
  864. }
  865. if (main_blkaddr + (segment_count_main << log_blocks_per_seg) !=
  866. segment0_blkaddr + (segment_count << log_blocks_per_seg)) {
  867. f2fs_msg(sb, KERN_INFO,
  868. "Wrong MAIN_AREA boundary, start(%u) end(%u) blocks(%u)",
  869. main_blkaddr,
  870. segment0_blkaddr + (segment_count << log_blocks_per_seg),
  871. segment_count_main << log_blocks_per_seg);
  872. return true;
  873. }
  874. return false;
  875. }
  876. static int sanity_check_raw_super(struct super_block *sb,
  877. struct f2fs_super_block *raw_super)
  878. {
  879. unsigned int blocksize;
  880. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  881. f2fs_msg(sb, KERN_INFO,
  882. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  883. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  884. return 1;
  885. }
  886. /* Currently, support only 4KB page cache size */
  887. if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
  888. f2fs_msg(sb, KERN_INFO,
  889. "Invalid page_cache_size (%lu), supports only 4KB\n",
  890. PAGE_CACHE_SIZE);
  891. return 1;
  892. }
  893. /* Currently, support only 4KB block size */
  894. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  895. if (blocksize != F2FS_BLKSIZE) {
  896. f2fs_msg(sb, KERN_INFO,
  897. "Invalid blocksize (%u), supports only 4KB\n",
  898. blocksize);
  899. return 1;
  900. }
  901. /* check log blocks per segment */
  902. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  903. f2fs_msg(sb, KERN_INFO,
  904. "Invalid log blocks per segment (%u)\n",
  905. le32_to_cpu(raw_super->log_blocks_per_seg));
  906. return 1;
  907. }
  908. /* Currently, support 512/1024/2048/4096 bytes sector size */
  909. if (le32_to_cpu(raw_super->log_sectorsize) >
  910. F2FS_MAX_LOG_SECTOR_SIZE ||
  911. le32_to_cpu(raw_super->log_sectorsize) <
  912. F2FS_MIN_LOG_SECTOR_SIZE) {
  913. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  914. le32_to_cpu(raw_super->log_sectorsize));
  915. return 1;
  916. }
  917. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  918. le32_to_cpu(raw_super->log_sectorsize) !=
  919. F2FS_MAX_LOG_SECTOR_SIZE) {
  920. f2fs_msg(sb, KERN_INFO,
  921. "Invalid log sectors per block(%u) log sectorsize(%u)",
  922. le32_to_cpu(raw_super->log_sectors_per_block),
  923. le32_to_cpu(raw_super->log_sectorsize));
  924. return 1;
  925. }
  926. /* check reserved ino info */
  927. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  928. le32_to_cpu(raw_super->meta_ino) != 2 ||
  929. le32_to_cpu(raw_super->root_ino) != 3) {
  930. f2fs_msg(sb, KERN_INFO,
  931. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  932. le32_to_cpu(raw_super->node_ino),
  933. le32_to_cpu(raw_super->meta_ino),
  934. le32_to_cpu(raw_super->root_ino));
  935. return 1;
  936. }
  937. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  938. if (sanity_check_area_boundary(sb, raw_super))
  939. return 1;
  940. return 0;
  941. }
  942. static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  943. {
  944. unsigned int total, fsmeta;
  945. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  946. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  947. total = le32_to_cpu(raw_super->segment_count);
  948. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  949. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  950. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  951. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  952. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  953. if (unlikely(fsmeta >= total))
  954. return 1;
  955. if (unlikely(f2fs_cp_error(sbi))) {
  956. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  957. return 1;
  958. }
  959. return 0;
  960. }
  961. static void init_sb_info(struct f2fs_sb_info *sbi)
  962. {
  963. struct f2fs_super_block *raw_super = sbi->raw_super;
  964. int i;
  965. sbi->log_sectors_per_block =
  966. le32_to_cpu(raw_super->log_sectors_per_block);
  967. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  968. sbi->blocksize = 1 << sbi->log_blocksize;
  969. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  970. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  971. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  972. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  973. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  974. sbi->total_node_count =
  975. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  976. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  977. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  978. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  979. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  980. sbi->cur_victim_sec = NULL_SECNO;
  981. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  982. for (i = 0; i < NR_COUNT_TYPE; i++)
  983. atomic_set(&sbi->nr_pages[i], 0);
  984. sbi->dir_level = DEF_DIR_LEVEL;
  985. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  986. sbi->interval_time[REQ_TIME] = DEF_IDLE_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. sbi->max_file_blocks = max_file_blocks();
  1114. sb->s_maxbytes = sbi->max_file_blocks <<
  1115. le32_to_cpu(raw_super->log_blocksize);
  1116. sb->s_max_links = F2FS_LINK_MAX;
  1117. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1118. sb->s_op = &f2fs_sops;
  1119. sb->s_xattr = f2fs_xattr_handlers;
  1120. sb->s_export_op = &f2fs_export_ops;
  1121. sb->s_magic = F2FS_SUPER_MAGIC;
  1122. sb->s_time_gran = 1;
  1123. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1124. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  1125. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  1126. /* init f2fs-specific super block info */
  1127. sbi->sb = sb;
  1128. sbi->raw_super = raw_super;
  1129. sbi->valid_super_block = valid_super_block;
  1130. mutex_init(&sbi->gc_mutex);
  1131. mutex_init(&sbi->writepages);
  1132. mutex_init(&sbi->cp_mutex);
  1133. init_rwsem(&sbi->node_write);
  1134. /* disallow all the data/node/meta page writes */
  1135. set_sbi_flag(sbi, SBI_POR_DOING);
  1136. spin_lock_init(&sbi->stat_lock);
  1137. init_rwsem(&sbi->read_io.io_rwsem);
  1138. sbi->read_io.sbi = sbi;
  1139. sbi->read_io.bio = NULL;
  1140. for (i = 0; i < NR_PAGE_TYPE; i++) {
  1141. init_rwsem(&sbi->write_io[i].io_rwsem);
  1142. sbi->write_io[i].sbi = sbi;
  1143. sbi->write_io[i].bio = NULL;
  1144. }
  1145. init_rwsem(&sbi->cp_rwsem);
  1146. init_waitqueue_head(&sbi->cp_wait);
  1147. init_sb_info(sbi);
  1148. /* get an inode for meta space */
  1149. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  1150. if (IS_ERR(sbi->meta_inode)) {
  1151. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  1152. err = PTR_ERR(sbi->meta_inode);
  1153. goto free_options;
  1154. }
  1155. err = get_valid_checkpoint(sbi);
  1156. if (err) {
  1157. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  1158. goto free_meta_inode;
  1159. }
  1160. /* sanity checking of checkpoint */
  1161. err = -EINVAL;
  1162. if (sanity_check_ckpt(sbi)) {
  1163. f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
  1164. goto free_cp;
  1165. }
  1166. sbi->total_valid_node_count =
  1167. le32_to_cpu(sbi->ckpt->valid_node_count);
  1168. sbi->total_valid_inode_count =
  1169. le32_to_cpu(sbi->ckpt->valid_inode_count);
  1170. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  1171. sbi->total_valid_block_count =
  1172. le64_to_cpu(sbi->ckpt->valid_block_count);
  1173. sbi->last_valid_block_count = sbi->total_valid_block_count;
  1174. sbi->alloc_valid_block_count = 0;
  1175. for (i = 0; i < NR_INODE_TYPE; i++) {
  1176. INIT_LIST_HEAD(&sbi->inode_list[i]);
  1177. spin_lock_init(&sbi->inode_lock[i]);
  1178. }
  1179. init_extent_cache_info(sbi);
  1180. init_ino_entry_info(sbi);
  1181. /* setup f2fs internal modules */
  1182. err = build_segment_manager(sbi);
  1183. if (err) {
  1184. f2fs_msg(sb, KERN_ERR,
  1185. "Failed to initialize F2FS segment manager");
  1186. goto free_sm;
  1187. }
  1188. err = build_node_manager(sbi);
  1189. if (err) {
  1190. f2fs_msg(sb, KERN_ERR,
  1191. "Failed to initialize F2FS node manager");
  1192. goto free_nm;
  1193. }
  1194. build_gc_manager(sbi);
  1195. /* get an inode for node space */
  1196. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  1197. if (IS_ERR(sbi->node_inode)) {
  1198. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  1199. err = PTR_ERR(sbi->node_inode);
  1200. goto free_nm;
  1201. }
  1202. f2fs_join_shrinker(sbi);
  1203. /* if there are nt orphan nodes free them */
  1204. err = recover_orphan_inodes(sbi);
  1205. if (err)
  1206. goto free_node_inode;
  1207. /* read root inode and dentry */
  1208. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  1209. if (IS_ERR(root)) {
  1210. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  1211. err = PTR_ERR(root);
  1212. goto free_node_inode;
  1213. }
  1214. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1215. iput(root);
  1216. err = -EINVAL;
  1217. goto free_node_inode;
  1218. }
  1219. sb->s_root = d_make_root(root); /* allocate root dentry */
  1220. if (!sb->s_root) {
  1221. err = -ENOMEM;
  1222. goto free_root_inode;
  1223. }
  1224. err = f2fs_build_stats(sbi);
  1225. if (err)
  1226. goto free_root_inode;
  1227. if (f2fs_proc_root)
  1228. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  1229. if (sbi->s_proc)
  1230. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  1231. &f2fs_seq_segment_info_fops, sb);
  1232. sbi->s_kobj.kset = f2fs_kset;
  1233. init_completion(&sbi->s_kobj_unregister);
  1234. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  1235. "%s", sb->s_id);
  1236. if (err)
  1237. goto free_proc;
  1238. /* recover fsynced data */
  1239. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  1240. /*
  1241. * mount should be failed, when device has readonly mode, and
  1242. * previous checkpoint was not done by clean system shutdown.
  1243. */
  1244. if (bdev_read_only(sb->s_bdev) &&
  1245. !is_set_ckpt_flags(sbi->ckpt, CP_UMOUNT_FLAG)) {
  1246. err = -EROFS;
  1247. goto free_kobj;
  1248. }
  1249. if (need_fsck)
  1250. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1251. err = recover_fsync_data(sbi);
  1252. if (err) {
  1253. need_fsck = true;
  1254. f2fs_msg(sb, KERN_ERR,
  1255. "Cannot recover all fsync data errno=%ld", err);
  1256. goto free_kobj;
  1257. }
  1258. }
  1259. /* recover_fsync_data() cleared this already */
  1260. clear_sbi_flag(sbi, SBI_POR_DOING);
  1261. /*
  1262. * If filesystem is not mounted as read-only then
  1263. * do start the gc_thread.
  1264. */
  1265. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  1266. /* After POR, we can run background GC thread.*/
  1267. err = start_gc_thread(sbi);
  1268. if (err)
  1269. goto free_kobj;
  1270. }
  1271. kfree(options);
  1272. /* recover broken superblock */
  1273. if (recovery && !f2fs_readonly(sb) && !bdev_read_only(sb->s_bdev)) {
  1274. f2fs_msg(sb, KERN_INFO, "Recover invalid superblock");
  1275. f2fs_commit_super(sbi, true);
  1276. }
  1277. f2fs_update_time(sbi, CP_TIME);
  1278. f2fs_update_time(sbi, REQ_TIME);
  1279. return 0;
  1280. free_kobj:
  1281. kobject_del(&sbi->s_kobj);
  1282. kobject_put(&sbi->s_kobj);
  1283. wait_for_completion(&sbi->s_kobj_unregister);
  1284. free_proc:
  1285. if (sbi->s_proc) {
  1286. remove_proc_entry("segment_info", sbi->s_proc);
  1287. remove_proc_entry(sb->s_id, f2fs_proc_root);
  1288. }
  1289. f2fs_destroy_stats(sbi);
  1290. free_root_inode:
  1291. dput(sb->s_root);
  1292. sb->s_root = NULL;
  1293. free_node_inode:
  1294. mutex_lock(&sbi->umount_mutex);
  1295. f2fs_leave_shrinker(sbi);
  1296. iput(sbi->node_inode);
  1297. mutex_unlock(&sbi->umount_mutex);
  1298. free_nm:
  1299. destroy_node_manager(sbi);
  1300. free_sm:
  1301. destroy_segment_manager(sbi);
  1302. free_cp:
  1303. kfree(sbi->ckpt);
  1304. free_meta_inode:
  1305. make_bad_inode(sbi->meta_inode);
  1306. iput(sbi->meta_inode);
  1307. free_options:
  1308. kfree(options);
  1309. free_sb_buf:
  1310. kfree(raw_super);
  1311. free_sbi:
  1312. kfree(sbi);
  1313. /* give only one another chance */
  1314. if (retry) {
  1315. retry = false;
  1316. shrink_dcache_sb(sb);
  1317. goto try_onemore;
  1318. }
  1319. return err;
  1320. }
  1321. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  1322. const char *dev_name, void *data)
  1323. {
  1324. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  1325. }
  1326. static void kill_f2fs_super(struct super_block *sb)
  1327. {
  1328. if (sb->s_root)
  1329. set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
  1330. kill_block_super(sb);
  1331. }
  1332. static struct file_system_type f2fs_fs_type = {
  1333. .owner = THIS_MODULE,
  1334. .name = "f2fs",
  1335. .mount = f2fs_mount,
  1336. .kill_sb = kill_f2fs_super,
  1337. .fs_flags = FS_REQUIRES_DEV,
  1338. };
  1339. MODULE_ALIAS_FS("f2fs");
  1340. static int __init init_inodecache(void)
  1341. {
  1342. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  1343. sizeof(struct f2fs_inode_info), 0,
  1344. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  1345. if (!f2fs_inode_cachep)
  1346. return -ENOMEM;
  1347. return 0;
  1348. }
  1349. static void destroy_inodecache(void)
  1350. {
  1351. /*
  1352. * Make sure all delayed rcu free inodes are flushed before we
  1353. * destroy cache.
  1354. */
  1355. rcu_barrier();
  1356. kmem_cache_destroy(f2fs_inode_cachep);
  1357. }
  1358. static int __init init_f2fs_fs(void)
  1359. {
  1360. int err;
  1361. f2fs_build_trace_ios();
  1362. err = init_inodecache();
  1363. if (err)
  1364. goto fail;
  1365. err = create_node_manager_caches();
  1366. if (err)
  1367. goto free_inodecache;
  1368. err = create_segment_manager_caches();
  1369. if (err)
  1370. goto free_node_manager_caches;
  1371. err = create_checkpoint_caches();
  1372. if (err)
  1373. goto free_segment_manager_caches;
  1374. err = create_extent_cache();
  1375. if (err)
  1376. goto free_checkpoint_caches;
  1377. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  1378. if (!f2fs_kset) {
  1379. err = -ENOMEM;
  1380. goto free_extent_cache;
  1381. }
  1382. err = f2fs_init_crypto();
  1383. if (err)
  1384. goto free_kset;
  1385. err = register_shrinker(&f2fs_shrinker_info);
  1386. if (err)
  1387. goto free_crypto;
  1388. err = register_filesystem(&f2fs_fs_type);
  1389. if (err)
  1390. goto free_shrinker;
  1391. err = f2fs_create_root_stats();
  1392. if (err)
  1393. goto free_filesystem;
  1394. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  1395. return 0;
  1396. free_filesystem:
  1397. unregister_filesystem(&f2fs_fs_type);
  1398. free_shrinker:
  1399. unregister_shrinker(&f2fs_shrinker_info);
  1400. free_crypto:
  1401. f2fs_exit_crypto();
  1402. free_kset:
  1403. kset_unregister(f2fs_kset);
  1404. free_extent_cache:
  1405. destroy_extent_cache();
  1406. free_checkpoint_caches:
  1407. destroy_checkpoint_caches();
  1408. free_segment_manager_caches:
  1409. destroy_segment_manager_caches();
  1410. free_node_manager_caches:
  1411. destroy_node_manager_caches();
  1412. free_inodecache:
  1413. destroy_inodecache();
  1414. fail:
  1415. return err;
  1416. }
  1417. static void __exit exit_f2fs_fs(void)
  1418. {
  1419. remove_proc_entry("fs/f2fs", NULL);
  1420. f2fs_destroy_root_stats();
  1421. unregister_shrinker(&f2fs_shrinker_info);
  1422. unregister_filesystem(&f2fs_fs_type);
  1423. f2fs_exit_crypto();
  1424. destroy_extent_cache();
  1425. destroy_checkpoint_caches();
  1426. destroy_segment_manager_caches();
  1427. destroy_node_manager_caches();
  1428. destroy_inodecache();
  1429. kset_unregister(f2fs_kset);
  1430. f2fs_destroy_trace_ios();
  1431. }
  1432. module_init(init_f2fs_fs)
  1433. module_exit(exit_f2fs_fs)
  1434. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  1435. MODULE_DESCRIPTION("Flash Friendly File System");
  1436. MODULE_LICENSE("GPL");