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