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