blob: 1896c72f871e878fd379dc8d1b7eaa3557098c02 [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 Kim73faec42016-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 Kime6b120d2017-07-10 12:55:09 -070050 [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 Kim73faec42016-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 Kime6b120d2017-07-10 12:55:09 -070084 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,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070092 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 Kime6b120d2017-07-10 12:55:09 -0700105 Opt_io_size_bits,
Jaegeuk Kim73faec42016-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 Kime6b120d2017-07-10 12:55:09 -0700119 {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"},
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700127 {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 Kime6b120d2017-07-10 12:55:09 -0700140 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec42016-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);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700222 } 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:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700229 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);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700235 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700239 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;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700252 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;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700268 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)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700338 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 Kime6b120d2017-07-10 12:55:09 -0700355 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 Kim73faec42016-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);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700371 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec42016-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 Kime6b120d2017-07-10 12:55:09 -0700390
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 Kima43e1c42016-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 Sun7b23ea12017-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{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700431 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 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700464 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900465 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700466 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700467 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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Kime6b120d2017-07-10 12:55:09 -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 Yu17a3fb52017-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 Kime6b120d2017-07-10 12:55:09 -0700587 /* be sure to wait for any on-going discard commands */
588 f2fs_wait_discard_bios(sbi);
589
Chao Yua88cedf2017-06-14 23:00:55 +0800590 if (f2fs_discard_en(sbi) && !sbi->discard_blks) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700591 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 Kim9bc576a2017-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 Yu17a3fb52017-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 Kime6b120d2017-07-10 12:55:09 -0700628 destroy_device_list(sbi);
629 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700630 destroy_percpu_info(sbi);
Jaegeuk Kimc4127262017-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 Kime6b120d2017-07-10 12:55:09 -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 Kim711acfd2017-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;
Chao Yu026bd9d2017-06-26 16:24:41 +0800694 buf->f_bavail = user_block_count - valid_user_blocks(sbi) -
695 sbi->reserved_blocks;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900696
Jaegeuk Kim711acfd2017-06-21 20:55:55 -0700697 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
698
699 if (avail_node_count > user_block_count) {
700 buf->f_files = user_block_count;
701 buf->f_ffree = buf->f_bavail;
702 } else {
703 buf->f_files = avail_node_count;
704 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
705 buf->f_bavail);
706 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900707
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900708 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900709 buf->f_fsid.val[0] = (u32)id;
710 buf->f_fsid.val[1] = (u32)(id >> 32);
711
712 return 0;
713}
714
715static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
716{
717 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
718
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700719 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
720 if (test_opt(sbi, FORCE_FG_GC))
721 seq_printf(seq, ",background_gc=%s", "sync");
722 else
723 seq_printf(seq, ",background_gc=%s", "on");
724 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900725 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700726 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900727 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
728 seq_puts(seq, ",disable_roll_forward");
729 if (test_opt(sbi, DISCARD))
730 seq_puts(seq, ",discard");
731 if (test_opt(sbi, NOHEAP))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700732 seq_puts(seq, ",no_heap");
733 else
734 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900735#ifdef CONFIG_F2FS_FS_XATTR
736 if (test_opt(sbi, XATTR_USER))
737 seq_puts(seq, ",user_xattr");
738 else
739 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900740 if (test_opt(sbi, INLINE_XATTR))
741 seq_puts(seq, ",inline_xattr");
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700742 else
743 seq_puts(seq, ",noinline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900744#endif
745#ifdef CONFIG_F2FS_FS_POSIX_ACL
746 if (test_opt(sbi, POSIX_ACL))
747 seq_puts(seq, ",acl");
748 else
749 seq_puts(seq, ",noacl");
750#endif
751 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100752 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +0800753 if (test_opt(sbi, INLINE_DATA))
754 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +0800755 else
756 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +0800757 if (test_opt(sbi, INLINE_DENTRY))
758 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +0800759 else
760 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +0800761 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900762 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700763 if (test_opt(sbi, NOBARRIER))
764 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700765 if (test_opt(sbi, FASTBOOT))
766 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +0800767 if (test_opt(sbi, EXTENT_CACHE))
768 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700769 else
770 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +0800771 if (test_opt(sbi, DATA_FLUSH))
772 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700773
774 seq_puts(seq, ",mode=");
775 if (test_opt(sbi, ADAPTIVE))
776 seq_puts(seq, "adaptive");
777 else if (test_opt(sbi, LFS))
778 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900779 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700780 if (F2FS_IO_SIZE_BITS(sbi))
781 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
782#ifdef CONFIG_F2FS_FAULT_INJECTION
783 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu93c0e2f2017-06-12 09:44:24 +0800784 seq_printf(seq, ",fault_injection=%u",
785 sbi->fault_info.inject_rate);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700786#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900787
788 return 0;
789}
790
Yunlei He498c5e92015-05-07 18:11:37 +0800791static void default_options(struct f2fs_sb_info *sbi)
792{
793 /* init some FS parameters */
794 sbi->active_logs = NR_CURSEG_TYPE;
795
796 set_opt(sbi, BG_GC);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700797 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +0800798 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +0800799 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700800 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700801 set_opt(sbi, NOHEAP);
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700802 sbi->sb->s_flags |= MS_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700803 set_opt(sbi, FLUSH_MERGE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700804 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700805 set_opt_mode(sbi, F2FS_MOUNT_LFS);
806 set_opt(sbi, DISCARD);
807 } else {
808 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
809 }
Yunlei He498c5e92015-05-07 18:11:37 +0800810
811#ifdef CONFIG_F2FS_FS_XATTR
812 set_opt(sbi, XATTR_USER);
813#endif
814#ifdef CONFIG_F2FS_FS_POSIX_ACL
815 set_opt(sbi, POSIX_ACL);
816#endif
Chao Yu36dbd322016-09-26 19:45:05 +0800817
818#ifdef CONFIG_F2FS_FAULT_INJECTION
819 f2fs_build_fault_attr(sbi, 0);
820#endif
Yunlei He498c5e92015-05-07 18:11:37 +0800821}
822
Namjae Jeon696c0182013-06-16 09:48:48 +0900823static int f2fs_remount(struct super_block *sb, int *flags, char *data)
824{
825 struct f2fs_sb_info *sbi = F2FS_SB(sb);
826 struct f2fs_mount_info org_mount_opt;
827 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +0800828 bool need_restart_gc = false;
829 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +0800830 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu2443b8b2016-09-26 19:45:06 +0800831#ifdef CONFIG_F2FS_FAULT_INJECTION
832 struct f2fs_fault_info ffi = sbi->fault_info;
833#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900834
835 /*
836 * Save the old mount options in case we
837 * need to restore them.
838 */
839 org_mount_opt = sbi->mount_opt;
840 active_logs = sbi->active_logs;
841
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700842 /* recover superblocks we couldn't write due to previous RO mount */
843 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
844 err = f2fs_commit_super(sbi, false);
845 f2fs_msg(sb, KERN_INFO,
846 "Try to recover all the superblocks, ret: %d", err);
847 if (!err)
848 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
849 }
850
Chao Yu26666c82014-09-15 18:04:44 +0800851 sbi->mount_opt.opt = 0;
Yunlei He498c5e92015-05-07 18:11:37 +0800852 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +0800853
Namjae Jeon696c0182013-06-16 09:48:48 +0900854 /* parse mount options */
855 err = parse_options(sb, data);
856 if (err)
857 goto restore_opts;
858
859 /*
860 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +0800861 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +0900862 */
Chao Yu6b2920a2014-07-07 11:21:59 +0800863 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +0900864 goto skip;
865
Chao Yu9cd81ce2015-09-18 16:55:26 +0800866 /* disallow enable/disable extent_cache dynamically */
867 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
868 err = -EINVAL;
869 f2fs_msg(sbi->sb, KERN_WARNING,
870 "switch extent_cache option is not allowed");
871 goto restore_opts;
872 }
873
Namjae Jeon696c0182013-06-16 09:48:48 +0900874 /*
875 * We stop the GC thread if FS is mounted as RO
876 * or if background_gc = off is passed in mount
877 * option. Also sync the filesystem.
878 */
879 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
880 if (sbi->gc_thread) {
881 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +0800882 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +0900883 }
Chao Yuaba291b2014-11-18 11:17:20 +0800884 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900885 err = start_gc_thread(sbi);
886 if (err)
887 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +0800888 need_stop_gc = true;
889 }
890
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -0700891 if (*flags & MS_RDONLY) {
892 writeback_inodes_sb(sb, WB_REASON_SYNC);
893 sync_inodes_sb(sb);
894
895 set_sbi_flag(sbi, SBI_IS_DIRTY);
896 set_sbi_flag(sbi, SBI_IS_CLOSE);
897 f2fs_sync_fs(sb, 1);
898 clear_sbi_flag(sbi, SBI_IS_CLOSE);
899 }
900
Gu Zheng876dc592014-04-11 17:50:00 +0800901 /*
902 * We stop issue flush thread if FS is mounted as RO
903 * or if flush_merge is not passed in mount option.
904 */
905 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700906 clear_opt(sbi, FLUSH_MERGE);
907 destroy_flush_cmd_control(sbi, false);
908 } else {
Gu Zheng2163d192014-04-27 14:21:33 +0800909 err = create_flush_cmd_control(sbi);
910 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800911 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +0900912 }
913skip:
914 /* Update the POSIXACL Flag */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700915 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Namjae Jeon696c0182013-06-16 09:48:48 +0900916 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700917
Namjae Jeon696c0182013-06-16 09:48:48 +0900918 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +0800919restore_gc:
920 if (need_restart_gc) {
921 if (start_gc_thread(sbi))
922 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +0900923 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +0800924 } else if (need_stop_gc) {
925 stop_gc_thread(sbi);
926 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900927restore_opts:
928 sbi->mount_opt = org_mount_opt;
929 sbi->active_logs = active_logs;
Chao Yu2443b8b2016-09-26 19:45:06 +0800930#ifdef CONFIG_F2FS_FAULT_INJECTION
931 sbi->fault_info = ffi;
932#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900933 return err;
934}
935
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900936static struct super_operations f2fs_sops = {
937 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900938 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900939 .destroy_inode = f2fs_destroy_inode,
940 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900941 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900942 .show_options = f2fs_show_options,
943 .evict_inode = f2fs_evict_inode,
944 .put_super = f2fs_put_super,
945 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +0900946 .freeze_fs = f2fs_freeze,
947 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900948 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +0900949 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900950};
951
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700952#ifdef CONFIG_F2FS_FS_ENCRYPTION
953static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
954{
955 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
956 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
957 ctx, len, NULL);
958}
959
960static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
961 void *fs_data)
962{
963 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
964 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
965 ctx, len, fs_data, XATTR_CREATE);
966}
967
968static unsigned f2fs_max_namelen(struct inode *inode)
969{
970 return S_ISLNK(inode->i_mode) ?
971 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
972}
973
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700974static const struct fscrypt_operations f2fs_cryptops = {
975 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700976 .get_context = f2fs_get_context,
977 .set_context = f2fs_set_context,
978 .is_encrypted = f2fs_encrypted_inode,
979 .empty_dir = f2fs_empty_dir,
980 .max_namelen = f2fs_max_namelen,
981};
982#else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700983static const struct fscrypt_operations f2fs_cryptops = {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700984 .is_encrypted = f2fs_encrypted_inode,
985};
986#endif
987
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900988static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
989 u64 ino, u32 generation)
990{
991 struct f2fs_sb_info *sbi = F2FS_SB(sb);
992 struct inode *inode;
993
Chao Yud6b7d4b2014-06-12 13:23:41 +0800994 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +0800995 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900996
997 /*
998 * f2fs_iget isn't quite right if the inode is currently unallocated!
999 * However f2fs_iget currently does appropriate checks to handle stale
1000 * inodes so everything is OK.
1001 */
1002 inode = f2fs_iget(sb, ino);
1003 if (IS_ERR(inode))
1004 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001005 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001006 /* we didn't find the right inode.. */
1007 iput(inode);
1008 return ERR_PTR(-ESTALE);
1009 }
1010 return inode;
1011}
1012
1013static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1014 int fh_len, int fh_type)
1015{
1016 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1017 f2fs_nfs_get_inode);
1018}
1019
1020static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1021 int fh_len, int fh_type)
1022{
1023 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1024 f2fs_nfs_get_inode);
1025}
1026
1027static const struct export_operations f2fs_export_ops = {
1028 .fh_to_dentry = f2fs_fh_to_dentry,
1029 .fh_to_parent = f2fs_fh_to_parent,
1030 .get_parent = f2fs_get_parent,
1031};
1032
Chao Yue0afc4d2015-12-31 14:35:37 +08001033static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001034{
Jaegeuk Kimde936532013-08-12 21:08:03 +09001035 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001036 loff_t leaf_count = ADDRS_PER_BLOCK;
1037
1038 /* two direct node blocks */
1039 result += (leaf_count * 2);
1040
1041 /* two indirect node blocks */
1042 leaf_count *= NIDS_PER_BLOCK;
1043 result += (leaf_count * 2);
1044
1045 /* one double indirect node block */
1046 leaf_count *= NIDS_PER_BLOCK;
1047 result += leaf_count;
1048
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001049 return result;
1050}
1051
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001052static int __f2fs_commit_super(struct buffer_head *bh,
1053 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001054{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001055 lock_buffer(bh);
1056 if (super)
1057 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1058 set_buffer_uptodate(bh);
1059 set_buffer_dirty(bh);
1060 unlock_buffer(bh);
1061
1062 /* it's rare case, we can do fua all the time */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001063 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001064}
1065
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001066static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001067 struct buffer_head *bh)
1068{
1069 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1070 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001071 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001072 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1073 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1074 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1075 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1076 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1077 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1078 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1079 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1080 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1081 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1082 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1083 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1084 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001085 u64 main_end_blkaddr = main_blkaddr +
1086 (segment_count_main << log_blocks_per_seg);
1087 u64 seg_end_blkaddr = segment0_blkaddr +
1088 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001089
1090 if (segment0_blkaddr != cp_blkaddr) {
1091 f2fs_msg(sb, KERN_INFO,
1092 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1093 segment0_blkaddr, cp_blkaddr);
1094 return true;
1095 }
1096
1097 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1098 sit_blkaddr) {
1099 f2fs_msg(sb, KERN_INFO,
1100 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1101 cp_blkaddr, sit_blkaddr,
1102 segment_count_ckpt << log_blocks_per_seg);
1103 return true;
1104 }
1105
1106 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1107 nat_blkaddr) {
1108 f2fs_msg(sb, KERN_INFO,
1109 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1110 sit_blkaddr, nat_blkaddr,
1111 segment_count_sit << log_blocks_per_seg);
1112 return true;
1113 }
1114
1115 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1116 ssa_blkaddr) {
1117 f2fs_msg(sb, KERN_INFO,
1118 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1119 nat_blkaddr, ssa_blkaddr,
1120 segment_count_nat << log_blocks_per_seg);
1121 return true;
1122 }
1123
1124 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1125 main_blkaddr) {
1126 f2fs_msg(sb, KERN_INFO,
1127 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1128 ssa_blkaddr, main_blkaddr,
1129 segment_count_ssa << log_blocks_per_seg);
1130 return true;
1131 }
1132
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001133 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08001134 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001135 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08001136 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001137 segment0_blkaddr +
1138 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08001139 segment_count_main << log_blocks_per_seg);
1140 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001141 } else if (main_end_blkaddr < seg_end_blkaddr) {
1142 int err = 0;
1143 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08001144
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001145 /* fix in-memory information all the time */
1146 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1147 segment0_blkaddr) >> log_blocks_per_seg);
1148
1149 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001150 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001151 res = "internally";
1152 } else {
1153 err = __f2fs_commit_super(bh, NULL);
1154 res = err ? "failed" : "done";
1155 }
1156 f2fs_msg(sb, KERN_INFO,
1157 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1158 res, main_blkaddr,
1159 segment0_blkaddr +
1160 (segment_count << log_blocks_per_seg),
1161 segment_count_main << log_blocks_per_seg);
1162 if (err)
1163 return true;
1164 }
Chao Yu9a59b622015-12-15 09:58:18 +08001165 return false;
1166}
1167
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001168static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001169 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001170{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001171 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1172 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001173 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001174 unsigned int blocksize;
1175
Namjae Jeona07ef782012-12-30 14:52:05 +09001176 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1177 f2fs_msg(sb, KERN_INFO,
1178 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1179 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001180 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001181 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001182
majianpeng5c9b4692013-02-01 19:07:57 +08001183 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001184 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08001185 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08001186 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001187 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08001188 return 1;
1189 }
1190
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001191 /* Currently, support only 4KB block size */
1192 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08001193 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001194 f2fs_msg(sb, KERN_INFO,
1195 "Invalid blocksize (%u), supports only 4KB\n",
1196 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001197 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001198 }
majianpeng5c9b4692013-02-01 19:07:57 +08001199
Chao Yu9a59b622015-12-15 09:58:18 +08001200 /* check log blocks per segment */
1201 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1202 f2fs_msg(sb, KERN_INFO,
1203 "Invalid log blocks per segment (%u)\n",
1204 le32_to_cpu(raw_super->log_blocks_per_seg));
1205 return 1;
1206 }
1207
Chao Yu55cf9cb2014-09-15 18:01:10 +08001208 /* Currently, support 512/1024/2048/4096 bytes sector size */
1209 if (le32_to_cpu(raw_super->log_sectorsize) >
1210 F2FS_MAX_LOG_SECTOR_SIZE ||
1211 le32_to_cpu(raw_super->log_sectorsize) <
1212 F2FS_MIN_LOG_SECTOR_SIZE) {
1213 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1214 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001215 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001216 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08001217 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1218 le32_to_cpu(raw_super->log_sectorsize) !=
1219 F2FS_MAX_LOG_SECTOR_SIZE) {
1220 f2fs_msg(sb, KERN_INFO,
1221 "Invalid log sectors per block(%u) log sectorsize(%u)",
1222 le32_to_cpu(raw_super->log_sectors_per_block),
1223 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001224 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001225 }
Chao Yu9a59b622015-12-15 09:58:18 +08001226
1227 /* check reserved ino info */
1228 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1229 le32_to_cpu(raw_super->meta_ino) != 2 ||
1230 le32_to_cpu(raw_super->root_ino) != 3) {
1231 f2fs_msg(sb, KERN_INFO,
1232 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1233 le32_to_cpu(raw_super->node_ino),
1234 le32_to_cpu(raw_super->meta_ino),
1235 le32_to_cpu(raw_super->root_ino));
1236 return 1;
1237 }
1238
Jin Qian93862952017-04-25 16:28:48 -07001239 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1240 f2fs_msg(sb, KERN_INFO,
1241 "Invalid segment count (%u)",
1242 le32_to_cpu(raw_super->segment_count));
1243 return 1;
1244 }
1245
Chao Yu9a59b622015-12-15 09:58:18 +08001246 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001247 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08001248 return 1;
1249
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001250 return 0;
1251}
1252
Shawn Lin984ec632016-02-17 11:26:32 +08001253int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001254{
1255 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001256 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1257 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001258 unsigned int ovp_segments, reserved_segments;
Jin Qiand90659f2017-05-15 10:45:08 -07001259 unsigned int main_segs, blocks_per_seg;
1260 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001261
1262 total = le32_to_cpu(raw_super->segment_count);
1263 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1264 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1265 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1266 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1267 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1268
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001269 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001270 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001271
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001272 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1273 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1274
1275 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1276 ovp_segments == 0 || reserved_segments == 0)) {
1277 f2fs_msg(sbi->sb, KERN_ERR,
1278 "Wrong layout: check mkfs.f2fs version");
1279 return 1;
1280 }
1281
Jin Qiand90659f2017-05-15 10:45:08 -07001282 main_segs = le32_to_cpu(raw_super->segment_count_main);
1283 blocks_per_seg = sbi->blocks_per_seg;
1284
1285 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1286 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1287 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1288 return 1;
1289 }
1290 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1291 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1292 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1293 return 1;
1294 }
1295
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07001296 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001297 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1298 return 1;
1299 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001300 return 0;
1301}
1302
1303static void init_sb_info(struct f2fs_sb_info *sbi)
1304{
1305 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yu06504682017-05-19 23:37:00 +08001306 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001307
1308 sbi->log_sectors_per_block =
1309 le32_to_cpu(raw_super->log_sectors_per_block);
1310 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1311 sbi->blocksize = 1 << sbi->log_blocksize;
1312 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1313 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1314 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1315 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1316 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1317 sbi->total_node_count =
1318 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1319 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1320 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1321 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1322 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001323 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09001324 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001325
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001326 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001327 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001328 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08001329 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001330
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001331 for (i = 0; i < NR_COUNT_TYPE; i++)
1332 atomic_set(&sbi->nr_pages[i], 0);
1333
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001334 atomic_set(&sbi->wb_sync_req, 0);
1335
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001336 INIT_LIST_HEAD(&sbi->s_list);
1337 mutex_init(&sbi->umount_mutex);
Chao Yu06504682017-05-19 23:37:00 +08001338 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1339 for (j = HOT; j < NR_TEMP_TYPE; j++)
1340 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08001341 spin_lock_init(&sbi->cp_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001342}
1343
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001344static int init_percpu_info(struct f2fs_sb_info *sbi)
1345{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001346 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001347
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001348 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1349 if (err)
1350 return err;
1351
1352 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001353 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001354}
1355
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001356#ifdef CONFIG_BLK_DEV_ZONED
1357static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
1358{
1359 struct block_device *bdev = FDEV(devi).bdev;
1360 sector_t nr_sectors = bdev->bd_part->nr_sects;
1361 sector_t sector = 0;
1362 struct blk_zone *zones;
1363 unsigned int i, nr_zones;
1364 unsigned int n = 0;
1365 int err = -EIO;
1366
1367 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1368 return 0;
1369
1370 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
1371 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
1372 return -EINVAL;
1373 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
1374 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1375 __ilog2_u32(sbi->blocks_per_blkz))
1376 return -EINVAL;
1377 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
1378 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1379 sbi->log_blocks_per_blkz;
1380 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
1381 FDEV(devi).nr_blkz++;
1382
1383 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1384 if (!FDEV(devi).blkz_type)
1385 return -ENOMEM;
1386
1387#define F2FS_REPORT_NR_ZONES 4096
1388
1389 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1390 GFP_KERNEL);
1391 if (!zones)
1392 return -ENOMEM;
1393
1394 /* Get block zones type */
1395 while (zones && sector < nr_sectors) {
1396
1397 nr_zones = F2FS_REPORT_NR_ZONES;
1398 err = blkdev_report_zones(bdev, sector,
1399 zones, &nr_zones,
1400 GFP_KERNEL);
1401 if (err)
1402 break;
1403 if (!nr_zones) {
1404 err = -EIO;
1405 break;
1406 }
1407
1408 for (i = 0; i < nr_zones; i++) {
1409 FDEV(devi).blkz_type[n] = zones[i].type;
1410 sector += zones[i].len;
1411 n++;
1412 }
1413 }
1414
1415 kfree(zones);
1416
1417 return err;
1418}
1419#endif
1420
Gu Zheng9076a752013-10-14 18:47:11 +08001421/*
1422 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08001423 * Because we have two copies of super block, so read both of them
1424 * to get the first valid one. If any one of them is broken, we pass
1425 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08001426 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001427static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08001428 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08001429 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08001430{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001431 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08001432 int block;
Chao Yue8240f62015-12-15 17:19:26 +08001433 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001434 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08001435 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08001436
Yunlei Heb39f0de2015-12-15 17:17:20 +08001437 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1438 if (!super)
1439 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08001440
1441 for (block = 0; block < 2; block++) {
1442 bh = sb_bread(sb, block);
1443 if (!bh) {
1444 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08001445 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08001446 err = -EIO;
1447 continue;
1448 }
majianpeng14d7e9d2013-02-01 19:07:03 +08001449
Shawn Lin2b39e902016-02-17 08:59:01 +08001450 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001451 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08001452 f2fs_msg(sb, KERN_ERR,
1453 "Can't find valid F2FS filesystem in %dth superblock",
1454 block + 1);
1455 err = -EINVAL;
1456 brelse(bh);
1457 continue;
1458 }
1459
1460 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001461 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1462 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08001463 *valid_super_block = block;
1464 *raw_super = super;
1465 }
Chao Yue8240f62015-12-15 17:19:26 +08001466 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08001467 }
1468
1469 /* Fail to read any one of the superblocks*/
1470 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08001471 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08001472
hujianyangda554e42015-05-21 14:42:53 +08001473 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08001474 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08001475 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08001476 else
1477 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08001478
Shawn Lin2b39e902016-02-17 08:59:01 +08001479 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08001480}
1481
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001482int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001483{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001484 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001485 int err;
1486
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001487 if ((recover && f2fs_readonly(sbi->sb)) ||
1488 bdev_read_only(sbi->sb->s_bdev)) {
1489 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001490 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001491 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001492
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001493 /* write back-up superblock first */
1494 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001495 if (!bh)
1496 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001497 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001498 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08001499
1500 /* if we are in recovery path, skip writing valid superblock */
1501 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001502 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001503
Chao Yue8240f62015-12-15 17:19:26 +08001504 /* write current valid superblock */
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001505 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1506 if (!bh)
1507 return -EIO;
1508 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1509 brelse(bh);
1510 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001511}
1512
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001513static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1514{
1515 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1516 unsigned int max_devices = MAX_DEVICES;
1517 int i;
1518
1519 /* Initialize single device information */
1520 if (!RDEV(0).path[0]) {
1521#ifdef CONFIG_BLK_DEV_ZONED
1522 if (bdev_zoned_model(sbi->sb->s_bdev) == BLK_ZONED_NONE)
1523 return 0;
1524 max_devices = 1;
1525#else
1526 return 0;
1527#endif
1528 }
1529
1530 /*
1531 * Initialize multiple devices information, or single
1532 * zoned block device information.
1533 */
1534 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1535 GFP_KERNEL);
1536 if (!sbi->devs)
1537 return -ENOMEM;
1538
1539 for (i = 0; i < max_devices; i++) {
1540
1541 if (i > 0 && !RDEV(i).path[0])
1542 break;
1543
1544 if (max_devices == 1) {
1545 /* Single zoned block device mount */
1546 FDEV(0).bdev =
1547 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
1548 sbi->sb->s_mode, sbi->sb->s_type);
1549 } else {
1550 /* Multi-device mount */
1551 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1552 FDEV(i).total_segments =
1553 le32_to_cpu(RDEV(i).total_segments);
1554 if (i == 0) {
1555 FDEV(i).start_blk = 0;
1556 FDEV(i).end_blk = FDEV(i).start_blk +
1557 (FDEV(i).total_segments <<
1558 sbi->log_blocks_per_seg) - 1 +
1559 le32_to_cpu(raw_super->segment0_blkaddr);
1560 } else {
1561 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1562 FDEV(i).end_blk = FDEV(i).start_blk +
1563 (FDEV(i).total_segments <<
1564 sbi->log_blocks_per_seg) - 1;
1565 }
1566 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
1567 sbi->sb->s_mode, sbi->sb->s_type);
1568 }
1569 if (IS_ERR(FDEV(i).bdev))
1570 return PTR_ERR(FDEV(i).bdev);
1571
1572 /* to release errored devices */
1573 sbi->s_ndevs = i + 1;
1574
1575#ifdef CONFIG_BLK_DEV_ZONED
1576 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1577 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1578 f2fs_msg(sbi->sb, KERN_ERR,
1579 "Zoned block device feature not enabled\n");
1580 return -EINVAL;
1581 }
1582 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1583 if (init_blkz_info(sbi, i)) {
1584 f2fs_msg(sbi->sb, KERN_ERR,
1585 "Failed to initialize F2FS blkzone information");
1586 return -EINVAL;
1587 }
1588 if (max_devices == 1)
1589 break;
1590 f2fs_msg(sbi->sb, KERN_INFO,
1591 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1592 i, FDEV(i).path,
1593 FDEV(i).total_segments,
1594 FDEV(i).start_blk, FDEV(i).end_blk,
1595 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1596 "Host-aware" : "Host-managed");
1597 continue;
1598 }
1599#endif
1600 f2fs_msg(sbi->sb, KERN_INFO,
1601 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1602 i, FDEV(i).path,
1603 FDEV(i).total_segments,
1604 FDEV(i).start_blk, FDEV(i).end_blk);
1605 }
1606 f2fs_msg(sbi->sb, KERN_INFO,
1607 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
1608 return 0;
1609}
1610
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001611static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1612{
1613 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08001614 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001615 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08001616 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08001617 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001618 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001619 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001620 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001621
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001622try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08001623 err = -EINVAL;
1624 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001625 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08001626 recovery = 0;
1627
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001628 /* allocate memory for f2fs-specific super block info */
1629 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1630 if (!sbi)
1631 return -ENOMEM;
1632
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001633 sbi->sb = sb;
1634
Keith Mok43b65732016-03-02 12:04:24 -08001635 /* Load the checksum driver */
1636 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1637 if (IS_ERR(sbi->s_chksum_driver)) {
1638 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1639 err = PTR_ERR(sbi->s_chksum_driver);
1640 sbi->s_chksum_driver = NULL;
1641 goto free_sbi;
1642 }
1643
Namjae Jeonff9234a2013-01-12 14:41:13 +09001644 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001645 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001646 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001647 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09001648 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001649
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001650 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08001651 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08001652 if (err)
1653 goto free_sbi;
1654
Gu Zheng5fb08372013-06-07 14:16:53 +08001655 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001656 sbi->raw_super = raw_super;
1657
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001658 /*
1659 * The BLKZONED feature indicates that the drive was formatted with
1660 * zone alignment optimization. This is optional for host-aware
1661 * devices, but mandatory for host-managed zoned block devices.
1662 */
1663#ifndef CONFIG_BLK_DEV_ZONED
1664 if (f2fs_sb_mounted_blkzoned(sb)) {
1665 f2fs_msg(sb, KERN_ERR,
1666 "Zoned block device support is not enabled\n");
Chao Yu3b0de562017-06-12 09:44:27 +08001667 err = -EOPNOTSUPP;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001668 goto free_sb_buf;
1669 }
1670#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001671 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001672 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001673 options = kstrdup((const char *)data, GFP_KERNEL);
1674 if (data && !options) {
1675 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001676 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001677 }
1678
1679 err = parse_options(sb, options);
1680 if (err)
1681 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001682
Chao Yue0afc4d2015-12-31 14:35:37 +08001683 sbi->max_file_blocks = max_file_blocks();
1684 sb->s_maxbytes = sbi->max_file_blocks <<
1685 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001686 sb->s_max_links = F2FS_LINK_MAX;
1687 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1688
1689 sb->s_op = &f2fs_sops;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001690 sb->s_cop = &f2fs_cryptops;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001691 sb->s_xattr = f2fs_xattr_handlers;
1692 sb->s_export_op = &f2fs_export_ops;
1693 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001694 sb->s_time_gran = 1;
1695 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1696 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001697 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001698
1699 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08001700 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001701 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001702 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08001703 init_rwsem(&sbi->node_write);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001704 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07001705
1706 /* disallow all the data/node/meta page writes */
1707 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001708 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09001709
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001710 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001711 int n = (i == META) ? 1: NR_TEMP_TYPE;
1712 int j;
1713
1714 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
1715 GFP_KERNEL);
Christophe JAILLETd0917a42017-06-11 09:21:11 +02001716 if (!sbi->write_io[i]) {
1717 err = -ENOMEM;
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001718 goto free_options;
Christophe JAILLETd0917a42017-06-11 09:21:11 +02001719 }
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001720
1721 for (j = HOT; j < n; j++) {
1722 init_rwsem(&sbi->write_io[i][j].io_rwsem);
1723 sbi->write_io[i][j].sbi = sbi;
1724 sbi->write_io[i][j].bio = NULL;
Chao Yuc52dc0f2017-05-19 23:37:01 +08001725 spin_lock_init(&sbi->write_io[i][j].io_lock);
1726 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001727 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001728 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09001729
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001730 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09001731 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001732 init_sb_info(sbi);
1733
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001734 err = init_percpu_info(sbi);
1735 if (err)
1736 goto free_options;
1737
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001738 if (F2FS_IO_SIZE(sbi) > 1) {
1739 sbi->write_io_dummy =
1740 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu3b0de562017-06-12 09:44:27 +08001741 if (!sbi->write_io_dummy) {
1742 err = -ENOMEM;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001743 goto free_options;
Chao Yu3b0de562017-06-12 09:44:27 +08001744 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001745 }
1746
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001747 /* get an inode for meta space */
1748 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1749 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001750 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001751 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001752 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001753 }
1754
1755 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001756 if (err) {
1757 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001758 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09001759 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001760
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001761 /* Initialize device list */
1762 err = f2fs_scan_devices(sbi);
1763 if (err) {
1764 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
1765 goto free_devices;
1766 }
1767
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001768 sbi->total_valid_node_count =
1769 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001770 percpu_counter_set(&sbi->total_valid_inode_count,
1771 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001772 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1773 sbi->total_valid_block_count =
1774 le64_to_cpu(sbi->ckpt->valid_block_count);
1775 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yu026bd9d2017-06-26 16:24:41 +08001776 sbi->reserved_blocks = 0;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001777
Chao Yuc227f912015-12-16 13:09:20 +08001778 for (i = 0; i < NR_INODE_TYPE; i++) {
1779 INIT_LIST_HEAD(&sbi->inode_list[i]);
1780 spin_lock_init(&sbi->inode_lock[i]);
1781 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001782
Chao Yu1dcc3362015-02-05 17:57:31 +08001783 init_extent_cache_info(sbi);
1784
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001785 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001786
1787 /* setup f2fs internal modules */
1788 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001789 if (err) {
1790 f2fs_msg(sb, KERN_ERR,
1791 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001792 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09001793 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001794 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09001795 if (err) {
1796 f2fs_msg(sb, KERN_ERR,
1797 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001798 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09001799 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001800
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001801 /* For write statistics */
1802 if (sb->s_bdev->bd_part)
1803 sbi->sectors_written_start =
1804 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1805
1806 /* Read accumulated write IO statistics if exists */
1807 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1808 if (__exist_node_summaries(sbi))
1809 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08001810 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001811
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001812 build_gc_manager(sbi);
1813
1814 /* get an inode for node space */
1815 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1816 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001817 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001818 err = PTR_ERR(sbi->node_inode);
1819 goto free_nm;
1820 }
1821
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001822 f2fs_join_shrinker(sbi);
1823
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001824 err = f2fs_build_stats(sbi);
1825 if (err)
1826 goto free_nm;
1827
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001828 /* if there are nt orphan nodes free them */
Chao Yu8c14bfa2015-08-07 17:58:43 +08001829 err = recover_orphan_inodes(sbi);
1830 if (err)
1831 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001832
1833 /* read root inode and dentry */
1834 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1835 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001836 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001837 err = PTR_ERR(root);
1838 goto free_node_inode;
1839 }
Chao Yu8f99a942013-11-28 15:43:43 +08001840 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08001841 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08001842 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08001843 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08001844 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001845
1846 sb->s_root = d_make_root(root); /* allocate root dentry */
1847 if (!sb->s_root) {
1848 err = -ENOMEM;
1849 goto free_root_inode;
1850 }
1851
Chao Yu17a3fb52017-06-14 17:39:46 +08001852 err = f2fs_init_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001853 if (err)
Chao Yu17a3fb52017-06-14 17:39:46 +08001854 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001855
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001856 /* recover fsynced data */
1857 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001858 /*
1859 * mount should be failed, when device has readonly mode, and
1860 * previous checkpoint was not done by clean system shutdown.
1861 */
1862 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08001863 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001864 err = -EROFS;
Chao Yu17a3fb52017-06-14 17:39:46 +08001865 goto free_sysfs;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08001866 }
Chao Yu2adc3502015-03-16 21:08:44 +08001867
1868 if (need_fsck)
1869 set_sbi_flag(sbi, SBI_NEED_FSCK);
1870
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07001871 if (!retry)
1872 goto skip_recovery;
1873
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001874 err = recover_fsync_data(sbi, false);
1875 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08001876 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001877 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08001878 "Cannot recover all fsync data errno=%d", err);
Chao Yu17a3fb52017-06-14 17:39:46 +08001879 goto free_sysfs;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001880 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001881 } else {
1882 err = recover_fsync_data(sbi, true);
1883
1884 if (!f2fs_readonly(sb) && err > 0) {
1885 err = -EINVAL;
1886 f2fs_msg(sb, KERN_ERR,
1887 "Need to recover fsync data");
Chao Yu17a3fb52017-06-14 17:39:46 +08001888 goto free_sysfs;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07001889 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001890 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07001891skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07001892 /* recover_fsync_data() cleared this already */
1893 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001894
1895 /*
1896 * If filesystem is not mounted as read-only then
1897 * do start the gc_thread.
1898 */
Chao Yu6c029932014-11-18 11:16:01 +08001899 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001900 /* After POR, we can run background GC thread.*/
1901 err = start_gc_thread(sbi);
1902 if (err)
Chao Yu17a3fb52017-06-14 17:39:46 +08001903 goto free_sysfs;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001904 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001905 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08001906
1907 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07001908 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08001909 err = f2fs_commit_super(sbi, true);
1910 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08001911 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08001912 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08001913 }
1914
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001915 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
1916 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001917 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001918 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001919 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09001920
Chao Yu17a3fb52017-06-14 17:39:46 +08001921free_sysfs:
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001922 f2fs_sync_inode_meta(sbi);
Chao Yu17a3fb52017-06-14 17:39:46 +08001923 f2fs_exit_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001924free_root_inode:
1925 dput(sb->s_root);
1926 sb->s_root = NULL;
1927free_node_inode:
Jaegeuk Kimbb5dada2016-09-23 11:29:00 -07001928 truncate_inode_pages_final(NODE_MAPPING(sbi));
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001929 mutex_lock(&sbi->umount_mutex);
Jaegeuk Kimd41065e2016-09-21 11:39:42 -07001930 release_ino_entry(sbi, true);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001931 f2fs_leave_shrinker(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001932 /*
1933 * Some dirty meta pages can be produced by recover_orphan_inodes()
1934 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
1935 * followed by write_checkpoint() through f2fs_write_node_pages(), which
1936 * falls into an infinite loop in sync_meta_pages().
1937 */
1938 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001939 iput(sbi->node_inode);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001940 mutex_unlock(&sbi->umount_mutex);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001941 f2fs_destroy_stats(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001942free_nm:
1943 destroy_node_manager(sbi);
1944free_sm:
1945 destroy_segment_manager(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001946free_devices:
1947 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001948 kfree(sbi->ckpt);
1949free_meta_inode:
1950 make_bad_inode(sbi->meta_inode);
1951 iput(sbi->meta_inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001952free_io_dummy:
1953 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001954free_options:
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001955 for (i = 0; i < NR_PAGE_TYPE; i++)
1956 kfree(sbi->write_io[i]);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001957 destroy_percpu_info(sbi);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001958 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001959free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08001960 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001961free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08001962 if (sbi->s_chksum_driver)
1963 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001964 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001965
1966 /* give only one another chance */
1967 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09001968 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001969 shrink_dcache_sb(sb);
1970 goto try_onemore;
1971 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001972 return err;
1973}
1974
1975static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1976 const char *dev_name, void *data)
1977{
1978 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1979}
1980
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001981static void kill_f2fs_super(struct super_block *sb)
1982{
1983 if (sb->s_root)
Chao Yucaf00472015-01-28 17:48:42 +08001984 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001985 kill_block_super(sb);
1986}
1987
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001988static struct file_system_type f2fs_fs_type = {
1989 .owner = THIS_MODULE,
1990 .name = "f2fs",
1991 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08001992 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001993 .fs_flags = FS_REQUIRES_DEV,
1994};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001995MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001996
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001997static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001998{
Vladimir Davydov5d097052016-01-14 15:18:21 -08001999 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2000 sizeof(struct f2fs_inode_info), 0,
2001 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002002 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002003 return -ENOMEM;
2004 return 0;
2005}
2006
2007static void destroy_inodecache(void)
2008{
2009 /*
2010 * Make sure all delayed rcu free inodes are flushed before we
2011 * destroy cache.
2012 */
2013 rcu_barrier();
2014 kmem_cache_destroy(f2fs_inode_cachep);
2015}
2016
2017static int __init init_f2fs_fs(void)
2018{
2019 int err;
2020
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002021 f2fs_build_trace_ios();
2022
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002023 err = init_inodecache();
2024 if (err)
2025 goto fail;
2026 err = create_node_manager_caches();
2027 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002028 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002029 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002030 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002031 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002032 err = create_checkpoint_caches();
2033 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002034 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002035 err = create_extent_cache();
2036 if (err)
2037 goto free_checkpoint_caches;
Chao Yu17a3fb52017-06-14 17:39:46 +08002038 err = f2fs_register_sysfs();
2039 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002040 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002041 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002042 if (err)
Chao Yu17a3fb52017-06-14 17:39:46 +08002043 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002044 err = register_filesystem(&f2fs_fs_type);
2045 if (err)
2046 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002047 err = f2fs_create_root_stats();
2048 if (err)
2049 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002050 return 0;
2051
Chao Yu787c7b8c2015-10-29 09:13:04 +08002052free_filesystem:
2053 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002054free_shrinker:
2055 unregister_shrinker(&f2fs_shrinker_info);
Chao Yu17a3fb52017-06-14 17:39:46 +08002056free_sysfs:
2057 f2fs_unregister_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08002058free_extent_cache:
2059 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002060free_checkpoint_caches:
2061 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002062free_segment_manager_caches:
2063 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002064free_node_manager_caches:
2065 destroy_node_manager_caches();
2066free_inodecache:
2067 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002068fail:
2069 return err;
2070}
2071
2072static void __exit exit_f2fs_fs(void)
2073{
Namjae Jeon4589d252013-01-15 19:58:47 +09002074 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002075 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08002076 unregister_shrinker(&f2fs_shrinker_info);
Chao Yu17a3fb52017-06-14 17:39:46 +08002077 f2fs_unregister_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08002078 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002079 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09002080 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002081 destroy_node_manager_caches();
2082 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08002083 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002084}
2085
2086module_init(init_f2fs_fs)
2087module_exit(exit_f2fs_fs)
2088
2089MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2090MODULE_DESCRIPTION("Flash Friendly File System");
2091MODULE_LICENSE("GPL");
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002092