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 kobject_del(&one_device->devid_kobj); 1174 kobject_put(&one_device->devid_kobj); 1175 1176 wait_for_completion(&one_device->kobj_unregister); 1177 1178 return 0; 1179 } 1180 1181 list_for_each_entry(one_device, &fs_devices->devices, dev_list) { 1182 1183 if (one_device->bdev) { 1184 disk = one_device->bdev->bd_part; 1185 disk_kobj = &part_to_dev(disk)->kobj; 1186 sysfs_remove_link(fs_devices->devices_kobj, 1187 disk_kobj->name); 1188 } 1189 kobject_del(&one_device->devid_kobj); 1190 kobject_put(&one_device->devid_kobj); 1191 1192 wait_for_completion(&one_device->kobj_unregister); 1193 } 1194 1195 return 0; 1196 } 1197 1198 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, 1199 struct kobj_attribute *a, 1200 char *buf) 1201 { 1202 int val; 1203 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1204 devid_kobj); 1205 1206 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); 1207 1208 return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1209 } 1210 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); 1211 1212 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, 1213 struct kobj_attribute *a, char *buf) 1214 { 1215 int val; 1216 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1217 devid_kobj); 1218 1219 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); 1220 1221 return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1222 } 1223 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); 1224 1225 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, 1226 struct kobj_attribute *a, 1227 char *buf) 1228 { 1229 int val; 1230 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1231 devid_kobj); 1232 1233 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); 1234 1235 return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1236 } 1237 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); 1238 1239 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, 1240 struct kobj_attribute *a, char *buf) 1241 { 1242 int val; 1243 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1244 devid_kobj); 1245 1246 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); 1247 1248 return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1249 } 1250 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); 1251 1252 static struct attribute *devid_attrs[] = { 1253 BTRFS_ATTR_PTR(devid, in_fs_metadata), 1254 BTRFS_ATTR_PTR(devid, missing), 1255 BTRFS_ATTR_PTR(devid, replace_target), 1256 BTRFS_ATTR_PTR(devid, writeable), 1257 NULL 1258 }; 1259 ATTRIBUTE_GROUPS(devid); 1260 1261 static void btrfs_release_devid_kobj(struct kobject *kobj) 1262 { 1263 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1264 devid_kobj); 1265 1266 memset(&device->devid_kobj, 0, sizeof(struct kobject)); 1267 complete(&device->kobj_unregister); 1268 } 1269 1270 static struct kobj_type devid_ktype = { 1271 .sysfs_ops = &kobj_sysfs_ops, 1272 .default_groups = devid_groups, 1273 .release = btrfs_release_devid_kobj, 1274 }; 1275 1276 int btrfs_sysfs_add_devices_dir(struct btrfs_fs_devices *fs_devices, 1277 struct btrfs_device *one_device) 1278 { 1279 int error = 0; 1280 struct btrfs_device *dev; 1281 unsigned int nofs_flag; 1282 1283 nofs_flag = memalloc_nofs_save(); 1284 list_for_each_entry(dev, &fs_devices->devices, dev_list) { 1285 1286 if (one_device && one_device != dev) 1287 continue; 1288 1289 if (dev->bdev) { 1290 struct hd_struct *disk; 1291 struct kobject *disk_kobj; 1292 1293 disk = dev->bdev->bd_part; 1294 disk_kobj = &part_to_dev(disk)->kobj; 1295 1296 error = sysfs_create_link(fs_devices->devices_kobj, 1297 disk_kobj, disk_kobj->name); 1298 if (error) 1299 break; 1300 } 1301 1302 init_completion(&dev->kobj_unregister); 1303 error = kobject_init_and_add(&dev->devid_kobj, &devid_ktype, 1304 fs_devices->devinfo_kobj, "%llu", 1305 dev->devid); 1306 if (error) { 1307 kobject_put(&dev->devid_kobj); 1308 break; 1309 } 1310 } 1311 memalloc_nofs_restore(nofs_flag); 1312 1313 return error; 1314 } 1315 1316 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) 1317 { 1318 int ret; 1319 1320 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); 1321 if (ret) 1322 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", 1323 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), 1324 &disk_to_dev(bdev->bd_disk)->kobj); 1325 } 1326 1327 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices, 1328 const u8 *fsid) 1329 { 1330 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 1331 1332 /* 1333 * Sprouting changes fsid of the mounted filesystem, rename the fsid 1334 * directory 1335 */ 1336 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid); 1337 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) 1338 btrfs_warn(fs_devices->fs_info, 1339 "sysfs: failed to create fsid for sprout"); 1340 } 1341 1342 void btrfs_sysfs_update_devid(struct btrfs_device *device) 1343 { 1344 char tmp[24]; 1345 1346 snprintf(tmp, sizeof(tmp), "%llu", device->devid); 1347 1348 if (kobject_rename(&device->devid_kobj, tmp)) 1349 btrfs_warn(device->fs_devices->fs_info, 1350 "sysfs: failed to update devid for %llu", 1351 device->devid); 1352 } 1353 1354 /* /sys/fs/btrfs/ entry */ 1355 static struct kset *btrfs_kset; 1356 1357 /* 1358 * Creates: 1359 * /sys/fs/btrfs/UUID 1360 * 1361 * Can be called by the device discovery thread. 1362 */ 1363 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) 1364 { 1365 int error; 1366 1367 init_completion(&fs_devs->kobj_unregister); 1368 fs_devs->fsid_kobj.kset = btrfs_kset; 1369 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, 1370 "%pU", fs_devs->fsid); 1371 if (error) { 1372 kobject_put(&fs_devs->fsid_kobj); 1373 return error; 1374 } 1375 1376 fs_devs->devices_kobj = kobject_create_and_add("devices", 1377 &fs_devs->fsid_kobj); 1378 if (!fs_devs->devices_kobj) { 1379 btrfs_err(fs_devs->fs_info, 1380 "failed to init sysfs device interface"); 1381 btrfs_sysfs_remove_fsid(fs_devs); 1382 return -ENOMEM; 1383 } 1384 1385 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", 1386 &fs_devs->fsid_kobj); 1387 if (!fs_devs->devinfo_kobj) { 1388 btrfs_err(fs_devs->fs_info, 1389 "failed to init sysfs devinfo kobject"); 1390 btrfs_sysfs_remove_fsid(fs_devs); 1391 return -ENOMEM; 1392 } 1393 1394 return 0; 1395 } 1396 1397 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) 1398 { 1399 int error; 1400 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 1401 struct kobject *fsid_kobj = &fs_devs->fsid_kobj; 1402 1403 btrfs_set_fs_info_ptr(fs_info); 1404 1405 error = btrfs_sysfs_add_devices_dir(fs_devs, NULL); 1406 if (error) 1407 return error; 1408 1409 error = sysfs_create_files(fsid_kobj, btrfs_attrs); 1410 if (error) { 1411 btrfs_sysfs_remove_devices_dir(fs_devs, NULL); 1412 return error; 1413 } 1414 1415 error = sysfs_create_group(fsid_kobj, 1416 &btrfs_feature_attr_group); 1417 if (error) 1418 goto failure; 1419 1420 #ifdef CONFIG_BTRFS_DEBUG 1421 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); 1422 if (!fs_info->debug_kobj) { 1423 error = -ENOMEM; 1424 goto failure; 1425 } 1426 1427 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 1428 if (error) 1429 goto failure; 1430 1431 /* Discard directory */ 1432 fs_info->discard_debug_kobj = kobject_create_and_add("discard", 1433 fs_info->debug_kobj); 1434 if (!fs_info->discard_debug_kobj) { 1435 error = -ENOMEM; 1436 goto failure; 1437 } 1438 1439 error = sysfs_create_files(fs_info->discard_debug_kobj, 1440 discard_debug_attrs); 1441 if (error) 1442 goto failure; 1443 #endif 1444 1445 error = addrm_unknown_feature_attrs(fs_info, true); 1446 if (error) 1447 goto failure; 1448 1449 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); 1450 if (error) 1451 goto failure; 1452 1453 fs_info->space_info_kobj = kobject_create_and_add("allocation", 1454 fsid_kobj); 1455 if (!fs_info->space_info_kobj) { 1456 error = -ENOMEM; 1457 goto failure; 1458 } 1459 1460 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); 1461 if (error) 1462 goto failure; 1463 1464 return 0; 1465 failure: 1466 btrfs_sysfs_remove_mounted(fs_info); 1467 return error; 1468 } 1469 1470 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) 1471 { 1472 return to_fs_info(kobj->parent->parent); 1473 } 1474 1475 #define QGROUP_ATTR(_member, _show_name) \ 1476 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ 1477 struct kobj_attribute *a, \ 1478 char *buf) \ 1479 { \ 1480 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 1481 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 1482 struct btrfs_qgroup, kobj); \ 1483 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ 1484 } \ 1485 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) 1486 1487 #define QGROUP_RSV_ATTR(_name, _type) \ 1488 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ 1489 struct kobj_attribute *a, \ 1490 char *buf) \ 1491 { \ 1492 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 1493 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 1494 struct btrfs_qgroup, kobj); \ 1495 return btrfs_show_u64(&qgroup->rsv.values[_type], \ 1496 &fs_info->qgroup_lock, buf); \ 1497 } \ 1498 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) 1499 1500 QGROUP_ATTR(rfer, referenced); 1501 QGROUP_ATTR(excl, exclusive); 1502 QGROUP_ATTR(max_rfer, max_referenced); 1503 QGROUP_ATTR(max_excl, max_exclusive); 1504 QGROUP_ATTR(lim_flags, limit_flags); 1505 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); 1506 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); 1507 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); 1508 1509 static struct attribute *qgroup_attrs[] = { 1510 BTRFS_ATTR_PTR(qgroup, referenced), 1511 BTRFS_ATTR_PTR(qgroup, exclusive), 1512 BTRFS_ATTR_PTR(qgroup, max_referenced), 1513 BTRFS_ATTR_PTR(qgroup, max_exclusive), 1514 BTRFS_ATTR_PTR(qgroup, limit_flags), 1515 BTRFS_ATTR_PTR(qgroup, rsv_data), 1516 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), 1517 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), 1518 NULL 1519 }; 1520 ATTRIBUTE_GROUPS(qgroup); 1521 1522 static void qgroup_release(struct kobject *kobj) 1523 { 1524 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); 1525 1526 memset(&qgroup->kobj, 0, sizeof(*kobj)); 1527 } 1528 1529 static struct kobj_type qgroup_ktype = { 1530 .sysfs_ops = &kobj_sysfs_ops, 1531 .release = qgroup_release, 1532 .default_groups = qgroup_groups, 1533 }; 1534 1535 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, 1536 struct btrfs_qgroup *qgroup) 1537 { 1538 struct kobject *qgroups_kobj = fs_info->qgroups_kobj; 1539 int ret; 1540 1541 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1542 return 0; 1543 if (qgroup->kobj.state_initialized) 1544 return 0; 1545 if (!qgroups_kobj) 1546 return -EINVAL; 1547 1548 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, 1549 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), 1550 btrfs_qgroup_subvolid(qgroup->qgroupid)); 1551 if (ret < 0) 1552 kobject_put(&qgroup->kobj); 1553 1554 return ret; 1555 } 1556 1557 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) 1558 { 1559 struct btrfs_qgroup *qgroup; 1560 struct btrfs_qgroup *next; 1561 1562 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1563 return; 1564 1565 rbtree_postorder_for_each_entry_safe(qgroup, next, 1566 &fs_info->qgroup_tree, node) 1567 btrfs_sysfs_del_one_qgroup(fs_info, qgroup); 1568 if (fs_info->qgroups_kobj) { 1569 kobject_del(fs_info->qgroups_kobj); 1570 kobject_put(fs_info->qgroups_kobj); 1571 fs_info->qgroups_kobj = NULL; 1572 } 1573 } 1574 1575 /* Called when qgroups get initialized, thus there is no need for locking */ 1576 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) 1577 { 1578 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 1579 struct btrfs_qgroup *qgroup; 1580 struct btrfs_qgroup *next; 1581 int ret = 0; 1582 1583 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1584 return 0; 1585 1586 ASSERT(fsid_kobj); 1587 if (fs_info->qgroups_kobj) 1588 return 0; 1589 1590 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); 1591 if (!fs_info->qgroups_kobj) { 1592 ret = -ENOMEM; 1593 goto out; 1594 } 1595 rbtree_postorder_for_each_entry_safe(qgroup, next, 1596 &fs_info->qgroup_tree, node) { 1597 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); 1598 if (ret < 0) 1599 goto out; 1600 } 1601 1602 out: 1603 if (ret < 0) 1604 btrfs_sysfs_del_qgroups(fs_info); 1605 return ret; 1606 } 1607 1608 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 1609 struct btrfs_qgroup *qgroup) 1610 { 1611 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 1612 return; 1613 1614 if (qgroup->kobj.state_initialized) { 1615 kobject_del(&qgroup->kobj); 1616 kobject_put(&qgroup->kobj); 1617 } 1618 } 1619 1620 /* 1621 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current 1622 * values in superblock. Call after any changes to incompat/compat_ro flags 1623 */ 1624 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, 1625 u64 bit, enum btrfs_feature_set set) 1626 { 1627 struct btrfs_fs_devices *fs_devs; 1628 struct kobject *fsid_kobj; 1629 u64 features; 1630 int ret; 1631 1632 if (!fs_info) 1633 return; 1634 1635 features = get_features(fs_info, set); 1636 ASSERT(bit & supported_feature_masks[set]); 1637 1638 fs_devs = fs_info->fs_devices; 1639 fsid_kobj = &fs_devs->fsid_kobj; 1640 1641 if (!fsid_kobj->state_initialized) 1642 return; 1643 1644 /* 1645 * FIXME: this is too heavy to update just one value, ideally we'd like 1646 * to use sysfs_update_group but some refactoring is needed first. 1647 */ 1648 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 1649 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); 1650 } 1651 1652 int __init btrfs_init_sysfs(void) 1653 { 1654 int ret; 1655 1656 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); 1657 if (!btrfs_kset) 1658 return -ENOMEM; 1659 1660 init_feature_attrs(); 1661 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1662 if (ret) 1663 goto out2; 1664 ret = sysfs_merge_group(&btrfs_kset->kobj, 1665 &btrfs_static_feature_attr_group); 1666 if (ret) 1667 goto out_remove_group; 1668 1669 #ifdef CONFIG_BTRFS_DEBUG 1670 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 1671 if (ret) 1672 goto out2; 1673 #endif 1674 1675 return 0; 1676 1677 out_remove_group: 1678 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1679 out2: 1680 kset_unregister(btrfs_kset); 1681 1682 return ret; 1683 } 1684 1685 void __cold btrfs_exit_sysfs(void) 1686 { 1687 sysfs_unmerge_group(&btrfs_kset->kobj, 1688 &btrfs_static_feature_attr_group); 1689 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1690 #ifdef CONFIG_BTRFS_DEBUG 1691 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 1692 #endif 1693 kset_unregister(btrfs_kset); 1694 } 1695 1696