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