super.c 37 KB

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