blob: 32e4c025e97e556eafdefb887eab06d4217f1ddb [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>
Chao Yu0abd6752017-07-09 00:13:07 +080025#include <linux/quotaops.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090026#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090027#include <linux/sysfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090028
29#include "f2fs.h"
30#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090031#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090032#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090033#include "gc.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080034#include "trace.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090035
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090036#define CREATE_TRACE_POINTS
37#include <trace/events/f2fs.h>
38
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090039static struct kmem_cache *f2fs_inode_cachep;
40
Jaegeuk Kim73faec42016-04-29 15:34:32 -070041#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070042
43char *fault_name[FAULT_MAX] = {
44 [FAULT_KMALLOC] = "kmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070045 [FAULT_PAGE_ALLOC] = "page alloc",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070046 [FAULT_ALLOC_NID] = "alloc nid",
47 [FAULT_ORPHAN] = "orphan",
48 [FAULT_BLOCK] = "no more block",
49 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070050 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080051 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu8b038c72016-09-18 23:30:07 +080052 [FAULT_IO] = "IO error",
Chao Yu0f348022016-09-26 19:45:55 +080053 [FAULT_CHECKPOINT] = "checkpoint error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070054};
Sheng Yong08796892016-05-16 12:38:50 +080055
Chao Yu1ecc0c52016-09-23 21:30:09 +080056static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
57 unsigned int rate)
Sheng Yong08796892016-05-16 12:38:50 +080058{
Chao Yu1ecc0c52016-09-23 21:30:09 +080059 struct f2fs_fault_info *ffi = &sbi->fault_info;
60
Sheng Yong08796892016-05-16 12:38:50 +080061 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080062 atomic_set(&ffi->inject_ops, 0);
63 ffi->inject_rate = rate;
64 ffi->inject_type = (1 << FAULT_MAX) - 1;
Sheng Yong08796892016-05-16 12:38:50 +080065 } else {
Chao Yu1ecc0c52016-09-23 21:30:09 +080066 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080067 }
68}
Jaegeuk Kim73faec42016-04-29 15:34:32 -070069#endif
70
Jaegeuk Kim2658e502015-06-19 12:01:21 -070071/* f2fs-wide shrinker description */
72static struct shrinker f2fs_shrinker_info = {
73 .scan_objects = f2fs_shrink_scan,
74 .count_objects = f2fs_shrink_count,
75 .seeks = DEFAULT_SEEKS,
76};
77
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090078enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090079 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090080 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080081 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090082 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080083 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090084 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040085 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090086 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090087 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090088 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090089 Opt_noacl,
90 Opt_active_logs,
91 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +090092 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +080093 Opt_noinline_xattr,
Huajun Li8274de72013-11-10 23:13:17 +080094 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +080095 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +080096 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +090097 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -070098 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -070099 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700100 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800101 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700102 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800103 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800104 Opt_data_flush,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700105 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800106 Opt_io_size_bits,
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700107 Opt_fault_injection,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700108 Opt_lazytime,
109 Opt_nolazytime,
Chao Yu0abd6752017-07-09 00:13:07 +0800110 Opt_usrquota,
111 Opt_grpquota,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900112 Opt_err,
113};
114
115static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900116 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900117 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800118 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900119 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800120 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900121 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400122 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900123 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900124 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900125 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900126 {Opt_noacl, "noacl"},
127 {Opt_active_logs, "active_logs=%u"},
128 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900129 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800130 {Opt_noinline_xattr, "noinline_xattr"},
Huajun Li8274de72013-11-10 23:13:17 +0800131 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800132 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800133 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900134 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700135 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700136 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700137 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800138 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700139 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800140 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800141 {Opt_data_flush, "data_flush"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700142 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800143 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700144 {Opt_fault_injection, "fault_injection=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700145 {Opt_lazytime, "lazytime"},
146 {Opt_nolazytime, "nolazytime"},
Chao Yu0abd6752017-07-09 00:13:07 +0800147 {Opt_usrquota, "usrquota"},
148 {Opt_grpquota, "grpquota"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900149 {Opt_err, NULL},
150};
151
Namjae Jeona07ef782012-12-30 14:52:05 +0900152void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
153{
154 struct va_format vaf;
155 va_list args;
156
157 va_start(args, fmt);
158 vaf.fmt = fmt;
159 vaf.va = &args;
160 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
161 va_end(args);
162}
163
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900164static void init_once(void *foo)
165{
166 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
167
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900168 inode_init_once(&fi->vfs_inode);
169}
170
Namjae Jeon696c0182013-06-16 09:48:48 +0900171static int parse_options(struct super_block *sb, char *options)
172{
173 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu09d54cd2015-06-08 13:20:10 +0800174 struct request_queue *q;
Namjae Jeon696c0182013-06-16 09:48:48 +0900175 substring_t args[MAX_OPT_ARGS];
176 char *p, *name;
177 int arg = 0;
178
179 if (!options)
180 return 0;
181
182 while ((p = strsep(&options, ",")) != NULL) {
183 int token;
184 if (!*p)
185 continue;
186 /*
187 * Initialize args struct so we know whether arg was
188 * found; some options take optional arguments.
189 */
190 args[0].to = args[0].from = NULL;
191 token = match_token(p, f2fs_tokens, args);
192
193 switch (token) {
194 case Opt_gc_background:
195 name = match_strdup(&args[0]);
196
197 if (!name)
198 return -ENOMEM;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700199 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900200 set_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700201 clear_opt(sbi, FORCE_FG_GC);
202 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900203 clear_opt(sbi, BG_GC);
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700204 clear_opt(sbi, FORCE_FG_GC);
205 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
206 set_opt(sbi, BG_GC);
207 set_opt(sbi, FORCE_FG_GC);
208 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900209 kfree(name);
210 return -EINVAL;
211 }
212 kfree(name);
213 break;
214 case Opt_disable_roll_forward:
215 set_opt(sbi, DISABLE_ROLL_FORWARD);
216 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800217 case Opt_norecovery:
218 /* this option mounts f2fs with ro */
219 set_opt(sbi, DISABLE_ROLL_FORWARD);
220 if (!f2fs_readonly(sb))
221 return -EINVAL;
222 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900223 case Opt_discard:
Chao Yu09d54cd2015-06-08 13:20:10 +0800224 q = bdev_get_queue(sb->s_bdev);
225 if (blk_queue_discard(q)) {
226 set_opt(sbi, DISCARD);
Damien Le Moal0ab02992016-10-28 17:45:02 +0900227 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
Chao Yu09d54cd2015-06-08 13:20:10 +0800228 f2fs_msg(sb, KERN_WARNING,
229 "mounting with \"discard\" option, but "
230 "the device does not support discard");
231 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900232 break;
Chao Yu64058be2016-07-03 22:05:14 +0800233 case Opt_nodiscard:
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900234 if (f2fs_sb_mounted_blkzoned(sb)) {
235 f2fs_msg(sb, KERN_WARNING,
236 "discard is required for zoned block devices");
237 return -EINVAL;
238 }
Chao Yu64058be2016-07-03 22:05:14 +0800239 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900240 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900241 case Opt_noheap:
242 set_opt(sbi, NOHEAP);
243 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400244 case Opt_heap:
245 clear_opt(sbi, NOHEAP);
246 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900247#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900248 case Opt_user_xattr:
249 set_opt(sbi, XATTR_USER);
250 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900251 case Opt_nouser_xattr:
252 clear_opt(sbi, XATTR_USER);
253 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900254 case Opt_inline_xattr:
255 set_opt(sbi, INLINE_XATTR);
256 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800257 case Opt_noinline_xattr:
258 clear_opt(sbi, INLINE_XATTR);
259 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900260#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900261 case Opt_user_xattr:
262 f2fs_msg(sb, KERN_INFO,
263 "user_xattr options not supported");
264 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900265 case Opt_nouser_xattr:
266 f2fs_msg(sb, KERN_INFO,
267 "nouser_xattr options not supported");
268 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900269 case Opt_inline_xattr:
270 f2fs_msg(sb, KERN_INFO,
271 "inline_xattr options not supported");
272 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800273 case Opt_noinline_xattr:
274 f2fs_msg(sb, KERN_INFO,
275 "noinline_xattr options not supported");
276 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900277#endif
278#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900279 case Opt_acl:
280 set_opt(sbi, POSIX_ACL);
281 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900282 case Opt_noacl:
283 clear_opt(sbi, POSIX_ACL);
284 break;
285#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900286 case Opt_acl:
287 f2fs_msg(sb, KERN_INFO, "acl options not supported");
288 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900289 case Opt_noacl:
290 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
291 break;
292#endif
293 case Opt_active_logs:
294 if (args->from && match_int(args, &arg))
295 return -EINVAL;
296 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
297 return -EINVAL;
298 sbi->active_logs = arg;
299 break;
300 case Opt_disable_ext_identify:
301 set_opt(sbi, DISABLE_EXT_IDENTIFY);
302 break;
Huajun Li8274de72013-11-10 23:13:17 +0800303 case Opt_inline_data:
304 set_opt(sbi, INLINE_DATA);
305 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800306 case Opt_inline_dentry:
307 set_opt(sbi, INLINE_DENTRY);
308 break;
Chao Yu97c17942016-05-09 19:56:34 +0800309 case Opt_noinline_dentry:
310 clear_opt(sbi, INLINE_DENTRY);
311 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900312 case Opt_flush_merge:
313 set_opt(sbi, FLUSH_MERGE);
314 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700315 case Opt_noflush_merge:
316 clear_opt(sbi, FLUSH_MERGE);
317 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700318 case Opt_nobarrier:
319 set_opt(sbi, NOBARRIER);
320 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700321 case Opt_fastboot:
322 set_opt(sbi, FASTBOOT);
323 break;
Chao Yu89672152015-02-05 17:55:51 +0800324 case Opt_extent_cache:
325 set_opt(sbi, EXTENT_CACHE);
326 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700327 case Opt_noextent_cache:
328 clear_opt(sbi, EXTENT_CACHE);
329 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800330 case Opt_noinline_data:
331 clear_opt(sbi, INLINE_DATA);
332 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800333 case Opt_data_flush:
334 set_opt(sbi, DATA_FLUSH);
335 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700336 case Opt_mode:
337 name = match_strdup(&args[0]);
338
339 if (!name)
340 return -ENOMEM;
341 if (strlen(name) == 8 &&
342 !strncmp(name, "adaptive", 8)) {
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900343 if (f2fs_sb_mounted_blkzoned(sb)) {
344 f2fs_msg(sb, KERN_WARNING,
345 "adaptive mode is not allowed with "
346 "zoned block device feature");
347 kfree(name);
348 return -EINVAL;
349 }
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700350 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700351 } else if (strlen(name) == 3 &&
352 !strncmp(name, "lfs", 3)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700353 set_opt_mode(sbi, F2FS_MOUNT_LFS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700354 } else {
355 kfree(name);
356 return -EINVAL;
357 }
358 kfree(name);
359 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800360 case Opt_io_size_bits:
361 if (args->from && match_int(args, &arg))
362 return -EINVAL;
363 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
364 f2fs_msg(sb, KERN_WARNING,
365 "Not support %d, larger than %d",
366 1 << arg, BIO_MAX_PAGES);
367 return -EINVAL;
368 }
369 sbi->write_io_size_bits = arg;
370 break;
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700371 case Opt_fault_injection:
372 if (args->from && match_int(args, &arg))
373 return -EINVAL;
374#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +0800375 f2fs_build_fault_attr(sbi, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800376 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec42016-04-29 15:34:32 -0700377#else
378 f2fs_msg(sb, KERN_INFO,
379 "FAULT_INJECTION was not selected");
380#endif
381 break;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700382 case Opt_lazytime:
383 sb->s_flags |= MS_LAZYTIME;
384 break;
385 case Opt_nolazytime:
386 sb->s_flags &= ~MS_LAZYTIME;
387 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800388#ifdef CONFIG_QUOTA
389 case Opt_usrquota:
390 set_opt(sbi, USRQUOTA);
391 break;
392 case Opt_grpquota:
393 set_opt(sbi, GRPQUOTA);
394 break;
395#else
396 case Opt_usrquota:
397 case Opt_grpquota:
398 f2fs_msg(sb, KERN_INFO,
399 "quota operations not supported");
400 break;
401#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900402 default:
403 f2fs_msg(sb, KERN_ERR,
404 "Unrecognized mount option \"%s\" or missing value",
405 p);
406 return -EINVAL;
407 }
408 }
Jaegeuk Kimec915382016-12-21 17:09:19 -0800409
410 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
411 f2fs_msg(sb, KERN_ERR,
412 "Should set mode=lfs with %uKB-sized IO",
413 F2FS_IO_SIZE_KB(sbi));
414 return -EINVAL;
415 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900416 return 0;
417}
418
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900419static struct inode *f2fs_alloc_inode(struct super_block *sb)
420{
421 struct f2fs_inode_info *fi;
422
Chao Yua0acdfe2013-12-05 09:54:00 +0800423 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900424 if (!fi)
425 return NULL;
426
427 init_once((void *) fi);
428
Masanari Iida434720f2013-03-19 08:03:35 +0900429 /* Initialize f2fs-specific inode info */
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900430 fi->vfs_inode.i_version = 1;
Jaegeuk Kim204706c2016-12-02 15:11:32 -0800431 atomic_set(&fi->dirty_pages, 0);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900432 fi->i_current_depth = 1;
433 fi->i_advise = 0;
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900434 init_rwsem(&fi->i_sem);
Chao Yu2710fd72015-12-15 13:30:45 +0800435 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700436 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700437 INIT_LIST_HEAD(&fi->inmem_pages);
438 mutex_init(&fi->inmem_lock);
Chao Yu82e0a5a2016-07-13 09:18:29 +0800439 init_rwsem(&fi->dio_rwsem[READ]);
440 init_rwsem(&fi->dio_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +0800441 init_rwsem(&fi->i_mmap_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900442
Chao Yu0abd6752017-07-09 00:13:07 +0800443#ifdef CONFIG_QUOTA
444 memset(&fi->i_dquot, 0, sizeof(fi->i_dquot));
445 fi->i_reserved_quota = 0;
446#endif
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900447 /* Will be used by directory only */
448 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900449 return &fi->vfs_inode;
450}
451
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900452static int f2fs_drop_inode(struct inode *inode)
453{
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000454 int ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900455 /*
456 * This is to avoid a deadlock condition like below.
457 * writeback_single_inode(inode)
458 * - f2fs_write_data_page
459 * - f2fs_gc -> iput -> evict
460 * - inode_wait_for_writeback(inode)
461 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700462 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700463 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700464 /* to avoid evict_inode call simultaneously */
465 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700466 spin_unlock(&inode->i_lock);
467
468 /* some remained atomic pages should discarded */
469 if (f2fs_is_atomic_file(inode))
Chao Yu29b96b52016-02-06 14:38:29 +0800470 drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700471
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700472 /* should remain fi->extent_tree for writepage */
473 f2fs_destroy_extent_node(inode);
474
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700475 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700476 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700477
478 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -0700479 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700480
481 sb_end_intwrite(inode->i_sb);
482
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700483 fscrypt_put_encryption_info(inode, NULL);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700484 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700485 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700486 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000487 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900488 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -0700489 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +0000490 ret = generic_drop_inode(inode);
491 trace_f2fs_drop_inode(inode, ret);
492 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900493}
494
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700495int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700496{
497 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700498 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700499
500 spin_lock(&sbi->inode_lock[DIRTY_META]);
501 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700502 ret = 1;
503 } else {
504 set_inode_flag(inode, FI_DIRTY_INODE);
505 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700506 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700507 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
508 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700509 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700510 inc_page_count(sbi, F2FS_DIRTY_IMETA);
511 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700512 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700513 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700514}
515
516void f2fs_inode_synced(struct inode *inode)
517{
518 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
519
520 spin_lock(&sbi->inode_lock[DIRTY_META]);
521 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
522 spin_unlock(&sbi->inode_lock[DIRTY_META]);
523 return;
524 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700525 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
526 list_del_init(&F2FS_I(inode)->gdirty_list);
527 dec_page_count(sbi, F2FS_DIRTY_IMETA);
528 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700529 clear_inode_flag(inode, FI_DIRTY_INODE);
530 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700531 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
532 spin_unlock(&sbi->inode_lock[DIRTY_META]);
533}
534
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900535/*
536 * f2fs_dirty_inode() is called from __mark_inode_dirty()
537 *
538 * We should call set_dirty_inode to write the dirty inode through write_inode.
539 */
540static void f2fs_dirty_inode(struct inode *inode, int flags)
541{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700542 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
543
544 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
545 inode->i_ino == F2FS_META_INO(sbi))
546 return;
547
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700548 if (flags == I_DIRTY_TIME)
549 return;
550
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700551 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
552 clear_inode_flag(inode, FI_AUTO_RECOVER);
553
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700554 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +0900555}
556
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900557static void f2fs_i_callback(struct rcu_head *head)
558{
559 struct inode *inode = container_of(head, struct inode, i_rcu);
560 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
561}
562
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900563static void f2fs_destroy_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900564{
565 call_rcu(&inode->i_rcu, f2fs_i_callback);
566}
567
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700568static void destroy_percpu_info(struct f2fs_sb_info *sbi)
569{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700570 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700571 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700572}
573
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700574static void destroy_device_list(struct f2fs_sb_info *sbi)
575{
576 int i;
577
578 for (i = 0; i < sbi->s_ndevs; i++) {
579 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
580#ifdef CONFIG_BLK_DEV_ZONED
581 kfree(FDEV(i).blkz_type);
582#endif
583 }
584 kfree(sbi->devs);
585}
586
Chao Yu0abd6752017-07-09 00:13:07 +0800587static void f2fs_quota_off_umount(struct super_block *sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900588static void f2fs_put_super(struct super_block *sb)
589{
590 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +0800591 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900592
Chao Yu0abd6752017-07-09 00:13:07 +0800593 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900594
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700595 /* prevent remaining shrinker jobs */
596 mutex_lock(&sbi->umount_mutex);
597
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -0800598 /*
599 * We don't need to do checkpoint when superblock is clean.
600 * But, the previous checkpoint was not done by umount, it needs to do
601 * clean checkpoint again.
602 */
Chao Yucaf00472015-01-28 17:48:42 +0800603 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
Chao Yuaaec2b12016-09-20 11:04:18 +0800604 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700605 struct cp_control cpc = {
606 .reason = CP_UMOUNT,
607 };
608 write_checkpoint(sbi, &cpc);
609 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900610
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800611 /* be sure to wait for any on-going discard commands */
Chao Yud4314132017-04-05 18:19:49 +0800612 f2fs_wait_discard_bios(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -0800613
Chao Yu663f3872017-06-14 23:00:55 +0800614 if (f2fs_discard_en(sbi) && !sbi->discard_blks) {
Chao Yu1f43e2a2017-04-28 13:56:08 +0800615 struct cp_control cpc = {
616 .reason = CP_UMOUNT | CP_TRIMMED,
617 };
618 write_checkpoint(sbi, &cpc);
619 }
620
Jaegeuk Kimeca616f2015-06-15 14:52:29 -0700621 /* write_checkpoint can update stat informaion */
622 f2fs_destroy_stats(sbi);
623
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -0700624 /*
625 * normally superblock is clean, so we need to release this.
626 * In addition, EIO will skip do checkpoint, we need this as well.
627 */
Jaegeuk Kim74ef9242016-05-02 22:09:56 -0700628 release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -0700629
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700630 f2fs_leave_shrinker(sbi);
631 mutex_unlock(&sbi->umount_mutex);
632
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800633 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700634 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -0800635
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900636 iput(sbi->node_inode);
637 iput(sbi->meta_inode);
638
639 /* destroy f2fs internal modules */
640 destroy_node_manager(sbi);
641 destroy_segment_manager(sbi);
642
643 kfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +0800644
645 f2fs_exit_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900646
647 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -0800648 if (sbi->s_chksum_driver)
649 crypto_free_shash(sbi->s_chksum_driver);
Yunlei Heb39f0de2015-12-15 17:17:20 +0800650 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700651
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700652 destroy_device_list(sbi);
Chao Yub6895e82017-02-27 18:43:12 +0800653 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700654 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700655 for (i = 0; i < NR_PAGE_TYPE; i++)
656 kfree(sbi->write_io[i]);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900657 kfree(sbi);
658}
659
660int f2fs_sync_fs(struct super_block *sb, int sync)
661{
662 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +0800663 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900664
Namjae Jeona2a4a7e2013-04-20 01:28:40 +0900665 trace_f2fs_sync_fs(sb, sync);
666
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900667 if (sync) {
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700668 struct cp_control cpc;
669
Jaegeuk Kim119ee912015-01-29 11:45:33 -0800670 cpc.reason = __get_cp_reason(sbi);
671
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900672 mutex_lock(&sbi->gc_mutex);
Chao Yuc34f42e2015-12-23 17:50:30 +0800673 err = write_checkpoint(sbi, &cpc);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900674 mutex_unlock(&sbi->gc_mutex);
Jaegeuk Kimb7473752013-04-01 08:32:21 +0900675 }
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700676 f2fs_trace_ios(NULL, 1);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900677
Chao Yuc34f42e2015-12-23 17:50:30 +0800678 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900679}
680
Changman Leed6212a52013-01-29 18:30:07 +0900681static int f2fs_freeze(struct super_block *sb)
682{
Jaegeuk Kim77888c12013-05-20 20:28:47 +0900683 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +0900684 return 0;
685
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -0700686 /* IO error happened before */
687 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
688 return -EIO;
689
690 /* must be clean, since sync_filesystem() was already called */
691 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
692 return -EINVAL;
693 return 0;
Changman Leed6212a52013-01-29 18:30:07 +0900694}
695
696static int f2fs_unfreeze(struct super_block *sb)
697{
698 return 0;
699}
700
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900701static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
702{
703 struct super_block *sb = dentry->d_sb;
704 struct f2fs_sb_info *sbi = F2FS_SB(sb);
705 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
706 block_t total_count, user_block_count, start_count, ovp_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700707 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900708
709 total_count = le64_to_cpu(sbi->raw_super->block_count);
710 user_block_count = sbi->user_block_count;
711 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
712 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
713 buf->f_type = F2FS_SUPER_MAGIC;
714 buf->f_bsize = sbi->blocksize;
715
716 buf->f_blocks = total_count - start_count;
Chao Yu3e6d0b42016-07-06 14:13:07 +0800717 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
Chao Yudaeb4332017-06-26 16:24:41 +0800718 buf->f_bavail = user_block_count - valid_user_blocks(sbi) -
719 sbi->reserved_blocks;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900720
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -0700721 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
722
723 if (avail_node_count > user_block_count) {
724 buf->f_files = user_block_count;
725 buf->f_ffree = buf->f_bavail;
726 } else {
727 buf->f_files = avail_node_count;
728 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
729 buf->f_bavail);
730 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900731
Jaegeuk Kim5a20d332013-03-03 13:58:05 +0900732 buf->f_namelen = F2FS_NAME_LEN;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900733 buf->f_fsid.val[0] = (u32)id;
734 buf->f_fsid.val[1] = (u32)(id >> 32);
735
736 return 0;
737}
738
739static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
740{
741 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
742
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700743 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
744 if (test_opt(sbi, FORCE_FG_GC))
745 seq_printf(seq, ",background_gc=%s", "sync");
746 else
747 seq_printf(seq, ",background_gc=%s", "on");
748 } else {
Namjae Jeon696c0182013-06-16 09:48:48 +0900749 seq_printf(seq, ",background_gc=%s", "off");
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700750 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900751 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
752 seq_puts(seq, ",disable_roll_forward");
753 if (test_opt(sbi, DISCARD))
754 seq_puts(seq, ",discard");
755 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400756 seq_puts(seq, ",no_heap");
757 else
758 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900759#ifdef CONFIG_F2FS_FS_XATTR
760 if (test_opt(sbi, XATTR_USER))
761 seq_puts(seq, ",user_xattr");
762 else
763 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900764 if (test_opt(sbi, INLINE_XATTR))
765 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +0800766 else
767 seq_puts(seq, ",noinline_xattr");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900768#endif
769#ifdef CONFIG_F2FS_FS_POSIX_ACL
770 if (test_opt(sbi, POSIX_ACL))
771 seq_puts(seq, ",acl");
772 else
773 seq_puts(seq, ",noacl");
774#endif
775 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +0100776 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +0800777 if (test_opt(sbi, INLINE_DATA))
778 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +0800779 else
780 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +0800781 if (test_opt(sbi, INLINE_DENTRY))
782 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +0800783 else
784 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +0800785 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900786 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700787 if (test_opt(sbi, NOBARRIER))
788 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700789 if (test_opt(sbi, FASTBOOT))
790 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +0800791 if (test_opt(sbi, EXTENT_CACHE))
792 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700793 else
794 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +0800795 if (test_opt(sbi, DATA_FLUSH))
796 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700797
798 seq_puts(seq, ",mode=");
799 if (test_opt(sbi, ADAPTIVE))
800 seq_puts(seq, "adaptive");
801 else if (test_opt(sbi, LFS))
802 seq_puts(seq, "lfs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900803 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
Jaegeuk Kimec915382016-12-21 17:09:19 -0800804 if (F2FS_IO_SIZE_BITS(sbi))
805 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800806#ifdef CONFIG_F2FS_FAULT_INJECTION
807 if (test_opt(sbi, FAULT_INJECTION))
Chao Yu44529f82017-06-12 09:44:24 +0800808 seq_printf(seq, ",fault_injection=%u",
809 sbi->fault_info.inject_rate);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800810#endif
Chao Yu0abd6752017-07-09 00:13:07 +0800811#ifdef CONFIG_QUOTA
812 if (test_opt(sbi, USRQUOTA))
813 seq_puts(seq, ",usrquota");
814 if (test_opt(sbi, GRPQUOTA))
815 seq_puts(seq, ",grpquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900816#endif
817
818 return 0;
819}
820
Yunlei He498c5e92015-05-07 18:11:37 +0800821static void default_options(struct f2fs_sb_info *sbi)
822{
823 /* init some FS parameters */
824 sbi->active_logs = NR_CURSEG_TYPE;
825
826 set_opt(sbi, BG_GC);
Chao Yu39133a52017-02-08 17:39:44 +0800827 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +0800828 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +0800829 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700830 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400831 set_opt(sbi, NOHEAP);
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700832 sbi->sb->s_flags |= MS_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700833 set_opt(sbi, FLUSH_MERGE);
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900834 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700835 set_opt_mode(sbi, F2FS_MOUNT_LFS);
836 set_opt(sbi, DISCARD);
837 } else {
838 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
839 }
Yunlei He498c5e92015-05-07 18:11:37 +0800840
841#ifdef CONFIG_F2FS_FS_XATTR
842 set_opt(sbi, XATTR_USER);
843#endif
844#ifdef CONFIG_F2FS_FS_POSIX_ACL
845 set_opt(sbi, POSIX_ACL);
846#endif
Chao Yu36dbd322016-09-26 19:45:05 +0800847
848#ifdef CONFIG_F2FS_FAULT_INJECTION
849 f2fs_build_fault_attr(sbi, 0);
850#endif
Yunlei He498c5e92015-05-07 18:11:37 +0800851}
852
Namjae Jeon696c0182013-06-16 09:48:48 +0900853static int f2fs_remount(struct super_block *sb, int *flags, char *data)
854{
855 struct f2fs_sb_info *sbi = F2FS_SB(sb);
856 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +0800857 unsigned long old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +0900858 int err, active_logs;
Gu Zheng876dc592014-04-11 17:50:00 +0800859 bool need_restart_gc = false;
860 bool need_stop_gc = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +0800861 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Chao Yu2443b8b2016-09-26 19:45:06 +0800862#ifdef CONFIG_F2FS_FAULT_INJECTION
863 struct f2fs_fault_info ffi = sbi->fault_info;
864#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900865
866 /*
867 * Save the old mount options in case we
868 * need to restore them.
869 */
870 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +0800871 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +0900872 active_logs = sbi->active_logs;
873
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700874 /* recover superblocks we couldn't write due to previous RO mount */
875 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
876 err = f2fs_commit_super(sbi, false);
877 f2fs_msg(sb, KERN_INFO,
878 "Try to recover all the superblocks, ret: %d", err);
879 if (!err)
880 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
881 }
882
Yunlei He498c5e92015-05-07 18:11:37 +0800883 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +0800884
Namjae Jeon696c0182013-06-16 09:48:48 +0900885 /* parse mount options */
886 err = parse_options(sb, data);
887 if (err)
888 goto restore_opts;
889
890 /*
891 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +0800892 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +0900893 */
Chao Yu6b2920a2014-07-07 11:21:59 +0800894 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +0900895 goto skip;
896
Chao Yu0abd6752017-07-09 00:13:07 +0800897 if (!f2fs_readonly(sb) && (*flags & MS_RDONLY)) {
898 err = dquot_suspend(sb, -1);
899 if (err < 0)
900 goto restore_opts;
901 } else {
902 /* dquot_resume needs RW */
903 sb->s_flags &= ~MS_RDONLY;
904 dquot_resume(sb, -1);
905 }
906
Chao Yu9cd81ce2015-09-18 16:55:26 +0800907 /* disallow enable/disable extent_cache dynamically */
908 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
909 err = -EINVAL;
910 f2fs_msg(sbi->sb, KERN_WARNING,
911 "switch extent_cache option is not allowed");
912 goto restore_opts;
913 }
914
Namjae Jeon696c0182013-06-16 09:48:48 +0900915 /*
916 * We stop the GC thread if FS is mounted as RO
917 * or if background_gc = off is passed in mount
918 * option. Also sync the filesystem.
919 */
920 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
921 if (sbi->gc_thread) {
922 stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +0800923 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +0900924 }
Chao Yuaba291b2014-11-18 11:17:20 +0800925 } else if (!sbi->gc_thread) {
Namjae Jeon696c0182013-06-16 09:48:48 +0900926 err = start_gc_thread(sbi);
927 if (err)
928 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +0800929 need_stop_gc = true;
930 }
931
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -0700932 if (*flags & MS_RDONLY) {
933 writeback_inodes_sb(sb, WB_REASON_SYNC);
934 sync_inodes_sb(sb);
935
936 set_sbi_flag(sbi, SBI_IS_DIRTY);
937 set_sbi_flag(sbi, SBI_IS_CLOSE);
938 f2fs_sync_fs(sb, 1);
939 clear_sbi_flag(sbi, SBI_IS_CLOSE);
940 }
941
Gu Zheng876dc592014-04-11 17:50:00 +0800942 /*
943 * We stop issue flush thread if FS is mounted as RO
944 * or if flush_merge is not passed in mount option.
945 */
946 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -0800947 clear_opt(sbi, FLUSH_MERGE);
948 destroy_flush_cmd_control(sbi, false);
949 } else {
Gu Zheng2163d192014-04-27 14:21:33 +0800950 err = create_flush_cmd_control(sbi);
951 if (err)
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800952 goto restore_gc;
Namjae Jeon696c0182013-06-16 09:48:48 +0900953 }
954skip:
955 /* Update the POSIXACL Flag */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700956 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Namjae Jeon696c0182013-06-16 09:48:48 +0900957 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700958
Namjae Jeon696c0182013-06-16 09:48:48 +0900959 return 0;
Gu Zheng876dc592014-04-11 17:50:00 +0800960restore_gc:
961 if (need_restart_gc) {
962 if (start_gc_thread(sbi))
963 f2fs_msg(sbi->sb, KERN_WARNING,
arter97e1c42042014-08-06 23:22:50 +0900964 "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +0800965 } else if (need_stop_gc) {
966 stop_gc_thread(sbi);
967 }
Namjae Jeon696c0182013-06-16 09:48:48 +0900968restore_opts:
969 sbi->mount_opt = org_mount_opt;
970 sbi->active_logs = active_logs;
Chao Yu0abd6752017-07-09 00:13:07 +0800971 sb->s_flags = old_sb_flags;
Chao Yu2443b8b2016-09-26 19:45:06 +0800972#ifdef CONFIG_F2FS_FAULT_INJECTION
973 sbi->fault_info = ffi;
974#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900975 return err;
976}
977
Chao Yu0abd6752017-07-09 00:13:07 +0800978#ifdef CONFIG_QUOTA
979/* Read data from quotafile */
980static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
981 size_t len, loff_t off)
982{
983 struct inode *inode = sb_dqopt(sb)->files[type];
984 struct address_space *mapping = inode->i_mapping;
985 block_t blkidx = F2FS_BYTES_TO_BLK(off);
986 int offset = off & (sb->s_blocksize - 1);
987 int tocopy;
988 size_t toread;
989 loff_t i_size = i_size_read(inode);
990 struct page *page;
991 char *kaddr;
992
993 if (off > i_size)
994 return 0;
995
996 if (off + len > i_size)
997 len = i_size - off;
998 toread = len;
999 while (toread > 0) {
1000 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1001repeat:
1002 page = read_mapping_page(mapping, blkidx, NULL);
1003 if (IS_ERR(page))
1004 return PTR_ERR(page);
1005
1006 lock_page(page);
1007
1008 if (unlikely(page->mapping != mapping)) {
1009 f2fs_put_page(page, 1);
1010 goto repeat;
1011 }
1012 if (unlikely(!PageUptodate(page))) {
1013 f2fs_put_page(page, 1);
1014 return -EIO;
1015 }
1016
1017 kaddr = kmap_atomic(page);
1018 memcpy(data, kaddr + offset, tocopy);
1019 kunmap_atomic(kaddr);
1020 f2fs_put_page(page, 1);
1021
1022 offset = 0;
1023 toread -= tocopy;
1024 data += tocopy;
1025 blkidx++;
1026 }
1027 return len;
1028}
1029
1030/* Write to quotafile */
1031static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1032 const char *data, size_t len, loff_t off)
1033{
1034 struct inode *inode = sb_dqopt(sb)->files[type];
1035 struct address_space *mapping = inode->i_mapping;
1036 const struct address_space_operations *a_ops = mapping->a_ops;
1037 int offset = off & (sb->s_blocksize - 1);
1038 size_t towrite = len;
1039 struct page *page;
1040 char *kaddr;
1041 int err = 0;
1042 int tocopy;
1043
1044 while (towrite > 0) {
1045 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1046 towrite);
1047
1048 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1049 &page, NULL);
1050 if (unlikely(err))
1051 break;
1052
1053 kaddr = kmap_atomic(page);
1054 memcpy(kaddr + offset, data, tocopy);
1055 kunmap_atomic(kaddr);
1056 flush_dcache_page(page);
1057
1058 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1059 page, NULL);
1060 offset = 0;
1061 towrite -= tocopy;
1062 off += tocopy;
1063 data += tocopy;
1064 cond_resched();
1065 }
1066
1067 if (len == towrite)
1068 return err;
1069 inode->i_version++;
1070 inode->i_mtime = inode->i_ctime = current_time(inode);
1071 f2fs_mark_inode_dirty_sync(inode, false);
1072 return len - towrite;
1073}
1074
1075static struct dquot **f2fs_get_dquots(struct inode *inode)
1076{
1077 return F2FS_I(inode)->i_dquot;
1078}
1079
1080static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1081{
1082 return &F2FS_I(inode)->i_reserved_quota;
1083}
1084
1085static int f2fs_quota_sync(struct super_block *sb, int type)
1086{
1087 struct quota_info *dqopt = sb_dqopt(sb);
1088 int cnt;
1089 int ret;
1090
1091 ret = dquot_writeback_dquots(sb, type);
1092 if (ret)
1093 return ret;
1094
1095 /*
1096 * Now when everything is written we can discard the pagecache so
1097 * that userspace sees the changes.
1098 */
1099 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1100 if (type != -1 && cnt != type)
1101 continue;
1102 if (!sb_has_quota_active(sb, cnt))
1103 continue;
1104
1105 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1106 if (ret)
1107 return ret;
1108
1109 inode_lock(dqopt->files[cnt]);
1110 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1111 inode_unlock(dqopt->files[cnt]);
1112 }
1113 return 0;
1114}
1115
1116static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1117 const struct path *path)
1118{
1119 struct inode *inode;
1120 int err;
1121
1122 err = f2fs_quota_sync(sb, -1);
1123 if (err)
1124 return err;
1125
1126 err = dquot_quota_on(sb, type, format_id, path);
1127 if (err)
1128 return err;
1129
1130 inode = d_inode(path->dentry);
1131
1132 inode_lock(inode);
1133 F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL;
1134 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1135 S_NOATIME | S_IMMUTABLE);
1136 inode_unlock(inode);
1137 f2fs_mark_inode_dirty_sync(inode, false);
1138
1139 return 0;
1140}
1141
1142static int f2fs_quota_off(struct super_block *sb, int type)
1143{
1144 struct inode *inode = sb_dqopt(sb)->files[type];
1145 int err;
1146
1147 if (!inode || !igrab(inode))
1148 return dquot_quota_off(sb, type);
1149
1150 f2fs_quota_sync(sb, -1);
1151
1152 err = dquot_quota_off(sb, type);
1153 if (err)
1154 goto out_put;
1155
1156 inode_lock(inode);
1157 F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL);
1158 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1159 inode_unlock(inode);
1160 f2fs_mark_inode_dirty_sync(inode, false);
1161out_put:
1162 iput(inode);
1163 return err;
1164}
1165
1166static void f2fs_quota_off_umount(struct super_block *sb)
1167{
1168 int type;
1169
1170 for (type = 0; type < MAXQUOTAS; type++)
1171 f2fs_quota_off(sb, type);
1172}
1173
1174static const struct dquot_operations f2fs_quota_operations = {
1175 .get_reserved_space = f2fs_get_reserved_space,
1176 .write_dquot = dquot_commit,
1177 .acquire_dquot = dquot_acquire,
1178 .release_dquot = dquot_release,
1179 .mark_dirty = dquot_mark_dquot_dirty,
1180 .write_info = dquot_commit_info,
1181 .alloc_dquot = dquot_alloc,
1182 .destroy_dquot = dquot_destroy,
1183 .get_next_id = dquot_get_next_id,
1184};
1185
1186static const struct quotactl_ops f2fs_quotactl_ops = {
1187 .quota_on = f2fs_quota_on,
1188 .quota_off = f2fs_quota_off,
1189 .quota_sync = f2fs_quota_sync,
1190 .get_state = dquot_get_state,
1191 .set_info = dquot_set_dqinfo,
1192 .get_dqblk = dquot_get_dqblk,
1193 .set_dqblk = dquot_set_dqblk,
1194 .get_nextdqblk = dquot_get_next_dqblk,
1195};
1196#else
1197static inline void f2fs_quota_off_umount(struct super_block *sb)
1198{
1199}
1200#endif
1201
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001202static struct super_operations f2fs_sops = {
1203 .alloc_inode = f2fs_alloc_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001204 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001205 .destroy_inode = f2fs_destroy_inode,
1206 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001207 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001208 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08001209#ifdef CONFIG_QUOTA
1210 .quota_read = f2fs_quota_read,
1211 .quota_write = f2fs_quota_write,
1212 .get_dquots = f2fs_get_dquots,
1213#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001214 .evict_inode = f2fs_evict_inode,
1215 .put_super = f2fs_put_super,
1216 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09001217 .freeze_fs = f2fs_freeze,
1218 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001219 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09001220 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001221};
1222
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001223#ifdef CONFIG_F2FS_FS_ENCRYPTION
1224static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1225{
1226 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1227 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1228 ctx, len, NULL);
1229}
1230
1231static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1232 void *fs_data)
1233{
1234 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1235 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1236 ctx, len, fs_data, XATTR_CREATE);
1237}
1238
1239static unsigned f2fs_max_namelen(struct inode *inode)
1240{
1241 return S_ISLNK(inode->i_mode) ?
1242 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1243}
1244
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001245static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001246 .key_prefix = "f2fs:",
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001247 .get_context = f2fs_get_context,
1248 .set_context = f2fs_set_context,
1249 .is_encrypted = f2fs_encrypted_inode,
1250 .empty_dir = f2fs_empty_dir,
1251 .max_namelen = f2fs_max_namelen,
1252};
1253#else
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001254static const struct fscrypt_operations f2fs_cryptops = {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001255 .is_encrypted = f2fs_encrypted_inode,
1256};
1257#endif
1258
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001259static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
1260 u64 ino, u32 generation)
1261{
1262 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1263 struct inode *inode;
1264
Chao Yud6b7d4b2014-06-12 13:23:41 +08001265 if (check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08001266 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001267
1268 /*
1269 * f2fs_iget isn't quite right if the inode is currently unallocated!
1270 * However f2fs_iget currently does appropriate checks to handle stale
1271 * inodes so everything is OK.
1272 */
1273 inode = f2fs_iget(sb, ino);
1274 if (IS_ERR(inode))
1275 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001276 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001277 /* we didn't find the right inode.. */
1278 iput(inode);
1279 return ERR_PTR(-ESTALE);
1280 }
1281 return inode;
1282}
1283
1284static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1285 int fh_len, int fh_type)
1286{
1287 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1288 f2fs_nfs_get_inode);
1289}
1290
1291static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1292 int fh_len, int fh_type)
1293{
1294 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1295 f2fs_nfs_get_inode);
1296}
1297
1298static const struct export_operations f2fs_export_ops = {
1299 .fh_to_dentry = f2fs_fh_to_dentry,
1300 .fh_to_parent = f2fs_fh_to_parent,
1301 .get_parent = f2fs_get_parent,
1302};
1303
Chao Yue0afc4d2015-12-31 14:35:37 +08001304static loff_t max_file_blocks(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001305{
Jaegeuk Kimde936532013-08-12 21:08:03 +09001306 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001307 loff_t leaf_count = ADDRS_PER_BLOCK;
1308
1309 /* two direct node blocks */
1310 result += (leaf_count * 2);
1311
1312 /* two indirect node blocks */
1313 leaf_count *= NIDS_PER_BLOCK;
1314 result += (leaf_count * 2);
1315
1316 /* one double indirect node block */
1317 leaf_count *= NIDS_PER_BLOCK;
1318 result += leaf_count;
1319
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001320 return result;
1321}
1322
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001323static int __f2fs_commit_super(struct buffer_head *bh,
1324 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08001325{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001326 lock_buffer(bh);
1327 if (super)
1328 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1329 set_buffer_uptodate(bh);
1330 set_buffer_dirty(bh);
1331 unlock_buffer(bh);
1332
1333 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02001334 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001335}
1336
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001337static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001338 struct buffer_head *bh)
1339{
1340 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1341 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001342 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08001343 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1344 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1345 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1346 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1347 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1348 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1349 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1350 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1351 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1352 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1353 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1354 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1355 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001356 u64 main_end_blkaddr = main_blkaddr +
1357 (segment_count_main << log_blocks_per_seg);
1358 u64 seg_end_blkaddr = segment0_blkaddr +
1359 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08001360
1361 if (segment0_blkaddr != cp_blkaddr) {
1362 f2fs_msg(sb, KERN_INFO,
1363 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1364 segment0_blkaddr, cp_blkaddr);
1365 return true;
1366 }
1367
1368 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1369 sit_blkaddr) {
1370 f2fs_msg(sb, KERN_INFO,
1371 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1372 cp_blkaddr, sit_blkaddr,
1373 segment_count_ckpt << log_blocks_per_seg);
1374 return true;
1375 }
1376
1377 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1378 nat_blkaddr) {
1379 f2fs_msg(sb, KERN_INFO,
1380 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1381 sit_blkaddr, nat_blkaddr,
1382 segment_count_sit << log_blocks_per_seg);
1383 return true;
1384 }
1385
1386 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1387 ssa_blkaddr) {
1388 f2fs_msg(sb, KERN_INFO,
1389 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1390 nat_blkaddr, ssa_blkaddr,
1391 segment_count_nat << log_blocks_per_seg);
1392 return true;
1393 }
1394
1395 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1396 main_blkaddr) {
1397 f2fs_msg(sb, KERN_INFO,
1398 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1399 ssa_blkaddr, main_blkaddr,
1400 segment_count_ssa << log_blocks_per_seg);
1401 return true;
1402 }
1403
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001404 if (main_end_blkaddr > seg_end_blkaddr) {
Chao Yu9a59b622015-12-15 09:58:18 +08001405 f2fs_msg(sb, KERN_INFO,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001406 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
Chao Yu9a59b622015-12-15 09:58:18 +08001407 main_blkaddr,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001408 segment0_blkaddr +
1409 (segment_count << log_blocks_per_seg),
Chao Yu9a59b622015-12-15 09:58:18 +08001410 segment_count_main << log_blocks_per_seg);
1411 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001412 } else if (main_end_blkaddr < seg_end_blkaddr) {
1413 int err = 0;
1414 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08001415
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001416 /* fix in-memory information all the time */
1417 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1418 segment0_blkaddr) >> log_blocks_per_seg);
1419
1420 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001421 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001422 res = "internally";
1423 } else {
1424 err = __f2fs_commit_super(bh, NULL);
1425 res = err ? "failed" : "done";
1426 }
1427 f2fs_msg(sb, KERN_INFO,
1428 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1429 res, main_blkaddr,
1430 segment0_blkaddr +
1431 (segment_count << log_blocks_per_seg),
1432 segment_count_main << log_blocks_per_seg);
1433 if (err)
1434 return true;
1435 }
Chao Yu9a59b622015-12-15 09:58:18 +08001436 return false;
1437}
1438
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001439static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001440 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001441{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001442 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1443 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001444 struct super_block *sb = sbi->sb;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001445 unsigned int blocksize;
1446
Namjae Jeona07ef782012-12-30 14:52:05 +09001447 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1448 f2fs_msg(sb, KERN_INFO,
1449 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1450 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001451 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001452 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001453
majianpeng5c9b4692013-02-01 19:07:57 +08001454 /* Currently, support only 4KB page cache size */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001455 if (F2FS_BLKSIZE != PAGE_SIZE) {
majianpeng5c9b4692013-02-01 19:07:57 +08001456 f2fs_msg(sb, KERN_INFO,
majianpeng14d7e9d2013-02-01 19:07:03 +08001457 "Invalid page_cache_size (%lu), supports only 4KB\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 PAGE_SIZE);
majianpeng5c9b4692013-02-01 19:07:57 +08001459 return 1;
1460 }
1461
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001462 /* Currently, support only 4KB block size */
1463 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
majianpeng5c9b4692013-02-01 19:07:57 +08001464 if (blocksize != F2FS_BLKSIZE) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001465 f2fs_msg(sb, KERN_INFO,
1466 "Invalid blocksize (%u), supports only 4KB\n",
1467 blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001468 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001469 }
majianpeng5c9b4692013-02-01 19:07:57 +08001470
Chao Yu9a59b622015-12-15 09:58:18 +08001471 /* check log blocks per segment */
1472 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1473 f2fs_msg(sb, KERN_INFO,
1474 "Invalid log blocks per segment (%u)\n",
1475 le32_to_cpu(raw_super->log_blocks_per_seg));
1476 return 1;
1477 }
1478
Chao Yu55cf9cb2014-09-15 18:01:10 +08001479 /* Currently, support 512/1024/2048/4096 bytes sector size */
1480 if (le32_to_cpu(raw_super->log_sectorsize) >
1481 F2FS_MAX_LOG_SECTOR_SIZE ||
1482 le32_to_cpu(raw_super->log_sectorsize) <
1483 F2FS_MIN_LOG_SECTOR_SIZE) {
1484 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1485 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001486 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001487 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08001488 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1489 le32_to_cpu(raw_super->log_sectorsize) !=
1490 F2FS_MAX_LOG_SECTOR_SIZE) {
1491 f2fs_msg(sb, KERN_INFO,
1492 "Invalid log sectors per block(%u) log sectorsize(%u)",
1493 le32_to_cpu(raw_super->log_sectors_per_block),
1494 le32_to_cpu(raw_super->log_sectorsize));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001495 return 1;
Namjae Jeona07ef782012-12-30 14:52:05 +09001496 }
Chao Yu9a59b622015-12-15 09:58:18 +08001497
1498 /* check reserved ino info */
1499 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1500 le32_to_cpu(raw_super->meta_ino) != 2 ||
1501 le32_to_cpu(raw_super->root_ino) != 3) {
1502 f2fs_msg(sb, KERN_INFO,
1503 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1504 le32_to_cpu(raw_super->node_ino),
1505 le32_to_cpu(raw_super->meta_ino),
1506 le32_to_cpu(raw_super->root_ino));
1507 return 1;
1508 }
1509
Jin Qianb9dd4612017-04-25 16:28:48 -07001510 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1511 f2fs_msg(sb, KERN_INFO,
1512 "Invalid segment count (%u)",
1513 le32_to_cpu(raw_super->segment_count));
1514 return 1;
1515 }
1516
Chao Yu9a59b622015-12-15 09:58:18 +08001517 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001518 if (sanity_check_area_boundary(sbi, bh))
Chao Yu9a59b622015-12-15 09:58:18 +08001519 return 1;
1520
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001521 return 0;
1522}
1523
Shawn Lin984ec632016-02-17 11:26:32 +08001524int sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001525{
1526 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001527 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1528 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001529 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07001530 unsigned int main_segs, blocks_per_seg;
1531 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001532
1533 total = le32_to_cpu(raw_super->segment_count);
1534 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1535 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1536 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1537 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1538 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1539
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001540 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001541 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001542
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08001543 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1544 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1545
1546 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1547 ovp_segments == 0 || reserved_segments == 0)) {
1548 f2fs_msg(sbi->sb, KERN_ERR,
1549 "Wrong layout: check mkfs.f2fs version");
1550 return 1;
1551 }
1552
Jin Qian15d30422017-05-15 10:45:08 -07001553 main_segs = le32_to_cpu(raw_super->segment_count_main);
1554 blocks_per_seg = sbi->blocks_per_seg;
1555
1556 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1557 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1558 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1559 return 1;
1560 }
1561 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1562 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1563 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1564 return 1;
1565 }
1566
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07001567 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim577e3492013-01-24 19:56:11 +09001568 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1569 return 1;
1570 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001571 return 0;
1572}
1573
1574static void init_sb_info(struct f2fs_sb_info *sbi)
1575{
1576 struct f2fs_super_block *raw_super = sbi->raw_super;
Chao Yue41e6d72017-05-19 23:37:00 +08001577 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001578
1579 sbi->log_sectors_per_block =
1580 le32_to_cpu(raw_super->log_sectors_per_block);
1581 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1582 sbi->blocksize = 1 << sbi->log_blocksize;
1583 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1584 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1585 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1586 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1587 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1588 sbi->total_node_count =
1589 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1590 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1591 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1592 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1593 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001594 sbi->cur_victim_sec = NULL_SECNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09001595 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001596
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001597 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001598 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001599 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Chao Yucaf00472015-01-28 17:48:42 +08001600 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001601
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001602 for (i = 0; i < NR_COUNT_TYPE; i++)
1603 atomic_set(&sbi->nr_pages[i], 0);
1604
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001605 atomic_set(&sbi->wb_sync_req, 0);
1606
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001607 INIT_LIST_HEAD(&sbi->s_list);
1608 mutex_init(&sbi->umount_mutex);
Chao Yue41e6d72017-05-19 23:37:00 +08001609 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1610 for (j = HOT; j < NR_TEMP_TYPE; j++)
1611 mutex_init(&sbi->wio_mutex[i][j]);
Chao Yuaaec2b12016-09-20 11:04:18 +08001612 spin_lock_init(&sbi->cp_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001613}
1614
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001615static int init_percpu_info(struct f2fs_sb_info *sbi)
1616{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07001617 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001618
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001619 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1620 if (err)
1621 return err;
1622
1623 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001624 GFP_KERNEL);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001625}
1626
Damien Le Moal178053e2016-10-28 17:45:05 +09001627#ifdef CONFIG_BLK_DEV_ZONED
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001628static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09001629{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001630 struct block_device *bdev = FDEV(devi).bdev;
Damien Le Moal178053e2016-10-28 17:45:05 +09001631 sector_t nr_sectors = bdev->bd_part->nr_sects;
1632 sector_t sector = 0;
1633 struct blk_zone *zones;
1634 unsigned int i, nr_zones;
1635 unsigned int n = 0;
1636 int err = -EIO;
1637
1638 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1639 return 0;
1640
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001641 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07001642 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001643 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001644 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001645 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1646 __ilog2_u32(sbi->blocks_per_blkz))
1647 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09001648 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001649 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1650 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07001651 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001652 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09001653
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001654 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1655 if (!FDEV(devi).blkz_type)
Damien Le Moal178053e2016-10-28 17:45:05 +09001656 return -ENOMEM;
1657
1658#define F2FS_REPORT_NR_ZONES 4096
1659
1660 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1661 GFP_KERNEL);
1662 if (!zones)
1663 return -ENOMEM;
1664
1665 /* Get block zones type */
1666 while (zones && sector < nr_sectors) {
1667
1668 nr_zones = F2FS_REPORT_NR_ZONES;
1669 err = blkdev_report_zones(bdev, sector,
1670 zones, &nr_zones,
1671 GFP_KERNEL);
1672 if (err)
1673 break;
1674 if (!nr_zones) {
1675 err = -EIO;
1676 break;
1677 }
1678
1679 for (i = 0; i < nr_zones; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001680 FDEV(devi).blkz_type[n] = zones[i].type;
Damien Le Moal178053e2016-10-28 17:45:05 +09001681 sector += zones[i].len;
1682 n++;
1683 }
1684 }
1685
1686 kfree(zones);
1687
1688 return err;
1689}
1690#endif
1691
Gu Zheng9076a752013-10-14 18:47:11 +08001692/*
1693 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08001694 * Because we have two copies of super block, so read both of them
1695 * to get the first valid one. If any one of them is broken, we pass
1696 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08001697 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001698static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08001699 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08001700 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08001701{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001702 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08001703 int block;
Chao Yue8240f62015-12-15 17:19:26 +08001704 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001705 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08001706 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08001707
Yunlei Heb39f0de2015-12-15 17:17:20 +08001708 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1709 if (!super)
1710 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08001711
1712 for (block = 0; block < 2; block++) {
1713 bh = sb_bread(sb, block);
1714 if (!bh) {
1715 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
Gu Zheng9076a752013-10-14 18:47:11 +08001716 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08001717 err = -EIO;
1718 continue;
1719 }
majianpeng14d7e9d2013-02-01 19:07:03 +08001720
Shawn Lin2b39e902016-02-17 08:59:01 +08001721 /* sanity checking of raw super */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001722 if (sanity_check_raw_super(sbi, bh)) {
Shawn Lin2b39e902016-02-17 08:59:01 +08001723 f2fs_msg(sb, KERN_ERR,
1724 "Can't find valid F2FS filesystem in %dth superblock",
1725 block + 1);
1726 err = -EINVAL;
1727 brelse(bh);
1728 continue;
1729 }
1730
1731 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001732 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1733 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08001734 *valid_super_block = block;
1735 *raw_super = super;
1736 }
Chao Yue8240f62015-12-15 17:19:26 +08001737 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08001738 }
1739
1740 /* Fail to read any one of the superblocks*/
1741 if (err < 0)
hujianyangda554e42015-05-21 14:42:53 +08001742 *recovery = 1;
hujianyangda554e42015-05-21 14:42:53 +08001743
hujianyangda554e42015-05-21 14:42:53 +08001744 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08001745 if (!*raw_super)
Yunlei Heb39f0de2015-12-15 17:17:20 +08001746 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08001747 else
1748 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08001749
Shawn Lin2b39e902016-02-17 08:59:01 +08001750 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08001751}
1752
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001753int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001754{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001755 struct buffer_head *bh;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001756 int err;
1757
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001758 if ((recover && f2fs_readonly(sbi->sb)) ||
1759 bdev_read_only(sbi->sb->s_bdev)) {
1760 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07001761 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001762 }
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07001763
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001764 /* write back-up superblock first */
1765 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001766 if (!bh)
1767 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001768 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001769 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08001770
1771 /* if we are in recovery path, skip writing valid superblock */
1772 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08001773 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001774
Chao Yue8240f62015-12-15 17:19:26 +08001775 /* write current valid superblock */
Jaegeuk Kimfd694732016-03-20 15:33:20 -07001776 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1777 if (!bh)
1778 return -EIO;
1779 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1780 brelse(bh);
1781 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07001782}
1783
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001784static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1785{
1786 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001787 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001788 int i;
1789
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001790 /* Initialize single device information */
1791 if (!RDEV(0).path[0]) {
1792 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001793 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001794 max_devices = 1;
1795 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001796
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001797 /*
1798 * Initialize multiple devices information, or single
1799 * zoned block device information.
1800 */
1801 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1802 GFP_KERNEL);
1803 if (!sbi->devs)
1804 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001805
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001806 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001807
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001808 if (i > 0 && !RDEV(i).path[0])
1809 break;
1810
1811 if (max_devices == 1) {
1812 /* Single zoned block device mount */
1813 FDEV(0).bdev =
1814 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001815 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001816 } else {
1817 /* Multi-device mount */
1818 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1819 FDEV(i).total_segments =
1820 le32_to_cpu(RDEV(i).total_segments);
1821 if (i == 0) {
1822 FDEV(i).start_blk = 0;
1823 FDEV(i).end_blk = FDEV(i).start_blk +
1824 (FDEV(i).total_segments <<
1825 sbi->log_blocks_per_seg) - 1 +
1826 le32_to_cpu(raw_super->segment0_blkaddr);
1827 } else {
1828 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1829 FDEV(i).end_blk = FDEV(i).start_blk +
1830 (FDEV(i).total_segments <<
1831 sbi->log_blocks_per_seg) - 1;
1832 }
1833 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
1834 sbi->sb->s_mode, sbi->sb->s_type);
1835 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001836 if (IS_ERR(FDEV(i).bdev))
1837 return PTR_ERR(FDEV(i).bdev);
1838
1839 /* to release errored devices */
1840 sbi->s_ndevs = i + 1;
1841
1842#ifdef CONFIG_BLK_DEV_ZONED
1843 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1844 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1845 f2fs_msg(sbi->sb, KERN_ERR,
1846 "Zoned block device feature not enabled\n");
1847 return -EINVAL;
1848 }
1849 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1850 if (init_blkz_info(sbi, i)) {
1851 f2fs_msg(sbi->sb, KERN_ERR,
1852 "Failed to initialize F2FS blkzone information");
1853 return -EINVAL;
1854 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09001855 if (max_devices == 1)
1856 break;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001857 f2fs_msg(sbi->sb, KERN_INFO,
1858 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1859 i, FDEV(i).path,
1860 FDEV(i).total_segments,
1861 FDEV(i).start_blk, FDEV(i).end_blk,
1862 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1863 "Host-aware" : "Host-managed");
1864 continue;
1865 }
1866#endif
1867 f2fs_msg(sbi->sb, KERN_INFO,
1868 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1869 i, FDEV(i).path,
1870 FDEV(i).total_segments,
1871 FDEV(i).start_blk, FDEV(i).end_blk);
1872 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08001873 f2fs_msg(sbi->sb, KERN_INFO,
1874 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001875 return 0;
1876}
1877
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001878static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1879{
1880 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08001881 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001882 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08001883 int err;
Chao Yu2adc3502015-03-16 21:08:44 +08001884 bool retry = true, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001885 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001886 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001887 struct curseg_info *seg_i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001888
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07001889try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08001890 err = -EINVAL;
1891 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08001892 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08001893 recovery = 0;
1894
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001895 /* allocate memory for f2fs-specific super block info */
1896 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1897 if (!sbi)
1898 return -ENOMEM;
1899
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001900 sbi->sb = sb;
1901
Keith Mok43b65732016-03-02 12:04:24 -08001902 /* Load the checksum driver */
1903 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1904 if (IS_ERR(sbi->s_chksum_driver)) {
1905 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1906 err = PTR_ERR(sbi->s_chksum_driver);
1907 sbi->s_chksum_driver = NULL;
1908 goto free_sbi;
1909 }
1910
Namjae Jeonff9234a2013-01-12 14:41:13 +09001911 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001912 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Namjae Jeona07ef782012-12-30 14:52:05 +09001913 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001914 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09001915 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001916
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001917 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08001918 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08001919 if (err)
1920 goto free_sbi;
1921
Gu Zheng5fb08372013-06-07 14:16:53 +08001922 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07001923 sbi->raw_super = raw_super;
1924
Damien Le Moald1b959c2016-10-28 17:45:01 +09001925 /*
1926 * The BLKZONED feature indicates that the drive was formatted with
1927 * zone alignment optimization. This is optional for host-aware
1928 * devices, but mandatory for host-managed zoned block devices.
1929 */
1930#ifndef CONFIG_BLK_DEV_ZONED
1931 if (f2fs_sb_mounted_blkzoned(sb)) {
1932 f2fs_msg(sb, KERN_ERR,
1933 "Zoned block device support is not enabled\n");
Chao Yu1727f312017-06-12 09:44:27 +08001934 err = -EOPNOTSUPP;
Damien Le Moald1b959c2016-10-28 17:45:01 +09001935 goto free_sb_buf;
1936 }
Damien Le Moald1b959c2016-10-28 17:45:01 +09001937#endif
Yunlei He498c5e92015-05-07 18:11:37 +08001938 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001939 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001940 options = kstrdup((const char *)data, GFP_KERNEL);
1941 if (data && !options) {
1942 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001943 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08001944 }
1945
1946 err = parse_options(sb, options);
1947 if (err)
1948 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001949
Chao Yue0afc4d2015-12-31 14:35:37 +08001950 sbi->max_file_blocks = max_file_blocks();
1951 sb->s_maxbytes = sbi->max_file_blocks <<
1952 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001953 sb->s_max_links = F2FS_LINK_MAX;
1954 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1955
Chao Yu0abd6752017-07-09 00:13:07 +08001956#ifdef CONFIG_QUOTA
1957 sb->dq_op = &f2fs_quota_operations;
1958 sb->s_qcop = &f2fs_quotactl_ops;
1959 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1960#endif
1961
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001962 sb->s_op = &f2fs_sops;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001963 sb->s_cop = &f2fs_cryptops;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001964 sb->s_xattr = f2fs_xattr_handlers;
1965 sb->s_export_op = &f2fs_export_ops;
1966 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001967 sb->s_time_gran = 1;
1968 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1969 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02001970 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001971
1972 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08001973 sbi->valid_super_block = valid_super_block;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001974 mutex_init(&sbi->gc_mutex);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001975 mutex_init(&sbi->cp_mutex);
Chao Yub3582c62014-07-03 18:58:39 +08001976 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08001977 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07001978
1979 /* disallow all the data/node/meta page writes */
1980 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001981 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09001982
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001983 for (i = 0; i < NR_PAGE_TYPE; i++) {
Jaegeuk Kima912b542017-05-10 11:18:25 -07001984 int n = (i == META) ? 1: NR_TEMP_TYPE;
1985 int j;
1986
1987 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
1988 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02001989 if (!sbi->write_io[i]) {
1990 err = -ENOMEM;
Jaegeuk Kima912b542017-05-10 11:18:25 -07001991 goto free_options;
Christophe JAILLETb63def92017-06-11 09:21:11 +02001992 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07001993
1994 for (j = HOT; j < n; j++) {
1995 init_rwsem(&sbi->write_io[i][j].io_rwsem);
1996 sbi->write_io[i][j].sbi = sbi;
1997 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08001998 spin_lock_init(&sbi->write_io[i][j].io_lock);
1999 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Jaegeuk Kima912b542017-05-10 11:18:25 -07002000 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002001 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09002002
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002003 init_rwsem(&sbi->cp_rwsem);
Changman Leefb51b5e2013-11-07 12:48:25 +09002004 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002005 init_sb_info(sbi);
2006
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002007 err = init_percpu_info(sbi);
2008 if (err)
2009 goto free_options;
2010
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002011 if (F2FS_IO_SIZE(sbi) > 1) {
2012 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08002013 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08002014 if (!sbi->write_io_dummy) {
2015 err = -ENOMEM;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002016 goto free_options;
Chao Yu1727f312017-06-12 09:44:27 +08002017 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002018 }
2019
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002020 /* get an inode for meta space */
2021 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2022 if (IS_ERR(sbi->meta_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002023 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002024 err = PTR_ERR(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002025 goto free_io_dummy;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002026 }
2027
2028 err = get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002029 if (err) {
2030 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002031 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09002032 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002033
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002034 /* Initialize device list */
2035 err = f2fs_scan_devices(sbi);
2036 if (err) {
2037 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2038 goto free_devices;
2039 }
2040
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002041 sbi->total_valid_node_count =
2042 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07002043 percpu_counter_set(&sbi->total_valid_inode_count,
2044 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002045 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2046 sbi->total_valid_block_count =
2047 le64_to_cpu(sbi->ckpt->valid_block_count);
2048 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08002049 sbi->reserved_blocks = 0;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07002050
Chao Yuc227f912015-12-16 13:09:20 +08002051 for (i = 0; i < NR_INODE_TYPE; i++) {
2052 INIT_LIST_HEAD(&sbi->inode_list[i]);
2053 spin_lock_init(&sbi->inode_lock[i]);
2054 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002055
Chao Yu1dcc3362015-02-05 17:57:31 +08002056 init_extent_cache_info(sbi);
2057
Jaegeuk Kim6451e042014-07-25 15:47:17 -07002058 init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002059
2060 /* setup f2fs internal modules */
2061 err = build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002062 if (err) {
2063 f2fs_msg(sb, KERN_ERR,
2064 "Failed to initialize F2FS segment manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002065 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002066 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002067 err = build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09002068 if (err) {
2069 f2fs_msg(sb, KERN_ERR,
2070 "Failed to initialize F2FS node manager");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002071 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09002072 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002073
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002074 /* For write statistics */
2075 if (sb->s_bdev->bd_part)
2076 sbi->sectors_written_start =
2077 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
2078
2079 /* Read accumulated write IO statistics if exists */
2080 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
2081 if (__exist_node_summaries(sbi))
2082 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08002083 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08002084
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002085 build_gc_manager(sbi);
2086
2087 /* get an inode for node space */
2088 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
2089 if (IS_ERR(sbi->node_inode)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002090 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002091 err = PTR_ERR(sbi->node_inode);
2092 goto free_nm;
2093 }
2094
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002095 f2fs_join_shrinker(sbi);
2096
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002097 err = f2fs_build_stats(sbi);
2098 if (err)
2099 goto free_nm;
2100
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002101 /* if there are nt orphan nodes free them */
Chao Yu8c14bfa2015-08-07 17:58:43 +08002102 err = recover_orphan_inodes(sbi);
2103 if (err)
2104 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002105
2106 /* read root inode and dentry */
2107 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
2108 if (IS_ERR(root)) {
Namjae Jeona07ef782012-12-30 14:52:05 +09002109 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002110 err = PTR_ERR(root);
2111 goto free_node_inode;
2112 }
Chao Yu8f99a942013-11-28 15:43:43 +08002113 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Chao Yu9d847952014-07-25 12:55:09 +08002114 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08002115 err = -EINVAL;
Chao Yu9d847952014-07-25 12:55:09 +08002116 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08002117 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002118
2119 sb->s_root = d_make_root(root); /* allocate root dentry */
2120 if (!sb->s_root) {
2121 err = -ENOMEM;
2122 goto free_root_inode;
2123 }
2124
Chao Yua3981012017-06-14 17:39:46 +08002125 err = f2fs_init_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002126 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002127 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09002128
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002129 /* recover fsynced data */
2130 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002131 /*
2132 * mount should be failed, when device has readonly mode, and
2133 * previous checkpoint was not done by clean system shutdown.
2134 */
2135 if (bdev_read_only(sb->s_bdev) &&
Chao Yuaaec2b12016-09-20 11:04:18 +08002136 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002137 err = -EROFS;
Chao Yua3981012017-06-14 17:39:46 +08002138 goto free_sysfs;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08002139 }
Chao Yu2adc3502015-03-16 21:08:44 +08002140
2141 if (need_fsck)
2142 set_sbi_flag(sbi, SBI_NEED_FSCK);
2143
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002144 if (!retry)
2145 goto skip_recovery;
2146
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002147 err = recover_fsync_data(sbi, false);
2148 if (err < 0) {
Chao Yu2adc3502015-03-16 21:08:44 +08002149 need_fsck = true;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002150 f2fs_msg(sb, KERN_ERR,
Sheng Yong99e3e852016-05-11 17:08:14 +08002151 "Cannot recover all fsync data errno=%d", err);
Chao Yua3981012017-06-14 17:39:46 +08002152 goto free_sysfs;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002153 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002154 } else {
2155 err = recover_fsync_data(sbi, true);
2156
2157 if (!f2fs_readonly(sb) && err > 0) {
2158 err = -EINVAL;
2159 f2fs_msg(sb, KERN_ERR,
2160 "Need to recover fsync data");
Chao Yua3981012017-06-14 17:39:46 +08002161 goto free_sysfs;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07002162 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002163 }
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07002164skip_recovery:
Jaegeuk Kim315df832015-08-11 12:45:39 -07002165 /* recover_fsync_data() cleared this already */
2166 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002167
2168 /*
2169 * If filesystem is not mounted as read-only then
2170 * do start the gc_thread.
2171 */
Chao Yu6c029932014-11-18 11:16:01 +08002172 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002173 /* After POR, we can run background GC thread.*/
2174 err = start_gc_thread(sbi);
2175 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002176 goto free_sysfs;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002177 }
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002178 kfree(options);
hujianyangda554e42015-05-21 14:42:53 +08002179
2180 /* recover broken superblock */
Jaegeuk Kimf2353d7b2016-03-23 10:42:01 -07002181 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08002182 err = f2fs_commit_super(sbi, true);
2183 f2fs_msg(sb, KERN_INFO,
Sheng Yong99e3e852016-05-11 17:08:14 +08002184 "Try to recover %dth superblock, ret: %d",
Chao Yu41214b32016-02-22 18:33:20 +08002185 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08002186 }
2187
Jaegeuk Kim1200abb2017-02-09 13:25:35 -08002188 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
2189 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08002190 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002191 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002192 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09002193
Chao Yua3981012017-06-14 17:39:46 +08002194free_sysfs:
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002195 f2fs_sync_inode_meta(sbi);
Chao Yua3981012017-06-14 17:39:46 +08002196 f2fs_exit_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002197free_root_inode:
2198 dput(sb->s_root);
2199 sb->s_root = NULL;
2200free_node_inode:
Jaegeuk Kimbb5dada2016-09-23 11:29:00 -07002201 truncate_inode_pages_final(NODE_MAPPING(sbi));
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002202 mutex_lock(&sbi->umount_mutex);
Jaegeuk Kimd41065e2016-09-21 11:39:42 -07002203 release_ino_entry(sbi, true);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002204 f2fs_leave_shrinker(sbi);
Jaegeuk Kim09922802016-10-31 14:01:41 -07002205 /*
2206 * Some dirty meta pages can be produced by recover_orphan_inodes()
2207 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
2208 * followed by write_checkpoint() through f2fs_write_node_pages(), which
2209 * falls into an infinite loop in sync_meta_pages().
2210 */
2211 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002212 iput(sbi->node_inode);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002213 mutex_unlock(&sbi->umount_mutex);
Jaegeuk Kimaa51d082017-03-07 13:41:22 -08002214 f2fs_destroy_stats(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002215free_nm:
2216 destroy_node_manager(sbi);
2217free_sm:
2218 destroy_segment_manager(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07002219free_devices:
2220 destroy_device_list(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002221 kfree(sbi->ckpt);
2222free_meta_inode:
2223 make_bad_inode(sbi->meta_inode);
2224 iput(sbi->meta_inode);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08002225free_io_dummy:
2226 mempool_destroy(sbi->write_io_dummy);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002227free_options:
Jaegeuk Kima912b542017-05-10 11:18:25 -07002228 for (i = 0; i < NR_PAGE_TYPE; i++)
2229 kfree(sbi->write_io[i]);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07002230 destroy_percpu_info(sbi);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08002231 kfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002232free_sb_buf:
Yunlei Heb39f0de2015-12-15 17:17:20 +08002233 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002234free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08002235 if (sbi->s_chksum_driver)
2236 crypto_free_shash(sbi->s_chksum_driver);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002237 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002238
2239 /* give only one another chance */
2240 if (retry) {
Taehee Yoo9df47ba2015-04-13 21:48:06 +09002241 retry = false;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07002242 shrink_dcache_sb(sb);
2243 goto try_onemore;
2244 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002245 return err;
2246}
2247
2248static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
2249 const char *dev_name, void *data)
2250{
2251 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
2252}
2253
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002254static void kill_f2fs_super(struct super_block *sb)
2255{
Chao Yucce13252017-06-29 23:17:45 +08002256 if (sb->s_root) {
Chao Yucaf00472015-01-28 17:48:42 +08002257 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
Chao Yucce13252017-06-29 23:17:45 +08002258 stop_gc_thread(F2FS_SB(sb));
2259 stop_discard_thread(F2FS_SB(sb));
2260 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002261 kill_block_super(sb);
2262}
2263
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002264static struct file_system_type f2fs_fs_type = {
2265 .owner = THIS_MODULE,
2266 .name = "f2fs",
2267 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08002268 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002269 .fs_flags = FS_REQUIRES_DEV,
2270};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002271MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002272
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002273static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002274{
Vladimir Davydov5d097052016-01-14 15:18:21 -08002275 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
2276 sizeof(struct f2fs_inode_info), 0,
2277 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002278 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002279 return -ENOMEM;
2280 return 0;
2281}
2282
2283static void destroy_inodecache(void)
2284{
2285 /*
2286 * Make sure all delayed rcu free inodes are flushed before we
2287 * destroy cache.
2288 */
2289 rcu_barrier();
2290 kmem_cache_destroy(f2fs_inode_cachep);
2291}
2292
2293static int __init init_f2fs_fs(void)
2294{
2295 int err;
2296
Jaegeuk Kimc0508652015-01-07 14:07:36 -08002297 f2fs_build_trace_ios();
2298
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002299 err = init_inodecache();
2300 if (err)
2301 goto fail;
2302 err = create_node_manager_caches();
2303 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002304 goto free_inodecache;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002305 err = create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002306 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002307 goto free_node_manager_caches;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002308 err = create_checkpoint_caches();
2309 if (err)
Chao Yu06292072014-12-29 15:56:18 +08002310 goto free_segment_manager_caches;
Chao Yu1dcc3362015-02-05 17:57:31 +08002311 err = create_extent_cache();
2312 if (err)
2313 goto free_checkpoint_caches;
Chao Yua3981012017-06-14 17:39:46 +08002314 err = f2fs_register_sysfs();
2315 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08002316 goto free_extent_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002317 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07002318 if (err)
Chao Yua3981012017-06-14 17:39:46 +08002319 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002320 err = register_filesystem(&f2fs_fs_type);
2321 if (err)
2322 goto free_shrinker;
Chao Yu787c7b8c2015-10-29 09:13:04 +08002323 err = f2fs_create_root_stats();
2324 if (err)
2325 goto free_filesystem;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002326 return 0;
2327
Chao Yu787c7b8c2015-10-29 09:13:04 +08002328free_filesystem:
2329 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002330free_shrinker:
2331 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002332free_sysfs:
2333 f2fs_unregister_sysfs();
Chao Yu1dcc3362015-02-05 17:57:31 +08002334free_extent_cache:
2335 destroy_extent_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002336free_checkpoint_caches:
2337 destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002338free_segment_manager_caches:
2339 destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08002340free_node_manager_caches:
2341 destroy_node_manager_caches();
2342free_inodecache:
2343 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002344fail:
2345 return err;
2346}
2347
2348static void __exit exit_f2fs_fs(void)
2349{
Namjae Jeon4589d252013-01-15 19:58:47 +09002350 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002351 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08002352 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08002353 f2fs_unregister_sysfs();
Wanpeng Lifdf6c8b2015-03-06 15:00:54 +08002354 destroy_extent_cache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002355 destroy_checkpoint_caches();
Changman Lee5dcd8a72013-12-11 14:32:13 +09002356 destroy_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002357 destroy_node_manager_caches();
2358 destroy_inodecache();
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -08002359 f2fs_destroy_trace_ios();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002360}
2361
2362module_init(init_f2fs_fs)
2363module_exit(exit_f2fs_fs)
2364
2365MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2366MODULE_DESCRIPTION("Flash Friendly File System");
2367MODULE_LICENSE("GPL");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07002368