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