David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 7 | #include <linux/sched/mm.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 8 | #include <linux/slab.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/completion.h> |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 11 | #include <linux/bug.h> |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame^] | 12 | #include <crypto/hash.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 13 | |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 14 | #include "ctree.h" |
| 15 | #include "disk-io.h" |
| 16 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 17 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 18 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 19 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 20 | #include "block-group.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 21 | |
David Sterba | 9188db6 | 2019-08-02 12:52:48 +0200 | [diff] [blame] | 22 | struct btrfs_feature_attr { |
| 23 | struct kobj_attribute kobj_attr; |
| 24 | enum btrfs_feature_set feature_set; |
| 25 | u64 feature_bit; |
| 26 | }; |
| 27 | |
| 28 | /* For raid type sysfs entries */ |
| 29 | struct raid_kobject { |
| 30 | u64 flags; |
| 31 | struct kobject kobj; |
| 32 | }; |
| 33 | |
| 34 | #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ |
| 35 | { \ |
| 36 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 37 | .show = _show, \ |
| 38 | .store = _store, \ |
| 39 | } |
| 40 | |
| 41 | #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ |
| 42 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 43 | __INIT_KOBJ_ATTR(_name, 0644, _show, _store) |
| 44 | |
| 45 | #define BTRFS_ATTR(_prefix, _name, _show) \ |
| 46 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 47 | __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) |
| 48 | |
| 49 | #define BTRFS_ATTR_PTR(_prefix, _name) \ |
| 50 | (&btrfs_attr_##_prefix##_##_name.attr) |
| 51 | |
| 52 | #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ |
| 53 | static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ |
| 54 | .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ |
| 55 | btrfs_feature_attr_show, \ |
| 56 | btrfs_feature_attr_store), \ |
| 57 | .feature_set = _feature_set, \ |
| 58 | .feature_bit = _feature_prefix ##_## _feature_bit, \ |
| 59 | } |
| 60 | #define BTRFS_FEAT_ATTR_PTR(_name) \ |
| 61 | (&btrfs_attr_features_##_name.kobj_attr.attr) |
| 62 | |
| 63 | #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ |
| 64 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) |
| 65 | #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ |
| 66 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) |
| 67 | #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ |
| 68 | BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) |
| 69 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 70 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 71 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 72 | |
David Sterba | 8f52316 | 2019-08-02 13:07:38 +0200 | [diff] [blame] | 73 | static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) |
| 74 | { |
| 75 | return container_of(a, struct btrfs_feature_attr, kobj_attr); |
| 76 | } |
| 77 | |
| 78 | static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) |
| 79 | { |
| 80 | return container_of(attr, struct kobj_attribute, attr); |
| 81 | } |
| 82 | |
| 83 | static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( |
| 84 | struct attribute *attr) |
| 85 | { |
| 86 | return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); |
| 87 | } |
| 88 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 89 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 90 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 91 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 92 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 93 | if (set == FEAT_COMPAT) |
| 94 | return btrfs_super_compat_flags(disk_super); |
| 95 | else if (set == FEAT_COMPAT_RO) |
| 96 | return btrfs_super_compat_ro_flags(disk_super); |
| 97 | else |
| 98 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 101 | static void set_features(struct btrfs_fs_info *fs_info, |
| 102 | enum btrfs_feature_set set, u64 features) |
| 103 | { |
| 104 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 105 | if (set == FEAT_COMPAT) |
| 106 | btrfs_set_super_compat_flags(disk_super, features); |
| 107 | else if (set == FEAT_COMPAT_RO) |
| 108 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 109 | else |
| 110 | btrfs_set_super_incompat_flags(disk_super, features); |
| 111 | } |
| 112 | |
| 113 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 114 | { |
| 115 | int val = 0; |
| 116 | u64 set, clear; |
| 117 | switch (fa->feature_set) { |
| 118 | case FEAT_COMPAT: |
| 119 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 120 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 121 | break; |
| 122 | case FEAT_COMPAT_RO: |
| 123 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 124 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 125 | break; |
| 126 | case FEAT_INCOMPAT: |
| 127 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 128 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 129 | break; |
| 130 | default: |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 131 | pr_warn("btrfs: sysfs: unknown feature set %d\n", |
David Sterba | cc37bb0 | 2013-11-19 13:36:21 +0100 | [diff] [blame] | 132 | fa->feature_set); |
| 133 | return 0; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (set & fa->feature_bit) |
| 137 | val |= 1; |
| 138 | if (clear & fa->feature_bit) |
| 139 | val |= 2; |
| 140 | |
| 141 | return val; |
| 142 | } |
| 143 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 144 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 145 | struct kobj_attribute *a, char *buf) |
| 146 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 147 | int val = 0; |
| 148 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 149 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 150 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 151 | u64 features = get_features(fs_info, fa->feature_set); |
| 152 | if (features & fa->feature_bit) |
| 153 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 154 | } else |
| 155 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 156 | |
| 157 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 158 | } |
| 159 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 160 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 161 | struct kobj_attribute *a, |
| 162 | const char *buf, size_t count) |
| 163 | { |
| 164 | struct btrfs_fs_info *fs_info; |
| 165 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 166 | u64 features, set, clear; |
| 167 | unsigned long val; |
| 168 | int ret; |
| 169 | |
| 170 | fs_info = to_fs_info(kobj); |
| 171 | if (!fs_info) |
| 172 | return -EPERM; |
| 173 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 174 | if (sb_rdonly(fs_info->sb)) |
David Sterba | ee61113 | 2015-01-23 18:43:31 +0100 | [diff] [blame] | 175 | return -EROFS; |
| 176 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 177 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
| 181 | if (fa->feature_set == FEAT_COMPAT) { |
| 182 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 183 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 184 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 185 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 186 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 187 | } else { |
| 188 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 189 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 190 | } |
| 191 | |
| 192 | features = get_features(fs_info, fa->feature_set); |
| 193 | |
| 194 | /* Nothing to do */ |
| 195 | if ((val && (features & fa->feature_bit)) || |
| 196 | (!val && !(features & fa->feature_bit))) |
| 197 | return count; |
| 198 | |
| 199 | if ((val && !(set & fa->feature_bit)) || |
| 200 | (!val && !(clear & fa->feature_bit))) { |
| 201 | btrfs_info(fs_info, |
| 202 | "%sabling feature %s on mounted fs is not supported.", |
| 203 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 204 | return -EPERM; |
| 205 | } |
| 206 | |
| 207 | btrfs_info(fs_info, "%s %s feature flag", |
| 208 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 209 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 210 | spin_lock(&fs_info->super_lock); |
| 211 | features = get_features(fs_info, fa->feature_set); |
| 212 | if (val) |
| 213 | features |= fa->feature_bit; |
| 214 | else |
| 215 | features &= ~fa->feature_bit; |
| 216 | set_features(fs_info, fa->feature_set, features); |
| 217 | spin_unlock(&fs_info->super_lock); |
| 218 | |
David Sterba | 0eae274 | 2014-11-12 14:22:21 +0100 | [diff] [blame] | 219 | /* |
| 220 | * We don't want to do full transaction commit from inside sysfs |
| 221 | */ |
| 222 | btrfs_set_pending(fs_info, COMMIT); |
| 223 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 224 | |
| 225 | return count; |
| 226 | } |
| 227 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 228 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 229 | struct attribute *attr, int unused) |
| 230 | { |
| 231 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 232 | umode_t mode = attr->mode; |
| 233 | |
| 234 | if (fs_info) { |
| 235 | struct btrfs_feature_attr *fa; |
| 236 | u64 features; |
| 237 | |
| 238 | fa = attr_to_btrfs_feature_attr(attr); |
| 239 | features = get_features(fs_info, fa->feature_set); |
| 240 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 241 | if (can_modify_feature(fa)) |
| 242 | mode |= S_IWUSR; |
| 243 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 244 | mode = 0; |
| 245 | } |
| 246 | |
| 247 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 251 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 252 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 253 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 254 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 255 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 256 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 257 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 258 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 259 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 260 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 261 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 262 | |
| 263 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 264 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 265 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 266 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 267 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 268 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 269 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 270 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 271 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 272 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 273 | BTRFS_FEAT_ATTR_PTR(no_holes), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 274 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 275 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 276 | NULL |
| 277 | }; |
| 278 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 279 | /* |
| 280 | * Features which depend on feature bits and may differ between each fs. |
| 281 | * |
| 282 | * /sys/fs/btrfs/features lists all available features of this kernel while |
| 283 | * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or |
| 284 | * can be changed online. |
| 285 | */ |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 286 | static const struct attribute_group btrfs_feature_attr_group = { |
| 287 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 288 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 289 | .attrs = btrfs_supported_feature_attrs, |
| 290 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 291 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 292 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 293 | struct kobj_attribute *ka, char *buf) |
| 294 | { |
| 295 | return snprintf(buf, PAGE_SIZE, "0\n"); |
| 296 | } |
| 297 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 298 | |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 299 | static ssize_t supported_checksums_show(struct kobject *kobj, |
| 300 | struct kobj_attribute *a, char *buf) |
| 301 | { |
| 302 | ssize_t ret = 0; |
| 303 | int i; |
| 304 | |
| 305 | for (i = 0; i < btrfs_get_num_csums(); i++) { |
| 306 | /* |
| 307 | * This "trick" only works as long as 'enum btrfs_csum_type' has |
| 308 | * no holes in it |
| 309 | */ |
| 310 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
| 311 | (i == 0 ? "" : " "), btrfs_super_csum_name(i)); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 316 | return ret; |
| 317 | } |
| 318 | BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); |
| 319 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 320 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 321 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 322 | BTRFS_ATTR_PTR(static_feature, supported_checksums), |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 323 | NULL |
| 324 | }; |
| 325 | |
| 326 | /* |
| 327 | * Features which only depend on kernel version. |
| 328 | * |
| 329 | * These are listed in /sys/fs/btrfs/features along with |
| 330 | * btrfs_feature_attr_group |
| 331 | */ |
| 332 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 333 | .name = "features", |
| 334 | .attrs = btrfs_supported_static_feature_attrs, |
| 335 | }; |
| 336 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 337 | #ifdef CONFIG_BTRFS_DEBUG |
| 338 | |
| 339 | /* |
| 340 | * Runtime debugging exported via sysfs |
| 341 | * |
| 342 | * /sys/fs/btrfs/debug - applies to module or all filesystems |
| 343 | * /sys/fs/btrfs/UUID - applies only to the given filesystem |
| 344 | */ |
| 345 | static struct attribute *btrfs_debug_feature_attrs[] = { |
| 346 | NULL |
| 347 | }; |
| 348 | |
| 349 | static const struct attribute_group btrfs_debug_feature_attr_group = { |
| 350 | .name = "debug", |
| 351 | .attrs = btrfs_debug_feature_attrs, |
| 352 | }; |
| 353 | |
| 354 | #endif |
| 355 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 356 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 357 | { |
| 358 | u64 val; |
| 359 | if (lock) |
| 360 | spin_lock(lock); |
| 361 | val = *value_ptr; |
| 362 | if (lock) |
| 363 | spin_unlock(lock); |
| 364 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 365 | } |
| 366 | |
| 367 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 368 | struct kobj_attribute *ka, char *buf) |
| 369 | { |
| 370 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 371 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 372 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 373 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 374 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 375 | |
| 376 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 377 | struct kobj_attribute *a, char *buf) |
| 378 | { |
| 379 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 380 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 381 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 382 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 383 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 384 | |
| 385 | #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 386 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 387 | |
| 388 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 389 | struct kobj_attribute *attr, char *buf); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 390 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 391 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 392 | |
| 393 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 394 | struct kobj_attribute *attr, char *buf) |
| 395 | |
| 396 | { |
| 397 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
| 398 | struct btrfs_block_group_cache *block_group; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 399 | int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 400 | u64 val = 0; |
| 401 | |
| 402 | down_read(&sinfo->groups_sem); |
| 403 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 404 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 405 | val += block_group->key.offset; |
| 406 | else |
| 407 | val += btrfs_block_group_used(&block_group->item); |
| 408 | } |
| 409 | up_read(&sinfo->groups_sem); |
| 410 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 411 | } |
| 412 | |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 413 | static struct attribute *raid_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 414 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 415 | BTRFS_ATTR_PTR(raid, used_bytes), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 416 | NULL |
| 417 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 418 | ATTRIBUTE_GROUPS(raid); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 419 | |
| 420 | static void release_raid_kobj(struct kobject *kobj) |
| 421 | { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 422 | kfree(to_raid_kobj(kobj)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 423 | } |
| 424 | |
David Sterba | 536ea45 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 425 | static struct kobj_type btrfs_raid_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 426 | .sysfs_ops = &kobj_sysfs_ops, |
| 427 | .release = release_raid_kobj, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 428 | .default_groups = raid_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | #define SPACE_INFO_ATTR(field) \ |
| 432 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 433 | struct kobj_attribute *a, \ |
| 434 | char *buf) \ |
| 435 | { \ |
| 436 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 437 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 438 | } \ |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 439 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 440 | |
| 441 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 442 | struct kobj_attribute *a, |
| 443 | char *buf) |
| 444 | { |
| 445 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 446 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
| 447 | return snprintf(buf, PAGE_SIZE, "%lld\n", val); |
| 448 | } |
| 449 | |
| 450 | SPACE_INFO_ATTR(flags); |
| 451 | SPACE_INFO_ATTR(total_bytes); |
| 452 | SPACE_INFO_ATTR(bytes_used); |
| 453 | SPACE_INFO_ATTR(bytes_pinned); |
| 454 | SPACE_INFO_ATTR(bytes_reserved); |
| 455 | SPACE_INFO_ATTR(bytes_may_use); |
Wang Xiaoguang | c1fd5c3 | 2016-06-21 11:12:56 +0800 | [diff] [blame] | 456 | SPACE_INFO_ATTR(bytes_readonly); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 457 | SPACE_INFO_ATTR(disk_used); |
| 458 | SPACE_INFO_ATTR(disk_total); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 459 | BTRFS_ATTR(space_info, total_bytes_pinned, |
| 460 | btrfs_space_info_show_total_bytes_pinned); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 461 | |
| 462 | static struct attribute *space_info_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 463 | BTRFS_ATTR_PTR(space_info, flags), |
| 464 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 465 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 466 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 467 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 468 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 469 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 470 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 471 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 472 | BTRFS_ATTR_PTR(space_info, total_bytes_pinned), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 473 | NULL, |
| 474 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 475 | ATTRIBUTE_GROUPS(space_info); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 476 | |
| 477 | static void space_info_release(struct kobject *kobj) |
| 478 | { |
| 479 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 480 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 481 | kfree(sinfo); |
| 482 | } |
| 483 | |
David Sterba | 27992d0 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 484 | static struct kobj_type space_info_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 485 | .sysfs_ops = &kobj_sysfs_ops, |
| 486 | .release = space_info_release, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 487 | .default_groups = space_info_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | static const struct attribute *allocation_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 491 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 492 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 493 | NULL, |
| 494 | }; |
| 495 | |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 496 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 497 | struct kobj_attribute *a, char *buf) |
| 498 | { |
| 499 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 500 | char *label = fs_info->super_copy->label; |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 501 | ssize_t ret; |
| 502 | |
| 503 | spin_lock(&fs_info->super_lock); |
| 504 | ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); |
| 505 | spin_unlock(&fs_info->super_lock); |
| 506 | |
| 507 | return ret; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 511 | struct kobj_attribute *a, |
| 512 | const char *buf, size_t len) |
| 513 | { |
| 514 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 515 | size_t p_len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 516 | |
David Sterba | 66ac9fe | 2016-04-26 16:03:57 +0200 | [diff] [blame] | 517 | if (!fs_info) |
| 518 | return -EPERM; |
| 519 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 520 | if (sb_rdonly(fs_info->sb)) |
Anand Jain | 79aec2b | 2014-07-30 20:04:10 +0800 | [diff] [blame] | 521 | return -EROFS; |
| 522 | |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 523 | /* |
| 524 | * p_len is the len until the first occurrence of either |
| 525 | * '\n' or '\0' |
| 526 | */ |
| 527 | p_len = strcspn(buf, "\n"); |
| 528 | |
| 529 | if (p_len >= BTRFS_LABEL_SIZE) |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 530 | return -EINVAL; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 531 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 532 | spin_lock(&fs_info->super_lock); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 533 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 534 | memcpy(fs_info->super_copy->label, buf, p_len); |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 535 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 536 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 537 | /* |
| 538 | * We don't want to do full transaction commit from inside sysfs |
| 539 | */ |
| 540 | btrfs_set_pending(fs_info, COMMIT); |
| 541 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 542 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 543 | return len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 544 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 545 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 546 | |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 547 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 548 | struct kobj_attribute *a, char *buf) |
| 549 | { |
| 550 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 551 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 552 | return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 555 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 556 | |
| 557 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 558 | struct kobj_attribute *a, char *buf) |
| 559 | { |
| 560 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 561 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 562 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 563 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 566 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 567 | |
| 568 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 569 | struct kobj_attribute *a, char *buf) |
| 570 | { |
| 571 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 572 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 573 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 574 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 577 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 578 | |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 579 | static ssize_t quota_override_show(struct kobject *kobj, |
| 580 | struct kobj_attribute *a, char *buf) |
| 581 | { |
| 582 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 583 | int quota_override; |
| 584 | |
| 585 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 586 | return snprintf(buf, PAGE_SIZE, "%d\n", quota_override); |
| 587 | } |
| 588 | |
| 589 | static ssize_t quota_override_store(struct kobject *kobj, |
| 590 | struct kobj_attribute *a, |
| 591 | const char *buf, size_t len) |
| 592 | { |
| 593 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 594 | unsigned long knob; |
| 595 | int err; |
| 596 | |
| 597 | if (!fs_info) |
| 598 | return -EPERM; |
| 599 | |
| 600 | if (!capable(CAP_SYS_RESOURCE)) |
| 601 | return -EPERM; |
| 602 | |
| 603 | err = kstrtoul(buf, 10, &knob); |
| 604 | if (err) |
| 605 | return err; |
| 606 | if (knob > 1) |
| 607 | return -EINVAL; |
| 608 | |
| 609 | if (knob) |
| 610 | set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 611 | else |
| 612 | clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 613 | |
| 614 | return len; |
| 615 | } |
| 616 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 617 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 618 | |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 619 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 620 | struct kobj_attribute *a, char *buf) |
| 621 | { |
| 622 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 623 | |
| 624 | return snprintf(buf, PAGE_SIZE, "%pU\n", |
| 625 | fs_info->fs_devices->metadata_uuid); |
| 626 | } |
| 627 | |
| 628 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 629 | |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame^] | 630 | static ssize_t btrfs_checksum_show(struct kobject *kobj, |
| 631 | struct kobj_attribute *a, char *buf) |
| 632 | { |
| 633 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 634 | u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); |
| 635 | |
| 636 | return snprintf(buf, PAGE_SIZE, "%s (%s)\n", |
| 637 | btrfs_super_csum_name(csum_type), |
| 638 | crypto_shash_driver_name(fs_info->csum_shash)); |
| 639 | } |
| 640 | |
| 641 | BTRFS_ATTR(, checksum, btrfs_checksum_show); |
| 642 | |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 643 | static const struct attribute *btrfs_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 644 | BTRFS_ATTR_PTR(, label), |
| 645 | BTRFS_ATTR_PTR(, nodesize), |
| 646 | BTRFS_ATTR_PTR(, sectorsize), |
| 647 | BTRFS_ATTR_PTR(, clone_alignment), |
| 648 | BTRFS_ATTR_PTR(, quota_override), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 649 | BTRFS_ATTR_PTR(, metadata_uuid), |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame^] | 650 | BTRFS_ATTR_PTR(, checksum), |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 651 | NULL, |
| 652 | }; |
| 653 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 654 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 655 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 656 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
Anand Jain | 248d200 | 2015-03-10 06:38:19 +0800 | [diff] [blame] | 657 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 658 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 659 | complete(&fs_devs->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static struct kobj_type btrfs_ktype = { |
| 663 | .sysfs_ops = &kobj_sysfs_ops, |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 664 | .release = btrfs_release_fsid_kobj, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 665 | }; |
| 666 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 667 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 668 | { |
| 669 | if (kobj->ktype != &btrfs_ktype) |
| 670 | return NULL; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 671 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 672 | } |
| 673 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 674 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 675 | { |
| 676 | if (kobj->ktype != &btrfs_ktype) |
| 677 | return NULL; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 678 | return to_fs_devs(kobj)->fs_info; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 679 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 680 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 681 | #define NUM_FEATURE_BITS 64 |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 682 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 683 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 684 | static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 685 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 686 | static const u64 supported_feature_masks[FEAT_MAX] = { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 687 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 688 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 689 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 690 | }; |
| 691 | |
| 692 | static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 693 | { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 694 | int set; |
| 695 | |
| 696 | for (set = 0; set < FEAT_MAX; set++) { |
| 697 | int i; |
| 698 | struct attribute *attrs[2]; |
| 699 | struct attribute_group agroup = { |
| 700 | .name = "features", |
| 701 | .attrs = attrs, |
| 702 | }; |
| 703 | u64 features = get_features(fs_info, set); |
| 704 | features &= ~supported_feature_masks[set]; |
| 705 | |
| 706 | if (!features) |
| 707 | continue; |
| 708 | |
| 709 | attrs[1] = NULL; |
| 710 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 711 | struct btrfs_feature_attr *fa; |
| 712 | |
| 713 | if (!(features & (1ULL << i))) |
| 714 | continue; |
| 715 | |
| 716 | fa = &btrfs_feature_attrs[set][i]; |
| 717 | attrs[0] = &fa->kobj_attr.attr; |
| 718 | if (add) { |
| 719 | int ret; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 720 | ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 721 | &agroup); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | } else |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 725 | sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 726 | &agroup); |
| 727 | } |
| 728 | |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 733 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 734 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 735 | if (fs_devs->device_dir_kobj) { |
| 736 | kobject_del(fs_devs->device_dir_kobj); |
| 737 | kobject_put(fs_devs->device_dir_kobj); |
| 738 | fs_devs->device_dir_kobj = NULL; |
Anand Jain | aaf1330 | 2015-03-10 06:38:24 +0800 | [diff] [blame] | 739 | } |
| 740 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 741 | if (fs_devs->fsid_kobj.state_initialized) { |
| 742 | kobject_del(&fs_devs->fsid_kobj); |
| 743 | kobject_put(&fs_devs->fsid_kobj); |
Anand Jain | f90fc54 | 2015-06-22 18:18:32 +0800 | [diff] [blame] | 744 | wait_for_completion(&fs_devs->kobj_unregister); |
| 745 | } |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 746 | } |
| 747 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 748 | /* when fs_devs is NULL it will remove all fsid kobject */ |
Anand Jain | 1d1c1be | 2015-03-10 06:38:37 +0800 | [diff] [blame] | 749 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 750 | { |
| 751 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 752 | |
| 753 | if (fs_devs) { |
| 754 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 755 | return; |
| 756 | } |
| 757 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 758 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 759 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 760 | } |
| 761 | } |
| 762 | |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 763 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 764 | { |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 765 | btrfs_reset_fs_info_ptr(fs_info); |
| 766 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 767 | if (fs_info->space_info_kobj) { |
| 768 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
| 769 | kobject_del(fs_info->space_info_kobj); |
| 770 | kobject_put(fs_info->space_info_kobj); |
| 771 | } |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 772 | addrm_unknown_feature_attrs(fs_info, false); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 773 | sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group); |
| 774 | sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs); |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 775 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 776 | } |
| 777 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 778 | static const char * const btrfs_feature_set_names[FEAT_MAX] = { |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 779 | [FEAT_COMPAT] = "compat", |
| 780 | [FEAT_COMPAT_RO] = "compat_ro", |
| 781 | [FEAT_INCOMPAT] = "incompat", |
| 782 | }; |
| 783 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 784 | const char * const btrfs_feature_set_name(enum btrfs_feature_set set) |
| 785 | { |
| 786 | return btrfs_feature_set_names[set]; |
| 787 | } |
| 788 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 789 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 790 | { |
| 791 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 792 | int len = 0; |
| 793 | int i; |
| 794 | char *str; |
| 795 | |
| 796 | str = kmalloc(bufsize, GFP_KERNEL); |
| 797 | if (!str) |
| 798 | return str; |
| 799 | |
| 800 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 801 | const char *name; |
| 802 | |
| 803 | if (!(flags & (1ULL << i))) |
| 804 | continue; |
| 805 | |
| 806 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 807 | len += snprintf(str + len, bufsize - len, "%s%s", |
| 808 | len ? "," : "", name); |
| 809 | } |
| 810 | |
| 811 | return str; |
| 812 | } |
| 813 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 814 | static void init_feature_attrs(void) |
| 815 | { |
| 816 | struct btrfs_feature_attr *fa; |
| 817 | int set, i; |
| 818 | |
| 819 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 820 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 821 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 822 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 823 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 824 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 825 | memset(btrfs_unknown_feature_names, 0, |
| 826 | sizeof(btrfs_unknown_feature_names)); |
| 827 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 828 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 829 | struct btrfs_feature_attr *sfa; |
| 830 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 831 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 832 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 833 | bit = ilog2(sfa->feature_bit); |
| 834 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 835 | |
| 836 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 837 | } |
| 838 | |
| 839 | for (set = 0; set < FEAT_MAX; set++) { |
| 840 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 841 | char *name = btrfs_unknown_feature_names[set][i]; |
| 842 | fa = &btrfs_feature_attrs[set][i]; |
| 843 | |
| 844 | if (fa->kobj_attr.attr.name) |
| 845 | continue; |
| 846 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 847 | snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 848 | btrfs_feature_set_names[set], i); |
| 849 | |
| 850 | fa->kobj_attr.attr.name = name; |
| 851 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 852 | fa->feature_set = set; |
| 853 | fa->feature_bit = 1ULL << i; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 858 | /* |
| 859 | * Create a sysfs entry for a given block group type at path |
| 860 | * /sys/fs/btrfs/UUID/allocation/data/TYPE |
| 861 | */ |
| 862 | void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache) |
| 863 | { |
| 864 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 865 | struct btrfs_space_info *space_info = cache->space_info; |
| 866 | struct raid_kobject *rkobj; |
| 867 | const int index = btrfs_bg_flags_to_raid_index(cache->flags); |
| 868 | unsigned int nofs_flag; |
| 869 | int ret; |
| 870 | |
| 871 | /* |
| 872 | * Setup a NOFS context because kobject_add(), deep in its call chain, |
| 873 | * does GFP_KERNEL allocations, and we are often called in a context |
| 874 | * where if reclaim is triggered we can deadlock (we are either holding |
| 875 | * a transaction handle or some lock required for a transaction |
| 876 | * commit). |
| 877 | */ |
| 878 | nofs_flag = memalloc_nofs_save(); |
| 879 | |
| 880 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 881 | if (!rkobj) { |
| 882 | memalloc_nofs_restore(nofs_flag); |
| 883 | btrfs_warn(cache->fs_info, |
| 884 | "couldn't alloc memory for raid level kobject"); |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | rkobj->flags = cache->flags; |
| 889 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 890 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", |
| 891 | btrfs_bg_type_to_raid_name(rkobj->flags)); |
| 892 | memalloc_nofs_restore(nofs_flag); |
| 893 | if (ret) { |
| 894 | kobject_put(&rkobj->kobj); |
| 895 | btrfs_warn(fs_info, |
| 896 | "failed to add kobject for block cache, ignoring"); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
| 901 | } |
| 902 | |
David Sterba | b5865ba | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 903 | /* |
| 904 | * Remove sysfs directories for all block group types of a given space info and |
| 905 | * the space info as well |
| 906 | */ |
| 907 | void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) |
| 908 | { |
| 909 | int i; |
| 910 | |
| 911 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 912 | struct kobject *kobj; |
| 913 | |
| 914 | kobj = space_info->block_group_kobjs[i]; |
| 915 | space_info->block_group_kobjs[i] = NULL; |
| 916 | if (kobj) { |
| 917 | kobject_del(kobj); |
| 918 | kobject_put(kobj); |
| 919 | } |
| 920 | } |
| 921 | kobject_del(&space_info->kobj); |
| 922 | kobject_put(&space_info->kobj); |
| 923 | } |
| 924 | |
David Sterba | b882327 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 925 | static const char *alloc_name(u64 flags) |
| 926 | { |
| 927 | switch (flags) { |
| 928 | case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 929 | return "mixed"; |
| 930 | case BTRFS_BLOCK_GROUP_METADATA: |
| 931 | return "metadata"; |
| 932 | case BTRFS_BLOCK_GROUP_DATA: |
| 933 | return "data"; |
| 934 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 935 | return "system"; |
| 936 | default: |
| 937 | WARN_ON(1); |
| 938 | return "invalid-combination"; |
| 939 | }; |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Create a sysfs entry for a space info type at path |
| 944 | * /sys/fs/btrfs/UUID/allocation/TYPE |
| 945 | */ |
| 946 | int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, |
| 947 | struct btrfs_space_info *space_info) |
| 948 | { |
| 949 | int ret; |
| 950 | |
| 951 | ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, |
| 952 | fs_info->space_info_kobj, "%s", |
| 953 | alloc_name(space_info->flags)); |
| 954 | if (ret) { |
| 955 | kobject_put(&space_info->kobj); |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 962 | /* when one_device is NULL, it removes all device links */ |
| 963 | |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 964 | int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 965 | struct btrfs_device *one_device) |
| 966 | { |
| 967 | struct hd_struct *disk; |
| 968 | struct kobject *disk_kobj; |
| 969 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 970 | if (!fs_devices->device_dir_kobj) |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 971 | return -EINVAL; |
| 972 | |
Liu Bo | 87fa3bb | 2014-07-29 19:09:39 +0800 | [diff] [blame] | 973 | if (one_device && one_device->bdev) { |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 974 | disk = one_device->bdev->bd_part; |
| 975 | disk_kobj = &part_to_dev(disk)->kobj; |
| 976 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 977 | sysfs_remove_link(fs_devices->device_dir_kobj, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 978 | disk_kobj->name); |
| 979 | } |
| 980 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 981 | if (one_device) |
| 982 | return 0; |
| 983 | |
| 984 | list_for_each_entry(one_device, |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 985 | &fs_devices->devices, dev_list) { |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 986 | if (!one_device->bdev) |
| 987 | continue; |
| 988 | disk = one_device->bdev->bd_part; |
| 989 | disk_kobj = &part_to_dev(disk)->kobj; |
| 990 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 991 | sysfs_remove_link(fs_devices->device_dir_kobj, |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 992 | disk_kobj->name); |
| 993 | } |
| 994 | |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 995 | return 0; |
| 996 | } |
| 997 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 998 | int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 999 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1000 | if (!fs_devs->device_dir_kobj) |
| 1001 | fs_devs->device_dir_kobj = kobject_create_and_add("devices", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1002 | &fs_devs->fsid_kobj); |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 1003 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1004 | if (!fs_devs->device_dir_kobj) |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 1005 | return -ENOMEM; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 1010 | int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 1011 | struct btrfs_device *one_device) |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1012 | { |
| 1013 | int error = 0; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1014 | struct btrfs_device *dev; |
| 1015 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1016 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1017 | struct hd_struct *disk; |
| 1018 | struct kobject *disk_kobj; |
| 1019 | |
| 1020 | if (!dev->bdev) |
| 1021 | continue; |
| 1022 | |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 1023 | if (one_device && one_device != dev) |
| 1024 | continue; |
| 1025 | |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1026 | disk = dev->bdev->bd_part; |
| 1027 | disk_kobj = &part_to_dev(disk)->kobj; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1028 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1029 | error = sysfs_create_link(fs_devices->device_dir_kobj, |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1030 | disk_kobj, disk_kobj->name); |
| 1031 | if (error) |
| 1032 | break; |
| 1033 | } |
| 1034 | |
| 1035 | return error; |
| 1036 | } |
| 1037 | |
David Sterba | 5b28692 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1038 | void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) |
| 1039 | { |
| 1040 | int ret; |
| 1041 | |
| 1042 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 1043 | if (ret) |
| 1044 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 1045 | action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 1046 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 1047 | } |
| 1048 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1049 | void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices, |
| 1050 | const u8 *fsid) |
| 1051 | { |
| 1052 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 1053 | |
| 1054 | /* |
| 1055 | * Sprouting changes fsid of the mounted filesystem, rename the fsid |
| 1056 | * directory |
| 1057 | */ |
| 1058 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid); |
| 1059 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
| 1060 | btrfs_warn(fs_devices->fs_info, |
| 1061 | "sysfs: failed to create fsid for sprout"); |
| 1062 | } |
| 1063 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1064 | /* /sys/fs/btrfs/ entry */ |
| 1065 | static struct kset *btrfs_kset; |
| 1066 | |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1067 | /* |
| 1068 | * Can be called by the device discovery thread. |
| 1069 | * And parent can be specified for seed device |
| 1070 | */ |
Anand Jain | 0c10e2d | 2015-03-10 06:38:35 +0800 | [diff] [blame] | 1071 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1072 | struct kobject *parent) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1073 | { |
| 1074 | int error; |
| 1075 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1076 | init_completion(&fs_devs->kobj_unregister); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1077 | fs_devs->fsid_kobj.kset = btrfs_kset; |
| 1078 | error = kobject_init_and_add(&fs_devs->fsid_kobj, |
Anand Jain | 24bd69c | 2015-03-10 06:38:39 +0800 | [diff] [blame] | 1079 | &btrfs_ktype, parent, "%pU", fs_devs->fsid); |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1080 | if (error) { |
| 1081 | kobject_put(&fs_devs->fsid_kobj); |
| 1082 | return error; |
| 1083 | } |
| 1084 | |
| 1085 | return 0; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Anand Jain | 96f3136 | 2015-08-14 18:32:46 +0800 | [diff] [blame] | 1088 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1089 | { |
| 1090 | int error; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1091 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1092 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1093 | |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 1094 | btrfs_set_fs_info_ptr(fs_info); |
| 1095 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 1096 | error = btrfs_sysfs_add_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1097 | if (error) |
| 1098 | return error; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1099 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1100 | error = sysfs_create_files(fsid_kobj, btrfs_attrs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1101 | if (error) { |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1102 | btrfs_sysfs_rm_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1103 | return error; |
| 1104 | } |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1105 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1106 | error = sysfs_create_group(fsid_kobj, |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 1107 | &btrfs_feature_attr_group); |
| 1108 | if (error) |
| 1109 | goto failure; |
| 1110 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1111 | #ifdef CONFIG_BTRFS_DEBUG |
| 1112 | error = sysfs_create_group(fsid_kobj, |
| 1113 | &btrfs_debug_feature_attr_group); |
| 1114 | if (error) |
| 1115 | goto failure; |
| 1116 | #endif |
| 1117 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1118 | error = addrm_unknown_feature_attrs(fs_info, true); |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1119 | if (error) |
| 1120 | goto failure; |
| 1121 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1122 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1123 | fsid_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1124 | if (!fs_info->space_info_kobj) { |
| 1125 | error = -ENOMEM; |
| 1126 | goto failure; |
| 1127 | } |
| 1128 | |
| 1129 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 1130 | if (error) |
| 1131 | goto failure; |
| 1132 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1133 | return 0; |
| 1134 | failure: |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 1135 | btrfs_sysfs_remove_mounted(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1136 | return error; |
| 1137 | } |
| 1138 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1139 | |
| 1140 | /* |
| 1141 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 1142 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 1143 | */ |
| 1144 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, |
| 1145 | u64 bit, enum btrfs_feature_set set) |
| 1146 | { |
| 1147 | struct btrfs_fs_devices *fs_devs; |
| 1148 | struct kobject *fsid_kobj; |
| 1149 | u64 features; |
| 1150 | int ret; |
| 1151 | |
| 1152 | if (!fs_info) |
| 1153 | return; |
| 1154 | |
| 1155 | features = get_features(fs_info, set); |
| 1156 | ASSERT(bit & supported_feature_masks[set]); |
| 1157 | |
| 1158 | fs_devs = fs_info->fs_devices; |
| 1159 | fsid_kobj = &fs_devs->fsid_kobj; |
| 1160 | |
David Sterba | bf60920 | 2016-01-27 14:06:29 +0100 | [diff] [blame] | 1161 | if (!fsid_kobj->state_initialized) |
| 1162 | return; |
| 1163 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1164 | /* |
| 1165 | * FIXME: this is too heavy to update just one value, ideally we'd like |
| 1166 | * to use sysfs_update_group but some refactoring is needed first. |
| 1167 | */ |
| 1168 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1169 | ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1170 | } |
| 1171 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 1172 | int __init btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1173 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1174 | int ret; |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1175 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1176 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 1177 | if (!btrfs_kset) |
| 1178 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1179 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1180 | init_feature_attrs(); |
| 1181 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1182 | if (ret) |
| 1183 | goto out2; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1184 | ret = sysfs_merge_group(&btrfs_kset->kobj, |
| 1185 | &btrfs_static_feature_attr_group); |
| 1186 | if (ret) |
| 1187 | goto out_remove_group; |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1188 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1189 | #ifdef CONFIG_BTRFS_DEBUG |
| 1190 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1191 | if (ret) |
| 1192 | goto out2; |
| 1193 | #endif |
| 1194 | |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1195 | return 0; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1196 | |
| 1197 | out_remove_group: |
| 1198 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1199 | out2: |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1200 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1201 | |
| 1202 | return ret; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1203 | } |
| 1204 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1205 | void __cold btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1206 | { |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1207 | sysfs_unmerge_group(&btrfs_kset->kobj, |
| 1208 | &btrfs_static_feature_attr_group); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1209 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1210 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1211 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 1212 | |