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