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 <linux/list.h> 13 #include <linux/string_choices.h> 14 #include "messages.h" 15 #include "ctree.h" 16 #include "discard.h" 17 #include "disk-io.h" 18 #include "send.h" 19 #include "transaction.h" 20 #include "sysfs.h" 21 #include "volumes.h" 22 #include "space-info.h" 23 #include "block-group.h" 24 #include "qgroup.h" 25 #include "misc.h" 26 #include "fs.h" 27 #include "accessors.h" 28 29 /* 30 * Structure name Path 31 * -------------------------------------------------------------------------- 32 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features 33 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and 34 * /sys/fs/btrfs/<uuid>/features 35 * btrfs_attrs /sys/fs/btrfs/<uuid> 36 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid> 37 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation 38 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid> 39 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type> 40 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile> 41 * discard_attrs /sys/fs/btrfs/<uuid>/discard 42 * 43 * When built with BTRFS_CONFIG_DEBUG: 44 * 45 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug 46 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug 47 */ 48 49 struct btrfs_feature_attr { 50 struct kobj_attribute kobj_attr; 51 enum btrfs_feature_set feature_set; 52 u64 feature_bit; 53 }; 54 55 /* For raid type sysfs entries */ 56 struct raid_kobject { 57 u64 flags; 58 struct kobject kobj; 59 }; 60 61 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ 62 { \ 63 .attr = { .name = __stringify(_name), .mode = _mode }, \ 64 .show = _show, \ 65 .store = _store, \ 66 } 67 68 #define BTRFS_ATTR_W(_prefix, _name, _store) \ 69 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 70 __INIT_KOBJ_ATTR(_name, 0200, NULL, _store) 71 72 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ 73 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 74 __INIT_KOBJ_ATTR(_name, 0644, _show, _store) 75 76 #define BTRFS_ATTR(_prefix, _name, _show) \ 77 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 78 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) 79 80 #define BTRFS_ATTR_PTR(_prefix, _name) \ 81 (&btrfs_attr_##_prefix##_##_name.attr) 82 83 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ 84 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ 85 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ 86 btrfs_feature_attr_show, \ 87 btrfs_feature_attr_store), \ 88 .feature_set = _feature_set, \ 89 .feature_bit = _feature_prefix ##_## _feature_bit, \ 90 } 91 #define BTRFS_FEAT_ATTR_PTR(_name) \ 92 (&btrfs_attr_features_##_name.kobj_attr.attr) 93 94 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ 95 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) 96 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ 97 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) 98 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ 99 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) 100 101 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); 102 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); 103 static struct kobject *get_btrfs_kobj(struct kobject *kobj); 104 105 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) 106 { 107 return container_of(a, struct btrfs_feature_attr, kobj_attr); 108 } 109 110 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) 111 { 112 return container_of(attr, struct kobj_attribute, attr); 113 } 114 115 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( 116 struct attribute *attr) 117 { 118 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); 119 } 120 121 static u64 get_features(struct btrfs_fs_info *fs_info, 122 enum btrfs_feature_set set) 123 { 124 struct btrfs_super_block *disk_super = fs_info->super_copy; 125 if (set == FEAT_COMPAT) 126 return btrfs_super_compat_flags(disk_super); 127 else if (set == FEAT_COMPAT_RO) 128 return btrfs_super_compat_ro_flags(disk_super); 129 else 130 return btrfs_super_incompat_flags(disk_super); 131 } 132 133 static void set_features(struct btrfs_fs_info *fs_info, 134 enum btrfs_feature_set set, u64 features) 135 { 136 struct btrfs_super_block *disk_super = fs_info->super_copy; 137 if (set == FEAT_COMPAT) 138 btrfs_set_super_compat_flags(disk_super, features); 139 else if (set == FEAT_COMPAT_RO) 140 btrfs_set_super_compat_ro_flags(disk_super, features); 141 else 142 btrfs_set_super_incompat_flags(disk_super, features); 143 } 144 145 static int can_modify_feature(struct btrfs_feature_attr *fa) 146 { 147 int val = 0; 148 u64 set, clear; 149 switch (fa->feature_set) { 150 case FEAT_COMPAT: 151 set = BTRFS_FEATURE_COMPAT_SAFE_SET; 152 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 153 break; 154 case FEAT_COMPAT_RO: 155 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 156 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 157 break; 158 case FEAT_INCOMPAT: 159 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 160 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 161 break; 162 default: 163 btrfs_warn(NULL, "sysfs: unknown feature set %d", fa->feature_set); 164 return 0; 165 } 166 167 if (set & fa->feature_bit) 168 val |= 1; 169 if (clear & fa->feature_bit) 170 val |= 2; 171 172 return val; 173 } 174 175 static ssize_t btrfs_feature_attr_show(struct kobject *kobj, 176 struct kobj_attribute *a, char *buf) 177 { 178 int val = 0; 179 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 180 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 181 if (fs_info) { 182 u64 features = get_features(fs_info, fa->feature_set); 183 if (features & fa->feature_bit) 184 val = 1; 185 } else 186 val = can_modify_feature(fa); 187 188 return sysfs_emit(buf, "%d\n", val); 189 } 190 191 static ssize_t btrfs_feature_attr_store(struct kobject *kobj, 192 struct kobj_attribute *a, 193 const char *buf, size_t count) 194 { 195 struct btrfs_fs_info *fs_info; 196 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 197 u64 features, set, clear; 198 unsigned long val; 199 int ret; 200 201 fs_info = to_fs_info(kobj); 202 if (!fs_info) 203 return -EPERM; 204 205 if (sb_rdonly(fs_info->sb)) 206 return -EROFS; 207 208 ret = kstrtoul(skip_spaces(buf), 0, &val); 209 if (ret) 210 return ret; 211 212 if (fa->feature_set == FEAT_COMPAT) { 213 set = BTRFS_FEATURE_COMPAT_SAFE_SET; 214 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 215 } else if (fa->feature_set == FEAT_COMPAT_RO) { 216 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 217 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 218 } else { 219 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 220 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 221 } 222 223 features = get_features(fs_info, fa->feature_set); 224 225 /* Nothing to do */ 226 if ((val && (features & fa->feature_bit)) || 227 (!val && !(features & fa->feature_bit))) 228 return count; 229 230 if ((val && !(set & fa->feature_bit)) || 231 (!val && !(clear & fa->feature_bit))) { 232 btrfs_info(fs_info, 233 "%sabling feature %s on mounted fs is not supported.", 234 val ? "En" : "Dis", fa->kobj_attr.attr.name); 235 return -EPERM; 236 } 237 238 btrfs_info(fs_info, "%s %s feature flag", 239 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); 240 241 spin_lock(&fs_info->super_lock); 242 features = get_features(fs_info, fa->feature_set); 243 if (val) 244 features |= fa->feature_bit; 245 else 246 features &= ~fa->feature_bit; 247 set_features(fs_info, fa->feature_set, features); 248 spin_unlock(&fs_info->super_lock); 249 250 /* 251 * We don't want to do full transaction commit from inside sysfs 252 */ 253 set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags); 254 wake_up_process(fs_info->transaction_kthread); 255 256 return count; 257 } 258 259 static umode_t btrfs_feature_visible(struct kobject *kobj, 260 struct attribute *attr, int unused) 261 { 262 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 263 umode_t mode = attr->mode; 264 265 if (fs_info) { 266 struct btrfs_feature_attr *fa; 267 u64 features; 268 269 fa = attr_to_btrfs_feature_attr(attr); 270 features = get_features(fs_info, fa->feature_set); 271 272 if (can_modify_feature(fa)) 273 mode |= S_IWUSR; 274 else if (!(features & fa->feature_bit)) 275 mode = 0; 276 } 277 278 return mode; 279 } 280 281 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); 282 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); 283 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); 284 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); 285 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); 286 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); 287 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); 288 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); 289 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); 290 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); 291 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE); 292 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); 293 BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA); 294 #ifdef CONFIG_BLK_DEV_ZONED 295 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED); 296 #endif 297 #ifdef CONFIG_BTRFS_EXPERIMENTAL 298 /* Remove once support for extent tree v2 is feature complete */ 299 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2); 300 /* Remove once support for raid stripe tree is feature complete. */ 301 BTRFS_FEAT_ATTR_INCOMPAT(raid_stripe_tree, RAID_STRIPE_TREE); 302 /* Remove once support for remap tree is feature complete. */ 303 BTRFS_FEAT_ATTR_INCOMPAT(remap_tree, REMAP_TREE); 304 #endif 305 #ifdef CONFIG_FS_VERITY 306 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY); 307 #endif 308 309 /* 310 * Features which depend on feature bits and may differ between each fs. 311 * 312 * /sys/fs/btrfs/features - all available features implemented by this version 313 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or 314 * can be changed on a mounted filesystem. 315 */ 316 static struct attribute *btrfs_supported_feature_attrs[] = { 317 BTRFS_FEAT_ATTR_PTR(default_subvol), 318 BTRFS_FEAT_ATTR_PTR(mixed_groups), 319 BTRFS_FEAT_ATTR_PTR(compress_lzo), 320 BTRFS_FEAT_ATTR_PTR(compress_zstd), 321 BTRFS_FEAT_ATTR_PTR(extended_iref), 322 BTRFS_FEAT_ATTR_PTR(raid56), 323 BTRFS_FEAT_ATTR_PTR(skinny_metadata), 324 BTRFS_FEAT_ATTR_PTR(no_holes), 325 BTRFS_FEAT_ATTR_PTR(metadata_uuid), 326 BTRFS_FEAT_ATTR_PTR(free_space_tree), 327 BTRFS_FEAT_ATTR_PTR(raid1c34), 328 BTRFS_FEAT_ATTR_PTR(block_group_tree), 329 BTRFS_FEAT_ATTR_PTR(simple_quota), 330 #ifdef CONFIG_BLK_DEV_ZONED 331 BTRFS_FEAT_ATTR_PTR(zoned), 332 #endif 333 #ifdef CONFIG_BTRFS_EXPERIMENTAL 334 BTRFS_FEAT_ATTR_PTR(extent_tree_v2), 335 BTRFS_FEAT_ATTR_PTR(raid_stripe_tree), 336 BTRFS_FEAT_ATTR_PTR(remap_tree), 337 #endif 338 #ifdef CONFIG_FS_VERITY 339 BTRFS_FEAT_ATTR_PTR(verity), 340 #endif 341 NULL 342 }; 343 344 static const struct attribute_group btrfs_feature_attr_group = { 345 .name = "features", 346 .is_visible = btrfs_feature_visible, 347 .attrs = btrfs_supported_feature_attrs, 348 }; 349 350 static ssize_t rmdir_subvol_show(struct kobject *kobj, 351 struct kobj_attribute *ka, char *buf) 352 { 353 return sysfs_emit(buf, "0\n"); 354 } 355 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); 356 357 static ssize_t supported_checksums_show(struct kobject *kobj, 358 struct kobj_attribute *a, char *buf) 359 { 360 ssize_t ret = 0; 361 int i; 362 363 for (i = 0; i < btrfs_get_num_csums(); i++) { 364 /* 365 * This "trick" only works as long as 'enum btrfs_csum_type' has 366 * no holes in it 367 */ 368 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "), 369 btrfs_super_csum_name(i)); 370 371 } 372 373 ret += sysfs_emit_at(buf, ret, "\n"); 374 return ret; 375 } 376 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); 377 378 static ssize_t send_stream_version_show(struct kobject *kobj, 379 struct kobj_attribute *ka, char *buf) 380 { 381 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION); 382 } 383 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show); 384 385 static const char *rescue_opts[] = { 386 "usebackuproot", 387 "nologreplay", 388 "ignorebadroots", 389 "ignoredatacsums", 390 "ignoremetacsums", 391 "ignoresuperflags", 392 "all", 393 }; 394 395 static ssize_t supported_rescue_options_show(struct kobject *kobj, 396 struct kobj_attribute *a, 397 char *buf) 398 { 399 ssize_t ret = 0; 400 int i; 401 402 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++) 403 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]); 404 ret += sysfs_emit_at(buf, ret, "\n"); 405 return ret; 406 } 407 BTRFS_ATTR(static_feature, supported_rescue_options, 408 supported_rescue_options_show); 409 410 static ssize_t supported_sectorsizes_show(struct kobject *kobj, 411 struct kobj_attribute *a, 412 char *buf) 413 { 414 ssize_t ret = 0; 415 bool has_output = false; 416 417 for (u32 cur = BTRFS_MIN_BLOCKSIZE; cur <= BTRFS_MAX_BLOCKSIZE; cur *= 2) { 418 if (!btrfs_supported_blocksize(cur)) 419 continue; 420 if (has_output) 421 ret += sysfs_emit_at(buf, ret, " "); 422 ret += sysfs_emit_at(buf, ret, "%u", cur); 423 has_output = true; 424 } 425 ret += sysfs_emit_at(buf, ret, "\n"); 426 return ret; 427 } 428 BTRFS_ATTR(static_feature, supported_sectorsizes, 429 supported_sectorsizes_show); 430 431 static ssize_t acl_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) 432 { 433 return sysfs_emit(buf, "%d\n", IS_ENABLED(CONFIG_BTRFS_FS_POSIX_ACL)); 434 } 435 BTRFS_ATTR(static_feature, acl, acl_show); 436 437 static ssize_t temp_fsid_supported_show(struct kobject *kobj, 438 struct kobj_attribute *a, char *buf) 439 { 440 return sysfs_emit(buf, "0\n"); 441 } 442 BTRFS_ATTR(static_feature, temp_fsid, temp_fsid_supported_show); 443 444 /* 445 * Features which only depend on kernel version. 446 * 447 * These are listed in /sys/fs/btrfs/features along with 448 * btrfs_supported_feature_attrs. 449 */ 450 static struct attribute *btrfs_supported_static_feature_attrs[] = { 451 BTRFS_ATTR_PTR(static_feature, acl), 452 BTRFS_ATTR_PTR(static_feature, rmdir_subvol), 453 BTRFS_ATTR_PTR(static_feature, supported_checksums), 454 BTRFS_ATTR_PTR(static_feature, send_stream_version), 455 BTRFS_ATTR_PTR(static_feature, supported_rescue_options), 456 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes), 457 BTRFS_ATTR_PTR(static_feature, temp_fsid), 458 NULL 459 }; 460 461 static const struct attribute_group btrfs_static_feature_attr_group = { 462 .name = "features", 463 .attrs = btrfs_supported_static_feature_attrs, 464 }; 465 466 /* 467 * Discard statistics and tunables 468 */ 469 #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj)) 470 471 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, 472 struct kobj_attribute *a, 473 char *buf) 474 { 475 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 476 477 return sysfs_emit(buf, "%lld\n", 478 atomic64_read(&fs_info->discard_ctl.discardable_bytes)); 479 } 480 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); 481 482 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, 483 struct kobj_attribute *a, 484 char *buf) 485 { 486 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 487 488 return sysfs_emit(buf, "%d\n", 489 atomic_read(&fs_info->discard_ctl.discardable_extents)); 490 } 491 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); 492 493 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, 494 struct kobj_attribute *a, 495 char *buf) 496 { 497 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 498 499 return sysfs_emit(buf, "%llu\n", 500 fs_info->discard_ctl.discard_bitmap_bytes); 501 } 502 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); 503 504 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, 505 struct kobj_attribute *a, 506 char *buf) 507 { 508 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 509 510 return sysfs_emit(buf, "%lld\n", 511 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); 512 } 513 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); 514 515 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, 516 struct kobj_attribute *a, 517 char *buf) 518 { 519 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 520 521 return sysfs_emit(buf, "%llu\n", 522 fs_info->discard_ctl.discard_extent_bytes); 523 } 524 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); 525 526 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, 527 struct kobj_attribute *a, 528 char *buf) 529 { 530 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 531 532 return sysfs_emit(buf, "%u\n", 533 READ_ONCE(fs_info->discard_ctl.iops_limit)); 534 } 535 536 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, 537 struct kobj_attribute *a, 538 const char *buf, size_t len) 539 { 540 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 541 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 542 u32 iops_limit; 543 int ret; 544 545 ret = kstrtou32(buf, 10, &iops_limit); 546 if (ret) 547 return -EINVAL; 548 549 WRITE_ONCE(discard_ctl->iops_limit, iops_limit); 550 btrfs_discard_calc_delay(discard_ctl); 551 btrfs_discard_schedule_work(discard_ctl, true); 552 return len; 553 } 554 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, 555 btrfs_discard_iops_limit_store); 556 557 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, 558 struct kobj_attribute *a, 559 char *buf) 560 { 561 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 562 563 return sysfs_emit(buf, "%u\n", 564 READ_ONCE(fs_info->discard_ctl.kbps_limit)); 565 } 566 567 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, 568 struct kobj_attribute *a, 569 const char *buf, size_t len) 570 { 571 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 572 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 573 u32 kbps_limit; 574 int ret; 575 576 ret = kstrtou32(buf, 10, &kbps_limit); 577 if (ret) 578 return -EINVAL; 579 580 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); 581 btrfs_discard_schedule_work(discard_ctl, true); 582 return len; 583 } 584 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, 585 btrfs_discard_kbps_limit_store); 586 587 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, 588 struct kobj_attribute *a, 589 char *buf) 590 { 591 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 592 593 return sysfs_emit(buf, "%llu\n", 594 READ_ONCE(fs_info->discard_ctl.max_discard_size)); 595 } 596 597 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, 598 struct kobj_attribute *a, 599 const char *buf, size_t len) 600 { 601 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 602 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 603 u64 max_discard_size; 604 int ret; 605 606 ret = kstrtou64(buf, 10, &max_discard_size); 607 if (ret) 608 return -EINVAL; 609 610 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); 611 612 return len; 613 } 614 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, 615 btrfs_discard_max_discard_size_store); 616 617 /* 618 * Per-filesystem stats for discard (when mounted with discard=async). 619 * 620 * Path: /sys/fs/btrfs/<uuid>/discard/ 621 */ 622 static const struct attribute *discard_attrs[] = { 623 BTRFS_ATTR_PTR(discard, discardable_bytes), 624 BTRFS_ATTR_PTR(discard, discardable_extents), 625 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), 626 BTRFS_ATTR_PTR(discard, discard_bytes_saved), 627 BTRFS_ATTR_PTR(discard, discard_extent_bytes), 628 BTRFS_ATTR_PTR(discard, iops_limit), 629 BTRFS_ATTR_PTR(discard, kbps_limit), 630 BTRFS_ATTR_PTR(discard, max_discard_size), 631 NULL, 632 }; 633 634 #ifdef CONFIG_BTRFS_DEBUG 635 636 /* 637 * Per-filesystem runtime debugging exported via sysfs. 638 * 639 * Path: /sys/fs/btrfs/UUID/debug/ 640 */ 641 static const struct attribute *btrfs_debug_mount_attrs[] = { 642 NULL, 643 }; 644 645 /* 646 * Runtime debugging exported via sysfs, applies to all mounted filesystems. 647 * 648 * Path: /sys/fs/btrfs/debug 649 */ 650 static struct attribute *btrfs_debug_feature_attrs[] = { 651 NULL 652 }; 653 654 static const struct attribute_group btrfs_debug_feature_attr_group = { 655 .name = "debug", 656 .attrs = btrfs_debug_feature_attrs, 657 }; 658 659 #endif 660 661 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) 662 { 663 u64 val; 664 if (lock) 665 spin_lock(lock); 666 val = *value_ptr; 667 if (lock) 668 spin_unlock(lock); 669 return sysfs_emit(buf, "%llu\n", val); 670 } 671 672 static ssize_t global_rsv_size_show(struct kobject *kobj, 673 struct kobj_attribute *ka, char *buf) 674 { 675 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); 676 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 677 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); 678 } 679 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); 680 681 static ssize_t global_rsv_reserved_show(struct kobject *kobj, 682 struct kobj_attribute *a, char *buf) 683 { 684 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); 685 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 686 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); 687 } 688 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); 689 690 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) 691 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) 692 693 static ssize_t raid_bytes_show(struct kobject *kobj, 694 struct kobj_attribute *attr, char *buf); 695 BTRFS_ATTR(raid, total_bytes, raid_bytes_show); 696 BTRFS_ATTR(raid, used_bytes, raid_bytes_show); 697 698 static ssize_t raid_bytes_show(struct kobject *kobj, 699 struct kobj_attribute *attr, char *buf) 700 701 { 702 struct btrfs_space_info *sinfo = to_space_info(kobj->parent); 703 struct btrfs_block_group *block_group; 704 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); 705 u64 val = 0; 706 707 down_read(&sinfo->groups_sem); 708 list_for_each_entry(block_group, &sinfo->block_groups[index], list) { 709 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) 710 val += block_group->length; 711 else 712 val += block_group->used; 713 } 714 up_read(&sinfo->groups_sem); 715 return sysfs_emit(buf, "%llu\n", val); 716 } 717 718 /* 719 * Allocation information about block group profiles. 720 * 721 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/ 722 */ 723 static struct attribute *raid_attrs[] = { 724 BTRFS_ATTR_PTR(raid, total_bytes), 725 BTRFS_ATTR_PTR(raid, used_bytes), 726 NULL 727 }; 728 ATTRIBUTE_GROUPS(raid); 729 730 static void release_raid_kobj(struct kobject *kobj) 731 { 732 kfree(to_raid_kobj(kobj)); 733 } 734 735 static const struct kobj_type btrfs_raid_ktype = { 736 .sysfs_ops = &kobj_sysfs_ops, 737 .release = release_raid_kobj, 738 .default_groups = raid_groups, 739 }; 740 741 #define SPACE_INFO_ATTR(field) \ 742 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ 743 struct kobj_attribute *a, \ 744 char *buf) \ 745 { \ 746 struct btrfs_space_info *sinfo = to_space_info(kobj); \ 747 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ 748 } \ 749 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) 750 751 static ssize_t btrfs_chunk_size_show(struct kobject *kobj, 752 struct kobj_attribute *a, char *buf) 753 { 754 struct btrfs_space_info *sinfo = to_space_info(kobj); 755 756 return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size)); 757 } 758 759 /* 760 * Store new chunk size in space info. Can be called on a read-only filesystem. 761 * 762 * If the new chunk size value is larger than 10% of free space it is reduced 763 * to match that limit. Alignment must be to 256M and the system chunk size 764 * cannot be set. 765 */ 766 static ssize_t btrfs_chunk_size_store(struct kobject *kobj, 767 struct kobj_attribute *a, 768 const char *buf, size_t len) 769 { 770 struct btrfs_space_info *space_info = to_space_info(kobj); 771 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj)); 772 char *retptr; 773 u64 val; 774 775 if (!capable(CAP_SYS_ADMIN)) 776 return -EPERM; 777 778 if (!fs_info->fs_devices) 779 return -EINVAL; 780 781 if (btrfs_is_zoned(fs_info)) 782 return -EINVAL; 783 784 /* System block type must not be changed. */ 785 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM) 786 return -EPERM; 787 788 val = memparse(buf, &retptr); 789 /* There could be trailing '\n', also catch any typos after the value */ 790 retptr = skip_spaces(retptr); 791 if (*retptr != 0 || val == 0) 792 return -EINVAL; 793 794 val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE); 795 796 /* Limit stripe size to 10% of available space. */ 797 val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val); 798 799 /* Must be multiple of 256M. */ 800 val &= ~((u64)SZ_256M - 1); 801 802 /* Must be at least 256M. */ 803 if (val < SZ_256M) 804 return -EINVAL; 805 806 btrfs_update_space_info_chunk_size(space_info, val); 807 808 return len; 809 } 810 811 static ssize_t btrfs_size_classes_show(struct kobject *kobj, 812 struct kobj_attribute *a, char *buf) 813 { 814 struct btrfs_space_info *sinfo = to_space_info(kobj); 815 struct btrfs_block_group *bg; 816 u32 none = 0; 817 u32 small = 0; 818 u32 medium = 0; 819 u32 large = 0; 820 821 for (int i = 0; i < BTRFS_NR_RAID_TYPES; ++i) { 822 down_read(&sinfo->groups_sem); 823 list_for_each_entry(bg, &sinfo->block_groups[i], list) { 824 if (!btrfs_block_group_should_use_size_class(bg)) 825 continue; 826 switch (bg->size_class) { 827 case BTRFS_BG_SZ_NONE: 828 none++; 829 break; 830 case BTRFS_BG_SZ_SMALL: 831 small++; 832 break; 833 case BTRFS_BG_SZ_MEDIUM: 834 medium++; 835 break; 836 case BTRFS_BG_SZ_LARGE: 837 large++; 838 break; 839 } 840 } 841 up_read(&sinfo->groups_sem); 842 } 843 return sysfs_emit(buf, "none %u\n" 844 "small %u\n" 845 "medium %u\n" 846 "large %u\n", 847 none, small, medium, large); 848 } 849 850 #ifdef CONFIG_BTRFS_DEBUG 851 /* 852 * Request chunk allocation with current chunk size. 853 */ 854 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj, 855 struct kobj_attribute *a, 856 const char *buf, size_t len) 857 { 858 struct btrfs_space_info *space_info = to_space_info(kobj); 859 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj)); 860 struct btrfs_trans_handle *trans; 861 bool val; 862 int ret; 863 864 if (!capable(CAP_SYS_ADMIN)) 865 return -EPERM; 866 867 if (sb_rdonly(fs_info->sb)) 868 return -EROFS; 869 870 ret = kstrtobool(buf, &val); 871 if (ret) 872 return ret; 873 874 if (!val) 875 return -EINVAL; 876 877 /* 878 * This is unsafe to be called from sysfs context and may cause 879 * unexpected problems. 880 */ 881 trans = btrfs_start_transaction(fs_info->tree_root, 0); 882 if (IS_ERR(trans)) 883 return PTR_ERR(trans); 884 ret = btrfs_force_chunk_alloc(trans, space_info->flags); 885 btrfs_end_transaction(trans); 886 887 if (ret == 1) 888 return len; 889 890 return -ENOSPC; 891 } 892 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store); 893 894 #endif 895 896 SPACE_INFO_ATTR(flags); 897 SPACE_INFO_ATTR(total_bytes); 898 SPACE_INFO_ATTR(bytes_used); 899 SPACE_INFO_ATTR(bytes_pinned); 900 SPACE_INFO_ATTR(bytes_reserved); 901 SPACE_INFO_ATTR(bytes_may_use); 902 SPACE_INFO_ATTR(bytes_readonly); 903 SPACE_INFO_ATTR(bytes_zone_unusable); 904 SPACE_INFO_ATTR(disk_used); 905 SPACE_INFO_ATTR(disk_total); 906 SPACE_INFO_ATTR(reclaim_count); 907 SPACE_INFO_ATTR(reclaim_bytes); 908 SPACE_INFO_ATTR(reclaim_errors); 909 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store); 910 BTRFS_ATTR(space_info, size_classes, btrfs_size_classes_show); 911 912 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj, 913 struct kobj_attribute *a, 914 char *buf) 915 { 916 struct btrfs_space_info *space_info = to_space_info(kobj); 917 ssize_t ret; 918 919 spin_lock(&space_info->lock); 920 ret = sysfs_emit(buf, "%d\n", btrfs_calc_reclaim_threshold(space_info)); 921 spin_unlock(&space_info->lock); 922 return ret; 923 } 924 925 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj, 926 struct kobj_attribute *a, 927 const char *buf, size_t len) 928 { 929 struct btrfs_space_info *space_info = to_space_info(kobj); 930 int thresh; 931 int ret; 932 933 if (READ_ONCE(space_info->dynamic_reclaim)) 934 return -EINVAL; 935 936 ret = kstrtoint(buf, 10, &thresh); 937 if (ret) 938 return ret; 939 940 if (thresh < 0 || thresh > 100) 941 return -EINVAL; 942 943 WRITE_ONCE(space_info->bg_reclaim_threshold, thresh); 944 945 return len; 946 } 947 948 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold, 949 btrfs_sinfo_bg_reclaim_threshold_show, 950 btrfs_sinfo_bg_reclaim_threshold_store); 951 952 static ssize_t btrfs_sinfo_dynamic_reclaim_show(struct kobject *kobj, 953 struct kobj_attribute *a, 954 char *buf) 955 { 956 struct btrfs_space_info *space_info = to_space_info(kobj); 957 958 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->dynamic_reclaim)); 959 } 960 961 static ssize_t btrfs_sinfo_dynamic_reclaim_store(struct kobject *kobj, 962 struct kobj_attribute *a, 963 const char *buf, size_t len) 964 { 965 struct btrfs_space_info *space_info = to_space_info(kobj); 966 int dynamic_reclaim; 967 int ret; 968 969 ret = kstrtoint(buf, 10, &dynamic_reclaim); 970 if (ret) 971 return ret; 972 973 if (dynamic_reclaim < 0) 974 return -EINVAL; 975 976 WRITE_ONCE(space_info->dynamic_reclaim, dynamic_reclaim != 0); 977 978 return len; 979 } 980 981 BTRFS_ATTR_RW(space_info, dynamic_reclaim, 982 btrfs_sinfo_dynamic_reclaim_show, 983 btrfs_sinfo_dynamic_reclaim_store); 984 985 static ssize_t btrfs_sinfo_periodic_reclaim_show(struct kobject *kobj, 986 struct kobj_attribute *a, 987 char *buf) 988 { 989 struct btrfs_space_info *space_info = to_space_info(kobj); 990 991 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->periodic_reclaim)); 992 } 993 994 static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj, 995 struct kobj_attribute *a, 996 const char *buf, size_t len) 997 { 998 struct btrfs_space_info *space_info = to_space_info(kobj); 999 int periodic_reclaim; 1000 int ret; 1001 1002 ret = kstrtoint(buf, 10, &periodic_reclaim); 1003 if (ret) 1004 return ret; 1005 1006 if (periodic_reclaim < 0) 1007 return -EINVAL; 1008 1009 WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0); 1010 1011 return len; 1012 } 1013 1014 BTRFS_ATTR_RW(space_info, periodic_reclaim, 1015 btrfs_sinfo_periodic_reclaim_show, 1016 btrfs_sinfo_periodic_reclaim_store); 1017 1018 /* 1019 * Allocation information about block group types. 1020 * 1021 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/ 1022 */ 1023 static struct attribute *space_info_attrs[] = { 1024 BTRFS_ATTR_PTR(space_info, flags), 1025 BTRFS_ATTR_PTR(space_info, total_bytes), 1026 BTRFS_ATTR_PTR(space_info, bytes_used), 1027 BTRFS_ATTR_PTR(space_info, bytes_pinned), 1028 BTRFS_ATTR_PTR(space_info, bytes_reserved), 1029 BTRFS_ATTR_PTR(space_info, bytes_may_use), 1030 BTRFS_ATTR_PTR(space_info, bytes_readonly), 1031 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable), 1032 BTRFS_ATTR_PTR(space_info, disk_used), 1033 BTRFS_ATTR_PTR(space_info, disk_total), 1034 BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold), 1035 BTRFS_ATTR_PTR(space_info, dynamic_reclaim), 1036 BTRFS_ATTR_PTR(space_info, chunk_size), 1037 BTRFS_ATTR_PTR(space_info, size_classes), 1038 BTRFS_ATTR_PTR(space_info, reclaim_count), 1039 BTRFS_ATTR_PTR(space_info, reclaim_bytes), 1040 BTRFS_ATTR_PTR(space_info, reclaim_errors), 1041 BTRFS_ATTR_PTR(space_info, periodic_reclaim), 1042 #ifdef CONFIG_BTRFS_DEBUG 1043 BTRFS_ATTR_PTR(space_info, force_chunk_alloc), 1044 #endif 1045 NULL, 1046 }; 1047 ATTRIBUTE_GROUPS(space_info); 1048 1049 static void space_info_release(struct kobject *kobj) 1050 { 1051 struct btrfs_space_info *sinfo = to_space_info(kobj); 1052 kfree(sinfo); 1053 } 1054 1055 static const struct kobj_type space_info_ktype = { 1056 .sysfs_ops = &kobj_sysfs_ops, 1057 .release = space_info_release, 1058 .default_groups = space_info_groups, 1059 }; 1060 1061 /* 1062 * Allocation information about block groups. 1063 * 1064 * Path: /sys/fs/btrfs/<uuid>/allocation/ 1065 */ 1066 static const struct attribute *allocation_attrs[] = { 1067 BTRFS_ATTR_PTR(allocation, global_rsv_reserved), 1068 BTRFS_ATTR_PTR(allocation, global_rsv_size), 1069 NULL, 1070 }; 1071 1072 static ssize_t btrfs_label_show(struct kobject *kobj, 1073 struct kobj_attribute *a, char *buf) 1074 { 1075 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1076 char *label = fs_info->super_copy->label; 1077 ssize_t ret; 1078 1079 spin_lock(&fs_info->super_lock); 1080 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label); 1081 spin_unlock(&fs_info->super_lock); 1082 1083 return ret; 1084 } 1085 1086 static ssize_t btrfs_label_store(struct kobject *kobj, 1087 struct kobj_attribute *a, 1088 const char *buf, size_t len) 1089 { 1090 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1091 size_t p_len; 1092 1093 if (!fs_info) 1094 return -EPERM; 1095 1096 if (sb_rdonly(fs_info->sb)) 1097 return -EROFS; 1098 1099 /* 1100 * p_len is the len until the first occurrence of either 1101 * '\n' or '\0' 1102 */ 1103 p_len = strcspn(buf, "\n"); 1104 1105 if (p_len >= BTRFS_LABEL_SIZE) 1106 return -EINVAL; 1107 1108 spin_lock(&fs_info->super_lock); 1109 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); 1110 memcpy(fs_info->super_copy->label, buf, p_len); 1111 spin_unlock(&fs_info->super_lock); 1112 1113 /* 1114 * We don't want to do full transaction commit from inside sysfs 1115 */ 1116 set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags); 1117 wake_up_process(fs_info->transaction_kthread); 1118 1119 return len; 1120 } 1121 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); 1122 1123 static ssize_t btrfs_nodesize_show(struct kobject *kobj, 1124 struct kobj_attribute *a, char *buf) 1125 { 1126 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1127 1128 return sysfs_emit(buf, "%u\n", fs_info->nodesize); 1129 } 1130 1131 BTRFS_ATTR(, nodesize, btrfs_nodesize_show); 1132 1133 static ssize_t btrfs_sectorsize_show(struct kobject *kobj, 1134 struct kobj_attribute *a, char *buf) 1135 { 1136 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1137 1138 return sysfs_emit(buf, "%u\n", fs_info->sectorsize); 1139 } 1140 1141 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); 1142 1143 static ssize_t btrfs_commit_stats_show(struct kobject *kobj, 1144 struct kobj_attribute *a, char *buf) 1145 { 1146 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1147 u64 now = ktime_get_ns(); 1148 u64 start_time = fs_info->commit_stats.critical_section_start_time; 1149 u64 pending = 0; 1150 1151 if (start_time) 1152 pending = now - start_time; 1153 1154 return sysfs_emit(buf, 1155 "commits %llu\n" 1156 "cur_commit_ms %llu\n" 1157 "last_commit_ms %llu\n" 1158 "max_commit_ms %llu\n" 1159 "total_commit_ms %llu\n", 1160 fs_info->commit_stats.commit_count, 1161 div_u64(pending, NSEC_PER_MSEC), 1162 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC), 1163 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC), 1164 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC)); 1165 } 1166 1167 static ssize_t btrfs_commit_stats_store(struct kobject *kobj, 1168 struct kobj_attribute *a, 1169 const char *buf, size_t len) 1170 { 1171 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1172 unsigned long val; 1173 int ret; 1174 1175 if (!fs_info) 1176 return -EPERM; 1177 1178 if (!capable(CAP_SYS_RESOURCE)) 1179 return -EPERM; 1180 1181 ret = kstrtoul(buf, 10, &val); 1182 if (ret) 1183 return ret; 1184 if (val) 1185 return -EINVAL; 1186 1187 WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0); 1188 1189 return len; 1190 } 1191 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store); 1192 1193 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, 1194 struct kobj_attribute *a, char *buf) 1195 { 1196 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1197 1198 return sysfs_emit(buf, "%u\n", fs_info->sectorsize); 1199 } 1200 1201 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); 1202 1203 static ssize_t quota_override_show(struct kobject *kobj, 1204 struct kobj_attribute *a, char *buf) 1205 { 1206 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1207 int quota_override; 1208 1209 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 1210 return sysfs_emit(buf, "%d\n", quota_override); 1211 } 1212 1213 static ssize_t quota_override_store(struct kobject *kobj, 1214 struct kobj_attribute *a, 1215 const char *buf, size_t len) 1216 { 1217 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1218 unsigned long knob; 1219 int ret; 1220 1221 if (!fs_info) 1222 return -EPERM; 1223 1224 if (!capable(CAP_SYS_RESOURCE)) 1225 return -EPERM; 1226 1227 ret = kstrtoul(buf, 10, &knob); 1228 if (ret) 1229 return ret; 1230 if (knob > 1) 1231 return -EINVAL; 1232 1233 if (knob) 1234 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 1235 else 1236 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 1237 1238 return len; 1239 } 1240 1241 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); 1242 1243 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, 1244 struct kobj_attribute *a, char *buf) 1245 { 1246 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1247 1248 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid); 1249 } 1250 1251 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); 1252 1253 static ssize_t btrfs_checksum_show(struct kobject *kobj, 1254 struct kobj_attribute *a, char *buf) 1255 { 1256 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1257 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); 1258 const char *csum_name = btrfs_super_csum_name(csum_type); 1259 1260 return sysfs_emit(buf, "%s (%s-lib)\n", csum_name, csum_name); 1261 } 1262 1263 BTRFS_ATTR(, checksum, btrfs_checksum_show); 1264 1265 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, 1266 struct kobj_attribute *a, char *buf) 1267 { 1268 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1269 const char *str; 1270 1271 switch (READ_ONCE(fs_info->exclusive_operation)) { 1272 case BTRFS_EXCLOP_NONE: 1273 str = "none\n"; 1274 break; 1275 case BTRFS_EXCLOP_BALANCE: 1276 str = "balance\n"; 1277 break; 1278 case BTRFS_EXCLOP_BALANCE_PAUSED: 1279 str = "balance paused\n"; 1280 break; 1281 case BTRFS_EXCLOP_DEV_ADD: 1282 str = "device add\n"; 1283 break; 1284 case BTRFS_EXCLOP_DEV_REMOVE: 1285 str = "device remove\n"; 1286 break; 1287 case BTRFS_EXCLOP_DEV_REPLACE: 1288 str = "device replace\n"; 1289 break; 1290 case BTRFS_EXCLOP_RESIZE: 1291 str = "resize\n"; 1292 break; 1293 case BTRFS_EXCLOP_SWAP_ACTIVATE: 1294 str = "swap activate\n"; 1295 break; 1296 default: 1297 str = "UNKNOWN\n"; 1298 break; 1299 } 1300 return sysfs_emit(buf, "%s", str); 1301 } 1302 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); 1303 1304 static ssize_t btrfs_generation_show(struct kobject *kobj, 1305 struct kobj_attribute *a, char *buf) 1306 { 1307 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1308 1309 return sysfs_emit(buf, "%llu\n", btrfs_get_fs_generation(fs_info)); 1310 } 1311 BTRFS_ATTR(, generation, btrfs_generation_show); 1312 1313 static ssize_t btrfs_temp_fsid_show(struct kobject *kobj, 1314 struct kobj_attribute *a, char *buf) 1315 { 1316 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1317 1318 return sysfs_emit(buf, "%d\n", fs_info->fs_devices->temp_fsid); 1319 } 1320 BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show); 1321 1322 static const char *btrfs_read_policy_name[] = { 1323 "pid", 1324 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1325 "round-robin", 1326 "devid", 1327 #endif 1328 }; 1329 1330 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1331 1332 /* Global module configuration parameters. */ 1333 static char *read_policy; 1334 char *btrfs_get_mod_read_policy(void) 1335 { 1336 return read_policy; 1337 } 1338 1339 /* Set perms to 0, disable /sys/module/btrfs/parameter/read_policy interface. */ 1340 module_param(read_policy, charp, 0); 1341 MODULE_PARM_DESC(read_policy, 1342 "Global read policy: pid (default), round-robin[:<min_contig_read>], devid[:<devid>]"); 1343 #endif 1344 1345 int btrfs_read_policy_to_enum(const char *str, s64 *value_ret) 1346 { 1347 char param[32]; 1348 char __maybe_unused *value_str; 1349 1350 if (!str || strlen(str) == 0) 1351 return 0; 1352 1353 strscpy(param, str); 1354 1355 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1356 /* Separate value from input in policy:value format. */ 1357 value_str = strchr(param, ':'); 1358 if (value_str) { 1359 char *retptr; 1360 1361 *value_str = 0; 1362 value_str++; 1363 if (!value_ret) 1364 return -EINVAL; 1365 1366 *value_ret = memparse(value_str, &retptr); 1367 /* There could be any trailing typos after the value. */ 1368 retptr = skip_spaces(retptr); 1369 if (*retptr != 0 || *value_ret <= 0) 1370 return -EINVAL; 1371 } 1372 #endif 1373 1374 return sysfs_match_string(btrfs_read_policy_name, param); 1375 } 1376 1377 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1378 int __init btrfs_read_policy_init(void) 1379 { 1380 s64 value; 1381 1382 if (btrfs_read_policy_to_enum(read_policy, &value) == -EINVAL) { 1383 btrfs_err(NULL, "invalid read policy or value %s", read_policy); 1384 return -EINVAL; 1385 } 1386 1387 return 0; 1388 } 1389 #endif 1390 1391 static ssize_t btrfs_read_policy_show(struct kobject *kobj, 1392 struct kobj_attribute *a, char *buf) 1393 { 1394 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); 1395 const enum btrfs_read_policy policy = READ_ONCE(fs_devices->read_policy); 1396 ssize_t ret = 0; 1397 int i; 1398 1399 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { 1400 if (ret != 0) 1401 ret += sysfs_emit_at(buf, ret, " "); 1402 1403 if (i == policy) 1404 ret += sysfs_emit_at(buf, ret, "["); 1405 1406 ret += sysfs_emit_at(buf, ret, "%s", btrfs_read_policy_name[i]); 1407 1408 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1409 if (i == BTRFS_READ_POLICY_RR) 1410 ret += sysfs_emit_at(buf, ret, ":%u", 1411 READ_ONCE(fs_devices->rr_min_contig_read)); 1412 1413 if (i == BTRFS_READ_POLICY_DEVID) 1414 ret += sysfs_emit_at(buf, ret, ":%llu", 1415 READ_ONCE(fs_devices->read_devid)); 1416 #endif 1417 if (i == policy) 1418 ret += sysfs_emit_at(buf, ret, "]"); 1419 } 1420 1421 ret += sysfs_emit_at(buf, ret, "\n"); 1422 1423 return ret; 1424 } 1425 1426 static ssize_t btrfs_read_policy_store(struct kobject *kobj, 1427 struct kobj_attribute *a, 1428 const char *buf, size_t len) 1429 { 1430 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); 1431 int index; 1432 s64 value = -1; 1433 1434 index = btrfs_read_policy_to_enum(buf, &value); 1435 if (index < 0) 1436 return -EINVAL; 1437 1438 #ifdef CONFIG_BTRFS_EXPERIMENTAL 1439 /* If moving from RR then disable collecting fs stats. */ 1440 if (fs_devices->read_policy == BTRFS_READ_POLICY_RR && index != BTRFS_READ_POLICY_RR) 1441 fs_devices->collect_fs_stats = false; 1442 1443 if (index == BTRFS_READ_POLICY_RR) { 1444 if (value != -1) { 1445 const u32 sectorsize = fs_devices->fs_info->sectorsize; 1446 1447 if (!IS_ALIGNED(value, sectorsize)) { 1448 u64 temp_value = round_up(value, sectorsize); 1449 1450 btrfs_debug(fs_devices->fs_info, 1451 "read_policy: min contig read %lld should be multiple of sectorsize %u, rounded to %llu", 1452 value, sectorsize, temp_value); 1453 value = temp_value; 1454 } 1455 } else { 1456 value = BTRFS_DEFAULT_RR_MIN_CONTIG_READ; 1457 } 1458 1459 if (index != READ_ONCE(fs_devices->read_policy) || 1460 value != READ_ONCE(fs_devices->rr_min_contig_read)) { 1461 WRITE_ONCE(fs_devices->read_policy, index); 1462 WRITE_ONCE(fs_devices->rr_min_contig_read, value); 1463 1464 btrfs_info(fs_devices->fs_info, "read policy set to '%s:%lld'", 1465 btrfs_read_policy_name[index], value); 1466 } 1467 1468 fs_devices->collect_fs_stats = true; 1469 1470 return len; 1471 } 1472 1473 if (index == BTRFS_READ_POLICY_DEVID) { 1474 if (value != -1) { 1475 BTRFS_DEV_LOOKUP_ARGS(args); 1476 1477 /* Validate input devid. */ 1478 args.devid = value; 1479 if (btrfs_find_device(fs_devices, &args) == NULL) 1480 return -EINVAL; 1481 } else { 1482 /* Set default devid to the devid of the latest device. */ 1483 value = fs_devices->latest_dev->devid; 1484 } 1485 1486 if (index != READ_ONCE(fs_devices->read_policy) || 1487 value != READ_ONCE(fs_devices->read_devid)) { 1488 WRITE_ONCE(fs_devices->read_policy, index); 1489 WRITE_ONCE(fs_devices->read_devid, value); 1490 1491 btrfs_info(fs_devices->fs_info, "read policy set to '%s:%llu'", 1492 btrfs_read_policy_name[index], value); 1493 } 1494 1495 return len; 1496 } 1497 #endif 1498 if (index != READ_ONCE(fs_devices->read_policy)) { 1499 WRITE_ONCE(fs_devices->read_policy, index); 1500 btrfs_info(fs_devices->fs_info, "read policy set to '%s'", 1501 btrfs_read_policy_name[index]); 1502 } 1503 1504 return len; 1505 } 1506 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store); 1507 1508 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj, 1509 struct kobj_attribute *a, 1510 char *buf) 1511 { 1512 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1513 1514 return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold)); 1515 } 1516 1517 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj, 1518 struct kobj_attribute *a, 1519 const char *buf, size_t len) 1520 { 1521 struct btrfs_fs_info *fs_info = to_fs_info(kobj); 1522 int thresh; 1523 int ret; 1524 1525 ret = kstrtoint(buf, 10, &thresh); 1526 if (ret) 1527 return ret; 1528 1529 #ifdef CONFIG_BTRFS_DEBUG 1530 if (thresh != 0 && (thresh > 100)) 1531 return -EINVAL; 1532 #else 1533 if (thresh != 0 && (thresh <= 50 || thresh > 100)) 1534 return -EINVAL; 1535 #endif 1536 1537 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh); 1538 1539 return len; 1540 } 1541 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show, 1542 btrfs_bg_reclaim_threshold_store); 1543 1544 /* 1545 * Per-filesystem information and stats. 1546 * 1547 * Path: /sys/fs/btrfs/<uuid>/ 1548 */ 1549 static const struct attribute *btrfs_attrs[] = { 1550 BTRFS_ATTR_PTR(, label), 1551 BTRFS_ATTR_PTR(, nodesize), 1552 BTRFS_ATTR_PTR(, sectorsize), 1553 BTRFS_ATTR_PTR(, clone_alignment), 1554 BTRFS_ATTR_PTR(, quota_override), 1555 BTRFS_ATTR_PTR(, metadata_uuid), 1556 BTRFS_ATTR_PTR(, checksum), 1557 BTRFS_ATTR_PTR(, exclusive_operation), 1558 BTRFS_ATTR_PTR(, generation), 1559 BTRFS_ATTR_PTR(, read_policy), 1560 BTRFS_ATTR_PTR(, bg_reclaim_threshold), 1561 BTRFS_ATTR_PTR(, commit_stats), 1562 BTRFS_ATTR_PTR(, temp_fsid), 1563 NULL, 1564 }; 1565 1566 static void btrfs_release_fsid_kobj(struct kobject *kobj) 1567 { 1568 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); 1569 1570 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); 1571 complete(&fs_devs->kobj_unregister); 1572 } 1573 1574 static const struct kobj_type btrfs_ktype = { 1575 .sysfs_ops = &kobj_sysfs_ops, 1576 .release = btrfs_release_fsid_kobj, 1577 }; 1578 1579 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) 1580 { 1581 if (kobj->ktype != &btrfs_ktype) 1582 return NULL; 1583 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); 1584 } 1585 1586 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) 1587 { 1588 if (kobj->ktype != &btrfs_ktype) 1589 return NULL; 1590 return to_fs_devs(kobj)->fs_info; 1591 } 1592 1593 static struct kobject *get_btrfs_kobj(struct kobject *kobj) 1594 { 1595 while (kobj) { 1596 if (kobj->ktype == &btrfs_ktype) 1597 return kobj; 1598 kobj = kobj->parent; 1599 } 1600 return NULL; 1601 } 1602 1603 #define NUM_FEATURE_BITS 64 1604 #define BTRFS_FEATURE_NAME_MAX 13 1605 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; 1606 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; 1607 1608 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) == 1609 ARRAY_SIZE(btrfs_feature_attrs)); 1610 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) == 1611 ARRAY_SIZE(btrfs_feature_attrs[0])); 1612 1613 static const u64 supported_feature_masks[FEAT_MAX] = { 1614 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, 1615 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, 1616 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, 1617 }; 1618 1619 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) 1620 { 1621 int set; 1622 1623 for (set = 0; set < FEAT_MAX; set++) { 1624 int i; 1625 struct attribute *attrs[2]; 1626 struct attribute_group agroup = { 1627 .name = "features", 1628 .attrs = attrs, 1629 }; 1630 u64 features = get_features(fs_info, set); 1631 features &= ~supported_feature_masks[set]; 1632 1633 if (!features) 1634 continue; 1635 1636 attrs[1] = NULL; 1637 for (i = 0; i < NUM_FEATURE_BITS; i++) { 1638 struct btrfs_feature_attr *fa; 1639 1640 if (!(features & (1ULL << i))) 1641 continue; 1642 1643 fa = &btrfs_feature_attrs[set][i]; 1644 attrs[0] = &fa->kobj_attr.attr; 1645 if (add) { 1646 int ret; 1647 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, 1648 &agroup); 1649 if (ret) 1650 return ret; 1651 } else 1652 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, 1653 &agroup); 1654 } 1655 1656 } 1657 return 0; 1658 } 1659 1660 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 1661 { 1662 if (fs_devs->devinfo_kobj) { 1663 kobject_del(fs_devs->devinfo_kobj); 1664 kobject_put(fs_devs->devinfo_kobj); 1665 fs_devs->devinfo_kobj = NULL; 1666 } 1667 1668 if (fs_devs->devices_kobj) { 1669 kobject_del(fs_devs->devices_kobj); 1670 kobject_put(fs_devs->devices_kobj); 1671 fs_devs->devices_kobj = NULL; 1672 } 1673 1674 if (fs_devs->fsid_kobj.state_initialized) { 1675 kobject_del(&fs_devs->fsid_kobj); 1676 kobject_put(&fs_devs->fsid_kobj); 1677 wait_for_completion(&fs_devs->kobj_unregister); 1678 } 1679 } 1680 1681 /* when fs_devs is NULL it will remove all fsid kobject */ 1682 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 1683 { 1684 struct list_head *fs_uuids = btrfs_get_fs_uuids(); 1685 1686 if (fs_devs) { 1687 __btrfs_sysfs_remove_fsid(fs_devs); 1688 return; 1689 } 1690 1691 list_for_each_entry(fs_devs, fs_uuids, fs_list) { 1692 __btrfs_sysfs_remove_fsid(fs_devs); 1693 } 1694 } 1695 1696 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) 1697 { 1698 struct btrfs_device *device; 1699 struct btrfs_fs_devices *seed; 1700 1701 list_for_each_entry(device, &fs_devices->devices, dev_list) 1702 btrfs_sysfs_remove_device(device); 1703 1704 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 1705 list_for_each_entry(device, &seed->devices, dev_list) 1706 btrfs_sysfs_remove_device(device); 1707 } 1708 } 1709 1710 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) 1711 { 1712 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 1713 1714 sysfs_remove_link(fsid_kobj, "bdi"); 1715 1716 if (fs_info->space_info_kobj) { 1717 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); 1718 kobject_del(fs_info->space_info_kobj); 1719 kobject_put(fs_info->space_info_kobj); 1720 } 1721 if (fs_info->discard_kobj) { 1722 sysfs_remove_files(fs_info->discard_kobj, discard_attrs); 1723 kobject_del(fs_info->discard_kobj); 1724 kobject_put(fs_info->discard_kobj); 1725 } 1726 #ifdef CONFIG_BTRFS_DEBUG 1727 if (fs_info->debug_kobj) { 1728 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 1729 kobject_del(fs_info->debug_kobj); 1730 kobject_put(fs_info->debug_kobj); 1731 } 1732 #endif 1733 addrm_unknown_feature_attrs(fs_info, false); 1734 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 1735 sysfs_remove_files(fsid_kobj, btrfs_attrs); 1736 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices); 1737 } 1738 1739 static const char * const btrfs_feature_set_names[FEAT_MAX] = { 1740 [FEAT_COMPAT] = "compat", 1741 [FEAT_COMPAT_RO] = "compat_ro", 1742 [FEAT_INCOMPAT] = "incompat", 1743 }; 1744 1745 const char *btrfs_feature_set_name(enum btrfs_feature_set set) 1746 { 1747 return btrfs_feature_set_names[set]; 1748 } 1749 1750 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) 1751 { 1752 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ 1753 int len = 0; 1754 int i; 1755 char *str; 1756 1757 str = kmalloc(bufsize, GFP_KERNEL); 1758 if (!str) 1759 return str; 1760 1761 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 1762 const char *name; 1763 1764 if (!(flags & (1ULL << i))) 1765 continue; 1766 1767 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; 1768 len += scnprintf(str + len, bufsize - len, "%s%s", 1769 len ? "," : "", name); 1770 } 1771 1772 return str; 1773 } 1774 1775 static void init_feature_attrs(void) 1776 { 1777 struct btrfs_feature_attr *fa; 1778 int set, i; 1779 1780 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); 1781 memset(btrfs_unknown_feature_names, 0, 1782 sizeof(btrfs_unknown_feature_names)); 1783 1784 for (i = 0; btrfs_supported_feature_attrs[i]; i++) { 1785 struct btrfs_feature_attr *sfa; 1786 struct attribute *a = btrfs_supported_feature_attrs[i]; 1787 int bit; 1788 sfa = attr_to_btrfs_feature_attr(a); 1789 bit = ilog2(sfa->feature_bit); 1790 fa = &btrfs_feature_attrs[sfa->feature_set][bit]; 1791 1792 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; 1793 } 1794 1795 for (set = 0; set < FEAT_MAX; set++) { 1796 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 1797 char *name = btrfs_unknown_feature_names[set][i]; 1798 fa = &btrfs_feature_attrs[set][i]; 1799 1800 if (fa->kobj_attr.attr.name) 1801 continue; 1802 1803 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", 1804 btrfs_feature_set_names[set], i); 1805 1806 fa->kobj_attr.attr.name = name; 1807 fa->kobj_attr.attr.mode = S_IRUGO; 1808 fa->feature_set = set; 1809 fa->feature_bit = 1ULL << i; 1810 } 1811 } 1812 } 1813 1814 /* 1815 * Create a sysfs entry for a given block group type at path 1816 * /sys/fs/btrfs/UUID/allocation/data/TYPE 1817 */ 1818 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) 1819 { 1820 struct btrfs_fs_info *fs_info = cache->fs_info; 1821 struct btrfs_space_info *space_info = cache->space_info; 1822 struct raid_kobject *rkobj; 1823 const int index = btrfs_bg_flags_to_raid_index(cache->flags); 1824 unsigned int nofs_flag; 1825 int ret; 1826 1827 /* 1828 * Setup a NOFS context because kobject_add(), deep in its call chain, 1829 * does GFP_KERNEL allocations, and we are often called in a context 1830 * where if reclaim is triggered we can deadlock (we are either holding 1831 * a transaction handle or some lock required for a transaction 1832 * commit). 1833 */ 1834 nofs_flag = memalloc_nofs_save(); 1835 1836 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); 1837 if (!rkobj) { 1838 memalloc_nofs_restore(nofs_flag); 1839 btrfs_warn(cache->fs_info, 1840 "couldn't alloc memory for raid level kobject"); 1841 return; 1842 } 1843 1844 rkobj->flags = cache->flags; 1845 kobject_init(&rkobj->kobj, &btrfs_raid_ktype); 1846 1847 /* 1848 * We call this either on mount, or if we've created a block group for a 1849 * new index type while running (i.e. when restriping). The running 1850 * case is tricky because we could race with other threads, so we need 1851 * to have this check to make sure we didn't already init the kobject. 1852 * 1853 * We don't have to protect on the free side because it only happens on 1854 * unmount. 1855 */ 1856 spin_lock(&space_info->lock); 1857 if (space_info->block_group_kobjs[index]) { 1858 spin_unlock(&space_info->lock); 1859 kobject_put(&rkobj->kobj); 1860 return; 1861 } else { 1862 space_info->block_group_kobjs[index] = &rkobj->kobj; 1863 } 1864 spin_unlock(&space_info->lock); 1865 1866 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", 1867 btrfs_bg_type_to_raid_name(rkobj->flags)); 1868 memalloc_nofs_restore(nofs_flag); 1869 if (ret) { 1870 spin_lock(&space_info->lock); 1871 space_info->block_group_kobjs[index] = NULL; 1872 spin_unlock(&space_info->lock); 1873 kobject_put(&rkobj->kobj); 1874 btrfs_warn(fs_info, 1875 "failed to add kobject for block cache, ignoring"); 1876 return; 1877 } 1878 } 1879 1880 /* 1881 * Remove sysfs directories for all block group types of a given space info and 1882 * the space info as well 1883 */ 1884 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) 1885 { 1886 int i; 1887 1888 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 1889 struct kobject *kobj; 1890 1891 kobj = space_info->block_group_kobjs[i]; 1892 space_info->block_group_kobjs[i] = NULL; 1893 if (kobj) { 1894 kobject_del(kobj); 1895 kobject_put(kobj); 1896 } 1897 } 1898 kobject_del(&space_info->kobj); 1899 kobject_put(&space_info->kobj); 1900 } 1901 1902 static const char *alloc_name(struct btrfs_space_info *space_info) 1903 { 1904 u64 flags = space_info->flags; 1905 1906 switch (flags) { 1907 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: 1908 return "mixed"; 1909 case BTRFS_BLOCK_GROUP_METADATA: 1910 switch (space_info->subgroup_id) { 1911 case BTRFS_SUB_GROUP_PRIMARY: 1912 return "metadata"; 1913 case BTRFS_SUB_GROUP_TREELOG: 1914 return "metadata-treelog"; 1915 default: 1916 WARN_ON_ONCE(1); 1917 return "metadata (unknown sub-group)"; 1918 } 1919 case BTRFS_BLOCK_GROUP_DATA: 1920 switch (space_info->subgroup_id) { 1921 case BTRFS_SUB_GROUP_PRIMARY: 1922 return "data"; 1923 case BTRFS_SUB_GROUP_DATA_RELOC: 1924 return "data-reloc"; 1925 default: 1926 WARN_ON_ONCE(1); 1927 return "data (unknown sub-group)"; 1928 } 1929 case BTRFS_BLOCK_GROUP_SYSTEM: 1930 ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_PRIMARY); 1931 return "system"; 1932 case BTRFS_BLOCK_GROUP_METADATA_REMAP: 1933 return "metadata-remap"; 1934 default: 1935 WARN_ON(1); 1936 return "invalid-combination"; 1937 } 1938 } 1939 1940 /* 1941 * Create a sysfs entry for a space info type at path 1942 * /sys/fs/btrfs/UUID/allocation/TYPE 1943 */ 1944 int btrfs_sysfs_add_space_info_type(struct btrfs_space_info *space_info) 1945 { 1946 int ret; 1947 1948 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, 1949 space_info->fs_info->space_info_kobj, "%s", 1950 alloc_name(space_info)); 1951 if (ret) { 1952 kobject_put(&space_info->kobj); 1953 return ret; 1954 } 1955 1956 return 0; 1957 } 1958 1959 void btrfs_sysfs_remove_device(struct btrfs_device *device) 1960 { 1961 struct kobject *devices_kobj; 1962 1963 /* 1964 * Seed fs_devices devices_kobj aren't used, fetch kobject from the 1965 * fs_info::fs_devices. 1966 */ 1967 devices_kobj = device->fs_info->fs_devices->devices_kobj; 1968 ASSERT(devices_kobj); 1969 1970 if (device->bdev) 1971 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name); 1972 1973 if (device->devid_kobj.state_initialized) { 1974 kobject_del(&device->devid_kobj); 1975 kobject_put(&device->devid_kobj); 1976 wait_for_completion(&device->kobj_unregister); 1977 } 1978 } 1979 1980 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, 1981 struct kobj_attribute *a, 1982 char *buf) 1983 { 1984 int val; 1985 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1986 devid_kobj); 1987 1988 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); 1989 1990 return sysfs_emit(buf, "%d\n", val); 1991 } 1992 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); 1993 1994 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, 1995 struct kobj_attribute *a, char *buf) 1996 { 1997 int val; 1998 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1999 devid_kobj); 2000 2001 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); 2002 2003 return sysfs_emit(buf, "%d\n", val); 2004 } 2005 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); 2006 2007 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, 2008 struct kobj_attribute *a, 2009 char *buf) 2010 { 2011 int val; 2012 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2013 devid_kobj); 2014 2015 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); 2016 2017 return sysfs_emit(buf, "%d\n", val); 2018 } 2019 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); 2020 2021 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj, 2022 struct kobj_attribute *a, 2023 char *buf) 2024 { 2025 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2026 devid_kobj); 2027 2028 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max)); 2029 } 2030 2031 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj, 2032 struct kobj_attribute *a, 2033 const char *buf, size_t len) 2034 { 2035 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2036 devid_kobj); 2037 char *endptr; 2038 unsigned long long limit; 2039 2040 limit = memparse(buf, &endptr); 2041 /* There could be trailing '\n', also catch any typos after the value. */ 2042 endptr = skip_spaces(endptr); 2043 if (*endptr != 0) 2044 return -EINVAL; 2045 WRITE_ONCE(device->scrub_speed_max, limit); 2046 return len; 2047 } 2048 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show, 2049 btrfs_devinfo_scrub_speed_max_store); 2050 2051 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, 2052 struct kobj_attribute *a, char *buf) 2053 { 2054 int val; 2055 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2056 devid_kobj); 2057 2058 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); 2059 2060 return sysfs_emit(buf, "%d\n", val); 2061 } 2062 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); 2063 2064 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj, 2065 struct kobj_attribute *a, char *buf) 2066 { 2067 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2068 devid_kobj); 2069 2070 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid); 2071 } 2072 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show); 2073 2074 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj, 2075 struct kobj_attribute *a, char *buf) 2076 { 2077 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2078 devid_kobj); 2079 2080 if (!device->dev_stats_valid) 2081 return sysfs_emit(buf, "invalid\n"); 2082 2083 /* 2084 * Print all at once so we get a snapshot of all values from the same 2085 * time. Keep them in sync and in order of definition of 2086 * btrfs_dev_stat_values. 2087 */ 2088 return sysfs_emit(buf, 2089 "write_errs %d\n" 2090 "read_errs %d\n" 2091 "flush_errs %d\n" 2092 "corruption_errs %d\n" 2093 "generation_errs %d\n", 2094 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS), 2095 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS), 2096 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS), 2097 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS), 2098 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS)); 2099 } 2100 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show); 2101 2102 /* 2103 * Information about one device. 2104 * 2105 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/ 2106 */ 2107 static struct attribute *devid_attrs[] = { 2108 BTRFS_ATTR_PTR(devid, error_stats), 2109 BTRFS_ATTR_PTR(devid, fsid), 2110 BTRFS_ATTR_PTR(devid, in_fs_metadata), 2111 BTRFS_ATTR_PTR(devid, missing), 2112 BTRFS_ATTR_PTR(devid, replace_target), 2113 BTRFS_ATTR_PTR(devid, scrub_speed_max), 2114 BTRFS_ATTR_PTR(devid, writeable), 2115 NULL 2116 }; 2117 ATTRIBUTE_GROUPS(devid); 2118 2119 static void btrfs_release_devid_kobj(struct kobject *kobj) 2120 { 2121 struct btrfs_device *device = container_of(kobj, struct btrfs_device, 2122 devid_kobj); 2123 2124 memset(&device->devid_kobj, 0, sizeof(struct kobject)); 2125 complete(&device->kobj_unregister); 2126 } 2127 2128 static const struct kobj_type devid_ktype = { 2129 .sysfs_ops = &kobj_sysfs_ops, 2130 .default_groups = devid_groups, 2131 .release = btrfs_release_devid_kobj, 2132 }; 2133 2134 int btrfs_sysfs_add_device(struct btrfs_device *device) 2135 { 2136 int ret; 2137 unsigned int nofs_flag; 2138 struct kobject *devices_kobj; 2139 struct kobject *devinfo_kobj; 2140 2141 /* 2142 * Make sure we use the fs_info::fs_devices to fetch the kobjects even 2143 * for the seed fs_devices 2144 */ 2145 devices_kobj = device->fs_info->fs_devices->devices_kobj; 2146 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; 2147 ASSERT(devices_kobj); 2148 ASSERT(devinfo_kobj); 2149 2150 nofs_flag = memalloc_nofs_save(); 2151 2152 if (device->bdev) { 2153 struct kobject *disk_kobj = bdev_kobj(device->bdev); 2154 2155 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name); 2156 if (ret) { 2157 btrfs_warn(device->fs_info, 2158 "creating sysfs device link for devid %llu failed: %d", 2159 device->devid, ret); 2160 goto out; 2161 } 2162 } 2163 2164 init_completion(&device->kobj_unregister); 2165 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype, 2166 devinfo_kobj, "%llu", device->devid); 2167 if (ret) { 2168 kobject_put(&device->devid_kobj); 2169 btrfs_warn(device->fs_info, 2170 "devinfo init for devid %llu failed: %d", 2171 device->devid, ret); 2172 } 2173 2174 out: 2175 memalloc_nofs_restore(nofs_flag); 2176 return ret; 2177 } 2178 2179 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) 2180 { 2181 int ret; 2182 struct btrfs_device *device; 2183 struct btrfs_fs_devices *seed; 2184 2185 list_for_each_entry(device, &fs_devices->devices, dev_list) { 2186 ret = btrfs_sysfs_add_device(device); 2187 if (ret) 2188 goto fail; 2189 } 2190 2191 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 2192 list_for_each_entry(device, &seed->devices, dev_list) { 2193 ret = btrfs_sysfs_add_device(device); 2194 if (ret) 2195 goto fail; 2196 } 2197 } 2198 2199 return 0; 2200 2201 fail: 2202 btrfs_sysfs_remove_fs_devices(fs_devices); 2203 return ret; 2204 } 2205 2206 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) 2207 { 2208 int ret; 2209 2210 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); 2211 if (ret) 2212 btrfs_warn(NULL, "sending event %d to kobject: '%s' (%p): failed", 2213 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), 2214 &disk_to_dev(bdev->bd_disk)->kobj); 2215 } 2216 2217 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) 2218 2219 { 2220 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 2221 2222 /* 2223 * Sprouting changes fsid of the mounted filesystem, rename the fsid 2224 * directory 2225 */ 2226 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); 2227 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) 2228 btrfs_warn(fs_devices->fs_info, 2229 "sysfs: failed to create fsid for sprout"); 2230 } 2231 2232 void btrfs_sysfs_update_devid(struct btrfs_device *device) 2233 { 2234 char tmp[24]; 2235 2236 snprintf(tmp, sizeof(tmp), "%llu", device->devid); 2237 2238 if (kobject_rename(&device->devid_kobj, tmp)) 2239 btrfs_warn(device->fs_devices->fs_info, 2240 "sysfs: failed to update devid for %llu", 2241 device->devid); 2242 } 2243 2244 /* /sys/fs/btrfs/ entry */ 2245 static struct kset *btrfs_kset; 2246 2247 /* 2248 * Creates: 2249 * /sys/fs/btrfs/UUID 2250 * 2251 * Can be called by the device discovery thread. 2252 */ 2253 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) 2254 { 2255 int ret; 2256 2257 init_completion(&fs_devs->kobj_unregister); 2258 fs_devs->fsid_kobj.kset = btrfs_kset; 2259 ret = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, 2260 "%pU", fs_devs->fsid); 2261 if (ret) { 2262 kobject_put(&fs_devs->fsid_kobj); 2263 return ret; 2264 } 2265 2266 fs_devs->devices_kobj = kobject_create_and_add("devices", 2267 &fs_devs->fsid_kobj); 2268 if (!fs_devs->devices_kobj) { 2269 btrfs_err(fs_devs->fs_info, 2270 "failed to init sysfs device interface"); 2271 btrfs_sysfs_remove_fsid(fs_devs); 2272 return -ENOMEM; 2273 } 2274 2275 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", 2276 &fs_devs->fsid_kobj); 2277 if (!fs_devs->devinfo_kobj) { 2278 btrfs_err(fs_devs->fs_info, 2279 "failed to init sysfs devinfo kobject"); 2280 btrfs_sysfs_remove_fsid(fs_devs); 2281 return -ENOMEM; 2282 } 2283 2284 return 0; 2285 } 2286 2287 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) 2288 { 2289 int ret; 2290 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 2291 struct kobject *fsid_kobj = &fs_devs->fsid_kobj; 2292 2293 ret = btrfs_sysfs_add_fs_devices(fs_devs); 2294 if (ret) 2295 return ret; 2296 2297 ret = sysfs_create_files(fsid_kobj, btrfs_attrs); 2298 if (ret) { 2299 btrfs_sysfs_remove_fs_devices(fs_devs); 2300 return ret; 2301 } 2302 2303 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); 2304 if (ret) 2305 goto failure; 2306 2307 #ifdef CONFIG_BTRFS_DEBUG 2308 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); 2309 if (!fs_info->debug_kobj) { 2310 ret = -ENOMEM; 2311 goto failure; 2312 } 2313 2314 ret = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 2315 if (ret) 2316 goto failure; 2317 #endif 2318 2319 /* Discard directory */ 2320 fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj); 2321 if (!fs_info->discard_kobj) { 2322 ret = -ENOMEM; 2323 goto failure; 2324 } 2325 2326 ret = sysfs_create_files(fs_info->discard_kobj, discard_attrs); 2327 if (ret) 2328 goto failure; 2329 2330 ret = addrm_unknown_feature_attrs(fs_info, true); 2331 if (ret) 2332 goto failure; 2333 2334 ret = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); 2335 if (ret) 2336 goto failure; 2337 2338 fs_info->space_info_kobj = kobject_create_and_add("allocation", 2339 fsid_kobj); 2340 if (!fs_info->space_info_kobj) { 2341 ret = -ENOMEM; 2342 goto failure; 2343 } 2344 2345 ret = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); 2346 if (ret) 2347 goto failure; 2348 2349 return 0; 2350 failure: 2351 btrfs_sysfs_remove_mounted(fs_info); 2352 return ret; 2353 } 2354 2355 static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj, 2356 struct kobj_attribute *a, 2357 char *buf) 2358 { 2359 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent); 2360 bool enabled; 2361 2362 spin_lock(&fs_info->qgroup_lock); 2363 enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON; 2364 spin_unlock(&fs_info->qgroup_lock); 2365 2366 return sysfs_emit(buf, "%d\n", enabled); 2367 } 2368 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show); 2369 2370 static ssize_t qgroup_mode_show(struct kobject *qgroups_kobj, 2371 struct kobj_attribute *a, 2372 char *buf) 2373 { 2374 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent); 2375 ssize_t ret = 0; 2376 2377 spin_lock(&fs_info->qgroup_lock); 2378 ASSERT(btrfs_qgroup_enabled(fs_info)); 2379 switch (btrfs_qgroup_mode(fs_info)) { 2380 case BTRFS_QGROUP_MODE_FULL: 2381 ret = sysfs_emit(buf, "qgroup\n"); 2382 break; 2383 case BTRFS_QGROUP_MODE_SIMPLE: 2384 ret = sysfs_emit(buf, "squota\n"); 2385 break; 2386 default: 2387 btrfs_warn(fs_info, "unexpected qgroup mode %d\n", 2388 btrfs_qgroup_mode(fs_info)); 2389 break; 2390 } 2391 spin_unlock(&fs_info->qgroup_lock); 2392 2393 return ret; 2394 } 2395 BTRFS_ATTR(qgroups, mode, qgroup_mode_show); 2396 2397 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj, 2398 struct kobj_attribute *a, 2399 char *buf) 2400 { 2401 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent); 2402 bool inconsistent; 2403 2404 spin_lock(&fs_info->qgroup_lock); 2405 inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT); 2406 spin_unlock(&fs_info->qgroup_lock); 2407 2408 return sysfs_emit(buf, "%d\n", inconsistent); 2409 } 2410 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show); 2411 2412 static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj, 2413 struct kobj_attribute *a, 2414 char *buf) 2415 { 2416 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent); 2417 u8 result; 2418 2419 spin_lock(&fs_info->qgroup_lock); 2420 result = fs_info->qgroup_drop_subtree_thres; 2421 spin_unlock(&fs_info->qgroup_lock); 2422 2423 return sysfs_emit(buf, "%d\n", result); 2424 } 2425 2426 static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj, 2427 struct kobj_attribute *a, 2428 const char *buf, size_t len) 2429 { 2430 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent); 2431 u8 new_thres; 2432 int ret; 2433 2434 ret = kstrtou8(buf, 10, &new_thres); 2435 if (ret) 2436 return -EINVAL; 2437 2438 if (new_thres > BTRFS_MAX_LEVEL) 2439 return -EINVAL; 2440 2441 spin_lock(&fs_info->qgroup_lock); 2442 fs_info->qgroup_drop_subtree_thres = new_thres; 2443 spin_unlock(&fs_info->qgroup_lock); 2444 2445 return len; 2446 } 2447 BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show, 2448 qgroup_drop_subtree_thres_store); 2449 2450 /* 2451 * Qgroups global info 2452 * 2453 * Path: /sys/fs/btrfs/<uuid>/qgroups/ 2454 */ 2455 static struct attribute *qgroups_attrs[] = { 2456 BTRFS_ATTR_PTR(qgroups, enabled), 2457 BTRFS_ATTR_PTR(qgroups, inconsistent), 2458 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold), 2459 BTRFS_ATTR_PTR(qgroups, mode), 2460 NULL 2461 }; 2462 ATTRIBUTE_GROUPS(qgroups); 2463 2464 static void qgroups_release(struct kobject *kobj) 2465 { 2466 kfree(kobj); 2467 } 2468 2469 static const struct kobj_type qgroups_ktype = { 2470 .sysfs_ops = &kobj_sysfs_ops, 2471 .default_groups = qgroups_groups, 2472 .release = qgroups_release, 2473 }; 2474 2475 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) 2476 { 2477 return to_fs_info(kobj->parent->parent); 2478 } 2479 2480 #define QGROUP_ATTR(_member, _show_name) \ 2481 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ 2482 struct kobj_attribute *a, \ 2483 char *buf) \ 2484 { \ 2485 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 2486 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 2487 struct btrfs_qgroup, kobj); \ 2488 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ 2489 } \ 2490 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) 2491 2492 #define QGROUP_RSV_ATTR(_name, _type) \ 2493 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ 2494 struct kobj_attribute *a, \ 2495 char *buf) \ 2496 { \ 2497 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 2498 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 2499 struct btrfs_qgroup, kobj); \ 2500 return btrfs_show_u64(&qgroup->rsv.values[_type], \ 2501 &fs_info->qgroup_lock, buf); \ 2502 } \ 2503 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) 2504 2505 QGROUP_ATTR(rfer, referenced); 2506 QGROUP_ATTR(excl, exclusive); 2507 QGROUP_ATTR(max_rfer, max_referenced); 2508 QGROUP_ATTR(max_excl, max_exclusive); 2509 QGROUP_ATTR(lim_flags, limit_flags); 2510 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); 2511 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); 2512 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); 2513 2514 /* 2515 * Qgroup information. 2516 * 2517 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/ 2518 */ 2519 static struct attribute *qgroup_attrs[] = { 2520 BTRFS_ATTR_PTR(qgroup, referenced), 2521 BTRFS_ATTR_PTR(qgroup, exclusive), 2522 BTRFS_ATTR_PTR(qgroup, max_referenced), 2523 BTRFS_ATTR_PTR(qgroup, max_exclusive), 2524 BTRFS_ATTR_PTR(qgroup, limit_flags), 2525 BTRFS_ATTR_PTR(qgroup, rsv_data), 2526 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), 2527 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), 2528 NULL 2529 }; 2530 ATTRIBUTE_GROUPS(qgroup); 2531 2532 static void qgroup_release(struct kobject *kobj) 2533 { 2534 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); 2535 2536 memset(&qgroup->kobj, 0, sizeof(*kobj)); 2537 } 2538 2539 static const struct kobj_type qgroup_ktype = { 2540 .sysfs_ops = &kobj_sysfs_ops, 2541 .release = qgroup_release, 2542 .default_groups = qgroup_groups, 2543 }; 2544 2545 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, 2546 struct btrfs_qgroup *qgroup) 2547 { 2548 struct kobject *qgroups_kobj = fs_info->qgroups_kobj; 2549 int ret; 2550 2551 if (btrfs_is_testing(fs_info)) 2552 return 0; 2553 if (qgroup->kobj.state_initialized) 2554 return 0; 2555 if (!qgroups_kobj) 2556 return -EINVAL; 2557 2558 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, 2559 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), 2560 btrfs_qgroup_subvolid(qgroup->qgroupid)); 2561 if (ret < 0) 2562 kobject_put(&qgroup->kobj); 2563 2564 return ret; 2565 } 2566 2567 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) 2568 { 2569 struct btrfs_qgroup *qgroup; 2570 struct btrfs_qgroup *next; 2571 2572 if (btrfs_is_testing(fs_info)) 2573 return; 2574 2575 rbtree_postorder_for_each_entry_safe(qgroup, next, 2576 &fs_info->qgroup_tree, node) 2577 btrfs_sysfs_del_one_qgroup(fs_info, qgroup); 2578 if (fs_info->qgroups_kobj) { 2579 kobject_del(fs_info->qgroups_kobj); 2580 kobject_put(fs_info->qgroups_kobj); 2581 fs_info->qgroups_kobj = NULL; 2582 } 2583 } 2584 2585 /* Called when qgroups get initialized, thus there is no need for locking */ 2586 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) 2587 { 2588 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 2589 struct btrfs_qgroup *qgroup; 2590 struct btrfs_qgroup *next; 2591 int ret = 0; 2592 2593 if (btrfs_is_testing(fs_info)) 2594 return 0; 2595 2596 ASSERT(fsid_kobj); 2597 if (fs_info->qgroups_kobj) 2598 return 0; 2599 2600 fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); 2601 if (!fs_info->qgroups_kobj) 2602 return -ENOMEM; 2603 2604 ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype, 2605 fsid_kobj, "qgroups"); 2606 if (ret < 0) 2607 goto out; 2608 2609 rbtree_postorder_for_each_entry_safe(qgroup, next, 2610 &fs_info->qgroup_tree, node) { 2611 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); 2612 if (ret < 0) 2613 goto out; 2614 } 2615 2616 out: 2617 if (ret < 0) 2618 btrfs_sysfs_del_qgroups(fs_info); 2619 return ret; 2620 } 2621 2622 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 2623 struct btrfs_qgroup *qgroup) 2624 { 2625 if (btrfs_is_testing(fs_info)) 2626 return; 2627 2628 if (qgroup->kobj.state_initialized) { 2629 kobject_del(&qgroup->kobj); 2630 kobject_put(&qgroup->kobj); 2631 } 2632 } 2633 2634 /* 2635 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current 2636 * values in superblock. Call after any changes to incompat/compat_ro flags 2637 */ 2638 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info) 2639 { 2640 struct kobject *fsid_kobj; 2641 int ret; 2642 2643 if (!fs_info) 2644 return; 2645 2646 fsid_kobj = &fs_info->fs_devices->fsid_kobj; 2647 if (!fsid_kobj->state_initialized) 2648 return; 2649 2650 ret = sysfs_update_group(fsid_kobj, &btrfs_feature_attr_group); 2651 if (ret < 0) 2652 btrfs_warn(fs_info, 2653 "failed to update /sys/fs/btrfs/%pU/features: %d", 2654 fs_info->fs_devices->fsid, ret); 2655 } 2656 2657 int __init btrfs_init_sysfs(void) 2658 { 2659 int ret; 2660 2661 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); 2662 if (!btrfs_kset) 2663 return -ENOMEM; 2664 2665 init_feature_attrs(); 2666 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2667 if (ret) 2668 goto out2; 2669 ret = sysfs_merge_group(&btrfs_kset->kobj, 2670 &btrfs_static_feature_attr_group); 2671 if (ret) 2672 goto out_remove_group; 2673 2674 #ifdef CONFIG_BTRFS_DEBUG 2675 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 2676 if (ret) { 2677 sysfs_unmerge_group(&btrfs_kset->kobj, 2678 &btrfs_static_feature_attr_group); 2679 goto out_remove_group; 2680 } 2681 #endif 2682 2683 return 0; 2684 2685 out_remove_group: 2686 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2687 out2: 2688 kset_unregister(btrfs_kset); 2689 2690 return ret; 2691 } 2692 2693 void __cold btrfs_exit_sysfs(void) 2694 { 2695 sysfs_unmerge_group(&btrfs_kset->kobj, 2696 &btrfs_static_feature_attr_group); 2697 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 2698 #ifdef CONFIG_BTRFS_DEBUG 2699 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 2700 #endif 2701 kset_unregister(btrfs_kset); 2702 } 2703