ALSA: usb-audio: fix sign unintended sign extension on left shifts
[linux/fpc-iii.git] / fs / f2fs / super.c
blob2264f27fd26d20c4bb650d05ed42b210649cbf42
1 /*
2 * fs/f2fs/super.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/statfs.h>
15 #include <linux/buffer_head.h>
16 #include <linux/backing-dev.h>
17 #include <linux/kthread.h>
18 #include <linux/parser.h>
19 #include <linux/mount.h>
20 #include <linux/seq_file.h>
21 #include <linux/proc_fs.h>
22 #include <linux/random.h>
23 #include <linux/exportfs.h>
24 #include <linux/blkdev.h>
25 #include <linux/quotaops.h>
26 #include <linux/f2fs_fs.h>
27 #include <linux/sysfs.h>
28 #include <linux/quota.h>
30 #include "f2fs.h"
31 #include "node.h"
32 #include "segment.h"
33 #include "xattr.h"
34 #include "gc.h"
35 #include "trace.h"
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/f2fs.h>
40 static struct kmem_cache *f2fs_inode_cachep;
42 #ifdef CONFIG_F2FS_FAULT_INJECTION
44 char *f2fs_fault_name[FAULT_MAX] = {
45 [FAULT_KMALLOC] = "kmalloc",
46 [FAULT_KVMALLOC] = "kvmalloc",
47 [FAULT_PAGE_ALLOC] = "page alloc",
48 [FAULT_PAGE_GET] = "page get",
49 [FAULT_ALLOC_BIO] = "alloc bio",
50 [FAULT_ALLOC_NID] = "alloc nid",
51 [FAULT_ORPHAN] = "orphan",
52 [FAULT_BLOCK] = "no more block",
53 [FAULT_DIR_DEPTH] = "too big dir depth",
54 [FAULT_EVICT_INODE] = "evict_inode fail",
55 [FAULT_TRUNCATE] = "truncate fail",
56 [FAULT_IO] = "IO error",
57 [FAULT_CHECKPOINT] = "checkpoint error",
58 [FAULT_DISCARD] = "discard error",
61 void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
62 unsigned int type)
64 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
66 if (rate) {
67 atomic_set(&ffi->inject_ops, 0);
68 ffi->inject_rate = rate;
71 if (type)
72 ffi->inject_type = type;
74 if (!rate && !type)
75 memset(ffi, 0, sizeof(struct f2fs_fault_info));
77 #endif
79 /* f2fs-wide shrinker description */
80 static struct shrinker f2fs_shrinker_info = {
81 .scan_objects = f2fs_shrink_scan,
82 .count_objects = f2fs_shrink_count,
83 .seeks = DEFAULT_SEEKS,
86 enum {
87 Opt_gc_background,
88 Opt_disable_roll_forward,
89 Opt_norecovery,
90 Opt_discard,
91 Opt_nodiscard,
92 Opt_noheap,
93 Opt_heap,
94 Opt_user_xattr,
95 Opt_nouser_xattr,
96 Opt_acl,
97 Opt_noacl,
98 Opt_active_logs,
99 Opt_disable_ext_identify,
100 Opt_inline_xattr,
101 Opt_noinline_xattr,
102 Opt_inline_xattr_size,
103 Opt_inline_data,
104 Opt_inline_dentry,
105 Opt_noinline_dentry,
106 Opt_flush_merge,
107 Opt_noflush_merge,
108 Opt_nobarrier,
109 Opt_fastboot,
110 Opt_extent_cache,
111 Opt_noextent_cache,
112 Opt_noinline_data,
113 Opt_data_flush,
114 Opt_reserve_root,
115 Opt_resgid,
116 Opt_resuid,
117 Opt_mode,
118 Opt_io_size_bits,
119 Opt_fault_injection,
120 Opt_fault_type,
121 Opt_lazytime,
122 Opt_nolazytime,
123 Opt_quota,
124 Opt_noquota,
125 Opt_usrquota,
126 Opt_grpquota,
127 Opt_prjquota,
128 Opt_usrjquota,
129 Opt_grpjquota,
130 Opt_prjjquota,
131 Opt_offusrjquota,
132 Opt_offgrpjquota,
133 Opt_offprjjquota,
134 Opt_jqfmt_vfsold,
135 Opt_jqfmt_vfsv0,
136 Opt_jqfmt_vfsv1,
137 Opt_whint,
138 Opt_alloc,
139 Opt_fsync,
140 Opt_test_dummy_encryption,
141 Opt_err,
144 static match_table_t f2fs_tokens = {
145 {Opt_gc_background, "background_gc=%s"},
146 {Opt_disable_roll_forward, "disable_roll_forward"},
147 {Opt_norecovery, "norecovery"},
148 {Opt_discard, "discard"},
149 {Opt_nodiscard, "nodiscard"},
150 {Opt_noheap, "no_heap"},
151 {Opt_heap, "heap"},
152 {Opt_user_xattr, "user_xattr"},
153 {Opt_nouser_xattr, "nouser_xattr"},
154 {Opt_acl, "acl"},
155 {Opt_noacl, "noacl"},
156 {Opt_active_logs, "active_logs=%u"},
157 {Opt_disable_ext_identify, "disable_ext_identify"},
158 {Opt_inline_xattr, "inline_xattr"},
159 {Opt_noinline_xattr, "noinline_xattr"},
160 {Opt_inline_xattr_size, "inline_xattr_size=%u"},
161 {Opt_inline_data, "inline_data"},
162 {Opt_inline_dentry, "inline_dentry"},
163 {Opt_noinline_dentry, "noinline_dentry"},
164 {Opt_flush_merge, "flush_merge"},
165 {Opt_noflush_merge, "noflush_merge"},
166 {Opt_nobarrier, "nobarrier"},
167 {Opt_fastboot, "fastboot"},
168 {Opt_extent_cache, "extent_cache"},
169 {Opt_noextent_cache, "noextent_cache"},
170 {Opt_noinline_data, "noinline_data"},
171 {Opt_data_flush, "data_flush"},
172 {Opt_reserve_root, "reserve_root=%u"},
173 {Opt_resgid, "resgid=%u"},
174 {Opt_resuid, "resuid=%u"},
175 {Opt_mode, "mode=%s"},
176 {Opt_io_size_bits, "io_bits=%u"},
177 {Opt_fault_injection, "fault_injection=%u"},
178 {Opt_fault_type, "fault_type=%u"},
179 {Opt_lazytime, "lazytime"},
180 {Opt_nolazytime, "nolazytime"},
181 {Opt_quota, "quota"},
182 {Opt_noquota, "noquota"},
183 {Opt_usrquota, "usrquota"},
184 {Opt_grpquota, "grpquota"},
185 {Opt_prjquota, "prjquota"},
186 {Opt_usrjquota, "usrjquota=%s"},
187 {Opt_grpjquota, "grpjquota=%s"},
188 {Opt_prjjquota, "prjjquota=%s"},
189 {Opt_offusrjquota, "usrjquota="},
190 {Opt_offgrpjquota, "grpjquota="},
191 {Opt_offprjjquota, "prjjquota="},
192 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
193 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
194 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
195 {Opt_whint, "whint_mode=%s"},
196 {Opt_alloc, "alloc_mode=%s"},
197 {Opt_fsync, "fsync_mode=%s"},
198 {Opt_test_dummy_encryption, "test_dummy_encryption"},
199 {Opt_err, NULL},
202 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
204 struct va_format vaf;
205 va_list args;
207 va_start(args, fmt);
208 vaf.fmt = fmt;
209 vaf.va = &args;
210 printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
211 va_end(args);
214 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
216 block_t limit = (sbi->user_block_count << 1) / 1000;
218 /* limit is 0.2% */
219 if (test_opt(sbi, RESERVE_ROOT) &&
220 F2FS_OPTION(sbi).root_reserved_blocks > limit) {
221 F2FS_OPTION(sbi).root_reserved_blocks = limit;
222 f2fs_msg(sbi->sb, KERN_INFO,
223 "Reduce reserved blocks for root = %u",
224 F2FS_OPTION(sbi).root_reserved_blocks);
226 if (!test_opt(sbi, RESERVE_ROOT) &&
227 (!uid_eq(F2FS_OPTION(sbi).s_resuid,
228 make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
229 !gid_eq(F2FS_OPTION(sbi).s_resgid,
230 make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
231 f2fs_msg(sbi->sb, KERN_INFO,
232 "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
233 from_kuid_munged(&init_user_ns,
234 F2FS_OPTION(sbi).s_resuid),
235 from_kgid_munged(&init_user_ns,
236 F2FS_OPTION(sbi).s_resgid));
239 static void init_once(void *foo)
241 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
243 inode_init_once(&fi->vfs_inode);
246 #ifdef CONFIG_QUOTA
247 static const char * const quotatypes[] = INITQFNAMES;
248 #define QTYPE2NAME(t) (quotatypes[t])
249 static int f2fs_set_qf_name(struct super_block *sb, int qtype,
250 substring_t *args)
252 struct f2fs_sb_info *sbi = F2FS_SB(sb);
253 char *qname;
254 int ret = -EINVAL;
256 if (sb_any_quota_loaded(sb) && !F2FS_OPTION(sbi).s_qf_names[qtype]) {
257 f2fs_msg(sb, KERN_ERR,
258 "Cannot change journaled "
259 "quota options when quota turned on");
260 return -EINVAL;
262 if (f2fs_sb_has_quota_ino(sb)) {
263 f2fs_msg(sb, KERN_INFO,
264 "QUOTA feature is enabled, so ignore qf_name");
265 return 0;
268 qname = match_strdup(args);
269 if (!qname) {
270 f2fs_msg(sb, KERN_ERR,
271 "Not enough memory for storing quotafile name");
272 return -EINVAL;
274 if (F2FS_OPTION(sbi).s_qf_names[qtype]) {
275 if (strcmp(F2FS_OPTION(sbi).s_qf_names[qtype], qname) == 0)
276 ret = 0;
277 else
278 f2fs_msg(sb, KERN_ERR,
279 "%s quota file already specified",
280 QTYPE2NAME(qtype));
281 goto errout;
283 if (strchr(qname, '/')) {
284 f2fs_msg(sb, KERN_ERR,
285 "quotafile must be on filesystem root");
286 goto errout;
288 F2FS_OPTION(sbi).s_qf_names[qtype] = qname;
289 set_opt(sbi, QUOTA);
290 return 0;
291 errout:
292 kfree(qname);
293 return ret;
296 static int f2fs_clear_qf_name(struct super_block *sb, int qtype)
298 struct f2fs_sb_info *sbi = F2FS_SB(sb);
300 if (sb_any_quota_loaded(sb) && F2FS_OPTION(sbi).s_qf_names[qtype]) {
301 f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options"
302 " when quota turned on");
303 return -EINVAL;
305 kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
306 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
307 return 0;
310 static int f2fs_check_quota_options(struct f2fs_sb_info *sbi)
313 * We do the test below only for project quotas. 'usrquota' and
314 * 'grpquota' mount options are allowed even without quota feature
315 * to support legacy quotas in quota files.
317 if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) {
318 f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. "
319 "Cannot enable project quota enforcement.");
320 return -1;
322 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
323 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
324 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) {
325 if (test_opt(sbi, USRQUOTA) &&
326 F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
327 clear_opt(sbi, USRQUOTA);
329 if (test_opt(sbi, GRPQUOTA) &&
330 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
331 clear_opt(sbi, GRPQUOTA);
333 if (test_opt(sbi, PRJQUOTA) &&
334 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
335 clear_opt(sbi, PRJQUOTA);
337 if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) ||
338 test_opt(sbi, PRJQUOTA)) {
339 f2fs_msg(sbi->sb, KERN_ERR, "old and new quota "
340 "format mixing");
341 return -1;
344 if (!F2FS_OPTION(sbi).s_jquota_fmt) {
345 f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format "
346 "not specified");
347 return -1;
351 if (f2fs_sb_has_quota_ino(sbi->sb) && F2FS_OPTION(sbi).s_jquota_fmt) {
352 f2fs_msg(sbi->sb, KERN_INFO,
353 "QUOTA feature is enabled, so ignore jquota_fmt");
354 F2FS_OPTION(sbi).s_jquota_fmt = 0;
356 return 0;
358 #endif
360 static int parse_options(struct super_block *sb, char *options)
362 struct f2fs_sb_info *sbi = F2FS_SB(sb);
363 substring_t args[MAX_OPT_ARGS];
364 char *p, *name;
365 int arg = 0;
366 kuid_t uid;
367 kgid_t gid;
368 #ifdef CONFIG_QUOTA
369 int ret;
370 #endif
372 if (!options)
373 return 0;
375 while ((p = strsep(&options, ",")) != NULL) {
376 int token;
377 if (!*p)
378 continue;
380 * Initialize args struct so we know whether arg was
381 * found; some options take optional arguments.
383 args[0].to = args[0].from = NULL;
384 token = match_token(p, f2fs_tokens, args);
386 switch (token) {
387 case Opt_gc_background:
388 name = match_strdup(&args[0]);
390 if (!name)
391 return -ENOMEM;
392 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
393 set_opt(sbi, BG_GC);
394 clear_opt(sbi, FORCE_FG_GC);
395 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
396 clear_opt(sbi, BG_GC);
397 clear_opt(sbi, FORCE_FG_GC);
398 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
399 set_opt(sbi, BG_GC);
400 set_opt(sbi, FORCE_FG_GC);
401 } else {
402 kfree(name);
403 return -EINVAL;
405 kfree(name);
406 break;
407 case Opt_disable_roll_forward:
408 set_opt(sbi, DISABLE_ROLL_FORWARD);
409 break;
410 case Opt_norecovery:
411 /* this option mounts f2fs with ro */
412 set_opt(sbi, DISABLE_ROLL_FORWARD);
413 if (!f2fs_readonly(sb))
414 return -EINVAL;
415 break;
416 case Opt_discard:
417 set_opt(sbi, DISCARD);
418 break;
419 case Opt_nodiscard:
420 if (f2fs_sb_has_blkzoned(sb)) {
421 f2fs_msg(sb, KERN_WARNING,
422 "discard is required for zoned block devices");
423 return -EINVAL;
425 clear_opt(sbi, DISCARD);
426 break;
427 case Opt_noheap:
428 set_opt(sbi, NOHEAP);
429 break;
430 case Opt_heap:
431 clear_opt(sbi, NOHEAP);
432 break;
433 #ifdef CONFIG_F2FS_FS_XATTR
434 case Opt_user_xattr:
435 set_opt(sbi, XATTR_USER);
436 break;
437 case Opt_nouser_xattr:
438 clear_opt(sbi, XATTR_USER);
439 break;
440 case Opt_inline_xattr:
441 set_opt(sbi, INLINE_XATTR);
442 break;
443 case Opt_noinline_xattr:
444 clear_opt(sbi, INLINE_XATTR);
445 break;
446 case Opt_inline_xattr_size:
447 if (args->from && match_int(args, &arg))
448 return -EINVAL;
449 set_opt(sbi, INLINE_XATTR_SIZE);
450 F2FS_OPTION(sbi).inline_xattr_size = arg;
451 break;
452 #else
453 case Opt_user_xattr:
454 f2fs_msg(sb, KERN_INFO,
455 "user_xattr options not supported");
456 break;
457 case Opt_nouser_xattr:
458 f2fs_msg(sb, KERN_INFO,
459 "nouser_xattr options not supported");
460 break;
461 case Opt_inline_xattr:
462 f2fs_msg(sb, KERN_INFO,
463 "inline_xattr options not supported");
464 break;
465 case Opt_noinline_xattr:
466 f2fs_msg(sb, KERN_INFO,
467 "noinline_xattr options not supported");
468 break;
469 #endif
470 #ifdef CONFIG_F2FS_FS_POSIX_ACL
471 case Opt_acl:
472 set_opt(sbi, POSIX_ACL);
473 break;
474 case Opt_noacl:
475 clear_opt(sbi, POSIX_ACL);
476 break;
477 #else
478 case Opt_acl:
479 f2fs_msg(sb, KERN_INFO, "acl options not supported");
480 break;
481 case Opt_noacl:
482 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
483 break;
484 #endif
485 case Opt_active_logs:
486 if (args->from && match_int(args, &arg))
487 return -EINVAL;
488 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
489 return -EINVAL;
490 F2FS_OPTION(sbi).active_logs = arg;
491 break;
492 case Opt_disable_ext_identify:
493 set_opt(sbi, DISABLE_EXT_IDENTIFY);
494 break;
495 case Opt_inline_data:
496 set_opt(sbi, INLINE_DATA);
497 break;
498 case Opt_inline_dentry:
499 set_opt(sbi, INLINE_DENTRY);
500 break;
501 case Opt_noinline_dentry:
502 clear_opt(sbi, INLINE_DENTRY);
503 break;
504 case Opt_flush_merge:
505 set_opt(sbi, FLUSH_MERGE);
506 break;
507 case Opt_noflush_merge:
508 clear_opt(sbi, FLUSH_MERGE);
509 break;
510 case Opt_nobarrier:
511 set_opt(sbi, NOBARRIER);
512 break;
513 case Opt_fastboot:
514 set_opt(sbi, FASTBOOT);
515 break;
516 case Opt_extent_cache:
517 set_opt(sbi, EXTENT_CACHE);
518 break;
519 case Opt_noextent_cache:
520 clear_opt(sbi, EXTENT_CACHE);
521 break;
522 case Opt_noinline_data:
523 clear_opt(sbi, INLINE_DATA);
524 break;
525 case Opt_data_flush:
526 set_opt(sbi, DATA_FLUSH);
527 break;
528 case Opt_reserve_root:
529 if (args->from && match_int(args, &arg))
530 return -EINVAL;
531 if (test_opt(sbi, RESERVE_ROOT)) {
532 f2fs_msg(sb, KERN_INFO,
533 "Preserve previous reserve_root=%u",
534 F2FS_OPTION(sbi).root_reserved_blocks);
535 } else {
536 F2FS_OPTION(sbi).root_reserved_blocks = arg;
537 set_opt(sbi, RESERVE_ROOT);
539 break;
540 case Opt_resuid:
541 if (args->from && match_int(args, &arg))
542 return -EINVAL;
543 uid = make_kuid(current_user_ns(), arg);
544 if (!uid_valid(uid)) {
545 f2fs_msg(sb, KERN_ERR,
546 "Invalid uid value %d", arg);
547 return -EINVAL;
549 F2FS_OPTION(sbi).s_resuid = uid;
550 break;
551 case Opt_resgid:
552 if (args->from && match_int(args, &arg))
553 return -EINVAL;
554 gid = make_kgid(current_user_ns(), arg);
555 if (!gid_valid(gid)) {
556 f2fs_msg(sb, KERN_ERR,
557 "Invalid gid value %d", arg);
558 return -EINVAL;
560 F2FS_OPTION(sbi).s_resgid = gid;
561 break;
562 case Opt_mode:
563 name = match_strdup(&args[0]);
565 if (!name)
566 return -ENOMEM;
567 if (strlen(name) == 8 &&
568 !strncmp(name, "adaptive", 8)) {
569 if (f2fs_sb_has_blkzoned(sb)) {
570 f2fs_msg(sb, KERN_WARNING,
571 "adaptive mode is not allowed with "
572 "zoned block device feature");
573 kfree(name);
574 return -EINVAL;
576 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
577 } else if (strlen(name) == 3 &&
578 !strncmp(name, "lfs", 3)) {
579 set_opt_mode(sbi, F2FS_MOUNT_LFS);
580 } else {
581 kfree(name);
582 return -EINVAL;
584 kfree(name);
585 break;
586 case Opt_io_size_bits:
587 if (args->from && match_int(args, &arg))
588 return -EINVAL;
589 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
590 f2fs_msg(sb, KERN_WARNING,
591 "Not support %d, larger than %d",
592 1 << arg, BIO_MAX_PAGES);
593 return -EINVAL;
595 F2FS_OPTION(sbi).write_io_size_bits = arg;
596 break;
597 case Opt_fault_injection:
598 if (args->from && match_int(args, &arg))
599 return -EINVAL;
600 #ifdef CONFIG_F2FS_FAULT_INJECTION
601 f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
602 set_opt(sbi, FAULT_INJECTION);
603 #else
604 f2fs_msg(sb, KERN_INFO,
605 "FAULT_INJECTION was not selected");
606 #endif
607 break;
608 case Opt_fault_type:
609 if (args->from && match_int(args, &arg))
610 return -EINVAL;
611 #ifdef CONFIG_F2FS_FAULT_INJECTION
612 f2fs_build_fault_attr(sbi, 0, arg);
613 set_opt(sbi, FAULT_INJECTION);
614 #else
615 f2fs_msg(sb, KERN_INFO,
616 "FAULT_INJECTION was not selected");
617 #endif
618 break;
619 case Opt_lazytime:
620 sb->s_flags |= SB_LAZYTIME;
621 break;
622 case Opt_nolazytime:
623 sb->s_flags &= ~SB_LAZYTIME;
624 break;
625 #ifdef CONFIG_QUOTA
626 case Opt_quota:
627 case Opt_usrquota:
628 set_opt(sbi, USRQUOTA);
629 break;
630 case Opt_grpquota:
631 set_opt(sbi, GRPQUOTA);
632 break;
633 case Opt_prjquota:
634 set_opt(sbi, PRJQUOTA);
635 break;
636 case Opt_usrjquota:
637 ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]);
638 if (ret)
639 return ret;
640 break;
641 case Opt_grpjquota:
642 ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]);
643 if (ret)
644 return ret;
645 break;
646 case Opt_prjjquota:
647 ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]);
648 if (ret)
649 return ret;
650 break;
651 case Opt_offusrjquota:
652 ret = f2fs_clear_qf_name(sb, USRQUOTA);
653 if (ret)
654 return ret;
655 break;
656 case Opt_offgrpjquota:
657 ret = f2fs_clear_qf_name(sb, GRPQUOTA);
658 if (ret)
659 return ret;
660 break;
661 case Opt_offprjjquota:
662 ret = f2fs_clear_qf_name(sb, PRJQUOTA);
663 if (ret)
664 return ret;
665 break;
666 case Opt_jqfmt_vfsold:
667 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_OLD;
668 break;
669 case Opt_jqfmt_vfsv0:
670 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V0;
671 break;
672 case Opt_jqfmt_vfsv1:
673 F2FS_OPTION(sbi).s_jquota_fmt = QFMT_VFS_V1;
674 break;
675 case Opt_noquota:
676 clear_opt(sbi, QUOTA);
677 clear_opt(sbi, USRQUOTA);
678 clear_opt(sbi, GRPQUOTA);
679 clear_opt(sbi, PRJQUOTA);
680 break;
681 #else
682 case Opt_quota:
683 case Opt_usrquota:
684 case Opt_grpquota:
685 case Opt_prjquota:
686 case Opt_usrjquota:
687 case Opt_grpjquota:
688 case Opt_prjjquota:
689 case Opt_offusrjquota:
690 case Opt_offgrpjquota:
691 case Opt_offprjjquota:
692 case Opt_jqfmt_vfsold:
693 case Opt_jqfmt_vfsv0:
694 case Opt_jqfmt_vfsv1:
695 case Opt_noquota:
696 f2fs_msg(sb, KERN_INFO,
697 "quota operations not supported");
698 break;
699 #endif
700 case Opt_whint:
701 name = match_strdup(&args[0]);
702 if (!name)
703 return -ENOMEM;
704 if (strlen(name) == 10 &&
705 !strncmp(name, "user-based", 10)) {
706 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_USER;
707 } else if (strlen(name) == 3 &&
708 !strncmp(name, "off", 3)) {
709 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
710 } else if (strlen(name) == 8 &&
711 !strncmp(name, "fs-based", 8)) {
712 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
713 } else {
714 kfree(name);
715 return -EINVAL;
717 kfree(name);
718 break;
719 case Opt_alloc:
720 name = match_strdup(&args[0]);
721 if (!name)
722 return -ENOMEM;
724 if (strlen(name) == 7 &&
725 !strncmp(name, "default", 7)) {
726 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
727 } else if (strlen(name) == 5 &&
728 !strncmp(name, "reuse", 5)) {
729 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
730 } else {
731 kfree(name);
732 return -EINVAL;
734 kfree(name);
735 break;
736 case Opt_fsync:
737 name = match_strdup(&args[0]);
738 if (!name)
739 return -ENOMEM;
740 if (strlen(name) == 5 &&
741 !strncmp(name, "posix", 5)) {
742 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
743 } else if (strlen(name) == 6 &&
744 !strncmp(name, "strict", 6)) {
745 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_STRICT;
746 } else if (strlen(name) == 9 &&
747 !strncmp(name, "nobarrier", 9)) {
748 F2FS_OPTION(sbi).fsync_mode =
749 FSYNC_MODE_NOBARRIER;
750 } else {
751 kfree(name);
752 return -EINVAL;
754 kfree(name);
755 break;
756 case Opt_test_dummy_encryption:
757 #ifdef CONFIG_F2FS_FS_ENCRYPTION
758 if (!f2fs_sb_has_encrypt(sb)) {
759 f2fs_msg(sb, KERN_ERR, "Encrypt feature is off");
760 return -EINVAL;
763 F2FS_OPTION(sbi).test_dummy_encryption = true;
764 f2fs_msg(sb, KERN_INFO,
765 "Test dummy encryption mode enabled");
766 #else
767 f2fs_msg(sb, KERN_INFO,
768 "Test dummy encryption mount option ignored");
769 #endif
770 break;
771 default:
772 f2fs_msg(sb, KERN_ERR,
773 "Unrecognized mount option \"%s\" or missing value",
775 return -EINVAL;
778 #ifdef CONFIG_QUOTA
779 if (f2fs_check_quota_options(sbi))
780 return -EINVAL;
781 #else
782 if (f2fs_sb_has_quota_ino(sbi->sb) && !f2fs_readonly(sbi->sb)) {
783 f2fs_msg(sbi->sb, KERN_INFO,
784 "Filesystem with quota feature cannot be mounted RDWR "
785 "without CONFIG_QUOTA");
786 return -EINVAL;
788 if (f2fs_sb_has_project_quota(sbi->sb) && !f2fs_readonly(sbi->sb)) {
789 f2fs_msg(sb, KERN_ERR,
790 "Filesystem with project quota feature cannot be "
791 "mounted RDWR without CONFIG_QUOTA");
792 return -EINVAL;
794 #endif
796 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
797 f2fs_msg(sb, KERN_ERR,
798 "Should set mode=lfs with %uKB-sized IO",
799 F2FS_IO_SIZE_KB(sbi));
800 return -EINVAL;
803 if (test_opt(sbi, INLINE_XATTR_SIZE)) {
804 if (!f2fs_sb_has_extra_attr(sb) ||
805 !f2fs_sb_has_flexible_inline_xattr(sb)) {
806 f2fs_msg(sb, KERN_ERR,
807 "extra_attr or flexible_inline_xattr "
808 "feature is off");
809 return -EINVAL;
811 if (!test_opt(sbi, INLINE_XATTR)) {
812 f2fs_msg(sb, KERN_ERR,
813 "inline_xattr_size option should be "
814 "set with inline_xattr option");
815 return -EINVAL;
817 if (F2FS_OPTION(sbi).inline_xattr_size <
818 sizeof(struct f2fs_xattr_header) / sizeof(__le32) ||
819 F2FS_OPTION(sbi).inline_xattr_size >
820 DEF_ADDRS_PER_INODE -
821 F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -
822 DEF_INLINE_RESERVED_SIZE -
823 MIN_INLINE_DENTRY_SIZE / sizeof(__le32)) {
824 f2fs_msg(sb, KERN_ERR,
825 "inline xattr size is out of range");
826 return -EINVAL;
830 /* Not pass down write hints if the number of active logs is lesser
831 * than NR_CURSEG_TYPE.
833 if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
834 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
835 return 0;
838 static struct inode *f2fs_alloc_inode(struct super_block *sb)
840 struct f2fs_inode_info *fi;
842 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
843 if (!fi)
844 return NULL;
846 init_once((void *) fi);
848 /* Initialize f2fs-specific inode info */
849 atomic_set(&fi->dirty_pages, 0);
850 init_rwsem(&fi->i_sem);
851 INIT_LIST_HEAD(&fi->dirty_list);
852 INIT_LIST_HEAD(&fi->gdirty_list);
853 INIT_LIST_HEAD(&fi->inmem_ilist);
854 INIT_LIST_HEAD(&fi->inmem_pages);
855 mutex_init(&fi->inmem_lock);
856 init_rwsem(&fi->i_gc_rwsem[READ]);
857 init_rwsem(&fi->i_gc_rwsem[WRITE]);
858 init_rwsem(&fi->i_mmap_sem);
859 init_rwsem(&fi->i_xattr_sem);
861 /* Will be used by directory only */
862 fi->i_dir_level = F2FS_SB(sb)->dir_level;
864 return &fi->vfs_inode;
867 static int f2fs_drop_inode(struct inode *inode)
869 int ret;
871 * This is to avoid a deadlock condition like below.
872 * writeback_single_inode(inode)
873 * - f2fs_write_data_page
874 * - f2fs_gc -> iput -> evict
875 * - inode_wait_for_writeback(inode)
877 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
878 if (!inode->i_nlink && !is_bad_inode(inode)) {
879 /* to avoid evict_inode call simultaneously */
880 atomic_inc(&inode->i_count);
881 spin_unlock(&inode->i_lock);
883 /* some remained atomic pages should discarded */
884 if (f2fs_is_atomic_file(inode))
885 f2fs_drop_inmem_pages(inode);
887 /* should remain fi->extent_tree for writepage */
888 f2fs_destroy_extent_node(inode);
890 sb_start_intwrite(inode->i_sb);
891 f2fs_i_size_write(inode, 0);
893 if (F2FS_HAS_BLOCKS(inode))
894 f2fs_truncate(inode);
896 sb_end_intwrite(inode->i_sb);
898 spin_lock(&inode->i_lock);
899 atomic_dec(&inode->i_count);
901 trace_f2fs_drop_inode(inode, 0);
902 return 0;
904 ret = generic_drop_inode(inode);
905 trace_f2fs_drop_inode(inode, ret);
906 return ret;
909 int f2fs_inode_dirtied(struct inode *inode, bool sync)
911 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
912 int ret = 0;
914 spin_lock(&sbi->inode_lock[DIRTY_META]);
915 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
916 ret = 1;
917 } else {
918 set_inode_flag(inode, FI_DIRTY_INODE);
919 stat_inc_dirty_inode(sbi, DIRTY_META);
921 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
922 list_add_tail(&F2FS_I(inode)->gdirty_list,
923 &sbi->inode_list[DIRTY_META]);
924 inc_page_count(sbi, F2FS_DIRTY_IMETA);
926 spin_unlock(&sbi->inode_lock[DIRTY_META]);
927 return ret;
930 void f2fs_inode_synced(struct inode *inode)
932 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
934 spin_lock(&sbi->inode_lock[DIRTY_META]);
935 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
936 spin_unlock(&sbi->inode_lock[DIRTY_META]);
937 return;
939 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
940 list_del_init(&F2FS_I(inode)->gdirty_list);
941 dec_page_count(sbi, F2FS_DIRTY_IMETA);
943 clear_inode_flag(inode, FI_DIRTY_INODE);
944 clear_inode_flag(inode, FI_AUTO_RECOVER);
945 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
946 spin_unlock(&sbi->inode_lock[DIRTY_META]);
950 * f2fs_dirty_inode() is called from __mark_inode_dirty()
952 * We should call set_dirty_inode to write the dirty inode through write_inode.
954 static void f2fs_dirty_inode(struct inode *inode, int flags)
956 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
958 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
959 inode->i_ino == F2FS_META_INO(sbi))
960 return;
962 if (flags == I_DIRTY_TIME)
963 return;
965 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
966 clear_inode_flag(inode, FI_AUTO_RECOVER);
968 f2fs_inode_dirtied(inode, false);
971 static void f2fs_i_callback(struct rcu_head *head)
973 struct inode *inode = container_of(head, struct inode, i_rcu);
974 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
977 static void f2fs_destroy_inode(struct inode *inode)
979 call_rcu(&inode->i_rcu, f2fs_i_callback);
982 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
984 percpu_counter_destroy(&sbi->alloc_valid_block_count);
985 percpu_counter_destroy(&sbi->total_valid_inode_count);
988 static void destroy_device_list(struct f2fs_sb_info *sbi)
990 int i;
992 for (i = 0; i < sbi->s_ndevs; i++) {
993 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
994 #ifdef CONFIG_BLK_DEV_ZONED
995 kfree(FDEV(i).blkz_type);
996 #endif
998 kfree(sbi->devs);
1001 static void f2fs_put_super(struct super_block *sb)
1003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1004 int i;
1005 bool dropped;
1007 f2fs_quota_off_umount(sb);
1009 /* prevent remaining shrinker jobs */
1010 mutex_lock(&sbi->umount_mutex);
1013 * We don't need to do checkpoint when superblock is clean.
1014 * But, the previous checkpoint was not done by umount, it needs to do
1015 * clean checkpoint again.
1017 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
1018 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
1019 struct cp_control cpc = {
1020 .reason = CP_UMOUNT,
1022 f2fs_write_checkpoint(sbi, &cpc);
1025 /* be sure to wait for any on-going discard commands */
1026 dropped = f2fs_wait_discard_bios(sbi);
1028 if ((f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) &&
1029 !sbi->discard_blks && !dropped) {
1030 struct cp_control cpc = {
1031 .reason = CP_UMOUNT | CP_TRIMMED,
1033 f2fs_write_checkpoint(sbi, &cpc);
1037 * normally superblock is clean, so we need to release this.
1038 * In addition, EIO will skip do checkpoint, we need this as well.
1040 f2fs_release_ino_entry(sbi, true);
1042 f2fs_leave_shrinker(sbi);
1043 mutex_unlock(&sbi->umount_mutex);
1045 /* our cp_error case, we can wait for any writeback page */
1046 f2fs_flush_merged_writes(sbi);
1048 f2fs_wait_on_all_pages_writeback(sbi);
1050 f2fs_bug_on(sbi, sbi->fsync_node_num);
1052 iput(sbi->node_inode);
1053 iput(sbi->meta_inode);
1056 * iput() can update stat information, if f2fs_write_checkpoint()
1057 * above failed with error.
1059 f2fs_destroy_stats(sbi);
1061 /* destroy f2fs internal modules */
1062 f2fs_destroy_node_manager(sbi);
1063 f2fs_destroy_segment_manager(sbi);
1065 kfree(sbi->ckpt);
1067 f2fs_unregister_sysfs(sbi);
1069 sb->s_fs_info = NULL;
1070 if (sbi->s_chksum_driver)
1071 crypto_free_shash(sbi->s_chksum_driver);
1072 kfree(sbi->raw_super);
1074 destroy_device_list(sbi);
1075 mempool_destroy(sbi->write_io_dummy);
1076 #ifdef CONFIG_QUOTA
1077 for (i = 0; i < MAXQUOTAS; i++)
1078 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1079 #endif
1080 destroy_percpu_info(sbi);
1081 for (i = 0; i < NR_PAGE_TYPE; i++)
1082 kfree(sbi->write_io[i]);
1083 kfree(sbi);
1086 int f2fs_sync_fs(struct super_block *sb, int sync)
1088 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1089 int err = 0;
1091 if (unlikely(f2fs_cp_error(sbi)))
1092 return 0;
1094 trace_f2fs_sync_fs(sb, sync);
1096 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1097 return -EAGAIN;
1099 if (sync) {
1100 struct cp_control cpc;
1102 cpc.reason = __get_cp_reason(sbi);
1104 mutex_lock(&sbi->gc_mutex);
1105 err = f2fs_write_checkpoint(sbi, &cpc);
1106 mutex_unlock(&sbi->gc_mutex);
1108 f2fs_trace_ios(NULL, 1);
1110 return err;
1113 static int f2fs_freeze(struct super_block *sb)
1115 if (f2fs_readonly(sb))
1116 return 0;
1118 /* IO error happened before */
1119 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
1120 return -EIO;
1122 /* must be clean, since sync_filesystem() was already called */
1123 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
1124 return -EINVAL;
1125 return 0;
1128 static int f2fs_unfreeze(struct super_block *sb)
1130 return 0;
1133 #ifdef CONFIG_QUOTA
1134 static int f2fs_statfs_project(struct super_block *sb,
1135 kprojid_t projid, struct kstatfs *buf)
1137 struct kqid qid;
1138 struct dquot *dquot;
1139 u64 limit;
1140 u64 curblock;
1142 qid = make_kqid_projid(projid);
1143 dquot = dqget(sb, qid);
1144 if (IS_ERR(dquot))
1145 return PTR_ERR(dquot);
1146 spin_lock(&dquot->dq_dqb_lock);
1148 limit = (dquot->dq_dqb.dqb_bsoftlimit ?
1149 dquot->dq_dqb.dqb_bsoftlimit :
1150 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
1151 if (limit && buf->f_blocks > limit) {
1152 curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
1153 buf->f_blocks = limit;
1154 buf->f_bfree = buf->f_bavail =
1155 (buf->f_blocks > curblock) ?
1156 (buf->f_blocks - curblock) : 0;
1159 limit = dquot->dq_dqb.dqb_isoftlimit ?
1160 dquot->dq_dqb.dqb_isoftlimit :
1161 dquot->dq_dqb.dqb_ihardlimit;
1162 if (limit && buf->f_files > limit) {
1163 buf->f_files = limit;
1164 buf->f_ffree =
1165 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
1166 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
1169 spin_unlock(&dquot->dq_dqb_lock);
1170 dqput(dquot);
1171 return 0;
1173 #endif
1175 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
1177 struct super_block *sb = dentry->d_sb;
1178 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1179 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1180 block_t total_count, user_block_count, start_count;
1181 u64 avail_node_count;
1183 total_count = le64_to_cpu(sbi->raw_super->block_count);
1184 user_block_count = sbi->user_block_count;
1185 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
1186 buf->f_type = F2FS_SUPER_MAGIC;
1187 buf->f_bsize = sbi->blocksize;
1189 buf->f_blocks = total_count - start_count;
1190 buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
1191 sbi->current_reserved_blocks;
1192 if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
1193 buf->f_bavail = buf->f_bfree -
1194 F2FS_OPTION(sbi).root_reserved_blocks;
1195 else
1196 buf->f_bavail = 0;
1198 avail_node_count = sbi->total_node_count - sbi->nquota_files -
1199 F2FS_RESERVED_NODE_NUM;
1201 if (avail_node_count > user_block_count) {
1202 buf->f_files = user_block_count;
1203 buf->f_ffree = buf->f_bavail;
1204 } else {
1205 buf->f_files = avail_node_count;
1206 buf->f_ffree = min(avail_node_count - valid_node_count(sbi),
1207 buf->f_bavail);
1210 buf->f_namelen = F2FS_NAME_LEN;
1211 buf->f_fsid.val[0] = (u32)id;
1212 buf->f_fsid.val[1] = (u32)(id >> 32);
1214 #ifdef CONFIG_QUOTA
1215 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
1216 sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
1217 f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf);
1219 #endif
1220 return 0;
1223 static inline void f2fs_show_quota_options(struct seq_file *seq,
1224 struct super_block *sb)
1226 #ifdef CONFIG_QUOTA
1227 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1229 if (F2FS_OPTION(sbi).s_jquota_fmt) {
1230 char *fmtname = "";
1232 switch (F2FS_OPTION(sbi).s_jquota_fmt) {
1233 case QFMT_VFS_OLD:
1234 fmtname = "vfsold";
1235 break;
1236 case QFMT_VFS_V0:
1237 fmtname = "vfsv0";
1238 break;
1239 case QFMT_VFS_V1:
1240 fmtname = "vfsv1";
1241 break;
1243 seq_printf(seq, ",jqfmt=%s", fmtname);
1246 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
1247 seq_show_option(seq, "usrjquota",
1248 F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
1250 if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
1251 seq_show_option(seq, "grpjquota",
1252 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
1254 if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
1255 seq_show_option(seq, "prjjquota",
1256 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
1257 #endif
1260 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
1262 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
1264 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
1265 if (test_opt(sbi, FORCE_FG_GC))
1266 seq_printf(seq, ",background_gc=%s", "sync");
1267 else
1268 seq_printf(seq, ",background_gc=%s", "on");
1269 } else {
1270 seq_printf(seq, ",background_gc=%s", "off");
1272 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
1273 seq_puts(seq, ",disable_roll_forward");
1274 if (test_opt(sbi, DISCARD))
1275 seq_puts(seq, ",discard");
1276 if (test_opt(sbi, NOHEAP))
1277 seq_puts(seq, ",no_heap");
1278 else
1279 seq_puts(seq, ",heap");
1280 #ifdef CONFIG_F2FS_FS_XATTR
1281 if (test_opt(sbi, XATTR_USER))
1282 seq_puts(seq, ",user_xattr");
1283 else
1284 seq_puts(seq, ",nouser_xattr");
1285 if (test_opt(sbi, INLINE_XATTR))
1286 seq_puts(seq, ",inline_xattr");
1287 else
1288 seq_puts(seq, ",noinline_xattr");
1289 if (test_opt(sbi, INLINE_XATTR_SIZE))
1290 seq_printf(seq, ",inline_xattr_size=%u",
1291 F2FS_OPTION(sbi).inline_xattr_size);
1292 #endif
1293 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1294 if (test_opt(sbi, POSIX_ACL))
1295 seq_puts(seq, ",acl");
1296 else
1297 seq_puts(seq, ",noacl");
1298 #endif
1299 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
1300 seq_puts(seq, ",disable_ext_identify");
1301 if (test_opt(sbi, INLINE_DATA))
1302 seq_puts(seq, ",inline_data");
1303 else
1304 seq_puts(seq, ",noinline_data");
1305 if (test_opt(sbi, INLINE_DENTRY))
1306 seq_puts(seq, ",inline_dentry");
1307 else
1308 seq_puts(seq, ",noinline_dentry");
1309 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
1310 seq_puts(seq, ",flush_merge");
1311 if (test_opt(sbi, NOBARRIER))
1312 seq_puts(seq, ",nobarrier");
1313 if (test_opt(sbi, FASTBOOT))
1314 seq_puts(seq, ",fastboot");
1315 if (test_opt(sbi, EXTENT_CACHE))
1316 seq_puts(seq, ",extent_cache");
1317 else
1318 seq_puts(seq, ",noextent_cache");
1319 if (test_opt(sbi, DATA_FLUSH))
1320 seq_puts(seq, ",data_flush");
1322 seq_puts(seq, ",mode=");
1323 if (test_opt(sbi, ADAPTIVE))
1324 seq_puts(seq, "adaptive");
1325 else if (test_opt(sbi, LFS))
1326 seq_puts(seq, "lfs");
1327 seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
1328 if (test_opt(sbi, RESERVE_ROOT))
1329 seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
1330 F2FS_OPTION(sbi).root_reserved_blocks,
1331 from_kuid_munged(&init_user_ns,
1332 F2FS_OPTION(sbi).s_resuid),
1333 from_kgid_munged(&init_user_ns,
1334 F2FS_OPTION(sbi).s_resgid));
1335 if (F2FS_IO_SIZE_BITS(sbi))
1336 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
1337 #ifdef CONFIG_F2FS_FAULT_INJECTION
1338 if (test_opt(sbi, FAULT_INJECTION)) {
1339 seq_printf(seq, ",fault_injection=%u",
1340 F2FS_OPTION(sbi).fault_info.inject_rate);
1341 seq_printf(seq, ",fault_type=%u",
1342 F2FS_OPTION(sbi).fault_info.inject_type);
1344 #endif
1345 #ifdef CONFIG_QUOTA
1346 if (test_opt(sbi, QUOTA))
1347 seq_puts(seq, ",quota");
1348 if (test_opt(sbi, USRQUOTA))
1349 seq_puts(seq, ",usrquota");
1350 if (test_opt(sbi, GRPQUOTA))
1351 seq_puts(seq, ",grpquota");
1352 if (test_opt(sbi, PRJQUOTA))
1353 seq_puts(seq, ",prjquota");
1354 #endif
1355 f2fs_show_quota_options(seq, sbi->sb);
1356 if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER)
1357 seq_printf(seq, ",whint_mode=%s", "user-based");
1358 else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS)
1359 seq_printf(seq, ",whint_mode=%s", "fs-based");
1360 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1361 if (F2FS_OPTION(sbi).test_dummy_encryption)
1362 seq_puts(seq, ",test_dummy_encryption");
1363 #endif
1365 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
1366 seq_printf(seq, ",alloc_mode=%s", "default");
1367 else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
1368 seq_printf(seq, ",alloc_mode=%s", "reuse");
1370 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
1371 seq_printf(seq, ",fsync_mode=%s", "posix");
1372 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
1373 seq_printf(seq, ",fsync_mode=%s", "strict");
1374 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
1375 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
1376 return 0;
1379 static void default_options(struct f2fs_sb_info *sbi)
1381 /* init some FS parameters */
1382 F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1383 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
1384 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1385 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
1386 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1387 F2FS_OPTION(sbi).test_dummy_encryption = false;
1388 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
1389 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
1391 set_opt(sbi, BG_GC);
1392 set_opt(sbi, INLINE_XATTR);
1393 set_opt(sbi, INLINE_DATA);
1394 set_opt(sbi, INLINE_DENTRY);
1395 set_opt(sbi, EXTENT_CACHE);
1396 set_opt(sbi, NOHEAP);
1397 sbi->sb->s_flags |= SB_LAZYTIME;
1398 set_opt(sbi, FLUSH_MERGE);
1399 set_opt(sbi, DISCARD);
1400 if (f2fs_sb_has_blkzoned(sbi->sb))
1401 set_opt_mode(sbi, F2FS_MOUNT_LFS);
1402 else
1403 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
1405 #ifdef CONFIG_F2FS_FS_XATTR
1406 set_opt(sbi, XATTR_USER);
1407 #endif
1408 #ifdef CONFIG_F2FS_FS_POSIX_ACL
1409 set_opt(sbi, POSIX_ACL);
1410 #endif
1412 f2fs_build_fault_attr(sbi, 0, 0);
1415 #ifdef CONFIG_QUOTA
1416 static int f2fs_enable_quotas(struct super_block *sb);
1417 #endif
1418 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
1420 struct f2fs_sb_info *sbi = F2FS_SB(sb);
1421 struct f2fs_mount_info org_mount_opt;
1422 unsigned long old_sb_flags;
1423 int err;
1424 bool need_restart_gc = false;
1425 bool need_stop_gc = false;
1426 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
1427 #ifdef CONFIG_QUOTA
1428 int i, j;
1429 #endif
1432 * Save the old mount options in case we
1433 * need to restore them.
1435 org_mount_opt = sbi->mount_opt;
1436 old_sb_flags = sb->s_flags;
1438 #ifdef CONFIG_QUOTA
1439 org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
1440 for (i = 0; i < MAXQUOTAS; i++) {
1441 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1442 org_mount_opt.s_qf_names[i] =
1443 kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
1444 GFP_KERNEL);
1445 if (!org_mount_opt.s_qf_names[i]) {
1446 for (j = 0; j < i; j++)
1447 kfree(org_mount_opt.s_qf_names[j]);
1448 return -ENOMEM;
1450 } else {
1451 org_mount_opt.s_qf_names[i] = NULL;
1454 #endif
1456 /* recover superblocks we couldn't write due to previous RO mount */
1457 if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
1458 err = f2fs_commit_super(sbi, false);
1459 f2fs_msg(sb, KERN_INFO,
1460 "Try to recover all the superblocks, ret: %d", err);
1461 if (!err)
1462 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1465 default_options(sbi);
1467 /* parse mount options */
1468 err = parse_options(sb, data);
1469 if (err)
1470 goto restore_opts;
1473 * Previous and new state of filesystem is RO,
1474 * so skip checking GC and FLUSH_MERGE conditions.
1476 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
1477 goto skip;
1479 #ifdef CONFIG_QUOTA
1480 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
1481 err = dquot_suspend(sb, -1);
1482 if (err < 0)
1483 goto restore_opts;
1484 } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) {
1485 /* dquot_resume needs RW */
1486 sb->s_flags &= ~SB_RDONLY;
1487 if (sb_any_quota_suspended(sb)) {
1488 dquot_resume(sb, -1);
1489 } else if (f2fs_sb_has_quota_ino(sb)) {
1490 err = f2fs_enable_quotas(sb);
1491 if (err)
1492 goto restore_opts;
1495 #endif
1496 /* disallow enable/disable extent_cache dynamically */
1497 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
1498 err = -EINVAL;
1499 f2fs_msg(sbi->sb, KERN_WARNING,
1500 "switch extent_cache option is not allowed");
1501 goto restore_opts;
1505 * We stop the GC thread if FS is mounted as RO
1506 * or if background_gc = off is passed in mount
1507 * option. Also sync the filesystem.
1509 if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) {
1510 if (sbi->gc_thread) {
1511 f2fs_stop_gc_thread(sbi);
1512 need_restart_gc = true;
1514 } else if (!sbi->gc_thread) {
1515 err = f2fs_start_gc_thread(sbi);
1516 if (err)
1517 goto restore_opts;
1518 need_stop_gc = true;
1521 if (*flags & SB_RDONLY ||
1522 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1523 writeback_inodes_sb(sb, WB_REASON_SYNC);
1524 sync_inodes_sb(sb);
1526 set_sbi_flag(sbi, SBI_IS_DIRTY);
1527 set_sbi_flag(sbi, SBI_IS_CLOSE);
1528 f2fs_sync_fs(sb, 1);
1529 clear_sbi_flag(sbi, SBI_IS_CLOSE);
1533 * We stop issue flush thread if FS is mounted as RO
1534 * or if flush_merge is not passed in mount option.
1536 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
1537 clear_opt(sbi, FLUSH_MERGE);
1538 f2fs_destroy_flush_cmd_control(sbi, false);
1539 } else {
1540 err = f2fs_create_flush_cmd_control(sbi);
1541 if (err)
1542 goto restore_gc;
1544 skip:
1545 #ifdef CONFIG_QUOTA
1546 /* Release old quota file names */
1547 for (i = 0; i < MAXQUOTAS; i++)
1548 kfree(org_mount_opt.s_qf_names[i]);
1549 #endif
1550 /* Update the POSIXACL Flag */
1551 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1552 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
1554 limit_reserve_root(sbi);
1555 return 0;
1556 restore_gc:
1557 if (need_restart_gc) {
1558 if (f2fs_start_gc_thread(sbi))
1559 f2fs_msg(sbi->sb, KERN_WARNING,
1560 "background gc thread has stopped");
1561 } else if (need_stop_gc) {
1562 f2fs_stop_gc_thread(sbi);
1564 restore_opts:
1565 #ifdef CONFIG_QUOTA
1566 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
1567 for (i = 0; i < MAXQUOTAS; i++) {
1568 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
1569 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
1571 #endif
1572 sbi->mount_opt = org_mount_opt;
1573 sb->s_flags = old_sb_flags;
1574 return err;
1577 #ifdef CONFIG_QUOTA
1578 /* Read data from quotafile */
1579 static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
1580 size_t len, loff_t off)
1582 struct inode *inode = sb_dqopt(sb)->files[type];
1583 struct address_space *mapping = inode->i_mapping;
1584 block_t blkidx = F2FS_BYTES_TO_BLK(off);
1585 int offset = off & (sb->s_blocksize - 1);
1586 int tocopy;
1587 size_t toread;
1588 loff_t i_size = i_size_read(inode);
1589 struct page *page;
1590 char *kaddr;
1592 if (off > i_size)
1593 return 0;
1595 if (off + len > i_size)
1596 len = i_size - off;
1597 toread = len;
1598 while (toread > 0) {
1599 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1600 repeat:
1601 page = read_cache_page_gfp(mapping, blkidx, GFP_NOFS);
1602 if (IS_ERR(page)) {
1603 if (PTR_ERR(page) == -ENOMEM) {
1604 congestion_wait(BLK_RW_ASYNC, HZ/50);
1605 goto repeat;
1607 return PTR_ERR(page);
1610 lock_page(page);
1612 if (unlikely(page->mapping != mapping)) {
1613 f2fs_put_page(page, 1);
1614 goto repeat;
1616 if (unlikely(!PageUptodate(page))) {
1617 f2fs_put_page(page, 1);
1618 return -EIO;
1621 kaddr = kmap_atomic(page);
1622 memcpy(data, kaddr + offset, tocopy);
1623 kunmap_atomic(kaddr);
1624 f2fs_put_page(page, 1);
1626 offset = 0;
1627 toread -= tocopy;
1628 data += tocopy;
1629 blkidx++;
1631 return len;
1634 /* Write to quotafile */
1635 static ssize_t f2fs_quota_write(struct super_block *sb, int type,
1636 const char *data, size_t len, loff_t off)
1638 struct inode *inode = sb_dqopt(sb)->files[type];
1639 struct address_space *mapping = inode->i_mapping;
1640 const struct address_space_operations *a_ops = mapping->a_ops;
1641 int offset = off & (sb->s_blocksize - 1);
1642 size_t towrite = len;
1643 struct page *page;
1644 char *kaddr;
1645 int err = 0;
1646 int tocopy;
1648 while (towrite > 0) {
1649 tocopy = min_t(unsigned long, sb->s_blocksize - offset,
1650 towrite);
1651 retry:
1652 err = a_ops->write_begin(NULL, mapping, off, tocopy, 0,
1653 &page, NULL);
1654 if (unlikely(err)) {
1655 if (err == -ENOMEM) {
1656 congestion_wait(BLK_RW_ASYNC, HZ/50);
1657 goto retry;
1659 break;
1662 kaddr = kmap_atomic(page);
1663 memcpy(kaddr + offset, data, tocopy);
1664 kunmap_atomic(kaddr);
1665 flush_dcache_page(page);
1667 a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
1668 page, NULL);
1669 offset = 0;
1670 towrite -= tocopy;
1671 off += tocopy;
1672 data += tocopy;
1673 cond_resched();
1676 if (len == towrite)
1677 return err;
1678 inode->i_mtime = inode->i_ctime = current_time(inode);
1679 f2fs_mark_inode_dirty_sync(inode, false);
1680 return len - towrite;
1683 static struct dquot **f2fs_get_dquots(struct inode *inode)
1685 return F2FS_I(inode)->i_dquot;
1688 static qsize_t *f2fs_get_reserved_space(struct inode *inode)
1690 return &F2FS_I(inode)->i_reserved_quota;
1693 static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
1695 return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
1696 F2FS_OPTION(sbi).s_jquota_fmt, type);
1699 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
1701 int enabled = 0;
1702 int i, err;
1704 if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) {
1705 err = f2fs_enable_quotas(sbi->sb);
1706 if (err) {
1707 f2fs_msg(sbi->sb, KERN_ERR,
1708 "Cannot turn on quota_ino: %d", err);
1709 return 0;
1711 return 1;
1714 for (i = 0; i < MAXQUOTAS; i++) {
1715 if (F2FS_OPTION(sbi).s_qf_names[i]) {
1716 err = f2fs_quota_on_mount(sbi, i);
1717 if (!err) {
1718 enabled = 1;
1719 continue;
1721 f2fs_msg(sbi->sb, KERN_ERR,
1722 "Cannot turn on quotas: %d on %d", err, i);
1725 return enabled;
1728 static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
1729 unsigned int flags)
1731 struct inode *qf_inode;
1732 unsigned long qf_inum;
1733 int err;
1735 BUG_ON(!f2fs_sb_has_quota_ino(sb));
1737 qf_inum = f2fs_qf_ino(sb, type);
1738 if (!qf_inum)
1739 return -EPERM;
1741 qf_inode = f2fs_iget(sb, qf_inum);
1742 if (IS_ERR(qf_inode)) {
1743 f2fs_msg(sb, KERN_ERR,
1744 "Bad quota inode %u:%lu", type, qf_inum);
1745 return PTR_ERR(qf_inode);
1748 /* Don't account quota for quota files to avoid recursion */
1749 qf_inode->i_flags |= S_NOQUOTA;
1750 err = dquot_enable(qf_inode, type, format_id, flags);
1751 iput(qf_inode);
1752 return err;
1755 static int f2fs_enable_quotas(struct super_block *sb)
1757 int type, err = 0;
1758 unsigned long qf_inum;
1759 bool quota_mopt[MAXQUOTAS] = {
1760 test_opt(F2FS_SB(sb), USRQUOTA),
1761 test_opt(F2FS_SB(sb), GRPQUOTA),
1762 test_opt(F2FS_SB(sb), PRJQUOTA),
1765 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
1766 for (type = 0; type < MAXQUOTAS; type++) {
1767 qf_inum = f2fs_qf_ino(sb, type);
1768 if (qf_inum) {
1769 err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
1770 DQUOT_USAGE_ENABLED |
1771 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
1772 if (err) {
1773 f2fs_msg(sb, KERN_ERR,
1774 "Failed to enable quota tracking "
1775 "(type=%d, err=%d). Please run "
1776 "fsck to fix.", type, err);
1777 for (type--; type >= 0; type--)
1778 dquot_quota_off(sb, type);
1779 return err;
1783 return 0;
1786 static int f2fs_quota_sync(struct super_block *sb, int type)
1788 struct quota_info *dqopt = sb_dqopt(sb);
1789 int cnt;
1790 int ret;
1792 ret = dquot_writeback_dquots(sb, type);
1793 if (ret)
1794 return ret;
1797 * Now when everything is written we can discard the pagecache so
1798 * that userspace sees the changes.
1800 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1801 if (type != -1 && cnt != type)
1802 continue;
1803 if (!sb_has_quota_active(sb, cnt))
1804 continue;
1806 ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping);
1807 if (ret)
1808 return ret;
1810 inode_lock(dqopt->files[cnt]);
1811 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
1812 inode_unlock(dqopt->files[cnt]);
1814 return 0;
1817 static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
1818 const struct path *path)
1820 struct inode *inode;
1821 int err;
1823 err = f2fs_quota_sync(sb, type);
1824 if (err)
1825 return err;
1827 err = dquot_quota_on(sb, type, format_id, path);
1828 if (err)
1829 return err;
1831 inode = d_inode(path->dentry);
1833 inode_lock(inode);
1834 F2FS_I(inode)->i_flags |= F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL;
1835 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1836 S_NOATIME | S_IMMUTABLE);
1837 inode_unlock(inode);
1838 f2fs_mark_inode_dirty_sync(inode, false);
1840 return 0;
1843 static int f2fs_quota_off(struct super_block *sb, int type)
1845 struct inode *inode = sb_dqopt(sb)->files[type];
1846 int err;
1848 if (!inode || !igrab(inode))
1849 return dquot_quota_off(sb, type);
1851 err = f2fs_quota_sync(sb, type);
1852 if (err)
1853 goto out_put;
1855 err = dquot_quota_off(sb, type);
1856 if (err || f2fs_sb_has_quota_ino(sb))
1857 goto out_put;
1859 inode_lock(inode);
1860 F2FS_I(inode)->i_flags &= ~(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL);
1861 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1862 inode_unlock(inode);
1863 f2fs_mark_inode_dirty_sync(inode, false);
1864 out_put:
1865 iput(inode);
1866 return err;
1869 void f2fs_quota_off_umount(struct super_block *sb)
1871 int type;
1872 int err;
1874 for (type = 0; type < MAXQUOTAS; type++) {
1875 err = f2fs_quota_off(sb, type);
1876 if (err) {
1877 int ret = dquot_quota_off(sb, type);
1879 f2fs_msg(sb, KERN_ERR,
1880 "Fail to turn off disk quota "
1881 "(type: %d, err: %d, ret:%d), Please "
1882 "run fsck to fix it.", type, err, ret);
1883 set_sbi_flag(F2FS_SB(sb), SBI_NEED_FSCK);
1888 static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
1890 struct quota_info *dqopt = sb_dqopt(sb);
1891 int type;
1893 for (type = 0; type < MAXQUOTAS; type++) {
1894 if (!dqopt->files[type])
1895 continue;
1896 f2fs_inode_synced(dqopt->files[type]);
1901 static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
1903 *projid = F2FS_I(inode)->i_projid;
1904 return 0;
1907 static const struct dquot_operations f2fs_quota_operations = {
1908 .get_reserved_space = f2fs_get_reserved_space,
1909 .write_dquot = dquot_commit,
1910 .acquire_dquot = dquot_acquire,
1911 .release_dquot = dquot_release,
1912 .mark_dirty = dquot_mark_dquot_dirty,
1913 .write_info = dquot_commit_info,
1914 .alloc_dquot = dquot_alloc,
1915 .destroy_dquot = dquot_destroy,
1916 .get_projid = f2fs_get_projid,
1917 .get_next_id = dquot_get_next_id,
1920 static const struct quotactl_ops f2fs_quotactl_ops = {
1921 .quota_on = f2fs_quota_on,
1922 .quota_off = f2fs_quota_off,
1923 .quota_sync = f2fs_quota_sync,
1924 .get_state = dquot_get_state,
1925 .set_info = dquot_set_dqinfo,
1926 .get_dqblk = dquot_get_dqblk,
1927 .set_dqblk = dquot_set_dqblk,
1928 .get_nextdqblk = dquot_get_next_dqblk,
1930 #else
1931 void f2fs_quota_off_umount(struct super_block *sb)
1934 #endif
1936 static const struct super_operations f2fs_sops = {
1937 .alloc_inode = f2fs_alloc_inode,
1938 .drop_inode = f2fs_drop_inode,
1939 .destroy_inode = f2fs_destroy_inode,
1940 .write_inode = f2fs_write_inode,
1941 .dirty_inode = f2fs_dirty_inode,
1942 .show_options = f2fs_show_options,
1943 #ifdef CONFIG_QUOTA
1944 .quota_read = f2fs_quota_read,
1945 .quota_write = f2fs_quota_write,
1946 .get_dquots = f2fs_get_dquots,
1947 #endif
1948 .evict_inode = f2fs_evict_inode,
1949 .put_super = f2fs_put_super,
1950 .sync_fs = f2fs_sync_fs,
1951 .freeze_fs = f2fs_freeze,
1952 .unfreeze_fs = f2fs_unfreeze,
1953 .statfs = f2fs_statfs,
1954 .remount_fs = f2fs_remount,
1957 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1958 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
1960 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1961 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1962 ctx, len, NULL);
1965 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
1966 void *fs_data)
1968 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1971 * Encrypting the root directory is not allowed because fsck
1972 * expects lost+found directory to exist and remain unencrypted
1973 * if LOST_FOUND feature is enabled.
1976 if (f2fs_sb_has_lost_found(sbi->sb) &&
1977 inode->i_ino == F2FS_ROOT_INO(sbi))
1978 return -EPERM;
1980 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
1981 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
1982 ctx, len, fs_data, XATTR_CREATE);
1985 static bool f2fs_dummy_context(struct inode *inode)
1987 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
1990 static const struct fscrypt_operations f2fs_cryptops = {
1991 .key_prefix = "f2fs:",
1992 .get_context = f2fs_get_context,
1993 .set_context = f2fs_set_context,
1994 .dummy_context = f2fs_dummy_context,
1995 .empty_dir = f2fs_empty_dir,
1996 .max_namelen = F2FS_NAME_LEN,
1998 #endif
2000 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
2001 u64 ino, u32 generation)
2003 struct f2fs_sb_info *sbi = F2FS_SB(sb);
2004 struct inode *inode;
2006 if (f2fs_check_nid_range(sbi, ino))
2007 return ERR_PTR(-ESTALE);
2010 * f2fs_iget isn't quite right if the inode is currently unallocated!
2011 * However f2fs_iget currently does appropriate checks to handle stale
2012 * inodes so everything is OK.
2014 inode = f2fs_iget(sb, ino);
2015 if (IS_ERR(inode))
2016 return ERR_CAST(inode);
2017 if (unlikely(generation && inode->i_generation != generation)) {
2018 /* we didn't find the right inode.. */
2019 iput(inode);
2020 return ERR_PTR(-ESTALE);
2022 return inode;
2025 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
2026 int fh_len, int fh_type)
2028 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2029 f2fs_nfs_get_inode);
2032 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
2033 int fh_len, int fh_type)
2035 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2036 f2fs_nfs_get_inode);
2039 static const struct export_operations f2fs_export_ops = {
2040 .fh_to_dentry = f2fs_fh_to_dentry,
2041 .fh_to_parent = f2fs_fh_to_parent,
2042 .get_parent = f2fs_get_parent,
2045 static loff_t max_file_blocks(void)
2047 loff_t result = 0;
2048 loff_t leaf_count = ADDRS_PER_BLOCK;
2051 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
2052 * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
2053 * space in inode.i_addr, it will be more safe to reassign
2054 * result as zero.
2057 /* two direct node blocks */
2058 result += (leaf_count * 2);
2060 /* two indirect node blocks */
2061 leaf_count *= NIDS_PER_BLOCK;
2062 result += (leaf_count * 2);
2064 /* one double indirect node block */
2065 leaf_count *= NIDS_PER_BLOCK;
2066 result += leaf_count;
2068 return result;
2071 static int __f2fs_commit_super(struct buffer_head *bh,
2072 struct f2fs_super_block *super)
2074 lock_buffer(bh);
2075 if (super)
2076 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
2077 set_buffer_dirty(bh);
2078 unlock_buffer(bh);
2080 /* it's rare case, we can do fua all the time */
2081 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
2084 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
2085 struct buffer_head *bh)
2087 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2088 (bh->b_data + F2FS_SUPER_OFFSET);
2089 struct super_block *sb = sbi->sb;
2090 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2091 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
2092 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
2093 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
2094 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2095 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2096 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
2097 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
2098 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
2099 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
2100 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2101 u32 segment_count = le32_to_cpu(raw_super->segment_count);
2102 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2103 u64 main_end_blkaddr = main_blkaddr +
2104 (segment_count_main << log_blocks_per_seg);
2105 u64 seg_end_blkaddr = segment0_blkaddr +
2106 (segment_count << log_blocks_per_seg);
2108 if (segment0_blkaddr != cp_blkaddr) {
2109 f2fs_msg(sb, KERN_INFO,
2110 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
2111 segment0_blkaddr, cp_blkaddr);
2112 return true;
2115 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
2116 sit_blkaddr) {
2117 f2fs_msg(sb, KERN_INFO,
2118 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
2119 cp_blkaddr, sit_blkaddr,
2120 segment_count_ckpt << log_blocks_per_seg);
2121 return true;
2124 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
2125 nat_blkaddr) {
2126 f2fs_msg(sb, KERN_INFO,
2127 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
2128 sit_blkaddr, nat_blkaddr,
2129 segment_count_sit << log_blocks_per_seg);
2130 return true;
2133 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
2134 ssa_blkaddr) {
2135 f2fs_msg(sb, KERN_INFO,
2136 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
2137 nat_blkaddr, ssa_blkaddr,
2138 segment_count_nat << log_blocks_per_seg);
2139 return true;
2142 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
2143 main_blkaddr) {
2144 f2fs_msg(sb, KERN_INFO,
2145 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
2146 ssa_blkaddr, main_blkaddr,
2147 segment_count_ssa << log_blocks_per_seg);
2148 return true;
2151 if (main_end_blkaddr > seg_end_blkaddr) {
2152 f2fs_msg(sb, KERN_INFO,
2153 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
2154 main_blkaddr,
2155 segment0_blkaddr +
2156 (segment_count << log_blocks_per_seg),
2157 segment_count_main << log_blocks_per_seg);
2158 return true;
2159 } else if (main_end_blkaddr < seg_end_blkaddr) {
2160 int err = 0;
2161 char *res;
2163 /* fix in-memory information all the time */
2164 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
2165 segment0_blkaddr) >> log_blocks_per_seg);
2167 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
2168 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2169 res = "internally";
2170 } else {
2171 err = __f2fs_commit_super(bh, NULL);
2172 res = err ? "failed" : "done";
2174 f2fs_msg(sb, KERN_INFO,
2175 "Fix alignment : %s, start(%u) end(%u) block(%u)",
2176 res, main_blkaddr,
2177 segment0_blkaddr +
2178 (segment_count << log_blocks_per_seg),
2179 segment_count_main << log_blocks_per_seg);
2180 if (err)
2181 return true;
2183 return false;
2186 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
2187 struct buffer_head *bh)
2189 block_t segment_count, segs_per_sec, secs_per_zone;
2190 block_t total_sections, blocks_per_seg;
2191 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
2192 (bh->b_data + F2FS_SUPER_OFFSET);
2193 struct super_block *sb = sbi->sb;
2194 unsigned int blocksize;
2196 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
2197 f2fs_msg(sb, KERN_INFO,
2198 "Magic Mismatch, valid(0x%x) - read(0x%x)",
2199 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
2200 return 1;
2203 /* Currently, support only 4KB page cache size */
2204 if (F2FS_BLKSIZE != PAGE_SIZE) {
2205 f2fs_msg(sb, KERN_INFO,
2206 "Invalid page_cache_size (%lu), supports only 4KB\n",
2207 PAGE_SIZE);
2208 return 1;
2211 /* Currently, support only 4KB block size */
2212 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
2213 if (blocksize != F2FS_BLKSIZE) {
2214 f2fs_msg(sb, KERN_INFO,
2215 "Invalid blocksize (%u), supports only 4KB\n",
2216 blocksize);
2217 return 1;
2220 /* check log blocks per segment */
2221 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
2222 f2fs_msg(sb, KERN_INFO,
2223 "Invalid log blocks per segment (%u)\n",
2224 le32_to_cpu(raw_super->log_blocks_per_seg));
2225 return 1;
2228 /* Currently, support 512/1024/2048/4096 bytes sector size */
2229 if (le32_to_cpu(raw_super->log_sectorsize) >
2230 F2FS_MAX_LOG_SECTOR_SIZE ||
2231 le32_to_cpu(raw_super->log_sectorsize) <
2232 F2FS_MIN_LOG_SECTOR_SIZE) {
2233 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
2234 le32_to_cpu(raw_super->log_sectorsize));
2235 return 1;
2237 if (le32_to_cpu(raw_super->log_sectors_per_block) +
2238 le32_to_cpu(raw_super->log_sectorsize) !=
2239 F2FS_MAX_LOG_SECTOR_SIZE) {
2240 f2fs_msg(sb, KERN_INFO,
2241 "Invalid log sectors per block(%u) log sectorsize(%u)",
2242 le32_to_cpu(raw_super->log_sectors_per_block),
2243 le32_to_cpu(raw_super->log_sectorsize));
2244 return 1;
2247 segment_count = le32_to_cpu(raw_super->segment_count);
2248 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2249 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2250 total_sections = le32_to_cpu(raw_super->section_count);
2252 /* blocks_per_seg should be 512, given the above check */
2253 blocks_per_seg = 1 << le32_to_cpu(raw_super->log_blocks_per_seg);
2255 if (segment_count > F2FS_MAX_SEGMENT ||
2256 segment_count < F2FS_MIN_SEGMENTS) {
2257 f2fs_msg(sb, KERN_INFO,
2258 "Invalid segment count (%u)",
2259 segment_count);
2260 return 1;
2263 if (total_sections > segment_count ||
2264 total_sections < F2FS_MIN_SEGMENTS ||
2265 segs_per_sec > segment_count || !segs_per_sec) {
2266 f2fs_msg(sb, KERN_INFO,
2267 "Invalid segment/section count (%u, %u x %u)",
2268 segment_count, total_sections, segs_per_sec);
2269 return 1;
2272 if ((segment_count / segs_per_sec) < total_sections) {
2273 f2fs_msg(sb, KERN_INFO,
2274 "Small segment_count (%u < %u * %u)",
2275 segment_count, segs_per_sec, total_sections);
2276 return 1;
2279 if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
2280 f2fs_msg(sb, KERN_INFO,
2281 "Wrong segment_count / block_count (%u > %llu)",
2282 segment_count, le64_to_cpu(raw_super->block_count));
2283 return 1;
2286 if (secs_per_zone > total_sections || !secs_per_zone) {
2287 f2fs_msg(sb, KERN_INFO,
2288 "Wrong secs_per_zone / total_sections (%u, %u)",
2289 secs_per_zone, total_sections);
2290 return 1;
2292 if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
2293 raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
2294 (le32_to_cpu(raw_super->extension_count) +
2295 raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
2296 f2fs_msg(sb, KERN_INFO,
2297 "Corrupted extension count (%u + %u > %u)",
2298 le32_to_cpu(raw_super->extension_count),
2299 raw_super->hot_ext_count,
2300 F2FS_MAX_EXTENSION);
2301 return 1;
2304 if (le32_to_cpu(raw_super->cp_payload) >
2305 (blocks_per_seg - F2FS_CP_PACKS)) {
2306 f2fs_msg(sb, KERN_INFO,
2307 "Insane cp_payload (%u > %u)",
2308 le32_to_cpu(raw_super->cp_payload),
2309 blocks_per_seg - F2FS_CP_PACKS);
2310 return 1;
2313 /* check reserved ino info */
2314 if (le32_to_cpu(raw_super->node_ino) != 1 ||
2315 le32_to_cpu(raw_super->meta_ino) != 2 ||
2316 le32_to_cpu(raw_super->root_ino) != 3) {
2317 f2fs_msg(sb, KERN_INFO,
2318 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
2319 le32_to_cpu(raw_super->node_ino),
2320 le32_to_cpu(raw_super->meta_ino),
2321 le32_to_cpu(raw_super->root_ino));
2322 return 1;
2325 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
2326 if (sanity_check_area_boundary(sbi, bh))
2327 return 1;
2329 return 0;
2332 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
2334 unsigned int total, fsmeta;
2335 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2336 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2337 unsigned int ovp_segments, reserved_segments;
2338 unsigned int main_segs, blocks_per_seg;
2339 unsigned int sit_segs, nat_segs;
2340 unsigned int sit_bitmap_size, nat_bitmap_size;
2341 unsigned int log_blocks_per_seg;
2342 unsigned int segment_count_main;
2343 unsigned int cp_pack_start_sum, cp_payload;
2344 block_t user_block_count;
2345 int i, j;
2347 total = le32_to_cpu(raw_super->segment_count);
2348 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
2349 sit_segs = le32_to_cpu(raw_super->segment_count_sit);
2350 fsmeta += sit_segs;
2351 nat_segs = le32_to_cpu(raw_super->segment_count_nat);
2352 fsmeta += nat_segs;
2353 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
2354 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
2356 if (unlikely(fsmeta >= total))
2357 return 1;
2359 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2360 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2362 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
2363 ovp_segments == 0 || reserved_segments == 0)) {
2364 f2fs_msg(sbi->sb, KERN_ERR,
2365 "Wrong layout: check mkfs.f2fs version");
2366 return 1;
2369 user_block_count = le64_to_cpu(ckpt->user_block_count);
2370 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
2371 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2372 if (!user_block_count || user_block_count >=
2373 segment_count_main << log_blocks_per_seg) {
2374 f2fs_msg(sbi->sb, KERN_ERR,
2375 "Wrong user_block_count: %u", user_block_count);
2376 return 1;
2379 main_segs = le32_to_cpu(raw_super->segment_count_main);
2380 blocks_per_seg = sbi->blocks_per_seg;
2382 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2383 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
2384 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
2385 return 1;
2386 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
2387 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2388 le32_to_cpu(ckpt->cur_node_segno[j])) {
2389 f2fs_msg(sbi->sb, KERN_ERR,
2390 "Node segment (%u, %u) has the same "
2391 "segno: %u", i, j,
2392 le32_to_cpu(ckpt->cur_node_segno[i]));
2393 return 1;
2397 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
2398 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
2399 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
2400 return 1;
2401 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
2402 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
2403 le32_to_cpu(ckpt->cur_data_segno[j])) {
2404 f2fs_msg(sbi->sb, KERN_ERR,
2405 "Data segment (%u, %u) has the same "
2406 "segno: %u", i, j,
2407 le32_to_cpu(ckpt->cur_data_segno[i]));
2408 return 1;
2412 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
2413 for (j = i; j < NR_CURSEG_DATA_TYPE; j++) {
2414 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
2415 le32_to_cpu(ckpt->cur_data_segno[j])) {
2416 f2fs_msg(sbi->sb, KERN_ERR,
2417 "Data segment (%u) and Data segment (%u)"
2418 " has the same segno: %u", i, j,
2419 le32_to_cpu(ckpt->cur_node_segno[i]));
2420 return 1;
2425 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2426 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
2428 if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
2429 nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
2430 f2fs_msg(sbi->sb, KERN_ERR,
2431 "Wrong bitmap size: sit: %u, nat:%u",
2432 sit_bitmap_size, nat_bitmap_size);
2433 return 1;
2436 cp_pack_start_sum = __start_sum_addr(sbi);
2437 cp_payload = __cp_payload(sbi);
2438 if (cp_pack_start_sum < cp_payload + 1 ||
2439 cp_pack_start_sum > blocks_per_seg - 1 -
2440 NR_CURSEG_TYPE) {
2441 f2fs_msg(sbi->sb, KERN_ERR,
2442 "Wrong cp_pack_start_sum: %u",
2443 cp_pack_start_sum);
2444 return 1;
2447 if (unlikely(f2fs_cp_error(sbi))) {
2448 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
2449 return 1;
2451 return 0;
2454 static void init_sb_info(struct f2fs_sb_info *sbi)
2456 struct f2fs_super_block *raw_super = sbi->raw_super;
2457 int i, j;
2459 sbi->log_sectors_per_block =
2460 le32_to_cpu(raw_super->log_sectors_per_block);
2461 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
2462 sbi->blocksize = 1 << sbi->log_blocksize;
2463 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
2464 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
2465 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
2466 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
2467 sbi->total_sections = le32_to_cpu(raw_super->section_count);
2468 sbi->total_node_count =
2469 (le32_to_cpu(raw_super->segment_count_nat) / 2)
2470 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
2471 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
2472 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
2473 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
2474 sbi->cur_victim_sec = NULL_SECNO;
2475 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
2477 sbi->dir_level = DEF_DIR_LEVEL;
2478 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
2479 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
2480 clear_sbi_flag(sbi, SBI_NEED_FSCK);
2482 for (i = 0; i < NR_COUNT_TYPE; i++)
2483 atomic_set(&sbi->nr_pages[i], 0);
2485 for (i = 0; i < META; i++)
2486 atomic_set(&sbi->wb_sync_req[i], 0);
2488 INIT_LIST_HEAD(&sbi->s_list);
2489 mutex_init(&sbi->umount_mutex);
2490 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
2491 for (j = HOT; j < NR_TEMP_TYPE; j++)
2492 mutex_init(&sbi->wio_mutex[i][j]);
2493 init_rwsem(&sbi->io_order_lock);
2494 spin_lock_init(&sbi->cp_lock);
2496 sbi->dirty_device = 0;
2497 spin_lock_init(&sbi->dev_lock);
2499 init_rwsem(&sbi->sb_lock);
2502 static int init_percpu_info(struct f2fs_sb_info *sbi)
2504 int err;
2506 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
2507 if (err)
2508 return err;
2510 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
2511 GFP_KERNEL);
2514 #ifdef CONFIG_BLK_DEV_ZONED
2515 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
2517 struct block_device *bdev = FDEV(devi).bdev;
2518 sector_t nr_sectors = bdev->bd_part->nr_sects;
2519 sector_t sector = 0;
2520 struct blk_zone *zones;
2521 unsigned int i, nr_zones;
2522 unsigned int n = 0;
2523 int err = -EIO;
2525 if (!f2fs_sb_has_blkzoned(sbi->sb))
2526 return 0;
2528 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
2529 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
2530 return -EINVAL;
2531 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
2532 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
2533 __ilog2_u32(sbi->blocks_per_blkz))
2534 return -EINVAL;
2535 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
2536 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
2537 sbi->log_blocks_per_blkz;
2538 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
2539 FDEV(devi).nr_blkz++;
2541 FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz,
2542 GFP_KERNEL);
2543 if (!FDEV(devi).blkz_type)
2544 return -ENOMEM;
2546 #define F2FS_REPORT_NR_ZONES 4096
2548 zones = f2fs_kzalloc(sbi,
2549 array_size(F2FS_REPORT_NR_ZONES,
2550 sizeof(struct blk_zone)),
2551 GFP_KERNEL);
2552 if (!zones)
2553 return -ENOMEM;
2555 /* Get block zones type */
2556 while (zones && sector < nr_sectors) {
2558 nr_zones = F2FS_REPORT_NR_ZONES;
2559 err = blkdev_report_zones(bdev, sector,
2560 zones, &nr_zones,
2561 GFP_KERNEL);
2562 if (err)
2563 break;
2564 if (!nr_zones) {
2565 err = -EIO;
2566 break;
2569 for (i = 0; i < nr_zones; i++) {
2570 FDEV(devi).blkz_type[n] = zones[i].type;
2571 sector += zones[i].len;
2572 n++;
2576 kfree(zones);
2578 return err;
2580 #endif
2583 * Read f2fs raw super block.
2584 * Because we have two copies of super block, so read both of them
2585 * to get the first valid one. If any one of them is broken, we pass
2586 * them recovery flag back to the caller.
2588 static int read_raw_super_block(struct f2fs_sb_info *sbi,
2589 struct f2fs_super_block **raw_super,
2590 int *valid_super_block, int *recovery)
2592 struct super_block *sb = sbi->sb;
2593 int block;
2594 struct buffer_head *bh;
2595 struct f2fs_super_block *super;
2596 int err = 0;
2598 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
2599 if (!super)
2600 return -ENOMEM;
2602 for (block = 0; block < 2; block++) {
2603 bh = sb_bread(sb, block);
2604 if (!bh) {
2605 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
2606 block + 1);
2607 err = -EIO;
2608 continue;
2611 /* sanity checking of raw super */
2612 if (sanity_check_raw_super(sbi, bh)) {
2613 f2fs_msg(sb, KERN_ERR,
2614 "Can't find valid F2FS filesystem in %dth superblock",
2615 block + 1);
2616 err = -EINVAL;
2617 brelse(bh);
2618 continue;
2621 if (!*raw_super) {
2622 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
2623 sizeof(*super));
2624 *valid_super_block = block;
2625 *raw_super = super;
2627 brelse(bh);
2630 /* Fail to read any one of the superblocks*/
2631 if (err < 0)
2632 *recovery = 1;
2634 /* No valid superblock */
2635 if (!*raw_super)
2636 kfree(super);
2637 else
2638 err = 0;
2640 return err;
2643 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
2645 struct buffer_head *bh;
2646 int err;
2648 if ((recover && f2fs_readonly(sbi->sb)) ||
2649 bdev_read_only(sbi->sb->s_bdev)) {
2650 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
2651 return -EROFS;
2654 /* write back-up superblock first */
2655 bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
2656 if (!bh)
2657 return -EIO;
2658 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2659 brelse(bh);
2661 /* if we are in recovery path, skip writing valid superblock */
2662 if (recover || err)
2663 return err;
2665 /* write current valid superblock */
2666 bh = sb_bread(sbi->sb, sbi->valid_super_block);
2667 if (!bh)
2668 return -EIO;
2669 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
2670 brelse(bh);
2671 return err;
2674 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
2676 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2677 unsigned int max_devices = MAX_DEVICES;
2678 int i;
2680 /* Initialize single device information */
2681 if (!RDEV(0).path[0]) {
2682 if (!bdev_is_zoned(sbi->sb->s_bdev))
2683 return 0;
2684 max_devices = 1;
2688 * Initialize multiple devices information, or single
2689 * zoned block device information.
2691 sbi->devs = f2fs_kzalloc(sbi,
2692 array_size(max_devices,
2693 sizeof(struct f2fs_dev_info)),
2694 GFP_KERNEL);
2695 if (!sbi->devs)
2696 return -ENOMEM;
2698 for (i = 0; i < max_devices; i++) {
2700 if (i > 0 && !RDEV(i).path[0])
2701 break;
2703 if (max_devices == 1) {
2704 /* Single zoned block device mount */
2705 FDEV(0).bdev =
2706 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
2707 sbi->sb->s_mode, sbi->sb->s_type);
2708 } else {
2709 /* Multi-device mount */
2710 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
2711 FDEV(i).total_segments =
2712 le32_to_cpu(RDEV(i).total_segments);
2713 if (i == 0) {
2714 FDEV(i).start_blk = 0;
2715 FDEV(i).end_blk = FDEV(i).start_blk +
2716 (FDEV(i).total_segments <<
2717 sbi->log_blocks_per_seg) - 1 +
2718 le32_to_cpu(raw_super->segment0_blkaddr);
2719 } else {
2720 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
2721 FDEV(i).end_blk = FDEV(i).start_blk +
2722 (FDEV(i).total_segments <<
2723 sbi->log_blocks_per_seg) - 1;
2725 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
2726 sbi->sb->s_mode, sbi->sb->s_type);
2728 if (IS_ERR(FDEV(i).bdev))
2729 return PTR_ERR(FDEV(i).bdev);
2731 /* to release errored devices */
2732 sbi->s_ndevs = i + 1;
2734 #ifdef CONFIG_BLK_DEV_ZONED
2735 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
2736 !f2fs_sb_has_blkzoned(sbi->sb)) {
2737 f2fs_msg(sbi->sb, KERN_ERR,
2738 "Zoned block device feature not enabled\n");
2739 return -EINVAL;
2741 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
2742 if (init_blkz_info(sbi, i)) {
2743 f2fs_msg(sbi->sb, KERN_ERR,
2744 "Failed to initialize F2FS blkzone information");
2745 return -EINVAL;
2747 if (max_devices == 1)
2748 break;
2749 f2fs_msg(sbi->sb, KERN_INFO,
2750 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
2751 i, FDEV(i).path,
2752 FDEV(i).total_segments,
2753 FDEV(i).start_blk, FDEV(i).end_blk,
2754 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
2755 "Host-aware" : "Host-managed");
2756 continue;
2758 #endif
2759 f2fs_msg(sbi->sb, KERN_INFO,
2760 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
2761 i, FDEV(i).path,
2762 FDEV(i).total_segments,
2763 FDEV(i).start_blk, FDEV(i).end_blk);
2765 f2fs_msg(sbi->sb, KERN_INFO,
2766 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
2767 return 0;
2770 static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
2772 struct f2fs_sm_info *sm_i = SM_I(sbi);
2774 /* adjust parameters according to the volume size */
2775 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
2776 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
2777 sm_i->dcc_info->discard_granularity = 1;
2778 sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
2781 sbi->readdir_ra = 1;
2784 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
2786 struct f2fs_sb_info *sbi;
2787 struct f2fs_super_block *raw_super;
2788 struct inode *root;
2789 int err;
2790 bool retry = true, need_fsck = false;
2791 char *options = NULL;
2792 int recovery, i, valid_super_block;
2793 struct curseg_info *seg_i;
2795 try_onemore:
2796 err = -EINVAL;
2797 raw_super = NULL;
2798 valid_super_block = -1;
2799 recovery = 0;
2801 /* allocate memory for f2fs-specific super block info */
2802 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
2803 if (!sbi)
2804 return -ENOMEM;
2806 sbi->sb = sb;
2808 /* Load the checksum driver */
2809 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
2810 if (IS_ERR(sbi->s_chksum_driver)) {
2811 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
2812 err = PTR_ERR(sbi->s_chksum_driver);
2813 sbi->s_chksum_driver = NULL;
2814 goto free_sbi;
2817 /* set a block size */
2818 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
2819 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
2820 goto free_sbi;
2823 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
2824 &recovery);
2825 if (err)
2826 goto free_sbi;
2828 sb->s_fs_info = sbi;
2829 sbi->raw_super = raw_super;
2831 /* precompute checksum seed for metadata */
2832 if (f2fs_sb_has_inode_chksum(sb))
2833 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
2834 sizeof(raw_super->uuid));
2837 * The BLKZONED feature indicates that the drive was formatted with
2838 * zone alignment optimization. This is optional for host-aware
2839 * devices, but mandatory for host-managed zoned block devices.
2841 #ifndef CONFIG_BLK_DEV_ZONED
2842 if (f2fs_sb_has_blkzoned(sb)) {
2843 f2fs_msg(sb, KERN_ERR,
2844 "Zoned block device support is not enabled\n");
2845 err = -EOPNOTSUPP;
2846 goto free_sb_buf;
2848 #endif
2849 default_options(sbi);
2850 /* parse mount options */
2851 options = kstrdup((const char *)data, GFP_KERNEL);
2852 if (data && !options) {
2853 err = -ENOMEM;
2854 goto free_sb_buf;
2857 err = parse_options(sb, options);
2858 if (err)
2859 goto free_options;
2861 sbi->max_file_blocks = max_file_blocks();
2862 sb->s_maxbytes = sbi->max_file_blocks <<
2863 le32_to_cpu(raw_super->log_blocksize);
2864 sb->s_max_links = F2FS_LINK_MAX;
2865 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2867 #ifdef CONFIG_QUOTA
2868 sb->dq_op = &f2fs_quota_operations;
2869 if (f2fs_sb_has_quota_ino(sb))
2870 sb->s_qcop = &dquot_quotactl_sysfile_ops;
2871 else
2872 sb->s_qcop = &f2fs_quotactl_ops;
2873 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
2875 if (f2fs_sb_has_quota_ino(sbi->sb)) {
2876 for (i = 0; i < MAXQUOTAS; i++) {
2877 if (f2fs_qf_ino(sbi->sb, i))
2878 sbi->nquota_files++;
2881 #endif
2883 sb->s_op = &f2fs_sops;
2884 #ifdef CONFIG_F2FS_FS_ENCRYPTION
2885 sb->s_cop = &f2fs_cryptops;
2886 #endif
2887 sb->s_xattr = f2fs_xattr_handlers;
2888 sb->s_export_op = &f2fs_export_ops;
2889 sb->s_magic = F2FS_SUPER_MAGIC;
2890 sb->s_time_gran = 1;
2891 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
2892 (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
2893 memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
2894 sb->s_iflags |= SB_I_CGROUPWB;
2896 /* init f2fs-specific super block info */
2897 sbi->valid_super_block = valid_super_block;
2898 mutex_init(&sbi->gc_mutex);
2899 mutex_init(&sbi->writepages);
2900 mutex_init(&sbi->cp_mutex);
2901 init_rwsem(&sbi->node_write);
2902 init_rwsem(&sbi->node_change);
2904 /* disallow all the data/node/meta page writes */
2905 set_sbi_flag(sbi, SBI_POR_DOING);
2906 spin_lock_init(&sbi->stat_lock);
2908 /* init iostat info */
2909 spin_lock_init(&sbi->iostat_lock);
2910 sbi->iostat_enable = false;
2912 for (i = 0; i < NR_PAGE_TYPE; i++) {
2913 int n = (i == META) ? 1: NR_TEMP_TYPE;
2914 int j;
2916 sbi->write_io[i] =
2917 f2fs_kmalloc(sbi,
2918 array_size(n,
2919 sizeof(struct f2fs_bio_info)),
2920 GFP_KERNEL);
2921 if (!sbi->write_io[i]) {
2922 err = -ENOMEM;
2923 goto free_options;
2926 for (j = HOT; j < n; j++) {
2927 init_rwsem(&sbi->write_io[i][j].io_rwsem);
2928 sbi->write_io[i][j].sbi = sbi;
2929 sbi->write_io[i][j].bio = NULL;
2930 spin_lock_init(&sbi->write_io[i][j].io_lock);
2931 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
2935 init_rwsem(&sbi->cp_rwsem);
2936 init_waitqueue_head(&sbi->cp_wait);
2937 init_sb_info(sbi);
2939 err = init_percpu_info(sbi);
2940 if (err)
2941 goto free_bio_info;
2943 if (F2FS_IO_SIZE(sbi) > 1) {
2944 sbi->write_io_dummy =
2945 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
2946 if (!sbi->write_io_dummy) {
2947 err = -ENOMEM;
2948 goto free_percpu;
2952 /* get an inode for meta space */
2953 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
2954 if (IS_ERR(sbi->meta_inode)) {
2955 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
2956 err = PTR_ERR(sbi->meta_inode);
2957 goto free_io_dummy;
2960 err = f2fs_get_valid_checkpoint(sbi);
2961 if (err) {
2962 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
2963 goto free_meta_inode;
2966 /* Initialize device list */
2967 err = f2fs_scan_devices(sbi);
2968 if (err) {
2969 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
2970 goto free_devices;
2973 sbi->total_valid_node_count =
2974 le32_to_cpu(sbi->ckpt->valid_node_count);
2975 percpu_counter_set(&sbi->total_valid_inode_count,
2976 le32_to_cpu(sbi->ckpt->valid_inode_count));
2977 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
2978 sbi->total_valid_block_count =
2979 le64_to_cpu(sbi->ckpt->valid_block_count);
2980 sbi->last_valid_block_count = sbi->total_valid_block_count;
2981 sbi->reserved_blocks = 0;
2982 sbi->current_reserved_blocks = 0;
2983 limit_reserve_root(sbi);
2985 for (i = 0; i < NR_INODE_TYPE; i++) {
2986 INIT_LIST_HEAD(&sbi->inode_list[i]);
2987 spin_lock_init(&sbi->inode_lock[i]);
2990 f2fs_init_extent_cache_info(sbi);
2992 f2fs_init_ino_entry_info(sbi);
2994 f2fs_init_fsync_node_info(sbi);
2996 /* setup f2fs internal modules */
2997 err = f2fs_build_segment_manager(sbi);
2998 if (err) {
2999 f2fs_msg(sb, KERN_ERR,
3000 "Failed to initialize F2FS segment manager");
3001 goto free_sm;
3003 err = f2fs_build_node_manager(sbi);
3004 if (err) {
3005 f2fs_msg(sb, KERN_ERR,
3006 "Failed to initialize F2FS node manager");
3007 goto free_nm;
3010 /* For write statistics */
3011 if (sb->s_bdev->bd_part)
3012 sbi->sectors_written_start =
3013 (u64)part_stat_read(sb->s_bdev->bd_part,
3014 sectors[STAT_WRITE]);
3016 /* Read accumulated write IO statistics if exists */
3017 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
3018 if (__exist_node_summaries(sbi))
3019 sbi->kbytes_written =
3020 le64_to_cpu(seg_i->journal->info.kbytes_written);
3022 f2fs_build_gc_manager(sbi);
3024 err = f2fs_build_stats(sbi);
3025 if (err)
3026 goto free_nm;
3028 /* get an inode for node space */
3029 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
3030 if (IS_ERR(sbi->node_inode)) {
3031 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
3032 err = PTR_ERR(sbi->node_inode);
3033 goto free_stats;
3036 /* read root inode and dentry */
3037 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
3038 if (IS_ERR(root)) {
3039 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
3040 err = PTR_ERR(root);
3041 goto free_node_inode;
3043 if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
3044 !root->i_size || !root->i_nlink) {
3045 iput(root);
3046 err = -EINVAL;
3047 goto free_node_inode;
3050 sb->s_root = d_make_root(root); /* allocate root dentry */
3051 if (!sb->s_root) {
3052 err = -ENOMEM;
3053 goto free_root_inode;
3056 err = f2fs_register_sysfs(sbi);
3057 if (err)
3058 goto free_root_inode;
3060 #ifdef CONFIG_QUOTA
3061 /* Enable quota usage during mount */
3062 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb)) {
3063 err = f2fs_enable_quotas(sb);
3064 if (err) {
3065 f2fs_msg(sb, KERN_ERR,
3066 "Cannot turn on quotas: error %d", err);
3067 goto free_sysfs;
3070 #endif
3071 /* if there are nt orphan nodes free them */
3072 err = f2fs_recover_orphan_inodes(sbi);
3073 if (err)
3074 goto free_meta;
3076 /* recover fsynced data */
3077 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
3079 * mount should be failed, when device has readonly mode, and
3080 * previous checkpoint was not done by clean system shutdown.
3082 if (bdev_read_only(sb->s_bdev) &&
3083 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3084 err = -EROFS;
3085 goto free_meta;
3088 if (need_fsck)
3089 set_sbi_flag(sbi, SBI_NEED_FSCK);
3091 if (!retry)
3092 goto skip_recovery;
3094 err = f2fs_recover_fsync_data(sbi, false);
3095 if (err < 0) {
3096 need_fsck = true;
3097 f2fs_msg(sb, KERN_ERR,
3098 "Cannot recover all fsync data errno=%d", err);
3099 goto free_meta;
3101 } else {
3102 err = f2fs_recover_fsync_data(sbi, true);
3104 if (!f2fs_readonly(sb) && err > 0) {
3105 err = -EINVAL;
3106 f2fs_msg(sb, KERN_ERR,
3107 "Need to recover fsync data");
3108 goto free_meta;
3111 skip_recovery:
3112 /* f2fs_recover_fsync_data() cleared this already */
3113 clear_sbi_flag(sbi, SBI_POR_DOING);
3116 * If filesystem is not mounted as read-only then
3117 * do start the gc_thread.
3119 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
3120 /* After POR, we can run background GC thread.*/
3121 err = f2fs_start_gc_thread(sbi);
3122 if (err)
3123 goto free_meta;
3125 kfree(options);
3127 /* recover broken superblock */
3128 if (recovery) {
3129 err = f2fs_commit_super(sbi, true);
3130 f2fs_msg(sb, KERN_INFO,
3131 "Try to recover %dth superblock, ret: %d",
3132 sbi->valid_super_block ? 1 : 2, err);
3135 f2fs_join_shrinker(sbi);
3137 f2fs_tuning_parameters(sbi);
3139 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
3140 cur_cp_version(F2FS_CKPT(sbi)));
3141 f2fs_update_time(sbi, CP_TIME);
3142 f2fs_update_time(sbi, REQ_TIME);
3143 return 0;
3145 free_meta:
3146 #ifdef CONFIG_QUOTA
3147 f2fs_truncate_quota_inode_pages(sb);
3148 if (f2fs_sb_has_quota_ino(sb) && !f2fs_readonly(sb))
3149 f2fs_quota_off_umount(sbi->sb);
3150 #endif
3152 * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
3153 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
3154 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
3155 * falls into an infinite loop in f2fs_sync_meta_pages().
3157 truncate_inode_pages_final(META_MAPPING(sbi));
3158 #ifdef CONFIG_QUOTA
3159 free_sysfs:
3160 #endif
3161 f2fs_unregister_sysfs(sbi);
3162 free_root_inode:
3163 dput(sb->s_root);
3164 sb->s_root = NULL;
3165 free_node_inode:
3166 f2fs_release_ino_entry(sbi, true);
3167 truncate_inode_pages_final(NODE_MAPPING(sbi));
3168 iput(sbi->node_inode);
3169 free_stats:
3170 f2fs_destroy_stats(sbi);
3171 free_nm:
3172 f2fs_destroy_node_manager(sbi);
3173 free_sm:
3174 f2fs_destroy_segment_manager(sbi);
3175 free_devices:
3176 destroy_device_list(sbi);
3177 kfree(sbi->ckpt);
3178 free_meta_inode:
3179 make_bad_inode(sbi->meta_inode);
3180 iput(sbi->meta_inode);
3181 free_io_dummy:
3182 mempool_destroy(sbi->write_io_dummy);
3183 free_percpu:
3184 destroy_percpu_info(sbi);
3185 free_bio_info:
3186 for (i = 0; i < NR_PAGE_TYPE; i++)
3187 kfree(sbi->write_io[i]);
3188 free_options:
3189 #ifdef CONFIG_QUOTA
3190 for (i = 0; i < MAXQUOTAS; i++)
3191 kfree(F2FS_OPTION(sbi).s_qf_names[i]);
3192 #endif
3193 kfree(options);
3194 free_sb_buf:
3195 kfree(raw_super);
3196 free_sbi:
3197 if (sbi->s_chksum_driver)
3198 crypto_free_shash(sbi->s_chksum_driver);
3199 kfree(sbi);
3201 /* give only one another chance */
3202 if (retry) {
3203 retry = false;
3204 shrink_dcache_sb(sb);
3205 goto try_onemore;
3207 return err;
3210 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
3211 const char *dev_name, void *data)
3213 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
3216 static void kill_f2fs_super(struct super_block *sb)
3218 if (sb->s_root) {
3219 struct f2fs_sb_info *sbi = F2FS_SB(sb);
3221 set_sbi_flag(sbi, SBI_IS_CLOSE);
3222 f2fs_stop_gc_thread(sbi);
3223 f2fs_stop_discard_thread(sbi);
3225 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
3226 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3227 struct cp_control cpc = {
3228 .reason = CP_UMOUNT,
3230 f2fs_write_checkpoint(sbi, &cpc);
3233 if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
3234 sb->s_flags &= ~SB_RDONLY;
3236 kill_block_super(sb);
3239 static struct file_system_type f2fs_fs_type = {
3240 .owner = THIS_MODULE,
3241 .name = "f2fs",
3242 .mount = f2fs_mount,
3243 .kill_sb = kill_f2fs_super,
3244 .fs_flags = FS_REQUIRES_DEV,
3246 MODULE_ALIAS_FS("f2fs");
3248 static int __init init_inodecache(void)
3250 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
3251 sizeof(struct f2fs_inode_info), 0,
3252 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
3253 if (!f2fs_inode_cachep)
3254 return -ENOMEM;
3255 return 0;
3258 static void destroy_inodecache(void)
3261 * Make sure all delayed rcu free inodes are flushed before we
3262 * destroy cache.
3264 rcu_barrier();
3265 kmem_cache_destroy(f2fs_inode_cachep);
3268 static int __init init_f2fs_fs(void)
3270 int err;
3272 if (PAGE_SIZE != F2FS_BLKSIZE) {
3273 printk("F2FS not supported on PAGE_SIZE(%lu) != %d\n",
3274 PAGE_SIZE, F2FS_BLKSIZE);
3275 return -EINVAL;
3278 f2fs_build_trace_ios();
3280 err = init_inodecache();
3281 if (err)
3282 goto fail;
3283 err = f2fs_create_node_manager_caches();
3284 if (err)
3285 goto free_inodecache;
3286 err = f2fs_create_segment_manager_caches();
3287 if (err)
3288 goto free_node_manager_caches;
3289 err = f2fs_create_checkpoint_caches();
3290 if (err)
3291 goto free_segment_manager_caches;
3292 err = f2fs_create_extent_cache();
3293 if (err)
3294 goto free_checkpoint_caches;
3295 err = f2fs_init_sysfs();
3296 if (err)
3297 goto free_extent_cache;
3298 err = register_shrinker(&f2fs_shrinker_info);
3299 if (err)
3300 goto free_sysfs;
3301 err = register_filesystem(&f2fs_fs_type);
3302 if (err)
3303 goto free_shrinker;
3304 err = f2fs_create_root_stats();
3305 if (err)
3306 goto free_filesystem;
3307 err = f2fs_init_post_read_processing();
3308 if (err)
3309 goto free_root_stats;
3310 return 0;
3312 free_root_stats:
3313 f2fs_destroy_root_stats();
3314 free_filesystem:
3315 unregister_filesystem(&f2fs_fs_type);
3316 free_shrinker:
3317 unregister_shrinker(&f2fs_shrinker_info);
3318 free_sysfs:
3319 f2fs_exit_sysfs();
3320 free_extent_cache:
3321 f2fs_destroy_extent_cache();
3322 free_checkpoint_caches:
3323 f2fs_destroy_checkpoint_caches();
3324 free_segment_manager_caches:
3325 f2fs_destroy_segment_manager_caches();
3326 free_node_manager_caches:
3327 f2fs_destroy_node_manager_caches();
3328 free_inodecache:
3329 destroy_inodecache();
3330 fail:
3331 return err;
3334 static void __exit exit_f2fs_fs(void)
3336 f2fs_destroy_post_read_processing();
3337 f2fs_destroy_root_stats();
3338 unregister_filesystem(&f2fs_fs_type);
3339 unregister_shrinker(&f2fs_shrinker_info);
3340 f2fs_exit_sysfs();
3341 f2fs_destroy_extent_cache();
3342 f2fs_destroy_checkpoint_caches();
3343 f2fs_destroy_segment_manager_caches();
3344 f2fs_destroy_node_manager_caches();
3345 destroy_inodecache();
3346 f2fs_destroy_trace_ios();
3349 module_init(init_f2fs_fs)
3350 module_exit(exit_f2fs_fs)
3352 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
3353 MODULE_DESCRIPTION("Flash Friendly File System");
3354 MODULE_LICENSE("GPL");