super.c 28 KB

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