super.c 29 KB

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