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