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