blob: 3e0e34b4680c523b650c4e9b3b319880c99398db [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003 * fs/f2fs/super.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09007 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/fs.h>
11#include <linux/statfs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090012#include <linux/buffer_head.h>
13#include <linux/backing-dev.h>
14#include <linux/kthread.h>
15#include <linux/parser.h>
16#include <linux/mount.h>
17#include <linux/seq_file.h>
Jaegeuk Kim5e176d52013-06-28 12:47:01 +090018#include <linux/proc_fs.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090019#include <linux/random.h>
20#include <linux/exportfs.h>
Namjae Jeond3ee4562013-03-17 17:26:14 +090021#include <linux/blkdev.h>
Chao Yu0abd6752017-07-09 00:13:07 +080022#include <linux/quotaops.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090023#include <linux/f2fs_fs.h>
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090024#include <linux/sysfs.h>
Chao Yu4b2414d2017-08-08 10:54:31 +080025#include <linux/quota.h>
Daniel Rosenberg5aba5432019-07-23 16:05:28 -070026#include <linux/unicode.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010027#include <linux/part_stat.h>
Chao Yu3fde13f2021-01-22 17:46:43 +080028#include <linux/zstd.h>
29#include <linux/lz4.h>
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090030
31#include "f2fs.h"
32#include "node.h"
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090033#include "segment.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090034#include "xattr.h"
Namjae Jeonb59d0ba2013-08-04 23:09:40 +090035#include "gc.h"
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090036
Namjae Jeona2a4a7e2013-04-20 01:28:40 +090037#define CREATE_TRACE_POINTS
38#include <trace/events/f2fs.h>
39
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090040static struct kmem_cache *f2fs_inode_cachep;
41
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070042#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070043
Alexey Dobriyan19880e62018-11-24 12:06:42 +030044const char *f2fs_fault_name[FAULT_MAX] = {
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070045 [FAULT_KMALLOC] = "kmalloc",
Chao Yu628b3d12017-11-30 19:28:18 +080046 [FAULT_KVMALLOC] = "kvmalloc",
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070047 [FAULT_PAGE_ALLOC] = "page alloc",
Chao Yu01eccef2017-10-28 16:52:30 +080048 [FAULT_PAGE_GET] = "page get",
Jaegeuk Kimcb789422016-04-29 16:29:22 -070049 [FAULT_ALLOC_NID] = "alloc nid",
50 [FAULT_ORPHAN] = "orphan",
51 [FAULT_BLOCK] = "no more block",
52 [FAULT_DIR_DEPTH] = "too big dir depth",
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070053 [FAULT_EVICT_INODE] = "evict_inode fail",
Jaegeuk Kim14b44d22017-03-09 15:24:24 -080054 [FAULT_TRUNCATE] = "truncate fail",
Chao Yu6f5c2ed2018-09-12 09:22:29 +080055 [FAULT_READ_IO] = "read IO error",
Chao Yu0f348022016-09-26 19:45:55 +080056 [FAULT_CHECKPOINT] = "checkpoint error",
Chao Yub83dcfe2018-08-06 20:30:18 +080057 [FAULT_DISCARD] = "discard error",
Chao Yu6f5c2ed2018-09-12 09:22:29 +080058 [FAULT_WRITE_IO] = "write IO error",
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070059};
Sheng Yong08796892016-05-16 12:38:50 +080060
Chao Yud4945002018-08-08 17:36:41 +080061void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
62 unsigned int type)
Sheng Yong08796892016-05-16 12:38:50 +080063{
Chao Yu63189b72018-03-08 14:22:56 +080064 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
Chao Yu1ecc0c52016-09-23 21:30:09 +080065
Sheng Yong08796892016-05-16 12:38:50 +080066 if (rate) {
Chao Yu1ecc0c52016-09-23 21:30:09 +080067 atomic_set(&ffi->inject_ops, 0);
68 ffi->inject_rate = rate;
Sheng Yong08796892016-05-16 12:38:50 +080069 }
Chao Yud4945002018-08-08 17:36:41 +080070
71 if (type)
72 ffi->inject_type = type;
73
74 if (!rate && !type)
75 memset(ffi, 0, sizeof(struct f2fs_fault_info));
Sheng Yong08796892016-05-16 12:38:50 +080076}
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -070077#endif
78
Jaegeuk Kim2658e502015-06-19 12:01:21 -070079/* f2fs-wide shrinker description */
80static struct shrinker f2fs_shrinker_info = {
81 .scan_objects = f2fs_shrink_scan,
82 .count_objects = f2fs_shrink_count,
83 .seeks = DEFAULT_SEEKS,
84};
85
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090086enum {
Namjae Jeon696c0182013-06-16 09:48:48 +090087 Opt_gc_background,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090088 Opt_disable_roll_forward,
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -080089 Opt_norecovery,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090090 Opt_discard,
Chao Yu64058be2016-07-03 22:05:14 +080091 Opt_nodiscard,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090092 Opt_noheap,
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -040093 Opt_heap,
Kelly Anderson4058c512013-10-07 11:36:20 +090094 Opt_user_xattr,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090095 Opt_nouser_xattr,
Kelly Anderson4058c512013-10-07 11:36:20 +090096 Opt_acl,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +090097 Opt_noacl,
98 Opt_active_logs,
99 Opt_disable_ext_identify,
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900100 Opt_inline_xattr,
Chao Yu23cf7212017-02-15 10:34:45 +0800101 Opt_noinline_xattr,
Chao Yu6afc6622017-09-06 21:59:50 +0800102 Opt_inline_xattr_size,
Huajun Li8274de72013-11-10 23:13:17 +0800103 Opt_inline_data,
Chao Yu5efd3c62014-09-24 18:16:13 +0800104 Opt_inline_dentry,
Chao Yu97c17942016-05-09 19:56:34 +0800105 Opt_noinline_dentry,
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900106 Opt_flush_merge,
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700107 Opt_noflush_merge,
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700108 Opt_nobarrier,
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700109 Opt_fastboot,
Chao Yu89672152015-02-05 17:55:51 +0800110 Opt_extent_cache,
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700111 Opt_noextent_cache,
Wanpeng Li75342792015-03-24 10:20:27 +0800112 Opt_noinline_data,
Chao Yu343f40f2015-12-16 13:12:16 +0800113 Opt_data_flush,
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800114 Opt_reserve_root,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800115 Opt_resgid,
116 Opt_resuid,
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700117 Opt_mode,
Jaegeuk Kimec915382016-12-21 17:09:19 -0800118 Opt_io_size_bits,
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700119 Opt_fault_injection,
Chao Yud4945002018-08-08 17:36:41 +0800120 Opt_fault_type,
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700121 Opt_lazytime,
122 Opt_nolazytime,
Chao Yu4b2414d2017-08-08 10:54:31 +0800123 Opt_quota,
124 Opt_noquota,
Chao Yu0abd6752017-07-09 00:13:07 +0800125 Opt_usrquota,
126 Opt_grpquota,
Chao Yu5c571322017-07-26 00:01:41 +0800127 Opt_prjquota,
Chao Yu4b2414d2017-08-08 10:54:31 +0800128 Opt_usrjquota,
129 Opt_grpjquota,
130 Opt_prjjquota,
131 Opt_offusrjquota,
132 Opt_offgrpjquota,
133 Opt_offprjjquota,
134 Opt_jqfmt_vfsold,
135 Opt_jqfmt_vfsv0,
136 Opt_jqfmt_vfsv1,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900137 Opt_whint,
Jaegeuk Kim07939622018-02-18 08:50:49 -0800138 Opt_alloc,
Junling Zheng93cf93f2018-03-07 12:07:49 +0800139 Opt_fsync,
Sheng Yongff62af22018-03-15 18:51:42 +0800140 Opt_test_dummy_encryption,
Satya Tangirala27aacd22020-07-02 01:56:06 +0000141 Opt_inlinecrypt,
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700142 Opt_checkpoint_disable,
143 Opt_checkpoint_disable_cap,
144 Opt_checkpoint_disable_cap_perc,
145 Opt_checkpoint_enable,
Daeho Jeong261eeb92021-01-19 09:00:42 +0900146 Opt_checkpoint_merge,
147 Opt_nocheckpoint_merge,
Chao Yu4c8ff702019-11-01 18:07:14 +0800148 Opt_compress_algorithm,
149 Opt_compress_log_size,
150 Opt_compress_extension,
Chao Yub28f0472020-11-26 18:32:09 +0800151 Opt_compress_chksum,
Daeho Jeong602a16d2020-12-01 13:08:02 +0900152 Opt_compress_mode,
Chao Yu093749e2020-08-04 21:14:49 +0800153 Opt_atgc,
Chao Yu5911d2d2021-03-27 17:57:06 +0800154 Opt_gc_merge,
155 Opt_nogc_merge,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900156 Opt_err,
157};
158
159static match_table_t f2fs_tokens = {
Namjae Jeon696c0182013-06-16 09:48:48 +0900160 {Opt_gc_background, "background_gc=%s"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900161 {Opt_disable_roll_forward, "disable_roll_forward"},
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800162 {Opt_norecovery, "norecovery"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900163 {Opt_discard, "discard"},
Chao Yu64058be2016-07-03 22:05:14 +0800164 {Opt_nodiscard, "nodiscard"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900165 {Opt_noheap, "no_heap"},
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400166 {Opt_heap, "heap"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900167 {Opt_user_xattr, "user_xattr"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900168 {Opt_nouser_xattr, "nouser_xattr"},
Kelly Anderson4058c512013-10-07 11:36:20 +0900169 {Opt_acl, "acl"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900170 {Opt_noacl, "noacl"},
171 {Opt_active_logs, "active_logs=%u"},
172 {Opt_disable_ext_identify, "disable_ext_identify"},
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900173 {Opt_inline_xattr, "inline_xattr"},
Chao Yu23cf7212017-02-15 10:34:45 +0800174 {Opt_noinline_xattr, "noinline_xattr"},
Chao Yu6afc6622017-09-06 21:59:50 +0800175 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
Huajun Li8274de72013-11-10 23:13:17 +0800176 {Opt_inline_data, "inline_data"},
Chao Yu5efd3c62014-09-24 18:16:13 +0800177 {Opt_inline_dentry, "inline_dentry"},
Chao Yu97c17942016-05-09 19:56:34 +0800178 {Opt_noinline_dentry, "noinline_dentry"},
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900179 {Opt_flush_merge, "flush_merge"},
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700180 {Opt_noflush_merge, "noflush_merge"},
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700181 {Opt_nobarrier, "nobarrier"},
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700182 {Opt_fastboot, "fastboot"},
Chao Yu89672152015-02-05 17:55:51 +0800183 {Opt_extent_cache, "extent_cache"},
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700184 {Opt_noextent_cache, "noextent_cache"},
Wanpeng Li75342792015-03-24 10:20:27 +0800185 {Opt_noinline_data, "noinline_data"},
Chao Yu343f40f2015-12-16 13:12:16 +0800186 {Opt_data_flush, "data_flush"},
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800187 {Opt_reserve_root, "reserve_root=%u"},
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800188 {Opt_resgid, "resgid=%u"},
189 {Opt_resuid, "resuid=%u"},
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700190 {Opt_mode, "mode=%s"},
Jaegeuk Kimec915382016-12-21 17:09:19 -0800191 {Opt_io_size_bits, "io_bits=%u"},
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700192 {Opt_fault_injection, "fault_injection=%u"},
Chao Yud4945002018-08-08 17:36:41 +0800193 {Opt_fault_type, "fault_type=%u"},
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700194 {Opt_lazytime, "lazytime"},
195 {Opt_nolazytime, "nolazytime"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800196 {Opt_quota, "quota"},
197 {Opt_noquota, "noquota"},
Chao Yu0abd6752017-07-09 00:13:07 +0800198 {Opt_usrquota, "usrquota"},
199 {Opt_grpquota, "grpquota"},
Chao Yu5c571322017-07-26 00:01:41 +0800200 {Opt_prjquota, "prjquota"},
Chao Yu4b2414d2017-08-08 10:54:31 +0800201 {Opt_usrjquota, "usrjquota=%s"},
202 {Opt_grpjquota, "grpjquota=%s"},
203 {Opt_prjjquota, "prjjquota=%s"},
204 {Opt_offusrjquota, "usrjquota="},
205 {Opt_offgrpjquota, "grpjquota="},
206 {Opt_offprjjquota, "prjjquota="},
207 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
208 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
209 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900210 {Opt_whint, "whint_mode=%s"},
Jaegeuk Kim07939622018-02-18 08:50:49 -0800211 {Opt_alloc, "alloc_mode=%s"},
Junling Zheng93cf93f2018-03-07 12:07:49 +0800212 {Opt_fsync, "fsync_mode=%s"},
Eric Biggersed318a62020-05-12 16:32:50 -0700213 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
Sheng Yongff62af22018-03-15 18:51:42 +0800214 {Opt_test_dummy_encryption, "test_dummy_encryption"},
Satya Tangirala27aacd22020-07-02 01:56:06 +0000215 {Opt_inlinecrypt, "inlinecrypt"},
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700216 {Opt_checkpoint_disable, "checkpoint=disable"},
217 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
218 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
219 {Opt_checkpoint_enable, "checkpoint=enable"},
Daeho Jeong261eeb92021-01-19 09:00:42 +0900220 {Opt_checkpoint_merge, "checkpoint_merge"},
221 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
Chao Yu4c8ff702019-11-01 18:07:14 +0800222 {Opt_compress_algorithm, "compress_algorithm=%s"},
223 {Opt_compress_log_size, "compress_log_size=%u"},
224 {Opt_compress_extension, "compress_extension=%s"},
Chao Yub28f0472020-11-26 18:32:09 +0800225 {Opt_compress_chksum, "compress_chksum"},
Daeho Jeong602a16d2020-12-01 13:08:02 +0900226 {Opt_compress_mode, "compress_mode=%s"},
Chao Yu093749e2020-08-04 21:14:49 +0800227 {Opt_atgc, "atgc"},
Chao Yu5911d2d2021-03-27 17:57:06 +0800228 {Opt_gc_merge, "gc_merge"},
229 {Opt_nogc_merge, "nogc_merge"},
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900230 {Opt_err, NULL},
231};
232
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800233void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
Namjae Jeona07ef782012-12-30 14:52:05 +0900234{
235 struct va_format vaf;
236 va_list args;
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800237 int level;
Namjae Jeona07ef782012-12-30 14:52:05 +0900238
239 va_start(args, fmt);
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800240
241 level = printk_get_level(fmt);
242 vaf.fmt = printk_skip_level(fmt);
Namjae Jeona07ef782012-12-30 14:52:05 +0900243 vaf.va = &args;
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800244 printk("%c%cF2FS-fs (%s): %pV\n",
245 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
246
Namjae Jeona07ef782012-12-30 14:52:05 +0900247 va_end(args);
248}
249
Daniel Rosenberg5aba5432019-07-23 16:05:28 -0700250#ifdef CONFIG_UNICODE
251static const struct f2fs_sb_encodings {
252 __u16 magic;
253 char *name;
254 char *version;
255} f2fs_sb_encoding_map[] = {
256 {F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
257};
258
259static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
260 const struct f2fs_sb_encodings **encoding,
261 __u16 *flags)
262{
263 __u16 magic = le16_to_cpu(sb->s_encoding);
264 int i;
265
266 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
267 if (magic == f2fs_sb_encoding_map[i].magic)
268 break;
269
270 if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
271 return -EINVAL;
272
273 *encoding = &f2fs_sb_encoding_map[i];
274 *flags = le16_to_cpu(sb->s_encoding_flags);
275
276 return 0;
277}
278#endif
279
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800280static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
281{
Daniel Rosenberg9a9aeca2019-05-29 17:49:04 -0700282 block_t limit = min((sbi->user_block_count << 1) / 1000,
283 sbi->user_block_count - sbi->reserved_blocks);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800284
285 /* limit is 0.2% */
Chao Yu63189b72018-03-08 14:22:56 +0800286 if (test_opt(sbi, RESERVE_ROOT) &&
287 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
288 F2FS_OPTION(sbi).root_reserved_blocks = limit;
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800289 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
290 F2FS_OPTION(sbi).root_reserved_blocks);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800291 }
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800292 if (!test_opt(sbi, RESERVE_ROOT) &&
Chao Yu63189b72018-03-08 14:22:56 +0800293 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800294 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
Chao Yu63189b72018-03-08 14:22:56 +0800295 !gid_eq(F2FS_OPTION(sbi).s_resgid,
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800296 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800297 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
298 from_kuid_munged(&init_user_ns,
299 F2FS_OPTION(sbi).s_resuid),
300 from_kgid_munged(&init_user_ns,
301 F2FS_OPTION(sbi).s_resgid));
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800302}
303
Jaegeuk Kim1ae18f72020-05-15 17:20:50 -0700304static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
305{
306 if (!F2FS_OPTION(sbi).unusable_cap_perc)
307 return;
308
309 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
310 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
311 else
312 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
313 F2FS_OPTION(sbi).unusable_cap_perc;
314
315 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
316 F2FS_OPTION(sbi).unusable_cap,
317 F2FS_OPTION(sbi).unusable_cap_perc);
318}
319
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900320static void init_once(void *foo)
321{
322 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
323
Jaegeuk Kimaff063e2012-11-02 17:07:47 +0900324 inode_init_once(&fi->vfs_inode);
325}
326
Chao Yu4b2414d2017-08-08 10:54:31 +0800327#ifdef CONFIG_QUOTA
328static const char * const quotatypes[] = INITQFNAMES;
329#define QTYPE2NAME(t) (quotatypes[t])
330static int f2fs_set_qf_name(struct super_block *sb, int qtype,
331 substring_t *args)
332{
333 struct f2fs_sb_info *sbi = F2FS_SB(sb);
334 char *qname;
335 int ret = -EINVAL;
336
Chao Yu63189b72018-03-08 14:22:56 +0800337 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800338 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
Chao Yu4b2414d2017-08-08 10:54:31 +0800339 return -EINVAL;
340 }
Chao Yu7beb01f2018-10-24 18:34:26 +0800341 if (f2fs_sb_has_quota_ino(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800342 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
Jaegeuk Kimea676732017-10-06 09:14:28 -0700343 return 0;
344 }
345
Chao Yu4b2414d2017-08-08 10:54:31 +0800346 qname = match_strdup(args);
347 if (!qname) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800348 f2fs_err(sbi, "Not enough memory for storing quotafile name");
Chengguang Xuf365c6c2019-01-01 21:33:11 +0800349 return -ENOMEM;
Chao Yu4b2414d2017-08-08 10:54:31 +0800350 }
Chao Yu63189b72018-03-08 14:22:56 +0800351 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
352 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
Chao Yu4b2414d2017-08-08 10:54:31 +0800353 ret = 0;
354 else
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800355 f2fs_err(sbi, "%s quota file already specified",
Chao Yu4b2414d2017-08-08 10:54:31 +0800356 QTYPE2NAME(qtype));
357 goto errout;
358 }
359 if (strchr(qname, '/')) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800360 f2fs_err(sbi, "quotafile must be on filesystem root");
Chao Yu4b2414d2017-08-08 10:54:31 +0800361 goto errout;
362 }
Chao Yu63189b72018-03-08 14:22:56 +0800363 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
Chao Yu4b2414d2017-08-08 10:54:31 +0800364 set_opt(sbi, QUOTA);
365 return 0;
366errout:
Wang Xiaojunba87a452020-06-17 20:30:12 +0800367 kfree(qname);
Chao Yu4b2414d2017-08-08 10:54:31 +0800368 return ret;
369}
370
371static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
372{
373 struct f2fs_sb_info *sbi = F2FS_SB(sb);
374
Chao Yu63189b72018-03-08 14:22:56 +0800375 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800376 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
Chao Yu4b2414d2017-08-08 10:54:31 +0800377 return -EINVAL;
378 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800379 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
Chao Yu63189b72018-03-08 14:22:56 +0800380 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
Chao Yu4b2414d2017-08-08 10:54:31 +0800381 return 0;
382}
383
384static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
385{
386 /*
387 * We do the test below only for project quotas. 'usrquota' and
388 * 'grpquota' mount options are allowed even without quota feature
389 * to support legacy quotas in quota files.
390 */
Chao Yu7beb01f2018-10-24 18:34:26 +0800391 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800392 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
Chao Yu4b2414d2017-08-08 10:54:31 +0800393 return -1;
394 }
Chao Yu63189b72018-03-08 14:22:56 +0800395 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
396 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
397 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
398 if (test_opt(sbi, USRQUOTA) &&
399 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800400 clear_opt(sbi, USRQUOTA);
401
Chao Yu63189b72018-03-08 14:22:56 +0800402 if (test_opt(sbi, GRPQUOTA) &&
403 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800404 clear_opt(sbi, GRPQUOTA);
405
Chao Yu63189b72018-03-08 14:22:56 +0800406 if (test_opt(sbi, PRJQUOTA) &&
407 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
Chao Yu4b2414d2017-08-08 10:54:31 +0800408 clear_opt(sbi, PRJQUOTA);
409
410 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
411 test_opt(sbi, PRJQUOTA)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800412 f2fs_err(sbi, "old and new quota format mixing");
Chao Yu4b2414d2017-08-08 10:54:31 +0800413 return -1;
414 }
415
Chao Yu63189b72018-03-08 14:22:56 +0800416 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800417 f2fs_err(sbi, "journaled quota format not specified");
Chao Yu4b2414d2017-08-08 10:54:31 +0800418 return -1;
419 }
420 }
Jaegeuk Kimea676732017-10-06 09:14:28 -0700421
Chao Yu7beb01f2018-10-24 18:34:26 +0800422 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800423 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
Chao Yu63189b72018-03-08 14:22:56 +0800424 F2FS_OPTION(sbi).s_jquota_fmt = 0;
Jaegeuk Kimea676732017-10-06 09:14:28 -0700425 }
Chao Yu4b2414d2017-08-08 10:54:31 +0800426 return 0;
427}
428#endif
429
Eric Biggersed318a62020-05-12 16:32:50 -0700430static int f2fs_set_test_dummy_encryption(struct super_block *sb,
431 const char *opt,
432 const substring_t *arg,
433 bool is_remount)
434{
435 struct f2fs_sb_info *sbi = F2FS_SB(sb);
436#ifdef CONFIG_FS_ENCRYPTION
437 int err;
438
439 if (!f2fs_sb_has_encrypt(sbi)) {
440 f2fs_err(sbi, "Encrypt feature is off");
441 return -EINVAL;
442 }
443
444 /*
445 * This mount option is just for testing, and it's not worthwhile to
446 * implement the extra complexity (e.g. RCU protection) that would be
447 * needed to allow it to be set or changed during remount. We do allow
448 * it to be specified during remount, but only if there is no change.
449 */
Eric Biggersac4acb12020-09-16 21:11:35 -0700450 if (is_remount && !F2FS_OPTION(sbi).dummy_enc_policy.policy) {
Eric Biggersed318a62020-05-12 16:32:50 -0700451 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
452 return -EINVAL;
453 }
454 err = fscrypt_set_test_dummy_encryption(
Eric Biggersc8c868a2020-09-16 21:11:36 -0700455 sb, arg->from, &F2FS_OPTION(sbi).dummy_enc_policy);
Eric Biggersed318a62020-05-12 16:32:50 -0700456 if (err) {
457 if (err == -EEXIST)
458 f2fs_warn(sbi,
459 "Can't change test_dummy_encryption on remount");
460 else if (err == -EINVAL)
461 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
462 opt);
463 else
464 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
465 opt, err);
466 return -EINVAL;
467 }
468 f2fs_warn(sbi, "Test dummy encryption mode enabled");
469#else
470 f2fs_warn(sbi, "Test dummy encryption mount option ignored");
471#endif
472 return 0;
473}
474
Chao Yu3fde13f2021-01-22 17:46:43 +0800475#ifdef CONFIG_F2FS_FS_COMPRESSION
476#ifdef CONFIG_F2FS_FS_LZ4
477static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
478{
479#ifdef CONFIG_F2FS_FS_LZ4HC
480 unsigned int level;
481#endif
482
483 if (strlen(str) == 3) {
484 F2FS_OPTION(sbi).compress_level = 0;
485 return 0;
486 }
487
488#ifdef CONFIG_F2FS_FS_LZ4HC
489 str += 3;
490
491 if (str[0] != ':') {
492 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
493 return -EINVAL;
494 }
495 if (kstrtouint(str + 1, 10, &level))
496 return -EINVAL;
497
498 if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
499 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
500 return -EINVAL;
501 }
502
503 F2FS_OPTION(sbi).compress_level = level;
504 return 0;
505#else
506 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
507 return -EINVAL;
508#endif
509}
510#endif
511
512#ifdef CONFIG_F2FS_FS_ZSTD
513static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
514{
515 unsigned int level;
516 int len = 4;
517
518 if (strlen(str) == len) {
519 F2FS_OPTION(sbi).compress_level = 0;
520 return 0;
521 }
522
523 str += len;
524
525 if (str[0] != ':') {
526 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
527 return -EINVAL;
528 }
529 if (kstrtouint(str + 1, 10, &level))
530 return -EINVAL;
531
532 if (!level || level > ZSTD_maxCLevel()) {
533 f2fs_info(sbi, "invalid zstd compress level: %d", level);
534 return -EINVAL;
535 }
536
537 F2FS_OPTION(sbi).compress_level = level;
538 return 0;
539}
540#endif
541#endif
542
Eric Biggersed318a62020-05-12 16:32:50 -0700543static int parse_options(struct super_block *sb, char *options, bool is_remount)
Namjae Jeon696c0182013-06-16 09:48:48 +0900544{
545 struct f2fs_sb_info *sbi = F2FS_SB(sb);
546 substring_t args[MAX_OPT_ARGS];
Chao Yu1f0b0672020-07-29 21:21:36 +0800547#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800548 unsigned char (*ext)[F2FS_EXTENSION_LEN];
Chao Yu1f0b0672020-07-29 21:21:36 +0800549 int ext_cnt;
550#endif
Namjae Jeon696c0182013-06-16 09:48:48 +0900551 char *p, *name;
Chao Yu1f0b0672020-07-29 21:21:36 +0800552 int arg = 0;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800553 kuid_t uid;
554 kgid_t gid;
Chao Yu4b2414d2017-08-08 10:54:31 +0800555 int ret;
Namjae Jeon696c0182013-06-16 09:48:48 +0900556
557 if (!options)
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -0700558 goto default_check;
Namjae Jeon696c0182013-06-16 09:48:48 +0900559
560 while ((p = strsep(&options, ",")) != NULL) {
561 int token;
Yi Zhuang5f029c02021-04-06 09:47:35 +0800562
Namjae Jeon696c0182013-06-16 09:48:48 +0900563 if (!*p)
564 continue;
565 /*
566 * Initialize args struct so we know whether arg was
567 * found; some options take optional arguments.
568 */
569 args[0].to = args[0].from = NULL;
570 token = match_token(p, f2fs_tokens, args);
571
572 switch (token) {
573 case Opt_gc_background:
574 name = match_strdup(&args[0]);
575
576 if (!name)
577 return -ENOMEM;
Eric Biggers3c57f752020-05-01 16:35:23 -0700578 if (!strcmp(name, "on")) {
Chao Yubbbc34f2020-02-14 17:44:13 +0800579 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
Eric Biggers3c57f752020-05-01 16:35:23 -0700580 } else if (!strcmp(name, "off")) {
Chao Yubbbc34f2020-02-14 17:44:13 +0800581 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
Eric Biggers3c57f752020-05-01 16:35:23 -0700582 } else if (!strcmp(name, "sync")) {
Chao Yubbbc34f2020-02-14 17:44:13 +0800583 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700584 } else {
Wang Xiaojunba87a452020-06-17 20:30:12 +0800585 kfree(name);
Namjae Jeon696c0182013-06-16 09:48:48 +0900586 return -EINVAL;
587 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800588 kfree(name);
Namjae Jeon696c0182013-06-16 09:48:48 +0900589 break;
590 case Opt_disable_roll_forward:
591 set_opt(sbi, DISABLE_ROLL_FORWARD);
592 break;
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800593 case Opt_norecovery:
594 /* this option mounts f2fs with ro */
Chao Yua9117ec2020-02-14 17:45:11 +0800595 set_opt(sbi, NORECOVERY);
Jaegeuk Kim2d834bf2015-01-23 18:33:46 -0800596 if (!f2fs_readonly(sb))
597 return -EINVAL;
598 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900599 case Opt_discard:
Chao Yu7d20c8a2018-09-04 03:52:17 +0800600 set_opt(sbi, DISCARD);
Namjae Jeon696c0182013-06-16 09:48:48 +0900601 break;
Chao Yu64058be2016-07-03 22:05:14 +0800602 case Opt_nodiscard:
Chao Yu7beb01f2018-10-24 18:34:26 +0800603 if (f2fs_sb_has_blkzoned(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800604 f2fs_warn(sbi, "discard is required for zoned block devices");
Damien Le Moal96ba2de2016-10-28 17:45:03 +0900605 return -EINVAL;
606 }
Chao Yu64058be2016-07-03 22:05:14 +0800607 clear_opt(sbi, DISCARD);
Damien Le Moal487df612016-10-28 17:44:59 +0900608 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900609 case Opt_noheap:
610 set_opt(sbi, NOHEAP);
611 break;
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -0400612 case Opt_heap:
613 clear_opt(sbi, NOHEAP);
614 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900615#ifdef CONFIG_F2FS_FS_XATTR
Kelly Anderson4058c512013-10-07 11:36:20 +0900616 case Opt_user_xattr:
617 set_opt(sbi, XATTR_USER);
618 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900619 case Opt_nouser_xattr:
620 clear_opt(sbi, XATTR_USER);
621 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900622 case Opt_inline_xattr:
623 set_opt(sbi, INLINE_XATTR);
624 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800625 case Opt_noinline_xattr:
626 clear_opt(sbi, INLINE_XATTR);
627 break;
Chao Yu6afc6622017-09-06 21:59:50 +0800628 case Opt_inline_xattr_size:
629 if (args->from && match_int(args, &arg))
630 return -EINVAL;
631 set_opt(sbi, INLINE_XATTR_SIZE);
Chao Yu63189b72018-03-08 14:22:56 +0800632 F2FS_OPTION(sbi).inline_xattr_size = arg;
Chao Yu6afc6622017-09-06 21:59:50 +0800633 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900634#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900635 case Opt_user_xattr:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800636 f2fs_info(sbi, "user_xattr options not supported");
Kelly Anderson4058c512013-10-07 11:36:20 +0900637 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900638 case Opt_nouser_xattr:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800639 f2fs_info(sbi, "nouser_xattr options not supported");
Namjae Jeon696c0182013-06-16 09:48:48 +0900640 break;
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900641 case Opt_inline_xattr:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800642 f2fs_info(sbi, "inline_xattr options not supported");
Jaegeuk Kim444c5802013-08-08 15:16:22 +0900643 break;
Chao Yu23cf7212017-02-15 10:34:45 +0800644 case Opt_noinline_xattr:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800645 f2fs_info(sbi, "noinline_xattr options not supported");
Chao Yu23cf7212017-02-15 10:34:45 +0800646 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900647#endif
648#ifdef CONFIG_F2FS_FS_POSIX_ACL
Kelly Anderson4058c512013-10-07 11:36:20 +0900649 case Opt_acl:
650 set_opt(sbi, POSIX_ACL);
651 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900652 case Opt_noacl:
653 clear_opt(sbi, POSIX_ACL);
654 break;
655#else
Kelly Anderson4058c512013-10-07 11:36:20 +0900656 case Opt_acl:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800657 f2fs_info(sbi, "acl options not supported");
Kelly Anderson4058c512013-10-07 11:36:20 +0900658 break;
Namjae Jeon696c0182013-06-16 09:48:48 +0900659 case Opt_noacl:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800660 f2fs_info(sbi, "noacl options not supported");
Namjae Jeon696c0182013-06-16 09:48:48 +0900661 break;
662#endif
663 case Opt_active_logs:
664 if (args->from && match_int(args, &arg))
665 return -EINVAL;
Chao Yud0b9e422020-08-04 21:14:45 +0800666 if (arg != 2 && arg != 4 &&
667 arg != NR_CURSEG_PERSIST_TYPE)
Namjae Jeon696c0182013-06-16 09:48:48 +0900668 return -EINVAL;
Chao Yu63189b72018-03-08 14:22:56 +0800669 F2FS_OPTION(sbi).active_logs = arg;
Namjae Jeon696c0182013-06-16 09:48:48 +0900670 break;
671 case Opt_disable_ext_identify:
672 set_opt(sbi, DISABLE_EXT_IDENTIFY);
673 break;
Huajun Li8274de72013-11-10 23:13:17 +0800674 case Opt_inline_data:
675 set_opt(sbi, INLINE_DATA);
676 break;
Chao Yu5efd3c62014-09-24 18:16:13 +0800677 case Opt_inline_dentry:
678 set_opt(sbi, INLINE_DENTRY);
679 break;
Chao Yu97c17942016-05-09 19:56:34 +0800680 case Opt_noinline_dentry:
681 clear_opt(sbi, INLINE_DENTRY);
682 break;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900683 case Opt_flush_merge:
684 set_opt(sbi, FLUSH_MERGE);
685 break;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -0700686 case Opt_noflush_merge:
687 clear_opt(sbi, FLUSH_MERGE);
688 break;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700689 case Opt_nobarrier:
690 set_opt(sbi, NOBARRIER);
691 break;
Jaegeuk Kimd5053a342014-10-30 22:47:03 -0700692 case Opt_fastboot:
693 set_opt(sbi, FASTBOOT);
694 break;
Chao Yu89672152015-02-05 17:55:51 +0800695 case Opt_extent_cache:
696 set_opt(sbi, EXTENT_CACHE);
697 break;
Jaegeuk Kim7daaea22015-06-25 17:43:04 -0700698 case Opt_noextent_cache:
699 clear_opt(sbi, EXTENT_CACHE);
700 break;
Wanpeng Li75342792015-03-24 10:20:27 +0800701 case Opt_noinline_data:
702 clear_opt(sbi, INLINE_DATA);
703 break;
Chao Yu343f40f2015-12-16 13:12:16 +0800704 case Opt_data_flush:
705 set_opt(sbi, DATA_FLUSH);
706 break;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800707 case Opt_reserve_root:
708 if (args->from && match_int(args, &arg))
709 return -EINVAL;
710 if (test_opt(sbi, RESERVE_ROOT)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800711 f2fs_info(sbi, "Preserve previous reserve_root=%u",
712 F2FS_OPTION(sbi).root_reserved_blocks);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800713 } else {
Chao Yu63189b72018-03-08 14:22:56 +0800714 F2FS_OPTION(sbi).root_reserved_blocks = arg;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -0800715 set_opt(sbi, RESERVE_ROOT);
716 }
717 break;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800718 case Opt_resuid:
719 if (args->from && match_int(args, &arg))
720 return -EINVAL;
721 uid = make_kuid(current_user_ns(), arg);
722 if (!uid_valid(uid)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800723 f2fs_err(sbi, "Invalid uid value %d", arg);
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800724 return -EINVAL;
725 }
Chao Yu63189b72018-03-08 14:22:56 +0800726 F2FS_OPTION(sbi).s_resuid = uid;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800727 break;
728 case Opt_resgid:
729 if (args->from && match_int(args, &arg))
730 return -EINVAL;
731 gid = make_kgid(current_user_ns(), arg);
732 if (!gid_valid(gid)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800733 f2fs_err(sbi, "Invalid gid value %d", arg);
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800734 return -EINVAL;
735 }
Chao Yu63189b72018-03-08 14:22:56 +0800736 F2FS_OPTION(sbi).s_resgid = gid;
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -0800737 break;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700738 case Opt_mode:
739 name = match_strdup(&args[0]);
740
741 if (!name)
742 return -ENOMEM;
Eric Biggers3c57f752020-05-01 16:35:23 -0700743 if (!strcmp(name, "adaptive")) {
Chao Yu7beb01f2018-10-24 18:34:26 +0800744 if (f2fs_sb_has_blkzoned(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800745 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
Wang Xiaojunba87a452020-06-17 20:30:12 +0800746 kfree(name);
Damien Le Moal3adc57e2016-10-28 17:45:04 +0900747 return -EINVAL;
748 }
Chao Yub0332a02020-02-14 17:44:12 +0800749 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
Eric Biggers3c57f752020-05-01 16:35:23 -0700750 } else if (!strcmp(name, "lfs")) {
Chao Yub0332a02020-02-14 17:44:12 +0800751 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700752 } else {
Wang Xiaojunba87a452020-06-17 20:30:12 +0800753 kfree(name);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700754 return -EINVAL;
755 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800756 kfree(name);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700757 break;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800758 case Opt_io_size_bits:
759 if (args->from && match_int(args, &arg))
760 return -EINVAL;
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100761 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_VECS)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800762 f2fs_warn(sbi, "Not support %d, larger than %d",
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100763 1 << arg, BIO_MAX_VECS);
Jaegeuk Kimec915382016-12-21 17:09:19 -0800764 return -EINVAL;
765 }
Chao Yu63189b72018-03-08 14:22:56 +0800766 F2FS_OPTION(sbi).write_io_size_bits = arg;
Jaegeuk Kimec915382016-12-21 17:09:19 -0800767 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800768#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700769 case Opt_fault_injection:
770 if (args->from && match_int(args, &arg))
771 return -EINVAL;
Chao Yud4945002018-08-08 17:36:41 +0800772 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
773 set_opt(sbi, FAULT_INJECTION);
Chao Yud4945002018-08-08 17:36:41 +0800774 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800775
Chao Yud4945002018-08-08 17:36:41 +0800776 case Opt_fault_type:
777 if (args->from && match_int(args, &arg))
778 return -EINVAL;
Chao Yud4945002018-08-08 17:36:41 +0800779 f2fs_build_fault_attr(sbi, 0, arg);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +0800780 set_opt(sbi, FAULT_INJECTION);
Jaegeuk Kim73faec4d2016-04-29 15:34:32 -0700781 break;
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800782#else
783 case Opt_fault_injection:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800784 f2fs_info(sbi, "fault_injection options not supported");
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800785 break;
786
787 case Opt_fault_type:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800788 f2fs_info(sbi, "fault_type options not supported");
Chengguang Xu4cb037e2018-09-12 13:32:52 +0800789 break;
790#endif
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700791 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800792 sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700793 break;
794 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800795 sb->s_flags &= ~SB_LAZYTIME;
Jaegeuk Kim6d94c742016-05-20 21:47:24 -0700796 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800797#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +0800798 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800799 case Opt_usrquota:
800 set_opt(sbi, USRQUOTA);
801 break;
802 case Opt_grpquota:
803 set_opt(sbi, GRPQUOTA);
804 break;
Chao Yu5c571322017-07-26 00:01:41 +0800805 case Opt_prjquota:
806 set_opt(sbi, PRJQUOTA);
807 break;
Chao Yu4b2414d2017-08-08 10:54:31 +0800808 case Opt_usrjquota:
809 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
810 if (ret)
811 return ret;
812 break;
813 case Opt_grpjquota:
814 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
815 if (ret)
816 return ret;
817 break;
818 case Opt_prjjquota:
819 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
820 if (ret)
821 return ret;
822 break;
823 case Opt_offusrjquota:
824 ret = f2fs_clear_qf_name(sb, USRQUOTA);
825 if (ret)
826 return ret;
827 break;
828 case Opt_offgrpjquota:
829 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
830 if (ret)
831 return ret;
832 break;
833 case Opt_offprjjquota:
834 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
835 if (ret)
836 return ret;
837 break;
838 case Opt_jqfmt_vfsold:
Chao Yu63189b72018-03-08 14:22:56 +0800839 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
Chao Yu4b2414d2017-08-08 10:54:31 +0800840 break;
841 case Opt_jqfmt_vfsv0:
Chao Yu63189b72018-03-08 14:22:56 +0800842 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
Chao Yu4b2414d2017-08-08 10:54:31 +0800843 break;
844 case Opt_jqfmt_vfsv1:
Chao Yu63189b72018-03-08 14:22:56 +0800845 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
Chao Yu4b2414d2017-08-08 10:54:31 +0800846 break;
847 case Opt_noquota:
848 clear_opt(sbi, QUOTA);
849 clear_opt(sbi, USRQUOTA);
850 clear_opt(sbi, GRPQUOTA);
851 clear_opt(sbi, PRJQUOTA);
852 break;
Chao Yu0abd6752017-07-09 00:13:07 +0800853#else
Chao Yu4b2414d2017-08-08 10:54:31 +0800854 case Opt_quota:
Chao Yu0abd6752017-07-09 00:13:07 +0800855 case Opt_usrquota:
856 case Opt_grpquota:
Chao Yu5c571322017-07-26 00:01:41 +0800857 case Opt_prjquota:
Chao Yu4b2414d2017-08-08 10:54:31 +0800858 case Opt_usrjquota:
859 case Opt_grpjquota:
860 case Opt_prjjquota:
861 case Opt_offusrjquota:
862 case Opt_offgrpjquota:
863 case Opt_offprjjquota:
864 case Opt_jqfmt_vfsold:
865 case Opt_jqfmt_vfsv0:
866 case Opt_jqfmt_vfsv1:
867 case Opt_noquota:
Joe Perchesdcbb4c12019-06-18 17:48:42 +0800868 f2fs_info(sbi, "quota operations not supported");
Chao Yu0abd6752017-07-09 00:13:07 +0800869 break;
870#endif
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900871 case Opt_whint:
872 name = match_strdup(&args[0]);
873 if (!name)
874 return -ENOMEM;
Eric Biggers3c57f752020-05-01 16:35:23 -0700875 if (!strcmp(name, "user-based")) {
Chao Yu63189b72018-03-08 14:22:56 +0800876 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
Eric Biggers3c57f752020-05-01 16:35:23 -0700877 } else if (!strcmp(name, "off")) {
Chao Yu63189b72018-03-08 14:22:56 +0800878 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
Eric Biggers3c57f752020-05-01 16:35:23 -0700879 } else if (!strcmp(name, "fs-based")) {
Chao Yu63189b72018-03-08 14:22:56 +0800880 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900881 } else {
Wang Xiaojunba87a452020-06-17 20:30:12 +0800882 kfree(name);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900883 return -EINVAL;
884 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800885 kfree(name);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900886 break;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800887 case Opt_alloc:
888 name = match_strdup(&args[0]);
889 if (!name)
890 return -ENOMEM;
891
Eric Biggers3c57f752020-05-01 16:35:23 -0700892 if (!strcmp(name, "default")) {
Chao Yu63189b72018-03-08 14:22:56 +0800893 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
Eric Biggers3c57f752020-05-01 16:35:23 -0700894 } else if (!strcmp(name, "reuse")) {
Chao Yu63189b72018-03-08 14:22:56 +0800895 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
Jaegeuk Kim07939622018-02-18 08:50:49 -0800896 } else {
Wang Xiaojunba87a452020-06-17 20:30:12 +0800897 kfree(name);
Jaegeuk Kim07939622018-02-18 08:50:49 -0800898 return -EINVAL;
899 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800900 kfree(name);
Jaegeuk Kim07939622018-02-18 08:50:49 -0800901 break;
Junling Zheng93cf93f2018-03-07 12:07:49 +0800902 case Opt_fsync:
903 name = match_strdup(&args[0]);
904 if (!name)
905 return -ENOMEM;
Eric Biggers3c57f752020-05-01 16:35:23 -0700906 if (!strcmp(name, "posix")) {
Chao Yu63189b72018-03-08 14:22:56 +0800907 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
Eric Biggers3c57f752020-05-01 16:35:23 -0700908 } else if (!strcmp(name, "strict")) {
Chao Yu63189b72018-03-08 14:22:56 +0800909 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
Eric Biggers3c57f752020-05-01 16:35:23 -0700910 } else if (!strcmp(name, "nobarrier")) {
Jaegeuk Kimd6290812018-05-25 18:02:58 -0700911 F2FS_OPTION(sbi).fsync_mode =
912 FSYNC_MODE_NOBARRIER;
Junling Zheng93cf93f2018-03-07 12:07:49 +0800913 } else {
Wang Xiaojunba87a452020-06-17 20:30:12 +0800914 kfree(name);
Junling Zheng93cf93f2018-03-07 12:07:49 +0800915 return -EINVAL;
916 }
Wang Xiaojunba87a452020-06-17 20:30:12 +0800917 kfree(name);
Junling Zheng93cf93f2018-03-07 12:07:49 +0800918 break;
Sheng Yongff62af22018-03-15 18:51:42 +0800919 case Opt_test_dummy_encryption:
Eric Biggersed318a62020-05-12 16:32:50 -0700920 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
921 is_remount);
922 if (ret)
923 return ret;
Sheng Yongff62af22018-03-15 18:51:42 +0800924 break;
Satya Tangirala27aacd22020-07-02 01:56:06 +0000925 case Opt_inlinecrypt:
926#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
927 sb->s_flags |= SB_INLINECRYPT;
928#else
929 f2fs_info(sbi, "inline encryption not supported");
930#endif
931 break;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700932 case Opt_checkpoint_disable_cap_perc:
933 if (args->from && match_int(args, &arg))
Daniel Rosenberg43549942018-08-20 19:21:43 -0700934 return -EINVAL;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700935 if (arg < 0 || arg > 100)
936 return -EINVAL;
Jaegeuk Kim1ae18f72020-05-15 17:20:50 -0700937 F2FS_OPTION(sbi).unusable_cap_perc = arg;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -0700938 set_opt(sbi, DISABLE_CHECKPOINT);
939 break;
940 case Opt_checkpoint_disable_cap:
941 if (args->from && match_int(args, &arg))
942 return -EINVAL;
943 F2FS_OPTION(sbi).unusable_cap = arg;
944 set_opt(sbi, DISABLE_CHECKPOINT);
945 break;
946 case Opt_checkpoint_disable:
947 set_opt(sbi, DISABLE_CHECKPOINT);
948 break;
949 case Opt_checkpoint_enable:
950 clear_opt(sbi, DISABLE_CHECKPOINT);
Daniel Rosenberg43549942018-08-20 19:21:43 -0700951 break;
Daeho Jeong261eeb92021-01-19 09:00:42 +0900952 case Opt_checkpoint_merge:
953 set_opt(sbi, MERGE_CHECKPOINT);
954 break;
955 case Opt_nocheckpoint_merge:
956 clear_opt(sbi, MERGE_CHECKPOINT);
957 break;
Chao Yu1f0b0672020-07-29 21:21:36 +0800958#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +0800959 case Opt_compress_algorithm:
960 if (!f2fs_sb_has_compression(sbi)) {
Chao Yu69c0dd22020-09-03 10:14:41 +0800961 f2fs_info(sbi, "Image doesn't support compression");
962 break;
Chao Yu4c8ff702019-11-01 18:07:14 +0800963 }
964 name = match_strdup(&args[0]);
965 if (!name)
966 return -ENOMEM;
Eric Biggers3c57f752020-05-01 16:35:23 -0700967 if (!strcmp(name, "lzo")) {
Chao Yu32be0e92021-01-22 17:40:13 +0800968#ifdef CONFIG_F2FS_FS_LZO
Chao Yu3fde13f2021-01-22 17:46:43 +0800969 F2FS_OPTION(sbi).compress_level = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +0800970 F2FS_OPTION(sbi).compress_algorithm =
971 COMPRESS_LZO;
Chao Yu32be0e92021-01-22 17:40:13 +0800972#else
973 f2fs_info(sbi, "kernel doesn't support lzo compression");
974#endif
Chao Yu3fde13f2021-01-22 17:46:43 +0800975 } else if (!strncmp(name, "lz4", 3)) {
Chao Yu32be0e92021-01-22 17:40:13 +0800976#ifdef CONFIG_F2FS_FS_LZ4
Chao Yu3fde13f2021-01-22 17:46:43 +0800977 ret = f2fs_set_lz4hc_level(sbi, name);
978 if (ret) {
979 kfree(name);
980 return -EINVAL;
981 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800982 F2FS_OPTION(sbi).compress_algorithm =
983 COMPRESS_LZ4;
Chao Yu32be0e92021-01-22 17:40:13 +0800984#else
985 f2fs_info(sbi, "kernel doesn't support lz4 compression");
986#endif
Chao Yu3fde13f2021-01-22 17:46:43 +0800987 } else if (!strncmp(name, "zstd", 4)) {
Chao Yu32be0e92021-01-22 17:40:13 +0800988#ifdef CONFIG_F2FS_FS_ZSTD
Chao Yu3fde13f2021-01-22 17:46:43 +0800989 ret = f2fs_set_zstd_level(sbi, name);
990 if (ret) {
991 kfree(name);
992 return -EINVAL;
993 }
Chao Yu50cfa662020-03-03 17:46:02 +0800994 F2FS_OPTION(sbi).compress_algorithm =
995 COMPRESS_ZSTD;
Chao Yu32be0e92021-01-22 17:40:13 +0800996#else
997 f2fs_info(sbi, "kernel doesn't support zstd compression");
998#endif
Chao Yu6d92b202020-04-08 19:56:32 +0800999 } else if (!strcmp(name, "lzo-rle")) {
Chao Yu32be0e92021-01-22 17:40:13 +08001000#ifdef CONFIG_F2FS_FS_LZORLE
Chao Yu3fde13f2021-01-22 17:46:43 +08001001 F2FS_OPTION(sbi).compress_level = 0;
Chao Yu6d92b202020-04-08 19:56:32 +08001002 F2FS_OPTION(sbi).compress_algorithm =
1003 COMPRESS_LZORLE;
Chao Yu32be0e92021-01-22 17:40:13 +08001004#else
1005 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1006#endif
Chao Yu4c8ff702019-11-01 18:07:14 +08001007 } else {
1008 kfree(name);
1009 return -EINVAL;
1010 }
1011 kfree(name);
1012 break;
1013 case Opt_compress_log_size:
1014 if (!f2fs_sb_has_compression(sbi)) {
Chao Yu69c0dd22020-09-03 10:14:41 +08001015 f2fs_info(sbi, "Image doesn't support compression");
1016 break;
Chao Yu4c8ff702019-11-01 18:07:14 +08001017 }
1018 if (args->from && match_int(args, &arg))
1019 return -EINVAL;
1020 if (arg < MIN_COMPRESS_LOG_SIZE ||
1021 arg > MAX_COMPRESS_LOG_SIZE) {
1022 f2fs_err(sbi,
1023 "Compress cluster log size is out of range");
1024 return -EINVAL;
1025 }
1026 F2FS_OPTION(sbi).compress_log_size = arg;
1027 break;
1028 case Opt_compress_extension:
1029 if (!f2fs_sb_has_compression(sbi)) {
Chao Yu69c0dd22020-09-03 10:14:41 +08001030 f2fs_info(sbi, "Image doesn't support compression");
1031 break;
Chao Yu4c8ff702019-11-01 18:07:14 +08001032 }
1033 name = match_strdup(&args[0]);
1034 if (!name)
1035 return -ENOMEM;
1036
1037 ext = F2FS_OPTION(sbi).extensions;
1038 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1039
1040 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1041 ext_cnt >= COMPRESS_EXT_NUM) {
1042 f2fs_err(sbi,
1043 "invalid extension length/number");
1044 kfree(name);
1045 return -EINVAL;
1046 }
1047
1048 strcpy(ext[ext_cnt], name);
1049 F2FS_OPTION(sbi).compress_ext_cnt++;
1050 kfree(name);
1051 break;
Chao Yub28f0472020-11-26 18:32:09 +08001052 case Opt_compress_chksum:
1053 F2FS_OPTION(sbi).compress_chksum = true;
1054 break;
Daeho Jeong602a16d2020-12-01 13:08:02 +09001055 case Opt_compress_mode:
1056 name = match_strdup(&args[0]);
1057 if (!name)
1058 return -ENOMEM;
1059 if (!strcmp(name, "fs")) {
1060 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1061 } else if (!strcmp(name, "user")) {
1062 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1063 } else {
1064 kfree(name);
1065 return -EINVAL;
1066 }
1067 kfree(name);
1068 break;
Chao Yu1f0b0672020-07-29 21:21:36 +08001069#else
1070 case Opt_compress_algorithm:
1071 case Opt_compress_log_size:
1072 case Opt_compress_extension:
Chao Yub28f0472020-11-26 18:32:09 +08001073 case Opt_compress_chksum:
Daeho Jeong602a16d2020-12-01 13:08:02 +09001074 case Opt_compress_mode:
Chao Yu1f0b0672020-07-29 21:21:36 +08001075 f2fs_info(sbi, "compression options not supported");
1076 break;
1077#endif
Chao Yu093749e2020-08-04 21:14:49 +08001078 case Opt_atgc:
1079 set_opt(sbi, ATGC);
1080 break;
Chao Yu5911d2d2021-03-27 17:57:06 +08001081 case Opt_gc_merge:
1082 set_opt(sbi, GC_MERGE);
1083 break;
1084 case Opt_nogc_merge:
1085 clear_opt(sbi, GC_MERGE);
1086 break;
Namjae Jeon696c0182013-06-16 09:48:48 +09001087 default:
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001088 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1089 p);
Namjae Jeon696c0182013-06-16 09:48:48 +09001090 return -EINVAL;
1091 }
1092 }
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07001093default_check:
Chao Yu4b2414d2017-08-08 10:54:31 +08001094#ifdef CONFIG_QUOTA
1095 if (f2fs_check_quota_options(sbi))
1096 return -EINVAL;
Sheng Yong00960c2cd2018-07-24 20:17:52 +08001097#else
Chao Yu7beb01f2018-10-24 18:34:26 +08001098 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001099 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
Sheng Yong00960c2cd2018-07-24 20:17:52 +08001100 return -EINVAL;
1101 }
Chao Yu7beb01f2018-10-24 18:34:26 +08001102 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001103 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
Chao Yu4ddc1b22018-07-26 07:19:48 +08001104 return -EINVAL;
1105 }
Chao Yu4b2414d2017-08-08 10:54:31 +08001106#endif
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07001107#ifndef CONFIG_UNICODE
1108 if (f2fs_sb_has_casefold(sbi)) {
1109 f2fs_err(sbi,
1110 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1111 return -EINVAL;
1112 }
1113#endif
Chao Yud0660122020-09-21 20:53:14 +08001114 /*
1115 * The BLKZONED feature indicates that the drive was formatted with
1116 * zone alignment optimization. This is optional for host-aware
1117 * devices, but mandatory for host-managed zoned block devices.
1118 */
1119#ifndef CONFIG_BLK_DEV_ZONED
1120 if (f2fs_sb_has_blkzoned(sbi)) {
1121 f2fs_err(sbi, "Zoned block device support is not enabled");
1122 return -EINVAL;
1123 }
1124#endif
Jaegeuk Kimec915382016-12-21 17:09:19 -08001125
Chao Yub0332a02020-02-14 17:44:12 +08001126 if (F2FS_IO_SIZE_BITS(sbi) && !f2fs_lfs_mode(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001127 f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
1128 F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kimec915382016-12-21 17:09:19 -08001129 return -EINVAL;
1130 }
Chao Yu6afc6622017-09-06 21:59:50 +08001131
1132 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
Jaegeuk Kim70db5b02019-03-12 11:49:53 -07001133 int min_size, max_size;
1134
Chao Yu7beb01f2018-10-24 18:34:26 +08001135 if (!f2fs_sb_has_extra_attr(sbi) ||
1136 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001137 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
Chao Yu4d817ae2018-01-27 17:29:48 +08001138 return -EINVAL;
1139 }
Chao Yu6afc6622017-09-06 21:59:50 +08001140 if (!test_opt(sbi, INLINE_XATTR)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001141 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
Chao Yu6afc6622017-09-06 21:59:50 +08001142 return -EINVAL;
1143 }
Jaegeuk Kim70db5b02019-03-12 11:49:53 -07001144
1145 min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
Chao Yudd6c89b2019-03-04 17:19:04 +08001146 max_size = MAX_INLINE_XATTR_SIZE;
Jaegeuk Kim70db5b02019-03-12 11:49:53 -07001147
1148 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1149 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001150 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1151 min_size, max_size);
Chao Yu6afc6622017-09-06 21:59:50 +08001152 return -EINVAL;
1153 }
1154 }
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001155
Chao Yub0332a02020-02-14 17:44:12 +08001156 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
Joe Perches833dcd32021-05-26 13:05:36 -07001157 f2fs_err(sbi, "LFS not compatible with checkpoint=disable");
Daniel Rosenberg43549942018-08-20 19:21:43 -07001158 return -EINVAL;
1159 }
1160
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001161 /* Not pass down write hints if the number of active logs is lesser
Chao Yud0b9e422020-08-04 21:14:45 +08001162 * than NR_CURSEG_PERSIST_TYPE.
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001163 */
Chao Yu63189b72018-03-08 14:22:56 +08001164 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
1165 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07001166
1167 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1168 f2fs_err(sbi, "Allow to mount readonly mode only");
1169 return -EROFS;
1170 }
Namjae Jeon696c0182013-06-16 09:48:48 +09001171 return 0;
1172}
1173
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001174static struct inode *f2fs_alloc_inode(struct super_block *sb)
1175{
1176 struct f2fs_inode_info *fi;
1177
Chao Yua0acdfe2013-12-05 09:54:00 +08001178 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001179 if (!fi)
1180 return NULL;
1181
1182 init_once((void *) fi);
1183
Masanari Iida434720f2013-03-19 08:03:35 +09001184 /* Initialize f2fs-specific inode info */
Jaegeuk Kim204706c2016-12-02 15:11:32 -08001185 atomic_set(&fi->dirty_pages, 0);
Daeho Jeongc2759eb2020-09-08 11:44:10 +09001186 atomic_set(&fi->i_compr_blocks, 0);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +09001187 init_rwsem(&fi->i_sem);
Chao Yuc10c9822020-02-27 19:30:03 +08001188 spin_lock_init(&fi->i_size_lock);
Chao Yu2710fd72015-12-15 13:30:45 +08001189 INIT_LIST_HEAD(&fi->dirty_list);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001190 INIT_LIST_HEAD(&fi->gdirty_list);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07001191 INIT_LIST_HEAD(&fi->inmem_ilist);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001192 INIT_LIST_HEAD(&fi->inmem_pages);
1193 mutex_init(&fi->inmem_lock);
Chao Yub2532c62018-04-24 10:55:28 +08001194 init_rwsem(&fi->i_gc_rwsem[READ]);
1195 init_rwsem(&fi->i_gc_rwsem[WRITE]);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001196 init_rwsem(&fi->i_mmap_sem);
Yunlei He27161f12017-09-07 10:40:54 +08001197 init_rwsem(&fi->i_xattr_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001198
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001199 /* Will be used by directory only */
1200 fi->i_dir_level = F2FS_SB(sb)->dir_level;
Chao Yuf2470372017-07-19 00:19:05 +08001201
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001202 return &fi->vfs_inode;
1203}
1204
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001205static int f2fs_drop_inode(struct inode *inode)
1206{
Chao Yua8933b62019-07-18 16:39:59 +08001207 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Hou Pengyangb8d96a32017-02-27 13:02:58 +00001208 int ret;
Chao Yua8933b62019-07-18 16:39:59 +08001209
1210 /*
1211 * during filesystem shutdown, if checkpoint is disabled,
1212 * drop useless meta/node dirty pages.
1213 */
1214 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1215 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1216 inode->i_ino == F2FS_META_INO(sbi)) {
1217 trace_f2fs_drop_inode(inode, 1);
1218 return 1;
1219 }
1220 }
1221
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001222 /*
1223 * This is to avoid a deadlock condition like below.
1224 * writeback_single_inode(inode)
1225 * - f2fs_write_data_page
1226 * - f2fs_gc -> iput -> evict
1227 * - inode_wait_for_writeback(inode)
1228 */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001229 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001230 if (!inode->i_nlink && !is_bad_inode(inode)) {
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001231 /* to avoid evict_inode call simultaneously */
1232 atomic_inc(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001233 spin_unlock(&inode->i_lock);
1234
1235 /* some remained atomic pages should discarded */
1236 if (f2fs_is_atomic_file(inode))
Chao Yu4d57b862018-05-30 00:20:41 +08001237 f2fs_drop_inmem_pages(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001238
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001239 /* should remain fi->extent_tree for writepage */
1240 f2fs_destroy_extent_node(inode);
1241
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001242 sb_start_intwrite(inode->i_sb);
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001243 f2fs_i_size_write(inode, 0);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001244
zhenglianga0770e12019-03-04 09:32:25 +08001245 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1246 inode, NULL, 0, DATA);
1247 truncate_inode_pages_final(inode->i_mapping);
1248
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001249 if (F2FS_HAS_BLOCKS(inode))
Jaegeuk Kim9a449e92016-06-02 13:49:38 -07001250 f2fs_truncate(inode);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001251
1252 sb_end_intwrite(inode->i_sb);
1253
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001254 spin_lock(&inode->i_lock);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001255 atomic_dec(&inode->i_count);
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001256 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +00001257 trace_f2fs_drop_inode(inode, 0);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001258 return 0;
Jaegeuk Kim06e1bc02015-05-13 14:35:14 -07001259 }
Hou Pengyangb8d96a32017-02-27 13:02:58 +00001260 ret = generic_drop_inode(inode);
Eric Biggers8ce589c2019-08-04 19:35:48 -07001261 if (!ret)
1262 ret = fscrypt_drop_inode(inode);
Hou Pengyangb8d96a32017-02-27 13:02:58 +00001263 trace_f2fs_drop_inode(inode, ret);
1264 return ret;
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001265}
1266
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001267int f2fs_inode_dirtied(struct inode *inode, bool sync)
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001268{
1269 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001270 int ret = 0;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001271
1272 spin_lock(&sbi->inode_lock[DIRTY_META]);
1273 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001274 ret = 1;
1275 } else {
1276 set_inode_flag(inode, FI_DIRTY_INODE);
1277 stat_inc_dirty_inode(sbi, DIRTY_META);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001278 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001279 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1280 list_add_tail(&F2FS_I(inode)->gdirty_list,
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001281 &sbi->inode_list[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001282 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1283 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001284 spin_unlock(&sbi->inode_lock[DIRTY_META]);
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001285 return ret;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001286}
1287
1288void f2fs_inode_synced(struct inode *inode)
1289{
1290 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1291
1292 spin_lock(&sbi->inode_lock[DIRTY_META]);
1293 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1294 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1295 return;
1296 }
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001297 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1298 list_del_init(&F2FS_I(inode)->gdirty_list);
1299 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1300 }
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001301 clear_inode_flag(inode, FI_DIRTY_INODE);
1302 clear_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -07001303 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1304 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1305}
1306
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001307/*
1308 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1309 *
1310 * We should call set_dirty_inode to write the dirty inode through write_inode.
1311 */
1312static void f2fs_dirty_inode(struct inode *inode, int flags)
1313{
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001314 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1315
1316 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1317 inode->i_ino == F2FS_META_INO(sbi))
1318 return;
1319
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001320 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1321 clear_inode_flag(inode, FI_AUTO_RECOVER);
1322
Jaegeuk Kim7c457292016-10-14 11:51:23 -07001323 f2fs_inode_dirtied(inode, false);
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001324}
1325
Al Virod01718a2019-04-15 19:29:14 -04001326static void f2fs_free_inode(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001327{
Eric Biggers2c58d542019-04-10 13:21:15 -07001328 fscrypt_free_inode(inode);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001329 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1330}
1331
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001332static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1333{
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001334 percpu_counter_destroy(&sbi->alloc_valid_block_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001335 percpu_counter_destroy(&sbi->total_valid_inode_count);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001336}
1337
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001338static void destroy_device_list(struct f2fs_sb_info *sbi)
1339{
1340 int i;
1341
1342 for (i = 0; i < sbi->s_ndevs; i++) {
1343 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1344#ifdef CONFIG_BLK_DEV_ZONED
Damien Le Moal95175da2019-03-16 09:13:07 +09001345 kvfree(FDEV(i).blkz_seq);
Aravind Rameshde881df2020-07-16 18:26:56 +05301346 kfree(FDEV(i).zone_capacity_blocks);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001347#endif
1348 }
Jaegeuk Kim52225952018-12-13 18:38:33 -08001349 kvfree(sbi->devs);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001350}
1351
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001352static void f2fs_put_super(struct super_block *sb)
1353{
1354 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yua3981012017-06-14 17:39:46 +08001355 int i;
Chao Yucf5c7592017-10-04 09:08:37 +08001356 bool dropped;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001357
Li Guifu99c787c2020-07-24 09:38:11 +08001358 /* unregister procfs/sysfs entries in advance to avoid race case */
1359 f2fs_unregister_sysfs(sbi);
1360
Chao Yu0abd6752017-07-09 00:13:07 +08001361 f2fs_quota_off_umount(sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001362
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001363 /* prevent remaining shrinker jobs */
1364 mutex_lock(&sbi->umount_mutex);
1365
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -08001366 /*
Daeho Jeong261eeb92021-01-19 09:00:42 +09001367 * flush all issued checkpoints and stop checkpoint issue thread.
1368 * after then, all checkpoints should be done by each process context.
1369 */
1370 f2fs_stop_ckpt_thread(sbi);
1371
1372 /*
Jaegeuk Kim85dc2f22015-01-14 17:41:41 -08001373 * We don't need to do checkpoint when superblock is clean.
1374 * But, the previous checkpoint was not done by umount, it needs to do
1375 * clean checkpoint again.
1376 */
Daniel Rosenberg43549942018-08-20 19:21:43 -07001377 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1378 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -07001379 struct cp_control cpc = {
1380 .reason = CP_UMOUNT,
1381 };
Chao Yu4d57b862018-05-30 00:20:41 +08001382 f2fs_write_checkpoint(sbi, &cpc);
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -07001383 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001384
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001385 /* be sure to wait for any on-going discard commands */
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08001386 dropped = f2fs_issue_discard_timeout(sbi);
Jaegeuk Kim4e6a8d92016-12-29 14:07:53 -08001387
Chao Yu7d20c8a2018-09-04 03:52:17 +08001388 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1389 !sbi->discard_blks && !dropped) {
Chao Yu1f43e2a2017-04-28 13:56:08 +08001390 struct cp_control cpc = {
1391 .reason = CP_UMOUNT | CP_TRIMMED,
1392 };
Chao Yu4d57b862018-05-30 00:20:41 +08001393 f2fs_write_checkpoint(sbi, &cpc);
Chao Yu1f43e2a2017-04-28 13:56:08 +08001394 }
1395
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001396 /*
1397 * normally superblock is clean, so we need to release this.
1398 * In addition, EIO will skip do checkpoint, we need this as well.
1399 */
Chao Yu4d57b862018-05-30 00:20:41 +08001400 f2fs_release_ino_entry(sbi, true);
Jaegeuk Kim6f12ac22014-08-19 09:48:22 -07001401
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001402 f2fs_leave_shrinker(sbi);
1403 mutex_unlock(&sbi->umount_mutex);
1404
Jaegeuk Kim17c19122016-01-29 08:57:59 -08001405 /* our cp_error case, we can wait for any writeback page */
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001406 f2fs_flush_merged_writes(sbi);
Jaegeuk Kim17c19122016-01-29 08:57:59 -08001407
Sahitya Tummalabf22c3c2020-02-18 09:19:07 +05301408 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
Chao Yu50fa53e2018-08-02 23:03:19 +08001409
1410 f2fs_bug_on(sbi, sbi->fsync_node_num);
1411
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001412 iput(sbi->node_inode);
Jaegeuk Kim7c77bf72019-01-01 00:11:30 -08001413 sbi->node_inode = NULL;
1414
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001415 iput(sbi->meta_inode);
Jaegeuk Kim7c77bf72019-01-01 00:11:30 -08001416 sbi->meta_inode = NULL;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001417
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05301418 /*
1419 * iput() can update stat information, if f2fs_write_checkpoint()
1420 * above failed with error.
1421 */
1422 f2fs_destroy_stats(sbi);
1423
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001424 /* destroy f2fs internal modules */
Chao Yu4d57b862018-05-30 00:20:41 +08001425 f2fs_destroy_node_manager(sbi);
1426 f2fs_destroy_segment_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001427
Chao Yu4c8ff702019-11-01 18:07:14 +08001428 f2fs_destroy_post_read_wq(sbi);
1429
Jaegeuk Kim52225952018-12-13 18:38:33 -08001430 kvfree(sbi->ckpt);
Chao Yua3981012017-06-14 17:39:46 +08001431
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001432 sb->s_fs_info = NULL;
Keith Mok43b65732016-03-02 12:04:24 -08001433 if (sbi->s_chksum_driver)
1434 crypto_free_shash(sbi->s_chksum_driver);
Denis Efremov742532d2020-06-10 01:14:46 +03001435 kfree(sbi->raw_super);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001436
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001437 destroy_device_list(sbi);
Chao Yu31083032020-09-14 17:05:13 +08001438 f2fs_destroy_page_array_cache(sbi);
Chao Yua9991502020-02-25 18:17:10 +08001439 f2fs_destroy_xattr_caches(sbi);
Chao Yub6895e82017-02-27 18:43:12 +08001440 mempool_destroy(sbi->write_io_dummy);
Chao Yu4b2414d2017-08-08 10:54:31 +08001441#ifdef CONFIG_QUOTA
1442 for (i = 0; i < MAXQUOTAS; i++)
Wang Xiaojunba87a452020-06-17 20:30:12 +08001443 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001444#endif
Eric Biggersac4acb12020-09-16 21:11:35 -07001445 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001446 destroy_percpu_info(sbi);
Jaegeuk Kima912b542017-05-10 11:18:25 -07001447 for (i = 0; i < NR_PAGE_TYPE; i++)
Jaegeuk Kim52225952018-12-13 18:38:33 -08001448 kvfree(sbi->write_io[i]);
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07001449#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -07001450 utf8_unload(sb->s_encoding);
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07001451#endif
Denis Efremov742532d2020-06-10 01:14:46 +03001452 kfree(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001453}
1454
1455int f2fs_sync_fs(struct super_block *sb, int sync)
1456{
1457 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yuc34f42e2015-12-23 17:50:30 +08001458 int err = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001459
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001460 if (unlikely(f2fs_cp_error(sbi)))
1461 return 0;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001462 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1463 return 0;
Jaegeuk Kim1f227a32017-10-23 23:48:49 +02001464
Namjae Jeona2a4a7e2013-04-20 01:28:40 +09001465 trace_f2fs_sync_fs(sb, sync);
1466
Chao Yu4b2414d2017-08-08 10:54:31 +08001467 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1468 return -EAGAIN;
1469
Daeho Jeong261eeb92021-01-19 09:00:42 +09001470 if (sync)
1471 err = f2fs_issue_checkpoint(sbi);
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001472
Chao Yuc34f42e2015-12-23 17:50:30 +08001473 return err;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001474}
1475
Changman Leed6212a52013-01-29 18:30:07 +09001476static int f2fs_freeze(struct super_block *sb)
1477{
Jaegeuk Kim77888c12013-05-20 20:28:47 +09001478 if (f2fs_readonly(sb))
Changman Leed6212a52013-01-29 18:30:07 +09001479 return 0;
1480
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07001481 /* IO error happened before */
1482 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1483 return -EIO;
1484
1485 /* must be clean, since sync_filesystem() was already called */
1486 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1487 return -EINVAL;
Jaegeuk Kimd50dfc0c2021-02-08 13:42:21 -08001488
1489 /* ensure no checkpoint required */
1490 if (!llist_empty(&F2FS_SB(sb)->cprc_info.issue_list))
1491 return -EINVAL;
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07001492 return 0;
Changman Leed6212a52013-01-29 18:30:07 +09001493}
1494
1495static int f2fs_unfreeze(struct super_block *sb)
1496{
1497 return 0;
1498}
1499
Chao Yuddc34e32017-07-29 00:32:53 +08001500#ifdef CONFIG_QUOTA
1501static int f2fs_statfs_project(struct super_block *sb,
1502 kprojid_t projid, struct kstatfs *buf)
1503{
1504 struct kqid qid;
1505 struct dquot *dquot;
1506 u64 limit;
1507 u64 curblock;
1508
1509 qid = make_kqid_projid(projid);
1510 dquot = dqget(sb, qid);
1511 if (IS_ERR(dquot))
1512 return PTR_ERR(dquot);
Sheng Yong955ac6e2018-07-24 20:17:53 +08001513 spin_lock(&dquot->dq_dqb_lock);
Chao Yuddc34e32017-07-29 00:32:53 +08001514
Chengguang Xubf2cbd32020-01-04 22:20:04 +08001515 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1516 dquot->dq_dqb.dqb_bhardlimit);
Chengguang Xuacdf2172020-01-04 22:20:03 +08001517 if (limit)
1518 limit >>= sb->s_blocksize_bits;
Chengguang Xu909110c2019-11-25 11:20:36 +08001519
Chao Yuddc34e32017-07-29 00:32:53 +08001520 if (limit && buf->f_blocks > limit) {
Konstantin Khlebnikovbaaa7eb2020-05-11 09:15:18 +03001521 curblock = (dquot->dq_dqb.dqb_curspace +
1522 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
Chao Yuddc34e32017-07-29 00:32:53 +08001523 buf->f_blocks = limit;
1524 buf->f_bfree = buf->f_bavail =
1525 (buf->f_blocks > curblock) ?
1526 (buf->f_blocks - curblock) : 0;
1527 }
1528
Chengguang Xubf2cbd32020-01-04 22:20:04 +08001529 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1530 dquot->dq_dqb.dqb_ihardlimit);
Chengguang Xu909110c2019-11-25 11:20:36 +08001531
Chao Yuddc34e32017-07-29 00:32:53 +08001532 if (limit && buf->f_files > limit) {
1533 buf->f_files = limit;
1534 buf->f_ffree =
1535 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1536 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1537 }
1538
Sheng Yong955ac6e2018-07-24 20:17:53 +08001539 spin_unlock(&dquot->dq_dqb_lock);
Chao Yuddc34e32017-07-29 00:32:53 +08001540 dqput(dquot);
1541 return 0;
1542}
1543#endif
1544
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001545static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1546{
1547 struct super_block *sb = dentry->d_sb;
1548 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1549 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001550 block_t total_count, user_block_count, start_count;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001551 u64 avail_node_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001552
1553 total_count = le64_to_cpu(sbi->raw_super->block_count);
1554 user_block_count = sbi->user_block_count;
1555 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001556 buf->f_type = F2FS_SUPER_MAGIC;
1557 buf->f_bsize = sbi->blocksize;
1558
1559 buf->f_blocks = total_count - start_count;
Jaegeuk Kimf66c0272018-01-03 10:55:07 -08001560 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
Yunlong Song80d42142017-10-27 20:45:05 +08001561 sbi->current_reserved_blocks;
Chao Yuc9c8ed52019-05-05 11:40:46 +08001562
1563 spin_lock(&sbi->stat_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001564 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1565 buf->f_bfree = 0;
1566 else
1567 buf->f_bfree -= sbi->unusable_block_count;
Chao Yuc9c8ed52019-05-05 11:40:46 +08001568 spin_unlock(&sbi->stat_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001569
Chao Yu63189b72018-03-08 14:22:56 +08001570 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1571 buf->f_bavail = buf->f_bfree -
1572 F2FS_OPTION(sbi).root_reserved_blocks;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001573 else
1574 buf->f_bavail = 0;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001575
Chao Yu27cae0b2019-08-05 18:27:25 +08001576 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kim0cc091d2017-06-21 20:55:55 -07001577
1578 if (avail_node_count > user_block_count) {
1579 buf->f_files = user_block_count;
1580 buf->f_ffree = buf->f_bavail;
1581 } else {
1582 buf->f_files = avail_node_count;
1583 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1584 buf->f_bavail);
1585 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001586
Jaegeuk Kim5a20d332013-03-03 13:58:05 +09001587 buf->f_namelen = F2FS_NAME_LEN;
Al Viro6d1349c2020-09-18 16:45:50 -04001588 buf->f_fsid = u64_to_fsid(id);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001589
Chao Yuddc34e32017-07-29 00:32:53 +08001590#ifdef CONFIG_QUOTA
1591 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1592 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1593 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1594 }
1595#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001596 return 0;
1597}
1598
Chao Yu4b2414d2017-08-08 10:54:31 +08001599static inline void f2fs_show_quota_options(struct seq_file *seq,
1600 struct super_block *sb)
1601{
1602#ifdef CONFIG_QUOTA
1603 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1604
Chao Yu63189b72018-03-08 14:22:56 +08001605 if (F2FS_OPTION(sbi).s_jquota_fmt) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001606 char *fmtname = "";
1607
Chao Yu63189b72018-03-08 14:22:56 +08001608 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001609 case QFMT_VFS_OLD:
1610 fmtname = "vfsold";
1611 break;
1612 case QFMT_VFS_V0:
1613 fmtname = "vfsv0";
1614 break;
1615 case QFMT_VFS_V1:
1616 fmtname = "vfsv1";
1617 break;
1618 }
1619 seq_printf(seq, ",jqfmt=%s", fmtname);
1620 }
1621
Chao Yu63189b72018-03-08 14:22:56 +08001622 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1623 seq_show_option(seq, "usrjquota",
1624 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001625
Chao Yu63189b72018-03-08 14:22:56 +08001626 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1627 seq_show_option(seq, "grpjquota",
1628 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001629
Chao Yu63189b72018-03-08 14:22:56 +08001630 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1631 seq_show_option(seq, "prjjquota",
1632 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001633#endif
1634}
1635
Chao Yucd6ee732021-02-20 17:38:41 +08001636#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +08001637static inline void f2fs_show_compress_options(struct seq_file *seq,
1638 struct super_block *sb)
1639{
1640 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1641 char *algtype = "";
1642 int i;
1643
1644 if (!f2fs_sb_has_compression(sbi))
1645 return;
1646
1647 switch (F2FS_OPTION(sbi).compress_algorithm) {
1648 case COMPRESS_LZO:
1649 algtype = "lzo";
1650 break;
1651 case COMPRESS_LZ4:
1652 algtype = "lz4";
1653 break;
Chao Yu50cfa662020-03-03 17:46:02 +08001654 case COMPRESS_ZSTD:
1655 algtype = "zstd";
1656 break;
Chao Yu6d92b202020-04-08 19:56:32 +08001657 case COMPRESS_LZORLE:
1658 algtype = "lzo-rle";
1659 break;
Chao Yu4c8ff702019-11-01 18:07:14 +08001660 }
1661 seq_printf(seq, ",compress_algorithm=%s", algtype);
1662
Chao Yu3fde13f2021-01-22 17:46:43 +08001663 if (F2FS_OPTION(sbi).compress_level)
1664 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1665
Chao Yu4c8ff702019-11-01 18:07:14 +08001666 seq_printf(seq, ",compress_log_size=%u",
1667 F2FS_OPTION(sbi).compress_log_size);
1668
1669 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1670 seq_printf(seq, ",compress_extension=%s",
1671 F2FS_OPTION(sbi).extensions[i]);
1672 }
Chao Yub28f0472020-11-26 18:32:09 +08001673
1674 if (F2FS_OPTION(sbi).compress_chksum)
1675 seq_puts(seq, ",compress_chksum");
Daeho Jeong602a16d2020-12-01 13:08:02 +09001676
1677 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1678 seq_printf(seq, ",compress_mode=%s", "fs");
1679 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1680 seq_printf(seq, ",compress_mode=%s", "user");
Chao Yu4c8ff702019-11-01 18:07:14 +08001681}
Chao Yucd6ee732021-02-20 17:38:41 +08001682#endif
Chao Yu4c8ff702019-11-01 18:07:14 +08001683
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001684static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1685{
1686 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1687
Chao Yubbbc34f2020-02-14 17:44:13 +08001688 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1689 seq_printf(seq, ",background_gc=%s", "sync");
1690 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1691 seq_printf(seq, ",background_gc=%s", "on");
1692 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
Namjae Jeon696c0182013-06-16 09:48:48 +09001693 seq_printf(seq, ",background_gc=%s", "off");
Chao Yubbbc34f2020-02-14 17:44:13 +08001694
Chao Yu5911d2d2021-03-27 17:57:06 +08001695 if (test_opt(sbi, GC_MERGE))
1696 seq_puts(seq, ",gc_merge");
1697
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001698 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1699 seq_puts(seq, ",disable_roll_forward");
Chao Yua9117ec2020-02-14 17:45:11 +08001700 if (test_opt(sbi, NORECOVERY))
1701 seq_puts(seq, ",norecovery");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001702 if (test_opt(sbi, DISCARD))
1703 seq_puts(seq, ",discard");
Sahitya Tummala81621f92019-05-24 14:38:39 +05301704 else
1705 seq_puts(seq, ",nodiscard");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001706 if (test_opt(sbi, NOHEAP))
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001707 seq_puts(seq, ",no_heap");
1708 else
1709 seq_puts(seq, ",heap");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001710#ifdef CONFIG_F2FS_FS_XATTR
1711 if (test_opt(sbi, XATTR_USER))
1712 seq_puts(seq, ",user_xattr");
1713 else
1714 seq_puts(seq, ",nouser_xattr");
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001715 if (test_opt(sbi, INLINE_XATTR))
1716 seq_puts(seq, ",inline_xattr");
Chao Yu23cf7212017-02-15 10:34:45 +08001717 else
1718 seq_puts(seq, ",noinline_xattr");
Chao Yu6afc6622017-09-06 21:59:50 +08001719 if (test_opt(sbi, INLINE_XATTR_SIZE))
1720 seq_printf(seq, ",inline_xattr_size=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001721 F2FS_OPTION(sbi).inline_xattr_size);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001722#endif
1723#ifdef CONFIG_F2FS_FS_POSIX_ACL
1724 if (test_opt(sbi, POSIX_ACL))
1725 seq_puts(seq, ",acl");
1726 else
1727 seq_puts(seq, ",noacl");
1728#endif
1729 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
Alejandro Martinez Ruizaa435072013-01-25 19:08:59 +01001730 seq_puts(seq, ",disable_ext_identify");
Huajun Li8274de72013-11-10 23:13:17 +08001731 if (test_opt(sbi, INLINE_DATA))
1732 seq_puts(seq, ",inline_data");
Wanpeng Li75342792015-03-24 10:20:27 +08001733 else
1734 seq_puts(seq, ",noinline_data");
Chao Yu5efd3c62014-09-24 18:16:13 +08001735 if (test_opt(sbi, INLINE_DENTRY))
1736 seq_puts(seq, ",inline_dentry");
Chao Yu97c17942016-05-09 19:56:34 +08001737 else
1738 seq_puts(seq, ",noinline_dentry");
Gu Zhengb270ad62014-04-11 17:49:55 +08001739 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +09001740 seq_puts(seq, ",flush_merge");
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -07001741 if (test_opt(sbi, NOBARRIER))
1742 seq_puts(seq, ",nobarrier");
Jaegeuk Kimd5053a342014-10-30 22:47:03 -07001743 if (test_opt(sbi, FASTBOOT))
1744 seq_puts(seq, ",fastboot");
Chao Yu89672152015-02-05 17:55:51 +08001745 if (test_opt(sbi, EXTENT_CACHE))
1746 seq_puts(seq, ",extent_cache");
Jaegeuk Kim7daaea22015-06-25 17:43:04 -07001747 else
1748 seq_puts(seq, ",noextent_cache");
Chao Yu343f40f2015-12-16 13:12:16 +08001749 if (test_opt(sbi, DATA_FLUSH))
1750 seq_puts(seq, ",data_flush");
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001751
1752 seq_puts(seq, ",mode=");
Chao Yub0332a02020-02-14 17:44:12 +08001753 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001754 seq_puts(seq, "adaptive");
Chao Yub0332a02020-02-14 17:44:12 +08001755 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001756 seq_puts(seq, "lfs");
Chao Yu63189b72018-03-08 14:22:56 +08001757 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08001758 if (test_opt(sbi, RESERVE_ROOT))
Jaegeuk Kim7c2e5962018-01-04 21:36:09 -08001759 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001760 F2FS_OPTION(sbi).root_reserved_blocks,
1761 from_kuid_munged(&init_user_ns,
1762 F2FS_OPTION(sbi).s_resuid),
1763 from_kgid_munged(&init_user_ns,
1764 F2FS_OPTION(sbi).s_resgid));
Jaegeuk Kimec915382016-12-21 17:09:19 -08001765 if (F2FS_IO_SIZE_BITS(sbi))
Chengguang Xuc6b18672018-09-22 22:43:09 +08001766 seq_printf(seq, ",io_bits=%u",
1767 F2FS_OPTION(sbi).write_io_size_bits);
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001768#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yud4945002018-08-08 17:36:41 +08001769 if (test_opt(sbi, FAULT_INJECTION)) {
Chao Yu44529f82017-06-12 09:44:24 +08001770 seq_printf(seq, ",fault_injection=%u",
Chao Yu63189b72018-03-08 14:22:56 +08001771 F2FS_OPTION(sbi).fault_info.inject_rate);
Chao Yud4945002018-08-08 17:36:41 +08001772 seq_printf(seq, ",fault_type=%u",
1773 F2FS_OPTION(sbi).fault_info.inject_type);
1774 }
Kaixu Xia0cc0dec2017-01-27 09:35:37 +08001775#endif
Chao Yu0abd6752017-07-09 00:13:07 +08001776#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001777 if (test_opt(sbi, QUOTA))
1778 seq_puts(seq, ",quota");
Chao Yu0abd6752017-07-09 00:13:07 +08001779 if (test_opt(sbi, USRQUOTA))
1780 seq_puts(seq, ",usrquota");
1781 if (test_opt(sbi, GRPQUOTA))
1782 seq_puts(seq, ",grpquota");
Chao Yu5c571322017-07-26 00:01:41 +08001783 if (test_opt(sbi, PRJQUOTA))
1784 seq_puts(seq, ",prjquota");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001785#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08001786 f2fs_show_quota_options(seq, sbi->sb);
Chao Yu63189b72018-03-08 14:22:56 +08001787 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09001788 seq_printf(seq, ",whint_mode=%s", "user-based");
Chao Yu63189b72018-03-08 14:22:56 +08001789 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
Hyunchul Leef2e703f2018-01-31 11:36:58 +09001790 seq_printf(seq, ",whint_mode=%s", "fs-based");
Eric Biggersed318a62020-05-12 16:32:50 -07001791
1792 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001793
Satya Tangirala27aacd22020-07-02 01:56:06 +00001794 if (sbi->sb->s_flags & SB_INLINECRYPT)
1795 seq_puts(seq, ",inlinecrypt");
1796
Chao Yu63189b72018-03-08 14:22:56 +08001797 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
Jaegeuk Kim07939622018-02-18 08:50:49 -08001798 seq_printf(seq, ",alloc_mode=%s", "default");
Chao Yu63189b72018-03-08 14:22:56 +08001799 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
Jaegeuk Kim07939622018-02-18 08:50:49 -08001800 seq_printf(seq, ",alloc_mode=%s", "reuse");
Junling Zheng93cf93f2018-03-07 12:07:49 +08001801
Daniel Rosenberg43549942018-08-20 19:21:43 -07001802 if (test_opt(sbi, DISABLE_CHECKPOINT))
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -07001803 seq_printf(seq, ",checkpoint=disable:%u",
1804 F2FS_OPTION(sbi).unusable_cap);
Daeho Jeong261eeb92021-01-19 09:00:42 +09001805 if (test_opt(sbi, MERGE_CHECKPOINT))
1806 seq_puts(seq, ",checkpoint_merge");
1807 else
1808 seq_puts(seq, ",nocheckpoint_merge");
Chao Yu63189b72018-03-08 14:22:56 +08001809 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
Junling Zheng93cf93f2018-03-07 12:07:49 +08001810 seq_printf(seq, ",fsync_mode=%s", "posix");
Chao Yu63189b72018-03-08 14:22:56 +08001811 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
Junling Zheng93cf93f2018-03-07 12:07:49 +08001812 seq_printf(seq, ",fsync_mode=%s", "strict");
Sahitya Tummaladc132802018-07-02 11:37:40 +05301813 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1814 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
Chao Yu4c8ff702019-11-01 18:07:14 +08001815
Chao Yu1f0b0672020-07-29 21:21:36 +08001816#ifdef CONFIG_F2FS_FS_COMPRESSION
Chao Yu4c8ff702019-11-01 18:07:14 +08001817 f2fs_show_compress_options(seq, sbi->sb);
Chao Yu1f0b0672020-07-29 21:21:36 +08001818#endif
Chao Yu093749e2020-08-04 21:14:49 +08001819
1820 if (test_opt(sbi, ATGC))
1821 seq_puts(seq, ",atgc");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09001822 return 0;
1823}
1824
Yunlei He498c5e92015-05-07 18:11:37 +08001825static void default_options(struct f2fs_sb_info *sbi)
1826{
1827 /* init some FS parameters */
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07001828 if (f2fs_sb_has_readonly(sbi))
1829 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
1830 else
1831 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
1832
Chao Yu63189b72018-03-08 14:22:56 +08001833 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1834 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1835 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1836 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
Chao Yu0aa7e0f2018-06-06 23:55:02 +08001837 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1838 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
Chao Yu91faa532020-03-10 20:50:05 +08001839 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
Chao Yu4c8ff702019-11-01 18:07:14 +08001840 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
1841 F2FS_OPTION(sbi).compress_ext_cnt = 0;
Daeho Jeong602a16d2020-12-01 13:08:02 +09001842 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
Chao Yubbbc34f2020-02-14 17:44:13 +08001843 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
Yunlei He498c5e92015-05-07 18:11:37 +08001844
Satya Tangirala27aacd22020-07-02 01:56:06 +00001845 sbi->sb->s_flags &= ~SB_INLINECRYPT;
1846
Chao Yu39133a52017-02-08 17:39:44 +08001847 set_opt(sbi, INLINE_XATTR);
Yunlei He498c5e92015-05-07 18:11:37 +08001848 set_opt(sbi, INLINE_DATA);
Chao Yu97c17942016-05-09 19:56:34 +08001849 set_opt(sbi, INLINE_DENTRY);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07001850 set_opt(sbi, EXTENT_CACHE);
Jaegeuk Kim7a20b8a2017-03-24 20:41:45 -04001851 set_opt(sbi, NOHEAP);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001852 clear_opt(sbi, DISABLE_CHECKPOINT);
Jaegeuk Kimb5d15192021-04-01 17:25:20 -07001853 set_opt(sbi, MERGE_CHECKPOINT);
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -07001854 F2FS_OPTION(sbi).unusable_cap = 0;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001855 sbi->sb->s_flags |= SB_LAZYTIME;
Jaegeuk Kim69e9e422016-05-20 22:39:20 -07001856 set_opt(sbi, FLUSH_MERGE);
Chao Yu7d20c8a2018-09-04 03:52:17 +08001857 set_opt(sbi, DISCARD);
Chao Yu7beb01f2018-10-24 18:34:26 +08001858 if (f2fs_sb_has_blkzoned(sbi))
Chao Yub0332a02020-02-14 17:44:12 +08001859 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
Chao Yua39e53652018-07-05 14:24:11 +08001860 else
Chao Yub0332a02020-02-14 17:44:12 +08001861 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
Yunlei He498c5e92015-05-07 18:11:37 +08001862
1863#ifdef CONFIG_F2FS_FS_XATTR
1864 set_opt(sbi, XATTR_USER);
1865#endif
1866#ifdef CONFIG_F2FS_FS_POSIX_ACL
1867 set_opt(sbi, POSIX_ACL);
1868#endif
Chao Yu36dbd322016-09-26 19:45:05 +08001869
Chao Yud4945002018-08-08 17:36:41 +08001870 f2fs_build_fault_attr(sbi, 0, 0);
Yunlei He498c5e92015-05-07 18:11:37 +08001871}
1872
Jaegeuk Kimea676732017-10-06 09:14:28 -07001873#ifdef CONFIG_QUOTA
1874static int f2fs_enable_quotas(struct super_block *sb);
1875#endif
Daniel Rosenberg43549942018-08-20 19:21:43 -07001876
1877static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1878{
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001879 unsigned int s_flags = sbi->sb->s_flags;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001880 struct cp_control cpc;
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001881 int err = 0;
1882 int ret;
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -07001883 block_t unusable;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001884
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001885 if (s_flags & SB_RDONLY) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001886 f2fs_err(sbi, "checkpoint=disable on readonly fs");
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001887 return -EINVAL;
1888 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07001889 sbi->sb->s_flags |= SB_ACTIVE;
1890
Daniel Rosenberg43549942018-08-20 19:21:43 -07001891 f2fs_update_time(sbi, DISABLE_TIME);
1892
1893 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
Chao Yufb24fea2020-01-14 19:36:50 +08001894 down_write(&sbi->gc_lock);
Chao Yu7dede8862021-02-20 17:35:40 +08001895 err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001896 if (err == -ENODATA) {
1897 err = 0;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001898 break;
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001899 }
Jaegeuk Kim8f31b462018-12-17 17:08:26 -08001900 if (err && err != -EAGAIN)
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001901 break;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001902 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07001903
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001904 ret = sync_filesystem(sbi->sb);
1905 if (ret || err) {
Yi Zhuang5f029c02021-04-06 09:47:35 +08001906 err = ret ? ret : err;
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001907 goto restore_flag;
1908 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07001909
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -07001910 unusable = f2fs_get_unusable_blocks(sbi);
1911 if (f2fs_disable_cp_again(sbi, unusable)) {
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001912 err = -EAGAIN;
1913 goto restore_flag;
1914 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07001915
Chao Yufb24fea2020-01-14 19:36:50 +08001916 down_write(&sbi->gc_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001917 cpc.reason = CP_PAUSE;
1918 set_sbi_flag(sbi, SBI_CP_DISABLED);
Chao Yu896285a2019-04-26 17:57:54 +08001919 err = f2fs_write_checkpoint(sbi, &cpc);
1920 if (err)
1921 goto out_unlock;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001922
Chao Yuc9c8ed52019-05-05 11:40:46 +08001923 spin_lock(&sbi->stat_lock);
Daniel Rosenberg4d3aed72019-05-29 17:49:06 -07001924 sbi->unusable_block_count = unusable;
Chao Yuc9c8ed52019-05-05 11:40:46 +08001925 spin_unlock(&sbi->stat_lock);
1926
Chao Yu896285a2019-04-26 17:57:54 +08001927out_unlock:
Chao Yufb24fea2020-01-14 19:36:50 +08001928 up_write(&sbi->gc_lock);
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001929restore_flag:
Chao Yu7a88ddb2020-02-27 19:30:05 +08001930 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
Jaegeuk Kim812a9592019-01-22 14:04:33 -08001931 return err;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001932}
1933
1934static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1935{
Jaegeuk Kimb0ff4fe2021-01-26 17:00:42 -08001936 /* we should flush all the data to keep data consistency */
1937 sync_inodes_sb(sbi->sb);
1938
Chao Yufb24fea2020-01-14 19:36:50 +08001939 down_write(&sbi->gc_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001940 f2fs_dirty_to_prefree(sbi);
1941
1942 clear_sbi_flag(sbi, SBI_CP_DISABLED);
1943 set_sbi_flag(sbi, SBI_IS_DIRTY);
Chao Yufb24fea2020-01-14 19:36:50 +08001944 up_write(&sbi->gc_lock);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001945
1946 f2fs_sync_fs(sbi->sb, 1);
1947}
1948
Namjae Jeon696c0182013-06-16 09:48:48 +09001949static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1950{
1951 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1952 struct f2fs_mount_info org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001953 unsigned long old_sb_flags;
Chao Yu63189b72018-03-08 14:22:56 +08001954 int err;
Chao Yu3fd97352021-03-17 17:56:04 +08001955 bool need_restart_gc = false, need_stop_gc = false;
1956 bool need_restart_ckpt = false, need_stop_ckpt = false;
1957 bool need_restart_flush = false, need_stop_flush = false;
Chao Yu9cd81ce2015-09-18 16:55:26 +08001958 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001959 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
Chao Yu1f78adf2019-07-12 16:57:00 +08001960 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
Chao Yu093749e2020-08-04 21:14:49 +08001961 bool no_atgc = !test_opt(sbi, ATGC);
Daniel Rosenberg43549942018-08-20 19:21:43 -07001962 bool checkpoint_changed;
Chao Yu4b2414d2017-08-08 10:54:31 +08001963#ifdef CONFIG_QUOTA
Chao Yu4b2414d2017-08-08 10:54:31 +08001964 int i, j;
1965#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09001966
1967 /*
1968 * Save the old mount options in case we
1969 * need to restore them.
1970 */
1971 org_mount_opt = sbi->mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08001972 old_sb_flags = sb->s_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09001973
Chao Yu4b2414d2017-08-08 10:54:31 +08001974#ifdef CONFIG_QUOTA
Chao Yu63189b72018-03-08 14:22:56 +08001975 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
Chao Yu4b2414d2017-08-08 10:54:31 +08001976 for (i = 0; i < MAXQUOTAS; i++) {
Chao Yu63189b72018-03-08 14:22:56 +08001977 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1978 org_mount_opt.s_qf_names[i] =
1979 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1980 GFP_KERNEL);
1981 if (!org_mount_opt.s_qf_names[i]) {
Chao Yu4b2414d2017-08-08 10:54:31 +08001982 for (j = 0; j < i; j++)
Wang Xiaojunba87a452020-06-17 20:30:12 +08001983 kfree(org_mount_opt.s_qf_names[j]);
Chao Yu4b2414d2017-08-08 10:54:31 +08001984 return -ENOMEM;
1985 }
1986 } else {
Chao Yu63189b72018-03-08 14:22:56 +08001987 org_mount_opt.s_qf_names[i] = NULL;
Chao Yu4b2414d2017-08-08 10:54:31 +08001988 }
1989 }
1990#endif
1991
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001992 /* recover superblocks we couldn't write due to previous RO mount */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001993 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001994 err = f2fs_commit_super(sbi, false);
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001995 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
1996 err);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001997 if (!err)
1998 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1999 }
2000
Yunlei He498c5e92015-05-07 18:11:37 +08002001 default_options(sbi);
Chao Yu26666c82014-09-15 18:04:44 +08002002
Namjae Jeon696c0182013-06-16 09:48:48 +09002003 /* parse mount options */
Eric Biggersed318a62020-05-12 16:32:50 -07002004 err = parse_options(sb, data, true);
Namjae Jeon696c0182013-06-16 09:48:48 +09002005 if (err)
2006 goto restore_opts;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002007 checkpoint_changed =
2008 disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
Namjae Jeon696c0182013-06-16 09:48:48 +09002009
2010 /*
2011 * Previous and new state of filesystem is RO,
Gu Zheng876dc592014-04-11 17:50:00 +08002012 * so skip checking GC and FLUSH_MERGE conditions.
Namjae Jeon696c0182013-06-16 09:48:48 +09002013 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002014 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
Namjae Jeon696c0182013-06-16 09:48:48 +09002015 goto skip;
2016
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07002017 if (f2fs_sb_has_readonly(sbi) && !(*flags & SB_RDONLY)) {
2018 err = -EROFS;
2019 goto restore_opts;
2020 }
2021
Jaegeuk Kimea676732017-10-06 09:14:28 -07002022#ifdef CONFIG_QUOTA
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002023 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08002024 err = dquot_suspend(sb, -1);
2025 if (err < 0)
2026 goto restore_opts;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002027 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
Chao Yu0abd6752017-07-09 00:13:07 +08002028 /* dquot_resume needs RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002029 sb->s_flags &= ~SB_RDONLY;
Jaegeuk Kimea676732017-10-06 09:14:28 -07002030 if (sb_any_quota_suspended(sb)) {
2031 dquot_resume(sb, -1);
Chao Yu7beb01f2018-10-24 18:34:26 +08002032 } else if (f2fs_sb_has_quota_ino(sbi)) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07002033 err = f2fs_enable_quotas(sb);
2034 if (err)
2035 goto restore_opts;
2036 }
Chao Yu0abd6752017-07-09 00:13:07 +08002037 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07002038#endif
Chao Yu093749e2020-08-04 21:14:49 +08002039 /* disallow enable atgc dynamically */
2040 if (no_atgc == !!test_opt(sbi, ATGC)) {
2041 err = -EINVAL;
2042 f2fs_warn(sbi, "switch atgc option is not allowed");
2043 goto restore_opts;
2044 }
2045
Chao Yu9cd81ce2015-09-18 16:55:26 +08002046 /* disallow enable/disable extent_cache dynamically */
2047 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
2048 err = -EINVAL;
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002049 f2fs_warn(sbi, "switch extent_cache option is not allowed");
Chao Yu9cd81ce2015-09-18 16:55:26 +08002050 goto restore_opts;
2051 }
2052
Chao Yu1f78adf2019-07-12 16:57:00 +08002053 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
2054 err = -EINVAL;
2055 f2fs_warn(sbi, "switch io_bits option is not allowed");
2056 goto restore_opts;
2057 }
2058
Daniel Rosenberg43549942018-08-20 19:21:43 -07002059 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2060 err = -EINVAL;
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002061 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
Daniel Rosenberg43549942018-08-20 19:21:43 -07002062 goto restore_opts;
2063 }
2064
Namjae Jeon696c0182013-06-16 09:48:48 +09002065 /*
2066 * We stop the GC thread if FS is mounted as RO
2067 * or if background_gc = off is passed in mount
2068 * option. Also sync the filesystem.
2069 */
Chao Yubbbc34f2020-02-14 17:44:13 +08002070 if ((*flags & SB_RDONLY) ||
Chao Yu5911d2d2021-03-27 17:57:06 +08002071 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2072 !test_opt(sbi, GC_MERGE))) {
Namjae Jeon696c0182013-06-16 09:48:48 +09002073 if (sbi->gc_thread) {
Chao Yu4d57b862018-05-30 00:20:41 +08002074 f2fs_stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08002075 need_restart_gc = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09002076 }
Chao Yuaba291b2014-11-18 11:17:20 +08002077 } else if (!sbi->gc_thread) {
Chao Yu4d57b862018-05-30 00:20:41 +08002078 err = f2fs_start_gc_thread(sbi);
Namjae Jeon696c0182013-06-16 09:48:48 +09002079 if (err)
2080 goto restore_opts;
Gu Zheng876dc592014-04-11 17:50:00 +08002081 need_stop_gc = true;
2082 }
2083
Chao Yu63189b72018-03-08 14:22:56 +08002084 if (*flags & SB_RDONLY ||
2085 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
Jaegeuk Kimfaa0e552016-03-24 10:29:39 -07002086 sync_inodes_sb(sb);
2087
2088 set_sbi_flag(sbi, SBI_IS_DIRTY);
2089 set_sbi_flag(sbi, SBI_IS_CLOSE);
2090 f2fs_sync_fs(sb, 1);
2091 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2092 }
2093
Chao Yu3f7070b2021-03-17 17:56:03 +08002094 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2095 !test_opt(sbi, MERGE_CHECKPOINT)) {
2096 f2fs_stop_ckpt_thread(sbi);
Chao Yu3fd97352021-03-17 17:56:04 +08002097 need_restart_ckpt = true;
Chao Yu3f7070b2021-03-17 17:56:03 +08002098 } else {
Daeho Jeong261eeb92021-01-19 09:00:42 +09002099 err = f2fs_start_ckpt_thread(sbi);
2100 if (err) {
2101 f2fs_err(sbi,
2102 "Failed to start F2FS issue_checkpoint_thread (%d)",
2103 err);
2104 goto restore_gc;
2105 }
Chao Yu3fd97352021-03-17 17:56:04 +08002106 need_stop_ckpt = true;
Daeho Jeong261eeb92021-01-19 09:00:42 +09002107 }
2108
Gu Zheng876dc592014-04-11 17:50:00 +08002109 /*
2110 * We stop issue flush thread if FS is mounted as RO
2111 * or if flush_merge is not passed in mount option.
2112 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002113 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08002114 clear_opt(sbi, FLUSH_MERGE);
Chao Yu4d57b862018-05-30 00:20:41 +08002115 f2fs_destroy_flush_cmd_control(sbi, false);
Chao Yu3fd97352021-03-17 17:56:04 +08002116 need_restart_flush = true;
Jaegeuk Kim5eba8c52016-12-07 16:23:32 -08002117 } else {
Chao Yu4d57b862018-05-30 00:20:41 +08002118 err = f2fs_create_flush_cmd_control(sbi);
Gu Zheng2163d192014-04-27 14:21:33 +08002119 if (err)
Chao Yu3fd97352021-03-17 17:56:04 +08002120 goto restore_ckpt;
2121 need_stop_flush = true;
Namjae Jeon696c0182013-06-16 09:48:48 +09002122 }
Chao Yu3fd97352021-03-17 17:56:04 +08002123
2124 if (checkpoint_changed) {
2125 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2126 err = f2fs_disable_checkpoint(sbi);
2127 if (err)
2128 goto restore_flush;
2129 } else {
2130 f2fs_enable_checkpoint(sbi);
2131 }
2132 }
2133
Namjae Jeon696c0182013-06-16 09:48:48 +09002134skip:
Chao Yu4b2414d2017-08-08 10:54:31 +08002135#ifdef CONFIG_QUOTA
2136 /* Release old quota file names */
2137 for (i = 0; i < MAXQUOTAS; i++)
Wang Xiaojunba87a452020-06-17 20:30:12 +08002138 kfree(org_mount_opt.s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08002139#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09002140 /* Update the POSIXACL Flag */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002141 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2142 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002143
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08002144 limit_reserve_root(sbi);
Jaegeuk Kim1ae18f72020-05-15 17:20:50 -07002145 adjust_unusable_cap_perc(sbi);
Jaegeuk Kim095680f2018-09-28 00:24:39 -07002146 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
Namjae Jeon696c0182013-06-16 09:48:48 +09002147 return 0;
Chao Yu3fd97352021-03-17 17:56:04 +08002148restore_flush:
2149 if (need_restart_flush) {
2150 if (f2fs_create_flush_cmd_control(sbi))
2151 f2fs_warn(sbi, "background flush thread has stopped");
2152 } else if (need_stop_flush) {
2153 clear_opt(sbi, FLUSH_MERGE);
2154 f2fs_destroy_flush_cmd_control(sbi, false);
2155 }
2156restore_ckpt:
2157 if (need_restart_ckpt) {
2158 if (f2fs_start_ckpt_thread(sbi))
2159 f2fs_warn(sbi, "background ckpt thread has stopped");
2160 } else if (need_stop_ckpt) {
2161 f2fs_stop_ckpt_thread(sbi);
2162 }
Gu Zheng876dc592014-04-11 17:50:00 +08002163restore_gc:
2164 if (need_restart_gc) {
Chao Yu4d57b862018-05-30 00:20:41 +08002165 if (f2fs_start_gc_thread(sbi))
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002166 f2fs_warn(sbi, "background gc thread has stopped");
Gu Zheng876dc592014-04-11 17:50:00 +08002167 } else if (need_stop_gc) {
Chao Yu4d57b862018-05-30 00:20:41 +08002168 f2fs_stop_gc_thread(sbi);
Gu Zheng876dc592014-04-11 17:50:00 +08002169 }
Namjae Jeon696c0182013-06-16 09:48:48 +09002170restore_opts:
Chao Yu4b2414d2017-08-08 10:54:31 +08002171#ifdef CONFIG_QUOTA
Chao Yu63189b72018-03-08 14:22:56 +08002172 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
Chao Yu4b2414d2017-08-08 10:54:31 +08002173 for (i = 0; i < MAXQUOTAS; i++) {
Wang Xiaojunba87a452020-06-17 20:30:12 +08002174 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
Chao Yu63189b72018-03-08 14:22:56 +08002175 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
Chao Yu4b2414d2017-08-08 10:54:31 +08002176 }
2177#endif
Namjae Jeon696c0182013-06-16 09:48:48 +09002178 sbi->mount_opt = org_mount_opt;
Chao Yu0abd6752017-07-09 00:13:07 +08002179 sb->s_flags = old_sb_flags;
Namjae Jeon696c0182013-06-16 09:48:48 +09002180 return err;
2181}
2182
Chao Yu0abd6752017-07-09 00:13:07 +08002183#ifdef CONFIG_QUOTA
2184/* Read data from quotafile */
2185static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2186 size_t len, loff_t off)
2187{
2188 struct inode *inode = sb_dqopt(sb)->files[type];
2189 struct address_space *mapping = inode->i_mapping;
2190 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2191 int offset = off & (sb->s_blocksize - 1);
2192 int tocopy;
2193 size_t toread;
2194 loff_t i_size = i_size_read(inode);
2195 struct page *page;
2196 char *kaddr;
2197
2198 if (off > i_size)
2199 return 0;
2200
2201 if (off + len > i_size)
2202 len = i_size - off;
2203 toread = len;
2204 while (toread > 0) {
2205 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2206repeat:
Ritesh Harjani02117b82018-03-16 18:53:53 +05302207 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002208 if (IS_ERR(page)) {
2209 if (PTR_ERR(page) == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +08002210 congestion_wait(BLK_RW_ASYNC,
2211 DEFAULT_IO_TIMEOUT);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002212 goto repeat;
2213 }
Chao Yuaf033b22018-09-20 20:05:00 +08002214 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Chao Yu0abd6752017-07-09 00:13:07 +08002215 return PTR_ERR(page);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002216 }
Chao Yu0abd6752017-07-09 00:13:07 +08002217
2218 lock_page(page);
2219
2220 if (unlikely(page->mapping != mapping)) {
2221 f2fs_put_page(page, 1);
2222 goto repeat;
2223 }
2224 if (unlikely(!PageUptodate(page))) {
2225 f2fs_put_page(page, 1);
Chao Yuaf033b22018-09-20 20:05:00 +08002226 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Chao Yu0abd6752017-07-09 00:13:07 +08002227 return -EIO;
2228 }
2229
2230 kaddr = kmap_atomic(page);
2231 memcpy(data, kaddr + offset, tocopy);
2232 kunmap_atomic(kaddr);
2233 f2fs_put_page(page, 1);
2234
2235 offset = 0;
2236 toread -= tocopy;
2237 data += tocopy;
2238 blkidx++;
2239 }
2240 return len;
2241}
2242
2243/* Write to quotafile */
2244static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2245 const char *data, size_t len, loff_t off)
2246{
2247 struct inode *inode = sb_dqopt(sb)->files[type];
2248 struct address_space *mapping = inode->i_mapping;
2249 const struct address_space_operations *a_ops = mapping->a_ops;
2250 int offset = off & (sb->s_blocksize - 1);
2251 size_t towrite = len;
2252 struct page *page;
Chao Yu62f63ee2020-03-19 19:58:00 +08002253 void *fsdata = NULL;
Chao Yu0abd6752017-07-09 00:13:07 +08002254 char *kaddr;
2255 int err = 0;
2256 int tocopy;
2257
2258 while (towrite > 0) {
2259 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2260 towrite);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002261retry:
Chao Yu0abd6752017-07-09 00:13:07 +08002262 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
Chao Yu62f63ee2020-03-19 19:58:00 +08002263 &page, &fsdata);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002264 if (unlikely(err)) {
2265 if (err == -ENOMEM) {
Chao Yu5df7731f2020-02-17 17:45:44 +08002266 congestion_wait(BLK_RW_ASYNC,
2267 DEFAULT_IO_TIMEOUT);
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002268 goto retry;
2269 }
Chao Yuaf033b22018-09-20 20:05:00 +08002270 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Chao Yu0abd6752017-07-09 00:13:07 +08002271 break;
Jaegeuk Kim4e46a022017-10-19 09:43:56 -07002272 }
Chao Yu0abd6752017-07-09 00:13:07 +08002273
2274 kaddr = kmap_atomic(page);
2275 memcpy(kaddr + offset, data, tocopy);
2276 kunmap_atomic(kaddr);
2277 flush_dcache_page(page);
2278
2279 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
Chao Yu62f63ee2020-03-19 19:58:00 +08002280 page, fsdata);
Chao Yu0abd6752017-07-09 00:13:07 +08002281 offset = 0;
2282 towrite -= tocopy;
2283 off += tocopy;
2284 data += tocopy;
2285 cond_resched();
2286 }
2287
2288 if (len == towrite)
Jaegeuk Kim6e5b5d42017-10-19 12:07:11 -07002289 return err;
Chao Yu0abd6752017-07-09 00:13:07 +08002290 inode->i_mtime = inode->i_ctime = current_time(inode);
2291 f2fs_mark_inode_dirty_sync(inode, false);
2292 return len - towrite;
2293}
2294
2295static struct dquot **f2fs_get_dquots(struct inode *inode)
2296{
2297 return F2FS_I(inode)->i_dquot;
2298}
2299
2300static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2301{
2302 return &F2FS_I(inode)->i_reserved_quota;
2303}
2304
Chao Yu4b2414d2017-08-08 10:54:31 +08002305static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2306{
Chao Yuaf033b22018-09-20 20:05:00 +08002307 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002308 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
Chao Yuaf033b22018-09-20 20:05:00 +08002309 return 0;
2310 }
2311
Chao Yu63189b72018-03-08 14:22:56 +08002312 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2313 F2FS_OPTION(sbi).s_jquota_fmt, type);
Chao Yu4b2414d2017-08-08 10:54:31 +08002314}
2315
Jaegeuk Kimea676732017-10-06 09:14:28 -07002316int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
Chao Yu4b2414d2017-08-08 10:54:31 +08002317{
Jaegeuk Kimea676732017-10-06 09:14:28 -07002318 int enabled = 0;
2319 int i, err;
2320
Chao Yu7beb01f2018-10-24 18:34:26 +08002321 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07002322 err = f2fs_enable_quotas(sbi->sb);
2323 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002324 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002325 return 0;
2326 }
2327 return 1;
2328 }
Chao Yu4b2414d2017-08-08 10:54:31 +08002329
2330 for (i = 0; i < MAXQUOTAS; i++) {
Chao Yu63189b72018-03-08 14:22:56 +08002331 if (F2FS_OPTION(sbi).s_qf_names[i]) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07002332 err = f2fs_quota_on_mount(sbi, i);
2333 if (!err) {
2334 enabled = 1;
2335 continue;
2336 }
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002337 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2338 err, i);
Chao Yu4b2414d2017-08-08 10:54:31 +08002339 }
2340 }
Jaegeuk Kimea676732017-10-06 09:14:28 -07002341 return enabled;
2342}
2343
2344static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2345 unsigned int flags)
2346{
2347 struct inode *qf_inode;
2348 unsigned long qf_inum;
2349 int err;
2350
Chao Yu7beb01f2018-10-24 18:34:26 +08002351 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
Jaegeuk Kimea676732017-10-06 09:14:28 -07002352
2353 qf_inum = f2fs_qf_ino(sb, type);
2354 if (!qf_inum)
2355 return -EPERM;
2356
2357 qf_inode = f2fs_iget(sb, qf_inum);
2358 if (IS_ERR(qf_inode)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002359 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002360 return PTR_ERR(qf_inode);
2361 }
2362
2363 /* Don't account quota for quota files to avoid recursion */
2364 qf_inode->i_flags |= S_NOQUOTA;
Jan Kara7212b952019-11-01 18:55:38 +01002365 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002366 iput(qf_inode);
2367 return err;
2368}
2369
2370static int f2fs_enable_quotas(struct super_block *sb)
2371{
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002372 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002373 int type, err = 0;
2374 unsigned long qf_inum;
2375 bool quota_mopt[MAXQUOTAS] = {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002376 test_opt(sbi, USRQUOTA),
2377 test_opt(sbi, GRPQUOTA),
2378 test_opt(sbi, PRJQUOTA),
Jaegeuk Kimea676732017-10-06 09:14:28 -07002379 };
2380
Chao Yuaf033b22018-09-20 20:05:00 +08002381 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002382 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
Chao Yuaf033b22018-09-20 20:05:00 +08002383 return 0;
2384 }
2385
2386 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2387
Jaegeuk Kimea676732017-10-06 09:14:28 -07002388 for (type = 0; type < MAXQUOTAS; type++) {
2389 qf_inum = f2fs_qf_ino(sb, type);
2390 if (qf_inum) {
2391 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2392 DQUOT_USAGE_ENABLED |
2393 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2394 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002395 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2396 type, err);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002397 for (type--; type >= 0; type--)
2398 dquot_quota_off(sb, type);
Chao Yuaf033b22018-09-20 20:05:00 +08002399 set_sbi_flag(F2FS_SB(sb),
2400 SBI_QUOTA_NEED_REPAIR);
Jaegeuk Kimea676732017-10-06 09:14:28 -07002401 return err;
2402 }
2403 }
2404 }
2405 return 0;
Chao Yu4b2414d2017-08-08 10:54:31 +08002406}
2407
Chao Yuaf033b22018-09-20 20:05:00 +08002408int f2fs_quota_sync(struct super_block *sb, int type)
Chao Yu0abd6752017-07-09 00:13:07 +08002409{
Chao Yuaf033b22018-09-20 20:05:00 +08002410 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Chao Yu0abd6752017-07-09 00:13:07 +08002411 struct quota_info *dqopt = sb_dqopt(sb);
2412 int cnt;
2413 int ret;
2414
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002415 /*
2416 * do_quotactl
2417 * f2fs_quota_sync
2418 * down_read(quota_sem)
2419 * dquot_writeback_dquots()
2420 * f2fs_dquot_commit
2421 * block_operation
2422 * down_read(quota_sem)
2423 */
2424 f2fs_lock_op(sbi);
2425
2426 down_read(&sbi->quota_sem);
Chao Yu0abd6752017-07-09 00:13:07 +08002427 ret = dquot_writeback_dquots(sb, type);
2428 if (ret)
Chao Yuaf033b22018-09-20 20:05:00 +08002429 goto out;
Chao Yu0abd6752017-07-09 00:13:07 +08002430
2431 /*
2432 * Now when everything is written we can discard the pagecache so
2433 * that userspace sees the changes.
2434 */
2435 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Chao Yuaf033b22018-09-20 20:05:00 +08002436 struct address_space *mapping;
2437
Chao Yu0abd6752017-07-09 00:13:07 +08002438 if (type != -1 && cnt != type)
2439 continue;
2440 if (!sb_has_quota_active(sb, cnt))
2441 continue;
2442
Chao Yuaf033b22018-09-20 20:05:00 +08002443 mapping = dqopt->files[cnt]->i_mapping;
2444
2445 ret = filemap_fdatawrite(mapping);
Chao Yu0abd6752017-07-09 00:13:07 +08002446 if (ret)
Chao Yuaf033b22018-09-20 20:05:00 +08002447 goto out;
2448
2449 /* if we are using journalled quota */
2450 if (is_journalled_quota(sbi))
2451 continue;
2452
2453 ret = filemap_fdatawait(mapping);
2454 if (ret)
2455 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Chao Yu0abd6752017-07-09 00:13:07 +08002456
2457 inode_lock(dqopt->files[cnt]);
2458 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2459 inode_unlock(dqopt->files[cnt]);
2460 }
Chao Yuaf033b22018-09-20 20:05:00 +08002461out:
2462 if (ret)
2463 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002464 up_read(&sbi->quota_sem);
2465 f2fs_unlock_op(sbi);
Chao Yuaf033b22018-09-20 20:05:00 +08002466 return ret;
Chao Yu0abd6752017-07-09 00:13:07 +08002467}
2468
2469static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2470 const struct path *path)
2471{
2472 struct inode *inode;
2473 int err;
2474
Chao Yufe973b02019-07-25 17:33:37 +08002475 /* if quota sysfile exists, deny enabling quota with specific file */
2476 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2477 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2478 return -EBUSY;
2479 }
2480
Chao Yu9a20d392017-08-07 16:37:59 +08002481 err = f2fs_quota_sync(sb, type);
Chao Yu0abd6752017-07-09 00:13:07 +08002482 if (err)
2483 return err;
2484
2485 err = dquot_quota_on(sb, type, format_id, path);
2486 if (err)
2487 return err;
2488
2489 inode = d_inode(path->dentry);
2490
2491 inode_lock(inode);
Chao Yu59c84402018-04-03 15:08:17 +08002492 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
Chao Yu9149a5e2018-10-07 19:06:15 +08002493 f2fs_set_inode_flags(inode);
Chao Yu0abd6752017-07-09 00:13:07 +08002494 inode_unlock(inode);
2495 f2fs_mark_inode_dirty_sync(inode, false);
2496
2497 return 0;
2498}
2499
Chao Yufe973b02019-07-25 17:33:37 +08002500static int __f2fs_quota_off(struct super_block *sb, int type)
Chao Yu0abd6752017-07-09 00:13:07 +08002501{
2502 struct inode *inode = sb_dqopt(sb)->files[type];
2503 int err;
2504
2505 if (!inode || !igrab(inode))
2506 return dquot_quota_off(sb, type);
2507
Yunlei Hecda9cc52018-06-26 13:12:43 +08002508 err = f2fs_quota_sync(sb, type);
2509 if (err)
2510 goto out_put;
Chao Yu0abd6752017-07-09 00:13:07 +08002511
2512 err = dquot_quota_off(sb, type);
Chao Yu7beb01f2018-10-24 18:34:26 +08002513 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
Chao Yu0abd6752017-07-09 00:13:07 +08002514 goto out_put;
2515
2516 inode_lock(inode);
Chao Yu59c84402018-04-03 15:08:17 +08002517 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
Chao Yu9149a5e2018-10-07 19:06:15 +08002518 f2fs_set_inode_flags(inode);
Chao Yu0abd6752017-07-09 00:13:07 +08002519 inode_unlock(inode);
2520 f2fs_mark_inode_dirty_sync(inode, false);
2521out_put:
2522 iput(inode);
2523 return err;
2524}
2525
Chao Yufe973b02019-07-25 17:33:37 +08002526static int f2fs_quota_off(struct super_block *sb, int type)
2527{
2528 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2529 int err;
2530
2531 err = __f2fs_quota_off(sb, type);
2532
2533 /*
2534 * quotactl can shutdown journalled quota, result in inconsistence
2535 * between quota record and fs data by following updates, tag the
2536 * flag to let fsck be aware of it.
2537 */
2538 if (is_journalled_quota(sbi))
2539 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2540 return err;
2541}
2542
Chao Yu4b2414d2017-08-08 10:54:31 +08002543void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08002544{
2545 int type;
Yunlei Hecda9cc52018-06-26 13:12:43 +08002546 int err;
Chao Yu0abd6752017-07-09 00:13:07 +08002547
Yunlei Hecda9cc52018-06-26 13:12:43 +08002548 for (type = 0; type < MAXQUOTAS; type++) {
Chao Yufe973b02019-07-25 17:33:37 +08002549 err = __f2fs_quota_off(sb, type);
Yunlei Hecda9cc52018-06-26 13:12:43 +08002550 if (err) {
2551 int ret = dquot_quota_off(sb, type);
2552
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002553 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2554 type, err, ret);
Chao Yuaf033b22018-09-20 20:05:00 +08002555 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Yunlei Hecda9cc52018-06-26 13:12:43 +08002556 }
2557 }
Jaegeuk Kim0e0667b2019-01-27 17:59:53 -08002558 /*
2559 * In case of checkpoint=disable, we must flush quota blocks.
2560 * This can cause NULL exception for node_inode in end_io, since
2561 * put_super already dropped it.
2562 */
2563 sync_filesystem(sb);
Chao Yu0abd6752017-07-09 00:13:07 +08002564}
2565
Sheng Yong26b5a072018-10-12 18:49:26 +08002566static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2567{
2568 struct quota_info *dqopt = sb_dqopt(sb);
2569 int type;
2570
2571 for (type = 0; type < MAXQUOTAS; type++) {
2572 if (!dqopt->files[type])
2573 continue;
2574 f2fs_inode_synced(dqopt->files[type]);
2575 }
2576}
2577
Chao Yuaf033b22018-09-20 20:05:00 +08002578static int f2fs_dquot_commit(struct dquot *dquot)
2579{
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002580 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
Chao Yuaf033b22018-09-20 20:05:00 +08002581 int ret;
2582
Jaegeuk Kim2c4e0c52019-12-03 17:31:00 -08002583 down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
Chao Yuaf033b22018-09-20 20:05:00 +08002584 ret = dquot_commit(dquot);
2585 if (ret < 0)
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002586 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2587 up_read(&sbi->quota_sem);
Chao Yuaf033b22018-09-20 20:05:00 +08002588 return ret;
2589}
2590
2591static int f2fs_dquot_acquire(struct dquot *dquot)
2592{
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002593 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
Chao Yuaf033b22018-09-20 20:05:00 +08002594 int ret;
2595
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002596 down_read(&sbi->quota_sem);
Chao Yuaf033b22018-09-20 20:05:00 +08002597 ret = dquot_acquire(dquot);
2598 if (ret < 0)
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002599 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2600 up_read(&sbi->quota_sem);
Chao Yuaf033b22018-09-20 20:05:00 +08002601 return ret;
2602}
2603
2604static int f2fs_dquot_release(struct dquot *dquot)
2605{
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002606 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
Jaegeuk Kim2c4e0c52019-12-03 17:31:00 -08002607 int ret = dquot_release(dquot);
Chao Yuaf033b22018-09-20 20:05:00 +08002608
Chao Yuaf033b22018-09-20 20:05:00 +08002609 if (ret < 0)
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002610 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
Chao Yuaf033b22018-09-20 20:05:00 +08002611 return ret;
2612}
2613
2614static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2615{
2616 struct super_block *sb = dquot->dq_sb;
2617 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kim2c4e0c52019-12-03 17:31:00 -08002618 int ret = dquot_mark_dquot_dirty(dquot);
Chao Yuaf033b22018-09-20 20:05:00 +08002619
2620 /* if we are using journalled quota */
2621 if (is_journalled_quota(sbi))
2622 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2623
2624 return ret;
2625}
2626
2627static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2628{
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002629 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Jaegeuk Kim2c4e0c52019-12-03 17:31:00 -08002630 int ret = dquot_commit_info(sb, type);
Chao Yuaf033b22018-09-20 20:05:00 +08002631
Chao Yuaf033b22018-09-20 20:05:00 +08002632 if (ret < 0)
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07002633 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
Chao Yuaf033b22018-09-20 20:05:00 +08002634 return ret;
2635}
Sheng Yong26b5a072018-10-12 18:49:26 +08002636
Wei Yongjun94b1e102018-01-05 09:41:20 +00002637static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
Chao Yu5c571322017-07-26 00:01:41 +08002638{
2639 *projid = F2FS_I(inode)->i_projid;
2640 return 0;
2641}
2642
Chao Yu0abd6752017-07-09 00:13:07 +08002643static const struct dquot_operations f2fs_quota_operations = {
2644 .get_reserved_space = f2fs_get_reserved_space,
Chao Yuaf033b22018-09-20 20:05:00 +08002645 .write_dquot = f2fs_dquot_commit,
2646 .acquire_dquot = f2fs_dquot_acquire,
2647 .release_dquot = f2fs_dquot_release,
2648 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2649 .write_info = f2fs_dquot_commit_info,
Chao Yu0abd6752017-07-09 00:13:07 +08002650 .alloc_dquot = dquot_alloc,
2651 .destroy_dquot = dquot_destroy,
Chao Yu5c571322017-07-26 00:01:41 +08002652 .get_projid = f2fs_get_projid,
Chao Yu0abd6752017-07-09 00:13:07 +08002653 .get_next_id = dquot_get_next_id,
2654};
2655
2656static const struct quotactl_ops f2fs_quotactl_ops = {
2657 .quota_on = f2fs_quota_on,
2658 .quota_off = f2fs_quota_off,
2659 .quota_sync = f2fs_quota_sync,
2660 .get_state = dquot_get_state,
2661 .set_info = dquot_set_dqinfo,
2662 .get_dqblk = dquot_get_dqblk,
2663 .set_dqblk = dquot_set_dqblk,
2664 .get_nextdqblk = dquot_get_next_dqblk,
2665};
2666#else
Chao Yuaf033b22018-09-20 20:05:00 +08002667int f2fs_quota_sync(struct super_block *sb, int type)
2668{
2669 return 0;
2670}
2671
Chao Yu4b2414d2017-08-08 10:54:31 +08002672void f2fs_quota_off_umount(struct super_block *sb)
Chao Yu0abd6752017-07-09 00:13:07 +08002673{
2674}
2675#endif
2676
Arvind Yadavf62fc9f2017-08-31 15:06:24 +05302677static const struct super_operations f2fs_sops = {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002678 .alloc_inode = f2fs_alloc_inode,
Al Virod01718a2019-04-15 19:29:14 -04002679 .free_inode = f2fs_free_inode,
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09002680 .drop_inode = f2fs_drop_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002681 .write_inode = f2fs_write_inode,
Jaegeuk Kimb3783872013-06-10 09:17:01 +09002682 .dirty_inode = f2fs_dirty_inode,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002683 .show_options = f2fs_show_options,
Chao Yu0abd6752017-07-09 00:13:07 +08002684#ifdef CONFIG_QUOTA
2685 .quota_read = f2fs_quota_read,
2686 .quota_write = f2fs_quota_write,
2687 .get_dquots = f2fs_get_dquots,
2688#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002689 .evict_inode = f2fs_evict_inode,
2690 .put_super = f2fs_put_super,
2691 .sync_fs = f2fs_sync_fs,
Changman Leed6212a52013-01-29 18:30:07 +09002692 .freeze_fs = f2fs_freeze,
2693 .unfreeze_fs = f2fs_unfreeze,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002694 .statfs = f2fs_statfs,
Namjae Jeon696c0182013-06-16 09:48:48 +09002695 .remount_fs = f2fs_remount,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002696};
2697
Chandan Rajendra643fa962018-12-12 15:20:12 +05302698#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002699static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2700{
2701 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2702 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2703 ctx, len, NULL);
2704}
2705
2706static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2707 void *fs_data)
2708{
Sheng Yongb7c409d2018-03-15 18:51:41 +08002709 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2710
2711 /*
2712 * Encrypting the root directory is not allowed because fsck
2713 * expects lost+found directory to exist and remain unencrypted
2714 * if LOST_FOUND feature is enabled.
2715 *
2716 */
Chao Yu7beb01f2018-10-24 18:34:26 +08002717 if (f2fs_sb_has_lost_found(sbi) &&
Sheng Yongb7c409d2018-03-15 18:51:41 +08002718 inode->i_ino == F2FS_ROOT_INO(sbi))
2719 return -EPERM;
2720
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002721 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2722 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2723 ctx, len, fs_data, XATTR_CREATE);
2724}
2725
Eric Biggersac4acb12020-09-16 21:11:35 -07002726static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
Sheng Yongff62af22018-03-15 18:51:42 +08002727{
Eric Biggersac4acb12020-09-16 21:11:35 -07002728 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
Sheng Yongff62af22018-03-15 18:51:42 +08002729}
2730
Eric Biggers0eee17e2019-10-24 14:54:38 -07002731static bool f2fs_has_stable_inodes(struct super_block *sb)
2732{
2733 return true;
2734}
2735
2736static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
2737 int *ino_bits_ret, int *lblk_bits_ret)
2738{
2739 *ino_bits_ret = 8 * sizeof(nid_t);
2740 *lblk_bits_ret = 8 * sizeof(block_t);
2741}
2742
Satya Tangirala27aacd22020-07-02 01:56:06 +00002743static int f2fs_get_num_devices(struct super_block *sb)
2744{
2745 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2746
2747 if (f2fs_is_multi_device(sbi))
2748 return sbi->s_ndevs;
2749 return 1;
2750}
2751
2752static void f2fs_get_devices(struct super_block *sb,
2753 struct request_queue **devs)
2754{
2755 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2756 int i;
2757
2758 for (i = 0; i < sbi->s_ndevs; i++)
2759 devs[i] = bdev_get_queue(FDEV(i).bdev);
2760}
2761
Eric Biggers6f69f0e2017-02-07 12:42:10 -08002762static const struct fscrypt_operations f2fs_cryptops = {
Eric Biggers0eee17e2019-10-24 14:54:38 -07002763 .key_prefix = "f2fs:",
2764 .get_context = f2fs_get_context,
2765 .set_context = f2fs_set_context,
Eric Biggersac4acb12020-09-16 21:11:35 -07002766 .get_dummy_policy = f2fs_get_dummy_policy,
Eric Biggers0eee17e2019-10-24 14:54:38 -07002767 .empty_dir = f2fs_empty_dir,
2768 .max_namelen = F2FS_NAME_LEN,
2769 .has_stable_inodes = f2fs_has_stable_inodes,
2770 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
Satya Tangirala27aacd22020-07-02 01:56:06 +00002771 .get_num_devices = f2fs_get_num_devices,
2772 .get_devices = f2fs_get_devices,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002773};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002774#endif
2775
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002776static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2777 u64 ino, u32 generation)
2778{
2779 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2780 struct inode *inode;
2781
Chao Yu4d57b862018-05-30 00:20:41 +08002782 if (f2fs_check_nid_range(sbi, ino))
Chao Yu910bb122014-03-12 17:08:36 +08002783 return ERR_PTR(-ESTALE);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002784
2785 /*
2786 * f2fs_iget isn't quite right if the inode is currently unallocated!
2787 * However f2fs_iget currently does appropriate checks to handle stale
2788 * inodes so everything is OK.
2789 */
2790 inode = f2fs_iget(sb, ino);
2791 if (IS_ERR(inode))
2792 return ERR_CAST(inode);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002793 if (unlikely(generation && inode->i_generation != generation)) {
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002794 /* we didn't find the right inode.. */
2795 iput(inode);
2796 return ERR_PTR(-ESTALE);
2797 }
2798 return inode;
2799}
2800
2801static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2802 int fh_len, int fh_type)
2803{
2804 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2805 f2fs_nfs_get_inode);
2806}
2807
2808static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2809 int fh_len, int fh_type)
2810{
2811 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2812 f2fs_nfs_get_inode);
2813}
2814
2815static const struct export_operations f2fs_export_ops = {
2816 .fh_to_dentry = f2fs_fh_to_dentry,
2817 .fh_to_parent = f2fs_fh_to_parent,
2818 .get_parent = f2fs_get_parent,
2819};
2820
Chengguang Xu6d1451b2021-01-13 13:21:54 +08002821loff_t max_file_blocks(struct inode *inode)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002822{
Chao Yu7a2af762017-07-19 00:19:06 +08002823 loff_t result = 0;
Chengguang Xu6d1451b2021-01-13 13:21:54 +08002824 loff_t leaf_count;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002825
Chao Yu7a2af762017-07-19 00:19:06 +08002826 /*
2827 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
Chao Yu6afc6622017-09-06 21:59:50 +08002828 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
Chao Yu7a2af762017-07-19 00:19:06 +08002829 * space in inode.i_addr, it will be more safe to reassign
2830 * result as zero.
2831 */
2832
Chengguang Xu6d1451b2021-01-13 13:21:54 +08002833 if (inode && f2fs_compressed_file(inode))
2834 leaf_count = ADDRS_PER_BLOCK(inode);
2835 else
2836 leaf_count = DEF_ADDRS_PER_BLOCK;
2837
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002838 /* two direct node blocks */
2839 result += (leaf_count * 2);
2840
2841 /* two indirect node blocks */
2842 leaf_count *= NIDS_PER_BLOCK;
2843 result += (leaf_count * 2);
2844
2845 /* one double indirect node block */
2846 leaf_count *= NIDS_PER_BLOCK;
2847 result += leaf_count;
2848
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002849 return result;
2850}
2851
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002852static int __f2fs_commit_super(struct buffer_head *bh,
2853 struct f2fs_super_block *super)
Chao Yu9a59b622015-12-15 09:58:18 +08002854{
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002855 lock_buffer(bh);
2856 if (super)
2857 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002858 set_buffer_dirty(bh);
2859 unlock_buffer(bh);
2860
2861 /* it's rare case, we can do fua all the time */
Jan Kara3adc5fcb2017-05-02 17:03:47 +02002862 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002863}
2864
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002865static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002866 struct buffer_head *bh)
2867{
2868 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2869 (bh->b_data + F2FS_SUPER_OFFSET);
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002870 struct super_block *sb = sbi->sb;
Chao Yu9a59b622015-12-15 09:58:18 +08002871 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2872 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2873 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2874 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2875 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2876 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2877 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2878 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2879 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2880 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2881 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2882 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2883 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002884 u64 main_end_blkaddr = main_blkaddr +
2885 (segment_count_main << log_blocks_per_seg);
2886 u64 seg_end_blkaddr = segment0_blkaddr +
2887 (segment_count << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002888
2889 if (segment0_blkaddr != cp_blkaddr) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002890 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2891 segment0_blkaddr, cp_blkaddr);
Chao Yu9a59b622015-12-15 09:58:18 +08002892 return true;
2893 }
2894
2895 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2896 sit_blkaddr) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002897 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2898 cp_blkaddr, sit_blkaddr,
2899 segment_count_ckpt << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002900 return true;
2901 }
2902
2903 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2904 nat_blkaddr) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002905 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2906 sit_blkaddr, nat_blkaddr,
2907 segment_count_sit << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002908 return true;
2909 }
2910
2911 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2912 ssa_blkaddr) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002913 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2914 nat_blkaddr, ssa_blkaddr,
2915 segment_count_nat << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002916 return true;
2917 }
2918
2919 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2920 main_blkaddr) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002921 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2922 ssa_blkaddr, main_blkaddr,
2923 segment_count_ssa << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002924 return true;
2925 }
2926
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002927 if (main_end_blkaddr > seg_end_blkaddr) {
Wang Xiaojund89f5892020-09-18 08:31:24 +08002928 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
2929 main_blkaddr, seg_end_blkaddr,
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002930 segment_count_main << log_blocks_per_seg);
Chao Yu9a59b622015-12-15 09:58:18 +08002931 return true;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002932 } else if (main_end_blkaddr < seg_end_blkaddr) {
2933 int err = 0;
2934 char *res;
Chao Yu9a59b622015-12-15 09:58:18 +08002935
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002936 /* fix in-memory information all the time */
2937 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2938 segment0_blkaddr) >> log_blocks_per_seg);
2939
2940 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002941 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002942 res = "internally";
2943 } else {
2944 err = __f2fs_commit_super(bh, NULL);
2945 res = err ? "failed" : "done";
2946 }
Wang Xiaojund89f5892020-09-18 08:31:24 +08002947 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
2948 res, main_blkaddr, seg_end_blkaddr,
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002949 segment_count_main << log_blocks_per_seg);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002950 if (err)
2951 return true;
2952 }
Chao Yu9a59b622015-12-15 09:58:18 +08002953 return false;
2954}
2955
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07002956static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002957 struct buffer_head *bh)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002958{
Wang Xiaojunf99ba9a2020-09-17 19:11:58 +08002959 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07002960 block_t total_sections, blocks_per_seg;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07002961 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2962 (bh->b_data + F2FS_SUPER_OFFSET);
Junling Zhengd440c522018-09-28 20:25:56 +08002963 size_t crc_offset = 0;
2964 __u32 crc = 0;
2965
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08002966 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2967 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2968 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2969 return -EINVAL;
2970 }
2971
Junling Zhengd440c522018-09-28 20:25:56 +08002972 /* Check checksum_offset and crc in superblock */
Chao Yu7beb01f2018-10-24 18:34:26 +08002973 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
Junling Zhengd440c522018-09-28 20:25:56 +08002974 crc_offset = le32_to_cpu(raw_super->checksum_offset);
2975 if (crc_offset !=
2976 offsetof(struct f2fs_super_block, crc)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002977 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2978 crc_offset);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08002979 return -EFSCORRUPTED;
Junling Zhengd440c522018-09-28 20:25:56 +08002980 }
2981 crc = le32_to_cpu(raw_super->crc);
2982 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002983 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08002984 return -EFSCORRUPTED;
Junling Zhengd440c522018-09-28 20:25:56 +08002985 }
2986 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002987
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09002988 /* Currently, support only 4KB block size */
Chao Yue584bbe2020-12-09 16:49:36 +08002989 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
2990 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
2991 le32_to_cpu(raw_super->log_blocksize),
2992 F2FS_BLKSIZE_BITS);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08002993 return -EFSCORRUPTED;
Namjae Jeona07ef782012-12-30 14:52:05 +09002994 }
majianpeng5c9b4692013-02-01 19:07:57 +08002995
Chao Yu9a59b622015-12-15 09:58:18 +08002996 /* check log blocks per segment */
2997 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002998 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
2999 le32_to_cpu(raw_super->log_blocks_per_seg));
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003000 return -EFSCORRUPTED;
Chao Yu9a59b622015-12-15 09:58:18 +08003001 }
3002
Chao Yu55cf9cb2014-09-15 18:01:10 +08003003 /* Currently, support 512/1024/2048/4096 bytes sector size */
3004 if (le32_to_cpu(raw_super->log_sectorsize) >
3005 F2FS_MAX_LOG_SECTOR_SIZE ||
3006 le32_to_cpu(raw_super->log_sectorsize) <
3007 F2FS_MIN_LOG_SECTOR_SIZE) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003008 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3009 le32_to_cpu(raw_super->log_sectorsize));
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003010 return -EFSCORRUPTED;
Namjae Jeona07ef782012-12-30 14:52:05 +09003011 }
Chao Yu55cf9cb2014-09-15 18:01:10 +08003012 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3013 le32_to_cpu(raw_super->log_sectorsize) !=
3014 F2FS_MAX_LOG_SECTOR_SIZE) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003015 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3016 le32_to_cpu(raw_super->log_sectors_per_block),
3017 le32_to_cpu(raw_super->log_sectorsize));
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003018 return -EFSCORRUPTED;
Namjae Jeona07ef782012-12-30 14:52:05 +09003019 }
Chao Yu9a59b622015-12-15 09:58:18 +08003020
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003021 segment_count = le32_to_cpu(raw_super->segment_count);
Wang Xiaojunf99ba9a2020-09-17 19:11:58 +08003022 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003023 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3024 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3025 total_sections = le32_to_cpu(raw_super->section_count);
3026
3027 /* blocks_per_seg should be 512, given the above check */
3028 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
3029
3030 if (segment_count > F2FS_MAX_SEGMENT ||
3031 segment_count < F2FS_MIN_SEGMENTS) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003032 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003033 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003034 }
3035
Wang Xiaojunf99ba9a2020-09-17 19:11:58 +08003036 if (total_sections > segment_count_main || total_sections < 1 ||
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003037 segs_per_sec > segment_count || !segs_per_sec) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003038 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3039 segment_count, total_sections, segs_per_sec);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003040 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003041 }
3042
Chao Yu3a22e9a2020-09-29 09:23:34 +08003043 if (segment_count_main != total_sections * segs_per_sec) {
3044 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3045 segment_count_main, total_sections, segs_per_sec);
3046 return -EFSCORRUPTED;
3047 }
3048
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003049 if ((segment_count / segs_per_sec) < total_sections) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003050 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3051 segment_count, segs_per_sec, total_sections);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003052 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003053 }
3054
Martin Blumenstingl88960062018-12-22 11:22:26 +01003055 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003056 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3057 segment_count, le64_to_cpu(raw_super->block_count));
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003058 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003059 }
3060
Qiuyang Sun9f701f62019-09-23 12:22:35 +08003061 if (RDEV(0).path[0]) {
3062 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3063 int i = 1;
3064
3065 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3066 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3067 i++;
3068 }
3069 if (segment_count != dev_seg_count) {
3070 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3071 segment_count, dev_seg_count);
3072 return -EFSCORRUPTED;
3073 }
Chao Yu07eb1d62020-09-21 20:53:13 +08003074 } else {
3075 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3076 !bdev_is_zoned(sbi->sb->s_bdev)) {
3077 f2fs_info(sbi, "Zoned block device path is missing");
3078 return -EFSCORRUPTED;
3079 }
Qiuyang Sun9f701f62019-09-23 12:22:35 +08003080 }
3081
Chao Yu42bf5462018-06-23 00:12:36 +08003082 if (secs_per_zone > total_sections || !secs_per_zone) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003083 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3084 secs_per_zone, total_sections);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003085 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003086 }
3087 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3088 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3089 (le32_to_cpu(raw_super->extension_count) +
3090 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003091 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3092 le32_to_cpu(raw_super->extension_count),
3093 raw_super->hot_ext_count,
3094 F2FS_MAX_EXTENSION);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003095 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003096 }
3097
3098 if (le32_to_cpu(raw_super->cp_payload) >
3099 (blocks_per_seg - F2FS_CP_PACKS)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003100 f2fs_info(sbi, "Insane cp_payload (%u > %u)",
3101 le32_to_cpu(raw_super->cp_payload),
3102 blocks_per_seg - F2FS_CP_PACKS);
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003103 return -EFSCORRUPTED;
Jaegeuk Kim0cfe75c2018-04-27 19:03:22 -07003104 }
3105
Chao Yu9a59b622015-12-15 09:58:18 +08003106 /* check reserved ino info */
3107 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3108 le32_to_cpu(raw_super->meta_ino) != 2 ||
3109 le32_to_cpu(raw_super->root_ino) != 3) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003110 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3111 le32_to_cpu(raw_super->node_ino),
3112 le32_to_cpu(raw_super->meta_ino),
3113 le32_to_cpu(raw_super->root_ino));
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003114 return -EFSCORRUPTED;
Chao Yu9a59b622015-12-15 09:58:18 +08003115 }
3116
3117 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003118 if (sanity_check_area_boundary(sbi, bh))
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003119 return -EFSCORRUPTED;
Chao Yu9a59b622015-12-15 09:58:18 +08003120
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003121 return 0;
3122}
3123
Chao Yu4d57b862018-05-30 00:20:41 +08003124int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003125{
3126 unsigned int total, fsmeta;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09003127 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3128 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08003129 unsigned int ovp_segments, reserved_segments;
Jin Qian15d30422017-05-15 10:45:08 -07003130 unsigned int main_segs, blocks_per_seg;
Chao Yuc77ec612018-06-23 11:25:19 +08003131 unsigned int sit_segs, nat_segs;
3132 unsigned int sit_bitmap_size, nat_bitmap_size;
3133 unsigned int log_blocks_per_seg;
Chao Yu9dc956b2018-06-27 18:05:54 +08003134 unsigned int segment_count_main;
Chao Yue494c2f2018-08-01 19:16:11 +08003135 unsigned int cp_pack_start_sum, cp_payload;
Chao Yu7b63f722019-04-15 15:30:50 +08003136 block_t user_block_count, valid_user_blocks;
3137 block_t avail_node_count, valid_node_count;
Chao Yu042be0f2018-09-06 20:34:12 +08003138 int i, j;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003139
3140 total = le32_to_cpu(raw_super->segment_count);
3141 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
Chao Yuc77ec612018-06-23 11:25:19 +08003142 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3143 fsmeta += sit_segs;
3144 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3145 fsmeta += nat_segs;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003146 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3147 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3148
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003149 if (unlikely(fsmeta >= total))
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003150 return 1;
Jaegeuk Kim577e3492013-01-24 19:56:11 +09003151
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08003152 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3153 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3154
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003155 if (!f2fs_sb_has_readonly(sbi) &&
3156 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08003157 ovp_segments == 0 || reserved_segments == 0)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003158 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
Jaegeuk Kim2040fce82016-12-05 13:56:04 -08003159 return 1;
3160 }
Chao Yu9dc956b2018-06-27 18:05:54 +08003161 user_block_count = le64_to_cpu(ckpt->user_block_count);
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003162 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3163 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
Chao Yu9dc956b2018-06-27 18:05:54 +08003164 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3165 if (!user_block_count || user_block_count >=
3166 segment_count_main << log_blocks_per_seg) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003167 f2fs_err(sbi, "Wrong user_block_count: %u",
3168 user_block_count);
Chao Yu9dc956b2018-06-27 18:05:54 +08003169 return 1;
3170 }
3171
Chao Yu7b63f722019-04-15 15:30:50 +08003172 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3173 if (valid_user_blocks > user_block_count) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003174 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3175 valid_user_blocks, user_block_count);
Chao Yu7b63f722019-04-15 15:30:50 +08003176 return 1;
3177 }
3178
3179 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
Chao Yu27cae0b2019-08-05 18:27:25 +08003180 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
Chao Yu7b63f722019-04-15 15:30:50 +08003181 if (valid_node_count > avail_node_count) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003182 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3183 valid_node_count, avail_node_count);
Chao Yu7b63f722019-04-15 15:30:50 +08003184 return 1;
3185 }
3186
Jin Qian15d30422017-05-15 10:45:08 -07003187 main_segs = le32_to_cpu(raw_super->segment_count_main);
3188 blocks_per_seg = sbi->blocks_per_seg;
3189
3190 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3191 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3192 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3193 return 1;
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003194
3195 if (f2fs_sb_has_readonly(sbi))
3196 goto check_data;
3197
Chao Yu042be0f2018-09-06 20:34:12 +08003198 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3199 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3200 le32_to_cpu(ckpt->cur_node_segno[j])) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003201 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3202 i, j,
3203 le32_to_cpu(ckpt->cur_node_segno[i]));
Chao Yu042be0f2018-09-06 20:34:12 +08003204 return 1;
3205 }
3206 }
Jin Qian15d30422017-05-15 10:45:08 -07003207 }
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003208check_data:
Jin Qian15d30422017-05-15 10:45:08 -07003209 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3210 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3211 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3212 return 1;
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003213
3214 if (f2fs_sb_has_readonly(sbi))
3215 goto skip_cross;
3216
Chao Yu042be0f2018-09-06 20:34:12 +08003217 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3218 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3219 le32_to_cpu(ckpt->cur_data_segno[j])) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003220 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3221 i, j,
3222 le32_to_cpu(ckpt->cur_data_segno[i]));
Chao Yu042be0f2018-09-06 20:34:12 +08003223 return 1;
3224 }
3225 }
3226 }
3227 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
Surbhi Palande1166c1f2019-08-23 15:40:45 -07003228 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
Chao Yu042be0f2018-09-06 20:34:12 +08003229 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3230 le32_to_cpu(ckpt->cur_data_segno[j])) {
Surbhi Palande1166c1f2019-08-23 15:40:45 -07003231 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003232 i, j,
3233 le32_to_cpu(ckpt->cur_node_segno[i]));
Chao Yu042be0f2018-09-06 20:34:12 +08003234 return 1;
3235 }
3236 }
Jin Qian15d30422017-05-15 10:45:08 -07003237 }
Jaegeuk Kima7d9fe32021-05-21 01:32:53 -07003238skip_cross:
Chao Yuc77ec612018-06-23 11:25:19 +08003239 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3240 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
Chao Yuc77ec612018-06-23 11:25:19 +08003241
3242 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3243 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003244 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3245 sit_bitmap_size, nat_bitmap_size);
Chao Yuc77ec612018-06-23 11:25:19 +08003246 return 1;
3247 }
3248
Chao Yue494c2f2018-08-01 19:16:11 +08003249 cp_pack_start_sum = __start_sum_addr(sbi);
3250 cp_payload = __cp_payload(sbi);
3251 if (cp_pack_start_sum < cp_payload + 1 ||
3252 cp_pack_start_sum > blocks_per_seg - 1 -
Chao Yud0b9e422020-08-04 21:14:45 +08003253 NR_CURSEG_PERSIST_TYPE) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003254 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3255 cp_pack_start_sum);
Chao Yue494c2f2018-08-01 19:16:11 +08003256 return 1;
3257 }
3258
Chao Yu5dae2d32019-05-20 10:09:22 +08003259 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3260 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
Chao Yu2d008832019-07-11 09:29:15 +08003261 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3262 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3263 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003264 le32_to_cpu(ckpt->checksum_offset));
Chao Yu5dae2d32019-05-20 10:09:22 +08003265 return 1;
3266 }
3267
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07003268 if (unlikely(f2fs_cp_error(sbi))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003269 f2fs_err(sbi, "A bug case: need to run fsck");
Jaegeuk Kim577e3492013-01-24 19:56:11 +09003270 return 1;
3271 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003272 return 0;
3273}
3274
3275static void init_sb_info(struct f2fs_sb_info *sbi)
3276{
3277 struct f2fs_super_block *raw_super = sbi->raw_super;
Yunlong Song089842d2018-10-24 16:09:42 +08003278 int i;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003279
3280 sbi->log_sectors_per_block =
3281 le32_to_cpu(raw_super->log_sectors_per_block);
3282 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3283 sbi->blocksize = 1 << sbi->log_blocksize;
3284 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3285 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
3286 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3287 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3288 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3289 sbi->total_node_count =
3290 (le32_to_cpu(raw_super->segment_count_nat) / 2)
3291 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
Yangtao Lib9ec1092020-12-07 18:59:33 +08003292 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3293 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3294 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09003295 sbi->cur_victim_sec = NULL_SECNO;
Chao Yue3080b02018-10-24 18:37:27 +08003296 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3297 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09003298 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
Chao Yue3080b02018-10-24 18:37:27 +08003299 sbi->migration_granularity = sbi->segs_per_sec;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003300
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09003301 sbi->dir_level = DEF_DIR_LEVEL;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08003302 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003303 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
Sahitya Tummalaa7d10cf2018-09-19 14:18:47 +05303304 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3305 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003306 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
Jaegeuk Kim03f2c022019-01-14 10:42:11 -08003307 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3308 DEF_UMOUNT_DISCARD_TIMEOUT;
Chao Yucaf00472015-01-28 17:48:42 +08003309 clear_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003310
Jaegeuk Kim35782b22016-10-20 19:09:57 -07003311 for (i = 0; i < NR_COUNT_TYPE; i++)
3312 atomic_set(&sbi->nr_pages[i], 0);
3313
Chao Yuc29fd0c2018-06-04 23:20:36 +08003314 for (i = 0; i < META; i++)
3315 atomic_set(&sbi->wb_sync_req[i], 0);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003316
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003317 INIT_LIST_HEAD(&sbi->s_list);
3318 mutex_init(&sbi->umount_mutex);
Chao Yu107a8052018-05-26 09:00:13 +08003319 init_rwsem(&sbi->io_order_lock);
Chao Yuaaec2b12016-09-20 11:04:18 +08003320 spin_lock_init(&sbi->cp_lock);
Chao Yu1228b482017-09-29 13:59:39 +08003321
3322 sbi->dirty_device = 0;
3323 spin_lock_init(&sbi->dev_lock);
Chao Yud0d3f1b2018-02-11 22:53:20 +08003324
Chao Yu846ae672018-02-26 22:04:13 +08003325 init_rwsem(&sbi->sb_lock);
Jaegeuk Kimf5a53ed2019-10-18 10:06:40 -07003326 init_rwsem(&sbi->pin_sem);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003327}
3328
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07003329static int init_percpu_info(struct f2fs_sb_info *sbi)
3330{
Jaegeuk Kim35782b22016-10-20 19:09:57 -07003331 int err;
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07003332
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07003333 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3334 if (err)
3335 return err;
3336
Chao Yu4a70e252018-09-05 14:54:02 +08003337 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07003338 GFP_KERNEL);
Chao Yu4a70e252018-09-05 14:54:02 +08003339 if (err)
3340 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3341
3342 return err;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07003343}
3344
Damien Le Moal178053e2016-10-28 17:45:05 +09003345#ifdef CONFIG_BLK_DEV_ZONED
Christoph Hellwigd4100352019-11-11 11:39:30 +09003346
Aravind Rameshde881df2020-07-16 18:26:56 +05303347struct f2fs_report_zones_args {
3348 struct f2fs_dev_info *dev;
3349 bool zone_cap_mismatch;
3350};
3351
3352static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3353 void *data)
3354{
3355 struct f2fs_report_zones_args *rz_args = data;
3356
3357 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3358 return 0;
3359
3360 set_bit(idx, rz_args->dev->blkz_seq);
3361 rz_args->dev->zone_capacity_blocks[idx] = zone->capacity >>
3362 F2FS_LOG_SECTORS_PER_BLOCK;
3363 if (zone->len != zone->capacity && !rz_args->zone_cap_mismatch)
3364 rz_args->zone_cap_mismatch = true;
3365
Christoph Hellwigd4100352019-11-11 11:39:30 +09003366 return 0;
3367}
3368
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003369static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
Damien Le Moal178053e2016-10-28 17:45:05 +09003370{
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003371 struct block_device *bdev = FDEV(devi).bdev;
Christoph Hellwiga7824832020-11-26 18:43:37 +01003372 sector_t nr_sectors = bdev_nr_sectors(bdev);
Aravind Rameshde881df2020-07-16 18:26:56 +05303373 struct f2fs_report_zones_args rep_zone_arg;
Christoph Hellwigd4100352019-11-11 11:39:30 +09003374 int ret;
Damien Le Moal178053e2016-10-28 17:45:05 +09003375
Chao Yu7beb01f2018-10-24 18:34:26 +08003376 if (!f2fs_sb_has_blkzoned(sbi))
Damien Le Moal178053e2016-10-28 17:45:05 +09003377 return 0;
3378
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003379 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
Damien Le Moalf99e8642017-01-12 07:58:32 -07003380 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003381 return -EINVAL;
Damien Le Moalf99e8642017-01-12 07:58:32 -07003382 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003383 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
3384 __ilog2_u32(sbi->blocks_per_blkz))
3385 return -EINVAL;
Damien Le Moal178053e2016-10-28 17:45:05 +09003386 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003387 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
3388 sbi->log_blocks_per_blkz;
Damien Le Moalf99e8642017-01-12 07:58:32 -07003389 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003390 FDEV(devi).nr_blkz++;
Damien Le Moal178053e2016-10-28 17:45:05 +09003391
Eric Biggers0b6d4ca2020-06-04 21:57:48 -07003392 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
Damien Le Moal95175da2019-03-16 09:13:07 +09003393 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3394 * sizeof(unsigned long),
3395 GFP_KERNEL);
3396 if (!FDEV(devi).blkz_seq)
Damien Le Moal178053e2016-10-28 17:45:05 +09003397 return -ENOMEM;
3398
Aravind Rameshde881df2020-07-16 18:26:56 +05303399 /* Get block zones type and zone-capacity */
3400 FDEV(devi).zone_capacity_blocks = f2fs_kzalloc(sbi,
3401 FDEV(devi).nr_blkz * sizeof(block_t),
3402 GFP_KERNEL);
3403 if (!FDEV(devi).zone_capacity_blocks)
3404 return -ENOMEM;
3405
3406 rep_zone_arg.dev = &FDEV(devi);
3407 rep_zone_arg.zone_cap_mismatch = false;
3408
Christoph Hellwigd4100352019-11-11 11:39:30 +09003409 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
Aravind Rameshde881df2020-07-16 18:26:56 +05303410 &rep_zone_arg);
Christoph Hellwigd4100352019-11-11 11:39:30 +09003411 if (ret < 0)
3412 return ret;
Damien Le Moal178053e2016-10-28 17:45:05 +09003413
Aravind Rameshde881df2020-07-16 18:26:56 +05303414 if (!rep_zone_arg.zone_cap_mismatch) {
3415 kfree(FDEV(devi).zone_capacity_blocks);
3416 FDEV(devi).zone_capacity_blocks = NULL;
3417 }
3418
Christoph Hellwigd4100352019-11-11 11:39:30 +09003419 return 0;
Damien Le Moal178053e2016-10-28 17:45:05 +09003420}
3421#endif
3422
Gu Zheng9076a752013-10-14 18:47:11 +08003423/*
3424 * Read f2fs raw super block.
Shawn Lin2b39e902016-02-17 08:59:01 +08003425 * Because we have two copies of super block, so read both of them
3426 * to get the first valid one. If any one of them is broken, we pass
3427 * them recovery flag back to the caller.
Gu Zheng9076a752013-10-14 18:47:11 +08003428 */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003429static int read_raw_super_block(struct f2fs_sb_info *sbi,
Gu Zheng9076a752013-10-14 18:47:11 +08003430 struct f2fs_super_block **raw_super,
Chao Yue8240f62015-12-15 17:19:26 +08003431 int *valid_super_block, int *recovery)
majianpeng14d7e9d2013-02-01 19:07:03 +08003432{
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003433 struct super_block *sb = sbi->sb;
Shawn Lin2b39e902016-02-17 08:59:01 +08003434 int block;
Chao Yue8240f62015-12-15 17:19:26 +08003435 struct buffer_head *bh;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003436 struct f2fs_super_block *super;
hujianyangda554e42015-05-21 14:42:53 +08003437 int err = 0;
majianpeng14d7e9d2013-02-01 19:07:03 +08003438
Yunlei Heb39f0de2015-12-15 17:17:20 +08003439 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3440 if (!super)
3441 return -ENOMEM;
Shawn Lin2b39e902016-02-17 08:59:01 +08003442
3443 for (block = 0; block < 2; block++) {
3444 bh = sb_bread(sb, block);
3445 if (!bh) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003446 f2fs_err(sbi, "Unable to read %dth superblock",
3447 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08003448 err = -EIO;
Chengguang Xu via Linux-f2fs-develed3520422019-09-27 09:35:48 +08003449 *recovery = 1;
Shawn Lin2b39e902016-02-17 08:59:01 +08003450 continue;
3451 }
majianpeng14d7e9d2013-02-01 19:07:03 +08003452
Shawn Lin2b39e902016-02-17 08:59:01 +08003453 /* sanity checking of raw super */
Icenowy Zheng38fb6d02019-07-25 11:08:52 +08003454 err = sanity_check_raw_super(sbi, bh);
3455 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003456 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3457 block + 1);
Shawn Lin2b39e902016-02-17 08:59:01 +08003458 brelse(bh);
Chengguang Xu via Linux-f2fs-develed3520422019-09-27 09:35:48 +08003459 *recovery = 1;
Shawn Lin2b39e902016-02-17 08:59:01 +08003460 continue;
3461 }
3462
3463 if (!*raw_super) {
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003464 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3465 sizeof(*super));
Shawn Lin2b39e902016-02-17 08:59:01 +08003466 *valid_super_block = block;
3467 *raw_super = super;
3468 }
Chao Yue8240f62015-12-15 17:19:26 +08003469 brelse(bh);
Shawn Lin2b39e902016-02-17 08:59:01 +08003470 }
3471
hujianyangda554e42015-05-21 14:42:53 +08003472 /* No valid superblock */
Shawn Lin2b39e902016-02-17 08:59:01 +08003473 if (!*raw_super)
Denis Efremov742532d2020-06-10 01:14:46 +03003474 kfree(super);
Shawn Lin2b39e902016-02-17 08:59:01 +08003475 else
3476 err = 0;
hujianyangda554e42015-05-21 14:42:53 +08003477
Shawn Lin2b39e902016-02-17 08:59:01 +08003478 return err;
majianpeng14d7e9d2013-02-01 19:07:03 +08003479}
3480
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003481int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07003482{
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08003483 struct buffer_head *bh;
Junling Zhengd440c522018-09-28 20:25:56 +08003484 __u32 crc = 0;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07003485 int err;
3486
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003487 if ((recover && f2fs_readonly(sbi->sb)) ||
3488 bdev_read_only(sbi->sb->s_bdev)) {
3489 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07003490 return -EROFS;
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003491 }
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07003492
Junling Zhengd440c522018-09-28 20:25:56 +08003493 /* we should update superblock crc here */
Chao Yu7beb01f2018-10-24 18:34:26 +08003494 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
Junling Zhengd440c522018-09-28 20:25:56 +08003495 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3496 offsetof(struct f2fs_super_block, crc));
3497 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3498 }
3499
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003500 /* write back-up superblock first */
Sheng Yong0964fc12018-01-29 19:13:15 +08003501 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08003502 if (!bh)
3503 return -EIO;
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003504 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08003505 brelse(bh);
Chao Yuc5bda1c2015-06-08 13:28:03 +08003506
3507 /* if we are in recovery path, skip writing valid superblock */
3508 if (recover || err)
Jaegeuk Kim5d909cd2015-12-07 10:16:58 -08003509 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07003510
Chao Yue8240f62015-12-15 17:19:26 +08003511 /* write current valid superblock */
Sheng Yong0964fc12018-01-29 19:13:15 +08003512 bh = sb_bread(sbi->sb, sbi->valid_super_block);
Jaegeuk Kimfd694732016-03-20 15:33:20 -07003513 if (!bh)
3514 return -EIO;
3515 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3516 brelse(bh);
3517 return err;
Jaegeuk Kim26d815ad2015-04-20 18:49:51 -07003518}
3519
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003520static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3521{
3522 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003523 unsigned int max_devices = MAX_DEVICES;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003524 int i;
3525
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003526 /* Initialize single device information */
3527 if (!RDEV(0).path[0]) {
3528 if (!bdev_is_zoned(sbi->sb->s_bdev))
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003529 return 0;
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003530 max_devices = 1;
3531 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003532
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003533 /*
3534 * Initialize multiple devices information, or single
3535 * zoned block device information.
3536 */
Kees Cook026f0502018-06-12 14:28:23 -07003537 sbi->devs = f2fs_kzalloc(sbi,
3538 array_size(max_devices,
3539 sizeof(struct f2fs_dev_info)),
3540 GFP_KERNEL);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003541 if (!sbi->devs)
3542 return -ENOMEM;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003543
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003544 for (i = 0; i < max_devices; i++) {
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003545
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003546 if (i > 0 && !RDEV(i).path[0])
3547 break;
3548
3549 if (max_devices == 1) {
3550 /* Single zoned block device mount */
3551 FDEV(0).bdev =
3552 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003553 sbi->sb->s_mode, sbi->sb->s_type);
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003554 } else {
3555 /* Multi-device mount */
3556 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3557 FDEV(i).total_segments =
3558 le32_to_cpu(RDEV(i).total_segments);
3559 if (i == 0) {
3560 FDEV(i).start_blk = 0;
3561 FDEV(i).end_blk = FDEV(i).start_blk +
3562 (FDEV(i).total_segments <<
3563 sbi->log_blocks_per_seg) - 1 +
3564 le32_to_cpu(raw_super->segment0_blkaddr);
3565 } else {
3566 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3567 FDEV(i).end_blk = FDEV(i).start_blk +
3568 (FDEV(i).total_segments <<
3569 sbi->log_blocks_per_seg) - 1;
3570 }
3571 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3572 sbi->sb->s_mode, sbi->sb->s_type);
3573 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003574 if (IS_ERR(FDEV(i).bdev))
3575 return PTR_ERR(FDEV(i).bdev);
3576
3577 /* to release errored devices */
3578 sbi->s_ndevs = i + 1;
3579
3580#ifdef CONFIG_BLK_DEV_ZONED
3581 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
Chao Yu7beb01f2018-10-24 18:34:26 +08003582 !f2fs_sb_has_blkzoned(sbi)) {
Joe Perches833dcd32021-05-26 13:05:36 -07003583 f2fs_err(sbi, "Zoned block device feature not enabled");
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003584 return -EINVAL;
3585 }
3586 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3587 if (init_blkz_info(sbi, i)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003588 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003589 return -EINVAL;
3590 }
Masato Suzuki7bb3a372017-02-27 20:52:49 +09003591 if (max_devices == 1)
3592 break;
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003593 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3594 i, FDEV(i).path,
3595 FDEV(i).total_segments,
3596 FDEV(i).start_blk, FDEV(i).end_blk,
3597 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3598 "Host-aware" : "Host-managed");
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003599 continue;
3600 }
3601#endif
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003602 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3603 i, FDEV(i).path,
3604 FDEV(i).total_segments,
3605 FDEV(i).start_blk, FDEV(i).end_blk);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003606 }
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003607 f2fs_info(sbi,
3608 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003609 return 0;
3610}
3611
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07003612static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3613{
3614#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -07003615 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07003616 const struct f2fs_sb_encodings *encoding_info;
3617 struct unicode_map *encoding;
3618 __u16 encoding_flags;
3619
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07003620 if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3621 &encoding_flags)) {
3622 f2fs_err(sbi,
3623 "Encoding requested by superblock is unknown");
3624 return -EINVAL;
3625 }
3626
3627 encoding = utf8_load(encoding_info->version);
3628 if (IS_ERR(encoding)) {
3629 f2fs_err(sbi,
3630 "can't mount with superblock charset: %s-%s "
3631 "not supported by the kernel. flags: 0x%x.",
3632 encoding_info->name, encoding_info->version,
3633 encoding_flags);
3634 return PTR_ERR(encoding);
3635 }
3636 f2fs_info(sbi, "Using encoding defined by superblock: "
3637 "%s-%s with flags 0x%hx", encoding_info->name,
3638 encoding_info->version?:"\b", encoding_flags);
3639
Daniel Rosenbergeca48732020-07-08 02:12:36 -07003640 sbi->sb->s_encoding = encoding;
3641 sbi->sb->s_encoding_flags = encoding_flags;
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07003642 }
3643#else
3644 if (f2fs_sb_has_casefold(sbi)) {
3645 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3646 return -EINVAL;
3647 }
3648#endif
3649 return 0;
3650}
3651
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08003652static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3653{
3654 struct f2fs_sm_info *sm_i = SM_I(sbi);
3655
3656 /* adjust parameters according to the volume size */
3657 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
Chao Yu63189b72018-03-08 14:22:56 +08003658 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08003659 sm_i->dcc_info->discard_granularity = 1;
3660 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3661 }
Chao Yu4cac90d2018-06-11 18:02:01 +08003662
3663 sbi->readdir_ra = 1;
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08003664}
3665
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003666static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3667{
3668 struct f2fs_sb_info *sbi;
hujianyangda554e42015-05-21 14:42:53 +08003669 struct f2fs_super_block *raw_super;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003670 struct inode *root;
Sheng Yong99e3e852016-05-11 17:08:14 +08003671 int err;
Chao Yuaa2c8c42019-02-19 16:23:53 +08003672 bool skip_recovery = false, need_fsck = false;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003673 char *options = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08003674 int recovery, i, valid_super_block;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003675 struct curseg_info *seg_i;
Chao Yuaa2c8c42019-02-19 16:23:53 +08003676 int retry_cnt = 1;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003677
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07003678try_onemore:
hujianyangda554e42015-05-21 14:42:53 +08003679 err = -EINVAL;
3680 raw_super = NULL;
Chao Yue8240f62015-12-15 17:19:26 +08003681 valid_super_block = -1;
hujianyangda554e42015-05-21 14:42:53 +08003682 recovery = 0;
3683
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003684 /* allocate memory for f2fs-specific super block info */
3685 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3686 if (!sbi)
3687 return -ENOMEM;
3688
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003689 sbi->sb = sb;
3690
Keith Mok43b65732016-03-02 12:04:24 -08003691 /* Load the checksum driver */
3692 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3693 if (IS_ERR(sbi->s_chksum_driver)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003694 f2fs_err(sbi, "Cannot load crc32 driver.");
Keith Mok43b65732016-03-02 12:04:24 -08003695 err = PTR_ERR(sbi->s_chksum_driver);
3696 sbi->s_chksum_driver = NULL;
3697 goto free_sbi;
3698 }
3699
Namjae Jeonff9234a2013-01-12 14:41:13 +09003700 /* set a block size */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003701 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003702 f2fs_err(sbi, "unable to set blocksize");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003703 goto free_sbi;
Namjae Jeona07ef782012-12-30 14:52:05 +09003704 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003705
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07003706 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
Chao Yue8240f62015-12-15 17:19:26 +08003707 &recovery);
Gu Zheng9076a752013-10-14 18:47:11 +08003708 if (err)
3709 goto free_sbi;
3710
Gu Zheng5fb08372013-06-07 14:16:53 +08003711 sb->s_fs_info = sbi;
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003712 sbi->raw_super = raw_super;
3713
Chao Yu704956e2017-07-31 20:19:09 +08003714 /* precompute checksum seed for metadata */
Chao Yu7beb01f2018-10-24 18:34:26 +08003715 if (f2fs_sb_has_inode_chksum(sbi))
Chao Yu704956e2017-07-31 20:19:09 +08003716 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3717 sizeof(raw_super->uuid));
3718
Yunlei He498c5e92015-05-07 18:11:37 +08003719 default_options(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003720 /* parse mount options */
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003721 options = kstrdup((const char *)data, GFP_KERNEL);
3722 if (data && !options) {
3723 err = -ENOMEM;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003724 goto free_sb_buf;
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003725 }
3726
Eric Biggersed318a62020-05-12 16:32:50 -07003727 err = parse_options(sb, options, false);
Jaegeuk Kimdabc4a52015-01-23 17:41:39 -08003728 if (err)
3729 goto free_options;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003730
Chengguang Xu6d1451b2021-01-13 13:21:54 +08003731 sb->s_maxbytes = max_file_blocks(NULL) <<
Chao Yue0afc4d2015-12-31 14:35:37 +08003732 le32_to_cpu(raw_super->log_blocksize);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003733 sb->s_max_links = F2FS_LINK_MAX;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003734
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07003735 err = f2fs_setup_casefold(sbi);
3736 if (err)
3737 goto free_options;
3738
Chao Yu0abd6752017-07-09 00:13:07 +08003739#ifdef CONFIG_QUOTA
3740 sb->dq_op = &f2fs_quota_operations;
Jaegeuk Kimbc88ac92019-05-20 16:17:56 -07003741 sb->s_qcop = &f2fs_quotactl_ops;
Chao Yu5c571322017-07-26 00:01:41 +08003742 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Chao Yu292c196a2017-11-16 16:59:14 +08003743
Chao Yu7beb01f2018-10-24 18:34:26 +08003744 if (f2fs_sb_has_quota_ino(sbi)) {
Chao Yu292c196a2017-11-16 16:59:14 +08003745 for (i = 0; i < MAXQUOTAS; i++) {
3746 if (f2fs_qf_ino(sbi->sb, i))
3747 sbi->nquota_files++;
3748 }
3749 }
Chao Yu0abd6752017-07-09 00:13:07 +08003750#endif
3751
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003752 sb->s_op = &f2fs_sops;
Chandan Rajendra643fa962018-12-12 15:20:12 +05303753#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07003754 sb->s_cop = &f2fs_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07003755#endif
Eric Biggers95ae2512019-07-22 09:26:24 -07003756#ifdef CONFIG_FS_VERITY
3757 sb->s_vop = &f2fs_verityops;
3758#endif
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003759 sb->s_xattr = f2fs_xattr_handlers;
3760 sb->s_export_op = &f2fs_export_ops;
3761 sb->s_magic = F2FS_SUPER_MAGIC;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003762 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003763 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3764 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
Christoph Hellwig85787092017-05-10 15:06:33 +02003765 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
Yufen Yu578c6472018-01-09 19:33:39 +08003766 sb->s_iflags |= SB_I_CGROUPWB;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003767
3768 /* init f2fs-specific super block info */
Chao Yue8240f62015-12-15 17:19:26 +08003769 sbi->valid_super_block = valid_super_block;
Chao Yufb24fea2020-01-14 19:36:50 +08003770 init_rwsem(&sbi->gc_lock);
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003771 mutex_init(&sbi->writepages);
Sahitya Tummala87699182020-11-23 10:58:32 +05303772 init_rwsem(&sbi->cp_global_sem);
Chao Yub3582c62014-07-03 18:58:39 +08003773 init_rwsem(&sbi->node_write);
Yunlei He59c90812017-03-13 20:22:18 +08003774 init_rwsem(&sbi->node_change);
Jaegeuk Kim315df832015-08-11 12:45:39 -07003775
3776 /* disallow all the data/node/meta page writes */
3777 set_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003778 spin_lock_init(&sbi->stat_lock);
Jaegeuk Kim971767c2013-11-18 17:16:17 +09003779
Chao Yub0af6d42017-08-02 23:21:48 +08003780 /* init iostat info */
3781 spin_lock_init(&sbi->iostat_lock);
3782 sbi->iostat_enable = false;
Daeho Jeong2bc4bea2020-03-30 03:30:59 +00003783 sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
Chao Yub0af6d42017-08-02 23:21:48 +08003784
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003785 for (i = 0; i < NR_PAGE_TYPE; i++) {
Yi Zhuang5f029c02021-04-06 09:47:35 +08003786 int n = (i == META) ? 1 : NR_TEMP_TYPE;
Jaegeuk Kima912b542017-05-10 11:18:25 -07003787 int j;
3788
Kees Cookc8606592018-06-12 14:28:16 -07003789 sbi->write_io[i] =
3790 f2fs_kmalloc(sbi,
3791 array_size(n,
3792 sizeof(struct f2fs_bio_info)),
3793 GFP_KERNEL);
Christophe JAILLETb63def92017-06-11 09:21:11 +02003794 if (!sbi->write_io[i]) {
3795 err = -ENOMEM;
Chao Yu0b2103e82018-09-05 14:54:01 +08003796 goto free_bio_info;
Christophe JAILLETb63def92017-06-11 09:21:11 +02003797 }
Jaegeuk Kima912b542017-05-10 11:18:25 -07003798
3799 for (j = HOT; j < n; j++) {
3800 init_rwsem(&sbi->write_io[i][j].io_rwsem);
3801 sbi->write_io[i][j].sbi = sbi;
3802 sbi->write_io[i][j].bio = NULL;
Chao Yufb830fc2017-05-19 23:37:01 +08003803 spin_lock_init(&sbi->write_io[i][j].io_lock);
3804 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
Chao Yu0b20fce2019-09-30 18:53:25 +08003805 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
3806 init_rwsem(&sbi->write_io[i][j].bio_list_lock);
Jaegeuk Kima912b542017-05-10 11:18:25 -07003807 }
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003808 }
Jaegeuk Kim971767c2013-11-18 17:16:17 +09003809
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003810 init_rwsem(&sbi->cp_rwsem);
Jaegeuk Kimdb6ec532019-05-29 10:58:45 -07003811 init_rwsem(&sbi->quota_sem);
Changman Leefb51b5e2013-11-07 12:48:25 +09003812 init_waitqueue_head(&sbi->cp_wait);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003813 init_sb_info(sbi);
3814
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07003815 err = init_percpu_info(sbi);
3816 if (err)
Chao Yud7997e62018-01-17 16:31:35 +08003817 goto free_bio_info;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07003818
Chao Yuc72db712019-07-12 16:55:42 +08003819 if (F2FS_IO_ALIGNED(sbi)) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003820 sbi->write_io_dummy =
Chao Yua3ebfe42017-02-27 18:43:13 +08003821 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
Chao Yu1727f312017-06-12 09:44:27 +08003822 if (!sbi->write_io_dummy) {
3823 err = -ENOMEM;
Chao Yud7997e62018-01-17 16:31:35 +08003824 goto free_percpu;
Chao Yu1727f312017-06-12 09:44:27 +08003825 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08003826 }
3827
Chao Yua9991502020-02-25 18:17:10 +08003828 /* init per sbi slab cache */
3829 err = f2fs_init_xattr_caches(sbi);
3830 if (err)
3831 goto free_io_dummy;
Chao Yu31083032020-09-14 17:05:13 +08003832 err = f2fs_init_page_array_cache(sbi);
3833 if (err)
3834 goto free_xattr_cache;
Chao Yua9991502020-02-25 18:17:10 +08003835
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003836 /* get an inode for meta space */
3837 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3838 if (IS_ERR(sbi->meta_inode)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003839 f2fs_err(sbi, "Failed to read F2FS meta data inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003840 err = PTR_ERR(sbi->meta_inode);
Chao Yu31083032020-09-14 17:05:13 +08003841 goto free_page_array_cache;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003842 }
3843
Chao Yu4d57b862018-05-30 00:20:41 +08003844 err = f2fs_get_valid_checkpoint(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09003845 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003846 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003847 goto free_meta_inode;
Namjae Jeona07ef782012-12-30 14:52:05 +09003848 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003849
Chao Yuaf033b22018-09-20 20:05:00 +08003850 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3851 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08003852 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3853 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3854 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3855 }
Chao Yuaf033b22018-09-20 20:05:00 +08003856
Qiuyang Sun04f0b2e2019-06-05 11:33:25 +08003857 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3858 set_sbi_flag(sbi, SBI_NEED_FSCK);
3859
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003860 /* Initialize device list */
3861 err = f2fs_scan_devices(sbi);
3862 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003863 f2fs_err(sbi, "Failed to find devices");
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07003864 goto free_devices;
3865 }
3866
Chao Yu4c8ff702019-11-01 18:07:14 +08003867 err = f2fs_init_post_read_wq(sbi);
3868 if (err) {
3869 f2fs_err(sbi, "Failed to initialize post read workqueue");
3870 goto free_devices;
3871 }
3872
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003873 sbi->total_valid_node_count =
3874 le32_to_cpu(sbi->ckpt->valid_node_count);
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07003875 percpu_counter_set(&sbi->total_valid_inode_count,
3876 le32_to_cpu(sbi->ckpt->valid_inode_count));
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003877 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3878 sbi->total_valid_block_count =
3879 le64_to_cpu(sbi->ckpt->valid_block_count);
3880 sbi->last_valid_block_count = sbi->total_valid_block_count;
Chao Yudaeb4332017-06-26 16:24:41 +08003881 sbi->reserved_blocks = 0;
Yunlong Song80d42142017-10-27 20:45:05 +08003882 sbi->current_reserved_blocks = 0;
Jaegeuk Kim7e65be42017-12-27 15:05:52 -08003883 limit_reserve_root(sbi);
Jaegeuk Kim1ae18f72020-05-15 17:20:50 -07003884 adjust_unusable_cap_perc(sbi);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07003885
Chao Yuc227f912015-12-16 13:09:20 +08003886 for (i = 0; i < NR_INODE_TYPE; i++) {
3887 INIT_LIST_HEAD(&sbi->inode_list[i]);
3888 spin_lock_init(&sbi->inode_lock[i]);
3889 }
Chao Yu040d2bb2019-05-20 17:36:59 +08003890 mutex_init(&sbi->flush_lock);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003891
Chao Yu4d57b862018-05-30 00:20:41 +08003892 f2fs_init_extent_cache_info(sbi);
Chao Yu1dcc3362015-02-05 17:57:31 +08003893
Chao Yu4d57b862018-05-30 00:20:41 +08003894 f2fs_init_ino_entry_info(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003895
Chao Yu50fa53e2018-08-02 23:03:19 +08003896 f2fs_init_fsync_node_info(sbi);
3897
Daeho Jeong261eeb92021-01-19 09:00:42 +09003898 /* setup checkpoint request control and start checkpoint issue thread */
3899 f2fs_init_ckpt_req_control(sbi);
Chao Yu3f7070b2021-03-17 17:56:03 +08003900 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
Daeho Jeong261eeb92021-01-19 09:00:42 +09003901 test_opt(sbi, MERGE_CHECKPOINT)) {
3902 err = f2fs_start_ckpt_thread(sbi);
3903 if (err) {
3904 f2fs_err(sbi,
3905 "Failed to start F2FS issue_checkpoint_thread (%d)",
3906 err);
3907 goto stop_ckpt_thread;
3908 }
3909 }
3910
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003911 /* setup f2fs internal modules */
Chao Yu4d57b862018-05-30 00:20:41 +08003912 err = f2fs_build_segment_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09003913 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003914 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3915 err);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003916 goto free_sm;
Namjae Jeona07ef782012-12-30 14:52:05 +09003917 }
Chao Yu4d57b862018-05-30 00:20:41 +08003918 err = f2fs_build_node_manager(sbi);
Namjae Jeona07ef782012-12-30 14:52:05 +09003919 if (err) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003920 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3921 err);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003922 goto free_nm;
Namjae Jeona07ef782012-12-30 14:52:05 +09003923 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003924
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003925 /* For write statistics */
Chao Yu3a0a9cb2020-11-27 21:20:06 +08003926 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003927
3928 /* Read accumulated write IO statistics if exists */
3929 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3930 if (__exist_node_summaries(sbi))
3931 sbi->kbytes_written =
Shuoran Liub2dde6f2016-03-29 18:00:15 +08003932 le64_to_cpu(seg_i->journal->info.kbytes_written);
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08003933
Chao Yu4d57b862018-05-30 00:20:41 +08003934 f2fs_build_gc_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003935
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05303936 err = f2fs_build_stats(sbi);
3937 if (err)
3938 goto free_nm;
3939
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003940 /* get an inode for node space */
3941 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3942 if (IS_ERR(sbi->node_inode)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003943 f2fs_err(sbi, "Failed to read node inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003944 err = PTR_ERR(sbi->node_inode);
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05303945 goto free_stats;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003946 }
3947
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003948 /* read root inode and dentry */
3949 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3950 if (IS_ERR(root)) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003951 f2fs_err(sbi, "Failed to read root inode");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003952 err = PTR_ERR(root);
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05303953 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003954 }
Chao Yubcbfbd62018-06-29 00:19:25 +08003955 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3956 !root->i_size || !root->i_nlink) {
Chao Yu9d847952014-07-25 12:55:09 +08003957 iput(root);
Chao Yu8f99a942013-11-28 15:43:43 +08003958 err = -EINVAL;
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05303959 goto free_node_inode;
Chao Yu8f99a942013-11-28 15:43:43 +08003960 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003961
3962 sb->s_root = d_make_root(root); /* allocate root dentry */
3963 if (!sb->s_root) {
3964 err = -ENOMEM;
Chengguang Xu025cdb162019-01-23 15:49:44 +08003965 goto free_node_inode;
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09003966 }
3967
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07003968 err = f2fs_register_sysfs(sbi);
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09003969 if (err)
Chao Yua3981012017-06-14 17:39:46 +08003970 goto free_root_inode;
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09003971
Jaegeuk Kimea676732017-10-06 09:14:28 -07003972#ifdef CONFIG_QUOTA
Sheng Yong76cf05d2018-07-26 19:24:25 +08003973 /* Enable quota usage during mount */
Chao Yu7beb01f2018-10-24 18:34:26 +08003974 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
Jaegeuk Kimea676732017-10-06 09:14:28 -07003975 err = f2fs_enable_quotas(sb);
Jaegeuk Kim730746c2018-10-02 17:20:58 -07003976 if (err)
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003977 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
Jaegeuk Kimea676732017-10-06 09:14:28 -07003978 }
3979#endif
Chao Yu7a88ddb2020-02-27 19:30:05 +08003980 /* if there are any orphan inodes, free them */
Chao Yu4d57b862018-05-30 00:20:41 +08003981 err = f2fs_recover_orphan_inodes(sbi);
Chao Yu4b2414d2017-08-08 10:54:31 +08003982 if (err)
Jaegeuk Kimea676732017-10-06 09:14:28 -07003983 goto free_meta;
Chao Yu4b2414d2017-08-08 10:54:31 +08003984
Daniel Rosenberg43549942018-08-20 19:21:43 -07003985 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
Chao Yuaa2c8c42019-02-19 16:23:53 +08003986 goto reset_checkpoint;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003987
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09003988 /* recover fsynced data */
Chao Yua9117ec2020-02-14 17:45:11 +08003989 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
3990 !test_opt(sbi, NORECOVERY)) {
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08003991 /*
3992 * mount should be failed, when device has readonly mode, and
3993 * previous checkpoint was not done by clean system shutdown.
3994 */
Chao Yub61af312019-04-22 20:22:37 +08003995 if (f2fs_hw_is_readonly(sbi)) {
Chao Yu23738e72021-03-31 11:16:32 +08003996 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3997 err = f2fs_recover_fsync_data(sbi, true);
3998 if (err > 0) {
3999 err = -EROFS;
4000 f2fs_err(sbi, "Need to recover fsync data, but "
4001 "write access unavailable, please try "
4002 "mount w/ disable_roll_forward or norecovery");
4003 }
4004 if (err < 0)
4005 goto free_meta;
4006 }
4007 f2fs_info(sbi, "write access unavailable, skipping recovery");
Chao Yub61af312019-04-22 20:22:37 +08004008 goto reset_checkpoint;
Jaegeuk Kim081d78c2015-01-23 19:16:59 -08004009 }
Chao Yu2adc3502015-03-16 21:08:44 +08004010
4011 if (need_fsck)
4012 set_sbi_flag(sbi, SBI_NEED_FSCK);
4013
Chao Yuaa2c8c42019-02-19 16:23:53 +08004014 if (skip_recovery)
4015 goto reset_checkpoint;
Jaegeuk Kima468f0e2016-09-19 17:55:10 -07004016
Chao Yu4d57b862018-05-30 00:20:41 +08004017 err = f2fs_recover_fsync_data(sbi, false);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07004018 if (err < 0) {
Chao Yuaa2c8c42019-02-19 16:23:53 +08004019 if (err != -ENOMEM)
4020 skip_recovery = true;
Chao Yu2adc3502015-03-16 21:08:44 +08004021 need_fsck = true;
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004022 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4023 err);
Chao Yu4b2414d2017-08-08 10:54:31 +08004024 goto free_meta;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07004025 }
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07004026 } else {
Chao Yu4d57b862018-05-30 00:20:41 +08004027 err = f2fs_recover_fsync_data(sbi, true);
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07004028
4029 if (!f2fs_readonly(sb) && err > 0) {
4030 err = -EINVAL;
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004031 f2fs_err(sbi, "Need to recover fsync data");
Jaegeuk Kimea676732017-10-06 09:14:28 -07004032 goto free_meta;
Jaegeuk Kim6781eab2016-03-23 16:12:58 -07004033 }
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004034 }
Shin'ichiro Kawasakid508c942019-12-09 19:44:45 +09004035
4036 /*
4037 * If the f2fs is not readonly and fsync data recovery succeeds,
4038 * check zoned block devices' write pointer consistency.
4039 */
4040 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4041 err = f2fs_check_write_pointer(sbi);
4042 if (err)
4043 goto free_meta;
4044 }
4045
Chao Yuaa2c8c42019-02-19 16:23:53 +08004046reset_checkpoint:
Chao Yu093749e2020-08-04 21:14:49 +08004047 f2fs_init_inmem_curseg(sbi);
4048
Chao Yu4d57b862018-05-30 00:20:41 +08004049 /* f2fs_recover_fsync_data() cleared this already */
Jaegeuk Kim315df832015-08-11 12:45:39 -07004050 clear_sbi_flag(sbi, SBI_POR_DOING);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004051
Daniel Rosenberg43549942018-08-20 19:21:43 -07004052 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4053 err = f2fs_disable_checkpoint(sbi);
4054 if (err)
Jaegeuk Kim812a9592019-01-22 14:04:33 -08004055 goto sync_free_meta;
Daniel Rosenberg43549942018-08-20 19:21:43 -07004056 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4057 f2fs_enable_checkpoint(sbi);
4058 }
4059
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004060 /*
4061 * If filesystem is not mounted as read-only then
4062 * do start the gc_thread.
4063 */
Chao Yu5911d2d2021-03-27 17:57:06 +08004064 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4065 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004066 /* After POR, we can run background GC thread.*/
Chao Yu4d57b862018-05-30 00:20:41 +08004067 err = f2fs_start_gc_thread(sbi);
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004068 if (err)
Jaegeuk Kim812a9592019-01-22 14:04:33 -08004069 goto sync_free_meta;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004070 }
Jaegeuk Kim52225952018-12-13 18:38:33 -08004071 kvfree(options);
hujianyangda554e42015-05-21 14:42:53 +08004072
4073 /* recover broken superblock */
Jaegeuk Kimf2353d72016-03-23 10:42:01 -07004074 if (recovery) {
Chao Yu41214b32016-02-22 18:33:20 +08004075 err = f2fs_commit_super(sbi, true);
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004076 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4077 sbi->valid_super_block ? 1 : 2, err);
hujianyangda554e42015-05-21 14:42:53 +08004078 }
4079
Chao Yubae01ed2017-11-30 19:28:20 +08004080 f2fs_join_shrinker(sbi);
4081
Jaegeuk Kim84b89e52018-02-22 14:09:30 -08004082 f2fs_tuning_parameters(sbi);
4083
Joe Perchesdcbb4c12019-06-18 17:48:42 +08004084 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4085 cur_cp_version(F2FS_CKPT(sbi)));
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08004086 f2fs_update_time(sbi, CP_TIME);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08004087 f2fs_update_time(sbi, REQ_TIME);
Jaegeuk Kimdb610a62019-01-24 17:48:38 -08004088 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004089 return 0;
Jaegeuk Kim6437d1b2014-02-19 18:23:32 +09004090
Jaegeuk Kim812a9592019-01-22 14:04:33 -08004091sync_free_meta:
4092 /* safe to flush all the data */
4093 sync_filesystem(sbi->sb);
Chao Yuaa2c8c42019-02-19 16:23:53 +08004094 retry_cnt = 0;
Jaegeuk Kim812a9592019-01-22 14:04:33 -08004095
Chao Yu4b2414d2017-08-08 10:54:31 +08004096free_meta:
Jaegeuk Kimea676732017-10-06 09:14:28 -07004097#ifdef CONFIG_QUOTA
Sheng Yong26b5a072018-10-12 18:49:26 +08004098 f2fs_truncate_quota_inode_pages(sb);
Chao Yu7beb01f2018-10-24 18:34:26 +08004099 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
Jaegeuk Kimea676732017-10-06 09:14:28 -07004100 f2fs_quota_off_umount(sbi->sb);
4101#endif
Chao Yu4b2414d2017-08-08 10:54:31 +08004102 /*
Chao Yu4d57b862018-05-30 00:20:41 +08004103 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
Chao Yu4b2414d2017-08-08 10:54:31 +08004104 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
Chao Yu4d57b862018-05-30 00:20:41 +08004105 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4106 * falls into an infinite loop in f2fs_sync_meta_pages().
Chao Yu4b2414d2017-08-08 10:54:31 +08004107 */
4108 truncate_inode_pages_final(META_MAPPING(sbi));
Jaegeuk Kim812a9592019-01-22 14:04:33 -08004109 /* evict some inodes being cached by GC */
4110 evict_inodes(sb);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07004111 f2fs_unregister_sysfs(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004112free_root_inode:
4113 dput(sb->s_root);
4114 sb->s_root = NULL;
Chao Yubae01ed2017-11-30 19:28:20 +08004115free_node_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08004116 f2fs_release_ino_entry(sbi, true);
Chao Yubae01ed2017-11-30 19:28:20 +08004117 truncate_inode_pages_final(NODE_MAPPING(sbi));
4118 iput(sbi->node_inode);
Jaegeuk Kim7c77bf72019-01-01 00:11:30 -08004119 sbi->node_inode = NULL;
Sahitya Tummala60aa4d52018-12-26 11:20:29 +05304120free_stats:
4121 f2fs_destroy_stats(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004122free_nm:
Chao Yu4d57b862018-05-30 00:20:41 +08004123 f2fs_destroy_node_manager(sbi);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004124free_sm:
Chao Yu4d57b862018-05-30 00:20:41 +08004125 f2fs_destroy_segment_manager(sbi);
Chao Yu4c8ff702019-11-01 18:07:14 +08004126 f2fs_destroy_post_read_wq(sbi);
Daeho Jeong261eeb92021-01-19 09:00:42 +09004127stop_ckpt_thread:
4128 f2fs_stop_ckpt_thread(sbi);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07004129free_devices:
4130 destroy_device_list(sbi);
Jaegeuk Kim52225952018-12-13 18:38:33 -08004131 kvfree(sbi->ckpt);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004132free_meta_inode:
4133 make_bad_inode(sbi->meta_inode);
4134 iput(sbi->meta_inode);
Jaegeuk Kim7c77bf72019-01-01 00:11:30 -08004135 sbi->meta_inode = NULL;
Chao Yu31083032020-09-14 17:05:13 +08004136free_page_array_cache:
4137 f2fs_destroy_page_array_cache(sbi);
Chao Yua9991502020-02-25 18:17:10 +08004138free_xattr_cache:
4139 f2fs_destroy_xattr_caches(sbi);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -08004140free_io_dummy:
4141 mempool_destroy(sbi->write_io_dummy);
Chao Yud7997e62018-01-17 16:31:35 +08004142free_percpu:
4143 destroy_percpu_info(sbi);
4144free_bio_info:
Jaegeuk Kima912b542017-05-10 11:18:25 -07004145 for (i = 0; i < NR_PAGE_TYPE; i++)
Jaegeuk Kim52225952018-12-13 18:38:33 -08004146 kvfree(sbi->write_io[i]);
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07004147
4148#ifdef CONFIG_UNICODE
Daniel Rosenbergeca48732020-07-08 02:12:36 -07004149 utf8_unload(sb->s_encoding);
Hyeongseok Kim89ff6002020-11-12 18:14:54 +09004150 sb->s_encoding = NULL;
Daniel Rosenberg5aba5432019-07-23 16:05:28 -07004151#endif
Chao Yud7997e62018-01-17 16:31:35 +08004152free_options:
Chao Yu4b2414d2017-08-08 10:54:31 +08004153#ifdef CONFIG_QUOTA
4154 for (i = 0; i < MAXQUOTAS; i++)
Wang Xiaojunba87a452020-06-17 20:30:12 +08004155 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
Chao Yu4b2414d2017-08-08 10:54:31 +08004156#endif
Eric Biggersac4acb12020-09-16 21:11:35 -07004157 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
Jaegeuk Kim52225952018-12-13 18:38:33 -08004158 kvfree(options);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004159free_sb_buf:
Denis Efremov742532d2020-06-10 01:14:46 +03004160 kfree(raw_super);
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004161free_sbi:
Keith Mok43b65732016-03-02 12:04:24 -08004162 if (sbi->s_chksum_driver)
4163 crypto_free_shash(sbi->s_chksum_driver);
Denis Efremov742532d2020-06-10 01:14:46 +03004164 kfree(sbi);
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07004165
4166 /* give only one another chance */
Chao Yuaa2c8c42019-02-19 16:23:53 +08004167 if (retry_cnt > 0 && skip_recovery) {
4168 retry_cnt--;
Jaegeuk Kimed2e6212014-08-08 15:37:41 -07004169 shrink_dcache_sb(sb);
4170 goto try_onemore;
4171 }
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004172 return err;
4173}
4174
4175static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4176 const char *dev_name, void *data)
4177{
4178 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4179}
4180
Jaegeuk Kim30a55372015-01-14 16:34:24 -08004181static void kill_f2fs_super(struct super_block *sb)
4182{
Chao Yucce13252017-06-29 23:17:45 +08004183 if (sb->s_root) {
Jaegeuk Kim1cb50f82018-07-06 16:47:34 -07004184 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4185
4186 set_sbi_flag(sbi, SBI_IS_CLOSE);
4187 f2fs_stop_gc_thread(sbi);
4188 f2fs_stop_discard_thread(sbi);
4189
4190 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4191 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4192 struct cp_control cpc = {
4193 .reason = CP_UMOUNT,
4194 };
4195 f2fs_write_checkpoint(sbi, &cpc);
4196 }
Chao Yu13787522018-08-22 17:11:05 +08004197
4198 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4199 sb->s_flags &= ~SB_RDONLY;
Chao Yucce13252017-06-29 23:17:45 +08004200 }
Jaegeuk Kim30a55372015-01-14 16:34:24 -08004201 kill_block_super(sb);
4202}
4203
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004204static struct file_system_type f2fs_fs_type = {
4205 .owner = THIS_MODULE,
4206 .name = "f2fs",
4207 .mount = f2fs_mount,
Jaegeuk Kim30a55372015-01-14 16:34:24 -08004208 .kill_sb = kill_f2fs_super,
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004209 .fs_flags = FS_REQUIRES_DEV,
4210};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08004211MODULE_ALIAS_FS("f2fs");
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004212
Namjae Jeon6e6093a2013-01-17 00:08:30 +09004213static int __init init_inodecache(void)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004214{
Vladimir Davydov5d097052016-01-14 15:18:21 -08004215 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4216 sizeof(struct f2fs_inode_info), 0,
4217 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09004218 if (!f2fs_inode_cachep)
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004219 return -ENOMEM;
4220 return 0;
4221}
4222
4223static void destroy_inodecache(void)
4224{
4225 /*
4226 * Make sure all delayed rcu free inodes are flushed before we
4227 * destroy cache.
4228 */
4229 rcu_barrier();
4230 kmem_cache_destroy(f2fs_inode_cachep);
4231}
4232
4233static int __init init_f2fs_fs(void)
4234{
4235 int err;
4236
Anatoly Pugachev4071e672018-05-28 02:06:37 +03004237 if (PAGE_SIZE != F2FS_BLKSIZE) {
4238 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4239 PAGE_SIZE, F2FS_BLKSIZE);
4240 return -EINVAL;
4241 }
4242
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004243 err = init_inodecache();
4244 if (err)
4245 goto fail;
Chao Yu4d57b862018-05-30 00:20:41 +08004246 err = f2fs_create_node_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004247 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004248 goto free_inodecache;
Chao Yu4d57b862018-05-30 00:20:41 +08004249 err = f2fs_create_segment_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004250 if (err)
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004251 goto free_node_manager_caches;
Chao Yu4d57b862018-05-30 00:20:41 +08004252 err = f2fs_create_checkpoint_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004253 if (err)
Chao Yu06292072014-12-29 15:56:18 +08004254 goto free_segment_manager_caches;
Chao Yucad83c92021-05-07 18:10:38 +08004255 err = f2fs_create_recovery_cache();
Chao Yu1dcc3362015-02-05 17:57:31 +08004256 if (err)
4257 goto free_checkpoint_caches;
Chao Yucad83c92021-05-07 18:10:38 +08004258 err = f2fs_create_extent_cache();
4259 if (err)
4260 goto free_recovery_cache;
Chao Yu093749e2020-08-04 21:14:49 +08004261 err = f2fs_create_garbage_collection_cache();
Chao Yua3981012017-06-14 17:39:46 +08004262 if (err)
Chao Yu1dcc3362015-02-05 17:57:31 +08004263 goto free_extent_cache;
Chao Yu093749e2020-08-04 21:14:49 +08004264 err = f2fs_init_sysfs();
4265 if (err)
4266 goto free_garbage_collection_cache;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07004267 err = register_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimcfc4d972015-05-15 15:37:24 -07004268 if (err)
Chao Yua3981012017-06-14 17:39:46 +08004269 goto free_sysfs;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07004270 err = register_filesystem(&f2fs_fs_type);
4271 if (err)
4272 goto free_shrinker;
Greg Kroah-Hartman21acc072019-01-04 14:26:18 +01004273 f2fs_create_root_stats();
Eric Biggers6dbb1792018-04-18 11:09:48 -07004274 err = f2fs_init_post_read_processing();
4275 if (err)
4276 goto free_root_stats;
Chao Yu0b20fce2019-09-30 18:53:25 +08004277 err = f2fs_init_bio_entry_cache();
4278 if (err)
4279 goto free_post_read;
Chao Yuf5438052019-12-04 09:52:58 +08004280 err = f2fs_init_bioset();
4281 if (err)
4282 goto free_bio_enrty_cache;
Chao Yu5e6bbde2020-04-08 19:56:05 +08004283 err = f2fs_init_compress_mempool();
4284 if (err)
4285 goto free_bioset;
Chao Yuc68d6c82020-09-14 17:05:14 +08004286 err = f2fs_init_compress_cache();
4287 if (err)
4288 goto free_compress_mempool;
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004289 return 0;
Chao Yuc68d6c82020-09-14 17:05:14 +08004290free_compress_mempool:
4291 f2fs_destroy_compress_mempool();
Chao Yu5e6bbde2020-04-08 19:56:05 +08004292free_bioset:
4293 f2fs_destroy_bioset();
Chao Yuf5438052019-12-04 09:52:58 +08004294free_bio_enrty_cache:
4295 f2fs_destroy_bio_entry_cache();
Chao Yu0b20fce2019-09-30 18:53:25 +08004296free_post_read:
4297 f2fs_destroy_post_read_processing();
Eric Biggers6dbb1792018-04-18 11:09:48 -07004298free_root_stats:
4299 f2fs_destroy_root_stats();
Chao Yu787c7b8c2015-10-29 09:13:04 +08004300 unregister_filesystem(&f2fs_fs_type);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07004301free_shrinker:
4302 unregister_shrinker(&f2fs_shrinker_info);
Chao Yua3981012017-06-14 17:39:46 +08004303free_sysfs:
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07004304 f2fs_exit_sysfs();
Chao Yu093749e2020-08-04 21:14:49 +08004305free_garbage_collection_cache:
4306 f2fs_destroy_garbage_collection_cache();
Chao Yu1dcc3362015-02-05 17:57:31 +08004307free_extent_cache:
Chao Yu4d57b862018-05-30 00:20:41 +08004308 f2fs_destroy_extent_cache();
Chao Yucad83c92021-05-07 18:10:38 +08004309free_recovery_cache:
4310 f2fs_destroy_recovery_cache();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004311free_checkpoint_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08004312 f2fs_destroy_checkpoint_caches();
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09004313free_segment_manager_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08004314 f2fs_destroy_segment_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004315free_node_manager_caches:
Chao Yu4d57b862018-05-30 00:20:41 +08004316 f2fs_destroy_node_manager_caches();
Zhao Hongjiang9890ff32013-08-20 16:49:51 +08004317free_inodecache:
4318 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004319fail:
4320 return err;
4321}
4322
4323static void __exit exit_f2fs_fs(void)
4324{
Chao Yuc68d6c82020-09-14 17:05:14 +08004325 f2fs_destroy_compress_cache();
Chao Yu5e6bbde2020-04-08 19:56:05 +08004326 f2fs_destroy_compress_mempool();
Chao Yuf5438052019-12-04 09:52:58 +08004327 f2fs_destroy_bioset();
Chao Yu0b20fce2019-09-30 18:53:25 +08004328 f2fs_destroy_bio_entry_cache();
Eric Biggers6dbb1792018-04-18 11:09:48 -07004329 f2fs_destroy_post_read_processing();
Namjae Jeon4589d252013-01-15 19:58:47 +09004330 f2fs_destroy_root_stats();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004331 unregister_filesystem(&f2fs_fs_type);
Tiezhu Yangb8bef792016-05-18 08:02:25 +08004332 unregister_shrinker(&f2fs_shrinker_info);
Jaegeuk Kimdc6b2052017-07-26 11:24:13 -07004333 f2fs_exit_sysfs();
Chao Yu093749e2020-08-04 21:14:49 +08004334 f2fs_destroy_garbage_collection_cache();
Chao Yu4d57b862018-05-30 00:20:41 +08004335 f2fs_destroy_extent_cache();
Chao Yucad83c92021-05-07 18:10:38 +08004336 f2fs_destroy_recovery_cache();
Chao Yu4d57b862018-05-30 00:20:41 +08004337 f2fs_destroy_checkpoint_caches();
4338 f2fs_destroy_segment_manager_caches();
4339 f2fs_destroy_node_manager_caches();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004340 destroy_inodecache();
Jaegeuk Kimaff063e2012-11-02 17:07:47 +09004341}
4342
4343module_init(init_f2fs_fs)
4344module_exit(exit_f2fs_fs)
4345
4346MODULE_AUTHOR("Samsung Electronics's Praesto Team");
4347MODULE_DESCRIPTION("Flash Friendly File System");
4348MODULE_LICENSE("GPL");
Chao Yu0dd57172021-05-18 09:57:54 +08004349MODULE_SOFTDEP("pre: crc32");
Jaegeuk Kimb4b9d342016-11-04 14:59:15 -07004350