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