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