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