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