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