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