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