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 atomic write is not committed, set inode w/ atomic dirty */
1504 if (!ret && f2fs_is_atomic_file(inode) &&
1505 !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
1506 set_inode_flag(inode, FI_ATOMIC_DIRTIED);
1507
1508 return ret;
1509 }
1510
f2fs_inode_synced(struct inode * inode)1511 void f2fs_inode_synced(struct inode *inode)
1512 {
1513 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1514
1515 spin_lock(&sbi->inode_lock[DIRTY_META]);
1516 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1517 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1518 return;
1519 }
1520 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1521 list_del_init(&F2FS_I(inode)->gdirty_list);
1522 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1523 }
1524 clear_inode_flag(inode, FI_DIRTY_INODE);
1525 clear_inode_flag(inode, FI_AUTO_RECOVER);
1526 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1527 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1528 }
1529
1530 /*
1531 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1532 *
1533 * We should call set_dirty_inode to write the dirty inode through write_inode.
1534 */
f2fs_dirty_inode(struct inode * inode,int flags)1535 static void f2fs_dirty_inode(struct inode *inode, int flags)
1536 {
1537 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1538
1539 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1540 inode->i_ino == F2FS_META_INO(sbi))
1541 return;
1542
1543 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1544 clear_inode_flag(inode, FI_AUTO_RECOVER);
1545
1546 f2fs_inode_dirtied(inode, false);
1547 }
1548
f2fs_free_inode(struct inode * inode)1549 static void f2fs_free_inode(struct inode *inode)
1550 {
1551 fscrypt_free_inode(inode);
1552 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1553 }
1554
destroy_percpu_info(struct f2fs_sb_info * sbi)1555 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1556 {
1557 percpu_counter_destroy(&sbi->total_valid_inode_count);
1558 percpu_counter_destroy(&sbi->rf_node_block_count);
1559 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1560 }
1561
destroy_device_list(struct f2fs_sb_info * sbi)1562 static void destroy_device_list(struct f2fs_sb_info *sbi)
1563 {
1564 int i;
1565
1566 for (i = 0; i < sbi->s_ndevs; i++) {
1567 if (i > 0)
1568 blkdev_put(FDEV(i).bdev, sbi->sb);
1569 #ifdef CONFIG_BLK_DEV_ZONED
1570 kvfree(FDEV(i).blkz_seq);
1571 #endif
1572 }
1573 kvfree(sbi->devs);
1574 }
1575
f2fs_put_super(struct super_block * sb)1576 static void f2fs_put_super(struct super_block *sb)
1577 {
1578 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1579 int i;
1580 int err = 0;
1581 bool done;
1582
1583 /* unregister procfs/sysfs entries in advance to avoid race case */
1584 f2fs_unregister_sysfs(sbi);
1585
1586 f2fs_quota_off_umount(sb);
1587
1588 /* prevent remaining shrinker jobs */
1589 mutex_lock(&sbi->umount_mutex);
1590
1591 /*
1592 * flush all issued checkpoints and stop checkpoint issue thread.
1593 * after then, all checkpoints should be done by each process context.
1594 */
1595 f2fs_stop_ckpt_thread(sbi);
1596
1597 /*
1598 * We don't need to do checkpoint when superblock is clean.
1599 * But, the previous checkpoint was not done by umount, it needs to do
1600 * clean checkpoint again.
1601 */
1602 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1603 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
1604 struct cp_control cpc = {
1605 .reason = CP_UMOUNT,
1606 };
1607 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1608 err = f2fs_write_checkpoint(sbi, &cpc);
1609 }
1610
1611 /* be sure to wait for any on-going discard commands */
1612 done = f2fs_issue_discard_timeout(sbi);
1613 if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
1614 struct cp_control cpc = {
1615 .reason = CP_UMOUNT | CP_TRIMMED,
1616 };
1617 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1618 err = f2fs_write_checkpoint(sbi, &cpc);
1619 }
1620
1621 /*
1622 * normally superblock is clean, so we need to release this.
1623 * In addition, EIO will skip do checkpoint, we need this as well.
1624 */
1625 f2fs_release_ino_entry(sbi, true);
1626
1627 f2fs_leave_shrinker(sbi);
1628 mutex_unlock(&sbi->umount_mutex);
1629
1630 /* our cp_error case, we can wait for any writeback page */
1631 f2fs_flush_merged_writes(sbi);
1632
1633 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
1634
1635 if (err || f2fs_cp_error(sbi)) {
1636 truncate_inode_pages_final(NODE_MAPPING(sbi));
1637 truncate_inode_pages_final(META_MAPPING(sbi));
1638 }
1639
1640 for (i = 0; i < NR_COUNT_TYPE; i++) {
1641 if (!get_pages(sbi, i))
1642 continue;
1643 f2fs_err(sbi, "detect filesystem reference count leak during "
1644 "umount, type: %d, count: %lld", i, get_pages(sbi, i));
1645 f2fs_bug_on(sbi, 1);
1646 }
1647
1648 f2fs_bug_on(sbi, sbi->fsync_node_num);
1649
1650 f2fs_destroy_compress_inode(sbi);
1651
1652 iput(sbi->node_inode);
1653 sbi->node_inode = NULL;
1654
1655 iput(sbi->meta_inode);
1656 sbi->meta_inode = NULL;
1657
1658 /*
1659 * iput() can update stat information, if f2fs_write_checkpoint()
1660 * above failed with error.
1661 */
1662 f2fs_destroy_stats(sbi);
1663
1664 /* destroy f2fs internal modules */
1665 f2fs_destroy_node_manager(sbi);
1666 f2fs_destroy_segment_manager(sbi);
1667
1668 /* flush s_error_work before sbi destroy */
1669 flush_work(&sbi->s_error_work);
1670
1671 f2fs_destroy_post_read_wq(sbi);
1672
1673 kvfree(sbi->ckpt);
1674
1675 sb->s_fs_info = NULL;
1676 if (sbi->s_chksum_driver)
1677 crypto_free_shash(sbi->s_chksum_driver);
1678 kfree(sbi->raw_super);
1679
1680 destroy_device_list(sbi);
1681 f2fs_destroy_page_array_cache(sbi);
1682 f2fs_destroy_xattr_caches(sbi);
1683 #ifdef CONFIG_QUOTA
1684 for (i = 0; i < MAXQUOTAS; i++)
1685 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1686 #endif
1687 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
1688 destroy_percpu_info(sbi);
1689 f2fs_destroy_iostat(sbi);
1690 for (i = 0; i < NR_PAGE_TYPE; i++)
1691 kvfree(sbi->write_io[i]);
1692 #if IS_ENABLED(CONFIG_UNICODE)
1693 utf8_unload(sb->s_encoding);
1694 #endif
1695 kfree(sbi);
1696 }
1697
f2fs_sync_fs(struct super_block * sb,int sync)1698 int f2fs_sync_fs(struct super_block *sb, int sync)
1699 {
1700 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1701 int err = 0;
1702
1703 if (unlikely(f2fs_cp_error(sbi)))
1704 return 0;
1705 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1706 return 0;
1707
1708 trace_f2fs_sync_fs(sb, sync);
1709
1710 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1711 return -EAGAIN;
1712
1713 if (sync) {
1714 stat_inc_cp_call_count(sbi, TOTAL_CALL);
1715 err = f2fs_issue_checkpoint(sbi);
1716 }
1717
1718 return err;
1719 }
1720
f2fs_freeze(struct super_block * sb)1721 static int f2fs_freeze(struct super_block *sb)
1722 {
1723 if (f2fs_readonly(sb))
1724 return 0;
1725
1726 /* IO error happened before */
1727 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1728 return -EIO;
1729
1730 /* must be clean, since sync_filesystem() was already called */
1731 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1732 return -EINVAL;
1733
1734 /* Let's flush checkpoints and stop the thread. */
1735 f2fs_flush_ckpt_thread(F2FS_SB(sb));
1736
1737 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1738 set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1739 return 0;
1740 }
1741
f2fs_unfreeze(struct super_block * sb)1742 static int f2fs_unfreeze(struct super_block *sb)
1743 {
1744 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1745
1746 /*
1747 * It will update discard_max_bytes of mounted lvm device to zero
1748 * after creating snapshot on this lvm device, let's drop all
1749 * remained discards.
1750 * We don't need to disable real-time discard because discard_max_bytes
1751 * will recover after removal of snapshot.
1752 */
1753 if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi))
1754 f2fs_issue_discard_timeout(sbi);
1755
1756 clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
1757 return 0;
1758 }
1759
1760 #ifdef CONFIG_QUOTA
f2fs_statfs_project(struct super_block * sb,kprojid_t projid,struct kstatfs * buf)1761 static int f2fs_statfs_project(struct super_block *sb,
1762 kprojid_t projid, struct kstatfs *buf)
1763 {
1764 struct kqid qid;
1765 struct dquot *dquot;
1766 u64 limit;
1767 u64 curblock;
1768
1769 qid = make_kqid_projid(projid);
1770 dquot = dqget(sb, qid);
1771 if (IS_ERR(dquot))
1772 return PTR_ERR(dquot);
1773 spin_lock(&dquot->dq_dqb_lock);
1774
1775 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1776 dquot->dq_dqb.dqb_bhardlimit);
1777 if (limit)
1778 limit >>= sb->s_blocksize_bits;
1779
1780 if (limit && buf->f_blocks > limit) {
1781 curblock = (dquot->dq_dqb.dqb_curspace +
1782 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
1783 buf->f_blocks = limit;
1784 buf->f_bfree = buf->f_bavail =
1785 (buf->f_blocks > curblock) ?
1786 (buf->f_blocks - curblock) : 0;
1787 }
1788
1789 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1790 dquot->dq_dqb.dqb_ihardlimit);
1791
1792 if (limit && buf->f_files > limit) {
1793 buf->f_files = limit;
1794 buf->f_ffree =
1795 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1796 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1797 }
1798
1799 spin_unlock(&dquot->dq_dqb_lock);
1800 dqput(dquot);
1801 return 0;
1802 }
1803 #endif
1804
f2fs_statfs(struct dentry * dentry,struct kstatfs * buf)1805 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1806 {
1807 struct super_block *sb = dentry->d_sb;
1808 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1809 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1810 block_t total_count, user_block_count, start_count;
1811 u64 avail_node_count;
1812 unsigned int total_valid_node_count;
1813
1814 total_count = le64_to_cpu(sbi->raw_super->block_count);
1815 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1816 buf->f_type = F2FS_SUPER_MAGIC;
1817 buf->f_bsize = sbi->blocksize;
1818
1819 buf->f_blocks = total_count - start_count;
1820
1821 spin_lock(&sbi->stat_lock);
1822
1823 user_block_count = sbi->user_block_count;
1824 total_valid_node_count = valid_node_count(sbi);
1825 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
1826 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1827 sbi->current_reserved_blocks;
1828
1829 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1830 buf->f_bfree = 0;
1831 else
1832 buf->f_bfree -= sbi->unusable_block_count;
1833 spin_unlock(&sbi->stat_lock);
1834
1835 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1836 buf->f_bavail = buf->f_bfree -
1837 F2FS_OPTION(sbi).root_reserved_blocks;
1838 else
1839 buf->f_bavail = 0;
1840
1841 if (avail_node_count > user_block_count) {
1842 buf->f_files = user_block_count;
1843 buf->f_ffree = buf->f_bavail;
1844 } else {
1845 buf->f_files = avail_node_count;
1846 buf->f_ffree = min(avail_node_count - total_valid_node_count,
1847 buf->f_bavail);
1848 }
1849
1850 buf->f_namelen = F2FS_NAME_LEN;
1851 buf->f_fsid = u64_to_fsid(id);
1852
1853 #ifdef CONFIG_QUOTA
1854 if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) &&
1855 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1856 f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf);
1857 }
1858 #endif
1859 return 0;
1860 }
1861
f2fs_show_quota_options(struct seq_file * seq,struct super_block * sb)1862 static inline void f2fs_show_quota_options(struct seq_file *seq,
1863 struct super_block *sb)
1864 {
1865 #ifdef CONFIG_QUOTA
1866 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1867
1868 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1869 char *fmtname = "";
1870
1871 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1872 case QFMT_VFS_OLD:
1873 fmtname = "vfsold";
1874 break;
1875 case QFMT_VFS_V0:
1876 fmtname = "vfsv0";
1877 break;
1878 case QFMT_VFS_V1:
1879 fmtname = "vfsv1";
1880 break;
1881 }
1882 seq_printf(seq, ",jqfmt=%s", fmtname);
1883 }
1884
1885 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1886 seq_show_option(seq, "usrjquota",
1887 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1888
1889 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1890 seq_show_option(seq, "grpjquota",
1891 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1892
1893 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1894 seq_show_option(seq, "prjjquota",
1895 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1896 #endif
1897 }
1898
1899 #ifdef CONFIG_F2FS_FS_COMPRESSION
f2fs_show_compress_options(struct seq_file * seq,struct super_block * sb)1900 static inline void f2fs_show_compress_options(struct seq_file *seq,
1901 struct super_block *sb)
1902 {
1903 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1904 char *algtype = "";
1905 int i;
1906
1907 if (!f2fs_sb_has_compression(sbi))
1908 return;
1909
1910 switch (F2FS_OPTION(sbi).compress_algorithm) {
1911 case COMPRESS_LZO:
1912 algtype = "lzo";
1913 break;
1914 case COMPRESS_LZ4:
1915 algtype = "lz4";
1916 break;
1917 case COMPRESS_ZSTD:
1918 algtype = "zstd";
1919 break;
1920 case COMPRESS_LZORLE:
1921 algtype = "lzo-rle";
1922 break;
1923 }
1924 seq_printf(seq, ",compress_algorithm=%s", algtype);
1925
1926 if (F2FS_OPTION(sbi).compress_level)
1927 seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
1928
1929 seq_printf(seq, ",compress_log_size=%u",
1930 F2FS_OPTION(sbi).compress_log_size);
1931
1932 for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
1933 seq_printf(seq, ",compress_extension=%s",
1934 F2FS_OPTION(sbi).extensions[i]);
1935 }
1936
1937 for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
1938 seq_printf(seq, ",nocompress_extension=%s",
1939 F2FS_OPTION(sbi).noextensions[i]);
1940 }
1941
1942 if (F2FS_OPTION(sbi).compress_chksum)
1943 seq_puts(seq, ",compress_chksum");
1944
1945 if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1946 seq_printf(seq, ",compress_mode=%s", "fs");
1947 else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1948 seq_printf(seq, ",compress_mode=%s", "user");
1949
1950 if (test_opt(sbi, COMPRESS_CACHE))
1951 seq_puts(seq, ",compress_cache");
1952 }
1953 #endif
1954
f2fs_show_options(struct seq_file * seq,struct dentry * root)1955 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1956 {
1957 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1958
1959 if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
1960 seq_printf(seq, ",background_gc=%s", "sync");
1961 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
1962 seq_printf(seq, ",background_gc=%s", "on");
1963 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
1964 seq_printf(seq, ",background_gc=%s", "off");
1965
1966 if (test_opt(sbi, GC_MERGE))
1967 seq_puts(seq, ",gc_merge");
1968 else
1969 seq_puts(seq, ",nogc_merge");
1970
1971 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1972 seq_puts(seq, ",disable_roll_forward");
1973 if (test_opt(sbi, NORECOVERY))
1974 seq_puts(seq, ",norecovery");
1975 if (test_opt(sbi, DISCARD)) {
1976 seq_puts(seq, ",discard");
1977 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
1978 seq_printf(seq, ",discard_unit=%s", "block");
1979 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
1980 seq_printf(seq, ",discard_unit=%s", "segment");
1981 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
1982 seq_printf(seq, ",discard_unit=%s", "section");
1983 } else {
1984 seq_puts(seq, ",nodiscard");
1985 }
1986 #ifdef CONFIG_F2FS_FS_XATTR
1987 if (test_opt(sbi, XATTR_USER))
1988 seq_puts(seq, ",user_xattr");
1989 else
1990 seq_puts(seq, ",nouser_xattr");
1991 if (test_opt(sbi, INLINE_XATTR))
1992 seq_puts(seq, ",inline_xattr");
1993 else
1994 seq_puts(seq, ",noinline_xattr");
1995 if (test_opt(sbi, INLINE_XATTR_SIZE))
1996 seq_printf(seq, ",inline_xattr_size=%u",
1997 F2FS_OPTION(sbi).inline_xattr_size);
1998 #endif
1999 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2000 if (test_opt(sbi, POSIX_ACL))
2001 seq_puts(seq, ",acl");
2002 else
2003 seq_puts(seq, ",noacl");
2004 #endif
2005 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
2006 seq_puts(seq, ",disable_ext_identify");
2007 if (test_opt(sbi, INLINE_DATA))
2008 seq_puts(seq, ",inline_data");
2009 else
2010 seq_puts(seq, ",noinline_data");
2011 if (test_opt(sbi, INLINE_DENTRY))
2012 seq_puts(seq, ",inline_dentry");
2013 else
2014 seq_puts(seq, ",noinline_dentry");
2015 if (test_opt(sbi, FLUSH_MERGE))
2016 seq_puts(seq, ",flush_merge");
2017 else
2018 seq_puts(seq, ",noflush_merge");
2019 if (test_opt(sbi, NOBARRIER))
2020 seq_puts(seq, ",nobarrier");
2021 else
2022 seq_puts(seq, ",barrier");
2023 if (test_opt(sbi, FASTBOOT))
2024 seq_puts(seq, ",fastboot");
2025 if (test_opt(sbi, READ_EXTENT_CACHE))
2026 seq_puts(seq, ",extent_cache");
2027 else
2028 seq_puts(seq, ",noextent_cache");
2029 if (test_opt(sbi, AGE_EXTENT_CACHE))
2030 seq_puts(seq, ",age_extent_cache");
2031 if (test_opt(sbi, DATA_FLUSH))
2032 seq_puts(seq, ",data_flush");
2033
2034 seq_puts(seq, ",mode=");
2035 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
2036 seq_puts(seq, "adaptive");
2037 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
2038 seq_puts(seq, "lfs");
2039 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
2040 seq_puts(seq, "fragment:segment");
2041 else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2042 seq_puts(seq, "fragment:block");
2043 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
2044 if (test_opt(sbi, RESERVE_ROOT))
2045 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
2046 F2FS_OPTION(sbi).root_reserved_blocks,
2047 from_kuid_munged(&init_user_ns,
2048 F2FS_OPTION(sbi).s_resuid),
2049 from_kgid_munged(&init_user_ns,
2050 F2FS_OPTION(sbi).s_resgid));
2051 #ifdef CONFIG_F2FS_FAULT_INJECTION
2052 if (test_opt(sbi, FAULT_INJECTION)) {
2053 seq_printf(seq, ",fault_injection=%u",
2054 F2FS_OPTION(sbi).fault_info.inject_rate);
2055 seq_printf(seq, ",fault_type=%u",
2056 F2FS_OPTION(sbi).fault_info.inject_type);
2057 }
2058 #endif
2059 #ifdef CONFIG_QUOTA
2060 if (test_opt(sbi, QUOTA))
2061 seq_puts(seq, ",quota");
2062 if (test_opt(sbi, USRQUOTA))
2063 seq_puts(seq, ",usrquota");
2064 if (test_opt(sbi, GRPQUOTA))
2065 seq_puts(seq, ",grpquota");
2066 if (test_opt(sbi, PRJQUOTA))
2067 seq_puts(seq, ",prjquota");
2068 #endif
2069 f2fs_show_quota_options(seq, sbi->sb);
2070
2071 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
2072
2073 if (sbi->sb->s_flags & SB_INLINECRYPT)
2074 seq_puts(seq, ",inlinecrypt");
2075
2076 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
2077 seq_printf(seq, ",alloc_mode=%s", "default");
2078 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2079 seq_printf(seq, ",alloc_mode=%s", "reuse");
2080
2081 if (test_opt(sbi, DISABLE_CHECKPOINT))
2082 seq_printf(seq, ",checkpoint=disable:%u",
2083 F2FS_OPTION(sbi).unusable_cap);
2084 if (test_opt(sbi, MERGE_CHECKPOINT))
2085 seq_puts(seq, ",checkpoint_merge");
2086 else
2087 seq_puts(seq, ",nocheckpoint_merge");
2088 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
2089 seq_printf(seq, ",fsync_mode=%s", "posix");
2090 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
2091 seq_printf(seq, ",fsync_mode=%s", "strict");
2092 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
2093 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
2094
2095 #ifdef CONFIG_F2FS_FS_COMPRESSION
2096 f2fs_show_compress_options(seq, sbi->sb);
2097 #endif
2098
2099 if (test_opt(sbi, ATGC))
2100 seq_puts(seq, ",atgc");
2101
2102 if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
2103 seq_printf(seq, ",memory=%s", "normal");
2104 else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
2105 seq_printf(seq, ",memory=%s", "low");
2106
2107 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
2108 seq_printf(seq, ",errors=%s", "remount-ro");
2109 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
2110 seq_printf(seq, ",errors=%s", "continue");
2111 else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
2112 seq_printf(seq, ",errors=%s", "panic");
2113
2114 return 0;
2115 }
2116
default_options(struct f2fs_sb_info * sbi,bool remount)2117 static void default_options(struct f2fs_sb_info *sbi, bool remount)
2118 {
2119 /* init some FS parameters */
2120 if (!remount) {
2121 set_opt(sbi, READ_EXTENT_CACHE);
2122 clear_opt(sbi, DISABLE_CHECKPOINT);
2123
2124 if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2125 set_opt(sbi, DISCARD);
2126
2127 if (f2fs_sb_has_blkzoned(sbi))
2128 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2129 else
2130 F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2131 }
2132
2133 if (f2fs_sb_has_readonly(sbi))
2134 F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
2135 else
2136 F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
2137
2138 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
2139 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
2140 SMALL_VOLUME_SEGMENTS)
2141 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2142 else
2143 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
2144 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
2145 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
2146 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
2147 if (f2fs_sb_has_compression(sbi)) {
2148 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
2149 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
2150 F2FS_OPTION(sbi).compress_ext_cnt = 0;
2151 F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
2152 }
2153 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
2154 F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
2155 F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
2156
2157 set_opt(sbi, INLINE_XATTR);
2158 set_opt(sbi, INLINE_DATA);
2159 set_opt(sbi, INLINE_DENTRY);
2160 set_opt(sbi, MERGE_CHECKPOINT);
2161 F2FS_OPTION(sbi).unusable_cap = 0;
2162 sbi->sb->s_flags |= SB_LAZYTIME;
2163 if (!f2fs_is_readonly(sbi))
2164 set_opt(sbi, FLUSH_MERGE);
2165 if (f2fs_sb_has_blkzoned(sbi))
2166 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2167 else
2168 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2169
2170 #ifdef CONFIG_F2FS_FS_XATTR
2171 set_opt(sbi, XATTR_USER);
2172 #endif
2173 #ifdef CONFIG_F2FS_FS_POSIX_ACL
2174 set_opt(sbi, POSIX_ACL);
2175 #endif
2176
2177 f2fs_build_fault_attr(sbi, 0, 0);
2178 }
2179
2180 #ifdef CONFIG_QUOTA
2181 static int f2fs_enable_quotas(struct super_block *sb);
2182 #endif
2183
f2fs_disable_checkpoint(struct f2fs_sb_info * sbi)2184 static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
2185 {
2186 unsigned int s_flags = sbi->sb->s_flags;
2187 struct cp_control cpc;
2188 unsigned int gc_mode = sbi->gc_mode;
2189 int err = 0;
2190 int ret;
2191 block_t unusable;
2192
2193 if (s_flags & SB_RDONLY) {
2194 f2fs_err(sbi, "checkpoint=disable on readonly fs");
2195 return -EINVAL;
2196 }
2197 sbi->sb->s_flags |= SB_ACTIVE;
2198
2199 /* check if we need more GC first */
2200 unusable = f2fs_get_unusable_blocks(sbi);
2201 if (!f2fs_disable_cp_again(sbi, unusable))
2202 goto skip_gc;
2203
2204 f2fs_update_time(sbi, DISABLE_TIME);
2205
2206 sbi->gc_mode = GC_URGENT_HIGH;
2207
2208 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
2209 struct f2fs_gc_control gc_control = {
2210 .victim_segno = NULL_SEGNO,
2211 .init_gc_type = FG_GC,
2212 .should_migrate_blocks = false,
2213 .err_gc_skipped = true,
2214 .nr_free_secs = 1 };
2215
2216 f2fs_down_write(&sbi->gc_lock);
2217 stat_inc_gc_call_count(sbi, FOREGROUND);
2218 err = f2fs_gc(sbi, &gc_control);
2219 if (err == -ENODATA) {
2220 err = 0;
2221 break;
2222 }
2223 if (err && err != -EAGAIN)
2224 break;
2225 }
2226
2227 ret = sync_filesystem(sbi->sb);
2228 if (ret || err) {
2229 err = ret ? ret : err;
2230 goto restore_flag;
2231 }
2232
2233 unusable = f2fs_get_unusable_blocks(sbi);
2234 if (f2fs_disable_cp_again(sbi, unusable)) {
2235 err = -EAGAIN;
2236 goto restore_flag;
2237 }
2238
2239 skip_gc:
2240 f2fs_down_write(&sbi->gc_lock);
2241 cpc.reason = CP_PAUSE;
2242 set_sbi_flag(sbi, SBI_CP_DISABLED);
2243 stat_inc_cp_call_count(sbi, TOTAL_CALL);
2244 err = f2fs_write_checkpoint(sbi, &cpc);
2245 if (err)
2246 goto out_unlock;
2247
2248 spin_lock(&sbi->stat_lock);
2249 sbi->unusable_block_count = unusable;
2250 spin_unlock(&sbi->stat_lock);
2251
2252 out_unlock:
2253 f2fs_up_write(&sbi->gc_lock);
2254 restore_flag:
2255 sbi->gc_mode = gc_mode;
2256 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2257 return err;
2258 }
2259
f2fs_enable_checkpoint(struct f2fs_sb_info * sbi)2260 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
2261 {
2262 int retry = DEFAULT_RETRY_IO_COUNT;
2263
2264 /* we should flush all the data to keep data consistency */
2265 do {
2266 sync_inodes_sb(sbi->sb);
2267 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2268 } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2269
2270 if (unlikely(retry < 0))
2271 f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2272
2273 f2fs_down_write(&sbi->gc_lock);
2274 f2fs_dirty_to_prefree(sbi);
2275
2276 clear_sbi_flag(sbi, SBI_CP_DISABLED);
2277 set_sbi_flag(sbi, SBI_IS_DIRTY);
2278 f2fs_up_write(&sbi->gc_lock);
2279
2280 f2fs_sync_fs(sbi->sb, 1);
2281
2282 /* Let's ensure there's no pending checkpoint anymore */
2283 f2fs_flush_ckpt_thread(sbi);
2284 }
2285
f2fs_remount(struct super_block * sb,int * flags,char * data)2286 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
2287 {
2288 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2289 struct f2fs_mount_info org_mount_opt;
2290 unsigned long old_sb_flags;
2291 int err;
2292 bool need_restart_gc = false, need_stop_gc = false;
2293 bool need_restart_ckpt = false, need_stop_ckpt = false;
2294 bool need_restart_flush = false, need_stop_flush = false;
2295 bool need_restart_discard = false, need_stop_discard = false;
2296 bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2297 bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
2298 bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
2299 bool no_atgc = !test_opt(sbi, ATGC);
2300 bool no_discard = !test_opt(sbi, DISCARD);
2301 bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
2302 bool block_unit_discard = f2fs_block_unit_discard(sbi);
2303 #ifdef CONFIG_QUOTA
2304 int i, j;
2305 #endif
2306
2307 /*
2308 * Save the old mount options in case we
2309 * need to restore them.
2310 */
2311 org_mount_opt = sbi->mount_opt;
2312 old_sb_flags = sb->s_flags;
2313
2314 #ifdef CONFIG_QUOTA
2315 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
2316 for (i = 0; i < MAXQUOTAS; i++) {
2317 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2318 org_mount_opt.s_qf_names[i] =
2319 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
2320 GFP_KERNEL);
2321 if (!org_mount_opt.s_qf_names[i]) {
2322 for (j = 0; j < i; j++)
2323 kfree(org_mount_opt.s_qf_names[j]);
2324 return -ENOMEM;
2325 }
2326 } else {
2327 org_mount_opt.s_qf_names[i] = NULL;
2328 }
2329 }
2330 #endif
2331
2332 /* recover superblocks we couldn't write due to previous RO mount */
2333 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
2334 err = f2fs_commit_super(sbi, false);
2335 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
2336 err);
2337 if (!err)
2338 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2339 }
2340
2341 default_options(sbi, true);
2342
2343 /* parse mount options */
2344 err = parse_options(sb, data, true);
2345 if (err)
2346 goto restore_opts;
2347
2348 /* flush outstanding errors before changing fs state */
2349 flush_work(&sbi->s_error_work);
2350
2351 /*
2352 * Previous and new state of filesystem is RO,
2353 * so skip checking GC and FLUSH_MERGE conditions.
2354 */
2355 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
2356 goto skip;
2357
2358 if (f2fs_dev_is_readonly(sbi) && !(*flags & SB_RDONLY)) {
2359 err = -EROFS;
2360 goto restore_opts;
2361 }
2362
2363 #ifdef CONFIG_QUOTA
2364 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
2365 err = dquot_suspend(sb, -1);
2366 if (err < 0)
2367 goto restore_opts;
2368 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
2369 /* dquot_resume needs RW */
2370 sb->s_flags &= ~SB_RDONLY;
2371 if (sb_any_quota_suspended(sb)) {
2372 dquot_resume(sb, -1);
2373 } else if (f2fs_sb_has_quota_ino(sbi)) {
2374 err = f2fs_enable_quotas(sb);
2375 if (err)
2376 goto restore_opts;
2377 }
2378 }
2379 #endif
2380 if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
2381 err = -EINVAL;
2382 f2fs_warn(sbi, "LFS is not compatible with IPU");
2383 goto restore_opts;
2384 }
2385
2386 /* disallow enable atgc dynamically */
2387 if (no_atgc == !!test_opt(sbi, ATGC)) {
2388 err = -EINVAL;
2389 f2fs_warn(sbi, "switch atgc option is not allowed");
2390 goto restore_opts;
2391 }
2392
2393 /* disallow enable/disable extent_cache dynamically */
2394 if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
2395 err = -EINVAL;
2396 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2397 goto restore_opts;
2398 }
2399 /* disallow enable/disable age extent_cache dynamically */
2400 if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2401 err = -EINVAL;
2402 f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
2403 goto restore_opts;
2404 }
2405
2406 if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2407 err = -EINVAL;
2408 f2fs_warn(sbi, "switch compress_cache option is not allowed");
2409 goto restore_opts;
2410 }
2411
2412 if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
2413 err = -EINVAL;
2414 f2fs_warn(sbi, "switch discard_unit option is not allowed");
2415 goto restore_opts;
2416 }
2417
2418 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
2419 err = -EINVAL;
2420 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
2421 goto restore_opts;
2422 }
2423
2424 /*
2425 * We stop the GC thread if FS is mounted as RO
2426 * or if background_gc = off is passed in mount
2427 * option. Also sync the filesystem.
2428 */
2429 if ((*flags & SB_RDONLY) ||
2430 (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2431 !test_opt(sbi, GC_MERGE))) {
2432 if (sbi->gc_thread) {
2433 f2fs_stop_gc_thread(sbi);
2434 need_restart_gc = true;
2435 }
2436 } else if (!sbi->gc_thread) {
2437 err = f2fs_start_gc_thread(sbi);
2438 if (err)
2439 goto restore_opts;
2440 need_stop_gc = true;
2441 }
2442
2443 if (*flags & SB_RDONLY) {
2444 sync_inodes_sb(sb);
2445
2446 set_sbi_flag(sbi, SBI_IS_DIRTY);
2447 set_sbi_flag(sbi, SBI_IS_CLOSE);
2448 f2fs_sync_fs(sb, 1);
2449 clear_sbi_flag(sbi, SBI_IS_CLOSE);
2450 }
2451
2452 if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2453 !test_opt(sbi, MERGE_CHECKPOINT)) {
2454 f2fs_stop_ckpt_thread(sbi);
2455 need_restart_ckpt = true;
2456 } else {
2457 /* Flush if the prevous checkpoint, if exists. */
2458 f2fs_flush_ckpt_thread(sbi);
2459
2460 err = f2fs_start_ckpt_thread(sbi);
2461 if (err) {
2462 f2fs_err(sbi,
2463 "Failed to start F2FS issue_checkpoint_thread (%d)",
2464 err);
2465 goto restore_gc;
2466 }
2467 need_stop_ckpt = true;
2468 }
2469
2470 /*
2471 * We stop issue flush thread if FS is mounted as RO
2472 * or if flush_merge is not passed in mount option.
2473 */
2474 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
2475 clear_opt(sbi, FLUSH_MERGE);
2476 f2fs_destroy_flush_cmd_control(sbi, false);
2477 need_restart_flush = true;
2478 } else {
2479 err = f2fs_create_flush_cmd_control(sbi);
2480 if (err)
2481 goto restore_ckpt;
2482 need_stop_flush = true;
2483 }
2484
2485 if (no_discard == !!test_opt(sbi, DISCARD)) {
2486 if (test_opt(sbi, DISCARD)) {
2487 err = f2fs_start_discard_thread(sbi);
2488 if (err)
2489 goto restore_flush;
2490 need_stop_discard = true;
2491 } else {
2492 f2fs_stop_discard_thread(sbi);
2493 f2fs_issue_discard_timeout(sbi);
2494 need_restart_discard = true;
2495 }
2496 }
2497
2498 if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
2499 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2500 err = f2fs_disable_checkpoint(sbi);
2501 if (err)
2502 goto restore_discard;
2503 } else {
2504 f2fs_enable_checkpoint(sbi);
2505 }
2506 }
2507
2508 skip:
2509 #ifdef CONFIG_QUOTA
2510 /* Release old quota file names */
2511 for (i = 0; i < MAXQUOTAS; i++)
2512 kfree(org_mount_opt.s_qf_names[i]);
2513 #endif
2514 /* Update the POSIXACL Flag */
2515 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2516 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2517
2518 limit_reserve_root(sbi);
2519 adjust_unusable_cap_perc(sbi);
2520 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
2521 return 0;
2522 restore_discard:
2523 if (need_restart_discard) {
2524 if (f2fs_start_discard_thread(sbi))
2525 f2fs_warn(sbi, "discard has been stopped");
2526 } else if (need_stop_discard) {
2527 f2fs_stop_discard_thread(sbi);
2528 }
2529 restore_flush:
2530 if (need_restart_flush) {
2531 if (f2fs_create_flush_cmd_control(sbi))
2532 f2fs_warn(sbi, "background flush thread has stopped");
2533 } else if (need_stop_flush) {
2534 clear_opt(sbi, FLUSH_MERGE);
2535 f2fs_destroy_flush_cmd_control(sbi, false);
2536 }
2537 restore_ckpt:
2538 if (need_restart_ckpt) {
2539 if (f2fs_start_ckpt_thread(sbi))
2540 f2fs_warn(sbi, "background ckpt thread has stopped");
2541 } else if (need_stop_ckpt) {
2542 f2fs_stop_ckpt_thread(sbi);
2543 }
2544 restore_gc:
2545 if (need_restart_gc) {
2546 if (f2fs_start_gc_thread(sbi))
2547 f2fs_warn(sbi, "background gc thread has stopped");
2548 } else if (need_stop_gc) {
2549 f2fs_stop_gc_thread(sbi);
2550 }
2551 restore_opts:
2552 #ifdef CONFIG_QUOTA
2553 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
2554 for (i = 0; i < MAXQUOTAS; i++) {
2555 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
2556 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
2557 }
2558 #endif
2559 sbi->mount_opt = org_mount_opt;
2560 sb->s_flags = old_sb_flags;
2561 return err;
2562 }
2563
f2fs_shutdown(struct super_block * sb)2564 static void f2fs_shutdown(struct super_block *sb)
2565 {
2566 f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
2567 }
2568
2569 #ifdef CONFIG_QUOTA
f2fs_need_recovery(struct f2fs_sb_info * sbi)2570 static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
2571 {
2572 /* need to recovery orphan */
2573 if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
2574 return true;
2575 /* need to recovery data */
2576 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
2577 return false;
2578 if (test_opt(sbi, NORECOVERY))
2579 return false;
2580 return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
2581 }
2582
f2fs_recover_quota_begin(struct f2fs_sb_info * sbi)2583 static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
2584 {
2585 bool readonly = f2fs_readonly(sbi->sb);
2586
2587 if (!f2fs_need_recovery(sbi))
2588 return false;
2589
2590 /* it doesn't need to check f2fs_sb_has_readonly() */
2591 if (f2fs_hw_is_readonly(sbi))
2592 return false;
2593
2594 if (readonly) {
2595 sbi->sb->s_flags &= ~SB_RDONLY;
2596 set_sbi_flag(sbi, SBI_IS_WRITABLE);
2597 }
2598
2599 /*
2600 * Turn on quotas which were not enabled for read-only mounts if
2601 * filesystem has quota feature, so that they are updated correctly.
2602 */
2603 return f2fs_enable_quota_files(sbi, readonly);
2604 }
2605
f2fs_recover_quota_end(struct f2fs_sb_info * sbi,bool quota_enabled)2606 static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
2607 bool quota_enabled)
2608 {
2609 if (quota_enabled)
2610 f2fs_quota_off_umount(sbi->sb);
2611
2612 if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
2613 clear_sbi_flag(sbi, SBI_IS_WRITABLE);
2614 sbi->sb->s_flags |= SB_RDONLY;
2615 }
2616 }
2617
2618 /* Read data from quotafile */
f2fs_quota_read(struct super_block * sb,int type,char * data,size_t len,loff_t off)2619 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
2620 size_t len, loff_t off)
2621 {
2622 struct inode *inode = sb_dqopt(sb)->files[type];
2623 struct address_space *mapping = inode->i_mapping;
2624 block_t blkidx = F2FS_BYTES_TO_BLK(off);
2625 int offset = off & (sb->s_blocksize - 1);
2626 int tocopy;
2627 size_t toread;
2628 loff_t i_size = i_size_read(inode);
2629 struct page *page;
2630
2631 if (off > i_size)
2632 return 0;
2633
2634 if (off + len > i_size)
2635 len = i_size - off;
2636 toread = len;
2637 while (toread > 0) {
2638 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
2639 repeat:
2640 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
2641 if (IS_ERR(page)) {
2642 if (PTR_ERR(page) == -ENOMEM) {
2643 memalloc_retry_wait(GFP_NOFS);
2644 goto repeat;
2645 }
2646 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2647 return PTR_ERR(page);
2648 }
2649
2650 lock_page(page);
2651
2652 if (unlikely(page->mapping != mapping)) {
2653 f2fs_put_page(page, 1);
2654 goto repeat;
2655 }
2656 if (unlikely(!PageUptodate(page))) {
2657 f2fs_put_page(page, 1);
2658 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2659 return -EIO;
2660 }
2661
2662 memcpy_from_page(data, page, offset, tocopy);
2663 f2fs_put_page(page, 1);
2664
2665 offset = 0;
2666 toread -= tocopy;
2667 data += tocopy;
2668 blkidx++;
2669 }
2670 return len;
2671 }
2672
2673 /* Write to quotafile */
f2fs_quota_write(struct super_block * sb,int type,const char * data,size_t len,loff_t off)2674 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
2675 const char *data, size_t len, loff_t off)
2676 {
2677 struct inode *inode = sb_dqopt(sb)->files[type];
2678 struct address_space *mapping = inode->i_mapping;
2679 const struct address_space_operations *a_ops = mapping->a_ops;
2680 int offset = off & (sb->s_blocksize - 1);
2681 size_t towrite = len;
2682 struct page *page;
2683 void *fsdata = NULL;
2684 int err = 0;
2685 int tocopy;
2686
2687 while (towrite > 0) {
2688 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
2689 towrite);
2690 retry:
2691 err = a_ops->write_begin(NULL, mapping, off, tocopy,
2692 &page, &fsdata);
2693 if (unlikely(err)) {
2694 if (err == -ENOMEM) {
2695 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
2696 goto retry;
2697 }
2698 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2699 break;
2700 }
2701
2702 memcpy_to_page(page, offset, data, tocopy);
2703
2704 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
2705 page, fsdata);
2706 offset = 0;
2707 towrite -= tocopy;
2708 off += tocopy;
2709 data += tocopy;
2710 cond_resched();
2711 }
2712
2713 if (len == towrite)
2714 return err;
2715 inode->i_mtime = inode_set_ctime_current(inode);
2716 f2fs_mark_inode_dirty_sync(inode, false);
2717 return len - towrite;
2718 }
2719
f2fs_dquot_initialize(struct inode * inode)2720 int f2fs_dquot_initialize(struct inode *inode)
2721 {
2722 if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
2723 return -ESRCH;
2724
2725 return dquot_initialize(inode);
2726 }
2727
f2fs_get_dquots(struct inode * inode)2728 static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
2729 {
2730 return F2FS_I(inode)->i_dquot;
2731 }
2732
f2fs_get_reserved_space(struct inode * inode)2733 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
2734 {
2735 return &F2FS_I(inode)->i_reserved_quota;
2736 }
2737
f2fs_quota_on_mount(struct f2fs_sb_info * sbi,int type)2738 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
2739 {
2740 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
2741 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
2742 return 0;
2743 }
2744
2745 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
2746 F2FS_OPTION(sbi).s_jquota_fmt, type);
2747 }
2748
f2fs_enable_quota_files(struct f2fs_sb_info * sbi,bool rdonly)2749 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
2750 {
2751 int enabled = 0;
2752 int i, err;
2753
2754 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
2755 err = f2fs_enable_quotas(sbi->sb);
2756 if (err) {
2757 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
2758 return 0;
2759 }
2760 return 1;
2761 }
2762
2763 for (i = 0; i < MAXQUOTAS; i++) {
2764 if (F2FS_OPTION(sbi).s_qf_names[i]) {
2765 err = f2fs_quota_on_mount(sbi, i);
2766 if (!err) {
2767 enabled = 1;
2768 continue;
2769 }
2770 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
2771 err, i);
2772 }
2773 }
2774 return enabled;
2775 }
2776
f2fs_quota_enable(struct super_block * sb,int type,int format_id,unsigned int flags)2777 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
2778 unsigned int flags)
2779 {
2780 struct inode *qf_inode;
2781 unsigned long qf_inum;
2782 unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
2783 int err;
2784
2785 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
2786
2787 qf_inum = f2fs_qf_ino(sb, type);
2788 if (!qf_inum)
2789 return -EPERM;
2790
2791 qf_inode = f2fs_iget(sb, qf_inum);
2792 if (IS_ERR(qf_inode)) {
2793 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
2794 return PTR_ERR(qf_inode);
2795 }
2796
2797 /* Don't account quota for quota files to avoid recursion */
2798 inode_lock(qf_inode);
2799 qf_inode->i_flags |= S_NOQUOTA;
2800
2801 if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
2802 F2FS_I(qf_inode)->i_flags |= qf_flag;
2803 f2fs_set_inode_flags(qf_inode);
2804 }
2805 inode_unlock(qf_inode);
2806
2807 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
2808 iput(qf_inode);
2809 return err;
2810 }
2811
f2fs_enable_quotas(struct super_block * sb)2812 static int f2fs_enable_quotas(struct super_block *sb)
2813 {
2814 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2815 int type, err = 0;
2816 unsigned long qf_inum;
2817 bool quota_mopt[MAXQUOTAS] = {
2818 test_opt(sbi, USRQUOTA),
2819 test_opt(sbi, GRPQUOTA),
2820 test_opt(sbi, PRJQUOTA),
2821 };
2822
2823 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
2824 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
2825 return 0;
2826 }
2827
2828 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
2829
2830 for (type = 0; type < MAXQUOTAS; type++) {
2831 qf_inum = f2fs_qf_ino(sb, type);
2832 if (qf_inum) {
2833 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
2834 DQUOT_USAGE_ENABLED |
2835 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
2836 if (err) {
2837 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
2838 type, err);
2839 for (type--; type >= 0; type--)
2840 dquot_quota_off(sb, type);
2841 set_sbi_flag(F2FS_SB(sb),
2842 SBI_QUOTA_NEED_REPAIR);
2843 return err;
2844 }
2845 }
2846 }
2847 return 0;
2848 }
2849
f2fs_quota_sync_file(struct f2fs_sb_info * sbi,int type)2850 static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2851 {
2852 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2853 struct address_space *mapping = dqopt->files[type]->i_mapping;
2854 int ret = 0;
2855
2856 ret = dquot_writeback_dquots(sbi->sb, type);
2857 if (ret)
2858 goto out;
2859
2860 ret = filemap_fdatawrite(mapping);
2861 if (ret)
2862 goto out;
2863
2864 /* if we are using journalled quota */
2865 if (is_journalled_quota(sbi))
2866 goto out;
2867
2868 ret = filemap_fdatawait(mapping);
2869
2870 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2871 out:
2872 if (ret)
2873 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2874 return ret;
2875 }
2876
f2fs_quota_sync(struct super_block * sb,int type)2877 int f2fs_quota_sync(struct super_block *sb, int type)
2878 {
2879 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2880 struct quota_info *dqopt = sb_dqopt(sb);
2881 int cnt;
2882 int ret = 0;
2883
2884 /*
2885 * Now when everything is written we can discard the pagecache so
2886 * that userspace sees the changes.
2887 */
2888 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2889
2890 if (type != -1 && cnt != type)
2891 continue;
2892
2893 if (!sb_has_quota_active(sb, cnt))
2894 continue;
2895
2896 if (!f2fs_sb_has_quota_ino(sbi))
2897 inode_lock(dqopt->files[cnt]);
2898
2899 /*
2900 * do_quotactl
2901 * f2fs_quota_sync
2902 * f2fs_down_read(quota_sem)
2903 * dquot_writeback_dquots()
2904 * f2fs_dquot_commit
2905 * block_operation
2906 * f2fs_down_read(quota_sem)
2907 */
2908 f2fs_lock_op(sbi);
2909 f2fs_down_read(&sbi->quota_sem);
2910
2911 ret = f2fs_quota_sync_file(sbi, cnt);
2912
2913 f2fs_up_read(&sbi->quota_sem);
2914 f2fs_unlock_op(sbi);
2915
2916 if (!f2fs_sb_has_quota_ino(sbi))
2917 inode_unlock(dqopt->files[cnt]);
2918
2919 if (ret)
2920 break;
2921 }
2922 return ret;
2923 }
2924
f2fs_quota_on(struct super_block * sb,int type,int format_id,const struct path * path)2925 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2926 const struct path *path)
2927 {
2928 struct inode *inode;
2929 int err;
2930
2931 /* if quota sysfile exists, deny enabling quota with specific file */
2932 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2933 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2934 return -EBUSY;
2935 }
2936
2937 if (path->dentry->d_sb != sb)
2938 return -EXDEV;
2939
2940 err = f2fs_quota_sync(sb, type);
2941 if (err)
2942 return err;
2943
2944 inode = d_inode(path->dentry);
2945
2946 err = filemap_fdatawrite(inode->i_mapping);
2947 if (err)
2948 return err;
2949
2950 err = filemap_fdatawait(inode->i_mapping);
2951 if (err)
2952 return err;
2953
2954 err = dquot_quota_on(sb, type, format_id, path);
2955 if (err)
2956 return err;
2957
2958 inode_lock(inode);
2959 F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
2960 f2fs_set_inode_flags(inode);
2961 inode_unlock(inode);
2962 f2fs_mark_inode_dirty_sync(inode, false);
2963
2964 return 0;
2965 }
2966
__f2fs_quota_off(struct super_block * sb,int type)2967 static int __f2fs_quota_off(struct super_block *sb, int type)
2968 {
2969 struct inode *inode = sb_dqopt(sb)->files[type];
2970 int err;
2971
2972 if (!inode || !igrab(inode))
2973 return dquot_quota_off(sb, type);
2974
2975 err = f2fs_quota_sync(sb, type);
2976 if (err)
2977 goto out_put;
2978
2979 err = dquot_quota_off(sb, type);
2980 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
2981 goto out_put;
2982
2983 inode_lock(inode);
2984 F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
2985 f2fs_set_inode_flags(inode);
2986 inode_unlock(inode);
2987 f2fs_mark_inode_dirty_sync(inode, false);
2988 out_put:
2989 iput(inode);
2990 return err;
2991 }
2992
f2fs_quota_off(struct super_block * sb,int type)2993 static int f2fs_quota_off(struct super_block *sb, int type)
2994 {
2995 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2996 int err;
2997
2998 err = __f2fs_quota_off(sb, type);
2999
3000 /*
3001 * quotactl can shutdown journalled quota, result in inconsistence
3002 * between quota record and fs data by following updates, tag the
3003 * flag to let fsck be aware of it.
3004 */
3005 if (is_journalled_quota(sbi))
3006 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3007 return err;
3008 }
3009
f2fs_quota_off_umount(struct super_block * sb)3010 void f2fs_quota_off_umount(struct super_block *sb)
3011 {
3012 int type;
3013 int err;
3014
3015 for (type = 0; type < MAXQUOTAS; type++) {
3016 err = __f2fs_quota_off(sb, type);
3017 if (err) {
3018 int ret = dquot_quota_off(sb, type);
3019
3020 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
3021 type, err, ret);
3022 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
3023 }
3024 }
3025 /*
3026 * In case of checkpoint=disable, we must flush quota blocks.
3027 * This can cause NULL exception for node_inode in end_io, since
3028 * put_super already dropped it.
3029 */
3030 sync_filesystem(sb);
3031 }
3032
f2fs_truncate_quota_inode_pages(struct super_block * sb)3033 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
3034 {
3035 struct quota_info *dqopt = sb_dqopt(sb);
3036 int type;
3037
3038 for (type = 0; type < MAXQUOTAS; type++) {
3039 if (!dqopt->files[type])
3040 continue;
3041 f2fs_inode_synced(dqopt->files[type]);
3042 }
3043 }
3044
f2fs_dquot_commit(struct dquot * dquot)3045 static int f2fs_dquot_commit(struct dquot *dquot)
3046 {
3047 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3048 int ret;
3049
3050 f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
3051 ret = dquot_commit(dquot);
3052 if (ret < 0)
3053 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3054 f2fs_up_read(&sbi->quota_sem);
3055 return ret;
3056 }
3057
f2fs_dquot_acquire(struct dquot * dquot)3058 static int f2fs_dquot_acquire(struct dquot *dquot)
3059 {
3060 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3061 int ret;
3062
3063 f2fs_down_read(&sbi->quota_sem);
3064 ret = dquot_acquire(dquot);
3065 if (ret < 0)
3066 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3067 f2fs_up_read(&sbi->quota_sem);
3068 return ret;
3069 }
3070
f2fs_dquot_release(struct dquot * dquot)3071 static int f2fs_dquot_release(struct dquot *dquot)
3072 {
3073 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
3074 int ret = dquot_release(dquot);
3075
3076 if (ret < 0)
3077 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3078 return ret;
3079 }
3080
f2fs_dquot_mark_dquot_dirty(struct dquot * dquot)3081 static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
3082 {
3083 struct super_block *sb = dquot->dq_sb;
3084 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3085 int ret = dquot_mark_dquot_dirty(dquot);
3086
3087 /* if we are using journalled quota */
3088 if (is_journalled_quota(sbi))
3089 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
3090
3091 return ret;
3092 }
3093
f2fs_dquot_commit_info(struct super_block * sb,int type)3094 static int f2fs_dquot_commit_info(struct super_block *sb, int type)
3095 {
3096 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3097 int ret = dquot_commit_info(sb, type);
3098
3099 if (ret < 0)
3100 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3101 return ret;
3102 }
3103
f2fs_get_projid(struct inode * inode,kprojid_t * projid)3104 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
3105 {
3106 *projid = F2FS_I(inode)->i_projid;
3107 return 0;
3108 }
3109
3110 static const struct dquot_operations f2fs_quota_operations = {
3111 .get_reserved_space = f2fs_get_reserved_space,
3112 .write_dquot = f2fs_dquot_commit,
3113 .acquire_dquot = f2fs_dquot_acquire,
3114 .release_dquot = f2fs_dquot_release,
3115 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
3116 .write_info = f2fs_dquot_commit_info,
3117 .alloc_dquot = dquot_alloc,
3118 .destroy_dquot = dquot_destroy,
3119 .get_projid = f2fs_get_projid,
3120 .get_next_id = dquot_get_next_id,
3121 };
3122
3123 static const struct quotactl_ops f2fs_quotactl_ops = {
3124 .quota_on = f2fs_quota_on,
3125 .quota_off = f2fs_quota_off,
3126 .quota_sync = f2fs_quota_sync,
3127 .get_state = dquot_get_state,
3128 .set_info = dquot_set_dqinfo,
3129 .get_dqblk = dquot_get_dqblk,
3130 .set_dqblk = dquot_set_dqblk,
3131 .get_nextdqblk = dquot_get_next_dqblk,
3132 };
3133 #else
f2fs_dquot_initialize(struct inode * inode)3134 int f2fs_dquot_initialize(struct inode *inode)
3135 {
3136 return 0;
3137 }
3138
f2fs_quota_sync(struct super_block * sb,int type)3139 int f2fs_quota_sync(struct super_block *sb, int type)
3140 {
3141 return 0;
3142 }
3143
f2fs_quota_off_umount(struct super_block * sb)3144 void f2fs_quota_off_umount(struct super_block *sb)
3145 {
3146 }
3147 #endif
3148
3149 static const struct super_operations f2fs_sops = {
3150 .alloc_inode = f2fs_alloc_inode,
3151 .free_inode = f2fs_free_inode,
3152 .drop_inode = f2fs_drop_inode,
3153 .write_inode = f2fs_write_inode,
3154 .dirty_inode = f2fs_dirty_inode,
3155 .show_options = f2fs_show_options,
3156 #ifdef CONFIG_QUOTA
3157 .quota_read = f2fs_quota_read,
3158 .quota_write = f2fs_quota_write,
3159 .get_dquots = f2fs_get_dquots,
3160 #endif
3161 .evict_inode = f2fs_evict_inode,
3162 .put_super = f2fs_put_super,
3163 .sync_fs = f2fs_sync_fs,
3164 .freeze_fs = f2fs_freeze,
3165 .unfreeze_fs = f2fs_unfreeze,
3166 .statfs = f2fs_statfs,
3167 .remount_fs = f2fs_remount,
3168 .shutdown = f2fs_shutdown,
3169 };
3170
3171 #ifdef CONFIG_FS_ENCRYPTION
f2fs_get_context(struct inode * inode,void * ctx,size_t len)3172 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
3173 {
3174 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3175 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3176 ctx, len, NULL);
3177 }
3178
f2fs_set_context(struct inode * inode,const void * ctx,size_t len,void * fs_data)3179 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
3180 void *fs_data)
3181 {
3182 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3183
3184 /*
3185 * Encrypting the root directory is not allowed because fsck
3186 * expects lost+found directory to exist and remain unencrypted
3187 * if LOST_FOUND feature is enabled.
3188 *
3189 */
3190 if (f2fs_sb_has_lost_found(sbi) &&
3191 inode->i_ino == F2FS_ROOT_INO(sbi))
3192 return -EPERM;
3193
3194 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
3195 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
3196 ctx, len, fs_data, XATTR_CREATE);
3197 }
3198
f2fs_get_dummy_policy(struct super_block * sb)3199 static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
3200 {
3201 return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
3202 }
3203
f2fs_has_stable_inodes(struct super_block * sb)3204 static bool f2fs_has_stable_inodes(struct super_block *sb)
3205 {
3206 return true;
3207 }
3208
f2fs_get_ino_and_lblk_bits(struct super_block * sb,int * ino_bits_ret,int * lblk_bits_ret)3209 static void f2fs_get_ino_and_lblk_bits(struct super_block *sb,
3210 int *ino_bits_ret, int *lblk_bits_ret)
3211 {
3212 *ino_bits_ret = 8 * sizeof(nid_t);
3213 *lblk_bits_ret = 8 * sizeof(block_t);
3214 }
3215
f2fs_get_devices(struct super_block * sb,unsigned int * num_devs)3216 static struct block_device **f2fs_get_devices(struct super_block *sb,
3217 unsigned int *num_devs)
3218 {
3219 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3220 struct block_device **devs;
3221 int i;
3222
3223 if (!f2fs_is_multi_device(sbi))
3224 return NULL;
3225
3226 devs = kmalloc_array(sbi->s_ndevs, sizeof(*devs), GFP_KERNEL);
3227 if (!devs)
3228 return ERR_PTR(-ENOMEM);
3229
3230 for (i = 0; i < sbi->s_ndevs; i++)
3231 devs[i] = FDEV(i).bdev;
3232 *num_devs = sbi->s_ndevs;
3233 return devs;
3234 }
3235
3236 static const struct fscrypt_operations f2fs_cryptops = {
3237 .key_prefix = "f2fs:",
3238 .get_context = f2fs_get_context,
3239 .set_context = f2fs_set_context,
3240 .get_dummy_policy = f2fs_get_dummy_policy,
3241 .empty_dir = f2fs_empty_dir,
3242 .has_stable_inodes = f2fs_has_stable_inodes,
3243 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
3244 .get_devices = f2fs_get_devices,
3245 };
3246 #endif
3247
f2fs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)3248 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
3249 u64 ino, u32 generation)
3250 {
3251 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3252 struct inode *inode;
3253
3254 if (f2fs_check_nid_range(sbi, ino))
3255 return ERR_PTR(-ESTALE);
3256
3257 /*
3258 * f2fs_iget isn't quite right if the inode is currently unallocated!
3259 * However f2fs_iget currently does appropriate checks to handle stale
3260 * inodes so everything is OK.
3261 */
3262 inode = f2fs_iget(sb, ino);
3263 if (IS_ERR(inode))
3264 return ERR_CAST(inode);
3265 if (unlikely(generation && inode->i_generation != generation)) {
3266 /* we didn't find the right inode.. */
3267 iput(inode);
3268 return ERR_PTR(-ESTALE);
3269 }
3270 return inode;
3271 }
3272
f2fs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)3273 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
3274 int fh_len, int fh_type)
3275 {
3276 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3277 f2fs_nfs_get_inode);
3278 }
3279
f2fs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)3280 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
3281 int fh_len, int fh_type)
3282 {
3283 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3284 f2fs_nfs_get_inode);
3285 }
3286
3287 static const struct export_operations f2fs_export_ops = {
3288 .fh_to_dentry = f2fs_fh_to_dentry,
3289 .fh_to_parent = f2fs_fh_to_parent,
3290 .get_parent = f2fs_get_parent,
3291 };
3292
max_file_blocks(struct inode * inode)3293 loff_t max_file_blocks(struct inode *inode)
3294 {
3295 loff_t result = 0;
3296 loff_t leaf_count;
3297
3298 /*
3299 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
3300 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
3301 * space in inode.i_addr, it will be more safe to reassign
3302 * result as zero.
3303 */
3304
3305 if (inode && f2fs_compressed_file(inode))
3306 leaf_count = ADDRS_PER_BLOCK(inode);
3307 else
3308 leaf_count = DEF_ADDRS_PER_BLOCK;
3309
3310 /* two direct node blocks */
3311 result += (leaf_count * 2);
3312
3313 /* two indirect node blocks */
3314 leaf_count *= NIDS_PER_BLOCK;
3315 result += (leaf_count * 2);
3316
3317 /* one double indirect node block */
3318 leaf_count *= NIDS_PER_BLOCK;
3319 result += leaf_count;
3320
3321 return result;
3322 }
3323
__f2fs_commit_super(struct buffer_head * bh,struct f2fs_super_block * super)3324 static int __f2fs_commit_super(struct buffer_head *bh,
3325 struct f2fs_super_block *super)
3326 {
3327 lock_buffer(bh);
3328 if (super)
3329 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
3330 set_buffer_dirty(bh);
3331 unlock_buffer(bh);
3332
3333 /* it's rare case, we can do fua all the time */
3334 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
3335 }
3336
sanity_check_area_boundary(struct f2fs_sb_info * sbi,struct buffer_head * bh)3337 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
3338 struct buffer_head *bh)
3339 {
3340 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3341 (bh->b_data + F2FS_SUPER_OFFSET);
3342 struct super_block *sb = sbi->sb;
3343 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
3344 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
3345 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
3346 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
3347 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
3348 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
3349 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
3350 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
3351 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
3352 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
3353 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3354 u32 segment_count = le32_to_cpu(raw_super->segment_count);
3355 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3356 u64 main_end_blkaddr = main_blkaddr +
3357 ((u64)segment_count_main << log_blocks_per_seg);
3358 u64 seg_end_blkaddr = segment0_blkaddr +
3359 ((u64)segment_count << log_blocks_per_seg);
3360
3361 if (segment0_blkaddr != cp_blkaddr) {
3362 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
3363 segment0_blkaddr, cp_blkaddr);
3364 return true;
3365 }
3366
3367 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
3368 sit_blkaddr) {
3369 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
3370 cp_blkaddr, sit_blkaddr,
3371 segment_count_ckpt << log_blocks_per_seg);
3372 return true;
3373 }
3374
3375 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
3376 nat_blkaddr) {
3377 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
3378 sit_blkaddr, nat_blkaddr,
3379 segment_count_sit << log_blocks_per_seg);
3380 return true;
3381 }
3382
3383 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
3384 ssa_blkaddr) {
3385 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
3386 nat_blkaddr, ssa_blkaddr,
3387 segment_count_nat << log_blocks_per_seg);
3388 return true;
3389 }
3390
3391 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
3392 main_blkaddr) {
3393 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
3394 ssa_blkaddr, main_blkaddr,
3395 segment_count_ssa << log_blocks_per_seg);
3396 return true;
3397 }
3398
3399 if (main_end_blkaddr > seg_end_blkaddr) {
3400 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3401 main_blkaddr, seg_end_blkaddr,
3402 segment_count_main << log_blocks_per_seg);
3403 return true;
3404 } else if (main_end_blkaddr < seg_end_blkaddr) {
3405 int err = 0;
3406 char *res;
3407
3408 /* fix in-memory information all the time */
3409 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
3410 segment0_blkaddr) >> log_blocks_per_seg);
3411
3412 if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
3413 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3414 res = "internally";
3415 } else {
3416 err = __f2fs_commit_super(bh, NULL);
3417 res = err ? "failed" : "done";
3418 }
3419 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3420 res, main_blkaddr, seg_end_blkaddr,
3421 segment_count_main << log_blocks_per_seg);
3422 if (err)
3423 return true;
3424 }
3425 return false;
3426 }
3427
sanity_check_raw_super(struct f2fs_sb_info * sbi,struct buffer_head * bh)3428 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
3429 struct buffer_head *bh)
3430 {
3431 block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
3432 block_t total_sections, blocks_per_seg;
3433 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
3434 (bh->b_data + F2FS_SUPER_OFFSET);
3435 size_t crc_offset = 0;
3436 __u32 crc = 0;
3437
3438 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
3439 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
3440 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
3441 return -EINVAL;
3442 }
3443
3444 /* Check checksum_offset and crc in superblock */
3445 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
3446 crc_offset = le32_to_cpu(raw_super->checksum_offset);
3447 if (crc_offset !=
3448 offsetof(struct f2fs_super_block, crc)) {
3449 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
3450 crc_offset);
3451 return -EFSCORRUPTED;
3452 }
3453 crc = le32_to_cpu(raw_super->crc);
3454 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
3455 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
3456 return -EFSCORRUPTED;
3457 }
3458 }
3459
3460 /* Currently, support only 4KB block size */
3461 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3462 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3463 le32_to_cpu(raw_super->log_blocksize),
3464 F2FS_BLKSIZE_BITS);
3465 return -EFSCORRUPTED;
3466 }
3467
3468 /* check log blocks per segment */
3469 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
3470 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
3471 le32_to_cpu(raw_super->log_blocks_per_seg));
3472 return -EFSCORRUPTED;
3473 }
3474
3475 /* Currently, support 512/1024/2048/4096 bytes sector size */
3476 if (le32_to_cpu(raw_super->log_sectorsize) >
3477 F2FS_MAX_LOG_SECTOR_SIZE ||
3478 le32_to_cpu(raw_super->log_sectorsize) <
3479 F2FS_MIN_LOG_SECTOR_SIZE) {
3480 f2fs_info(sbi, "Invalid log sectorsize (%u)",
3481 le32_to_cpu(raw_super->log_sectorsize));
3482 return -EFSCORRUPTED;
3483 }
3484 if (le32_to_cpu(raw_super->log_sectors_per_block) +
3485 le32_to_cpu(raw_super->log_sectorsize) !=
3486 F2FS_MAX_LOG_SECTOR_SIZE) {
3487 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
3488 le32_to_cpu(raw_super->log_sectors_per_block),
3489 le32_to_cpu(raw_super->log_sectorsize));
3490 return -EFSCORRUPTED;
3491 }
3492
3493 segment_count = le32_to_cpu(raw_super->segment_count);
3494 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3495 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3496 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3497 total_sections = le32_to_cpu(raw_super->section_count);
3498
3499 /* blocks_per_seg should be 512, given the above check */
3500 blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
3501
3502 if (segment_count > F2FS_MAX_SEGMENT ||
3503 segment_count < F2FS_MIN_SEGMENTS) {
3504 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
3505 return -EFSCORRUPTED;
3506 }
3507
3508 if (total_sections > segment_count_main || total_sections < 1 ||
3509 segs_per_sec > segment_count || !segs_per_sec) {
3510 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
3511 segment_count, total_sections, segs_per_sec);
3512 return -EFSCORRUPTED;
3513 }
3514
3515 if (segment_count_main != total_sections * segs_per_sec) {
3516 f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3517 segment_count_main, total_sections, segs_per_sec);
3518 return -EFSCORRUPTED;
3519 }
3520
3521 if ((segment_count / segs_per_sec) < total_sections) {
3522 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
3523 segment_count, segs_per_sec, total_sections);
3524 return -EFSCORRUPTED;
3525 }
3526
3527 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
3528 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
3529 segment_count, le64_to_cpu(raw_super->block_count));
3530 return -EFSCORRUPTED;
3531 }
3532
3533 if (RDEV(0).path[0]) {
3534 block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
3535 int i = 1;
3536
3537 while (i < MAX_DEVICES && RDEV(i).path[0]) {
3538 dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
3539 i++;
3540 }
3541 if (segment_count != dev_seg_count) {
3542 f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
3543 segment_count, dev_seg_count);
3544 return -EFSCORRUPTED;
3545 }
3546 } else {
3547 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3548 !bdev_is_zoned(sbi->sb->s_bdev)) {
3549 f2fs_info(sbi, "Zoned block device path is missing");
3550 return -EFSCORRUPTED;
3551 }
3552 }
3553
3554 if (secs_per_zone > total_sections || !secs_per_zone) {
3555 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
3556 secs_per_zone, total_sections);
3557 return -EFSCORRUPTED;
3558 }
3559 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
3560 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
3561 (le32_to_cpu(raw_super->extension_count) +
3562 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
3563 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
3564 le32_to_cpu(raw_super->extension_count),
3565 raw_super->hot_ext_count,
3566 F2FS_MAX_EXTENSION);
3567 return -EFSCORRUPTED;
3568 }
3569
3570 if (le32_to_cpu(raw_super->cp_payload) >=
3571 (blocks_per_seg - F2FS_CP_PACKS -
3572 NR_CURSEG_PERSIST_TYPE)) {
3573 f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
3574 le32_to_cpu(raw_super->cp_payload),
3575 blocks_per_seg - F2FS_CP_PACKS -
3576 NR_CURSEG_PERSIST_TYPE);
3577 return -EFSCORRUPTED;
3578 }
3579
3580 /* check reserved ino info */
3581 if (le32_to_cpu(raw_super->node_ino) != 1 ||
3582 le32_to_cpu(raw_super->meta_ino) != 2 ||
3583 le32_to_cpu(raw_super->root_ino) != 3) {
3584 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
3585 le32_to_cpu(raw_super->node_ino),
3586 le32_to_cpu(raw_super->meta_ino),
3587 le32_to_cpu(raw_super->root_ino));
3588 return -EFSCORRUPTED;
3589 }
3590
3591 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
3592 if (sanity_check_area_boundary(sbi, bh))
3593 return -EFSCORRUPTED;
3594
3595 return 0;
3596 }
3597
f2fs_sanity_check_ckpt(struct f2fs_sb_info * sbi)3598 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
3599 {
3600 unsigned int total, fsmeta;
3601 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3602 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3603 unsigned int ovp_segments, reserved_segments;
3604 unsigned int main_segs, blocks_per_seg;
3605 unsigned int sit_segs, nat_segs;
3606 unsigned int sit_bitmap_size, nat_bitmap_size;
3607 unsigned int log_blocks_per_seg;
3608 unsigned int segment_count_main;
3609 unsigned int cp_pack_start_sum, cp_payload;
3610 block_t user_block_count, valid_user_blocks;
3611 block_t avail_node_count, valid_node_count;
3612 unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
3613 unsigned int sit_blk_cnt;
3614 int i, j;
3615
3616 total = le32_to_cpu(raw_super->segment_count);
3617 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
3618 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
3619 fsmeta += sit_segs;
3620 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
3621 fsmeta += nat_segs;
3622 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
3623 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
3624
3625 if (unlikely(fsmeta >= total))
3626 return 1;
3627
3628 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
3629 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
3630
3631 if (!f2fs_sb_has_readonly(sbi) &&
3632 unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
3633 ovp_segments == 0 || reserved_segments == 0)) {
3634 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
3635 return 1;
3636 }
3637 user_block_count = le64_to_cpu(ckpt->user_block_count);
3638 segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3639 (f2fs_sb_has_readonly(sbi) ? 1 : 0);
3640 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3641 if (!user_block_count || user_block_count >=
3642 segment_count_main << log_blocks_per_seg) {
3643 f2fs_err(sbi, "Wrong user_block_count: %u",
3644 user_block_count);
3645 return 1;
3646 }
3647
3648 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
3649 if (valid_user_blocks > user_block_count) {
3650 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
3651 valid_user_blocks, user_block_count);
3652 return 1;
3653 }
3654
3655 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
3656 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
3657 if (valid_node_count > avail_node_count) {
3658 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
3659 valid_node_count, avail_node_count);
3660 return 1;
3661 }
3662
3663 main_segs = le32_to_cpu(raw_super->segment_count_main);
3664 blocks_per_seg = BLKS_PER_SEG(sbi);
3665
3666 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3667 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
3668 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
3669 return 1;
3670
3671 if (f2fs_sb_has_readonly(sbi))
3672 goto check_data;
3673
3674 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
3675 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3676 le32_to_cpu(ckpt->cur_node_segno[j])) {
3677 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
3678 i, j,
3679 le32_to_cpu(ckpt->cur_node_segno[i]));
3680 return 1;
3681 }
3682 }
3683 }
3684 check_data:
3685 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
3686 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
3687 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
3688 return 1;
3689
3690 if (f2fs_sb_has_readonly(sbi))
3691 goto skip_cross;
3692
3693 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
3694 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
3695 le32_to_cpu(ckpt->cur_data_segno[j])) {
3696 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
3697 i, j,
3698 le32_to_cpu(ckpt->cur_data_segno[i]));
3699 return 1;
3700 }
3701 }
3702 }
3703 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
3704 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
3705 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
3706 le32_to_cpu(ckpt->cur_data_segno[j])) {
3707 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
3708 i, j,
3709 le32_to_cpu(ckpt->cur_node_segno[i]));
3710 return 1;
3711 }
3712 }
3713 }
3714 skip_cross:
3715 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
3716 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
3717
3718 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
3719 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
3720 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
3721 sit_bitmap_size, nat_bitmap_size);
3722 return 1;
3723 }
3724
3725 sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK);
3726 if (sit_bitmap_size * 8 < sit_blk_cnt) {
3727 f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u",
3728 sit_bitmap_size, sit_blk_cnt);
3729 return 1;
3730 }
3731
3732 cp_pack_start_sum = __start_sum_addr(sbi);
3733 cp_payload = __cp_payload(sbi);
3734 if (cp_pack_start_sum < cp_payload + 1 ||
3735 cp_pack_start_sum > blocks_per_seg - 1 -
3736 NR_CURSEG_PERSIST_TYPE) {
3737 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
3738 cp_pack_start_sum);
3739 return 1;
3740 }
3741
3742 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
3743 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
3744 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
3745 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
3746 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
3747 le32_to_cpu(ckpt->checksum_offset));
3748 return 1;
3749 }
3750
3751 nat_blocks = nat_segs << log_blocks_per_seg;
3752 nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3753 nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3754 if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3755 (cp_payload + F2FS_CP_PACKS +
3756 NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3757 f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3758 cp_payload, nat_bits_blocks);
3759 return 1;
3760 }
3761
3762 if (unlikely(f2fs_cp_error(sbi))) {
3763 f2fs_err(sbi, "A bug case: need to run fsck");
3764 return 1;
3765 }
3766 return 0;
3767 }
3768
init_sb_info(struct f2fs_sb_info * sbi)3769 static void init_sb_info(struct f2fs_sb_info *sbi)
3770 {
3771 struct f2fs_super_block *raw_super = sbi->raw_super;
3772 int i;
3773
3774 sbi->log_sectors_per_block =
3775 le32_to_cpu(raw_super->log_sectors_per_block);
3776 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
3777 sbi->blocksize = BIT(sbi->log_blocksize);
3778 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
3779 sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
3780 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
3781 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
3782 sbi->total_sections = le32_to_cpu(raw_super->section_count);
3783 sbi->total_node_count =
3784 ((le32_to_cpu(raw_super->segment_count_nat) / 2) *
3785 NAT_ENTRY_PER_BLOCK) << sbi->log_blocks_per_seg;
3786 F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3787 F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3788 F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
3789 sbi->cur_victim_sec = NULL_SECNO;
3790 sbi->gc_mode = GC_NORMAL;
3791 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
3792 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
3793 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
3794 sbi->migration_granularity = SEGS_PER_SEC(sbi);
3795 sbi->seq_file_ra_mul = MIN_RA_MUL;
3796 sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3797 sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
3798 spin_lock_init(&sbi->gc_remaining_trials_lock);
3799 atomic64_set(&sbi->current_atomic_write, 0);
3800
3801 sbi->dir_level = DEF_DIR_LEVEL;
3802 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
3803 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
3804 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
3805 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
3806 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
3807 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
3808 DEF_UMOUNT_DISCARD_TIMEOUT;
3809 clear_sbi_flag(sbi, SBI_NEED_FSCK);
3810
3811 for (i = 0; i < NR_COUNT_TYPE; i++)
3812 atomic_set(&sbi->nr_pages[i], 0);
3813
3814 for (i = 0; i < META; i++)
3815 atomic_set(&sbi->wb_sync_req[i], 0);
3816
3817 INIT_LIST_HEAD(&sbi->s_list);
3818 mutex_init(&sbi->umount_mutex);
3819 init_f2fs_rwsem(&sbi->io_order_lock);
3820 spin_lock_init(&sbi->cp_lock);
3821
3822 sbi->dirty_device = 0;
3823 spin_lock_init(&sbi->dev_lock);
3824
3825 init_f2fs_rwsem(&sbi->sb_lock);
3826 init_f2fs_rwsem(&sbi->pin_sem);
3827 }
3828
init_percpu_info(struct f2fs_sb_info * sbi)3829 static int init_percpu_info(struct f2fs_sb_info *sbi)
3830 {
3831 int err;
3832
3833 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
3834 if (err)
3835 return err;
3836
3837 err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
3838 if (err)
3839 goto err_valid_block;
3840
3841 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
3842 GFP_KERNEL);
3843 if (err)
3844 goto err_node_block;
3845 return 0;
3846
3847 err_node_block:
3848 percpu_counter_destroy(&sbi->rf_node_block_count);
3849 err_valid_block:
3850 percpu_counter_destroy(&sbi->alloc_valid_block_count);
3851 return err;
3852 }
3853
3854 #ifdef CONFIG_BLK_DEV_ZONED
3855
3856 struct f2fs_report_zones_args {
3857 struct f2fs_sb_info *sbi;
3858 struct f2fs_dev_info *dev;
3859 };
3860
f2fs_report_zone_cb(struct blk_zone * zone,unsigned int idx,void * data)3861 static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3862 void *data)
3863 {
3864 struct f2fs_report_zones_args *rz_args = data;
3865 block_t unusable_blocks = (zone->len - zone->capacity) >>
3866 F2FS_LOG_SECTORS_PER_BLOCK;
3867
3868 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3869 return 0;
3870
3871 set_bit(idx, rz_args->dev->blkz_seq);
3872 if (!rz_args->sbi->unusable_blocks_per_sec) {
3873 rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
3874 return 0;
3875 }
3876 if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
3877 f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
3878 return -EINVAL;
3879 }
3880 return 0;
3881 }
3882
init_blkz_info(struct f2fs_sb_info * sbi,int devi)3883 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
3884 {
3885 struct block_device *bdev = FDEV(devi).bdev;
3886 sector_t nr_sectors = bdev_nr_sectors(bdev);
3887 struct f2fs_report_zones_args rep_zone_arg;
3888 u64 zone_sectors;
3889 int ret;
3890
3891 if (!f2fs_sb_has_blkzoned(sbi))
3892 return 0;
3893
3894 zone_sectors = bdev_zone_sectors(bdev);
3895 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
3896 SECTOR_TO_BLOCK(zone_sectors))
3897 return -EINVAL;
3898 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
3899 FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
3900 sbi->blocks_per_blkz);
3901 if (nr_sectors & (zone_sectors - 1))
3902 FDEV(devi).nr_blkz++;
3903
3904 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
3905 BITS_TO_LONGS(FDEV(devi).nr_blkz)
3906 * sizeof(unsigned long),
3907 GFP_KERNEL);
3908 if (!FDEV(devi).blkz_seq)
3909 return -ENOMEM;
3910
3911 rep_zone_arg.sbi = sbi;
3912 rep_zone_arg.dev = &FDEV(devi);
3913
3914 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3915 &rep_zone_arg);
3916 if (ret < 0)
3917 return ret;
3918 return 0;
3919 }
3920 #endif
3921
3922 /*
3923 * Read f2fs raw super block.
3924 * Because we have two copies of super block, so read both of them
3925 * to get the first valid one. If any one of them is broken, we pass
3926 * them recovery flag back to the caller.
3927 */
read_raw_super_block(struct f2fs_sb_info * sbi,struct f2fs_super_block ** raw_super,int * valid_super_block,int * recovery)3928 static int read_raw_super_block(struct f2fs_sb_info *sbi,
3929 struct f2fs_super_block **raw_super,
3930 int *valid_super_block, int *recovery)
3931 {
3932 struct super_block *sb = sbi->sb;
3933 int block;
3934 struct buffer_head *bh;
3935 struct f2fs_super_block *super;
3936 int err = 0;
3937
3938 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
3939 if (!super)
3940 return -ENOMEM;
3941
3942 for (block = 0; block < 2; block++) {
3943 bh = sb_bread(sb, block);
3944 if (!bh) {
3945 f2fs_err(sbi, "Unable to read %dth superblock",
3946 block + 1);
3947 err = -EIO;
3948 *recovery = 1;
3949 continue;
3950 }
3951
3952 /* sanity checking of raw super */
3953 err = sanity_check_raw_super(sbi, bh);
3954 if (err) {
3955 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
3956 block + 1);
3957 brelse(bh);
3958 *recovery = 1;
3959 continue;
3960 }
3961
3962 if (!*raw_super) {
3963 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
3964 sizeof(*super));
3965 *valid_super_block = block;
3966 *raw_super = super;
3967 }
3968 brelse(bh);
3969 }
3970
3971 /* No valid superblock */
3972 if (!*raw_super)
3973 kfree(super);
3974 else
3975 err = 0;
3976
3977 return err;
3978 }
3979
f2fs_commit_super(struct f2fs_sb_info * sbi,bool recover)3980 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3981 {
3982 struct buffer_head *bh;
3983 __u32 crc = 0;
3984 int err;
3985
3986 if ((recover && f2fs_readonly(sbi->sb)) ||
3987 f2fs_hw_is_readonly(sbi)) {
3988 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3989 return -EROFS;
3990 }
3991
3992 /* we should update superblock crc here */
3993 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3994 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3995 offsetof(struct f2fs_super_block, crc));
3996 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3997 }
3998
3999 /* write back-up superblock first */
4000 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
4001 if (!bh)
4002 return -EIO;
4003 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4004 brelse(bh);
4005
4006 /* if we are in recovery path, skip writing valid superblock */
4007 if (recover || err)
4008 return err;
4009
4010 /* write current valid superblock */
4011 bh = sb_bread(sbi->sb, sbi->valid_super_block);
4012 if (!bh)
4013 return -EIO;
4014 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
4015 brelse(bh);
4016 return err;
4017 }
4018
save_stop_reason(struct f2fs_sb_info * sbi,unsigned char reason)4019 static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
4020 {
4021 unsigned long flags;
4022
4023 spin_lock_irqsave(&sbi->error_lock, flags);
4024 if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
4025 sbi->stop_reason[reason]++;
4026 spin_unlock_irqrestore(&sbi->error_lock, flags);
4027 }
4028
f2fs_record_stop_reason(struct f2fs_sb_info * sbi)4029 static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
4030 {
4031 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4032 unsigned long flags;
4033 int err;
4034
4035 f2fs_down_write(&sbi->sb_lock);
4036
4037 spin_lock_irqsave(&sbi->error_lock, flags);
4038 if (sbi->error_dirty) {
4039 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4040 MAX_F2FS_ERRORS);
4041 sbi->error_dirty = false;
4042 }
4043 memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
4044 spin_unlock_irqrestore(&sbi->error_lock, flags);
4045
4046 err = f2fs_commit_super(sbi, false);
4047
4048 f2fs_up_write(&sbi->sb_lock);
4049 if (err)
4050 f2fs_err(sbi, "f2fs_commit_super fails to record err:%d", err);
4051 }
4052
f2fs_save_errors(struct f2fs_sb_info * sbi,unsigned char flag)4053 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
4054 {
4055 unsigned long flags;
4056
4057 spin_lock_irqsave(&sbi->error_lock, flags);
4058 if (!test_bit(flag, (unsigned long *)sbi->errors)) {
4059 set_bit(flag, (unsigned long *)sbi->errors);
4060 sbi->error_dirty = true;
4061 }
4062 spin_unlock_irqrestore(&sbi->error_lock, flags);
4063 }
4064
f2fs_update_errors(struct f2fs_sb_info * sbi)4065 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4066 {
4067 unsigned long flags;
4068 bool need_update = false;
4069
4070 spin_lock_irqsave(&sbi->error_lock, flags);
4071 if (sbi->error_dirty) {
4072 memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4073 MAX_F2FS_ERRORS);
4074 sbi->error_dirty = false;
4075 need_update = true;
4076 }
4077 spin_unlock_irqrestore(&sbi->error_lock, flags);
4078
4079 return need_update;
4080 }
4081
f2fs_record_errors(struct f2fs_sb_info * sbi,unsigned char error)4082 static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4083 {
4084 int err;
4085
4086 f2fs_down_write(&sbi->sb_lock);
4087
4088 if (!f2fs_update_errors(sbi))
4089 goto out_unlock;
4090
4091 err = f2fs_commit_super(sbi, false);
4092 if (err)
4093 f2fs_err(sbi, "f2fs_commit_super fails to record errors:%u, err:%d",
4094 error, err);
4095 out_unlock:
4096 f2fs_up_write(&sbi->sb_lock);
4097 }
4098
f2fs_handle_error(struct f2fs_sb_info * sbi,unsigned char error)4099 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4100 {
4101 f2fs_save_errors(sbi, error);
4102 f2fs_record_errors(sbi, error);
4103 }
4104
f2fs_handle_error_async(struct f2fs_sb_info * sbi,unsigned char error)4105 void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
4106 {
4107 f2fs_save_errors(sbi, error);
4108
4109 if (!sbi->error_dirty)
4110 return;
4111 if (!test_bit(error, (unsigned long *)sbi->errors))
4112 return;
4113 schedule_work(&sbi->s_error_work);
4114 }
4115
system_going_down(void)4116 static bool system_going_down(void)
4117 {
4118 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4119 || system_state == SYSTEM_RESTART;
4120 }
4121
f2fs_handle_critical_error(struct f2fs_sb_info * sbi,unsigned char reason)4122 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason)
4123 {
4124 struct super_block *sb = sbi->sb;
4125 bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
4126 bool continue_fs = !shutdown &&
4127 F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
4128
4129 set_ckpt_flags(sbi, CP_ERROR_FLAG);
4130
4131 if (!f2fs_hw_is_readonly(sbi)) {
4132 save_stop_reason(sbi, reason);
4133
4134 /*
4135 * always create an asynchronous task to record stop_reason
4136 * in order to avoid potential deadlock when running into
4137 * f2fs_record_stop_reason() synchronously.
4138 */
4139 schedule_work(&sbi->s_error_work);
4140 }
4141
4142 /*
4143 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4144 * could panic during 'reboot -f' as the underlying device got already
4145 * disabled.
4146 */
4147 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
4148 !shutdown && !system_going_down() &&
4149 !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
4150 panic("F2FS-fs (device %s): panic forced after error\n",
4151 sb->s_id);
4152
4153 if (shutdown)
4154 set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
4155
4156 /*
4157 * Continue filesystem operators if errors=continue. Should not set
4158 * RO by shutdown, since RO bypasses thaw_super which can hang the
4159 * system.
4160 */
4161 if (continue_fs || f2fs_readonly(sb) || shutdown) {
4162 f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
4163 return;
4164 }
4165
4166 f2fs_warn(sbi, "Remounting filesystem read-only");
4167
4168 /*
4169 * We have already set CP_ERROR_FLAG flag to stop all updates
4170 * to filesystem, so it doesn't need to set SB_RDONLY flag here
4171 * because the flag should be set covered w/ sb->s_umount semaphore
4172 * via remount procedure, otherwise, it will confuse code like
4173 * freeze_super() which will lead to deadlocks and other problems.
4174 */
4175 }
4176
f2fs_record_error_work(struct work_struct * work)4177 static void f2fs_record_error_work(struct work_struct *work)
4178 {
4179 struct f2fs_sb_info *sbi = container_of(work,
4180 struct f2fs_sb_info, s_error_work);
4181
4182 f2fs_record_stop_reason(sbi);
4183 }
4184
f2fs_scan_devices(struct f2fs_sb_info * sbi)4185 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
4186 {
4187 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4188 unsigned int max_devices = MAX_DEVICES;
4189 unsigned int logical_blksize;
4190 blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
4191 int i;
4192
4193 /* Initialize single device information */
4194 if (!RDEV(0).path[0]) {
4195 if (!bdev_is_zoned(sbi->sb->s_bdev))
4196 return 0;
4197 max_devices = 1;
4198 }
4199
4200 /*
4201 * Initialize multiple devices information, or single
4202 * zoned block device information.
4203 */
4204 sbi->devs = f2fs_kzalloc(sbi,
4205 array_size(max_devices,
4206 sizeof(struct f2fs_dev_info)),
4207 GFP_KERNEL);
4208 if (!sbi->devs)
4209 return -ENOMEM;
4210
4211 logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
4212 sbi->aligned_blksize = true;
4213
4214 for (i = 0; i < max_devices; i++) {
4215 if (i == 0)
4216 FDEV(0).bdev = sbi->sb->s_bdev;
4217 else if (!RDEV(i).path[0])
4218 break;
4219
4220 if (max_devices > 1) {
4221 /* Multi-device mount */
4222 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
4223 FDEV(i).total_segments =
4224 le32_to_cpu(RDEV(i).total_segments);
4225 if (i == 0) {
4226 FDEV(i).start_blk = 0;
4227 FDEV(i).end_blk = FDEV(i).start_blk +
4228 (FDEV(i).total_segments <<
4229 sbi->log_blocks_per_seg) - 1 +
4230 le32_to_cpu(raw_super->segment0_blkaddr);
4231 } else {
4232 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
4233 FDEV(i).end_blk = FDEV(i).start_blk +
4234 (FDEV(i).total_segments <<
4235 sbi->log_blocks_per_seg) - 1;
4236 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
4237 mode, sbi->sb, NULL);
4238 }
4239 }
4240 if (IS_ERR(FDEV(i).bdev))
4241 return PTR_ERR(FDEV(i).bdev);
4242
4243 /* to release errored devices */
4244 sbi->s_ndevs = i + 1;
4245
4246 if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
4247 sbi->aligned_blksize = false;
4248
4249 #ifdef CONFIG_BLK_DEV_ZONED
4250 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
4251 !f2fs_sb_has_blkzoned(sbi)) {
4252 f2fs_err(sbi, "Zoned block device feature not enabled");
4253 return -EINVAL;
4254 }
4255 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
4256 if (init_blkz_info(sbi, i)) {
4257 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
4258 return -EINVAL;
4259 }
4260 if (max_devices == 1)
4261 break;
4262 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
4263 i, FDEV(i).path,
4264 FDEV(i).total_segments,
4265 FDEV(i).start_blk, FDEV(i).end_blk,
4266 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
4267 "Host-aware" : "Host-managed");
4268 continue;
4269 }
4270 #endif
4271 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
4272 i, FDEV(i).path,
4273 FDEV(i).total_segments,
4274 FDEV(i).start_blk, FDEV(i).end_blk);
4275 }
4276 return 0;
4277 }
4278
f2fs_setup_casefold(struct f2fs_sb_info * sbi)4279 static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
4280 {
4281 #if IS_ENABLED(CONFIG_UNICODE)
4282 if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
4283 const struct f2fs_sb_encodings *encoding_info;
4284 struct unicode_map *encoding;
4285 __u16 encoding_flags;
4286
4287 encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
4288 if (!encoding_info) {
4289 f2fs_err(sbi,
4290 "Encoding requested by superblock is unknown");
4291 return -EINVAL;
4292 }
4293
4294 encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
4295 encoding = utf8_load(encoding_info->version);
4296 if (IS_ERR(encoding)) {
4297 f2fs_err(sbi,
4298 "can't mount with superblock charset: %s-%u.%u.%u "
4299 "not supported by the kernel. flags: 0x%x.",
4300 encoding_info->name,
4301 unicode_major(encoding_info->version),
4302 unicode_minor(encoding_info->version),
4303 unicode_rev(encoding_info->version),
4304 encoding_flags);
4305 return PTR_ERR(encoding);
4306 }
4307 f2fs_info(sbi, "Using encoding defined by superblock: "
4308 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4309 unicode_major(encoding_info->version),
4310 unicode_minor(encoding_info->version),
4311 unicode_rev(encoding_info->version),
4312 encoding_flags);
4313
4314 sbi->sb->s_encoding = encoding;
4315 sbi->sb->s_encoding_flags = encoding_flags;
4316 }
4317 #else
4318 if (f2fs_sb_has_casefold(sbi)) {
4319 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
4320 return -EINVAL;
4321 }
4322 #endif
4323 return 0;
4324 }
4325
f2fs_tuning_parameters(struct f2fs_sb_info * sbi)4326 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
4327 {
4328 /* adjust parameters according to the volume size */
4329 if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
4330 if (f2fs_block_unit_discard(sbi))
4331 SM_I(sbi)->dcc_info->discard_granularity =
4332 MIN_DISCARD_GRANULARITY;
4333 if (!f2fs_lfs_mode(sbi))
4334 SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
4335 BIT(F2FS_IPU_HONOR_OPU_WRITE);
4336 }
4337
4338 sbi->readdir_ra = true;
4339 }
4340
f2fs_fill_super(struct super_block * sb,void * data,int silent)4341 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
4342 {
4343 struct f2fs_sb_info *sbi;
4344 struct f2fs_super_block *raw_super;
4345 struct inode *root;
4346 int err;
4347 bool skip_recovery = false, need_fsck = false;
4348 char *options = NULL;
4349 int recovery, i, valid_super_block;
4350 struct curseg_info *seg_i;
4351 int retry_cnt = 1;
4352 #ifdef CONFIG_QUOTA
4353 bool quota_enabled = false;
4354 #endif
4355
4356 try_onemore:
4357 err = -EINVAL;
4358 raw_super = NULL;
4359 valid_super_block = -1;
4360 recovery = 0;
4361
4362 /* allocate memory for f2fs-specific super block info */
4363 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
4364 if (!sbi)
4365 return -ENOMEM;
4366
4367 sbi->sb = sb;
4368
4369 /* initialize locks within allocated memory */
4370 init_f2fs_rwsem(&sbi->gc_lock);
4371 mutex_init(&sbi->writepages);
4372 init_f2fs_rwsem(&sbi->cp_global_sem);
4373 init_f2fs_rwsem(&sbi->node_write);
4374 init_f2fs_rwsem(&sbi->node_change);
4375 spin_lock_init(&sbi->stat_lock);
4376 init_f2fs_rwsem(&sbi->cp_rwsem);
4377 init_f2fs_rwsem(&sbi->quota_sem);
4378 init_waitqueue_head(&sbi->cp_wait);
4379 spin_lock_init(&sbi->error_lock);
4380
4381 for (i = 0; i < NR_INODE_TYPE; i++) {
4382 INIT_LIST_HEAD(&sbi->inode_list[i]);
4383 spin_lock_init(&sbi->inode_lock[i]);
4384 }
4385 mutex_init(&sbi->flush_lock);
4386
4387 /* Load the checksum driver */
4388 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
4389 if (IS_ERR(sbi->s_chksum_driver)) {
4390 f2fs_err(sbi, "Cannot load crc32 driver.");
4391 err = PTR_ERR(sbi->s_chksum_driver);
4392 sbi->s_chksum_driver = NULL;
4393 goto free_sbi;
4394 }
4395
4396 /* set a block size */
4397 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
4398 f2fs_err(sbi, "unable to set blocksize");
4399 goto free_sbi;
4400 }
4401
4402 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
4403 &recovery);
4404 if (err)
4405 goto free_sbi;
4406
4407 sb->s_fs_info = sbi;
4408 sbi->raw_super = raw_super;
4409
4410 INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
4411 memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
4412 memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
4413
4414 /* precompute checksum seed for metadata */
4415 if (f2fs_sb_has_inode_chksum(sbi))
4416 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
4417 sizeof(raw_super->uuid));
4418
4419 default_options(sbi, false);
4420 /* parse mount options */
4421 options = kstrdup((const char *)data, GFP_KERNEL);
4422 if (data && !options) {
4423 err = -ENOMEM;
4424 goto free_sb_buf;
4425 }
4426
4427 err = parse_options(sb, options, false);
4428 if (err)
4429 goto free_options;
4430
4431 sb->s_maxbytes = max_file_blocks(NULL) <<
4432 le32_to_cpu(raw_super->log_blocksize);
4433 sb->s_max_links = F2FS_LINK_MAX;
4434
4435 err = f2fs_setup_casefold(sbi);
4436 if (err)
4437 goto free_options;
4438
4439 #ifdef CONFIG_QUOTA
4440 sb->dq_op = &f2fs_quota_operations;
4441 sb->s_qcop = &f2fs_quotactl_ops;
4442 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4443
4444 if (f2fs_sb_has_quota_ino(sbi)) {
4445 for (i = 0; i < MAXQUOTAS; i++) {
4446 if (f2fs_qf_ino(sbi->sb, i))
4447 sbi->nquota_files++;
4448 }
4449 }
4450 #endif
4451
4452 sb->s_op = &f2fs_sops;
4453 #ifdef CONFIG_FS_ENCRYPTION
4454 sb->s_cop = &f2fs_cryptops;
4455 #endif
4456 #ifdef CONFIG_FS_VERITY
4457 sb->s_vop = &f2fs_verityops;
4458 #endif
4459 sb->s_xattr = f2fs_xattr_handlers;
4460 sb->s_export_op = &f2fs_export_ops;
4461 sb->s_magic = F2FS_SUPER_MAGIC;
4462 sb->s_time_gran = 1;
4463 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4464 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
4465 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
4466 sb->s_iflags |= SB_I_CGROUPWB;
4467
4468 /* init f2fs-specific super block info */
4469 sbi->valid_super_block = valid_super_block;
4470
4471 /* disallow all the data/node/meta page writes */
4472 set_sbi_flag(sbi, SBI_POR_DOING);
4473
4474 err = f2fs_init_write_merge_io(sbi);
4475 if (err)
4476 goto free_bio_info;
4477
4478 init_sb_info(sbi);
4479
4480 err = f2fs_init_iostat(sbi);
4481 if (err)
4482 goto free_bio_info;
4483
4484 err = init_percpu_info(sbi);
4485 if (err)
4486 goto free_iostat;
4487
4488 /* init per sbi slab cache */
4489 err = f2fs_init_xattr_caches(sbi);
4490 if (err)
4491 goto free_percpu;
4492 err = f2fs_init_page_array_cache(sbi);
4493 if (err)
4494 goto free_xattr_cache;
4495
4496 /* get an inode for meta space */
4497 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
4498 if (IS_ERR(sbi->meta_inode)) {
4499 f2fs_err(sbi, "Failed to read F2FS meta data inode");
4500 err = PTR_ERR(sbi->meta_inode);
4501 goto free_page_array_cache;
4502 }
4503
4504 err = f2fs_get_valid_checkpoint(sbi);
4505 if (err) {
4506 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
4507 goto free_meta_inode;
4508 }
4509
4510 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
4511 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
4512 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
4513 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4514 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
4515 }
4516
4517 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
4518 set_sbi_flag(sbi, SBI_NEED_FSCK);
4519
4520 /* Initialize device list */
4521 err = f2fs_scan_devices(sbi);
4522 if (err) {
4523 f2fs_err(sbi, "Failed to find devices");
4524 goto free_devices;
4525 }
4526
4527 err = f2fs_init_post_read_wq(sbi);
4528 if (err) {
4529 f2fs_err(sbi, "Failed to initialize post read workqueue");
4530 goto free_devices;
4531 }
4532
4533 sbi->total_valid_node_count =
4534 le32_to_cpu(sbi->ckpt->valid_node_count);
4535 percpu_counter_set(&sbi->total_valid_inode_count,
4536 le32_to_cpu(sbi->ckpt->valid_inode_count));
4537 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
4538 sbi->total_valid_block_count =
4539 le64_to_cpu(sbi->ckpt->valid_block_count);
4540 sbi->last_valid_block_count = sbi->total_valid_block_count;
4541 sbi->reserved_blocks = 0;
4542 sbi->current_reserved_blocks = 0;
4543 limit_reserve_root(sbi);
4544 adjust_unusable_cap_perc(sbi);
4545
4546 f2fs_init_extent_cache_info(sbi);
4547
4548 f2fs_init_ino_entry_info(sbi);
4549
4550 f2fs_init_fsync_node_info(sbi);
4551
4552 /* setup checkpoint request control and start checkpoint issue thread */
4553 f2fs_init_ckpt_req_control(sbi);
4554 if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4555 test_opt(sbi, MERGE_CHECKPOINT)) {
4556 err = f2fs_start_ckpt_thread(sbi);
4557 if (err) {
4558 f2fs_err(sbi,
4559 "Failed to start F2FS issue_checkpoint_thread (%d)",
4560 err);
4561 goto stop_ckpt_thread;
4562 }
4563 }
4564
4565 /* setup f2fs internal modules */
4566 err = f2fs_build_segment_manager(sbi);
4567 if (err) {
4568 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
4569 err);
4570 goto free_sm;
4571 }
4572 err = f2fs_build_node_manager(sbi);
4573 if (err) {
4574 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
4575 err);
4576 goto free_nm;
4577 }
4578
4579 /* For write statistics */
4580 sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
4581
4582 /* Read accumulated write IO statistics if exists */
4583 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
4584 if (__exist_node_summaries(sbi))
4585 sbi->kbytes_written =
4586 le64_to_cpu(seg_i->journal->info.kbytes_written);
4587
4588 f2fs_build_gc_manager(sbi);
4589
4590 err = f2fs_build_stats(sbi);
4591 if (err)
4592 goto free_nm;
4593
4594 /* get an inode for node space */
4595 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
4596 if (IS_ERR(sbi->node_inode)) {
4597 f2fs_err(sbi, "Failed to read node inode");
4598 err = PTR_ERR(sbi->node_inode);
4599 goto free_stats;
4600 }
4601
4602 /* read root inode and dentry */
4603 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
4604 if (IS_ERR(root)) {
4605 f2fs_err(sbi, "Failed to read root inode");
4606 err = PTR_ERR(root);
4607 goto free_node_inode;
4608 }
4609 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
4610 !root->i_size || !root->i_nlink) {
4611 iput(root);
4612 err = -EINVAL;
4613 goto free_node_inode;
4614 }
4615
4616 sb->s_root = d_make_root(root); /* allocate root dentry */
4617 if (!sb->s_root) {
4618 err = -ENOMEM;
4619 goto free_node_inode;
4620 }
4621
4622 err = f2fs_init_compress_inode(sbi);
4623 if (err)
4624 goto free_root_inode;
4625
4626 err = f2fs_register_sysfs(sbi);
4627 if (err)
4628 goto free_compress_inode;
4629
4630 #ifdef CONFIG_QUOTA
4631 /* Enable quota usage during mount */
4632 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
4633 err = f2fs_enable_quotas(sb);
4634 if (err)
4635 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
4636 }
4637
4638 quota_enabled = f2fs_recover_quota_begin(sbi);
4639 #endif
4640 /* if there are any orphan inodes, free them */
4641 err = f2fs_recover_orphan_inodes(sbi);
4642 if (err)
4643 goto free_meta;
4644
4645 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
4646 goto reset_checkpoint;
4647
4648 /* recover fsynced data */
4649 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
4650 !test_opt(sbi, NORECOVERY)) {
4651 /*
4652 * mount should be failed, when device has readonly mode, and
4653 * previous checkpoint was not done by clean system shutdown.
4654 */
4655 if (f2fs_hw_is_readonly(sbi)) {
4656 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4657 err = f2fs_recover_fsync_data(sbi, true);
4658 if (err > 0) {
4659 err = -EROFS;
4660 f2fs_err(sbi, "Need to recover fsync data, but "
4661 "write access unavailable, please try "
4662 "mount w/ disable_roll_forward or norecovery");
4663 }
4664 if (err < 0)
4665 goto free_meta;
4666 }
4667 f2fs_info(sbi, "write access unavailable, skipping recovery");
4668 goto reset_checkpoint;
4669 }
4670
4671 if (need_fsck)
4672 set_sbi_flag(sbi, SBI_NEED_FSCK);
4673
4674 if (skip_recovery)
4675 goto reset_checkpoint;
4676
4677 err = f2fs_recover_fsync_data(sbi, false);
4678 if (err < 0) {
4679 if (err != -ENOMEM)
4680 skip_recovery = true;
4681 need_fsck = true;
4682 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
4683 err);
4684 goto free_meta;
4685 }
4686 } else {
4687 err = f2fs_recover_fsync_data(sbi, true);
4688
4689 if (!f2fs_readonly(sb) && err > 0) {
4690 err = -EINVAL;
4691 f2fs_err(sbi, "Need to recover fsync data");
4692 goto free_meta;
4693 }
4694 }
4695
4696 #ifdef CONFIG_QUOTA
4697 f2fs_recover_quota_end(sbi, quota_enabled);
4698 #endif
4699
4700 /*
4701 * If the f2fs is not readonly and fsync data recovery succeeds,
4702 * check zoned block devices' write pointer consistency.
4703 */
4704 if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4705 err = f2fs_check_write_pointer(sbi);
4706 if (err)
4707 goto free_meta;
4708 }
4709
4710 reset_checkpoint:
4711 f2fs_init_inmem_curseg(sbi);
4712
4713 /* f2fs_recover_fsync_data() cleared this already */
4714 clear_sbi_flag(sbi, SBI_POR_DOING);
4715
4716 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
4717 err = f2fs_disable_checkpoint(sbi);
4718 if (err)
4719 goto sync_free_meta;
4720 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
4721 f2fs_enable_checkpoint(sbi);
4722 }
4723
4724 /*
4725 * If filesystem is not mounted as read-only then
4726 * do start the gc_thread.
4727 */
4728 if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4729 test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
4730 /* After POR, we can run background GC thread.*/
4731 err = f2fs_start_gc_thread(sbi);
4732 if (err)
4733 goto sync_free_meta;
4734 }
4735 kvfree(options);
4736
4737 /* recover broken superblock */
4738 if (recovery) {
4739 err = f2fs_commit_super(sbi, true);
4740 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
4741 sbi->valid_super_block ? 1 : 2, err);
4742 }
4743
4744 f2fs_join_shrinker(sbi);
4745
4746 f2fs_tuning_parameters(sbi);
4747
4748 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
4749 cur_cp_version(F2FS_CKPT(sbi)));
4750 f2fs_update_time(sbi, CP_TIME);
4751 f2fs_update_time(sbi, REQ_TIME);
4752 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
4753 return 0;
4754
4755 sync_free_meta:
4756 /* safe to flush all the data */
4757 sync_filesystem(sbi->sb);
4758 retry_cnt = 0;
4759
4760 free_meta:
4761 #ifdef CONFIG_QUOTA
4762 f2fs_truncate_quota_inode_pages(sb);
4763 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
4764 f2fs_quota_off_umount(sbi->sb);
4765 #endif
4766 /*
4767 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
4768 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
4769 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
4770 * falls into an infinite loop in f2fs_sync_meta_pages().
4771 */
4772 truncate_inode_pages_final(META_MAPPING(sbi));
4773 /* evict some inodes being cached by GC */
4774 evict_inodes(sb);
4775 f2fs_unregister_sysfs(sbi);
4776 free_compress_inode:
4777 f2fs_destroy_compress_inode(sbi);
4778 free_root_inode:
4779 dput(sb->s_root);
4780 sb->s_root = NULL;
4781 free_node_inode:
4782 f2fs_release_ino_entry(sbi, true);
4783 truncate_inode_pages_final(NODE_MAPPING(sbi));
4784 iput(sbi->node_inode);
4785 sbi->node_inode = NULL;
4786 free_stats:
4787 f2fs_destroy_stats(sbi);
4788 free_nm:
4789 /* stop discard thread before destroying node manager */
4790 f2fs_stop_discard_thread(sbi);
4791 f2fs_destroy_node_manager(sbi);
4792 free_sm:
4793 f2fs_destroy_segment_manager(sbi);
4794 stop_ckpt_thread:
4795 f2fs_stop_ckpt_thread(sbi);
4796 /* flush s_error_work before sbi destroy */
4797 flush_work(&sbi->s_error_work);
4798 f2fs_destroy_post_read_wq(sbi);
4799 free_devices:
4800 destroy_device_list(sbi);
4801 kvfree(sbi->ckpt);
4802 free_meta_inode:
4803 make_bad_inode(sbi->meta_inode);
4804 iput(sbi->meta_inode);
4805 sbi->meta_inode = NULL;
4806 free_page_array_cache:
4807 f2fs_destroy_page_array_cache(sbi);
4808 free_xattr_cache:
4809 f2fs_destroy_xattr_caches(sbi);
4810 free_percpu:
4811 destroy_percpu_info(sbi);
4812 free_iostat:
4813 f2fs_destroy_iostat(sbi);
4814 free_bio_info:
4815 for (i = 0; i < NR_PAGE_TYPE; i++)
4816 kvfree(sbi->write_io[i]);
4817
4818 #if IS_ENABLED(CONFIG_UNICODE)
4819 utf8_unload(sb->s_encoding);
4820 sb->s_encoding = NULL;
4821 #endif
4822 free_options:
4823 #ifdef CONFIG_QUOTA
4824 for (i = 0; i < MAXQUOTAS; i++)
4825 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
4826 #endif
4827 fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
4828 kvfree(options);
4829 free_sb_buf:
4830 kfree(raw_super);
4831 free_sbi:
4832 if (sbi->s_chksum_driver)
4833 crypto_free_shash(sbi->s_chksum_driver);
4834 kfree(sbi);
4835
4836 /* give only one another chance */
4837 if (retry_cnt > 0 && skip_recovery) {
4838 retry_cnt--;
4839 shrink_dcache_sb(sb);
4840 goto try_onemore;
4841 }
4842 return err;
4843 }
4844
f2fs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)4845 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
4846 const char *dev_name, void *data)
4847 {
4848 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
4849 }
4850
kill_f2fs_super(struct super_block * sb)4851 static void kill_f2fs_super(struct super_block *sb)
4852 {
4853 if (sb->s_root) {
4854 struct f2fs_sb_info *sbi = F2FS_SB(sb);
4855
4856 set_sbi_flag(sbi, SBI_IS_CLOSE);
4857 f2fs_stop_gc_thread(sbi);
4858 f2fs_stop_discard_thread(sbi);
4859
4860 #ifdef CONFIG_F2FS_FS_COMPRESSION
4861 /*
4862 * latter evict_inode() can bypass checking and invalidating
4863 * compress inode cache.
4864 */
4865 if (test_opt(sbi, COMPRESS_CACHE))
4866 truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4867 #endif
4868
4869 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
4870 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
4871 struct cp_control cpc = {
4872 .reason = CP_UMOUNT,
4873 };
4874 stat_inc_cp_call_count(sbi, TOTAL_CALL);
4875 f2fs_write_checkpoint(sbi, &cpc);
4876 }
4877
4878 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
4879 sb->s_flags &= ~SB_RDONLY;
4880 }
4881 kill_block_super(sb);
4882 }
4883
4884 static struct file_system_type f2fs_fs_type = {
4885 .owner = THIS_MODULE,
4886 .name = "f2fs",
4887 .mount = f2fs_mount,
4888 .kill_sb = kill_f2fs_super,
4889 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
4890 };
4891 MODULE_ALIAS_FS("f2fs");
4892
init_inodecache(void)4893 static int __init init_inodecache(void)
4894 {
4895 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
4896 sizeof(struct f2fs_inode_info), 0,
4897 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
4898 return f2fs_inode_cachep ? 0 : -ENOMEM;
4899 }
4900
destroy_inodecache(void)4901 static void destroy_inodecache(void)
4902 {
4903 /*
4904 * Make sure all delayed rcu free inodes are flushed before we
4905 * destroy cache.
4906 */
4907 rcu_barrier();
4908 kmem_cache_destroy(f2fs_inode_cachep);
4909 }
4910
init_f2fs_fs(void)4911 static int __init init_f2fs_fs(void)
4912 {
4913 int err;
4914
4915 if (PAGE_SIZE != F2FS_BLKSIZE) {
4916 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
4917 PAGE_SIZE, F2FS_BLKSIZE);
4918 return -EINVAL;
4919 }
4920
4921 err = init_inodecache();
4922 if (err)
4923 goto fail;
4924 err = f2fs_create_node_manager_caches();
4925 if (err)
4926 goto free_inodecache;
4927 err = f2fs_create_segment_manager_caches();
4928 if (err)
4929 goto free_node_manager_caches;
4930 err = f2fs_create_checkpoint_caches();
4931 if (err)
4932 goto free_segment_manager_caches;
4933 err = f2fs_create_recovery_cache();
4934 if (err)
4935 goto free_checkpoint_caches;
4936 err = f2fs_create_extent_cache();
4937 if (err)
4938 goto free_recovery_cache;
4939 err = f2fs_create_garbage_collection_cache();
4940 if (err)
4941 goto free_extent_cache;
4942 err = f2fs_init_sysfs();
4943 if (err)
4944 goto free_garbage_collection_cache;
4945 err = register_shrinker(&f2fs_shrinker_info, "f2fs-shrinker");
4946 if (err)
4947 goto free_sysfs;
4948 f2fs_create_root_stats();
4949 err = f2fs_init_post_read_processing();
4950 if (err)
4951 goto free_root_stats;
4952 err = f2fs_init_iostat_processing();
4953 if (err)
4954 goto free_post_read;
4955 err = f2fs_init_bio_entry_cache();
4956 if (err)
4957 goto free_iostat;
4958 err = f2fs_init_bioset();
4959 if (err)
4960 goto free_bio_entry_cache;
4961 err = f2fs_init_compress_mempool();
4962 if (err)
4963 goto free_bioset;
4964 err = f2fs_init_compress_cache();
4965 if (err)
4966 goto free_compress_mempool;
4967 err = f2fs_create_casefold_cache();
4968 if (err)
4969 goto free_compress_cache;
4970 err = register_filesystem(&f2fs_fs_type);
4971 if (err)
4972 goto free_casefold_cache;
4973 return 0;
4974 free_casefold_cache:
4975 f2fs_destroy_casefold_cache();
4976 free_compress_cache:
4977 f2fs_destroy_compress_cache();
4978 free_compress_mempool:
4979 f2fs_destroy_compress_mempool();
4980 free_bioset:
4981 f2fs_destroy_bioset();
4982 free_bio_entry_cache:
4983 f2fs_destroy_bio_entry_cache();
4984 free_iostat:
4985 f2fs_destroy_iostat_processing();
4986 free_post_read:
4987 f2fs_destroy_post_read_processing();
4988 free_root_stats:
4989 f2fs_destroy_root_stats();
4990 unregister_shrinker(&f2fs_shrinker_info);
4991 free_sysfs:
4992 f2fs_exit_sysfs();
4993 free_garbage_collection_cache:
4994 f2fs_destroy_garbage_collection_cache();
4995 free_extent_cache:
4996 f2fs_destroy_extent_cache();
4997 free_recovery_cache:
4998 f2fs_destroy_recovery_cache();
4999 free_checkpoint_caches:
5000 f2fs_destroy_checkpoint_caches();
5001 free_segment_manager_caches:
5002 f2fs_destroy_segment_manager_caches();
5003 free_node_manager_caches:
5004 f2fs_destroy_node_manager_caches();
5005 free_inodecache:
5006 destroy_inodecache();
5007 fail:
5008 return err;
5009 }
5010
exit_f2fs_fs(void)5011 static void __exit exit_f2fs_fs(void)
5012 {
5013 unregister_filesystem(&f2fs_fs_type);
5014 f2fs_destroy_casefold_cache();
5015 f2fs_destroy_compress_cache();
5016 f2fs_destroy_compress_mempool();
5017 f2fs_destroy_bioset();
5018 f2fs_destroy_bio_entry_cache();
5019 f2fs_destroy_iostat_processing();
5020 f2fs_destroy_post_read_processing();
5021 f2fs_destroy_root_stats();
5022 unregister_shrinker(&f2fs_shrinker_info);
5023 f2fs_exit_sysfs();
5024 f2fs_destroy_garbage_collection_cache();
5025 f2fs_destroy_extent_cache();
5026 f2fs_destroy_recovery_cache();
5027 f2fs_destroy_checkpoint_caches();
5028 f2fs_destroy_segment_manager_caches();
5029 f2fs_destroy_node_manager_caches();
5030 destroy_inodecache();
5031 }
5032
5033 module_init(init_f2fs_fs)
5034 module_exit(exit_f2fs_fs)
5035
5036 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
5037 MODULE_DESCRIPTION("Flash Friendly File System");
5038 MODULE_LICENSE("GPL");
5039 MODULE_SOFTDEP("pre: crc32");
5040
5041