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" |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 15 | #include "discard.h" |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 16 | #include "disk-io.h" |
| 17 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 18 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 19 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 20 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 21 | #include "block-group.h" |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 22 | #include "qgroup.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 23 | |
David Sterba | 9188db6 | 2019-08-02 12:52:48 +0200 | [diff] [blame] | 24 | struct btrfs_feature_attr { |
| 25 | struct kobj_attribute kobj_attr; |
| 26 | enum btrfs_feature_set feature_set; |
| 27 | u64 feature_bit; |
| 28 | }; |
| 29 | |
| 30 | /* For raid type sysfs entries */ |
| 31 | struct raid_kobject { |
| 32 | u64 flags; |
| 33 | struct kobject kobj; |
| 34 | }; |
| 35 | |
| 36 | #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ |
| 37 | { \ |
| 38 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 39 | .show = _show, \ |
| 40 | .store = _store, \ |
| 41 | } |
| 42 | |
| 43 | #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ |
| 44 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 45 | __INIT_KOBJ_ATTR(_name, 0644, _show, _store) |
| 46 | |
| 47 | #define BTRFS_ATTR(_prefix, _name, _show) \ |
| 48 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 49 | __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) |
| 50 | |
| 51 | #define BTRFS_ATTR_PTR(_prefix, _name) \ |
| 52 | (&btrfs_attr_##_prefix##_##_name.attr) |
| 53 | |
| 54 | #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ |
| 55 | static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ |
| 56 | .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ |
| 57 | btrfs_feature_attr_show, \ |
| 58 | btrfs_feature_attr_store), \ |
| 59 | .feature_set = _feature_set, \ |
| 60 | .feature_bit = _feature_prefix ##_## _feature_bit, \ |
| 61 | } |
| 62 | #define BTRFS_FEAT_ATTR_PTR(_name) \ |
| 63 | (&btrfs_attr_features_##_name.kobj_attr.attr) |
| 64 | |
| 65 | #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ |
| 66 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) |
| 67 | #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ |
| 68 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) |
| 69 | #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ |
| 70 | BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) |
| 71 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 72 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 73 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 74 | |
David Sterba | 8f52316 | 2019-08-02 13:07:38 +0200 | [diff] [blame] | 75 | static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) |
| 76 | { |
| 77 | return container_of(a, struct btrfs_feature_attr, kobj_attr); |
| 78 | } |
| 79 | |
| 80 | static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) |
| 81 | { |
| 82 | return container_of(attr, struct kobj_attribute, attr); |
| 83 | } |
| 84 | |
| 85 | static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( |
| 86 | struct attribute *attr) |
| 87 | { |
| 88 | return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); |
| 89 | } |
| 90 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 91 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 92 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 93 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 94 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 95 | if (set == FEAT_COMPAT) |
| 96 | return btrfs_super_compat_flags(disk_super); |
| 97 | else if (set == FEAT_COMPAT_RO) |
| 98 | return btrfs_super_compat_ro_flags(disk_super); |
| 99 | else |
| 100 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 103 | static void set_features(struct btrfs_fs_info *fs_info, |
| 104 | enum btrfs_feature_set set, u64 features) |
| 105 | { |
| 106 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 107 | if (set == FEAT_COMPAT) |
| 108 | btrfs_set_super_compat_flags(disk_super, features); |
| 109 | else if (set == FEAT_COMPAT_RO) |
| 110 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 111 | else |
| 112 | btrfs_set_super_incompat_flags(disk_super, features); |
| 113 | } |
| 114 | |
| 115 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 116 | { |
| 117 | int val = 0; |
| 118 | u64 set, clear; |
| 119 | switch (fa->feature_set) { |
| 120 | case FEAT_COMPAT: |
| 121 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 122 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 123 | break; |
| 124 | case FEAT_COMPAT_RO: |
| 125 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 126 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 127 | break; |
| 128 | case FEAT_INCOMPAT: |
| 129 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 130 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 131 | break; |
| 132 | default: |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 133 | pr_warn("btrfs: sysfs: unknown feature set %d\n", |
David Sterba | cc37bb0 | 2013-11-19 13:36:21 +0100 | [diff] [blame] | 134 | fa->feature_set); |
| 135 | return 0; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (set & fa->feature_bit) |
| 139 | val |= 1; |
| 140 | if (clear & fa->feature_bit) |
| 141 | val |= 2; |
| 142 | |
| 143 | return val; |
| 144 | } |
| 145 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 146 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 147 | struct kobj_attribute *a, char *buf) |
| 148 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 149 | int val = 0; |
| 150 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 151 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 152 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 153 | u64 features = get_features(fs_info, fa->feature_set); |
| 154 | if (features & fa->feature_bit) |
| 155 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 156 | } else |
| 157 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 158 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 159 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 162 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 163 | struct kobj_attribute *a, |
| 164 | const char *buf, size_t count) |
| 165 | { |
| 166 | struct btrfs_fs_info *fs_info; |
| 167 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 168 | u64 features, set, clear; |
| 169 | unsigned long val; |
| 170 | int ret; |
| 171 | |
| 172 | fs_info = to_fs_info(kobj); |
| 173 | if (!fs_info) |
| 174 | return -EPERM; |
| 175 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 176 | if (sb_rdonly(fs_info->sb)) |
David Sterba | ee61113 | 2015-01-23 18:43:31 +0100 | [diff] [blame] | 177 | return -EROFS; |
| 178 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 179 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | if (fa->feature_set == FEAT_COMPAT) { |
| 184 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 185 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 186 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 187 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 188 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 189 | } else { |
| 190 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 191 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 192 | } |
| 193 | |
| 194 | features = get_features(fs_info, fa->feature_set); |
| 195 | |
| 196 | /* Nothing to do */ |
| 197 | if ((val && (features & fa->feature_bit)) || |
| 198 | (!val && !(features & fa->feature_bit))) |
| 199 | return count; |
| 200 | |
| 201 | if ((val && !(set & fa->feature_bit)) || |
| 202 | (!val && !(clear & fa->feature_bit))) { |
| 203 | btrfs_info(fs_info, |
| 204 | "%sabling feature %s on mounted fs is not supported.", |
| 205 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 206 | return -EPERM; |
| 207 | } |
| 208 | |
| 209 | btrfs_info(fs_info, "%s %s feature flag", |
| 210 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 211 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 212 | spin_lock(&fs_info->super_lock); |
| 213 | features = get_features(fs_info, fa->feature_set); |
| 214 | if (val) |
| 215 | features |= fa->feature_bit; |
| 216 | else |
| 217 | features &= ~fa->feature_bit; |
| 218 | set_features(fs_info, fa->feature_set, features); |
| 219 | spin_unlock(&fs_info->super_lock); |
| 220 | |
David Sterba | 0eae274 | 2014-11-12 14:22:21 +0100 | [diff] [blame] | 221 | /* |
| 222 | * We don't want to do full transaction commit from inside sysfs |
| 223 | */ |
| 224 | btrfs_set_pending(fs_info, COMMIT); |
| 225 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 226 | |
| 227 | return count; |
| 228 | } |
| 229 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 230 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 231 | struct attribute *attr, int unused) |
| 232 | { |
| 233 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 234 | umode_t mode = attr->mode; |
| 235 | |
| 236 | if (fs_info) { |
| 237 | struct btrfs_feature_attr *fa; |
| 238 | u64 features; |
| 239 | |
| 240 | fa = attr_to_btrfs_feature_attr(attr); |
| 241 | features = get_features(fs_info, fa->feature_set); |
| 242 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 243 | if (can_modify_feature(fa)) |
| 244 | mode |= S_IWUSR; |
| 245 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 246 | mode = 0; |
| 247 | } |
| 248 | |
| 249 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 253 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 254 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 255 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 256 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 257 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 258 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 259 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 260 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 261 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 262 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 263 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 264 | BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 265 | |
| 266 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 267 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 268 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 269 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 270 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 271 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 272 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 273 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 274 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 275 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 276 | BTRFS_FEAT_ATTR_PTR(no_holes), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 277 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 278 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 279 | BTRFS_FEAT_ATTR_PTR(raid1c34), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 280 | NULL |
| 281 | }; |
| 282 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 283 | /* |
| 284 | * Features which depend on feature bits and may differ between each fs. |
| 285 | * |
| 286 | * /sys/fs/btrfs/features lists all available features of this kernel while |
| 287 | * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or |
| 288 | * can be changed online. |
| 289 | */ |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 290 | static const struct attribute_group btrfs_feature_attr_group = { |
| 291 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 292 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 293 | .attrs = btrfs_supported_feature_attrs, |
| 294 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 295 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 296 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 297 | struct kobj_attribute *ka, char *buf) |
| 298 | { |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 299 | return scnprintf(buf, PAGE_SIZE, "0\n"); |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 300 | } |
| 301 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 302 | |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 303 | static ssize_t supported_checksums_show(struct kobject *kobj, |
| 304 | struct kobj_attribute *a, char *buf) |
| 305 | { |
| 306 | ssize_t ret = 0; |
| 307 | int i; |
| 308 | |
| 309 | for (i = 0; i < btrfs_get_num_csums(); i++) { |
| 310 | /* |
| 311 | * This "trick" only works as long as 'enum btrfs_csum_type' has |
| 312 | * no holes in it |
| 313 | */ |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 314 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 315 | (i == 0 ? "" : " "), btrfs_super_csum_name(i)); |
| 316 | |
| 317 | } |
| 318 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 319 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); |
| 323 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 324 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 325 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 326 | BTRFS_ATTR_PTR(static_feature, supported_checksums), |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 327 | NULL |
| 328 | }; |
| 329 | |
| 330 | /* |
| 331 | * Features which only depend on kernel version. |
| 332 | * |
| 333 | * These are listed in /sys/fs/btrfs/features along with |
| 334 | * btrfs_feature_attr_group |
| 335 | */ |
| 336 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 337 | .name = "features", |
| 338 | .attrs = btrfs_supported_static_feature_attrs, |
| 339 | }; |
| 340 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 341 | #ifdef CONFIG_BTRFS_DEBUG |
| 342 | |
| 343 | /* |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 344 | * Discard statistics and tunables |
| 345 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 346 | #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) |
| 347 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 348 | static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, |
| 349 | struct kobj_attribute *a, |
| 350 | char *buf) |
| 351 | { |
| 352 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 353 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 354 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 355 | atomic64_read(&fs_info->discard_ctl.discardable_bytes)); |
| 356 | } |
| 357 | BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); |
| 358 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 359 | static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, |
| 360 | struct kobj_attribute *a, |
| 361 | char *buf) |
| 362 | { |
| 363 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 364 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 365 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 366 | atomic_read(&fs_info->discard_ctl.discardable_extents)); |
| 367 | } |
| 368 | BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); |
| 369 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 370 | static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, |
| 371 | struct kobj_attribute *a, |
| 372 | char *buf) |
| 373 | { |
| 374 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 375 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 376 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 377 | fs_info->discard_ctl.discard_bitmap_bytes); |
| 378 | } |
| 379 | BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); |
| 380 | |
| 381 | static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, |
| 382 | struct kobj_attribute *a, |
| 383 | char *buf) |
| 384 | { |
| 385 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 386 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 387 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 388 | atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); |
| 389 | } |
| 390 | BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); |
| 391 | |
| 392 | static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, |
| 393 | struct kobj_attribute *a, |
| 394 | char *buf) |
| 395 | { |
| 396 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 397 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 398 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 399 | fs_info->discard_ctl.discard_extent_bytes); |
| 400 | } |
| 401 | BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); |
| 402 | |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 403 | static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, |
| 404 | struct kobj_attribute *a, |
| 405 | char *buf) |
| 406 | { |
| 407 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 408 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 409 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 410 | READ_ONCE(fs_info->discard_ctl.iops_limit)); |
| 411 | } |
| 412 | |
| 413 | static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, |
| 414 | struct kobj_attribute *a, |
| 415 | const char *buf, size_t len) |
| 416 | { |
| 417 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 418 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 419 | u32 iops_limit; |
| 420 | int ret; |
| 421 | |
| 422 | ret = kstrtou32(buf, 10, &iops_limit); |
| 423 | if (ret) |
| 424 | return -EINVAL; |
| 425 | |
| 426 | WRITE_ONCE(discard_ctl->iops_limit, iops_limit); |
| 427 | |
| 428 | return len; |
| 429 | } |
| 430 | BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, |
| 431 | btrfs_discard_iops_limit_store); |
| 432 | |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 433 | static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, |
| 434 | struct kobj_attribute *a, |
| 435 | char *buf) |
| 436 | { |
| 437 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 438 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 439 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 440 | READ_ONCE(fs_info->discard_ctl.kbps_limit)); |
| 441 | } |
| 442 | |
| 443 | static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, |
| 444 | struct kobj_attribute *a, |
| 445 | const char *buf, size_t len) |
| 446 | { |
| 447 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 448 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 449 | u32 kbps_limit; |
| 450 | int ret; |
| 451 | |
| 452 | ret = kstrtou32(buf, 10, &kbps_limit); |
| 453 | if (ret) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); |
| 457 | |
| 458 | return len; |
| 459 | } |
| 460 | BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, |
| 461 | btrfs_discard_kbps_limit_store); |
| 462 | |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 463 | static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, |
| 464 | struct kobj_attribute *a, |
| 465 | char *buf) |
| 466 | { |
| 467 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 468 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 469 | return scnprintf(buf, PAGE_SIZE, "%llu\n", |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 470 | READ_ONCE(fs_info->discard_ctl.max_discard_size)); |
| 471 | } |
| 472 | |
| 473 | static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, |
| 474 | struct kobj_attribute *a, |
| 475 | const char *buf, size_t len) |
| 476 | { |
| 477 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 478 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 479 | u64 max_discard_size; |
| 480 | int ret; |
| 481 | |
| 482 | ret = kstrtou64(buf, 10, &max_discard_size); |
| 483 | if (ret) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); |
| 487 | |
| 488 | return len; |
| 489 | } |
| 490 | BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, |
| 491 | btrfs_discard_max_discard_size_store); |
| 492 | |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 493 | static const struct attribute *discard_debug_attrs[] = { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 494 | BTRFS_ATTR_PTR(discard, discardable_bytes), |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 495 | BTRFS_ATTR_PTR(discard, discardable_extents), |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 496 | BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), |
| 497 | BTRFS_ATTR_PTR(discard, discard_bytes_saved), |
| 498 | BTRFS_ATTR_PTR(discard, discard_extent_bytes), |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 499 | BTRFS_ATTR_PTR(discard, iops_limit), |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 500 | BTRFS_ATTR_PTR(discard, kbps_limit), |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 501 | BTRFS_ATTR_PTR(discard, max_discard_size), |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 502 | NULL, |
| 503 | }; |
| 504 | |
| 505 | /* |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 506 | * Runtime debugging exported via sysfs |
| 507 | * |
| 508 | * /sys/fs/btrfs/debug - applies to module or all filesystems |
| 509 | * /sys/fs/btrfs/UUID - applies only to the given filesystem |
| 510 | */ |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 511 | static const struct attribute *btrfs_debug_mount_attrs[] = { |
| 512 | NULL, |
| 513 | }; |
| 514 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 515 | static struct attribute *btrfs_debug_feature_attrs[] = { |
| 516 | NULL |
| 517 | }; |
| 518 | |
| 519 | static const struct attribute_group btrfs_debug_feature_attr_group = { |
| 520 | .name = "debug", |
| 521 | .attrs = btrfs_debug_feature_attrs, |
| 522 | }; |
| 523 | |
| 524 | #endif |
| 525 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 526 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 527 | { |
| 528 | u64 val; |
| 529 | if (lock) |
| 530 | spin_lock(lock); |
| 531 | val = *value_ptr; |
| 532 | if (lock) |
| 533 | spin_unlock(lock); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 534 | return scnprintf(buf, PAGE_SIZE, "%llu\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 538 | struct kobj_attribute *ka, char *buf) |
| 539 | { |
| 540 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 541 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 542 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 543 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 544 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 545 | |
| 546 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 547 | struct kobj_attribute *a, char *buf) |
| 548 | { |
| 549 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 550 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 551 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 552 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 553 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 554 | |
| 555 | #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] | 556 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 557 | |
| 558 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 559 | struct kobj_attribute *attr, char *buf); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 560 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 561 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 562 | |
| 563 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 564 | struct kobj_attribute *attr, char *buf) |
| 565 | |
| 566 | { |
| 567 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 568 | struct btrfs_block_group *block_group; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 569 | 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] | 570 | u64 val = 0; |
| 571 | |
| 572 | down_read(&sinfo->groups_sem); |
| 573 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 574 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 575 | val += block_group->length; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 576 | else |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 577 | val += block_group->used; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 578 | } |
| 579 | up_read(&sinfo->groups_sem); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 580 | return scnprintf(buf, PAGE_SIZE, "%llu\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 583 | static struct attribute *raid_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 584 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 585 | BTRFS_ATTR_PTR(raid, used_bytes), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 586 | NULL |
| 587 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 588 | ATTRIBUTE_GROUPS(raid); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 589 | |
| 590 | static void release_raid_kobj(struct kobject *kobj) |
| 591 | { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 592 | kfree(to_raid_kobj(kobj)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 593 | } |
| 594 | |
David Sterba | 536ea45 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 595 | static struct kobj_type btrfs_raid_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 596 | .sysfs_ops = &kobj_sysfs_ops, |
| 597 | .release = release_raid_kobj, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 598 | .default_groups = raid_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | #define SPACE_INFO_ATTR(field) \ |
| 602 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 603 | struct kobj_attribute *a, \ |
| 604 | char *buf) \ |
| 605 | { \ |
| 606 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 607 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 608 | } \ |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 609 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 610 | |
| 611 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 612 | struct kobj_attribute *a, |
| 613 | char *buf) |
| 614 | { |
| 615 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 616 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 617 | return scnprintf(buf, PAGE_SIZE, "%lld\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | SPACE_INFO_ATTR(flags); |
| 621 | SPACE_INFO_ATTR(total_bytes); |
| 622 | SPACE_INFO_ATTR(bytes_used); |
| 623 | SPACE_INFO_ATTR(bytes_pinned); |
| 624 | SPACE_INFO_ATTR(bytes_reserved); |
| 625 | SPACE_INFO_ATTR(bytes_may_use); |
Wang Xiaoguang | c1fd5c3 | 2016-06-21 11:12:56 +0800 | [diff] [blame] | 626 | SPACE_INFO_ATTR(bytes_readonly); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 627 | SPACE_INFO_ATTR(disk_used); |
| 628 | SPACE_INFO_ATTR(disk_total); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 629 | BTRFS_ATTR(space_info, total_bytes_pinned, |
| 630 | btrfs_space_info_show_total_bytes_pinned); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 631 | |
| 632 | static struct attribute *space_info_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 633 | BTRFS_ATTR_PTR(space_info, flags), |
| 634 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 635 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 636 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 637 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 638 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 639 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 640 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 641 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 642 | BTRFS_ATTR_PTR(space_info, total_bytes_pinned), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 643 | NULL, |
| 644 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 645 | ATTRIBUTE_GROUPS(space_info); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 646 | |
| 647 | static void space_info_release(struct kobject *kobj) |
| 648 | { |
| 649 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 650 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 651 | kfree(sinfo); |
| 652 | } |
| 653 | |
David Sterba | 27992d0 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 654 | static struct kobj_type space_info_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 655 | .sysfs_ops = &kobj_sysfs_ops, |
| 656 | .release = space_info_release, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 657 | .default_groups = space_info_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 658 | }; |
| 659 | |
| 660 | static const struct attribute *allocation_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 661 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 662 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 663 | NULL, |
| 664 | }; |
| 665 | |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 666 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 667 | struct kobj_attribute *a, char *buf) |
| 668 | { |
| 669 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 670 | char *label = fs_info->super_copy->label; |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 671 | ssize_t ret; |
| 672 | |
| 673 | spin_lock(&fs_info->super_lock); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 674 | ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 675 | spin_unlock(&fs_info->super_lock); |
| 676 | |
| 677 | return ret; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 681 | struct kobj_attribute *a, |
| 682 | const char *buf, size_t len) |
| 683 | { |
| 684 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 685 | size_t p_len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 686 | |
David Sterba | 66ac9fe | 2016-04-26 16:03:57 +0200 | [diff] [blame] | 687 | if (!fs_info) |
| 688 | return -EPERM; |
| 689 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 690 | if (sb_rdonly(fs_info->sb)) |
Anand Jain | 79aec2b | 2014-07-30 20:04:10 +0800 | [diff] [blame] | 691 | return -EROFS; |
| 692 | |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 693 | /* |
| 694 | * p_len is the len until the first occurrence of either |
| 695 | * '\n' or '\0' |
| 696 | */ |
| 697 | p_len = strcspn(buf, "\n"); |
| 698 | |
| 699 | if (p_len >= BTRFS_LABEL_SIZE) |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 700 | return -EINVAL; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 701 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 702 | spin_lock(&fs_info->super_lock); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 703 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 704 | memcpy(fs_info->super_copy->label, buf, p_len); |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 705 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 706 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 707 | /* |
| 708 | * We don't want to do full transaction commit from inside sysfs |
| 709 | */ |
| 710 | btrfs_set_pending(fs_info, COMMIT); |
| 711 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 712 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 713 | return len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 714 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 715 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 716 | |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 717 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 718 | struct kobj_attribute *a, char *buf) |
| 719 | { |
| 720 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 721 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 722 | return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 725 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 726 | |
| 727 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 728 | struct kobj_attribute *a, char *buf) |
| 729 | { |
| 730 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 731 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 732 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 733 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 736 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 737 | |
| 738 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 739 | struct kobj_attribute *a, char *buf) |
| 740 | { |
| 741 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 742 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 743 | return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 746 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 747 | |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 748 | static ssize_t quota_override_show(struct kobject *kobj, |
| 749 | struct kobj_attribute *a, char *buf) |
| 750 | { |
| 751 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 752 | int quota_override; |
| 753 | |
| 754 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 755 | return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static ssize_t quota_override_store(struct kobject *kobj, |
| 759 | struct kobj_attribute *a, |
| 760 | const char *buf, size_t len) |
| 761 | { |
| 762 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 763 | unsigned long knob; |
| 764 | int err; |
| 765 | |
| 766 | if (!fs_info) |
| 767 | return -EPERM; |
| 768 | |
| 769 | if (!capable(CAP_SYS_RESOURCE)) |
| 770 | return -EPERM; |
| 771 | |
| 772 | err = kstrtoul(buf, 10, &knob); |
| 773 | if (err) |
| 774 | return err; |
| 775 | if (knob > 1) |
| 776 | return -EINVAL; |
| 777 | |
| 778 | if (knob) |
| 779 | set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 780 | else |
| 781 | clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 782 | |
| 783 | return len; |
| 784 | } |
| 785 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 786 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 787 | |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 788 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 789 | struct kobj_attribute *a, char *buf) |
| 790 | { |
| 791 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 792 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 793 | return scnprintf(buf, PAGE_SIZE, "%pU\n", |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 794 | fs_info->fs_devices->metadata_uuid); |
| 795 | } |
| 796 | |
| 797 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 798 | |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 799 | static ssize_t btrfs_checksum_show(struct kobject *kobj, |
| 800 | struct kobj_attribute *a, char *buf) |
| 801 | { |
| 802 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 803 | u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); |
| 804 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 805 | return scnprintf(buf, PAGE_SIZE, "%s (%s)\n", |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 806 | btrfs_super_csum_name(csum_type), |
| 807 | crypto_shash_driver_name(fs_info->csum_shash)); |
| 808 | } |
| 809 | |
| 810 | BTRFS_ATTR(, checksum, btrfs_checksum_show); |
| 811 | |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 812 | static const struct attribute *btrfs_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 813 | BTRFS_ATTR_PTR(, label), |
| 814 | BTRFS_ATTR_PTR(, nodesize), |
| 815 | BTRFS_ATTR_PTR(, sectorsize), |
| 816 | BTRFS_ATTR_PTR(, clone_alignment), |
| 817 | BTRFS_ATTR_PTR(, quota_override), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 818 | BTRFS_ATTR_PTR(, metadata_uuid), |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 819 | BTRFS_ATTR_PTR(, checksum), |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 820 | NULL, |
| 821 | }; |
| 822 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 823 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 824 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 825 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
Anand Jain | 248d200 | 2015-03-10 06:38:19 +0800 | [diff] [blame] | 826 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 827 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 828 | complete(&fs_devs->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static struct kobj_type btrfs_ktype = { |
| 832 | .sysfs_ops = &kobj_sysfs_ops, |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 833 | .release = btrfs_release_fsid_kobj, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 834 | }; |
| 835 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 836 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 837 | { |
| 838 | if (kobj->ktype != &btrfs_ktype) |
| 839 | return NULL; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 840 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 841 | } |
| 842 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 843 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 844 | { |
| 845 | if (kobj->ktype != &btrfs_ktype) |
| 846 | return NULL; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 847 | return to_fs_devs(kobj)->fs_info; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 848 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 849 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 850 | #define NUM_FEATURE_BITS 64 |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 851 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 852 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 853 | 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] | 854 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 855 | static const u64 supported_feature_masks[FEAT_MAX] = { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 856 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 857 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 858 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 859 | }; |
| 860 | |
| 861 | 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] | 862 | { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 863 | int set; |
| 864 | |
| 865 | for (set = 0; set < FEAT_MAX; set++) { |
| 866 | int i; |
| 867 | struct attribute *attrs[2]; |
| 868 | struct attribute_group agroup = { |
| 869 | .name = "features", |
| 870 | .attrs = attrs, |
| 871 | }; |
| 872 | u64 features = get_features(fs_info, set); |
| 873 | features &= ~supported_feature_masks[set]; |
| 874 | |
| 875 | if (!features) |
| 876 | continue; |
| 877 | |
| 878 | attrs[1] = NULL; |
| 879 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 880 | struct btrfs_feature_attr *fa; |
| 881 | |
| 882 | if (!(features & (1ULL << i))) |
| 883 | continue; |
| 884 | |
| 885 | fa = &btrfs_feature_attrs[set][i]; |
| 886 | attrs[0] = &fa->kobj_attr.attr; |
| 887 | if (add) { |
| 888 | int ret; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 889 | ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 890 | &agroup); |
| 891 | if (ret) |
| 892 | return ret; |
| 893 | } else |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 894 | sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 895 | &agroup); |
| 896 | } |
| 897 | |
| 898 | } |
| 899 | return 0; |
| 900 | } |
| 901 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 902 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 903 | { |
Anand Jain | a013d14 | 2020-02-12 17:28:10 +0800 | [diff] [blame] | 904 | if (fs_devs->devinfo_kobj) { |
| 905 | kobject_del(fs_devs->devinfo_kobj); |
| 906 | kobject_put(fs_devs->devinfo_kobj); |
| 907 | fs_devs->devinfo_kobj = NULL; |
| 908 | } |
| 909 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 910 | if (fs_devs->devices_kobj) { |
| 911 | kobject_del(fs_devs->devices_kobj); |
| 912 | kobject_put(fs_devs->devices_kobj); |
| 913 | fs_devs->devices_kobj = NULL; |
Anand Jain | aaf1330 | 2015-03-10 06:38:24 +0800 | [diff] [blame] | 914 | } |
| 915 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 916 | if (fs_devs->fsid_kobj.state_initialized) { |
| 917 | kobject_del(&fs_devs->fsid_kobj); |
| 918 | kobject_put(&fs_devs->fsid_kobj); |
Anand Jain | f90fc54 | 2015-06-22 18:18:32 +0800 | [diff] [blame] | 919 | wait_for_completion(&fs_devs->kobj_unregister); |
| 920 | } |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 921 | } |
| 922 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 923 | /* when fs_devs is NULL it will remove all fsid kobject */ |
Anand Jain | 1d1c1be | 2015-03-10 06:38:37 +0800 | [diff] [blame] | 924 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 925 | { |
| 926 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 927 | |
| 928 | if (fs_devs) { |
| 929 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 930 | return; |
| 931 | } |
| 932 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 933 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 934 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 935 | } |
| 936 | } |
| 937 | |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 938 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 939 | { |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 940 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 941 | |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 942 | btrfs_reset_fs_info_ptr(fs_info); |
| 943 | |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 944 | sysfs_remove_link(fsid_kobj, "bdi"); |
| 945 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 946 | if (fs_info->space_info_kobj) { |
| 947 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
| 948 | kobject_del(fs_info->space_info_kobj); |
| 949 | kobject_put(fs_info->space_info_kobj); |
| 950 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 951 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 952 | if (fs_info->discard_debug_kobj) { |
| 953 | sysfs_remove_files(fs_info->discard_debug_kobj, |
| 954 | discard_debug_attrs); |
| 955 | kobject_del(fs_info->discard_debug_kobj); |
| 956 | kobject_put(fs_info->discard_debug_kobj); |
| 957 | } |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 958 | if (fs_info->debug_kobj) { |
| 959 | sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
| 960 | kobject_del(fs_info->debug_kobj); |
| 961 | kobject_put(fs_info->debug_kobj); |
| 962 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 963 | #endif |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 964 | addrm_unknown_feature_attrs(fs_info, false); |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 965 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 966 | sysfs_remove_files(fsid_kobj, btrfs_attrs); |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 967 | btrfs_sysfs_remove_devices_dir(fs_info->fs_devices, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 968 | } |
| 969 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 970 | static const char * const btrfs_feature_set_names[FEAT_MAX] = { |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 971 | [FEAT_COMPAT] = "compat", |
| 972 | [FEAT_COMPAT_RO] = "compat_ro", |
| 973 | [FEAT_INCOMPAT] = "incompat", |
| 974 | }; |
| 975 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 976 | const char * const btrfs_feature_set_name(enum btrfs_feature_set set) |
| 977 | { |
| 978 | return btrfs_feature_set_names[set]; |
| 979 | } |
| 980 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 981 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 982 | { |
| 983 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 984 | int len = 0; |
| 985 | int i; |
| 986 | char *str; |
| 987 | |
| 988 | str = kmalloc(bufsize, GFP_KERNEL); |
| 989 | if (!str) |
| 990 | return str; |
| 991 | |
| 992 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 993 | const char *name; |
| 994 | |
| 995 | if (!(flags & (1ULL << i))) |
| 996 | continue; |
| 997 | |
| 998 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 999 | len += scnprintf(str + len, bufsize - len, "%s%s", |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1000 | len ? "," : "", name); |
| 1001 | } |
| 1002 | |
| 1003 | return str; |
| 1004 | } |
| 1005 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1006 | static void init_feature_attrs(void) |
| 1007 | { |
| 1008 | struct btrfs_feature_attr *fa; |
| 1009 | int set, i; |
| 1010 | |
| 1011 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 1012 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 1013 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 1014 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 1015 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1016 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 1017 | memset(btrfs_unknown_feature_names, 0, |
| 1018 | sizeof(btrfs_unknown_feature_names)); |
| 1019 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1020 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 1021 | struct btrfs_feature_attr *sfa; |
| 1022 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1023 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1024 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1025 | bit = ilog2(sfa->feature_bit); |
| 1026 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1027 | |
| 1028 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 1029 | } |
| 1030 | |
| 1031 | for (set = 0; set < FEAT_MAX; set++) { |
| 1032 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1033 | char *name = btrfs_unknown_feature_names[set][i]; |
| 1034 | fa = &btrfs_feature_attrs[set][i]; |
| 1035 | |
| 1036 | if (fa->kobj_attr.attr.name) |
| 1037 | continue; |
| 1038 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 1039 | snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1040 | btrfs_feature_set_names[set], i); |
| 1041 | |
| 1042 | fa->kobj_attr.attr.name = name; |
| 1043 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 1044 | fa->feature_set = set; |
| 1045 | fa->feature_bit = 1ULL << i; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1050 | /* |
| 1051 | * Create a sysfs entry for a given block group type at path |
| 1052 | * /sys/fs/btrfs/UUID/allocation/data/TYPE |
| 1053 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1054 | void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1055 | { |
| 1056 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 1057 | struct btrfs_space_info *space_info = cache->space_info; |
| 1058 | struct raid_kobject *rkobj; |
| 1059 | const int index = btrfs_bg_flags_to_raid_index(cache->flags); |
| 1060 | unsigned int nofs_flag; |
| 1061 | int ret; |
| 1062 | |
| 1063 | /* |
| 1064 | * Setup a NOFS context because kobject_add(), deep in its call chain, |
| 1065 | * does GFP_KERNEL allocations, and we are often called in a context |
| 1066 | * where if reclaim is triggered we can deadlock (we are either holding |
| 1067 | * a transaction handle or some lock required for a transaction |
| 1068 | * commit). |
| 1069 | */ |
| 1070 | nofs_flag = memalloc_nofs_save(); |
| 1071 | |
| 1072 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 1073 | if (!rkobj) { |
| 1074 | memalloc_nofs_restore(nofs_flag); |
| 1075 | btrfs_warn(cache->fs_info, |
| 1076 | "couldn't alloc memory for raid level kobject"); |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | rkobj->flags = cache->flags; |
| 1081 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 1082 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", |
| 1083 | btrfs_bg_type_to_raid_name(rkobj->flags)); |
| 1084 | memalloc_nofs_restore(nofs_flag); |
| 1085 | if (ret) { |
| 1086 | kobject_put(&rkobj->kobj); |
| 1087 | btrfs_warn(fs_info, |
| 1088 | "failed to add kobject for block cache, ignoring"); |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
| 1093 | } |
| 1094 | |
David Sterba | b5865ba | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1095 | /* |
| 1096 | * Remove sysfs directories for all block group types of a given space info and |
| 1097 | * the space info as well |
| 1098 | */ |
| 1099 | void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) |
| 1100 | { |
| 1101 | int i; |
| 1102 | |
| 1103 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 1104 | struct kobject *kobj; |
| 1105 | |
| 1106 | kobj = space_info->block_group_kobjs[i]; |
| 1107 | space_info->block_group_kobjs[i] = NULL; |
| 1108 | if (kobj) { |
| 1109 | kobject_del(kobj); |
| 1110 | kobject_put(kobj); |
| 1111 | } |
| 1112 | } |
| 1113 | kobject_del(&space_info->kobj); |
| 1114 | kobject_put(&space_info->kobj); |
| 1115 | } |
| 1116 | |
David Sterba | b882327 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1117 | static const char *alloc_name(u64 flags) |
| 1118 | { |
| 1119 | switch (flags) { |
| 1120 | case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 1121 | return "mixed"; |
| 1122 | case BTRFS_BLOCK_GROUP_METADATA: |
| 1123 | return "metadata"; |
| 1124 | case BTRFS_BLOCK_GROUP_DATA: |
| 1125 | return "data"; |
| 1126 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 1127 | return "system"; |
| 1128 | default: |
| 1129 | WARN_ON(1); |
| 1130 | return "invalid-combination"; |
| 1131 | }; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Create a sysfs entry for a space info type at path |
| 1136 | * /sys/fs/btrfs/UUID/allocation/TYPE |
| 1137 | */ |
| 1138 | int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, |
| 1139 | struct btrfs_space_info *space_info) |
| 1140 | { |
| 1141 | int ret; |
| 1142 | |
| 1143 | ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, |
| 1144 | fs_info->space_info_kobj, "%s", |
| 1145 | alloc_name(space_info->flags)); |
| 1146 | if (ret) { |
| 1147 | kobject_put(&space_info->kobj); |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1154 | /* when one_device is NULL, it removes all device links */ |
| 1155 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 1156 | int btrfs_sysfs_remove_devices_dir(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1157 | struct btrfs_device *one_device) |
| 1158 | { |
| 1159 | struct hd_struct *disk; |
| 1160 | struct kobject *disk_kobj; |
| 1161 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1162 | if (!fs_devices->devices_kobj) |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1163 | return -EINVAL; |
| 1164 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1165 | if (one_device) { |
| 1166 | if (one_device->bdev) { |
| 1167 | disk = one_device->bdev->bd_part; |
| 1168 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1169 | sysfs_remove_link(fs_devices->devices_kobj, |
| 1170 | disk_kobj->name); |
| 1171 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1172 | |
Anand Jain | b5ddcff | 2020-09-05 01:34:21 +0800 | [diff] [blame] | 1173 | if (one_device->devid_kobj.state_initialized) { |
| 1174 | kobject_del(&one_device->devid_kobj); |
| 1175 | kobject_put(&one_device->devid_kobj); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1176 | |
Anand Jain | b5ddcff | 2020-09-05 01:34:21 +0800 | [diff] [blame] | 1177 | wait_for_completion(&one_device->kobj_unregister); |
| 1178 | } |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1179 | |
| 1180 | return 0; |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1183 | list_for_each_entry(one_device, &fs_devices->devices, dev_list) { |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1184 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1185 | if (one_device->bdev) { |
| 1186 | disk = one_device->bdev->bd_part; |
| 1187 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1188 | sysfs_remove_link(fs_devices->devices_kobj, |
| 1189 | disk_kobj->name); |
| 1190 | } |
Anand Jain | b5ddcff | 2020-09-05 01:34:21 +0800 | [diff] [blame] | 1191 | if (one_device->devid_kobj.state_initialized) { |
| 1192 | kobject_del(&one_device->devid_kobj); |
| 1193 | kobject_put(&one_device->devid_kobj); |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1194 | |
Anand Jain | b5ddcff | 2020-09-05 01:34:21 +0800 | [diff] [blame] | 1195 | wait_for_completion(&one_device->kobj_unregister); |
| 1196 | } |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1197 | } |
| 1198 | |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1202 | static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, |
| 1203 | struct kobj_attribute *a, |
| 1204 | char *buf) |
| 1205 | { |
| 1206 | int val; |
| 1207 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1208 | devid_kobj); |
| 1209 | |
| 1210 | val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| 1211 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1212 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1213 | } |
| 1214 | BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); |
| 1215 | |
Anand Jain | 2586477 | 2020-02-13 16:40:53 +0800 | [diff] [blame] | 1216 | static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1217 | struct kobj_attribute *a, char *buf) |
| 1218 | { |
| 1219 | int val; |
| 1220 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1221 | devid_kobj); |
| 1222 | |
| 1223 | val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
| 1224 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1225 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1226 | } |
Anand Jain | 2586477 | 2020-02-13 16:40:53 +0800 | [diff] [blame] | 1227 | BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1228 | |
| 1229 | static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, |
| 1230 | struct kobj_attribute *a, |
| 1231 | char *buf) |
| 1232 | { |
| 1233 | int val; |
| 1234 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1235 | devid_kobj); |
| 1236 | |
| 1237 | val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| 1238 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1239 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1240 | } |
| 1241 | BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); |
| 1242 | |
| 1243 | static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, |
| 1244 | struct kobj_attribute *a, char *buf) |
| 1245 | { |
| 1246 | int val; |
| 1247 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1248 | devid_kobj); |
| 1249 | |
| 1250 | val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 1251 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1252 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1253 | } |
| 1254 | BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); |
| 1255 | |
| 1256 | static struct attribute *devid_attrs[] = { |
| 1257 | BTRFS_ATTR_PTR(devid, in_fs_metadata), |
| 1258 | BTRFS_ATTR_PTR(devid, missing), |
| 1259 | BTRFS_ATTR_PTR(devid, replace_target), |
| 1260 | BTRFS_ATTR_PTR(devid, writeable), |
| 1261 | NULL |
| 1262 | }; |
| 1263 | ATTRIBUTE_GROUPS(devid); |
| 1264 | |
| 1265 | static void btrfs_release_devid_kobj(struct kobject *kobj) |
| 1266 | { |
| 1267 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1268 | devid_kobj); |
| 1269 | |
| 1270 | memset(&device->devid_kobj, 0, sizeof(struct kobject)); |
| 1271 | complete(&device->kobj_unregister); |
| 1272 | } |
| 1273 | |
| 1274 | static struct kobj_type devid_ktype = { |
| 1275 | .sysfs_ops = &kobj_sysfs_ops, |
| 1276 | .default_groups = devid_groups, |
| 1277 | .release = btrfs_release_devid_kobj, |
| 1278 | }; |
| 1279 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 1280 | int btrfs_sysfs_add_devices_dir(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 1281 | struct btrfs_device *one_device) |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1282 | { |
| 1283 | int error = 0; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1284 | struct btrfs_device *dev; |
Josef Bacik | a47bd78 | 2020-07-21 10:17:50 -0400 | [diff] [blame] | 1285 | unsigned int nofs_flag; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1286 | |
Josef Bacik | a47bd78 | 2020-07-21 10:17:50 -0400 | [diff] [blame] | 1287 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1288 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1289 | |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 1290 | if (one_device && one_device != dev) |
| 1291 | continue; |
| 1292 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1293 | if (dev->bdev) { |
| 1294 | struct hd_struct *disk; |
| 1295 | struct kobject *disk_kobj; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1296 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1297 | disk = dev->bdev->bd_part; |
| 1298 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1299 | |
| 1300 | error = sysfs_create_link(fs_devices->devices_kobj, |
| 1301 | disk_kobj, disk_kobj->name); |
| 1302 | if (error) |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | init_completion(&dev->kobj_unregister); |
| 1307 | error = kobject_init_and_add(&dev->devid_kobj, &devid_ktype, |
Anand Jain | 1b9867e | 2020-02-12 17:28:11 +0800 | [diff] [blame] | 1308 | fs_devices->devinfo_kobj, "%llu", |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1309 | dev->devid); |
| 1310 | if (error) { |
| 1311 | kobject_put(&dev->devid_kobj); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1312 | break; |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1313 | } |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1314 | } |
Josef Bacik | a47bd78 | 2020-07-21 10:17:50 -0400 | [diff] [blame] | 1315 | memalloc_nofs_restore(nofs_flag); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1316 | |
| 1317 | return error; |
| 1318 | } |
| 1319 | |
David Sterba | 5b28692 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1320 | void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) |
| 1321 | { |
| 1322 | int ret; |
| 1323 | |
| 1324 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 1325 | if (ret) |
| 1326 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 1327 | action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 1328 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 1329 | } |
| 1330 | |
Nikolay Borisov | 8e56008 | 2020-08-12 16:18:51 +0300 | [diff] [blame^] | 1331 | void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) |
| 1332 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1333 | { |
| 1334 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 1335 | |
| 1336 | /* |
| 1337 | * Sprouting changes fsid of the mounted filesystem, rename the fsid |
| 1338 | * directory |
| 1339 | */ |
Nikolay Borisov | 8e56008 | 2020-08-12 16:18:51 +0300 | [diff] [blame^] | 1340 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1341 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
| 1342 | btrfs_warn(fs_devices->fs_info, |
| 1343 | "sysfs: failed to create fsid for sprout"); |
| 1344 | } |
| 1345 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1346 | void btrfs_sysfs_update_devid(struct btrfs_device *device) |
| 1347 | { |
| 1348 | char tmp[24]; |
| 1349 | |
| 1350 | snprintf(tmp, sizeof(tmp), "%llu", device->devid); |
| 1351 | |
| 1352 | if (kobject_rename(&device->devid_kobj, tmp)) |
| 1353 | btrfs_warn(device->fs_devices->fs_info, |
| 1354 | "sysfs: failed to update devid for %llu", |
| 1355 | device->devid); |
| 1356 | } |
| 1357 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1358 | /* /sys/fs/btrfs/ entry */ |
| 1359 | static struct kset *btrfs_kset; |
| 1360 | |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1361 | /* |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1362 | * Creates: |
| 1363 | * /sys/fs/btrfs/UUID |
| 1364 | * |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1365 | * Can be called by the device discovery thread. |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1366 | */ |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1367 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1368 | { |
| 1369 | int error; |
| 1370 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1371 | init_completion(&fs_devs->kobj_unregister); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1372 | fs_devs->fsid_kobj.kset = btrfs_kset; |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1373 | error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, |
| 1374 | "%pU", fs_devs->fsid); |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1375 | if (error) { |
| 1376 | kobject_put(&fs_devs->fsid_kobj); |
| 1377 | return error; |
| 1378 | } |
| 1379 | |
Anand Jain | bc036bb | 2019-11-21 17:33:34 +0800 | [diff] [blame] | 1380 | fs_devs->devices_kobj = kobject_create_and_add("devices", |
| 1381 | &fs_devs->fsid_kobj); |
| 1382 | if (!fs_devs->devices_kobj) { |
| 1383 | btrfs_err(fs_devs->fs_info, |
| 1384 | "failed to init sysfs device interface"); |
Anand Jain | 1f6087e | 2020-02-12 17:28:12 +0800 | [diff] [blame] | 1385 | btrfs_sysfs_remove_fsid(fs_devs); |
Anand Jain | bc036bb | 2019-11-21 17:33:34 +0800 | [diff] [blame] | 1386 | return -ENOMEM; |
| 1387 | } |
| 1388 | |
Anand Jain | a013d14 | 2020-02-12 17:28:10 +0800 | [diff] [blame] | 1389 | fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", |
| 1390 | &fs_devs->fsid_kobj); |
| 1391 | if (!fs_devs->devinfo_kobj) { |
| 1392 | btrfs_err(fs_devs->fs_info, |
| 1393 | "failed to init sysfs devinfo kobject"); |
| 1394 | btrfs_sysfs_remove_fsid(fs_devs); |
| 1395 | return -ENOMEM; |
| 1396 | } |
| 1397 | |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1398 | return 0; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Anand Jain | 96f3136 | 2015-08-14 18:32:46 +0800 | [diff] [blame] | 1401 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1402 | { |
| 1403 | int error; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1404 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1405 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1406 | |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 1407 | btrfs_set_fs_info_ptr(fs_info); |
| 1408 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 1409 | error = btrfs_sysfs_add_devices_dir(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1410 | if (error) |
| 1411 | return error; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1412 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1413 | error = sysfs_create_files(fsid_kobj, btrfs_attrs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1414 | if (error) { |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 1415 | btrfs_sysfs_remove_devices_dir(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1416 | return error; |
| 1417 | } |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1418 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1419 | error = sysfs_create_group(fsid_kobj, |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 1420 | &btrfs_feature_attr_group); |
| 1421 | if (error) |
| 1422 | goto failure; |
| 1423 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1424 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 1425 | fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); |
| 1426 | if (!fs_info->debug_kobj) { |
| 1427 | error = -ENOMEM; |
| 1428 | goto failure; |
| 1429 | } |
| 1430 | |
| 1431 | error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1432 | if (error) |
| 1433 | goto failure; |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 1434 | |
| 1435 | /* Discard directory */ |
| 1436 | fs_info->discard_debug_kobj = kobject_create_and_add("discard", |
| 1437 | fs_info->debug_kobj); |
| 1438 | if (!fs_info->discard_debug_kobj) { |
| 1439 | error = -ENOMEM; |
| 1440 | goto failure; |
| 1441 | } |
| 1442 | |
| 1443 | error = sysfs_create_files(fs_info->discard_debug_kobj, |
| 1444 | discard_debug_attrs); |
| 1445 | if (error) |
| 1446 | goto failure; |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1447 | #endif |
| 1448 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1449 | error = addrm_unknown_feature_attrs(fs_info, true); |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1450 | if (error) |
| 1451 | goto failure; |
| 1452 | |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 1453 | error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); |
| 1454 | if (error) |
| 1455 | goto failure; |
| 1456 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1457 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1458 | fsid_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1459 | if (!fs_info->space_info_kobj) { |
| 1460 | error = -ENOMEM; |
| 1461 | goto failure; |
| 1462 | } |
| 1463 | |
| 1464 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 1465 | if (error) |
| 1466 | goto failure; |
| 1467 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1468 | return 0; |
| 1469 | failure: |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 1470 | btrfs_sysfs_remove_mounted(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1471 | return error; |
| 1472 | } |
| 1473 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1474 | static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) |
| 1475 | { |
| 1476 | return to_fs_info(kobj->parent->parent); |
| 1477 | } |
| 1478 | |
| 1479 | #define QGROUP_ATTR(_member, _show_name) \ |
| 1480 | static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ |
| 1481 | struct kobj_attribute *a, \ |
| 1482 | char *buf) \ |
| 1483 | { \ |
| 1484 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 1485 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 1486 | struct btrfs_qgroup, kobj); \ |
| 1487 | return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ |
| 1488 | } \ |
| 1489 | BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) |
| 1490 | |
| 1491 | #define QGROUP_RSV_ATTR(_name, _type) \ |
| 1492 | static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ |
| 1493 | struct kobj_attribute *a, \ |
| 1494 | char *buf) \ |
| 1495 | { \ |
| 1496 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 1497 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 1498 | struct btrfs_qgroup, kobj); \ |
| 1499 | return btrfs_show_u64(&qgroup->rsv.values[_type], \ |
| 1500 | &fs_info->qgroup_lock, buf); \ |
| 1501 | } \ |
| 1502 | BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) |
| 1503 | |
| 1504 | QGROUP_ATTR(rfer, referenced); |
| 1505 | QGROUP_ATTR(excl, exclusive); |
| 1506 | QGROUP_ATTR(max_rfer, max_referenced); |
| 1507 | QGROUP_ATTR(max_excl, max_exclusive); |
| 1508 | QGROUP_ATTR(lim_flags, limit_flags); |
| 1509 | QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); |
| 1510 | QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); |
| 1511 | QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); |
| 1512 | |
| 1513 | static struct attribute *qgroup_attrs[] = { |
| 1514 | BTRFS_ATTR_PTR(qgroup, referenced), |
| 1515 | BTRFS_ATTR_PTR(qgroup, exclusive), |
| 1516 | BTRFS_ATTR_PTR(qgroup, max_referenced), |
| 1517 | BTRFS_ATTR_PTR(qgroup, max_exclusive), |
| 1518 | BTRFS_ATTR_PTR(qgroup, limit_flags), |
| 1519 | BTRFS_ATTR_PTR(qgroup, rsv_data), |
| 1520 | BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), |
| 1521 | BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), |
| 1522 | NULL |
| 1523 | }; |
| 1524 | ATTRIBUTE_GROUPS(qgroup); |
| 1525 | |
| 1526 | static void qgroup_release(struct kobject *kobj) |
| 1527 | { |
| 1528 | struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); |
| 1529 | |
| 1530 | memset(&qgroup->kobj, 0, sizeof(*kobj)); |
| 1531 | } |
| 1532 | |
| 1533 | static struct kobj_type qgroup_ktype = { |
| 1534 | .sysfs_ops = &kobj_sysfs_ops, |
| 1535 | .release = qgroup_release, |
| 1536 | .default_groups = qgroup_groups, |
| 1537 | }; |
| 1538 | |
| 1539 | int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, |
| 1540 | struct btrfs_qgroup *qgroup) |
| 1541 | { |
| 1542 | struct kobject *qgroups_kobj = fs_info->qgroups_kobj; |
| 1543 | int ret; |
| 1544 | |
| 1545 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1546 | return 0; |
| 1547 | if (qgroup->kobj.state_initialized) |
| 1548 | return 0; |
| 1549 | if (!qgroups_kobj) |
| 1550 | return -EINVAL; |
| 1551 | |
| 1552 | ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, |
| 1553 | "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), |
| 1554 | btrfs_qgroup_subvolid(qgroup->qgroupid)); |
| 1555 | if (ret < 0) |
| 1556 | kobject_put(&qgroup->kobj); |
| 1557 | |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
| 1561 | void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) |
| 1562 | { |
| 1563 | struct btrfs_qgroup *qgroup; |
| 1564 | struct btrfs_qgroup *next; |
| 1565 | |
| 1566 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1567 | return; |
| 1568 | |
| 1569 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 1570 | &fs_info->qgroup_tree, node) |
| 1571 | btrfs_sysfs_del_one_qgroup(fs_info, qgroup); |
Qu Wenruo | 62ab2cc | 2020-08-03 14:20:11 +0800 | [diff] [blame] | 1572 | if (fs_info->qgroups_kobj) { |
| 1573 | kobject_del(fs_info->qgroups_kobj); |
| 1574 | kobject_put(fs_info->qgroups_kobj); |
| 1575 | fs_info->qgroups_kobj = NULL; |
| 1576 | } |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* Called when qgroups get initialized, thus there is no need for locking */ |
| 1580 | int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) |
| 1581 | { |
| 1582 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 1583 | struct btrfs_qgroup *qgroup; |
| 1584 | struct btrfs_qgroup *next; |
| 1585 | int ret = 0; |
| 1586 | |
| 1587 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1588 | return 0; |
| 1589 | |
| 1590 | ASSERT(fsid_kobj); |
| 1591 | if (fs_info->qgroups_kobj) |
| 1592 | return 0; |
| 1593 | |
| 1594 | fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); |
| 1595 | if (!fs_info->qgroups_kobj) { |
| 1596 | ret = -ENOMEM; |
| 1597 | goto out; |
| 1598 | } |
| 1599 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 1600 | &fs_info->qgroup_tree, node) { |
| 1601 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 1602 | if (ret < 0) |
| 1603 | goto out; |
| 1604 | } |
| 1605 | |
| 1606 | out: |
| 1607 | if (ret < 0) |
| 1608 | btrfs_sysfs_del_qgroups(fs_info); |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, |
| 1613 | struct btrfs_qgroup *qgroup) |
| 1614 | { |
| 1615 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1616 | return; |
| 1617 | |
| 1618 | if (qgroup->kobj.state_initialized) { |
| 1619 | kobject_del(&qgroup->kobj); |
| 1620 | kobject_put(&qgroup->kobj); |
| 1621 | } |
| 1622 | } |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* |
| 1625 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 1626 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 1627 | */ |
| 1628 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, |
| 1629 | u64 bit, enum btrfs_feature_set set) |
| 1630 | { |
| 1631 | struct btrfs_fs_devices *fs_devs; |
| 1632 | struct kobject *fsid_kobj; |
| 1633 | u64 features; |
| 1634 | int ret; |
| 1635 | |
| 1636 | if (!fs_info) |
| 1637 | return; |
| 1638 | |
| 1639 | features = get_features(fs_info, set); |
| 1640 | ASSERT(bit & supported_feature_masks[set]); |
| 1641 | |
| 1642 | fs_devs = fs_info->fs_devices; |
| 1643 | fsid_kobj = &fs_devs->fsid_kobj; |
| 1644 | |
David Sterba | bf60920 | 2016-01-27 14:06:29 +0100 | [diff] [blame] | 1645 | if (!fsid_kobj->state_initialized) |
| 1646 | return; |
| 1647 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1648 | /* |
| 1649 | * FIXME: this is too heavy to update just one value, ideally we'd like |
| 1650 | * to use sysfs_update_group but some refactoring is needed first. |
| 1651 | */ |
| 1652 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1653 | ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1654 | } |
| 1655 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 1656 | int __init btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1657 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1658 | int ret; |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1659 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1660 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 1661 | if (!btrfs_kset) |
| 1662 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1663 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1664 | init_feature_attrs(); |
| 1665 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1666 | if (ret) |
| 1667 | goto out2; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1668 | ret = sysfs_merge_group(&btrfs_kset->kobj, |
| 1669 | &btrfs_static_feature_attr_group); |
| 1670 | if (ret) |
| 1671 | goto out_remove_group; |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1672 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1673 | #ifdef CONFIG_BTRFS_DEBUG |
| 1674 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1675 | if (ret) |
| 1676 | goto out2; |
| 1677 | #endif |
| 1678 | |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1679 | return 0; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1680 | |
| 1681 | out_remove_group: |
| 1682 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1683 | out2: |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1684 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1685 | |
| 1686 | return ret; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1687 | } |
| 1688 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1689 | void __cold btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1690 | { |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1691 | sysfs_unmerge_group(&btrfs_kset->kobj, |
| 1692 | &btrfs_static_feature_attr_group); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1693 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 1694 | #ifdef CONFIG_BTRFS_DEBUG |
| 1695 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1696 | #endif |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1697 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1698 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 1699 | |