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