super.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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. #ifdef CONFIG_F2FS_FAULT_INJECTION
  39. char *fault_name[FAULT_MAX] = {
  40. [FAULT_KMALLOC] = "kmalloc",
  41. [FAULT_PAGE_ALLOC] = "page alloc",
  42. [FAULT_ALLOC_NID] = "alloc nid",
  43. [FAULT_ORPHAN] = "orphan",
  44. [FAULT_BLOCK] = "no more block",
  45. [FAULT_DIR_DEPTH] = "too big dir depth",
  46. [FAULT_EVICT_INODE] = "evict_inode fail",
  47. [FAULT_IO] = "IO error",
  48. [FAULT_CHECKPOINT] = "checkpoint error",
  49. };
  50. static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
  51. unsigned int rate)
  52. {
  53. struct f2fs_fault_info *ffi = &sbi->fault_info;
  54. if (rate) {
  55. atomic_set(&ffi->inject_ops, 0);
  56. ffi->inject_rate = rate;
  57. ffi->inject_type = (1 << FAULT_MAX) - 1;
  58. } else {
  59. memset(ffi, 0, sizeof(struct f2fs_fault_info));
  60. }
  61. }
  62. #endif
  63. /* f2fs-wide shrinker description */
  64. static struct shrinker f2fs_shrinker_info = {
  65. .scan_objects = f2fs_shrink_scan,
  66. .count_objects = f2fs_shrink_count,
  67. .seeks = DEFAULT_SEEKS,
  68. };
  69. enum {
  70. Opt_gc_background,
  71. Opt_disable_roll_forward,
  72. Opt_norecovery,
  73. Opt_discard,
  74. Opt_nodiscard,
  75. Opt_noheap,
  76. Opt_user_xattr,
  77. Opt_nouser_xattr,
  78. Opt_acl,
  79. Opt_noacl,
  80. Opt_active_logs,
  81. Opt_disable_ext_identify,
  82. Opt_inline_xattr,
  83. Opt_inline_data,
  84. Opt_inline_dentry,
  85. Opt_noinline_dentry,
  86. Opt_flush_merge,
  87. Opt_noflush_merge,
  88. Opt_nobarrier,
  89. Opt_fastboot,
  90. Opt_extent_cache,
  91. Opt_noextent_cache,
  92. Opt_noinline_data,
  93. Opt_data_flush,
  94. Opt_mode,
  95. Opt_fault_injection,
  96. Opt_lazytime,
  97. Opt_nolazytime,
  98. Opt_err,
  99. };
  100. static match_table_t f2fs_tokens = {
  101. {Opt_gc_background, "background_gc=%s"},
  102. {Opt_disable_roll_forward, "disable_roll_forward"},
  103. {Opt_norecovery, "norecovery"},
  104. {Opt_discard, "discard"},
  105. {Opt_nodiscard, "nodiscard"},
  106. {Opt_noheap, "no_heap"},
  107. {Opt_user_xattr, "user_xattr"},
  108. {Opt_nouser_xattr, "nouser_xattr"},
  109. {Opt_acl, "acl"},
  110. {Opt_noacl, "noacl"},
  111. {Opt_active_logs, "active_logs=%u"},
  112. {Opt_disable_ext_identify, "disable_ext_identify"},
  113. {Opt_inline_xattr, "inline_xattr"},
  114. {Opt_inline_data, "inline_data"},
  115. {Opt_inline_dentry, "inline_dentry"},
  116. {Opt_noinline_dentry, "noinline_dentry"},
  117. {Opt_flush_merge, "flush_merge"},
  118. {Opt_noflush_merge, "noflush_merge"},
  119. {Opt_nobarrier, "nobarrier"},
  120. {Opt_fastboot, "fastboot"},
  121. {Opt_extent_cache, "extent_cache"},
  122. {Opt_noextent_cache, "noextent_cache"},
  123. {Opt_noinline_data, "noinline_data"},
  124. {Opt_data_flush, "data_flush"},
  125. {Opt_mode, "mode=%s"},
  126. {Opt_fault_injection, "fault_injection=%u"},
  127. {Opt_lazytime, "lazytime"},
  128. {Opt_nolazytime, "nolazytime"},
  129. {Opt_err, NULL},
  130. };
  131. /* Sysfs support for f2fs */
  132. enum {
  133. GC_THREAD, /* struct f2fs_gc_thread */
  134. SM_INFO, /* struct f2fs_sm_info */
  135. NM_INFO, /* struct f2fs_nm_info */
  136. F2FS_SBI, /* struct f2fs_sb_info */
  137. #ifdef CONFIG_F2FS_FAULT_INJECTION
  138. FAULT_INFO_RATE, /* struct f2fs_fault_info */
  139. FAULT_INFO_TYPE, /* struct f2fs_fault_info */
  140. #endif
  141. };
  142. struct f2fs_attr {
  143. struct attribute attr;
  144. ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
  145. ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
  146. const char *, size_t);
  147. int struct_type;
  148. int offset;
  149. };
  150. static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  151. {
  152. if (struct_type == GC_THREAD)
  153. return (unsigned char *)sbi->gc_thread;
  154. else if (struct_type == SM_INFO)
  155. return (unsigned char *)SM_I(sbi);
  156. else if (struct_type == NM_INFO)
  157. return (unsigned char *)NM_I(sbi);
  158. else if (struct_type == F2FS_SBI)
  159. return (unsigned char *)sbi;
  160. #ifdef CONFIG_F2FS_FAULT_INJECTION
  161. else if (struct_type == FAULT_INFO_RATE ||
  162. struct_type == FAULT_INFO_TYPE)
  163. return (unsigned char *)&sbi->fault_info;
  164. #endif
  165. return NULL;
  166. }
  167. static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
  168. struct f2fs_sb_info *sbi, char *buf)
  169. {
  170. struct super_block *sb = sbi->sb;
  171. if (!sb->s_bdev->bd_part)
  172. return snprintf(buf, PAGE_SIZE, "0\n");
  173. return snprintf(buf, PAGE_SIZE, "%llu\n",
  174. (unsigned long long)(sbi->kbytes_written +
  175. BD_PART_WRITTEN(sbi)));
  176. }
  177. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  178. struct f2fs_sb_info *sbi, char *buf)
  179. {
  180. unsigned char *ptr = NULL;
  181. unsigned int *ui;
  182. ptr = __struct_ptr(sbi, a->struct_type);
  183. if (!ptr)
  184. return -EINVAL;
  185. ui = (unsigned int *)(ptr + a->offset);
  186. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  187. }
  188. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  189. struct f2fs_sb_info *sbi,
  190. const char *buf, size_t count)
  191. {
  192. unsigned char *ptr;
  193. unsigned long t;
  194. unsigned int *ui;
  195. ssize_t ret;
  196. ptr = __struct_ptr(sbi, a->struct_type);
  197. if (!ptr)
  198. return -EINVAL;
  199. ui = (unsigned int *)(ptr + a->offset);
  200. ret = kstrtoul(skip_spaces(buf), 0, &t);
  201. if (ret < 0)
  202. return ret;
  203. #ifdef CONFIG_F2FS_FAULT_INJECTION
  204. if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
  205. return -EINVAL;
  206. #endif
  207. *ui = t;
  208. return count;
  209. }
  210. static ssize_t f2fs_attr_show(struct kobject *kobj,
  211. struct attribute *attr, char *buf)
  212. {
  213. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  214. s_kobj);
  215. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  216. return a->show ? a->show(a, sbi, buf) : 0;
  217. }
  218. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  219. const char *buf, size_t len)
  220. {
  221. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  222. s_kobj);
  223. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  224. return a->store ? a->store(a, sbi, buf, len) : 0;
  225. }
  226. static void f2fs_sb_release(struct kobject *kobj)
  227. {
  228. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  229. s_kobj);
  230. complete(&sbi->s_kobj_unregister);
  231. }
  232. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  233. static struct f2fs_attr f2fs_attr_##_name = { \
  234. .attr = {.name = __stringify(_name), .mode = _mode }, \
  235. .show = _show, \
  236. .store = _store, \
  237. .struct_type = _struct_type, \
  238. .offset = _offset \
  239. }
  240. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  241. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  242. f2fs_sbi_show, f2fs_sbi_store, \
  243. offsetof(struct struct_name, elname))
  244. #define F2FS_GENERAL_RO_ATTR(name) \
  245. static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  246. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
  247. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
  248. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
  249. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
  250. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
  251. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, max_small_discards, max_discards);
  252. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
  253. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
  254. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
  255. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
  256. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
  257. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
  258. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
  259. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
  260. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
  261. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
  262. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
  263. #ifdef CONFIG_F2FS_FAULT_INJECTION
  264. F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
  265. F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
  266. #endif
  267. F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
  268. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  269. static struct attribute *f2fs_attrs[] = {
  270. ATTR_LIST(gc_min_sleep_time),
  271. ATTR_LIST(gc_max_sleep_time),
  272. ATTR_LIST(gc_no_gc_sleep_time),
  273. ATTR_LIST(gc_idle),
  274. ATTR_LIST(reclaim_segments),
  275. ATTR_LIST(max_small_discards),
  276. ATTR_LIST(batched_trim_sections),
  277. ATTR_LIST(ipu_policy),
  278. ATTR_LIST(min_ipu_util),
  279. ATTR_LIST(min_fsync_blocks),
  280. ATTR_LIST(max_victim_search),
  281. ATTR_LIST(dir_level),
  282. ATTR_LIST(ram_thresh),
  283. ATTR_LIST(ra_nid_pages),
  284. ATTR_LIST(dirty_nats_ratio),
  285. ATTR_LIST(cp_interval),
  286. ATTR_LIST(idle_interval),
  287. #ifdef CONFIG_F2FS_FAULT_INJECTION
  288. ATTR_LIST(inject_rate),
  289. ATTR_LIST(inject_type),
  290. #endif
  291. ATTR_LIST(lifetime_write_kbytes),
  292. NULL,
  293. };
  294. static const struct sysfs_ops f2fs_attr_ops = {
  295. .show = f2fs_attr_show,
  296. .store = f2fs_attr_store,
  297. };
  298. static struct kobj_type f2fs_ktype = {
  299. .default_attrs = f2fs_attrs,
  300. .sysfs_ops = &f2fs_attr_ops,
  301. .release = f2fs_sb_release,
  302. };
  303. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  304. {
  305. struct va_format vaf;
  306. va_list args;
  307. va_start(args, fmt);
  308. vaf.fmt = fmt;
  309. vaf.va = &args;
  310. printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
  311. va_end(args);
  312. }
  313. static void init_once(void *foo)
  314. {
  315. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  316. inode_init_once(&fi->vfs_inode);
  317. }
  318. static int parse_options(struct super_block *sb, char *options)
  319. {
  320. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  321. struct request_queue *q;
  322. substring_t args[MAX_OPT_ARGS];
  323. char *p, *name;
  324. int arg = 0;
  325. if (!options)
  326. return 0;
  327. while ((p = strsep(&options, ",")) != NULL) {
  328. int token;
  329. if (!*p)
  330. continue;
  331. /*
  332. * Initialize args struct so we know whether arg was
  333. * found; some options take optional arguments.
  334. */
  335. args[0].to = args[0].from = NULL;
  336. token = match_token(p, f2fs_tokens, args);
  337. switch (token) {
  338. case Opt_gc_background:
  339. name = match_strdup(&args[0]);
  340. if (!name)
  341. return -ENOMEM;
  342. if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
  343. set_opt(sbi, BG_GC);
  344. clear_opt(sbi, FORCE_FG_GC);
  345. } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
  346. clear_opt(sbi, BG_GC);
  347. clear_opt(sbi, FORCE_FG_GC);
  348. } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
  349. set_opt(sbi, BG_GC);
  350. set_opt(sbi, FORCE_FG_GC);
  351. } else {
  352. kfree(name);
  353. return -EINVAL;
  354. }
  355. kfree(name);
  356. break;
  357. case Opt_disable_roll_forward:
  358. set_opt(sbi, DISABLE_ROLL_FORWARD);
  359. break;
  360. case Opt_norecovery:
  361. /* this option mounts f2fs with ro */
  362. set_opt(sbi, DISABLE_ROLL_FORWARD);
  363. if (!f2fs_readonly(sb))
  364. return -EINVAL;
  365. break;
  366. case Opt_discard:
  367. q = bdev_get_queue(sb->s_bdev);
  368. if (blk_queue_discard(q)) {
  369. set_opt(sbi, DISCARD);
  370. } else {
  371. f2fs_msg(sb, KERN_WARNING,
  372. "mounting with \"discard\" option, but "
  373. "the device does not support discard");
  374. }
  375. break;
  376. case Opt_nodiscard:
  377. clear_opt(sbi, DISCARD);
  378. case Opt_noheap:
  379. set_opt(sbi, NOHEAP);
  380. break;
  381. #ifdef CONFIG_F2FS_FS_XATTR
  382. case Opt_user_xattr:
  383. set_opt(sbi, XATTR_USER);
  384. break;
  385. case Opt_nouser_xattr:
  386. clear_opt(sbi, XATTR_USER);
  387. break;
  388. case Opt_inline_xattr:
  389. set_opt(sbi, INLINE_XATTR);
  390. break;
  391. #else
  392. case Opt_user_xattr:
  393. f2fs_msg(sb, KERN_INFO,
  394. "user_xattr options not supported");
  395. break;
  396. case Opt_nouser_xattr:
  397. f2fs_msg(sb, KERN_INFO,
  398. "nouser_xattr options not supported");
  399. break;
  400. case Opt_inline_xattr:
  401. f2fs_msg(sb, KERN_INFO,
  402. "inline_xattr options not supported");
  403. break;
  404. #endif
  405. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  406. case Opt_acl:
  407. set_opt(sbi, POSIX_ACL);
  408. break;
  409. case Opt_noacl:
  410. clear_opt(sbi, POSIX_ACL);
  411. break;
  412. #else
  413. case Opt_acl:
  414. f2fs_msg(sb, KERN_INFO, "acl options not supported");
  415. break;
  416. case Opt_noacl:
  417. f2fs_msg(sb, KERN_INFO, "noacl options not supported");
  418. break;
  419. #endif
  420. case Opt_active_logs:
  421. if (args->from && match_int(args, &arg))
  422. return -EINVAL;
  423. if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
  424. return -EINVAL;
  425. sbi->active_logs = arg;
  426. break;
  427. case Opt_disable_ext_identify:
  428. set_opt(sbi, DISABLE_EXT_IDENTIFY);
  429. break;
  430. case Opt_inline_data:
  431. set_opt(sbi, INLINE_DATA);
  432. break;
  433. case Opt_inline_dentry:
  434. set_opt(sbi, INLINE_DENTRY);
  435. break;
  436. case Opt_noinline_dentry:
  437. clear_opt(sbi, INLINE_DENTRY);
  438. break;
  439. case Opt_flush_merge:
  440. set_opt(sbi, FLUSH_MERGE);
  441. break;
  442. case Opt_noflush_merge:
  443. clear_opt(sbi, FLUSH_MERGE);
  444. break;
  445. case Opt_nobarrier:
  446. set_opt(sbi, NOBARRIER);
  447. break;
  448. case Opt_fastboot:
  449. set_opt(sbi, FASTBOOT);
  450. break;
  451. case Opt_extent_cache:
  452. set_opt(sbi, EXTENT_CACHE);
  453. break;
  454. case Opt_noextent_cache:
  455. clear_opt(sbi, EXTENT_CACHE);
  456. break;
  457. case Opt_noinline_data:
  458. clear_opt(sbi, INLINE_DATA);
  459. break;
  460. case Opt_data_flush:
  461. set_opt(sbi, DATA_FLUSH);
  462. break;
  463. case Opt_mode:
  464. name = match_strdup(&args[0]);
  465. if (!name)
  466. return -ENOMEM;
  467. if (strlen(name) == 8 &&
  468. !strncmp(name, "adaptive", 8)) {
  469. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  470. } else if (strlen(name) == 3 &&
  471. !strncmp(name, "lfs", 3)) {
  472. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  473. } else {
  474. kfree(name);
  475. return -EINVAL;
  476. }
  477. kfree(name);
  478. break;
  479. case Opt_fault_injection:
  480. if (args->from && match_int(args, &arg))
  481. return -EINVAL;
  482. #ifdef CONFIG_F2FS_FAULT_INJECTION
  483. f2fs_build_fault_attr(sbi, arg);
  484. #else
  485. f2fs_msg(sb, KERN_INFO,
  486. "FAULT_INJECTION was not selected");
  487. #endif
  488. break;
  489. case Opt_lazytime:
  490. sb->s_flags |= MS_LAZYTIME;
  491. break;
  492. case Opt_nolazytime:
  493. sb->s_flags &= ~MS_LAZYTIME;
  494. break;
  495. default:
  496. f2fs_msg(sb, KERN_ERR,
  497. "Unrecognized mount option \"%s\" or missing value",
  498. p);
  499. return -EINVAL;
  500. }
  501. }
  502. return 0;
  503. }
  504. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  505. {
  506. struct f2fs_inode_info *fi;
  507. fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
  508. if (!fi)
  509. return NULL;
  510. init_once((void *) fi);
  511. /* Initialize f2fs-specific inode info */
  512. fi->vfs_inode.i_version = 1;
  513. atomic_set(&fi->dirty_pages, 0);
  514. fi->i_current_depth = 1;
  515. fi->i_advise = 0;
  516. init_rwsem(&fi->i_sem);
  517. INIT_LIST_HEAD(&fi->dirty_list);
  518. INIT_LIST_HEAD(&fi->gdirty_list);
  519. INIT_LIST_HEAD(&fi->inmem_pages);
  520. mutex_init(&fi->inmem_lock);
  521. init_rwsem(&fi->dio_rwsem[READ]);
  522. init_rwsem(&fi->dio_rwsem[WRITE]);
  523. /* Will be used by directory only */
  524. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  525. return &fi->vfs_inode;
  526. }
  527. static int f2fs_drop_inode(struct inode *inode)
  528. {
  529. /*
  530. * This is to avoid a deadlock condition like below.
  531. * writeback_single_inode(inode)
  532. * - f2fs_write_data_page
  533. * - f2fs_gc -> iput -> evict
  534. * - inode_wait_for_writeback(inode)
  535. */
  536. if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
  537. if (!inode->i_nlink && !is_bad_inode(inode)) {
  538. /* to avoid evict_inode call simultaneously */
  539. atomic_inc(&inode->i_count);
  540. spin_unlock(&inode->i_lock);
  541. /* some remained atomic pages should discarded */
  542. if (f2fs_is_atomic_file(inode))
  543. drop_inmem_pages(inode);
  544. /* should remain fi->extent_tree for writepage */
  545. f2fs_destroy_extent_node(inode);
  546. sb_start_intwrite(inode->i_sb);
  547. f2fs_i_size_write(inode, 0);
  548. if (F2FS_HAS_BLOCKS(inode))
  549. f2fs_truncate(inode);
  550. sb_end_intwrite(inode->i_sb);
  551. fscrypt_put_encryption_info(inode, NULL);
  552. spin_lock(&inode->i_lock);
  553. atomic_dec(&inode->i_count);
  554. }
  555. return 0;
  556. }
  557. return generic_drop_inode(inode);
  558. }
  559. int f2fs_inode_dirtied(struct inode *inode)
  560. {
  561. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  562. spin_lock(&sbi->inode_lock[DIRTY_META]);
  563. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  564. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  565. return 1;
  566. }
  567. set_inode_flag(inode, FI_DIRTY_INODE);
  568. list_add_tail(&F2FS_I(inode)->gdirty_list,
  569. &sbi->inode_list[DIRTY_META]);
  570. inc_page_count(sbi, F2FS_DIRTY_IMETA);
  571. stat_inc_dirty_inode(sbi, DIRTY_META);
  572. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  573. return 0;
  574. }
  575. void f2fs_inode_synced(struct inode *inode)
  576. {
  577. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  578. spin_lock(&sbi->inode_lock[DIRTY_META]);
  579. if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  580. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  581. return;
  582. }
  583. list_del_init(&F2FS_I(inode)->gdirty_list);
  584. clear_inode_flag(inode, FI_DIRTY_INODE);
  585. clear_inode_flag(inode, FI_AUTO_RECOVER);
  586. dec_page_count(sbi, F2FS_DIRTY_IMETA);
  587. stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
  588. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  589. }
  590. /*
  591. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  592. *
  593. * We should call set_dirty_inode to write the dirty inode through write_inode.
  594. */
  595. static void f2fs_dirty_inode(struct inode *inode, int flags)
  596. {
  597. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  598. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  599. inode->i_ino == F2FS_META_INO(sbi))
  600. return;
  601. if (flags == I_DIRTY_TIME)
  602. return;
  603. if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
  604. clear_inode_flag(inode, FI_AUTO_RECOVER);
  605. f2fs_inode_dirtied(inode);
  606. }
  607. static void f2fs_i_callback(struct rcu_head *head)
  608. {
  609. struct inode *inode = container_of(head, struct inode, i_rcu);
  610. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  611. }
  612. static void f2fs_destroy_inode(struct inode *inode)
  613. {
  614. call_rcu(&inode->i_rcu, f2fs_i_callback);
  615. }
  616. static void destroy_percpu_info(struct f2fs_sb_info *sbi)
  617. {
  618. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  619. percpu_counter_destroy(&sbi->total_valid_inode_count);
  620. }
  621. static void f2fs_put_super(struct super_block *sb)
  622. {
  623. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  624. if (sbi->s_proc) {
  625. remove_proc_entry("segment_info", sbi->s_proc);
  626. remove_proc_entry("segment_bits", sbi->s_proc);
  627. remove_proc_entry(sb->s_id, f2fs_proc_root);
  628. }
  629. kobject_del(&sbi->s_kobj);
  630. stop_gc_thread(sbi);
  631. /* prevent remaining shrinker jobs */
  632. mutex_lock(&sbi->umount_mutex);
  633. /*
  634. * We don't need to do checkpoint when superblock is clean.
  635. * But, the previous checkpoint was not done by umount, it needs to do
  636. * clean checkpoint again.
  637. */
  638. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  639. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  640. struct cp_control cpc = {
  641. .reason = CP_UMOUNT,
  642. };
  643. write_checkpoint(sbi, &cpc);
  644. }
  645. /* write_checkpoint can update stat informaion */
  646. f2fs_destroy_stats(sbi);
  647. /*
  648. * normally superblock is clean, so we need to release this.
  649. * In addition, EIO will skip do checkpoint, we need this as well.
  650. */
  651. release_ino_entry(sbi, true);
  652. release_discard_addrs(sbi);
  653. f2fs_leave_shrinker(sbi);
  654. mutex_unlock(&sbi->umount_mutex);
  655. /* our cp_error case, we can wait for any writeback page */
  656. f2fs_flush_merged_bios(sbi);
  657. iput(sbi->node_inode);
  658. iput(sbi->meta_inode);
  659. /* destroy f2fs internal modules */
  660. destroy_node_manager(sbi);
  661. destroy_segment_manager(sbi);
  662. kfree(sbi->ckpt);
  663. kobject_put(&sbi->s_kobj);
  664. wait_for_completion(&sbi->s_kobj_unregister);
  665. sb->s_fs_info = NULL;
  666. if (sbi->s_chksum_driver)
  667. crypto_free_shash(sbi->s_chksum_driver);
  668. kfree(sbi->raw_super);
  669. destroy_percpu_info(sbi);
  670. kfree(sbi);
  671. }
  672. int f2fs_sync_fs(struct super_block *sb, int sync)
  673. {
  674. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  675. int err = 0;
  676. trace_f2fs_sync_fs(sb, sync);
  677. if (sync) {
  678. struct cp_control cpc;
  679. cpc.reason = __get_cp_reason(sbi);
  680. mutex_lock(&sbi->gc_mutex);
  681. err = write_checkpoint(sbi, &cpc);
  682. mutex_unlock(&sbi->gc_mutex);
  683. }
  684. f2fs_trace_ios(NULL, 1);
  685. return err;
  686. }
  687. static int f2fs_freeze(struct super_block *sb)
  688. {
  689. int err;
  690. if (f2fs_readonly(sb))
  691. return 0;
  692. err = f2fs_sync_fs(sb, 1);
  693. return err;
  694. }
  695. static int f2fs_unfreeze(struct super_block *sb)
  696. {
  697. return 0;
  698. }
  699. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  700. {
  701. struct super_block *sb = dentry->d_sb;
  702. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  703. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  704. block_t total_count, user_block_count, start_count, ovp_count;
  705. total_count = le64_to_cpu(sbi->raw_super->block_count);
  706. user_block_count = sbi->user_block_count;
  707. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  708. ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
  709. buf->f_type = F2FS_SUPER_MAGIC;
  710. buf->f_bsize = sbi->blocksize;
  711. buf->f_blocks = total_count - start_count;
  712. buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
  713. buf->f_bavail = user_block_count - valid_user_blocks(sbi);
  714. buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  715. buf->f_ffree = buf->f_files - valid_inode_count(sbi);
  716. buf->f_namelen = F2FS_NAME_LEN;
  717. buf->f_fsid.val[0] = (u32)id;
  718. buf->f_fsid.val[1] = (u32)(id >> 32);
  719. return 0;
  720. }
  721. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  722. {
  723. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  724. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
  725. if (test_opt(sbi, FORCE_FG_GC))
  726. seq_printf(seq, ",background_gc=%s", "sync");
  727. else
  728. seq_printf(seq, ",background_gc=%s", "on");
  729. } else {
  730. seq_printf(seq, ",background_gc=%s", "off");
  731. }
  732. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  733. seq_puts(seq, ",disable_roll_forward");
  734. if (test_opt(sbi, DISCARD))
  735. seq_puts(seq, ",discard");
  736. if (test_opt(sbi, NOHEAP))
  737. seq_puts(seq, ",no_heap_alloc");
  738. #ifdef CONFIG_F2FS_FS_XATTR
  739. if (test_opt(sbi, XATTR_USER))
  740. seq_puts(seq, ",user_xattr");
  741. else
  742. seq_puts(seq, ",nouser_xattr");
  743. if (test_opt(sbi, INLINE_XATTR))
  744. seq_puts(seq, ",inline_xattr");
  745. #endif
  746. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  747. if (test_opt(sbi, POSIX_ACL))
  748. seq_puts(seq, ",acl");
  749. else
  750. seq_puts(seq, ",noacl");
  751. #endif
  752. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  753. seq_puts(seq, ",disable_ext_identify");
  754. if (test_opt(sbi, INLINE_DATA))
  755. seq_puts(seq, ",inline_data");
  756. else
  757. seq_puts(seq, ",noinline_data");
  758. if (test_opt(sbi, INLINE_DENTRY))
  759. seq_puts(seq, ",inline_dentry");
  760. else
  761. seq_puts(seq, ",noinline_dentry");
  762. if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
  763. seq_puts(seq, ",flush_merge");
  764. if (test_opt(sbi, NOBARRIER))
  765. seq_puts(seq, ",nobarrier");
  766. if (test_opt(sbi, FASTBOOT))
  767. seq_puts(seq, ",fastboot");
  768. if (test_opt(sbi, EXTENT_CACHE))
  769. seq_puts(seq, ",extent_cache");
  770. else
  771. seq_puts(seq, ",noextent_cache");
  772. if (test_opt(sbi, DATA_FLUSH))
  773. seq_puts(seq, ",data_flush");
  774. seq_puts(seq, ",mode=");
  775. if (test_opt(sbi, ADAPTIVE))
  776. seq_puts(seq, "adaptive");
  777. else if (test_opt(sbi, LFS))
  778. seq_puts(seq, "lfs");
  779. seq_printf(seq, ",active_logs=%u", sbi->active_logs);
  780. return 0;
  781. }
  782. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  783. {
  784. struct super_block *sb = seq->private;
  785. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  786. unsigned int total_segs =
  787. le32_to_cpu(sbi->raw_super->segment_count_main);
  788. int i;
  789. seq_puts(seq, "format: segment_type|valid_blocks\n"
  790. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  791. for (i = 0; i < total_segs; i++) {
  792. struct seg_entry *se = get_seg_entry(sbi, i);
  793. if ((i % 10) == 0)
  794. seq_printf(seq, "%-10d", i);
  795. seq_printf(seq, "%d|%-3u", se->type,
  796. get_valid_blocks(sbi, i, 1));
  797. if ((i % 10) == 9 || i == (total_segs - 1))
  798. seq_putc(seq, '\n');
  799. else
  800. seq_putc(seq, ' ');
  801. }
  802. return 0;
  803. }
  804. static int segment_bits_seq_show(struct seq_file *seq, void *offset)
  805. {
  806. struct super_block *sb = seq->private;
  807. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  808. unsigned int total_segs =
  809. le32_to_cpu(sbi->raw_super->segment_count_main);
  810. int i, j;
  811. seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
  812. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  813. for (i = 0; i < total_segs; i++) {
  814. struct seg_entry *se = get_seg_entry(sbi, i);
  815. seq_printf(seq, "%-10d", i);
  816. seq_printf(seq, "%d|%-3u|", se->type,
  817. get_valid_blocks(sbi, i, 1));
  818. for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
  819. seq_printf(seq, " %.2x", se->cur_valid_map[j]);
  820. seq_putc(seq, '\n');
  821. }
  822. return 0;
  823. }
  824. #define F2FS_PROC_FILE_DEF(_name) \
  825. static int _name##_open_fs(struct inode *inode, struct file *file) \
  826. { \
  827. return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
  828. } \
  829. \
  830. static const struct file_operations f2fs_seq_##_name##_fops = { \
  831. .open = _name##_open_fs, \
  832. .read = seq_read, \
  833. .llseek = seq_lseek, \
  834. .release = single_release, \
  835. };
  836. F2FS_PROC_FILE_DEF(segment_info);
  837. F2FS_PROC_FILE_DEF(segment_bits);
  838. static void default_options(struct f2fs_sb_info *sbi)
  839. {
  840. /* init some FS parameters */
  841. sbi->active_logs = NR_CURSEG_TYPE;
  842. set_opt(sbi, BG_GC);
  843. set_opt(sbi, INLINE_DATA);
  844. set_opt(sbi, INLINE_DENTRY);
  845. set_opt(sbi, EXTENT_CACHE);
  846. sbi->sb->s_flags |= MS_LAZYTIME;
  847. set_opt(sbi, FLUSH_MERGE);
  848. if (f2fs_sb_mounted_hmsmr(sbi->sb)) {
  849. set_opt_mode(sbi, F2FS_MOUNT_LFS);
  850. set_opt(sbi, DISCARD);
  851. } else {
  852. set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
  853. }
  854. #ifdef CONFIG_F2FS_FS_XATTR
  855. set_opt(sbi, XATTR_USER);
  856. #endif
  857. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  858. set_opt(sbi, POSIX_ACL);
  859. #endif
  860. #ifdef CONFIG_F2FS_FAULT_INJECTION
  861. f2fs_build_fault_attr(sbi, 0);
  862. #endif
  863. }
  864. static int f2fs_remount(struct super_block *sb, int *flags, char *data)
  865. {
  866. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  867. struct f2fs_mount_info org_mount_opt;
  868. int err, active_logs;
  869. bool need_restart_gc = false;
  870. bool need_stop_gc = false;
  871. bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
  872. #ifdef CONFIG_F2FS_FAULT_INJECTION
  873. struct f2fs_fault_info ffi = sbi->fault_info;
  874. #endif
  875. /*
  876. * Save the old mount options in case we
  877. * need to restore them.
  878. */
  879. org_mount_opt = sbi->mount_opt;
  880. active_logs = sbi->active_logs;
  881. /* recover superblocks we couldn't write due to previous RO mount */
  882. if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  883. err = f2fs_commit_super(sbi, false);
  884. f2fs_msg(sb, KERN_INFO,
  885. "Try to recover all the superblocks, ret: %d", err);
  886. if (!err)
  887. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  888. }
  889. sbi->mount_opt.opt = 0;
  890. default_options(sbi);
  891. /* parse mount options */
  892. err = parse_options(sb, data);
  893. if (err)
  894. goto restore_opts;
  895. /*
  896. * Previous and new state of filesystem is RO,
  897. * so skip checking GC and FLUSH_MERGE conditions.
  898. */
  899. if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
  900. goto skip;
  901. /* disallow enable/disable extent_cache dynamically */
  902. if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
  903. err = -EINVAL;
  904. f2fs_msg(sbi->sb, KERN_WARNING,
  905. "switch extent_cache option is not allowed");
  906. goto restore_opts;
  907. }
  908. /*
  909. * We stop the GC thread if FS is mounted as RO
  910. * or if background_gc = off is passed in mount
  911. * option. Also sync the filesystem.
  912. */
  913. if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
  914. if (sbi->gc_thread) {
  915. stop_gc_thread(sbi);
  916. need_restart_gc = true;
  917. }
  918. } else if (!sbi->gc_thread) {
  919. err = start_gc_thread(sbi);
  920. if (err)
  921. goto restore_opts;
  922. need_stop_gc = true;
  923. }
  924. if (*flags & MS_RDONLY) {
  925. writeback_inodes_sb(sb, WB_REASON_SYNC);
  926. sync_inodes_sb(sb);
  927. set_sbi_flag(sbi, SBI_IS_DIRTY);
  928. set_sbi_flag(sbi, SBI_IS_CLOSE);
  929. f2fs_sync_fs(sb, 1);
  930. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  931. }
  932. /*
  933. * We stop issue flush thread if FS is mounted as RO
  934. * or if flush_merge is not passed in mount option.
  935. */
  936. if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  937. destroy_flush_cmd_control(sbi);
  938. } else if (!SM_I(sbi)->cmd_control_info) {
  939. err = create_flush_cmd_control(sbi);
  940. if (err)
  941. goto restore_gc;
  942. }
  943. skip:
  944. /* Update the POSIXACL Flag */
  945. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  946. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  947. return 0;
  948. restore_gc:
  949. if (need_restart_gc) {
  950. if (start_gc_thread(sbi))
  951. f2fs_msg(sbi->sb, KERN_WARNING,
  952. "background gc thread has stopped");
  953. } else if (need_stop_gc) {
  954. stop_gc_thread(sbi);
  955. }
  956. restore_opts:
  957. sbi->mount_opt = org_mount_opt;
  958. sbi->active_logs = active_logs;
  959. #ifdef CONFIG_F2FS_FAULT_INJECTION
  960. sbi->fault_info = ffi;
  961. #endif
  962. return err;
  963. }
  964. static struct super_operations f2fs_sops = {
  965. .alloc_inode = f2fs_alloc_inode,
  966. .drop_inode = f2fs_drop_inode,
  967. .destroy_inode = f2fs_destroy_inode,
  968. .write_inode = f2fs_write_inode,
  969. .dirty_inode = f2fs_dirty_inode,
  970. .show_options = f2fs_show_options,
  971. .evict_inode = f2fs_evict_inode,
  972. .put_super = f2fs_put_super,
  973. .sync_fs = f2fs_sync_fs,
  974. .freeze_fs = f2fs_freeze,
  975. .unfreeze_fs = f2fs_unfreeze,
  976. .statfs = f2fs_statfs,
  977. .remount_fs = f2fs_remount,
  978. };
  979. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  980. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  981. {
  982. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  983. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  984. ctx, len, NULL);
  985. }
  986. static int f2fs_key_prefix(struct inode *inode, u8 **key)
  987. {
  988. *key = F2FS_I_SB(inode)->key_prefix;
  989. return F2FS_I_SB(inode)->key_prefix_size;
  990. }
  991. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  992. void *fs_data)
  993. {
  994. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  995. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  996. ctx, len, fs_data, XATTR_CREATE);
  997. }
  998. static unsigned f2fs_max_namelen(struct inode *inode)
  999. {
  1000. return S_ISLNK(inode->i_mode) ?
  1001. inode->i_sb->s_blocksize : F2FS_NAME_LEN;
  1002. }
  1003. static struct fscrypt_operations f2fs_cryptops = {
  1004. .get_context = f2fs_get_context,
  1005. .key_prefix = f2fs_key_prefix,
  1006. .set_context = f2fs_set_context,
  1007. .is_encrypted = f2fs_encrypted_inode,
  1008. .empty_dir = f2fs_empty_dir,
  1009. .max_namelen = f2fs_max_namelen,
  1010. };
  1011. #else
  1012. static struct fscrypt_operations f2fs_cryptops = {
  1013. .is_encrypted = f2fs_encrypted_inode,
  1014. };
  1015. #endif
  1016. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  1017. u64 ino, u32 generation)
  1018. {
  1019. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1020. struct inode *inode;
  1021. if (check_nid_range(sbi, ino))
  1022. return ERR_PTR(-ESTALE);
  1023. /*
  1024. * f2fs_iget isn't quite right if the inode is currently unallocated!
  1025. * However f2fs_iget currently does appropriate checks to handle stale
  1026. * inodes so everything is OK.
  1027. */
  1028. inode = f2fs_iget(sb, ino);
  1029. if (IS_ERR(inode))
  1030. return ERR_CAST(inode);
  1031. if (unlikely(generation && inode->i_generation != generation)) {
  1032. /* we didn't find the right inode.. */
  1033. iput(inode);
  1034. return ERR_PTR(-ESTALE);
  1035. }
  1036. return inode;
  1037. }
  1038. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1039. int fh_len, int fh_type)
  1040. {
  1041. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1042. f2fs_nfs_get_inode);
  1043. }
  1044. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1045. int fh_len, int fh_type)
  1046. {
  1047. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1048. f2fs_nfs_get_inode);
  1049. }
  1050. static const struct export_operations f2fs_export_ops = {
  1051. .fh_to_dentry = f2fs_fh_to_dentry,
  1052. .fh_to_parent = f2fs_fh_to_parent,
  1053. .get_parent = f2fs_get_parent,
  1054. };
  1055. static loff_t max_file_blocks(void)
  1056. {
  1057. loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
  1058. loff_t leaf_count = ADDRS_PER_BLOCK;
  1059. /* two direct node blocks */
  1060. result += (leaf_count * 2);
  1061. /* two indirect node blocks */
  1062. leaf_count *= NIDS_PER_BLOCK;
  1063. result += (leaf_count * 2);
  1064. /* one double indirect node block */
  1065. leaf_count *= NIDS_PER_BLOCK;
  1066. result += leaf_count;
  1067. return result;
  1068. }
  1069. static int __f2fs_commit_super(struct buffer_head *bh,
  1070. struct f2fs_super_block *super)
  1071. {
  1072. lock_buffer(bh);
  1073. if (super)
  1074. memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
  1075. set_buffer_uptodate(bh);
  1076. set_buffer_dirty(bh);
  1077. unlock_buffer(bh);
  1078. /* it's rare case, we can do fua all the time */
  1079. return __sync_dirty_buffer(bh, WRITE_FLUSH_FUA);
  1080. }
  1081. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  1082. struct buffer_head *bh)
  1083. {
  1084. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1085. (bh->b_data + F2FS_SUPER_OFFSET);
  1086. struct super_block *sb = sbi->sb;
  1087. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1088. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  1089. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  1090. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  1091. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1092. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1093. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  1094. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  1095. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  1096. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  1097. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  1098. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  1099. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1100. u64 main_end_blkaddr = main_blkaddr +
  1101. (segment_count_main << log_blocks_per_seg);
  1102. u64 seg_end_blkaddr = segment0_blkaddr +
  1103. (segment_count << log_blocks_per_seg);
  1104. if (segment0_blkaddr != cp_blkaddr) {
  1105. f2fs_msg(sb, KERN_INFO,
  1106. "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  1107. segment0_blkaddr, cp_blkaddr);
  1108. return true;
  1109. }
  1110. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  1111. sit_blkaddr) {
  1112. f2fs_msg(sb, KERN_INFO,
  1113. "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  1114. cp_blkaddr, sit_blkaddr,
  1115. segment_count_ckpt << log_blocks_per_seg);
  1116. return true;
  1117. }
  1118. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  1119. nat_blkaddr) {
  1120. f2fs_msg(sb, KERN_INFO,
  1121. "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  1122. sit_blkaddr, nat_blkaddr,
  1123. segment_count_sit << log_blocks_per_seg);
  1124. return true;
  1125. }
  1126. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  1127. ssa_blkaddr) {
  1128. f2fs_msg(sb, KERN_INFO,
  1129. "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  1130. nat_blkaddr, ssa_blkaddr,
  1131. segment_count_nat << log_blocks_per_seg);
  1132. return true;
  1133. }
  1134. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  1135. main_blkaddr) {
  1136. f2fs_msg(sb, KERN_INFO,
  1137. "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  1138. ssa_blkaddr, main_blkaddr,
  1139. segment_count_ssa << log_blocks_per_seg);
  1140. return true;
  1141. }
  1142. if (main_end_blkaddr > seg_end_blkaddr) {
  1143. f2fs_msg(sb, KERN_INFO,
  1144. "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
  1145. main_blkaddr,
  1146. segment0_blkaddr +
  1147. (segment_count << log_blocks_per_seg),
  1148. segment_count_main << log_blocks_per_seg);
  1149. return true;
  1150. } else if (main_end_blkaddr < seg_end_blkaddr) {
  1151. int err = 0;
  1152. char *res;
  1153. /* fix in-memory information all the time */
  1154. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  1155. segment0_blkaddr) >> log_blocks_per_seg);
  1156. if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
  1157. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1158. res = "internally";
  1159. } else {
  1160. err = __f2fs_commit_super(bh, NULL);
  1161. res = err ? "failed" : "done";
  1162. }
  1163. f2fs_msg(sb, KERN_INFO,
  1164. "Fix alignment : %s, start(%u) end(%u) block(%u)",
  1165. res, main_blkaddr,
  1166. segment0_blkaddr +
  1167. (segment_count << log_blocks_per_seg),
  1168. segment_count_main << log_blocks_per_seg);
  1169. if (err)
  1170. return true;
  1171. }
  1172. return false;
  1173. }
  1174. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  1175. struct buffer_head *bh)
  1176. {
  1177. struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
  1178. (bh->b_data + F2FS_SUPER_OFFSET);
  1179. struct super_block *sb = sbi->sb;
  1180. unsigned int blocksize;
  1181. if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
  1182. f2fs_msg(sb, KERN_INFO,
  1183. "Magic Mismatch, valid(0x%x) - read(0x%x)",
  1184. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  1185. return 1;
  1186. }
  1187. /* Currently, support only 4KB page cache size */
  1188. if (F2FS_BLKSIZE != PAGE_SIZE) {
  1189. f2fs_msg(sb, KERN_INFO,
  1190. "Invalid page_cache_size (%lu), supports only 4KB\n",
  1191. PAGE_SIZE);
  1192. return 1;
  1193. }
  1194. /* Currently, support only 4KB block size */
  1195. blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
  1196. if (blocksize != F2FS_BLKSIZE) {
  1197. f2fs_msg(sb, KERN_INFO,
  1198. "Invalid blocksize (%u), supports only 4KB\n",
  1199. blocksize);
  1200. return 1;
  1201. }
  1202. /* check log blocks per segment */
  1203. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  1204. f2fs_msg(sb, KERN_INFO,
  1205. "Invalid log blocks per segment (%u)\n",
  1206. le32_to_cpu(raw_super->log_blocks_per_seg));
  1207. return 1;
  1208. }
  1209. /* Currently, support 512/1024/2048/4096 bytes sector size */
  1210. if (le32_to_cpu(raw_super->log_sectorsize) >
  1211. F2FS_MAX_LOG_SECTOR_SIZE ||
  1212. le32_to_cpu(raw_super->log_sectorsize) <
  1213. F2FS_MIN_LOG_SECTOR_SIZE) {
  1214. f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
  1215. le32_to_cpu(raw_super->log_sectorsize));
  1216. return 1;
  1217. }
  1218. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  1219. le32_to_cpu(raw_super->log_sectorsize) !=
  1220. F2FS_MAX_LOG_SECTOR_SIZE) {
  1221. f2fs_msg(sb, KERN_INFO,
  1222. "Invalid log sectors per block(%u) log sectorsize(%u)",
  1223. le32_to_cpu(raw_super->log_sectors_per_block),
  1224. le32_to_cpu(raw_super->log_sectorsize));
  1225. return 1;
  1226. }
  1227. /* check reserved ino info */
  1228. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  1229. le32_to_cpu(raw_super->meta_ino) != 2 ||
  1230. le32_to_cpu(raw_super->root_ino) != 3) {
  1231. f2fs_msg(sb, KERN_INFO,
  1232. "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  1233. le32_to_cpu(raw_super->node_ino),
  1234. le32_to_cpu(raw_super->meta_ino),
  1235. le32_to_cpu(raw_super->root_ino));
  1236. return 1;
  1237. }
  1238. if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
  1239. f2fs_msg(sb, KERN_INFO,
  1240. "Invalid segment count (%u)",
  1241. le32_to_cpu(raw_super->segment_count));
  1242. return 1;
  1243. }
  1244. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  1245. if (sanity_check_area_boundary(sbi, bh))
  1246. return 1;
  1247. return 0;
  1248. }
  1249. int sanity_check_ckpt(struct f2fs_sb_info *sbi)
  1250. {
  1251. unsigned int total, fsmeta;
  1252. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1253. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1254. unsigned int main_segs, blocks_per_seg;
  1255. int i;
  1256. total = le32_to_cpu(raw_super->segment_count);
  1257. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  1258. fsmeta += le32_to_cpu(raw_super->segment_count_sit);
  1259. fsmeta += le32_to_cpu(raw_super->segment_count_nat);
  1260. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  1261. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  1262. if (unlikely(fsmeta >= total))
  1263. return 1;
  1264. main_segs = le32_to_cpu(raw_super->segment_count_main);
  1265. blocks_per_seg = sbi->blocks_per_seg;
  1266. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  1267. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  1268. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
  1269. return 1;
  1270. }
  1271. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  1272. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  1273. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
  1274. return 1;
  1275. }
  1276. if (unlikely(f2fs_cp_error(sbi))) {
  1277. f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
  1278. return 1;
  1279. }
  1280. return 0;
  1281. }
  1282. static void init_sb_info(struct f2fs_sb_info *sbi)
  1283. {
  1284. struct f2fs_super_block *raw_super = sbi->raw_super;
  1285. int i;
  1286. sbi->log_sectors_per_block =
  1287. le32_to_cpu(raw_super->log_sectors_per_block);
  1288. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  1289. sbi->blocksize = 1 << sbi->log_blocksize;
  1290. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  1291. sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
  1292. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  1293. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  1294. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  1295. sbi->total_node_count =
  1296. (le32_to_cpu(raw_super->segment_count_nat) / 2)
  1297. * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
  1298. sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
  1299. sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
  1300. sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
  1301. sbi->cur_victim_sec = NULL_SECNO;
  1302. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  1303. sbi->dir_level = DEF_DIR_LEVEL;
  1304. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  1305. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  1306. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  1307. for (i = 0; i < NR_COUNT_TYPE; i++)
  1308. atomic_set(&sbi->nr_pages[i], 0);
  1309. INIT_LIST_HEAD(&sbi->s_list);
  1310. mutex_init(&sbi->umount_mutex);
  1311. mutex_init(&sbi->wio_mutex[NODE]);
  1312. mutex_init(&sbi->wio_mutex[DATA]);
  1313. spin_lock_init(&sbi->cp_lock);
  1314. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1315. memcpy(sbi->key_prefix, F2FS_KEY_DESC_PREFIX,
  1316. F2FS_KEY_DESC_PREFIX_SIZE);
  1317. sbi->key_prefix_size = F2FS_KEY_DESC_PREFIX_SIZE;
  1318. #endif
  1319. }
  1320. static int init_percpu_info(struct f2fs_sb_info *sbi)
  1321. {
  1322. int err;
  1323. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
  1324. if (err)
  1325. return err;
  1326. return percpu_counter_init(&sbi->total_valid_inode_count, 0,
  1327. GFP_KERNEL);
  1328. }
  1329. /*
  1330. * Read f2fs raw super block.
  1331. * Because we have two copies of super block, so read both of them
  1332. * to get the first valid one. If any one of them is broken, we pass
  1333. * them recovery flag back to the caller.
  1334. */
  1335. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  1336. struct f2fs_super_block **raw_super,
  1337. int *valid_super_block, int *recovery)
  1338. {
  1339. struct super_block *sb = sbi->sb;
  1340. int block;
  1341. struct buffer_head *bh;
  1342. struct f2fs_super_block *super;
  1343. int err = 0;
  1344. super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
  1345. if (!super)
  1346. return -ENOMEM;
  1347. for (block = 0; block < 2; block++) {
  1348. bh = sb_bread(sb, block);
  1349. if (!bh) {
  1350. f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
  1351. block + 1);
  1352. err = -EIO;
  1353. continue;
  1354. }
  1355. /* sanity checking of raw super */
  1356. if (sanity_check_raw_super(sbi, bh)) {
  1357. f2fs_msg(sb, KERN_ERR,
  1358. "Can't find valid F2FS filesystem in %dth superblock",
  1359. block + 1);
  1360. err = -EINVAL;
  1361. brelse(bh);
  1362. continue;
  1363. }
  1364. if (!*raw_super) {
  1365. memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
  1366. sizeof(*super));
  1367. *valid_super_block = block;
  1368. *raw_super = super;
  1369. }
  1370. brelse(bh);
  1371. }
  1372. /* Fail to read any one of the superblocks*/
  1373. if (err < 0)
  1374. *recovery = 1;
  1375. /* No valid superblock */
  1376. if (!*raw_super)
  1377. kfree(super);
  1378. else
  1379. err = 0;
  1380. return err;
  1381. }
  1382. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  1383. {
  1384. struct buffer_head *bh;
  1385. int err;
  1386. if ((recover && f2fs_readonly(sbi->sb)) ||
  1387. bdev_read_only(sbi->sb->s_bdev)) {
  1388. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  1389. return -EROFS;
  1390. }
  1391. /* write back-up superblock first */
  1392. bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
  1393. if (!bh)
  1394. return -EIO;
  1395. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1396. brelse(bh);
  1397. /* if we are in recovery path, skip writing valid superblock */
  1398. if (recover || err)
  1399. return err;
  1400. /* write current valid superblock */
  1401. bh = sb_getblk(sbi->sb, sbi->valid_super_block);
  1402. if (!bh)
  1403. return -EIO;
  1404. err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
  1405. brelse(bh);
  1406. return err;
  1407. }
  1408. static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
  1409. {
  1410. struct f2fs_sb_info *sbi;
  1411. struct f2fs_super_block *raw_super;
  1412. struct inode *root;
  1413. int err;
  1414. bool retry = true, need_fsck = false;
  1415. char *options = NULL;
  1416. int recovery, i, valid_super_block;
  1417. struct curseg_info *seg_i;
  1418. try_onemore:
  1419. err = -EINVAL;
  1420. raw_super = NULL;
  1421. valid_super_block = -1;
  1422. recovery = 0;
  1423. /* allocate memory for f2fs-specific super block info */
  1424. sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
  1425. if (!sbi)
  1426. return -ENOMEM;
  1427. sbi->sb = sb;
  1428. /* Load the checksum driver */
  1429. sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
  1430. if (IS_ERR(sbi->s_chksum_driver)) {
  1431. f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
  1432. err = PTR_ERR(sbi->s_chksum_driver);
  1433. sbi->s_chksum_driver = NULL;
  1434. goto free_sbi;
  1435. }
  1436. /* set a block size */
  1437. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  1438. f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
  1439. goto free_sbi;
  1440. }
  1441. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  1442. &recovery);
  1443. if (err)
  1444. goto free_sbi;
  1445. sb->s_fs_info = sbi;
  1446. sbi->raw_super = raw_super;
  1447. default_options(sbi);
  1448. /* parse mount options */
  1449. options = kstrdup((const char *)data, GFP_KERNEL);
  1450. if (data && !options) {
  1451. err = -ENOMEM;
  1452. goto free_sb_buf;
  1453. }
  1454. err = parse_options(sb, options);
  1455. if (err)
  1456. goto free_options;
  1457. sbi->max_file_blocks = max_file_blocks();
  1458. sb->s_maxbytes = sbi->max_file_blocks <<
  1459. le32_to_cpu(raw_super->log_blocksize);
  1460. sb->s_max_links = F2FS_LINK_MAX;
  1461. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1462. sb->s_op = &f2fs_sops;
  1463. sb->s_cop = &f2fs_cryptops;
  1464. sb->s_xattr = f2fs_xattr_handlers;
  1465. sb->s_export_op = &f2fs_export_ops;
  1466. sb->s_magic = F2FS_SUPER_MAGIC;
  1467. sb->s_time_gran = 1;
  1468. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1469. (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
  1470. memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
  1471. /* init f2fs-specific super block info */
  1472. sbi->valid_super_block = valid_super_block;
  1473. mutex_init(&sbi->gc_mutex);
  1474. mutex_init(&sbi->cp_mutex);
  1475. init_rwsem(&sbi->node_write);
  1476. /* disallow all the data/node/meta page writes */
  1477. set_sbi_flag(sbi, SBI_POR_DOING);
  1478. spin_lock_init(&sbi->stat_lock);
  1479. init_rwsem(&sbi->read_io.io_rwsem);
  1480. sbi->read_io.sbi = sbi;
  1481. sbi->read_io.bio = NULL;
  1482. for (i = 0; i < NR_PAGE_TYPE; i++) {
  1483. init_rwsem(&sbi->write_io[i].io_rwsem);
  1484. sbi->write_io[i].sbi = sbi;
  1485. sbi->write_io[i].bio = NULL;
  1486. }
  1487. init_rwsem(&sbi->cp_rwsem);
  1488. init_waitqueue_head(&sbi->cp_wait);
  1489. init_sb_info(sbi);
  1490. err = init_percpu_info(sbi);
  1491. if (err)
  1492. goto free_options;
  1493. /* get an inode for meta space */
  1494. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  1495. if (IS_ERR(sbi->meta_inode)) {
  1496. f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
  1497. err = PTR_ERR(sbi->meta_inode);
  1498. goto free_options;
  1499. }
  1500. err = get_valid_checkpoint(sbi);
  1501. if (err) {
  1502. f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
  1503. goto free_meta_inode;
  1504. }
  1505. sbi->total_valid_node_count =
  1506. le32_to_cpu(sbi->ckpt->valid_node_count);
  1507. percpu_counter_set(&sbi->total_valid_inode_count,
  1508. le32_to_cpu(sbi->ckpt->valid_inode_count));
  1509. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  1510. sbi->total_valid_block_count =
  1511. le64_to_cpu(sbi->ckpt->valid_block_count);
  1512. sbi->last_valid_block_count = sbi->total_valid_block_count;
  1513. for (i = 0; i < NR_INODE_TYPE; i++) {
  1514. INIT_LIST_HEAD(&sbi->inode_list[i]);
  1515. spin_lock_init(&sbi->inode_lock[i]);
  1516. }
  1517. init_extent_cache_info(sbi);
  1518. init_ino_entry_info(sbi);
  1519. /* setup f2fs internal modules */
  1520. err = build_segment_manager(sbi);
  1521. if (err) {
  1522. f2fs_msg(sb, KERN_ERR,
  1523. "Failed to initialize F2FS segment manager");
  1524. goto free_sm;
  1525. }
  1526. err = build_node_manager(sbi);
  1527. if (err) {
  1528. f2fs_msg(sb, KERN_ERR,
  1529. "Failed to initialize F2FS node manager");
  1530. goto free_nm;
  1531. }
  1532. /* For write statistics */
  1533. if (sb->s_bdev->bd_part)
  1534. sbi->sectors_written_start =
  1535. (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  1536. /* Read accumulated write IO statistics if exists */
  1537. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  1538. if (__exist_node_summaries(sbi))
  1539. sbi->kbytes_written =
  1540. le64_to_cpu(seg_i->journal->info.kbytes_written);
  1541. build_gc_manager(sbi);
  1542. /* get an inode for node space */
  1543. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  1544. if (IS_ERR(sbi->node_inode)) {
  1545. f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
  1546. err = PTR_ERR(sbi->node_inode);
  1547. goto free_nm;
  1548. }
  1549. f2fs_join_shrinker(sbi);
  1550. /* if there are nt orphan nodes free them */
  1551. err = recover_orphan_inodes(sbi);
  1552. if (err)
  1553. goto free_node_inode;
  1554. /* read root inode and dentry */
  1555. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  1556. if (IS_ERR(root)) {
  1557. f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
  1558. err = PTR_ERR(root);
  1559. goto free_node_inode;
  1560. }
  1561. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1562. iput(root);
  1563. err = -EINVAL;
  1564. goto free_node_inode;
  1565. }
  1566. sb->s_root = d_make_root(root); /* allocate root dentry */
  1567. if (!sb->s_root) {
  1568. err = -ENOMEM;
  1569. goto free_root_inode;
  1570. }
  1571. err = f2fs_build_stats(sbi);
  1572. if (err)
  1573. goto free_root_inode;
  1574. if (f2fs_proc_root)
  1575. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  1576. if (sbi->s_proc) {
  1577. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  1578. &f2fs_seq_segment_info_fops, sb);
  1579. proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
  1580. &f2fs_seq_segment_bits_fops, sb);
  1581. }
  1582. sbi->s_kobj.kset = f2fs_kset;
  1583. init_completion(&sbi->s_kobj_unregister);
  1584. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
  1585. "%s", sb->s_id);
  1586. if (err)
  1587. goto free_proc;
  1588. /* recover fsynced data */
  1589. if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
  1590. /*
  1591. * mount should be failed, when device has readonly mode, and
  1592. * previous checkpoint was not done by clean system shutdown.
  1593. */
  1594. if (bdev_read_only(sb->s_bdev) &&
  1595. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  1596. err = -EROFS;
  1597. goto free_kobj;
  1598. }
  1599. if (need_fsck)
  1600. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1601. if (!retry)
  1602. goto skip_recovery;
  1603. err = recover_fsync_data(sbi, false);
  1604. if (err < 0) {
  1605. need_fsck = true;
  1606. f2fs_msg(sb, KERN_ERR,
  1607. "Cannot recover all fsync data errno=%d", err);
  1608. goto free_kobj;
  1609. }
  1610. } else {
  1611. err = recover_fsync_data(sbi, true);
  1612. if (!f2fs_readonly(sb) && err > 0) {
  1613. err = -EINVAL;
  1614. f2fs_msg(sb, KERN_ERR,
  1615. "Need to recover fsync data");
  1616. goto free_kobj;
  1617. }
  1618. }
  1619. skip_recovery:
  1620. /* recover_fsync_data() cleared this already */
  1621. clear_sbi_flag(sbi, SBI_POR_DOING);
  1622. /*
  1623. * If filesystem is not mounted as read-only then
  1624. * do start the gc_thread.
  1625. */
  1626. if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
  1627. /* After POR, we can run background GC thread.*/
  1628. err = start_gc_thread(sbi);
  1629. if (err)
  1630. goto free_kobj;
  1631. }
  1632. kfree(options);
  1633. /* recover broken superblock */
  1634. if (recovery) {
  1635. err = f2fs_commit_super(sbi, true);
  1636. f2fs_msg(sb, KERN_INFO,
  1637. "Try to recover %dth superblock, ret: %d",
  1638. sbi->valid_super_block ? 1 : 2, err);
  1639. }
  1640. f2fs_update_time(sbi, CP_TIME);
  1641. f2fs_update_time(sbi, REQ_TIME);
  1642. return 0;
  1643. free_kobj:
  1644. f2fs_sync_inode_meta(sbi);
  1645. kobject_del(&sbi->s_kobj);
  1646. kobject_put(&sbi->s_kobj);
  1647. wait_for_completion(&sbi->s_kobj_unregister);
  1648. free_proc:
  1649. if (sbi->s_proc) {
  1650. remove_proc_entry("segment_info", sbi->s_proc);
  1651. remove_proc_entry("segment_bits", sbi->s_proc);
  1652. remove_proc_entry(sb->s_id, f2fs_proc_root);
  1653. }
  1654. f2fs_destroy_stats(sbi);
  1655. free_root_inode:
  1656. dput(sb->s_root);
  1657. sb->s_root = NULL;
  1658. free_node_inode:
  1659. truncate_inode_pages_final(NODE_MAPPING(sbi));
  1660. mutex_lock(&sbi->umount_mutex);
  1661. release_ino_entry(sbi, true);
  1662. f2fs_leave_shrinker(sbi);
  1663. iput(sbi->node_inode);
  1664. mutex_unlock(&sbi->umount_mutex);
  1665. free_nm:
  1666. destroy_node_manager(sbi);
  1667. free_sm:
  1668. destroy_segment_manager(sbi);
  1669. kfree(sbi->ckpt);
  1670. free_meta_inode:
  1671. make_bad_inode(sbi->meta_inode);
  1672. iput(sbi->meta_inode);
  1673. free_options:
  1674. destroy_percpu_info(sbi);
  1675. kfree(options);
  1676. free_sb_buf:
  1677. kfree(raw_super);
  1678. free_sbi:
  1679. if (sbi->s_chksum_driver)
  1680. crypto_free_shash(sbi->s_chksum_driver);
  1681. kfree(sbi);
  1682. /* give only one another chance */
  1683. if (retry) {
  1684. retry = false;
  1685. shrink_dcache_sb(sb);
  1686. goto try_onemore;
  1687. }
  1688. return err;
  1689. }
  1690. static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
  1691. const char *dev_name, void *data)
  1692. {
  1693. return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
  1694. }
  1695. static void kill_f2fs_super(struct super_block *sb)
  1696. {
  1697. if (sb->s_root)
  1698. set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
  1699. kill_block_super(sb);
  1700. }
  1701. static struct file_system_type f2fs_fs_type = {
  1702. .owner = THIS_MODULE,
  1703. .name = "f2fs",
  1704. .mount = f2fs_mount,
  1705. .kill_sb = kill_f2fs_super,
  1706. .fs_flags = FS_REQUIRES_DEV,
  1707. };
  1708. MODULE_ALIAS_FS("f2fs");
  1709. static int __init init_inodecache(void)
  1710. {
  1711. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  1712. sizeof(struct f2fs_inode_info), 0,
  1713. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  1714. if (!f2fs_inode_cachep)
  1715. return -ENOMEM;
  1716. return 0;
  1717. }
  1718. static void destroy_inodecache(void)
  1719. {
  1720. /*
  1721. * Make sure all delayed rcu free inodes are flushed before we
  1722. * destroy cache.
  1723. */
  1724. rcu_barrier();
  1725. kmem_cache_destroy(f2fs_inode_cachep);
  1726. }
  1727. static int __init init_f2fs_fs(void)
  1728. {
  1729. int err;
  1730. if (PAGE_SIZE != F2FS_BLKSIZE) {
  1731. printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
  1732. PAGE_SIZE, F2FS_BLKSIZE);
  1733. return -EINVAL;
  1734. }
  1735. f2fs_build_trace_ios();
  1736. err = init_inodecache();
  1737. if (err)
  1738. goto fail;
  1739. err = create_node_manager_caches();
  1740. if (err)
  1741. goto free_inodecache;
  1742. err = create_segment_manager_caches();
  1743. if (err)
  1744. goto free_node_manager_caches;
  1745. err = create_checkpoint_caches();
  1746. if (err)
  1747. goto free_segment_manager_caches;
  1748. err = create_extent_cache();
  1749. if (err)
  1750. goto free_checkpoint_caches;
  1751. f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
  1752. if (!f2fs_kset) {
  1753. err = -ENOMEM;
  1754. goto free_extent_cache;
  1755. }
  1756. err = register_shrinker(&f2fs_shrinker_info);
  1757. if (err)
  1758. goto free_kset;
  1759. err = register_filesystem(&f2fs_fs_type);
  1760. if (err)
  1761. goto free_shrinker;
  1762. err = f2fs_create_root_stats();
  1763. if (err)
  1764. goto free_filesystem;
  1765. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  1766. return 0;
  1767. free_filesystem:
  1768. unregister_filesystem(&f2fs_fs_type);
  1769. free_shrinker:
  1770. unregister_shrinker(&f2fs_shrinker_info);
  1771. free_kset:
  1772. kset_unregister(f2fs_kset);
  1773. free_extent_cache:
  1774. destroy_extent_cache();
  1775. free_checkpoint_caches:
  1776. destroy_checkpoint_caches();
  1777. free_segment_manager_caches:
  1778. destroy_segment_manager_caches();
  1779. free_node_manager_caches:
  1780. destroy_node_manager_caches();
  1781. free_inodecache:
  1782. destroy_inodecache();
  1783. fail:
  1784. return err;
  1785. }
  1786. static void __exit exit_f2fs_fs(void)
  1787. {
  1788. remove_proc_entry("fs/f2fs", NULL);
  1789. f2fs_destroy_root_stats();
  1790. unregister_filesystem(&f2fs_fs_type);
  1791. unregister_shrinker(&f2fs_shrinker_info);
  1792. kset_unregister(f2fs_kset);
  1793. destroy_extent_cache();
  1794. destroy_checkpoint_caches();
  1795. destroy_segment_manager_caches();
  1796. destroy_node_manager_caches();
  1797. destroy_inodecache();
  1798. f2fs_destroy_trace_ios();
  1799. }
  1800. module_init(init_f2fs_fs)
  1801. module_exit(exit_f2fs_fs)
  1802. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  1803. MODULE_DESCRIPTION("Flash Friendly File System");
  1804. MODULE_LICENSE("GPL");