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