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