Debian Codegraph @

fs/f2fs/super.c

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