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