Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.git] / fs / f2fs / super.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
aff063e2
JK
3 * fs/f2fs/super.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
aff063e2
JK
7 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/fs.h>
c5bca38d 11#include <linux/fs_context.h>
4034247a 12#include <linux/sched/mm.h>
aff063e2 13#include <linux/statfs.h>
aff063e2
JK
14#include <linux/kthread.h>
15#include <linux/parser.h>
16#include <linux/mount.h>
17#include <linux/seq_file.h>
5e176d54 18#include <linux/proc_fs.h>
aff063e2
JK
19#include <linux/random.h>
20#include <linux/exportfs.h>
d3ee456d 21#include <linux/blkdev.h>
0abd675e 22#include <linux/quotaops.h>
aff063e2 23#include <linux/f2fs_fs.h>
b59d0bae 24#include <linux/sysfs.h>
4b2414d0 25#include <linux/quota.h>
5aba5430 26#include <linux/unicode.h>
c6a564ff 27#include <linux/part_stat.h>
3fde13f8
CY
28#include <linux/zstd.h>
29#include <linux/lz4.h>
aff063e2
JK
30
31#include "f2fs.h"
32#include "node.h"
5ec4e49f 33#include "segment.h"
aff063e2 34#include "xattr.h"
b59d0bae 35#include "gc.h"
52118743 36#include "iostat.h"
aff063e2 37
a2a4a7e4
NJ
38#define CREATE_TRACE_POINTS
39#include <trace/events/f2fs.h>
40
aff063e2
JK
41static struct kmem_cache *f2fs_inode_cachep;
42
73faec4d 43#ifdef CONFIG_F2FS_FAULT_INJECTION
2c63fead 44
19880e6e 45const char *f2fs_fault_name[FAULT_MAX] = {
c7115e09
CY
46 [FAULT_KMALLOC] = "kmalloc",
47 [FAULT_KVMALLOC] = "kvmalloc",
48 [FAULT_PAGE_ALLOC] = "page alloc",
49 [FAULT_PAGE_GET] = "page get",
e073e927 50 [FAULT_ALLOC_BIO] = "alloc bio(obsolete)",
c7115e09
CY
51 [FAULT_ALLOC_NID] = "alloc nid",
52 [FAULT_ORPHAN] = "orphan",
53 [FAULT_BLOCK] = "no more block",
54 [FAULT_DIR_DEPTH] = "too big dir depth",
55 [FAULT_EVICT_INODE] = "evict_inode fail",
56 [FAULT_TRUNCATE] = "truncate fail",
57 [FAULT_READ_IO] = "read IO error",
58 [FAULT_CHECKPOINT] = "checkpoint error",
59 [FAULT_DISCARD] = "discard error",
60 [FAULT_WRITE_IO] = "write IO error",
61 [FAULT_SLAB_ALLOC] = "slab alloc",
62 [FAULT_DQUOT_INIT] = "dquot initialize",
63 [FAULT_LOCK_OP] = "lock_op",
64 [FAULT_BLKADDR_VALIDITY] = "invalid blkaddr",
65 [FAULT_BLKADDR_CONSISTENCE] = "inconsistent blkaddr",
8b10d365 66 [FAULT_NO_SEGMENT] = "no free segment",
1788971e 67 [FAULT_INCONSISTENT_FOOTER] = "inconsistent footer",
0244c77f 68 [FAULT_TIMEOUT] = "timeout",
54ca9be0 69 [FAULT_VMALLOC] = "vmalloc",
2c63fead 70};
08796897 71
4ed886b1 72int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
2be96c21 73 unsigned long type, enum fault_option fo)
08796897 74{
63189b78 75 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1ecc0c5c 76
2be96c21
CY
77 if (fo & FAULT_ALL) {
78 memset(ffi, 0, sizeof(struct f2fs_fault_info));
79 return 0;
80 }
81
82 if (fo & FAULT_RATE) {
4ed886b1
CY
83 if (rate > INT_MAX)
84 return -EINVAL;
1ecc0c5c 85 atomic_set(&ffi->inject_ops, 0);
4ed886b1 86 ffi->inject_rate = (int)rate;
2be96c21 87 f2fs_info(sbi, "build fault injection rate: %lu", rate);
08796897 88 }
d494500a 89
2be96c21 90 if (fo & FAULT_TYPE) {
4ed886b1
CY
91 if (type >= BIT(FAULT_MAX))
92 return -EINVAL;
93 ffi->inject_type = (unsigned int)type;
2be96c21 94 f2fs_info(sbi, "build fault injection type: 0x%lx", type);
4ed886b1 95 }
d494500a 96
4ed886b1 97 return 0;
08796897 98}
73faec4d
JK
99#endif
100
2658e50d 101/* f2fs-wide shrinker description */
bfcba5ba
QZ
102static struct shrinker *f2fs_shrinker_info;
103
104static int __init f2fs_init_shrinker(void)
105{
106 f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker");
107 if (!f2fs_shrinker_info)
108 return -ENOMEM;
109
110 f2fs_shrinker_info->count_objects = f2fs_shrink_count;
111 f2fs_shrinker_info->scan_objects = f2fs_shrink_scan;
112
113 shrinker_register(f2fs_shrinker_info);
114
115 return 0;
116}
117
118static void f2fs_exit_shrinker(void)
119{
120 shrinker_free(f2fs_shrinker_info);
121}
2658e50d 122
aff063e2 123enum {
696c018c 124 Opt_gc_background,
aff063e2 125 Opt_disable_roll_forward,
2d834bf9 126 Opt_norecovery,
aff063e2 127 Opt_discard,
64058be9 128 Opt_nodiscard,
aff063e2 129 Opt_noheap,
7a20b8a6 130 Opt_heap,
4058c511 131 Opt_user_xattr,
aff063e2 132 Opt_nouser_xattr,
4058c511 133 Opt_acl,
aff063e2
JK
134 Opt_noacl,
135 Opt_active_logs,
136 Opt_disable_ext_identify,
444c580f 137 Opt_inline_xattr,
23cf7212 138 Opt_noinline_xattr,
6afc662e 139 Opt_inline_xattr_size,
8274de77 140 Opt_inline_data,
5efd3c6f 141 Opt_inline_dentry,
97c1794a 142 Opt_noinline_dentry,
6b4afdd7 143 Opt_flush_merge,
69e9e427 144 Opt_noflush_merge,
6047de54 145 Opt_barrier,
0f7b2abd 146 Opt_nobarrier,
d5053a34 147 Opt_fastboot,
89672159 148 Opt_extent_cache,
7daaea25 149 Opt_noextent_cache,
75342797 150 Opt_noinline_data,
343f40f0 151 Opt_data_flush,
7e65be49 152 Opt_reserve_root,
7c2e5963
JK
153 Opt_resgid,
154 Opt_resuid,
36abef4e 155 Opt_mode,
73faec4d 156 Opt_fault_injection,
d494500a 157 Opt_fault_type,
acff9409
JK
158 Opt_lazytime,
159 Opt_nolazytime,
4b2414d0
CY
160 Opt_quota,
161 Opt_noquota,
0abd675e
CY
162 Opt_usrquota,
163 Opt_grpquota,
5c57132e 164 Opt_prjquota,
4b2414d0
CY
165 Opt_usrjquota,
166 Opt_grpjquota,
167 Opt_prjjquota,
168 Opt_offusrjquota,
169 Opt_offgrpjquota,
170 Opt_offprjjquota,
171 Opt_jqfmt_vfsold,
172 Opt_jqfmt_vfsv0,
173 Opt_jqfmt_vfsv1,
07939627 174 Opt_alloc,
93cf93f1 175 Opt_fsync,
ff62af20 176 Opt_test_dummy_encryption,
27aacd28 177 Opt_inlinecrypt,
4d3aed70
DR
178 Opt_checkpoint_disable,
179 Opt_checkpoint_disable_cap,
180 Opt_checkpoint_disable_cap_perc,
181 Opt_checkpoint_enable,
261eeb9c
DJ
182 Opt_checkpoint_merge,
183 Opt_nocheckpoint_merge,
4c8ff709
CY
184 Opt_compress_algorithm,
185 Opt_compress_log_size,
186 Opt_compress_extension,
151b1982 187 Opt_nocompress_extension,
b28f047b 188 Opt_compress_chksum,
602a16d5 189 Opt_compress_mode,
6ce19aff 190 Opt_compress_cache,
093749e2 191 Opt_atgc,
5911d2d1
CY
192 Opt_gc_merge,
193 Opt_nogc_merge,
4f993264 194 Opt_discard_unit,
7a8fc586 195 Opt_memory_mode,
71644dff 196 Opt_age_extent_cache,
b62e71be 197 Opt_errors,
c2ecba02 198 Opt_nat_bits,
aff063e2
JK
199 Opt_err,
200};
201
202static match_table_t f2fs_tokens = {
696c018c 203 {Opt_gc_background, "background_gc=%s"},
aff063e2 204 {Opt_disable_roll_forward, "disable_roll_forward"},
2d834bf9 205 {Opt_norecovery, "norecovery"},
aff063e2 206 {Opt_discard, "discard"},
64058be9 207 {Opt_nodiscard, "nodiscard"},
aff063e2 208 {Opt_noheap, "no_heap"},
7a20b8a6 209 {Opt_heap, "heap"},
4058c511 210 {Opt_user_xattr, "user_xattr"},
aff063e2 211 {Opt_nouser_xattr, "nouser_xattr"},
4058c511 212 {Opt_acl, "acl"},
aff063e2
JK
213 {Opt_noacl, "noacl"},
214 {Opt_active_logs, "active_logs=%u"},
215 {Opt_disable_ext_identify, "disable_ext_identify"},
444c580f 216 {Opt_inline_xattr, "inline_xattr"},
23cf7212 217 {Opt_noinline_xattr, "noinline_xattr"},
6afc662e 218 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
8274de77 219 {Opt_inline_data, "inline_data"},
5efd3c6f 220 {Opt_inline_dentry, "inline_dentry"},
97c1794a 221 {Opt_noinline_dentry, "noinline_dentry"},
6b4afdd7 222 {Opt_flush_merge, "flush_merge"},
69e9e427 223 {Opt_noflush_merge, "noflush_merge"},
6047de54 224 {Opt_barrier, "barrier"},
0f7b2abd 225 {Opt_nobarrier, "nobarrier"},
d5053a34 226 {Opt_fastboot, "fastboot"},
89672159 227 {Opt_extent_cache, "extent_cache"},
7daaea25 228 {Opt_noextent_cache, "noextent_cache"},
75342797 229 {Opt_noinline_data, "noinline_data"},
343f40f0 230 {Opt_data_flush, "data_flush"},
7e65be49 231 {Opt_reserve_root, "reserve_root=%u"},
7c2e5963
JK
232 {Opt_resgid, "resgid=%u"},
233 {Opt_resuid, "resuid=%u"},
36abef4e 234 {Opt_mode, "mode=%s"},
73faec4d 235 {Opt_fault_injection, "fault_injection=%u"},
d494500a 236 {Opt_fault_type, "fault_type=%u"},
acff9409
JK
237 {Opt_lazytime, "lazytime"},
238 {Opt_nolazytime, "nolazytime"},
4b2414d0
CY
239 {Opt_quota, "quota"},
240 {Opt_noquota, "noquota"},
0abd675e
CY
241 {Opt_usrquota, "usrquota"},
242 {Opt_grpquota, "grpquota"},
5c57132e 243 {Opt_prjquota, "prjquota"},
4b2414d0
CY
244 {Opt_usrjquota, "usrjquota=%s"},
245 {Opt_grpjquota, "grpjquota=%s"},
246 {Opt_prjjquota, "prjjquota=%s"},
247 {Opt_offusrjquota, "usrjquota="},
248 {Opt_offgrpjquota, "grpjquota="},
249 {Opt_offprjjquota, "prjjquota="},
250 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
251 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
252 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
07939627 253 {Opt_alloc, "alloc_mode=%s"},
93cf93f1 254 {Opt_fsync, "fsync_mode=%s"},
ed318a6c 255 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
ff62af20 256 {Opt_test_dummy_encryption, "test_dummy_encryption"},
27aacd28 257 {Opt_inlinecrypt, "inlinecrypt"},
4d3aed70
DR
258 {Opt_checkpoint_disable, "checkpoint=disable"},
259 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
260 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
261 {Opt_checkpoint_enable, "checkpoint=enable"},
261eeb9c
DJ
262 {Opt_checkpoint_merge, "checkpoint_merge"},
263 {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
4c8ff709
CY
264 {Opt_compress_algorithm, "compress_algorithm=%s"},
265 {Opt_compress_log_size, "compress_log_size=%u"},
266 {Opt_compress_extension, "compress_extension=%s"},
151b1982 267 {Opt_nocompress_extension, "nocompress_extension=%s"},
b28f047b 268 {Opt_compress_chksum, "compress_chksum"},
602a16d5 269 {Opt_compress_mode, "compress_mode=%s"},
6ce19aff 270 {Opt_compress_cache, "compress_cache"},
093749e2 271 {Opt_atgc, "atgc"},
5911d2d1
CY
272 {Opt_gc_merge, "gc_merge"},
273 {Opt_nogc_merge, "nogc_merge"},
4f993264 274 {Opt_discard_unit, "discard_unit=%s"},
7a8fc586 275 {Opt_memory_mode, "memory=%s"},
71644dff 276 {Opt_age_extent_cache, "age_extent_cache"},
b62e71be 277 {Opt_errors, "errors=%s"},
c2ecba02 278 {Opt_nat_bits, "nat_bits"},
aff063e2
JK
279 {Opt_err, NULL},
280};
281
b1c9d3f8
CY
282void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate,
283 const char *fmt, ...)
a07ef784
NJ
284{
285 struct va_format vaf;
286 va_list args;
dcbb4c10 287 int level;
a07ef784
NJ
288
289 va_start(args, fmt);
dcbb4c10
JP
290
291 level = printk_get_level(fmt);
292 vaf.fmt = printk_skip_level(fmt);
a07ef784 293 vaf.va = &args;
b1c9d3f8
CY
294 if (limit_rate)
295 printk_ratelimited("%c%cF2FS-fs (%s): %pV\n",
296 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
297 else
298 printk("%c%cF2FS-fs (%s): %pV\n",
299 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
dcbb4c10 300
a07ef784
NJ
301 va_end(args);
302}
303
5298d4bf 304#if IS_ENABLED(CONFIG_UNICODE)
5aba5430
DR
305static const struct f2fs_sb_encodings {
306 __u16 magic;
307 char *name;
49bd03cc 308 unsigned int version;
5aba5430 309} f2fs_sb_encoding_map[] = {
49bd03cc 310 {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
5aba5430
DR
311};
312
86e80575
CH
313static const struct f2fs_sb_encodings *
314f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
5aba5430
DR
315{
316 __u16 magic = le16_to_cpu(sb->s_encoding);
317 int i;
318
319 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
320 if (magic == f2fs_sb_encoding_map[i].magic)
86e80575 321 return &f2fs_sb_encoding_map[i];
5aba5430 322
86e80575 323 return NULL;
5aba5430 324}
4d9a2bb1
CY
325
326struct kmem_cache *f2fs_cf_name_slab;
327static int __init f2fs_create_casefold_cache(void)
328{
329 f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
28add38d 330 F2FS_NAME_LEN);
870af777 331 return f2fs_cf_name_slab ? 0 : -ENOMEM;
4d9a2bb1
CY
332}
333
334static void f2fs_destroy_casefold_cache(void)
335{
336 kmem_cache_destroy(f2fs_cf_name_slab);
337}
338#else
339static int __init f2fs_create_casefold_cache(void) { return 0; }
340static void f2fs_destroy_casefold_cache(void) { }
5aba5430
DR
341#endif
342
7e65be49
JK
343static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
344{
da35fe96 345 block_t limit = min((sbi->user_block_count >> 3),
9a9aecaa 346 sbi->user_block_count - sbi->reserved_blocks);
7e65be49 347
da35fe96 348 /* limit is 12.5% */
63189b78
CY
349 if (test_opt(sbi, RESERVE_ROOT) &&
350 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
351 F2FS_OPTION(sbi).root_reserved_blocks = limit;
dcbb4c10
JP
352 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
353 F2FS_OPTION(sbi).root_reserved_blocks);
7e65be49 354 }
7c2e5963 355 if (!test_opt(sbi, RESERVE_ROOT) &&
63189b78 356 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
7c2e5963 357 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
63189b78 358 !gid_eq(F2FS_OPTION(sbi).s_resgid,
7c2e5963 359 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
dcbb4c10
JP
360 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
361 from_kuid_munged(&init_user_ns,
362 F2FS_OPTION(sbi).s_resuid),
363 from_kgid_munged(&init_user_ns,
364 F2FS_OPTION(sbi).s_resgid));
7e65be49
JK
365}
366
1ae18f71
JK
367static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
368{
369 if (!F2FS_OPTION(sbi).unusable_cap_perc)
370 return;
371
372 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
373 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
374 else
375 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
376 F2FS_OPTION(sbi).unusable_cap_perc;
377
378 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
379 F2FS_OPTION(sbi).unusable_cap,
380 F2FS_OPTION(sbi).unusable_cap_perc);
381}
382
aff063e2
JK
383static void init_once(void *foo)
384{
385 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
386
aff063e2
JK
387 inode_init_once(&fi->vfs_inode);
388}
389
4b2414d0
CY
390#ifdef CONFIG_QUOTA
391static const char * const quotatypes[] = INITQFNAMES;
392#define QTYPE2NAME(t) (quotatypes[t])
b7de231b 393static int f2fs_set_qf_name(struct f2fs_sb_info *sbi, int qtype,
4b2414d0
CY
394 substring_t *args)
395{
b7de231b 396 struct super_block *sb = sbi->sb;
4b2414d0
CY
397 char *qname;
398 int ret = -EINVAL;
399
63189b78 400 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
dcbb4c10 401 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
4b2414d0
CY
402 return -EINVAL;
403 }
7beb01f7 404 if (f2fs_sb_has_quota_ino(sbi)) {
dcbb4c10 405 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
ea676733
JK
406 return 0;
407 }
408
4b2414d0
CY
409 qname = match_strdup(args);
410 if (!qname) {
dcbb4c10 411 f2fs_err(sbi, "Not enough memory for storing quotafile name");
f365c6cc 412 return -ENOMEM;
4b2414d0 413 }
63189b78
CY
414 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
415 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
4b2414d0
CY
416 ret = 0;
417 else
dcbb4c10 418 f2fs_err(sbi, "%s quota file already specified",
4b2414d0
CY
419 QTYPE2NAME(qtype));
420 goto errout;
421 }
422 if (strchr(qname, '/')) {
dcbb4c10 423 f2fs_err(sbi, "quotafile must be on filesystem root");
4b2414d0
CY
424 goto errout;
425 }
63189b78 426 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
4b2414d0
CY
427 set_opt(sbi, QUOTA);
428 return 0;
429errout:
ba87a45c 430 kfree(qname);
4b2414d0
CY
431 return ret;
432}
433
b7de231b 434static int f2fs_clear_qf_name(struct f2fs_sb_info *sbi, int qtype)
4b2414d0 435{
b7de231b 436 struct super_block *sb = sbi->sb;
4b2414d0 437
63189b78 438 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
dcbb4c10 439 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
4b2414d0
CY
440 return -EINVAL;
441 }
ba87a45c 442 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
63189b78 443 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
4b2414d0
CY
444 return 0;
445}
446
447static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
448{
449 /*
450 * We do the test below only for project quotas. 'usrquota' and
451 * 'grpquota' mount options are allowed even without quota feature
452 * to support legacy quotas in quota files.
453 */
7beb01f7 454 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
dcbb4c10 455 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
4b2414d0
CY
456 return -1;
457 }
63189b78
CY
458 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
459 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
460 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
461 if (test_opt(sbi, USRQUOTA) &&
462 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
4b2414d0
CY
463 clear_opt(sbi, USRQUOTA);
464
63189b78
CY
465 if (test_opt(sbi, GRPQUOTA) &&
466 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
4b2414d0
CY
467 clear_opt(sbi, GRPQUOTA);
468
63189b78
CY
469 if (test_opt(sbi, PRJQUOTA) &&
470 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4b2414d0
CY
471 clear_opt(sbi, PRJQUOTA);
472
473 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
474 test_opt(sbi, PRJQUOTA)) {
dcbb4c10 475 f2fs_err(sbi, "old and new quota format mixing");
4b2414d0
CY
476 return -1;
477 }
478
63189b78 479 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
dcbb4c10 480 f2fs_err(sbi, "journaled quota format not specified");
4b2414d0
CY
481 return -1;
482 }
483 }
ea676733 484
7beb01f7 485 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
dcbb4c10 486 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
63189b78 487 F2FS_OPTION(sbi).s_jquota_fmt = 0;
ea676733 488 }
4b2414d0
CY
489 return 0;
490}
491#endif
492
0edcb219 493static int f2fs_set_test_dummy_encryption(struct f2fs_sb_info *sbi,
ed318a6c
EB
494 const char *opt,
495 const substring_t *arg,
496 bool is_remount)
497{
c5bca38d
EB
498 struct fs_parameter param = {
499 .type = fs_value_is_string,
500 .string = arg->from ? arg->from : "",
501 };
502 struct fscrypt_dummy_policy *policy =
503 &F2FS_OPTION(sbi).dummy_enc_policy;
ed318a6c
EB
504 int err;
505
c5bca38d
EB
506 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
507 f2fs_warn(sbi, "test_dummy_encryption option not supported");
508 return -EINVAL;
509 }
510
ed318a6c
EB
511 if (!f2fs_sb_has_encrypt(sbi)) {
512 f2fs_err(sbi, "Encrypt feature is off");
513 return -EINVAL;
514 }
515
516 /*
517 * This mount option is just for testing, and it's not worthwhile to
518 * implement the extra complexity (e.g. RCU protection) that would be
519 * needed to allow it to be set or changed during remount. We do allow
520 * it to be specified during remount, but only if there is no change.
521 */
c5bca38d 522 if (is_remount && !fscrypt_is_dummy_policy_set(policy)) {
ed318a6c
EB
523 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
524 return -EINVAL;
525 }
c5bca38d
EB
526
527 err = fscrypt_parse_test_dummy_encryption(&param, policy);
ed318a6c
EB
528 if (err) {
529 if (err == -EEXIST)
530 f2fs_warn(sbi,
531 "Can't change test_dummy_encryption on remount");
532 else if (err == -EINVAL)
533 f2fs_warn(sbi, "Value of option \"%s\" is unrecognized",
534 opt);
535 else
536 f2fs_warn(sbi, "Error processing option \"%s\" [%d]",
537 opt, err);
538 return -EINVAL;
539 }
540 f2fs_warn(sbi, "Test dummy encryption mode enabled");
64e3ed0b 541 return 0;
ed318a6c
EB
542}
543
3fde13f8 544#ifdef CONFIG_F2FS_FS_COMPRESSION
7e1b150f
CY
545static bool is_compress_extension_exist(struct f2fs_sb_info *sbi,
546 const char *new_ext, bool is_ext)
547{
548 unsigned char (*ext)[F2FS_EXTENSION_LEN];
549 int ext_cnt;
550 int i;
551
552 if (is_ext) {
553 ext = F2FS_OPTION(sbi).extensions;
554 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
555 } else {
556 ext = F2FS_OPTION(sbi).noextensions;
557 ext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
558 }
559
560 for (i = 0; i < ext_cnt; i++) {
561 if (!strcasecmp(new_ext, ext[i]))
562 return true;
563 }
564
565 return false;
566}
567
151b1982
FC
568/*
569 * 1. The same extension name cannot not appear in both compress and non-compress extension
570 * at the same time.
571 * 2. If the compress extension specifies all files, the types specified by the non-compress
572 * extension will be treated as special cases and will not be compressed.
573 * 3. Don't allow the non-compress extension specifies all files.
574 */
575static int f2fs_test_compress_extension(struct f2fs_sb_info *sbi)
576{
577 unsigned char (*ext)[F2FS_EXTENSION_LEN];
578 unsigned char (*noext)[F2FS_EXTENSION_LEN];
579 int ext_cnt, noext_cnt, index = 0, no_index = 0;
580
581 ext = F2FS_OPTION(sbi).extensions;
582 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
583 noext = F2FS_OPTION(sbi).noextensions;
584 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
585
586 if (!noext_cnt)
587 return 0;
588
589 for (no_index = 0; no_index < noext_cnt; no_index++) {
590 if (!strcasecmp("*", noext[no_index])) {
591 f2fs_info(sbi, "Don't allow the nocompress extension specifies all files");
592 return -EINVAL;
593 }
594 for (index = 0; index < ext_cnt; index++) {
595 if (!strcasecmp(ext[index], noext[no_index])) {
596 f2fs_info(sbi, "Don't allow the same extension %s appear in both compress and nocompress extension",
597 ext[index]);
598 return -EINVAL;
599 }
600 }
601 }
602 return 0;
603}
604
3fde13f8
CY
605#ifdef CONFIG_F2FS_FS_LZ4
606static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
607{
608#ifdef CONFIG_F2FS_FS_LZ4HC
609 unsigned int level;
3fde13f8
CY
610
611 if (strlen(str) == 3) {
091a4dfb 612 F2FS_OPTION(sbi).compress_level = 0;
3fde13f8
CY
613 return 0;
614 }
615
3fde13f8
CY
616 str += 3;
617
618 if (str[0] != ':') {
619 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
620 return -EINVAL;
621 }
622 if (kstrtouint(str + 1, 10, &level))
623 return -EINVAL;
624
c571fbb5 625 if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
3fde13f8
CY
626 f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
627 return -EINVAL;
628 }
629
630 F2FS_OPTION(sbi).compress_level = level;
631 return 0;
632#else
00e120b5
JK
633 if (strlen(str) == 3) {
634 F2FS_OPTION(sbi).compress_level = 0;
635 return 0;
636 }
3fde13f8
CY
637 f2fs_info(sbi, "kernel doesn't support lz4hc compression");
638 return -EINVAL;
639#endif
640}
641#endif
642
643#ifdef CONFIG_F2FS_FS_ZSTD
644static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
645{
e39602da 646 int level;
3fde13f8
CY
647 int len = 4;
648
649 if (strlen(str) == len) {
00e120b5 650 F2FS_OPTION(sbi).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
3fde13f8
CY
651 return 0;
652 }
653
654 str += len;
655
656 if (str[0] != ':') {
657 f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
658 return -EINVAL;
659 }
e39602da 660 if (kstrtoint(str + 1, 10, &level))
3fde13f8
CY
661 return -EINVAL;
662
e39602da
CY
663 /* f2fs does not support negative compress level now */
664 if (level < 0) {
665 f2fs_info(sbi, "do not support negative compress level: %d", level);
666 return -ERANGE;
667 }
668
c571fbb5 669 if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
3fde13f8
CY
670 f2fs_info(sbi, "invalid zstd compress level: %d", level);
671 return -EINVAL;
672 }
673
674 F2FS_OPTION(sbi).compress_level = level;
675 return 0;
676}
677#endif
678#endif
679
71e9bd3d 680static int parse_options(struct f2fs_sb_info *sbi, char *options, bool is_remount)
696c018c 681{
696c018c 682 substring_t args[MAX_OPT_ARGS];
1f0b067b 683#ifdef CONFIG_F2FS_FS_COMPRESSION
4c8ff709 684 unsigned char (*ext)[F2FS_EXTENSION_LEN];
151b1982
FC
685 unsigned char (*noext)[F2FS_EXTENSION_LEN];
686 int ext_cnt, noext_cnt;
1f0b067b 687#endif
696c018c 688 char *p, *name;
1f0b067b 689 int arg = 0;
7c2e5963
JK
690 kuid_t uid;
691 kgid_t gid;
4b2414d0 692 int ret;
696c018c
NJ
693
694 if (!options)
abd0e040 695 return 0;
696c018c
NJ
696
697 while ((p = strsep(&options, ",")) != NULL) {
698 int token;
5f029c04 699
696c018c
NJ
700 if (!*p)
701 continue;
702 /*
703 * Initialize args struct so we know whether arg was
704 * found; some options take optional arguments.
705 */
706 args[0].to = args[0].from = NULL;
707 token = match_token(p, f2fs_tokens, args);
708
709 switch (token) {
710 case Opt_gc_background:
711 name = match_strdup(&args[0]);
712
713 if (!name)
714 return -ENOMEM;
3c57f751 715 if (!strcmp(name, "on")) {
bbbc34fd 716 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
3c57f751 717 } else if (!strcmp(name, "off")) {
5062b5be
DJ
718 if (f2fs_sb_has_blkzoned(sbi)) {
719 f2fs_warn(sbi, "zoned devices need bggc");
720 kfree(name);
721 return -EINVAL;
722 }
bbbc34fd 723 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
3c57f751 724 } else if (!strcmp(name, "sync")) {
bbbc34fd 725 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
6aefd93b 726 } else {
ba87a45c 727 kfree(name);
696c018c
NJ
728 return -EINVAL;
729 }
ba87a45c 730 kfree(name);
696c018c
NJ
731 break;
732 case Opt_disable_roll_forward:
733 set_opt(sbi, DISABLE_ROLL_FORWARD);
734 break;
2d834bf9 735 case Opt_norecovery:
9cca4987 736 /* requires ro mount, checked in f2fs_default_check */
a9117eca 737 set_opt(sbi, NORECOVERY);
2d834bf9 738 break;
696c018c 739 case Opt_discard:
f7db8dd6
CY
740 if (!f2fs_hw_support_discard(sbi)) {
741 f2fs_warn(sbi, "device does not support discard");
742 break;
743 }
7d20c8ab 744 set_opt(sbi, DISCARD);
696c018c 745 break;
64058be9 746 case Opt_nodiscard:
f7db8dd6 747 if (f2fs_hw_should_discard(sbi)) {
dcbb4c10 748 f2fs_warn(sbi, "discard is required for zoned block devices");
96ba2dec
DLM
749 return -EINVAL;
750 }
64058be9 751 clear_opt(sbi, DISCARD);
487df616 752 break;
696c018c 753 case Opt_noheap:
7a20b8a6 754 case Opt_heap:
4e0197f9 755 f2fs_warn(sbi, "heap/no_heap options were deprecated");
7a20b8a6 756 break;
696c018c 757#ifdef CONFIG_F2FS_FS_XATTR
4058c511
KA
758 case Opt_user_xattr:
759 set_opt(sbi, XATTR_USER);
760 break;
696c018c
NJ
761 case Opt_nouser_xattr:
762 clear_opt(sbi, XATTR_USER);
763 break;
444c580f
JK
764 case Opt_inline_xattr:
765 set_opt(sbi, INLINE_XATTR);
766 break;
23cf7212
CY
767 case Opt_noinline_xattr:
768 clear_opt(sbi, INLINE_XATTR);
769 break;
6afc662e
CY
770 case Opt_inline_xattr_size:
771 if (args->from && match_int(args, &arg))
772 return -EINVAL;
773 set_opt(sbi, INLINE_XATTR_SIZE);
63189b78 774 F2FS_OPTION(sbi).inline_xattr_size = arg;
6afc662e 775 break;
696c018c 776#else
4058c511 777 case Opt_user_xattr:
696c018c 778 case Opt_nouser_xattr:
444c580f 779 case Opt_inline_xattr:
23cf7212 780 case Opt_noinline_xattr:
277352b6
ES
781 case Opt_inline_xattr_size:
782 f2fs_info(sbi, "xattr options not supported");
23cf7212 783 break;
696c018c
NJ
784#endif
785#ifdef CONFIG_F2FS_FS_POSIX_ACL
4058c511
KA
786 case Opt_acl:
787 set_opt(sbi, POSIX_ACL);
788 break;
696c018c
NJ
789 case Opt_noacl:
790 clear_opt(sbi, POSIX_ACL);
791 break;
792#else
4058c511 793 case Opt_acl:
696c018c 794 case Opt_noacl:
277352b6 795 f2fs_info(sbi, "acl options not supported");
696c018c
NJ
796 break;
797#endif
798 case Opt_active_logs:
799 if (args->from && match_int(args, &arg))
800 return -EINVAL;
d0b9e42a
CY
801 if (arg != 2 && arg != 4 &&
802 arg != NR_CURSEG_PERSIST_TYPE)
696c018c 803 return -EINVAL;
63189b78 804 F2FS_OPTION(sbi).active_logs = arg;
696c018c
NJ
805 break;
806 case Opt_disable_ext_identify:
807 set_opt(sbi, DISABLE_EXT_IDENTIFY);
808 break;
8274de77
HL
809 case Opt_inline_data:
810 set_opt(sbi, INLINE_DATA);
811 break;
5efd3c6f
CY
812 case Opt_inline_dentry:
813 set_opt(sbi, INLINE_DENTRY);
814 break;
97c1794a
CY
815 case Opt_noinline_dentry:
816 clear_opt(sbi, INLINE_DENTRY);
817 break;
6b4afdd7
JK
818 case Opt_flush_merge:
819 set_opt(sbi, FLUSH_MERGE);
820 break;
69e9e427
JK
821 case Opt_noflush_merge:
822 clear_opt(sbi, FLUSH_MERGE);
823 break;
0f7b2abd
JK
824 case Opt_nobarrier:
825 set_opt(sbi, NOBARRIER);
826 break;
6047de54
YL
827 case Opt_barrier:
828 clear_opt(sbi, NOBARRIER);
829 break;
d5053a34
JK
830 case Opt_fastboot:
831 set_opt(sbi, FASTBOOT);
832 break;
89672159 833 case Opt_extent_cache:
12607c1b 834 set_opt(sbi, READ_EXTENT_CACHE);
89672159 835 break;
7daaea25 836 case Opt_noextent_cache:
64ee7503 837 if (f2fs_sb_has_device_alias(sbi)) {
128d333f
DJ
838 f2fs_err(sbi, "device aliasing requires extent cache");
839 return -EINVAL;
840 }
12607c1b 841 clear_opt(sbi, READ_EXTENT_CACHE);
7daaea25 842 break;
75342797
WL
843 case Opt_noinline_data:
844 clear_opt(sbi, INLINE_DATA);
845 break;
343f40f0
CY
846 case Opt_data_flush:
847 set_opt(sbi, DATA_FLUSH);
848 break;
7e65be49
JK
849 case Opt_reserve_root:
850 if (args->from && match_int(args, &arg))
851 return -EINVAL;
852 if (test_opt(sbi, RESERVE_ROOT)) {
dcbb4c10
JP
853 f2fs_info(sbi, "Preserve previous reserve_root=%u",
854 F2FS_OPTION(sbi).root_reserved_blocks);
7e65be49 855 } else {
63189b78 856 F2FS_OPTION(sbi).root_reserved_blocks = arg;
7e65be49
JK
857 set_opt(sbi, RESERVE_ROOT);
858 }
859 break;
7c2e5963
JK
860 case Opt_resuid:
861 if (args->from && match_int(args, &arg))
862 return -EINVAL;
863 uid = make_kuid(current_user_ns(), arg);
864 if (!uid_valid(uid)) {
dcbb4c10 865 f2fs_err(sbi, "Invalid uid value %d", arg);
7c2e5963
JK
866 return -EINVAL;
867 }
63189b78 868 F2FS_OPTION(sbi).s_resuid = uid;
7c2e5963
JK
869 break;
870 case Opt_resgid:
871 if (args->from && match_int(args, &arg))
872 return -EINVAL;
873 gid = make_kgid(current_user_ns(), arg);
874 if (!gid_valid(gid)) {
dcbb4c10 875 f2fs_err(sbi, "Invalid gid value %d", arg);
7c2e5963
JK
876 return -EINVAL;
877 }
63189b78 878 F2FS_OPTION(sbi).s_resgid = gid;
7c2e5963 879 break;
36abef4e
JK
880 case Opt_mode:
881 name = match_strdup(&args[0]);
882
883 if (!name)
884 return -ENOMEM;
3c57f751 885 if (!strcmp(name, "adaptive")) {
b0332a0f 886 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
3c57f751 887 } else if (!strcmp(name, "lfs")) {
b0332a0f 888 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
6691d940
DJ
889 } else if (!strcmp(name, "fragment:segment")) {
890 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
891 } else if (!strcmp(name, "fragment:block")) {
892 F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
36abef4e 893 } else {
ba87a45c 894 kfree(name);
36abef4e
JK
895 return -EINVAL;
896 }
ba87a45c 897 kfree(name);
36abef4e 898 break;
4cb037ec 899#ifdef CONFIG_F2FS_FAULT_INJECTION
73faec4d
JK
900 case Opt_fault_injection:
901 if (args->from && match_int(args, &arg))
902 return -EINVAL;
2be96c21 903 if (f2fs_build_fault_attr(sbi, arg, 0, FAULT_RATE))
4ed886b1 904 return -EINVAL;
d494500a 905 set_opt(sbi, FAULT_INJECTION);
d494500a 906 break;
4cb037ec 907
d494500a
CY
908 case Opt_fault_type:
909 if (args->from && match_int(args, &arg))
910 return -EINVAL;
2be96c21 911 if (f2fs_build_fault_attr(sbi, 0, arg, FAULT_TYPE))
4ed886b1 912 return -EINVAL;
0cc0dec2 913 set_opt(sbi, FAULT_INJECTION);
4cb037ec 914 break;
73faec4d 915#else
4cb037ec 916 case Opt_fault_injection:
4cb037ec 917 case Opt_fault_type:
277352b6 918 f2fs_info(sbi, "fault injection options not supported");
4cb037ec
CX
919 break;
920#endif
acff9409 921 case Opt_lazytime:
9100adf3 922 set_opt(sbi, LAZYTIME);
acff9409
JK
923 break;
924 case Opt_nolazytime:
9100adf3 925 clear_opt(sbi, LAZYTIME);
acff9409 926 break;
0abd675e 927#ifdef CONFIG_QUOTA
4b2414d0 928 case Opt_quota:
0abd675e
CY
929 case Opt_usrquota:
930 set_opt(sbi, USRQUOTA);
931 break;
932 case Opt_grpquota:
933 set_opt(sbi, GRPQUOTA);
934 break;
5c57132e
CY
935 case Opt_prjquota:
936 set_opt(sbi, PRJQUOTA);
937 break;
4b2414d0 938 case Opt_usrjquota:
b7de231b 939 ret = f2fs_set_qf_name(sbi, USRQUOTA, &args[0]);
4b2414d0
CY
940 if (ret)
941 return ret;
942 break;
943 case Opt_grpjquota:
b7de231b 944 ret = f2fs_set_qf_name(sbi, GRPQUOTA, &args[0]);
4b2414d0
CY
945 if (ret)
946 return ret;
947 break;
948 case Opt_prjjquota:
b7de231b 949 ret = f2fs_set_qf_name(sbi, PRJQUOTA, &args[0]);
4b2414d0
CY
950 if (ret)
951 return ret;
952 break;
953 case Opt_offusrjquota:
b7de231b 954 ret = f2fs_clear_qf_name(sbi, USRQUOTA);
4b2414d0
CY
955 if (ret)
956 return ret;
957 break;
958 case Opt_offgrpjquota:
b7de231b 959 ret = f2fs_clear_qf_name(sbi, GRPQUOTA);
4b2414d0
CY
960 if (ret)
961 return ret;
962 break;
963 case Opt_offprjjquota:
b7de231b 964 ret = f2fs_clear_qf_name(sbi, PRJQUOTA);
4b2414d0
CY
965 if (ret)
966 return ret;
967 break;
968 case Opt_jqfmt_vfsold:
63189b78 969 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
4b2414d0
CY
970 break;
971 case Opt_jqfmt_vfsv0:
63189b78 972 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
4b2414d0
CY
973 break;
974 case Opt_jqfmt_vfsv1:
63189b78 975 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
4b2414d0
CY
976 break;
977 case Opt_noquota:
978 clear_opt(sbi, QUOTA);
979 clear_opt(sbi, USRQUOTA);
980 clear_opt(sbi, GRPQUOTA);
981 clear_opt(sbi, PRJQUOTA);
982 break;
0abd675e 983#else
4b2414d0 984 case Opt_quota:
0abd675e
CY
985 case Opt_usrquota:
986 case Opt_grpquota:
5c57132e 987 case Opt_prjquota:
4b2414d0
CY
988 case Opt_usrjquota:
989 case Opt_grpjquota:
990 case Opt_prjjquota:
991 case Opt_offusrjquota:
992 case Opt_offgrpjquota:
993 case Opt_offprjjquota:
994 case Opt_jqfmt_vfsold:
995 case Opt_jqfmt_vfsv0:
996 case Opt_jqfmt_vfsv1:
997 case Opt_noquota:
dcbb4c10 998 f2fs_info(sbi, "quota operations not supported");
0abd675e
CY
999 break;
1000#endif
07939627
JK
1001 case Opt_alloc:
1002 name = match_strdup(&args[0]);
1003 if (!name)
1004 return -ENOMEM;
1005
3c57f751 1006 if (!strcmp(name, "default")) {
63189b78 1007 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
3c57f751 1008 } else if (!strcmp(name, "reuse")) {
63189b78 1009 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
07939627 1010 } else {
ba87a45c 1011 kfree(name);
07939627
JK
1012 return -EINVAL;
1013 }
ba87a45c 1014 kfree(name);
07939627 1015 break;
93cf93f1
JZ
1016 case Opt_fsync:
1017 name = match_strdup(&args[0]);
1018 if (!name)
1019 return -ENOMEM;
3c57f751 1020 if (!strcmp(name, "posix")) {
63189b78 1021 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
3c57f751 1022 } else if (!strcmp(name, "strict")) {
63189b78 1023 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
3c57f751 1024 } else if (!strcmp(name, "nobarrier")) {
d6290814
JK
1025 F2FS_OPTION(sbi).fsync_mode =
1026 FSYNC_MODE_NOBARRIER;
93cf93f1 1027 } else {
ba87a45c 1028 kfree(name);
93cf93f1
JZ
1029 return -EINVAL;
1030 }
ba87a45c 1031 kfree(name);
93cf93f1 1032 break;
ff62af20 1033 case Opt_test_dummy_encryption:
0edcb219 1034 ret = f2fs_set_test_dummy_encryption(sbi, p, &args[0],
ed318a6c
EB
1035 is_remount);
1036 if (ret)
1037 return ret;
ff62af20 1038 break;
27aacd28
ST
1039 case Opt_inlinecrypt:
1040#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
7d6ee503 1041 set_opt(sbi, INLINECRYPT);
27aacd28
ST
1042#else
1043 f2fs_info(sbi, "inline encryption not supported");
1044#endif
1045 break;
4d3aed70
DR
1046 case Opt_checkpoint_disable_cap_perc:
1047 if (args->from && match_int(args, &arg))
4354994f 1048 return -EINVAL;
4d3aed70
DR
1049 if (arg < 0 || arg > 100)
1050 return -EINVAL;
1ae18f71 1051 F2FS_OPTION(sbi).unusable_cap_perc = arg;
4d3aed70
DR
1052 set_opt(sbi, DISABLE_CHECKPOINT);
1053 break;
1054 case Opt_checkpoint_disable_cap:
1055 if (args->from && match_int(args, &arg))
1056 return -EINVAL;
1057 F2FS_OPTION(sbi).unusable_cap = arg;
1058 set_opt(sbi, DISABLE_CHECKPOINT);
1059 break;
1060 case Opt_checkpoint_disable:
1061 set_opt(sbi, DISABLE_CHECKPOINT);
1062 break;
1063 case Opt_checkpoint_enable:
1064 clear_opt(sbi, DISABLE_CHECKPOINT);
4354994f 1065 break;
261eeb9c
DJ
1066 case Opt_checkpoint_merge:
1067 set_opt(sbi, MERGE_CHECKPOINT);
1068 break;
1069 case Opt_nocheckpoint_merge:
1070 clear_opt(sbi, MERGE_CHECKPOINT);
1071 break;
1f0b067b 1072#ifdef CONFIG_F2FS_FS_COMPRESSION
4c8ff709
CY
1073 case Opt_compress_algorithm:
1074 if (!f2fs_sb_has_compression(sbi)) {
69c0dd29
CY
1075 f2fs_info(sbi, "Image doesn't support compression");
1076 break;
4c8ff709
CY
1077 }
1078 name = match_strdup(&args[0]);
1079 if (!name)
1080 return -ENOMEM;
3c57f751 1081 if (!strcmp(name, "lzo")) {
32be0e97 1082#ifdef CONFIG_F2FS_FS_LZO
3fde13f8 1083 F2FS_OPTION(sbi).compress_level = 0;
4c8ff709
CY
1084 F2FS_OPTION(sbi).compress_algorithm =
1085 COMPRESS_LZO;
32be0e97
CY
1086#else
1087 f2fs_info(sbi, "kernel doesn't support lzo compression");
1088#endif
3fde13f8 1089 } else if (!strncmp(name, "lz4", 3)) {
32be0e97 1090#ifdef CONFIG_F2FS_FS_LZ4
3fde13f8
CY
1091 ret = f2fs_set_lz4hc_level(sbi, name);
1092 if (ret) {
1093 kfree(name);
1094 return -EINVAL;
1095 }
4c8ff709
CY
1096 F2FS_OPTION(sbi).compress_algorithm =
1097 COMPRESS_LZ4;
32be0e97
CY
1098#else
1099 f2fs_info(sbi, "kernel doesn't support lz4 compression");
1100#endif
3fde13f8 1101 } else if (!strncmp(name, "zstd", 4)) {
32be0e97 1102#ifdef CONFIG_F2FS_FS_ZSTD
3fde13f8
CY
1103 ret = f2fs_set_zstd_level(sbi, name);
1104 if (ret) {
1105 kfree(name);
1106 return -EINVAL;
1107 }
50cfa66f
CY
1108 F2FS_OPTION(sbi).compress_algorithm =
1109 COMPRESS_ZSTD;
32be0e97
CY
1110#else
1111 f2fs_info(sbi, "kernel doesn't support zstd compression");
1112#endif
6d92b201 1113 } else if (!strcmp(name, "lzo-rle")) {
32be0e97 1114#ifdef CONFIG_F2FS_FS_LZORLE
3fde13f8 1115 F2FS_OPTION(sbi).compress_level = 0;
6d92b201
CY
1116 F2FS_OPTION(sbi).compress_algorithm =
1117 COMPRESS_LZORLE;
32be0e97
CY
1118#else
1119 f2fs_info(sbi, "kernel doesn't support lzorle compression");
1120#endif
4c8ff709
CY
1121 } else {
1122 kfree(name);
1123 return -EINVAL;
1124 }
1125 kfree(name);
1126 break;
1127 case Opt_compress_log_size:
1128 if (!f2fs_sb_has_compression(sbi)) {
69c0dd29
CY
1129 f2fs_info(sbi, "Image doesn't support compression");
1130 break;
4c8ff709
CY
1131 }
1132 if (args->from && match_int(args, &arg))
1133 return -EINVAL;
1134 if (arg < MIN_COMPRESS_LOG_SIZE ||
1135 arg > MAX_COMPRESS_LOG_SIZE) {
1136 f2fs_err(sbi,
1137 "Compress cluster log size is out of range");
1138 return -EINVAL;
1139 }
1140 F2FS_OPTION(sbi).compress_log_size = arg;
1141 break;
1142 case Opt_compress_extension:
1143 if (!f2fs_sb_has_compression(sbi)) {
69c0dd29
CY
1144 f2fs_info(sbi, "Image doesn't support compression");
1145 break;
4c8ff709
CY
1146 }
1147 name = match_strdup(&args[0]);
1148 if (!name)
1149 return -ENOMEM;
1150
1151 ext = F2FS_OPTION(sbi).extensions;
1152 ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
1153
1154 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1155 ext_cnt >= COMPRESS_EXT_NUM) {
1156 f2fs_err(sbi,
1157 "invalid extension length/number");
1158 kfree(name);
1159 return -EINVAL;
1160 }
1161
7e1b150f
CY
1162 if (is_compress_extension_exist(sbi, name, true)) {
1163 kfree(name);
1164 break;
1165 }
1166
789ca0eb
DY
1167 ret = strscpy(ext[ext_cnt], name);
1168 if (ret < 0) {
1169 kfree(name);
1170 return ret;
1171 }
4c8ff709
CY
1172 F2FS_OPTION(sbi).compress_ext_cnt++;
1173 kfree(name);
1174 break;
151b1982
FC
1175 case Opt_nocompress_extension:
1176 if (!f2fs_sb_has_compression(sbi)) {
1177 f2fs_info(sbi, "Image doesn't support compression");
1178 break;
1179 }
1180 name = match_strdup(&args[0]);
1181 if (!name)
1182 return -ENOMEM;
1183
1184 noext = F2FS_OPTION(sbi).noextensions;
1185 noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
1186
1187 if (strlen(name) >= F2FS_EXTENSION_LEN ||
1188 noext_cnt >= COMPRESS_EXT_NUM) {
1189 f2fs_err(sbi,
1190 "invalid extension length/number");
1191 kfree(name);
1192 return -EINVAL;
1193 }
1194
7e1b150f
CY
1195 if (is_compress_extension_exist(sbi, name, false)) {
1196 kfree(name);
1197 break;
1198 }
1199
789ca0eb
DY
1200 ret = strscpy(noext[noext_cnt], name);
1201 if (ret < 0) {
1202 kfree(name);
1203 return ret;
1204 }
151b1982
FC
1205 F2FS_OPTION(sbi).nocompress_ext_cnt++;
1206 kfree(name);
1207 break;
b28f047b 1208 case Opt_compress_chksum:
d4998b78
YL
1209 if (!f2fs_sb_has_compression(sbi)) {
1210 f2fs_info(sbi, "Image doesn't support compression");
1211 break;
1212 }
b28f047b
CY
1213 F2FS_OPTION(sbi).compress_chksum = true;
1214 break;
602a16d5 1215 case Opt_compress_mode:
d4998b78
YL
1216 if (!f2fs_sb_has_compression(sbi)) {
1217 f2fs_info(sbi, "Image doesn't support compression");
1218 break;
1219 }
602a16d5
DJ
1220 name = match_strdup(&args[0]);
1221 if (!name)
1222 return -ENOMEM;
1223 if (!strcmp(name, "fs")) {
1224 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1225 } else if (!strcmp(name, "user")) {
1226 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1227 } else {
1228 kfree(name);
1229 return -EINVAL;
1230 }
1231 kfree(name);
1232 break;
6ce19aff 1233 case Opt_compress_cache:
d4998b78
YL
1234 if (!f2fs_sb_has_compression(sbi)) {
1235 f2fs_info(sbi, "Image doesn't support compression");
1236 break;
1237 }
6ce19aff
CY
1238 set_opt(sbi, COMPRESS_CACHE);
1239 break;
1f0b067b
CY
1240#else
1241 case Opt_compress_algorithm:
1242 case Opt_compress_log_size:
1243 case Opt_compress_extension:
151b1982 1244 case Opt_nocompress_extension:
b28f047b 1245 case Opt_compress_chksum:
602a16d5 1246 case Opt_compress_mode:
6ce19aff 1247 case Opt_compress_cache:
1f0b067b
CY
1248 f2fs_info(sbi, "compression options not supported");
1249 break;
1250#endif
093749e2
CY
1251 case Opt_atgc:
1252 set_opt(sbi, ATGC);
1253 break;
5911d2d1
CY
1254 case Opt_gc_merge:
1255 set_opt(sbi, GC_MERGE);
1256 break;
1257 case Opt_nogc_merge:
1258 clear_opt(sbi, GC_MERGE);
1259 break;
4f993264
CY
1260 case Opt_discard_unit:
1261 name = match_strdup(&args[0]);
1262 if (!name)
1263 return -ENOMEM;
1264 if (!strcmp(name, "block")) {
1265 F2FS_OPTION(sbi).discard_unit =
1266 DISCARD_UNIT_BLOCK;
1267 } else if (!strcmp(name, "segment")) {
1268 F2FS_OPTION(sbi).discard_unit =
1269 DISCARD_UNIT_SEGMENT;
1270 } else if (!strcmp(name, "section")) {
1271 F2FS_OPTION(sbi).discard_unit =
1272 DISCARD_UNIT_SECTION;
1273 } else {
1274 kfree(name);
1275 return -EINVAL;
1276 }
1277 kfree(name);
1278 break;
7a8fc586
DJ
1279 case Opt_memory_mode:
1280 name = match_strdup(&args[0]);
1281 if (!name)
1282 return -ENOMEM;
1283 if (!strcmp(name, "normal")) {
1284 F2FS_OPTION(sbi).memory_mode =
1285 MEMORY_MODE_NORMAL;
1286 } else if (!strcmp(name, "low")) {
1287 F2FS_OPTION(sbi).memory_mode =
1288 MEMORY_MODE_LOW;
1289 } else {
1290 kfree(name);
1291 return -EINVAL;
1292 }
1293 kfree(name);
1294 break;
71644dff
JK
1295 case Opt_age_extent_cache:
1296 set_opt(sbi, AGE_EXTENT_CACHE);
1297 break;
b62e71be
CY
1298 case Opt_errors:
1299 name = match_strdup(&args[0]);
1300 if (!name)
1301 return -ENOMEM;
1302 if (!strcmp(name, "remount-ro")) {
1303 F2FS_OPTION(sbi).errors =
1304 MOUNT_ERRORS_READONLY;
1305 } else if (!strcmp(name, "continue")) {
1306 F2FS_OPTION(sbi).errors =
1307 MOUNT_ERRORS_CONTINUE;
1308 } else if (!strcmp(name, "panic")) {
1309 F2FS_OPTION(sbi).errors =
1310 MOUNT_ERRORS_PANIC;
1311 } else {
1312 kfree(name);
1313 return -EINVAL;
1314 }
1315 kfree(name);
1316 break;
c2ecba02
CY
1317 case Opt_nat_bits:
1318 set_opt(sbi, NAT_BITS);
1319 break;
696c018c 1320 default:
dcbb4c10
JP
1321 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
1322 p);
696c018c
NJ
1323 return -EINVAL;
1324 }
1325 }
abd0e040
ES
1326 return 0;
1327}
1328
1329static int f2fs_default_check(struct f2fs_sb_info *sbi)
1330{
4b2414d0
CY
1331#ifdef CONFIG_QUOTA
1332 if (f2fs_check_quota_options(sbi))
1333 return -EINVAL;
00960c2c 1334#else
7beb01f7 1335 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
dcbb4c10 1336 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
00960c2c
SY
1337 return -EINVAL;
1338 }
7beb01f7 1339 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
dcbb4c10 1340 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
4ddc1b28
CY
1341 return -EINVAL;
1342 }
4b2414d0 1343#endif
28add38d
GKB
1344
1345 if (!IS_ENABLED(CONFIG_UNICODE) && f2fs_sb_has_casefold(sbi)) {
5aba5430
DR
1346 f2fs_err(sbi,
1347 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1348 return -EINVAL;
1349 }
28add38d 1350
d0660122
CY
1351 /*
1352 * The BLKZONED feature indicates that the drive was formatted with
1353 * zone alignment optimization. This is optional for host-aware
1354 * devices, but mandatory for host-managed zoned block devices.
1355 */
4f993264 1356 if (f2fs_sb_has_blkzoned(sbi)) {
b5a711ac 1357#ifdef CONFIG_BLK_DEV_ZONED
4f993264
CY
1358 if (F2FS_OPTION(sbi).discard_unit !=
1359 DISCARD_UNIT_SECTION) {
1360 f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
1361 F2FS_OPTION(sbi).discard_unit =
1362 DISCARD_UNIT_SECTION;
1363 }
2bd4df8f
CG
1364
1365 if (F2FS_OPTION(sbi).fs_mode != FS_MODE_LFS) {
1366 f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
1367 return -EINVAL;
1368 }
b5a711ac
YL
1369#else
1370 f2fs_err(sbi, "Zoned block device support is not enabled");
1371 return -EINVAL;
1372#endif
4f993264 1373 }
ec91538d 1374
151b1982
FC
1375#ifdef CONFIG_F2FS_FS_COMPRESSION
1376 if (f2fs_test_compress_extension(sbi)) {
1377 f2fs_err(sbi, "invalid compress or nocompress extension");
1378 return -EINVAL;
1379 }
1380#endif
1381
6afc662e 1382 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
70db5b04
JK
1383 int min_size, max_size;
1384
7beb01f7
CY
1385 if (!f2fs_sb_has_extra_attr(sbi) ||
1386 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
dcbb4c10 1387 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
4d817ae0
CY
1388 return -EINVAL;
1389 }
6afc662e 1390 if (!test_opt(sbi, INLINE_XATTR)) {
dcbb4c10 1391 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
6afc662e
CY
1392 return -EINVAL;
1393 }
70db5b04 1394
dde38c03 1395 min_size = MIN_INLINE_XATTR_SIZE;
dd6c89b5 1396 max_size = MAX_INLINE_XATTR_SIZE;
70db5b04
JK
1397
1398 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
1399 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
dcbb4c10
JP
1400 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
1401 min_size, max_size);
6afc662e
CY
1402 return -EINVAL;
1403 }
1404 }
0cdd3195 1405
80dc113a 1406 if (test_opt(sbi, ATGC) && f2fs_lfs_mode(sbi)) {
c5bf8348 1407 f2fs_err(sbi, "LFS is not compatible with ATGC");
80dc113a
JK
1408 return -EINVAL;
1409 }
1410
ed8ac22b 1411 if (f2fs_is_readonly(sbi) && test_opt(sbi, FLUSH_MERGE)) {
967eaad1
YL
1412 f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
1413 return -EINVAL;
1414 }
1415
a7d9fe3c
JK
1416 if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1417 f2fs_err(sbi, "Allow to mount readonly mode only");
1418 return -EROFS;
1419 }
9cca4987
ES
1420
1421 if (test_opt(sbi, NORECOVERY) && !f2fs_readonly(sbi->sb)) {
1422 f2fs_err(sbi, "norecovery requires readonly mount");
1423 return -EINVAL;
1424 }
1425
696c018c
NJ
1426 return 0;
1427}
1428
aff063e2
JK
1429static struct inode *f2fs_alloc_inode(struct super_block *sb)
1430{
1431 struct f2fs_inode_info *fi;
1432
c40e15a9 1433 if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
65d3af64 1434 return NULL;
65d3af64
MS
1435
1436 fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
aff063e2
JK
1437 if (!fi)
1438 return NULL;
1439
1440 init_once((void *) fi);
1441
434720fa 1442 /* Initialize f2fs-specific inode info */
204706c7 1443 atomic_set(&fi->dirty_pages, 0);
c2759eba 1444 atomic_set(&fi->i_compr_blocks, 0);
e4544b63 1445 init_f2fs_rwsem(&fi->i_sem);
c10c9820 1446 spin_lock_init(&fi->i_size_lock);
2710fd7e 1447 INIT_LIST_HEAD(&fi->dirty_list);
0f18b462 1448 INIT_LIST_HEAD(&fi->gdirty_list);
ef0c333c 1449 INIT_LIST_HEAD(&fi->gdonate_list);
e4544b63
TM
1450 init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1451 init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1452 init_f2fs_rwsem(&fi->i_xattr_sem);
aff063e2 1453
ab9fa662
JK
1454 /* Will be used by directory only */
1455 fi->i_dir_level = F2FS_SB(sb)->dir_level;
f2470371 1456
aff063e2
JK
1457 return &fi->vfs_inode;
1458}
1459
531ad7d5
JK
1460static int f2fs_drop_inode(struct inode *inode)
1461{
a8933b6b 1462 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
b8d96a30 1463 int ret;
a8933b6b
CY
1464
1465 /*
1466 * during filesystem shutdown, if checkpoint is disabled,
1467 * drop useless meta/node dirty pages.
1468 */
1469 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1470 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1471 inode->i_ino == F2FS_META_INO(sbi)) {
1472 trace_f2fs_drop_inode(inode, 1);
1473 return 1;
1474 }
1475 }
1476
531ad7d5
JK
1477 /*
1478 * This is to avoid a deadlock condition like below.
1479 * writeback_single_inode(inode)
1480 * - f2fs_write_data_page
1481 * - f2fs_gc -> iput -> evict
1482 * - inode_wait_for_writeback(inode)
1483 */
0f18b462 1484 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
06e1bc05 1485 if (!inode->i_nlink && !is_bad_inode(inode)) {
3e72f721
JK
1486 /* to avoid evict_inode call simultaneously */
1487 atomic_inc(&inode->i_count);
06e1bc05
JK
1488 spin_unlock(&inode->i_lock);
1489
3e72f721
JK
1490 /* should remain fi->extent_tree for writepage */
1491 f2fs_destroy_extent_node(inode);
1492
06e1bc05 1493 sb_start_intwrite(inode->i_sb);
fc9581c8 1494 f2fs_i_size_write(inode, 0);
06e1bc05 1495
a0770e13 1496 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
1497 inode, NULL, 0, DATA);
1498 truncate_inode_pages_final(inode->i_mapping);
1499
06e1bc05 1500 if (F2FS_HAS_BLOCKS(inode))
9a449e9c 1501 f2fs_truncate(inode);
06e1bc05
JK
1502
1503 sb_end_intwrite(inode->i_sb);
1504
06e1bc05 1505 spin_lock(&inode->i_lock);
3e72f721 1506 atomic_dec(&inode->i_count);
06e1bc05 1507 }
b8d96a30 1508 trace_f2fs_drop_inode(inode, 0);
531ad7d5 1509 return 0;
06e1bc05 1510 }
b8d96a30 1511 ret = generic_drop_inode(inode);
8ce589c7
EB
1512 if (!ret)
1513 ret = fscrypt_drop_inode(inode);
b8d96a30
HP
1514 trace_f2fs_drop_inode(inode, ret);
1515 return ret;
531ad7d5
JK
1516}
1517
7c45729a 1518int f2fs_inode_dirtied(struct inode *inode, bool sync)
b3783873 1519{
0f18b462 1520 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
7c45729a 1521 int ret = 0;
0f18b462 1522
0f18b462
JK
1523 spin_lock(&sbi->inode_lock[DIRTY_META]);
1524 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
7c45729a
JK
1525 ret = 1;
1526 } else {
1527 set_inode_flag(inode, FI_DIRTY_INODE);
1528 stat_inc_dirty_inode(sbi, DIRTY_META);
0f18b462 1529 }
7c45729a
JK
1530 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
1531 list_add_tail(&F2FS_I(inode)->gdirty_list,
0f18b462 1532 &sbi->inode_list[DIRTY_META]);
7c45729a
JK
1533 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1534 }
338bbfa0 1535 spin_unlock(&sbi->inode_lock[DIRTY_META]);
f098aeba 1536
db03c20c
CY
1537 /* if atomic write is not committed, set inode w/ atomic dirty */
1538 if (!ret && f2fs_is_atomic_file(inode) &&
1539 !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
f098aeba
YG
1540 set_inode_flag(inode, FI_ATOMIC_DIRTIED);
1541
7c45729a 1542 return ret;
0f18b462
JK
1543}
1544
1545void f2fs_inode_synced(struct inode *inode)
1546{
1547 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1548
1549 spin_lock(&sbi->inode_lock[DIRTY_META]);
1550 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1551 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1552 return;
1553 }
7c45729a
JK
1554 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1555 list_del_init(&F2FS_I(inode)->gdirty_list);
1556 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1557 }
0f18b462 1558 clear_inode_flag(inode, FI_DIRTY_INODE);
26de9b11 1559 clear_inode_flag(inode, FI_AUTO_RECOVER);
0f18b462 1560 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
338bbfa0 1561 spin_unlock(&sbi->inode_lock[DIRTY_META]);
b3783873
JK
1562}
1563
b56ab837
JK
1564/*
1565 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1566 *
1567 * We should call set_dirty_inode to write the dirty inode through write_inode.
1568 */
1569static void f2fs_dirty_inode(struct inode *inode, int flags)
1570{
1571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1572
1573 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1574 inode->i_ino == F2FS_META_INO(sbi))
1575 return;
1576
b56ab837
JK
1577 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1578 clear_inode_flag(inode, FI_AUTO_RECOVER);
1579
7c45729a 1580 f2fs_inode_dirtied(inode, false);
b56ab837
JK
1581}
1582
d01718a0 1583static void f2fs_free_inode(struct inode *inode)
aff063e2 1584{
2c58d548 1585 fscrypt_free_inode(inode);
aff063e2
JK
1586 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1587}
1588
523be8a6
JK
1589static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1590{
513c5f37 1591 percpu_counter_destroy(&sbi->total_valid_inode_count);
47c8ebcc
JK
1592 percpu_counter_destroy(&sbi->rf_node_block_count);
1593 percpu_counter_destroy(&sbi->alloc_valid_block_count);
523be8a6
JK
1594}
1595
3c62be17
JK
1596static void destroy_device_list(struct f2fs_sb_info *sbi)
1597{
1598 int i;
1599
1600 for (i = 0; i < sbi->s_ndevs; i++) {
51bf8d3c 1601 if (i > 0)
22650a99 1602 bdev_fput(FDEV(i).bdev_file);
3c62be17 1603#ifdef CONFIG_BLK_DEV_ZONED
95175daf 1604 kvfree(FDEV(i).blkz_seq);
3c62be17
JK
1605#endif
1606 }
5222595d 1607 kvfree(sbi->devs);
3c62be17
JK
1608}
1609
aff063e2
JK
1610static void f2fs_put_super(struct super_block *sb)
1611{
1612 struct f2fs_sb_info *sbi = F2FS_SB(sb);
a398101a 1613 int i;
20872584 1614 int err = 0;
b1c5ef26 1615 bool done;
aff063e2 1616
99c787cf
LG
1617 /* unregister procfs/sysfs entries in advance to avoid race case */
1618 f2fs_unregister_sysfs(sbi);
1619
0abd675e 1620 f2fs_quota_off_umount(sb);
aff063e2 1621
2658e50d
JK
1622 /* prevent remaining shrinker jobs */
1623 mutex_lock(&sbi->umount_mutex);
1624
261eeb9c
DJ
1625 /*
1626 * flush all issued checkpoints and stop checkpoint issue thread.
1627 * after then, all checkpoints should be done by each process context.
1628 */
1629 f2fs_stop_ckpt_thread(sbi);
1630
85dc2f2c
JK
1631 /*
1632 * We don't need to do checkpoint when superblock is clean.
1633 * But, the previous checkpoint was not done by umount, it needs to do
1634 * clean checkpoint again.
1635 */
4354994f
DR
1636 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1637 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
75ab4cb8
JK
1638 struct cp_control cpc = {
1639 .reason = CP_UMOUNT,
1640 };
eb61c2cc 1641 stat_inc_cp_call_count(sbi, TOTAL_CALL);
20872584 1642 err = f2fs_write_checkpoint(sbi, &cpc);
75ab4cb8 1643 }
aff063e2 1644
4e6a8d9b 1645 /* be sure to wait for any on-going discard commands */
b1c5ef26
YL
1646 done = f2fs_issue_discard_timeout(sbi);
1647 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1f43e2ad
CY
1648 struct cp_control cpc = {
1649 .reason = CP_UMOUNT | CP_TRIMMED,
1650 };
eb61c2cc 1651 stat_inc_cp_call_count(sbi, TOTAL_CALL);
20872584 1652 err = f2fs_write_checkpoint(sbi, &cpc);
1f43e2ad
CY
1653 }
1654
cf779cab
JK
1655 /*
1656 * normally superblock is clean, so we need to release this.
1657 * In addition, EIO will skip do checkpoint, we need this as well.
1658 */
4d57b86d 1659 f2fs_release_ino_entry(sbi, true);
6f12ac25 1660
2658e50d
JK
1661 f2fs_leave_shrinker(sbi);
1662 mutex_unlock(&sbi->umount_mutex);
1663
17c19120 1664 /* our cp_error case, we can wait for any writeback page */
b9109b0e 1665 f2fs_flush_merged_writes(sbi);
17c19120 1666
bf22c3cc 1667 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
50fa53ec 1668
a4639380 1669 if (err || f2fs_cp_error(sbi)) {
20872584
CY
1670 truncate_inode_pages_final(NODE_MAPPING(sbi));
1671 truncate_inode_pages_final(META_MAPPING(sbi));
1672 }
1673
1674 for (i = 0; i < NR_COUNT_TYPE; i++) {
1675 if (!get_pages(sbi, i))
1676 continue;
1677 f2fs_err(sbi, "detect filesystem reference count leak during "
1678 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1679 f2fs_bug_on(sbi, 1);
1680 }
1681
50fa53ec
CY
1682 f2fs_bug_on(sbi, sbi->fsync_node_num);
1683
6ce19aff
CY
1684 f2fs_destroy_compress_inode(sbi);
1685
aff063e2 1686 iput(sbi->node_inode);
7c77bf7d
JK
1687 sbi->node_inode = NULL;
1688
aff063e2 1689 iput(sbi->meta_inode);
7c77bf7d 1690 sbi->meta_inode = NULL;
aff063e2 1691
60aa4d55
ST
1692 /*
1693 * iput() can update stat information, if f2fs_write_checkpoint()
1694 * above failed with error.
1695 */
1696 f2fs_destroy_stats(sbi);
1697
aff063e2 1698 /* destroy f2fs internal modules */
4d57b86d
CY
1699 f2fs_destroy_node_manager(sbi);
1700 f2fs_destroy_segment_manager(sbi);
aff063e2 1701
b62e71be
CY
1702 /* flush s_error_work before sbi destroy */
1703 flush_work(&sbi->s_error_work);
1704
4c8ff709
CY
1705 f2fs_destroy_post_read_wq(sbi);
1706
5222595d 1707 kvfree(sbi->ckpt);
a398101a 1708
742532d1 1709 kfree(sbi->raw_super);
523be8a6 1710
31083031 1711 f2fs_destroy_page_array_cache(sbi);
a999150f 1712 f2fs_destroy_xattr_caches(sbi);
4b2414d0
CY
1713#ifdef CONFIG_QUOTA
1714 for (i = 0; i < MAXQUOTAS; i++)
ba87a45c 1715 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4b2414d0 1716#endif
ac4acb1f 1717 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
523be8a6 1718 destroy_percpu_info(sbi);
a4b68176 1719 f2fs_destroy_iostat(sbi);
a912b54d 1720 for (i = 0; i < NR_PAGE_TYPE; i++)
5222595d 1721 kvfree(sbi->write_io[i]);
5298d4bf 1722#if IS_ENABLED(CONFIG_UNICODE)
eca4873e 1723 utf8_unload(sb->s_encoding);
5aba5430 1724#endif
aff063e2
JK
1725}
1726
1727int f2fs_sync_fs(struct super_block *sb, int sync)
1728{
1729 struct f2fs_sb_info *sbi = F2FS_SB(sb);
c34f42e2 1730 int err = 0;
aff063e2 1731
1f227a3e
JK
1732 if (unlikely(f2fs_cp_error(sbi)))
1733 return 0;
4354994f
DR
1734 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1735 return 0;
1f227a3e 1736
a2a4a7e4
NJ
1737 trace_f2fs_sync_fs(sb, sync);
1738
4b2414d0
CY
1739 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1740 return -EAGAIN;
1741
eb61c2cc
CY
1742 if (sync) {
1743 stat_inc_cp_call_count(sbi, TOTAL_CALL);
261eeb9c 1744 err = f2fs_issue_checkpoint(sbi);
eb61c2cc 1745 }
119ee914 1746
c34f42e2 1747 return err;
aff063e2
JK
1748}
1749
d6212a5f
CL
1750static int f2fs_freeze(struct super_block *sb)
1751{
eb85c241
CY
1752 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1753
77888c1e 1754 if (f2fs_readonly(sb))
d6212a5f
CL
1755 return 0;
1756
b4b9d34c 1757 /* IO error happened before */
eb85c241 1758 if (unlikely(f2fs_cp_error(sbi)))
b4b9d34c
JK
1759 return -EIO;
1760
1761 /* must be clean, since sync_filesystem() was already called */
eb85c241 1762 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY))
b4b9d34c 1763 return -EINVAL;
d50dfc0c 1764
eb85c241
CY
1765 sbi->umount_lock_holder = current;
1766
c7b58576 1767 /* Let's flush checkpoints and stop the thread. */
eb85c241
CY
1768 f2fs_flush_ckpt_thread(sbi);
1769
1770 sbi->umount_lock_holder = NULL;
ba900534
JK
1771
1772 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
eb85c241 1773 set_sbi_flag(sbi, SBI_IS_FREEZING);
b4b9d34c 1774 return 0;
d6212a5f
CL
1775}
1776
1777static int f2fs_unfreeze(struct super_block *sb)
1778{
bc8aeb04
CY
1779 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1780
1781 /*
1782 * It will update discard_max_bytes of mounted lvm device to zero
1783 * after creating snapshot on this lvm device, let's drop all
1784 * remained discards.
1785 * We don't need to disable real-time discard because discard_max_bytes
1786 * will recover after removal of snapshot.
1787 */
1788 if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi))
1789 f2fs_issue_discard_timeout(sbi);
1790
ba900534 1791 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
d6212a5f
CL
1792 return 0;
1793}
1794
ddc34e32
CY
1795#ifdef CONFIG_QUOTA
1796static int f2fs_statfs_project(struct super_block *sb,
1797 kprojid_t projid, struct kstatfs *buf)
1798{
1799 struct kqid qid;
1800 struct dquot *dquot;
1801 u64 limit;
1802 u64 curblock;
1803
1804 qid = make_kqid_projid(projid);
1805 dquot = dqget(sb, qid);
1806 if (IS_ERR(dquot))
1807 return PTR_ERR(dquot);
955ac6e5 1808 spin_lock(&dquot->dq_dqb_lock);
ddc34e32 1809
bf2cbd3c
CX
1810 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1811 dquot->dq_dqb.dqb_bhardlimit);
a9201960
CY
1812 limit >>= sb->s_blocksize_bits;
1813
1814 if (limit) {
1815 uint64_t remaining = 0;
909110c0 1816
baaa7ebf
KK
1817 curblock = (dquot->dq_dqb.dqb_curspace +
1818 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
a9201960
CY
1819 if (limit > curblock)
1820 remaining = limit - curblock;
1821
1822 buf->f_blocks = min(buf->f_blocks, limit);
1823 buf->f_bfree = min(buf->f_bfree, remaining);
1824 buf->f_bavail = min(buf->f_bavail, remaining);
ddc34e32
CY
1825 }
1826
bf2cbd3c
CX
1827 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1828 dquot->dq_dqb.dqb_ihardlimit);
909110c0 1829
a9201960
CY
1830 if (limit) {
1831 uint64_t remaining = 0;
1832
1833 if (limit > dquot->dq_dqb.dqb_curinodes)
1834 remaining = limit - dquot->dq_dqb.dqb_curinodes;
1835
1836 buf->f_files = min(buf->f_files, limit);
1837 buf->f_ffree = min(buf->f_ffree, remaining);
ddc34e32
CY
1838 }
1839
955ac6e5 1840 spin_unlock(&dquot->dq_dqb_lock);
ddc34e32
CY
1841 dqput(dquot);
1842 return 0;
1843}
1844#endif
1845
aff063e2
JK
1846static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1847{
1848 struct super_block *sb = dentry->d_sb;
1849 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1850 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
f66c027e 1851 block_t total_count, user_block_count, start_count;
0cc091d0 1852 u64 avail_node_count;
4de85145 1853 unsigned int total_valid_node_count;
aff063e2
JK
1854
1855 total_count = le64_to_cpu(sbi->raw_super->block_count);
aff063e2 1856 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
aff063e2
JK
1857 buf->f_type = F2FS_SUPER_MAGIC;
1858 buf->f_bsize = sbi->blocksize;
1859
1860 buf->f_blocks = total_count - start_count;
4de85145
ND
1861
1862 spin_lock(&sbi->stat_lock);
d7b549de
DJ
1863 if (sbi->carve_out)
1864 buf->f_blocks -= sbi->current_reserved_blocks;
4de85145
ND
1865 user_block_count = sbi->user_block_count;
1866 total_valid_node_count = valid_node_count(sbi);
1867 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
f66c027e 1868 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
80d42145 1869 sbi->current_reserved_blocks;
c9c8ed50 1870
4354994f
DR
1871 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1872 buf->f_bfree = 0;
1873 else
1874 buf->f_bfree -= sbi->unusable_block_count;
c9c8ed50 1875 spin_unlock(&sbi->stat_lock);
4354994f 1876
63189b78
CY
1877 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1878 buf->f_bavail = buf->f_bfree -
1879 F2FS_OPTION(sbi).root_reserved_blocks;
7e65be49
JK
1880 else
1881 buf->f_bavail = 0;
aff063e2 1882
0cc091d0
JK
1883 if (avail_node_count > user_block_count) {
1884 buf->f_files = user_block_count;
1885 buf->f_ffree = buf->f_bavail;
1886 } else {
1887 buf->f_files = avail_node_count;
4de85145 1888 buf->f_ffree = min(avail_node_count - total_valid_node_count,
0cc091d0
JK
1889 buf->f_bavail);
1890 }
aff063e2 1891
5a20d339 1892 buf->f_namelen = F2FS_NAME_LEN;
6d1349c7 1893 buf->f_fsid = u64_to_fsid(id);
aff063e2 1894
ddc34e32 1895#ifdef CONFIG_QUOTA
a6c397a3 1896 if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) &&
ddc34e32 1897 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
a6c397a3 1898 f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf);
ddc34e32
CY
1899 }
1900#endif
aff063e2
JK
1901 return 0;
1902}
1903
4b2414d0
CY
1904static inline void f2fs_show_quota_options(struct seq_file *seq,
1905 struct super_block *sb)
1906{
1907#ifdef CONFIG_QUOTA
1908 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1909
63189b78 1910 if (F2FS_OPTION(sbi).s_jquota_fmt) {
4b2414d0
CY
1911 char *fmtname = "";
1912
63189b78 1913 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
4b2414d0
CY
1914 case QFMT_VFS_OLD:
1915 fmtname = "vfsold";
1916 break;
1917 case QFMT_VFS_V0:
1918 fmtname = "vfsv0";
1919 break;
1920 case QFMT_VFS_V1:
1921 fmtname = "vfsv1";
1922 break;
1923 }
1924 seq_printf(seq, ",jqfmt=%s", fmtname);
1925 }
1926
63189b78
CY
1927 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1928 seq_show_option(seq, "usrjquota",
1929 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
4b2414d0 1930
63189b78
CY
1931 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1932 seq_show_option(seq, "grpjquota",
1933 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
4b2414d0 1934
63189b78
CY
1935 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1936 seq_show_option(seq, "prjjquota",
1937 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
4b2414d0
CY
1938#endif
1939}
1940
cd6ee739 1941#ifdef CONFIG_F2FS_FS_COMPRESSION
4c8ff709
CY
1942static inline void f2fs_show_compress_options(struct seq_file *seq,
1943 struct super_block *sb)
1944{
1945 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1946 char *algtype = "";
1947 int i;
1948
1949 if (!f2fs_sb_has_compression(sbi))
1950 return;
1951
1952 switch (F2FS_OPTION(sbi).compress_algorithm) {
1953 case COMPRESS_LZO:
1954 algtype = "lzo";
1955 break;
1956 case COMPRESS_LZ4:
1957 algtype = "lz4";
1958 break;
50cfa66f
CY
1959 case COMPRESS_ZSTD:
1960 algtype = "zstd";
1961 break;
6d92b201
CY
1962 case COMPRESS_LZORLE:
1963 algtype = "lzo-rle";
1964 break;
4c8ff709
CY
1965 }
1966 seq_printf(seq, ",compress_algorithm=%s", algtype);
1967
3fde13f8
CY
1968 if (F2FS_OPTION(sbi).compress_level)
1969 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1970
4c8ff709
CY
1971 seq_printf(seq, ",compress_log_size=%u",
1972 F2FS_OPTION(sbi).compress_log_size);
1973
1974 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1975 seq_printf(seq, ",compress_extension=%s",
1976 F2FS_OPTION(sbi).extensions[i]);
1977 }
b28f047b 1978
151b1982
FC
1979 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1980 seq_printf(seq, ",nocompress_extension=%s",
1981 F2FS_OPTION(sbi).noextensions[i]);
1982 }
1983
b28f047b
CY
1984 if (F2FS_OPTION(sbi).compress_chksum)
1985 seq_puts(seq, ",compress_chksum");
602a16d5
DJ
1986
1987 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1988 seq_printf(seq, ",compress_mode=%s", "fs");
1989 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1990 seq_printf(seq, ",compress_mode=%s", "user");
6ce19aff
CY
1991
1992 if (test_opt(sbi, COMPRESS_CACHE))
1993 seq_puts(seq, ",compress_cache");
4c8ff709 1994}
cd6ee739 1995#endif
4c8ff709 1996
aff063e2
JK
1997static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1998{
1999 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
2000
bbbc34fd
CY
2001 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
2002 seq_printf(seq, ",background_gc=%s", "sync");
2003 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
2004 seq_printf(seq, ",background_gc=%s", "on");
2005 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
696c018c 2006 seq_printf(seq, ",background_gc=%s", "off");
bbbc34fd 2007
5911d2d1
CY
2008 if (test_opt(sbi, GC_MERGE))
2009 seq_puts(seq, ",gc_merge");
04d7a7ae
YL
2010 else
2011 seq_puts(seq, ",nogc_merge");
5911d2d1 2012
aff063e2
JK
2013 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2014 seq_puts(seq, ",disable_roll_forward");
a9117eca
CY
2015 if (test_opt(sbi, NORECOVERY))
2016 seq_puts(seq, ",norecovery");
2381a685 2017 if (test_opt(sbi, DISCARD)) {
aff063e2 2018 seq_puts(seq, ",discard");
2381a685
YL
2019 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
2020 seq_printf(seq, ",discard_unit=%s", "block");
2021 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2022 seq_printf(seq, ",discard_unit=%s", "segment");
2023 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2024 seq_printf(seq, ",discard_unit=%s", "section");
2025 } else {
81621f97 2026 seq_puts(seq, ",nodiscard");
2381a685 2027 }
aff063e2
JK
2028#ifdef CONFIG_F2FS_FS_XATTR
2029 if (test_opt(sbi, XATTR_USER))
2030 seq_puts(seq, ",user_xattr");
2031 else
2032 seq_puts(seq, ",nouser_xattr");
444c580f
JK
2033 if (test_opt(sbi, INLINE_XATTR))
2034 seq_puts(seq, ",inline_xattr");
23cf7212
CY
2035 else
2036 seq_puts(seq, ",noinline_xattr");
6afc662e
CY
2037 if (test_opt(sbi, INLINE_XATTR_SIZE))
2038 seq_printf(seq, ",inline_xattr_size=%u",
63189b78 2039 F2FS_OPTION(sbi).inline_xattr_size);
aff063e2
JK
2040#endif
2041#ifdef CONFIG_F2FS_FS_POSIX_ACL
2042 if (test_opt(sbi, POSIX_ACL))
2043 seq_puts(seq, ",acl");
2044 else
2045 seq_puts(seq, ",noacl");
2046#endif
2047 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
aa43507f 2048 seq_puts(seq, ",disable_ext_identify");
8274de77
HL
2049 if (test_opt(sbi, INLINE_DATA))
2050 seq_puts(seq, ",inline_data");
75342797
WL
2051 else
2052 seq_puts(seq, ",noinline_data");
5efd3c6f
CY
2053 if (test_opt(sbi, INLINE_DENTRY))
2054 seq_puts(seq, ",inline_dentry");
97c1794a
CY
2055 else
2056 seq_puts(seq, ",noinline_dentry");
967eaad1 2057 if (test_opt(sbi, FLUSH_MERGE))
6b4afdd7 2058 seq_puts(seq, ",flush_merge");
967eaad1
YL
2059 else
2060 seq_puts(seq, ",noflush_merge");
0f7b2abd
JK
2061 if (test_opt(sbi, NOBARRIER))
2062 seq_puts(seq, ",nobarrier");
6047de54
YL
2063 else
2064 seq_puts(seq, ",barrier");
d5053a34
JK
2065 if (test_opt(sbi, FASTBOOT))
2066 seq_puts(seq, ",fastboot");
12607c1b 2067 if (test_opt(sbi, READ_EXTENT_CACHE))
89672159 2068 seq_puts(seq, ",extent_cache");
7daaea25
JK
2069 else
2070 seq_puts(seq, ",noextent_cache");
71644dff
JK
2071 if (test_opt(sbi, AGE_EXTENT_CACHE))
2072 seq_puts(seq, ",age_extent_cache");
343f40f0
CY
2073 if (test_opt(sbi, DATA_FLUSH))
2074 seq_puts(seq, ",data_flush");
36abef4e
JK
2075
2076 seq_puts(seq, ",mode=");
b0332a0f 2077 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
36abef4e 2078 seq_puts(seq, "adaptive");
b0332a0f 2079 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
36abef4e 2080 seq_puts(seq, "lfs");
6691d940
DJ
2081 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2082 seq_puts(seq, "fragment:segment");
2083 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2084 seq_puts(seq, "fragment:block");
63189b78 2085 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
7e65be49 2086 if (test_opt(sbi, RESERVE_ROOT))
7c2e5963 2087 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
63189b78
CY
2088 F2FS_OPTION(sbi).root_reserved_blocks,
2089 from_kuid_munged(&init_user_ns,
2090 F2FS_OPTION(sbi).s_resuid),
2091 from_kgid_munged(&init_user_ns,
2092 F2FS_OPTION(sbi).s_resgid));
0cc0dec2 2093#ifdef CONFIG_F2FS_FAULT_INJECTION
d494500a 2094 if (test_opt(sbi, FAULT_INJECTION)) {
44529f89 2095 seq_printf(seq, ",fault_injection=%u",
63189b78 2096 F2FS_OPTION(sbi).fault_info.inject_rate);
d494500a
CY
2097 seq_printf(seq, ",fault_type=%u",
2098 F2FS_OPTION(sbi).fault_info.inject_type);
2099 }
0cc0dec2 2100#endif
0abd675e 2101#ifdef CONFIG_QUOTA
4b2414d0
CY
2102 if (test_opt(sbi, QUOTA))
2103 seq_puts(seq, ",quota");
0abd675e
CY
2104 if (test_opt(sbi, USRQUOTA))
2105 seq_puts(seq, ",usrquota");
2106 if (test_opt(sbi, GRPQUOTA))
2107 seq_puts(seq, ",grpquota");
5c57132e
CY
2108 if (test_opt(sbi, PRJQUOTA))
2109 seq_puts(seq, ",prjquota");
0cc0dec2 2110#endif
4b2414d0 2111 f2fs_show_quota_options(seq, sbi->sb);
ed318a6c
EB
2112
2113 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
aff063e2 2114
27aacd28
ST
2115 if (sbi->sb->s_flags & SB_INLINECRYPT)
2116 seq_puts(seq, ",inlinecrypt");
2117
63189b78 2118 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
07939627 2119 seq_printf(seq, ",alloc_mode=%s", "default");
63189b78 2120 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
07939627 2121 seq_printf(seq, ",alloc_mode=%s", "reuse");
93cf93f1 2122
4354994f 2123 if (test_opt(sbi, DISABLE_CHECKPOINT))
4d3aed70
DR
2124 seq_printf(seq, ",checkpoint=disable:%u",
2125 F2FS_OPTION(sbi).unusable_cap);
261eeb9c
DJ
2126 if (test_opt(sbi, MERGE_CHECKPOINT))
2127 seq_puts(seq, ",checkpoint_merge");
2128 else
2129 seq_puts(seq, ",nocheckpoint_merge");
63189b78 2130 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
93cf93f1 2131 seq_printf(seq, ",fsync_mode=%s", "posix");
63189b78 2132 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
93cf93f1 2133 seq_printf(seq, ",fsync_mode=%s", "strict");
dc132802
ST
2134 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2135 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
4c8ff709 2136
1f0b067b 2137#ifdef CONFIG_F2FS_FS_COMPRESSION
4c8ff709 2138 f2fs_show_compress_options(seq, sbi->sb);
1f0b067b 2139#endif
093749e2
CY
2140
2141 if (test_opt(sbi, ATGC))
2142 seq_puts(seq, ",atgc");
4f993264 2143
7a8fc586
DJ
2144 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2145 seq_printf(seq, ",memory=%s", "normal");
2146 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2147 seq_printf(seq, ",memory=%s", "low");
2148
b62e71be
CY
2149 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2150 seq_printf(seq, ",errors=%s", "remount-ro");
2151 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2152 seq_printf(seq, ",errors=%s", "continue");
2153 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2154 seq_printf(seq, ",errors=%s", "panic");
2155
c2ecba02
CY
2156 if (test_opt(sbi, NAT_BITS))
2157 seq_puts(seq, ",nat_bits");
2158
aff063e2
JK
2159 return 0;
2160}
2161
458c15df 2162static void default_options(struct f2fs_sb_info *sbi, bool remount)
498c5e9f
YH
2163{
2164 /* init some FS parameters */
458c15df
CY
2165 if (!remount) {
2166 set_opt(sbi, READ_EXTENT_CACHE);
2167 clear_opt(sbi, DISABLE_CHECKPOINT);
2168
2169 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2170 set_opt(sbi, DISCARD);
2171
2172 if (f2fs_sb_has_blkzoned(sbi))
2173 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2174 else
2175 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2176 }
2177
a7d9fe3c
JK
2178 if (f2fs_sb_has_readonly(sbi))
2179 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2180 else
2181 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2182
63189b78 2183 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
b7ad23ce
YG
2184 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2185 SMALL_VOLUME_SEGMENTS)
2186 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2187 else
2188 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
63189b78 2189 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
0aa7e0f8
CY
2190 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2191 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
c2c14ca5
YL
2192 if (f2fs_sb_has_compression(sbi)) {
2193 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2194 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2195 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2196 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2197 }
bbbc34fd 2198 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
7a8fc586 2199 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
b62e71be 2200 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
498c5e9f 2201
39133a50 2202 set_opt(sbi, INLINE_XATTR);
498c5e9f 2203 set_opt(sbi, INLINE_DATA);
97c1794a 2204 set_opt(sbi, INLINE_DENTRY);
b5d15199 2205 set_opt(sbi, MERGE_CHECKPOINT);
9100adf3 2206 set_opt(sbi, LAZYTIME);
4d3aed70 2207 F2FS_OPTION(sbi).unusable_cap = 0;
ed8ac22b 2208 if (!f2fs_is_readonly(sbi))
967eaad1 2209 set_opt(sbi, FLUSH_MERGE);
458c15df 2210 if (f2fs_sb_has_blkzoned(sbi))
b0332a0f 2211 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
458c15df 2212 else
b0332a0f 2213 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
498c5e9f
YH
2214
2215#ifdef CONFIG_F2FS_FS_XATTR
2216 set_opt(sbi, XATTR_USER);
2217#endif
2218#ifdef CONFIG_F2FS_FS_POSIX_ACL
2219 set_opt(sbi, POSIX_ACL);
2220#endif
36dbd328 2221
2be96c21 2222 f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL);
498c5e9f
YH
2223}
2224
ea676733
JK
2225#ifdef CONFIG_QUOTA
2226static int f2fs_enable_quotas(struct super_block *sb);
2227#endif
4354994f
DR
2228
2229static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2230{
812a9597 2231 unsigned int s_flags = sbi->sb->s_flags;
4354994f 2232 struct cp_control cpc;
2880f47b 2233 unsigned int gc_mode = sbi->gc_mode;
812a9597
JK
2234 int err = 0;
2235 int ret;
4d3aed70 2236 block_t unusable;
4354994f 2237
812a9597 2238 if (s_flags & SB_RDONLY) {
dcbb4c10 2239 f2fs_err(sbi, "checkpoint=disable on readonly fs");
812a9597
JK
2240 return -EINVAL;
2241 }
4354994f
DR
2242 sbi->sb->s_flags |= SB_ACTIVE;
2243
2880f47b
WG
2244 /* check if we need more GC first */
2245 unusable = f2fs_get_unusable_blocks(sbi);
2246 if (!f2fs_disable_cp_again(sbi, unusable))
2247 goto skip_gc;
2248
4354994f
DR
2249 f2fs_update_time(sbi, DISABLE_TIME);
2250
98e92867
CY
2251 sbi->gc_mode = GC_URGENT_HIGH;
2252
4354994f 2253 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
d147ea4a
JK
2254 struct f2fs_gc_control gc_control = {
2255 .victim_segno = NULL_SEGNO,
2256 .init_gc_type = FG_GC,
2257 .should_migrate_blocks = false,
c81d5bae 2258 .err_gc_skipped = true,
de252407 2259 .no_bg_gc = true,
c81d5bae 2260 .nr_free_secs = 1 };
d147ea4a 2261
e4544b63 2262 f2fs_down_write(&sbi->gc_lock);
9bf1dcbd 2263 stat_inc_gc_call_count(sbi, FOREGROUND);
d147ea4a 2264 err = f2fs_gc(sbi, &gc_control);
812a9597
JK
2265 if (err == -ENODATA) {
2266 err = 0;
4354994f 2267 break;
812a9597 2268 }
8f31b466 2269 if (err && err != -EAGAIN)
812a9597 2270 break;
4354994f 2271 }
4354994f 2272
812a9597
JK
2273 ret = sync_filesystem(sbi->sb);
2274 if (ret || err) {
5f029c04 2275 err = ret ? ret : err;
812a9597
JK
2276 goto restore_flag;
2277 }
4354994f 2278
4d3aed70
DR
2279 unusable = f2fs_get_unusable_blocks(sbi);
2280 if (f2fs_disable_cp_again(sbi, unusable)) {
812a9597
JK
2281 err = -EAGAIN;
2282 goto restore_flag;
2283 }
4354994f 2284
2880f47b 2285skip_gc:
e4544b63 2286 f2fs_down_write(&sbi->gc_lock);
4354994f
DR
2287 cpc.reason = CP_PAUSE;
2288 set_sbi_flag(sbi, SBI_CP_DISABLED);
eb61c2cc 2289 stat_inc_cp_call_count(sbi, TOTAL_CALL);
896285ad
CY
2290 err = f2fs_write_checkpoint(sbi, &cpc);
2291 if (err)
2292 goto out_unlock;
4354994f 2293
c9c8ed50 2294 spin_lock(&sbi->stat_lock);
4d3aed70 2295 sbi->unusable_block_count = unusable;
c9c8ed50
CY
2296 spin_unlock(&sbi->stat_lock);
2297
896285ad 2298out_unlock:
e4544b63 2299 f2fs_up_write(&sbi->gc_lock);
812a9597 2300restore_flag:
98e92867 2301 sbi->gc_mode = gc_mode;
7a88ddb5 2302 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
812a9597 2303 return err;
4354994f
DR
2304}
2305
2306static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2307{
dddd3d65
JK
2308 int retry = DEFAULT_RETRY_IO_COUNT;
2309
b0ff4fe7 2310 /* we should flush all the data to keep data consistency */
dddd3d65
JK
2311 do {
2312 sync_inodes_sb(sbi->sb);
a64239d0 2313 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
dddd3d65
JK
2314 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2315
2316 if (unlikely(retry < 0))
2317 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
b0ff4fe7 2318
e4544b63 2319 f2fs_down_write(&sbi->gc_lock);
4354994f
DR
2320 f2fs_dirty_to_prefree(sbi);
2321
2322 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2323 set_sbi_flag(sbi, SBI_IS_DIRTY);
e4544b63 2324 f2fs_up_write(&sbi->gc_lock);
4354994f
DR
2325
2326 f2fs_sync_fs(sbi->sb, 1);
4f99484d
JK
2327
2328 /* Let's ensure there's no pending checkpoint anymore */
2329 f2fs_flush_ckpt_thread(sbi);
4354994f
DR
2330}
2331
696c018c
NJ
2332static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2333{
2334 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2335 struct f2fs_mount_info org_mount_opt;
0abd675e 2336 unsigned long old_sb_flags;
63189b78 2337 int err;
3fd97359 2338 bool need_restart_gc = false, need_stop_gc = false;
3fd97359 2339 bool need_restart_flush = false, need_stop_flush = false;
4d674904 2340 bool need_restart_discard = false, need_stop_discard = false;
1e7bef5f 2341 bool need_enable_checkpoint = false, need_disable_checkpoint = false;
12607c1b 2342 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
71644dff 2343 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
277afbde 2344 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
093749e2 2345 bool no_atgc = !test_opt(sbi, ATGC);
4d674904 2346 bool no_discard = !test_opt(sbi, DISCARD);
6ce19aff 2347 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
4f993264 2348 bool block_unit_discard = f2fs_block_unit_discard(sbi);
c2ecba02 2349 bool no_nat_bits = !test_opt(sbi, NAT_BITS);
4b2414d0 2350#ifdef CONFIG_QUOTA
4b2414d0
CY
2351 int i, j;
2352#endif
696c018c
NJ
2353
2354 /*
2355 * Save the old mount options in case we
2356 * need to restore them.
2357 */
2358 org_mount_opt = sbi->mount_opt;
0abd675e 2359 old_sb_flags = sb->s_flags;
696c018c 2360
eb85c241
CY
2361 sbi->umount_lock_holder = current;
2362
4b2414d0 2363#ifdef CONFIG_QUOTA
63189b78 2364 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
4b2414d0 2365 for (i = 0; i < MAXQUOTAS; i++) {
63189b78
CY
2366 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2367 org_mount_opt.s_qf_names[i] =
2368 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2369 GFP_KERNEL);
2370 if (!org_mount_opt.s_qf_names[i]) {
4b2414d0 2371 for (j = 0; j < i; j++)
ba87a45c 2372 kfree(org_mount_opt.s_qf_names[j]);
4b2414d0
CY
2373 return -ENOMEM;
2374 }
2375 } else {
63189b78 2376 org_mount_opt.s_qf_names[i] = NULL;
4b2414d0
CY
2377 }
2378 }
2379#endif
2380
df728b0f 2381 /* recover superblocks we couldn't write due to previous RO mount */
1751e8a6 2382 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
df728b0f 2383 err = f2fs_commit_super(sbi, false);
dcbb4c10
JP
2384 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2385 err);
df728b0f
JK
2386 if (!err)
2387 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2388 }
2389
458c15df 2390 default_options(sbi, true);
26666c8a 2391
696c018c 2392 /* parse mount options */
71e9bd3d 2393 err = parse_options(sbi, data, true);
696c018c
NJ
2394 if (err)
2395 goto restore_opts;
2396
0f9b1214
WQ
2397#ifdef CONFIG_BLK_DEV_ZONED
2398 if (f2fs_sb_has_blkzoned(sbi) &&
2399 sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
2400 f2fs_err(sbi,
2401 "zoned: max open zones %u is too small, need at least %u open zones",
2402 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
2403 err = -EINVAL;
2404 goto restore_opts;
2405 }
2406#endif
2407
abd0e040
ES
2408 err = f2fs_default_check(sbi);
2409 if (err)
2410 goto restore_opts;
2411
b62e71be
CY
2412 /* flush outstanding errors before changing fs state */
2413 flush_work(&sbi->s_error_work);
2414
696c018c
NJ
2415 /*
2416 * Previous and new state of filesystem is RO,
876dc59e 2417 * so skip checking GC and FLUSH_MERGE conditions.
696c018c 2418 */
1751e8a6 2419 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
696c018c
NJ
2420 goto skip;
2421
d78dfefc 2422 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
a7d9fe3c
JK
2423 err = -EROFS;
2424 goto restore_opts;
2425 }
2426
ea676733 2427#ifdef CONFIG_QUOTA
1751e8a6 2428 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
0abd675e
CY
2429 err = dquot_suspend(sb, -1);
2430 if (err < 0)
2431 goto restore_opts;
4354994f 2432 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
0abd675e 2433 /* dquot_resume needs RW */
1751e8a6 2434 sb->s_flags &= ~SB_RDONLY;
ea676733
JK
2435 if (sb_any_quota_suspended(sb)) {
2436 dquot_resume(sb, -1);
7beb01f7 2437 } else if (f2fs_sb_has_quota_ino(sbi)) {
ea676733
JK
2438 err = f2fs_enable_quotas(sb);
2439 if (err)
2440 goto restore_opts;
2441 }
0abd675e 2442 }
ea676733 2443#endif
c5bf8348
YL
2444 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2445 err = -EINVAL;
2446 f2fs_warn(sbi, "LFS is not compatible with IPU");
2447 goto restore_opts;
2448 }
2449
093749e2
CY
2450 /* disallow enable atgc dynamically */
2451 if (no_atgc == !!test_opt(sbi, ATGC)) {
2452 err = -EINVAL;
2453 f2fs_warn(sbi, "switch atgc option is not allowed");
2454 goto restore_opts;
2455 }
2456
9cd81ce3 2457 /* disallow enable/disable extent_cache dynamically */
12607c1b 2458 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
9cd81ce3 2459 err = -EINVAL;
dcbb4c10 2460 f2fs_warn(sbi, "switch extent_cache option is not allowed");
9cd81ce3
CY
2461 goto restore_opts;
2462 }
71644dff
JK
2463 /* disallow enable/disable age extent_cache dynamically */
2464 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2465 err = -EINVAL;
2466 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2467 goto restore_opts;
2468 }
9cd81ce3 2469
6ce19aff
CY
2470 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2471 err = -EINVAL;
2472 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2473 goto restore_opts;
2474 }
2475
4f993264
CY
2476 if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2477 err = -EINVAL;
2478 f2fs_warn(sbi, "switch discard_unit option is not allowed");
c2ecba02
CY
2479 goto restore_opts;
2480 }
2481
2482 if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) {
2483 err = -EINVAL;
2484 f2fs_warn(sbi, "switch nat_bits option is not allowed");
4f993264
CY
2485 goto restore_opts;
2486 }
2487
4354994f
DR
2488 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2489 err = -EINVAL;
dcbb4c10 2490 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
4354994f
DR
2491 goto restore_opts;
2492 }
2493
696c018c
NJ
2494 /*
2495 * We stop the GC thread if FS is mounted as RO
2496 * or if background_gc = off is passed in mount
2497 * option. Also sync the filesystem.
2498 */
bbbc34fd 2499 if ((*flags & SB_RDONLY) ||
5911d2d1
CY
2500 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2501 !test_opt(sbi, GC_MERGE))) {
696c018c 2502 if (sbi->gc_thread) {
4d57b86d 2503 f2fs_stop_gc_thread(sbi);
876dc59e 2504 need_restart_gc = true;
696c018c 2505 }
aba291b3 2506 } else if (!sbi->gc_thread) {
4d57b86d 2507 err = f2fs_start_gc_thread(sbi);
696c018c
NJ
2508 if (err)
2509 goto restore_opts;
876dc59e
GZ
2510 need_stop_gc = true;
2511 }
2512
930e2607 2513 if (*flags & SB_RDONLY) {
faa0e55b
JK
2514 sync_inodes_sb(sb);
2515
2516 set_sbi_flag(sbi, SBI_IS_DIRTY);
2517 set_sbi_flag(sbi, SBI_IS_CLOSE);
2518 f2fs_sync_fs(sb, 1);
2519 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2520 }
2521
876dc59e
GZ
2522 /*
2523 * We stop issue flush thread if FS is mounted as RO
2524 * or if flush_merge is not passed in mount option.
2525 */
1751e8a6 2526 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
5eba8c5d 2527 clear_opt(sbi, FLUSH_MERGE);
4d57b86d 2528 f2fs_destroy_flush_cmd_control(sbi, false);
3fd97359 2529 need_restart_flush = true;
5eba8c5d 2530 } else {
4d57b86d 2531 err = f2fs_create_flush_cmd_control(sbi);
2163d198 2532 if (err)
1e7bef5f 2533 goto restore_gc;
3fd97359 2534 need_stop_flush = true;
696c018c 2535 }
3fd97359 2536
4d674904
FC
2537 if (no_discard == !!test_opt(sbi, DISCARD)) {
2538 if (test_opt(sbi, DISCARD)) {
2539 err = f2fs_start_discard_thread(sbi);
2540 if (err)
2541 goto restore_flush;
2542 need_stop_discard = true;
2543 } else {
4d674904 2544 f2fs_stop_discard_thread(sbi);
25547439 2545 f2fs_issue_discard_timeout(sbi);
4d674904
FC
2546 need_restart_discard = true;
2547 }
2548 }
2549
a35749b1 2550 adjust_unusable_cap_perc(sbi);
277afbde 2551 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
3fd97359
CY
2552 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2553 err = f2fs_disable_checkpoint(sbi);
2554 if (err)
4d674904 2555 goto restore_discard;
1e7bef5f 2556 need_enable_checkpoint = true;
3fd97359
CY
2557 } else {
2558 f2fs_enable_checkpoint(sbi);
1e7bef5f
DJ
2559 need_disable_checkpoint = true;
2560 }
2561 }
2562
2563 /*
2564 * Place this routine at the end, since a new checkpoint would be
2565 * triggered while remount and we need to take care of it before
2566 * returning from remount.
2567 */
2568 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2569 !test_opt(sbi, MERGE_CHECKPOINT)) {
2570 f2fs_stop_ckpt_thread(sbi);
2571 } else {
2572 /* Flush if the prevous checkpoint, if exists. */
2573 f2fs_flush_ckpt_thread(sbi);
2574
2575 err = f2fs_start_ckpt_thread(sbi);
2576 if (err) {
2577 f2fs_err(sbi,
2578 "Failed to start F2FS issue_checkpoint_thread (%d)",
2579 err);
2580 goto restore_checkpoint;
3fd97359
CY
2581 }
2582 }
2583
696c018c 2584skip:
4b2414d0
CY
2585#ifdef CONFIG_QUOTA
2586 /* Release old quota file names */
2587 for (i = 0; i < MAXQUOTAS; i++)
ba87a45c 2588 kfree(org_mount_opt.s_qf_names[i]);
4b2414d0 2589#endif
696c018c 2590 /* Update the POSIXACL Flag */
1751e8a6
LT
2591 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2592 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
df728b0f 2593
7e65be49 2594 limit_reserve_root(sbi);
095680f2 2595 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
eb85c241
CY
2596
2597 sbi->umount_lock_holder = NULL;
696c018c 2598 return 0;
1e7bef5f
DJ
2599restore_checkpoint:
2600 if (need_enable_checkpoint) {
2601 f2fs_enable_checkpoint(sbi);
2602 } else if (need_disable_checkpoint) {
2603 if (f2fs_disable_checkpoint(sbi))
2604 f2fs_warn(sbi, "checkpoint has not been disabled");
2605 }
4d674904
FC
2606restore_discard:
2607 if (need_restart_discard) {
2608 if (f2fs_start_discard_thread(sbi))
2609 f2fs_warn(sbi, "discard has been stopped");
2610 } else if (need_stop_discard) {
2611 f2fs_stop_discard_thread(sbi);
2612 }
3fd97359
CY
2613restore_flush:
2614 if (need_restart_flush) {
2615 if (f2fs_create_flush_cmd_control(sbi))
2616 f2fs_warn(sbi, "background flush thread has stopped");
2617 } else if (need_stop_flush) {
2618 clear_opt(sbi, FLUSH_MERGE);
2619 f2fs_destroy_flush_cmd_control(sbi, false);
2620 }
876dc59e
GZ
2621restore_gc:
2622 if (need_restart_gc) {
4d57b86d 2623 if (f2fs_start_gc_thread(sbi))
dcbb4c10 2624 f2fs_warn(sbi, "background gc thread has stopped");
876dc59e 2625 } else if (need_stop_gc) {
4d57b86d 2626 f2fs_stop_gc_thread(sbi);
876dc59e 2627 }
696c018c 2628restore_opts:
4b2414d0 2629#ifdef CONFIG_QUOTA
63189b78 2630 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
4b2414d0 2631 for (i = 0; i < MAXQUOTAS; i++) {
ba87a45c 2632 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
63189b78 2633 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
4b2414d0
CY
2634 }
2635#endif
696c018c 2636 sbi->mount_opt = org_mount_opt;
0abd675e 2637 sb->s_flags = old_sb_flags;
eb85c241
CY
2638
2639 sbi->umount_lock_holder = NULL;
696c018c
NJ
2640 return err;
2641}
2642
ee745e47
CY
2643static void f2fs_shutdown(struct super_block *sb)
2644{
c7f114d8 2645 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
ee745e47
CY
2646}
2647
0abd675e 2648#ifdef CONFIG_QUOTA
e1bb7d3d
CY
2649static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2650{
2651 /* need to recovery orphan */
2652 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2653 return true;
2654 /* need to recovery data */
2655 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2656 return false;
2657 if (test_opt(sbi, NORECOVERY))
2658 return false;
2659 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2660}
2661
2662static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2663{
2664 bool readonly = f2fs_readonly(sbi->sb);
2665
2666 if (!f2fs_need_recovery(sbi))
2667 return false;
2668
2669 /* it doesn't need to check f2fs_sb_has_readonly() */
2670 if (f2fs_hw_is_readonly(sbi))
2671 return false;
2672
2673 if (readonly) {
2674 sbi->sb->s_flags &= ~SB_RDONLY;
2675 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2676 }
2677
2678 /*
2679 * Turn on quotas which were not enabled for read-only mounts if
2680 * filesystem has quota feature, so that they are updated correctly.
2681 */
2682 return f2fs_enable_quota_files(sbi, readonly);
2683}
2684
2685static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2686 bool quota_enabled)
2687{
2688 if (quota_enabled)
2689 f2fs_quota_off_umount(sbi->sb);
2690
2691 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2692 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2693 sbi->sb->s_flags |= SB_RDONLY;
2694 }
2695}
2696
0abd675e
CY
2697/* Read data from quotafile */
2698static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2699 size_t len, loff_t off)
2700{
2701 struct inode *inode = sb_dqopt(sb)->files[type];
2702 struct address_space *mapping = inode->i_mapping;
0abd675e
CY
2703 int tocopy;
2704 size_t toread;
2705 loff_t i_size = i_size_read(inode);
0abd675e
CY
2706
2707 if (off > i_size)
2708 return 0;
2709
2710 if (off + len > i_size)
2711 len = i_size - off;
2712 toread = len;
2713 while (toread > 0) {
0d1e687e
MWO
2714 struct folio *folio;
2715 size_t offset;
2716
0abd675e 2717repeat:
0d1e687e
MWO
2718 folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT,
2719 GFP_NOFS);
2720 if (IS_ERR(folio)) {
2721 if (PTR_ERR(folio) == -ENOMEM) {
4034247a 2722 memalloc_retry_wait(GFP_NOFS);
4e46a023
JK
2723 goto repeat;
2724 }
af033b2a 2725 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
0d1e687e 2726 return PTR_ERR(folio);
4e46a023 2727 }
0d1e687e
MWO
2728 offset = offset_in_folio(folio, off);
2729 tocopy = min(folio_size(folio) - offset, toread);
0abd675e 2730
0d1e687e 2731 folio_lock(folio);
0abd675e 2732
0d1e687e
MWO
2733 if (unlikely(folio->mapping != mapping)) {
2734 f2fs_folio_put(folio, true);
0abd675e
CY
2735 goto repeat;
2736 }
0abd675e 2737
5827e3c7
CY
2738 /*
2739 * should never happen, just leave f2fs_bug_on() here to catch
2740 * any potential bug.
2741 */
2742 f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio));
2743
0d1e687e
MWO
2744 memcpy_from_folio(data, folio, offset, tocopy);
2745 f2fs_folio_put(folio, true);
0abd675e 2746
0abd675e
CY
2747 toread -= tocopy;
2748 data += tocopy;
0d1e687e 2749 off += tocopy;
0abd675e
CY
2750 }
2751 return len;
2752}
2753
2754/* Write to quotafile */
2755static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2756 const char *data, size_t len, loff_t off)
2757{
2758 struct inode *inode = sb_dqopt(sb)->files[type];
2759 struct address_space *mapping = inode->i_mapping;
2760 const struct address_space_operations *a_ops = mapping->a_ops;
2761 int offset = off & (sb->s_blocksize - 1);
2762 size_t towrite = len;
1da86618 2763 struct folio *folio;
62f63eea 2764 void *fsdata = NULL;
0abd675e
CY
2765 int err = 0;
2766 int tocopy;
2767
2768 while (towrite > 0) {
2769 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2770 towrite);
4e46a023 2771retry:
9d6b0cd7 2772 err = a_ops->write_begin(NULL, mapping, off, tocopy,
1da86618 2773 &folio, &fsdata);
4e46a023
JK
2774 if (unlikely(err)) {
2775 if (err == -ENOMEM) {
a64239d0 2776 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
4e46a023
JK
2777 goto retry;
2778 }
af033b2a 2779 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
0abd675e 2780 break;
4e46a023 2781 }
0abd675e 2782
1da86618 2783 memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy);
0abd675e
CY
2784
2785 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1da86618 2786 folio, fsdata);
0abd675e
CY
2787 offset = 0;
2788 towrite -= tocopy;
2789 off += tocopy;
2790 data += tocopy;
2791 cond_resched();
2792 }
2793
2794 if (len == towrite)
6e5b5d41 2795 return err;
11cc6426 2796 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
0abd675e
CY
2797 f2fs_mark_inode_dirty_sync(inode, false);
2798 return len - towrite;
2799}
2800
10a26878
CY
2801int f2fs_dquot_initialize(struct inode *inode)
2802{
c40e15a9 2803 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
10a26878 2804 return -ESRCH;
10a26878
CY
2805
2806 return dquot_initialize(inode);
2807}
2808
ccb49011 2809static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
0abd675e
CY
2810{
2811 return F2FS_I(inode)->i_dquot;
2812}
2813
2814static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2815{
2816 return &F2FS_I(inode)->i_reserved_quota;
2817}
2818
4b2414d0
CY
2819static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2820{
af033b2a 2821 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
dcbb4c10 2822 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
af033b2a
CY
2823 return 0;
2824 }
2825
63189b78
CY
2826 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2827 F2FS_OPTION(sbi).s_jquota_fmt, type);
4b2414d0
CY
2828}
2829
ea676733 2830int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
4b2414d0 2831{
ea676733
JK
2832 int enabled = 0;
2833 int i, err;
2834
7beb01f7 2835 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
ea676733
JK
2836 err = f2fs_enable_quotas(sbi->sb);
2837 if (err) {
dcbb4c10 2838 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
ea676733
JK
2839 return 0;
2840 }
2841 return 1;
2842 }
4b2414d0
CY
2843
2844 for (i = 0; i < MAXQUOTAS; i++) {
63189b78 2845 if (F2FS_OPTION(sbi).s_qf_names[i]) {
ea676733
JK
2846 err = f2fs_quota_on_mount(sbi, i);
2847 if (!err) {
2848 enabled = 1;
2849 continue;
2850 }
dcbb4c10
JP
2851 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2852 err, i);
4b2414d0
CY
2853 }
2854 }
ea676733
JK
2855 return enabled;
2856}
2857
2858static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2859 unsigned int flags)
2860{
2861 struct inode *qf_inode;
2862 unsigned long qf_inum;
ccf3ff2b 2863 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
ea676733
JK
2864 int err;
2865
7beb01f7 2866 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
ea676733
JK
2867
2868 qf_inum = f2fs_qf_ino(sb, type);
2869 if (!qf_inum)
2870 return -EPERM;
2871
2872 qf_inode = f2fs_iget(sb, qf_inum);
2873 if (IS_ERR(qf_inode)) {
dcbb4c10 2874 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
ea676733
JK
2875 return PTR_ERR(qf_inode);
2876 }
2877
2878 /* Don't account quota for quota files to avoid recursion */
90b7c4b7 2879 inode_lock(qf_inode);
ea676733 2880 qf_inode->i_flags |= S_NOQUOTA;
90b7c4b7
CY
2881
2882 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2883 F2FS_I(qf_inode)->i_flags |= qf_flag;
2884 f2fs_set_inode_flags(qf_inode);
2885 }
2886 inode_unlock(qf_inode);
2887
7212b95e 2888 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
ea676733
JK
2889 iput(qf_inode);
2890 return err;
2891}
2892
2893static int f2fs_enable_quotas(struct super_block *sb)
2894{
dcbb4c10 2895 struct f2fs_sb_info *sbi = F2FS_SB(sb);
ea676733
JK
2896 int type, err = 0;
2897 unsigned long qf_inum;
2898 bool quota_mopt[MAXQUOTAS] = {
dcbb4c10
JP
2899 test_opt(sbi, USRQUOTA),
2900 test_opt(sbi, GRPQUOTA),
2901 test_opt(sbi, PRJQUOTA),
ea676733
JK
2902 };
2903
af033b2a 2904 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
dcbb4c10 2905 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
af033b2a
CY
2906 return 0;
2907 }
2908
2909 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2910
ea676733
JK
2911 for (type = 0; type < MAXQUOTAS; type++) {
2912 qf_inum = f2fs_qf_ino(sb, type);
2913 if (qf_inum) {
2914 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2915 DQUOT_USAGE_ENABLED |
2916 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2917 if (err) {
dcbb4c10
JP
2918 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2919 type, err);
ea676733
JK
2920 for (type--; type >= 0; type--)
2921 dquot_quota_off(sb, type);
af033b2a
CY
2922 set_sbi_flag(F2FS_SB(sb),
2923 SBI_QUOTA_NEED_REPAIR);
ea676733
JK
2924 return err;
2925 }
4b2414d0
CY
2926 }
2927 }
ea676733 2928 return 0;
4b2414d0
CY
2929}
2930
9de71ede
CY
2931static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2932{
2933 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2934 struct address_space *mapping = dqopt->files[type]->i_mapping;
2935 int ret = 0;
2936
2937 ret = dquot_writeback_dquots(sbi->sb, type);
2938 if (ret)
2939 goto out;
2940
2941 ret = filemap_fdatawrite(mapping);
2942 if (ret)
2943 goto out;
2944
2945 /* if we are using journalled quota */
2946 if (is_journalled_quota(sbi))
2947 goto out;
2948
2949 ret = filemap_fdatawait(mapping);
2950
2951 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2952out:
2953 if (ret)
2954 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2955 return ret;
2956}
2957
eb85c241 2958int f2fs_do_quota_sync(struct super_block *sb, int type)
0abd675e 2959{
af033b2a 2960 struct f2fs_sb_info *sbi = F2FS_SB(sb);
0abd675e
CY
2961 struct quota_info *dqopt = sb_dqopt(sb);
2962 int cnt;
680af5b8 2963 int ret = 0;
0abd675e 2964
0abd675e
CY
2965 /*
2966 * Now when everything is written we can discard the pagecache so
2967 * that userspace sees the changes.
2968 */
2969 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
af033b2a 2970
0abd675e
CY
2971 if (type != -1 && cnt != type)
2972 continue;
0abd675e 2973
680af5b8
JP
2974 if (!sb_has_quota_active(sb, cnt))
2975 continue;
af033b2a 2976
6213f5d4
JK
2977 if (!f2fs_sb_has_quota_ino(sbi))
2978 inode_lock(dqopt->files[cnt]);
af033b2a 2979
9de71ede
CY
2980 /*
2981 * do_quotactl
2982 * f2fs_quota_sync
e4544b63 2983 * f2fs_down_read(quota_sem)
9de71ede
CY
2984 * dquot_writeback_dquots()
2985 * f2fs_dquot_commit
2986 * block_operation
e4544b63 2987 * f2fs_down_read(quota_sem)
9de71ede
CY
2988 */
2989 f2fs_lock_op(sbi);
e4544b63 2990 f2fs_down_read(&sbi->quota_sem);
af033b2a 2991
9de71ede
CY
2992 ret = f2fs_quota_sync_file(sbi, cnt);
2993
e4544b63 2994 f2fs_up_read(&sbi->quota_sem);
9de71ede 2995 f2fs_unlock_op(sbi);
0abd675e 2996
6213f5d4
JK
2997 if (!f2fs_sb_has_quota_ino(sbi))
2998 inode_unlock(dqopt->files[cnt]);
9de71ede
CY
2999
3000 if (ret)
3001 break;
0abd675e 3002 }
af033b2a 3003 return ret;
0abd675e
CY
3004}
3005
eb85c241
CY
3006static int f2fs_quota_sync(struct super_block *sb, int type)
3007{
3008 int ret;
3009
3010 F2FS_SB(sb)->umount_lock_holder = current;
3011 ret = f2fs_do_quota_sync(sb, type);
3012 F2FS_SB(sb)->umount_lock_holder = NULL;
3013 return ret;
3014}
3015
0abd675e
CY
3016static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
3017 const struct path *path)
3018{
3019 struct inode *inode;
eb85c241 3020 int err = 0;
0abd675e 3021
fe973b06
CY
3022 /* if quota sysfile exists, deny enabling quota with specific file */
3023 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
3024 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
3025 return -EBUSY;
3026 }
3027
5079e1c0
CY
3028 if (path->dentry->d_sb != sb)
3029 return -EXDEV;
3030
eb85c241
CY
3031 F2FS_SB(sb)->umount_lock_holder = current;
3032
3033 err = f2fs_do_quota_sync(sb, type);
0abd675e 3034 if (err)
eb85c241 3035 goto out;
0abd675e 3036
5079e1c0
CY
3037 inode = d_inode(path->dentry);
3038
3039 err = filemap_fdatawrite(inode->i_mapping);
0abd675e 3040 if (err)
eb85c241 3041 goto out;
0abd675e 3042
5079e1c0
CY
3043 err = filemap_fdatawait(inode->i_mapping);
3044 if (err)
eb85c241 3045 goto out;
5079e1c0
CY
3046
3047 err = dquot_quota_on(sb, type, format_id, path);
3048 if (err)
eb85c241 3049 goto out;
0abd675e
CY
3050
3051 inode_lock(inode);
ccf3ff2b 3052 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
9149a5eb 3053 f2fs_set_inode_flags(inode);
0abd675e
CY
3054 inode_unlock(inode);
3055 f2fs_mark_inode_dirty_sync(inode, false);
eb85c241
CY
3056out:
3057 F2FS_SB(sb)->umount_lock_holder = NULL;
3058 return err;
0abd675e
CY
3059}
3060
fe973b06 3061static int __f2fs_quota_off(struct super_block *sb, int type)
0abd675e
CY
3062{
3063 struct inode *inode = sb_dqopt(sb)->files[type];
3064 int err;
3065
3066 if (!inode || !igrab(inode))
3067 return dquot_quota_off(sb, type);
3068
eb85c241 3069 err = f2fs_do_quota_sync(sb, type);
cda9cc59
YH
3070 if (err)
3071 goto out_put;
0abd675e
CY
3072
3073 err = dquot_quota_off(sb, type);
7beb01f7 3074 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
0abd675e
CY
3075 goto out_put;
3076
3077 inode_lock(inode);
ccf3ff2b 3078 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
9149a5eb 3079 f2fs_set_inode_flags(inode);
0abd675e
CY
3080 inode_unlock(inode);
3081 f2fs_mark_inode_dirty_sync(inode, false);
3082out_put:
3083 iput(inode);
3084 return err;
3085}
3086
fe973b06
CY
3087static int f2fs_quota_off(struct super_block *sb, int type)
3088{
3089 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3090 int err;
3091
eb85c241
CY
3092 F2FS_SB(sb)->umount_lock_holder = current;
3093
fe973b06
CY
3094 err = __f2fs_quota_off(sb, type);
3095
3096 /*
3097 * quotactl can shutdown journalled quota, result in inconsistence
3098 * between quota record and fs data by following updates, tag the
3099 * flag to let fsck be aware of it.
3100 */
3101 if (is_journalled_quota(sbi))
3102 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
eb85c241
CY
3103
3104 F2FS_SB(sb)->umount_lock_holder = NULL;
3105
fe973b06
CY
3106 return err;
3107}
3108
4b2414d0 3109void f2fs_quota_off_umount(struct super_block *sb)
0abd675e
CY
3110{
3111 int type;
cda9cc59
YH
3112 int err;
3113
3114 for (type = 0; type < MAXQUOTAS; type++) {
fe973b06 3115 err = __f2fs_quota_off(sb, type);
cda9cc59
YH
3116 if (err) {
3117 int ret = dquot_quota_off(sb, type);
0abd675e 3118
dcbb4c10
JP
3119 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3120 type, err, ret);
af033b2a 3121 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
cda9cc59
YH
3122 }
3123 }
0e0667b6
JK
3124 /*
3125 * In case of checkpoint=disable, we must flush quota blocks.
3126 * This can cause NULL exception for node_inode in end_io, since
3127 * put_super already dropped it.
3128 */
3129 sync_filesystem(sb);
0abd675e
CY
3130}
3131
26b5a079
SY
3132static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3133{
3134 struct quota_info *dqopt = sb_dqopt(sb);
3135 int type;
3136
3137 for (type = 0; type < MAXQUOTAS; type++) {
3138 if (!dqopt->files[type])
3139 continue;
3140 f2fs_inode_synced(dqopt->files[type]);
3141 }
3142}
3143
af033b2a
CY
3144static int f2fs_dquot_commit(struct dquot *dquot)
3145{
db6ec53b 3146 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
af033b2a
CY
3147 int ret;
3148
e4544b63 3149 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
af033b2a
CY
3150 ret = dquot_commit(dquot);
3151 if (ret < 0)
db6ec53b 3152 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
e4544b63 3153 f2fs_up_read(&sbi->quota_sem);
af033b2a
CY
3154 return ret;
3155}
3156
3157static int f2fs_dquot_acquire(struct dquot *dquot)
3158{
db6ec53b 3159 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
af033b2a
CY
3160 int ret;
3161
e4544b63 3162 f2fs_down_read(&sbi->quota_sem);
af033b2a
CY
3163 ret = dquot_acquire(dquot);
3164 if (ret < 0)
db6ec53b 3165 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
e4544b63 3166 f2fs_up_read(&sbi->quota_sem);
af033b2a
CY
3167 return ret;
3168}
3169
3170static int f2fs_dquot_release(struct dquot *dquot)
3171{
db6ec53b 3172 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2c4e0c52 3173 int ret = dquot_release(dquot);
af033b2a 3174
af033b2a 3175 if (ret < 0)
db6ec53b 3176 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
af033b2a
CY
3177 return ret;
3178}
3179
3180static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3181{
3182 struct super_block *sb = dquot->dq_sb;
3183 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2c4e0c52 3184 int ret = dquot_mark_dquot_dirty(dquot);
af033b2a
CY
3185
3186 /* if we are using journalled quota */
3187 if (is_journalled_quota(sbi))
3188 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3189
3190 return ret;
3191}
3192
3193static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3194{
db6ec53b 3195 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2c4e0c52 3196 int ret = dquot_commit_info(sb, type);
af033b2a 3197
af033b2a 3198 if (ret < 0)
db6ec53b 3199 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
af033b2a
CY
3200 return ret;
3201}
26b5a079 3202
94b1e10e 3203static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
5c57132e
CY
3204{
3205 *projid = F2FS_I(inode)->i_projid;
3206 return 0;
3207}
3208
0abd675e
CY
3209static const struct dquot_operations f2fs_quota_operations = {
3210 .get_reserved_space = f2fs_get_reserved_space,
af033b2a
CY
3211 .write_dquot = f2fs_dquot_commit,
3212 .acquire_dquot = f2fs_dquot_acquire,
3213 .release_dquot = f2fs_dquot_release,
3214 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3215 .write_info = f2fs_dquot_commit_info,
0abd675e
CY
3216 .alloc_dquot = dquot_alloc,
3217 .destroy_dquot = dquot_destroy,
5c57132e 3218 .get_projid = f2fs_get_projid,
0abd675e
CY
3219 .get_next_id = dquot_get_next_id,
3220};
3221
3222static const struct quotactl_ops f2fs_quotactl_ops = {
3223 .quota_on = f2fs_quota_on,
3224 .quota_off = f2fs_quota_off,
3225 .quota_sync = f2fs_quota_sync,
3226 .get_state = dquot_get_state,
3227 .set_info = dquot_set_dqinfo,
3228 .get_dqblk = dquot_get_dqblk,
3229 .set_dqblk = dquot_set_dqblk,
3230 .get_nextdqblk = dquot_get_next_dqblk,
3231};
3232#else
10a26878
CY
3233int f2fs_dquot_initialize(struct inode *inode)
3234{
3235 return 0;
3236}
3237
eb85c241 3238int f2fs_do_quota_sync(struct super_block *sb, int type)
af033b2a
CY
3239{
3240 return 0;
3241}
3242
4b2414d0 3243void f2fs_quota_off_umount(struct super_block *sb)
0abd675e
CY
3244{
3245}
3246#endif
3247
f62fc9f9 3248static const struct super_operations f2fs_sops = {
aff063e2 3249 .alloc_inode = f2fs_alloc_inode,
d01718a0 3250 .free_inode = f2fs_free_inode,
531ad7d5 3251 .drop_inode = f2fs_drop_inode,
aff063e2 3252 .write_inode = f2fs_write_inode,
b3783873 3253 .dirty_inode = f2fs_dirty_inode,
aff063e2 3254 .show_options = f2fs_show_options,
0abd675e
CY
3255#ifdef CONFIG_QUOTA
3256 .quota_read = f2fs_quota_read,
3257 .quota_write = f2fs_quota_write,
3258 .get_dquots = f2fs_get_dquots,
3259#endif
aff063e2
JK
3260 .evict_inode = f2fs_evict_inode,
3261 .put_super = f2fs_put_super,
3262 .sync_fs = f2fs_sync_fs,
d6212a5f
CL
3263 .freeze_fs = f2fs_freeze,
3264 .unfreeze_fs = f2fs_unfreeze,
aff063e2 3265 .statfs = f2fs_statfs,
696c018c 3266 .remount_fs = f2fs_remount,
ee745e47 3267 .shutdown = f2fs_shutdown,
aff063e2
JK
3268};
3269
643fa961 3270#ifdef CONFIG_FS_ENCRYPTION
0b81d077
JK
3271static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3272{
3273 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3274 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3275 ctx, len, NULL);
3276}
3277
3278static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3279 void *fs_data)
3280{
b7c409de
SY
3281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3282
3283 /*
3284 * Encrypting the root directory is not allowed because fsck
3285 * expects lost+found directory to exist and remain unencrypted
3286 * if LOST_FOUND feature is enabled.
3287 *
3288 */
7beb01f7 3289 if (f2fs_sb_has_lost_found(sbi) &&
b7c409de
SY
3290 inode->i_ino == F2FS_ROOT_INO(sbi))
3291 return -EPERM;
3292
0b81d077
JK
3293 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3294 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3295 ctx, len, fs_data, XATTR_CREATE);
3296}
3297
ac4acb1f 3298static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
ff62af20 3299{
ac4acb1f 3300 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
ff62af20
SY
3301}
3302
0eee17e3
EB
3303static bool f2fs_has_stable_inodes(struct super_block *sb)
3304{
3305 return true;
3306}
3307
0e91fc1e
CH
3308static struct block_device **f2fs_get_devices(struct super_block *sb,
3309 unsigned int *num_devs)
27aacd28
ST
3310{
3311 struct f2fs_sb_info *sbi = F2FS_SB(sb);
0e91fc1e
CH
3312 struct block_device **devs;
3313 int i;
27aacd28 3314
0e91fc1e
CH
3315 if (!f2fs_is_multi_device(sbi))
3316 return NULL;
27aacd28 3317
0e91fc1e
CH
3318 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3319 if (!devs)
3320 return ERR_PTR(-ENOMEM);
27aacd28
ST
3321
3322 for (i = 0; i < sbi->s_ndevs; i++)
0e91fc1e
CH
3323 devs[i] = FDEV(i).bdev;
3324 *num_devs = sbi->s_ndevs;
3325 return devs;
27aacd28
ST
3326}
3327
6f69f0ed 3328static const struct fscrypt_operations f2fs_cryptops = {
40e13e18 3329 .needs_bounce_pages = 1,
7a0263dc 3330 .has_32bit_inodes = 1,
5b118884 3331 .supports_subblock_data_units = 1,
5970fbad 3332 .legacy_key_prefix = "f2fs:",
0eee17e3
EB
3333 .get_context = f2fs_get_context,
3334 .set_context = f2fs_set_context,
ac4acb1f 3335 .get_dummy_policy = f2fs_get_dummy_policy,
0eee17e3 3336 .empty_dir = f2fs_empty_dir,
0eee17e3 3337 .has_stable_inodes = f2fs_has_stable_inodes,
27aacd28 3338 .get_devices = f2fs_get_devices,
0b81d077 3339};
0b81d077
JK
3340#endif
3341
aff063e2
JK
3342static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3343 u64 ino, u32 generation)
3344{
3345 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3346 struct inode *inode;
3347
4d57b86d 3348 if (f2fs_check_nid_range(sbi, ino))
910bb12d 3349 return ERR_PTR(-ESTALE);
aff063e2
JK
3350
3351 /*
3352 * f2fs_iget isn't quite right if the inode is currently unallocated!
3353 * However f2fs_iget currently does appropriate checks to handle stale
3354 * inodes so everything is OK.
3355 */
3356 inode = f2fs_iget(sb, ino);
3357 if (IS_ERR(inode))
3358 return ERR_CAST(inode);
6bacf52f 3359 if (unlikely(generation && inode->i_generation != generation)) {
aff063e2
JK
3360 /* we didn't find the right inode.. */
3361 iput(inode);
3362 return ERR_PTR(-ESTALE);
3363 }
3364 return inode;
3365}
3366
3367static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3368 int fh_len, int fh_type)
3369{
3370 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3371 f2fs_nfs_get_inode);
3372}
3373
3374static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3375 int fh_len, int fh_type)
3376{
3377 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3378 f2fs_nfs_get_inode);
3379}
3380
3381static const struct export_operations f2fs_export_ops = {
e21fc203 3382 .encode_fh = generic_encode_ino32_fh,
aff063e2
JK
3383 .fh_to_dentry = f2fs_fh_to_dentry,
3384 .fh_to_parent = f2fs_fh_to_parent,
3385 .get_parent = f2fs_get_parent,
3386};
3387
6d1451bf 3388loff_t max_file_blocks(struct inode *inode)
aff063e2 3389{
7a2af766 3390 loff_t result = 0;
6d1451bf 3391 loff_t leaf_count;
aff063e2 3392
7a2af766
CY
3393 /*
3394 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
6afc662e 3395 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
7a2af766
CY
3396 * space in inode.i_addr, it will be more safe to reassign
3397 * result as zero.
3398 */
3399
6d1451bf
CX
3400 if (inode && f2fs_compressed_file(inode))
3401 leaf_count = ADDRS_PER_BLOCK(inode);
3402 else
3403 leaf_count = DEF_ADDRS_PER_BLOCK;
3404
aff063e2
JK
3405 /* two direct node blocks */
3406 result += (leaf_count * 2);
3407
3408 /* two indirect node blocks */
3409 leaf_count *= NIDS_PER_BLOCK;
3410 result += (leaf_count * 2);
3411
3412 /* one double indirect node block */
3413 leaf_count *= NIDS_PER_BLOCK;
3414 result += leaf_count;
3415
a6a010f5
DR
3416 /*
3417 * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with
3418 * a 4K crypto data unit, we must restrict the max filesize to what can
3419 * fit within U32_MAX + 1 data units.
3420 */
3421
3273d8ad 3422 result = umin(result, F2FS_BYTES_TO_BLK(((loff_t)U32_MAX + 1) * 4096));
a6a010f5 3423
aff063e2
JK
3424 return result;
3425}
3426
5bcde455
CY
3427static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
3428 pgoff_t index, bool update)
fd694733 3429{
5bcde455 3430 struct bio *bio;
fd694733 3431 /* it's rare case, we can do fua all the time */
5bcde455
CY
3432 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
3433 int ret;
3434
3435 folio_lock(folio);
3436 folio_wait_writeback(folio);
3437 if (update)
3438 memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi),
3439 sizeof(struct f2fs_super_block));
3440 folio_mark_dirty(folio);
3441 folio_clear_dirty_for_io(folio);
3442 folio_start_writeback(folio);
3443 folio_unlock(folio);
3444
3445 bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
3446
3447 /* it doesn't need to set crypto context for superblock update */
0427e811 3448 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio->index);
5bcde455
CY
3449
3450 if (!bio_add_folio(bio, folio, folio_size(folio), 0))
3451 f2fs_bug_on(sbi, 1);
3452
3453 ret = submit_bio_wait(bio);
3454 folio_end_writeback(folio);
3455
3456 return ret;
fd694733
JK
3457}
3458
df728b0f 3459static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
5bcde455 3460 struct folio *folio, pgoff_t index)
9a59b62f 3461{
5bcde455 3462 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
df728b0f 3463 struct super_block *sb = sbi->sb;
9a59b62f
CY
3464 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3465 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3466 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3467 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3468 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3469 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3470 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3471 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3472 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3473 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3474 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3475 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3476 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
fd694733 3477 u64 main_end_blkaddr = main_blkaddr +
50438dbc 3478 ((u64)segment_count_main << log_blocks_per_seg);
fd694733 3479 u64 seg_end_blkaddr = segment0_blkaddr +
50438dbc 3480 ((u64)segment_count << log_blocks_per_seg);
9a59b62f
CY
3481
3482 if (segment0_blkaddr != cp_blkaddr) {
dcbb4c10
JP
3483 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3484 segment0_blkaddr, cp_blkaddr);
9a59b62f
CY
3485 return true;
3486 }
3487
3488 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3489 sit_blkaddr) {
dcbb4c10
JP
3490 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3491 cp_blkaddr, sit_blkaddr,
3492 segment_count_ckpt << log_blocks_per_seg);
9a59b62f
CY
3493 return true;
3494 }
3495
3496 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3497 nat_blkaddr) {
dcbb4c10
JP
3498 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3499 sit_blkaddr, nat_blkaddr,
3500 segment_count_sit << log_blocks_per_seg);
9a59b62f
CY
3501 return true;
3502 }
3503
3504 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3505 ssa_blkaddr) {
dcbb4c10
JP
3506 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3507 nat_blkaddr, ssa_blkaddr,
3508 segment_count_nat << log_blocks_per_seg);
9a59b62f
CY
3509 return true;
3510 }
3511
3512 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3513 main_blkaddr) {
dcbb4c10
JP
3514 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3515 ssa_blkaddr, main_blkaddr,
3516 segment_count_ssa << log_blocks_per_seg);
9a59b62f
CY
3517 return true;
3518 }
3519
fd694733 3520 if (main_end_blkaddr > seg_end_blkaddr) {
d89f5891
WX
3521 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3522 main_blkaddr, seg_end_blkaddr,
dcbb4c10 3523 segment_count_main << log_blocks_per_seg);
9a59b62f 3524 return true;
fd694733
JK
3525 } else if (main_end_blkaddr < seg_end_blkaddr) {
3526 int err = 0;
3527 char *res;
3528
3529 /* fix in-memory information all the time */
3530 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3531 segment0_blkaddr) >> log_blocks_per_seg);
3532
68f0453d 3533 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
df728b0f 3534 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
fd694733
JK
3535 res = "internally";
3536 } else {
5bcde455 3537 err = __f2fs_commit_super(sbi, folio, index, false);
fd694733
JK
3538 res = err ? "failed" : "done";
3539 }
d89f5891
WX
3540 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3541 res, main_blkaddr, seg_end_blkaddr,
dcbb4c10 3542 segment_count_main << log_blocks_per_seg);
fd694733
JK
3543 if (err)
3544 return true;
9a59b62f 3545 }
9a59b62f
CY
3546 return false;
3547}
3548
df728b0f 3549static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
5bcde455 3550 struct folio *folio, pgoff_t index)
aff063e2 3551{
f99ba9ad 3552 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
0cfe75c5 3553 block_t total_sections, blocks_per_seg;
5bcde455 3554 struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
d440c52d
JZ
3555 size_t crc_offset = 0;
3556 __u32 crc = 0;
3557
38fb6d0e
IZ
3558 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3559 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3560 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3561 return -EINVAL;
3562 }
3563
d440c52d 3564 /* Check checksum_offset and crc in superblock */
7beb01f7 3565 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
d440c52d
JZ
3566 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3567 if (crc_offset !=
3568 offsetof(struct f2fs_super_block, crc)) {
dcbb4c10
JP
3569 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3570 crc_offset);
38fb6d0e 3571 return -EFSCORRUPTED;
d440c52d
JZ
3572 }
3573 crc = le32_to_cpu(raw_super->crc);
d005af3b 3574 if (crc != f2fs_crc32(raw_super, crc_offset)) {
dcbb4c10 3575 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
38fb6d0e 3576 return -EFSCORRUPTED;
d440c52d
JZ
3577 }
3578 }
aff063e2 3579
5bf624c0 3580 /* only support block_size equals to PAGE_SIZE */
e584bbe8
CY
3581 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3582 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3583 le32_to_cpu(raw_super->log_blocksize),
3584 F2FS_BLKSIZE_BITS);
38fb6d0e 3585 return -EFSCORRUPTED;
a07ef784 3586 }
5c9b4692 3587
9a59b62f
CY
3588 /* check log blocks per segment */
3589 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
dcbb4c10
JP
3590 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3591 le32_to_cpu(raw_super->log_blocks_per_seg));
38fb6d0e 3592 return -EFSCORRUPTED;
9a59b62f
CY
3593 }
3594
d7e9a903 3595 /* Currently, support 512/1024/2048/4096/16K bytes sector size */
55cf9cb6
CY
3596 if (le32_to_cpu(raw_super->log_sectorsize) >
3597 F2FS_MAX_LOG_SECTOR_SIZE ||
3598 le32_to_cpu(raw_super->log_sectorsize) <
3599 F2FS_MIN_LOG_SECTOR_SIZE) {
dcbb4c10
JP
3600 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3601 le32_to_cpu(raw_super->log_sectorsize));
38fb6d0e 3602 return -EFSCORRUPTED;
a07ef784 3603 }
55cf9cb6
CY
3604 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3605 le32_to_cpu(raw_super->log_sectorsize) !=
3606 F2FS_MAX_LOG_SECTOR_SIZE) {
dcbb4c10
JP
3607 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3608 le32_to_cpu(raw_super->log_sectors_per_block),
3609 le32_to_cpu(raw_super->log_sectorsize));
38fb6d0e 3610 return -EFSCORRUPTED;
a07ef784 3611 }
9a59b62f 3612
0cfe75c5 3613 segment_count = le32_to_cpu(raw_super->segment_count);
f99ba9ad 3614 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
0cfe75c5
JK
3615 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3616 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3617 total_sections = le32_to_cpu(raw_super->section_count);
3618
3619 /* blocks_per_seg should be 512, given the above check */
447286eb 3620 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
0cfe75c5
JK
3621
3622 if (segment_count > F2FS_MAX_SEGMENT ||
3623 segment_count < F2FS_MIN_SEGMENTS) {
dcbb4c10 3624 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
38fb6d0e 3625 return -EFSCORRUPTED;
0cfe75c5
JK
3626 }
3627
f99ba9ad 3628 if (total_sections > segment_count_main || total_sections < 1 ||
0cfe75c5 3629 segs_per_sec > segment_count || !segs_per_sec) {
dcbb4c10
JP
3630 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3631 segment_count, total_sections, segs_per_sec);
38fb6d0e 3632 return -EFSCORRUPTED;
0cfe75c5
JK
3633 }
3634
3a22e9ac
CY
3635 if (segment_count_main != total_sections * segs_per_sec) {
3636 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3637 segment_count_main, total_sections, segs_per_sec);
3638 return -EFSCORRUPTED;
3639 }
3640
0cfe75c5 3641 if ((segment_count / segs_per_sec) < total_sections) {
dcbb4c10
JP
3642 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3643 segment_count, segs_per_sec, total_sections);
38fb6d0e 3644 return -EFSCORRUPTED;
0cfe75c5
JK
3645 }
3646
88960068 3647 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
dcbb4c10
JP
3648 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3649 segment_count, le64_to_cpu(raw_super->block_count));
38fb6d0e 3650 return -EFSCORRUPTED;
0cfe75c5
JK
3651 }
3652
9f701f6c
QS
3653 if (RDEV(0).path[0]) {
3654 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3655 int i = 1;
3656
3657 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3658 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3659 i++;
3660 }
3661 if (segment_count != dev_seg_count) {
3662 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3663 segment_count, dev_seg_count);
3664 return -EFSCORRUPTED;
3665 }
07eb1d69
CY
3666 } else {
3667 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3668 !bdev_is_zoned(sbi->sb->s_bdev)) {
3669 f2fs_info(sbi, "Zoned block device path is missing");
3670 return -EFSCORRUPTED;
3671 }
9f701f6c
QS
3672 }
3673
42bf546c 3674 if (secs_per_zone > total_sections || !secs_per_zone) {
dcbb4c10
JP
3675 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3676 secs_per_zone, total_sections);
38fb6d0e 3677 return -EFSCORRUPTED;
0cfe75c5
JK
3678 }
3679 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3680 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3681 (le32_to_cpu(raw_super->extension_count) +
3682 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
dcbb4c10
JP
3683 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3684 le32_to_cpu(raw_super->extension_count),
3685 raw_super->hot_ext_count,
3686 F2FS_MAX_EXTENSION);
38fb6d0e 3687 return -EFSCORRUPTED;
0cfe75c5
JK
3688 }
3689
65ddf656
CY
3690 if (le32_to_cpu(raw_super->cp_payload) >=
3691 (blocks_per_seg - F2FS_CP_PACKS -
3692 NR_CURSEG_PERSIST_TYPE)) {
3693 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
dcbb4c10 3694 le32_to_cpu(raw_super->cp_payload),
65ddf656
CY
3695 blocks_per_seg - F2FS_CP_PACKS -
3696 NR_CURSEG_PERSIST_TYPE);
38fb6d0e 3697 return -EFSCORRUPTED;
0cfe75c5
JK
3698 }
3699
9a59b62f
CY
3700 /* check reserved ino info */
3701 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3702 le32_to_cpu(raw_super->meta_ino) != 2 ||
3703 le32_to_cpu(raw_super->root_ino) != 3) {
dcbb4c10
JP
3704 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3705 le32_to_cpu(raw_super->node_ino),
3706 le32_to_cpu(raw_super->meta_ino),
3707 le32_to_cpu(raw_super->root_ino));
38fb6d0e 3708 return -EFSCORRUPTED;
9a59b62f
CY
3709 }
3710
3711 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
5bcde455 3712 if (sanity_check_area_boundary(sbi, folio, index))
38fb6d0e 3713 return -EFSCORRUPTED;
9a59b62f 3714
aff063e2
JK
3715 return 0;
3716}
3717
4d57b86d 3718int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
aff063e2
JK
3719{
3720 unsigned int total, fsmeta;
577e3495
JK
3721 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3722 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2040fce8 3723 unsigned int ovp_segments, reserved_segments;
15d3042a 3724 unsigned int main_segs, blocks_per_seg;
c77ec61c
CY
3725 unsigned int sit_segs, nat_segs;
3726 unsigned int sit_bitmap_size, nat_bitmap_size;
3727 unsigned int log_blocks_per_seg;
9dc956b2 3728 unsigned int segment_count_main;
e494c2f9 3729 unsigned int cp_pack_start_sum, cp_payload;
7b63f72f
CY
3730 block_t user_block_count, valid_user_blocks;
3731 block_t avail_node_count, valid_node_count;
65ddf656 3732 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
5db0d252 3733 unsigned int sit_blk_cnt;
042be0f8 3734 int i, j;
aff063e2
JK
3735
3736 total = le32_to_cpu(raw_super->segment_count);
3737 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
c77ec61c
CY
3738 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3739 fsmeta += sit_segs;
3740 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3741 fsmeta += nat_segs;
aff063e2
JK
3742 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3743 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3744
6bacf52f 3745 if (unlikely(fsmeta >= total))
aff063e2 3746 return 1;
577e3495 3747
2040fce8
JK
3748 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3749 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3750
a7d9fe3c
JK
3751 if (!f2fs_sb_has_readonly(sbi) &&
3752 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
2040fce8 3753 ovp_segments == 0 || reserved_segments == 0)) {
dcbb4c10 3754 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
2040fce8
JK
3755 return 1;
3756 }
9dc956b2 3757 user_block_count = le64_to_cpu(ckpt->user_block_count);
a7d9fe3c
JK
3758 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3759 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
9dc956b2
CY
3760 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3761 if (!user_block_count || user_block_count >=
3762 segment_count_main << log_blocks_per_seg) {
dcbb4c10
JP
3763 f2fs_err(sbi, "Wrong user_block_count: %u",
3764 user_block_count);
9dc956b2
CY
3765 return 1;
3766 }
3767
7b63f72f
CY
3768 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3769 if (valid_user_blocks > user_block_count) {
dcbb4c10
JP
3770 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3771 valid_user_blocks, user_block_count);
7b63f72f
CY
3772 return 1;
3773 }
3774
3775 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
27cae0bc 3776 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
7b63f72f 3777 if (valid_node_count > avail_node_count) {
dcbb4c10
JP
3778 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3779 valid_node_count, avail_node_count);
7b63f72f
CY
3780 return 1;
3781 }
3782
15d3042a 3783 main_segs = le32_to_cpu(raw_super->segment_count_main);
a60108f7 3784 blocks_per_seg = BLKS_PER_SEG(sbi);
15d3042a
JQ
3785
3786 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3787 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3788 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3789 return 1;
a7d9fe3c
JK
3790
3791 if (f2fs_sb_has_readonly(sbi))
3792 goto check_data;
3793
042be0f8
CY
3794 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3795 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3796 le32_to_cpu(ckpt->cur_node_segno[j])) {
dcbb4c10
JP
3797 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3798 i, j,
3799 le32_to_cpu(ckpt->cur_node_segno[i]));
042be0f8
CY
3800 return 1;
3801 }
3802 }
15d3042a 3803 }
a7d9fe3c 3804check_data:
15d3042a
JQ
3805 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3806 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3807 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3808 return 1;
a7d9fe3c
JK
3809
3810 if (f2fs_sb_has_readonly(sbi))
3811 goto skip_cross;
3812
042be0f8
CY
3813 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3814 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3815 le32_to_cpu(ckpt->cur_data_segno[j])) {
dcbb4c10
JP
3816 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3817 i, j,
3818 le32_to_cpu(ckpt->cur_data_segno[i]));
042be0f8
CY
3819 return 1;
3820 }
3821 }
3822 }
3823 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1166c1f2 3824 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
042be0f8
CY
3825 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3826 le32_to_cpu(ckpt->cur_data_segno[j])) {
1166c1f2 3827 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
dcbb4c10
JP
3828 i, j,
3829 le32_to_cpu(ckpt->cur_node_segno[i]));
042be0f8
CY
3830 return 1;
3831 }
3832 }
15d3042a 3833 }
a7d9fe3c 3834skip_cross:
c77ec61c
CY
3835 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3836 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
c77ec61c
CY
3837
3838 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3839 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
dcbb4c10
JP
3840 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3841 sit_bitmap_size, nat_bitmap_size);
c77ec61c
CY
3842 return 1;
3843 }
e494c2f9 3844
5db0d252
CY
3845 sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK);
3846 if (sit_bitmap_size * 8 < sit_blk_cnt) {
3847 f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u",
3848 sit_bitmap_size, sit_blk_cnt);
3849 return 1;
3850 }
3851
e494c2f9
CY
3852 cp_pack_start_sum = __start_sum_addr(sbi);
3853 cp_payload = __cp_payload(sbi);
3854 if (cp_pack_start_sum < cp_payload + 1 ||
3855 cp_pack_start_sum > blocks_per_seg - 1 -
d0b9e42a 3856 NR_CURSEG_PERSIST_TYPE) {
dcbb4c10
JP
3857 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3858 cp_pack_start_sum);
e494c2f9
CY
3859 return 1;
3860 }
c77ec61c 3861
5dae2d39
CY
3862 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3863 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
2d008835
CY
3864 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3865 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3866 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
dcbb4c10 3867 le32_to_cpu(ckpt->checksum_offset));
5dae2d39
CY
3868 return 1;
3869 }
3870
65ddf656
CY
3871 nat_blocks = nat_segs << log_blocks_per_seg;
3872 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3873 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3874 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3875 (cp_payload + F2FS_CP_PACKS +
3876 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3877 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3878 cp_payload, nat_bits_blocks);
ca98d721 3879 return 1;
65ddf656
CY
3880 }
3881
1e968fdf 3882 if (unlikely(f2fs_cp_error(sbi))) {
dcbb4c10 3883 f2fs_err(sbi, "A bug case: need to run fsck");
577e3495
JK
3884 return 1;
3885 }
aff063e2
JK
3886 return 0;
3887}
3888
3889static void init_sb_info(struct f2fs_sb_info *sbi)
3890{
3891 struct f2fs_super_block *raw_super = sbi->raw_super;
089842de 3892 int i;
aff063e2
JK
3893
3894 sbi->log_sectors_per_block =
3895 le32_to_cpu(raw_super->log_sectors_per_block);
3896 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
447286eb 3897 sbi->blocksize = BIT(sbi->log_blocksize);
aff063e2 3898 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
447286eb 3899 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
aff063e2
JK
3900 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3901 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3902 sbi->total_sections = le32_to_cpu(raw_super->section_count);
45809cd3
CY
3903 sbi->total_node_count = SEGS_TO_BLKS(sbi,
3904 ((le32_to_cpu(raw_super->segment_count_nat) / 2) *
3905 NAT_ENTRY_PER_BLOCK));
b9ec1094
YL
3906 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3907 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3908 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
5ec4e49f 3909 sbi->cur_victim_sec = NULL_SECNO;
c86868bb 3910 sbi->gc_mode = GC_NORMAL;
e3080b01
CY
3911 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3912 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
b1c57c1c 3913 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
a60108f7 3914 sbi->migration_granularity = SEGS_PER_SEC(sbi);
8c890c4c
DJ
3915 sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ?
3916 DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi);
0f6b56ec 3917 sbi->seq_file_ra_mul = MIN_RA_MUL;
6691d940
DJ
3918 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3919 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
e5a0db6a 3920 spin_lock_init(&sbi->gc_remaining_trials_lock);
f8e2f32b 3921 atomic64_set(&sbi->current_atomic_write, 0);
aff063e2 3922
ab9fa662 3923 sbi->dir_level = DEF_DIR_LEVEL;
6beceb54 3924 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
d0239e1b 3925 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
a7d10cf3
ST
3926 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3927 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
4354994f 3928 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
03f2c02d
JK
3929 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3930 DEF_UMOUNT_DISCARD_TIMEOUT;
caf0047e 3931 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2658e50d 3932
35782b23
JK
3933 for (i = 0; i < NR_COUNT_TYPE; i++)
3934 atomic_set(&sbi->nr_pages[i], 0);
3935
c29fd0c0
CY
3936 for (i = 0; i < META; i++)
3937 atomic_set(&sbi->wb_sync_req[i], 0);
687de7f1 3938
2658e50d
JK
3939 INIT_LIST_HEAD(&sbi->s_list);
3940 mutex_init(&sbi->umount_mutex);
e4544b63 3941 init_f2fs_rwsem(&sbi->io_order_lock);
aaec2b1d 3942 spin_lock_init(&sbi->cp_lock);
1228b482
CY
3943
3944 sbi->dirty_device = 0;
3945 spin_lock_init(&sbi->dev_lock);
d0d3f1b3 3946
e4544b63
TM
3947 init_f2fs_rwsem(&sbi->sb_lock);
3948 init_f2fs_rwsem(&sbi->pin_sem);
aff063e2
JK
3949}
3950
523be8a6
JK
3951static int init_percpu_info(struct f2fs_sb_info *sbi)
3952{
35782b23 3953 int err;
41382ec4 3954
513c5f37
JK
3955 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3956 if (err)
3957 return err;
3958
47c8ebcc
JK
3959 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3960 if (err)
3961 goto err_valid_block;
3962
4a70e255 3963 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
41382ec4 3964 GFP_KERNEL);
4a70e255 3965 if (err)
47c8ebcc
JK
3966 goto err_node_block;
3967 return 0;
4a70e255 3968
47c8ebcc
JK
3969err_node_block:
3970 percpu_counter_destroy(&sbi->rf_node_block_count);
3971err_valid_block:
3972 percpu_counter_destroy(&sbi->alloc_valid_block_count);
4a70e255 3973 return err;
523be8a6
JK
3974}
3975
178053e2 3976#ifdef CONFIG_BLK_DEV_ZONED
de881df9
AR
3977
3978struct f2fs_report_zones_args {
b771aadc 3979 struct f2fs_sb_info *sbi;
de881df9 3980 struct f2fs_dev_info *dev;
de881df9
AR
3981};
3982
d4100351 3983static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
de881df9 3984 void *data)
d4100351 3985{
de881df9 3986 struct f2fs_report_zones_args *rz_args = data;
b771aadc
JK
3987 block_t unusable_blocks = (zone->len - zone->capacity) >>
3988 F2FS_LOG_SECTORS_PER_BLOCK;
de881df9
AR
3989
3990 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3991 return 0;
3992
3993 set_bit(idx, rz_args->dev->blkz_seq);
b771aadc
JK
3994 if (!rz_args->sbi->unusable_blocks_per_sec) {
3995 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3996 return 0;
3997 }
3998 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3999 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
4000 return -EINVAL;
4001 }
d4100351
CH
4002 return 0;
4003}
4004
3c62be17 4005static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
178053e2 4006{
3c62be17 4007 struct block_device *bdev = FDEV(devi).bdev;
a782483c 4008 sector_t nr_sectors = bdev_nr_sectors(bdev);
de881df9 4009 struct f2fs_report_zones_args rep_zone_arg;
d46db459 4010 u64 zone_sectors;
0f9b1214 4011 unsigned int max_open_zones;
d4100351 4012 int ret;
178053e2 4013
7beb01f7 4014 if (!f2fs_sb_has_blkzoned(sbi))
178053e2
DLM
4015 return 0;
4016
0f9b1214
WQ
4017 if (bdev_is_zoned(FDEV(devi).bdev)) {
4018 max_open_zones = bdev_max_open_zones(bdev);
4019 if (max_open_zones && (max_open_zones < sbi->max_open_zones))
4020 sbi->max_open_zones = max_open_zones;
4021 if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
4022 f2fs_err(sbi,
4023 "zoned: max open zones %u is too small, need at least %u open zones",
4024 sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
4025 return -EINVAL;
4026 }
4027 }
4028
d46db459 4029 zone_sectors = bdev_zone_sectors(bdev);
3c62be17 4030 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
d46db459 4031 SECTOR_TO_BLOCK(zone_sectors))
3c62be17 4032 return -EINVAL;
d46db459 4033 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
2e2c6e9b
JK
4034 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
4035 sbi->blocks_per_blkz);
d46db459 4036 if (nr_sectors & (zone_sectors - 1))
3c62be17 4037 FDEV(devi).nr_blkz++;
178053e2 4038
0b6d4ca0 4039 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
95175daf
DLM
4040 BITS_TO_LONGS(FDEV(devi).nr_blkz)
4041 * sizeof(unsigned long),
4042 GFP_KERNEL);
4043 if (!FDEV(devi).blkz_seq)
178053e2
DLM
4044 return -ENOMEM;
4045
b771aadc 4046 rep_zone_arg.sbi = sbi;
de881df9 4047 rep_zone_arg.dev = &FDEV(devi);
de881df9 4048
d4100351 4049 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
de881df9 4050 &rep_zone_arg);
d4100351
CH
4051 if (ret < 0)
4052 return ret;
d4100351 4053 return 0;
178053e2
DLM
4054}
4055#endif
4056
9076a75f
GZ
4057/*
4058 * Read f2fs raw super block.
2b39e907
SL
4059 * Because we have two copies of super block, so read both of them
4060 * to get the first valid one. If any one of them is broken, we pass
4061 * them recovery flag back to the caller.
9076a75f 4062 */
df728b0f 4063static int read_raw_super_block(struct f2fs_sb_info *sbi,
9076a75f 4064 struct f2fs_super_block **raw_super,
e8240f65 4065 int *valid_super_block, int *recovery)
14d7e9de 4066{
df728b0f 4067 struct super_block *sb = sbi->sb;
2b39e907 4068 int block;
5bcde455 4069 struct folio *folio;
fd694733 4070 struct f2fs_super_block *super;
da554e48 4071 int err = 0;
14d7e9de 4072
b39f0de2
YH
4073 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
4074 if (!super)
4075 return -ENOMEM;
2b39e907
SL
4076
4077 for (block = 0; block < 2; block++) {
5bcde455
CY
4078 folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL);
4079 if (IS_ERR(folio)) {
dcbb4c10
JP
4080 f2fs_err(sbi, "Unable to read %dth superblock",
4081 block + 1);
5bcde455 4082 err = PTR_ERR(folio);
ed352042 4083 *recovery = 1;
2b39e907
SL
4084 continue;
4085 }
14d7e9de 4086
2b39e907 4087 /* sanity checking of raw super */
5bcde455 4088 err = sanity_check_raw_super(sbi, folio, block);
38fb6d0e 4089 if (err) {
dcbb4c10
JP
4090 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
4091 block + 1);
5bcde455 4092 folio_put(folio);
ed352042 4093 *recovery = 1;
2b39e907
SL
4094 continue;
4095 }
14d7e9de 4096
2b39e907 4097 if (!*raw_super) {
5bcde455 4098 memcpy(super, F2FS_SUPER_BLOCK(folio, block),
fd694733 4099 sizeof(*super));
2b39e907
SL
4100 *valid_super_block = block;
4101 *raw_super = super;
4102 }
5bcde455 4103 folio_put(folio);
da554e48 4104 }
4105
da554e48 4106 /* No valid superblock */
2b39e907 4107 if (!*raw_super)
742532d1 4108 kfree(super);
2b39e907
SL
4109 else
4110 err = 0;
da554e48 4111
2b39e907 4112 return err;
14d7e9de 4113}
4114
fd694733 4115int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
26d815ad 4116{
5bcde455
CY
4117 struct folio *folio;
4118 pgoff_t index;
d440c52d 4119 __u32 crc = 0;
26d815ad
JK
4120 int err;
4121
df728b0f 4122 if ((recover && f2fs_readonly(sbi->sb)) ||
68f0453d 4123 f2fs_hw_is_readonly(sbi)) {
df728b0f 4124 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
f2353d7b 4125 return -EROFS;
df728b0f 4126 }
f2353d7b 4127
d440c52d 4128 /* we should update superblock crc here */
7beb01f7 4129 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
d005af3b 4130 crc = f2fs_crc32(F2FS_RAW_SUPER(sbi),
d440c52d
JZ
4131 offsetof(struct f2fs_super_block, crc));
4132 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
4133 }
4134
fd694733 4135 /* write back-up superblock first */
5bcde455
CY
4136 index = sbi->valid_super_block ? 0 : 1;
4137 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4138 if (IS_ERR(folio))
4139 return PTR_ERR(folio);
4140 err = __f2fs_commit_super(sbi, folio, index, true);
4141 folio_put(folio);
c5bda1c8
CY
4142
4143 /* if we are in recovery path, skip writing valid superblock */
4144 if (recover || err)
5d909cdb 4145 return err;
26d815ad
JK
4146
4147 /* write current valid superblock */
5bcde455
CY
4148 index = sbi->valid_super_block;
4149 folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
4150 if (IS_ERR(folio))
4151 return PTR_ERR(folio);
4152 err = __f2fs_commit_super(sbi, folio, index, true);
4153 folio_put(folio);
fd694733 4154 return err;
26d815ad
JK
4155}
4156
b62e71be
CY
4157static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4158{
4159 unsigned long flags;
4160
4161 spin_lock_irqsave(&sbi->error_lock, flags);
4162 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4163 sbi->stop_reason[reason]++;
4164 spin_unlock_irqrestore(&sbi->error_lock, flags);
4165}
4166
4167static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
a9cfee0e
CY
4168{
4169 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
b62e71be 4170 unsigned long flags;
a9cfee0e
CY
4171 int err;
4172
a9cfee0e
CY
4173 f2fs_down_write(&sbi->sb_lock);
4174
b62e71be 4175 spin_lock_irqsave(&sbi->error_lock, flags);
901c12d1
CY
4176 if (sbi->error_dirty) {
4177 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4178 MAX_F2FS_ERRORS);
4179 sbi->error_dirty = false;
4180 }
b62e71be
CY
4181 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4182 spin_unlock_irqrestore(&sbi->error_lock, flags);
a9cfee0e
CY
4183
4184 err = f2fs_commit_super(sbi, false);
b62e71be 4185
95fa90c9 4186 f2fs_up_write(&sbi->sb_lock);
b62e71be 4187 if (err)
0b8eb814
CY
4188 f2fs_err_ratelimited(sbi,
4189 "f2fs_commit_super fails to record stop_reason, err:%d",
4190 err);
95fa90c9
CY
4191}
4192
1aa161e4 4193void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
95fa90c9 4194{
b62e71be
CY
4195 unsigned long flags;
4196
4197 spin_lock_irqsave(&sbi->error_lock, flags);
95fa90c9
CY
4198 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4199 set_bit(flag, (unsigned long *)sbi->errors);
4200 sbi->error_dirty = true;
4201 }
b62e71be 4202 spin_unlock_irqrestore(&sbi->error_lock, flags);
95fa90c9
CY
4203}
4204
4205static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4206{
b62e71be 4207 unsigned long flags;
95fa90c9
CY
4208 bool need_update = false;
4209
b62e71be 4210 spin_lock_irqsave(&sbi->error_lock, flags);
95fa90c9
CY
4211 if (sbi->error_dirty) {
4212 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4213 MAX_F2FS_ERRORS);
4214 sbi->error_dirty = false;
4215 need_update = true;
4216 }
b62e71be 4217 spin_unlock_irqrestore(&sbi->error_lock, flags);
95fa90c9
CY
4218
4219 return need_update;
4220}
a9cfee0e 4221
901c12d1 4222static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
95fa90c9
CY
4223{
4224 int err;
4225
95fa90c9
CY
4226 f2fs_down_write(&sbi->sb_lock);
4227
4228 if (!f2fs_update_errors(sbi))
4229 goto out_unlock;
4230
4231 err = f2fs_commit_super(sbi, false);
4232 if (err)
0b8eb814
CY
4233 f2fs_err_ratelimited(sbi,
4234 "f2fs_commit_super fails to record errors:%u, err:%d",
4235 error, err);
95fa90c9 4236out_unlock:
a9cfee0e
CY
4237 f2fs_up_write(&sbi->sb_lock);
4238}
4239
901c12d1
CY
4240void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4241{
4242 f2fs_save_errors(sbi, error);
4243 f2fs_record_errors(sbi, error);
4244}
4245
4246void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4247{
4248 f2fs_save_errors(sbi, error);
4249
4250 if (!sbi->error_dirty)
4251 return;
4252 if (!test_bit(error, (unsigned long *)sbi->errors))
4253 return;
4254 schedule_work(&sbi->s_error_work);
4255}
4256
b62e71be
CY
4257static bool system_going_down(void)
4258{
4259 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4260 || system_state == SYSTEM_RESTART;
4261}
4262
f10a8903 4263void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason)
b62e71be
CY
4264{
4265 struct super_block *sb = sbi->sb;
4266 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4267 bool continue_fs = !shutdown &&
4268 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4269
4270 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4271
4272 if (!f2fs_hw_is_readonly(sbi)) {
4273 save_stop_reason(sbi, reason);
4274
f10a8903
CY
4275 /*
4276 * always create an asynchronous task to record stop_reason
4277 * in order to avoid potential deadlock when running into
4278 * f2fs_record_stop_reason() synchronously.
4279 */
4280 schedule_work(&sbi->s_error_work);
b62e71be
CY
4281 }
4282
4283 /*
4284 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4285 * could panic during 'reboot -f' as the underlying device got already
4286 * disabled.
4287 */
4288 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4289 !shutdown && !system_going_down() &&
4290 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4291 panic("F2FS-fs (device %s): panic forced after error\n",
4292 sb->s_id);
4293
4294 if (shutdown)
4295 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4f91f074
CY
4296 else
4297 dump_stack();
b62e71be 4298
3bdb7f16
JK
4299 /*
4300 * Continue filesystem operators if errors=continue. Should not set
4301 * RO by shutdown, since RO bypasses thaw_super which can hang the
4302 * system.
4303 */
4304 if (continue_fs || f2fs_readonly(sb) || shutdown) {
4305 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
b62e71be 4306 return;
3bdb7f16 4307 }
b62e71be
CY
4308
4309 f2fs_warn(sbi, "Remounting filesystem read-only");
930c6ab9 4310
b62e71be 4311 /*
930c6ab9
CY
4312 * We have already set CP_ERROR_FLAG flag to stop all updates
4313 * to filesystem, so it doesn't need to set SB_RDONLY flag here
4314 * because the flag should be set covered w/ sb->s_umount semaphore
4315 * via remount procedure, otherwise, it will confuse code like
4316 * freeze_super() which will lead to deadlocks and other problems.
b62e71be 4317 */
b62e71be
CY
4318}
4319
4320static void f2fs_record_error_work(struct work_struct *work)
4321{
4322 struct f2fs_sb_info *sbi = container_of(work,
4323 struct f2fs_sb_info, s_error_work);
4324
4325 f2fs_record_stop_reason(sbi);
4326}
4327
dc6d9ef5 4328static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi)
5bc5aae8 4329{
dc6d9ef5
CY
4330#ifdef CONFIG_BLK_DEV_ZONED
4331 unsigned int zoneno, total_zones;
5bc5aae8
CY
4332 int devi;
4333
dc6d9ef5
CY
4334 if (!f2fs_sb_has_blkzoned(sbi))
4335 return NULL_SEGNO;
4336
4337 for (devi = 0; devi < sbi->s_ndevs; devi++) {
4338 if (!bdev_is_zoned(FDEV(devi).bdev))
4339 continue;
4340
4341 total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments);
4342
4343 for (zoneno = 0; zoneno < total_zones; zoneno++) {
4344 unsigned int segs, blks;
4345
4346 if (!f2fs_zone_is_seq(sbi, devi, zoneno))
4347 continue;
4348
4349 segs = GET_SEG_FROM_SEC(sbi,
4350 zoneno * sbi->secs_per_zone);
4351 blks = SEGS_TO_BLKS(sbi, segs);
4352 return GET_SEGNO(sbi, FDEV(devi).start_blk + blks);
4353 }
4354 }
4355#endif
4356 return NULL_SEGNO;
5bc5aae8
CY
4357}
4358
3c62be17
JK
4359static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4360{
4361 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
7bb3a371 4362 unsigned int max_devices = MAX_DEVICES;
71f2c820 4363 unsigned int logical_blksize;
05bdb996 4364 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
3c62be17
JK
4365 int i;
4366
7bb3a371
MS
4367 /* Initialize single device information */
4368 if (!RDEV(0).path[0]) {
4369 if (!bdev_is_zoned(sbi->sb->s_bdev))
3c62be17 4370 return 0;
7bb3a371
MS
4371 max_devices = 1;
4372 }
3c62be17 4373
7bb3a371
MS
4374 /*
4375 * Initialize multiple devices information, or single
4376 * zoned block device information.
4377 */
026f0507
KC
4378 sbi->devs = f2fs_kzalloc(sbi,
4379 array_size(max_devices,
4380 sizeof(struct f2fs_dev_info)),
4381 GFP_KERNEL);
7bb3a371
MS
4382 if (!sbi->devs)
4383 return -ENOMEM;
3c62be17 4384
71f2c820
CY
4385 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4386 sbi->aligned_blksize = true;
0f9b1214
WQ
4387#ifdef CONFIG_BLK_DEV_ZONED
4388 sbi->max_open_zones = UINT_MAX;
8444ce52 4389 sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ;
0f9b1214 4390#endif
71f2c820 4391
7bb3a371 4392 for (i = 0; i < max_devices; i++) {
dc6d9ef5
CY
4393 if (max_devices == 1) {
4394 FDEV(i).total_segments =
4395 le32_to_cpu(raw_super->segment_count_main);
4396 FDEV(i).start_blk = 0;
4397 FDEV(i).end_blk = FDEV(i).total_segments *
4398 BLKS_PER_SEG(sbi);
4399 }
4400
51bf8d3c 4401 if (i == 0)
512383ae 4402 FDEV(0).bdev_file = sbi->sb->s_bdev_file;
51bf8d3c 4403 else if (!RDEV(i).path[0])
7bb3a371
MS
4404 break;
4405
51bf8d3c 4406 if (max_devices > 1) {
7bb3a371
MS
4407 /* Multi-device mount */
4408 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4409 FDEV(i).total_segments =
4410 le32_to_cpu(RDEV(i).total_segments);
4411 if (i == 0) {
4412 FDEV(i).start_blk = 0;
4413 FDEV(i).end_blk = FDEV(i).start_blk +
45809cd3
CY
4414 SEGS_TO_BLKS(sbi,
4415 FDEV(i).total_segments) - 1 +
4416 le32_to_cpu(raw_super->segment0_blkaddr);
7bb3a371
MS
4417 } else {
4418 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4419 FDEV(i).end_blk = FDEV(i).start_blk +
45809cd3
CY
4420 SEGS_TO_BLKS(sbi,
4421 FDEV(i).total_segments) - 1;
512383ae 4422 FDEV(i).bdev_file = bdev_file_open_by_path(
2b107946 4423 FDEV(i).path, mode, sbi->sb, NULL);
7bb3a371 4424 }
7bb3a371 4425 }
512383ae
CB
4426 if (IS_ERR(FDEV(i).bdev_file))
4427 return PTR_ERR(FDEV(i).bdev_file);
3c62be17 4428
512383ae 4429 FDEV(i).bdev = file_bdev(FDEV(i).bdev_file);
3c62be17
JK
4430 /* to release errored devices */
4431 sbi->s_ndevs = i + 1;
4432
71f2c820
CY
4433 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4434 sbi->aligned_blksize = false;
4435
3c62be17 4436#ifdef CONFIG_BLK_DEV_ZONED
7437bb73
CH
4437 if (bdev_is_zoned(FDEV(i).bdev)) {
4438 if (!f2fs_sb_has_blkzoned(sbi)) {
4439 f2fs_err(sbi, "Zoned block device feature not enabled");
4440 return -EINVAL;
4441 }
3c62be17 4442 if (init_blkz_info(sbi, i)) {
dcbb4c10 4443 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
3c62be17
JK
4444 return -EINVAL;
4445 }
7bb3a371
MS
4446 if (max_devices == 1)
4447 break;
7437bb73 4448 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)",
dcbb4c10
JP
4449 i, FDEV(i).path,
4450 FDEV(i).total_segments,
7437bb73 4451 FDEV(i).start_blk, FDEV(i).end_blk);
3c62be17
JK
4452 continue;
4453 }
4454#endif
dcbb4c10
JP
4455 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4456 i, FDEV(i).path,
4457 FDEV(i).total_segments,
4458 FDEV(i).start_blk, FDEV(i).end_blk);
4459 }
3c62be17
JK
4460 return 0;
4461}
4462
5aba5430
DR
4463static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4464{
5298d4bf 4465#if IS_ENABLED(CONFIG_UNICODE)
eca4873e 4466 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
5aba5430
DR
4467 const struct f2fs_sb_encodings *encoding_info;
4468 struct unicode_map *encoding;
4469 __u16 encoding_flags;
4470
86e80575
CH
4471 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4472 if (!encoding_info) {
5aba5430
DR
4473 f2fs_err(sbi,
4474 "Encoding requested by superblock is unknown");
4475 return -EINVAL;
4476 }
4477
86e80575 4478 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
5aba5430
DR
4479 encoding = utf8_load(encoding_info->version);
4480 if (IS_ERR(encoding)) {
4481 f2fs_err(sbi,
49bd03cc 4482 "can't mount with superblock charset: %s-%u.%u.%u "
5aba5430 4483 "not supported by the kernel. flags: 0x%x.",
49bd03cc
CH
4484 encoding_info->name,
4485 unicode_major(encoding_info->version),
4486 unicode_minor(encoding_info->version),
4487 unicode_rev(encoding_info->version),
5aba5430
DR
4488 encoding_flags);
4489 return PTR_ERR(encoding);
4490 }
4491 f2fs_info(sbi, "Using encoding defined by superblock: "
49bd03cc
CH
4492 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4493 unicode_major(encoding_info->version),
4494 unicode_minor(encoding_info->version),
4495 unicode_rev(encoding_info->version),
4496 encoding_flags);
5aba5430 4497
eca4873e
DR
4498 sbi->sb->s_encoding = encoding;
4499 sbi->sb->s_encoding_flags = encoding_flags;
5aba5430
DR
4500 }
4501#else
4502 if (f2fs_sb_has_casefold(sbi)) {
4503 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4504 return -EINVAL;
4505 }
4506#endif
4507 return 0;
4508}
4509
84b89e5d
JK
4510static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4511{
84b89e5d 4512 /* adjust parameters according to the volume size */
777cd95b 4513 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4f993264 4514 if (f2fs_block_unit_discard(sbi))
1cd2e6d5
YL
4515 SM_I(sbi)->dcc_info->discard_granularity =
4516 MIN_DISCARD_GRANULARITY;
c5bf8348
YL
4517 if (!f2fs_lfs_mode(sbi))
4518 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4519 BIT(F2FS_IPU_HONOR_OPU_WRITE);
84b89e5d 4520 }
4cac90d5 4521
66aee5aa 4522 sbi->readdir_ra = true;
84b89e5d
JK
4523}
4524
aff063e2
JK
4525static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4526{
4527 struct f2fs_sb_info *sbi;
da554e48 4528 struct f2fs_super_block *raw_super;
aff063e2 4529 struct inode *root;
99e3e858 4530 int err;
aa2c8c43 4531 bool skip_recovery = false, need_fsck = false;
dabc4a5c 4532 char *options = NULL;
e8240f65 4533 int recovery, i, valid_super_block;
8f1dbbbb 4534 struct curseg_info *seg_i;
aa2c8c43 4535 int retry_cnt = 1;
e1bb7d3d
CY
4536#ifdef CONFIG_QUOTA
4537 bool quota_enabled = false;
4538#endif
aff063e2 4539
ed2e621a 4540try_onemore:
da554e48 4541 err = -EINVAL;
4542 raw_super = NULL;
e8240f65 4543 valid_super_block = -1;
da554e48 4544 recovery = 0;
4545
aff063e2
JK
4546 /* allocate memory for f2fs-specific super block info */
4547 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4548 if (!sbi)
4549 return -ENOMEM;
4550
df728b0f
JK
4551 sbi->sb = sb;
4552
92b4cf5b
TH
4553 /* initialize locks within allocated memory */
4554 init_f2fs_rwsem(&sbi->gc_lock);
4555 mutex_init(&sbi->writepages);
4556 init_f2fs_rwsem(&sbi->cp_global_sem);
4557 init_f2fs_rwsem(&sbi->node_write);
4558 init_f2fs_rwsem(&sbi->node_change);
4559 spin_lock_init(&sbi->stat_lock);
4560 init_f2fs_rwsem(&sbi->cp_rwsem);
4561 init_f2fs_rwsem(&sbi->quota_sem);
4562 init_waitqueue_head(&sbi->cp_wait);
4563 spin_lock_init(&sbi->error_lock);
4564
4565 for (i = 0; i < NR_INODE_TYPE; i++) {
4566 INIT_LIST_HEAD(&sbi->inode_list[i]);
4567 spin_lock_init(&sbi->inode_lock[i]);
4568 }
4569 mutex_init(&sbi->flush_lock);
4570
ff9234ad 4571 /* set a block size */
6bacf52f 4572 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
dcbb4c10 4573 f2fs_err(sbi, "unable to set blocksize");
aff063e2 4574 goto free_sbi;
a07ef784 4575 }
aff063e2 4576
df728b0f 4577 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
e8240f65 4578 &recovery);
9076a75f
GZ
4579 if (err)
4580 goto free_sbi;
4581
5fb08372 4582 sb->s_fs_info = sbi;
52763a4b
JK
4583 sbi->raw_super = raw_super;
4584
b62e71be 4585 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
92b4cf5b 4586 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
b62e71be 4587 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
92b4cf5b 4588
704956ec 4589 /* precompute checksum seed for metadata */
7beb01f7 4590 if (f2fs_sb_has_inode_chksum(sbi))
d005af3b
EB
4591 sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid,
4592 sizeof(raw_super->uuid));
704956ec 4593
458c15df 4594 default_options(sbi, false);
aff063e2 4595 /* parse mount options */
dabc4a5c
JK
4596 options = kstrdup((const char *)data, GFP_KERNEL);
4597 if (data && !options) {
4598 err = -ENOMEM;
aff063e2 4599 goto free_sb_buf;
dabc4a5c
JK
4600 }
4601
71e9bd3d 4602 err = parse_options(sbi, options, false);
dabc4a5c
JK
4603 if (err)
4604 goto free_options;
aff063e2 4605
abd0e040
ES
4606 err = f2fs_default_check(sbi);
4607 if (err)
4608 goto free_options;
4609
6d1451bf 4610 sb->s_maxbytes = max_file_blocks(NULL) <<
e0afc4d6 4611 le32_to_cpu(raw_super->log_blocksize);
aff063e2 4612 sb->s_max_links = F2FS_LINK_MAX;
aff063e2 4613
5aba5430
DR
4614 err = f2fs_setup_casefold(sbi);
4615 if (err)
4616 goto free_options;
4617
0abd675e
CY
4618#ifdef CONFIG_QUOTA
4619 sb->dq_op = &f2fs_quota_operations;
bc88ac96 4620 sb->s_qcop = &f2fs_quotactl_ops;
5c57132e 4621 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
292c196a 4622
7beb01f7 4623 if (f2fs_sb_has_quota_ino(sbi)) {
292c196a
CY
4624 for (i = 0; i < MAXQUOTAS; i++) {
4625 if (f2fs_qf_ino(sbi->sb, i))
4626 sbi->nquota_files++;
4627 }
4628 }
0abd675e
CY
4629#endif
4630
aff063e2 4631 sb->s_op = &f2fs_sops;
643fa961 4632#ifdef CONFIG_FS_ENCRYPTION
0b81d077 4633 sb->s_cop = &f2fs_cryptops;
95ae251f
EB
4634#endif
4635#ifdef CONFIG_FS_VERITY
4636 sb->s_vop = &f2fs_verityops;
ffcc4182 4637#endif
aff063e2
JK
4638 sb->s_xattr = f2fs_xattr_handlers;
4639 sb->s_export_op = &f2fs_export_ops;
4640 sb->s_magic = F2FS_SUPER_MAGIC;
aff063e2 4641 sb->s_time_gran = 1;
1751e8a6
LT
4642 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4643 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
7d6ee503
ES
4644 if (test_opt(sbi, INLINECRYPT))
4645 sb->s_flags |= SB_INLINECRYPT;
4646
9100adf3
ES
4647 if (test_opt(sbi, LAZYTIME))
4648 sb->s_flags |= SB_LAZYTIME;
4649 else
4650 sb->s_flags &= ~SB_LAZYTIME;
4651
a4af51ce 4652 super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid));
cc260b66 4653 super_set_sysfs_name_bdev(sb);
578c6478 4654 sb->s_iflags |= SB_I_CGROUPWB;
aff063e2
JK
4655
4656 /* init f2fs-specific super block info */
e8240f65 4657 sbi->valid_super_block = valid_super_block;
315df839
JK
4658
4659 /* disallow all the data/node/meta page writes */
4660 set_sbi_flag(sbi, SBI_POR_DOING);
971767ca 4661
908ea654
YY
4662 err = f2fs_init_write_merge_io(sbi);
4663 if (err)
4664 goto free_bio_info;
971767ca 4665
aff063e2
JK
4666 init_sb_info(sbi);
4667
52118743 4668 err = f2fs_init_iostat(sbi);
523be8a6 4669 if (err)
d7997e63 4670 goto free_bio_info;
523be8a6 4671
523be8a6
JK
4672 err = init_percpu_info(sbi);
4673 if (err)
a4b68176 4674 goto free_iostat;
523be8a6 4675
a999150f
CY
4676 /* init per sbi slab cache */
4677 err = f2fs_init_xattr_caches(sbi);
4678 if (err)
87161a2b 4679 goto free_percpu;
31083031
CY
4680 err = f2fs_init_page_array_cache(sbi);
4681 if (err)
4682 goto free_xattr_cache;
a999150f 4683
aff063e2
JK
4684 /* get an inode for meta space */
4685 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4686 if (IS_ERR(sbi->meta_inode)) {
dcbb4c10 4687 f2fs_err(sbi, "Failed to read F2FS meta data inode");
aff063e2 4688 err = PTR_ERR(sbi->meta_inode);
31083031 4689 goto free_page_array_cache;
aff063e2
JK
4690 }
4691
4d57b86d 4692 err = f2fs_get_valid_checkpoint(sbi);
a07ef784 4693 if (err) {
dcbb4c10 4694 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
aff063e2 4695 goto free_meta_inode;
a07ef784 4696 }
aff063e2 4697
af033b2a
CY
4698 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4699 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
db610a64
JK
4700 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4701 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4702 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4703 }
af033b2a 4704
04f0b2ea
QS
4705 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4706 set_sbi_flag(sbi, SBI_NEED_FSCK);
4707
3c62be17
JK
4708 /* Initialize device list */
4709 err = f2fs_scan_devices(sbi);
4710 if (err) {
dcbb4c10 4711 f2fs_err(sbi, "Failed to find devices");
3c62be17
JK
4712 goto free_devices;
4713 }
4714
4c8ff709
CY
4715 err = f2fs_init_post_read_wq(sbi);
4716 if (err) {
4717 f2fs_err(sbi, "Failed to initialize post read workqueue");
4718 goto free_devices;
4719 }
4720
aff063e2
JK
4721 sbi->total_valid_node_count =
4722 le32_to_cpu(sbi->ckpt->valid_node_count);
513c5f37
JK
4723 percpu_counter_set(&sbi->total_valid_inode_count,
4724 le32_to_cpu(sbi->ckpt->valid_inode_count));
aff063e2
JK
4725 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4726 sbi->total_valid_block_count =
4727 le64_to_cpu(sbi->ckpt->valid_block_count);
4728 sbi->last_valid_block_count = sbi->total_valid_block_count;
daeb433e 4729 sbi->reserved_blocks = 0;
80d42145 4730 sbi->current_reserved_blocks = 0;
7e65be49 4731 limit_reserve_root(sbi);
1ae18f71 4732 adjust_unusable_cap_perc(sbi);
41382ec4 4733
4d57b86d 4734 f2fs_init_extent_cache_info(sbi);
1dcc336b 4735
4d57b86d 4736 f2fs_init_ino_entry_info(sbi);
aff063e2 4737
50fa53ec
CY
4738 f2fs_init_fsync_node_info(sbi);
4739
261eeb9c
DJ
4740 /* setup checkpoint request control and start checkpoint issue thread */
4741 f2fs_init_ckpt_req_control(sbi);
3f7070b0 4742 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
261eeb9c
DJ
4743 test_opt(sbi, MERGE_CHECKPOINT)) {
4744 err = f2fs_start_ckpt_thread(sbi);
4745 if (err) {
4746 f2fs_err(sbi,
4747 "Failed to start F2FS issue_checkpoint_thread (%d)",
4748 err);
4749 goto stop_ckpt_thread;
4750 }
4751 }
4752
aff063e2 4753 /* setup f2fs internal modules */
4d57b86d 4754 err = f2fs_build_segment_manager(sbi);
a07ef784 4755 if (err) {
dcbb4c10
JP
4756 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4757 err);
aff063e2 4758 goto free_sm;
a07ef784 4759 }
4d57b86d 4760 err = f2fs_build_node_manager(sbi);
a07ef784 4761 if (err) {
dcbb4c10
JP
4762 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4763 err);
aff063e2 4764 goto free_nm;
a07ef784 4765 }
aff063e2 4766
8f1dbbbb 4767 /* For write statistics */
3a0a9cbc 4768 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
8f1dbbbb 4769
5bc5aae8 4770 /* get segno of first zoned block device */
dc6d9ef5 4771 sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
5bc5aae8 4772
8f1dbbbb
SL
4773 /* Read accumulated write IO statistics if exists */
4774 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4775 if (__exist_node_summaries(sbi))
4776 sbi->kbytes_written =
b2dde6fc 4777 le64_to_cpu(seg_i->journal->info.kbytes_written);
8f1dbbbb 4778
4d57b86d 4779 f2fs_build_gc_manager(sbi);
aff063e2 4780
60aa4d55
ST
4781 err = f2fs_build_stats(sbi);
4782 if (err)
4783 goto free_nm;
4784
aff063e2
JK
4785 /* get an inode for node space */
4786 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4787 if (IS_ERR(sbi->node_inode)) {
dcbb4c10 4788 f2fs_err(sbi, "Failed to read node inode");
aff063e2 4789 err = PTR_ERR(sbi->node_inode);
60aa4d55 4790 goto free_stats;
aff063e2
JK
4791 }
4792
aff063e2
JK
4793 /* read root inode and dentry */
4794 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4795 if (IS_ERR(root)) {
dcbb4c10 4796 f2fs_err(sbi, "Failed to read root inode");
aff063e2 4797 err = PTR_ERR(root);
60aa4d55 4798 goto free_node_inode;
aff063e2 4799 }
bcbfbd60
CY
4800 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4801 !root->i_size || !root->i_nlink) {
9d847950 4802 iput(root);
8f99a946 4803 err = -EINVAL;
60aa4d55 4804 goto free_node_inode;
8f99a946 4805 }
aff063e2 4806
be2760a7 4807 generic_set_sb_d_ops(sb);
aff063e2
JK
4808 sb->s_root = d_make_root(root); /* allocate root dentry */
4809 if (!sb->s_root) {
4810 err = -ENOMEM;
025cdb16 4811 goto free_node_inode;
aff063e2
JK
4812 }
4813
6ce19aff 4814 err = f2fs_init_compress_inode(sbi);
b59d0bae 4815 if (err)
a398101a 4816 goto free_root_inode;
b59d0bae 4817
6ce19aff
CY
4818 err = f2fs_register_sysfs(sbi);
4819 if (err)
4820 goto free_compress_inode;
4821
eb85c241 4822 sbi->umount_lock_holder = current;
ea676733 4823#ifdef CONFIG_QUOTA
76cf05d7 4824 /* Enable quota usage during mount */
7beb01f7 4825 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
ea676733 4826 err = f2fs_enable_quotas(sb);
730746ce 4827 if (err)
dcbb4c10 4828 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
ea676733 4829 }
e1bb7d3d
CY
4830
4831 quota_enabled = f2fs_recover_quota_begin(sbi);
ea676733 4832#endif
7a88ddb5 4833 /* if there are any orphan inodes, free them */
4d57b86d 4834 err = f2fs_recover_orphan_inodes(sbi);
4b2414d0 4835 if (err)
ea676733 4836 goto free_meta;
4b2414d0 4837
201e07ae
JK
4838 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) {
4839 skip_recovery = true;
aa2c8c43 4840 goto reset_checkpoint;
201e07ae 4841 }
4354994f 4842
6437d1b0 4843 /* recover fsynced data */
a9117eca
CY
4844 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4845 !test_opt(sbi, NORECOVERY)) {
081d78c2
JK
4846 /*
4847 * mount should be failed, when device has readonly mode, and
4848 * previous checkpoint was not done by clean system shutdown.
4849 */
b61af314 4850 if (f2fs_hw_is_readonly(sbi)) {
23738e74
CY
4851 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4852 err = f2fs_recover_fsync_data(sbi, true);
4853 if (err > 0) {
4854 err = -EROFS;
4855 f2fs_err(sbi, "Need to recover fsync data, but "
4856 "write access unavailable, please try "
4857 "mount w/ disable_roll_forward or norecovery");
4858 }
4859 if (err < 0)
4860 goto free_meta;
4861 }
4862 f2fs_info(sbi, "write access unavailable, skipping recovery");
b61af314 4863 goto reset_checkpoint;
081d78c2 4864 }
2adc3505
CY
4865
4866 if (need_fsck)
4867 set_sbi_flag(sbi, SBI_NEED_FSCK);
4868
aa2c8c43
CY
4869 if (skip_recovery)
4870 goto reset_checkpoint;
a468f0ef 4871
4d57b86d 4872 err = f2fs_recover_fsync_data(sbi, false);
6781eabb 4873 if (err < 0) {
aa2c8c43
CY
4874 if (err != -ENOMEM)
4875 skip_recovery = true;
2adc3505 4876 need_fsck = true;
dcbb4c10
JP
4877 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4878 err);
4b2414d0 4879 goto free_meta;
ed2e621a 4880 }
6781eabb 4881 } else {
4d57b86d 4882 err = f2fs_recover_fsync_data(sbi, true);
6781eabb
JK
4883
4884 if (!f2fs_readonly(sb) && err > 0) {
4885 err = -EINVAL;
dcbb4c10 4886 f2fs_err(sbi, "Need to recover fsync data");
ea676733 4887 goto free_meta;
6781eabb 4888 }
6437d1b0 4889 }
d508c94e 4890
d8f5b91d 4891reset_checkpoint:
e1bb7d3d
CY
4892#ifdef CONFIG_QUOTA
4893 f2fs_recover_quota_end(sbi, quota_enabled);
4894#endif
d508c94e
SK
4895 /*
4896 * If the f2fs is not readonly and fsync data recovery succeeds,
10150356
SY
4897 * write pointer consistency of cursegs and other zones are already
4898 * checked and fixed during recovery. However, if recovery fails,
4899 * write pointers are left untouched, and retry-mount should check
4900 * them here.
d508c94e 4901 */
10150356
SY
4902 if (skip_recovery)
4903 err = f2fs_check_and_fix_write_pointer(sbi);
4d4c5938
JK
4904 if (err)
4905 goto free_meta;
d508c94e 4906
f88c7904
SY
4907 /* f2fs_recover_fsync_data() cleared this already */
4908 clear_sbi_flag(sbi, SBI_POR_DOING);
4909
24593061
ZN
4910 err = f2fs_init_inmem_curseg(sbi);
4911 if (err)
4912 goto sync_free_meta;
093749e2 4913
4354994f
DR
4914 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4915 err = f2fs_disable_checkpoint(sbi);
4916 if (err)
812a9597 4917 goto sync_free_meta;
4354994f
DR
4918 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4919 f2fs_enable_checkpoint(sbi);
4920 }
4921
6437d1b0
JK
4922 /*
4923 * If filesystem is not mounted as read-only then
4924 * do start the gc_thread.
4925 */
5911d2d1
CY
4926 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4927 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
6437d1b0 4928 /* After POR, we can run background GC thread.*/
4d57b86d 4929 err = f2fs_start_gc_thread(sbi);
6437d1b0 4930 if (err)
812a9597 4931 goto sync_free_meta;
6437d1b0 4932 }
5222595d 4933 kvfree(options);
da554e48 4934
4935 /* recover broken superblock */
f2353d7b 4936 if (recovery) {
41214b3c 4937 err = f2fs_commit_super(sbi, true);
dcbb4c10
JP
4938 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4939 sbi->valid_super_block ? 1 : 2, err);
da554e48 4940 }
4941
bae01eda
CY
4942 f2fs_join_shrinker(sbi);
4943
84b89e5d
JK
4944 f2fs_tuning_parameters(sbi);
4945
dcbb4c10
JP
4946 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4947 cur_cp_version(F2FS_CKPT(sbi)));
6beceb54 4948 f2fs_update_time(sbi, CP_TIME);
d0239e1b 4949 f2fs_update_time(sbi, REQ_TIME);
db610a64 4950 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
eb85c241
CY
4951
4952 sbi->umount_lock_holder = NULL;
aff063e2 4953 return 0;
6437d1b0 4954
812a9597
JK
4955sync_free_meta:
4956 /* safe to flush all the data */
4957 sync_filesystem(sbi->sb);
aa2c8c43 4958 retry_cnt = 0;
812a9597 4959
4b2414d0 4960free_meta:
ea676733 4961#ifdef CONFIG_QUOTA
26b5a079 4962 f2fs_truncate_quota_inode_pages(sb);
7beb01f7 4963 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
ea676733
JK
4964 f2fs_quota_off_umount(sbi->sb);
4965#endif
4b2414d0 4966 /*
4d57b86d 4967 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4b2414d0 4968 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4d57b86d
CY
4969 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4970 * falls into an infinite loop in f2fs_sync_meta_pages().
4b2414d0
CY
4971 */
4972 truncate_inode_pages_final(META_MAPPING(sbi));
812a9597
JK
4973 /* evict some inodes being cached by GC */
4974 evict_inodes(sb);
dc6b2055 4975 f2fs_unregister_sysfs(sbi);
6ce19aff
CY
4976free_compress_inode:
4977 f2fs_destroy_compress_inode(sbi);
aff063e2
JK
4978free_root_inode:
4979 dput(sb->s_root);
4980 sb->s_root = NULL;
4981free_node_inode:
4d57b86d 4982 f2fs_release_ino_entry(sbi, true);
bae01eda 4983 truncate_inode_pages_final(NODE_MAPPING(sbi));
aff063e2 4984 iput(sbi->node_inode);
7c77bf7d 4985 sbi->node_inode = NULL;
60aa4d55
ST
4986free_stats:
4987 f2fs_destroy_stats(sbi);
aff063e2 4988free_nm:
5429c9db
DM
4989 /* stop discard thread before destroying node manager */
4990 f2fs_stop_discard_thread(sbi);
4d57b86d 4991 f2fs_destroy_node_manager(sbi);
aff063e2 4992free_sm:
4d57b86d 4993 f2fs_destroy_segment_manager(sbi);
261eeb9c
DJ
4994stop_ckpt_thread:
4995 f2fs_stop_ckpt_thread(sbi);
b62e71be
CY
4996 /* flush s_error_work before sbi destroy */
4997 flush_work(&sbi->s_error_work);
7b02b220 4998 f2fs_destroy_post_read_wq(sbi);
3c62be17
JK
4999free_devices:
5000 destroy_device_list(sbi);
5222595d 5001 kvfree(sbi->ckpt);
aff063e2
JK
5002free_meta_inode:
5003 make_bad_inode(sbi->meta_inode);
5004 iput(sbi->meta_inode);
7c77bf7d 5005 sbi->meta_inode = NULL;
31083031
CY
5006free_page_array_cache:
5007 f2fs_destroy_page_array_cache(sbi);
a999150f
CY
5008free_xattr_cache:
5009 f2fs_destroy_xattr_caches(sbi);
d7997e63
CY
5010free_percpu:
5011 destroy_percpu_info(sbi);
a4b68176
DJ
5012free_iostat:
5013 f2fs_destroy_iostat(sbi);
d7997e63 5014free_bio_info:
a912b54d 5015 for (i = 0; i < NR_PAGE_TYPE; i++)
5222595d 5016 kvfree(sbi->write_io[i]);
5aba5430 5017
5298d4bf 5018#if IS_ENABLED(CONFIG_UNICODE)
eca4873e 5019 utf8_unload(sb->s_encoding);
89ff6005 5020 sb->s_encoding = NULL;
5aba5430 5021#endif
d7997e63 5022free_options:
4b2414d0
CY
5023#ifdef CONFIG_QUOTA
5024 for (i = 0; i < MAXQUOTAS; i++)
ba87a45c 5025 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4b2414d0 5026#endif
ac4acb1f 5027 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
5222595d 5028 kvfree(options);
aff063e2 5029free_sb_buf:
742532d1 5030 kfree(raw_super);
aff063e2 5031free_sbi:
742532d1 5032 kfree(sbi);
c919330d 5033 sb->s_fs_info = NULL;
ed2e621a
JK
5034
5035 /* give only one another chance */
aa2c8c43
CY
5036 if (retry_cnt > 0 && skip_recovery) {
5037 retry_cnt--;
ed2e621a
JK
5038 shrink_dcache_sb(sb);
5039 goto try_onemore;
5040 }
aff063e2
JK
5041 return err;
5042}
5043
5044static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
5045 const char *dev_name, void *data)
5046{
5047 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
5048}
5049
30a5537f
JK
5050static void kill_f2fs_super(struct super_block *sb)
5051{
275dca46 5052 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1cb50f87 5053
275dca46 5054 if (sb->s_root) {
eb85c241
CY
5055 sbi->umount_lock_holder = current;
5056
1cb50f87
JK
5057 set_sbi_flag(sbi, SBI_IS_CLOSE);
5058 f2fs_stop_gc_thread(sbi);
5059 f2fs_stop_discard_thread(sbi);
5060
6ce19aff
CY
5061#ifdef CONFIG_F2FS_FS_COMPRESSION
5062 /*
5063 * latter evict_inode() can bypass checking and invalidating
5064 * compress inode cache.
5065 */
5066 if (test_opt(sbi, COMPRESS_CACHE))
5067 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
5068#endif
5069
1cb50f87
JK
5070 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
5071 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
5072 struct cp_control cpc = {
5073 .reason = CP_UMOUNT,
5074 };
eb61c2cc 5075 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1cb50f87
JK
5076 f2fs_write_checkpoint(sbi, &cpc);
5077 }
1378752b
CY
5078
5079 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
5080 sb->s_flags &= ~SB_RDONLY;
cce13252 5081 }
30a5537f 5082 kill_block_super(sb);
275dca46
EB
5083 /* Release block devices last, after fscrypt_destroy_keyring(). */
5084 if (sbi) {
5085 destroy_device_list(sbi);
5086 kfree(sbi);
5087 sb->s_fs_info = NULL;
5088 }
30a5537f
JK
5089}
5090
aff063e2
JK
5091static struct file_system_type f2fs_fs_type = {
5092 .owner = THIS_MODULE,
5093 .name = "f2fs",
5094 .mount = f2fs_mount,
30a5537f 5095 .kill_sb = kill_f2fs_super,
984fc4e7 5096 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
aff063e2 5097};
7f78e035 5098MODULE_ALIAS_FS("f2fs");
aff063e2 5099
6e6093a8 5100static int __init init_inodecache(void)
aff063e2 5101{
5d097056
VD
5102 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
5103 sizeof(struct f2fs_inode_info), 0,
5104 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
870af777 5105 return f2fs_inode_cachep ? 0 : -ENOMEM;
aff063e2
JK
5106}
5107
5108static void destroy_inodecache(void)
5109{
5110 /*
5111 * Make sure all delayed rcu free inodes are flushed before we
5112 * destroy cache.
5113 */
5114 rcu_barrier();
5115 kmem_cache_destroy(f2fs_inode_cachep);
5116}
5117
5118static int __init init_f2fs_fs(void)
5119{
5120 int err;
5121
5122 err = init_inodecache();
5123 if (err)
5124 goto fail;
4d57b86d 5125 err = f2fs_create_node_manager_caches();
aff063e2 5126 if (err)
9890ff3f 5127 goto free_inodecache;
4d57b86d 5128 err = f2fs_create_segment_manager_caches();
aff063e2 5129 if (err)
9890ff3f 5130 goto free_node_manager_caches;
4d57b86d 5131 err = f2fs_create_checkpoint_caches();
aff063e2 5132 if (err)
06292073 5133 goto free_segment_manager_caches;
cad83c96 5134 err = f2fs_create_recovery_cache();
1dcc336b
CY
5135 if (err)
5136 goto free_checkpoint_caches;
cad83c96
CY
5137 err = f2fs_create_extent_cache();
5138 if (err)
5139 goto free_recovery_cache;
093749e2 5140 err = f2fs_create_garbage_collection_cache();
a398101a 5141 if (err)
1dcc336b 5142 goto free_extent_cache;
093749e2
CY
5143 err = f2fs_init_sysfs();
5144 if (err)
5145 goto free_garbage_collection_cache;
bfcba5ba 5146 err = f2fs_init_shrinker();
cfc4d971 5147 if (err)
a398101a 5148 goto free_sysfs;
21acc07d 5149 f2fs_create_root_stats();
6dbb1796
EB
5150 err = f2fs_init_post_read_processing();
5151 if (err)
5152 goto free_root_stats;
a4b68176 5153 err = f2fs_init_iostat_processing();
0b20fcec
CY
5154 if (err)
5155 goto free_post_read;
a4b68176
DJ
5156 err = f2fs_init_bio_entry_cache();
5157 if (err)
5158 goto free_iostat;
f543805f
CY
5159 err = f2fs_init_bioset();
5160 if (err)
db8dcd25 5161 goto free_bio_entry_cache;
5e6bbde9
CY
5162 err = f2fs_init_compress_mempool();
5163 if (err)
5164 goto free_bioset;
c68d6c88
CY
5165 err = f2fs_init_compress_cache();
5166 if (err)
5167 goto free_compress_mempool;
4d9a2bb1
CY
5168 err = f2fs_create_casefold_cache();
5169 if (err)
5170 goto free_compress_cache;
b7d0a97b
YB
5171 err = register_filesystem(&f2fs_fs_type);
5172 if (err)
5173 goto free_casefold_cache;
9890ff3f 5174 return 0;
b7d0a97b
YB
5175free_casefold_cache:
5176 f2fs_destroy_casefold_cache();
4d9a2bb1
CY
5177free_compress_cache:
5178 f2fs_destroy_compress_cache();
c68d6c88
CY
5179free_compress_mempool:
5180 f2fs_destroy_compress_mempool();
5e6bbde9
CY
5181free_bioset:
5182 f2fs_destroy_bioset();
db8dcd25 5183free_bio_entry_cache:
f543805f 5184 f2fs_destroy_bio_entry_cache();
a4b68176
DJ
5185free_iostat:
5186 f2fs_destroy_iostat_processing();
0b20fcec
CY
5187free_post_read:
5188 f2fs_destroy_post_read_processing();
6dbb1796
EB
5189free_root_stats:
5190 f2fs_destroy_root_stats();
bfcba5ba 5191 f2fs_exit_shrinker();
a398101a 5192free_sysfs:
dc6b2055 5193 f2fs_exit_sysfs();
093749e2
CY
5194free_garbage_collection_cache:
5195 f2fs_destroy_garbage_collection_cache();
1dcc336b 5196free_extent_cache:
4d57b86d 5197 f2fs_destroy_extent_cache();
cad83c96
CY
5198free_recovery_cache:
5199 f2fs_destroy_recovery_cache();
9890ff3f 5200free_checkpoint_caches:
4d57b86d 5201 f2fs_destroy_checkpoint_caches();
7fd9e544 5202free_segment_manager_caches:
4d57b86d 5203 f2fs_destroy_segment_manager_caches();
9890ff3f 5204free_node_manager_caches:
4d57b86d 5205 f2fs_destroy_node_manager_caches();
9890ff3f
ZH
5206free_inodecache:
5207 destroy_inodecache();
aff063e2
JK
5208fail:
5209 return err;
5210}
5211
5212static void __exit exit_f2fs_fs(void)
5213{
b7d0a97b 5214 unregister_filesystem(&f2fs_fs_type);
4d9a2bb1 5215 f2fs_destroy_casefold_cache();
c68d6c88 5216 f2fs_destroy_compress_cache();
5e6bbde9 5217 f2fs_destroy_compress_mempool();
f543805f 5218 f2fs_destroy_bioset();
0b20fcec 5219 f2fs_destroy_bio_entry_cache();
a4b68176 5220 f2fs_destroy_iostat_processing();
6dbb1796 5221 f2fs_destroy_post_read_processing();
4589d25d 5222 f2fs_destroy_root_stats();
bfcba5ba 5223 f2fs_exit_shrinker();
dc6b2055 5224 f2fs_exit_sysfs();
093749e2 5225 f2fs_destroy_garbage_collection_cache();
4d57b86d 5226 f2fs_destroy_extent_cache();
cad83c96 5227 f2fs_destroy_recovery_cache();
4d57b86d
CY
5228 f2fs_destroy_checkpoint_caches();
5229 f2fs_destroy_segment_manager_caches();
5230 f2fs_destroy_node_manager_caches();
aff063e2
JK
5231 destroy_inodecache();
5232}
5233
5234module_init(init_f2fs_fs)
5235module_exit(exit_f2fs_fs)
5236
5237MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5238MODULE_DESCRIPTION("Flash Friendly File System");
5239MODULE_LICENSE("GPL");