]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/f2fs/super.c
9081570bc616b0a6f0d30088845ad35f141b2883
[mirror_ubuntu-jammy-kernel.git] / fs / f2fs / super.c
1 /*
2 * fs/f2fs/super.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
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.
10 */
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/f2fs_fs.h>
26 #include <linux/sysfs.h>
27
28 #include "f2fs.h"
29 #include "node.h"
30 #include "segment.h"
31 #include "xattr.h"
32 #include "gc.h"
33 #include "trace.h"
34
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/f2fs.h>
37
38 static struct kmem_cache *f2fs_inode_cachep;
39
40 #ifdef CONFIG_F2FS_FAULT_INJECTION
41
42 char *fault_name[FAULT_MAX] = {
43 [FAULT_KMALLOC] = "kmalloc",
44 [FAULT_PAGE_ALLOC] = "page alloc",
45 [FAULT_ALLOC_NID] = "alloc nid",
46 [FAULT_ORPHAN] = "orphan",
47 [FAULT_BLOCK] = "no more block",
48 [FAULT_DIR_DEPTH] = "too big dir depth",
49 [FAULT_EVICT_INODE] = "evict_inode fail",
50 [FAULT_TRUNCATE] = "truncate fail",
51 [FAULT_IO] = "IO error",
52 [FAULT_CHECKPOINT] = "checkpoint error",
53 };
54
55 static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
56 unsigned int rate)
57 {
58 struct f2fs_fault_info *ffi = &sbi->fault_info;
59
60 if (rate) {
61 atomic_set(&ffi->inject_ops, 0);
62 ffi->inject_rate = rate;
63 ffi->inject_type = (1 << FAULT_MAX) - 1;
64 } else {
65 memset(ffi, 0, sizeof(struct f2fs_fault_info));
66 }
67 }
68 #endif
69
70 /* f2fs-wide shrinker description */
71 static struct shrinker f2fs_shrinker_info = {
72 .scan_objects = f2fs_shrink_scan,
73 .count_objects = f2fs_shrink_count,
74 .seeks = DEFAULT_SEEKS,
75 };
76
77 enum {
78 Opt_gc_background,
79 Opt_disable_roll_forward,
80 Opt_norecovery,
81 Opt_discard,
82 Opt_nodiscard,
83 Opt_noheap,
84 Opt_heap,
85 Opt_user_xattr,
86 Opt_nouser_xattr,
87 Opt_acl,
88 Opt_noacl,
89 Opt_active_logs,
90 Opt_disable_ext_identify,
91 Opt_inline_xattr,
92 Opt_noinline_xattr,
93 Opt_inline_data,
94 Opt_inline_dentry,
95 Opt_noinline_dentry,
96 Opt_flush_merge,
97 Opt_noflush_merge,
98 Opt_nobarrier,
99 Opt_fastboot,
100 Opt_extent_cache,
101 Opt_noextent_cache,
102 Opt_noinline_data,
103 Opt_data_flush,
104 Opt_mode,
105 Opt_io_size_bits,
106 Opt_fault_injection,
107 Opt_lazytime,
108 Opt_nolazytime,
109 Opt_err,
110 };
111
112 static match_table_t f2fs_tokens = {
113 {Opt_gc_background, "background_gc=%s"},
114 {Opt_disable_roll_forward, "disable_roll_forward"},
115 {Opt_norecovery, "norecovery"},
116 {Opt_discard, "discard"},
117 {Opt_nodiscard, "nodiscard"},
118 {Opt_noheap, "no_heap"},
119 {Opt_heap, "heap"},
120 {Opt_user_xattr, "user_xattr"},
121 {Opt_nouser_xattr, "nouser_xattr"},
122 {Opt_acl, "acl"},
123 {Opt_noacl, "noacl"},
124 {Opt_active_logs, "active_logs=%u"},
125 {Opt_disable_ext_identify, "disable_ext_identify"},
126 {Opt_inline_xattr, "inline_xattr"},
127 {Opt_noinline_xattr, "noinline_xattr"},
128 {Opt_inline_data, "inline_data"},
129 {Opt_inline_dentry, "inline_dentry"},
130 {Opt_noinline_dentry, "noinline_dentry"},
131 {Opt_flush_merge, "flush_merge"},
132 {Opt_noflush_merge, "noflush_merge"},
133 {Opt_nobarrier, "nobarrier"},
134 {Opt_fastboot, "fastboot"},
135 {Opt_extent_cache, "extent_cache"},
136 {Opt_noextent_cache, "noextent_cache"},
137 {Opt_noinline_data, "noinline_data"},
138 {Opt_data_flush, "data_flush"},
139 {Opt_mode, "mode=%s"},
140 {Opt_io_size_bits, "io_bits=%u"},
141 {Opt_fault_injection, "fault_injection=%u"},
142 {Opt_lazytime, "lazytime"},
143 {Opt_nolazytime, "nolazytime"},
144 {Opt_err, NULL},
145 };
146
147 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...)
148 {
149 struct va_format vaf;
150 va_list args;
151
152 va_start(args, fmt);
153 vaf.fmt = fmt;
154 vaf.va = &args;
155 printk("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf);
156 va_end(args);
157 }
158
159 static void init_once(void *foo)
160 {
161 struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
162
163 inode_init_once(&fi->vfs_inode);
164 }
165
166 static int parse_options(struct super_block *sb, char *options)
167 {
168 struct f2fs_sb_info *sbi = F2FS_SB(sb);
169 struct request_queue *q;
170 substring_t args[MAX_OPT_ARGS];
171 char *p, *name;
172 int arg = 0;
173
174 if (!options)
175 return 0;
176
177 while ((p = strsep(&options, ",")) != NULL) {
178 int token;
179 if (!*p)
180 continue;
181 /*
182 * Initialize args struct so we know whether arg was
183 * found; some options take optional arguments.
184 */
185 args[0].to = args[0].from = NULL;
186 token = match_token(p, f2fs_tokens, args);
187
188 switch (token) {
189 case Opt_gc_background:
190 name = match_strdup(&args[0]);
191
192 if (!name)
193 return -ENOMEM;
194 if (strlen(name) == 2 && !strncmp(name, "on", 2)) {
195 set_opt(sbi, BG_GC);
196 clear_opt(sbi, FORCE_FG_GC);
197 } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) {
198 clear_opt(sbi, BG_GC);
199 clear_opt(sbi, FORCE_FG_GC);
200 } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) {
201 set_opt(sbi, BG_GC);
202 set_opt(sbi, FORCE_FG_GC);
203 } else {
204 kfree(name);
205 return -EINVAL;
206 }
207 kfree(name);
208 break;
209 case Opt_disable_roll_forward:
210 set_opt(sbi, DISABLE_ROLL_FORWARD);
211 break;
212 case Opt_norecovery:
213 /* this option mounts f2fs with ro */
214 set_opt(sbi, DISABLE_ROLL_FORWARD);
215 if (!f2fs_readonly(sb))
216 return -EINVAL;
217 break;
218 case Opt_discard:
219 q = bdev_get_queue(sb->s_bdev);
220 if (blk_queue_discard(q)) {
221 set_opt(sbi, DISCARD);
222 } else if (!f2fs_sb_mounted_blkzoned(sb)) {
223 f2fs_msg(sb, KERN_WARNING,
224 "mounting with \"discard\" option, but "
225 "the device does not support discard");
226 }
227 break;
228 case Opt_nodiscard:
229 if (f2fs_sb_mounted_blkzoned(sb)) {
230 f2fs_msg(sb, KERN_WARNING,
231 "discard is required for zoned block devices");
232 return -EINVAL;
233 }
234 clear_opt(sbi, DISCARD);
235 break;
236 case Opt_noheap:
237 set_opt(sbi, NOHEAP);
238 break;
239 case Opt_heap:
240 clear_opt(sbi, NOHEAP);
241 break;
242 #ifdef CONFIG_F2FS_FS_XATTR
243 case Opt_user_xattr:
244 set_opt(sbi, XATTR_USER);
245 break;
246 case Opt_nouser_xattr:
247 clear_opt(sbi, XATTR_USER);
248 break;
249 case Opt_inline_xattr:
250 set_opt(sbi, INLINE_XATTR);
251 break;
252 case Opt_noinline_xattr:
253 clear_opt(sbi, INLINE_XATTR);
254 break;
255 #else
256 case Opt_user_xattr:
257 f2fs_msg(sb, KERN_INFO,
258 "user_xattr options not supported");
259 break;
260 case Opt_nouser_xattr:
261 f2fs_msg(sb, KERN_INFO,
262 "nouser_xattr options not supported");
263 break;
264 case Opt_inline_xattr:
265 f2fs_msg(sb, KERN_INFO,
266 "inline_xattr options not supported");
267 break;
268 case Opt_noinline_xattr:
269 f2fs_msg(sb, KERN_INFO,
270 "noinline_xattr options not supported");
271 break;
272 #endif
273 #ifdef CONFIG_F2FS_FS_POSIX_ACL
274 case Opt_acl:
275 set_opt(sbi, POSIX_ACL);
276 break;
277 case Opt_noacl:
278 clear_opt(sbi, POSIX_ACL);
279 break;
280 #else
281 case Opt_acl:
282 f2fs_msg(sb, KERN_INFO, "acl options not supported");
283 break;
284 case Opt_noacl:
285 f2fs_msg(sb, KERN_INFO, "noacl options not supported");
286 break;
287 #endif
288 case Opt_active_logs:
289 if (args->from && match_int(args, &arg))
290 return -EINVAL;
291 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
292 return -EINVAL;
293 sbi->active_logs = arg;
294 break;
295 case Opt_disable_ext_identify:
296 set_opt(sbi, DISABLE_EXT_IDENTIFY);
297 break;
298 case Opt_inline_data:
299 set_opt(sbi, INLINE_DATA);
300 break;
301 case Opt_inline_dentry:
302 set_opt(sbi, INLINE_DENTRY);
303 break;
304 case Opt_noinline_dentry:
305 clear_opt(sbi, INLINE_DENTRY);
306 break;
307 case Opt_flush_merge:
308 set_opt(sbi, FLUSH_MERGE);
309 break;
310 case Opt_noflush_merge:
311 clear_opt(sbi, FLUSH_MERGE);
312 break;
313 case Opt_nobarrier:
314 set_opt(sbi, NOBARRIER);
315 break;
316 case Opt_fastboot:
317 set_opt(sbi, FASTBOOT);
318 break;
319 case Opt_extent_cache:
320 set_opt(sbi, EXTENT_CACHE);
321 break;
322 case Opt_noextent_cache:
323 clear_opt(sbi, EXTENT_CACHE);
324 break;
325 case Opt_noinline_data:
326 clear_opt(sbi, INLINE_DATA);
327 break;
328 case Opt_data_flush:
329 set_opt(sbi, DATA_FLUSH);
330 break;
331 case Opt_mode:
332 name = match_strdup(&args[0]);
333
334 if (!name)
335 return -ENOMEM;
336 if (strlen(name) == 8 &&
337 !strncmp(name, "adaptive", 8)) {
338 if (f2fs_sb_mounted_blkzoned(sb)) {
339 f2fs_msg(sb, KERN_WARNING,
340 "adaptive mode is not allowed with "
341 "zoned block device feature");
342 kfree(name);
343 return -EINVAL;
344 }
345 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
346 } else if (strlen(name) == 3 &&
347 !strncmp(name, "lfs", 3)) {
348 set_opt_mode(sbi, F2FS_MOUNT_LFS);
349 } else {
350 kfree(name);
351 return -EINVAL;
352 }
353 kfree(name);
354 break;
355 case Opt_io_size_bits:
356 if (args->from && match_int(args, &arg))
357 return -EINVAL;
358 if (arg > __ilog2_u32(BIO_MAX_PAGES)) {
359 f2fs_msg(sb, KERN_WARNING,
360 "Not support %d, larger than %d",
361 1 << arg, BIO_MAX_PAGES);
362 return -EINVAL;
363 }
364 sbi->write_io_size_bits = arg;
365 break;
366 case Opt_fault_injection:
367 if (args->from && match_int(args, &arg))
368 return -EINVAL;
369 #ifdef CONFIG_F2FS_FAULT_INJECTION
370 f2fs_build_fault_attr(sbi, arg);
371 set_opt(sbi, FAULT_INJECTION);
372 #else
373 f2fs_msg(sb, KERN_INFO,
374 "FAULT_INJECTION was not selected");
375 #endif
376 break;
377 case Opt_lazytime:
378 sb->s_flags |= MS_LAZYTIME;
379 break;
380 case Opt_nolazytime:
381 sb->s_flags &= ~MS_LAZYTIME;
382 break;
383 default:
384 f2fs_msg(sb, KERN_ERR,
385 "Unrecognized mount option \"%s\" or missing value",
386 p);
387 return -EINVAL;
388 }
389 }
390
391 if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) {
392 f2fs_msg(sb, KERN_ERR,
393 "Should set mode=lfs with %uKB-sized IO",
394 F2FS_IO_SIZE_KB(sbi));
395 return -EINVAL;
396 }
397 return 0;
398 }
399
400 static struct inode *f2fs_alloc_inode(struct super_block *sb)
401 {
402 struct f2fs_inode_info *fi;
403
404 fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO);
405 if (!fi)
406 return NULL;
407
408 init_once((void *) fi);
409
410 /* Initialize f2fs-specific inode info */
411 fi->vfs_inode.i_version = 1;
412 atomic_set(&fi->dirty_pages, 0);
413 fi->i_current_depth = 1;
414 fi->i_advise = 0;
415 init_rwsem(&fi->i_sem);
416 INIT_LIST_HEAD(&fi->dirty_list);
417 INIT_LIST_HEAD(&fi->gdirty_list);
418 INIT_LIST_HEAD(&fi->inmem_pages);
419 mutex_init(&fi->inmem_lock);
420 init_rwsem(&fi->dio_rwsem[READ]);
421 init_rwsem(&fi->dio_rwsem[WRITE]);
422 init_rwsem(&fi->i_mmap_sem);
423
424 /* Will be used by directory only */
425 fi->i_dir_level = F2FS_SB(sb)->dir_level;
426 return &fi->vfs_inode;
427 }
428
429 static int f2fs_drop_inode(struct inode *inode)
430 {
431 int ret;
432 /*
433 * This is to avoid a deadlock condition like below.
434 * writeback_single_inode(inode)
435 * - f2fs_write_data_page
436 * - f2fs_gc -> iput -> evict
437 * - inode_wait_for_writeback(inode)
438 */
439 if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) {
440 if (!inode->i_nlink && !is_bad_inode(inode)) {
441 /* to avoid evict_inode call simultaneously */
442 atomic_inc(&inode->i_count);
443 spin_unlock(&inode->i_lock);
444
445 /* some remained atomic pages should discarded */
446 if (f2fs_is_atomic_file(inode))
447 drop_inmem_pages(inode);
448
449 /* should remain fi->extent_tree for writepage */
450 f2fs_destroy_extent_node(inode);
451
452 sb_start_intwrite(inode->i_sb);
453 f2fs_i_size_write(inode, 0);
454
455 if (F2FS_HAS_BLOCKS(inode))
456 f2fs_truncate(inode);
457
458 sb_end_intwrite(inode->i_sb);
459
460 fscrypt_put_encryption_info(inode, NULL);
461 spin_lock(&inode->i_lock);
462 atomic_dec(&inode->i_count);
463 }
464 trace_f2fs_drop_inode(inode, 0);
465 return 0;
466 }
467 ret = generic_drop_inode(inode);
468 trace_f2fs_drop_inode(inode, ret);
469 return ret;
470 }
471
472 int f2fs_inode_dirtied(struct inode *inode, bool sync)
473 {
474 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
475 int ret = 0;
476
477 spin_lock(&sbi->inode_lock[DIRTY_META]);
478 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
479 ret = 1;
480 } else {
481 set_inode_flag(inode, FI_DIRTY_INODE);
482 stat_inc_dirty_inode(sbi, DIRTY_META);
483 }
484 if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
485 list_add_tail(&F2FS_I(inode)->gdirty_list,
486 &sbi->inode_list[DIRTY_META]);
487 inc_page_count(sbi, F2FS_DIRTY_IMETA);
488 }
489 spin_unlock(&sbi->inode_lock[DIRTY_META]);
490 return ret;
491 }
492
493 void f2fs_inode_synced(struct inode *inode)
494 {
495 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
496
497 spin_lock(&sbi->inode_lock[DIRTY_META]);
498 if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
499 spin_unlock(&sbi->inode_lock[DIRTY_META]);
500 return;
501 }
502 if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
503 list_del_init(&F2FS_I(inode)->gdirty_list);
504 dec_page_count(sbi, F2FS_DIRTY_IMETA);
505 }
506 clear_inode_flag(inode, FI_DIRTY_INODE);
507 clear_inode_flag(inode, FI_AUTO_RECOVER);
508 stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
509 spin_unlock(&sbi->inode_lock[DIRTY_META]);
510 }
511
512 /*
513 * f2fs_dirty_inode() is called from __mark_inode_dirty()
514 *
515 * We should call set_dirty_inode to write the dirty inode through write_inode.
516 */
517 static void f2fs_dirty_inode(struct inode *inode, int flags)
518 {
519 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
520
521 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
522 inode->i_ino == F2FS_META_INO(sbi))
523 return;
524
525 if (flags == I_DIRTY_TIME)
526 return;
527
528 if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
529 clear_inode_flag(inode, FI_AUTO_RECOVER);
530
531 f2fs_inode_dirtied(inode, false);
532 }
533
534 static void f2fs_i_callback(struct rcu_head *head)
535 {
536 struct inode *inode = container_of(head, struct inode, i_rcu);
537 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
538 }
539
540 static void f2fs_destroy_inode(struct inode *inode)
541 {
542 call_rcu(&inode->i_rcu, f2fs_i_callback);
543 }
544
545 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
546 {
547 percpu_counter_destroy(&sbi->alloc_valid_block_count);
548 percpu_counter_destroy(&sbi->total_valid_inode_count);
549 }
550
551 static void destroy_device_list(struct f2fs_sb_info *sbi)
552 {
553 int i;
554
555 for (i = 0; i < sbi->s_ndevs; i++) {
556 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
557 #ifdef CONFIG_BLK_DEV_ZONED
558 kfree(FDEV(i).blkz_type);
559 #endif
560 }
561 kfree(sbi->devs);
562 }
563
564 static void f2fs_put_super(struct super_block *sb)
565 {
566 struct f2fs_sb_info *sbi = F2FS_SB(sb);
567 int i;
568
569 stop_gc_thread(sbi);
570
571 /* prevent remaining shrinker jobs */
572 mutex_lock(&sbi->umount_mutex);
573
574 /*
575 * We don't need to do checkpoint when superblock is clean.
576 * But, the previous checkpoint was not done by umount, it needs to do
577 * clean checkpoint again.
578 */
579 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
580 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
581 struct cp_control cpc = {
582 .reason = CP_UMOUNT,
583 };
584 write_checkpoint(sbi, &cpc);
585 }
586
587 /* be sure to wait for any on-going discard commands */
588 f2fs_wait_discard_bios(sbi);
589
590 if (!sbi->discard_blks) {
591 struct cp_control cpc = {
592 .reason = CP_UMOUNT | CP_TRIMMED,
593 };
594 write_checkpoint(sbi, &cpc);
595 }
596
597 /* write_checkpoint can update stat informaion */
598 f2fs_destroy_stats(sbi);
599
600 /*
601 * normally superblock is clean, so we need to release this.
602 * In addition, EIO will skip do checkpoint, we need this as well.
603 */
604 release_ino_entry(sbi, true);
605
606 f2fs_leave_shrinker(sbi);
607 mutex_unlock(&sbi->umount_mutex);
608
609 /* our cp_error case, we can wait for any writeback page */
610 f2fs_flush_merged_writes(sbi);
611
612 iput(sbi->node_inode);
613 iput(sbi->meta_inode);
614
615 /* destroy f2fs internal modules */
616 destroy_node_manager(sbi);
617 destroy_segment_manager(sbi);
618
619 kfree(sbi->ckpt);
620
621 f2fs_exit_sysfs(sbi);
622
623 sb->s_fs_info = NULL;
624 if (sbi->s_chksum_driver)
625 crypto_free_shash(sbi->s_chksum_driver);
626 kfree(sbi->raw_super);
627
628 destroy_device_list(sbi);
629 mempool_destroy(sbi->write_io_dummy);
630 destroy_percpu_info(sbi);
631 for (i = 0; i < NR_PAGE_TYPE; i++)
632 kfree(sbi->write_io[i]);
633 kfree(sbi);
634 }
635
636 int f2fs_sync_fs(struct super_block *sb, int sync)
637 {
638 struct f2fs_sb_info *sbi = F2FS_SB(sb);
639 int err = 0;
640
641 trace_f2fs_sync_fs(sb, sync);
642
643 if (sync) {
644 struct cp_control cpc;
645
646 cpc.reason = __get_cp_reason(sbi);
647
648 mutex_lock(&sbi->gc_mutex);
649 err = write_checkpoint(sbi, &cpc);
650 mutex_unlock(&sbi->gc_mutex);
651 }
652 f2fs_trace_ios(NULL, 1);
653
654 return err;
655 }
656
657 static int f2fs_freeze(struct super_block *sb)
658 {
659 if (f2fs_readonly(sb))
660 return 0;
661
662 /* IO error happened before */
663 if (unlikely(f2fs_cp_error(F2FS_SB(sb))))
664 return -EIO;
665
666 /* must be clean, since sync_filesystem() was already called */
667 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
668 return -EINVAL;
669 return 0;
670 }
671
672 static int f2fs_unfreeze(struct super_block *sb)
673 {
674 return 0;
675 }
676
677 static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
678 {
679 struct super_block *sb = dentry->d_sb;
680 struct f2fs_sb_info *sbi = F2FS_SB(sb);
681 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
682 block_t total_count, user_block_count, start_count, ovp_count;
683
684 total_count = le64_to_cpu(sbi->raw_super->block_count);
685 user_block_count = sbi->user_block_count;
686 start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
687 ovp_count = SM_I(sbi)->ovp_segments << sbi->log_blocks_per_seg;
688 buf->f_type = F2FS_SUPER_MAGIC;
689 buf->f_bsize = sbi->blocksize;
690
691 buf->f_blocks = total_count - start_count;
692 buf->f_bfree = user_block_count - valid_user_blocks(sbi) + ovp_count;
693 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
694
695 buf->f_files = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
696 buf->f_ffree = min(buf->f_files - valid_node_count(sbi),
697 buf->f_bavail);
698
699 buf->f_namelen = F2FS_NAME_LEN;
700 buf->f_fsid.val[0] = (u32)id;
701 buf->f_fsid.val[1] = (u32)(id >> 32);
702
703 return 0;
704 }
705
706 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
707 {
708 struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
709
710 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) {
711 if (test_opt(sbi, FORCE_FG_GC))
712 seq_printf(seq, ",background_gc=%s", "sync");
713 else
714 seq_printf(seq, ",background_gc=%s", "on");
715 } else {
716 seq_printf(seq, ",background_gc=%s", "off");
717 }
718 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
719 seq_puts(seq, ",disable_roll_forward");
720 if (test_opt(sbi, DISCARD))
721 seq_puts(seq, ",discard");
722 if (test_opt(sbi, NOHEAP))
723 seq_puts(seq, ",no_heap");
724 else
725 seq_puts(seq, ",heap");
726 #ifdef CONFIG_F2FS_FS_XATTR
727 if (test_opt(sbi, XATTR_USER))
728 seq_puts(seq, ",user_xattr");
729 else
730 seq_puts(seq, ",nouser_xattr");
731 if (test_opt(sbi, INLINE_XATTR))
732 seq_puts(seq, ",inline_xattr");
733 else
734 seq_puts(seq, ",noinline_xattr");
735 #endif
736 #ifdef CONFIG_F2FS_FS_POSIX_ACL
737 if (test_opt(sbi, POSIX_ACL))
738 seq_puts(seq, ",acl");
739 else
740 seq_puts(seq, ",noacl");
741 #endif
742 if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
743 seq_puts(seq, ",disable_ext_identify");
744 if (test_opt(sbi, INLINE_DATA))
745 seq_puts(seq, ",inline_data");
746 else
747 seq_puts(seq, ",noinline_data");
748 if (test_opt(sbi, INLINE_DENTRY))
749 seq_puts(seq, ",inline_dentry");
750 else
751 seq_puts(seq, ",noinline_dentry");
752 if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE))
753 seq_puts(seq, ",flush_merge");
754 if (test_opt(sbi, NOBARRIER))
755 seq_puts(seq, ",nobarrier");
756 if (test_opt(sbi, FASTBOOT))
757 seq_puts(seq, ",fastboot");
758 if (test_opt(sbi, EXTENT_CACHE))
759 seq_puts(seq, ",extent_cache");
760 else
761 seq_puts(seq, ",noextent_cache");
762 if (test_opt(sbi, DATA_FLUSH))
763 seq_puts(seq, ",data_flush");
764
765 seq_puts(seq, ",mode=");
766 if (test_opt(sbi, ADAPTIVE))
767 seq_puts(seq, "adaptive");
768 else if (test_opt(sbi, LFS))
769 seq_puts(seq, "lfs");
770 seq_printf(seq, ",active_logs=%u", sbi->active_logs);
771 if (F2FS_IO_SIZE_BITS(sbi))
772 seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
773 #ifdef CONFIG_F2FS_FAULT_INJECTION
774 if (test_opt(sbi, FAULT_INJECTION))
775 seq_printf(seq, ",fault_injection=%u",
776 sbi->fault_info.inject_rate);
777 #endif
778
779 return 0;
780 }
781
782 static void default_options(struct f2fs_sb_info *sbi)
783 {
784 /* init some FS parameters */
785 sbi->active_logs = NR_CURSEG_TYPE;
786
787 set_opt(sbi, BG_GC);
788 set_opt(sbi, INLINE_XATTR);
789 set_opt(sbi, INLINE_DATA);
790 set_opt(sbi, INLINE_DENTRY);
791 set_opt(sbi, EXTENT_CACHE);
792 set_opt(sbi, NOHEAP);
793 sbi->sb->s_flags |= MS_LAZYTIME;
794 set_opt(sbi, FLUSH_MERGE);
795 if (f2fs_sb_mounted_blkzoned(sbi->sb)) {
796 set_opt_mode(sbi, F2FS_MOUNT_LFS);
797 set_opt(sbi, DISCARD);
798 } else {
799 set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE);
800 }
801
802 #ifdef CONFIG_F2FS_FS_XATTR
803 set_opt(sbi, XATTR_USER);
804 #endif
805 #ifdef CONFIG_F2FS_FS_POSIX_ACL
806 set_opt(sbi, POSIX_ACL);
807 #endif
808
809 #ifdef CONFIG_F2FS_FAULT_INJECTION
810 f2fs_build_fault_attr(sbi, 0);
811 #endif
812 }
813
814 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
815 {
816 struct f2fs_sb_info *sbi = F2FS_SB(sb);
817 struct f2fs_mount_info org_mount_opt;
818 int err, active_logs;
819 bool need_restart_gc = false;
820 bool need_stop_gc = false;
821 bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
822 #ifdef CONFIG_F2FS_FAULT_INJECTION
823 struct f2fs_fault_info ffi = sbi->fault_info;
824 #endif
825
826 /*
827 * Save the old mount options in case we
828 * need to restore them.
829 */
830 org_mount_opt = sbi->mount_opt;
831 active_logs = sbi->active_logs;
832
833 /* recover superblocks we couldn't write due to previous RO mount */
834 if (!(*flags & MS_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
835 err = f2fs_commit_super(sbi, false);
836 f2fs_msg(sb, KERN_INFO,
837 "Try to recover all the superblocks, ret: %d", err);
838 if (!err)
839 clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
840 }
841
842 sbi->mount_opt.opt = 0;
843 default_options(sbi);
844
845 /* parse mount options */
846 err = parse_options(sb, data);
847 if (err)
848 goto restore_opts;
849
850 /*
851 * Previous and new state of filesystem is RO,
852 * so skip checking GC and FLUSH_MERGE conditions.
853 */
854 if (f2fs_readonly(sb) && (*flags & MS_RDONLY))
855 goto skip;
856
857 /* disallow enable/disable extent_cache dynamically */
858 if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
859 err = -EINVAL;
860 f2fs_msg(sbi->sb, KERN_WARNING,
861 "switch extent_cache option is not allowed");
862 goto restore_opts;
863 }
864
865 /*
866 * We stop the GC thread if FS is mounted as RO
867 * or if background_gc = off is passed in mount
868 * option. Also sync the filesystem.
869 */
870 if ((*flags & MS_RDONLY) || !test_opt(sbi, BG_GC)) {
871 if (sbi->gc_thread) {
872 stop_gc_thread(sbi);
873 need_restart_gc = true;
874 }
875 } else if (!sbi->gc_thread) {
876 err = start_gc_thread(sbi);
877 if (err)
878 goto restore_opts;
879 need_stop_gc = true;
880 }
881
882 if (*flags & MS_RDONLY) {
883 writeback_inodes_sb(sb, WB_REASON_SYNC);
884 sync_inodes_sb(sb);
885
886 set_sbi_flag(sbi, SBI_IS_DIRTY);
887 set_sbi_flag(sbi, SBI_IS_CLOSE);
888 f2fs_sync_fs(sb, 1);
889 clear_sbi_flag(sbi, SBI_IS_CLOSE);
890 }
891
892 /*
893 * We stop issue flush thread if FS is mounted as RO
894 * or if flush_merge is not passed in mount option.
895 */
896 if ((*flags & MS_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
897 clear_opt(sbi, FLUSH_MERGE);
898 destroy_flush_cmd_control(sbi, false);
899 } else {
900 err = create_flush_cmd_control(sbi);
901 if (err)
902 goto restore_gc;
903 }
904 skip:
905 /* Update the POSIXACL Flag */
906 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
907 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
908
909 return 0;
910 restore_gc:
911 if (need_restart_gc) {
912 if (start_gc_thread(sbi))
913 f2fs_msg(sbi->sb, KERN_WARNING,
914 "background gc thread has stopped");
915 } else if (need_stop_gc) {
916 stop_gc_thread(sbi);
917 }
918 restore_opts:
919 sbi->mount_opt = org_mount_opt;
920 sbi->active_logs = active_logs;
921 #ifdef CONFIG_F2FS_FAULT_INJECTION
922 sbi->fault_info = ffi;
923 #endif
924 return err;
925 }
926
927 static struct super_operations f2fs_sops = {
928 .alloc_inode = f2fs_alloc_inode,
929 .drop_inode = f2fs_drop_inode,
930 .destroy_inode = f2fs_destroy_inode,
931 .write_inode = f2fs_write_inode,
932 .dirty_inode = f2fs_dirty_inode,
933 .show_options = f2fs_show_options,
934 .evict_inode = f2fs_evict_inode,
935 .put_super = f2fs_put_super,
936 .sync_fs = f2fs_sync_fs,
937 .freeze_fs = f2fs_freeze,
938 .unfreeze_fs = f2fs_unfreeze,
939 .statfs = f2fs_statfs,
940 .remount_fs = f2fs_remount,
941 };
942
943 #ifdef CONFIG_F2FS_FS_ENCRYPTION
944 static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
945 {
946 return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
947 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
948 ctx, len, NULL);
949 }
950
951 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
952 void *fs_data)
953 {
954 return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
955 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
956 ctx, len, fs_data, XATTR_CREATE);
957 }
958
959 static unsigned f2fs_max_namelen(struct inode *inode)
960 {
961 return S_ISLNK(inode->i_mode) ?
962 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
963 }
964
965 static const struct fscrypt_operations f2fs_cryptops = {
966 .key_prefix = "f2fs:",
967 .get_context = f2fs_get_context,
968 .set_context = f2fs_set_context,
969 .is_encrypted = f2fs_encrypted_inode,
970 .empty_dir = f2fs_empty_dir,
971 .max_namelen = f2fs_max_namelen,
972 };
973 #else
974 static const struct fscrypt_operations f2fs_cryptops = {
975 .is_encrypted = f2fs_encrypted_inode,
976 };
977 #endif
978
979 static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
980 u64 ino, u32 generation)
981 {
982 struct f2fs_sb_info *sbi = F2FS_SB(sb);
983 struct inode *inode;
984
985 if (check_nid_range(sbi, ino))
986 return ERR_PTR(-ESTALE);
987
988 /*
989 * f2fs_iget isn't quite right if the inode is currently unallocated!
990 * However f2fs_iget currently does appropriate checks to handle stale
991 * inodes so everything is OK.
992 */
993 inode = f2fs_iget(sb, ino);
994 if (IS_ERR(inode))
995 return ERR_CAST(inode);
996 if (unlikely(generation && inode->i_generation != generation)) {
997 /* we didn't find the right inode.. */
998 iput(inode);
999 return ERR_PTR(-ESTALE);
1000 }
1001 return inode;
1002 }
1003
1004 static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1005 int fh_len, int fh_type)
1006 {
1007 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1008 f2fs_nfs_get_inode);
1009 }
1010
1011 static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
1012 int fh_len, int fh_type)
1013 {
1014 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1015 f2fs_nfs_get_inode);
1016 }
1017
1018 static const struct export_operations f2fs_export_ops = {
1019 .fh_to_dentry = f2fs_fh_to_dentry,
1020 .fh_to_parent = f2fs_fh_to_parent,
1021 .get_parent = f2fs_get_parent,
1022 };
1023
1024 static loff_t max_file_blocks(void)
1025 {
1026 loff_t result = (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS);
1027 loff_t leaf_count = ADDRS_PER_BLOCK;
1028
1029 /* two direct node blocks */
1030 result += (leaf_count * 2);
1031
1032 /* two indirect node blocks */
1033 leaf_count *= NIDS_PER_BLOCK;
1034 result += (leaf_count * 2);
1035
1036 /* one double indirect node block */
1037 leaf_count *= NIDS_PER_BLOCK;
1038 result += leaf_count;
1039
1040 return result;
1041 }
1042
1043 static int __f2fs_commit_super(struct buffer_head *bh,
1044 struct f2fs_super_block *super)
1045 {
1046 lock_buffer(bh);
1047 if (super)
1048 memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super));
1049 set_buffer_uptodate(bh);
1050 set_buffer_dirty(bh);
1051 unlock_buffer(bh);
1052
1053 /* it's rare case, we can do fua all the time */
1054 return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
1055 }
1056
1057 static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
1058 struct buffer_head *bh)
1059 {
1060 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1061 (bh->b_data + F2FS_SUPER_OFFSET);
1062 struct super_block *sb = sbi->sb;
1063 u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1064 u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
1065 u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
1066 u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
1067 u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1068 u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1069 u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
1070 u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
1071 u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
1072 u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
1073 u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
1074 u32 segment_count = le32_to_cpu(raw_super->segment_count);
1075 u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1076 u64 main_end_blkaddr = main_blkaddr +
1077 (segment_count_main << log_blocks_per_seg);
1078 u64 seg_end_blkaddr = segment0_blkaddr +
1079 (segment_count << log_blocks_per_seg);
1080
1081 if (segment0_blkaddr != cp_blkaddr) {
1082 f2fs_msg(sb, KERN_INFO,
1083 "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
1084 segment0_blkaddr, cp_blkaddr);
1085 return true;
1086 }
1087
1088 if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
1089 sit_blkaddr) {
1090 f2fs_msg(sb, KERN_INFO,
1091 "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
1092 cp_blkaddr, sit_blkaddr,
1093 segment_count_ckpt << log_blocks_per_seg);
1094 return true;
1095 }
1096
1097 if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
1098 nat_blkaddr) {
1099 f2fs_msg(sb, KERN_INFO,
1100 "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
1101 sit_blkaddr, nat_blkaddr,
1102 segment_count_sit << log_blocks_per_seg);
1103 return true;
1104 }
1105
1106 if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
1107 ssa_blkaddr) {
1108 f2fs_msg(sb, KERN_INFO,
1109 "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
1110 nat_blkaddr, ssa_blkaddr,
1111 segment_count_nat << log_blocks_per_seg);
1112 return true;
1113 }
1114
1115 if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
1116 main_blkaddr) {
1117 f2fs_msg(sb, KERN_INFO,
1118 "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
1119 ssa_blkaddr, main_blkaddr,
1120 segment_count_ssa << log_blocks_per_seg);
1121 return true;
1122 }
1123
1124 if (main_end_blkaddr > seg_end_blkaddr) {
1125 f2fs_msg(sb, KERN_INFO,
1126 "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
1127 main_blkaddr,
1128 segment0_blkaddr +
1129 (segment_count << log_blocks_per_seg),
1130 segment_count_main << log_blocks_per_seg);
1131 return true;
1132 } else if (main_end_blkaddr < seg_end_blkaddr) {
1133 int err = 0;
1134 char *res;
1135
1136 /* fix in-memory information all the time */
1137 raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
1138 segment0_blkaddr) >> log_blocks_per_seg);
1139
1140 if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) {
1141 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1142 res = "internally";
1143 } else {
1144 err = __f2fs_commit_super(bh, NULL);
1145 res = err ? "failed" : "done";
1146 }
1147 f2fs_msg(sb, KERN_INFO,
1148 "Fix alignment : %s, start(%u) end(%u) block(%u)",
1149 res, main_blkaddr,
1150 segment0_blkaddr +
1151 (segment_count << log_blocks_per_seg),
1152 segment_count_main << log_blocks_per_seg);
1153 if (err)
1154 return true;
1155 }
1156 return false;
1157 }
1158
1159 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
1160 struct buffer_head *bh)
1161 {
1162 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
1163 (bh->b_data + F2FS_SUPER_OFFSET);
1164 struct super_block *sb = sbi->sb;
1165 unsigned int blocksize;
1166
1167 if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
1168 f2fs_msg(sb, KERN_INFO,
1169 "Magic Mismatch, valid(0x%x) - read(0x%x)",
1170 F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
1171 return 1;
1172 }
1173
1174 /* Currently, support only 4KB page cache size */
1175 if (F2FS_BLKSIZE != PAGE_SIZE) {
1176 f2fs_msg(sb, KERN_INFO,
1177 "Invalid page_cache_size (%lu), supports only 4KB\n",
1178 PAGE_SIZE);
1179 return 1;
1180 }
1181
1182 /* Currently, support only 4KB block size */
1183 blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
1184 if (blocksize != F2FS_BLKSIZE) {
1185 f2fs_msg(sb, KERN_INFO,
1186 "Invalid blocksize (%u), supports only 4KB\n",
1187 blocksize);
1188 return 1;
1189 }
1190
1191 /* check log blocks per segment */
1192 if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
1193 f2fs_msg(sb, KERN_INFO,
1194 "Invalid log blocks per segment (%u)\n",
1195 le32_to_cpu(raw_super->log_blocks_per_seg));
1196 return 1;
1197 }
1198
1199 /* Currently, support 512/1024/2048/4096 bytes sector size */
1200 if (le32_to_cpu(raw_super->log_sectorsize) >
1201 F2FS_MAX_LOG_SECTOR_SIZE ||
1202 le32_to_cpu(raw_super->log_sectorsize) <
1203 F2FS_MIN_LOG_SECTOR_SIZE) {
1204 f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)",
1205 le32_to_cpu(raw_super->log_sectorsize));
1206 return 1;
1207 }
1208 if (le32_to_cpu(raw_super->log_sectors_per_block) +
1209 le32_to_cpu(raw_super->log_sectorsize) !=
1210 F2FS_MAX_LOG_SECTOR_SIZE) {
1211 f2fs_msg(sb, KERN_INFO,
1212 "Invalid log sectors per block(%u) log sectorsize(%u)",
1213 le32_to_cpu(raw_super->log_sectors_per_block),
1214 le32_to_cpu(raw_super->log_sectorsize));
1215 return 1;
1216 }
1217
1218 /* check reserved ino info */
1219 if (le32_to_cpu(raw_super->node_ino) != 1 ||
1220 le32_to_cpu(raw_super->meta_ino) != 2 ||
1221 le32_to_cpu(raw_super->root_ino) != 3) {
1222 f2fs_msg(sb, KERN_INFO,
1223 "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
1224 le32_to_cpu(raw_super->node_ino),
1225 le32_to_cpu(raw_super->meta_ino),
1226 le32_to_cpu(raw_super->root_ino));
1227 return 1;
1228 }
1229
1230 if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) {
1231 f2fs_msg(sb, KERN_INFO,
1232 "Invalid segment count (%u)",
1233 le32_to_cpu(raw_super->segment_count));
1234 return 1;
1235 }
1236
1237 /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
1238 if (sanity_check_area_boundary(sbi, bh))
1239 return 1;
1240
1241 return 0;
1242 }
1243
1244 int sanity_check_ckpt(struct f2fs_sb_info *sbi)
1245 {
1246 unsigned int total, fsmeta;
1247 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1248 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1249 unsigned int ovp_segments, reserved_segments;
1250 unsigned int main_segs, blocks_per_seg;
1251 int i;
1252
1253 total = le32_to_cpu(raw_super->segment_count);
1254 fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
1255 fsmeta += le32_to_cpu(raw_super->segment_count_sit);
1256 fsmeta += le32_to_cpu(raw_super->segment_count_nat);
1257 fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
1258 fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
1259
1260 if (unlikely(fsmeta >= total))
1261 return 1;
1262
1263 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1264 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1265
1266 if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
1267 ovp_segments == 0 || reserved_segments == 0)) {
1268 f2fs_msg(sbi->sb, KERN_ERR,
1269 "Wrong layout: check mkfs.f2fs version");
1270 return 1;
1271 }
1272
1273 main_segs = le32_to_cpu(raw_super->segment_count_main);
1274 blocks_per_seg = sbi->blocks_per_seg;
1275
1276 for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1277 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
1278 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
1279 return 1;
1280 }
1281 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1282 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
1283 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
1284 return 1;
1285 }
1286
1287 if (unlikely(f2fs_cp_error(sbi))) {
1288 f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck");
1289 return 1;
1290 }
1291 return 0;
1292 }
1293
1294 static void init_sb_info(struct f2fs_sb_info *sbi)
1295 {
1296 struct f2fs_super_block *raw_super = sbi->raw_super;
1297 int i, j;
1298
1299 sbi->log_sectors_per_block =
1300 le32_to_cpu(raw_super->log_sectors_per_block);
1301 sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
1302 sbi->blocksize = 1 << sbi->log_blocksize;
1303 sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
1304 sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1305 sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
1306 sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
1307 sbi->total_sections = le32_to_cpu(raw_super->section_count);
1308 sbi->total_node_count =
1309 (le32_to_cpu(raw_super->segment_count_nat) / 2)
1310 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1311 sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
1312 sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
1313 sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
1314 sbi->cur_victim_sec = NULL_SECNO;
1315 sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
1316
1317 sbi->dir_level = DEF_DIR_LEVEL;
1318 sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
1319 sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
1320 clear_sbi_flag(sbi, SBI_NEED_FSCK);
1321
1322 for (i = 0; i < NR_COUNT_TYPE; i++)
1323 atomic_set(&sbi->nr_pages[i], 0);
1324
1325 atomic_set(&sbi->wb_sync_req, 0);
1326
1327 INIT_LIST_HEAD(&sbi->s_list);
1328 mutex_init(&sbi->umount_mutex);
1329 for (i = 0; i < NR_PAGE_TYPE - 1; i++)
1330 for (j = HOT; j < NR_TEMP_TYPE; j++)
1331 mutex_init(&sbi->wio_mutex[i][j]);
1332 spin_lock_init(&sbi->cp_lock);
1333 }
1334
1335 static int init_percpu_info(struct f2fs_sb_info *sbi)
1336 {
1337 int err;
1338
1339 err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
1340 if (err)
1341 return err;
1342
1343 return percpu_counter_init(&sbi->total_valid_inode_count, 0,
1344 GFP_KERNEL);
1345 }
1346
1347 #ifdef CONFIG_BLK_DEV_ZONED
1348 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
1349 {
1350 struct block_device *bdev = FDEV(devi).bdev;
1351 sector_t nr_sectors = bdev->bd_part->nr_sects;
1352 sector_t sector = 0;
1353 struct blk_zone *zones;
1354 unsigned int i, nr_zones;
1355 unsigned int n = 0;
1356 int err = -EIO;
1357
1358 if (!f2fs_sb_mounted_blkzoned(sbi->sb))
1359 return 0;
1360
1361 if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
1362 SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)))
1363 return -EINVAL;
1364 sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev));
1365 if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz !=
1366 __ilog2_u32(sbi->blocks_per_blkz))
1367 return -EINVAL;
1368 sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz);
1369 FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >>
1370 sbi->log_blocks_per_blkz;
1371 if (nr_sectors & (bdev_zone_sectors(bdev) - 1))
1372 FDEV(devi).nr_blkz++;
1373
1374 FDEV(devi).blkz_type = kmalloc(FDEV(devi).nr_blkz, GFP_KERNEL);
1375 if (!FDEV(devi).blkz_type)
1376 return -ENOMEM;
1377
1378 #define F2FS_REPORT_NR_ZONES 4096
1379
1380 zones = kcalloc(F2FS_REPORT_NR_ZONES, sizeof(struct blk_zone),
1381 GFP_KERNEL);
1382 if (!zones)
1383 return -ENOMEM;
1384
1385 /* Get block zones type */
1386 while (zones && sector < nr_sectors) {
1387
1388 nr_zones = F2FS_REPORT_NR_ZONES;
1389 err = blkdev_report_zones(bdev, sector,
1390 zones, &nr_zones,
1391 GFP_KERNEL);
1392 if (err)
1393 break;
1394 if (!nr_zones) {
1395 err = -EIO;
1396 break;
1397 }
1398
1399 for (i = 0; i < nr_zones; i++) {
1400 FDEV(devi).blkz_type[n] = zones[i].type;
1401 sector += zones[i].len;
1402 n++;
1403 }
1404 }
1405
1406 kfree(zones);
1407
1408 return err;
1409 }
1410 #endif
1411
1412 /*
1413 * Read f2fs raw super block.
1414 * Because we have two copies of super block, so read both of them
1415 * to get the first valid one. If any one of them is broken, we pass
1416 * them recovery flag back to the caller.
1417 */
1418 static int read_raw_super_block(struct f2fs_sb_info *sbi,
1419 struct f2fs_super_block **raw_super,
1420 int *valid_super_block, int *recovery)
1421 {
1422 struct super_block *sb = sbi->sb;
1423 int block;
1424 struct buffer_head *bh;
1425 struct f2fs_super_block *super;
1426 int err = 0;
1427
1428 super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL);
1429 if (!super)
1430 return -ENOMEM;
1431
1432 for (block = 0; block < 2; block++) {
1433 bh = sb_bread(sb, block);
1434 if (!bh) {
1435 f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock",
1436 block + 1);
1437 err = -EIO;
1438 continue;
1439 }
1440
1441 /* sanity checking of raw super */
1442 if (sanity_check_raw_super(sbi, bh)) {
1443 f2fs_msg(sb, KERN_ERR,
1444 "Can't find valid F2FS filesystem in %dth superblock",
1445 block + 1);
1446 err = -EINVAL;
1447 brelse(bh);
1448 continue;
1449 }
1450
1451 if (!*raw_super) {
1452 memcpy(super, bh->b_data + F2FS_SUPER_OFFSET,
1453 sizeof(*super));
1454 *valid_super_block = block;
1455 *raw_super = super;
1456 }
1457 brelse(bh);
1458 }
1459
1460 /* Fail to read any one of the superblocks*/
1461 if (err < 0)
1462 *recovery = 1;
1463
1464 /* No valid superblock */
1465 if (!*raw_super)
1466 kfree(super);
1467 else
1468 err = 0;
1469
1470 return err;
1471 }
1472
1473 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
1474 {
1475 struct buffer_head *bh;
1476 int err;
1477
1478 if ((recover && f2fs_readonly(sbi->sb)) ||
1479 bdev_read_only(sbi->sb->s_bdev)) {
1480 set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
1481 return -EROFS;
1482 }
1483
1484 /* write back-up superblock first */
1485 bh = sb_getblk(sbi->sb, sbi->valid_super_block ? 0: 1);
1486 if (!bh)
1487 return -EIO;
1488 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1489 brelse(bh);
1490
1491 /* if we are in recovery path, skip writing valid superblock */
1492 if (recover || err)
1493 return err;
1494
1495 /* write current valid superblock */
1496 bh = sb_getblk(sbi->sb, sbi->valid_super_block);
1497 if (!bh)
1498 return -EIO;
1499 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
1500 brelse(bh);
1501 return err;
1502 }
1503
1504 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
1505 {
1506 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1507 unsigned int max_devices = MAX_DEVICES;
1508 int i;
1509
1510 /* Initialize single device information */
1511 if (!RDEV(0).path[0]) {
1512 if (!bdev_is_zoned(sbi->sb->s_bdev))
1513 return 0;
1514 max_devices = 1;
1515 }
1516
1517 /*
1518 * Initialize multiple devices information, or single
1519 * zoned block device information.
1520 */
1521 sbi->devs = kcalloc(max_devices, sizeof(struct f2fs_dev_info),
1522 GFP_KERNEL);
1523 if (!sbi->devs)
1524 return -ENOMEM;
1525
1526 for (i = 0; i < max_devices; i++) {
1527
1528 if (i > 0 && !RDEV(i).path[0])
1529 break;
1530
1531 if (max_devices == 1) {
1532 /* Single zoned block device mount */
1533 FDEV(0).bdev =
1534 blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev,
1535 sbi->sb->s_mode, sbi->sb->s_type);
1536 } else {
1537 /* Multi-device mount */
1538 memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
1539 FDEV(i).total_segments =
1540 le32_to_cpu(RDEV(i).total_segments);
1541 if (i == 0) {
1542 FDEV(i).start_blk = 0;
1543 FDEV(i).end_blk = FDEV(i).start_blk +
1544 (FDEV(i).total_segments <<
1545 sbi->log_blocks_per_seg) - 1 +
1546 le32_to_cpu(raw_super->segment0_blkaddr);
1547 } else {
1548 FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
1549 FDEV(i).end_blk = FDEV(i).start_blk +
1550 (FDEV(i).total_segments <<
1551 sbi->log_blocks_per_seg) - 1;
1552 }
1553 FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path,
1554 sbi->sb->s_mode, sbi->sb->s_type);
1555 }
1556 if (IS_ERR(FDEV(i).bdev))
1557 return PTR_ERR(FDEV(i).bdev);
1558
1559 /* to release errored devices */
1560 sbi->s_ndevs = i + 1;
1561
1562 #ifdef CONFIG_BLK_DEV_ZONED
1563 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
1564 !f2fs_sb_mounted_blkzoned(sbi->sb)) {
1565 f2fs_msg(sbi->sb, KERN_ERR,
1566 "Zoned block device feature not enabled\n");
1567 return -EINVAL;
1568 }
1569 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
1570 if (init_blkz_info(sbi, i)) {
1571 f2fs_msg(sbi->sb, KERN_ERR,
1572 "Failed to initialize F2FS blkzone information");
1573 return -EINVAL;
1574 }
1575 if (max_devices == 1)
1576 break;
1577 f2fs_msg(sbi->sb, KERN_INFO,
1578 "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)",
1579 i, FDEV(i).path,
1580 FDEV(i).total_segments,
1581 FDEV(i).start_blk, FDEV(i).end_blk,
1582 bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ?
1583 "Host-aware" : "Host-managed");
1584 continue;
1585 }
1586 #endif
1587 f2fs_msg(sbi->sb, KERN_INFO,
1588 "Mount Device [%2d]: %20s, %8u, %8x - %8x",
1589 i, FDEV(i).path,
1590 FDEV(i).total_segments,
1591 FDEV(i).start_blk, FDEV(i).end_blk);
1592 }
1593 f2fs_msg(sbi->sb, KERN_INFO,
1594 "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi));
1595 return 0;
1596 }
1597
1598 static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
1599 {
1600 struct f2fs_sb_info *sbi;
1601 struct f2fs_super_block *raw_super;
1602 struct inode *root;
1603 int err;
1604 bool retry = true, need_fsck = false;
1605 char *options = NULL;
1606 int recovery, i, valid_super_block;
1607 struct curseg_info *seg_i;
1608
1609 try_onemore:
1610 err = -EINVAL;
1611 raw_super = NULL;
1612 valid_super_block = -1;
1613 recovery = 0;
1614
1615 /* allocate memory for f2fs-specific super block info */
1616 sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL);
1617 if (!sbi)
1618 return -ENOMEM;
1619
1620 sbi->sb = sb;
1621
1622 /* Load the checksum driver */
1623 sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
1624 if (IS_ERR(sbi->s_chksum_driver)) {
1625 f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver.");
1626 err = PTR_ERR(sbi->s_chksum_driver);
1627 sbi->s_chksum_driver = NULL;
1628 goto free_sbi;
1629 }
1630
1631 /* set a block size */
1632 if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
1633 f2fs_msg(sb, KERN_ERR, "unable to set blocksize");
1634 goto free_sbi;
1635 }
1636
1637 err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
1638 &recovery);
1639 if (err)
1640 goto free_sbi;
1641
1642 sb->s_fs_info = sbi;
1643 sbi->raw_super = raw_super;
1644
1645 /*
1646 * The BLKZONED feature indicates that the drive was formatted with
1647 * zone alignment optimization. This is optional for host-aware
1648 * devices, but mandatory for host-managed zoned block devices.
1649 */
1650 #ifndef CONFIG_BLK_DEV_ZONED
1651 if (f2fs_sb_mounted_blkzoned(sb)) {
1652 f2fs_msg(sb, KERN_ERR,
1653 "Zoned block device support is not enabled\n");
1654 err = -EOPNOTSUPP;
1655 goto free_sb_buf;
1656 }
1657 #endif
1658 default_options(sbi);
1659 /* parse mount options */
1660 options = kstrdup((const char *)data, GFP_KERNEL);
1661 if (data && !options) {
1662 err = -ENOMEM;
1663 goto free_sb_buf;
1664 }
1665
1666 err = parse_options(sb, options);
1667 if (err)
1668 goto free_options;
1669
1670 sbi->max_file_blocks = max_file_blocks();
1671 sb->s_maxbytes = sbi->max_file_blocks <<
1672 le32_to_cpu(raw_super->log_blocksize);
1673 sb->s_max_links = F2FS_LINK_MAX;
1674 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1675
1676 sb->s_op = &f2fs_sops;
1677 sb->s_cop = &f2fs_cryptops;
1678 sb->s_xattr = f2fs_xattr_handlers;
1679 sb->s_export_op = &f2fs_export_ops;
1680 sb->s_magic = F2FS_SUPER_MAGIC;
1681 sb->s_time_gran = 1;
1682 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1683 (test_opt(sbi, POSIX_ACL) ? MS_POSIXACL : 0);
1684 memcpy(sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid));
1685
1686 /* init f2fs-specific super block info */
1687 sbi->valid_super_block = valid_super_block;
1688 mutex_init(&sbi->gc_mutex);
1689 mutex_init(&sbi->cp_mutex);
1690 init_rwsem(&sbi->node_write);
1691 init_rwsem(&sbi->node_change);
1692
1693 /* disallow all the data/node/meta page writes */
1694 set_sbi_flag(sbi, SBI_POR_DOING);
1695 spin_lock_init(&sbi->stat_lock);
1696
1697 for (i = 0; i < NR_PAGE_TYPE; i++) {
1698 int n = (i == META) ? 1: NR_TEMP_TYPE;
1699 int j;
1700
1701 sbi->write_io[i] = kmalloc(n * sizeof(struct f2fs_bio_info),
1702 GFP_KERNEL);
1703 if (!sbi->write_io[i]) {
1704 err = -ENOMEM;
1705 goto free_options;
1706 }
1707
1708 for (j = HOT; j < n; j++) {
1709 init_rwsem(&sbi->write_io[i][j].io_rwsem);
1710 sbi->write_io[i][j].sbi = sbi;
1711 sbi->write_io[i][j].bio = NULL;
1712 spin_lock_init(&sbi->write_io[i][j].io_lock);
1713 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
1714 }
1715 }
1716
1717 init_rwsem(&sbi->cp_rwsem);
1718 init_waitqueue_head(&sbi->cp_wait);
1719 init_sb_info(sbi);
1720
1721 err = init_percpu_info(sbi);
1722 if (err)
1723 goto free_options;
1724
1725 if (F2FS_IO_SIZE(sbi) > 1) {
1726 sbi->write_io_dummy =
1727 mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0);
1728 if (!sbi->write_io_dummy) {
1729 err = -ENOMEM;
1730 goto free_options;
1731 }
1732 }
1733
1734 /* get an inode for meta space */
1735 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
1736 if (IS_ERR(sbi->meta_inode)) {
1737 f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode");
1738 err = PTR_ERR(sbi->meta_inode);
1739 goto free_io_dummy;
1740 }
1741
1742 err = get_valid_checkpoint(sbi);
1743 if (err) {
1744 f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint");
1745 goto free_meta_inode;
1746 }
1747
1748 /* Initialize device list */
1749 err = f2fs_scan_devices(sbi);
1750 if (err) {
1751 f2fs_msg(sb, KERN_ERR, "Failed to find devices");
1752 goto free_devices;
1753 }
1754
1755 sbi->total_valid_node_count =
1756 le32_to_cpu(sbi->ckpt->valid_node_count);
1757 percpu_counter_set(&sbi->total_valid_inode_count,
1758 le32_to_cpu(sbi->ckpt->valid_inode_count));
1759 sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
1760 sbi->total_valid_block_count =
1761 le64_to_cpu(sbi->ckpt->valid_block_count);
1762 sbi->last_valid_block_count = sbi->total_valid_block_count;
1763
1764 for (i = 0; i < NR_INODE_TYPE; i++) {
1765 INIT_LIST_HEAD(&sbi->inode_list[i]);
1766 spin_lock_init(&sbi->inode_lock[i]);
1767 }
1768
1769 init_extent_cache_info(sbi);
1770
1771 init_ino_entry_info(sbi);
1772
1773 /* setup f2fs internal modules */
1774 err = build_segment_manager(sbi);
1775 if (err) {
1776 f2fs_msg(sb, KERN_ERR,
1777 "Failed to initialize F2FS segment manager");
1778 goto free_sm;
1779 }
1780 err = build_node_manager(sbi);
1781 if (err) {
1782 f2fs_msg(sb, KERN_ERR,
1783 "Failed to initialize F2FS node manager");
1784 goto free_nm;
1785 }
1786
1787 /* For write statistics */
1788 if (sb->s_bdev->bd_part)
1789 sbi->sectors_written_start =
1790 (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]);
1791
1792 /* Read accumulated write IO statistics if exists */
1793 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
1794 if (__exist_node_summaries(sbi))
1795 sbi->kbytes_written =
1796 le64_to_cpu(seg_i->journal->info.kbytes_written);
1797
1798 build_gc_manager(sbi);
1799
1800 /* get an inode for node space */
1801 sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
1802 if (IS_ERR(sbi->node_inode)) {
1803 f2fs_msg(sb, KERN_ERR, "Failed to read node inode");
1804 err = PTR_ERR(sbi->node_inode);
1805 goto free_nm;
1806 }
1807
1808 f2fs_join_shrinker(sbi);
1809
1810 err = f2fs_build_stats(sbi);
1811 if (err)
1812 goto free_nm;
1813
1814 /* if there are nt orphan nodes free them */
1815 err = recover_orphan_inodes(sbi);
1816 if (err)
1817 goto free_node_inode;
1818
1819 /* read root inode and dentry */
1820 root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
1821 if (IS_ERR(root)) {
1822 f2fs_msg(sb, KERN_ERR, "Failed to read root inode");
1823 err = PTR_ERR(root);
1824 goto free_node_inode;
1825 }
1826 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1827 iput(root);
1828 err = -EINVAL;
1829 goto free_node_inode;
1830 }
1831
1832 sb->s_root = d_make_root(root); /* allocate root dentry */
1833 if (!sb->s_root) {
1834 err = -ENOMEM;
1835 goto free_root_inode;
1836 }
1837
1838 err = f2fs_init_sysfs(sbi);
1839 if (err)
1840 goto free_root_inode;
1841
1842 /* recover fsynced data */
1843 if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) {
1844 /*
1845 * mount should be failed, when device has readonly mode, and
1846 * previous checkpoint was not done by clean system shutdown.
1847 */
1848 if (bdev_read_only(sb->s_bdev) &&
1849 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
1850 err = -EROFS;
1851 goto free_sysfs;
1852 }
1853
1854 if (need_fsck)
1855 set_sbi_flag(sbi, SBI_NEED_FSCK);
1856
1857 if (!retry)
1858 goto skip_recovery;
1859
1860 err = recover_fsync_data(sbi, false);
1861 if (err < 0) {
1862 need_fsck = true;
1863 f2fs_msg(sb, KERN_ERR,
1864 "Cannot recover all fsync data errno=%d", err);
1865 goto free_sysfs;
1866 }
1867 } else {
1868 err = recover_fsync_data(sbi, true);
1869
1870 if (!f2fs_readonly(sb) && err > 0) {
1871 err = -EINVAL;
1872 f2fs_msg(sb, KERN_ERR,
1873 "Need to recover fsync data");
1874 goto free_sysfs;
1875 }
1876 }
1877 skip_recovery:
1878 /* recover_fsync_data() cleared this already */
1879 clear_sbi_flag(sbi, SBI_POR_DOING);
1880
1881 /*
1882 * If filesystem is not mounted as read-only then
1883 * do start the gc_thread.
1884 */
1885 if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) {
1886 /* After POR, we can run background GC thread.*/
1887 err = start_gc_thread(sbi);
1888 if (err)
1889 goto free_sysfs;
1890 }
1891 kfree(options);
1892
1893 /* recover broken superblock */
1894 if (recovery) {
1895 err = f2fs_commit_super(sbi, true);
1896 f2fs_msg(sb, KERN_INFO,
1897 "Try to recover %dth superblock, ret: %d",
1898 sbi->valid_super_block ? 1 : 2, err);
1899 }
1900
1901 f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx",
1902 cur_cp_version(F2FS_CKPT(sbi)));
1903 f2fs_update_time(sbi, CP_TIME);
1904 f2fs_update_time(sbi, REQ_TIME);
1905 return 0;
1906
1907 free_sysfs:
1908 f2fs_sync_inode_meta(sbi);
1909 f2fs_exit_sysfs(sbi);
1910 free_root_inode:
1911 dput(sb->s_root);
1912 sb->s_root = NULL;
1913 free_node_inode:
1914 truncate_inode_pages_final(NODE_MAPPING(sbi));
1915 mutex_lock(&sbi->umount_mutex);
1916 release_ino_entry(sbi, true);
1917 f2fs_leave_shrinker(sbi);
1918 /*
1919 * Some dirty meta pages can be produced by recover_orphan_inodes()
1920 * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
1921 * followed by write_checkpoint() through f2fs_write_node_pages(), which
1922 * falls into an infinite loop in sync_meta_pages().
1923 */
1924 truncate_inode_pages_final(META_MAPPING(sbi));
1925 iput(sbi->node_inode);
1926 mutex_unlock(&sbi->umount_mutex);
1927 f2fs_destroy_stats(sbi);
1928 free_nm:
1929 destroy_node_manager(sbi);
1930 free_sm:
1931 destroy_segment_manager(sbi);
1932 free_devices:
1933 destroy_device_list(sbi);
1934 kfree(sbi->ckpt);
1935 free_meta_inode:
1936 make_bad_inode(sbi->meta_inode);
1937 iput(sbi->meta_inode);
1938 free_io_dummy:
1939 mempool_destroy(sbi->write_io_dummy);
1940 free_options:
1941 for (i = 0; i < NR_PAGE_TYPE; i++)
1942 kfree(sbi->write_io[i]);
1943 destroy_percpu_info(sbi);
1944 kfree(options);
1945 free_sb_buf:
1946 kfree(raw_super);
1947 free_sbi:
1948 if (sbi->s_chksum_driver)
1949 crypto_free_shash(sbi->s_chksum_driver);
1950 kfree(sbi);
1951
1952 /* give only one another chance */
1953 if (retry) {
1954 retry = false;
1955 shrink_dcache_sb(sb);
1956 goto try_onemore;
1957 }
1958 return err;
1959 }
1960
1961 static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,
1962 const char *dev_name, void *data)
1963 {
1964 return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);
1965 }
1966
1967 static void kill_f2fs_super(struct super_block *sb)
1968 {
1969 if (sb->s_root)
1970 set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE);
1971 kill_block_super(sb);
1972 }
1973
1974 static struct file_system_type f2fs_fs_type = {
1975 .owner = THIS_MODULE,
1976 .name = "f2fs",
1977 .mount = f2fs_mount,
1978 .kill_sb = kill_f2fs_super,
1979 .fs_flags = FS_REQUIRES_DEV,
1980 };
1981 MODULE_ALIAS_FS("f2fs");
1982
1983 static int __init init_inodecache(void)
1984 {
1985 f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
1986 sizeof(struct f2fs_inode_info), 0,
1987 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
1988 if (!f2fs_inode_cachep)
1989 return -ENOMEM;
1990 return 0;
1991 }
1992
1993 static void destroy_inodecache(void)
1994 {
1995 /*
1996 * Make sure all delayed rcu free inodes are flushed before we
1997 * destroy cache.
1998 */
1999 rcu_barrier();
2000 kmem_cache_destroy(f2fs_inode_cachep);
2001 }
2002
2003 static int __init init_f2fs_fs(void)
2004 {
2005 int err;
2006
2007 f2fs_build_trace_ios();
2008
2009 err = init_inodecache();
2010 if (err)
2011 goto fail;
2012 err = create_node_manager_caches();
2013 if (err)
2014 goto free_inodecache;
2015 err = create_segment_manager_caches();
2016 if (err)
2017 goto free_node_manager_caches;
2018 err = create_checkpoint_caches();
2019 if (err)
2020 goto free_segment_manager_caches;
2021 err = create_extent_cache();
2022 if (err)
2023 goto free_checkpoint_caches;
2024 err = f2fs_register_sysfs();
2025 if (err)
2026 goto free_extent_cache;
2027 err = register_shrinker(&f2fs_shrinker_info);
2028 if (err)
2029 goto free_sysfs;
2030 err = register_filesystem(&f2fs_fs_type);
2031 if (err)
2032 goto free_shrinker;
2033 err = f2fs_create_root_stats();
2034 if (err)
2035 goto free_filesystem;
2036 return 0;
2037
2038 free_filesystem:
2039 unregister_filesystem(&f2fs_fs_type);
2040 free_shrinker:
2041 unregister_shrinker(&f2fs_shrinker_info);
2042 free_sysfs:
2043 f2fs_unregister_sysfs();
2044 free_extent_cache:
2045 destroy_extent_cache();
2046 free_checkpoint_caches:
2047 destroy_checkpoint_caches();
2048 free_segment_manager_caches:
2049 destroy_segment_manager_caches();
2050 free_node_manager_caches:
2051 destroy_node_manager_caches();
2052 free_inodecache:
2053 destroy_inodecache();
2054 fail:
2055 return err;
2056 }
2057
2058 static void __exit exit_f2fs_fs(void)
2059 {
2060 f2fs_destroy_root_stats();
2061 unregister_filesystem(&f2fs_fs_type);
2062 unregister_shrinker(&f2fs_shrinker_info);
2063 f2fs_unregister_sysfs();
2064 destroy_extent_cache();
2065 destroy_checkpoint_caches();
2066 destroy_segment_manager_caches();
2067 destroy_node_manager_caches();
2068 destroy_inodecache();
2069 f2fs_destroy_trace_ios();
2070 }
2071
2072 module_init(init_f2fs_fs)
2073 module_exit(exit_f2fs_fs)
2074
2075 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
2076 MODULE_DESCRIPTION("Flash Friendly File System");
2077 MODULE_LICENSE("GPL");
2078