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