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