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