~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/f2fs/super.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php