super.c 26 KB

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