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