blob: 13e5efaca164ac69728b726d8098025d9571a266 [file] [log] [blame]
Jaegeuk Kim315f4552015-11-29 09:25:08 -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"
33#include "trace.h"
34
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
42/* f2fs-wide shrinker description */
43static struct shrinker f2fs_shrinker_info = {
44 .shrink = f2fs_shrink_scan,
45 .seeks = DEFAULT_SEEKS,
46};
47
48enum {
49 Opt_gc_background,
50 Opt_disable_roll_forward,
51 Opt_norecovery,
52 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,
61 Opt_inline_data,
62 Opt_inline_dentry,
63 Opt_flush_merge,
64 Opt_nobarrier,
65 Opt_fastboot,
66 Opt_extent_cache,
67 Opt_noextent_cache,
68 Opt_noinline_data,
Chao Yu45c1c142015-12-16 13:12:16 +080069 Opt_data_flush,
Jaegeuk Kim315f4552015-11-29 09:25:08 -080070 Opt_err,
71};
72
73static match_table_t f2fs_tokens = {
74 {Opt_gc_background, "background_gc=%s"},
75 {Opt_disable_roll_forward, "disable_roll_forward"},
76 {Opt_norecovery, "norecovery"},
77 {Opt_discard, "discard"},
78 {Opt_noheap, "no_heap"},
79 {Opt_user_xattr, "user_xattr"},
80 {Opt_nouser_xattr, "nouser_xattr"},
81 {Opt_acl, "acl"},
82 {Opt_noacl, "noacl"},
83 {Opt_active_logs, "active_logs=%u"},
84 {Opt_disable_ext_identify, "disable_ext_identify"},
85 {Opt_inline_xattr, "inline_xattr"},
86 {Opt_inline_data, "inline_data"},
87 {Opt_inline_dentry, "inline_dentry"},
88 {Opt_flush_merge, "flush_merge"},
89 {Opt_nobarrier, "nobarrier"},
90 {Opt_fastboot, "fastboot"},
91 {Opt_extent_cache, "extent_cache"},
92 {Opt_noextent_cache, "noextent_cache"},
93 {Opt_noinline_data, "noinline_data"},
Chao Yu45c1c142015-12-16 13:12:16 +080094 {Opt_data_flush, "data_flush"},
Jaegeuk Kim315f4552015-11-29 09:25:08 -080095 {Opt_err, NULL},
96};
97
98/* Sysfs support for f2fs */
99enum {
100 GC_THREAD, /* struct f2fs_gc_thread */
101 SM_INFO, /* struct f2fs_sm_info */
102 NM_INFO, /* struct f2fs_nm_info */
103 F2FS_SBI, /* struct f2fs_sb_info */
104};
105
106struct f2fs_attr {
107 struct attribute attr;
108 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
109 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
110 const char *, size_t);
111 int struct_type;
112 int offset;
113};
114
115static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
116{
117 if (struct_type == GC_THREAD)
118 return (unsigned char *)sbi->gc_thread;
119 else if (struct_type == SM_INFO)
120 return (unsigned char *)SM_I(sbi);
121 else if (struct_type == NM_INFO)
122 return (unsigned char *)NM_I(sbi);
123 else if (struct_type == F2FS_SBI)
124 return (unsigned char *)sbi;
125 return NULL;
126}
127
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800128static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
129 struct f2fs_sb_info *sbi, char *buf)
130{
131 struct super_block *sb = sbi->sb;
132
133 if (!sb->s_bdev->bd_part)
134 return snprintf(buf, PAGE_SIZE, "0\n");
135
136 return snprintf(buf, PAGE_SIZE, "%llu\n",
137 (unsigned long long)(sbi->kbytes_written +
138 BD_PART_WRITTEN(sbi)));
139}
140
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800141static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
142 struct f2fs_sb_info *sbi, char *buf)
143{
144 unsigned char *ptr = NULL;
145 unsigned int *ui;
146
147 ptr = __struct_ptr(sbi, a->struct_type);
148 if (!ptr)
149 return -EINVAL;
150
151 ui = (unsigned int *)(ptr + a->offset);
152
153 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
154}
155
156static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
157 struct f2fs_sb_info *sbi,
158 const char *buf, size_t count)
159{
160 unsigned char *ptr;
161 unsigned long t;
162 unsigned int *ui;
163 ssize_t ret;
164
165 ptr = __struct_ptr(sbi, a->struct_type);
166 if (!ptr)
167 return -EINVAL;
168
169 ui = (unsigned int *)(ptr + a->offset);
170
171 ret = kstrtoul(skip_spaces(buf), 0, &t);
172 if (ret < 0)
173 return ret;
174 *ui = t;
175 return count;
176}
177
178static ssize_t f2fs_attr_show(struct kobject *kobj,
179 struct attribute *attr, char *buf)
180{
181 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
182 s_kobj);
183 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
184
185 return a->show ? a->show(a, sbi, buf) : 0;
186}
187
188static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
189 const char *buf, size_t len)
190{
191 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
192 s_kobj);
193 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
194
195 return a->store ? a->store(a, sbi, buf, len) : 0;
196}
197
198static void f2fs_sb_release(struct kobject *kobj)
199{
200 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
201 s_kobj);
202 complete(&sbi->s_kobj_unregister);
203}
204
205#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
206static struct f2fs_attr f2fs_attr_##_name = { \
207 .attr = {.name = __stringify(_name), .mode = _mode }, \
208 .show = _show, \
209 .store = _store, \
210 .struct_type = _struct_type, \
211 .offset = _offset \
212}
213
214#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
215 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
216 f2fs_sbi_show, f2fs_sbi_store, \
217 offsetof(struct struct_name, elname))
218
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800219#define F2FS_GENERAL_RO_ATTR(name) \
220static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
221
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800222F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
223F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
224F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
225F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
226F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
227F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, max_small_discards, max_discards);
228F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
229F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
230F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
231F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
232F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
233F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
Chao Yu69e1bba2016-01-18 18:32:58 +0800234F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800235F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
236F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
Jaegeuk Kimdedfbf82016-01-08 15:51:50 -0800237F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
Jaegeuk Kim2c88a922016-01-08 16:57:48 -0800238F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800239F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800240
241#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
242static struct attribute *f2fs_attrs[] = {
243 ATTR_LIST(gc_min_sleep_time),
244 ATTR_LIST(gc_max_sleep_time),
245 ATTR_LIST(gc_no_gc_sleep_time),
246 ATTR_LIST(gc_idle),
247 ATTR_LIST(reclaim_segments),
248 ATTR_LIST(max_small_discards),
249 ATTR_LIST(batched_trim_sections),
250 ATTR_LIST(ipu_policy),
251 ATTR_LIST(min_ipu_util),
252 ATTR_LIST(min_fsync_blocks),
253 ATTR_LIST(max_victim_search),
254 ATTR_LIST(dir_level),
255 ATTR_LIST(ram_thresh),
256 ATTR_LIST(ra_nid_pages),
Chao Yu69e1bba2016-01-18 18:32:58 +0800257 ATTR_LIST(dirty_nats_ratio),
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800258 ATTR_LIST(cp_interval),
Jaegeuk Kim2c88a922016-01-08 16:57:48 -0800259 ATTR_LIST(idle_interval),
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800260 ATTR_LIST(lifetime_write_kbytes),
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800261 NULL,
262};
263
264static const struct sysfs_ops f2fs_attr_ops = {
265 .show = f2fs_attr_show,
266 .store = f2fs_attr_store,
267};
268
269static struct kobj_type f2fs_ktype = {
270 .default_attrs = f2fs_attrs,
271 .sysfs_ops = &f2fs_attr_ops,
272 .release = f2fs_sb_release,
273};
274
275void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
276{
277 struct va_format vaf;
278 va_list args;
279
280 va_start(args, fmt);
281 vaf.fmt = fmt;
282 vaf.va = &args;
283 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
284 va_end(args);
285}
286
287static void init_once(void *foo)
288{
289 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
290
291 inode_init_once(&fi->vfs_inode);
292}
293
294static int parse_options(struct super_block *sb, char *options)
295{
296 struct f2fs_sb_info *sbi = F2FS_SB(sb);
297 struct request_queue *q;
298 substring_t args[MAX_OPT_ARGS];
299 char *p, *name;
300 int arg = 0;
301
302 if (!options)
303 return 0;
304
305 while ((p = strsep(&options, ",")) != NULL) {
306 int token;
307 if (!*p)
308 continue;
309 /*
310 * Initialize args struct so we know whether arg was
311 * found; some options take optional arguments.
312 */
313 args[0].to = args[0].from = NULL;
314 token = match_token(p, f2fs_tokens, args);
315
316 switch (token) {
317 case Opt_gc_background:
318 name = match_strdup(&args[0]);
319
320 if (!name)
321 return -ENOMEM;
322 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
323 set_opt(sbi, BG_GC);
324 clear_opt(sbi, FORCE_FG_GC);
325 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
326 clear_opt(sbi, BG_GC);
327 clear_opt(sbi, FORCE_FG_GC);
328 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
329 set_opt(sbi, BG_GC);
330 set_opt(sbi, FORCE_FG_GC);
331 } else {
332 kfree(name);
333 return -EINVAL;
334 }
335 kfree(name);
336 break;
337 case Opt_disable_roll_forward:
338 set_opt(sbi, DISABLE_ROLL_FORWARD);
339 break;
340 case Opt_norecovery:
341 /* this option mounts f2fs with ro */
342 set_opt(sbi, DISABLE_ROLL_FORWARD);
343 if (!f2fs_readonly(sb))
344 return -EINVAL;
345 break;
346 case Opt_discard:
347 q = bdev_get_queue(sb->s_bdev);
348 if (blk_queue_discard(q)) {
349 set_opt(sbi, DISCARD);
350 } else {
351 f2fs_msg(sb, KERN_WARNING,
352 "mounting with \"discard\" option, but "
353 "the device does not support discard");
354 }
355 break;
356 case Opt_noheap:
357 set_opt(sbi, NOHEAP);
358 break;
359#ifdef CONFIG_F2FS_FS_XATTR
360 case Opt_user_xattr:
361 set_opt(sbi, XATTR_USER);
362 break;
363 case Opt_nouser_xattr:
364 clear_opt(sbi, XATTR_USER);
365 break;
366 case Opt_inline_xattr:
367 set_opt(sbi, INLINE_XATTR);
368 break;
369#else
370 case Opt_user_xattr:
371 f2fs_msg(sb, KERN_INFO,
372 "user_xattr options not supported");
373 break;
374 case Opt_nouser_xattr:
375 f2fs_msg(sb, KERN_INFO,
376 "nouser_xattr options not supported");
377 break;
378 case Opt_inline_xattr:
379 f2fs_msg(sb, KERN_INFO,
380 "inline_xattr options not supported");
381 break;
382#endif
383#ifdef CONFIG_F2FS_FS_POSIX_ACL
384 case Opt_acl:
385 set_opt(sbi, POSIX_ACL);
386 break;
387 case Opt_noacl:
388 clear_opt(sbi, POSIX_ACL);
389 break;
390#else
391 case Opt_acl:
392 f2fs_msg(sb, KERN_INFO, "acl options not supported");
393 break;
394 case Opt_noacl:
395 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
396 break;
397#endif
398 case Opt_active_logs:
399 if (args->from && match_int(args, &arg))
400 return -EINVAL;
401 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
402 return -EINVAL;
403 sbi->active_logs = arg;
404 break;
405 case Opt_disable_ext_identify:
406 set_opt(sbi, DISABLE_EXT_IDENTIFY);
407 break;
408 case Opt_inline_data:
409 set_opt(sbi, INLINE_DATA);
410 break;
411 case Opt_inline_dentry:
412 set_opt(sbi, INLINE_DENTRY);
413 break;
414 case Opt_flush_merge:
415 set_opt(sbi, FLUSH_MERGE);
416 break;
417 case Opt_nobarrier:
418 set_opt(sbi, NOBARRIER);
419 break;
420 case Opt_fastboot:
421 set_opt(sbi, FASTBOOT);
422 break;
423 case Opt_extent_cache:
424 set_opt(sbi, EXTENT_CACHE);
425 break;
426 case Opt_noextent_cache:
427 clear_opt(sbi, EXTENT_CACHE);
428 break;
429 case Opt_noinline_data:
430 clear_opt(sbi, INLINE_DATA);
431 break;
Chao Yu45c1c142015-12-16 13:12:16 +0800432 case Opt_data_flush:
433 set_opt(sbi, DATA_FLUSH);
434 break;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800435 default:
436 f2fs_msg(sb, KERN_ERR,
437 "Unrecognized mount option \"%s\" or missing value",
438 p);
439 return -EINVAL;
440 }
441 }
442 return 0;
443}
444
445static struct inode *f2fs_alloc_inode(struct super_block *sb)
446{
447 struct f2fs_inode_info *fi;
448
449 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
450 if (!fi)
451 return NULL;
452
453 init_once((void *) fi);
454
455 /* Initialize f2fs-specific inode info */
456 fi->vfs_inode.i_version = 1;
457 atomic_set(&fi->dirty_pages, 0);
458 fi->i_current_depth = 1;
459 fi->i_advise = 0;
460 init_rwsem(&fi->i_sem);
Chao Yubc2a2142015-12-15 13:30:45 +0800461 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800462 INIT_LIST_HEAD(&fi->inmem_pages);
463 mutex_init(&fi->inmem_lock);
464
465 set_inode_flag(fi, FI_NEW_INODE);
466
467 if (test_opt(F2FS_SB(sb), INLINE_XATTR))
468 set_inode_flag(fi, FI_INLINE_XATTR);
469
470 /* Will be used by directory only */
471 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800472 return &fi->vfs_inode;
473}
474
475static int f2fs_drop_inode(struct inode *inode)
476{
477 /*
478 * This is to avoid a deadlock condition like below.
479 * writeback_single_inode(inode)
480 * - f2fs_write_data_page
481 * - f2fs_gc -> iput -> evict
482 * - inode_wait_for_writeback(inode)
483 */
484 if (!inode_unhashed(inode) && inode->i_state & I_SYNC) {
485 if (!inode->i_nlink && !is_bad_inode(inode)) {
486 /* to avoid evict_inode call simultaneously */
487 atomic_inc(&inode->i_count);
488 spin_unlock(&inode->i_lock);
489
490 /* some remained atomic pages should discarded */
491 if (f2fs_is_atomic_file(inode))
Chao Yub99c7432016-02-06 14:38:29 +0800492 drop_inmem_pages(inode);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800493
494 i_size_write(inode, 0);
495
496 if (F2FS_HAS_BLOCKS(inode))
497 f2fs_truncate(inode, true);
498
Jaegeuk Kim86e0d582015-05-15 16:26:10 -0700499 fscrypt_put_encryption_info(inode, NULL);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800500 spin_lock(&inode->i_lock);
501 atomic_dec(&inode->i_count);
502 }
503 return 0;
504 }
505 return generic_drop_inode(inode);
506}
507
508/*
509 * f2fs_dirty_inode() is called from __mark_inode_dirty()
510 *
511 * We should call set_dirty_inode to write the dirty inode through write_inode.
512 */
513static void f2fs_dirty_inode(struct inode *inode, int flags)
514{
515 set_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
516}
517
518static void f2fs_i_callback(struct rcu_head *head)
519{
520 struct inode *inode = container_of(head, struct inode, i_rcu);
521 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
522}
523
524static void f2fs_destroy_inode(struct inode *inode)
525{
526 call_rcu(&inode->i_rcu, f2fs_i_callback);
527}
528
529static void f2fs_put_super(struct super_block *sb)
530{
531 struct f2fs_sb_info *sbi = F2FS_SB(sb);
532
533 if (sbi->s_proc) {
534 remove_proc_entry("segment_info", sbi->s_proc);
535 remove_proc_entry(sb->s_id, f2fs_proc_root);
536 }
537 kobject_del(&sbi->s_kobj);
538
539 stop_gc_thread(sbi);
540
541 /* prevent remaining shrinker jobs */
542 mutex_lock(&sbi->umount_mutex);
543
544 /*
545 * We don't need to do checkpoint when superblock is clean.
546 * But, the previous checkpoint was not done by umount, it needs to do
547 * clean checkpoint again.
548 */
549 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
550 !is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
551 struct cp_control cpc = {
552 .reason = CP_UMOUNT,
553 };
554 write_checkpoint(sbi, &cpc);
555 }
556
557 /* write_checkpoint can update stat informaion */
558 f2fs_destroy_stats(sbi);
559
560 /*
561 * normally superblock is clean, so we need to release this.
562 * In addition, EIO will skip do checkpoint, we need this as well.
563 */
Chao Yudbc32c22015-12-15 13:29:47 +0800564 release_ino_entry(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800565 release_discard_addrs(sbi);
566
567 f2fs_leave_shrinker(sbi);
568 mutex_unlock(&sbi->umount_mutex);
569
Jaegeuk Kim19e927c2016-01-29 08:57:59 -0800570 /* our cp_error case, we can wait for any writeback page */
Chao Yuf8fb0a82016-02-24 17:17:55 +0800571 if (get_pages(sbi, F2FS_WRITEBACK))
572 f2fs_flush_merged_bios(sbi);
Jaegeuk Kim19e927c2016-01-29 08:57:59 -0800573
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800574 iput(sbi->node_inode);
575 iput(sbi->meta_inode);
576
577 /* destroy f2fs internal modules */
578 destroy_node_manager(sbi);
579 destroy_segment_manager(sbi);
580
581 kfree(sbi->ckpt);
582 kobject_put(&sbi->s_kobj);
583 wait_for_completion(&sbi->s_kobj_unregister);
584
585 sb->s_fs_info = NULL;
Yunlei He33e79272015-12-15 17:17:20 +0800586 kfree(sbi->raw_super);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800587 kfree(sbi);
588}
589
590int f2fs_sync_fs(struct super_block *sb, int sync)
591{
592 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yub962dc12015-12-23 17:50:30 +0800593 int err = 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800594
595 trace_f2fs_sync_fs(sb, sync);
596
597 if (sync) {
598 struct cp_control cpc;
599
600 cpc.reason = __get_cp_reason(sbi);
601
602 mutex_lock(&sbi->gc_mutex);
Chao Yub962dc12015-12-23 17:50:30 +0800603 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800604 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800605 }
606 f2fs_trace_ios(NULL, 1);
607
Chao Yub962dc12015-12-23 17:50:30 +0800608 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800609}
610
611static int f2fs_freeze(struct super_block *sb)
612{
613 int err;
614
615 if (f2fs_readonly(sb))
616 return 0;
617
618 err = f2fs_sync_fs(sb, 1);
619 return err;
620}
621
622static int f2fs_unfreeze(struct super_block *sb)
623{
624 return 0;
625}
626
627static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
628{
629 struct super_block *sb = dentry->d_sb;
630 struct f2fs_sb_info *sbi = F2FS_SB(sb);
631 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
632 block_t total_count, user_block_count, start_count, ovp_count;
633
634 total_count = le64_to_cpu(sbi->raw_super->block_count);
635 user_block_count = sbi->user_block_count;
636 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
637 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
638 buf->f_type = F2FS_SUPER_MAGIC;
639 buf->f_bsize = sbi->blocksize;
640
641 buf->f_blocks = total_count - start_count;
642 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
643 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
644
645 buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
646 buf->f_ffree = buf->f_files - valid_inode_count(sbi);
647
648 buf->f_namelen = F2FS_NAME_LEN;
649 buf->f_fsid.val[0] = (u32)id;
650 buf->f_fsid.val[1] = (u32)(id >> 32);
651
652 return 0;
653}
654
655static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
656{
657 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
658
659 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
660 if (test_opt(sbi, FORCE_FG_GC))
661 seq_printf(seq, ",background_gc=%s", "sync");
662 else
663 seq_printf(seq, ",background_gc=%s", "on");
664 } else {
665 seq_printf(seq, ",background_gc=%s", "off");
666 }
667 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
668 seq_puts(seq, ",disable_roll_forward");
669 if (test_opt(sbi, DISCARD))
670 seq_puts(seq, ",discard");
671 if (test_opt(sbi, NOHEAP))
672 seq_puts(seq, ",no_heap_alloc");
673#ifdef CONFIG_F2FS_FS_XATTR
674 if (test_opt(sbi, XATTR_USER))
675 seq_puts(seq, ",user_xattr");
676 else
677 seq_puts(seq, ",nouser_xattr");
678 if (test_opt(sbi, INLINE_XATTR))
679 seq_puts(seq, ",inline_xattr");
680#endif
681#ifdef CONFIG_F2FS_FS_POSIX_ACL
682 if (test_opt(sbi, POSIX_ACL))
683 seq_puts(seq, ",acl");
684 else
685 seq_puts(seq, ",noacl");
686#endif
687 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
688 seq_puts(seq, ",disable_ext_identify");
689 if (test_opt(sbi, INLINE_DATA))
690 seq_puts(seq, ",inline_data");
691 else
692 seq_puts(seq, ",noinline_data");
693 if (test_opt(sbi, INLINE_DENTRY))
694 seq_puts(seq, ",inline_dentry");
695 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
696 seq_puts(seq, ",flush_merge");
697 if (test_opt(sbi, NOBARRIER))
698 seq_puts(seq, ",nobarrier");
699 if (test_opt(sbi, FASTBOOT))
700 seq_puts(seq, ",fastboot");
701 if (test_opt(sbi, EXTENT_CACHE))
702 seq_puts(seq, ",extent_cache");
703 else
704 seq_puts(seq, ",noextent_cache");
Chao Yu45c1c142015-12-16 13:12:16 +0800705 if (test_opt(sbi, DATA_FLUSH))
706 seq_puts(seq, ",data_flush");
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800707 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
708
709 return 0;
710}
711
712static int segment_info_seq_show(struct seq_file *seq, void *offset)
713{
714 struct super_block *sb = seq->private;
715 struct f2fs_sb_info *sbi = F2FS_SB(sb);
716 unsigned int total_segs =
717 le32_to_cpu(sbi->raw_super->segment_count_main);
718 int i;
719
720 seq_puts(seq, "format: segment_type|valid_blocks\n"
721 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
722
723 for (i = 0; i < total_segs; i++) {
724 struct seg_entry *se = get_seg_entry(sbi, i);
725
726 if ((i % 10) == 0)
727 seq_printf(seq, "%-10d", i);
728 seq_printf(seq, "%d|%-3u", se->type,
729 get_valid_blocks(sbi, i, 1));
730 if ((i % 10) == 9 || i == (total_segs - 1))
731 seq_putc(seq, '\n');
732 else
733 seq_putc(seq, ' ');
734 }
735
736 return 0;
737}
738
739static int segment_info_open_fs(struct inode *inode, struct file *file)
740{
741 return single_open(file, segment_info_seq_show, PDE(inode)->data);
742}
743
744static const struct file_operations f2fs_seq_segment_info_fops = {
745 .owner = THIS_MODULE,
746 .open = segment_info_open_fs,
747 .read = seq_read,
748 .llseek = seq_lseek,
749 .release = single_release,
750};
751
752static void default_options(struct f2fs_sb_info *sbi)
753{
754 /* init some FS parameters */
755 sbi->active_logs = NR_CURSEG_TYPE;
756
757 set_opt(sbi, BG_GC);
758 set_opt(sbi, INLINE_DATA);
759 set_opt(sbi, EXTENT_CACHE);
760
761#ifdef CONFIG_F2FS_FS_XATTR
762 set_opt(sbi, XATTR_USER);
763#endif
764#ifdef CONFIG_F2FS_FS_POSIX_ACL
765 set_opt(sbi, POSIX_ACL);
766#endif
767}
768
769static int f2fs_remount(struct super_block *sb, int *flags, char *data)
770{
771 struct f2fs_sb_info *sbi = F2FS_SB(sb);
772 struct f2fs_mount_info org_mount_opt;
773 int err, active_logs;
774 bool need_restart_gc = false;
775 bool need_stop_gc = false;
776 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
777
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800778 /*
779 * Save the old mount options in case we
780 * need to restore them.
781 */
782 org_mount_opt = sbi->mount_opt;
783 active_logs = sbi->active_logs;
784
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800785 if (*flags & MS_RDONLY) {
786 set_opt(sbi, FASTBOOT);
787 set_sbi_flag(sbi, SBI_IS_DIRTY);
788 }
789
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -0700790 /* recover superblocks we couldn't write due to previous RO mount */
791 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
792 err = f2fs_commit_super(sbi, false);
793 f2fs_msg(sb, KERN_INFO,
794 "Try to recover all the superblocks, ret: %d", err);
795 if (!err)
796 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
797 }
798
Shuoran Liufa8a1d72016-01-27 09:57:30 +0800799 sync_filesystem(sb);
800
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800801 sbi->mount_opt.opt = 0;
802 default_options(sbi);
803
804 /* parse mount options */
805 err = parse_options(sb, data);
806 if (err)
807 goto restore_opts;
808
809 /*
810 * Previous and new state of filesystem is RO,
811 * so skip checking GC and FLUSH_MERGE conditions.
812 */
813 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
814 goto skip;
815
816 /* disallow enable/disable extent_cache dynamically */
817 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
818 err = -EINVAL;
819 f2fs_msg(sbi->sb, KERN_WARNING,
820 "switch extent_cache option is not allowed");
821 goto restore_opts;
822 }
823
824 /*
825 * We stop the GC thread if FS is mounted as RO
826 * or if background_gc = off is passed in mount
827 * option. Also sync the filesystem.
828 */
829 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
830 if (sbi->gc_thread) {
831 stop_gc_thread(sbi);
832 f2fs_sync_fs(sb, 1);
833 need_restart_gc = true;
834 }
835 } else if (!sbi->gc_thread) {
836 err = start_gc_thread(sbi);
837 if (err)
838 goto restore_opts;
839 need_stop_gc = true;
840 }
841
842 /*
843 * We stop issue flush thread if FS is mounted as RO
844 * or if flush_merge is not passed in mount option.
845 */
846 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
847 destroy_flush_cmd_control(sbi);
848 } else if (!SM_I(sbi)->cmd_control_info) {
849 err = create_flush_cmd_control(sbi);
850 if (err)
851 goto restore_gc;
852 }
853skip:
854 /* Update the POSIXACL Flag */
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -0700855 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800856 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -0700857
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800858 return 0;
859restore_gc:
860 if (need_restart_gc) {
861 if (start_gc_thread(sbi))
862 f2fs_msg(sbi->sb, KERN_WARNING,
863 "background gc thread has stopped");
864 } else if (need_stop_gc) {
865 stop_gc_thread(sbi);
866 }
867restore_opts:
868 sbi->mount_opt = org_mount_opt;
869 sbi->active_logs = active_logs;
870 return err;
871}
872
873static struct super_operations f2fs_sops = {
874 .alloc_inode = f2fs_alloc_inode,
875 .drop_inode = f2fs_drop_inode,
876 .destroy_inode = f2fs_destroy_inode,
877 .write_inode = f2fs_write_inode,
878 .dirty_inode = f2fs_dirty_inode,
879 .show_options = f2fs_show_options,
880 .evict_inode = f2fs_evict_inode,
881 .put_super = f2fs_put_super,
882 .sync_fs = f2fs_sync_fs,
883 .freeze_fs = f2fs_freeze,
884 .unfreeze_fs = f2fs_unfreeze,
885 .statfs = f2fs_statfs,
886 .remount_fs = f2fs_remount,
887};
888
Jaegeuk Kim86e0d582015-05-15 16:26:10 -0700889#ifdef CONFIG_F2FS_FS_ENCRYPTION
890static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
891{
892 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
893 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
894 ctx, len, NULL);
895}
896
897static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
898 void *fs_data)
899{
900 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
901 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
902 ctx, len, fs_data, XATTR_CREATE);
903}
904
905static unsigned f2fs_max_namelen(struct inode *inode)
906{
907 return S_ISLNK(inode->i_mode) ?
908 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
909}
910
911static struct fscrypt_operations f2fs_cryptops = {
912 .get_context = f2fs_get_context,
913 .set_context = f2fs_set_context,
914 .is_encrypted = f2fs_encrypted_inode,
915 .empty_dir = f2fs_empty_dir,
916 .max_namelen = f2fs_max_namelen,
917};
918#else
919static struct fscrypt_operations f2fs_cryptops = {
920 .is_encrypted = f2fs_encrypted_inode,
921};
922#endif
923
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800924static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
925 u64 ino, u32 generation)
926{
927 struct f2fs_sb_info *sbi = F2FS_SB(sb);
928 struct inode *inode;
929
930 if (check_nid_range(sbi, ino))
931 return ERR_PTR(-ESTALE);
932
933 /*
934 * f2fs_iget isn't quite right if the inode is currently unallocated!
935 * However f2fs_iget currently does appropriate checks to handle stale
936 * inodes so everything is OK.
937 */
938 inode = f2fs_iget(sb, ino);
939 if (IS_ERR(inode))
940 return ERR_CAST(inode);
941 if (unlikely(generation && inode->i_generation != generation)) {
942 /* we didn't find the right inode.. */
943 iput(inode);
944 return ERR_PTR(-ESTALE);
945 }
946 return inode;
947}
948
949static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
950 int fh_len, int fh_type)
951{
952 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
953 f2fs_nfs_get_inode);
954}
955
956static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
957 int fh_len, int fh_type)
958{
959 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
960 f2fs_nfs_get_inode);
961}
962
963static const struct export_operations f2fs_export_ops = {
964 .fh_to_dentry = f2fs_fh_to_dentry,
965 .fh_to_parent = f2fs_fh_to_parent,
966 .get_parent = f2fs_get_parent,
967};
968
Chao Yu70dde032015-12-31 14:35:37 +0800969static loff_t max_file_blocks(void)
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800970{
971 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
972 loff_t leaf_count = ADDRS_PER_BLOCK;
973
974 /* two direct node blocks */
975 result += (leaf_count * 2);
976
977 /* two indirect node blocks */
978 leaf_count *= NIDS_PER_BLOCK;
979 result += (leaf_count * 2);
980
981 /* one double indirect node block */
982 leaf_count *= NIDS_PER_BLOCK;
983 result += leaf_count;
984
Jaegeuk Kim315f4552015-11-29 09:25:08 -0800985 return result;
986}
987
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -0700988static int __f2fs_commit_super(struct buffer_head *bh,
989 struct f2fs_super_block *super)
Chao Yufc2c7902015-12-15 09:58:18 +0800990{
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -0700991 lock_buffer(bh);
992 if (super)
993 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
994 set_buffer_uptodate(bh);
995 set_buffer_dirty(bh);
996 unlock_buffer(bh);
997
998 /* it's rare case, we can do fua all the time */
999 return __sync_dirty_buffer(bh, WRITE_FLUSH_FUA);
1000}
1001
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001002static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001003 struct buffer_head *bh)
1004{
1005 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1006 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001007 struct super_block *sb = sbi->sb;
Chao Yufc2c7902015-12-15 09:58:18 +08001008 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1009 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1010 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1011 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1012 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1013 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1014 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1015 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1016 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1017 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1018 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1019 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1020 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001021 u64 main_end_blkaddr = main_blkaddr +
1022 (segment_count_main << log_blocks_per_seg);
1023 u64 seg_end_blkaddr = segment0_blkaddr +
1024 (segment_count << log_blocks_per_seg);
Chao Yufc2c7902015-12-15 09:58:18 +08001025
1026 if (segment0_blkaddr != cp_blkaddr) {
1027 f2fs_msg(sb, KERN_INFO,
1028 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1029 segment0_blkaddr, cp_blkaddr);
1030 return true;
1031 }
1032
1033 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1034 sit_blkaddr) {
1035 f2fs_msg(sb, KERN_INFO,
1036 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1037 cp_blkaddr, sit_blkaddr,
1038 segment_count_ckpt << log_blocks_per_seg);
1039 return true;
1040 }
1041
1042 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1043 nat_blkaddr) {
1044 f2fs_msg(sb, KERN_INFO,
1045 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1046 sit_blkaddr, nat_blkaddr,
1047 segment_count_sit << log_blocks_per_seg);
1048 return true;
1049 }
1050
1051 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1052 ssa_blkaddr) {
1053 f2fs_msg(sb, KERN_INFO,
1054 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1055 nat_blkaddr, ssa_blkaddr,
1056 segment_count_nat << log_blocks_per_seg);
1057 return true;
1058 }
1059
1060 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1061 main_blkaddr) {
1062 f2fs_msg(sb, KERN_INFO,
1063 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1064 ssa_blkaddr, main_blkaddr,
1065 segment_count_ssa << log_blocks_per_seg);
1066 return true;
1067 }
1068
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001069 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yufc2c7902015-12-15 09:58:18 +08001070 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001071 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yufc2c7902015-12-15 09:58:18 +08001072 main_blkaddr,
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001073 segment0_blkaddr +
1074 (segment_count << log_blocks_per_seg),
Chao Yufc2c7902015-12-15 09:58:18 +08001075 segment_count_main << log_blocks_per_seg);
1076 return true;
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001077 } else if (main_end_blkaddr < seg_end_blkaddr) {
1078 int err = 0;
1079 char *res;
Chao Yufc2c7902015-12-15 09:58:18 +08001080
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001081 /* fix in-memory information all the time */
1082 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1083 segment0_blkaddr) >> log_blocks_per_seg);
1084
1085 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001086 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001087 res = "internally";
1088 } else {
1089 err = __f2fs_commit_super(bh, NULL);
1090 res = err ? "failed" : "done";
1091 }
1092 f2fs_msg(sb, KERN_INFO,
1093 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1094 res, main_blkaddr,
1095 segment0_blkaddr +
1096 (segment_count << log_blocks_per_seg),
1097 segment_count_main << log_blocks_per_seg);
1098 if (err)
1099 return true;
1100 }
Chao Yufc2c7902015-12-15 09:58:18 +08001101 return false;
1102}
1103
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001104static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001105 struct buffer_head *bh)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001106{
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001107 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1108 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001109 struct super_block *sb = sbi->sb;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001110 unsigned int blocksize;
1111
1112 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1113 f2fs_msg(sb, KERN_INFO,
1114 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1115 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
1116 return 1;
1117 }
1118
1119 /* Currently, support only 4KB page cache size */
1120 if (F2FS_BLKSIZE != PAGE_CACHE_SIZE) {
1121 f2fs_msg(sb, KERN_INFO,
1122 "Invalid page_cache_size (%lu), supports only 4KB\n",
1123 PAGE_CACHE_SIZE);
1124 return 1;
1125 }
1126
1127 /* Currently, support only 4KB block size */
1128 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
1129 if (blocksize != F2FS_BLKSIZE) {
1130 f2fs_msg(sb, KERN_INFO,
1131 "Invalid blocksize (%u), supports only 4KB\n",
1132 blocksize);
1133 return 1;
1134 }
1135
Chao Yufc2c7902015-12-15 09:58:18 +08001136 /* check log blocks per segment */
1137 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1138 f2fs_msg(sb, KERN_INFO,
1139 "Invalid log blocks per segment (%u)\n",
1140 le32_to_cpu(raw_super->log_blocks_per_seg));
1141 return 1;
1142 }
1143
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001144 /* Currently, support 512/1024/2048/4096 bytes sector size */
1145 if (le32_to_cpu(raw_super->log_sectorsize) >
1146 F2FS_MAX_LOG_SECTOR_SIZE ||
1147 le32_to_cpu(raw_super->log_sectorsize) <
1148 F2FS_MIN_LOG_SECTOR_SIZE) {
1149 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1150 le32_to_cpu(raw_super->log_sectorsize));
1151 return 1;
1152 }
1153 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1154 le32_to_cpu(raw_super->log_sectorsize) !=
1155 F2FS_MAX_LOG_SECTOR_SIZE) {
1156 f2fs_msg(sb, KERN_INFO,
1157 "Invalid log sectors per block(%u) log sectorsize(%u)",
1158 le32_to_cpu(raw_super->log_sectors_per_block),
1159 le32_to_cpu(raw_super->log_sectorsize));
1160 return 1;
1161 }
Chao Yufc2c7902015-12-15 09:58:18 +08001162
1163 /* check reserved ino info */
1164 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1165 le32_to_cpu(raw_super->meta_ino) != 2 ||
1166 le32_to_cpu(raw_super->root_ino) != 3) {
1167 f2fs_msg(sb, KERN_INFO,
1168 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1169 le32_to_cpu(raw_super->node_ino),
1170 le32_to_cpu(raw_super->meta_ino),
1171 le32_to_cpu(raw_super->root_ino));
1172 return 1;
1173 }
1174
1175 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001176 if (sanity_check_area_boundary(sbi, bh))
Chao Yufc2c7902015-12-15 09:58:18 +08001177 return 1;
1178
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001179 return 0;
1180}
1181
Shawn Lin65999d62016-02-17 11:26:32 +08001182int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001183{
1184 unsigned int total, fsmeta;
1185 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1186 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1187
1188 total = le32_to_cpu(raw_super->segment_count);
1189 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1190 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1191 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1192 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1193 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1194
1195 if (unlikely(fsmeta >= total))
1196 return 1;
1197
1198 if (unlikely(f2fs_cp_error(sbi))) {
1199 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1200 return 1;
1201 }
1202 return 0;
1203}
1204
1205static void init_sb_info(struct f2fs_sb_info *sbi)
1206{
1207 struct f2fs_super_block *raw_super = sbi->raw_super;
1208 int i;
1209
1210 sbi->log_sectors_per_block =
1211 le32_to_cpu(raw_super->log_sectors_per_block);
1212 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1213 sbi->blocksize = 1 << sbi->log_blocksize;
1214 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1215 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1216 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1217 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1218 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1219 sbi->total_node_count =
1220 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1221 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1222 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1223 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1224 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
1225 sbi->cur_victim_sec = NULL_SECNO;
1226 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
1227
1228 for (i = 0; i < NR_COUNT_TYPE; i++)
1229 atomic_set(&sbi->nr_pages[i], 0);
1230
1231 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kimdedfbf82016-01-08 15:51:50 -08001232 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kim2c88a922016-01-08 16:57:48 -08001233 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001234 clear_sbi_flag(sbi, SBI_NEED_FSCK);
1235
1236 INIT_LIST_HEAD(&sbi->s_list);
1237 mutex_init(&sbi->umount_mutex);
1238}
1239
1240/*
1241 * Read f2fs raw super block.
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001242 * Because we have two copies of super block, so read both of them
1243 * to get the first valid one. If any one of them is broken, we pass
1244 * them recovery flag back to the caller.
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001245 */
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001246static int read_raw_super_block(struct f2fs_sb_info *sbi,
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001247 struct f2fs_super_block **raw_super,
Chao Yuc9162be2015-12-15 17:19:26 +08001248 int *valid_super_block, int *recovery)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001249{
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001250 struct super_block *sb = sbi->sb;
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001251 int block;
Chao Yuc9162be2015-12-15 17:19:26 +08001252 struct buffer_head *bh;
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001253 struct f2fs_super_block *super;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001254 int err = 0;
1255
Yunlei He33e79272015-12-15 17:17:20 +08001256 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1257 if (!super)
1258 return -ENOMEM;
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001259
1260 for (block = 0; block < 2; block++) {
1261 bh = sb_bread(sb, block);
1262 if (!bh) {
1263 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001264 block + 1);
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001265 err = -EIO;
1266 continue;
1267 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001268
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001269 /* sanity checking of raw super */
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001270 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001271 f2fs_msg(sb, KERN_ERR,
1272 "Can't find valid F2FS filesystem in %dth superblock",
1273 block + 1);
1274 err = -EINVAL;
1275 brelse(bh);
1276 continue;
1277 }
1278
1279 if (!*raw_super) {
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001280 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1281 sizeof(*super));
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001282 *valid_super_block = block;
1283 *raw_super = super;
1284 }
Chao Yuc9162be2015-12-15 17:19:26 +08001285 brelse(bh);
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001286 }
1287
1288 /* Fail to read any one of the superblocks*/
1289 if (err < 0)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001290 *recovery = 1;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001291
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001292 /* No valid superblock */
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001293 if (!*raw_super)
Yunlei He33e79272015-12-15 17:17:20 +08001294 kfree(super);
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001295 else
1296 err = 0;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001297
Shawn Lin0d3a76d2016-02-17 08:59:01 +08001298 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001299}
1300
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001301int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001302{
Jaegeuk Kimfd10c5c2015-12-07 10:16:58 -08001303 struct buffer_head *bh;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001304 int err;
1305
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001306 if ((recover && f2fs_readonly(sbi->sb)) ||
1307 bdev_read_only(sbi->sb->s_bdev)) {
1308 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kim2ae41232016-03-23 10:42:01 -07001309 return -EROFS;
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001310 }
Jaegeuk Kim2ae41232016-03-23 10:42:01 -07001311
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001312 /* write back-up superblock first */
1313 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kimfd10c5c2015-12-07 10:16:58 -08001314 if (!bh)
1315 return -EIO;
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001316 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kimfd10c5c2015-12-07 10:16:58 -08001317 brelse(bh);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001318
1319 /* if we are in recovery path, skip writing valid superblock */
1320 if (recover || err)
Jaegeuk Kimfd10c5c2015-12-07 10:16:58 -08001321 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001322
Chao Yuc9162be2015-12-15 17:19:26 +08001323 /* write current valid superblock */
Jaegeuk Kim58a2aa72016-03-20 15:33:20 -07001324 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1325 if (!bh)
1326 return -EIO;
1327 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1328 brelse(bh);
1329 return err;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001330}
1331
1332static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1333{
1334 struct f2fs_sb_info *sbi;
1335 struct f2fs_super_block *raw_super;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001336 struct inode *root;
1337 long err;
1338 bool retry = true, need_fsck = false;
1339 char *options = NULL;
Chao Yuc9162be2015-12-15 17:19:26 +08001340 int recovery, i, valid_super_block;
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001341 struct curseg_info *seg_i;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001342
1343try_onemore:
1344 err = -EINVAL;
1345 raw_super = NULL;
Chao Yuc9162be2015-12-15 17:19:26 +08001346 valid_super_block = -1;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001347 recovery = 0;
1348
1349 /* allocate memory for f2fs-specific super block info */
1350 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1351 if (!sbi)
1352 return -ENOMEM;
1353
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001354 sbi->sb = sb;
1355
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001356 /* set a block size */
1357 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
1358 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
1359 goto free_sbi;
1360 }
1361
Jaegeuk Kim8fbda3d2016-03-23 17:05:27 -07001362 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yuc9162be2015-12-15 17:19:26 +08001363 &recovery);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001364 if (err)
1365 goto free_sbi;
1366
1367 sb->s_fs_info = sbi;
1368 default_options(sbi);
1369 /* parse mount options */
1370 options = kstrdup((const char *)data, GFP_KERNEL);
1371 if (data && !options) {
1372 err = -ENOMEM;
1373 goto free_sb_buf;
1374 }
1375
1376 err = parse_options(sb, options);
1377 if (err)
1378 goto free_options;
1379
Chao Yu70dde032015-12-31 14:35:37 +08001380 sbi->max_file_blocks = max_file_blocks();
1381 sb->s_maxbytes = sbi->max_file_blocks <<
1382 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001383 sb->s_max_links = F2FS_LINK_MAX;
1384 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1385
1386 sb->s_op = &f2fs_sops;
Jaegeuk Kim86e0d582015-05-15 16:26:10 -07001387 sb->s_cop = &f2fs_cryptops;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001388 sb->s_xattr = f2fs_xattr_handlers;
1389 sb->s_export_op = &f2fs_export_ops;
1390 sb->s_magic = F2FS_SUPER_MAGIC;
1391 sb->s_time_gran = 1;
1392 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1393 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
1394 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
1395
1396 /* init f2fs-specific super block info */
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001397 sbi->raw_super = raw_super;
Chao Yuc9162be2015-12-15 17:19:26 +08001398 sbi->valid_super_block = valid_super_block;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001399 mutex_init(&sbi->gc_mutex);
1400 mutex_init(&sbi->writepages);
1401 mutex_init(&sbi->cp_mutex);
1402 init_rwsem(&sbi->node_write);
1403
1404 /* disallow all the data/node/meta page writes */
1405 set_sbi_flag(sbi, SBI_POR_DOING);
1406 spin_lock_init(&sbi->stat_lock);
1407
1408 init_rwsem(&sbi->read_io.io_rwsem);
1409 sbi->read_io.sbi = sbi;
1410 sbi->read_io.bio = NULL;
1411 for (i = 0; i < NR_PAGE_TYPE; i++) {
1412 init_rwsem(&sbi->write_io[i].io_rwsem);
1413 sbi->write_io[i].sbi = sbi;
1414 sbi->write_io[i].bio = NULL;
1415 }
1416
1417 init_rwsem(&sbi->cp_rwsem);
1418 init_waitqueue_head(&sbi->cp_wait);
1419 init_sb_info(sbi);
1420
1421 /* get an inode for meta space */
1422 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1423 if (IS_ERR(sbi->meta_inode)) {
1424 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
1425 err = PTR_ERR(sbi->meta_inode);
1426 goto free_options;
1427 }
1428
1429 err = get_valid_checkpoint(sbi);
1430 if (err) {
1431 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
1432 goto free_meta_inode;
1433 }
1434
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001435 sbi->total_valid_node_count =
1436 le32_to_cpu(sbi->ckpt->valid_node_count);
1437 sbi->total_valid_inode_count =
1438 le32_to_cpu(sbi->ckpt->valid_inode_count);
1439 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1440 sbi->total_valid_block_count =
1441 le64_to_cpu(sbi->ckpt->valid_block_count);
1442 sbi->last_valid_block_count = sbi->total_valid_block_count;
1443 sbi->alloc_valid_block_count = 0;
Chao Yufb1825c2015-12-16 13:09:20 +08001444 for (i = 0; i < NR_INODE_TYPE; i++) {
1445 INIT_LIST_HEAD(&sbi->inode_list[i]);
1446 spin_lock_init(&sbi->inode_lock[i]);
1447 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001448
1449 init_extent_cache_info(sbi);
1450
1451 init_ino_entry_info(sbi);
1452
1453 /* setup f2fs internal modules */
1454 err = build_segment_manager(sbi);
1455 if (err) {
1456 f2fs_msg(sb, KERN_ERR,
1457 "Failed to initialize F2FS segment manager");
1458 goto free_sm;
1459 }
1460 err = build_node_manager(sbi);
1461 if (err) {
1462 f2fs_msg(sb, KERN_ERR,
1463 "Failed to initialize F2FS node manager");
1464 goto free_nm;
1465 }
1466
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001467 /* For write statistics */
1468 if (sb->s_bdev->bd_part)
1469 sbi->sectors_written_start =
1470 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1471
1472 /* Read accumulated write IO statistics if exists */
1473 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1474 if (__exist_node_summaries(sbi))
1475 sbi->kbytes_written =
Shuoran Liu6f4d7ee2016-03-29 18:00:15 +08001476 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liufa8a1d72016-01-27 09:57:30 +08001477
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001478 build_gc_manager(sbi);
1479
1480 /* get an inode for node space */
1481 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1482 if (IS_ERR(sbi->node_inode)) {
1483 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
1484 err = PTR_ERR(sbi->node_inode);
1485 goto free_nm;
1486 }
1487
1488 f2fs_join_shrinker(sbi);
1489
1490 /* if there are nt orphan nodes free them */
1491 err = recover_orphan_inodes(sbi);
1492 if (err)
1493 goto free_node_inode;
1494
1495 /* read root inode and dentry */
1496 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1497 if (IS_ERR(root)) {
1498 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
1499 err = PTR_ERR(root);
1500 goto free_node_inode;
1501 }
1502 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1503 iput(root);
1504 err = -EINVAL;
1505 goto free_node_inode;
1506 }
1507
1508 sb->s_root = d_make_root(root); /* allocate root dentry */
1509 if (!sb->s_root) {
1510 err = -ENOMEM;
1511 goto free_root_inode;
1512 }
1513
1514 err = f2fs_build_stats(sbi);
1515 if (err)
1516 goto free_root_inode;
1517
1518 if (f2fs_proc_root)
1519 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1520
1521 if (sbi->s_proc)
1522 proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
1523 &f2fs_seq_segment_info_fops, sb);
1524
1525 sbi->s_kobj.kset = f2fs_kset;
1526 init_completion(&sbi->s_kobj_unregister);
1527 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_ktype, NULL,
1528 "%s", sb->s_id);
1529 if (err)
1530 goto free_proc;
1531
1532 /* recover fsynced data */
1533 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
1534 /*
1535 * mount should be failed, when device has readonly mode, and
1536 * previous checkpoint was not done by clean system shutdown.
1537 */
1538 if (bdev_read_only(sb->s_bdev) &&
1539 !is_set_ckpt_flags(sbi->ckpt, CP_UMOUNT_FLAG)) {
1540 err = -EROFS;
1541 goto free_kobj;
1542 }
1543
1544 if (need_fsck)
1545 set_sbi_flag(sbi, SBI_NEED_FSCK);
1546
Jaegeuk Kimed229932016-03-23 16:12:58 -07001547 err = recover_fsync_data(sbi, false);
1548 if (err < 0) {
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001549 need_fsck = true;
1550 f2fs_msg(sb, KERN_ERR,
1551 "Cannot recover all fsync data errno=%ld", err);
1552 goto free_kobj;
1553 }
Jaegeuk Kimed229932016-03-23 16:12:58 -07001554 } else {
1555 err = recover_fsync_data(sbi, true);
1556
1557 if (!f2fs_readonly(sb) && err > 0) {
1558 err = -EINVAL;
1559 f2fs_msg(sb, KERN_ERR,
1560 "Need to recover fsync data");
1561 goto free_kobj;
1562 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001563 }
Jaegeuk Kimed229932016-03-23 16:12:58 -07001564
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001565 /* recover_fsync_data() cleared this already */
1566 clear_sbi_flag(sbi, SBI_POR_DOING);
1567
1568 /*
1569 * If filesystem is not mounted as read-only then
1570 * do start the gc_thread.
1571 */
1572 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
1573 /* After POR, we can run background GC thread.*/
1574 err = start_gc_thread(sbi);
1575 if (err)
1576 goto free_kobj;
1577 }
1578 kfree(options);
1579
1580 /* recover broken superblock */
Jaegeuk Kim2ae41232016-03-23 10:42:01 -07001581 if (recovery) {
Chao Yu6d56c272016-02-22 18:33:20 +08001582 err = f2fs_commit_super(sbi, true);
1583 f2fs_msg(sb, KERN_INFO,
1584 "Try to recover %dth superblock, ret: %ld",
1585 sbi->valid_super_block ? 1 : 2, err);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001586 }
1587
Jaegeuk Kimdedfbf82016-01-08 15:51:50 -08001588 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kim2c88a922016-01-08 16:57:48 -08001589 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001590 return 0;
1591
1592free_kobj:
1593 kobject_del(&sbi->s_kobj);
Chao Yu98fbe362015-11-16 20:38:25 +08001594 kobject_put(&sbi->s_kobj);
1595 wait_for_completion(&sbi->s_kobj_unregister);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001596free_proc:
1597 if (sbi->s_proc) {
1598 remove_proc_entry("segment_info", sbi->s_proc);
1599 remove_proc_entry(sb->s_id, f2fs_proc_root);
1600 }
1601 f2fs_destroy_stats(sbi);
1602free_root_inode:
1603 dput(sb->s_root);
1604 sb->s_root = NULL;
1605free_node_inode:
1606 mutex_lock(&sbi->umount_mutex);
1607 f2fs_leave_shrinker(sbi);
1608 iput(sbi->node_inode);
1609 mutex_unlock(&sbi->umount_mutex);
1610free_nm:
1611 destroy_node_manager(sbi);
1612free_sm:
1613 destroy_segment_manager(sbi);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001614 kfree(sbi->ckpt);
1615free_meta_inode:
1616 make_bad_inode(sbi->meta_inode);
1617 iput(sbi->meta_inode);
1618free_options:
1619 kfree(options);
1620free_sb_buf:
Yunlei He33e79272015-12-15 17:17:20 +08001621 kfree(raw_super);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001622free_sbi:
1623 kfree(sbi);
1624
1625 /* give only one another chance */
1626 if (retry) {
1627 retry = false;
1628 shrink_dcache_sb(sb);
1629 goto try_onemore;
1630 }
1631 return err;
1632}
1633
1634static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1635 const char *dev_name, void *data)
1636{
1637 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1638}
1639
1640static void kill_f2fs_super(struct super_block *sb)
1641{
1642 if (sb->s_root)
1643 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
1644 kill_block_super(sb);
1645}
1646
1647static struct file_system_type f2fs_fs_type = {
1648 .owner = THIS_MODULE,
1649 .name = "f2fs",
1650 .mount = f2fs_mount,
1651 .kill_sb = kill_f2fs_super,
1652 .fs_flags = FS_REQUIRES_DEV,
1653};
1654
1655static int __init init_inodecache(void)
1656{
1657 f2fs_inode_cachep = f2fs_kmem_cache_create("f2fs_inode_cache",
1658 sizeof(struct f2fs_inode_info));
1659 if (!f2fs_inode_cachep)
1660 return -ENOMEM;
1661 return 0;
1662}
1663
1664static void destroy_inodecache(void)
1665{
1666 /*
1667 * Make sure all delayed rcu free inodes are flushed before we
1668 * destroy cache.
1669 */
1670 rcu_barrier();
1671 kmem_cache_destroy(f2fs_inode_cachep);
1672}
1673
1674static int __init init_f2fs_fs(void)
1675{
1676 int err;
1677
1678 f2fs_build_trace_ios();
1679
1680 err = init_inodecache();
1681 if (err)
1682 goto fail;
1683 err = create_node_manager_caches();
1684 if (err)
1685 goto free_inodecache;
1686 err = create_segment_manager_caches();
1687 if (err)
1688 goto free_node_manager_caches;
1689 err = create_checkpoint_caches();
1690 if (err)
1691 goto free_segment_manager_caches;
1692 err = create_extent_cache();
1693 if (err)
1694 goto free_checkpoint_caches;
1695 f2fs_kset = kset_create_and_add("f2fs", NULL, fs_kobj);
1696 if (!f2fs_kset) {
1697 err = -ENOMEM;
1698 goto free_extent_cache;
1699 }
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001700 register_shrinker(&f2fs_shrinker_info);
1701
1702 err = register_filesystem(&f2fs_fs_type);
1703 if (err)
1704 goto free_shrinker;
Chao Yu3e36b0b2015-10-29 09:13:04 +08001705 err = f2fs_create_root_stats();
1706 if (err)
1707 goto free_filesystem;
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001708 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1709 return 0;
1710
Chao Yu3e36b0b2015-10-29 09:13:04 +08001711free_filesystem:
1712 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001713free_shrinker:
1714 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001715 kset_unregister(f2fs_kset);
1716free_extent_cache:
1717 destroy_extent_cache();
1718free_checkpoint_caches:
1719 destroy_checkpoint_caches();
1720free_segment_manager_caches:
1721 destroy_segment_manager_caches();
1722free_node_manager_caches:
1723 destroy_node_manager_caches();
1724free_inodecache:
1725 destroy_inodecache();
1726fail:
1727 return err;
1728}
1729
1730static void __exit exit_f2fs_fs(void)
1731{
1732 remove_proc_entry("fs/f2fs", NULL);
1733 f2fs_destroy_root_stats();
1734 unregister_shrinker(&f2fs_shrinker_info);
1735 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim315f4552015-11-29 09:25:08 -08001736 destroy_extent_cache();
1737 destroy_checkpoint_caches();
1738 destroy_segment_manager_caches();
1739 destroy_node_manager_caches();
1740 destroy_inodecache();
1741 kset_unregister(f2fs_kset);
1742 f2fs_destroy_trace_ios();
1743}
1744
1745module_init(init_f2fs_fs)
1746module_exit(exit_f2fs_fs)
1747
1748MODULE_AUTHOR("Samsung Electronics's Praesto Team");
1749MODULE_DESCRIPTION("Flash Friendly File System");
1750MODULE_LICENSE("GPL");