blob: 41bf656658ba8bb9344b0dfce041bc6301e78fed [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * fs/f2fs/super.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
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>
David Brazdil0f672f62019-12-10 10:32:29 +000026#include <linux/unicode.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
28#include "f2fs.h"
29#include "node.h"
30#include "segment.h"
31#include "xattr.h"
32#include "gc.h"
33#include "trace.h"
34
35#define CREATE_TRACE_POINTS
36#include <trace/events/f2fs.h>
37
38static struct kmem_cache *f2fs_inode_cachep;
39
40#ifdef CONFIG_F2FS_FAULT_INJECTION
41
David Brazdil0f672f62019-12-10 10:32:29 +000042const char *f2fs_fault_name[FAULT_MAX] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 [FAULT_KMALLOC] = "kmalloc",
44 [FAULT_KVMALLOC] = "kvmalloc",
45 [FAULT_PAGE_ALLOC] = "page alloc",
46 [FAULT_PAGE_GET] = "page get",
47 [FAULT_ALLOC_BIO] = "alloc bio",
48 [FAULT_ALLOC_NID] = "alloc nid",
49 [FAULT_ORPHAN] = "orphan",
50 [FAULT_BLOCK] = "no more block",
51 [FAULT_DIR_DEPTH] = "too big dir depth",
52 [FAULT_EVICT_INODE] = "evict_inode fail",
53 [FAULT_TRUNCATE] = "truncate fail",
David Brazdil0f672f62019-12-10 10:32:29 +000054 [FAULT_READ_IO] = "read IO error",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 [FAULT_CHECKPOINT] = "checkpoint error",
56 [FAULT_DISCARD] = "discard error",
David Brazdil0f672f62019-12-10 10:32:29 +000057 [FAULT_WRITE_IO] = "write IO error",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058};
59
60void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
61 unsigned int type)
62{
63 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
64
65 if (rate) {
66 atomic_set(&ffi->inject_ops, 0);
67 ffi->inject_rate = rate;
68 }
69
70 if (type)
71 ffi->inject_type = type;
72
73 if (!rate && !type)
74 memset(ffi, 0, sizeof(struct f2fs_fault_info));
75}
76#endif
77
78/* f2fs-wide shrinker description */
79static struct shrinker f2fs_shrinker_info = {
80 .scan_objects = f2fs_shrink_scan,
81 .count_objects = f2fs_shrink_count,
82 .seeks = DEFAULT_SEEKS,
83};
84
85enum {
86 Opt_gc_background,
87 Opt_disable_roll_forward,
88 Opt_norecovery,
89 Opt_discard,
90 Opt_nodiscard,
91 Opt_noheap,
92 Opt_heap,
93 Opt_user_xattr,
94 Opt_nouser_xattr,
95 Opt_acl,
96 Opt_noacl,
97 Opt_active_logs,
98 Opt_disable_ext_identify,
99 Opt_inline_xattr,
100 Opt_noinline_xattr,
101 Opt_inline_xattr_size,
102 Opt_inline_data,
103 Opt_inline_dentry,
104 Opt_noinline_dentry,
105 Opt_flush_merge,
106 Opt_noflush_merge,
107 Opt_nobarrier,
108 Opt_fastboot,
109 Opt_extent_cache,
110 Opt_noextent_cache,
111 Opt_noinline_data,
112 Opt_data_flush,
113 Opt_reserve_root,
114 Opt_resgid,
115 Opt_resuid,
116 Opt_mode,
117 Opt_io_size_bits,
118 Opt_fault_injection,
119 Opt_fault_type,
120 Opt_lazytime,
121 Opt_nolazytime,
122 Opt_quota,
123 Opt_noquota,
124 Opt_usrquota,
125 Opt_grpquota,
126 Opt_prjquota,
127 Opt_usrjquota,
128 Opt_grpjquota,
129 Opt_prjjquota,
130 Opt_offusrjquota,
131 Opt_offgrpjquota,
132 Opt_offprjjquota,
133 Opt_jqfmt_vfsold,
134 Opt_jqfmt_vfsv0,
135 Opt_jqfmt_vfsv1,
136 Opt_whint,
137 Opt_alloc,
138 Opt_fsync,
139 Opt_test_dummy_encryption,
David Brazdil0f672f62019-12-10 10:32:29 +0000140 Opt_checkpoint_disable,
141 Opt_checkpoint_disable_cap,
142 Opt_checkpoint_disable_cap_perc,
143 Opt_checkpoint_enable,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144 Opt_err,
145};
146
147static match_table_t f2fs_tokens = {
148 {Opt_gc_background, "background_gc=%s"},
149 {Opt_disable_roll_forward, "disable_roll_forward"},
150 {Opt_norecovery, "norecovery"},
151 {Opt_discard, "discard"},
152 {Opt_nodiscard, "nodiscard"},
153 {Opt_noheap, "no_heap"},
154 {Opt_heap, "heap"},
155 {Opt_user_xattr, "user_xattr"},
156 {Opt_nouser_xattr, "nouser_xattr"},
157 {Opt_acl, "acl"},
158 {Opt_noacl, "noacl"},
159 {Opt_active_logs, "active_logs=%u"},
160 {Opt_disable_ext_identify, "disable_ext_identify"},
161 {Opt_inline_xattr, "inline_xattr"},
162 {Opt_noinline_xattr, "noinline_xattr"},
163 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
164 {Opt_inline_data, "inline_data"},
165 {Opt_inline_dentry, "inline_dentry"},
166 {Opt_noinline_dentry, "noinline_dentry"},
167 {Opt_flush_merge, "flush_merge"},
168 {Opt_noflush_merge, "noflush_merge"},
169 {Opt_nobarrier, "nobarrier"},
170 {Opt_fastboot, "fastboot"},
171 {Opt_extent_cache, "extent_cache"},
172 {Opt_noextent_cache, "noextent_cache"},
173 {Opt_noinline_data, "noinline_data"},
174 {Opt_data_flush, "data_flush"},
175 {Opt_reserve_root, "reserve_root=%u"},
176 {Opt_resgid, "resgid=%u"},
177 {Opt_resuid, "resuid=%u"},
178 {Opt_mode, "mode=%s"},
179 {Opt_io_size_bits, "io_bits=%u"},
180 {Opt_fault_injection, "fault_injection=%u"},
181 {Opt_fault_type, "fault_type=%u"},
182 {Opt_lazytime, "lazytime"},
183 {Opt_nolazytime, "nolazytime"},
184 {Opt_quota, "quota"},
185 {Opt_noquota, "noquota"},
186 {Opt_usrquota, "usrquota"},
187 {Opt_grpquota, "grpquota"},
188 {Opt_prjquota, "prjquota"},
189 {Opt_usrjquota, "usrjquota=%s"},
190 {Opt_grpjquota, "grpjquota=%s"},
191 {Opt_prjjquota, "prjjquota=%s"},
192 {Opt_offusrjquota, "usrjquota="},
193 {Opt_offgrpjquota, "grpjquota="},
194 {Opt_offprjjquota, "prjjquota="},
195 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
196 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
197 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
198 {Opt_whint, "whint_mode=%s"},
199 {Opt_alloc, "alloc_mode=%s"},
200 {Opt_fsync, "fsync_mode=%s"},
201 {Opt_test_dummy_encryption, "test_dummy_encryption"},
David Brazdil0f672f62019-12-10 10:32:29 +0000202 {Opt_checkpoint_disable, "checkpoint=disable"},
203 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
204 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
205 {Opt_checkpoint_enable, "checkpoint=enable"},
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 {Opt_err, NULL},
207};
208
David Brazdil0f672f62019-12-10 10:32:29 +0000209void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210{
211 struct va_format vaf;
212 va_list args;
David Brazdil0f672f62019-12-10 10:32:29 +0000213 int level;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214
215 va_start(args, fmt);
David Brazdil0f672f62019-12-10 10:32:29 +0000216
217 level = printk_get_level(fmt);
218 vaf.fmt = printk_skip_level(fmt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219 vaf.va = &args;
David Brazdil0f672f62019-12-10 10:32:29 +0000220 printk("%c%cF2FS-fs (%s): %pV\n",
221 KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
222
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 va_end(args);
224}
225
David Brazdil0f672f62019-12-10 10:32:29 +0000226#ifdef CONFIG_UNICODE
227static const struct f2fs_sb_encodings {
228 __u16 magic;
229 char *name;
230 char *version;
231} f2fs_sb_encoding_map[] = {
232 {F2FS_ENC_UTF8_12_1, "utf8", "12.1.0"},
233};
234
235static int f2fs_sb_read_encoding(const struct f2fs_super_block *sb,
236 const struct f2fs_sb_encodings **encoding,
237 __u16 *flags)
238{
239 __u16 magic = le16_to_cpu(sb->s_encoding);
240 int i;
241
242 for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
243 if (magic == f2fs_sb_encoding_map[i].magic)
244 break;
245
246 if (i >= ARRAY_SIZE(f2fs_sb_encoding_map))
247 return -EINVAL;
248
249 *encoding = &f2fs_sb_encoding_map[i];
250 *flags = le16_to_cpu(sb->s_encoding_flags);
251
252 return 0;
253}
254#endif
255
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
257{
David Brazdil0f672f62019-12-10 10:32:29 +0000258 block_t limit = min((sbi->user_block_count << 1) / 1000,
259 sbi->user_block_count - sbi->reserved_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260
261 /* limit is 0.2% */
262 if (test_opt(sbi, RESERVE_ROOT) &&
263 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
264 F2FS_OPTION(sbi).root_reserved_blocks = limit;
David Brazdil0f672f62019-12-10 10:32:29 +0000265 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
266 F2FS_OPTION(sbi).root_reserved_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 }
268 if (!test_opt(sbi, RESERVE_ROOT) &&
269 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
270 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
271 !gid_eq(F2FS_OPTION(sbi).s_resgid,
272 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
David Brazdil0f672f62019-12-10 10:32:29 +0000273 f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
274 from_kuid_munged(&init_user_ns,
275 F2FS_OPTION(sbi).s_resuid),
276 from_kgid_munged(&init_user_ns,
277 F2FS_OPTION(sbi).s_resgid));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278}
279
Olivier Deprez0e641232021-09-23 10:07:05 +0200280static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
281{
282 if (!F2FS_OPTION(sbi).unusable_cap_perc)
283 return;
284
285 if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
286 F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
287 else
288 F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
289 F2FS_OPTION(sbi).unusable_cap_perc;
290
291 f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
292 F2FS_OPTION(sbi).unusable_cap,
293 F2FS_OPTION(sbi).unusable_cap_perc);
294}
295
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296static void init_once(void *foo)
297{
298 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
299
300 inode_init_once(&fi->vfs_inode);
301}
302
303#ifdef CONFIG_QUOTA
304static const char * const quotatypes[] = INITQFNAMES;
305#define QTYPE2NAME(t) (quotatypes[t])
306static int f2fs_set_qf_name(struct super_block *sb, int qtype,
307 substring_t *args)
308{
309 struct f2fs_sb_info *sbi = F2FS_SB(sb);
310 char *qname;
311 int ret = -EINVAL;
312
313 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
David Brazdil0f672f62019-12-10 10:32:29 +0000314 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 return -EINVAL;
316 }
David Brazdil0f672f62019-12-10 10:32:29 +0000317 if (f2fs_sb_has_quota_ino(sbi)) {
318 f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319 return 0;
320 }
321
322 qname = match_strdup(args);
323 if (!qname) {
David Brazdil0f672f62019-12-10 10:32:29 +0000324 f2fs_err(sbi, "Not enough memory for storing quotafile name");
325 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 }
327 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
328 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
329 ret = 0;
330 else
David Brazdil0f672f62019-12-10 10:32:29 +0000331 f2fs_err(sbi, "%s quota file already specified",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332 QTYPE2NAME(qtype));
333 goto errout;
334 }
335 if (strchr(qname, '/')) {
David Brazdil0f672f62019-12-10 10:32:29 +0000336 f2fs_err(sbi, "quotafile must be on filesystem root");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000337 goto errout;
338 }
339 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
340 set_opt(sbi, QUOTA);
341 return 0;
342errout:
David Brazdil0f672f62019-12-10 10:32:29 +0000343 kvfree(qname);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344 return ret;
345}
346
347static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
348{
349 struct f2fs_sb_info *sbi = F2FS_SB(sb);
350
351 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
David Brazdil0f672f62019-12-10 10:32:29 +0000352 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 return -EINVAL;
354 }
David Brazdil0f672f62019-12-10 10:32:29 +0000355 kvfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
357 return 0;
358}
359
360static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
361{
362 /*
363 * We do the test below only for project quotas. 'usrquota' and
364 * 'grpquota' mount options are allowed even without quota feature
365 * to support legacy quotas in quota files.
366 */
David Brazdil0f672f62019-12-10 10:32:29 +0000367 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi)) {
368 f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369 return -1;
370 }
371 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
372 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
373 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
374 if (test_opt(sbi, USRQUOTA) &&
375 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
376 clear_opt(sbi, USRQUOTA);
377
378 if (test_opt(sbi, GRPQUOTA) &&
379 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
380 clear_opt(sbi, GRPQUOTA);
381
382 if (test_opt(sbi, PRJQUOTA) &&
383 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
384 clear_opt(sbi, PRJQUOTA);
385
386 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
387 test_opt(sbi, PRJQUOTA)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000388 f2fs_err(sbi, "old and new quota format mixing");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 return -1;
390 }
391
392 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
David Brazdil0f672f62019-12-10 10:32:29 +0000393 f2fs_err(sbi, "journaled quota format not specified");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 return -1;
395 }
396 }
397
David Brazdil0f672f62019-12-10 10:32:29 +0000398 if (f2fs_sb_has_quota_ino(sbi) && F2FS_OPTION(sbi).s_jquota_fmt) {
399 f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400 F2FS_OPTION(sbi).s_jquota_fmt = 0;
401 }
402 return 0;
403}
404#endif
405
406static int parse_options(struct super_block *sb, char *options)
407{
408 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409 substring_t args[MAX_OPT_ARGS];
410 char *p, *name;
411 int arg = 0;
412 kuid_t uid;
413 kgid_t gid;
414#ifdef CONFIG_QUOTA
415 int ret;
416#endif
417
418 if (!options)
419 return 0;
420
421 while ((p = strsep(&options, ",")) != NULL) {
422 int token;
423 if (!*p)
424 continue;
425 /*
426 * Initialize args struct so we know whether arg was
427 * found; some options take optional arguments.
428 */
429 args[0].to = args[0].from = NULL;
430 token = match_token(p, f2fs_tokens, args);
431
432 switch (token) {
433 case Opt_gc_background:
434 name = match_strdup(&args[0]);
435
436 if (!name)
437 return -ENOMEM;
438 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
439 set_opt(sbi, BG_GC);
440 clear_opt(sbi, FORCE_FG_GC);
441 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
442 clear_opt(sbi, BG_GC);
443 clear_opt(sbi, FORCE_FG_GC);
444 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
445 set_opt(sbi, BG_GC);
446 set_opt(sbi, FORCE_FG_GC);
447 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000448 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 return -EINVAL;
450 }
David Brazdil0f672f62019-12-10 10:32:29 +0000451 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 break;
453 case Opt_disable_roll_forward:
454 set_opt(sbi, DISABLE_ROLL_FORWARD);
455 break;
456 case Opt_norecovery:
457 /* this option mounts f2fs with ro */
Olivier Deprez0e641232021-09-23 10:07:05 +0200458 set_opt(sbi, NORECOVERY);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 if (!f2fs_readonly(sb))
460 return -EINVAL;
461 break;
462 case Opt_discard:
David Brazdil0f672f62019-12-10 10:32:29 +0000463 set_opt(sbi, DISCARD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464 break;
465 case Opt_nodiscard:
David Brazdil0f672f62019-12-10 10:32:29 +0000466 if (f2fs_sb_has_blkzoned(sbi)) {
467 f2fs_warn(sbi, "discard is required for zoned block devices");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000468 return -EINVAL;
469 }
470 clear_opt(sbi, DISCARD);
471 break;
472 case Opt_noheap:
473 set_opt(sbi, NOHEAP);
474 break;
475 case Opt_heap:
476 clear_opt(sbi, NOHEAP);
477 break;
478#ifdef CONFIG_F2FS_FS_XATTR
479 case Opt_user_xattr:
480 set_opt(sbi, XATTR_USER);
481 break;
482 case Opt_nouser_xattr:
483 clear_opt(sbi, XATTR_USER);
484 break;
485 case Opt_inline_xattr:
486 set_opt(sbi, INLINE_XATTR);
487 break;
488 case Opt_noinline_xattr:
489 clear_opt(sbi, INLINE_XATTR);
490 break;
491 case Opt_inline_xattr_size:
492 if (args->from && match_int(args, &arg))
493 return -EINVAL;
494 set_opt(sbi, INLINE_XATTR_SIZE);
495 F2FS_OPTION(sbi).inline_xattr_size = arg;
496 break;
497#else
498 case Opt_user_xattr:
David Brazdil0f672f62019-12-10 10:32:29 +0000499 f2fs_info(sbi, "user_xattr options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 break;
501 case Opt_nouser_xattr:
David Brazdil0f672f62019-12-10 10:32:29 +0000502 f2fs_info(sbi, "nouser_xattr options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 break;
504 case Opt_inline_xattr:
David Brazdil0f672f62019-12-10 10:32:29 +0000505 f2fs_info(sbi, "inline_xattr options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506 break;
507 case Opt_noinline_xattr:
David Brazdil0f672f62019-12-10 10:32:29 +0000508 f2fs_info(sbi, "noinline_xattr options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000509 break;
510#endif
511#ifdef CONFIG_F2FS_FS_POSIX_ACL
512 case Opt_acl:
513 set_opt(sbi, POSIX_ACL);
514 break;
515 case Opt_noacl:
516 clear_opt(sbi, POSIX_ACL);
517 break;
518#else
519 case Opt_acl:
David Brazdil0f672f62019-12-10 10:32:29 +0000520 f2fs_info(sbi, "acl options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521 break;
522 case Opt_noacl:
David Brazdil0f672f62019-12-10 10:32:29 +0000523 f2fs_info(sbi, "noacl options not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524 break;
525#endif
526 case Opt_active_logs:
527 if (args->from && match_int(args, &arg))
528 return -EINVAL;
529 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
530 return -EINVAL;
531 F2FS_OPTION(sbi).active_logs = arg;
532 break;
533 case Opt_disable_ext_identify:
534 set_opt(sbi, DISABLE_EXT_IDENTIFY);
535 break;
536 case Opt_inline_data:
537 set_opt(sbi, INLINE_DATA);
538 break;
539 case Opt_inline_dentry:
540 set_opt(sbi, INLINE_DENTRY);
541 break;
542 case Opt_noinline_dentry:
543 clear_opt(sbi, INLINE_DENTRY);
544 break;
545 case Opt_flush_merge:
546 set_opt(sbi, FLUSH_MERGE);
547 break;
548 case Opt_noflush_merge:
549 clear_opt(sbi, FLUSH_MERGE);
550 break;
551 case Opt_nobarrier:
552 set_opt(sbi, NOBARRIER);
553 break;
554 case Opt_fastboot:
555 set_opt(sbi, FASTBOOT);
556 break;
557 case Opt_extent_cache:
558 set_opt(sbi, EXTENT_CACHE);
559 break;
560 case Opt_noextent_cache:
561 clear_opt(sbi, EXTENT_CACHE);
562 break;
563 case Opt_noinline_data:
564 clear_opt(sbi, INLINE_DATA);
565 break;
566 case Opt_data_flush:
567 set_opt(sbi, DATA_FLUSH);
568 break;
569 case Opt_reserve_root:
570 if (args->from && match_int(args, &arg))
571 return -EINVAL;
572 if (test_opt(sbi, RESERVE_ROOT)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000573 f2fs_info(sbi, "Preserve previous reserve_root=%u",
574 F2FS_OPTION(sbi).root_reserved_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000575 } else {
576 F2FS_OPTION(sbi).root_reserved_blocks = arg;
577 set_opt(sbi, RESERVE_ROOT);
578 }
579 break;
580 case Opt_resuid:
581 if (args->from && match_int(args, &arg))
582 return -EINVAL;
583 uid = make_kuid(current_user_ns(), arg);
584 if (!uid_valid(uid)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000585 f2fs_err(sbi, "Invalid uid value %d", arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586 return -EINVAL;
587 }
588 F2FS_OPTION(sbi).s_resuid = uid;
589 break;
590 case Opt_resgid:
591 if (args->from && match_int(args, &arg))
592 return -EINVAL;
593 gid = make_kgid(current_user_ns(), arg);
594 if (!gid_valid(gid)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000595 f2fs_err(sbi, "Invalid gid value %d", arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000596 return -EINVAL;
597 }
598 F2FS_OPTION(sbi).s_resgid = gid;
599 break;
600 case Opt_mode:
601 name = match_strdup(&args[0]);
602
603 if (!name)
604 return -ENOMEM;
605 if (strlen(name) == 8 &&
606 !strncmp(name, "adaptive", 8)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000607 if (f2fs_sb_has_blkzoned(sbi)) {
608 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
609 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610 return -EINVAL;
611 }
612 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
613 } else if (strlen(name) == 3 &&
614 !strncmp(name, "lfs", 3)) {
615 set_opt_mode(sbi, F2FS_MOUNT_LFS);
616 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000617 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 return -EINVAL;
619 }
David Brazdil0f672f62019-12-10 10:32:29 +0000620 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000621 break;
622 case Opt_io_size_bits:
623 if (args->from && match_int(args, &arg))
624 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +0000625 if (arg <= 0 || arg > __ilog2_u32(BIO_MAX_PAGES)) {
626 f2fs_warn(sbi, "Not support %d, larger than %d",
627 1 << arg, BIO_MAX_PAGES);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628 return -EINVAL;
629 }
630 F2FS_OPTION(sbi).write_io_size_bits = arg;
631 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000632#ifdef CONFIG_F2FS_FAULT_INJECTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 case Opt_fault_injection:
634 if (args->from && match_int(args, &arg))
635 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000636 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
637 set_opt(sbi, FAULT_INJECTION);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000639
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640 case Opt_fault_type:
641 if (args->from && match_int(args, &arg))
642 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643 f2fs_build_fault_attr(sbi, 0, arg);
644 set_opt(sbi, FAULT_INJECTION);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000646#else
647 case Opt_fault_injection:
648 f2fs_info(sbi, "fault_injection options not supported");
649 break;
650
651 case Opt_fault_type:
652 f2fs_info(sbi, "fault_type options not supported");
653 break;
654#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000655 case Opt_lazytime:
656 sb->s_flags |= SB_LAZYTIME;
657 break;
658 case Opt_nolazytime:
659 sb->s_flags &= ~SB_LAZYTIME;
660 break;
661#ifdef CONFIG_QUOTA
662 case Opt_quota:
663 case Opt_usrquota:
664 set_opt(sbi, USRQUOTA);
665 break;
666 case Opt_grpquota:
667 set_opt(sbi, GRPQUOTA);
668 break;
669 case Opt_prjquota:
670 set_opt(sbi, PRJQUOTA);
671 break;
672 case Opt_usrjquota:
673 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
674 if (ret)
675 return ret;
676 break;
677 case Opt_grpjquota:
678 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
679 if (ret)
680 return ret;
681 break;
682 case Opt_prjjquota:
683 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
684 if (ret)
685 return ret;
686 break;
687 case Opt_offusrjquota:
688 ret = f2fs_clear_qf_name(sb, USRQUOTA);
689 if (ret)
690 return ret;
691 break;
692 case Opt_offgrpjquota:
693 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
694 if (ret)
695 return ret;
696 break;
697 case Opt_offprjjquota:
698 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
699 if (ret)
700 return ret;
701 break;
702 case Opt_jqfmt_vfsold:
703 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
704 break;
705 case Opt_jqfmt_vfsv0:
706 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
707 break;
708 case Opt_jqfmt_vfsv1:
709 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
710 break;
711 case Opt_noquota:
712 clear_opt(sbi, QUOTA);
713 clear_opt(sbi, USRQUOTA);
714 clear_opt(sbi, GRPQUOTA);
715 clear_opt(sbi, PRJQUOTA);
716 break;
717#else
718 case Opt_quota:
719 case Opt_usrquota:
720 case Opt_grpquota:
721 case Opt_prjquota:
722 case Opt_usrjquota:
723 case Opt_grpjquota:
724 case Opt_prjjquota:
725 case Opt_offusrjquota:
726 case Opt_offgrpjquota:
727 case Opt_offprjjquota:
728 case Opt_jqfmt_vfsold:
729 case Opt_jqfmt_vfsv0:
730 case Opt_jqfmt_vfsv1:
731 case Opt_noquota:
David Brazdil0f672f62019-12-10 10:32:29 +0000732 f2fs_info(sbi, "quota operations not supported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000733 break;
734#endif
735 case Opt_whint:
736 name = match_strdup(&args[0]);
737 if (!name)
738 return -ENOMEM;
739 if (strlen(name) == 10 &&
740 !strncmp(name, "user-based", 10)) {
741 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
742 } else if (strlen(name) == 3 &&
743 !strncmp(name, "off", 3)) {
744 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
745 } else if (strlen(name) == 8 &&
746 !strncmp(name, "fs-based", 8)) {
747 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
748 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000749 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 return -EINVAL;
751 }
David Brazdil0f672f62019-12-10 10:32:29 +0000752 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753 break;
754 case Opt_alloc:
755 name = match_strdup(&args[0]);
756 if (!name)
757 return -ENOMEM;
758
759 if (strlen(name) == 7 &&
760 !strncmp(name, "default", 7)) {
761 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
762 } else if (strlen(name) == 5 &&
763 !strncmp(name, "reuse", 5)) {
764 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
765 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000766 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767 return -EINVAL;
768 }
David Brazdil0f672f62019-12-10 10:32:29 +0000769 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770 break;
771 case Opt_fsync:
772 name = match_strdup(&args[0]);
773 if (!name)
774 return -ENOMEM;
775 if (strlen(name) == 5 &&
776 !strncmp(name, "posix", 5)) {
777 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
778 } else if (strlen(name) == 6 &&
779 !strncmp(name, "strict", 6)) {
780 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
781 } else if (strlen(name) == 9 &&
782 !strncmp(name, "nobarrier", 9)) {
783 F2FS_OPTION(sbi).fsync_mode =
784 FSYNC_MODE_NOBARRIER;
785 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000786 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787 return -EINVAL;
788 }
David Brazdil0f672f62019-12-10 10:32:29 +0000789 kvfree(name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000790 break;
791 case Opt_test_dummy_encryption:
David Brazdil0f672f62019-12-10 10:32:29 +0000792#ifdef CONFIG_FS_ENCRYPTION
793 if (!f2fs_sb_has_encrypt(sbi)) {
794 f2fs_err(sbi, "Encrypt feature is off");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795 return -EINVAL;
796 }
797
798 F2FS_OPTION(sbi).test_dummy_encryption = true;
David Brazdil0f672f62019-12-10 10:32:29 +0000799 f2fs_info(sbi, "Test dummy encryption mode enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800#else
David Brazdil0f672f62019-12-10 10:32:29 +0000801 f2fs_info(sbi, "Test dummy encryption mount option ignored");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802#endif
803 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000804 case Opt_checkpoint_disable_cap_perc:
805 if (args->from && match_int(args, &arg))
806 return -EINVAL;
807 if (arg < 0 || arg > 100)
808 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200809 F2FS_OPTION(sbi).unusable_cap_perc = arg;
David Brazdil0f672f62019-12-10 10:32:29 +0000810 set_opt(sbi, DISABLE_CHECKPOINT);
811 break;
812 case Opt_checkpoint_disable_cap:
813 if (args->from && match_int(args, &arg))
814 return -EINVAL;
815 F2FS_OPTION(sbi).unusable_cap = arg;
816 set_opt(sbi, DISABLE_CHECKPOINT);
817 break;
818 case Opt_checkpoint_disable:
819 set_opt(sbi, DISABLE_CHECKPOINT);
820 break;
821 case Opt_checkpoint_enable:
822 clear_opt(sbi, DISABLE_CHECKPOINT);
823 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000824 default:
David Brazdil0f672f62019-12-10 10:32:29 +0000825 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
826 p);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827 return -EINVAL;
828 }
829 }
830#ifdef CONFIG_QUOTA
831 if (f2fs_check_quota_options(sbi))
832 return -EINVAL;
833#else
David Brazdil0f672f62019-12-10 10:32:29 +0000834 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sbi->sb)) {
835 f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000836 return -EINVAL;
837 }
David Brazdil0f672f62019-12-10 10:32:29 +0000838 if (f2fs_sb_has_project_quota(sbi) && !f2fs_readonly(sbi->sb)) {
839 f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
840 return -EINVAL;
841 }
842#endif
843#ifndef CONFIG_UNICODE
844 if (f2fs_sb_has_casefold(sbi)) {
845 f2fs_err(sbi,
846 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847 return -EINVAL;
848 }
849#endif
850
851 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000852 f2fs_err(sbi, "Should set mode=lfs with %uKB-sized IO",
853 F2FS_IO_SIZE_KB(sbi));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000854 return -EINVAL;
855 }
856
857 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000858 int min_size, max_size;
859
860 if (!f2fs_sb_has_extra_attr(sbi) ||
861 !f2fs_sb_has_flexible_inline_xattr(sbi)) {
862 f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000863 return -EINVAL;
864 }
865 if (!test_opt(sbi, INLINE_XATTR)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000866 f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000867 return -EINVAL;
868 }
David Brazdil0f672f62019-12-10 10:32:29 +0000869
870 min_size = sizeof(struct f2fs_xattr_header) / sizeof(__le32);
871 max_size = MAX_INLINE_XATTR_SIZE;
872
873 if (F2FS_OPTION(sbi).inline_xattr_size < min_size ||
874 F2FS_OPTION(sbi).inline_xattr_size > max_size) {
875 f2fs_err(sbi, "inline xattr size is out of range: %d ~ %d",
876 min_size, max_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000877 return -EINVAL;
878 }
879 }
880
David Brazdil0f672f62019-12-10 10:32:29 +0000881 if (test_opt(sbi, DISABLE_CHECKPOINT) && test_opt(sbi, LFS)) {
882 f2fs_err(sbi, "LFS not compatible with checkpoint=disable\n");
883 return -EINVAL;
884 }
885
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000886 /* Not pass down write hints if the number of active logs is lesser
887 * than NR_CURSEG_TYPE.
888 */
889 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
890 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
891 return 0;
892}
893
894static struct inode *f2fs_alloc_inode(struct super_block *sb)
895{
896 struct f2fs_inode_info *fi;
897
898 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
899 if (!fi)
900 return NULL;
901
902 init_once((void *) fi);
903
904 /* Initialize f2fs-specific inode info */
905 atomic_set(&fi->dirty_pages, 0);
906 init_rwsem(&fi->i_sem);
907 INIT_LIST_HEAD(&fi->dirty_list);
908 INIT_LIST_HEAD(&fi->gdirty_list);
909 INIT_LIST_HEAD(&fi->inmem_ilist);
910 INIT_LIST_HEAD(&fi->inmem_pages);
911 mutex_init(&fi->inmem_lock);
912 init_rwsem(&fi->i_gc_rwsem[READ]);
913 init_rwsem(&fi->i_gc_rwsem[WRITE]);
914 init_rwsem(&fi->i_mmap_sem);
915 init_rwsem(&fi->i_xattr_sem);
916
917 /* Will be used by directory only */
918 fi->i_dir_level = F2FS_SB(sb)->dir_level;
919
920 return &fi->vfs_inode;
921}
922
923static int f2fs_drop_inode(struct inode *inode)
924{
David Brazdil0f672f62019-12-10 10:32:29 +0000925 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000927
928 /*
929 * during filesystem shutdown, if checkpoint is disabled,
930 * drop useless meta/node dirty pages.
931 */
932 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
933 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
934 inode->i_ino == F2FS_META_INO(sbi)) {
935 trace_f2fs_drop_inode(inode, 1);
936 return 1;
937 }
938 }
939
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000940 /*
941 * This is to avoid a deadlock condition like below.
942 * writeback_single_inode(inode)
943 * - f2fs_write_data_page
944 * - f2fs_gc -> iput -> evict
945 * - inode_wait_for_writeback(inode)
946 */
947 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
948 if (!inode->i_nlink && !is_bad_inode(inode)) {
949 /* to avoid evict_inode call simultaneously */
950 atomic_inc(&inode->i_count);
951 spin_unlock(&inode->i_lock);
952
953 /* some remained atomic pages should discarded */
954 if (f2fs_is_atomic_file(inode))
955 f2fs_drop_inmem_pages(inode);
956
957 /* should remain fi->extent_tree for writepage */
958 f2fs_destroy_extent_node(inode);
959
960 sb_start_intwrite(inode->i_sb);
961 f2fs_i_size_write(inode, 0);
962
David Brazdil0f672f62019-12-10 10:32:29 +0000963 f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
964 inode, NULL, 0, DATA);
965 truncate_inode_pages_final(inode->i_mapping);
966
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967 if (F2FS_HAS_BLOCKS(inode))
968 f2fs_truncate(inode);
969
970 sb_end_intwrite(inode->i_sb);
971
972 spin_lock(&inode->i_lock);
973 atomic_dec(&inode->i_count);
974 }
975 trace_f2fs_drop_inode(inode, 0);
976 return 0;
977 }
978 ret = generic_drop_inode(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000979 if (!ret)
980 ret = fscrypt_drop_inode(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000981 trace_f2fs_drop_inode(inode, ret);
982 return ret;
983}
984
985int f2fs_inode_dirtied(struct inode *inode, bool sync)
986{
987 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
988 int ret = 0;
989
990 spin_lock(&sbi->inode_lock[DIRTY_META]);
991 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
992 ret = 1;
993 } else {
994 set_inode_flag(inode, FI_DIRTY_INODE);
995 stat_inc_dirty_inode(sbi, DIRTY_META);
996 }
997 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
998 list_add_tail(&F2FS_I(inode)->gdirty_list,
999 &sbi->inode_list[DIRTY_META]);
1000 inc_page_count(sbi, F2FS_DIRTY_IMETA);
1001 }
1002 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1003 return ret;
1004}
1005
1006void f2fs_inode_synced(struct inode *inode)
1007{
1008 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1009
1010 spin_lock(&sbi->inode_lock[DIRTY_META]);
1011 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
1012 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1013 return;
1014 }
1015 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
1016 list_del_init(&F2FS_I(inode)->gdirty_list);
1017 dec_page_count(sbi, F2FS_DIRTY_IMETA);
1018 }
1019 clear_inode_flag(inode, FI_DIRTY_INODE);
1020 clear_inode_flag(inode, FI_AUTO_RECOVER);
1021 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
1022 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1023}
1024
1025/*
1026 * f2fs_dirty_inode() is called from __mark_inode_dirty()
1027 *
1028 * We should call set_dirty_inode to write the dirty inode through write_inode.
1029 */
1030static void f2fs_dirty_inode(struct inode *inode, int flags)
1031{
1032 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1033
1034 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
1035 inode->i_ino == F2FS_META_INO(sbi))
1036 return;
1037
1038 if (flags == I_DIRTY_TIME)
1039 return;
1040
1041 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
1042 clear_inode_flag(inode, FI_AUTO_RECOVER);
1043
1044 f2fs_inode_dirtied(inode, false);
1045}
1046
David Brazdil0f672f62019-12-10 10:32:29 +00001047static void f2fs_free_inode(struct inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001048{
David Brazdil0f672f62019-12-10 10:32:29 +00001049 fscrypt_free_inode(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1051}
1052
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001053static void destroy_percpu_info(struct f2fs_sb_info *sbi)
1054{
1055 percpu_counter_destroy(&sbi->alloc_valid_block_count);
1056 percpu_counter_destroy(&sbi->total_valid_inode_count);
1057}
1058
1059static void destroy_device_list(struct f2fs_sb_info *sbi)
1060{
1061 int i;
1062
1063 for (i = 0; i < sbi->s_ndevs; i++) {
1064 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
1065#ifdef CONFIG_BLK_DEV_ZONED
David Brazdil0f672f62019-12-10 10:32:29 +00001066 kvfree(FDEV(i).blkz_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067#endif
1068 }
David Brazdil0f672f62019-12-10 10:32:29 +00001069 kvfree(sbi->devs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001070}
1071
1072static void f2fs_put_super(struct super_block *sb)
1073{
1074 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1075 int i;
1076 bool dropped;
1077
Olivier Deprez0e641232021-09-23 10:07:05 +02001078 /* unregister procfs/sysfs entries in advance to avoid race case */
1079 f2fs_unregister_sysfs(sbi);
1080
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001081 f2fs_quota_off_umount(sb);
1082
1083 /* prevent remaining shrinker jobs */
1084 mutex_lock(&sbi->umount_mutex);
1085
1086 /*
1087 * We don't need to do checkpoint when superblock is clean.
1088 * But, the previous checkpoint was not done by umount, it needs to do
1089 * clean checkpoint again.
1090 */
David Brazdil0f672f62019-12-10 10:32:29 +00001091 if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1092 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093 struct cp_control cpc = {
1094 .reason = CP_UMOUNT,
1095 };
1096 f2fs_write_checkpoint(sbi, &cpc);
1097 }
1098
1099 /* be sure to wait for any on-going discard commands */
David Brazdil0f672f62019-12-10 10:32:29 +00001100 dropped = f2fs_issue_discard_timeout(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101
David Brazdil0f672f62019-12-10 10:32:29 +00001102 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1103 !sbi->discard_blks && !dropped) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001104 struct cp_control cpc = {
1105 .reason = CP_UMOUNT | CP_TRIMMED,
1106 };
1107 f2fs_write_checkpoint(sbi, &cpc);
1108 }
1109
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001110 /*
1111 * normally superblock is clean, so we need to release this.
1112 * In addition, EIO will skip do checkpoint, we need this as well.
1113 */
1114 f2fs_release_ino_entry(sbi, true);
1115
1116 f2fs_leave_shrinker(sbi);
1117 mutex_unlock(&sbi->umount_mutex);
1118
1119 /* our cp_error case, we can wait for any writeback page */
1120 f2fs_flush_merged_writes(sbi);
1121
Olivier Deprez0e641232021-09-23 10:07:05 +02001122 f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123
1124 f2fs_bug_on(sbi, sbi->fsync_node_num);
1125
1126 iput(sbi->node_inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001127 sbi->node_inode = NULL;
1128
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129 iput(sbi->meta_inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001130 sbi->meta_inode = NULL;
1131
1132 /*
1133 * iput() can update stat information, if f2fs_write_checkpoint()
1134 * above failed with error.
1135 */
1136 f2fs_destroy_stats(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001137
1138 /* destroy f2fs internal modules */
1139 f2fs_destroy_node_manager(sbi);
1140 f2fs_destroy_segment_manager(sbi);
1141
David Brazdil0f672f62019-12-10 10:32:29 +00001142 kvfree(sbi->ckpt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001144 sb->s_fs_info = NULL;
1145 if (sbi->s_chksum_driver)
1146 crypto_free_shash(sbi->s_chksum_driver);
David Brazdil0f672f62019-12-10 10:32:29 +00001147 kvfree(sbi->raw_super);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148
1149 destroy_device_list(sbi);
1150 mempool_destroy(sbi->write_io_dummy);
1151#ifdef CONFIG_QUOTA
1152 for (i = 0; i < MAXQUOTAS; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00001153 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001154#endif
1155 destroy_percpu_info(sbi);
1156 for (i = 0; i < NR_PAGE_TYPE; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00001157 kvfree(sbi->write_io[i]);
1158#ifdef CONFIG_UNICODE
1159 utf8_unload(sbi->s_encoding);
1160#endif
1161 kvfree(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001162}
1163
1164int f2fs_sync_fs(struct super_block *sb, int sync)
1165{
1166 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1167 int err = 0;
1168
1169 if (unlikely(f2fs_cp_error(sbi)))
1170 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001171 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
1172 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173
1174 trace_f2fs_sync_fs(sb, sync);
1175
1176 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1177 return -EAGAIN;
1178
1179 if (sync) {
1180 struct cp_control cpc;
1181
1182 cpc.reason = __get_cp_reason(sbi);
1183
1184 mutex_lock(&sbi->gc_mutex);
1185 err = f2fs_write_checkpoint(sbi, &cpc);
1186 mutex_unlock(&sbi->gc_mutex);
1187 }
1188 f2fs_trace_ios(NULL, 1);
1189
1190 return err;
1191}
1192
1193static int f2fs_freeze(struct super_block *sb)
1194{
1195 if (f2fs_readonly(sb))
1196 return 0;
1197
1198 /* IO error happened before */
1199 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1200 return -EIO;
1201
1202 /* must be clean, since sync_filesystem() was already called */
1203 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1204 return -EINVAL;
1205 return 0;
1206}
1207
1208static int f2fs_unfreeze(struct super_block *sb)
1209{
1210 return 0;
1211}
1212
1213#ifdef CONFIG_QUOTA
1214static int f2fs_statfs_project(struct super_block *sb,
1215 kprojid_t projid, struct kstatfs *buf)
1216{
1217 struct kqid qid;
1218 struct dquot *dquot;
1219 u64 limit;
1220 u64 curblock;
1221
1222 qid = make_kqid_projid(projid);
1223 dquot = dqget(sb, qid);
1224 if (IS_ERR(dquot))
1225 return PTR_ERR(dquot);
1226 spin_lock(&dquot->dq_dqb_lock);
1227
Olivier Deprez0e641232021-09-23 10:07:05 +02001228 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
1229 dquot->dq_dqb.dqb_bhardlimit);
1230 if (limit)
1231 limit >>= sb->s_blocksize_bits;
1232
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001233 if (limit && buf->f_blocks > limit) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001234 curblock = (dquot->dq_dqb.dqb_curspace +
1235 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001236 buf->f_blocks = limit;
1237 buf->f_bfree = buf->f_bavail =
1238 (buf->f_blocks > curblock) ?
1239 (buf->f_blocks - curblock) : 0;
1240 }
1241
Olivier Deprez0e641232021-09-23 10:07:05 +02001242 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
1243 dquot->dq_dqb.dqb_ihardlimit);
1244
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245 if (limit && buf->f_files > limit) {
1246 buf->f_files = limit;
1247 buf->f_ffree =
1248 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1249 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1250 }
1251
1252 spin_unlock(&dquot->dq_dqb_lock);
1253 dqput(dquot);
1254 return 0;
1255}
1256#endif
1257
1258static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1259{
1260 struct super_block *sb = dentry->d_sb;
1261 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1262 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1263 block_t total_count, user_block_count, start_count;
1264 u64 avail_node_count;
1265
1266 total_count = le64_to_cpu(sbi->raw_super->block_count);
1267 user_block_count = sbi->user_block_count;
1268 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1269 buf->f_type = F2FS_SUPER_MAGIC;
1270 buf->f_bsize = sbi->blocksize;
1271
1272 buf->f_blocks = total_count - start_count;
1273 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1274 sbi->current_reserved_blocks;
David Brazdil0f672f62019-12-10 10:32:29 +00001275
1276 spin_lock(&sbi->stat_lock);
1277 if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
1278 buf->f_bfree = 0;
1279 else
1280 buf->f_bfree -= sbi->unusable_block_count;
1281 spin_unlock(&sbi->stat_lock);
1282
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001283 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1284 buf->f_bavail = buf->f_bfree -
1285 F2FS_OPTION(sbi).root_reserved_blocks;
1286 else
1287 buf->f_bavail = 0;
1288
David Brazdil0f672f62019-12-10 10:32:29 +00001289 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290
1291 if (avail_node_count > user_block_count) {
1292 buf->f_files = user_block_count;
1293 buf->f_ffree = buf->f_bavail;
1294 } else {
1295 buf->f_files = avail_node_count;
1296 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1297 buf->f_bavail);
1298 }
1299
1300 buf->f_namelen = F2FS_NAME_LEN;
1301 buf->f_fsid.val[0] = (u32)id;
1302 buf->f_fsid.val[1] = (u32)(id >> 32);
1303
1304#ifdef CONFIG_QUOTA
1305 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1306 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1307 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1308 }
1309#endif
1310 return 0;
1311}
1312
1313static inline void f2fs_show_quota_options(struct seq_file *seq,
1314 struct super_block *sb)
1315{
1316#ifdef CONFIG_QUOTA
1317 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1318
1319 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1320 char *fmtname = "";
1321
1322 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1323 case QFMT_VFS_OLD:
1324 fmtname = "vfsold";
1325 break;
1326 case QFMT_VFS_V0:
1327 fmtname = "vfsv0";
1328 break;
1329 case QFMT_VFS_V1:
1330 fmtname = "vfsv1";
1331 break;
1332 }
1333 seq_printf(seq, ",jqfmt=%s", fmtname);
1334 }
1335
1336 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1337 seq_show_option(seq, "usrjquota",
1338 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1339
1340 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1341 seq_show_option(seq, "grpjquota",
1342 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1343
1344 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1345 seq_show_option(seq, "prjjquota",
1346 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1347#endif
1348}
1349
1350static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1351{
1352 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1353
1354 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1355 if (test_opt(sbi, FORCE_FG_GC))
1356 seq_printf(seq, ",background_gc=%s", "sync");
1357 else
1358 seq_printf(seq, ",background_gc=%s", "on");
1359 } else {
1360 seq_printf(seq, ",background_gc=%s", "off");
1361 }
1362 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1363 seq_puts(seq, ",disable_roll_forward");
Olivier Deprez0e641232021-09-23 10:07:05 +02001364 if (test_opt(sbi, NORECOVERY))
1365 seq_puts(seq, ",norecovery");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001366 if (test_opt(sbi, DISCARD))
1367 seq_puts(seq, ",discard");
David Brazdil0f672f62019-12-10 10:32:29 +00001368 else
1369 seq_puts(seq, ",nodiscard");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001370 if (test_opt(sbi, NOHEAP))
1371 seq_puts(seq, ",no_heap");
1372 else
1373 seq_puts(seq, ",heap");
1374#ifdef CONFIG_F2FS_FS_XATTR
1375 if (test_opt(sbi, XATTR_USER))
1376 seq_puts(seq, ",user_xattr");
1377 else
1378 seq_puts(seq, ",nouser_xattr");
1379 if (test_opt(sbi, INLINE_XATTR))
1380 seq_puts(seq, ",inline_xattr");
1381 else
1382 seq_puts(seq, ",noinline_xattr");
1383 if (test_opt(sbi, INLINE_XATTR_SIZE))
1384 seq_printf(seq, ",inline_xattr_size=%u",
1385 F2FS_OPTION(sbi).inline_xattr_size);
1386#endif
1387#ifdef CONFIG_F2FS_FS_POSIX_ACL
1388 if (test_opt(sbi, POSIX_ACL))
1389 seq_puts(seq, ",acl");
1390 else
1391 seq_puts(seq, ",noacl");
1392#endif
1393 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1394 seq_puts(seq, ",disable_ext_identify");
1395 if (test_opt(sbi, INLINE_DATA))
1396 seq_puts(seq, ",inline_data");
1397 else
1398 seq_puts(seq, ",noinline_data");
1399 if (test_opt(sbi, INLINE_DENTRY))
1400 seq_puts(seq, ",inline_dentry");
1401 else
1402 seq_puts(seq, ",noinline_dentry");
1403 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1404 seq_puts(seq, ",flush_merge");
1405 if (test_opt(sbi, NOBARRIER))
1406 seq_puts(seq, ",nobarrier");
1407 if (test_opt(sbi, FASTBOOT))
1408 seq_puts(seq, ",fastboot");
1409 if (test_opt(sbi, EXTENT_CACHE))
1410 seq_puts(seq, ",extent_cache");
1411 else
1412 seq_puts(seq, ",noextent_cache");
1413 if (test_opt(sbi, DATA_FLUSH))
1414 seq_puts(seq, ",data_flush");
1415
1416 seq_puts(seq, ",mode=");
1417 if (test_opt(sbi, ADAPTIVE))
1418 seq_puts(seq, "adaptive");
1419 else if (test_opt(sbi, LFS))
1420 seq_puts(seq, "lfs");
1421 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1422 if (test_opt(sbi, RESERVE_ROOT))
1423 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1424 F2FS_OPTION(sbi).root_reserved_blocks,
1425 from_kuid_munged(&init_user_ns,
1426 F2FS_OPTION(sbi).s_resuid),
1427 from_kgid_munged(&init_user_ns,
1428 F2FS_OPTION(sbi).s_resgid));
1429 if (F2FS_IO_SIZE_BITS(sbi))
David Brazdil0f672f62019-12-10 10:32:29 +00001430 seq_printf(seq, ",io_bits=%u",
1431 F2FS_OPTION(sbi).write_io_size_bits);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001432#ifdef CONFIG_F2FS_FAULT_INJECTION
1433 if (test_opt(sbi, FAULT_INJECTION)) {
1434 seq_printf(seq, ",fault_injection=%u",
1435 F2FS_OPTION(sbi).fault_info.inject_rate);
1436 seq_printf(seq, ",fault_type=%u",
1437 F2FS_OPTION(sbi).fault_info.inject_type);
1438 }
1439#endif
1440#ifdef CONFIG_QUOTA
1441 if (test_opt(sbi, QUOTA))
1442 seq_puts(seq, ",quota");
1443 if (test_opt(sbi, USRQUOTA))
1444 seq_puts(seq, ",usrquota");
1445 if (test_opt(sbi, GRPQUOTA))
1446 seq_puts(seq, ",grpquota");
1447 if (test_opt(sbi, PRJQUOTA))
1448 seq_puts(seq, ",prjquota");
1449#endif
1450 f2fs_show_quota_options(seq, sbi->sb);
1451 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1452 seq_printf(seq, ",whint_mode=%s", "user-based");
1453 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1454 seq_printf(seq, ",whint_mode=%s", "fs-based");
David Brazdil0f672f62019-12-10 10:32:29 +00001455#ifdef CONFIG_FS_ENCRYPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456 if (F2FS_OPTION(sbi).test_dummy_encryption)
1457 seq_puts(seq, ",test_dummy_encryption");
1458#endif
1459
1460 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1461 seq_printf(seq, ",alloc_mode=%s", "default");
1462 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1463 seq_printf(seq, ",alloc_mode=%s", "reuse");
1464
David Brazdil0f672f62019-12-10 10:32:29 +00001465 if (test_opt(sbi, DISABLE_CHECKPOINT))
1466 seq_printf(seq, ",checkpoint=disable:%u",
1467 F2FS_OPTION(sbi).unusable_cap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001468 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1469 seq_printf(seq, ",fsync_mode=%s", "posix");
1470 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1471 seq_printf(seq, ",fsync_mode=%s", "strict");
1472 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1473 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1474 return 0;
1475}
1476
1477static void default_options(struct f2fs_sb_info *sbi)
1478{
1479 /* init some FS parameters */
1480 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1481 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1482 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1483 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1484 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1485 F2FS_OPTION(sbi).test_dummy_encryption = false;
1486 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1487 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1488
1489 set_opt(sbi, BG_GC);
1490 set_opt(sbi, INLINE_XATTR);
1491 set_opt(sbi, INLINE_DATA);
1492 set_opt(sbi, INLINE_DENTRY);
1493 set_opt(sbi, EXTENT_CACHE);
1494 set_opt(sbi, NOHEAP);
David Brazdil0f672f62019-12-10 10:32:29 +00001495 clear_opt(sbi, DISABLE_CHECKPOINT);
1496 F2FS_OPTION(sbi).unusable_cap = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001497 sbi->sb->s_flags |= SB_LAZYTIME;
1498 set_opt(sbi, FLUSH_MERGE);
David Brazdil0f672f62019-12-10 10:32:29 +00001499 set_opt(sbi, DISCARD);
1500 if (f2fs_sb_has_blkzoned(sbi))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001501 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1502 else
1503 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1504
1505#ifdef CONFIG_F2FS_FS_XATTR
1506 set_opt(sbi, XATTR_USER);
1507#endif
1508#ifdef CONFIG_F2FS_FS_POSIX_ACL
1509 set_opt(sbi, POSIX_ACL);
1510#endif
1511
1512 f2fs_build_fault_attr(sbi, 0, 0);
1513}
1514
1515#ifdef CONFIG_QUOTA
1516static int f2fs_enable_quotas(struct super_block *sb);
1517#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001518
1519static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
1520{
1521 unsigned int s_flags = sbi->sb->s_flags;
1522 struct cp_control cpc;
1523 int err = 0;
1524 int ret;
1525 block_t unusable;
1526
1527 if (s_flags & SB_RDONLY) {
1528 f2fs_err(sbi, "checkpoint=disable on readonly fs");
1529 return -EINVAL;
1530 }
1531 sbi->sb->s_flags |= SB_ACTIVE;
1532
1533 f2fs_update_time(sbi, DISABLE_TIME);
1534
1535 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1536 mutex_lock(&sbi->gc_mutex);
1537 err = f2fs_gc(sbi, true, false, NULL_SEGNO);
1538 if (err == -ENODATA) {
1539 err = 0;
1540 break;
1541 }
1542 if (err && err != -EAGAIN)
1543 break;
1544 }
1545
1546 ret = sync_filesystem(sbi->sb);
1547 if (ret || err) {
1548 err = ret ? ret: err;
1549 goto restore_flag;
1550 }
1551
1552 unusable = f2fs_get_unusable_blocks(sbi);
1553 if (f2fs_disable_cp_again(sbi, unusable)) {
1554 err = -EAGAIN;
1555 goto restore_flag;
1556 }
1557
1558 mutex_lock(&sbi->gc_mutex);
1559 cpc.reason = CP_PAUSE;
1560 set_sbi_flag(sbi, SBI_CP_DISABLED);
1561 err = f2fs_write_checkpoint(sbi, &cpc);
1562 if (err)
1563 goto out_unlock;
1564
1565 spin_lock(&sbi->stat_lock);
1566 sbi->unusable_block_count = unusable;
1567 spin_unlock(&sbi->stat_lock);
1568
1569out_unlock:
1570 mutex_unlock(&sbi->gc_mutex);
1571restore_flag:
1572 sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1573 return err;
1574}
1575
1576static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
1577{
1578 mutex_lock(&sbi->gc_mutex);
1579 f2fs_dirty_to_prefree(sbi);
1580
1581 clear_sbi_flag(sbi, SBI_CP_DISABLED);
1582 set_sbi_flag(sbi, SBI_IS_DIRTY);
1583 mutex_unlock(&sbi->gc_mutex);
1584
1585 f2fs_sync_fs(sbi->sb, 1);
1586}
1587
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1589{
1590 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1591 struct f2fs_mount_info org_mount_opt;
1592 unsigned long old_sb_flags;
1593 int err;
1594 bool need_restart_gc = false;
1595 bool need_stop_gc = false;
1596 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
David Brazdil0f672f62019-12-10 10:32:29 +00001597 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
1598 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
1599 bool checkpoint_changed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001600#ifdef CONFIG_QUOTA
1601 int i, j;
1602#endif
1603
1604 /*
1605 * Save the old mount options in case we
1606 * need to restore them.
1607 */
1608 org_mount_opt = sbi->mount_opt;
1609 old_sb_flags = sb->s_flags;
1610
1611#ifdef CONFIG_QUOTA
1612 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1613 for (i = 0; i < MAXQUOTAS; i++) {
1614 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1615 org_mount_opt.s_qf_names[i] =
1616 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1617 GFP_KERNEL);
1618 if (!org_mount_opt.s_qf_names[i]) {
1619 for (j = 0; j < i; j++)
David Brazdil0f672f62019-12-10 10:32:29 +00001620 kvfree(org_mount_opt.s_qf_names[j]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001621 return -ENOMEM;
1622 }
1623 } else {
1624 org_mount_opt.s_qf_names[i] = NULL;
1625 }
1626 }
1627#endif
1628
1629 /* recover superblocks we couldn't write due to previous RO mount */
1630 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1631 err = f2fs_commit_super(sbi, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001632 f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
1633 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001634 if (!err)
1635 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1636 }
1637
1638 default_options(sbi);
1639
1640 /* parse mount options */
1641 err = parse_options(sb, data);
1642 if (err)
1643 goto restore_opts;
David Brazdil0f672f62019-12-10 10:32:29 +00001644 checkpoint_changed =
1645 disable_checkpoint != test_opt(sbi, DISABLE_CHECKPOINT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646
1647 /*
1648 * Previous and new state of filesystem is RO,
1649 * so skip checking GC and FLUSH_MERGE conditions.
1650 */
1651 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1652 goto skip;
1653
1654#ifdef CONFIG_QUOTA
1655 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1656 err = dquot_suspend(sb, -1);
1657 if (err < 0)
1658 goto restore_opts;
David Brazdil0f672f62019-12-10 10:32:29 +00001659 } else if (f2fs_readonly(sb) && !(*flags & SB_RDONLY)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001660 /* dquot_resume needs RW */
1661 sb->s_flags &= ~SB_RDONLY;
1662 if (sb_any_quota_suspended(sb)) {
1663 dquot_resume(sb, -1);
David Brazdil0f672f62019-12-10 10:32:29 +00001664 } else if (f2fs_sb_has_quota_ino(sbi)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665 err = f2fs_enable_quotas(sb);
1666 if (err)
1667 goto restore_opts;
1668 }
1669 }
1670#endif
1671 /* disallow enable/disable extent_cache dynamically */
1672 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1673 err = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001674 f2fs_warn(sbi, "switch extent_cache option is not allowed");
1675 goto restore_opts;
1676 }
1677
1678 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
1679 err = -EINVAL;
1680 f2fs_warn(sbi, "switch io_bits option is not allowed");
1681 goto restore_opts;
1682 }
1683
1684 if ((*flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
1685 err = -EINVAL;
1686 f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001687 goto restore_opts;
1688 }
1689
1690 /*
1691 * We stop the GC thread if FS is mounted as RO
1692 * or if background_gc = off is passed in mount
1693 * option. Also sync the filesystem.
1694 */
1695 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
1696 if (sbi->gc_thread) {
1697 f2fs_stop_gc_thread(sbi);
1698 need_restart_gc = true;
1699 }
1700 } else if (!sbi->gc_thread) {
1701 err = f2fs_start_gc_thread(sbi);
1702 if (err)
1703 goto restore_opts;
1704 need_stop_gc = true;
1705 }
1706
1707 if (*flags & SB_RDONLY ||
1708 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1709 writeback_inodes_sb(sb, WB_REASON_SYNC);
1710 sync_inodes_sb(sb);
1711
1712 set_sbi_flag(sbi, SBI_IS_DIRTY);
1713 set_sbi_flag(sbi, SBI_IS_CLOSE);
1714 f2fs_sync_fs(sb, 1);
1715 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1716 }
1717
David Brazdil0f672f62019-12-10 10:32:29 +00001718 if (checkpoint_changed) {
1719 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
1720 err = f2fs_disable_checkpoint(sbi);
1721 if (err)
1722 goto restore_gc;
1723 } else {
1724 f2fs_enable_checkpoint(sbi);
1725 }
1726 }
1727
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001728 /*
1729 * We stop issue flush thread if FS is mounted as RO
1730 * or if flush_merge is not passed in mount option.
1731 */
1732 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1733 clear_opt(sbi, FLUSH_MERGE);
1734 f2fs_destroy_flush_cmd_control(sbi, false);
1735 } else {
1736 err = f2fs_create_flush_cmd_control(sbi);
1737 if (err)
1738 goto restore_gc;
1739 }
1740skip:
1741#ifdef CONFIG_QUOTA
1742 /* Release old quota file names */
1743 for (i = 0; i < MAXQUOTAS; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00001744 kvfree(org_mount_opt.s_qf_names[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001745#endif
1746 /* Update the POSIXACL Flag */
1747 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1748 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
1749
1750 limit_reserve_root(sbi);
Olivier Deprez0e641232021-09-23 10:07:05 +02001751 adjust_unusable_cap_perc(sbi);
David Brazdil0f672f62019-12-10 10:32:29 +00001752 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001753 return 0;
1754restore_gc:
1755 if (need_restart_gc) {
1756 if (f2fs_start_gc_thread(sbi))
David Brazdil0f672f62019-12-10 10:32:29 +00001757 f2fs_warn(sbi, "background gc thread has stopped");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001758 } else if (need_stop_gc) {
1759 f2fs_stop_gc_thread(sbi);
1760 }
1761restore_opts:
1762#ifdef CONFIG_QUOTA
1763 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
1764 for (i = 0; i < MAXQUOTAS; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001765 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001766 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
1767 }
1768#endif
1769 sbi->mount_opt = org_mount_opt;
1770 sb->s_flags = old_sb_flags;
1771 return err;
1772}
1773
1774#ifdef CONFIG_QUOTA
1775/* Read data from quotafile */
1776static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1777 size_t len, loff_t off)
1778{
1779 struct inode *inode = sb_dqopt(sb)->files[type];
1780 struct address_space *mapping = inode->i_mapping;
1781 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1782 int offset = off & (sb->s_blocksize - 1);
1783 int tocopy;
1784 size_t toread;
1785 loff_t i_size = i_size_read(inode);
1786 struct page *page;
1787 char *kaddr;
1788
1789 if (off > i_size)
1790 return 0;
1791
1792 if (off + len > i_size)
1793 len = i_size - off;
1794 toread = len;
1795 while (toread > 0) {
1796 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1797repeat:
1798 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
1799 if (IS_ERR(page)) {
1800 if (PTR_ERR(page) == -ENOMEM) {
1801 congestion_wait(BLK_RW_ASYNC, HZ/50);
1802 goto repeat;
1803 }
David Brazdil0f672f62019-12-10 10:32:29 +00001804 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001805 return PTR_ERR(page);
1806 }
1807
1808 lock_page(page);
1809
1810 if (unlikely(page->mapping != mapping)) {
1811 f2fs_put_page(page, 1);
1812 goto repeat;
1813 }
1814 if (unlikely(!PageUptodate(page))) {
1815 f2fs_put_page(page, 1);
David Brazdil0f672f62019-12-10 10:32:29 +00001816 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817 return -EIO;
1818 }
1819
1820 kaddr = kmap_atomic(page);
1821 memcpy(data, kaddr + offset, tocopy);
1822 kunmap_atomic(kaddr);
1823 f2fs_put_page(page, 1);
1824
1825 offset = 0;
1826 toread -= tocopy;
1827 data += tocopy;
1828 blkidx++;
1829 }
1830 return len;
1831}
1832
1833/* Write to quotafile */
1834static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1835 const char *data, size_t len, loff_t off)
1836{
1837 struct inode *inode = sb_dqopt(sb)->files[type];
1838 struct address_space *mapping = inode->i_mapping;
1839 const struct address_space_operations *a_ops = mapping->a_ops;
1840 int offset = off & (sb->s_blocksize - 1);
1841 size_t towrite = len;
1842 struct page *page;
Olivier Deprez0e641232021-09-23 10:07:05 +02001843 void *fsdata = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001844 char *kaddr;
1845 int err = 0;
1846 int tocopy;
1847
1848 while (towrite > 0) {
1849 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1850 towrite);
1851retry:
1852 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
Olivier Deprez0e641232021-09-23 10:07:05 +02001853 &page, &fsdata);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854 if (unlikely(err)) {
1855 if (err == -ENOMEM) {
1856 congestion_wait(BLK_RW_ASYNC, HZ/50);
1857 goto retry;
1858 }
David Brazdil0f672f62019-12-10 10:32:29 +00001859 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860 break;
1861 }
1862
1863 kaddr = kmap_atomic(page);
1864 memcpy(kaddr + offset, data, tocopy);
1865 kunmap_atomic(kaddr);
1866 flush_dcache_page(page);
1867
1868 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
Olivier Deprez0e641232021-09-23 10:07:05 +02001869 page, fsdata);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001870 offset = 0;
1871 towrite -= tocopy;
1872 off += tocopy;
1873 data += tocopy;
1874 cond_resched();
1875 }
1876
1877 if (len == towrite)
1878 return err;
1879 inode->i_mtime = inode->i_ctime = current_time(inode);
1880 f2fs_mark_inode_dirty_sync(inode, false);
1881 return len - towrite;
1882}
1883
1884static struct dquot **f2fs_get_dquots(struct inode *inode)
1885{
1886 return F2FS_I(inode)->i_dquot;
1887}
1888
1889static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1890{
1891 return &F2FS_I(inode)->i_reserved_quota;
1892}
1893
1894static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1895{
David Brazdil0f672f62019-12-10 10:32:29 +00001896 if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
1897 f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
1898 return 0;
1899 }
1900
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001901 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1902 F2FS_OPTION(sbi).s_jquota_fmt, type);
1903}
1904
1905int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
1906{
1907 int enabled = 0;
1908 int i, err;
1909
David Brazdil0f672f62019-12-10 10:32:29 +00001910 if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001911 err = f2fs_enable_quotas(sbi->sb);
1912 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001913 f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001914 return 0;
1915 }
1916 return 1;
1917 }
1918
1919 for (i = 0; i < MAXQUOTAS; i++) {
1920 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1921 err = f2fs_quota_on_mount(sbi, i);
1922 if (!err) {
1923 enabled = 1;
1924 continue;
1925 }
David Brazdil0f672f62019-12-10 10:32:29 +00001926 f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
1927 err, i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001928 }
1929 }
1930 return enabled;
1931}
1932
1933static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1934 unsigned int flags)
1935{
1936 struct inode *qf_inode;
1937 unsigned long qf_inum;
1938 int err;
1939
David Brazdil0f672f62019-12-10 10:32:29 +00001940 BUG_ON(!f2fs_sb_has_quota_ino(F2FS_SB(sb)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001941
1942 qf_inum = f2fs_qf_ino(sb, type);
1943 if (!qf_inum)
1944 return -EPERM;
1945
1946 qf_inode = f2fs_iget(sb, qf_inum);
1947 if (IS_ERR(qf_inode)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001948 f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001949 return PTR_ERR(qf_inode);
1950 }
1951
1952 /* Don't account quota for quota files to avoid recursion */
1953 qf_inode->i_flags |= S_NOQUOTA;
1954 err = dquot_enable(qf_inode, type, format_id, flags);
1955 iput(qf_inode);
1956 return err;
1957}
1958
1959static int f2fs_enable_quotas(struct super_block *sb)
1960{
David Brazdil0f672f62019-12-10 10:32:29 +00001961 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001962 int type, err = 0;
1963 unsigned long qf_inum;
1964 bool quota_mopt[MAXQUOTAS] = {
David Brazdil0f672f62019-12-10 10:32:29 +00001965 test_opt(sbi, USRQUOTA),
1966 test_opt(sbi, GRPQUOTA),
1967 test_opt(sbi, PRJQUOTA),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001968 };
1969
David Brazdil0f672f62019-12-10 10:32:29 +00001970 if (is_set_ckpt_flags(F2FS_SB(sb), CP_QUOTA_NEED_FSCK_FLAG)) {
1971 f2fs_err(sbi, "quota file may be corrupted, skip loading it");
1972 return 0;
1973 }
1974
1975 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
1976
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001977 for (type = 0; type < MAXQUOTAS; type++) {
1978 qf_inum = f2fs_qf_ino(sb, type);
1979 if (qf_inum) {
1980 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1981 DQUOT_USAGE_ENABLED |
1982 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1983 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001984 f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
1985 type, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001986 for (type--; type >= 0; type--)
1987 dquot_quota_off(sb, type);
David Brazdil0f672f62019-12-10 10:32:29 +00001988 set_sbi_flag(F2FS_SB(sb),
1989 SBI_QUOTA_NEED_REPAIR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001990 return err;
1991 }
1992 }
1993 }
1994 return 0;
1995}
1996
Olivier Deprez0e641232021-09-23 10:07:05 +02001997static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
1998{
1999 struct quota_info *dqopt = sb_dqopt(sbi->sb);
2000 struct address_space *mapping = dqopt->files[type]->i_mapping;
2001 int ret = 0;
2002
2003 ret = dquot_writeback_dquots(sbi->sb, type);
2004 if (ret)
2005 goto out;
2006
2007 ret = filemap_fdatawrite(mapping);
2008 if (ret)
2009 goto out;
2010
2011 /* if we are using journalled quota */
2012 if (is_journalled_quota(sbi))
2013 goto out;
2014
2015 ret = filemap_fdatawait(mapping);
2016
2017 truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2018out:
2019 if (ret)
2020 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2021 return ret;
2022}
2023
David Brazdil0f672f62019-12-10 10:32:29 +00002024int f2fs_quota_sync(struct super_block *sb, int type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002025{
David Brazdil0f672f62019-12-10 10:32:29 +00002026 struct f2fs_sb_info *sbi = F2FS_SB(sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002027 struct quota_info *dqopt = sb_dqopt(sb);
2028 int cnt;
2029 int ret;
2030
David Brazdil0f672f62019-12-10 10:32:29 +00002031 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002032 * Now when everything is written we can discard the pagecache so
2033 * that userspace sees the changes.
2034 */
2035 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
David Brazdil0f672f62019-12-10 10:32:29 +00002036
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002037 if (type != -1 && cnt != type)
2038 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002039
Olivier Deprez0e641232021-09-23 10:07:05 +02002040 if (!sb_has_quota_active(sb, type))
2041 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002042
2043 inode_lock(dqopt->files[cnt]);
Olivier Deprez0e641232021-09-23 10:07:05 +02002044
2045 /*
2046 * do_quotactl
2047 * f2fs_quota_sync
2048 * down_read(quota_sem)
2049 * dquot_writeback_dquots()
2050 * f2fs_dquot_commit
2051 * block_operation
2052 * down_read(quota_sem)
2053 */
2054 f2fs_lock_op(sbi);
2055 down_read(&sbi->quota_sem);
2056
2057 ret = f2fs_quota_sync_file(sbi, cnt);
2058
2059 up_read(&sbi->quota_sem);
2060 f2fs_unlock_op(sbi);
2061
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002062 inode_unlock(dqopt->files[cnt]);
Olivier Deprez0e641232021-09-23 10:07:05 +02002063
2064 if (ret)
2065 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002066 }
David Brazdil0f672f62019-12-10 10:32:29 +00002067 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002068}
2069
2070static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
2071 const struct path *path)
2072{
2073 struct inode *inode;
2074 int err;
2075
David Brazdil0f672f62019-12-10 10:32:29 +00002076 /* if quota sysfile exists, deny enabling quota with specific file */
2077 if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
2078 f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
2079 return -EBUSY;
2080 }
2081
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002082 err = f2fs_quota_sync(sb, type);
2083 if (err)
2084 return err;
2085
2086 err = dquot_quota_on(sb, type, format_id, path);
2087 if (err)
2088 return err;
2089
2090 inode = d_inode(path->dentry);
2091
2092 inode_lock(inode);
2093 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
David Brazdil0f672f62019-12-10 10:32:29 +00002094 f2fs_set_inode_flags(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002095 inode_unlock(inode);
2096 f2fs_mark_inode_dirty_sync(inode, false);
2097
2098 return 0;
2099}
2100
David Brazdil0f672f62019-12-10 10:32:29 +00002101static int __f2fs_quota_off(struct super_block *sb, int type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002102{
2103 struct inode *inode = sb_dqopt(sb)->files[type];
2104 int err;
2105
2106 if (!inode || !igrab(inode))
2107 return dquot_quota_off(sb, type);
2108
2109 err = f2fs_quota_sync(sb, type);
2110 if (err)
2111 goto out_put;
2112
2113 err = dquot_quota_off(sb, type);
David Brazdil0f672f62019-12-10 10:32:29 +00002114 if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002115 goto out_put;
2116
2117 inode_lock(inode);
2118 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
David Brazdil0f672f62019-12-10 10:32:29 +00002119 f2fs_set_inode_flags(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002120 inode_unlock(inode);
2121 f2fs_mark_inode_dirty_sync(inode, false);
2122out_put:
2123 iput(inode);
2124 return err;
2125}
2126
David Brazdil0f672f62019-12-10 10:32:29 +00002127static int f2fs_quota_off(struct super_block *sb, int type)
2128{
2129 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2130 int err;
2131
2132 err = __f2fs_quota_off(sb, type);
2133
2134 /*
2135 * quotactl can shutdown journalled quota, result in inconsistence
2136 * between quota record and fs data by following updates, tag the
2137 * flag to let fsck be aware of it.
2138 */
2139 if (is_journalled_quota(sbi))
2140 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2141 return err;
2142}
2143
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002144void f2fs_quota_off_umount(struct super_block *sb)
2145{
2146 int type;
2147 int err;
2148
2149 for (type = 0; type < MAXQUOTAS; type++) {
David Brazdil0f672f62019-12-10 10:32:29 +00002150 err = __f2fs_quota_off(sb, type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002151 if (err) {
2152 int ret = dquot_quota_off(sb, type);
2153
David Brazdil0f672f62019-12-10 10:32:29 +00002154 f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
2155 type, err, ret);
2156 set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002157 }
2158 }
David Brazdil0f672f62019-12-10 10:32:29 +00002159 /*
2160 * In case of checkpoint=disable, we must flush quota blocks.
2161 * This can cause NULL exception for node_inode in end_io, since
2162 * put_super already dropped it.
2163 */
2164 sync_filesystem(sb);
2165}
2166
2167static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
2168{
2169 struct quota_info *dqopt = sb_dqopt(sb);
2170 int type;
2171
2172 for (type = 0; type < MAXQUOTAS; type++) {
2173 if (!dqopt->files[type])
2174 continue;
2175 f2fs_inode_synced(dqopt->files[type]);
2176 }
2177}
2178
2179static int f2fs_dquot_commit(struct dquot *dquot)
2180{
2181 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2182 int ret;
2183
2184 down_read(&sbi->quota_sem);
2185 ret = dquot_commit(dquot);
2186 if (ret < 0)
2187 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2188 up_read(&sbi->quota_sem);
2189 return ret;
2190}
2191
2192static int f2fs_dquot_acquire(struct dquot *dquot)
2193{
2194 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2195 int ret;
2196
2197 down_read(&sbi->quota_sem);
2198 ret = dquot_acquire(dquot);
2199 if (ret < 0)
2200 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2201 up_read(&sbi->quota_sem);
2202 return ret;
2203}
2204
2205static int f2fs_dquot_release(struct dquot *dquot)
2206{
2207 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
2208 int ret;
2209
2210 down_read(&sbi->quota_sem);
2211 ret = dquot_release(dquot);
2212 if (ret < 0)
2213 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2214 up_read(&sbi->quota_sem);
2215 return ret;
2216}
2217
2218static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
2219{
2220 struct super_block *sb = dquot->dq_sb;
2221 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2222 int ret;
2223
2224 down_read(&sbi->quota_sem);
2225 ret = dquot_mark_dquot_dirty(dquot);
2226
2227 /* if we are using journalled quota */
2228 if (is_journalled_quota(sbi))
2229 set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
2230
2231 up_read(&sbi->quota_sem);
2232 return ret;
2233}
2234
2235static int f2fs_dquot_commit_info(struct super_block *sb, int type)
2236{
2237 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2238 int ret;
2239
2240 down_read(&sbi->quota_sem);
2241 ret = dquot_commit_info(sb, type);
2242 if (ret < 0)
2243 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2244 up_read(&sbi->quota_sem);
2245 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002246}
2247
2248static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
2249{
2250 *projid = F2FS_I(inode)->i_projid;
2251 return 0;
2252}
2253
2254static const struct dquot_operations f2fs_quota_operations = {
2255 .get_reserved_space = f2fs_get_reserved_space,
David Brazdil0f672f62019-12-10 10:32:29 +00002256 .write_dquot = f2fs_dquot_commit,
2257 .acquire_dquot = f2fs_dquot_acquire,
2258 .release_dquot = f2fs_dquot_release,
2259 .mark_dirty = f2fs_dquot_mark_dquot_dirty,
2260 .write_info = f2fs_dquot_commit_info,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002261 .alloc_dquot = dquot_alloc,
2262 .destroy_dquot = dquot_destroy,
2263 .get_projid = f2fs_get_projid,
2264 .get_next_id = dquot_get_next_id,
2265};
2266
2267static const struct quotactl_ops f2fs_quotactl_ops = {
2268 .quota_on = f2fs_quota_on,
2269 .quota_off = f2fs_quota_off,
2270 .quota_sync = f2fs_quota_sync,
2271 .get_state = dquot_get_state,
2272 .set_info = dquot_set_dqinfo,
2273 .get_dqblk = dquot_get_dqblk,
2274 .set_dqblk = dquot_set_dqblk,
2275 .get_nextdqblk = dquot_get_next_dqblk,
2276};
2277#else
David Brazdil0f672f62019-12-10 10:32:29 +00002278int f2fs_quota_sync(struct super_block *sb, int type)
2279{
2280 return 0;
2281}
2282
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002283void f2fs_quota_off_umount(struct super_block *sb)
2284{
2285}
2286#endif
2287
2288static const struct super_operations f2fs_sops = {
2289 .alloc_inode = f2fs_alloc_inode,
David Brazdil0f672f62019-12-10 10:32:29 +00002290 .free_inode = f2fs_free_inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002291 .drop_inode = f2fs_drop_inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002292 .write_inode = f2fs_write_inode,
2293 .dirty_inode = f2fs_dirty_inode,
2294 .show_options = f2fs_show_options,
2295#ifdef CONFIG_QUOTA
2296 .quota_read = f2fs_quota_read,
2297 .quota_write = f2fs_quota_write,
2298 .get_dquots = f2fs_get_dquots,
2299#endif
2300 .evict_inode = f2fs_evict_inode,
2301 .put_super = f2fs_put_super,
2302 .sync_fs = f2fs_sync_fs,
2303 .freeze_fs = f2fs_freeze,
2304 .unfreeze_fs = f2fs_unfreeze,
2305 .statfs = f2fs_statfs,
2306 .remount_fs = f2fs_remount,
2307};
2308
David Brazdil0f672f62019-12-10 10:32:29 +00002309#ifdef CONFIG_FS_ENCRYPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002310static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
2311{
2312 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2313 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2314 ctx, len, NULL);
2315}
2316
2317static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
2318 void *fs_data)
2319{
2320 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2321
2322 /*
2323 * Encrypting the root directory is not allowed because fsck
2324 * expects lost+found directory to exist and remain unencrypted
2325 * if LOST_FOUND feature is enabled.
2326 *
2327 */
David Brazdil0f672f62019-12-10 10:32:29 +00002328 if (f2fs_sb_has_lost_found(sbi) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002329 inode->i_ino == F2FS_ROOT_INO(sbi))
2330 return -EPERM;
2331
2332 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
2333 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
2334 ctx, len, fs_data, XATTR_CREATE);
2335}
2336
2337static bool f2fs_dummy_context(struct inode *inode)
2338{
2339 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
2340}
2341
2342static const struct fscrypt_operations f2fs_cryptops = {
2343 .key_prefix = "f2fs:",
2344 .get_context = f2fs_get_context,
2345 .set_context = f2fs_set_context,
2346 .dummy_context = f2fs_dummy_context,
2347 .empty_dir = f2fs_empty_dir,
2348 .max_namelen = F2FS_NAME_LEN,
2349};
2350#endif
2351
2352static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2353 u64 ino, u32 generation)
2354{
2355 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2356 struct inode *inode;
2357
2358 if (f2fs_check_nid_range(sbi, ino))
2359 return ERR_PTR(-ESTALE);
2360
2361 /*
2362 * f2fs_iget isn't quite right if the inode is currently unallocated!
2363 * However f2fs_iget currently does appropriate checks to handle stale
2364 * inodes so everything is OK.
2365 */
2366 inode = f2fs_iget(sb, ino);
2367 if (IS_ERR(inode))
2368 return ERR_CAST(inode);
2369 if (unlikely(generation && inode->i_generation != generation)) {
2370 /* we didn't find the right inode.. */
2371 iput(inode);
2372 return ERR_PTR(-ESTALE);
2373 }
2374 return inode;
2375}
2376
2377static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2378 int fh_len, int fh_type)
2379{
2380 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2381 f2fs_nfs_get_inode);
2382}
2383
2384static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2385 int fh_len, int fh_type)
2386{
2387 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2388 f2fs_nfs_get_inode);
2389}
2390
2391static const struct export_operations f2fs_export_ops = {
2392 .fh_to_dentry = f2fs_fh_to_dentry,
2393 .fh_to_parent = f2fs_fh_to_parent,
2394 .get_parent = f2fs_get_parent,
2395};
2396
2397static loff_t max_file_blocks(void)
2398{
2399 loff_t result = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002400 loff_t leaf_count = DEF_ADDRS_PER_BLOCK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002401
2402 /*
2403 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2404 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2405 * space in inode.i_addr, it will be more safe to reassign
2406 * result as zero.
2407 */
2408
2409 /* two direct node blocks */
2410 result += (leaf_count * 2);
2411
2412 /* two indirect node blocks */
2413 leaf_count *= NIDS_PER_BLOCK;
2414 result += (leaf_count * 2);
2415
2416 /* one double indirect node block */
2417 leaf_count *= NIDS_PER_BLOCK;
2418 result += leaf_count;
2419
2420 return result;
2421}
2422
2423static int __f2fs_commit_super(struct buffer_head *bh,
2424 struct f2fs_super_block *super)
2425{
2426 lock_buffer(bh);
2427 if (super)
2428 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2429 set_buffer_dirty(bh);
2430 unlock_buffer(bh);
2431
2432 /* it's rare case, we can do fua all the time */
2433 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2434}
2435
2436static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2437 struct buffer_head *bh)
2438{
2439 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2440 (bh->b_data + F2FS_SUPER_OFFSET);
2441 struct super_block *sb = sbi->sb;
2442 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2443 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2444 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2445 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2446 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2447 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2448 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2449 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2450 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2451 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2452 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2453 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2454 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2455 u64 main_end_blkaddr = main_blkaddr +
2456 (segment_count_main << log_blocks_per_seg);
2457 u64 seg_end_blkaddr = segment0_blkaddr +
2458 (segment_count << log_blocks_per_seg);
2459
2460 if (segment0_blkaddr != cp_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002461 f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2462 segment0_blkaddr, cp_blkaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002463 return true;
2464 }
2465
2466 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2467 sit_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002468 f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2469 cp_blkaddr, sit_blkaddr,
2470 segment_count_ckpt << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002471 return true;
2472 }
2473
2474 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2475 nat_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002476 f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2477 sit_blkaddr, nat_blkaddr,
2478 segment_count_sit << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002479 return true;
2480 }
2481
2482 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2483 ssa_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002484 f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2485 nat_blkaddr, ssa_blkaddr,
2486 segment_count_nat << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002487 return true;
2488 }
2489
2490 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2491 main_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002492 f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2493 ssa_blkaddr, main_blkaddr,
2494 segment_count_ssa << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002495 return true;
2496 }
2497
2498 if (main_end_blkaddr > seg_end_blkaddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002499 f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
2500 main_blkaddr,
2501 segment0_blkaddr +
2502 (segment_count << log_blocks_per_seg),
2503 segment_count_main << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002504 return true;
2505 } else if (main_end_blkaddr < seg_end_blkaddr) {
2506 int err = 0;
2507 char *res;
2508
2509 /* fix in-memory information all the time */
2510 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2511 segment0_blkaddr) >> log_blocks_per_seg);
2512
2513 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2514 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2515 res = "internally";
2516 } else {
2517 err = __f2fs_commit_super(bh, NULL);
2518 res = err ? "failed" : "done";
2519 }
David Brazdil0f672f62019-12-10 10:32:29 +00002520 f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%u) block(%u)",
2521 res, main_blkaddr,
2522 segment0_blkaddr +
2523 (segment_count << log_blocks_per_seg),
2524 segment_count_main << log_blocks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002525 if (err)
2526 return true;
2527 }
2528 return false;
2529}
2530
2531static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2532 struct buffer_head *bh)
2533{
2534 block_t segment_count, segs_per_sec, secs_per_zone;
2535 block_t total_sections, blocks_per_seg;
2536 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2537 (bh->b_data + F2FS_SUPER_OFFSET);
David Brazdil0f672f62019-12-10 10:32:29 +00002538 size_t crc_offset = 0;
2539 __u32 crc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002540
David Brazdil0f672f62019-12-10 10:32:29 +00002541 if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
2542 f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
2543 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2544 return -EINVAL;
2545 }
2546
2547 /* Check checksum_offset and crc in superblock */
2548 if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
2549 crc_offset = le32_to_cpu(raw_super->checksum_offset);
2550 if (crc_offset !=
2551 offsetof(struct f2fs_super_block, crc)) {
2552 f2fs_info(sbi, "Invalid SB checksum offset: %zu",
2553 crc_offset);
2554 return -EFSCORRUPTED;
2555 }
2556 crc = le32_to_cpu(raw_super->crc);
2557 if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
2558 f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
2559 return -EFSCORRUPTED;
2560 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002561 }
2562
2563 /* Currently, support only 4KB page cache size */
2564 if (F2FS_BLKSIZE != PAGE_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +00002565 f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
2566 PAGE_SIZE);
2567 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002568 }
2569
2570 /* Currently, support only 4KB block size */
Olivier Deprez0e641232021-09-23 10:07:05 +02002571 if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
2572 f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
2573 le32_to_cpu(raw_super->log_blocksize),
2574 F2FS_BLKSIZE_BITS);
David Brazdil0f672f62019-12-10 10:32:29 +00002575 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002576 }
2577
2578 /* check log blocks per segment */
2579 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
David Brazdil0f672f62019-12-10 10:32:29 +00002580 f2fs_info(sbi, "Invalid log blocks per segment (%u)",
2581 le32_to_cpu(raw_super->log_blocks_per_seg));
2582 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002583 }
2584
2585 /* Currently, support 512/1024/2048/4096 bytes sector size */
2586 if (le32_to_cpu(raw_super->log_sectorsize) >
2587 F2FS_MAX_LOG_SECTOR_SIZE ||
2588 le32_to_cpu(raw_super->log_sectorsize) <
2589 F2FS_MIN_LOG_SECTOR_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +00002590 f2fs_info(sbi, "Invalid log sectorsize (%u)",
2591 le32_to_cpu(raw_super->log_sectorsize));
2592 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002593 }
2594 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2595 le32_to_cpu(raw_super->log_sectorsize) !=
2596 F2FS_MAX_LOG_SECTOR_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +00002597 f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
2598 le32_to_cpu(raw_super->log_sectors_per_block),
2599 le32_to_cpu(raw_super->log_sectorsize));
2600 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002601 }
2602
2603 segment_count = le32_to_cpu(raw_super->segment_count);
2604 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2605 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2606 total_sections = le32_to_cpu(raw_super->section_count);
2607
2608 /* blocks_per_seg should be 512, given the above check */
2609 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2610
2611 if (segment_count > F2FS_MAX_SEGMENT ||
2612 segment_count < F2FS_MIN_SEGMENTS) {
David Brazdil0f672f62019-12-10 10:32:29 +00002613 f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
2614 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002615 }
2616
2617 if (total_sections > segment_count ||
2618 total_sections < F2FS_MIN_SEGMENTS ||
2619 segs_per_sec > segment_count || !segs_per_sec) {
David Brazdil0f672f62019-12-10 10:32:29 +00002620 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
2621 segment_count, total_sections, segs_per_sec);
2622 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002623 }
2624
2625 if ((segment_count / segs_per_sec) < total_sections) {
David Brazdil0f672f62019-12-10 10:32:29 +00002626 f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
2627 segment_count, segs_per_sec, total_sections);
2628 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002629 }
2630
David Brazdil0f672f62019-12-10 10:32:29 +00002631 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
2632 f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
2633 segment_count, le64_to_cpu(raw_super->block_count));
2634 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002635 }
2636
2637 if (secs_per_zone > total_sections || !secs_per_zone) {
David Brazdil0f672f62019-12-10 10:32:29 +00002638 f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
2639 secs_per_zone, total_sections);
2640 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002641 }
2642 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2643 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2644 (le32_to_cpu(raw_super->extension_count) +
2645 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
David Brazdil0f672f62019-12-10 10:32:29 +00002646 f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
2647 le32_to_cpu(raw_super->extension_count),
2648 raw_super->hot_ext_count,
2649 F2FS_MAX_EXTENSION);
2650 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002651 }
2652
2653 if (le32_to_cpu(raw_super->cp_payload) >
2654 (blocks_per_seg - F2FS_CP_PACKS)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002655 f2fs_info(sbi, "Insane cp_payload (%u > %u)",
2656 le32_to_cpu(raw_super->cp_payload),
2657 blocks_per_seg - F2FS_CP_PACKS);
2658 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002659 }
2660
2661 /* check reserved ino info */
2662 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2663 le32_to_cpu(raw_super->meta_ino) != 2 ||
2664 le32_to_cpu(raw_super->root_ino) != 3) {
David Brazdil0f672f62019-12-10 10:32:29 +00002665 f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2666 le32_to_cpu(raw_super->node_ino),
2667 le32_to_cpu(raw_super->meta_ino),
2668 le32_to_cpu(raw_super->root_ino));
2669 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002670 }
2671
2672 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
2673 if (sanity_check_area_boundary(sbi, bh))
David Brazdil0f672f62019-12-10 10:32:29 +00002674 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002675
2676 return 0;
2677}
2678
2679int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
2680{
2681 unsigned int total, fsmeta;
2682 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2683 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2684 unsigned int ovp_segments, reserved_segments;
2685 unsigned int main_segs, blocks_per_seg;
2686 unsigned int sit_segs, nat_segs;
2687 unsigned int sit_bitmap_size, nat_bitmap_size;
2688 unsigned int log_blocks_per_seg;
2689 unsigned int segment_count_main;
2690 unsigned int cp_pack_start_sum, cp_payload;
David Brazdil0f672f62019-12-10 10:32:29 +00002691 block_t user_block_count, valid_user_blocks;
2692 block_t avail_node_count, valid_node_count;
2693 int i, j;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002694
2695 total = le32_to_cpu(raw_super->segment_count);
2696 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2697 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2698 fsmeta += sit_segs;
2699 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2700 fsmeta += nat_segs;
2701 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2702 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2703
2704 if (unlikely(fsmeta >= total))
2705 return 1;
2706
2707 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2708 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2709
2710 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2711 ovp_segments == 0 || reserved_segments == 0)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002712 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002713 return 1;
2714 }
2715
2716 user_block_count = le64_to_cpu(ckpt->user_block_count);
2717 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2718 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2719 if (!user_block_count || user_block_count >=
2720 segment_count_main << log_blocks_per_seg) {
David Brazdil0f672f62019-12-10 10:32:29 +00002721 f2fs_err(sbi, "Wrong user_block_count: %u",
2722 user_block_count);
2723 return 1;
2724 }
2725
2726 valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
2727 if (valid_user_blocks > user_block_count) {
2728 f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
2729 valid_user_blocks, user_block_count);
2730 return 1;
2731 }
2732
2733 valid_node_count = le32_to_cpu(ckpt->valid_node_count);
2734 avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
2735 if (valid_node_count > avail_node_count) {
2736 f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
2737 valid_node_count, avail_node_count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002738 return 1;
2739 }
2740
2741 main_segs = le32_to_cpu(raw_super->segment_count_main);
2742 blocks_per_seg = sbi->blocks_per_seg;
2743
2744 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2745 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2746 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2747 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00002748 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2749 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2750 le32_to_cpu(ckpt->cur_node_segno[j])) {
2751 f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
2752 i, j,
2753 le32_to_cpu(ckpt->cur_node_segno[i]));
2754 return 1;
2755 }
2756 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002757 }
2758 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2759 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2760 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2761 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00002762 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2763 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2764 le32_to_cpu(ckpt->cur_data_segno[j])) {
2765 f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
2766 i, j,
2767 le32_to_cpu(ckpt->cur_data_segno[i]));
2768 return 1;
2769 }
2770 }
2771 }
2772 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2773 for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
2774 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2775 le32_to_cpu(ckpt->cur_data_segno[j])) {
2776 f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
2777 i, j,
2778 le32_to_cpu(ckpt->cur_node_segno[i]));
2779 return 1;
2780 }
2781 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002782 }
2783
2784 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2785 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
2786
2787 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2788 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
David Brazdil0f672f62019-12-10 10:32:29 +00002789 f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
2790 sit_bitmap_size, nat_bitmap_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002791 return 1;
2792 }
2793
2794 cp_pack_start_sum = __start_sum_addr(sbi);
2795 cp_payload = __cp_payload(sbi);
2796 if (cp_pack_start_sum < cp_payload + 1 ||
2797 cp_pack_start_sum > blocks_per_seg - 1 -
2798 NR_CURSEG_TYPE) {
David Brazdil0f672f62019-12-10 10:32:29 +00002799 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
2800 cp_pack_start_sum);
2801 return 1;
2802 }
2803
2804 if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
2805 le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
2806 f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
2807 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
2808 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
2809 le32_to_cpu(ckpt->checksum_offset));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002810 return 1;
2811 }
2812
2813 if (unlikely(f2fs_cp_error(sbi))) {
David Brazdil0f672f62019-12-10 10:32:29 +00002814 f2fs_err(sbi, "A bug case: need to run fsck");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002815 return 1;
2816 }
2817 return 0;
2818}
2819
2820static void init_sb_info(struct f2fs_sb_info *sbi)
2821{
2822 struct f2fs_super_block *raw_super = sbi->raw_super;
David Brazdil0f672f62019-12-10 10:32:29 +00002823 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002824
2825 sbi->log_sectors_per_block =
2826 le32_to_cpu(raw_super->log_sectors_per_block);
2827 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2828 sbi->blocksize = 1 << sbi->log_blocksize;
2829 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2830 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2831 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2832 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2833 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2834 sbi->total_node_count =
2835 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2836 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2837 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2838 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2839 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
2840 sbi->cur_victim_sec = NULL_SECNO;
David Brazdil0f672f62019-12-10 10:32:29 +00002841 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
2842 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002843 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
David Brazdil0f672f62019-12-10 10:32:29 +00002844 sbi->migration_granularity = sbi->segs_per_sec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002845
2846 sbi->dir_level = DEF_DIR_LEVEL;
2847 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
2848 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002849 sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
2850 sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
2851 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
2852 sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
2853 DEF_UMOUNT_DISCARD_TIMEOUT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002854 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2855
2856 for (i = 0; i < NR_COUNT_TYPE; i++)
2857 atomic_set(&sbi->nr_pages[i], 0);
2858
2859 for (i = 0; i < META; i++)
2860 atomic_set(&sbi->wb_sync_req[i], 0);
2861
2862 INIT_LIST_HEAD(&sbi->s_list);
2863 mutex_init(&sbi->umount_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002864 init_rwsem(&sbi->io_order_lock);
2865 spin_lock_init(&sbi->cp_lock);
2866
2867 sbi->dirty_device = 0;
2868 spin_lock_init(&sbi->dev_lock);
2869
2870 init_rwsem(&sbi->sb_lock);
2871}
2872
2873static int init_percpu_info(struct f2fs_sb_info *sbi)
2874{
2875 int err;
2876
2877 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2878 if (err)
2879 return err;
2880
David Brazdil0f672f62019-12-10 10:32:29 +00002881 err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002882 GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +00002883 if (err)
2884 percpu_counter_destroy(&sbi->alloc_valid_block_count);
2885
2886 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002887}
2888
2889#ifdef CONFIG_BLK_DEV_ZONED
2890static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
2891{
2892 struct block_device *bdev = FDEV(devi).bdev;
2893 sector_t nr_sectors = bdev->bd_part->nr_sects;
2894 sector_t sector = 0;
2895 struct blk_zone *zones;
2896 unsigned int i, nr_zones;
2897 unsigned int n = 0;
2898 int err = -EIO;
2899
David Brazdil0f672f62019-12-10 10:32:29 +00002900 if (!f2fs_sb_has_blkzoned(sbi))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002901 return 0;
2902
2903 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
2904 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
2905 return -EINVAL;
2906 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
2907 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2908 __ilog2_u32(sbi->blocks_per_blkz))
2909 return -EINVAL;
2910 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
2911 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2912 sbi->log_blocks_per_blkz;
2913 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
2914 FDEV(devi).nr_blkz++;
2915
Olivier Deprez0e641232021-09-23 10:07:05 +02002916 FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
David Brazdil0f672f62019-12-10 10:32:29 +00002917 BITS_TO_LONGS(FDEV(devi).nr_blkz)
2918 * sizeof(unsigned long),
2919 GFP_KERNEL);
2920 if (!FDEV(devi).blkz_seq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921 return -ENOMEM;
2922
2923#define F2FS_REPORT_NR_ZONES 4096
2924
2925 zones = f2fs_kzalloc(sbi,
2926 array_size(F2FS_REPORT_NR_ZONES,
2927 sizeof(struct blk_zone)),
2928 GFP_KERNEL);
2929 if (!zones)
2930 return -ENOMEM;
2931
2932 /* Get block zones type */
2933 while (zones && sector < nr_sectors) {
2934
2935 nr_zones = F2FS_REPORT_NR_ZONES;
David Brazdil0f672f62019-12-10 10:32:29 +00002936 err = blkdev_report_zones(bdev, sector, zones, &nr_zones);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002937 if (err)
2938 break;
2939 if (!nr_zones) {
2940 err = -EIO;
2941 break;
2942 }
2943
2944 for (i = 0; i < nr_zones; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00002945 if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL)
2946 set_bit(n, FDEV(devi).blkz_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002947 sector += zones[i].len;
2948 n++;
2949 }
2950 }
2951
David Brazdil0f672f62019-12-10 10:32:29 +00002952 kvfree(zones);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002953
2954 return err;
2955}
2956#endif
2957
2958/*
2959 * Read f2fs raw super block.
2960 * Because we have two copies of super block, so read both of them
2961 * to get the first valid one. If any one of them is broken, we pass
2962 * them recovery flag back to the caller.
2963 */
2964static int read_raw_super_block(struct f2fs_sb_info *sbi,
2965 struct f2fs_super_block **raw_super,
2966 int *valid_super_block, int *recovery)
2967{
2968 struct super_block *sb = sbi->sb;
2969 int block;
2970 struct buffer_head *bh;
2971 struct f2fs_super_block *super;
2972 int err = 0;
2973
2974 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2975 if (!super)
2976 return -ENOMEM;
2977
2978 for (block = 0; block < 2; block++) {
2979 bh = sb_bread(sb, block);
2980 if (!bh) {
David Brazdil0f672f62019-12-10 10:32:29 +00002981 f2fs_err(sbi, "Unable to read %dth superblock",
2982 block + 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002983 err = -EIO;
2984 continue;
2985 }
2986
2987 /* sanity checking of raw super */
David Brazdil0f672f62019-12-10 10:32:29 +00002988 err = sanity_check_raw_super(sbi, bh);
2989 if (err) {
2990 f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
2991 block + 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002992 brelse(bh);
2993 continue;
2994 }
2995
2996 if (!*raw_super) {
2997 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2998 sizeof(*super));
2999 *valid_super_block = block;
3000 *raw_super = super;
3001 }
3002 brelse(bh);
3003 }
3004
3005 /* Fail to read any one of the superblocks*/
3006 if (err < 0)
3007 *recovery = 1;
3008
3009 /* No valid superblock */
3010 if (!*raw_super)
David Brazdil0f672f62019-12-10 10:32:29 +00003011 kvfree(super);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003012 else
3013 err = 0;
3014
3015 return err;
3016}
3017
3018int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
3019{
3020 struct buffer_head *bh;
David Brazdil0f672f62019-12-10 10:32:29 +00003021 __u32 crc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003022 int err;
3023
3024 if ((recover && f2fs_readonly(sbi->sb)) ||
3025 bdev_read_only(sbi->sb->s_bdev)) {
3026 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
3027 return -EROFS;
3028 }
3029
David Brazdil0f672f62019-12-10 10:32:29 +00003030 /* we should update superblock crc here */
3031 if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
3032 crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
3033 offsetof(struct f2fs_super_block, crc));
3034 F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
3035 }
3036
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003037 /* write back-up superblock first */
3038 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
3039 if (!bh)
3040 return -EIO;
3041 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3042 brelse(bh);
3043
3044 /* if we are in recovery path, skip writing valid superblock */
3045 if (recover || err)
3046 return err;
3047
3048 /* write current valid superblock */
3049 bh = sb_bread(sbi->sb, sbi->valid_super_block);
3050 if (!bh)
3051 return -EIO;
3052 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
3053 brelse(bh);
3054 return err;
3055}
3056
3057static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
3058{
3059 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3060 unsigned int max_devices = MAX_DEVICES;
3061 int i;
3062
3063 /* Initialize single device information */
3064 if (!RDEV(0).path[0]) {
3065 if (!bdev_is_zoned(sbi->sb->s_bdev))
3066 return 0;
3067 max_devices = 1;
3068 }
3069
3070 /*
3071 * Initialize multiple devices information, or single
3072 * zoned block device information.
3073 */
3074 sbi->devs = f2fs_kzalloc(sbi,
3075 array_size(max_devices,
3076 sizeof(struct f2fs_dev_info)),
3077 GFP_KERNEL);
3078 if (!sbi->devs)
3079 return -ENOMEM;
3080
3081 for (i = 0; i < max_devices; i++) {
3082
3083 if (i > 0 && !RDEV(i).path[0])
3084 break;
3085
3086 if (max_devices == 1) {
3087 /* Single zoned block device mount */
3088 FDEV(0).bdev =
3089 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
3090 sbi->sb->s_mode, sbi->sb->s_type);
3091 } else {
3092 /* Multi-device mount */
3093 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
3094 FDEV(i).total_segments =
3095 le32_to_cpu(RDEV(i).total_segments);
3096 if (i == 0) {
3097 FDEV(i).start_blk = 0;
3098 FDEV(i).end_blk = FDEV(i).start_blk +
3099 (FDEV(i).total_segments <<
3100 sbi->log_blocks_per_seg) - 1 +
3101 le32_to_cpu(raw_super->segment0_blkaddr);
3102 } else {
3103 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
3104 FDEV(i).end_blk = FDEV(i).start_blk +
3105 (FDEV(i).total_segments <<
3106 sbi->log_blocks_per_seg) - 1;
3107 }
3108 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
3109 sbi->sb->s_mode, sbi->sb->s_type);
3110 }
3111 if (IS_ERR(FDEV(i).bdev))
3112 return PTR_ERR(FDEV(i).bdev);
3113
3114 /* to release errored devices */
3115 sbi->s_ndevs = i + 1;
3116
3117#ifdef CONFIG_BLK_DEV_ZONED
3118 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
David Brazdil0f672f62019-12-10 10:32:29 +00003119 !f2fs_sb_has_blkzoned(sbi)) {
3120 f2fs_err(sbi, "Zoned block device feature not enabled\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003121 return -EINVAL;
3122 }
3123 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
3124 if (init_blkz_info(sbi, i)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003125 f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003126 return -EINVAL;
3127 }
3128 if (max_devices == 1)
3129 break;
David Brazdil0f672f62019-12-10 10:32:29 +00003130 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
3131 i, FDEV(i).path,
3132 FDEV(i).total_segments,
3133 FDEV(i).start_blk, FDEV(i).end_blk,
3134 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
3135 "Host-aware" : "Host-managed");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003136 continue;
3137 }
3138#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003139 f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
3140 i, FDEV(i).path,
3141 FDEV(i).total_segments,
3142 FDEV(i).start_blk, FDEV(i).end_blk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003143 }
David Brazdil0f672f62019-12-10 10:32:29 +00003144 f2fs_info(sbi,
3145 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
3146 return 0;
3147}
3148
3149static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
3150{
3151#ifdef CONFIG_UNICODE
3152 if (f2fs_sb_has_casefold(sbi) && !sbi->s_encoding) {
3153 const struct f2fs_sb_encodings *encoding_info;
3154 struct unicode_map *encoding;
3155 __u16 encoding_flags;
3156
3157 if (f2fs_sb_has_encrypt(sbi)) {
3158 f2fs_err(sbi,
3159 "Can't mount with encoding and encryption");
3160 return -EINVAL;
3161 }
3162
3163 if (f2fs_sb_read_encoding(sbi->raw_super, &encoding_info,
3164 &encoding_flags)) {
3165 f2fs_err(sbi,
3166 "Encoding requested by superblock is unknown");
3167 return -EINVAL;
3168 }
3169
3170 encoding = utf8_load(encoding_info->version);
3171 if (IS_ERR(encoding)) {
3172 f2fs_err(sbi,
3173 "can't mount with superblock charset: %s-%s "
3174 "not supported by the kernel. flags: 0x%x.",
3175 encoding_info->name, encoding_info->version,
3176 encoding_flags);
3177 return PTR_ERR(encoding);
3178 }
3179 f2fs_info(sbi, "Using encoding defined by superblock: "
3180 "%s-%s with flags 0x%hx", encoding_info->name,
3181 encoding_info->version?:"\b", encoding_flags);
3182
3183 sbi->s_encoding = encoding;
3184 sbi->s_encoding_flags = encoding_flags;
3185 sbi->sb->s_d_op = &f2fs_dentry_ops;
3186 }
3187#else
3188 if (f2fs_sb_has_casefold(sbi)) {
3189 f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
3190 return -EINVAL;
3191 }
3192#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003193 return 0;
3194}
3195
3196static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
3197{
3198 struct f2fs_sm_info *sm_i = SM_I(sbi);
3199
3200 /* adjust parameters according to the volume size */
3201 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
3202 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
3203 sm_i->dcc_info->discard_granularity = 1;
3204 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3205 }
3206
3207 sbi->readdir_ra = 1;
3208}
3209
3210static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
3211{
3212 struct f2fs_sb_info *sbi;
3213 struct f2fs_super_block *raw_super;
3214 struct inode *root;
3215 int err;
David Brazdil0f672f62019-12-10 10:32:29 +00003216 bool skip_recovery = false, need_fsck = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003217 char *options = NULL;
3218 int recovery, i, valid_super_block;
3219 struct curseg_info *seg_i;
David Brazdil0f672f62019-12-10 10:32:29 +00003220 int retry_cnt = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003221
3222try_onemore:
3223 err = -EINVAL;
3224 raw_super = NULL;
3225 valid_super_block = -1;
3226 recovery = 0;
3227
3228 /* allocate memory for f2fs-specific super block info */
3229 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
3230 if (!sbi)
3231 return -ENOMEM;
3232
3233 sbi->sb = sb;
3234
3235 /* Load the checksum driver */
3236 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
3237 if (IS_ERR(sbi->s_chksum_driver)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003238 f2fs_err(sbi, "Cannot load crc32 driver.");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003239 err = PTR_ERR(sbi->s_chksum_driver);
3240 sbi->s_chksum_driver = NULL;
3241 goto free_sbi;
3242 }
3243
3244 /* set a block size */
3245 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
David Brazdil0f672f62019-12-10 10:32:29 +00003246 f2fs_err(sbi, "unable to set blocksize");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003247 goto free_sbi;
3248 }
3249
3250 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
3251 &recovery);
3252 if (err)
3253 goto free_sbi;
3254
3255 sb->s_fs_info = sbi;
3256 sbi->raw_super = raw_super;
3257
3258 /* precompute checksum seed for metadata */
David Brazdil0f672f62019-12-10 10:32:29 +00003259 if (f2fs_sb_has_inode_chksum(sbi))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003260 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
3261 sizeof(raw_super->uuid));
3262
3263 /*
3264 * The BLKZONED feature indicates that the drive was formatted with
3265 * zone alignment optimization. This is optional for host-aware
3266 * devices, but mandatory for host-managed zoned block devices.
3267 */
3268#ifndef CONFIG_BLK_DEV_ZONED
David Brazdil0f672f62019-12-10 10:32:29 +00003269 if (f2fs_sb_has_blkzoned(sbi)) {
3270 f2fs_err(sbi, "Zoned block device support is not enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003271 err = -EOPNOTSUPP;
3272 goto free_sb_buf;
3273 }
3274#endif
3275 default_options(sbi);
3276 /* parse mount options */
3277 options = kstrdup((const char *)data, GFP_KERNEL);
3278 if (data && !options) {
3279 err = -ENOMEM;
3280 goto free_sb_buf;
3281 }
3282
3283 err = parse_options(sb, options);
3284 if (err)
3285 goto free_options;
3286
3287 sbi->max_file_blocks = max_file_blocks();
3288 sb->s_maxbytes = sbi->max_file_blocks <<
3289 le32_to_cpu(raw_super->log_blocksize);
3290 sb->s_max_links = F2FS_LINK_MAX;
David Brazdil0f672f62019-12-10 10:32:29 +00003291
3292 err = f2fs_setup_casefold(sbi);
3293 if (err)
3294 goto free_options;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003295
3296#ifdef CONFIG_QUOTA
3297 sb->dq_op = &f2fs_quota_operations;
David Brazdil0f672f62019-12-10 10:32:29 +00003298 sb->s_qcop = &f2fs_quotactl_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003299 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
3300
David Brazdil0f672f62019-12-10 10:32:29 +00003301 if (f2fs_sb_has_quota_ino(sbi)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003302 for (i = 0; i < MAXQUOTAS; i++) {
3303 if (f2fs_qf_ino(sbi->sb, i))
3304 sbi->nquota_files++;
3305 }
3306 }
3307#endif
3308
3309 sb->s_op = &f2fs_sops;
David Brazdil0f672f62019-12-10 10:32:29 +00003310#ifdef CONFIG_FS_ENCRYPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003311 sb->s_cop = &f2fs_cryptops;
3312#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003313#ifdef CONFIG_FS_VERITY
3314 sb->s_vop = &f2fs_verityops;
3315#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003316 sb->s_xattr = f2fs_xattr_handlers;
3317 sb->s_export_op = &f2fs_export_ops;
3318 sb->s_magic = F2FS_SUPER_MAGIC;
3319 sb->s_time_gran = 1;
3320 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
3321 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
3322 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
3323 sb->s_iflags |= SB_I_CGROUPWB;
3324
3325 /* init f2fs-specific super block info */
3326 sbi->valid_super_block = valid_super_block;
3327 mutex_init(&sbi->gc_mutex);
3328 mutex_init(&sbi->writepages);
3329 mutex_init(&sbi->cp_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +00003330 mutex_init(&sbi->resize_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003331 init_rwsem(&sbi->node_write);
3332 init_rwsem(&sbi->node_change);
3333
3334 /* disallow all the data/node/meta page writes */
3335 set_sbi_flag(sbi, SBI_POR_DOING);
3336 spin_lock_init(&sbi->stat_lock);
3337
3338 /* init iostat info */
3339 spin_lock_init(&sbi->iostat_lock);
3340 sbi->iostat_enable = false;
3341
3342 for (i = 0; i < NR_PAGE_TYPE; i++) {
3343 int n = (i == META) ? 1: NR_TEMP_TYPE;
3344 int j;
3345
3346 sbi->write_io[i] =
3347 f2fs_kmalloc(sbi,
3348 array_size(n,
3349 sizeof(struct f2fs_bio_info)),
3350 GFP_KERNEL);
3351 if (!sbi->write_io[i]) {
3352 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00003353 goto free_bio_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003354 }
3355
3356 for (j = HOT; j < n; j++) {
3357 init_rwsem(&sbi->write_io[i][j].io_rwsem);
3358 sbi->write_io[i][j].sbi = sbi;
3359 sbi->write_io[i][j].bio = NULL;
3360 spin_lock_init(&sbi->write_io[i][j].io_lock);
3361 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
3362 }
3363 }
3364
3365 init_rwsem(&sbi->cp_rwsem);
David Brazdil0f672f62019-12-10 10:32:29 +00003366 init_rwsem(&sbi->quota_sem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003367 init_waitqueue_head(&sbi->cp_wait);
3368 init_sb_info(sbi);
3369
3370 err = init_percpu_info(sbi);
3371 if (err)
3372 goto free_bio_info;
3373
David Brazdil0f672f62019-12-10 10:32:29 +00003374 if (F2FS_IO_ALIGNED(sbi)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375 sbi->write_io_dummy =
3376 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
3377 if (!sbi->write_io_dummy) {
3378 err = -ENOMEM;
3379 goto free_percpu;
3380 }
3381 }
3382
3383 /* get an inode for meta space */
3384 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
3385 if (IS_ERR(sbi->meta_inode)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003386 f2fs_err(sbi, "Failed to read F2FS meta data inode");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003387 err = PTR_ERR(sbi->meta_inode);
3388 goto free_io_dummy;
3389 }
3390
3391 err = f2fs_get_valid_checkpoint(sbi);
3392 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00003393 f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003394 goto free_meta_inode;
3395 }
3396
David Brazdil0f672f62019-12-10 10:32:29 +00003397 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
3398 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3399 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
3400 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
3401 sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
3402 }
3403
3404 if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3405 set_sbi_flag(sbi, SBI_NEED_FSCK);
3406
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003407 /* Initialize device list */
3408 err = f2fs_scan_devices(sbi);
3409 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00003410 f2fs_err(sbi, "Failed to find devices");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003411 goto free_devices;
3412 }
3413
3414 sbi->total_valid_node_count =
3415 le32_to_cpu(sbi->ckpt->valid_node_count);
3416 percpu_counter_set(&sbi->total_valid_inode_count,
3417 le32_to_cpu(sbi->ckpt->valid_inode_count));
3418 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
3419 sbi->total_valid_block_count =
3420 le64_to_cpu(sbi->ckpt->valid_block_count);
3421 sbi->last_valid_block_count = sbi->total_valid_block_count;
3422 sbi->reserved_blocks = 0;
3423 sbi->current_reserved_blocks = 0;
3424 limit_reserve_root(sbi);
Olivier Deprez0e641232021-09-23 10:07:05 +02003425 adjust_unusable_cap_perc(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003426
3427 for (i = 0; i < NR_INODE_TYPE; i++) {
3428 INIT_LIST_HEAD(&sbi->inode_list[i]);
3429 spin_lock_init(&sbi->inode_lock[i]);
3430 }
David Brazdil0f672f62019-12-10 10:32:29 +00003431 mutex_init(&sbi->flush_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003432
3433 f2fs_init_extent_cache_info(sbi);
3434
3435 f2fs_init_ino_entry_info(sbi);
3436
3437 f2fs_init_fsync_node_info(sbi);
3438
3439 /* setup f2fs internal modules */
3440 err = f2fs_build_segment_manager(sbi);
3441 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00003442 f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
3443 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003444 goto free_sm;
3445 }
3446 err = f2fs_build_node_manager(sbi);
3447 if (err) {
David Brazdil0f672f62019-12-10 10:32:29 +00003448 f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
3449 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003450 goto free_nm;
3451 }
3452
3453 /* For write statistics */
3454 if (sb->s_bdev->bd_part)
3455 sbi->sectors_written_start =
3456 (u64)part_stat_read(sb->s_bdev->bd_part,
3457 sectors[STAT_WRITE]);
3458
3459 /* Read accumulated write IO statistics if exists */
3460 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3461 if (__exist_node_summaries(sbi))
3462 sbi->kbytes_written =
3463 le64_to_cpu(seg_i->journal->info.kbytes_written);
3464
3465 f2fs_build_gc_manager(sbi);
3466
David Brazdil0f672f62019-12-10 10:32:29 +00003467 err = f2fs_build_stats(sbi);
3468 if (err)
3469 goto free_nm;
3470
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003471 /* get an inode for node space */
3472 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3473 if (IS_ERR(sbi->node_inode)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003474 f2fs_err(sbi, "Failed to read node inode");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003475 err = PTR_ERR(sbi->node_inode);
David Brazdil0f672f62019-12-10 10:32:29 +00003476 goto free_stats;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003477 }
3478
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003479 /* read root inode and dentry */
3480 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3481 if (IS_ERR(root)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003482 f2fs_err(sbi, "Failed to read root inode");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003483 err = PTR_ERR(root);
David Brazdil0f672f62019-12-10 10:32:29 +00003484 goto free_node_inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003485 }
3486 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3487 !root->i_size || !root->i_nlink) {
3488 iput(root);
3489 err = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00003490 goto free_node_inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003491 }
3492
3493 sb->s_root = d_make_root(root); /* allocate root dentry */
3494 if (!sb->s_root) {
3495 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00003496 goto free_node_inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003497 }
3498
3499 err = f2fs_register_sysfs(sbi);
3500 if (err)
3501 goto free_root_inode;
3502
3503#ifdef CONFIG_QUOTA
3504 /* Enable quota usage during mount */
David Brazdil0f672f62019-12-10 10:32:29 +00003505 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003506 err = f2fs_enable_quotas(sb);
David Brazdil0f672f62019-12-10 10:32:29 +00003507 if (err)
3508 f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003509 }
3510#endif
3511 /* if there are nt orphan nodes free them */
3512 err = f2fs_recover_orphan_inodes(sbi);
3513 if (err)
3514 goto free_meta;
3515
David Brazdil0f672f62019-12-10 10:32:29 +00003516 if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)))
3517 goto reset_checkpoint;
3518
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003519 /* recover fsynced data */
Olivier Deprez0e641232021-09-23 10:07:05 +02003520 if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
3521 !test_opt(sbi, NORECOVERY)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003522 /*
3523 * mount should be failed, when device has readonly mode, and
3524 * previous checkpoint was not done by clean system shutdown.
3525 */
David Brazdil0f672f62019-12-10 10:32:29 +00003526 if (f2fs_hw_is_readonly(sbi)) {
3527 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3528 err = -EROFS;
3529 f2fs_err(sbi, "Need to recover fsync data, but write access unavailable");
3530 goto free_meta;
3531 }
3532 f2fs_info(sbi, "write access unavailable, skipping recovery");
3533 goto reset_checkpoint;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003534 }
3535
3536 if (need_fsck)
3537 set_sbi_flag(sbi, SBI_NEED_FSCK);
3538
David Brazdil0f672f62019-12-10 10:32:29 +00003539 if (skip_recovery)
3540 goto reset_checkpoint;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003541
3542 err = f2fs_recover_fsync_data(sbi, false);
3543 if (err < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00003544 if (err != -ENOMEM)
3545 skip_recovery = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003546 need_fsck = true;
David Brazdil0f672f62019-12-10 10:32:29 +00003547 f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
3548 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003549 goto free_meta;
3550 }
3551 } else {
3552 err = f2fs_recover_fsync_data(sbi, true);
3553
3554 if (!f2fs_readonly(sb) && err > 0) {
3555 err = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00003556 f2fs_err(sbi, "Need to recover fsync data");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003557 goto free_meta;
3558 }
3559 }
David Brazdil0f672f62019-12-10 10:32:29 +00003560reset_checkpoint:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003561 /* f2fs_recover_fsync_data() cleared this already */
3562 clear_sbi_flag(sbi, SBI_POR_DOING);
3563
David Brazdil0f672f62019-12-10 10:32:29 +00003564 if (test_opt(sbi, DISABLE_CHECKPOINT)) {
3565 err = f2fs_disable_checkpoint(sbi);
3566 if (err)
3567 goto sync_free_meta;
3568 } else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG)) {
3569 f2fs_enable_checkpoint(sbi);
3570 }
3571
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003572 /*
3573 * If filesystem is not mounted as read-only then
3574 * do start the gc_thread.
3575 */
3576 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
3577 /* After POR, we can run background GC thread.*/
3578 err = f2fs_start_gc_thread(sbi);
3579 if (err)
David Brazdil0f672f62019-12-10 10:32:29 +00003580 goto sync_free_meta;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003581 }
David Brazdil0f672f62019-12-10 10:32:29 +00003582 kvfree(options);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003583
3584 /* recover broken superblock */
3585 if (recovery) {
3586 err = f2fs_commit_super(sbi, true);
David Brazdil0f672f62019-12-10 10:32:29 +00003587 f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
3588 sbi->valid_super_block ? 1 : 2, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003589 }
3590
3591 f2fs_join_shrinker(sbi);
3592
3593 f2fs_tuning_parameters(sbi);
3594
David Brazdil0f672f62019-12-10 10:32:29 +00003595 f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
3596 cur_cp_version(F2FS_CKPT(sbi)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003597 f2fs_update_time(sbi, CP_TIME);
3598 f2fs_update_time(sbi, REQ_TIME);
David Brazdil0f672f62019-12-10 10:32:29 +00003599 clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003600 return 0;
3601
David Brazdil0f672f62019-12-10 10:32:29 +00003602sync_free_meta:
3603 /* safe to flush all the data */
3604 sync_filesystem(sbi->sb);
3605 retry_cnt = 0;
3606
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003607free_meta:
3608#ifdef CONFIG_QUOTA
David Brazdil0f672f62019-12-10 10:32:29 +00003609 f2fs_truncate_quota_inode_pages(sb);
3610 if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003611 f2fs_quota_off_umount(sbi->sb);
3612#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003613 /*
3614 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
3615 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
3616 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3617 * falls into an infinite loop in f2fs_sync_meta_pages().
3618 */
3619 truncate_inode_pages_final(META_MAPPING(sbi));
David Brazdil0f672f62019-12-10 10:32:29 +00003620 /* evict some inodes being cached by GC */
3621 evict_inodes(sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003622 f2fs_unregister_sysfs(sbi);
3623free_root_inode:
3624 dput(sb->s_root);
3625 sb->s_root = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003626free_node_inode:
3627 f2fs_release_ino_entry(sbi, true);
3628 truncate_inode_pages_final(NODE_MAPPING(sbi));
3629 iput(sbi->node_inode);
David Brazdil0f672f62019-12-10 10:32:29 +00003630 sbi->node_inode = NULL;
3631free_stats:
3632 f2fs_destroy_stats(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003633free_nm:
3634 f2fs_destroy_node_manager(sbi);
3635free_sm:
3636 f2fs_destroy_segment_manager(sbi);
3637free_devices:
3638 destroy_device_list(sbi);
David Brazdil0f672f62019-12-10 10:32:29 +00003639 kvfree(sbi->ckpt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003640free_meta_inode:
3641 make_bad_inode(sbi->meta_inode);
3642 iput(sbi->meta_inode);
David Brazdil0f672f62019-12-10 10:32:29 +00003643 sbi->meta_inode = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003644free_io_dummy:
3645 mempool_destroy(sbi->write_io_dummy);
3646free_percpu:
3647 destroy_percpu_info(sbi);
3648free_bio_info:
3649 for (i = 0; i < NR_PAGE_TYPE; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00003650 kvfree(sbi->write_io[i]);
3651
3652#ifdef CONFIG_UNICODE
3653 utf8_unload(sbi->s_encoding);
3654#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003655free_options:
3656#ifdef CONFIG_QUOTA
3657 for (i = 0; i < MAXQUOTAS; i++)
David Brazdil0f672f62019-12-10 10:32:29 +00003658 kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003659#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003660 kvfree(options);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003661free_sb_buf:
David Brazdil0f672f62019-12-10 10:32:29 +00003662 kvfree(raw_super);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003663free_sbi:
3664 if (sbi->s_chksum_driver)
3665 crypto_free_shash(sbi->s_chksum_driver);
David Brazdil0f672f62019-12-10 10:32:29 +00003666 kvfree(sbi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003667
3668 /* give only one another chance */
David Brazdil0f672f62019-12-10 10:32:29 +00003669 if (retry_cnt > 0 && skip_recovery) {
3670 retry_cnt--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003671 shrink_dcache_sb(sb);
3672 goto try_onemore;
3673 }
3674 return err;
3675}
3676
3677static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3678 const char *dev_name, void *data)
3679{
3680 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3681}
3682
3683static void kill_f2fs_super(struct super_block *sb)
3684{
3685 if (sb->s_root) {
3686 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3687
3688 set_sbi_flag(sbi, SBI_IS_CLOSE);
3689 f2fs_stop_gc_thread(sbi);
3690 f2fs_stop_discard_thread(sbi);
3691
3692 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3693 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3694 struct cp_control cpc = {
3695 .reason = CP_UMOUNT,
3696 };
3697 f2fs_write_checkpoint(sbi, &cpc);
3698 }
3699
3700 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3701 sb->s_flags &= ~SB_RDONLY;
3702 }
3703 kill_block_super(sb);
3704}
3705
3706static struct file_system_type f2fs_fs_type = {
3707 .owner = THIS_MODULE,
3708 .name = "f2fs",
3709 .mount = f2fs_mount,
3710 .kill_sb = kill_f2fs_super,
3711 .fs_flags = FS_REQUIRES_DEV,
3712};
3713MODULE_ALIAS_FS("f2fs");
3714
3715static int __init init_inodecache(void)
3716{
3717 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3718 sizeof(struct f2fs_inode_info), 0,
3719 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
3720 if (!f2fs_inode_cachep)
3721 return -ENOMEM;
3722 return 0;
3723}
3724
3725static void destroy_inodecache(void)
3726{
3727 /*
3728 * Make sure all delayed rcu free inodes are flushed before we
3729 * destroy cache.
3730 */
3731 rcu_barrier();
3732 kmem_cache_destroy(f2fs_inode_cachep);
3733}
3734
3735static int __init init_f2fs_fs(void)
3736{
3737 int err;
3738
3739 if (PAGE_SIZE != F2FS_BLKSIZE) {
3740 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3741 PAGE_SIZE, F2FS_BLKSIZE);
3742 return -EINVAL;
3743 }
3744
3745 f2fs_build_trace_ios();
3746
3747 err = init_inodecache();
3748 if (err)
3749 goto fail;
3750 err = f2fs_create_node_manager_caches();
3751 if (err)
3752 goto free_inodecache;
3753 err = f2fs_create_segment_manager_caches();
3754 if (err)
3755 goto free_node_manager_caches;
3756 err = f2fs_create_checkpoint_caches();
3757 if (err)
3758 goto free_segment_manager_caches;
3759 err = f2fs_create_extent_cache();
3760 if (err)
3761 goto free_checkpoint_caches;
3762 err = f2fs_init_sysfs();
3763 if (err)
3764 goto free_extent_cache;
3765 err = register_shrinker(&f2fs_shrinker_info);
3766 if (err)
3767 goto free_sysfs;
3768 err = register_filesystem(&f2fs_fs_type);
3769 if (err)
3770 goto free_shrinker;
David Brazdil0f672f62019-12-10 10:32:29 +00003771 f2fs_create_root_stats();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003772 err = f2fs_init_post_read_processing();
3773 if (err)
3774 goto free_root_stats;
3775 return 0;
3776
3777free_root_stats:
3778 f2fs_destroy_root_stats();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003779 unregister_filesystem(&f2fs_fs_type);
3780free_shrinker:
3781 unregister_shrinker(&f2fs_shrinker_info);
3782free_sysfs:
3783 f2fs_exit_sysfs();
3784free_extent_cache:
3785 f2fs_destroy_extent_cache();
3786free_checkpoint_caches:
3787 f2fs_destroy_checkpoint_caches();
3788free_segment_manager_caches:
3789 f2fs_destroy_segment_manager_caches();
3790free_node_manager_caches:
3791 f2fs_destroy_node_manager_caches();
3792free_inodecache:
3793 destroy_inodecache();
3794fail:
3795 return err;
3796}
3797
3798static void __exit exit_f2fs_fs(void)
3799{
3800 f2fs_destroy_post_read_processing();
3801 f2fs_destroy_root_stats();
3802 unregister_filesystem(&f2fs_fs_type);
3803 unregister_shrinker(&f2fs_shrinker_info);
3804 f2fs_exit_sysfs();
3805 f2fs_destroy_extent_cache();
3806 f2fs_destroy_checkpoint_caches();
3807 f2fs_destroy_segment_manager_caches();
3808 f2fs_destroy_node_manager_caches();
3809 destroy_inodecache();
3810 f2fs_destroy_trace_ios();
3811}
3812
3813module_init(init_f2fs_fs)
3814module_exit(exit_f2fs_fs)
3815
3816MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3817MODULE_DESCRIPTION("Flash Friendly File System");
3818MODULE_LICENSE("GPL");
Olivier Deprez0e641232021-09-23 10:07:05 +02003819MODULE_SOFTDEP("pre: crc32");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003820