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