Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 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> |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 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> |
Jaegeuk Kim | 5e176d5 | 2013-06-28 12:47:01 +0900 | [diff] [blame] | 21 | #include <linux/proc_fs.h> |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 22 | #include <linux/random.h> |
| 23 | #include <linux/exportfs.h> |
Namjae Jeon | d3ee456 | 2013-03-17 17:26:14 +0900 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 25 | #include <linux/quotaops.h> |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 26 | #include <linux/f2fs_fs.h> |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 27 | #include <linux/sysfs.h> |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 28 | #include <linux/quota.h> |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 29 | |
| 30 | #include "f2fs.h" |
| 31 | #include "node.h" |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 32 | #include "segment.h" |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 33 | #include "xattr.h" |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 34 | #include "gc.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 35 | #include "trace.h" |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 36 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 37 | #define CREATE_TRACE_POINTS |
| 38 | #include <trace/events/f2fs.h> |
| 39 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 40 | static struct kmem_cache *f2fs_inode_cachep; |
| 41 | |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 42 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Jaegeuk Kim | 2c63fea | 2016-04-29 15:49:56 -0700 | [diff] [blame] | 43 | |
| 44 | char *fault_name[FAULT_MAX] = { |
| 45 | [FAULT_KMALLOC] = "kmalloc", |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 46 | [FAULT_KVMALLOC] = "kvmalloc", |
Jaegeuk Kim | c41f3cc3 | 2016-04-29 16:17:09 -0700 | [diff] [blame] | 47 | [FAULT_PAGE_ALLOC] = "page alloc", |
Chao Yu | 01eccef | 2017-10-28 16:52:30 +0800 | [diff] [blame] | 48 | [FAULT_PAGE_GET] = "page get", |
Chao Yu | d62fe97 | 2017-10-28 16:52:31 +0800 | [diff] [blame] | 49 | [FAULT_ALLOC_BIO] = "alloc bio", |
Jaegeuk Kim | cb78942 | 2016-04-29 16:29:22 -0700 | [diff] [blame] | 50 | [FAULT_ALLOC_NID] = "alloc nid", |
| 51 | [FAULT_ORPHAN] = "orphan", |
| 52 | [FAULT_BLOCK] = "no more block", |
| 53 | [FAULT_DIR_DEPTH] = "too big dir depth", |
Jaegeuk Kim | 53aa6bb | 2016-05-25 15:24:18 -0700 | [diff] [blame] | 54 | [FAULT_EVICT_INODE] = "evict_inode fail", |
Jaegeuk Kim | 14b44d2 | 2017-03-09 15:24:24 -0800 | [diff] [blame] | 55 | [FAULT_TRUNCATE] = "truncate fail", |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 56 | [FAULT_IO] = "IO error", |
Chao Yu | 0f34802 | 2016-09-26 19:45:55 +0800 | [diff] [blame] | 57 | [FAULT_CHECKPOINT] = "checkpoint error", |
Jaegeuk Kim | 2c63fea | 2016-04-29 15:49:56 -0700 | [diff] [blame] | 58 | }; |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 59 | |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 60 | static void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, |
| 61 | unsigned int rate) |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 62 | { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 63 | struct f2fs_fault_info *ffi = &sbi->fault_info; |
| 64 | |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 65 | if (rate) { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 66 | atomic_set(&ffi->inject_ops, 0); |
| 67 | ffi->inject_rate = rate; |
| 68 | ffi->inject_type = (1 << FAULT_MAX) - 1; |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 69 | } else { |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 70 | memset(ffi, 0, sizeof(struct f2fs_fault_info)); |
Sheng Yong | 0879689 | 2016-05-16 12:38:50 +0800 | [diff] [blame] | 71 | } |
| 72 | } |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 75 | /* f2fs-wide shrinker description */ |
| 76 | static struct shrinker f2fs_shrinker_info = { |
| 77 | .scan_objects = f2fs_shrink_scan, |
| 78 | .count_objects = f2fs_shrink_count, |
| 79 | .seeks = DEFAULT_SEEKS, |
| 80 | }; |
| 81 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 82 | enum { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 83 | Opt_gc_background, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 84 | Opt_disable_roll_forward, |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 85 | Opt_norecovery, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 86 | Opt_discard, |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 87 | Opt_nodiscard, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 88 | Opt_noheap, |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 89 | Opt_heap, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 90 | Opt_user_xattr, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 91 | Opt_nouser_xattr, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 92 | Opt_acl, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 93 | Opt_noacl, |
| 94 | Opt_active_logs, |
| 95 | Opt_disable_ext_identify, |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 96 | Opt_inline_xattr, |
Chao Yu | 23cf721 | 2017-02-15 10:34:45 +0800 | [diff] [blame] | 97 | Opt_noinline_xattr, |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 98 | Opt_inline_xattr_size, |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 99 | Opt_inline_data, |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 100 | Opt_inline_dentry, |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 101 | Opt_noinline_dentry, |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 102 | Opt_flush_merge, |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 103 | Opt_noflush_merge, |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 104 | Opt_nobarrier, |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 105 | Opt_fastboot, |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 106 | Opt_extent_cache, |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 107 | Opt_noextent_cache, |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 108 | Opt_noinline_data, |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 109 | Opt_data_flush, |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 110 | Opt_reserve_root, |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 111 | Opt_resgid, |
| 112 | Opt_resuid, |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 113 | Opt_mode, |
Jaegeuk Kim | ec91538 | 2016-12-21 17:09:19 -0800 | [diff] [blame] | 114 | Opt_io_size_bits, |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 115 | Opt_fault_injection, |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 116 | Opt_lazytime, |
| 117 | Opt_nolazytime, |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 118 | Opt_quota, |
| 119 | Opt_noquota, |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 120 | Opt_usrquota, |
| 121 | Opt_grpquota, |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 122 | Opt_prjquota, |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 123 | Opt_usrjquota, |
| 124 | Opt_grpjquota, |
| 125 | Opt_prjjquota, |
| 126 | Opt_offusrjquota, |
| 127 | Opt_offgrpjquota, |
| 128 | Opt_offprjjquota, |
| 129 | Opt_jqfmt_vfsold, |
| 130 | Opt_jqfmt_vfsv0, |
| 131 | Opt_jqfmt_vfsv1, |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 132 | Opt_whint, |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 133 | Opt_alloc, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 134 | Opt_err, |
| 135 | }; |
| 136 | |
| 137 | static match_table_t f2fs_tokens = { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 138 | {Opt_gc_background, "background_gc=%s"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 139 | {Opt_disable_roll_forward, "disable_roll_forward"}, |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 140 | {Opt_norecovery, "norecovery"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 141 | {Opt_discard, "discard"}, |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 142 | {Opt_nodiscard, "nodiscard"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 143 | {Opt_noheap, "no_heap"}, |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 144 | {Opt_heap, "heap"}, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 145 | {Opt_user_xattr, "user_xattr"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 146 | {Opt_nouser_xattr, "nouser_xattr"}, |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 147 | {Opt_acl, "acl"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 148 | {Opt_noacl, "noacl"}, |
| 149 | {Opt_active_logs, "active_logs=%u"}, |
| 150 | {Opt_disable_ext_identify, "disable_ext_identify"}, |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 151 | {Opt_inline_xattr, "inline_xattr"}, |
Chao Yu | 23cf721 | 2017-02-15 10:34:45 +0800 | [diff] [blame] | 152 | {Opt_noinline_xattr, "noinline_xattr"}, |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 153 | {Opt_inline_xattr_size, "inline_xattr_size=%u"}, |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 154 | {Opt_inline_data, "inline_data"}, |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 155 | {Opt_inline_dentry, "inline_dentry"}, |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 156 | {Opt_noinline_dentry, "noinline_dentry"}, |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 157 | {Opt_flush_merge, "flush_merge"}, |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 158 | {Opt_noflush_merge, "noflush_merge"}, |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 159 | {Opt_nobarrier, "nobarrier"}, |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 160 | {Opt_fastboot, "fastboot"}, |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 161 | {Opt_extent_cache, "extent_cache"}, |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 162 | {Opt_noextent_cache, "noextent_cache"}, |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 163 | {Opt_noinline_data, "noinline_data"}, |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 164 | {Opt_data_flush, "data_flush"}, |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 165 | {Opt_reserve_root, "reserve_root=%u"}, |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 166 | {Opt_resgid, "resgid=%u"}, |
| 167 | {Opt_resuid, "resuid=%u"}, |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 168 | {Opt_mode, "mode=%s"}, |
Jaegeuk Kim | ec91538 | 2016-12-21 17:09:19 -0800 | [diff] [blame] | 169 | {Opt_io_size_bits, "io_bits=%u"}, |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 170 | {Opt_fault_injection, "fault_injection=%u"}, |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 171 | {Opt_lazytime, "lazytime"}, |
| 172 | {Opt_nolazytime, "nolazytime"}, |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 173 | {Opt_quota, "quota"}, |
| 174 | {Opt_noquota, "noquota"}, |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 175 | {Opt_usrquota, "usrquota"}, |
| 176 | {Opt_grpquota, "grpquota"}, |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 177 | {Opt_prjquota, "prjquota"}, |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 178 | {Opt_usrjquota, "usrjquota=%s"}, |
| 179 | {Opt_grpjquota, "grpjquota=%s"}, |
| 180 | {Opt_prjjquota, "prjjquota=%s"}, |
| 181 | {Opt_offusrjquota, "usrjquota="}, |
| 182 | {Opt_offgrpjquota, "grpjquota="}, |
| 183 | {Opt_offprjjquota, "prjjquota="}, |
| 184 | {Opt_jqfmt_vfsold, "jqfmt=vfsold"}, |
| 185 | {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"}, |
| 186 | {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"}, |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 187 | {Opt_whint, "whint_mode=%s"}, |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 188 | {Opt_alloc, "alloc_mode=%s"}, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 189 | {Opt_err, NULL}, |
| 190 | }; |
| 191 | |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 192 | void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...) |
| 193 | { |
| 194 | struct va_format vaf; |
| 195 | va_list args; |
| 196 | |
| 197 | va_start(args, fmt); |
| 198 | vaf.fmt = fmt; |
| 199 | vaf.va = &args; |
Jaegeuk Kim | a36c106 | 2017-08-02 20:58:29 -0700 | [diff] [blame] | 200 | printk_ratelimited("%sF2FS-fs (%s): %pV\n", level, sb->s_id, &vaf); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 201 | va_end(args); |
| 202 | } |
| 203 | |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 204 | static inline void limit_reserve_root(struct f2fs_sb_info *sbi) |
| 205 | { |
| 206 | block_t limit = (sbi->user_block_count << 1) / 1000; |
| 207 | |
| 208 | /* limit is 0.2% */ |
| 209 | if (test_opt(sbi, RESERVE_ROOT) && sbi->root_reserved_blocks > limit) { |
| 210 | sbi->root_reserved_blocks = limit; |
| 211 | f2fs_msg(sbi->sb, KERN_INFO, |
| 212 | "Reduce reserved blocks for root = %u", |
| 213 | sbi->root_reserved_blocks); |
| 214 | } |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 215 | if (!test_opt(sbi, RESERVE_ROOT) && |
| 216 | (!uid_eq(sbi->s_resuid, |
| 217 | make_kuid(&init_user_ns, F2FS_DEF_RESUID)) || |
| 218 | !gid_eq(sbi->s_resgid, |
| 219 | make_kgid(&init_user_ns, F2FS_DEF_RESGID)))) |
| 220 | f2fs_msg(sbi->sb, KERN_INFO, |
| 221 | "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root", |
| 222 | from_kuid_munged(&init_user_ns, sbi->s_resuid), |
| 223 | from_kgid_munged(&init_user_ns, sbi->s_resgid)); |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 226 | static void init_once(void *foo) |
| 227 | { |
| 228 | struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo; |
| 229 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 230 | inode_init_once(&fi->vfs_inode); |
| 231 | } |
| 232 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 233 | #ifdef CONFIG_QUOTA |
| 234 | static const char * const quotatypes[] = INITQFNAMES; |
| 235 | #define QTYPE2NAME(t) (quotatypes[t]) |
| 236 | static int f2fs_set_qf_name(struct super_block *sb, int qtype, |
| 237 | substring_t *args) |
| 238 | { |
| 239 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 240 | char *qname; |
| 241 | int ret = -EINVAL; |
| 242 | |
| 243 | if (sb_any_quota_loaded(sb) && !sbi->s_qf_names[qtype]) { |
| 244 | f2fs_msg(sb, KERN_ERR, |
| 245 | "Cannot change journaled " |
| 246 | "quota options when quota turned on"); |
| 247 | return -EINVAL; |
| 248 | } |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 249 | if (f2fs_sb_has_quota_ino(sb)) { |
| 250 | f2fs_msg(sb, KERN_INFO, |
| 251 | "QUOTA feature is enabled, so ignore qf_name"); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 255 | qname = match_strdup(args); |
| 256 | if (!qname) { |
| 257 | f2fs_msg(sb, KERN_ERR, |
| 258 | "Not enough memory for storing quotafile name"); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | if (sbi->s_qf_names[qtype]) { |
| 262 | if (strcmp(sbi->s_qf_names[qtype], qname) == 0) |
| 263 | ret = 0; |
| 264 | else |
| 265 | f2fs_msg(sb, KERN_ERR, |
| 266 | "%s quota file already specified", |
| 267 | QTYPE2NAME(qtype)); |
| 268 | goto errout; |
| 269 | } |
| 270 | if (strchr(qname, '/')) { |
| 271 | f2fs_msg(sb, KERN_ERR, |
| 272 | "quotafile must be on filesystem root"); |
| 273 | goto errout; |
| 274 | } |
| 275 | sbi->s_qf_names[qtype] = qname; |
| 276 | set_opt(sbi, QUOTA); |
| 277 | return 0; |
| 278 | errout: |
| 279 | kfree(qname); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static int f2fs_clear_qf_name(struct super_block *sb, int qtype) |
| 284 | { |
| 285 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 286 | |
| 287 | if (sb_any_quota_loaded(sb) && sbi->s_qf_names[qtype]) { |
| 288 | f2fs_msg(sb, KERN_ERR, "Cannot change journaled quota options" |
| 289 | " when quota turned on"); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | kfree(sbi->s_qf_names[qtype]); |
| 293 | sbi->s_qf_names[qtype] = NULL; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int f2fs_check_quota_options(struct f2fs_sb_info *sbi) |
| 298 | { |
| 299 | /* |
| 300 | * We do the test below only for project quotas. 'usrquota' and |
| 301 | * 'grpquota' mount options are allowed even without quota feature |
| 302 | * to support legacy quotas in quota files. |
| 303 | */ |
| 304 | if (test_opt(sbi, PRJQUOTA) && !f2fs_sb_has_project_quota(sbi->sb)) { |
| 305 | f2fs_msg(sbi->sb, KERN_ERR, "Project quota feature not enabled. " |
| 306 | "Cannot enable project quota enforcement."); |
| 307 | return -1; |
| 308 | } |
| 309 | if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA] || |
| 310 | sbi->s_qf_names[PRJQUOTA]) { |
| 311 | if (test_opt(sbi, USRQUOTA) && sbi->s_qf_names[USRQUOTA]) |
| 312 | clear_opt(sbi, USRQUOTA); |
| 313 | |
| 314 | if (test_opt(sbi, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA]) |
| 315 | clear_opt(sbi, GRPQUOTA); |
| 316 | |
| 317 | if (test_opt(sbi, PRJQUOTA) && sbi->s_qf_names[PRJQUOTA]) |
| 318 | clear_opt(sbi, PRJQUOTA); |
| 319 | |
| 320 | if (test_opt(sbi, GRPQUOTA) || test_opt(sbi, USRQUOTA) || |
| 321 | test_opt(sbi, PRJQUOTA)) { |
| 322 | f2fs_msg(sbi->sb, KERN_ERR, "old and new quota " |
| 323 | "format mixing"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | if (!sbi->s_jquota_fmt) { |
| 328 | f2fs_msg(sbi->sb, KERN_ERR, "journaled quota format " |
| 329 | "not specified"); |
| 330 | return -1; |
| 331 | } |
| 332 | } |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 333 | |
| 334 | if (f2fs_sb_has_quota_ino(sbi->sb) && sbi->s_jquota_fmt) { |
| 335 | f2fs_msg(sbi->sb, KERN_INFO, |
| 336 | "QUOTA feature is enabled, so ignore jquota_fmt"); |
| 337 | sbi->s_jquota_fmt = 0; |
| 338 | } |
| 339 | if (f2fs_sb_has_quota_ino(sbi->sb) && sb_rdonly(sbi->sb)) { |
| 340 | f2fs_msg(sbi->sb, KERN_INFO, |
| 341 | "Filesystem with quota feature cannot be mounted RDWR " |
| 342 | "without CONFIG_QUOTA"); |
| 343 | return -1; |
| 344 | } |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | #endif |
| 348 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 349 | static int parse_options(struct super_block *sb, char *options) |
| 350 | { |
| 351 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 352 | struct request_queue *q; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 353 | substring_t args[MAX_OPT_ARGS]; |
| 354 | char *p, *name; |
| 355 | int arg = 0; |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 356 | kuid_t uid; |
| 357 | kgid_t gid; |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 358 | #ifdef CONFIG_QUOTA |
| 359 | int ret; |
| 360 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 361 | |
| 362 | if (!options) |
| 363 | return 0; |
| 364 | |
| 365 | while ((p = strsep(&options, ",")) != NULL) { |
| 366 | int token; |
| 367 | if (!*p) |
| 368 | continue; |
| 369 | /* |
| 370 | * Initialize args struct so we know whether arg was |
| 371 | * found; some options take optional arguments. |
| 372 | */ |
| 373 | args[0].to = args[0].from = NULL; |
| 374 | token = match_token(p, f2fs_tokens, args); |
| 375 | |
| 376 | switch (token) { |
| 377 | case Opt_gc_background: |
| 378 | name = match_strdup(&args[0]); |
| 379 | |
| 380 | if (!name) |
| 381 | return -ENOMEM; |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 382 | if (strlen(name) == 2 && !strncmp(name, "on", 2)) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 383 | set_opt(sbi, BG_GC); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 384 | clear_opt(sbi, FORCE_FG_GC); |
| 385 | } else if (strlen(name) == 3 && !strncmp(name, "off", 3)) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 386 | clear_opt(sbi, BG_GC); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 387 | clear_opt(sbi, FORCE_FG_GC); |
| 388 | } else if (strlen(name) == 4 && !strncmp(name, "sync", 4)) { |
| 389 | set_opt(sbi, BG_GC); |
| 390 | set_opt(sbi, FORCE_FG_GC); |
| 391 | } else { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 392 | kfree(name); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | kfree(name); |
| 396 | break; |
| 397 | case Opt_disable_roll_forward: |
| 398 | set_opt(sbi, DISABLE_ROLL_FORWARD); |
| 399 | break; |
Jaegeuk Kim | 2d834bf | 2015-01-23 18:33:46 -0800 | [diff] [blame] | 400 | case Opt_norecovery: |
| 401 | /* this option mounts f2fs with ro */ |
| 402 | set_opt(sbi, DISABLE_ROLL_FORWARD); |
| 403 | if (!f2fs_readonly(sb)) |
| 404 | return -EINVAL; |
| 405 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 406 | case Opt_discard: |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 407 | q = bdev_get_queue(sb->s_bdev); |
| 408 | if (blk_queue_discard(q)) { |
| 409 | set_opt(sbi, DISCARD); |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 410 | } else if (!f2fs_sb_has_blkzoned(sb)) { |
Chao Yu | 09d54cd | 2015-06-08 13:20:10 +0800 | [diff] [blame] | 411 | f2fs_msg(sb, KERN_WARNING, |
| 412 | "mounting with \"discard\" option, but " |
| 413 | "the device does not support discard"); |
| 414 | } |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 415 | break; |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 416 | case Opt_nodiscard: |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 417 | if (f2fs_sb_has_blkzoned(sb)) { |
Damien Le Moal | 96ba2de | 2016-10-28 17:45:03 +0900 | [diff] [blame] | 418 | f2fs_msg(sb, KERN_WARNING, |
| 419 | "discard is required for zoned block devices"); |
| 420 | return -EINVAL; |
| 421 | } |
Chao Yu | 64058be | 2016-07-03 22:05:14 +0800 | [diff] [blame] | 422 | clear_opt(sbi, DISCARD); |
Damien Le Moal | 487df61 | 2016-10-28 17:44:59 +0900 | [diff] [blame] | 423 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 424 | case Opt_noheap: |
| 425 | set_opt(sbi, NOHEAP); |
| 426 | break; |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 427 | case Opt_heap: |
| 428 | clear_opt(sbi, NOHEAP); |
| 429 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 430 | #ifdef CONFIG_F2FS_FS_XATTR |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 431 | case Opt_user_xattr: |
| 432 | set_opt(sbi, XATTR_USER); |
| 433 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 434 | case Opt_nouser_xattr: |
| 435 | clear_opt(sbi, XATTR_USER); |
| 436 | break; |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 437 | case Opt_inline_xattr: |
| 438 | set_opt(sbi, INLINE_XATTR); |
| 439 | break; |
Chao Yu | 23cf721 | 2017-02-15 10:34:45 +0800 | [diff] [blame] | 440 | case Opt_noinline_xattr: |
| 441 | clear_opt(sbi, INLINE_XATTR); |
| 442 | break; |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 443 | case Opt_inline_xattr_size: |
| 444 | if (args->from && match_int(args, &arg)) |
| 445 | return -EINVAL; |
| 446 | set_opt(sbi, INLINE_XATTR_SIZE); |
| 447 | sbi->inline_xattr_size = arg; |
| 448 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 449 | #else |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 450 | case Opt_user_xattr: |
| 451 | f2fs_msg(sb, KERN_INFO, |
| 452 | "user_xattr options not supported"); |
| 453 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 454 | case Opt_nouser_xattr: |
| 455 | f2fs_msg(sb, KERN_INFO, |
| 456 | "nouser_xattr options not supported"); |
| 457 | break; |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 458 | case Opt_inline_xattr: |
| 459 | f2fs_msg(sb, KERN_INFO, |
| 460 | "inline_xattr options not supported"); |
| 461 | break; |
Chao Yu | 23cf721 | 2017-02-15 10:34:45 +0800 | [diff] [blame] | 462 | case Opt_noinline_xattr: |
| 463 | f2fs_msg(sb, KERN_INFO, |
| 464 | "noinline_xattr options not supported"); |
| 465 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 466 | #endif |
| 467 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 468 | case Opt_acl: |
| 469 | set_opt(sbi, POSIX_ACL); |
| 470 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 471 | case Opt_noacl: |
| 472 | clear_opt(sbi, POSIX_ACL); |
| 473 | break; |
| 474 | #else |
Kelly Anderson | 4058c51 | 2013-10-07 11:36:20 +0900 | [diff] [blame] | 475 | case Opt_acl: |
| 476 | f2fs_msg(sb, KERN_INFO, "acl options not supported"); |
| 477 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 478 | case Opt_noacl: |
| 479 | f2fs_msg(sb, KERN_INFO, "noacl options not supported"); |
| 480 | break; |
| 481 | #endif |
| 482 | case Opt_active_logs: |
| 483 | if (args->from && match_int(args, &arg)) |
| 484 | return -EINVAL; |
| 485 | if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE) |
| 486 | return -EINVAL; |
| 487 | sbi->active_logs = arg; |
| 488 | break; |
| 489 | case Opt_disable_ext_identify: |
| 490 | set_opt(sbi, DISABLE_EXT_IDENTIFY); |
| 491 | break; |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 492 | case Opt_inline_data: |
| 493 | set_opt(sbi, INLINE_DATA); |
| 494 | break; |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 495 | case Opt_inline_dentry: |
| 496 | set_opt(sbi, INLINE_DENTRY); |
| 497 | break; |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 498 | case Opt_noinline_dentry: |
| 499 | clear_opt(sbi, INLINE_DENTRY); |
| 500 | break; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 501 | case Opt_flush_merge: |
| 502 | set_opt(sbi, FLUSH_MERGE); |
| 503 | break; |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 504 | case Opt_noflush_merge: |
| 505 | clear_opt(sbi, FLUSH_MERGE); |
| 506 | break; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 507 | case Opt_nobarrier: |
| 508 | set_opt(sbi, NOBARRIER); |
| 509 | break; |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 510 | case Opt_fastboot: |
| 511 | set_opt(sbi, FASTBOOT); |
| 512 | break; |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 513 | case Opt_extent_cache: |
| 514 | set_opt(sbi, EXTENT_CACHE); |
| 515 | break; |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 516 | case Opt_noextent_cache: |
| 517 | clear_opt(sbi, EXTENT_CACHE); |
| 518 | break; |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 519 | case Opt_noinline_data: |
| 520 | clear_opt(sbi, INLINE_DATA); |
| 521 | break; |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 522 | case Opt_data_flush: |
| 523 | set_opt(sbi, DATA_FLUSH); |
| 524 | break; |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 525 | case Opt_reserve_root: |
| 526 | if (args->from && match_int(args, &arg)) |
| 527 | return -EINVAL; |
| 528 | if (test_opt(sbi, RESERVE_ROOT)) { |
| 529 | f2fs_msg(sb, KERN_INFO, |
| 530 | "Preserve previous reserve_root=%u", |
| 531 | sbi->root_reserved_blocks); |
| 532 | } else { |
| 533 | sbi->root_reserved_blocks = arg; |
| 534 | set_opt(sbi, RESERVE_ROOT); |
| 535 | } |
| 536 | break; |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 537 | case Opt_resuid: |
| 538 | if (args->from && match_int(args, &arg)) |
| 539 | return -EINVAL; |
| 540 | uid = make_kuid(current_user_ns(), arg); |
| 541 | if (!uid_valid(uid)) { |
| 542 | f2fs_msg(sb, KERN_ERR, |
| 543 | "Invalid uid value %d", arg); |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | sbi->s_resuid = uid; |
| 547 | break; |
| 548 | case Opt_resgid: |
| 549 | if (args->from && match_int(args, &arg)) |
| 550 | return -EINVAL; |
| 551 | gid = make_kgid(current_user_ns(), arg); |
| 552 | if (!gid_valid(gid)) { |
| 553 | f2fs_msg(sb, KERN_ERR, |
| 554 | "Invalid gid value %d", arg); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | sbi->s_resgid = gid; |
| 558 | break; |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 559 | case Opt_mode: |
| 560 | name = match_strdup(&args[0]); |
| 561 | |
| 562 | if (!name) |
| 563 | return -ENOMEM; |
| 564 | if (strlen(name) == 8 && |
| 565 | !strncmp(name, "adaptive", 8)) { |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 566 | if (f2fs_sb_has_blkzoned(sb)) { |
Damien Le Moal | 3adc57e | 2016-10-28 17:45:04 +0900 | [diff] [blame] | 567 | f2fs_msg(sb, KERN_WARNING, |
| 568 | "adaptive mode is not allowed with " |
| 569 | "zoned block device feature"); |
| 570 | kfree(name); |
| 571 | return -EINVAL; |
| 572 | } |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 573 | set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 574 | } else if (strlen(name) == 3 && |
| 575 | !strncmp(name, "lfs", 3)) { |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 576 | set_opt_mode(sbi, F2FS_MOUNT_LFS); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 577 | } else { |
| 578 | kfree(name); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | kfree(name); |
| 582 | break; |
Jaegeuk Kim | ec91538 | 2016-12-21 17:09:19 -0800 | [diff] [blame] | 583 | case Opt_io_size_bits: |
| 584 | if (args->from && match_int(args, &arg)) |
| 585 | return -EINVAL; |
| 586 | if (arg > __ilog2_u32(BIO_MAX_PAGES)) { |
| 587 | f2fs_msg(sb, KERN_WARNING, |
| 588 | "Not support %d, larger than %d", |
| 589 | 1 << arg, BIO_MAX_PAGES); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | sbi->write_io_size_bits = arg; |
| 593 | break; |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 594 | case Opt_fault_injection: |
| 595 | if (args->from && match_int(args, &arg)) |
| 596 | return -EINVAL; |
| 597 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Chao Yu | 1ecc0c5 | 2016-09-23 21:30:09 +0800 | [diff] [blame] | 598 | f2fs_build_fault_attr(sbi, arg); |
Kaixu Xia | 0cc0dec | 2017-01-27 09:35:37 +0800 | [diff] [blame] | 599 | set_opt(sbi, FAULT_INJECTION); |
Jaegeuk Kim | 73faec4d | 2016-04-29 15:34:32 -0700 | [diff] [blame] | 600 | #else |
| 601 | f2fs_msg(sb, KERN_INFO, |
| 602 | "FAULT_INJECTION was not selected"); |
| 603 | #endif |
| 604 | break; |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 605 | case Opt_lazytime: |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 606 | sb->s_flags |= SB_LAZYTIME; |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 607 | break; |
| 608 | case Opt_nolazytime: |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 609 | sb->s_flags &= ~SB_LAZYTIME; |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 610 | break; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 611 | #ifdef CONFIG_QUOTA |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 612 | case Opt_quota: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 613 | case Opt_usrquota: |
| 614 | set_opt(sbi, USRQUOTA); |
| 615 | break; |
| 616 | case Opt_grpquota: |
| 617 | set_opt(sbi, GRPQUOTA); |
| 618 | break; |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 619 | case Opt_prjquota: |
| 620 | set_opt(sbi, PRJQUOTA); |
| 621 | break; |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 622 | case Opt_usrjquota: |
| 623 | ret = f2fs_set_qf_name(sb, USRQUOTA, &args[0]); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | break; |
| 627 | case Opt_grpjquota: |
| 628 | ret = f2fs_set_qf_name(sb, GRPQUOTA, &args[0]); |
| 629 | if (ret) |
| 630 | return ret; |
| 631 | break; |
| 632 | case Opt_prjjquota: |
| 633 | ret = f2fs_set_qf_name(sb, PRJQUOTA, &args[0]); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | break; |
| 637 | case Opt_offusrjquota: |
| 638 | ret = f2fs_clear_qf_name(sb, USRQUOTA); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | break; |
| 642 | case Opt_offgrpjquota: |
| 643 | ret = f2fs_clear_qf_name(sb, GRPQUOTA); |
| 644 | if (ret) |
| 645 | return ret; |
| 646 | break; |
| 647 | case Opt_offprjjquota: |
| 648 | ret = f2fs_clear_qf_name(sb, PRJQUOTA); |
| 649 | if (ret) |
| 650 | return ret; |
| 651 | break; |
| 652 | case Opt_jqfmt_vfsold: |
| 653 | sbi->s_jquota_fmt = QFMT_VFS_OLD; |
| 654 | break; |
| 655 | case Opt_jqfmt_vfsv0: |
| 656 | sbi->s_jquota_fmt = QFMT_VFS_V0; |
| 657 | break; |
| 658 | case Opt_jqfmt_vfsv1: |
| 659 | sbi->s_jquota_fmt = QFMT_VFS_V1; |
| 660 | break; |
| 661 | case Opt_noquota: |
| 662 | clear_opt(sbi, QUOTA); |
| 663 | clear_opt(sbi, USRQUOTA); |
| 664 | clear_opt(sbi, GRPQUOTA); |
| 665 | clear_opt(sbi, PRJQUOTA); |
| 666 | break; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 667 | #else |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 668 | case Opt_quota: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 669 | case Opt_usrquota: |
| 670 | case Opt_grpquota: |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 671 | case Opt_prjquota: |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 672 | case Opt_usrjquota: |
| 673 | case Opt_grpjquota: |
| 674 | case Opt_prjjquota: |
| 675 | case Opt_offusrjquota: |
| 676 | case Opt_offgrpjquota: |
| 677 | case Opt_offprjjquota: |
| 678 | case Opt_jqfmt_vfsold: |
| 679 | case Opt_jqfmt_vfsv0: |
| 680 | case Opt_jqfmt_vfsv1: |
| 681 | case Opt_noquota: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 682 | f2fs_msg(sb, KERN_INFO, |
| 683 | "quota operations not supported"); |
| 684 | break; |
| 685 | #endif |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 686 | case Opt_whint: |
| 687 | name = match_strdup(&args[0]); |
| 688 | if (!name) |
| 689 | return -ENOMEM; |
| 690 | if (strlen(name) == 10 && |
| 691 | !strncmp(name, "user-based", 10)) { |
| 692 | sbi->whint_mode = WHINT_MODE_USER; |
| 693 | } else if (strlen(name) == 3 && |
| 694 | !strncmp(name, "off", 3)) { |
| 695 | sbi->whint_mode = WHINT_MODE_OFF; |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 696 | } else if (strlen(name) == 8 && |
| 697 | !strncmp(name, "fs-based", 8)) { |
| 698 | sbi->whint_mode = WHINT_MODE_FS; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 699 | } else { |
| 700 | kfree(name); |
| 701 | return -EINVAL; |
| 702 | } |
| 703 | kfree(name); |
| 704 | break; |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 705 | case Opt_alloc: |
| 706 | name = match_strdup(&args[0]); |
| 707 | if (!name) |
| 708 | return -ENOMEM; |
| 709 | |
| 710 | if (strlen(name) == 7 && |
| 711 | !strncmp(name, "default", 7)) { |
| 712 | sbi->alloc_mode = ALLOC_MODE_DEFAULT; |
| 713 | } else if (strlen(name) == 5 && |
| 714 | !strncmp(name, "reuse", 5)) { |
| 715 | sbi->alloc_mode = ALLOC_MODE_REUSE; |
| 716 | } else { |
| 717 | kfree(name); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | kfree(name); |
| 721 | break; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 722 | default: |
| 723 | f2fs_msg(sb, KERN_ERR, |
| 724 | "Unrecognized mount option \"%s\" or missing value", |
| 725 | p); |
| 726 | return -EINVAL; |
| 727 | } |
| 728 | } |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 729 | #ifdef CONFIG_QUOTA |
| 730 | if (f2fs_check_quota_options(sbi)) |
| 731 | return -EINVAL; |
| 732 | #endif |
Jaegeuk Kim | ec91538 | 2016-12-21 17:09:19 -0800 | [diff] [blame] | 733 | |
| 734 | if (F2FS_IO_SIZE_BITS(sbi) && !test_opt(sbi, LFS)) { |
| 735 | f2fs_msg(sb, KERN_ERR, |
| 736 | "Should set mode=lfs with %uKB-sized IO", |
| 737 | F2FS_IO_SIZE_KB(sbi)); |
| 738 | return -EINVAL; |
| 739 | } |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 740 | |
| 741 | if (test_opt(sbi, INLINE_XATTR_SIZE)) { |
Chao Yu | 4d817ae | 2018-01-27 17:29:48 +0800 | [diff] [blame] | 742 | if (!f2fs_sb_has_extra_attr(sb) || |
| 743 | !f2fs_sb_has_flexible_inline_xattr(sb)) { |
| 744 | f2fs_msg(sb, KERN_ERR, |
| 745 | "extra_attr or flexible_inline_xattr " |
| 746 | "feature is off"); |
| 747 | return -EINVAL; |
| 748 | } |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 749 | if (!test_opt(sbi, INLINE_XATTR)) { |
| 750 | f2fs_msg(sb, KERN_ERR, |
| 751 | "inline_xattr_size option should be " |
| 752 | "set with inline_xattr option"); |
| 753 | return -EINVAL; |
| 754 | } |
| 755 | if (!sbi->inline_xattr_size || |
| 756 | sbi->inline_xattr_size >= DEF_ADDRS_PER_INODE - |
| 757 | F2FS_TOTAL_EXTRA_ATTR_SIZE - |
| 758 | DEF_INLINE_RESERVED_SIZE - |
| 759 | DEF_MIN_INLINE_SIZE) { |
| 760 | f2fs_msg(sb, KERN_ERR, |
| 761 | "inline xattr size is out of range"); |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | } |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 765 | |
| 766 | /* Not pass down write hints if the number of active logs is lesser |
| 767 | * than NR_CURSEG_TYPE. |
| 768 | */ |
| 769 | if (sbi->active_logs != NR_CURSEG_TYPE) |
| 770 | sbi->whint_mode = WHINT_MODE_OFF; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 774 | static struct inode *f2fs_alloc_inode(struct super_block *sb) |
| 775 | { |
| 776 | struct f2fs_inode_info *fi; |
| 777 | |
Chao Yu | a0acdfe | 2013-12-05 09:54:00 +0800 | [diff] [blame] | 778 | fi = kmem_cache_alloc(f2fs_inode_cachep, GFP_F2FS_ZERO); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 779 | if (!fi) |
| 780 | return NULL; |
| 781 | |
| 782 | init_once((void *) fi); |
| 783 | |
Masanari Iida | 434720f | 2013-03-19 08:03:35 +0900 | [diff] [blame] | 784 | /* Initialize f2fs-specific inode info */ |
Jaegeuk Kim | 204706c | 2016-12-02 15:11:32 -0800 | [diff] [blame] | 785 | atomic_set(&fi->dirty_pages, 0); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 786 | fi->i_current_depth = 1; |
| 787 | fi->i_advise = 0; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 788 | init_rwsem(&fi->i_sem); |
Chao Yu | 2710fd7 | 2015-12-15 13:30:45 +0800 | [diff] [blame] | 789 | INIT_LIST_HEAD(&fi->dirty_list); |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 790 | INIT_LIST_HEAD(&fi->gdirty_list); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 791 | INIT_LIST_HEAD(&fi->inmem_ilist); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 792 | INIT_LIST_HEAD(&fi->inmem_pages); |
| 793 | mutex_init(&fi->inmem_lock); |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 794 | init_rwsem(&fi->dio_rwsem[READ]); |
| 795 | init_rwsem(&fi->dio_rwsem[WRITE]); |
Qiuyang Sun | 5a3a2d8 | 2017-05-18 11:06:45 +0800 | [diff] [blame] | 796 | init_rwsem(&fi->i_mmap_sem); |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 797 | init_rwsem(&fi->i_xattr_sem); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 798 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 799 | #ifdef CONFIG_QUOTA |
| 800 | memset(&fi->i_dquot, 0, sizeof(fi->i_dquot)); |
| 801 | fi->i_reserved_quota = 0; |
| 802 | #endif |
Jaegeuk Kim | ab9fa66 | 2014-02-27 20:09:05 +0900 | [diff] [blame] | 803 | /* Will be used by directory only */ |
| 804 | fi->i_dir_level = F2FS_SB(sb)->dir_level; |
Chao Yu | f247037 | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 805 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 806 | return &fi->vfs_inode; |
| 807 | } |
| 808 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 809 | static int f2fs_drop_inode(struct inode *inode) |
| 810 | { |
Hou Pengyang | b8d96a3 | 2017-02-27 13:02:58 +0000 | [diff] [blame] | 811 | int ret; |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 812 | /* |
| 813 | * This is to avoid a deadlock condition like below. |
| 814 | * writeback_single_inode(inode) |
| 815 | * - f2fs_write_data_page |
| 816 | * - f2fs_gc -> iput -> evict |
| 817 | * - inode_wait_for_writeback(inode) |
| 818 | */ |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 819 | if ((!inode_unhashed(inode) && inode->i_state & I_SYNC)) { |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 820 | if (!inode->i_nlink && !is_bad_inode(inode)) { |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 821 | /* to avoid evict_inode call simultaneously */ |
| 822 | atomic_inc(&inode->i_count); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 823 | spin_unlock(&inode->i_lock); |
| 824 | |
| 825 | /* some remained atomic pages should discarded */ |
| 826 | if (f2fs_is_atomic_file(inode)) |
Chao Yu | 29b96b5 | 2016-02-06 14:38:29 +0800 | [diff] [blame] | 827 | drop_inmem_pages(inode); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 828 | |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 829 | /* should remain fi->extent_tree for writepage */ |
| 830 | f2fs_destroy_extent_node(inode); |
| 831 | |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 832 | sb_start_intwrite(inode->i_sb); |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 833 | f2fs_i_size_write(inode, 0); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 834 | |
| 835 | if (F2FS_HAS_BLOCKS(inode)) |
Jaegeuk Kim | 9a449e9 | 2016-06-02 13:49:38 -0700 | [diff] [blame] | 836 | f2fs_truncate(inode); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 837 | |
| 838 | sb_end_intwrite(inode->i_sb); |
| 839 | |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 840 | spin_lock(&inode->i_lock); |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 841 | atomic_dec(&inode->i_count); |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 842 | } |
Hou Pengyang | b8d96a3 | 2017-02-27 13:02:58 +0000 | [diff] [blame] | 843 | trace_f2fs_drop_inode(inode, 0); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 844 | return 0; |
Jaegeuk Kim | 06e1bc0 | 2015-05-13 14:35:14 -0700 | [diff] [blame] | 845 | } |
Hou Pengyang | b8d96a3 | 2017-02-27 13:02:58 +0000 | [diff] [blame] | 846 | ret = generic_drop_inode(inode); |
| 847 | trace_f2fs_drop_inode(inode, ret); |
| 848 | return ret; |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 849 | } |
| 850 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 851 | int f2fs_inode_dirtied(struct inode *inode, bool sync) |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 852 | { |
| 853 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 854 | int ret = 0; |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 855 | |
| 856 | spin_lock(&sbi->inode_lock[DIRTY_META]); |
| 857 | if (is_inode_flag_set(inode, FI_DIRTY_INODE)) { |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 858 | ret = 1; |
| 859 | } else { |
| 860 | set_inode_flag(inode, FI_DIRTY_INODE); |
| 861 | stat_inc_dirty_inode(sbi, DIRTY_META); |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 862 | } |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 863 | if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) { |
| 864 | list_add_tail(&F2FS_I(inode)->gdirty_list, |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 865 | &sbi->inode_list[DIRTY_META]); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 866 | inc_page_count(sbi, F2FS_DIRTY_IMETA); |
| 867 | } |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 868 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 869 | return ret; |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void f2fs_inode_synced(struct inode *inode) |
| 873 | { |
| 874 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 875 | |
| 876 | spin_lock(&sbi->inode_lock[DIRTY_META]); |
| 877 | if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) { |
| 878 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
| 879 | return; |
| 880 | } |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 881 | if (!list_empty(&F2FS_I(inode)->gdirty_list)) { |
| 882 | list_del_init(&F2FS_I(inode)->gdirty_list); |
| 883 | dec_page_count(sbi, F2FS_DIRTY_IMETA); |
| 884 | } |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 885 | clear_inode_flag(inode, FI_DIRTY_INODE); |
| 886 | clear_inode_flag(inode, FI_AUTO_RECOVER); |
Jaegeuk Kim | b56ab83 | 2016-06-30 19:09:37 -0700 | [diff] [blame] | 887 | stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META); |
| 888 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
| 889 | } |
| 890 | |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 891 | /* |
| 892 | * f2fs_dirty_inode() is called from __mark_inode_dirty() |
| 893 | * |
| 894 | * We should call set_dirty_inode to write the dirty inode through write_inode. |
| 895 | */ |
| 896 | static void f2fs_dirty_inode(struct inode *inode, int flags) |
| 897 | { |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 898 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 899 | |
| 900 | if (inode->i_ino == F2FS_NODE_INO(sbi) || |
| 901 | inode->i_ino == F2FS_META_INO(sbi)) |
| 902 | return; |
| 903 | |
Jaegeuk Kim | 6d94c74 | 2016-05-20 21:47:24 -0700 | [diff] [blame] | 904 | if (flags == I_DIRTY_TIME) |
| 905 | return; |
| 906 | |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 907 | if (is_inode_flag_set(inode, FI_AUTO_RECOVER)) |
| 908 | clear_inode_flag(inode, FI_AUTO_RECOVER); |
| 909 | |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 910 | f2fs_inode_dirtied(inode, false); |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 911 | } |
| 912 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 913 | static void f2fs_i_callback(struct rcu_head *head) |
| 914 | { |
| 915 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 916 | kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode)); |
| 917 | } |
| 918 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 919 | static void f2fs_destroy_inode(struct inode *inode) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 920 | { |
| 921 | call_rcu(&inode->i_rcu, f2fs_i_callback); |
| 922 | } |
| 923 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 924 | static void destroy_percpu_info(struct f2fs_sb_info *sbi) |
| 925 | { |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 926 | percpu_counter_destroy(&sbi->alloc_valid_block_count); |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 927 | percpu_counter_destroy(&sbi->total_valid_inode_count); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 930 | static void destroy_device_list(struct f2fs_sb_info *sbi) |
| 931 | { |
| 932 | int i; |
| 933 | |
| 934 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 935 | blkdev_put(FDEV(i).bdev, FMODE_EXCL); |
| 936 | #ifdef CONFIG_BLK_DEV_ZONED |
| 937 | kfree(FDEV(i).blkz_type); |
| 938 | #endif |
| 939 | } |
| 940 | kfree(sbi->devs); |
| 941 | } |
| 942 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 943 | static void f2fs_put_super(struct super_block *sb) |
| 944 | { |
| 945 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 946 | int i; |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 947 | bool dropped; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 948 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 949 | f2fs_quota_off_umount(sb); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 950 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 951 | /* prevent remaining shrinker jobs */ |
| 952 | mutex_lock(&sbi->umount_mutex); |
| 953 | |
Jaegeuk Kim | 85dc2f2 | 2015-01-14 17:41:41 -0800 | [diff] [blame] | 954 | /* |
| 955 | * We don't need to do checkpoint when superblock is clean. |
| 956 | * But, the previous checkpoint was not done by umount, it needs to do |
| 957 | * clean checkpoint again. |
| 958 | */ |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 959 | if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) || |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 960 | !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { |
Jaegeuk Kim | 75ab4cb | 2014-09-20 21:57:51 -0700 | [diff] [blame] | 961 | struct cp_control cpc = { |
| 962 | .reason = CP_UMOUNT, |
| 963 | }; |
| 964 | write_checkpoint(sbi, &cpc); |
| 965 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 966 | |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 967 | /* be sure to wait for any on-going discard commands */ |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 968 | dropped = f2fs_wait_discard_bios(sbi); |
Jaegeuk Kim | 4e6a8d9 | 2016-12-29 14:07:53 -0800 | [diff] [blame] | 969 | |
Chao Yu | cf5c759 | 2017-10-04 09:08:37 +0800 | [diff] [blame] | 970 | if (f2fs_discard_en(sbi) && !sbi->discard_blks && !dropped) { |
Chao Yu | 1f43e2a | 2017-04-28 13:56:08 +0800 | [diff] [blame] | 971 | struct cp_control cpc = { |
| 972 | .reason = CP_UMOUNT | CP_TRIMMED, |
| 973 | }; |
| 974 | write_checkpoint(sbi, &cpc); |
| 975 | } |
| 976 | |
Jaegeuk Kim | eca616f | 2015-06-15 14:52:29 -0700 | [diff] [blame] | 977 | /* write_checkpoint can update stat informaion */ |
| 978 | f2fs_destroy_stats(sbi); |
| 979 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 980 | /* |
| 981 | * normally superblock is clean, so we need to release this. |
| 982 | * In addition, EIO will skip do checkpoint, we need this as well. |
| 983 | */ |
Jaegeuk Kim | 74ef924 | 2016-05-02 22:09:56 -0700 | [diff] [blame] | 984 | release_ino_entry(sbi, true); |
Jaegeuk Kim | 6f12ac2 | 2014-08-19 09:48:22 -0700 | [diff] [blame] | 985 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 986 | f2fs_leave_shrinker(sbi); |
| 987 | mutex_unlock(&sbi->umount_mutex); |
| 988 | |
Jaegeuk Kim | 17c1912 | 2016-01-29 08:57:59 -0800 | [diff] [blame] | 989 | /* our cp_error case, we can wait for any writeback page */ |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 990 | f2fs_flush_merged_writes(sbi); |
Jaegeuk Kim | 17c1912 | 2016-01-29 08:57:59 -0800 | [diff] [blame] | 991 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 992 | iput(sbi->node_inode); |
| 993 | iput(sbi->meta_inode); |
| 994 | |
| 995 | /* destroy f2fs internal modules */ |
| 996 | destroy_node_manager(sbi); |
| 997 | destroy_segment_manager(sbi); |
| 998 | |
| 999 | kfree(sbi->ckpt); |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 1000 | |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 1001 | f2fs_unregister_sysfs(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1002 | |
| 1003 | sb->s_fs_info = NULL; |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 1004 | if (sbi->s_chksum_driver) |
| 1005 | crypto_free_shash(sbi->s_chksum_driver); |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 1006 | kfree(sbi->raw_super); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1007 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1008 | destroy_device_list(sbi); |
Chao Yu | b6895e8 | 2017-02-27 18:43:12 +0800 | [diff] [blame] | 1009 | mempool_destroy(sbi->write_io_dummy); |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1010 | #ifdef CONFIG_QUOTA |
| 1011 | for (i = 0; i < MAXQUOTAS; i++) |
| 1012 | kfree(sbi->s_qf_names[i]); |
| 1013 | #endif |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 1014 | destroy_percpu_info(sbi); |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 1015 | for (i = 0; i < NR_PAGE_TYPE; i++) |
| 1016 | kfree(sbi->write_io[i]); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1017 | kfree(sbi); |
| 1018 | } |
| 1019 | |
| 1020 | int f2fs_sync_fs(struct super_block *sb, int sync) |
| 1021 | { |
| 1022 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 1023 | int err = 0; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1024 | |
Jaegeuk Kim | 1f227a3 | 2017-10-23 23:48:49 +0200 | [diff] [blame] | 1025 | if (unlikely(f2fs_cp_error(sbi))) |
| 1026 | return 0; |
| 1027 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 1028 | trace_f2fs_sync_fs(sb, sync); |
| 1029 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1030 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1031 | return -EAGAIN; |
| 1032 | |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 1033 | if (sync) { |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 1034 | struct cp_control cpc; |
| 1035 | |
Jaegeuk Kim | 119ee91 | 2015-01-29 11:45:33 -0800 | [diff] [blame] | 1036 | cpc.reason = __get_cp_reason(sbi); |
| 1037 | |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 1038 | mutex_lock(&sbi->gc_mutex); |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 1039 | err = write_checkpoint(sbi, &cpc); |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 1040 | mutex_unlock(&sbi->gc_mutex); |
Jaegeuk Kim | b747375 | 2013-04-01 08:32:21 +0900 | [diff] [blame] | 1041 | } |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1042 | f2fs_trace_ios(NULL, 1); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1043 | |
Chao Yu | c34f42e | 2015-12-23 17:50:30 +0800 | [diff] [blame] | 1044 | return err; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1045 | } |
| 1046 | |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 1047 | static int f2fs_freeze(struct super_block *sb) |
| 1048 | { |
Jaegeuk Kim | 77888c1 | 2013-05-20 20:28:47 +0900 | [diff] [blame] | 1049 | if (f2fs_readonly(sb)) |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 1050 | return 0; |
| 1051 | |
Jaegeuk Kim | b4b9d34 | 2016-11-04 14:59:15 -0700 | [diff] [blame] | 1052 | /* IO error happened before */ |
| 1053 | if (unlikely(f2fs_cp_error(F2FS_SB(sb)))) |
| 1054 | return -EIO; |
| 1055 | |
| 1056 | /* must be clean, since sync_filesystem() was already called */ |
| 1057 | if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY)) |
| 1058 | return -EINVAL; |
| 1059 | return 0; |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static int f2fs_unfreeze(struct super_block *sb) |
| 1063 | { |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Chao Yu | ddc34e3 | 2017-07-29 00:32:53 +0800 | [diff] [blame] | 1067 | #ifdef CONFIG_QUOTA |
| 1068 | static int f2fs_statfs_project(struct super_block *sb, |
| 1069 | kprojid_t projid, struct kstatfs *buf) |
| 1070 | { |
| 1071 | struct kqid qid; |
| 1072 | struct dquot *dquot; |
| 1073 | u64 limit; |
| 1074 | u64 curblock; |
| 1075 | |
| 1076 | qid = make_kqid_projid(projid); |
| 1077 | dquot = dqget(sb, qid); |
| 1078 | if (IS_ERR(dquot)) |
| 1079 | return PTR_ERR(dquot); |
| 1080 | spin_lock(&dq_data_lock); |
| 1081 | |
| 1082 | limit = (dquot->dq_dqb.dqb_bsoftlimit ? |
| 1083 | dquot->dq_dqb.dqb_bsoftlimit : |
| 1084 | dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits; |
| 1085 | if (limit && buf->f_blocks > limit) { |
| 1086 | curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits; |
| 1087 | buf->f_blocks = limit; |
| 1088 | buf->f_bfree = buf->f_bavail = |
| 1089 | (buf->f_blocks > curblock) ? |
| 1090 | (buf->f_blocks - curblock) : 0; |
| 1091 | } |
| 1092 | |
| 1093 | limit = dquot->dq_dqb.dqb_isoftlimit ? |
| 1094 | dquot->dq_dqb.dqb_isoftlimit : |
| 1095 | dquot->dq_dqb.dqb_ihardlimit; |
| 1096 | if (limit && buf->f_files > limit) { |
| 1097 | buf->f_files = limit; |
| 1098 | buf->f_ffree = |
| 1099 | (buf->f_files > dquot->dq_dqb.dqb_curinodes) ? |
| 1100 | (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0; |
| 1101 | } |
| 1102 | |
| 1103 | spin_unlock(&dq_data_lock); |
| 1104 | dqput(dquot); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1109 | static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 1110 | { |
| 1111 | struct super_block *sb = dentry->d_sb; |
| 1112 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 1113 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
Jaegeuk Kim | f66c027 | 2018-01-03 10:55:07 -0800 | [diff] [blame] | 1114 | block_t total_count, user_block_count, start_count; |
Jaegeuk Kim | 0cc091d | 2017-06-21 20:55:55 -0700 | [diff] [blame] | 1115 | u64 avail_node_count; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1116 | |
| 1117 | total_count = le64_to_cpu(sbi->raw_super->block_count); |
| 1118 | user_block_count = sbi->user_block_count; |
| 1119 | start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1120 | buf->f_type = F2FS_SUPER_MAGIC; |
| 1121 | buf->f_bsize = sbi->blocksize; |
| 1122 | |
| 1123 | buf->f_blocks = total_count - start_count; |
Jaegeuk Kim | f66c027 | 2018-01-03 10:55:07 -0800 | [diff] [blame] | 1124 | buf->f_bfree = user_block_count - valid_user_blocks(sbi) - |
Yunlong Song | 80d4214 | 2017-10-27 20:45:05 +0800 | [diff] [blame] | 1125 | sbi->current_reserved_blocks; |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 1126 | if (buf->f_bfree > sbi->root_reserved_blocks) |
| 1127 | buf->f_bavail = buf->f_bfree - sbi->root_reserved_blocks; |
| 1128 | else |
| 1129 | buf->f_bavail = 0; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1130 | |
Chao Yu | 292c196a | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 1131 | avail_node_count = sbi->total_node_count - sbi->nquota_files - |
| 1132 | F2FS_RESERVED_NODE_NUM; |
Jaegeuk Kim | 0cc091d | 2017-06-21 20:55:55 -0700 | [diff] [blame] | 1133 | |
| 1134 | if (avail_node_count > user_block_count) { |
| 1135 | buf->f_files = user_block_count; |
| 1136 | buf->f_ffree = buf->f_bavail; |
| 1137 | } else { |
| 1138 | buf->f_files = avail_node_count; |
| 1139 | buf->f_ffree = min(avail_node_count - valid_node_count(sbi), |
| 1140 | buf->f_bavail); |
| 1141 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1142 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 1143 | buf->f_namelen = F2FS_NAME_LEN; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1144 | buf->f_fsid.val[0] = (u32)id; |
| 1145 | buf->f_fsid.val[1] = (u32)(id >> 32); |
| 1146 | |
Chao Yu | ddc34e3 | 2017-07-29 00:32:53 +0800 | [diff] [blame] | 1147 | #ifdef CONFIG_QUOTA |
| 1148 | if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) && |
| 1149 | sb_has_quota_limits_enabled(sb, PRJQUOTA)) { |
| 1150 | f2fs_statfs_project(sb, F2FS_I(dentry->d_inode)->i_projid, buf); |
| 1151 | } |
| 1152 | #endif |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1156 | static inline void f2fs_show_quota_options(struct seq_file *seq, |
| 1157 | struct super_block *sb) |
| 1158 | { |
| 1159 | #ifdef CONFIG_QUOTA |
| 1160 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 1161 | |
| 1162 | if (sbi->s_jquota_fmt) { |
| 1163 | char *fmtname = ""; |
| 1164 | |
| 1165 | switch (sbi->s_jquota_fmt) { |
| 1166 | case QFMT_VFS_OLD: |
| 1167 | fmtname = "vfsold"; |
| 1168 | break; |
| 1169 | case QFMT_VFS_V0: |
| 1170 | fmtname = "vfsv0"; |
| 1171 | break; |
| 1172 | case QFMT_VFS_V1: |
| 1173 | fmtname = "vfsv1"; |
| 1174 | break; |
| 1175 | } |
| 1176 | seq_printf(seq, ",jqfmt=%s", fmtname); |
| 1177 | } |
| 1178 | |
| 1179 | if (sbi->s_qf_names[USRQUOTA]) |
| 1180 | seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]); |
| 1181 | |
| 1182 | if (sbi->s_qf_names[GRPQUOTA]) |
| 1183 | seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]); |
| 1184 | |
| 1185 | if (sbi->s_qf_names[PRJQUOTA]) |
| 1186 | seq_show_option(seq, "prjjquota", sbi->s_qf_names[PRJQUOTA]); |
| 1187 | #endif |
| 1188 | } |
| 1189 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1190 | static int f2fs_show_options(struct seq_file *seq, struct dentry *root) |
| 1191 | { |
| 1192 | struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb); |
| 1193 | |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 1194 | if (!f2fs_readonly(sbi->sb) && test_opt(sbi, BG_GC)) { |
| 1195 | if (test_opt(sbi, FORCE_FG_GC)) |
| 1196 | seq_printf(seq, ",background_gc=%s", "sync"); |
| 1197 | else |
| 1198 | seq_printf(seq, ",background_gc=%s", "on"); |
| 1199 | } else { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1200 | seq_printf(seq, ",background_gc=%s", "off"); |
Jaegeuk Kim | 6aefd93 | 2015-10-05 11:02:54 -0700 | [diff] [blame] | 1201 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1202 | if (test_opt(sbi, DISABLE_ROLL_FORWARD)) |
| 1203 | seq_puts(seq, ",disable_roll_forward"); |
| 1204 | if (test_opt(sbi, DISCARD)) |
| 1205 | seq_puts(seq, ",discard"); |
| 1206 | if (test_opt(sbi, NOHEAP)) |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1207 | seq_puts(seq, ",no_heap"); |
| 1208 | else |
| 1209 | seq_puts(seq, ",heap"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1210 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1211 | if (test_opt(sbi, XATTR_USER)) |
| 1212 | seq_puts(seq, ",user_xattr"); |
| 1213 | else |
| 1214 | seq_puts(seq, ",nouser_xattr"); |
Jaegeuk Kim | 444c580 | 2013-08-08 15:16:22 +0900 | [diff] [blame] | 1215 | if (test_opt(sbi, INLINE_XATTR)) |
| 1216 | seq_puts(seq, ",inline_xattr"); |
Chao Yu | 23cf721 | 2017-02-15 10:34:45 +0800 | [diff] [blame] | 1217 | else |
| 1218 | seq_puts(seq, ",noinline_xattr"); |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 1219 | if (test_opt(sbi, INLINE_XATTR_SIZE)) |
| 1220 | seq_printf(seq, ",inline_xattr_size=%u", |
| 1221 | sbi->inline_xattr_size); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1222 | #endif |
| 1223 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 1224 | if (test_opt(sbi, POSIX_ACL)) |
| 1225 | seq_puts(seq, ",acl"); |
| 1226 | else |
| 1227 | seq_puts(seq, ",noacl"); |
| 1228 | #endif |
| 1229 | if (test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Alejandro Martinez Ruiz | aa43507 | 2013-01-25 19:08:59 +0100 | [diff] [blame] | 1230 | seq_puts(seq, ",disable_ext_identify"); |
Huajun Li | 8274de7 | 2013-11-10 23:13:17 +0800 | [diff] [blame] | 1231 | if (test_opt(sbi, INLINE_DATA)) |
| 1232 | seq_puts(seq, ",inline_data"); |
Wanpeng Li | 7534279 | 2015-03-24 10:20:27 +0800 | [diff] [blame] | 1233 | else |
| 1234 | seq_puts(seq, ",noinline_data"); |
Chao Yu | 5efd3c6 | 2014-09-24 18:16:13 +0800 | [diff] [blame] | 1235 | if (test_opt(sbi, INLINE_DENTRY)) |
| 1236 | seq_puts(seq, ",inline_dentry"); |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 1237 | else |
| 1238 | seq_puts(seq, ",noinline_dentry"); |
Gu Zheng | b270ad6 | 2014-04-11 17:49:55 +0800 | [diff] [blame] | 1239 | if (!f2fs_readonly(sbi->sb) && test_opt(sbi, FLUSH_MERGE)) |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 1240 | seq_puts(seq, ",flush_merge"); |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 1241 | if (test_opt(sbi, NOBARRIER)) |
| 1242 | seq_puts(seq, ",nobarrier"); |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 1243 | if (test_opt(sbi, FASTBOOT)) |
| 1244 | seq_puts(seq, ",fastboot"); |
Chao Yu | 8967215 | 2015-02-05 17:55:51 +0800 | [diff] [blame] | 1245 | if (test_opt(sbi, EXTENT_CACHE)) |
| 1246 | seq_puts(seq, ",extent_cache"); |
Jaegeuk Kim | 7daaea2 | 2015-06-25 17:43:04 -0700 | [diff] [blame] | 1247 | else |
| 1248 | seq_puts(seq, ",noextent_cache"); |
Chao Yu | 343f40f | 2015-12-16 13:12:16 +0800 | [diff] [blame] | 1249 | if (test_opt(sbi, DATA_FLUSH)) |
| 1250 | seq_puts(seq, ",data_flush"); |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 1251 | |
| 1252 | seq_puts(seq, ",mode="); |
| 1253 | if (test_opt(sbi, ADAPTIVE)) |
| 1254 | seq_puts(seq, "adaptive"); |
| 1255 | else if (test_opt(sbi, LFS)) |
| 1256 | seq_puts(seq, "lfs"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1257 | seq_printf(seq, ",active_logs=%u", sbi->active_logs); |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 1258 | if (test_opt(sbi, RESERVE_ROOT)) |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 1259 | seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u", |
| 1260 | sbi->root_reserved_blocks, |
| 1261 | from_kuid_munged(&init_user_ns, sbi->s_resuid), |
| 1262 | from_kgid_munged(&init_user_ns, sbi->s_resgid)); |
Jaegeuk Kim | ec91538 | 2016-12-21 17:09:19 -0800 | [diff] [blame] | 1263 | if (F2FS_IO_SIZE_BITS(sbi)) |
| 1264 | seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi)); |
Kaixu Xia | 0cc0dec | 2017-01-27 09:35:37 +0800 | [diff] [blame] | 1265 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 1266 | if (test_opt(sbi, FAULT_INJECTION)) |
Chao Yu | 44529f8 | 2017-06-12 09:44:24 +0800 | [diff] [blame] | 1267 | seq_printf(seq, ",fault_injection=%u", |
| 1268 | sbi->fault_info.inject_rate); |
Kaixu Xia | 0cc0dec | 2017-01-27 09:35:37 +0800 | [diff] [blame] | 1269 | #endif |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1270 | #ifdef CONFIG_QUOTA |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1271 | if (test_opt(sbi, QUOTA)) |
| 1272 | seq_puts(seq, ",quota"); |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1273 | if (test_opt(sbi, USRQUOTA)) |
| 1274 | seq_puts(seq, ",usrquota"); |
| 1275 | if (test_opt(sbi, GRPQUOTA)) |
| 1276 | seq_puts(seq, ",grpquota"); |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 1277 | if (test_opt(sbi, PRJQUOTA)) |
| 1278 | seq_puts(seq, ",prjquota"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1279 | #endif |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1280 | f2fs_show_quota_options(seq, sbi->sb); |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 1281 | if (sbi->whint_mode == WHINT_MODE_USER) |
| 1282 | seq_printf(seq, ",whint_mode=%s", "user-based"); |
Hyunchul Lee | f2e703f | 2018-01-31 11:36:58 +0900 | [diff] [blame] | 1283 | else if (sbi->whint_mode == WHINT_MODE_FS) |
| 1284 | seq_printf(seq, ",whint_mode=%s", "fs-based"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1285 | |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 1286 | if (sbi->alloc_mode == ALLOC_MODE_DEFAULT) |
| 1287 | seq_printf(seq, ",alloc_mode=%s", "default"); |
| 1288 | else if (sbi->alloc_mode == ALLOC_MODE_REUSE) |
| 1289 | seq_printf(seq, ",alloc_mode=%s", "reuse"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1293 | static void default_options(struct f2fs_sb_info *sbi) |
| 1294 | { |
| 1295 | /* init some FS parameters */ |
| 1296 | sbi->active_logs = NR_CURSEG_TYPE; |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 1297 | sbi->inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 1298 | sbi->whint_mode = WHINT_MODE_OFF; |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 1299 | sbi->alloc_mode = ALLOC_MODE_DEFAULT; |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1300 | |
| 1301 | set_opt(sbi, BG_GC); |
Chao Yu | 39133a5 | 2017-02-08 17:39:44 +0800 | [diff] [blame] | 1302 | set_opt(sbi, INLINE_XATTR); |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1303 | set_opt(sbi, INLINE_DATA); |
Chao Yu | 97c1794 | 2016-05-09 19:56:34 +0800 | [diff] [blame] | 1304 | set_opt(sbi, INLINE_DENTRY); |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 1305 | set_opt(sbi, EXTENT_CACHE); |
Jaegeuk Kim | 7a20b8a | 2017-03-24 20:41:45 -0400 | [diff] [blame] | 1306 | set_opt(sbi, NOHEAP); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1307 | sbi->sb->s_flags |= SB_LAZYTIME; |
Jaegeuk Kim | 69e9e42 | 2016-05-20 22:39:20 -0700 | [diff] [blame] | 1308 | set_opt(sbi, FLUSH_MERGE); |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 1309 | if (f2fs_sb_has_blkzoned(sbi->sb)) { |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 1310 | set_opt_mode(sbi, F2FS_MOUNT_LFS); |
| 1311 | set_opt(sbi, DISCARD); |
| 1312 | } else { |
| 1313 | set_opt_mode(sbi, F2FS_MOUNT_ADAPTIVE); |
| 1314 | } |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1315 | |
| 1316 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1317 | set_opt(sbi, XATTR_USER); |
| 1318 | #endif |
| 1319 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 1320 | set_opt(sbi, POSIX_ACL); |
| 1321 | #endif |
Chao Yu | 36dbd32 | 2016-09-26 19:45:05 +0800 | [diff] [blame] | 1322 | |
| 1323 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 1324 | f2fs_build_fault_attr(sbi, 0); |
| 1325 | #endif |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1328 | #ifdef CONFIG_QUOTA |
| 1329 | static int f2fs_enable_quotas(struct super_block *sb); |
| 1330 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1331 | static int f2fs_remount(struct super_block *sb, int *flags, char *data) |
| 1332 | { |
| 1333 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 1334 | struct f2fs_mount_info org_mount_opt; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1335 | unsigned long old_sb_flags; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1336 | int err, active_logs; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1337 | bool need_restart_gc = false; |
| 1338 | bool need_stop_gc = false; |
Chao Yu | 9cd81ce | 2015-09-18 16:55:26 +0800 | [diff] [blame] | 1339 | bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE); |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 1340 | int old_whint_mode = sbi->whint_mode; |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 1341 | int old_alloc_mode = sbi->alloc_mode; |
Chao Yu | 2443b8b | 2016-09-26 19:45:06 +0800 | [diff] [blame] | 1342 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 1343 | struct f2fs_fault_info ffi = sbi->fault_info; |
| 1344 | #endif |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1345 | #ifdef CONFIG_QUOTA |
| 1346 | int s_jquota_fmt; |
| 1347 | char *s_qf_names[MAXQUOTAS]; |
| 1348 | int i, j; |
| 1349 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * Save the old mount options in case we |
| 1353 | * need to restore them. |
| 1354 | */ |
| 1355 | org_mount_opt = sbi->mount_opt; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1356 | old_sb_flags = sb->s_flags; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1357 | active_logs = sbi->active_logs; |
| 1358 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1359 | #ifdef CONFIG_QUOTA |
| 1360 | s_jquota_fmt = sbi->s_jquota_fmt; |
| 1361 | for (i = 0; i < MAXQUOTAS; i++) { |
| 1362 | if (sbi->s_qf_names[i]) { |
| 1363 | s_qf_names[i] = kstrdup(sbi->s_qf_names[i], |
| 1364 | GFP_KERNEL); |
| 1365 | if (!s_qf_names[i]) { |
| 1366 | for (j = 0; j < i; j++) |
| 1367 | kfree(s_qf_names[j]); |
| 1368 | return -ENOMEM; |
| 1369 | } |
| 1370 | } else { |
| 1371 | s_qf_names[i] = NULL; |
| 1372 | } |
| 1373 | } |
| 1374 | #endif |
| 1375 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1376 | /* recover superblocks we couldn't write due to previous RO mount */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1377 | if (!(*flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) { |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1378 | err = f2fs_commit_super(sbi, false); |
| 1379 | f2fs_msg(sb, KERN_INFO, |
| 1380 | "Try to recover all the superblocks, ret: %d", err); |
| 1381 | if (!err) |
| 1382 | clear_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
| 1383 | } |
| 1384 | |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 1385 | default_options(sbi); |
Chao Yu | 26666c8 | 2014-09-15 18:04:44 +0800 | [diff] [blame] | 1386 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1387 | /* parse mount options */ |
| 1388 | err = parse_options(sb, data); |
| 1389 | if (err) |
| 1390 | goto restore_opts; |
| 1391 | |
| 1392 | /* |
| 1393 | * Previous and new state of filesystem is RO, |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1394 | * so skip checking GC and FLUSH_MERGE conditions. |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1395 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1396 | if (f2fs_readonly(sb) && (*flags & SB_RDONLY)) |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1397 | goto skip; |
| 1398 | |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1399 | #ifdef CONFIG_QUOTA |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1400 | if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) { |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1401 | err = dquot_suspend(sb, -1); |
| 1402 | if (err < 0) |
| 1403 | goto restore_opts; |
Jaegeuk Kim | 6279398 | 2018-01-02 11:03:19 -0800 | [diff] [blame] | 1404 | } else if (f2fs_readonly(sb) && !(*flags & MS_RDONLY)) { |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1405 | /* dquot_resume needs RW */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1406 | sb->s_flags &= ~SB_RDONLY; |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1407 | if (sb_any_quota_suspended(sb)) { |
| 1408 | dquot_resume(sb, -1); |
| 1409 | } else if (f2fs_sb_has_quota_ino(sb)) { |
| 1410 | err = f2fs_enable_quotas(sb); |
| 1411 | if (err) |
| 1412 | goto restore_opts; |
| 1413 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1414 | } |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1415 | #endif |
Chao Yu | 9cd81ce | 2015-09-18 16:55:26 +0800 | [diff] [blame] | 1416 | /* disallow enable/disable extent_cache dynamically */ |
| 1417 | if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) { |
| 1418 | err = -EINVAL; |
| 1419 | f2fs_msg(sbi->sb, KERN_WARNING, |
| 1420 | "switch extent_cache option is not allowed"); |
| 1421 | goto restore_opts; |
| 1422 | } |
| 1423 | |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1424 | /* |
| 1425 | * We stop the GC thread if FS is mounted as RO |
| 1426 | * or if background_gc = off is passed in mount |
| 1427 | * option. Also sync the filesystem. |
| 1428 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1429 | if ((*flags & SB_RDONLY) || !test_opt(sbi, BG_GC)) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1430 | if (sbi->gc_thread) { |
| 1431 | stop_gc_thread(sbi); |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1432 | need_restart_gc = true; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1433 | } |
Chao Yu | aba291b | 2014-11-18 11:17:20 +0800 | [diff] [blame] | 1434 | } else if (!sbi->gc_thread) { |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1435 | err = start_gc_thread(sbi); |
| 1436 | if (err) |
| 1437 | goto restore_opts; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1438 | need_stop_gc = true; |
| 1439 | } |
| 1440 | |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 1441 | if (*flags & SB_RDONLY || sbi->whint_mode != old_whint_mode) { |
Jaegeuk Kim | faa0e55 | 2016-03-24 10:29:39 -0700 | [diff] [blame] | 1442 | writeback_inodes_sb(sb, WB_REASON_SYNC); |
| 1443 | sync_inodes_sb(sb); |
| 1444 | |
| 1445 | set_sbi_flag(sbi, SBI_IS_DIRTY); |
| 1446 | set_sbi_flag(sbi, SBI_IS_CLOSE); |
| 1447 | f2fs_sync_fs(sb, 1); |
| 1448 | clear_sbi_flag(sbi, SBI_IS_CLOSE); |
| 1449 | } |
| 1450 | |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1451 | /* |
| 1452 | * We stop issue flush thread if FS is mounted as RO |
| 1453 | * or if flush_merge is not passed in mount option. |
| 1454 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1455 | if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) { |
Jaegeuk Kim | 5eba8c5 | 2016-12-07 16:23:32 -0800 | [diff] [blame] | 1456 | clear_opt(sbi, FLUSH_MERGE); |
| 1457 | destroy_flush_cmd_control(sbi, false); |
| 1458 | } else { |
Gu Zheng | 2163d19 | 2014-04-27 14:21:33 +0800 | [diff] [blame] | 1459 | err = create_flush_cmd_control(sbi); |
| 1460 | if (err) |
Gu Zheng | a688b9d9e | 2014-04-27 14:21:21 +0800 | [diff] [blame] | 1461 | goto restore_gc; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1462 | } |
| 1463 | skip: |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1464 | #ifdef CONFIG_QUOTA |
| 1465 | /* Release old quota file names */ |
| 1466 | for (i = 0; i < MAXQUOTAS; i++) |
| 1467 | kfree(s_qf_names[i]); |
| 1468 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1469 | /* Update the POSIXACL Flag */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1470 | sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | |
| 1471 | (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1472 | |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 1473 | limit_reserve_root(sbi); |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1474 | return 0; |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1475 | restore_gc: |
| 1476 | if (need_restart_gc) { |
| 1477 | if (start_gc_thread(sbi)) |
| 1478 | f2fs_msg(sbi->sb, KERN_WARNING, |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1479 | "background gc thread has stopped"); |
Gu Zheng | 876dc59 | 2014-04-11 17:50:00 +0800 | [diff] [blame] | 1480 | } else if (need_stop_gc) { |
| 1481 | stop_gc_thread(sbi); |
| 1482 | } |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1483 | restore_opts: |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1484 | #ifdef CONFIG_QUOTA |
| 1485 | sbi->s_jquota_fmt = s_jquota_fmt; |
| 1486 | for (i = 0; i < MAXQUOTAS; i++) { |
| 1487 | kfree(sbi->s_qf_names[i]); |
| 1488 | sbi->s_qf_names[i] = s_qf_names[i]; |
| 1489 | } |
| 1490 | #endif |
Jaegeuk Kim | 0793962 | 2018-02-18 08:50:49 -0800 | [diff] [blame] | 1491 | sbi->alloc_mode = old_alloc_mode; |
Hyunchul Lee | 0cdd319 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 1492 | sbi->whint_mode = old_whint_mode; |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1493 | sbi->mount_opt = org_mount_opt; |
| 1494 | sbi->active_logs = active_logs; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1495 | sb->s_flags = old_sb_flags; |
Chao Yu | 2443b8b | 2016-09-26 19:45:06 +0800 | [diff] [blame] | 1496 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
| 1497 | sbi->fault_info = ffi; |
| 1498 | #endif |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1499 | return err; |
| 1500 | } |
| 1501 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1502 | #ifdef CONFIG_QUOTA |
| 1503 | /* Read data from quotafile */ |
| 1504 | static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data, |
| 1505 | size_t len, loff_t off) |
| 1506 | { |
| 1507 | struct inode *inode = sb_dqopt(sb)->files[type]; |
| 1508 | struct address_space *mapping = inode->i_mapping; |
| 1509 | block_t blkidx = F2FS_BYTES_TO_BLK(off); |
| 1510 | int offset = off & (sb->s_blocksize - 1); |
| 1511 | int tocopy; |
| 1512 | size_t toread; |
| 1513 | loff_t i_size = i_size_read(inode); |
| 1514 | struct page *page; |
| 1515 | char *kaddr; |
| 1516 | |
| 1517 | if (off > i_size) |
| 1518 | return 0; |
| 1519 | |
| 1520 | if (off + len > i_size) |
| 1521 | len = i_size - off; |
| 1522 | toread = len; |
| 1523 | while (toread > 0) { |
| 1524 | tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread); |
| 1525 | repeat: |
| 1526 | page = read_mapping_page(mapping, blkidx, NULL); |
Jaegeuk Kim | 4e46a02 | 2017-10-19 09:43:56 -0700 | [diff] [blame] | 1527 | if (IS_ERR(page)) { |
| 1528 | if (PTR_ERR(page) == -ENOMEM) { |
| 1529 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 1530 | goto repeat; |
| 1531 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1532 | return PTR_ERR(page); |
Jaegeuk Kim | 4e46a02 | 2017-10-19 09:43:56 -0700 | [diff] [blame] | 1533 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1534 | |
| 1535 | lock_page(page); |
| 1536 | |
| 1537 | if (unlikely(page->mapping != mapping)) { |
| 1538 | f2fs_put_page(page, 1); |
| 1539 | goto repeat; |
| 1540 | } |
| 1541 | if (unlikely(!PageUptodate(page))) { |
| 1542 | f2fs_put_page(page, 1); |
| 1543 | return -EIO; |
| 1544 | } |
| 1545 | |
| 1546 | kaddr = kmap_atomic(page); |
| 1547 | memcpy(data, kaddr + offset, tocopy); |
| 1548 | kunmap_atomic(kaddr); |
| 1549 | f2fs_put_page(page, 1); |
| 1550 | |
| 1551 | offset = 0; |
| 1552 | toread -= tocopy; |
| 1553 | data += tocopy; |
| 1554 | blkidx++; |
| 1555 | } |
| 1556 | return len; |
| 1557 | } |
| 1558 | |
| 1559 | /* Write to quotafile */ |
| 1560 | static ssize_t f2fs_quota_write(struct super_block *sb, int type, |
| 1561 | const char *data, size_t len, loff_t off) |
| 1562 | { |
| 1563 | struct inode *inode = sb_dqopt(sb)->files[type]; |
| 1564 | struct address_space *mapping = inode->i_mapping; |
| 1565 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 1566 | int offset = off & (sb->s_blocksize - 1); |
| 1567 | size_t towrite = len; |
| 1568 | struct page *page; |
| 1569 | char *kaddr; |
| 1570 | int err = 0; |
| 1571 | int tocopy; |
| 1572 | |
| 1573 | while (towrite > 0) { |
| 1574 | tocopy = min_t(unsigned long, sb->s_blocksize - offset, |
| 1575 | towrite); |
Jaegeuk Kim | 4e46a02 | 2017-10-19 09:43:56 -0700 | [diff] [blame] | 1576 | retry: |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1577 | err = a_ops->write_begin(NULL, mapping, off, tocopy, 0, |
| 1578 | &page, NULL); |
Jaegeuk Kim | 4e46a02 | 2017-10-19 09:43:56 -0700 | [diff] [blame] | 1579 | if (unlikely(err)) { |
| 1580 | if (err == -ENOMEM) { |
| 1581 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 1582 | goto retry; |
| 1583 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1584 | break; |
Jaegeuk Kim | 4e46a02 | 2017-10-19 09:43:56 -0700 | [diff] [blame] | 1585 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1586 | |
| 1587 | kaddr = kmap_atomic(page); |
| 1588 | memcpy(kaddr + offset, data, tocopy); |
| 1589 | kunmap_atomic(kaddr); |
| 1590 | flush_dcache_page(page); |
| 1591 | |
| 1592 | a_ops->write_end(NULL, mapping, off, tocopy, tocopy, |
| 1593 | page, NULL); |
| 1594 | offset = 0; |
| 1595 | towrite -= tocopy; |
| 1596 | off += tocopy; |
| 1597 | data += tocopy; |
| 1598 | cond_resched(); |
| 1599 | } |
| 1600 | |
| 1601 | if (len == towrite) |
Jaegeuk Kim | 6e5b5d4 | 2017-10-19 12:07:11 -0700 | [diff] [blame] | 1602 | return err; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1603 | inode->i_mtime = inode->i_ctime = current_time(inode); |
| 1604 | f2fs_mark_inode_dirty_sync(inode, false); |
| 1605 | return len - towrite; |
| 1606 | } |
| 1607 | |
| 1608 | static struct dquot **f2fs_get_dquots(struct inode *inode) |
| 1609 | { |
| 1610 | return F2FS_I(inode)->i_dquot; |
| 1611 | } |
| 1612 | |
| 1613 | static qsize_t *f2fs_get_reserved_space(struct inode *inode) |
| 1614 | { |
| 1615 | return &F2FS_I(inode)->i_reserved_quota; |
| 1616 | } |
| 1617 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1618 | static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type) |
| 1619 | { |
| 1620 | return dquot_quota_on_mount(sbi->sb, sbi->s_qf_names[type], |
| 1621 | sbi->s_jquota_fmt, type); |
| 1622 | } |
| 1623 | |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1624 | int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly) |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1625 | { |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1626 | int enabled = 0; |
| 1627 | int i, err; |
| 1628 | |
| 1629 | if (f2fs_sb_has_quota_ino(sbi->sb) && rdonly) { |
| 1630 | err = f2fs_enable_quotas(sbi->sb); |
| 1631 | if (err) { |
| 1632 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1633 | "Cannot turn on quota_ino: %d", err); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | return 1; |
| 1637 | } |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1638 | |
| 1639 | for (i = 0; i < MAXQUOTAS; i++) { |
| 1640 | if (sbi->s_qf_names[i]) { |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1641 | err = f2fs_quota_on_mount(sbi, i); |
| 1642 | if (!err) { |
| 1643 | enabled = 1; |
| 1644 | continue; |
| 1645 | } |
| 1646 | f2fs_msg(sbi->sb, KERN_ERR, |
| 1647 | "Cannot turn on quotas: %d on %d", err, i); |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1648 | } |
| 1649 | } |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1650 | return enabled; |
| 1651 | } |
| 1652 | |
| 1653 | static int f2fs_quota_enable(struct super_block *sb, int type, int format_id, |
| 1654 | unsigned int flags) |
| 1655 | { |
| 1656 | struct inode *qf_inode; |
| 1657 | unsigned long qf_inum; |
| 1658 | int err; |
| 1659 | |
| 1660 | BUG_ON(!f2fs_sb_has_quota_ino(sb)); |
| 1661 | |
| 1662 | qf_inum = f2fs_qf_ino(sb, type); |
| 1663 | if (!qf_inum) |
| 1664 | return -EPERM; |
| 1665 | |
| 1666 | qf_inode = f2fs_iget(sb, qf_inum); |
| 1667 | if (IS_ERR(qf_inode)) { |
| 1668 | f2fs_msg(sb, KERN_ERR, |
| 1669 | "Bad quota inode %u:%lu", type, qf_inum); |
| 1670 | return PTR_ERR(qf_inode); |
| 1671 | } |
| 1672 | |
| 1673 | /* Don't account quota for quota files to avoid recursion */ |
| 1674 | qf_inode->i_flags |= S_NOQUOTA; |
| 1675 | err = dquot_enable(qf_inode, type, format_id, flags); |
| 1676 | iput(qf_inode); |
| 1677 | return err; |
| 1678 | } |
| 1679 | |
| 1680 | static int f2fs_enable_quotas(struct super_block *sb) |
| 1681 | { |
| 1682 | int type, err = 0; |
| 1683 | unsigned long qf_inum; |
| 1684 | bool quota_mopt[MAXQUOTAS] = { |
| 1685 | test_opt(F2FS_SB(sb), USRQUOTA), |
| 1686 | test_opt(F2FS_SB(sb), GRPQUOTA), |
| 1687 | test_opt(F2FS_SB(sb), PRJQUOTA), |
| 1688 | }; |
| 1689 | |
| 1690 | sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; |
| 1691 | for (type = 0; type < MAXQUOTAS; type++) { |
| 1692 | qf_inum = f2fs_qf_ino(sb, type); |
| 1693 | if (qf_inum) { |
| 1694 | err = f2fs_quota_enable(sb, type, QFMT_VFS_V1, |
| 1695 | DQUOT_USAGE_ENABLED | |
| 1696 | (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0)); |
| 1697 | if (err) { |
| 1698 | f2fs_msg(sb, KERN_ERR, |
| 1699 | "Failed to enable quota tracking " |
| 1700 | "(type=%d, err=%d). Please run " |
| 1701 | "fsck to fix.", type, err); |
| 1702 | for (type--; type >= 0; type--) |
| 1703 | dquot_quota_off(sb, type); |
| 1704 | return err; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | return 0; |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1709 | } |
| 1710 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1711 | static int f2fs_quota_sync(struct super_block *sb, int type) |
| 1712 | { |
| 1713 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1714 | int cnt; |
| 1715 | int ret; |
| 1716 | |
| 1717 | ret = dquot_writeback_dquots(sb, type); |
| 1718 | if (ret) |
| 1719 | return ret; |
| 1720 | |
| 1721 | /* |
| 1722 | * Now when everything is written we can discard the pagecache so |
| 1723 | * that userspace sees the changes. |
| 1724 | */ |
| 1725 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1726 | if (type != -1 && cnt != type) |
| 1727 | continue; |
| 1728 | if (!sb_has_quota_active(sb, cnt)) |
| 1729 | continue; |
| 1730 | |
| 1731 | ret = filemap_write_and_wait(dqopt->files[cnt]->i_mapping); |
| 1732 | if (ret) |
| 1733 | return ret; |
| 1734 | |
| 1735 | inode_lock(dqopt->files[cnt]); |
| 1736 | truncate_inode_pages(&dqopt->files[cnt]->i_data, 0); |
| 1737 | inode_unlock(dqopt->files[cnt]); |
| 1738 | } |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static int f2fs_quota_on(struct super_block *sb, int type, int format_id, |
| 1743 | const struct path *path) |
| 1744 | { |
| 1745 | struct inode *inode; |
| 1746 | int err; |
| 1747 | |
Chao Yu | 9a20d39 | 2017-08-07 16:37:59 +0800 | [diff] [blame] | 1748 | err = f2fs_quota_sync(sb, type); |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1749 | if (err) |
| 1750 | return err; |
| 1751 | |
| 1752 | err = dquot_quota_on(sb, type, format_id, path); |
| 1753 | if (err) |
| 1754 | return err; |
| 1755 | |
| 1756 | inode = d_inode(path->dentry); |
| 1757 | |
| 1758 | inode_lock(inode); |
| 1759 | F2FS_I(inode)->i_flags |= FS_NOATIME_FL | FS_IMMUTABLE_FL; |
| 1760 | inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, |
| 1761 | S_NOATIME | S_IMMUTABLE); |
| 1762 | inode_unlock(inode); |
| 1763 | f2fs_mark_inode_dirty_sync(inode, false); |
| 1764 | |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | static int f2fs_quota_off(struct super_block *sb, int type) |
| 1769 | { |
| 1770 | struct inode *inode = sb_dqopt(sb)->files[type]; |
| 1771 | int err; |
| 1772 | |
| 1773 | if (!inode || !igrab(inode)) |
| 1774 | return dquot_quota_off(sb, type); |
| 1775 | |
Chao Yu | 9a20d39 | 2017-08-07 16:37:59 +0800 | [diff] [blame] | 1776 | f2fs_quota_sync(sb, type); |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1777 | |
| 1778 | err = dquot_quota_off(sb, type); |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 1779 | if (err || f2fs_sb_has_quota_ino(sb)) |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1780 | goto out_put; |
| 1781 | |
| 1782 | inode_lock(inode); |
| 1783 | F2FS_I(inode)->i_flags &= ~(FS_NOATIME_FL | FS_IMMUTABLE_FL); |
| 1784 | inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); |
| 1785 | inode_unlock(inode); |
| 1786 | f2fs_mark_inode_dirty_sync(inode, false); |
| 1787 | out_put: |
| 1788 | iput(inode); |
| 1789 | return err; |
| 1790 | } |
| 1791 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1792 | void f2fs_quota_off_umount(struct super_block *sb) |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1793 | { |
| 1794 | int type; |
| 1795 | |
| 1796 | for (type = 0; type < MAXQUOTAS; type++) |
| 1797 | f2fs_quota_off(sb, type); |
| 1798 | } |
| 1799 | |
Wei Yongjun | 94b1e10 | 2018-01-05 09:41:20 +0000 | [diff] [blame] | 1800 | static int f2fs_get_projid(struct inode *inode, kprojid_t *projid) |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 1801 | { |
| 1802 | *projid = F2FS_I(inode)->i_projid; |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1806 | static const struct dquot_operations f2fs_quota_operations = { |
| 1807 | .get_reserved_space = f2fs_get_reserved_space, |
| 1808 | .write_dquot = dquot_commit, |
| 1809 | .acquire_dquot = dquot_acquire, |
| 1810 | .release_dquot = dquot_release, |
| 1811 | .mark_dirty = dquot_mark_dquot_dirty, |
| 1812 | .write_info = dquot_commit_info, |
| 1813 | .alloc_dquot = dquot_alloc, |
| 1814 | .destroy_dquot = dquot_destroy, |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 1815 | .get_projid = f2fs_get_projid, |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1816 | .get_next_id = dquot_get_next_id, |
| 1817 | }; |
| 1818 | |
| 1819 | static const struct quotactl_ops f2fs_quotactl_ops = { |
| 1820 | .quota_on = f2fs_quota_on, |
| 1821 | .quota_off = f2fs_quota_off, |
| 1822 | .quota_sync = f2fs_quota_sync, |
| 1823 | .get_state = dquot_get_state, |
| 1824 | .set_info = dquot_set_dqinfo, |
| 1825 | .get_dqblk = dquot_get_dqblk, |
| 1826 | .set_dqblk = dquot_set_dqblk, |
| 1827 | .get_nextdqblk = dquot_get_next_dqblk, |
| 1828 | }; |
| 1829 | #else |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 1830 | void f2fs_quota_off_umount(struct super_block *sb) |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1831 | { |
| 1832 | } |
| 1833 | #endif |
| 1834 | |
Arvind Yadav | f62fc9f | 2017-08-31 15:06:24 +0530 | [diff] [blame] | 1835 | static const struct super_operations f2fs_sops = { |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1836 | .alloc_inode = f2fs_alloc_inode, |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 1837 | .drop_inode = f2fs_drop_inode, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1838 | .destroy_inode = f2fs_destroy_inode, |
| 1839 | .write_inode = f2fs_write_inode, |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 1840 | .dirty_inode = f2fs_dirty_inode, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1841 | .show_options = f2fs_show_options, |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1842 | #ifdef CONFIG_QUOTA |
| 1843 | .quota_read = f2fs_quota_read, |
| 1844 | .quota_write = f2fs_quota_write, |
| 1845 | .get_dquots = f2fs_get_dquots, |
| 1846 | #endif |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1847 | .evict_inode = f2fs_evict_inode, |
| 1848 | .put_super = f2fs_put_super, |
| 1849 | .sync_fs = f2fs_sync_fs, |
Changman Lee | d6212a5 | 2013-01-29 18:30:07 +0900 | [diff] [blame] | 1850 | .freeze_fs = f2fs_freeze, |
| 1851 | .unfreeze_fs = f2fs_unfreeze, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1852 | .statfs = f2fs_statfs, |
Namjae Jeon | 696c018 | 2013-06-16 09:48:48 +0900 | [diff] [blame] | 1853 | .remount_fs = f2fs_remount, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1854 | }; |
| 1855 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1856 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 1857 | static int f2fs_get_context(struct inode *inode, void *ctx, size_t len) |
| 1858 | { |
| 1859 | return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, |
| 1860 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 1861 | ctx, len, NULL); |
| 1862 | } |
| 1863 | |
| 1864 | static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len, |
| 1865 | void *fs_data) |
| 1866 | { |
| 1867 | return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, |
| 1868 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 1869 | ctx, len, fs_data, XATTR_CREATE); |
| 1870 | } |
| 1871 | |
| 1872 | static unsigned f2fs_max_namelen(struct inode *inode) |
| 1873 | { |
| 1874 | return S_ISLNK(inode->i_mode) ? |
| 1875 | inode->i_sb->s_blocksize : F2FS_NAME_LEN; |
| 1876 | } |
| 1877 | |
Eric Biggers | 6f69f0e | 2017-02-07 12:42:10 -0800 | [diff] [blame] | 1878 | static const struct fscrypt_operations f2fs_cryptops = { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 1879 | .key_prefix = "f2fs:", |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1880 | .get_context = f2fs_get_context, |
| 1881 | .set_context = f2fs_set_context, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1882 | .empty_dir = f2fs_empty_dir, |
| 1883 | .max_namelen = f2fs_max_namelen, |
| 1884 | }; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1885 | #endif |
| 1886 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1887 | static struct inode *f2fs_nfs_get_inode(struct super_block *sb, |
| 1888 | u64 ino, u32 generation) |
| 1889 | { |
| 1890 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 1891 | struct inode *inode; |
| 1892 | |
Chao Yu | d6b7d4b | 2014-06-12 13:23:41 +0800 | [diff] [blame] | 1893 | if (check_nid_range(sbi, ino)) |
Chao Yu | 910bb12 | 2014-03-12 17:08:36 +0800 | [diff] [blame] | 1894 | return ERR_PTR(-ESTALE); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1895 | |
| 1896 | /* |
| 1897 | * f2fs_iget isn't quite right if the inode is currently unallocated! |
| 1898 | * However f2fs_iget currently does appropriate checks to handle stale |
| 1899 | * inodes so everything is OK. |
| 1900 | */ |
| 1901 | inode = f2fs_iget(sb, ino); |
| 1902 | if (IS_ERR(inode)) |
| 1903 | return ERR_CAST(inode); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1904 | if (unlikely(generation && inode->i_generation != generation)) { |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1905 | /* we didn't find the right inode.. */ |
| 1906 | iput(inode); |
| 1907 | return ERR_PTR(-ESTALE); |
| 1908 | } |
| 1909 | return inode; |
| 1910 | } |
| 1911 | |
| 1912 | static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 1913 | int fh_len, int fh_type) |
| 1914 | { |
| 1915 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 1916 | f2fs_nfs_get_inode); |
| 1917 | } |
| 1918 | |
| 1919 | static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 1920 | int fh_len, int fh_type) |
| 1921 | { |
| 1922 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 1923 | f2fs_nfs_get_inode); |
| 1924 | } |
| 1925 | |
| 1926 | static const struct export_operations f2fs_export_ops = { |
| 1927 | .fh_to_dentry = f2fs_fh_to_dentry, |
| 1928 | .fh_to_parent = f2fs_fh_to_parent, |
| 1929 | .get_parent = f2fs_get_parent, |
| 1930 | }; |
| 1931 | |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 1932 | static loff_t max_file_blocks(void) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1933 | { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 1934 | loff_t result = 0; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1935 | loff_t leaf_count = ADDRS_PER_BLOCK; |
| 1936 | |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 1937 | /* |
| 1938 | * note: previously, result is equal to (DEF_ADDRS_PER_INODE - |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 1939 | * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 1940 | * space in inode.i_addr, it will be more safe to reassign |
| 1941 | * result as zero. |
| 1942 | */ |
| 1943 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1944 | /* two direct node blocks */ |
| 1945 | result += (leaf_count * 2); |
| 1946 | |
| 1947 | /* two indirect node blocks */ |
| 1948 | leaf_count *= NIDS_PER_BLOCK; |
| 1949 | result += (leaf_count * 2); |
| 1950 | |
| 1951 | /* one double indirect node block */ |
| 1952 | leaf_count *= NIDS_PER_BLOCK; |
| 1953 | result += leaf_count; |
| 1954 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 1955 | return result; |
| 1956 | } |
| 1957 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1958 | static int __f2fs_commit_super(struct buffer_head *bh, |
| 1959 | struct f2fs_super_block *super) |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1960 | { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1961 | lock_buffer(bh); |
| 1962 | if (super) |
| 1963 | memcpy(bh->b_data + F2FS_SUPER_OFFSET, super, sizeof(*super)); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1964 | set_buffer_dirty(bh); |
| 1965 | unlock_buffer(bh); |
| 1966 | |
| 1967 | /* it's rare case, we can do fua all the time */ |
Jan Kara | 3adc5fcb | 2017-05-02 17:03:47 +0200 | [diff] [blame] | 1968 | return __sync_dirty_buffer(bh, REQ_SYNC | REQ_PREFLUSH | REQ_FUA); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1971 | static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1972 | struct buffer_head *bh) |
| 1973 | { |
| 1974 | struct f2fs_super_block *raw_super = (struct f2fs_super_block *) |
| 1975 | (bh->b_data + F2FS_SUPER_OFFSET); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 1976 | struct super_block *sb = sbi->sb; |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1977 | u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); |
| 1978 | u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr); |
| 1979 | u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr); |
| 1980 | u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr); |
| 1981 | u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); |
| 1982 | u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); |
| 1983 | u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt); |
| 1984 | u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit); |
| 1985 | u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat); |
| 1986 | u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa); |
| 1987 | u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main); |
| 1988 | u32 segment_count = le32_to_cpu(raw_super->segment_count); |
| 1989 | u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 1990 | u64 main_end_blkaddr = main_blkaddr + |
| 1991 | (segment_count_main << log_blocks_per_seg); |
| 1992 | u64 seg_end_blkaddr = segment0_blkaddr + |
| 1993 | (segment_count << log_blocks_per_seg); |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 1994 | |
| 1995 | if (segment0_blkaddr != cp_blkaddr) { |
| 1996 | f2fs_msg(sb, KERN_INFO, |
| 1997 | "Mismatch start address, segment0(%u) cp_blkaddr(%u)", |
| 1998 | segment0_blkaddr, cp_blkaddr); |
| 1999 | return true; |
| 2000 | } |
| 2001 | |
| 2002 | if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) != |
| 2003 | sit_blkaddr) { |
| 2004 | f2fs_msg(sb, KERN_INFO, |
| 2005 | "Wrong CP boundary, start(%u) end(%u) blocks(%u)", |
| 2006 | cp_blkaddr, sit_blkaddr, |
| 2007 | segment_count_ckpt << log_blocks_per_seg); |
| 2008 | return true; |
| 2009 | } |
| 2010 | |
| 2011 | if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) != |
| 2012 | nat_blkaddr) { |
| 2013 | f2fs_msg(sb, KERN_INFO, |
| 2014 | "Wrong SIT boundary, start(%u) end(%u) blocks(%u)", |
| 2015 | sit_blkaddr, nat_blkaddr, |
| 2016 | segment_count_sit << log_blocks_per_seg); |
| 2017 | return true; |
| 2018 | } |
| 2019 | |
| 2020 | if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) != |
| 2021 | ssa_blkaddr) { |
| 2022 | f2fs_msg(sb, KERN_INFO, |
| 2023 | "Wrong NAT boundary, start(%u) end(%u) blocks(%u)", |
| 2024 | nat_blkaddr, ssa_blkaddr, |
| 2025 | segment_count_nat << log_blocks_per_seg); |
| 2026 | return true; |
| 2027 | } |
| 2028 | |
| 2029 | if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) != |
| 2030 | main_blkaddr) { |
| 2031 | f2fs_msg(sb, KERN_INFO, |
| 2032 | "Wrong SSA boundary, start(%u) end(%u) blocks(%u)", |
| 2033 | ssa_blkaddr, main_blkaddr, |
| 2034 | segment_count_ssa << log_blocks_per_seg); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2038 | if (main_end_blkaddr > seg_end_blkaddr) { |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2039 | f2fs_msg(sb, KERN_INFO, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2040 | "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)", |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2041 | main_blkaddr, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2042 | segment0_blkaddr + |
| 2043 | (segment_count << log_blocks_per_seg), |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2044 | segment_count_main << log_blocks_per_seg); |
| 2045 | return true; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2046 | } else if (main_end_blkaddr < seg_end_blkaddr) { |
| 2047 | int err = 0; |
| 2048 | char *res; |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2049 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2050 | /* fix in-memory information all the time */ |
| 2051 | raw_super->segment_count = cpu_to_le32((main_end_blkaddr - |
| 2052 | segment0_blkaddr) >> log_blocks_per_seg); |
| 2053 | |
| 2054 | if (f2fs_readonly(sb) || bdev_read_only(sb->s_bdev)) { |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2055 | set_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2056 | res = "internally"; |
| 2057 | } else { |
| 2058 | err = __f2fs_commit_super(bh, NULL); |
| 2059 | res = err ? "failed" : "done"; |
| 2060 | } |
| 2061 | f2fs_msg(sb, KERN_INFO, |
| 2062 | "Fix alignment : %s, start(%u) end(%u) block(%u)", |
| 2063 | res, main_blkaddr, |
| 2064 | segment0_blkaddr + |
| 2065 | (segment_count << log_blocks_per_seg), |
| 2066 | segment_count_main << log_blocks_per_seg); |
| 2067 | if (err) |
| 2068 | return true; |
| 2069 | } |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2070 | return false; |
| 2071 | } |
| 2072 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2073 | static int sanity_check_raw_super(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2074 | struct buffer_head *bh) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2075 | { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2076 | struct f2fs_super_block *raw_super = (struct f2fs_super_block *) |
| 2077 | (bh->b_data + F2FS_SUPER_OFFSET); |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2078 | struct super_block *sb = sbi->sb; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2079 | unsigned int blocksize; |
| 2080 | |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2081 | if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) { |
| 2082 | f2fs_msg(sb, KERN_INFO, |
| 2083 | "Magic Mismatch, valid(0x%x) - read(0x%x)", |
| 2084 | F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2085 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2086 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2087 | |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 2088 | /* Currently, support only 4KB page cache size */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2089 | if (F2FS_BLKSIZE != PAGE_SIZE) { |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 2090 | f2fs_msg(sb, KERN_INFO, |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 2091 | "Invalid page_cache_size (%lu), supports only 4KB\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2092 | PAGE_SIZE); |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 2093 | return 1; |
| 2094 | } |
| 2095 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2096 | /* Currently, support only 4KB block size */ |
| 2097 | blocksize = 1 << le32_to_cpu(raw_super->log_blocksize); |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 2098 | if (blocksize != F2FS_BLKSIZE) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2099 | f2fs_msg(sb, KERN_INFO, |
| 2100 | "Invalid blocksize (%u), supports only 4KB\n", |
| 2101 | blocksize); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2102 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2103 | } |
majianpeng | 5c9b469 | 2013-02-01 19:07:57 +0800 | [diff] [blame] | 2104 | |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2105 | /* check log blocks per segment */ |
| 2106 | if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) { |
| 2107 | f2fs_msg(sb, KERN_INFO, |
| 2108 | "Invalid log blocks per segment (%u)\n", |
| 2109 | le32_to_cpu(raw_super->log_blocks_per_seg)); |
| 2110 | return 1; |
| 2111 | } |
| 2112 | |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 2113 | /* Currently, support 512/1024/2048/4096 bytes sector size */ |
| 2114 | if (le32_to_cpu(raw_super->log_sectorsize) > |
| 2115 | F2FS_MAX_LOG_SECTOR_SIZE || |
| 2116 | le32_to_cpu(raw_super->log_sectorsize) < |
| 2117 | F2FS_MIN_LOG_SECTOR_SIZE) { |
| 2118 | f2fs_msg(sb, KERN_INFO, "Invalid log sectorsize (%u)", |
| 2119 | le32_to_cpu(raw_super->log_sectorsize)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2120 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2121 | } |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 2122 | if (le32_to_cpu(raw_super->log_sectors_per_block) + |
| 2123 | le32_to_cpu(raw_super->log_sectorsize) != |
| 2124 | F2FS_MAX_LOG_SECTOR_SIZE) { |
| 2125 | f2fs_msg(sb, KERN_INFO, |
| 2126 | "Invalid log sectors per block(%u) log sectorsize(%u)", |
| 2127 | le32_to_cpu(raw_super->log_sectors_per_block), |
| 2128 | le32_to_cpu(raw_super->log_sectorsize)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2129 | return 1; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2130 | } |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2131 | |
| 2132 | /* check reserved ino info */ |
| 2133 | if (le32_to_cpu(raw_super->node_ino) != 1 || |
| 2134 | le32_to_cpu(raw_super->meta_ino) != 2 || |
| 2135 | le32_to_cpu(raw_super->root_ino) != 3) { |
| 2136 | f2fs_msg(sb, KERN_INFO, |
| 2137 | "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)", |
| 2138 | le32_to_cpu(raw_super->node_ino), |
| 2139 | le32_to_cpu(raw_super->meta_ino), |
| 2140 | le32_to_cpu(raw_super->root_ino)); |
| 2141 | return 1; |
| 2142 | } |
| 2143 | |
Jin Qian | b9dd461 | 2017-04-25 16:28:48 -0700 | [diff] [blame] | 2144 | if (le32_to_cpu(raw_super->segment_count) > F2FS_MAX_SEGMENT) { |
| 2145 | f2fs_msg(sb, KERN_INFO, |
| 2146 | "Invalid segment count (%u)", |
| 2147 | le32_to_cpu(raw_super->segment_count)); |
| 2148 | return 1; |
| 2149 | } |
| 2150 | |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2151 | /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2152 | if (sanity_check_area_boundary(sbi, bh)) |
Chao Yu | 9a59b62 | 2015-12-15 09:58:18 +0800 | [diff] [blame] | 2153 | return 1; |
| 2154 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2155 | return 0; |
| 2156 | } |
| 2157 | |
Shawn Lin | 984ec63 | 2016-02-17 11:26:32 +0800 | [diff] [blame] | 2158 | int sanity_check_ckpt(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2159 | { |
| 2160 | unsigned int total, fsmeta; |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 2161 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
| 2162 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
Jaegeuk Kim | 2040fce8 | 2016-12-05 13:56:04 -0800 | [diff] [blame] | 2163 | unsigned int ovp_segments, reserved_segments; |
Jin Qian | 15d3042 | 2017-05-15 10:45:08 -0700 | [diff] [blame] | 2164 | unsigned int main_segs, blocks_per_seg; |
| 2165 | int i; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2166 | |
| 2167 | total = le32_to_cpu(raw_super->segment_count); |
| 2168 | fsmeta = le32_to_cpu(raw_super->segment_count_ckpt); |
| 2169 | fsmeta += le32_to_cpu(raw_super->segment_count_sit); |
| 2170 | fsmeta += le32_to_cpu(raw_super->segment_count_nat); |
| 2171 | fsmeta += le32_to_cpu(ckpt->rsvd_segment_count); |
| 2172 | fsmeta += le32_to_cpu(raw_super->segment_count_ssa); |
| 2173 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2174 | if (unlikely(fsmeta >= total)) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2175 | return 1; |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 2176 | |
Jaegeuk Kim | 2040fce8 | 2016-12-05 13:56:04 -0800 | [diff] [blame] | 2177 | ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); |
| 2178 | reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); |
| 2179 | |
| 2180 | if (unlikely(fsmeta < F2FS_MIN_SEGMENTS || |
| 2181 | ovp_segments == 0 || reserved_segments == 0)) { |
| 2182 | f2fs_msg(sbi->sb, KERN_ERR, |
| 2183 | "Wrong layout: check mkfs.f2fs version"); |
| 2184 | return 1; |
| 2185 | } |
| 2186 | |
Jin Qian | 15d3042 | 2017-05-15 10:45:08 -0700 | [diff] [blame] | 2187 | main_segs = le32_to_cpu(raw_super->segment_count_main); |
| 2188 | blocks_per_seg = sbi->blocks_per_seg; |
| 2189 | |
| 2190 | for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) { |
| 2191 | if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs || |
| 2192 | le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg) |
| 2193 | return 1; |
| 2194 | } |
| 2195 | for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) { |
| 2196 | if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs || |
| 2197 | le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg) |
| 2198 | return 1; |
| 2199 | } |
| 2200 | |
Jaegeuk Kim | 1e968fd | 2014-08-11 16:49:25 -0700 | [diff] [blame] | 2201 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 577e349 | 2013-01-24 19:56:11 +0900 | [diff] [blame] | 2202 | f2fs_msg(sbi->sb, KERN_ERR, "A bug case: need to run fsck"); |
| 2203 | return 1; |
| 2204 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2205 | return 0; |
| 2206 | } |
| 2207 | |
| 2208 | static void init_sb_info(struct f2fs_sb_info *sbi) |
| 2209 | { |
| 2210 | struct f2fs_super_block *raw_super = sbi->raw_super; |
Chao Yu | e41e6d7 | 2017-05-19 23:37:00 +0800 | [diff] [blame] | 2211 | int i, j; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2212 | |
| 2213 | sbi->log_sectors_per_block = |
| 2214 | le32_to_cpu(raw_super->log_sectors_per_block); |
| 2215 | sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize); |
| 2216 | sbi->blocksize = 1 << sbi->log_blocksize; |
| 2217 | sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg); |
| 2218 | sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg; |
| 2219 | sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec); |
| 2220 | sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone); |
| 2221 | sbi->total_sections = le32_to_cpu(raw_super->section_count); |
| 2222 | sbi->total_node_count = |
| 2223 | (le32_to_cpu(raw_super->segment_count_nat) / 2) |
| 2224 | * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK; |
| 2225 | sbi->root_ino_num = le32_to_cpu(raw_super->root_ino); |
| 2226 | sbi->node_ino_num = le32_to_cpu(raw_super->node_ino); |
| 2227 | sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino); |
Jaegeuk Kim | 5ec4e49 | 2013-03-31 13:26:03 +0900 | [diff] [blame] | 2228 | sbi->cur_victim_sec = NULL_SECNO; |
Jaegeuk Kim | b1c57c1 | 2014-01-08 13:45:08 +0900 | [diff] [blame] | 2229 | sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2230 | |
Jaegeuk Kim | ab9fa66 | 2014-02-27 20:09:05 +0900 | [diff] [blame] | 2231 | sbi->dir_level = DEF_DIR_LEVEL; |
Jaegeuk Kim | 6beceb5 | 2016-01-08 15:51:50 -0800 | [diff] [blame] | 2232 | sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL; |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 2233 | sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL; |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 2234 | clear_sbi_flag(sbi, SBI_NEED_FSCK); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 2235 | |
Jaegeuk Kim | 35782b2 | 2016-10-20 19:09:57 -0700 | [diff] [blame] | 2236 | for (i = 0; i < NR_COUNT_TYPE; i++) |
| 2237 | atomic_set(&sbi->nr_pages[i], 0); |
| 2238 | |
Jaegeuk Kim | 687de7f | 2017-03-28 18:07:38 -0700 | [diff] [blame] | 2239 | atomic_set(&sbi->wb_sync_req, 0); |
| 2240 | |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 2241 | INIT_LIST_HEAD(&sbi->s_list); |
| 2242 | mutex_init(&sbi->umount_mutex); |
Chao Yu | e41e6d7 | 2017-05-19 23:37:00 +0800 | [diff] [blame] | 2243 | for (i = 0; i < NR_PAGE_TYPE - 1; i++) |
| 2244 | for (j = HOT; j < NR_TEMP_TYPE; j++) |
| 2245 | mutex_init(&sbi->wio_mutex[i][j]); |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 2246 | spin_lock_init(&sbi->cp_lock); |
Chao Yu | 1228b48 | 2017-09-29 13:59:39 +0800 | [diff] [blame] | 2247 | |
| 2248 | sbi->dirty_device = 0; |
| 2249 | spin_lock_init(&sbi->dev_lock); |
Chao Yu | d0d3f1b | 2018-02-11 22:53:20 +0800 | [diff] [blame] | 2250 | |
Chao Yu | 846ae67 | 2018-02-26 22:04:13 +0800 | [diff] [blame] | 2251 | init_rwsem(&sbi->sb_lock); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2252 | } |
| 2253 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 2254 | static int init_percpu_info(struct f2fs_sb_info *sbi) |
| 2255 | { |
Jaegeuk Kim | 35782b2 | 2016-10-20 19:09:57 -0700 | [diff] [blame] | 2256 | int err; |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 2257 | |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 2258 | err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL); |
| 2259 | if (err) |
| 2260 | return err; |
| 2261 | |
| 2262 | return percpu_counter_init(&sbi->total_valid_inode_count, 0, |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 2263 | GFP_KERNEL); |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2266 | #ifdef CONFIG_BLK_DEV_ZONED |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2267 | static int init_blkz_info(struct f2fs_sb_info *sbi, int devi) |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2268 | { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2269 | struct block_device *bdev = FDEV(devi).bdev; |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2270 | sector_t nr_sectors = bdev->bd_part->nr_sects; |
| 2271 | sector_t sector = 0; |
| 2272 | struct blk_zone *zones; |
| 2273 | unsigned int i, nr_zones; |
| 2274 | unsigned int n = 0; |
| 2275 | int err = -EIO; |
| 2276 | |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 2277 | if (!f2fs_sb_has_blkzoned(sbi->sb)) |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2278 | return 0; |
| 2279 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2280 | if (sbi->blocks_per_blkz && sbi->blocks_per_blkz != |
Damien Le Moal | f99e864 | 2017-01-12 07:58:32 -0700 | [diff] [blame] | 2281 | SECTOR_TO_BLOCK(bdev_zone_sectors(bdev))) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2282 | return -EINVAL; |
Damien Le Moal | f99e864 | 2017-01-12 07:58:32 -0700 | [diff] [blame] | 2283 | sbi->blocks_per_blkz = SECTOR_TO_BLOCK(bdev_zone_sectors(bdev)); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2284 | if (sbi->log_blocks_per_blkz && sbi->log_blocks_per_blkz != |
| 2285 | __ilog2_u32(sbi->blocks_per_blkz)) |
| 2286 | return -EINVAL; |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2287 | sbi->log_blocks_per_blkz = __ilog2_u32(sbi->blocks_per_blkz); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2288 | FDEV(devi).nr_blkz = SECTOR_TO_BLOCK(nr_sectors) >> |
| 2289 | sbi->log_blocks_per_blkz; |
Damien Le Moal | f99e864 | 2017-01-12 07:58:32 -0700 | [diff] [blame] | 2290 | if (nr_sectors & (bdev_zone_sectors(bdev) - 1)) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2291 | FDEV(devi).nr_blkz++; |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2292 | |
Chao Yu | 4e6aad2 | 2017-11-30 19:28:19 +0800 | [diff] [blame] | 2293 | FDEV(devi).blkz_type = f2fs_kmalloc(sbi, FDEV(devi).nr_blkz, |
| 2294 | GFP_KERNEL); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2295 | if (!FDEV(devi).blkz_type) |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2296 | return -ENOMEM; |
| 2297 | |
| 2298 | #define F2FS_REPORT_NR_ZONES 4096 |
| 2299 | |
Chao Yu | 4e6aad2 | 2017-11-30 19:28:19 +0800 | [diff] [blame] | 2300 | zones = f2fs_kzalloc(sbi, sizeof(struct blk_zone) * |
| 2301 | F2FS_REPORT_NR_ZONES, GFP_KERNEL); |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2302 | if (!zones) |
| 2303 | return -ENOMEM; |
| 2304 | |
| 2305 | /* Get block zones type */ |
| 2306 | while (zones && sector < nr_sectors) { |
| 2307 | |
| 2308 | nr_zones = F2FS_REPORT_NR_ZONES; |
| 2309 | err = blkdev_report_zones(bdev, sector, |
| 2310 | zones, &nr_zones, |
| 2311 | GFP_KERNEL); |
| 2312 | if (err) |
| 2313 | break; |
| 2314 | if (!nr_zones) { |
| 2315 | err = -EIO; |
| 2316 | break; |
| 2317 | } |
| 2318 | |
| 2319 | for (i = 0; i < nr_zones; i++) { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2320 | FDEV(devi).blkz_type[n] = zones[i].type; |
Damien Le Moal | 178053e | 2016-10-28 17:45:05 +0900 | [diff] [blame] | 2321 | sector += zones[i].len; |
| 2322 | n++; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | kfree(zones); |
| 2327 | |
| 2328 | return err; |
| 2329 | } |
| 2330 | #endif |
| 2331 | |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 2332 | /* |
| 2333 | * Read f2fs raw super block. |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2334 | * Because we have two copies of super block, so read both of them |
| 2335 | * to get the first valid one. If any one of them is broken, we pass |
| 2336 | * them recovery flag back to the caller. |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 2337 | */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2338 | static int read_raw_super_block(struct f2fs_sb_info *sbi, |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 2339 | struct f2fs_super_block **raw_super, |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2340 | int *valid_super_block, int *recovery) |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 2341 | { |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2342 | struct super_block *sb = sbi->sb; |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2343 | int block; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2344 | struct buffer_head *bh; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2345 | struct f2fs_super_block *super; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2346 | int err = 0; |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 2347 | |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 2348 | super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL); |
| 2349 | if (!super) |
| 2350 | return -ENOMEM; |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2351 | |
| 2352 | for (block = 0; block < 2; block++) { |
| 2353 | bh = sb_bread(sb, block); |
| 2354 | if (!bh) { |
| 2355 | f2fs_msg(sb, KERN_ERR, "Unable to read %dth superblock", |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 2356 | block + 1); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2357 | err = -EIO; |
| 2358 | continue; |
| 2359 | } |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 2360 | |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2361 | /* sanity checking of raw super */ |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2362 | if (sanity_check_raw_super(sbi, bh)) { |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2363 | f2fs_msg(sb, KERN_ERR, |
| 2364 | "Can't find valid F2FS filesystem in %dth superblock", |
| 2365 | block + 1); |
| 2366 | err = -EINVAL; |
| 2367 | brelse(bh); |
| 2368 | continue; |
| 2369 | } |
| 2370 | |
| 2371 | if (!*raw_super) { |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2372 | memcpy(super, bh->b_data + F2FS_SUPER_OFFSET, |
| 2373 | sizeof(*super)); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2374 | *valid_super_block = block; |
| 2375 | *raw_super = super; |
| 2376 | } |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2377 | brelse(bh); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* Fail to read any one of the superblocks*/ |
| 2381 | if (err < 0) |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2382 | *recovery = 1; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2383 | |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2384 | /* No valid superblock */ |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2385 | if (!*raw_super) |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 2386 | kfree(super); |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2387 | else |
| 2388 | err = 0; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2389 | |
Shawn Lin | 2b39e90 | 2016-02-17 08:59:01 +0800 | [diff] [blame] | 2390 | return err; |
majianpeng | 14d7e9d | 2013-02-01 19:07:03 +0800 | [diff] [blame] | 2391 | } |
| 2392 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2393 | int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover) |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 2394 | { |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 2395 | struct buffer_head *bh; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 2396 | int err; |
| 2397 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2398 | if ((recover && f2fs_readonly(sbi->sb)) || |
| 2399 | bdev_read_only(sbi->sb->s_bdev)) { |
| 2400 | set_sbi_flag(sbi, SBI_NEED_SB_WRITE); |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 2401 | return -EROFS; |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2402 | } |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 2403 | |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2404 | /* write back-up superblock first */ |
Sheng Yong | 0964fc1 | 2018-01-29 19:13:15 +0800 | [diff] [blame] | 2405 | bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1); |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 2406 | if (!bh) |
| 2407 | return -EIO; |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2408 | err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi)); |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 2409 | brelse(bh); |
Chao Yu | c5bda1c | 2015-06-08 13:28:03 +0800 | [diff] [blame] | 2410 | |
| 2411 | /* if we are in recovery path, skip writing valid superblock */ |
| 2412 | if (recover || err) |
Jaegeuk Kim | 5d909cd | 2015-12-07 10:16:58 -0800 | [diff] [blame] | 2413 | return err; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 2414 | |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2415 | /* write current valid superblock */ |
Sheng Yong | 0964fc1 | 2018-01-29 19:13:15 +0800 | [diff] [blame] | 2416 | bh = sb_bread(sbi->sb, sbi->valid_super_block); |
Jaegeuk Kim | fd69473 | 2016-03-20 15:33:20 -0700 | [diff] [blame] | 2417 | if (!bh) |
| 2418 | return -EIO; |
| 2419 | err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi)); |
| 2420 | brelse(bh); |
| 2421 | return err; |
Jaegeuk Kim | 26d815ad | 2015-04-20 18:49:51 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2424 | static int f2fs_scan_devices(struct f2fs_sb_info *sbi) |
| 2425 | { |
| 2426 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2427 | unsigned int max_devices = MAX_DEVICES; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2428 | int i; |
| 2429 | |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2430 | /* Initialize single device information */ |
| 2431 | if (!RDEV(0).path[0]) { |
| 2432 | if (!bdev_is_zoned(sbi->sb->s_bdev)) |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2433 | return 0; |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2434 | max_devices = 1; |
| 2435 | } |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2436 | |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2437 | /* |
| 2438 | * Initialize multiple devices information, or single |
| 2439 | * zoned block device information. |
| 2440 | */ |
Chao Yu | 4e6aad2 | 2017-11-30 19:28:19 +0800 | [diff] [blame] | 2441 | sbi->devs = f2fs_kzalloc(sbi, sizeof(struct f2fs_dev_info) * |
| 2442 | max_devices, GFP_KERNEL); |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2443 | if (!sbi->devs) |
| 2444 | return -ENOMEM; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2445 | |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2446 | for (i = 0; i < max_devices; i++) { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2447 | |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2448 | if (i > 0 && !RDEV(i).path[0]) |
| 2449 | break; |
| 2450 | |
| 2451 | if (max_devices == 1) { |
| 2452 | /* Single zoned block device mount */ |
| 2453 | FDEV(0).bdev = |
| 2454 | blkdev_get_by_dev(sbi->sb->s_bdev->bd_dev, |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2455 | sbi->sb->s_mode, sbi->sb->s_type); |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2456 | } else { |
| 2457 | /* Multi-device mount */ |
| 2458 | memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN); |
| 2459 | FDEV(i).total_segments = |
| 2460 | le32_to_cpu(RDEV(i).total_segments); |
| 2461 | if (i == 0) { |
| 2462 | FDEV(i).start_blk = 0; |
| 2463 | FDEV(i).end_blk = FDEV(i).start_blk + |
| 2464 | (FDEV(i).total_segments << |
| 2465 | sbi->log_blocks_per_seg) - 1 + |
| 2466 | le32_to_cpu(raw_super->segment0_blkaddr); |
| 2467 | } else { |
| 2468 | FDEV(i).start_blk = FDEV(i - 1).end_blk + 1; |
| 2469 | FDEV(i).end_blk = FDEV(i).start_blk + |
| 2470 | (FDEV(i).total_segments << |
| 2471 | sbi->log_blocks_per_seg) - 1; |
| 2472 | } |
| 2473 | FDEV(i).bdev = blkdev_get_by_path(FDEV(i).path, |
| 2474 | sbi->sb->s_mode, sbi->sb->s_type); |
| 2475 | } |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2476 | if (IS_ERR(FDEV(i).bdev)) |
| 2477 | return PTR_ERR(FDEV(i).bdev); |
| 2478 | |
| 2479 | /* to release errored devices */ |
| 2480 | sbi->s_ndevs = i + 1; |
| 2481 | |
| 2482 | #ifdef CONFIG_BLK_DEV_ZONED |
| 2483 | if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM && |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 2484 | !f2fs_sb_has_blkzoned(sbi->sb)) { |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2485 | f2fs_msg(sbi->sb, KERN_ERR, |
| 2486 | "Zoned block device feature not enabled\n"); |
| 2487 | return -EINVAL; |
| 2488 | } |
| 2489 | if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) { |
| 2490 | if (init_blkz_info(sbi, i)) { |
| 2491 | f2fs_msg(sbi->sb, KERN_ERR, |
| 2492 | "Failed to initialize F2FS blkzone information"); |
| 2493 | return -EINVAL; |
| 2494 | } |
Masato Suzuki | 7bb3a37 | 2017-02-27 20:52:49 +0900 | [diff] [blame] | 2495 | if (max_devices == 1) |
| 2496 | break; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2497 | f2fs_msg(sbi->sb, KERN_INFO, |
| 2498 | "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: %s)", |
| 2499 | i, FDEV(i).path, |
| 2500 | FDEV(i).total_segments, |
| 2501 | FDEV(i).start_blk, FDEV(i).end_blk, |
| 2502 | bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HA ? |
| 2503 | "Host-aware" : "Host-managed"); |
| 2504 | continue; |
| 2505 | } |
| 2506 | #endif |
| 2507 | f2fs_msg(sbi->sb, KERN_INFO, |
| 2508 | "Mount Device [%2d]: %20s, %8u, %8x - %8x", |
| 2509 | i, FDEV(i).path, |
| 2510 | FDEV(i).total_segments, |
| 2511 | FDEV(i).start_blk, FDEV(i).end_blk); |
| 2512 | } |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2513 | f2fs_msg(sbi->sb, KERN_INFO, |
| 2514 | "IO Block Size: %8d KB", F2FS_IO_SIZE_KB(sbi)); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2515 | return 0; |
| 2516 | } |
| 2517 | |
Jaegeuk Kim | 84b89e5 | 2018-02-22 14:09:30 -0800 | [diff] [blame^] | 2518 | static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi) |
| 2519 | { |
| 2520 | struct f2fs_sm_info *sm_i = SM_I(sbi); |
| 2521 | |
| 2522 | /* adjust parameters according to the volume size */ |
| 2523 | if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) { |
| 2524 | sbi->alloc_mode = ALLOC_MODE_REUSE; |
| 2525 | sm_i->dcc_info->discard_granularity = 1; |
| 2526 | sm_i->ipu_policy = 1 << F2FS_IPU_FORCE; |
| 2527 | } |
| 2528 | } |
| 2529 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2530 | static int f2fs_fill_super(struct super_block *sb, void *data, int silent) |
| 2531 | { |
| 2532 | struct f2fs_sb_info *sbi; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2533 | struct f2fs_super_block *raw_super; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2534 | struct inode *root; |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 2535 | int err; |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 2536 | bool retry = true, need_fsck = false; |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 2537 | char *options = NULL; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2538 | int recovery, i, valid_super_block; |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 2539 | struct curseg_info *seg_i; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2540 | |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 2541 | try_onemore: |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2542 | err = -EINVAL; |
| 2543 | raw_super = NULL; |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2544 | valid_super_block = -1; |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2545 | recovery = 0; |
| 2546 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2547 | /* allocate memory for f2fs-specific super block info */ |
| 2548 | sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL); |
| 2549 | if (!sbi) |
| 2550 | return -ENOMEM; |
| 2551 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2552 | sbi->sb = sb; |
| 2553 | |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 2554 | /* Load the checksum driver */ |
| 2555 | sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0); |
| 2556 | if (IS_ERR(sbi->s_chksum_driver)) { |
| 2557 | f2fs_msg(sb, KERN_ERR, "Cannot load crc32 driver."); |
| 2558 | err = PTR_ERR(sbi->s_chksum_driver); |
| 2559 | sbi->s_chksum_driver = NULL; |
| 2560 | goto free_sbi; |
| 2561 | } |
| 2562 | |
Namjae Jeon | ff9234a | 2013-01-12 14:41:13 +0900 | [diff] [blame] | 2563 | /* set a block size */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2564 | if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2565 | f2fs_msg(sb, KERN_ERR, "unable to set blocksize"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2566 | goto free_sbi; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2567 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2568 | |
Jaegeuk Kim | df728b0 | 2016-03-23 17:05:27 -0700 | [diff] [blame] | 2569 | err = read_raw_super_block(sbi, &raw_super, &valid_super_block, |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2570 | &recovery); |
Gu Zheng | 9076a75 | 2013-10-14 18:47:11 +0800 | [diff] [blame] | 2571 | if (err) |
| 2572 | goto free_sbi; |
| 2573 | |
Gu Zheng | 5fb0837 | 2013-06-07 14:16:53 +0800 | [diff] [blame] | 2574 | sb->s_fs_info = sbi; |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 2575 | sbi->raw_super = raw_super; |
| 2576 | |
Jaegeuk Kim | 7c2e596 | 2018-01-04 21:36:09 -0800 | [diff] [blame] | 2577 | sbi->s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID); |
| 2578 | sbi->s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID); |
| 2579 | |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 2580 | /* precompute checksum seed for metadata */ |
| 2581 | if (f2fs_sb_has_inode_chksum(sb)) |
| 2582 | sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid, |
| 2583 | sizeof(raw_super->uuid)); |
| 2584 | |
Damien Le Moal | d1b959c | 2016-10-28 17:45:01 +0900 | [diff] [blame] | 2585 | /* |
| 2586 | * The BLKZONED feature indicates that the drive was formatted with |
| 2587 | * zone alignment optimization. This is optional for host-aware |
| 2588 | * devices, but mandatory for host-managed zoned block devices. |
| 2589 | */ |
| 2590 | #ifndef CONFIG_BLK_DEV_ZONED |
Sheng Yong | ccd31cb | 2018-02-06 12:31:17 +0800 | [diff] [blame] | 2591 | if (f2fs_sb_has_blkzoned(sb)) { |
Damien Le Moal | d1b959c | 2016-10-28 17:45:01 +0900 | [diff] [blame] | 2592 | f2fs_msg(sb, KERN_ERR, |
| 2593 | "Zoned block device support is not enabled\n"); |
Chao Yu | 1727f31 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 2594 | err = -EOPNOTSUPP; |
Damien Le Moal | d1b959c | 2016-10-28 17:45:01 +0900 | [diff] [blame] | 2595 | goto free_sb_buf; |
| 2596 | } |
Damien Le Moal | d1b959c | 2016-10-28 17:45:01 +0900 | [diff] [blame] | 2597 | #endif |
Yunlei He | 498c5e9 | 2015-05-07 18:11:37 +0800 | [diff] [blame] | 2598 | default_options(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2599 | /* parse mount options */ |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 2600 | options = kstrdup((const char *)data, GFP_KERNEL); |
| 2601 | if (data && !options) { |
| 2602 | err = -ENOMEM; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2603 | goto free_sb_buf; |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | err = parse_options(sb, options); |
| 2607 | if (err) |
| 2608 | goto free_options; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2609 | |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 2610 | sbi->max_file_blocks = max_file_blocks(); |
| 2611 | sb->s_maxbytes = sbi->max_file_blocks << |
| 2612 | le32_to_cpu(raw_super->log_blocksize); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2613 | sb->s_max_links = F2FS_LINK_MAX; |
| 2614 | get_random_bytes(&sbi->s_next_generation, sizeof(u32)); |
| 2615 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 2616 | #ifdef CONFIG_QUOTA |
| 2617 | sb->dq_op = &f2fs_quota_operations; |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2618 | if (f2fs_sb_has_quota_ino(sb)) |
| 2619 | sb->s_qcop = &dquot_quotactl_sysfile_ops; |
| 2620 | else |
| 2621 | sb->s_qcop = &f2fs_quotactl_ops; |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 2622 | sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; |
Chao Yu | 292c196a | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 2623 | |
| 2624 | if (f2fs_sb_has_quota_ino(sbi->sb)) { |
| 2625 | for (i = 0; i < MAXQUOTAS; i++) { |
| 2626 | if (f2fs_qf_ino(sbi->sb, i)) |
| 2627 | sbi->nquota_files++; |
| 2628 | } |
| 2629 | } |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 2630 | #endif |
| 2631 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2632 | sb->s_op = &f2fs_sops; |
Eric Biggers | ffcc4182 | 2017-10-09 12:15:38 -0700 | [diff] [blame] | 2633 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 2634 | sb->s_cop = &f2fs_cryptops; |
Eric Biggers | ffcc4182 | 2017-10-09 12:15:38 -0700 | [diff] [blame] | 2635 | #endif |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2636 | sb->s_xattr = f2fs_xattr_handlers; |
| 2637 | sb->s_export_op = &f2fs_export_ops; |
| 2638 | sb->s_magic = F2FS_SUPER_MAGIC; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2639 | sb->s_time_gran = 1; |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2640 | sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | |
| 2641 | (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0); |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 2642 | memcpy(&sb->s_uuid, raw_super->uuid, sizeof(raw_super->uuid)); |
Yufen Yu | 578c647 | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 2643 | sb->s_iflags |= SB_I_CGROUPWB; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2644 | |
| 2645 | /* init f2fs-specific super block info */ |
Chao Yu | e8240f6 | 2015-12-15 17:19:26 +0800 | [diff] [blame] | 2646 | sbi->valid_super_block = valid_super_block; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2647 | mutex_init(&sbi->gc_mutex); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2648 | mutex_init(&sbi->cp_mutex); |
Chao Yu | b3582c6 | 2014-07-03 18:58:39 +0800 | [diff] [blame] | 2649 | init_rwsem(&sbi->node_write); |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 2650 | init_rwsem(&sbi->node_change); |
Jaegeuk Kim | 315df83 | 2015-08-11 12:45:39 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* disallow all the data/node/meta page writes */ |
| 2653 | set_sbi_flag(sbi, SBI_POR_DOING); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2654 | spin_lock_init(&sbi->stat_lock); |
Jaegeuk Kim | 971767c | 2013-11-18 17:16:17 +0900 | [diff] [blame] | 2655 | |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2656 | /* init iostat info */ |
| 2657 | spin_lock_init(&sbi->iostat_lock); |
| 2658 | sbi->iostat_enable = false; |
| 2659 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2660 | for (i = 0; i < NR_PAGE_TYPE; i++) { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2661 | int n = (i == META) ? 1: NR_TEMP_TYPE; |
| 2662 | int j; |
| 2663 | |
Chao Yu | 4e6aad2 | 2017-11-30 19:28:19 +0800 | [diff] [blame] | 2664 | sbi->write_io[i] = f2fs_kmalloc(sbi, |
| 2665 | n * sizeof(struct f2fs_bio_info), |
| 2666 | GFP_KERNEL); |
Christophe JAILLET | b63def9 | 2017-06-11 09:21:11 +0200 | [diff] [blame] | 2667 | if (!sbi->write_io[i]) { |
| 2668 | err = -ENOMEM; |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2669 | goto free_options; |
Christophe JAILLET | b63def9 | 2017-06-11 09:21:11 +0200 | [diff] [blame] | 2670 | } |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2671 | |
| 2672 | for (j = HOT; j < n; j++) { |
| 2673 | init_rwsem(&sbi->write_io[i][j].io_rwsem); |
| 2674 | sbi->write_io[i][j].sbi = sbi; |
| 2675 | sbi->write_io[i][j].bio = NULL; |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 2676 | spin_lock_init(&sbi->write_io[i][j].io_lock); |
| 2677 | INIT_LIST_HEAD(&sbi->write_io[i][j].io_list); |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2678 | } |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2679 | } |
Jaegeuk Kim | 971767c | 2013-11-18 17:16:17 +0900 | [diff] [blame] | 2680 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 2681 | init_rwsem(&sbi->cp_rwsem); |
Changman Lee | fb51b5e | 2013-11-07 12:48:25 +0900 | [diff] [blame] | 2682 | init_waitqueue_head(&sbi->cp_wait); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2683 | init_sb_info(sbi); |
| 2684 | |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 2685 | err = init_percpu_info(sbi); |
| 2686 | if (err) |
Chao Yu | d7997e6 | 2018-01-17 16:31:35 +0800 | [diff] [blame] | 2687 | goto free_bio_info; |
Jaegeuk Kim | 523be8a | 2016-05-13 12:36:58 -0700 | [diff] [blame] | 2688 | |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2689 | if (F2FS_IO_SIZE(sbi) > 1) { |
| 2690 | sbi->write_io_dummy = |
Chao Yu | a3ebfe4 | 2017-02-27 18:43:13 +0800 | [diff] [blame] | 2691 | mempool_create_page_pool(2 * (F2FS_IO_SIZE(sbi) - 1), 0); |
Chao Yu | 1727f31 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 2692 | if (!sbi->write_io_dummy) { |
| 2693 | err = -ENOMEM; |
Chao Yu | d7997e6 | 2018-01-17 16:31:35 +0800 | [diff] [blame] | 2694 | goto free_percpu; |
Chao Yu | 1727f31 | 2017-06-12 09:44:27 +0800 | [diff] [blame] | 2695 | } |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2698 | /* get an inode for meta space */ |
| 2699 | sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi)); |
| 2700 | if (IS_ERR(sbi->meta_inode)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2701 | f2fs_msg(sb, KERN_ERR, "Failed to read F2FS meta data inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2702 | err = PTR_ERR(sbi->meta_inode); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2703 | goto free_io_dummy; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | err = get_valid_checkpoint(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2707 | if (err) { |
| 2708 | f2fs_msg(sb, KERN_ERR, "Failed to get valid F2FS checkpoint"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2709 | goto free_meta_inode; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2710 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2711 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2712 | /* Initialize device list */ |
| 2713 | err = f2fs_scan_devices(sbi); |
| 2714 | if (err) { |
| 2715 | f2fs_msg(sb, KERN_ERR, "Failed to find devices"); |
| 2716 | goto free_devices; |
| 2717 | } |
| 2718 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2719 | sbi->total_valid_node_count = |
| 2720 | le32_to_cpu(sbi->ckpt->valid_node_count); |
Jaegeuk Kim | 513c5f3 | 2016-05-16 11:42:32 -0700 | [diff] [blame] | 2721 | percpu_counter_set(&sbi->total_valid_inode_count, |
| 2722 | le32_to_cpu(sbi->ckpt->valid_inode_count)); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2723 | sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count); |
| 2724 | sbi->total_valid_block_count = |
| 2725 | le64_to_cpu(sbi->ckpt->valid_block_count); |
| 2726 | sbi->last_valid_block_count = sbi->total_valid_block_count; |
Chao Yu | daeb433 | 2017-06-26 16:24:41 +0800 | [diff] [blame] | 2727 | sbi->reserved_blocks = 0; |
Yunlong Song | 80d4214 | 2017-10-27 20:45:05 +0800 | [diff] [blame] | 2728 | sbi->current_reserved_blocks = 0; |
Jaegeuk Kim | 7e65be4 | 2017-12-27 15:05:52 -0800 | [diff] [blame] | 2729 | limit_reserve_root(sbi); |
Jaegeuk Kim | 41382ec | 2016-05-16 11:06:50 -0700 | [diff] [blame] | 2730 | |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 2731 | for (i = 0; i < NR_INODE_TYPE; i++) { |
| 2732 | INIT_LIST_HEAD(&sbi->inode_list[i]); |
| 2733 | spin_lock_init(&sbi->inode_lock[i]); |
| 2734 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2735 | |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 2736 | init_extent_cache_info(sbi); |
| 2737 | |
Jaegeuk Kim | 6451e04 | 2014-07-25 15:47:17 -0700 | [diff] [blame] | 2738 | init_ino_entry_info(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2739 | |
| 2740 | /* setup f2fs internal modules */ |
| 2741 | err = build_segment_manager(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2742 | if (err) { |
| 2743 | f2fs_msg(sb, KERN_ERR, |
| 2744 | "Failed to initialize F2FS segment manager"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2745 | goto free_sm; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2746 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2747 | err = build_node_manager(sbi); |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2748 | if (err) { |
| 2749 | f2fs_msg(sb, KERN_ERR, |
| 2750 | "Failed to initialize F2FS node manager"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2751 | goto free_nm; |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2752 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2753 | |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 2754 | /* For write statistics */ |
| 2755 | if (sb->s_bdev->bd_part) |
| 2756 | sbi->sectors_written_start = |
| 2757 | (u64)part_stat_read(sb->s_bdev->bd_part, sectors[1]); |
| 2758 | |
| 2759 | /* Read accumulated write IO statistics if exists */ |
| 2760 | seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE); |
| 2761 | if (__exist_node_summaries(sbi)) |
| 2762 | sbi->kbytes_written = |
Shuoran Liu | b2dde6f | 2016-03-29 18:00:15 +0800 | [diff] [blame] | 2763 | le64_to_cpu(seg_i->journal->info.kbytes_written); |
Shuoran Liu | 8f1dbbb | 2016-01-27 09:57:30 +0800 | [diff] [blame] | 2764 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2765 | build_gc_manager(sbi); |
| 2766 | |
| 2767 | /* get an inode for node space */ |
| 2768 | sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi)); |
| 2769 | if (IS_ERR(sbi->node_inode)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2770 | f2fs_msg(sb, KERN_ERR, "Failed to read node inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2771 | err = PTR_ERR(sbi->node_inode); |
| 2772 | goto free_nm; |
| 2773 | } |
| 2774 | |
Jaegeuk Kim | aa51d08 | 2017-03-07 13:41:22 -0800 | [diff] [blame] | 2775 | err = f2fs_build_stats(sbi); |
| 2776 | if (err) |
Chao Yu | bae01ed | 2017-11-30 19:28:20 +0800 | [diff] [blame] | 2777 | goto free_node_inode; |
Jaegeuk Kim | aa51d08 | 2017-03-07 13:41:22 -0800 | [diff] [blame] | 2778 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2779 | /* read root inode and dentry */ |
| 2780 | root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); |
| 2781 | if (IS_ERR(root)) { |
Namjae Jeon | a07ef78 | 2012-12-30 14:52:05 +0900 | [diff] [blame] | 2782 | f2fs_msg(sb, KERN_ERR, "Failed to read root inode"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2783 | err = PTR_ERR(root); |
Chao Yu | bae01ed | 2017-11-30 19:28:20 +0800 | [diff] [blame] | 2784 | goto free_stats; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2785 | } |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 2786 | if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { |
Chao Yu | 9d84795 | 2014-07-25 12:55:09 +0800 | [diff] [blame] | 2787 | iput(root); |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 2788 | err = -EINVAL; |
Chao Yu | 9d84795 | 2014-07-25 12:55:09 +0800 | [diff] [blame] | 2789 | goto free_node_inode; |
Chao Yu | 8f99a94 | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 2790 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2791 | |
| 2792 | sb->s_root = d_make_root(root); /* allocate root dentry */ |
| 2793 | if (!sb->s_root) { |
| 2794 | err = -ENOMEM; |
| 2795 | goto free_root_inode; |
| 2796 | } |
| 2797 | |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 2798 | err = f2fs_register_sysfs(sbi); |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 2799 | if (err) |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 2800 | goto free_root_inode; |
Namjae Jeon | b59d0ba | 2013-08-04 23:09:40 +0900 | [diff] [blame] | 2801 | |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2802 | #ifdef CONFIG_QUOTA |
| 2803 | /* |
| 2804 | * Turn on quotas which were not enabled for read-only mounts if |
| 2805 | * filesystem has quota feature, so that they are updated correctly. |
| 2806 | */ |
| 2807 | if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) { |
| 2808 | err = f2fs_enable_quotas(sb); |
| 2809 | if (err) { |
| 2810 | f2fs_msg(sb, KERN_ERR, |
| 2811 | "Cannot turn on quotas: error %d", err); |
| 2812 | goto free_sysfs; |
| 2813 | } |
| 2814 | } |
| 2815 | #endif |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2816 | /* if there are nt orphan nodes free them */ |
| 2817 | err = recover_orphan_inodes(sbi); |
| 2818 | if (err) |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2819 | goto free_meta; |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2820 | |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2821 | /* recover fsynced data */ |
| 2822 | if (!test_opt(sbi, DISABLE_ROLL_FORWARD)) { |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 2823 | /* |
| 2824 | * mount should be failed, when device has readonly mode, and |
| 2825 | * previous checkpoint was not done by clean system shutdown. |
| 2826 | */ |
| 2827 | if (bdev_read_only(sb->s_bdev) && |
Chao Yu | aaec2b1 | 2016-09-20 11:04:18 +0800 | [diff] [blame] | 2828 | !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 2829 | err = -EROFS; |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2830 | goto free_meta; |
Jaegeuk Kim | 081d78c | 2015-01-23 19:16:59 -0800 | [diff] [blame] | 2831 | } |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 2832 | |
| 2833 | if (need_fsck) |
| 2834 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
| 2835 | |
Jaegeuk Kim | a468f0e | 2016-09-19 17:55:10 -0700 | [diff] [blame] | 2836 | if (!retry) |
| 2837 | goto skip_recovery; |
| 2838 | |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 2839 | err = recover_fsync_data(sbi, false); |
| 2840 | if (err < 0) { |
Chao Yu | 2adc350 | 2015-03-16 21:08:44 +0800 | [diff] [blame] | 2841 | need_fsck = true; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2842 | f2fs_msg(sb, KERN_ERR, |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 2843 | "Cannot recover all fsync data errno=%d", err); |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2844 | goto free_meta; |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 2845 | } |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 2846 | } else { |
| 2847 | err = recover_fsync_data(sbi, true); |
| 2848 | |
| 2849 | if (!f2fs_readonly(sb) && err > 0) { |
| 2850 | err = -EINVAL; |
| 2851 | f2fs_msg(sb, KERN_ERR, |
| 2852 | "Need to recover fsync data"); |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2853 | goto free_meta; |
Jaegeuk Kim | 6781eab | 2016-03-23 16:12:58 -0700 | [diff] [blame] | 2854 | } |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2855 | } |
Jaegeuk Kim | a468f0e | 2016-09-19 17:55:10 -0700 | [diff] [blame] | 2856 | skip_recovery: |
Jaegeuk Kim | 315df83 | 2015-08-11 12:45:39 -0700 | [diff] [blame] | 2857 | /* recover_fsync_data() cleared this already */ |
| 2858 | clear_sbi_flag(sbi, SBI_POR_DOING); |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2859 | |
| 2860 | /* |
| 2861 | * If filesystem is not mounted as read-only then |
| 2862 | * do start the gc_thread. |
| 2863 | */ |
Chao Yu | 6c02993 | 2014-11-18 11:16:01 +0800 | [diff] [blame] | 2864 | if (test_opt(sbi, BG_GC) && !f2fs_readonly(sb)) { |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2865 | /* After POR, we can run background GC thread.*/ |
| 2866 | err = start_gc_thread(sbi); |
| 2867 | if (err) |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2868 | goto free_meta; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2869 | } |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 2870 | kfree(options); |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2871 | |
| 2872 | /* recover broken superblock */ |
Jaegeuk Kim | f2353d7 | 2016-03-23 10:42:01 -0700 | [diff] [blame] | 2873 | if (recovery) { |
Chao Yu | 41214b3 | 2016-02-22 18:33:20 +0800 | [diff] [blame] | 2874 | err = f2fs_commit_super(sbi, true); |
| 2875 | f2fs_msg(sb, KERN_INFO, |
Sheng Yong | 99e3e85 | 2016-05-11 17:08:14 +0800 | [diff] [blame] | 2876 | "Try to recover %dth superblock, ret: %d", |
Chao Yu | 41214b3 | 2016-02-22 18:33:20 +0800 | [diff] [blame] | 2877 | sbi->valid_super_block ? 1 : 2, err); |
hujianyang | da554e4 | 2015-05-21 14:42:53 +0800 | [diff] [blame] | 2878 | } |
| 2879 | |
Chao Yu | bae01ed | 2017-11-30 19:28:20 +0800 | [diff] [blame] | 2880 | f2fs_join_shrinker(sbi); |
| 2881 | |
Jaegeuk Kim | 84b89e5 | 2018-02-22 14:09:30 -0800 | [diff] [blame^] | 2882 | f2fs_tuning_parameters(sbi); |
| 2883 | |
Jaegeuk Kim | 1200abb | 2017-02-09 13:25:35 -0800 | [diff] [blame] | 2884 | f2fs_msg(sbi->sb, KERN_NOTICE, "Mounted with checkpoint version = %llx", |
| 2885 | cur_cp_version(F2FS_CKPT(sbi))); |
Jaegeuk Kim | 6beceb5 | 2016-01-08 15:51:50 -0800 | [diff] [blame] | 2886 | f2fs_update_time(sbi, CP_TIME); |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 2887 | f2fs_update_time(sbi, REQ_TIME); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2888 | return 0; |
Jaegeuk Kim | 6437d1b | 2014-02-19 18:23:32 +0900 | [diff] [blame] | 2889 | |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2890 | free_meta: |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2891 | #ifdef CONFIG_QUOTA |
| 2892 | if (f2fs_sb_has_quota_ino(sb) && !sb_rdonly(sb)) |
| 2893 | f2fs_quota_off_umount(sbi->sb); |
| 2894 | #endif |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 2895 | f2fs_sync_inode_meta(sbi); |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2896 | /* |
| 2897 | * Some dirty meta pages can be produced by recover_orphan_inodes() |
| 2898 | * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg() |
| 2899 | * followed by write_checkpoint() through f2fs_write_node_pages(), which |
| 2900 | * falls into an infinite loop in sync_meta_pages(). |
| 2901 | */ |
| 2902 | truncate_inode_pages_final(META_MAPPING(sbi)); |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2903 | #ifdef CONFIG_QUOTA |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2904 | free_sysfs: |
Jaegeuk Kim | ea67673 | 2017-10-06 09:14:28 -0700 | [diff] [blame] | 2905 | #endif |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 2906 | f2fs_unregister_sysfs(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2907 | free_root_inode: |
| 2908 | dput(sb->s_root); |
| 2909 | sb->s_root = NULL; |
Chao Yu | bae01ed | 2017-11-30 19:28:20 +0800 | [diff] [blame] | 2910 | free_stats: |
Jaegeuk Kim | aa51d08 | 2017-03-07 13:41:22 -0800 | [diff] [blame] | 2911 | f2fs_destroy_stats(sbi); |
Chao Yu | bae01ed | 2017-11-30 19:28:20 +0800 | [diff] [blame] | 2912 | free_node_inode: |
| 2913 | release_ino_entry(sbi, true); |
| 2914 | truncate_inode_pages_final(NODE_MAPPING(sbi)); |
| 2915 | iput(sbi->node_inode); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2916 | free_nm: |
| 2917 | destroy_node_manager(sbi); |
| 2918 | free_sm: |
| 2919 | destroy_segment_manager(sbi); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 2920 | free_devices: |
| 2921 | destroy_device_list(sbi); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2922 | kfree(sbi->ckpt); |
| 2923 | free_meta_inode: |
| 2924 | make_bad_inode(sbi->meta_inode); |
| 2925 | iput(sbi->meta_inode); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 2926 | free_io_dummy: |
| 2927 | mempool_destroy(sbi->write_io_dummy); |
Chao Yu | d7997e6 | 2018-01-17 16:31:35 +0800 | [diff] [blame] | 2928 | free_percpu: |
| 2929 | destroy_percpu_info(sbi); |
| 2930 | free_bio_info: |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 2931 | for (i = 0; i < NR_PAGE_TYPE; i++) |
| 2932 | kfree(sbi->write_io[i]); |
Chao Yu | d7997e6 | 2018-01-17 16:31:35 +0800 | [diff] [blame] | 2933 | free_options: |
Chao Yu | 4b2414d | 2017-08-08 10:54:31 +0800 | [diff] [blame] | 2934 | #ifdef CONFIG_QUOTA |
| 2935 | for (i = 0; i < MAXQUOTAS; i++) |
| 2936 | kfree(sbi->s_qf_names[i]); |
| 2937 | #endif |
Jaegeuk Kim | dabc4a5 | 2015-01-23 17:41:39 -0800 | [diff] [blame] | 2938 | kfree(options); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2939 | free_sb_buf: |
Yunlei He | b39f0de | 2015-12-15 17:17:20 +0800 | [diff] [blame] | 2940 | kfree(raw_super); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2941 | free_sbi: |
Keith Mok | 43b6573 | 2016-03-02 12:04:24 -0800 | [diff] [blame] | 2942 | if (sbi->s_chksum_driver) |
| 2943 | crypto_free_shash(sbi->s_chksum_driver); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2944 | kfree(sbi); |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 2945 | |
| 2946 | /* give only one another chance */ |
| 2947 | if (retry) { |
Taehee Yoo | 9df47ba | 2015-04-13 21:48:06 +0900 | [diff] [blame] | 2948 | retry = false; |
Jaegeuk Kim | ed2e621 | 2014-08-08 15:37:41 -0700 | [diff] [blame] | 2949 | shrink_dcache_sb(sb); |
| 2950 | goto try_onemore; |
| 2951 | } |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2952 | return err; |
| 2953 | } |
| 2954 | |
| 2955 | static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags, |
| 2956 | const char *dev_name, void *data) |
| 2957 | { |
| 2958 | return mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super); |
| 2959 | } |
| 2960 | |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 2961 | static void kill_f2fs_super(struct super_block *sb) |
| 2962 | { |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 2963 | if (sb->s_root) { |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 2964 | set_sbi_flag(F2FS_SB(sb), SBI_IS_CLOSE); |
Chao Yu | cce1325 | 2017-06-29 23:17:45 +0800 | [diff] [blame] | 2965 | stop_gc_thread(F2FS_SB(sb)); |
| 2966 | stop_discard_thread(F2FS_SB(sb)); |
| 2967 | } |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 2968 | kill_block_super(sb); |
| 2969 | } |
| 2970 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2971 | static struct file_system_type f2fs_fs_type = { |
| 2972 | .owner = THIS_MODULE, |
| 2973 | .name = "f2fs", |
| 2974 | .mount = f2fs_mount, |
Jaegeuk Kim | 30a5537 | 2015-01-14 16:34:24 -0800 | [diff] [blame] | 2975 | .kill_sb = kill_f2fs_super, |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2976 | .fs_flags = FS_REQUIRES_DEV, |
| 2977 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 2978 | MODULE_ALIAS_FS("f2fs"); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2979 | |
Namjae Jeon | 6e6093a | 2013-01-17 00:08:30 +0900 | [diff] [blame] | 2980 | static int __init init_inodecache(void) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2981 | { |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 2982 | f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache", |
| 2983 | sizeof(struct f2fs_inode_info), 0, |
| 2984 | SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2985 | if (!f2fs_inode_cachep) |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 2986 | return -ENOMEM; |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | static void destroy_inodecache(void) |
| 2991 | { |
| 2992 | /* |
| 2993 | * Make sure all delayed rcu free inodes are flushed before we |
| 2994 | * destroy cache. |
| 2995 | */ |
| 2996 | rcu_barrier(); |
| 2997 | kmem_cache_destroy(f2fs_inode_cachep); |
| 2998 | } |
| 2999 | |
| 3000 | static int __init init_f2fs_fs(void) |
| 3001 | { |
| 3002 | int err; |
| 3003 | |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 3004 | f2fs_build_trace_ios(); |
| 3005 | |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3006 | err = init_inodecache(); |
| 3007 | if (err) |
| 3008 | goto fail; |
| 3009 | err = create_node_manager_caches(); |
| 3010 | if (err) |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 3011 | goto free_inodecache; |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3012 | err = create_segment_manager_caches(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3013 | if (err) |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 3014 | goto free_node_manager_caches; |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3015 | err = create_checkpoint_caches(); |
| 3016 | if (err) |
Chao Yu | 0629207 | 2014-12-29 15:56:18 +0800 | [diff] [blame] | 3017 | goto free_segment_manager_caches; |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 3018 | err = create_extent_cache(); |
| 3019 | if (err) |
| 3020 | goto free_checkpoint_caches; |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 3021 | err = f2fs_init_sysfs(); |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 3022 | if (err) |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 3023 | goto free_extent_cache; |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 3024 | err = register_shrinker(&f2fs_shrinker_info); |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame] | 3025 | if (err) |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 3026 | goto free_sysfs; |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 3027 | err = register_filesystem(&f2fs_fs_type); |
| 3028 | if (err) |
| 3029 | goto free_shrinker; |
Chao Yu | 787c7b8c | 2015-10-29 09:13:04 +0800 | [diff] [blame] | 3030 | err = f2fs_create_root_stats(); |
| 3031 | if (err) |
| 3032 | goto free_filesystem; |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 3033 | return 0; |
| 3034 | |
Chao Yu | 787c7b8c | 2015-10-29 09:13:04 +0800 | [diff] [blame] | 3035 | free_filesystem: |
| 3036 | unregister_filesystem(&f2fs_fs_type); |
Jaegeuk Kim | 2658e50 | 2015-06-19 12:01:21 -0700 | [diff] [blame] | 3037 | free_shrinker: |
| 3038 | unregister_shrinker(&f2fs_shrinker_info); |
Chao Yu | a398101 | 2017-06-14 17:39:46 +0800 | [diff] [blame] | 3039 | free_sysfs: |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 3040 | f2fs_exit_sysfs(); |
Chao Yu | 1dcc336 | 2015-02-05 17:57:31 +0800 | [diff] [blame] | 3041 | free_extent_cache: |
| 3042 | destroy_extent_cache(); |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 3043 | free_checkpoint_caches: |
| 3044 | destroy_checkpoint_caches(); |
Jaegeuk Kim | 7fd9e54 | 2013-11-15 13:55:58 +0900 | [diff] [blame] | 3045 | free_segment_manager_caches: |
| 3046 | destroy_segment_manager_caches(); |
Zhao Hongjiang | 9890ff3 | 2013-08-20 16:49:51 +0800 | [diff] [blame] | 3047 | free_node_manager_caches: |
| 3048 | destroy_node_manager_caches(); |
| 3049 | free_inodecache: |
| 3050 | destroy_inodecache(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3051 | fail: |
| 3052 | return err; |
| 3053 | } |
| 3054 | |
| 3055 | static void __exit exit_f2fs_fs(void) |
| 3056 | { |
Namjae Jeon | 4589d25 | 2013-01-15 19:58:47 +0900 | [diff] [blame] | 3057 | f2fs_destroy_root_stats(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3058 | unregister_filesystem(&f2fs_fs_type); |
Tiezhu Yang | b8bef79 | 2016-05-18 08:02:25 +0800 | [diff] [blame] | 3059 | unregister_shrinker(&f2fs_shrinker_info); |
Jaegeuk Kim | dc6b205 | 2017-07-26 11:24:13 -0700 | [diff] [blame] | 3060 | f2fs_exit_sysfs(); |
Wanpeng Li | fdf6c8b | 2015-03-06 15:00:54 +0800 | [diff] [blame] | 3061 | destroy_extent_cache(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3062 | destroy_checkpoint_caches(); |
Changman Lee | 5dcd8a7 | 2013-12-11 14:32:13 +0900 | [diff] [blame] | 3063 | destroy_segment_manager_caches(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3064 | destroy_node_manager_caches(); |
| 3065 | destroy_inodecache(); |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 3066 | f2fs_destroy_trace_ios(); |
Jaegeuk Kim | aff063e | 2012-11-02 17:07:47 +0900 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | module_init(init_f2fs_fs) |
| 3070 | module_exit(exit_f2fs_fs) |
| 3071 | |
| 3072 | MODULE_AUTHOR("Samsung Electronics's Praesto Team"); |
| 3073 | MODULE_DESCRIPTION("Flash Friendly File System"); |
| 3074 | MODULE_LICENSE("GPL"); |
Jaegeuk Kim | b4b9d34 | 2016-11-04 14:59:15 -0700 | [diff] [blame] | 3075 | |