super.c 27 KB

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