blob: 3da6fb276f8b6cd5dc564d87b01852a1a228419b [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002 * 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>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090015#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>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090021#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090022#include <linux/random.h>
23#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090024#include <linux/blkdev.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090025#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090026#include <linux/sysfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090027
28#include "f2fs.h"
29#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090030#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090031#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090032#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080033#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090034
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090035#define CREATE_TRACE_POINTS
36#include <trace/events/f2fs.h>
37
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090038static struct kmem_cache *f2fs_inode_cachep;
39
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070040#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070041
42char *fault_name[FAULT_MAX] = {
43 [FAULT_KMALLOC] = "kmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070044 [FAULT_PAGE_ALLOC] = "page alloc",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070045 [FAULT_ALLOC_NID] = "alloc nid",
46 [FAULT_ORPHAN] = "orphan",
47 [FAULT_BLOCK] = "no more block",
48 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070049 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080050 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu8b038c72016-09-18 23:30:07 +080051 [FAULT_IO] = "IO error",
Chao Yu0f348022016-09-26 19:45:55 +080052 [FAULT_CHECKPOINT] = "checkpoint error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070053};
Sheng Yong08796892016-05-16 12:38:50 +080054
Chao Yu1ecc0c52016-09-23 21:30:09 +080055static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
56 unsigned int rate)
Sheng Yong08796892016-05-16 12:38:50 +080057{
Chao Yu1ecc0c52016-09-23 21:30:09 +080058 struct f2fs_fault_info *ffi = &sbi->fault_info;
59
Sheng Yong08796892016-05-16 12:38:50 +080060 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080061 atomic_set(&ffi->inject_ops, 0);
62 ffi->inject_rate = rate;
63 ffi->inject_type = (1 << FAULT_MAX) - 1;
Sheng Yong08796892016-05-16 12:38:50 +080064 } else {
Chao Yu1ecc0c52016-09-23 21:30:09 +080065 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080066 }
67}
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070068#endif
69
Jaegeuk Kim2658e502015-06-19 12:01:21 -070070/* f2fs-wide shrinker description */
71static struct shrinker f2fs_shrinker_info = {
72 .scan_objects = f2fs_shrink_scan,
73 .count_objects = f2fs_shrink_count,
74 .seeks = DEFAULT_SEEKS,
75};
76
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090077enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090078 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090079 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080080 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090081 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080082 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090083 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040084 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090085 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090086 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090087 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 Opt_noacl,
89 Opt_active_logs,
90 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090091 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080092 Opt_noinline_xattr,
Huajun Li8274de72013-11-10 23:13:17 +080093 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +080094 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +080095 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090096 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -070097 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -070098 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -070099 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800100 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700101 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800102 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800103 Opt_data_flush,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700104 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800105 Opt_io_size_bits,
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700106 Opt_fault_injection,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700107 Opt_lazytime,
108 Opt_nolazytime,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900109 Opt_err,
110};
111
112static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900113 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900114 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800115 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900116 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800117 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900118 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400119 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900120 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900121 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900122 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900123 {Opt_noacl, "noacl"},
124 {Opt_active_logs, "active_logs=%u"},
125 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900126 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800127 {Opt_noinline_xattr, "noinline_xattr"},
Huajun Li8274de72013-11-10 23:13:17 +0800128 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800129 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800130 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900131 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700132 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700133 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700134 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800135 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700136 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800137 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800138 {Opt_data_flush, "data_flush"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700139 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800140 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700141 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700142 {Opt_lazytime, "lazytime"},
143 {Opt_nolazytime, "nolazytime"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900144 {Opt_err, NULL},
145};
146
Namjae Jeona07ef782012-12-30 14:52:05 +0900147void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
148{
149 struct va_format vaf;
150 va_list args;
151
152 va_start(args, fmt);
153 vaf.fmt = fmt;
154 vaf.va = &args;
155 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
156 va_end(args);
157}
158
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900159static void init_once(void *foo)
160{
161 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
162
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900163 inode_init_once(&fi->vfs_inode);
164}
165
Namjae Jeon696c0182013-06-16 09:48:48 +0900166static int parse_options(struct super_block *sb, char *options)
167{
168 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800169 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900170 substring_t args[MAX_OPT_ARGS];
171 char *p, *name;
172 int arg = 0;
173
174 if (!options)
175 return 0;
176
177 while ((p = strsep(&options, ",")) != NULL) {
178 int token;
179 if (!*p)
180 continue;
181 /*
182 * Initialize args struct so we know whether arg was
183 * found; some options take optional arguments.
184 */
185 args[0].to = args[0].from = NULL;
186 token = match_token(p, f2fs_tokens, args);
187
188 switch (token) {
189 case Opt_gc_background:
190 name = match_strdup(&args[0]);
191
192 if (!name)
193 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700194 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900195 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700196 clear_opt(sbi, FORCE_FG_GC);
197 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900198 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700199 clear_opt(sbi, FORCE_FG_GC);
200 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
201 set_opt(sbi, BG_GC);
202 set_opt(sbi, FORCE_FG_GC);
203 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900204 kfree(name);
205 return -EINVAL;
206 }
207 kfree(name);
208 break;
209 case Opt_disable_roll_forward:
210 set_opt(sbi, DISABLE_ROLL_FORWARD);
211 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800212 case Opt_norecovery:
213 /* this option mounts f2fs with ro */
214 set_opt(sbi, DISABLE_ROLL_FORWARD);
215 if (!f2fs_readonly(sb))
216 return -EINVAL;
217 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900218 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800219 q = bdev_get_queue(sb->s_bdev);
220 if (blk_queue_discard(q)) {
221 set_opt(sbi, DISCARD);
Damien Le Moal0ab02992016-10-28 17:45:02 +0900222 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800223 f2fs_msg(sb, KERN_WARNING,
224 "mounting with \"discard\" option, but "
225 "the device does not support discard");
226 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900227 break;
Chao Yu64058be2016-07-03 22:05:14 +0800228 case Opt_nodiscard:
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900229 if (f2fs_sb_mounted_blkzoned(sb)) {
230 f2fs_msg(sb, KERN_WARNING,
231 "discard is required for zoned block devices");
232 return -EINVAL;
233 }
Chao Yu64058be2016-07-03 22:05:14 +0800234 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900235 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400239 case Opt_heap:
240 clear_opt(sbi, NOHEAP);
241 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900242#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900243 case Opt_user_xattr:
244 set_opt(sbi, XATTR_USER);
245 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900246 case Opt_nouser_xattr:
247 clear_opt(sbi, XATTR_USER);
248 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900249 case Opt_inline_xattr:
250 set_opt(sbi, INLINE_XATTR);
251 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800252 case Opt_noinline_xattr:
253 clear_opt(sbi, INLINE_XATTR);
254 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900255#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900256 case Opt_user_xattr:
257 f2fs_msg(sb, KERN_INFO,
258 "user_xattr options not supported");
259 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900260 case Opt_nouser_xattr:
261 f2fs_msg(sb, KERN_INFO,
262 "nouser_xattr options not supported");
263 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900264 case Opt_inline_xattr:
265 f2fs_msg(sb, KERN_INFO,
266 "inline_xattr options not supported");
267 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800268 case Opt_noinline_xattr:
269 f2fs_msg(sb, KERN_INFO,
270 "noinline_xattr options not supported");
271 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900272#endif
273#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900274 case Opt_acl:
275 set_opt(sbi, POSIX_ACL);
276 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900277 case Opt_noacl:
278 clear_opt(sbi, POSIX_ACL);
279 break;
280#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900281 case Opt_acl:
282 f2fs_msg(sb, KERN_INFO, "acl options not supported");
283 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900284 case Opt_noacl:
285 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
286 break;
287#endif
288 case Opt_active_logs:
289 if (args->from && match_int(args, &arg))
290 return -EINVAL;
291 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
292 return -EINVAL;
293 sbi->active_logs = arg;
294 break;
295 case Opt_disable_ext_identify:
296 set_opt(sbi, DISABLE_EXT_IDENTIFY);
297 break;
Huajun Li8274de72013-11-10 23:13:17 +0800298 case Opt_inline_data:
299 set_opt(sbi, INLINE_DATA);
300 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800301 case Opt_inline_dentry:
302 set_opt(sbi, INLINE_DENTRY);
303 break;
Chao Yu97c17942016-05-09 19:56:34 +0800304 case Opt_noinline_dentry:
305 clear_opt(sbi, INLINE_DENTRY);
306 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900307 case Opt_flush_merge:
308 set_opt(sbi, FLUSH_MERGE);
309 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700310 case Opt_noflush_merge:
311 clear_opt(sbi, FLUSH_MERGE);
312 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700313 case Opt_nobarrier:
314 set_opt(sbi, NOBARRIER);
315 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700316 case Opt_fastboot:
317 set_opt(sbi, FASTBOOT);
318 break;
Chao Yu89672152015-02-05 17:55:51 +0800319 case Opt_extent_cache:
320 set_opt(sbi, EXTENT_CACHE);
321 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700322 case Opt_noextent_cache:
323 clear_opt(sbi, EXTENT_CACHE);
324 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800325 case Opt_noinline_data:
326 clear_opt(sbi, INLINE_DATA);
327 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800328 case Opt_data_flush:
329 set_opt(sbi, DATA_FLUSH);
330 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700331 case Opt_mode:
332 name = match_strdup(&args[0]);
333
334 if (!name)
335 return -ENOMEM;
336 if (strlen(name) == 8 &&
337 !strncmp(name, "adaptive", 8)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900338 if (f2fs_sb_mounted_blkzoned(sb)) {
339 f2fs_msg(sb, KERN_WARNING,
340 "adaptive mode is not allowed with "
341 "zoned block device feature");
342 kfree(name);
343 return -EINVAL;
344 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700345 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700346 } else if (strlen(name) == 3 &&
347 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700348 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700349 } else {
350 kfree(name);
351 return -EINVAL;
352 }
353 kfree(name);
354 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800355 case Opt_io_size_bits:
356 if (args->from && match_int(args, &arg))
357 return -EINVAL;
358 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
359 f2fs_msg(sb, KERN_WARNING,
360 "Not support %d, larger than %d",
361 1 << arg, BIO_MAX_PAGES);
362 return -EINVAL;
363 }
364 sbi->write_io_size_bits = arg;
365 break;
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700366 case Opt_fault_injection:
367 if (args->from && match_int(args, &arg))
368 return -EINVAL;
369#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800370 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800371 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700372#else
373 f2fs_msg(sb, KERN_INFO,
374 "FAULT_INJECTION was not selected");
375#endif
376 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700377 case Opt_lazytime:
378 sb->s_flags |= MS_LAZYTIME;
379 break;
380 case Opt_nolazytime:
381 sb->s_flags &= ~MS_LAZYTIME;
382 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900383 default:
384 f2fs_msg(sb, KERN_ERR,
385 "Unrecognized mount option \"%s\" or missing value",
386 p);
387 return -EINVAL;
388 }
389 }
Jaegeuk Kimec915382016-12-21 17:09:19 -0800390
391 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
392 f2fs_msg(sb, KERN_ERR,
393 "Should set mode=lfs with %uKB-sized IO",
394 F2FS_IO_SIZE_KB(sbi));
395 return -EINVAL;
396 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900397 return 0;
398}
399
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900400static struct inode *f2fs_alloc_inode(struct super_block *sb)
401{
402 struct f2fs_inode_info *fi;
403
Chao Yua0acdfe2013-12-05 09:54:00 +0800404 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900405 if (!fi)
406 return NULL;
407
408 init_once((void *) fi);
409
Masanari Iida434720f2013-03-19 08:03:35 +0900410 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900411 fi->vfs_inode.i_version = 1;
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800412 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900413 fi->i_current_depth = 1;
414 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900415 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800416 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700417 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700418 INIT_LIST_HEAD(&fi->inmem_pages);
419 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800420 init_rwsem(&fi->dio_rwsem[READ]);
421 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800422 init_rwsem(&fi->i_mmap_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900423
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900424 /* Will be used by directory only */
425 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900426 return &fi->vfs_inode;
427}
428
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900429static int f2fs_drop_inode(struct inode *inode)
430{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000431 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900432 /*
433 * This is to avoid a deadlock condition like below.
434 * writeback_single_inode(inode)
435 * - f2fs_write_data_page
436 * - f2fs_gc -> iput -> evict
437 * - inode_wait_for_writeback(inode)
438 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700439 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700440 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700441 /* to avoid evict_inode call simultaneously */
442 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700443 spin_unlock(&inode->i_lock);
444
445 /* some remained atomic pages should discarded */
446 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800447 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700448
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700449 /* should remain fi->extent_tree for writepage */
450 f2fs_destroy_extent_node(inode);
451
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700452 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700453 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700454
455 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700456 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700457
458 sb_end_intwrite(inode->i_sb);
459
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700460 fscrypt_put_encryption_info(inode, NULL);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700461 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700462 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700463 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000464 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900465 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700466 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000467 ret = generic_drop_inode(inode);
468 trace_f2fs_drop_inode(inode, ret);
469 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900470}
471
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700472int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700473{
474 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700475 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700476
477 spin_lock(&sbi->inode_lock[DIRTY_META]);
478 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700479 ret = 1;
480 } else {
481 set_inode_flag(inode, FI_DIRTY_INODE);
482 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700483 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700484 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
485 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700486 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700487 inc_page_count(sbi, F2FS_DIRTY_IMETA);
488 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700489 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700490 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700491}
492
493void f2fs_inode_synced(struct inode *inode)
494{
495 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
496
497 spin_lock(&sbi->inode_lock[DIRTY_META]);
498 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
499 spin_unlock(&sbi->inode_lock[DIRTY_META]);
500 return;
501 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700502 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
503 list_del_init(&F2FS_I(inode)->gdirty_list);
504 dec_page_count(sbi, F2FS_DIRTY_IMETA);
505 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700506 clear_inode_flag(inode, FI_DIRTY_INODE);
507 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700508 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
509 spin_unlock(&sbi->inode_lock[DIRTY_META]);
510}
511
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900512/*
513 * f2fs_dirty_inode() is called from __mark_inode_dirty()
514 *
515 * We should call set_dirty_inode to write the dirty inode through write_inode.
516 */
517static void f2fs_dirty_inode(struct inode *inode, int flags)
518{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700519 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
520
521 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
522 inode->i_ino == F2FS_META_INO(sbi))
523 return;
524
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700525 if (flags == I_DIRTY_TIME)
526 return;
527
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700528 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
529 clear_inode_flag(inode, FI_AUTO_RECOVER);
530
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700531 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900532}
533
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900534static void f2fs_i_callback(struct rcu_head *head)
535{
536 struct inode *inode = container_of(head, struct inode, i_rcu);
537 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
538}
539
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900540static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900541{
542 call_rcu(&inode->i_rcu, f2fs_i_callback);
543}
544
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700545static void destroy_percpu_info(struct f2fs_sb_info *sbi)
546{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700547 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700548 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700549}
550
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700551static void destroy_device_list(struct f2fs_sb_info *sbi)
552{
553 int i;
554
555 for (i = 0; i < sbi->s_ndevs; i++) {
556 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
557#ifdef CONFIG_BLK_DEV_ZONED
558 kfree(FDEV(i).blkz_type);
559#endif
560 }
561 kfree(sbi->devs);
562}
563
Chao Yua3981012017-06-14 17:39:46 +0800564static void f2fs_put_super(struct super_block *sb)
565{
566 struct f2fs_sb_info *sbi = F2FS_SB(sb);
567 int i;
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900568
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900569 stop_gc_thread(sbi);
570
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700571 /* prevent remaining shrinker jobs */
572 mutex_lock(&sbi->umount_mutex);
573
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800574 /*
575 * We don't need to do checkpoint when superblock is clean.
576 * But, the previous checkpoint was not done by umount, it needs to do
577 * clean checkpoint again.
578 */
Chao Yucaf00472015-01-28 17:48:42 +0800579 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800580 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700581 struct cp_control cpc = {
582 .reason = CP_UMOUNT,
583 };
584 write_checkpoint(sbi, &cpc);
585 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900586
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800587 /* be sure to wait for any on-going discard commands */
Chao Yud4314132017-04-05 18:19:49 +0800588 f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800589
Chao Yu663f3872017-06-14 23:00:55 +0800590 if (f2fs_discard_en(sbi) && !sbi->discard_blks) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800591 struct cp_control cpc = {
592 .reason = CP_UMOUNT | CP_TRIMMED,
593 };
594 write_checkpoint(sbi, &cpc);
595 }
596
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700597 /* write_checkpoint can update stat informaion */
598 f2fs_destroy_stats(sbi);
599
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700600 /*
601 * normally superblock is clean, so we need to release this.
602 * In addition, EIO will skip do checkpoint, we need this as well.
603 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700604 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700605
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700606 f2fs_leave_shrinker(sbi);
607 mutex_unlock(&sbi->umount_mutex);
608
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800609 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700610 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800611
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900612 iput(sbi->node_inode);
613 iput(sbi->meta_inode);
614
615 /* destroy f2fs internal modules */
616 destroy_node_manager(sbi);
617 destroy_segment_manager(sbi);
618
619 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +0800620
621 f2fs_exit_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900622
623 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -0800624 if (sbi->s_chksum_driver)
625 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +0800626 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700627
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700628 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +0800629 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700630 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700631 for (i = 0; i < NR_PAGE_TYPE; i++)
632 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900633 kfree(sbi);
634}
635
636int f2fs_sync_fs(struct super_block *sb, int sync)
637{
638 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +0800639 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900640
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900641 trace_f2fs_sync_fs(sb, sync);
642
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900643 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700644 struct cp_control cpc;
645
Jaegeuk Kim119ee912015-01-29 11:45:33 -0800646 cpc.reason = __get_cp_reason(sbi);
647
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900648 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +0800649 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900650 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900651 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700652 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900653
Chao Yuc34f42e2015-12-23 17:50:30 +0800654 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900655}
656
Changman Leed6212a52013-01-29 18:30:07 +0900657static int f2fs_freeze(struct super_block *sb)
658{
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900659 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900660 return 0;
661
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -0700662 /* IO error happened before */
663 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
664 return -EIO;
665
666 /* must be clean, since sync_filesystem() was already called */
667 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
668 return -EINVAL;
669 return 0;
Changman Leed6212a52013-01-29 18:30:07 +0900670}
671
672static int f2fs_unfreeze(struct super_block *sb)
673{
674 return 0;
675}
676
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900677static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
678{
679 struct super_block *sb = dentry->d_sb;
680 struct f2fs_sb_info *sbi = F2FS_SB(sb);
681 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
682 block_t total_count, user_block_count, start_count, ovp_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700683 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900684
685 total_count = le64_to_cpu(sbi->raw_super->block_count);
686 user_block_count = sbi->user_block_count;
687 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
688 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
689 buf->f_type = F2FS_SUPER_MAGIC;
690 buf->f_bsize = sbi->blocksize;
691
692 buf->f_blocks = total_count - start_count;
Chao Yu3e6d0b42016-07-06 14:13:07 +0800693 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900694 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
695
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700696 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
697
698 if (avail_node_count > user_block_count) {
699 buf->f_files = user_block_count;
700 buf->f_ffree = buf->f_bavail;
701 } else {
702 buf->f_files = avail_node_count;
703 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
704 buf->f_bavail);
705 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900706
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900707 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900708 buf->f_fsid.val[0] = (u32)id;
709 buf->f_fsid.val[1] = (u32)(id >> 32);
710
711 return 0;
712}
713
714static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
715{
716 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
717
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700718 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
719 if (test_opt(sbi, FORCE_FG_GC))
720 seq_printf(seq, ",background_gc=%s", "sync");
721 else
722 seq_printf(seq, ",background_gc=%s", "on");
723 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900724 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700725 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900726 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
727 seq_puts(seq, ",disable_roll_forward");
728 if (test_opt(sbi, DISCARD))
729 seq_puts(seq, ",discard");
730 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400731 seq_puts(seq, ",no_heap");
732 else
733 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900734#ifdef CONFIG_F2FS_FS_XATTR
735 if (test_opt(sbi, XATTR_USER))
736 seq_puts(seq, ",user_xattr");
737 else
738 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900739 if (test_opt(sbi, INLINE_XATTR))
740 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +0800741 else
742 seq_puts(seq, ",noinline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900743#endif
744#ifdef CONFIG_F2FS_FS_POSIX_ACL
745 if (test_opt(sbi, POSIX_ACL))
746 seq_puts(seq, ",acl");
747 else
748 seq_puts(seq, ",noacl");
749#endif
750 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100751 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +0800752 if (test_opt(sbi, INLINE_DATA))
753 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +0800754 else
755 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +0800756 if (test_opt(sbi, INLINE_DENTRY))
757 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +0800758 else
759 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +0800760 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900761 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700762 if (test_opt(sbi, NOBARRIER))
763 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700764 if (test_opt(sbi, FASTBOOT))
765 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +0800766 if (test_opt(sbi, EXTENT_CACHE))
767 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700768 else
769 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +0800770 if (test_opt(sbi, DATA_FLUSH))
771 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700772
773 seq_puts(seq, ",mode=");
774 if (test_opt(sbi, ADAPTIVE))
775 seq_puts(seq, "adaptive");
776 else if (test_opt(sbi, LFS))
777 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900778 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kimec915382016-12-21 17:09:19 -0800779 if (F2FS_IO_SIZE_BITS(sbi))
780 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800781#ifdef CONFIG_F2FS_FAULT_INJECTION
782 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +0800783 seq_printf(seq, ",fault_injection=%u",
784 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800785#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900786
787 return 0;
788}
789
Yunlei He498c5e92015-05-07 18:11:37 +0800790static void default_options(struct f2fs_sb_info *sbi)
791{
792 /* init some FS parameters */
793 sbi->active_logs = NR_CURSEG_TYPE;
794
795 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +0800796 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +0800797 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +0800798 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700799 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400800 set_opt(sbi, NOHEAP);
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700801 sbi->sb->s_flags |= MS_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700802 set_opt(sbi, FLUSH_MERGE);
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900803 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700804 set_opt_mode(sbi, F2FS_MOUNT_LFS);
805 set_opt(sbi, DISCARD);
806 } else {
807 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
808 }
Yunlei He498c5e92015-05-07 18:11:37 +0800809
810#ifdef CONFIG_F2FS_FS_XATTR
811 set_opt(sbi, XATTR_USER);
812#endif
813#ifdef CONFIG_F2FS_FS_POSIX_ACL
814 set_opt(sbi, POSIX_ACL);
815#endif
Chao Yu36dbd322016-09-26 19:45:05 +0800816
817#ifdef CONFIG_F2FS_FAULT_INJECTION
818 f2fs_build_fault_attr(sbi, 0);
819#endif
Yunlei He498c5e92015-05-07 18:11:37 +0800820}
821
Namjae Jeon696c0182013-06-16 09:48:48 +0900822static int f2fs_remount(struct super_block *sb, int *flags, char *data)
823{
824 struct f2fs_sb_info *sbi = F2FS_SB(sb);
825 struct f2fs_mount_info org_mount_opt;
826 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +0800827 bool need_restart_gc = false;
828 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +0800829 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu2443b8b2016-09-26 19:45:06 +0800830#ifdef CONFIG_F2FS_FAULT_INJECTION
831 struct f2fs_fault_info ffi = sbi->fault_info;
832#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900833
834 /*
835 * Save the old mount options in case we
836 * need to restore them.
837 */
838 org_mount_opt = sbi->mount_opt;
839 active_logs = sbi->active_logs;
840
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700841 /* recover superblocks we couldn't write due to previous RO mount */
842 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
843 err = f2fs_commit_super(sbi, false);
844 f2fs_msg(sb, KERN_INFO,
845 "Try to recover all the superblocks, ret: %d", err);
846 if (!err)
847 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
848 }
849
Chao Yu26666c82014-09-15 18:04:44 +0800850 sbi->mount_opt.opt = 0;
Yunlei He498c5e92015-05-07 18:11:37 +0800851 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +0800852
Namjae Jeon696c0182013-06-16 09:48:48 +0900853 /* parse mount options */
854 err = parse_options(sb, data);
855 if (err)
856 goto restore_opts;
857
858 /*
859 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +0800860 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +0900861 */
Chao Yu6b2920a2014-07-07 11:21:59 +0800862 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +0900863 goto skip;
864
Chao Yu9cd81ce2015-09-18 16:55:26 +0800865 /* disallow enable/disable extent_cache dynamically */
866 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
867 err = -EINVAL;
868 f2fs_msg(sbi->sb, KERN_WARNING,
869 "switch extent_cache option is not allowed");
870 goto restore_opts;
871 }
872
Namjae Jeon696c0182013-06-16 09:48:48 +0900873 /*
874 * We stop the GC thread if FS is mounted as RO
875 * or if background_gc = off is passed in mount
876 * option. Also sync the filesystem.
877 */
878 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
879 if (sbi->gc_thread) {
880 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +0800881 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +0900882 }
Chao Yuaba291b2014-11-18 11:17:20 +0800883 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900884 err = start_gc_thread(sbi);
885 if (err)
886 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +0800887 need_stop_gc = true;
888 }
889
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -0700890 if (*flags & MS_RDONLY) {
891 writeback_inodes_sb(sb, WB_REASON_SYNC);
892 sync_inodes_sb(sb);
893
894 set_sbi_flag(sbi, SBI_IS_DIRTY);
895 set_sbi_flag(sbi, SBI_IS_CLOSE);
896 f2fs_sync_fs(sb, 1);
897 clear_sbi_flag(sbi, SBI_IS_CLOSE);
898 }
899
Gu Zheng876dc592014-04-11 17:50:00 +0800900 /*
901 * We stop issue flush thread if FS is mounted as RO
902 * or if flush_merge is not passed in mount option.
903 */
904 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800905 clear_opt(sbi, FLUSH_MERGE);
906 destroy_flush_cmd_control(sbi, false);
907 } else {
Gu Zheng2163d192014-04-27 14:21:33 +0800908 err = create_flush_cmd_control(sbi);
909 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800910 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +0900911 }
912skip:
913 /* Update the POSIXACL Flag */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700914 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Namjae Jeon696c0182013-06-16 09:48:48 +0900915 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700916
Namjae Jeon696c0182013-06-16 09:48:48 +0900917 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +0800918restore_gc:
919 if (need_restart_gc) {
920 if (start_gc_thread(sbi))
921 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +0900922 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +0800923 } else if (need_stop_gc) {
924 stop_gc_thread(sbi);
925 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900926restore_opts:
927 sbi->mount_opt = org_mount_opt;
928 sbi->active_logs = active_logs;
Chao Yu2443b8b2016-09-26 19:45:06 +0800929#ifdef CONFIG_F2FS_FAULT_INJECTION
930 sbi->fault_info = ffi;
931#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900932 return err;
933}
934
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900935static struct super_operations f2fs_sops = {
936 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900937 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900938 .destroy_inode = f2fs_destroy_inode,
939 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900940 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900941 .show_options = f2fs_show_options,
942 .evict_inode = f2fs_evict_inode,
943 .put_super = f2fs_put_super,
944 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900945 .freeze_fs = f2fs_freeze,
946 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900947 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900948 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900949};
950
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700951#ifdef CONFIG_F2FS_FS_ENCRYPTION
952static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
953{
954 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
955 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
956 ctx, len, NULL);
957}
958
959static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
960 void *fs_data)
961{
962 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
963 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
964 ctx, len, fs_data, XATTR_CREATE);
965}
966
967static unsigned f2fs_max_namelen(struct inode *inode)
968{
969 return S_ISLNK(inode->i_mode) ?
970 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
971}
972
Eric Biggers6f69f0e2017-02-07 12:42:10 -0800973static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -0800974 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700975 .get_context = f2fs_get_context,
976 .set_context = f2fs_set_context,
977 .is_encrypted = f2fs_encrypted_inode,
978 .empty_dir = f2fs_empty_dir,
979 .max_namelen = f2fs_max_namelen,
980};
981#else
Eric Biggers6f69f0e2017-02-07 12:42:10 -0800982static const struct fscrypt_operations f2fs_cryptops = {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700983 .is_encrypted = f2fs_encrypted_inode,
984};
985#endif
986
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900987static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
988 u64 ino, u32 generation)
989{
990 struct f2fs_sb_info *sbi = F2FS_SB(sb);
991 struct inode *inode;
992
Chao Yud6b7d4b2014-06-12 13:23:41 +0800993 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +0800994 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900995
996 /*
997 * f2fs_iget isn't quite right if the inode is currently unallocated!
998 * However f2fs_iget currently does appropriate checks to handle stale
999 * inodes so everything is OK.
1000 */
1001 inode = f2fs_iget(sb, ino);
1002 if (IS_ERR(inode))
1003 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001004 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001005 /* we didn't find the right inode.. */
1006 iput(inode);
1007 return ERR_PTR(-ESTALE);
1008 }
1009 return inode;
1010}
1011
1012static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1013 int fh_len, int fh_type)
1014{
1015 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1016 f2fs_nfs_get_inode);
1017}
1018
1019static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1020 int fh_len, int fh_type)
1021{
1022 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1023 f2fs_nfs_get_inode);
1024}
1025
1026static const struct export_operations f2fs_export_ops = {
1027 .fh_to_dentry = f2fs_fh_to_dentry,
1028 .fh_to_parent = f2fs_fh_to_parent,
1029 .get_parent = f2fs_get_parent,
1030};
1031
Chao Yue0afc4d2015-12-31 14:35:37 +08001032static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001033{
Jaegeuk Kimde936532013-08-12 21:08:03 +09001034 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001035 loff_t leaf_count = ADDRS_PER_BLOCK;
1036
1037 /* two direct node blocks */
1038 result += (leaf_count * 2);
1039
1040 /* two indirect node blocks */
1041 leaf_count *= NIDS_PER_BLOCK;
1042 result += (leaf_count * 2);
1043
1044 /* one double indirect node block */
1045 leaf_count *= NIDS_PER_BLOCK;
1046 result += leaf_count;
1047
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001048 return result;
1049}
1050
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001051static int __f2fs_commit_super(struct buffer_head *bh,
1052 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001053{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001054 lock_buffer(bh);
1055 if (super)
1056 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1057 set_buffer_uptodate(bh);
1058 set_buffer_dirty(bh);
1059 unlock_buffer(bh);
1060
1061 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001062 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001063}
1064
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001065static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001066 struct buffer_head *bh)
1067{
1068 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1069 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001070 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001071 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1072 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1073 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1074 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1075 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1076 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1077 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1078 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1079 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1080 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1081 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1082 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1083 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001084 u64 main_end_blkaddr = main_blkaddr +
1085 (segment_count_main << log_blocks_per_seg);
1086 u64 seg_end_blkaddr = segment0_blkaddr +
1087 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001088
1089 if (segment0_blkaddr != cp_blkaddr) {
1090 f2fs_msg(sb, KERN_INFO,
1091 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1092 segment0_blkaddr, cp_blkaddr);
1093 return true;
1094 }
1095
1096 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1097 sit_blkaddr) {
1098 f2fs_msg(sb, KERN_INFO,
1099 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1100 cp_blkaddr, sit_blkaddr,
1101 segment_count_ckpt << log_blocks_per_seg);
1102 return true;
1103 }
1104
1105 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1106 nat_blkaddr) {
1107 f2fs_msg(sb, KERN_INFO,
1108 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1109 sit_blkaddr, nat_blkaddr,
1110 segment_count_sit << log_blocks_per_seg);
1111 return true;
1112 }
1113
1114 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1115 ssa_blkaddr) {
1116 f2fs_msg(sb, KERN_INFO,
1117 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1118 nat_blkaddr, ssa_blkaddr,
1119 segment_count_nat << log_blocks_per_seg);
1120 return true;
1121 }
1122
1123 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1124 main_blkaddr) {
1125 f2fs_msg(sb, KERN_INFO,
1126 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1127 ssa_blkaddr, main_blkaddr,
1128 segment_count_ssa << log_blocks_per_seg);
1129 return true;
1130 }
1131
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001132 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08001133 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001134 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08001135 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001136 segment0_blkaddr +
1137 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08001138 segment_count_main << log_blocks_per_seg);
1139 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001140 } else if (main_end_blkaddr < seg_end_blkaddr) {
1141 int err = 0;
1142 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08001143
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001144 /* fix in-memory information all the time */
1145 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1146 segment0_blkaddr) >> log_blocks_per_seg);
1147
1148 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001149 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001150 res = "internally";
1151 } else {
1152 err = __f2fs_commit_super(bh, NULL);
1153 res = err ? "failed" : "done";
1154 }
1155 f2fs_msg(sb, KERN_INFO,
1156 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1157 res, main_blkaddr,
1158 segment0_blkaddr +
1159 (segment_count << log_blocks_per_seg),
1160 segment_count_main << log_blocks_per_seg);
1161 if (err)
1162 return true;
1163 }
Chao Yu9a59b622015-12-15 09:58:18 +08001164 return false;
1165}
1166
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001167static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001168 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001169{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001170 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1171 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001172 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001173 unsigned int blocksize;
1174
Namjae Jeona07ef782012-12-30 14:52:05 +09001175 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1176 f2fs_msg(sb, KERN_INFO,
1177 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1178 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001179 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001180 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001181
majianpeng5c9b4692013-02-01 19:07:57 +08001182 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001183 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08001184 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08001185 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08001187 return 1;
1188 }
1189
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001190 /* Currently, support only 4KB block size */
1191 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08001192 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001193 f2fs_msg(sb, KERN_INFO,
1194 "Invalid blocksize (%u), supports only 4KB\n",
1195 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001196 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001197 }
majianpeng5c9b4692013-02-01 19:07:57 +08001198
Chao Yu9a59b622015-12-15 09:58:18 +08001199 /* check log blocks per segment */
1200 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1201 f2fs_msg(sb, KERN_INFO,
1202 "Invalid log blocks per segment (%u)\n",
1203 le32_to_cpu(raw_super->log_blocks_per_seg));
1204 return 1;
1205 }
1206
Chao Yu55cf9cb2014-09-15 18:01:10 +08001207 /* Currently, support 512/1024/2048/4096 bytes sector size */
1208 if (le32_to_cpu(raw_super->log_sectorsize) >
1209 F2FS_MAX_LOG_SECTOR_SIZE ||
1210 le32_to_cpu(raw_super->log_sectorsize) <
1211 F2FS_MIN_LOG_SECTOR_SIZE) {
1212 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1213 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001214 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001215 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08001216 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1217 le32_to_cpu(raw_super->log_sectorsize) !=
1218 F2FS_MAX_LOG_SECTOR_SIZE) {
1219 f2fs_msg(sb, KERN_INFO,
1220 "Invalid log sectors per block(%u) log sectorsize(%u)",
1221 le32_to_cpu(raw_super->log_sectors_per_block),
1222 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001223 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001224 }
Chao Yu9a59b622015-12-15 09:58:18 +08001225
1226 /* check reserved ino info */
1227 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1228 le32_to_cpu(raw_super->meta_ino) != 2 ||
1229 le32_to_cpu(raw_super->root_ino) != 3) {
1230 f2fs_msg(sb, KERN_INFO,
1231 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1232 le32_to_cpu(raw_super->node_ino),
1233 le32_to_cpu(raw_super->meta_ino),
1234 le32_to_cpu(raw_super->root_ino));
1235 return 1;
1236 }
1237
Jin Qianb9dd4612017-04-25 16:28:48 -07001238 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1239 f2fs_msg(sb, KERN_INFO,
1240 "Invalid segment count (%u)",
1241 le32_to_cpu(raw_super->segment_count));
1242 return 1;
1243 }
1244
Chao Yu9a59b622015-12-15 09:58:18 +08001245 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001246 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08001247 return 1;
1248
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001249 return 0;
1250}
1251
Shawn Lin984ec632016-02-17 11:26:32 +08001252int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001253{
1254 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001255 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1256 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001257 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07001258 unsigned int main_segs, blocks_per_seg;
1259 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001260
1261 total = le32_to_cpu(raw_super->segment_count);
1262 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1263 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1264 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1265 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1266 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1267
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001268 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001269 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001270
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001271 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1272 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1273
1274 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1275 ovp_segments == 0 || reserved_segments == 0)) {
1276 f2fs_msg(sbi->sb, KERN_ERR,
1277 "Wrong layout: check mkfs.f2fs version");
1278 return 1;
1279 }
1280
Jin Qian15d30422017-05-15 10:45:08 -07001281 main_segs = le32_to_cpu(raw_super->segment_count_main);
1282 blocks_per_seg = sbi->blocks_per_seg;
1283
1284 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1285 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1286 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1287 return 1;
1288 }
1289 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1290 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1291 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1292 return 1;
1293 }
1294
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07001295 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001296 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1297 return 1;
1298 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001299 return 0;
1300}
1301
1302static void init_sb_info(struct f2fs_sb_info *sbi)
1303{
1304 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08001305 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001306
1307 sbi->log_sectors_per_block =
1308 le32_to_cpu(raw_super->log_sectors_per_block);
1309 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1310 sbi->blocksize = 1 << sbi->log_blocksize;
1311 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1312 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1313 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1314 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1315 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1316 sbi->total_node_count =
1317 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1318 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1319 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1320 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1321 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001322 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09001323 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001324
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001325 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001326 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001327 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08001328 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001329
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001330 for (i = 0; i < NR_COUNT_TYPE; i++)
1331 atomic_set(&sbi->nr_pages[i], 0);
1332
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001333 atomic_set(&sbi->wb_sync_req, 0);
1334
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001335 INIT_LIST_HEAD(&sbi->s_list);
1336 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08001337 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1338 for (j = HOT; j < NR_TEMP_TYPE; j++)
1339 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08001340 spin_lock_init(&sbi->cp_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001341}
1342
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001343static int init_percpu_info(struct f2fs_sb_info *sbi)
1344{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001345 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001346
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001347 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1348 if (err)
1349 return err;
1350
1351 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001352 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001353}
1354
Damien Le Moal178053e2016-10-28 17:45:05 +09001355#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001356static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09001357{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001358 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09001359 sector_t nr_sectors = bdev->bd_part->nr_sects;
1360 sector_t sector = 0;
1361 struct blk_zone *zones;
1362 unsigned int i, nr_zones;
1363 unsigned int n = 0;
1364 int err = -EIO;
1365
1366 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1367 return 0;
1368
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001369 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07001370 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001371 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001372 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001373 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1374 __ilog2_u32(sbi->blocks_per_blkz))
1375 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09001376 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001377 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1378 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001379 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001380 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09001381
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001382 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1383 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09001384 return -ENOMEM;
1385
1386#define F2FS_REPORT_NR_ZONES 4096
1387
1388 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1389 GFP_KERNEL);
1390 if (!zones)
1391 return -ENOMEM;
1392
1393 /* Get block zones type */
1394 while (zones && sector < nr_sectors) {
1395
1396 nr_zones = F2FS_REPORT_NR_ZONES;
1397 err = blkdev_report_zones(bdev, sector,
1398 zones, &nr_zones,
1399 GFP_KERNEL);
1400 if (err)
1401 break;
1402 if (!nr_zones) {
1403 err = -EIO;
1404 break;
1405 }
1406
1407 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001408 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09001409 sector += zones[i].len;
1410 n++;
1411 }
1412 }
1413
1414 kfree(zones);
1415
1416 return err;
1417}
1418#endif
1419
Gu Zheng9076a752013-10-14 18:47:11 +08001420/*
1421 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08001422 * Because we have two copies of super block, so read both of them
1423 * to get the first valid one. If any one of them is broken, we pass
1424 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08001425 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001426static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08001427 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08001428 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08001429{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001430 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08001431 int block;
Chao Yue8240f62015-12-15 17:19:26 +08001432 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001433 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08001434 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08001435
Yunlei Heb39f0de2015-12-15 17:17:20 +08001436 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1437 if (!super)
1438 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08001439
1440 for (block = 0; block < 2; block++) {
1441 bh = sb_bread(sb, block);
1442 if (!bh) {
1443 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08001444 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08001445 err = -EIO;
1446 continue;
1447 }
majianpeng14d7e9d2013-02-01 19:07:03 +08001448
Shawn Lin2b39e902016-02-17 08:59:01 +08001449 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001450 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08001451 f2fs_msg(sb, KERN_ERR,
1452 "Can't find valid F2FS filesystem in %dth superblock",
1453 block + 1);
1454 err = -EINVAL;
1455 brelse(bh);
1456 continue;
1457 }
1458
1459 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001460 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1461 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08001462 *valid_super_block = block;
1463 *raw_super = super;
1464 }
Chao Yue8240f62015-12-15 17:19:26 +08001465 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08001466 }
1467
1468 /* Fail to read any one of the superblocks*/
1469 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08001470 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08001471
hujianyangda554e42015-05-21 14:42:53 +08001472 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08001473 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08001474 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08001475 else
1476 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08001477
Shawn Lin2b39e902016-02-17 08:59:01 +08001478 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08001479}
1480
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001481int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001482{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001483 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001484 int err;
1485
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001486 if ((recover && f2fs_readonly(sbi->sb)) ||
1487 bdev_read_only(sbi->sb->s_bdev)) {
1488 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001489 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001490 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001491
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001492 /* write back-up superblock first */
1493 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001494 if (!bh)
1495 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001496 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001497 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08001498
1499 /* if we are in recovery path, skip writing valid superblock */
1500 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001501 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001502
Chao Yue8240f62015-12-15 17:19:26 +08001503 /* write current valid superblock */
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001504 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1505 if (!bh)
1506 return -EIO;
1507 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1508 brelse(bh);
1509 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001510}
1511
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001512static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1513{
1514 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001515 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001516 int i;
1517
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001518 /* Initialize single device information */
1519 if (!RDEV(0).path[0]) {
1520 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001521 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001522 max_devices = 1;
1523 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001524
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001525 /*
1526 * Initialize multiple devices information, or single
1527 * zoned block device information.
1528 */
1529 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1530 GFP_KERNEL);
1531 if (!sbi->devs)
1532 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001533
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001534 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001535
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001536 if (i > 0 && !RDEV(i).path[0])
1537 break;
1538
1539 if (max_devices == 1) {
1540 /* Single zoned block device mount */
1541 FDEV(0).bdev =
1542 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001543 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001544 } else {
1545 /* Multi-device mount */
1546 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1547 FDEV(i).total_segments =
1548 le32_to_cpu(RDEV(i).total_segments);
1549 if (i == 0) {
1550 FDEV(i).start_blk = 0;
1551 FDEV(i).end_blk = FDEV(i).start_blk +
1552 (FDEV(i).total_segments <<
1553 sbi->log_blocks_per_seg) - 1 +
1554 le32_to_cpu(raw_super->segment0_blkaddr);
1555 } else {
1556 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1557 FDEV(i).end_blk = FDEV(i).start_blk +
1558 (FDEV(i).total_segments <<
1559 sbi->log_blocks_per_seg) - 1;
1560 }
1561 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
1562 sbi->sb->s_mode, sbi->sb->s_type);
1563 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001564 if (IS_ERR(FDEV(i).bdev))
1565 return PTR_ERR(FDEV(i).bdev);
1566
1567 /* to release errored devices */
1568 sbi->s_ndevs = i + 1;
1569
1570#ifdef CONFIG_BLK_DEV_ZONED
1571 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1572 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1573 f2fs_msg(sbi->sb, KERN_ERR,
1574 "Zoned block device feature not enabled\n");
1575 return -EINVAL;
1576 }
1577 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1578 if (init_blkz_info(sbi, i)) {
1579 f2fs_msg(sbi->sb, KERN_ERR,
1580 "Failed to initialize F2FS blkzone information");
1581 return -EINVAL;
1582 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001583 if (max_devices == 1)
1584 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001585 f2fs_msg(sbi->sb, KERN_INFO,
1586 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1587 i, FDEV(i).path,
1588 FDEV(i).total_segments,
1589 FDEV(i).start_blk, FDEV(i).end_blk,
1590 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1591 "Host-aware" : "Host-managed");
1592 continue;
1593 }
1594#endif
1595 f2fs_msg(sbi->sb, KERN_INFO,
1596 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1597 i, FDEV(i).path,
1598 FDEV(i).total_segments,
1599 FDEV(i).start_blk, FDEV(i).end_blk);
1600 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001601 f2fs_msg(sbi->sb, KERN_INFO,
1602 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001603 return 0;
1604}
1605
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001606static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1607{
1608 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08001609 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001610 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08001611 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08001612 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001613 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001614 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001615 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001616
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001617try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08001618 err = -EINVAL;
1619 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001620 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08001621 recovery = 0;
1622
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001623 /* allocate memory for f2fs-specific super block info */
1624 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1625 if (!sbi)
1626 return -ENOMEM;
1627
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001628 sbi->sb = sb;
1629
Keith Mok43b65732016-03-02 12:04:24 -08001630 /* Load the checksum driver */
1631 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1632 if (IS_ERR(sbi->s_chksum_driver)) {
1633 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1634 err = PTR_ERR(sbi->s_chksum_driver);
1635 sbi->s_chksum_driver = NULL;
1636 goto free_sbi;
1637 }
1638
Namjae Jeonff9234a2013-01-12 14:41:13 +09001639 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001640 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001641 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001642 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09001643 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001644
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001645 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08001646 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08001647 if (err)
1648 goto free_sbi;
1649
Gu Zheng5fb08372013-06-07 14:16:53 +08001650 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001651 sbi->raw_super = raw_super;
1652
Damien Le Moald1b959c2016-10-28 17:45:01 +09001653 /*
1654 * The BLKZONED feature indicates that the drive was formatted with
1655 * zone alignment optimization. This is optional for host-aware
1656 * devices, but mandatory for host-managed zoned block devices.
1657 */
1658#ifndef CONFIG_BLK_DEV_ZONED
1659 if (f2fs_sb_mounted_blkzoned(sb)) {
1660 f2fs_msg(sb, KERN_ERR,
1661 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08001662 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09001663 goto free_sb_buf;
1664 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09001665#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001666 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001667 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001668 options = kstrdup((const char *)data, GFP_KERNEL);
1669 if (data && !options) {
1670 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001671 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001672 }
1673
1674 err = parse_options(sb, options);
1675 if (err)
1676 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001677
Chao Yue0afc4d2015-12-31 14:35:37 +08001678 sbi->max_file_blocks = max_file_blocks();
1679 sb->s_maxbytes = sbi->max_file_blocks <<
1680 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001681 sb->s_max_links = F2FS_LINK_MAX;
1682 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1683
1684 sb->s_op = &f2fs_sops;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001685 sb->s_cop = &f2fs_cryptops;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001686 sb->s_xattr = f2fs_xattr_handlers;
1687 sb->s_export_op = &f2fs_export_ops;
1688 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001689 sb->s_time_gran = 1;
1690 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1691 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
1692 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
1693
1694 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08001695 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001696 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001697 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08001698 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08001699 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07001700
1701 /* disallow all the data/node/meta page writes */
1702 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001703 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09001704
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001705 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07001706 int n = (i == META) ? 1: NR_TEMP_TYPE;
1707 int j;
1708
1709 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
1710 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02001711 if (!sbi->write_io[i]) {
1712 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07001713 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02001714 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07001715
1716 for (j = HOT; j < n; j++) {
1717 init_rwsem(&sbi->write_io[i][j].io_rwsem);
1718 sbi->write_io[i][j].sbi = sbi;
1719 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08001720 spin_lock_init(&sbi->write_io[i][j].io_lock);
1721 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07001722 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001723 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09001724
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001725 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09001726 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001727 init_sb_info(sbi);
1728
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001729 err = init_percpu_info(sbi);
1730 if (err)
1731 goto free_options;
1732
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001733 if (F2FS_IO_SIZE(sbi) > 1) {
1734 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08001735 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08001736 if (!sbi->write_io_dummy) {
1737 err = -ENOMEM;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001738 goto free_options;
Chao Yu1727f312017-06-12 09:44:27 +08001739 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001740 }
1741
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001742 /* get an inode for meta space */
1743 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1744 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001745 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001746 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001747 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001748 }
1749
1750 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001751 if (err) {
1752 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001753 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09001754 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001755
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001756 /* Initialize device list */
1757 err = f2fs_scan_devices(sbi);
1758 if (err) {
1759 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
1760 goto free_devices;
1761 }
1762
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001763 sbi->total_valid_node_count =
1764 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001765 percpu_counter_set(&sbi->total_valid_inode_count,
1766 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001767 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1768 sbi->total_valid_block_count =
1769 le64_to_cpu(sbi->ckpt->valid_block_count);
1770 sbi->last_valid_block_count = sbi->total_valid_block_count;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001771
Chao Yuc227f912015-12-16 13:09:20 +08001772 for (i = 0; i < NR_INODE_TYPE; i++) {
1773 INIT_LIST_HEAD(&sbi->inode_list[i]);
1774 spin_lock_init(&sbi->inode_lock[i]);
1775 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001776
Chao Yu1dcc3362015-02-05 17:57:31 +08001777 init_extent_cache_info(sbi);
1778
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001779 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001780
1781 /* setup f2fs internal modules */
1782 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001783 if (err) {
1784 f2fs_msg(sb, KERN_ERR,
1785 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001786 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09001787 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001788 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001789 if (err) {
1790 f2fs_msg(sb, KERN_ERR,
1791 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001792 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09001793 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001794
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001795 /* For write statistics */
1796 if (sb->s_bdev->bd_part)
1797 sbi->sectors_written_start =
1798 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1799
1800 /* Read accumulated write IO statistics if exists */
1801 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1802 if (__exist_node_summaries(sbi))
1803 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08001804 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001805
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001806 build_gc_manager(sbi);
1807
1808 /* get an inode for node space */
1809 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1810 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001811 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001812 err = PTR_ERR(sbi->node_inode);
1813 goto free_nm;
1814 }
1815
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001816 f2fs_join_shrinker(sbi);
1817
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08001818 err = f2fs_build_stats(sbi);
1819 if (err)
1820 goto free_nm;
1821
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001822 /* if there are nt orphan nodes free them */
Chao Yu8c14bfa2015-08-07 17:58:43 +08001823 err = recover_orphan_inodes(sbi);
1824 if (err)
1825 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001826
1827 /* read root inode and dentry */
1828 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1829 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001830 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001831 err = PTR_ERR(root);
1832 goto free_node_inode;
1833 }
Chao Yu8f99a942013-11-28 15:43:43 +08001834 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08001835 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08001836 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08001837 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08001838 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001839
1840 sb->s_root = d_make_root(root); /* allocate root dentry */
1841 if (!sb->s_root) {
1842 err = -ENOMEM;
1843 goto free_root_inode;
1844 }
1845
Chao Yua3981012017-06-14 17:39:46 +08001846 err = f2fs_init_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001847 if (err)
Chao Yua3981012017-06-14 17:39:46 +08001848 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001849
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001850 /* recover fsynced data */
1851 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001852 /*
1853 * mount should be failed, when device has readonly mode, and
1854 * previous checkpoint was not done by clean system shutdown.
1855 */
1856 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08001857 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001858 err = -EROFS;
Chao Yua3981012017-06-14 17:39:46 +08001859 goto free_sysfs;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001860 }
Chao Yu2adc3502015-03-16 21:08:44 +08001861
1862 if (need_fsck)
1863 set_sbi_flag(sbi, SBI_NEED_FSCK);
1864
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07001865 if (!retry)
1866 goto skip_recovery;
1867
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001868 err = recover_fsync_data(sbi, false);
1869 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08001870 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001871 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08001872 "Cannot recover all fsync data errno=%d", err);
Chao Yua3981012017-06-14 17:39:46 +08001873 goto free_sysfs;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001874 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001875 } else {
1876 err = recover_fsync_data(sbi, true);
1877
1878 if (!f2fs_readonly(sb) && err > 0) {
1879 err = -EINVAL;
1880 f2fs_msg(sb, KERN_ERR,
1881 "Need to recover fsync data");
Chao Yua3981012017-06-14 17:39:46 +08001882 goto free_sysfs;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001883 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001884 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07001885skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07001886 /* recover_fsync_data() cleared this already */
1887 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001888
1889 /*
1890 * If filesystem is not mounted as read-only then
1891 * do start the gc_thread.
1892 */
Chao Yu6c029932014-11-18 11:16:01 +08001893 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001894 /* After POR, we can run background GC thread.*/
1895 err = start_gc_thread(sbi);
1896 if (err)
Chao Yua3981012017-06-14 17:39:46 +08001897 goto free_sysfs;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001898 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001899 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08001900
1901 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001902 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08001903 err = f2fs_commit_super(sbi, true);
1904 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08001905 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08001906 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08001907 }
1908
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08001909 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
1910 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001911 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001912 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001913 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001914
Chao Yua3981012017-06-14 17:39:46 +08001915free_sysfs:
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001916 f2fs_sync_inode_meta(sbi);
Chao Yua3981012017-06-14 17:39:46 +08001917 f2fs_exit_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001918free_root_inode:
1919 dput(sb->s_root);
1920 sb->s_root = NULL;
1921free_node_inode:
Jaegeuk Kimbb5dada2016-09-23 11:29:00 -07001922 truncate_inode_pages_final(NODE_MAPPING(sbi));
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001923 mutex_lock(&sbi->umount_mutex);
Jaegeuk Kimd41065e2016-09-21 11:39:42 -07001924 release_ino_entry(sbi, true);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001925 f2fs_leave_shrinker(sbi);
Jaegeuk Kim09922802016-10-31 14:01:41 -07001926 /*
1927 * Some dirty meta pages can be produced by recover_orphan_inodes()
1928 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
1929 * followed by write_checkpoint() through f2fs_write_node_pages(), which
1930 * falls into an infinite loop in sync_meta_pages().
1931 */
1932 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001933 iput(sbi->node_inode);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001934 mutex_unlock(&sbi->umount_mutex);
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08001935 f2fs_destroy_stats(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001936free_nm:
1937 destroy_node_manager(sbi);
1938free_sm:
1939 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001940free_devices:
1941 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001942 kfree(sbi->ckpt);
1943free_meta_inode:
1944 make_bad_inode(sbi->meta_inode);
1945 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001946free_io_dummy:
1947 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001948free_options:
Jaegeuk Kima912b542017-05-10 11:18:25 -07001949 for (i = 0; i < NR_PAGE_TYPE; i++)
1950 kfree(sbi->write_io[i]);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001951 destroy_percpu_info(sbi);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001952 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001953free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08001954 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001955free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08001956 if (sbi->s_chksum_driver)
1957 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001958 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001959
1960 /* give only one another chance */
1961 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09001962 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001963 shrink_dcache_sb(sb);
1964 goto try_onemore;
1965 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001966 return err;
1967}
1968
1969static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1970 const char *dev_name, void *data)
1971{
1972 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1973}
1974
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001975static void kill_f2fs_super(struct super_block *sb)
1976{
1977 if (sb->s_root)
Chao Yucaf00472015-01-28 17:48:42 +08001978 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001979 kill_block_super(sb);
1980}
1981
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001982static struct file_system_type f2fs_fs_type = {
1983 .owner = THIS_MODULE,
1984 .name = "f2fs",
1985 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001986 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001987 .fs_flags = FS_REQUIRES_DEV,
1988};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001989MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001990
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001991static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001992{
Vladimir Davydov5d097052016-01-14 15:18:21 -08001993 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
1994 sizeof(struct f2fs_inode_info), 0,
1995 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001996 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001997 return -ENOMEM;
1998 return 0;
1999}
2000
2001static void destroy_inodecache(void)
2002{
2003 /*
2004 * Make sure all delayed rcu free inodes are flushed before we
2005 * destroy cache.
2006 */
2007 rcu_barrier();
2008 kmem_cache_destroy(f2fs_inode_cachep);
2009}
2010
2011static int __init init_f2fs_fs(void)
2012{
2013 int err;
2014
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002015 f2fs_build_trace_ios();
2016
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002017 err = init_inodecache();
2018 if (err)
2019 goto fail;
2020 err = create_node_manager_caches();
2021 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002022 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002023 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002024 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002025 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002026 err = create_checkpoint_caches();
2027 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002028 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002029 err = create_extent_cache();
2030 if (err)
2031 goto free_checkpoint_caches;
Chao Yua3981012017-06-14 17:39:46 +08002032 err = f2fs_register_sysfs();
2033 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002034 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002035 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002036 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002037 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002038 err = register_filesystem(&f2fs_fs_type);
2039 if (err)
2040 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002041 err = f2fs_create_root_stats();
2042 if (err)
2043 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002044 return 0;
2045
Chao Yu787c7b8c2015-10-29 09:13:04 +08002046free_filesystem:
2047 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002048free_shrinker:
2049 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002050free_sysfs:
2051 f2fs_unregister_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08002052free_extent_cache:
2053 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002054free_checkpoint_caches:
2055 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002056free_segment_manager_caches:
2057 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002058free_node_manager_caches:
2059 destroy_node_manager_caches();
2060free_inodecache:
2061 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002062fail:
2063 return err;
2064}
2065
2066static void __exit exit_f2fs_fs(void)
2067{
Namjae Jeon4589d252013-01-15 19:58:47 +09002068 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002069 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08002070 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002071 f2fs_unregister_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08002072 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002073 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09002074 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002075 destroy_node_manager_caches();
2076 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08002077 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002078}
2079
2080module_init(init_f2fs_fs)
2081module_exit(exit_f2fs_fs)
2082
2083MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2084MODULE_DESCRIPTION("Flash Friendly File System");
2085MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07002086