super.c 33 KB

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