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