blob: a79d9600fc869fb187828731bfcd2d8b4cd8ad14 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
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
28#include "f2fs.h"
29#include "node.h"
30#include "segment.h"
31#include "xattr.h"
32#include "gc.h"
Jaegeuk Kime0cea842015-02-18 20:43:11 -060033#include "trace.h"
Linus Torvalds8005ecc2012-12-20 13:54:51 -080034
35#define CREATE_TRACE_POINTS
36#include <trace/events/f2fs.h>
37
38static struct proc_dir_entry *f2fs_proc_root;
39static struct kmem_cache *f2fs_inode_cachep;
40static struct kset *f2fs_kset;
41
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -060042/* f2fs-wide shrinker description */
43static struct shrinker f2fs_shrinker_info = {
44 .shrink = f2fs_shrink_scan,
45 .seeks = DEFAULT_SEEKS,
46};
47
Linus Torvalds8005ecc2012-12-20 13:54:51 -080048enum {
49 Opt_gc_background,
50 Opt_disable_roll_forward,
Jaegeuk Kime0cea842015-02-18 20:43:11 -060051 Opt_norecovery,
Linus Torvalds8005ecc2012-12-20 13:54:51 -080052 Opt_discard,
53 Opt_noheap,
54 Opt_user_xattr,
55 Opt_nouser_xattr,
56 Opt_acl,
57 Opt_noacl,
58 Opt_active_logs,
59 Opt_disable_ext_identify,
60 Opt_inline_xattr,
Changman Leeb1a94e82013-11-15 10:42:51 +090061 Opt_inline_data,
Jaegeuk Kime0cea842015-02-18 20:43:11 -060062 Opt_inline_dentry,
Evan McClainf3f030d2014-07-17 21:16:35 -040063 Opt_flush_merge,
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -070064 Opt_nobarrier,
Jaegeuk Kime0cea842015-02-18 20:43:11 -060065 Opt_fastboot,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -060066 Opt_extent_cache,
67 Opt_noextent_cache,
68 Opt_noinline_data,
Linus Torvalds8005ecc2012-12-20 13:54:51 -080069 Opt_err,
70};
71
72static match_table_t f2fs_tokens = {
73 {Opt_gc_background, "background_gc=%s"},
74 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kime0cea842015-02-18 20:43:11 -060075 {Opt_norecovery, "norecovery"},
Linus Torvalds8005ecc2012-12-20 13:54:51 -080076 {Opt_discard, "discard"},
77 {Opt_noheap, "no_heap"},
78 {Opt_user_xattr, "user_xattr"},
79 {Opt_nouser_xattr, "nouser_xattr"},
80 {Opt_acl, "acl"},
81 {Opt_noacl, "noacl"},
82 {Opt_active_logs, "active_logs=%u"},
83 {Opt_disable_ext_identify, "disable_ext_identify"},
84 {Opt_inline_xattr, "inline_xattr"},
Changman Leeb1a94e82013-11-15 10:42:51 +090085 {Opt_inline_data, "inline_data"},
Jaegeuk Kime0cea842015-02-18 20:43:11 -060086 {Opt_inline_dentry, "inline_dentry"},
Evan McClainf3f030d2014-07-17 21:16:35 -040087 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -070088 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kime0cea842015-02-18 20:43:11 -060089 {Opt_fastboot, "fastboot"},
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -060090 {Opt_extent_cache, "extent_cache"},
91 {Opt_noextent_cache, "noextent_cache"},
92 {Opt_noinline_data, "noinline_data"},
Linus Torvalds8005ecc2012-12-20 13:54:51 -080093 {Opt_err, NULL},
94};
95
96/* Sysfs support for f2fs */
97enum {
98 GC_THREAD, /* struct f2fs_gc_thread */
99 SM_INFO, /* struct f2fs_sm_info */
Jaegeuk Kim327c57d2014-03-19 13:31:37 +0900100 NM_INFO, /* struct f2fs_nm_info */
Changman Leeb1a94e82013-11-15 10:42:51 +0900101 F2FS_SBI, /* struct f2fs_sb_info */
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800102};
103
104struct f2fs_attr {
105 struct attribute attr;
106 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
107 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
108 const char *, size_t);
109 int struct_type;
110 int offset;
111};
112
113static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
114{
115 if (struct_type == GC_THREAD)
116 return (unsigned char *)sbi->gc_thread;
117 else if (struct_type == SM_INFO)
118 return (unsigned char *)SM_I(sbi);
Jaegeuk Kim327c57d2014-03-19 13:31:37 +0900119 else if (struct_type == NM_INFO)
120 return (unsigned char *)NM_I(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900121 else if (struct_type == F2FS_SBI)
122 return (unsigned char *)sbi;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800123 return NULL;
124}
125
126static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
127 struct f2fs_sb_info *sbi, char *buf)
128{
129 unsigned char *ptr = NULL;
130 unsigned int *ui;
131
132 ptr = __struct_ptr(sbi, a->struct_type);
133 if (!ptr)
134 return -EINVAL;
135
136 ui = (unsigned int *)(ptr + a->offset);
137
138 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
139}
140
141static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
142 struct f2fs_sb_info *sbi,
143 const char *buf, size_t count)
144{
145 unsigned char *ptr;
146 unsigned long t;
147 unsigned int *ui;
148 ssize_t ret;
149
150 ptr = __struct_ptr(sbi, a->struct_type);
151 if (!ptr)
152 return -EINVAL;
153
154 ui = (unsigned int *)(ptr + a->offset);
155
156 ret = kstrtoul(skip_spaces(buf), 0, &t);
157 if (ret < 0)
158 return ret;
159 *ui = t;
160 return count;
161}
162
163static ssize_t f2fs_attr_show(struct kobject *kobj,
164 struct attribute *attr, char *buf)
165{
166 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
167 s_kobj);
168 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
169
170 return a->show ? a->show(a, sbi, buf) : 0;
171}
172
173static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
174 const char *buf, size_t len)
175{
176 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
177 s_kobj);
178 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
179
180 return a->store ? a->store(a, sbi, buf, len) : 0;
181}
182
183static void f2fs_sb_release(struct kobject *kobj)
184{
185 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
186 s_kobj);
187 complete(&sbi->s_kobj_unregister);
188}
189
190#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
191static struct f2fs_attr f2fs_attr_##_name = { \
192 .attr = {.name = __stringify(_name), .mode = _mode }, \
193 .show = _show, \
194 .store = _store, \
195 .struct_type = _struct_type, \
196 .offset = _offset \
197}
198
199#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
200 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
201 f2fs_sbi_show, f2fs_sbi_store, \
202 offsetof(struct struct_name, elname))
203
204F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
205F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
206F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
207F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
208F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
Changman Leeb1a94e82013-11-15 10:42:51 +0900209F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, max_small_discards, max_discards);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600210F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
Changman Leeb1a94e82013-11-15 10:42:51 +0900211F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
212F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600213F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
Jaegeuk Kim327c57d2014-03-19 13:31:37 +0900214F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
Changman Leeb1a94e82013-11-15 10:42:51 +0900215F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
Jaegeuk Kima753aba2014-02-27 20:09:05 +0900216F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600217F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, cp_interval);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800218
219#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
220static struct attribute *f2fs_attrs[] = {
221 ATTR_LIST(gc_min_sleep_time),
222 ATTR_LIST(gc_max_sleep_time),
223 ATTR_LIST(gc_no_gc_sleep_time),
224 ATTR_LIST(gc_idle),
225 ATTR_LIST(reclaim_segments),
Changman Leeb1a94e82013-11-15 10:42:51 +0900226 ATTR_LIST(max_small_discards),
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600227 ATTR_LIST(batched_trim_sections),
Changman Leeb1a94e82013-11-15 10:42:51 +0900228 ATTR_LIST(ipu_policy),
229 ATTR_LIST(min_ipu_util),
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600230 ATTR_LIST(min_fsync_blocks),
Changman Leeb1a94e82013-11-15 10:42:51 +0900231 ATTR_LIST(max_victim_search),
Jaegeuk Kima753aba2014-02-27 20:09:05 +0900232 ATTR_LIST(dir_level),
Jaegeuk Kim327c57d2014-03-19 13:31:37 +0900233 ATTR_LIST(ram_thresh),
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600234 ATTR_LIST(cp_interval),
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800235 NULL,
236};
237
238static const struct sysfs_ops f2fs_attr_ops = {
239 .show = f2fs_attr_show,
240 .store = f2fs_attr_store,
241};
242
243static struct kobj_type f2fs_ktype = {
244 .default_attrs = f2fs_attrs,
245 .sysfs_ops = &f2fs_attr_ops,
246 .release = f2fs_sb_release,
247};
248
249void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
250{
251 struct va_format vaf;
252 va_list args;
253
254 va_start(args, fmt);
255 vaf.fmt = fmt;
256 vaf.va = &args;
257 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
258 va_end(args);
259}
260
261static void init_once(void *foo)
262{
263 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
264
265 inode_init_once(&fi->vfs_inode);
266}
267
268static int parse_options(struct super_block *sb, char *options)
269{
270 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600271 struct request_queue *q;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800272 substring_t args[MAX_OPT_ARGS];
273 char *p, *name;
274 int arg = 0;
275
276 if (!options)
277 return 0;
278
279 while ((p = strsep(&options, ",")) != NULL) {
280 int token;
281 if (!*p)
282 continue;
283 /*
284 * Initialize args struct so we know whether arg was
285 * found; some options take optional arguments.
286 */
287 args[0].to = args[0].from = NULL;
288 token = match_token(p, f2fs_tokens, args);
289
290 switch (token) {
291 case Opt_gc_background:
292 name = match_strdup(&args[0]);
293
294 if (!name)
295 return -ENOMEM;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600296 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800297 set_opt(sbi, BG_GC);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600298 clear_opt(sbi, FORCE_FG_GC);
299 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800300 clear_opt(sbi, BG_GC);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600301 clear_opt(sbi, FORCE_FG_GC);
302 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
303 set_opt(sbi, BG_GC);
304 set_opt(sbi, FORCE_FG_GC);
305 } else {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800306 kfree(name);
307 return -EINVAL;
308 }
309 kfree(name);
310 break;
311 case Opt_disable_roll_forward:
312 set_opt(sbi, DISABLE_ROLL_FORWARD);
313 break;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600314 case Opt_norecovery:
315 /* this option mounts f2fs with ro */
316 set_opt(sbi, DISABLE_ROLL_FORWARD);
317 if (!f2fs_readonly(sb))
318 return -EINVAL;
319 break;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800320 case Opt_discard:
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600321 q = bdev_get_queue(sb->s_bdev);
322 if (blk_queue_discard(q)) {
323 set_opt(sbi, DISCARD);
324 } else {
325 f2fs_msg(sb, KERN_WARNING,
326 "mounting with \"discard\" option, but "
327 "the device does not support discard");
328 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800329 break;
330 case Opt_noheap:
331 set_opt(sbi, NOHEAP);
332 break;
333#ifdef CONFIG_F2FS_FS_XATTR
334 case Opt_user_xattr:
335 set_opt(sbi, XATTR_USER);
336 break;
337 case Opt_nouser_xattr:
338 clear_opt(sbi, XATTR_USER);
339 break;
340 case Opt_inline_xattr:
341 set_opt(sbi, INLINE_XATTR);
342 break;
343#else
344 case Opt_user_xattr:
345 f2fs_msg(sb, KERN_INFO,
346 "user_xattr options not supported");
347 break;
348 case Opt_nouser_xattr:
349 f2fs_msg(sb, KERN_INFO,
350 "nouser_xattr options not supported");
351 break;
352 case Opt_inline_xattr:
353 f2fs_msg(sb, KERN_INFO,
354 "inline_xattr options not supported");
355 break;
356#endif
357#ifdef CONFIG_F2FS_FS_POSIX_ACL
358 case Opt_acl:
359 set_opt(sbi, POSIX_ACL);
360 break;
361 case Opt_noacl:
362 clear_opt(sbi, POSIX_ACL);
363 break;
364#else
365 case Opt_acl:
366 f2fs_msg(sb, KERN_INFO, "acl options not supported");
367 break;
368 case Opt_noacl:
369 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
370 break;
371#endif
372 case Opt_active_logs:
373 if (args->from && match_int(args, &arg))
374 return -EINVAL;
375 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
376 return -EINVAL;
377 sbi->active_logs = arg;
378 break;
379 case Opt_disable_ext_identify:
380 set_opt(sbi, DISABLE_EXT_IDENTIFY);
381 break;
Changman Leeb1a94e82013-11-15 10:42:51 +0900382 case Opt_inline_data:
383 set_opt(sbi, INLINE_DATA);
384 break;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600385 case Opt_inline_dentry:
386 set_opt(sbi, INLINE_DENTRY);
387 break;
Evan McClainf3f030d2014-07-17 21:16:35 -0400388 case Opt_flush_merge:
389 set_opt(sbi, FLUSH_MERGE);
390 break;
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700391 case Opt_nobarrier:
392 set_opt(sbi, NOBARRIER);
393 break;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600394 case Opt_fastboot:
395 set_opt(sbi, FASTBOOT);
396 break;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600397 case Opt_extent_cache:
398 set_opt(sbi, EXTENT_CACHE);
399 break;
400 case Opt_noextent_cache:
401 clear_opt(sbi, EXTENT_CACHE);
402 break;
403 case Opt_noinline_data:
404 clear_opt(sbi, INLINE_DATA);
405 break;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800406 default:
407 f2fs_msg(sb, KERN_ERR,
408 "Unrecognized mount option \"%s\" or missing value",
409 p);
410 return -EINVAL;
411 }
412 }
413 return 0;
414}
415
416static struct inode *f2fs_alloc_inode(struct super_block *sb)
417{
418 struct f2fs_inode_info *fi;
419
Changman Leeb1a94e82013-11-15 10:42:51 +0900420 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800421 if (!fi)
422 return NULL;
423
424 init_once((void *) fi);
425
Evan McClainf3f030d2014-07-17 21:16:35 -0400426 /* Initialize f2fs-specific inode info */
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800427 fi->vfs_inode.i_version = 1;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600428 atomic_set(&fi->dirty_pages, 0);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800429 fi->i_current_depth = 1;
430 fi->i_advise = 0;
Jaegeuk Kimee722542014-03-20 19:10:08 +0900431 init_rwsem(&fi->i_sem);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600432 INIT_LIST_HEAD(&fi->inmem_pages);
433 mutex_init(&fi->inmem_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800434
435 set_inode_flag(fi, FI_NEW_INODE);
436
437 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
438 set_inode_flag(fi, FI_INLINE_XATTR);
439
Jaegeuk Kima753aba2014-02-27 20:09:05 +0900440 /* Will be used by directory only */
441 fi->i_dir_level = F2FS_SB(sb)->dir_level;
442
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600443#ifdef CONFIG_F2FS_FS_ENCRYPTION
444 fi->i_crypt_info = NULL;
445#endif
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800446 return &fi->vfs_inode;
447}
448
449static int f2fs_drop_inode(struct inode *inode)
450{
451 /*
452 * This is to avoid a deadlock condition like below.
453 * writeback_single_inode(inode)
454 * - f2fs_write_data_page
455 * - f2fs_gc -> iput -> evict
456 * - inode_wait_for_writeback(inode)
457 */
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600458 if (!inode_unhashed(inode) && inode->i_state & I_SYNC) {
459 if (!inode->i_nlink && !is_bad_inode(inode)) {
460 /* to avoid evict_inode call simultaneously */
461 atomic_inc(&inode->i_count);
462 spin_unlock(&inode->i_lock);
463
464 /* some remained atomic pages should discarded */
465 if (f2fs_is_atomic_file(inode))
466 commit_inmem_pages(inode, true);
467
468 i_size_write(inode, 0);
469
470 if (F2FS_HAS_BLOCKS(inode))
471 f2fs_truncate(inode, true);
472
473#ifdef CONFIG_F2FS_FS_ENCRYPTION
474 if (F2FS_I(inode)->i_crypt_info)
475 f2fs_free_encryption_info(inode,
476 F2FS_I(inode)->i_crypt_info);
477#endif
478 spin_lock(&inode->i_lock);
479 atomic_dec(&inode->i_count);
480 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800481 return 0;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600482 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800483 return generic_drop_inode(inode);
484}
485
486/*
487 * f2fs_dirty_inode() is called from __mark_inode_dirty()
488 *
489 * We should call set_dirty_inode to write the dirty inode through write_inode.
490 */
491static void f2fs_dirty_inode(struct inode *inode, int flags)
492{
493 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
494}
495
496static void f2fs_i_callback(struct rcu_head *head)
497{
498 struct inode *inode = container_of(head, struct inode, i_rcu);
499 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
500}
501
502static void f2fs_destroy_inode(struct inode *inode)
503{
504 call_rcu(&inode->i_rcu, f2fs_i_callback);
505}
506
507static void f2fs_put_super(struct super_block *sb)
508{
509 struct f2fs_sb_info *sbi = F2FS_SB(sb);
510
511 if (sbi->s_proc) {
512 remove_proc_entry("segment_info", sbi->s_proc);
513 remove_proc_entry(sb->s_id, f2fs_proc_root);
514 }
515 kobject_del(&sbi->s_kobj);
516
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800517 stop_gc_thread(sbi);
518
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600519 /* prevent remaining shrinker jobs */
520 mutex_lock(&sbi->umount_mutex);
521
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600522 /*
523 * We don't need to do checkpoint when superblock is clean.
524 * But, the previous checkpoint was not done by umount, it needs to do
525 * clean checkpoint again.
526 */
527 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
528 !is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
529 struct cp_control cpc = {
530 .reason = CP_UMOUNT,
531 };
532 write_checkpoint(sbi, &cpc);
533 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800534
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600535 /* write_checkpoint can update stat informaion */
536 f2fs_destroy_stats(sbi);
537
Jaegeuk Kim5da13f32014-08-11 18:37:46 -0700538 /*
539 * normally superblock is clean, so we need to release this.
540 * In addition, EIO will skip do checkpoint, we need this as well.
541 */
Jaegeuk Kimf2c07cd2014-08-19 09:48:22 -0700542 release_dirty_inode(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600543 release_discard_addrs(sbi);
Jaegeuk Kimf2c07cd2014-08-19 09:48:22 -0700544
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600545 f2fs_leave_shrinker(sbi);
546 mutex_unlock(&sbi->umount_mutex);
547
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800548 iput(sbi->node_inode);
549 iput(sbi->meta_inode);
550
551 /* destroy f2fs internal modules */
552 destroy_node_manager(sbi);
553 destroy_segment_manager(sbi);
554
555 kfree(sbi->ckpt);
556 kobject_put(&sbi->s_kobj);
557 wait_for_completion(&sbi->s_kobj_unregister);
558
559 sb->s_fs_info = NULL;
560 brelse(sbi->raw_super_buf);
561 kfree(sbi);
562}
563
564int f2fs_sync_fs(struct super_block *sb, int sync)
565{
566 struct f2fs_sb_info *sbi = F2FS_SB(sb);
567
568 trace_f2fs_sync_fs(sb, sync);
569
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800570 if (sync) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600571 struct cp_control cpc;
572
573 cpc.reason = __get_cp_reason(sbi);
574
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800575 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600576 write_checkpoint(sbi, &cpc);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800577 mutex_unlock(&sbi->gc_mutex);
578 } else {
579 f2fs_balance_fs(sbi);
580 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600581 f2fs_trace_ios(NULL, 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800582
583 return 0;
584}
585
Evan McClainf3f030d2014-07-17 21:16:35 -0400586static int f2fs_freeze(struct super_block *sb)
587{
588 int err;
589
590 if (f2fs_readonly(sb))
591 return 0;
592
593 err = f2fs_sync_fs(sb, 1);
594 return err;
595}
596
597static int f2fs_unfreeze(struct super_block *sb)
598{
599 return 0;
600}
601
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800602static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
603{
604 struct super_block *sb = dentry->d_sb;
605 struct f2fs_sb_info *sbi = F2FS_SB(sb);
606 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
607 block_t total_count, user_block_count, start_count, ovp_count;
608
609 total_count = le64_to_cpu(sbi->raw_super->block_count);
610 user_block_count = sbi->user_block_count;
611 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
612 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
613 buf->f_type = F2FS_SUPER_MAGIC;
614 buf->f_bsize = sbi->blocksize;
615
616 buf->f_blocks = total_count - start_count;
617 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
618 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
619
Chao Yu7ec82c42014-08-20 18:36:46 +0800620 buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
621 buf->f_ffree = buf->f_files - valid_inode_count(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800622
623 buf->f_namelen = F2FS_NAME_LEN;
624 buf->f_fsid.val[0] = (u32)id;
625 buf->f_fsid.val[1] = (u32)(id >> 32);
626
627 return 0;
628}
629
630static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
631{
632 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
633
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600634 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
635 if (test_opt(sbi, FORCE_FG_GC))
636 seq_printf(seq, ",background_gc=%s", "sync");
637 else
638 seq_printf(seq, ",background_gc=%s", "on");
639 } else {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800640 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600641 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800642 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
643 seq_puts(seq, ",disable_roll_forward");
644 if (test_opt(sbi, DISCARD))
645 seq_puts(seq, ",discard");
646 if (test_opt(sbi, NOHEAP))
647 seq_puts(seq, ",no_heap_alloc");
648#ifdef CONFIG_F2FS_FS_XATTR
649 if (test_opt(sbi, XATTR_USER))
650 seq_puts(seq, ",user_xattr");
651 else
652 seq_puts(seq, ",nouser_xattr");
653 if (test_opt(sbi, INLINE_XATTR))
654 seq_puts(seq, ",inline_xattr");
655#endif
656#ifdef CONFIG_F2FS_FS_POSIX_ACL
657 if (test_opt(sbi, POSIX_ACL))
658 seq_puts(seq, ",acl");
659 else
660 seq_puts(seq, ",noacl");
661#endif
662 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
663 seq_puts(seq, ",disable_ext_identify");
Changman Leeb1a94e82013-11-15 10:42:51 +0900664 if (test_opt(sbi, INLINE_DATA))
665 seq_puts(seq, ",inline_data");
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600666 else
667 seq_puts(seq, ",noinline_data");
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600668 if (test_opt(sbi, INLINE_DENTRY))
669 seq_puts(seq, ",inline_dentry");
Evan McClainf3f030d2014-07-17 21:16:35 -0400670 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
671 seq_puts(seq, ",flush_merge");
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700672 if (test_opt(sbi, NOBARRIER))
673 seq_puts(seq, ",nobarrier");
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600674 if (test_opt(sbi, FASTBOOT))
675 seq_puts(seq, ",fastboot");
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600676 if (test_opt(sbi, EXTENT_CACHE))
677 seq_puts(seq, ",extent_cache");
678 else
679 seq_puts(seq, ",noextent_cache");
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800680 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
681
682 return 0;
683}
684
685static int segment_info_seq_show(struct seq_file *seq, void *offset)
686{
687 struct super_block *sb = seq->private;
688 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Changman Leeb1a94e82013-11-15 10:42:51 +0900689 unsigned int total_segs =
690 le32_to_cpu(sbi->raw_super->segment_count_main);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800691 int i;
692
Chao Yufee2a042014-03-17 10:31:06 +0800693 seq_puts(seq, "format: segment_type|valid_blocks\n"
694 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
695
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800696 for (i = 0; i < total_segs; i++) {
Chao Yufee2a042014-03-17 10:31:06 +0800697 struct seg_entry *se = get_seg_entry(sbi, i);
698
699 if ((i % 10) == 0)
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600700 seq_printf(seq, "%-10d", i);
Chao Yufee2a042014-03-17 10:31:06 +0800701 seq_printf(seq, "%d|%-3u", se->type,
702 get_valid_blocks(sbi, i, 1));
Gu Zheng13039f62014-03-07 18:43:33 +0800703 if ((i % 10) == 9 || i == (total_segs - 1))
704 seq_putc(seq, '\n');
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800705 else
Gu Zheng13039f62014-03-07 18:43:33 +0800706 seq_putc(seq, ' ');
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800707 }
Gu Zheng13039f62014-03-07 18:43:33 +0800708
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800709 return 0;
710}
711
712static int segment_info_open_fs(struct inode *inode, struct file *file)
713{
Evan McClain1a808fb2014-03-20 09:12:16 -0400714 return single_open(file, segment_info_seq_show, PDE(inode)->data);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800715}
716
717static const struct file_operations f2fs_seq_segment_info_fops = {
718 .owner = THIS_MODULE,
719 .open = segment_info_open_fs,
720 .read = seq_read,
721 .llseek = seq_lseek,
722 .release = single_release,
723};
724
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600725static void default_options(struct f2fs_sb_info *sbi)
726{
727 /* init some FS parameters */
728 sbi->active_logs = NR_CURSEG_TYPE;
729
730 set_opt(sbi, BG_GC);
731 set_opt(sbi, INLINE_DATA);
732 set_opt(sbi, EXTENT_CACHE);
733
734#ifdef CONFIG_F2FS_FS_XATTR
735 set_opt(sbi, XATTR_USER);
736#endif
737#ifdef CONFIG_F2FS_FS_POSIX_ACL
738 set_opt(sbi, POSIX_ACL);
739#endif
740}
741
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800742static int f2fs_remount(struct super_block *sb, int *flags, char *data)
743{
744 struct f2fs_sb_info *sbi = F2FS_SB(sb);
745 struct f2fs_mount_info org_mount_opt;
746 int err, active_logs;
Evan McClainf3f030d2014-07-17 21:16:35 -0400747 bool need_restart_gc = false;
748 bool need_stop_gc = false;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600749 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Evan McClainf3f030d2014-07-17 21:16:35 -0400750
751 sync_filesystem(sb);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800752
753 /*
754 * Save the old mount options in case we
755 * need to restore them.
756 */
757 org_mount_opt = sbi->mount_opt;
758 active_logs = sbi->active_logs;
759
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600760 sbi->mount_opt.opt = 0;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600761 default_options(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600762
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800763 /* parse mount options */
764 err = parse_options(sb, data);
765 if (err)
766 goto restore_opts;
767
768 /*
769 * Previous and new state of filesystem is RO,
Evan McClainf3f030d2014-07-17 21:16:35 -0400770 * so skip checking GC and FLUSH_MERGE conditions.
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800771 */
Evan McClainf3f030d2014-07-17 21:16:35 -0400772 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800773 goto skip;
774
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600775 /* disallow enable/disable extent_cache dynamically */
776 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
777 err = -EINVAL;
778 f2fs_msg(sbi->sb, KERN_WARNING,
779 "switch extent_cache option is not allowed");
780 goto restore_opts;
781 }
782
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800783 /*
784 * We stop the GC thread if FS is mounted as RO
785 * or if background_gc = off is passed in mount
786 * option. Also sync the filesystem.
787 */
788 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
789 if (sbi->gc_thread) {
790 stop_gc_thread(sbi);
791 f2fs_sync_fs(sb, 1);
Evan McClainf3f030d2014-07-17 21:16:35 -0400792 need_restart_gc = true;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800793 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600794 } else if (!sbi->gc_thread) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800795 err = start_gc_thread(sbi);
796 if (err)
797 goto restore_opts;
Evan McClainf3f030d2014-07-17 21:16:35 -0400798 need_stop_gc = true;
799 }
800
801 /*
802 * We stop issue flush thread if FS is mounted as RO
803 * or if flush_merge is not passed in mount option.
804 */
805 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
806 destroy_flush_cmd_control(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600807 } else if (!SM_I(sbi)->cmd_control_info) {
Evan McClainf3f030d2014-07-17 21:16:35 -0400808 err = create_flush_cmd_control(sbi);
809 if (err)
810 goto restore_gc;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800811 }
812skip:
813 /* Update the POSIXACL Flag */
814 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
815 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
816 return 0;
Evan McClainf3f030d2014-07-17 21:16:35 -0400817restore_gc:
818 if (need_restart_gc) {
819 if (start_gc_thread(sbi))
820 f2fs_msg(sbi->sb, KERN_WARNING,
arter97f4081402014-08-06 23:22:50 +0900821 "background gc thread has stopped");
Evan McClainf3f030d2014-07-17 21:16:35 -0400822 } else if (need_stop_gc) {
823 stop_gc_thread(sbi);
824 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800825restore_opts:
826 sbi->mount_opt = org_mount_opt;
827 sbi->active_logs = active_logs;
828 return err;
829}
830
831static struct super_operations f2fs_sops = {
832 .alloc_inode = f2fs_alloc_inode,
833 .drop_inode = f2fs_drop_inode,
834 .destroy_inode = f2fs_destroy_inode,
835 .write_inode = f2fs_write_inode,
836 .dirty_inode = f2fs_dirty_inode,
837 .show_options = f2fs_show_options,
838 .evict_inode = f2fs_evict_inode,
839 .put_super = f2fs_put_super,
840 .sync_fs = f2fs_sync_fs,
Evan McClainf3f030d2014-07-17 21:16:35 -0400841 .freeze_fs = f2fs_freeze,
842 .unfreeze_fs = f2fs_unfreeze,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800843 .statfs = f2fs_statfs,
844 .remount_fs = f2fs_remount,
845};
846
847static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
848 u64 ino, u32 generation)
849{
850 struct f2fs_sb_info *sbi = F2FS_SB(sb);
851 struct inode *inode;
852
Chao Yuada8ce22014-06-12 13:23:41 +0800853 if (check_nid_range(sbi, ino))
Chao Yu4f9d66d2014-03-12 17:08:36 +0800854 return ERR_PTR(-ESTALE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800855
856 /*
857 * f2fs_iget isn't quite right if the inode is currently unallocated!
858 * However f2fs_iget currently does appropriate checks to handle stale
859 * inodes so everything is OK.
860 */
861 inode = f2fs_iget(sb, ino);
862 if (IS_ERR(inode))
863 return ERR_CAST(inode);
Changman Leeb1a94e82013-11-15 10:42:51 +0900864 if (unlikely(generation && inode->i_generation != generation)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800865 /* we didn't find the right inode.. */
866 iput(inode);
867 return ERR_PTR(-ESTALE);
868 }
869 return inode;
870}
871
872static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
873 int fh_len, int fh_type)
874{
875 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
876 f2fs_nfs_get_inode);
877}
878
879static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
880 int fh_len, int fh_type)
881{
882 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
883 f2fs_nfs_get_inode);
884}
885
886static const struct export_operations f2fs_export_ops = {
887 .fh_to_dentry = f2fs_fh_to_dentry,
888 .fh_to_parent = f2fs_fh_to_parent,
889 .get_parent = f2fs_get_parent,
890};
891
892static loff_t max_file_size(unsigned bits)
893{
894 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
895 loff_t leaf_count = ADDRS_PER_BLOCK;
896
897 /* two direct node blocks */
898 result += (leaf_count * 2);
899
900 /* two indirect node blocks */
901 leaf_count *= NIDS_PER_BLOCK;
902 result += (leaf_count * 2);
903
904 /* one double indirect node block */
905 leaf_count *= NIDS_PER_BLOCK;
906 result += leaf_count;
907
908 result <<= bits;
909 return result;
910}
911
912static int sanity_check_raw_super(struct super_block *sb,
913 struct f2fs_super_block *raw_super)
914{
915 unsigned int blocksize;
916
917 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
918 f2fs_msg(sb, KERN_INFO,
919 "Magic Mismatch, valid(0x%x) - read(0x%x)",
920 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
921 return 1;
922 }
923
924 /* Currently, support only 4KB page cache size */
925 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
926 f2fs_msg(sb, KERN_INFO,
927 "Invalid page_cache_size (%lu), supports only 4KB\n",
928 PAGE_CACHE_SIZE);
929 return 1;
930 }
931
932 /* Currently, support only 4KB block size */
933 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
934 if (blocksize != F2FS_BLKSIZE) {
935 f2fs_msg(sb, KERN_INFO,
936 "Invalid blocksize (%u), supports only 4KB\n",
937 blocksize);
938 return 1;
939 }
940
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600941 /* Currently, support 512/1024/2048/4096 bytes sector size */
942 if (le32_to_cpu(raw_super->log_sectorsize) >
943 F2FS_MAX_LOG_SECTOR_SIZE ||
944 le32_to_cpu(raw_super->log_sectorsize) <
945 F2FS_MIN_LOG_SECTOR_SIZE) {
946 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
947 le32_to_cpu(raw_super->log_sectorsize));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800948 return 1;
949 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600950 if (le32_to_cpu(raw_super->log_sectors_per_block) +
951 le32_to_cpu(raw_super->log_sectorsize) !=
952 F2FS_MAX_LOG_SECTOR_SIZE) {
953 f2fs_msg(sb, KERN_INFO,
954 "Invalid log sectors per block(%u) log sectorsize(%u)",
955 le32_to_cpu(raw_super->log_sectors_per_block),
956 le32_to_cpu(raw_super->log_sectorsize));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800957 return 1;
958 }
959 return 0;
960}
961
962static int sanity_check_ckpt(struct f2fs_sb_info *sbi)
963{
964 unsigned int total, fsmeta;
965 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
966 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
967
968 total = le32_to_cpu(raw_super->segment_count);
969 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
970 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
971 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
972 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
973 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
974
Changman Leeb1a94e82013-11-15 10:42:51 +0900975 if (unlikely(fsmeta >= total))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800976 return 1;
977
Jaegeuk Kim3ba29472014-08-11 16:49:25 -0700978 if (unlikely(f2fs_cp_error(sbi))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800979 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
980 return 1;
981 }
982 return 0;
983}
984
985static void init_sb_info(struct f2fs_sb_info *sbi)
986{
987 struct f2fs_super_block *raw_super = sbi->raw_super;
988 int i;
989
990 sbi->log_sectors_per_block =
991 le32_to_cpu(raw_super->log_sectors_per_block);
992 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
993 sbi->blocksize = 1 << sbi->log_blocksize;
994 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
995 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
996 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
997 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
998 sbi->total_sections = le32_to_cpu(raw_super->section_count);
999 sbi->total_node_count =
1000 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1001 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1002 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1003 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1004 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
1005 sbi->cur_victim_sec = NULL_SECNO;
Changman Leeb1a94e82013-11-15 10:42:51 +09001006 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001007
1008 for (i = 0; i < NR_COUNT_TYPE; i++)
1009 atomic_set(&sbi->nr_pages[i], 0);
Jaegeuk Kima753aba2014-02-27 20:09:05 +09001010
1011 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001012 sbi->cp_interval = DEF_CP_INTERVAL;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001013 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001014
1015 INIT_LIST_HEAD(&sbi->s_list);
1016 mutex_init(&sbi->umount_mutex);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001017}
1018
1019/*
1020 * Read f2fs raw super block.
1021 * Because we have two copies of super block, so read the first one at first,
1022 * if the first one is invalid, move to read the second one.
1023 */
1024static int read_raw_super_block(struct super_block *sb,
1025 struct f2fs_super_block **raw_super,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001026 struct buffer_head **raw_super_buf,
1027 int *recovery)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001028{
1029 int block = 0;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001030 struct buffer_head *buffer;
1031 struct f2fs_super_block *super;
1032 int err = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001033
1034retry:
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001035 buffer = sb_bread(sb, block);
1036 if (!buffer) {
1037 *recovery = 1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001038 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
1039 block + 1);
1040 if (block == 0) {
1041 block++;
1042 goto retry;
1043 } else {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001044 err = -EIO;
1045 goto out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001046 }
1047 }
1048
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001049 super = (struct f2fs_super_block *)
1050 ((char *)(buffer)->b_data + F2FS_SUPER_OFFSET);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001051
1052 /* sanity checking of raw super */
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001053 if (sanity_check_raw_super(sb, super)) {
1054 brelse(buffer);
1055 *recovery = 1;
Changman Leeb1a94e82013-11-15 10:42:51 +09001056 f2fs_msg(sb, KERN_ERR,
1057 "Can't find valid F2FS filesystem in %dth superblock",
1058 block + 1);
1059 if (block == 0) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001060 block++;
1061 goto retry;
1062 } else {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001063 err = -EINVAL;
1064 goto out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001065 }
1066 }
1067
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001068 if (!*raw_super) {
1069 *raw_super_buf = buffer;
1070 *raw_super = super;
1071 } else {
1072 /* already have a valid superblock */
1073 brelse(buffer);
1074 }
1075
1076 /* check the validity of the second superblock */
1077 if (block == 0) {
1078 block++;
1079 goto retry;
1080 }
1081
1082out:
1083 /* No valid superblock */
1084 if (!*raw_super)
1085 return err;
1086
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001087 return 0;
1088}
1089
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001090int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
1091{
1092 struct buffer_head *sbh = sbi->raw_super_buf;
1093 sector_t block = sbh->b_blocknr;
1094 int err;
1095
1096 /* write back-up superblock first */
1097 sbh->b_blocknr = block ? 0 : 1;
1098 mark_buffer_dirty(sbh);
1099 err = sync_dirty_buffer(sbh);
1100
1101 sbh->b_blocknr = block;
1102
1103 /* if we are in recovery path, skip writing valid superblock */
1104 if (recover || err)
1105 goto out;
1106
1107 /* write current valid superblock */
1108 mark_buffer_dirty(sbh);
1109 err = sync_dirty_buffer(sbh);
1110out:
1111 clear_buffer_write_io_error(sbh);
1112 set_buffer_uptodate(sbh);
1113 return err;
1114}
1115
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001116static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1117{
1118 struct f2fs_sb_info *sbi;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001119 struct f2fs_super_block *raw_super;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001120 struct buffer_head *raw_super_buf;
1121 struct inode *root;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001122 long err;
1123 bool retry = true, need_fsck = false;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001124 char *options = NULL;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001125 int recovery, i;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001126
Jaegeuk Kim16e77e12014-08-08 15:37:41 -07001127try_onemore:
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001128 err = -EINVAL;
1129 raw_super = NULL;
1130 raw_super_buf = NULL;
1131 recovery = 0;
1132
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001133 /* allocate memory for f2fs-specific super block info */
1134 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1135 if (!sbi)
1136 return -ENOMEM;
1137
1138 /* set a block size */
Changman Leeb1a94e82013-11-15 10:42:51 +09001139 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001140 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
1141 goto free_sbi;
1142 }
1143
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001144 err = read_raw_super_block(sb, &raw_super, &raw_super_buf, &recovery);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001145 if (err)
1146 goto free_sbi;
1147
1148 sb->s_fs_info = sbi;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001149 default_options(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001150 /* parse mount options */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001151 options = kstrdup((const char *)data, GFP_KERNEL);
1152 if (data && !options) {
1153 err = -ENOMEM;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001154 goto free_sb_buf;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001155 }
1156
1157 err = parse_options(sb, options);
1158 if (err)
1159 goto free_options;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001160
1161 sb->s_maxbytes = max_file_size(le32_to_cpu(raw_super->log_blocksize));
1162 sb->s_max_links = F2FS_LINK_MAX;
1163 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1164
1165 sb->s_op = &f2fs_sops;
1166 sb->s_xattr = f2fs_xattr_handlers;
1167 sb->s_export_op = &f2fs_export_ops;
1168 sb->s_magic = F2FS_SUPER_MAGIC;
1169 sb->s_time_gran = 1;
1170 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1171 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
1172 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
1173
1174 /* init f2fs-specific super block info */
1175 sbi->sb = sb;
1176 sbi->raw_super = raw_super;
1177 sbi->raw_super_buf = raw_super_buf;
1178 mutex_init(&sbi->gc_mutex);
1179 mutex_init(&sbi->writepages);
1180 mutex_init(&sbi->cp_mutex);
Chao Yu0b14d472014-07-03 18:58:39 +08001181 init_rwsem(&sbi->node_write);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001182
1183 /* disallow all the data/node/meta page writes */
1184 set_sbi_flag(sbi, SBI_POR_DOING);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001185 spin_lock_init(&sbi->stat_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +09001186
Evan McClainf3f030d2014-07-17 21:16:35 -04001187 init_rwsem(&sbi->read_io.io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +09001188 sbi->read_io.sbi = sbi;
1189 sbi->read_io.bio = NULL;
1190 for (i = 0; i < NR_PAGE_TYPE; i++) {
Evan McClainf3f030d2014-07-17 21:16:35 -04001191 init_rwsem(&sbi->write_io[i].io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +09001192 sbi->write_io[i].sbi = sbi;
1193 sbi->write_io[i].bio = NULL;
1194 }
1195
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001196 init_rwsem(&sbi->cp_rwsem);
1197 init_waitqueue_head(&sbi->cp_wait);
1198 init_sb_info(sbi);
1199
1200 /* get an inode for meta space */
1201 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1202 if (IS_ERR(sbi->meta_inode)) {
1203 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
1204 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001205 goto free_options;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001206 }
1207
1208 err = get_valid_checkpoint(sbi);
1209 if (err) {
1210 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
1211 goto free_meta_inode;
1212 }
1213
1214 /* sanity checking of checkpoint */
1215 err = -EINVAL;
1216 if (sanity_check_ckpt(sbi)) {
1217 f2fs_msg(sb, KERN_ERR, "Invalid F2FS checkpoint");
1218 goto free_cp;
1219 }
1220
1221 sbi->total_valid_node_count =
1222 le32_to_cpu(sbi->ckpt->valid_node_count);
1223 sbi->total_valid_inode_count =
1224 le32_to_cpu(sbi->ckpt->valid_inode_count);
1225 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1226 sbi->total_valid_block_count =
1227 le64_to_cpu(sbi->ckpt->valid_block_count);
1228 sbi->last_valid_block_count = sbi->total_valid_block_count;
1229 sbi->alloc_valid_block_count = 0;
1230 INIT_LIST_HEAD(&sbi->dir_inode_list);
1231 spin_lock_init(&sbi->dir_inode_lock);
1232
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001233 init_extent_cache_info(sbi);
1234
Jaegeuk Kima6377a62014-07-25 15:47:17 -07001235 init_ino_entry_info(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001236
1237 /* setup f2fs internal modules */
1238 err = build_segment_manager(sbi);
1239 if (err) {
1240 f2fs_msg(sb, KERN_ERR,
1241 "Failed to initialize F2FS segment manager");
1242 goto free_sm;
1243 }
1244 err = build_node_manager(sbi);
1245 if (err) {
1246 f2fs_msg(sb, KERN_ERR,
1247 "Failed to initialize F2FS node manager");
1248 goto free_nm;
1249 }
1250
1251 build_gc_manager(sbi);
1252
1253 /* get an inode for node space */
1254 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1255 if (IS_ERR(sbi->node_inode)) {
1256 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
1257 err = PTR_ERR(sbi->node_inode);
1258 goto free_nm;
1259 }
1260
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001261 f2fs_join_shrinker(sbi);
1262
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001263 /* if there are nt orphan nodes free them */
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001264 err = recover_orphan_inodes(sbi);
1265 if (err)
1266 goto free_node_inode;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001267
1268 /* read root inode and dentry */
1269 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1270 if (IS_ERR(root)) {
1271 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
1272 err = PTR_ERR(root);
1273 goto free_node_inode;
1274 }
Changman Leeb1a94e82013-11-15 10:42:51 +09001275 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu7384b162014-07-25 12:55:09 +08001276 iput(root);
Changman Leeb1a94e82013-11-15 10:42:51 +09001277 err = -EINVAL;
Chao Yu7384b162014-07-25 12:55:09 +08001278 goto free_node_inode;
Changman Leeb1a94e82013-11-15 10:42:51 +09001279 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001280
1281 sb->s_root = d_make_root(root); /* allocate root dentry */
1282 if (!sb->s_root) {
1283 err = -ENOMEM;
1284 goto free_root_inode;
1285 }
1286
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001287 err = f2fs_build_stats(sbi);
1288 if (err)
Jaegeuk Kim07264102014-02-19 18:23:32 +09001289 goto free_root_inode;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001290
1291 if (f2fs_proc_root)
1292 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1293
1294 if (sbi->s_proc)
1295 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
1296 &f2fs_seq_segment_info_fops, sb);
1297
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001298 sbi->s_kobj.kset = f2fs_kset;
1299 init_completion(&sbi->s_kobj_unregister);
1300 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
1301 "%s", sb->s_id);
1302 if (err)
Jaegeuk Kim07264102014-02-19 18:23:32 +09001303 goto free_proc;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001304
Jaegeuk Kim07264102014-02-19 18:23:32 +09001305 /* recover fsynced data */
1306 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001307 /*
1308 * mount should be failed, when device has readonly mode, and
1309 * previous checkpoint was not done by clean system shutdown.
1310 */
1311 if (bdev_read_only(sb->s_bdev) &&
1312 !is_set_ckpt_flags(sbi->ckpt, CP_UMOUNT_FLAG)) {
1313 err = -EROFS;
1314 goto free_kobj;
1315 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001316
1317 if (need_fsck)
1318 set_sbi_flag(sbi, SBI_NEED_FSCK);
1319
Jaegeuk Kim07264102014-02-19 18:23:32 +09001320 err = recover_fsync_data(sbi);
Jaegeuk Kim16e77e12014-08-08 15:37:41 -07001321 if (err) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001322 need_fsck = true;
Jaegeuk Kim07264102014-02-19 18:23:32 +09001323 f2fs_msg(sb, KERN_ERR,
1324 "Cannot recover all fsync data errno=%ld", err);
Jaegeuk Kim16e77e12014-08-08 15:37:41 -07001325 goto free_kobj;
1326 }
Jaegeuk Kim07264102014-02-19 18:23:32 +09001327 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001328 /* recover_fsync_data() cleared this already */
1329 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim07264102014-02-19 18:23:32 +09001330
1331 /*
1332 * If filesystem is not mounted as read-only then
1333 * do start the gc_thread.
1334 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001335 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim07264102014-02-19 18:23:32 +09001336 /* After POR, we can run background GC thread.*/
1337 err = start_gc_thread(sbi);
1338 if (err)
1339 goto free_kobj;
1340 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001341 kfree(options);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001342
1343 /* recover broken superblock */
1344 if (recovery && !f2fs_readonly(sb) && !bdev_read_only(sb->s_bdev)) {
1345 f2fs_msg(sb, KERN_INFO, "Recover invalid superblock");
1346 f2fs_commit_super(sbi, true);
1347 }
1348
1349 sbi->cp_expires = round_jiffies_up(jiffies);
1350
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001351 return 0;
Jaegeuk Kim07264102014-02-19 18:23:32 +09001352
1353free_kobj:
1354 kobject_del(&sbi->s_kobj);
1355free_proc:
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001356 if (sbi->s_proc) {
1357 remove_proc_entry("segment_info", sbi->s_proc);
1358 remove_proc_entry(sb->s_id, f2fs_proc_root);
1359 }
1360 f2fs_destroy_stats(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001361free_root_inode:
1362 dput(sb->s_root);
1363 sb->s_root = NULL;
1364free_node_inode:
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001365 mutex_lock(&sbi->umount_mutex);
1366 f2fs_leave_shrinker(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001367 iput(sbi->node_inode);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001368 mutex_unlock(&sbi->umount_mutex);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001369free_nm:
1370 destroy_node_manager(sbi);
1371free_sm:
1372 destroy_segment_manager(sbi);
1373free_cp:
1374 kfree(sbi->ckpt);
1375free_meta_inode:
1376 make_bad_inode(sbi->meta_inode);
1377 iput(sbi->meta_inode);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001378free_options:
1379 kfree(options);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001380free_sb_buf:
1381 brelse(raw_super_buf);
1382free_sbi:
1383 kfree(sbi);
Jaegeuk Kim16e77e12014-08-08 15:37:41 -07001384
1385 /* give only one another chance */
1386 if (retry) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001387 retry = false;
Jaegeuk Kim16e77e12014-08-08 15:37:41 -07001388 shrink_dcache_sb(sb);
1389 goto try_onemore;
1390 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001391 return err;
1392}
1393
1394static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1395 const char *dev_name, void *data)
1396{
1397 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1398}
1399
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001400static void kill_f2fs_super(struct super_block *sb)
1401{
1402 if (sb->s_root)
1403 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
1404 kill_block_super(sb);
1405}
1406
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001407static struct file_system_type f2fs_fs_type = {
1408 .owner = THIS_MODULE,
1409 .name = "f2fs",
1410 .mount = f2fs_mount,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001411 .kill_sb = kill_f2fs_super,
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001412 .fs_flags = FS_REQUIRES_DEV,
1413};
1414
1415static int __init init_inodecache(void)
1416{
1417 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
Gu Zhenge33dcea2014-03-07 18:43:28 +08001418 sizeof(struct f2fs_inode_info));
Changman Leeb1a94e82013-11-15 10:42:51 +09001419 if (!f2fs_inode_cachep)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001420 return -ENOMEM;
1421 return 0;
1422}
1423
1424static void destroy_inodecache(void)
1425{
1426 /*
1427 * Make sure all delayed rcu free inodes are flushed before we
1428 * destroy cache.
1429 */
1430 rcu_barrier();
1431 kmem_cache_destroy(f2fs_inode_cachep);
1432}
1433
1434static int __init init_f2fs_fs(void)
1435{
1436 int err;
1437
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001438 f2fs_build_trace_ios();
1439
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001440 err = init_inodecache();
1441 if (err)
1442 goto fail;
1443 err = create_node_manager_caches();
1444 if (err)
1445 goto free_inodecache;
Changman Leeb1a94e82013-11-15 10:42:51 +09001446 err = create_segment_manager_caches();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001447 if (err)
1448 goto free_node_manager_caches;
1449 err = create_checkpoint_caches();
1450 if (err)
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001451 goto free_segment_manager_caches;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001452 err = create_extent_cache();
1453 if (err)
1454 goto free_checkpoint_caches;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001455 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
1456 if (!f2fs_kset) {
1457 err = -ENOMEM;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001458 goto free_extent_cache;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001459 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001460 err = f2fs_init_crypto();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001461 if (err)
1462 goto free_kset;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001463
1464 register_shrinker(&f2fs_shrinker_info);
1465
1466 err = register_filesystem(&f2fs_fs_type);
1467 if (err)
1468 goto free_shrinker;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001469 f2fs_create_root_stats();
1470 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1471 return 0;
1472
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001473free_shrinker:
1474 unregister_shrinker(&f2fs_shrinker_info);
1475 f2fs_exit_crypto();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001476free_kset:
1477 kset_unregister(f2fs_kset);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001478free_extent_cache:
1479 destroy_extent_cache();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001480free_checkpoint_caches:
1481 destroy_checkpoint_caches();
Changman Leeb1a94e82013-11-15 10:42:51 +09001482free_segment_manager_caches:
1483 destroy_segment_manager_caches();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001484free_node_manager_caches:
1485 destroy_node_manager_caches();
1486free_inodecache:
1487 destroy_inodecache();
1488fail:
1489 return err;
1490}
1491
1492static void __exit exit_f2fs_fs(void)
1493{
1494 remove_proc_entry("fs/f2fs", NULL);
1495 f2fs_destroy_root_stats();
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001496 unregister_shrinker(&f2fs_shrinker_info);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001497 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001498 f2fs_exit_crypto();
1499 destroy_extent_cache();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001500 destroy_checkpoint_caches();
Changman Leeb1a94e82013-11-15 10:42:51 +09001501 destroy_segment_manager_caches();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001502 destroy_node_manager_caches();
1503 destroy_inodecache();
1504 kset_unregister(f2fs_kset);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001505 f2fs_destroy_trace_ios();
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001506}
1507
1508module_init(init_f2fs_fs)
1509module_exit(exit_f2fs_fs)
1510
1511MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1512MODULE_DESCRIPTION("Flash Friendly File System");
1513MODULE_LICENSE("GPL");