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