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