1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #include <linux/sched.h> 7 #include <linux/sched/mm.h> 8 #include <linux/slab.h> 9 #include <linux/spinlock.h> 10 #include <linux/completion.h> 11 #include <linux/bug.h> 12 #include <crypto/hash.h> 13 14 #include "ctree.h" 15 #include "discard.h" 16 #include "disk-io.h" 17 #include "send.h" 18 #include "transaction.h" 19 #include "sysfs.h" 20 #include "volumes.h" 21 #include "space-info.h" 22 #include "block-group.h" 23 #include "qgroup.h" 24 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 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 93 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); 94 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); 95 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 112 static u64 get_features(struct btrfs_fs_info *fs_info, 113 enum btrfs_feature_set set) 114 { 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); 122 } 123 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: 154 pr_warn("btrfs: sysfs: unknown feature set %d\n", 155 fa->feature_set); 156 return 0; 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 167 static ssize_t btrfs_feature_attr_show(struct kobject *kobj, 168 struct kobj_attribute *a, char *buf) 169 { 170 int val = 0; 171 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 172 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 173 if (fs_info) { 174 u64 features = get_features(fs_info, fa->feature_set); 175 if (features & fa->feature_bit) 176 val = 1; 177 } else 178 val = can_modify_feature(fa); 179 180 return sysfs_emit(buf, "%d\n", val); 181 } 182 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); 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 197 if (sb_rdonly(fs_info->sb)) 198 return -EROFS; 199 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 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 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); 247 248 return count; 249 } 250 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 264 if (can_modify_feature(fa)) 265 mode |= S_IWUSR; 266 else if (!(features & fa->feature_bit)) 267 mode = 0; 268 } 269 270 return mode; 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); 277 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); 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); 282 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); 283 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); 284 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); 285 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); 286 /* Remove once support for zoned allocation is feature complete */ 287 #ifdef CONFIG_BTRFS_DEBUG 288 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED); 289 #endif 290 #ifdef CONFIG_FS_VERITY 291 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY); 292 #endif 293 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 */ 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), 306 BTRFS_FEAT_ATTR_PTR(compress_zstd), 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), 311 BTRFS_FEAT_ATTR_PTR(no_holes), 312 BTRFS_FEAT_ATTR_PTR(metadata_uuid), 313 BTRFS_FEAT_ATTR_PTR(free_space_tree), 314 BTRFS_FEAT_ATTR_PTR(raid1c34), 315 #ifdef CONFIG_BTRFS_DEBUG 316 BTRFS_FEAT_ATTR_PTR(zoned), 317 #endif 318 #ifdef CONFIG_FS_VERITY 319 BTRFS_FEAT_ATTR_PTR(verity), 320 #endif 321 NULL 322 }; 323 324 static const struct attribute_group btrfs_feature_attr_group = { 325 .name = "features", 326 .is_visible = btrfs_feature_visible, 327 .attrs = btrfs_supported_feature_attrs, 328 }; 329 330 static ssize_t rmdir_subvol_show(struct kobject *kobj, 331 struct kobj_attribute *ka, char *buf) 332 { 333 return sysfs_emit(buf, "0\n"); 334 } 335 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); 336 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 */ 348 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "), 349 btrfs_super_csum_name(i)); 350 351 } 352 353 ret += sysfs_emit_at(buf, ret, "\n"); 354 return ret; 355 } 356 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); 357 358 static ssize_t send_stream_version_show(struct kobject *kobj, 359 struct kobj_attribute *ka, char *buf) 360 { 361 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION); 362 } 363 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show); 364 365 static const char *rescue_opts[] = { 366 "usebackuproot", 367 "nologreplay", 368 "ignorebadroots", 369 "ignoredatacsums", 370 "all", 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++) 381 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]); 382 ret += sysfs_emit_at(buf, ret, "\n"); 383 return ret; 384 } 385 BTRFS_ATTR(static_feature, supported_rescue_options, 386 supported_rescue_options_show); 387 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 394 /* 4K sector size is also supported with 64K page size */ 395 if (PAGE_SIZE == SZ_64K) 396 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K); 397 398 /* Only sectorsize == PAGE_SIZE is now supported */ 399 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE); 400 401 return ret; 402 } 403 BTRFS_ATTR(static_feature, supported_sectorsizes, 404 supported_sectorsizes_show); 405 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 */ 412 static struct attribute *btrfs_supported_static_feature_attrs[] = { 413 BTRFS_ATTR_PTR(static_feature, rmdir_subvol), 414 BTRFS_ATTR_PTR(static_feature, supported_checksums), 415 BTRFS_ATTR_PTR(static_feature, send_stream_version), 416 BTRFS_ATTR_PTR(static_feature, supported_rescue_options), 417 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes), 418 NULL 419 }; 420 421 static const struct attribute_group btrfs_static_feature_attr_group = { 422 .name = "features", 423 .attrs = btrfs_supported_static_feature_attrs, 424 }; 425 426 #ifdef CONFIG_BTRFS_DEBUG 427 428 /* 429 * Discard statistics and tunables 430 */ 431 #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) 432 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 439 return sysfs_emit(buf, "%lld\n", 440 atomic64_read(&fs_info->discard_ctl.discardable_bytes)); 441 } 442 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); 443 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 450 return sysfs_emit(buf, "%d\n", 451 atomic_read(&fs_info->discard_ctl.discardable_extents)); 452 } 453 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); 454 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 461 return sysfs_emit(buf, "%llu\n", 462 fs_info->discard_ctl.discard_bitmap_bytes); 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 472 return sysfs_emit(buf, "%lld\n", 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 483 return sysfs_emit(buf, "%llu\n", 484 fs_info->discard_ctl.discard_extent_bytes); 485 } 486 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); 487 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 494 return sysfs_emit(buf, "%u\n", 495 READ_ONCE(fs_info->discard_ctl.iops_limit)); 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); 512 btrfs_discard_calc_delay(discard_ctl); 513 btrfs_discard_schedule_work(discard_ctl, true); 514 return len; 515 } 516 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, 517 btrfs_discard_iops_limit_store); 518 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 525 return sysfs_emit(buf, "%u\n", 526 READ_ONCE(fs_info->discard_ctl.kbps_limit)); 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); 543 btrfs_discard_schedule_work(discard_ctl, true); 544 return len; 545 } 546 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, 547 btrfs_discard_kbps_limit_store); 548 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 555 return sysfs_emit(buf, "%llu\n", 556 READ_ONCE(fs_info->discard_ctl.max_discard_size)); 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 579 /* 580 * Per-filesystem debugging of discard (when mounted with discard=async). 581 * 582 * Path: /sys/fs/btrfs/<uuid>/debug/discard/ 583 */ 584 static const struct attribute *discard_debug_attrs[] = { 585 BTRFS_ATTR_PTR(discard, discardable_bytes), 586 BTRFS_ATTR_PTR(discard, discardable_extents), 587 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), 588 BTRFS_ATTR_PTR(discard, discard_bytes_saved), 589 BTRFS_ATTR_PTR(discard, discard_extent_bytes), 590 BTRFS_ATTR_PTR(discard, iops_limit), 591 BTRFS_ATTR_PTR(discard, kbps_limit), 592 BTRFS_ATTR_PTR(discard, max_discard_size), 593 NULL, 594 }; 595 596 /* 597 * Per-filesystem runtime debugging exported via sysfs. 598 * 599 * Path: /sys/fs/btrfs/UUID/debug/ 600 */ 601 static const struct attribute *btrfs_debug_mount_attrs[] = { 602 NULL, 603 }; 604 605 /* 606 * Runtime debugging exported via sysfs, applies to all mounted filesystems. 607 * 608 * Path: /sys/fs/btrfs/debug 609 */ 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 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); 629 return sysfs_emit(buf, "%llu\n", val); 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 } 639 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); 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 } 648 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); 649 650 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) 651 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) 652 653 static ssize_t raid_bytes_show(struct kobject *kobj, 654 struct kobj_attribute *attr, char *buf); 655 BTRFS_ATTR(raid, total_bytes, raid_bytes_show); 656 BTRFS_ATTR(raid, used_bytes, raid_bytes_show); 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); 663 struct btrfs_block_group *block_group; 664 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); 665 u64 val = 0; 666 667 down_read(&sinfo->groups_sem); 668 list_for_each_entry(block_group, &sinfo->block_groups[index], list) { 669 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) 670 val += block_group->length; 671 else 672 val += block_group->used; 673 } 674 up_read(&sinfo->groups_sem); 675 return sysfs_emit(buf, "%llu\n", val); 676 } 677 678 /* 679 * Allocation information about block group profiles. 680 * 681 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/ 682 */ 683 static struct attribute *raid_attrs[] = { 684 BTRFS_ATTR_PTR(raid, total_bytes), 685 BTRFS_ATTR_PTR(raid, used_bytes), 686 NULL 687 }; 688 ATTRIBUTE_GROUPS(raid); 689 690 static void release_raid_kobj(struct kobject *kobj) 691 { 692 kfree(to_raid_kobj(kobj)); 693 } 694 695 static struct kobj_type btrfs_raid_ktype = { 696 .sysfs_ops = &kobj_sysfs_ops, 697 .release = release_raid_kobj, 698 .default_groups = raid_groups, 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 } \ 709 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) 710 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); 717 SPACE_INFO_ATTR(bytes_readonly); 718 SPACE_INFO_ATTR(bytes_zone_unusable); 719 SPACE_INFO_ATTR(disk_used); 720 SPACE_INFO_ATTR(disk_total); 721 722 /* 723 * Allocation information about block group types. 724 * 725 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/ 726 */ 727 static struct attribute *space_info_attrs[] = { 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), 735 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable), 736 BTRFS_ATTR_PTR(space_info, disk_used), 737 BTRFS_ATTR_PTR(space_info, disk_total), 738 NULL, 739 }; 740 ATTRIBUTE_GROUPS(space_info); 741 742 static void space_info_release(struct kobject *kobj) 743 { 744 struct btrfs_space_info *sinfo = to_space_info(kobj); 745 kfree(sinfo); 746 } 747 748 static struct kobj_type space_info_ktype = { 749 .sysfs_ops = &kobj_sysfs_ops, 750 .release = space_info_release, 751 .default_groups = space_info_groups, 752 }; 753 754 /* 755 * Allocation information about block groups. 756 * 757 * Path: /sys/fs/btrfs/<uuid>/allocation/ 758 */ 759 static const struct attribute *allocation_attrs[] = { 760 BTRFS_ATTR_PTR(allocation, global_rsv_reserved), 761 BTRFS_ATTR_PTR(allocation, global_rsv_size), 762 NULL, 763 }; 764 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); 769 char *label = fs_info->super_copy->label; 770 ssize_t ret; 771 772 spin_lock(&fs_info->super_lock); 773 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label); 774 spin_unlock(&fs_info->super_lock); 775 776 return ret; 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); 784 size_t p_len; 785 786 if (!fs_info) 787 return -EPERM; 788 789 if (sb_rdonly(fs_info->sb)) 790 return -EROFS; 791 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) 799 return -EINVAL; 800 801 spin_lock(&fs_info->super_lock); 802 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); 803 memcpy(fs_info->super_copy->label, buf, p_len); 804 spin_unlock(&fs_info->super_lock); 805 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); 811 812 return len; 813 } 814 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); 815 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 821 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize); 822 } 823 824 BTRFS_ATTR(, nodesize, btrfs_nodesize_show); 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 831 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize); 832 } 833 834 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); 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 841 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize); 842 } 843 844 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); 845 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); 853 return sysfs_emit(buf, "%d\n", quota_override); 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 884 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); 885 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 891 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid); 892 } 893 894 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); 895 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 902 return sysfs_emit(buf, "%s (%s)\n", 903 btrfs_super_csum_name(csum_type), 904 crypto_shash_driver_name(fs_info->csum_shash)); 905 } 906 907 BTRFS_ATTR(, checksum, btrfs_checksum_show); 908 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 } 941 return sysfs_emit(buf, "%s", str); 942 } 943 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); 944 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 950 return sysfs_emit(buf, "%llu\n", fs_info->generation); 951 } 952 BTRFS_ATTR(, generation, btrfs_generation_show); 953 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 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 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 1028 ret = sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold)); 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 1045 if (thresh != 0 && (thresh <= 50 || thresh > 100)) 1046 return -EINVAL; 1047 1048 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh); 1049 1050 return len; 1051 } 1052 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show, 1053 btrfs_bg_reclaim_threshold_store); 1054 1055 /* 1056 * Per-filesystem information and stats. 1057 * 1058 * Path: /sys/fs/btrfs/<uuid>/ 1059 */ 1060 static const struct attribute *btrfs_attrs[] = { 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), 1066 BTRFS_ATTR_PTR(, metadata_uuid), 1067 BTRFS_ATTR_PTR(, checksum), 1068 BTRFS_ATTR_PTR(, exclusive_operation), 1069 BTRFS_ATTR_PTR(, generation), 1070 BTRFS_ATTR_PTR(, read_policy), 1071 BTRFS_ATTR_PTR(, bg_reclaim_threshold), 1072 NULL, 1073 }; 1074 1075 static void btrfs_release_fsid_kobj(struct kobject *kobj) 1076 { 1077 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); 1078 1079 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); 1080 complete(&fs_devs->kobj_unregister); 1081 } 1082 1083 static struct kobj_type btrfs_ktype = { 1084 .sysfs_ops = &kobj_sysfs_ops, 1085 .release = btrfs_release_fsid_kobj, 1086 }; 1087 1088 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) 1089 { 1090 if (kobj->ktype != &btrfs_ktype) 1091 return NULL; 1092 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); 1093 } 1094 1095 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) 1096 { 1097 if (kobj->ktype != &btrfs_ktype) 1098 return NULL; 1099 return to_fs_devs(kobj)->fs_info; 1100 } 1101 1102 #define NUM_FEATURE_BITS 64 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]; 1106 1107 static const u64 supported_feature_masks[FEAT_MAX] = { 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) 1114 { 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; 1141 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, 1142 &agroup); 1143 if (ret) 1144 return ret; 1145 } else 1146 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, 1147 &agroup); 1148 } 1149 1150 } 1151 return 0; 1152 } 1153 1154 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 1155 { 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 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; 1166 } 1167 1168 if (fs_devs->fsid_kobj.state_initialized) { 1169 kobject_del(&fs_devs->fsid_kobj); 1170 kobject_put(&fs_devs->fsid_kobj); 1171 wait_for_completion(&fs_devs->kobj_unregister); 1172 } 1173 } 1174 1175 /* when fs_devs is NULL it will remove all fsid kobject */ 1176 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 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 1185 list_for_each_entry(fs_devs, fs_uuids, fs_list) { 1186 __btrfs_sysfs_remove_fsid(fs_devs); 1187 } 1188 } 1189 1190 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) 1191 { 1192 struct btrfs_device *device; 1193 struct btrfs_fs_devices *seed; 1194 1195 list_for_each_entry(device, &fs_devices->devices, dev_list) 1196 btrfs_sysfs_remove_device(device); 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 } 1202 } 1203 1204 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) 1205 { 1206 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 1207 1208 sysfs_remove_link(fsid_kobj, "bdi"); 1209 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 } 1215 #ifdef CONFIG_BTRFS_DEBUG 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 } 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 } 1227 #endif 1228 addrm_unknown_feature_attrs(fs_info, false); 1229 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 1230 sysfs_remove_files(fsid_kobj, btrfs_attrs); 1231 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices); 1232 } 1233 1234 static const char * const btrfs_feature_set_names[FEAT_MAX] = { 1235 [FEAT_COMPAT] = "compat", 1236 [FEAT_COMPAT_RO] = "compat_ro", 1237 [FEAT_INCOMPAT] = "incompat", 1238 }; 1239 1240 const char *btrfs_feature_set_name(enum btrfs_feature_set set) 1241 { 1242 return btrfs_feature_set_names[set]; 1243 } 1244 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; 1263 len += scnprintf(str + len, bufsize - len, "%s%s", 1264 len ? "," : "", name); 1265 } 1266 1267 return str; 1268 } 1269 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 1280 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); 1281 memset(btrfs_unknown_feature_names, 0, 1282 sizeof(btrfs_unknown_feature_names)); 1283 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]; 1287 int bit; 1288 sfa = attr_to_btrfs_feature_attr(a); 1289 bit = ilog2(sfa->feature_bit); 1290 fa = &btrfs_feature_attrs[sfa->feature_set][bit]; 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 1303 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", 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 1314 /* 1315 * Create a sysfs entry for a given block group type at path 1316 * /sys/fs/btrfs/UUID/allocation/data/TYPE 1317 */ 1318 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) 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); 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 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) { 1370 spin_lock(&space_info->lock); 1371 space_info->block_group_kobjs[index] = NULL; 1372 spin_unlock(&space_info->lock); 1373 kobject_put(&rkobj->kobj); 1374 btrfs_warn(fs_info, 1375 "failed to add kobject for block cache, ignoring"); 1376 return; 1377 } 1378 } 1379 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 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"; 1416 } 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 1439 void btrfs_sysfs_remove_device(struct btrfs_device *device) 1440 { 1441 struct kobject *devices_kobj; 1442 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); 1449 1450 if (device->bdev) 1451 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name); 1452 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 } 1459 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 1470 return sysfs_emit(buf, "%d\n", val); 1471 } 1472 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); 1473 1474 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, 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 1483 return sysfs_emit(buf, "%d\n", val); 1484 } 1485 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); 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 1497 return sysfs_emit(buf, "%d\n", val); 1498 } 1499 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); 1500 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 1508 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max)); 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 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 1536 return sysfs_emit(buf, "%d\n", val); 1537 } 1538 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); 1539 1540 static ssize_t btrfs_devinfo_error_stats_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 if (!device->dev_stats_valid) 1547 return sysfs_emit(buf, "invalid\n"); 1548 1549 /* 1550 * Print all at once so we get a snapshot of all values from the same 1551 * time. Keep them in sync and in order of definition of 1552 * btrfs_dev_stat_values. 1553 */ 1554 return sysfs_emit(buf, 1555 "write_errs %d\n" 1556 "read_errs %d\n" 1557 "flush_errs %d\n" 1558 "corruption_errs %d\n" 1559 "generation_errs %d\n", 1560 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS), 1561 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS), 1562 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS), 1563 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS), 1564 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS)); 1565 } 1566 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show); 1567 1568 /* 1569 * Information about one device. 1570 * 1571 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/ 1572 */ 1573 static struct attribute *devid_attrs[] = { 1574 BTRFS_ATTR_PTR(devid, error_stats), 1575 BTRFS_ATTR_PTR(devid, in_fs_metadata), 1576 BTRFS_ATTR_PTR(devid, missing), 1577 BTRFS_ATTR_PTR(devid, replace_target), 1578 BTRFS_ATTR_PTR(devid, scrub_speed_max), 1579 BTRFS_ATTR_PTR(devid, writeable), 1580 NULL 1581 }; 1582 ATTRIBUTE_GROUPS(devid); 1583 1584 static void btrfs_release_devid_kobj(struct kobject *kobj) 1585 { 1586 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1587 devid_kobj); 1588 1589 memset(&device->devid_kobj, 0, sizeof(struct kobject)); 1590 complete(&device->kobj_unregister); 1591 } 1592 1593 static struct kobj_type devid_ktype = { 1594 .sysfs_ops = &kobj_sysfs_ops, 1595 .default_groups = devid_groups, 1596 .release = btrfs_release_devid_kobj, 1597 }; 1598 1599 int btrfs_sysfs_add_device(struct btrfs_device *device) 1600 { 1601 int ret; 1602 unsigned int nofs_flag; 1603 struct kobject *devices_kobj; 1604 struct kobject *devinfo_kobj; 1605 1606 /* 1607 * Make sure we use the fs_info::fs_devices to fetch the kobjects even 1608 * for the seed fs_devices 1609 */ 1610 devices_kobj = device->fs_info->fs_devices->devices_kobj; 1611 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; 1612 ASSERT(devices_kobj); 1613 ASSERT(devinfo_kobj); 1614 1615 nofs_flag = memalloc_nofs_save(); 1616 1617 if (device->bdev) { 1618 struct kobject *disk_kobj = bdev_kobj(device->bdev); 1619 1620 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name); 1621 if (ret) { 1622 btrfs_warn(device->fs_info, 1623 "creating sysfs device link for devid %llu failed: %d", 1624 device->devid, ret); 1625 goto out; 1626 } 1627 } 1628 1629 init_completion(&device->kobj_unregister); 1630 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype, 1631 devinfo_kobj, "%llu", device->devid); 1632 if (ret) { 1633 kobject_put(&device->devid_kobj); 1634 btrfs_warn(device->fs_info, 1635 "devinfo init for devid %llu failed: %d", 1636 device->devid, ret); 1637 } 1638 1639 out: 1640 memalloc_nofs_restore(nofs_flag); 1641 return ret; 1642 } 1643 1644 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) 1645 { 1646 int ret; 1647 struct btrfs_device *device; 1648 struct btrfs_fs_devices *seed; 1649 1650 list_for_each_entry(device, &fs_devices->devices, dev_list) { 1651 ret = btrfs_sysfs_add_device(device); 1652 if (ret) 1653 goto fail; 1654 } 1655 1656 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 1657 list_for_each_entry(device, &seed->devices, dev_list) { 1658 ret = btrfs_sysfs_add_device(device); 1659 if (ret) 1660 goto fail; 1661 } 1662 } 1663 1664 return 0; 1665 1666 fail: 1667 btrfs_sysfs_remove_fs_devices(fs_devices); 1668 return ret; 1669 } 1670 1671 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) 1672 { 1673 int ret; 1674 1675 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); 1676 if (ret) 1677 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", 1678 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), 1679 &disk_to_dev(bdev->bd_disk)->kobj); 1680 } 1681 1682 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) 1683 1684 { 1685 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 1686 1687 /* 1688 * Sprouting changes fsid of the mounted filesystem, rename the fsid 1689 * directory 1690 */ 1691 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); 1692 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) 1693 btrfs_warn(fs_devices->fs_info, 1694 "sysfs: failed to create fsid for sprout"); 1695 } 1696 1697 void btrfs_sysfs_update_devid(struct btrfs_device *device) 1698 { 1699 char tmp[24]; 1700 1701 snprintf(tmp, sizeof(tmp), "%llu", device->devid); 1702 1703 if (kobject_rename(&device->devid_kobj, tmp)) 1704 btrfs_warn(device->fs_devices->fs_info, 1705 "sysfs: failed to update devid for %llu", 1706 device->devid); 1707 } 1708 1709 /* /sys/fs/btrfs/ entry */ 1710 static struct kset *btrfs_kset; 1711 1712 /* 1713 * Creates: 1714 * /sys/fs/btrfs/UUID 1715 * 1716 * Can be called by the device discovery thread. 1717 */ 1718 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) 1719 { 1720 int error; 1721 1722 init_completion(&fs_devs->kobj_unregister); 1723 fs_devs->fsid_kobj.kset = btrfs_kset; 1724 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, 1725 "%pU", fs_devs->fsid); 1726 if (error) { 1727 kobject_put(&fs_devs->fsid_kobj); 1728 return error; 1729 } 1730 1731 fs_devs->devices_kobj = kobject_create_and_add("devices", 1732 &fs_devs->fsid_kobj); 1733 if (!fs_devs->devices_kobj) { 1734 btrfs_err(fs_devs->fs_info, 1735 "failed to init sysfs device interface"); 1736 btrfs_sysfs_remove_fsid(fs_devs); 1737 return -ENOMEM; 1738 } 1739 1740 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", 1741 &fs_devs->fsid_kobj); 1742 if (!fs_devs->devinfo_kobj) { 1743 btrfs_err(fs_devs->fs_info, 1744 "failed to init sysfs devinfo kobject"); 1745 btrfs_sysfs_remove_fsid(fs_devs); 1746 return -ENOMEM; 1747 } 1748 1749 return 0; 1750 } 1751 1752 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) 1753 { 1754 int error; 1755 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 1756 struct kobject *fsid_kobj = &fs_devs->fsid_kobj; 1757 1758 error = btrfs_sysfs_add_fs_devices(fs_devs); 1759 if (error) 1760 return error; 1761 1762 error = sysfs_create_files(fsid_kobj, btrfs_attrs); 1763 if (error) { 1764 btrfs_sysfs_remove_fs_devices(fs_devs); 1765 return error; 1766 } 1767 1768 error = sysfs_create_group(fsid_kobj, 1769 &btrfs_feature_attr_group); 1770 if (error) 1771 goto failure; 1772 1773 #ifdef CONFIG_BTRFS_DEBUG 1774 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); 1775 if (!fs_info->debug_kobj) { 1776 error = -ENOMEM; 1777 goto failure; 1778 } 1779 1780 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 1781 if (error) 1782 goto failure; 1783 1784 /* Discard directory */ 1785 fs_info->discard_debug_kobj = kobject_create_and_add("discard", 1786 fs_info->debug_kobj); 1787 if (!fs_info->discard_debug_kobj) { 1788 error = -ENOMEM; 1789 goto failure; 1790 } 1791 1792 error = sysfs_create_files(fs_info->discard_debug_kobj, 1793 discard_debug_attrs); 1794 if (error) 1795 goto failure; 1796 #endif 1797 1798 error = addrm_unknown_feature_attrs(fs_info, true); 1799 if (error) 1800 goto failure; 1801 1802 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); 1803 if (error) 1804 goto failure; 1805 1806 fs_info->space_info_kobj = kobject_create_and_add("allocation", 1807 fsid_kobj); 1808 if (!fs_info->space_info_kobj) { 1809 error = -ENOMEM; 1810 goto failure; 1811 } 1812 1813 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); 1814 if (error) 1815 goto failure; 1816 1817 return 0; 1818 failure: 1819 btrfs_sysfs_remove_mounted(fs_info); 1820 return error; 1821 } 1822 1823 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) 1824 { 1825 return to_fs_info(kobj->parent->parent); 1826 } 1827 1828 #define QGROUP_ATTR(_member, _show_name) \ 1829 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ 1830 struct kobj_attribute *a, \ 1831 char *buf) \ 1832 { \ 1833 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 1834 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 1835 struct btrfs_qgroup, kobj); \ 1836 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ 1837 } \ 1838 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) 1839 1840 #define QGROUP_RSV_ATTR(_name, _type) \ 1841 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ 1842 struct kobj_attribute *a, \ 1843 char *buf) \ 1844 { \ 1845 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 1846 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 1847 struct btrfs_qgroup, kobj); \ 1848 return btrfs_show_u64(&qgroup->rsv.values[_type], \ 1849 &fs_info->qgroup_lock, buf); \ 1850 } \ 1851 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) 1852 1853 QGROUP_ATTR(rfer, referenced); 1854 QGROUP_ATTR(excl, exclusive); 1855 QGROUP_ATTR(max_rfer, max_referenced); 1856 QGROUP_ATTR(max_excl, max_exclusive); 1857 QGROUP_ATTR(lim_flags, limit_flags); 1858 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); 1859 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); 1860 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); 1861 1862 /* 1863 * Qgroup information. 1864 * 1865 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/ 1866 */ 1867 static struct attribute *qgroup_attrs[] = { 1868 BTRFS_ATTR_PTR(qgroup, referenced), 1869 BTRFS_ATTR_PTR(qgroup, exclusive), 1870 BTRFS_ATTR_PTR(qgroup, max_referenced), 1871 BTRFS_ATTR_PTR(qgroup, max_exclusive), 1872 BTRFS_ATTR_PTR(qgroup, limit_flags), 1873 BTRFS_ATTR_PTR(qgroup, rsv_data), 1874 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), 1875 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), 1876 NULL 1877 }; 1878 ATTRIBUTE_GROUPS(qgroup); 1879 1880 static void qgroup_release(struct kobject *kobj) 1881 { 1882 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); 1883 1884 memset(&qgroup->kobj, 0, sizeof(*kobj)); 1885 } 1886 1887 static struct kobj_type qgroup_ktype = { 1888 .sysfs_ops = &kobj_sysfs_ops, 1889 .release = qgroup_release, 1890 .default_groups = qgroup_groups, 1891 }; 1892 1893 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, 1894 struct btrfs_qgroup *qgroup) 1895 { 1896 struct kobject *qgroups_kobj = fs_info->qgroups_kobj; 1897 int ret; 1898 1899 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1900 return 0; 1901 if (qgroup->kobj.state_initialized) 1902 return 0; 1903 if (!qgroups_kobj) 1904 return -EINVAL; 1905 1906 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, 1907 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), 1908 btrfs_qgroup_subvolid(qgroup->qgroupid)); 1909 if (ret < 0) 1910 kobject_put(&qgroup->kobj); 1911 1912 return ret; 1913 } 1914 1915 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) 1916 { 1917 struct btrfs_qgroup *qgroup; 1918 struct btrfs_qgroup *next; 1919 1920 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1921 return; 1922 1923 rbtree_postorder_for_each_entry_safe(qgroup, next, 1924 &fs_info->qgroup_tree, node) 1925 btrfs_sysfs_del_one_qgroup(fs_info, qgroup); 1926 if (fs_info->qgroups_kobj) { 1927 kobject_del(fs_info->qgroups_kobj); 1928 kobject_put(fs_info->qgroups_kobj); 1929 fs_info->qgroups_kobj = NULL; 1930 } 1931 } 1932 1933 /* Called when qgroups get initialized, thus there is no need for locking */ 1934 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) 1935 { 1936 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 1937 struct btrfs_qgroup *qgroup; 1938 struct btrfs_qgroup *next; 1939 int ret = 0; 1940 1941 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1942 return 0; 1943 1944 ASSERT(fsid_kobj); 1945 if (fs_info->qgroups_kobj) 1946 return 0; 1947 1948 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); 1949 if (!fs_info->qgroups_kobj) { 1950 ret = -ENOMEM; 1951 goto out; 1952 } 1953 rbtree_postorder_for_each_entry_safe(qgroup, next, 1954 &fs_info->qgroup_tree, node) { 1955 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); 1956 if (ret < 0) 1957 goto out; 1958 } 1959 1960 out: 1961 if (ret < 0) 1962 btrfs_sysfs_del_qgroups(fs_info); 1963 return ret; 1964 } 1965 1966 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 1967 struct btrfs_qgroup *qgroup) 1968 { 1969 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1970 return; 1971 1972 if (qgroup->kobj.state_initialized) { 1973 kobject_del(&qgroup->kobj); 1974 kobject_put(&qgroup->kobj); 1975 } 1976 } 1977 1978 /* 1979 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current 1980 * values in superblock. Call after any changes to incompat/compat_ro flags 1981 */ 1982 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, 1983 u64 bit, enum btrfs_feature_set set) 1984 { 1985 struct btrfs_fs_devices *fs_devs; 1986 struct kobject *fsid_kobj; 1987 u64 __maybe_unused features; 1988 int __maybe_unused ret; 1989 1990 if (!fs_info) 1991 return; 1992 1993 /* 1994 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not 1995 * safe when called from some contexts (eg. balance) 1996 */ 1997 features = get_features(fs_info, set); 1998 ASSERT(bit & supported_feature_masks[set]); 1999 2000 fs_devs = fs_info->fs_devices; 2001 fsid_kobj = &fs_devs->fsid_kobj; 2002 2003 if (!fsid_kobj->state_initialized) 2004 return; 2005 2006 /* 2007 * FIXME: this is too heavy to update just one value, ideally we'd like 2008 * to use sysfs_update_group but some refactoring is needed first. 2009 */ 2010 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 2011 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); 2012 } 2013 2014 int __init btrfs_init_sysfs(void) 2015 { 2016 int ret; 2017 2018 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); 2019 if (!btrfs_kset) 2020 return -ENOMEM; 2021 2022 init_feature_attrs(); 2023 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2024 if (ret) 2025 goto out2; 2026 ret = sysfs_merge_group(&btrfs_kset->kobj, 2027 &btrfs_static_feature_attr_group); 2028 if (ret) 2029 goto out_remove_group; 2030 2031 #ifdef CONFIG_BTRFS_DEBUG 2032 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 2033 if (ret) 2034 goto out2; 2035 #endif 2036 2037 return 0; 2038 2039 out_remove_group: 2040 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2041 out2: 2042 kset_unregister(btrfs_kset); 2043 2044 return ret; 2045 } 2046 2047 void __cold btrfs_exit_sysfs(void) 2048 { 2049 sysfs_unmerge_group(&btrfs_kset->kobj, 2050 &btrfs_static_feature_attr_group); 2051 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2052 #ifdef CONFIG_BTRFS_DEBUG 2053 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 2054 #endif 2055 kset_unregister(btrfs_kset); 2056 } 2057 2058