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