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