1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/sched/mm.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/completion.h>
11 #include <linux/bug.h>
12 #include <crypto/hash.h>
18 #include "transaction.h"
21 #include "space-info.h"
22 #include "block-group.h"
25 struct btrfs_feature_attr {
26 struct kobj_attribute kobj_attr;
27 enum btrfs_feature_set feature_set;
31 /* For raid type sysfs entries */
37 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
39 .attr = { .name = __stringify(_name), .mode = _mode }, \
44 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
45 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
46 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
48 #define BTRFS_ATTR(_prefix, _name, _show) \
49 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
50 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
52 #define BTRFS_ATTR_PTR(_prefix, _name) \
53 (&btrfs_attr_##_prefix##_##_name.attr)
55 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
56 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
57 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
58 btrfs_feature_attr_show, \
59 btrfs_feature_attr_store), \
60 .feature_set = _feature_set, \
61 .feature_bit = _feature_prefix ##_## _feature_bit, \
63 #define BTRFS_FEAT_ATTR_PTR(_name) \
64 (&btrfs_attr_features_##_name.kobj_attr.attr)
66 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
68 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
69 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
70 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
71 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
73 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
74 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
76 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
78 return container_of(a, struct btrfs_feature_attr, kobj_attr);
81 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
83 return container_of(attr, struct kobj_attribute, attr);
86 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
87 struct attribute *attr)
89 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
92 static u64 get_features(struct btrfs_fs_info *fs_info,
93 enum btrfs_feature_set set)
95 struct btrfs_super_block *disk_super = fs_info->super_copy;
96 if (set == FEAT_COMPAT)
97 return btrfs_super_compat_flags(disk_super);
98 else if (set == FEAT_COMPAT_RO)
99 return btrfs_super_compat_ro_flags(disk_super);
101 return btrfs_super_incompat_flags(disk_super);
104 static void set_features(struct btrfs_fs_info *fs_info,
105 enum btrfs_feature_set set, u64 features)
107 struct btrfs_super_block *disk_super = fs_info->super_copy;
108 if (set == FEAT_COMPAT)
109 btrfs_set_super_compat_flags(disk_super, features);
110 else if (set == FEAT_COMPAT_RO)
111 btrfs_set_super_compat_ro_flags(disk_super, features);
113 btrfs_set_super_incompat_flags(disk_super, features);
116 static int can_modify_feature(struct btrfs_feature_attr *fa)
120 switch (fa->feature_set) {
122 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
123 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
126 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
127 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
130 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
131 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
134 pr_warn("btrfs: sysfs: unknown feature set %d\n",
139 if (set & fa->feature_bit)
141 if (clear & fa->feature_bit)
147 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
148 struct kobj_attribute *a, char *buf)
151 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
152 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
154 u64 features = get_features(fs_info, fa->feature_set);
155 if (features & fa->feature_bit)
158 val = can_modify_feature(fa);
160 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
163 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
164 struct kobj_attribute *a,
165 const char *buf, size_t count)
167 struct btrfs_fs_info *fs_info;
168 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
169 u64 features, set, clear;
173 fs_info = to_fs_info(kobj);
177 if (sb_rdonly(fs_info->sb))
180 ret = kstrtoul(skip_spaces(buf), 0, &val);
184 if (fa->feature_set == FEAT_COMPAT) {
185 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
186 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
187 } else if (fa->feature_set == FEAT_COMPAT_RO) {
188 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
189 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
191 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
192 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
195 features = get_features(fs_info, fa->feature_set);
198 if ((val && (features & fa->feature_bit)) ||
199 (!val && !(features & fa->feature_bit)))
202 if ((val && !(set & fa->feature_bit)) ||
203 (!val && !(clear & fa->feature_bit))) {
205 "%sabling feature %s on mounted fs is not supported.",
206 val ? "En" : "Dis", fa->kobj_attr.attr.name);
210 btrfs_info(fs_info, "%s %s feature flag",
211 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
213 spin_lock(&fs_info->super_lock);
214 features = get_features(fs_info, fa->feature_set);
216 features |= fa->feature_bit;
218 features &= ~fa->feature_bit;
219 set_features(fs_info, fa->feature_set, features);
220 spin_unlock(&fs_info->super_lock);
223 * We don't want to do full transaction commit from inside sysfs
225 btrfs_set_pending(fs_info, COMMIT);
226 wake_up_process(fs_info->transaction_kthread);
231 static umode_t btrfs_feature_visible(struct kobject *kobj,
232 struct attribute *attr, int unused)
234 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
235 umode_t mode = attr->mode;
238 struct btrfs_feature_attr *fa;
241 fa = attr_to_btrfs_feature_attr(attr);
242 features = get_features(fs_info, fa->feature_set);
244 if (can_modify_feature(fa))
246 else if (!(features & fa->feature_bit))
253 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
254 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
255 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
256 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
257 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
258 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
259 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
260 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
261 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
262 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
263 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
264 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
265 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
266 /* Remove once support for zoned allocation is feature complete */
267 #ifdef CONFIG_BTRFS_DEBUG
268 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
271 static struct attribute *btrfs_supported_feature_attrs[] = {
272 BTRFS_FEAT_ATTR_PTR(mixed_backref),
273 BTRFS_FEAT_ATTR_PTR(default_subvol),
274 BTRFS_FEAT_ATTR_PTR(mixed_groups),
275 BTRFS_FEAT_ATTR_PTR(compress_lzo),
276 BTRFS_FEAT_ATTR_PTR(compress_zstd),
277 BTRFS_FEAT_ATTR_PTR(big_metadata),
278 BTRFS_FEAT_ATTR_PTR(extended_iref),
279 BTRFS_FEAT_ATTR_PTR(raid56),
280 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
281 BTRFS_FEAT_ATTR_PTR(no_holes),
282 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
283 BTRFS_FEAT_ATTR_PTR(free_space_tree),
284 BTRFS_FEAT_ATTR_PTR(raid1c34),
285 #ifdef CONFIG_BTRFS_DEBUG
286 BTRFS_FEAT_ATTR_PTR(zoned),
292 * Features which depend on feature bits and may differ between each fs.
294 * /sys/fs/btrfs/features lists all available features of this kernel while
295 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
296 * can be changed online.
298 static const struct attribute_group btrfs_feature_attr_group = {
300 .is_visible = btrfs_feature_visible,
301 .attrs = btrfs_supported_feature_attrs,
304 static ssize_t rmdir_subvol_show(struct kobject *kobj,
305 struct kobj_attribute *ka, char *buf)
307 return scnprintf(buf, PAGE_SIZE, "0\n");
309 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
311 static ssize_t supported_checksums_show(struct kobject *kobj,
312 struct kobj_attribute *a, char *buf)
317 for (i = 0; i < btrfs_get_num_csums(); i++) {
319 * This "trick" only works as long as 'enum btrfs_csum_type' has
322 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
323 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
327 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
330 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
332 static ssize_t send_stream_version_show(struct kobject *kobj,
333 struct kobj_attribute *ka, char *buf)
335 return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
337 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
339 static const char *rescue_opts[] = {
347 static ssize_t supported_rescue_options_show(struct kobject *kobj,
348 struct kobj_attribute *a,
354 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
355 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
356 (i ? " " : ""), rescue_opts[i]);
357 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
360 BTRFS_ATTR(static_feature, supported_rescue_options,
361 supported_rescue_options_show);
363 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
364 struct kobj_attribute *a,
369 /* Only sectorsize == PAGE_SIZE is now supported */
370 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%lu\n", PAGE_SIZE);
374 BTRFS_ATTR(static_feature, supported_sectorsizes,
375 supported_sectorsizes_show);
377 static struct attribute *btrfs_supported_static_feature_attrs[] = {
378 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
379 BTRFS_ATTR_PTR(static_feature, supported_checksums),
380 BTRFS_ATTR_PTR(static_feature, send_stream_version),
381 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
382 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
387 * Features which only depend on kernel version.
389 * These are listed in /sys/fs/btrfs/features along with
390 * btrfs_feature_attr_group
392 static const struct attribute_group btrfs_static_feature_attr_group = {
394 .attrs = btrfs_supported_static_feature_attrs,
397 #ifdef CONFIG_BTRFS_DEBUG
400 * Discard statistics and tunables
402 #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
404 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
405 struct kobj_attribute *a,
408 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
410 return scnprintf(buf, PAGE_SIZE, "%lld\n",
411 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
413 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
415 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
416 struct kobj_attribute *a,
419 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
421 return scnprintf(buf, PAGE_SIZE, "%d\n",
422 atomic_read(&fs_info->discard_ctl.discardable_extents));
424 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
426 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
427 struct kobj_attribute *a,
430 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
432 return scnprintf(buf, PAGE_SIZE, "%llu\n",
433 fs_info->discard_ctl.discard_bitmap_bytes);
435 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
437 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
438 struct kobj_attribute *a,
441 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
443 return scnprintf(buf, PAGE_SIZE, "%lld\n",
444 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
446 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
448 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
449 struct kobj_attribute *a,
452 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
454 return scnprintf(buf, PAGE_SIZE, "%llu\n",
455 fs_info->discard_ctl.discard_extent_bytes);
457 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
459 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
460 struct kobj_attribute *a,
463 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
465 return scnprintf(buf, PAGE_SIZE, "%u\n",
466 READ_ONCE(fs_info->discard_ctl.iops_limit));
469 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
470 struct kobj_attribute *a,
471 const char *buf, size_t len)
473 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
474 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
478 ret = kstrtou32(buf, 10, &iops_limit);
482 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
483 btrfs_discard_calc_delay(discard_ctl);
484 btrfs_discard_schedule_work(discard_ctl, true);
487 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
488 btrfs_discard_iops_limit_store);
490 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
491 struct kobj_attribute *a,
494 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
496 return scnprintf(buf, PAGE_SIZE, "%u\n",
497 READ_ONCE(fs_info->discard_ctl.kbps_limit));
500 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
501 struct kobj_attribute *a,
502 const char *buf, size_t len)
504 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
505 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
509 ret = kstrtou32(buf, 10, &kbps_limit);
513 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
514 btrfs_discard_schedule_work(discard_ctl, true);
517 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
518 btrfs_discard_kbps_limit_store);
520 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
521 struct kobj_attribute *a,
524 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
526 return scnprintf(buf, PAGE_SIZE, "%llu\n",
527 READ_ONCE(fs_info->discard_ctl.max_discard_size));
530 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
531 struct kobj_attribute *a,
532 const char *buf, size_t len)
534 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
535 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
536 u64 max_discard_size;
539 ret = kstrtou64(buf, 10, &max_discard_size);
543 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
547 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
548 btrfs_discard_max_discard_size_store);
550 static const struct attribute *discard_debug_attrs[] = {
551 BTRFS_ATTR_PTR(discard, discardable_bytes),
552 BTRFS_ATTR_PTR(discard, discardable_extents),
553 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
554 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
555 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
556 BTRFS_ATTR_PTR(discard, iops_limit),
557 BTRFS_ATTR_PTR(discard, kbps_limit),
558 BTRFS_ATTR_PTR(discard, max_discard_size),
563 * Runtime debugging exported via sysfs
565 * /sys/fs/btrfs/debug - applies to module or all filesystems
566 * /sys/fs/btrfs/UUID - applies only to the given filesystem
568 static const struct attribute *btrfs_debug_mount_attrs[] = {
572 static struct attribute *btrfs_debug_feature_attrs[] = {
576 static const struct attribute_group btrfs_debug_feature_attr_group = {
578 .attrs = btrfs_debug_feature_attrs,
583 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
591 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
594 static ssize_t global_rsv_size_show(struct kobject *kobj,
595 struct kobj_attribute *ka, char *buf)
597 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
598 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
599 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
601 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
603 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
604 struct kobj_attribute *a, char *buf)
606 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
607 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
608 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
610 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
612 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
613 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
615 static ssize_t raid_bytes_show(struct kobject *kobj,
616 struct kobj_attribute *attr, char *buf);
617 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
618 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
620 static ssize_t raid_bytes_show(struct kobject *kobj,
621 struct kobj_attribute *attr, char *buf)
624 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
625 struct btrfs_block_group *block_group;
626 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
629 down_read(&sinfo->groups_sem);
630 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
631 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
632 val += block_group->length;
634 val += block_group->used;
636 up_read(&sinfo->groups_sem);
637 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
640 static struct attribute *raid_attrs[] = {
641 BTRFS_ATTR_PTR(raid, total_bytes),
642 BTRFS_ATTR_PTR(raid, used_bytes),
645 ATTRIBUTE_GROUPS(raid);
647 static void release_raid_kobj(struct kobject *kobj)
649 kfree(to_raid_kobj(kobj));
652 static struct kobj_type btrfs_raid_ktype = {
653 .sysfs_ops = &kobj_sysfs_ops,
654 .release = release_raid_kobj,
655 .default_groups = raid_groups,
658 #define SPACE_INFO_ATTR(field) \
659 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
660 struct kobj_attribute *a, \
663 struct btrfs_space_info *sinfo = to_space_info(kobj); \
664 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
666 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
668 SPACE_INFO_ATTR(flags);
669 SPACE_INFO_ATTR(total_bytes);
670 SPACE_INFO_ATTR(bytes_used);
671 SPACE_INFO_ATTR(bytes_pinned);
672 SPACE_INFO_ATTR(bytes_reserved);
673 SPACE_INFO_ATTR(bytes_may_use);
674 SPACE_INFO_ATTR(bytes_readonly);
675 SPACE_INFO_ATTR(bytes_zone_unusable);
676 SPACE_INFO_ATTR(disk_used);
677 SPACE_INFO_ATTR(disk_total);
679 static struct attribute *space_info_attrs[] = {
680 BTRFS_ATTR_PTR(space_info, flags),
681 BTRFS_ATTR_PTR(space_info, total_bytes),
682 BTRFS_ATTR_PTR(space_info, bytes_used),
683 BTRFS_ATTR_PTR(space_info, bytes_pinned),
684 BTRFS_ATTR_PTR(space_info, bytes_reserved),
685 BTRFS_ATTR_PTR(space_info, bytes_may_use),
686 BTRFS_ATTR_PTR(space_info, bytes_readonly),
687 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
688 BTRFS_ATTR_PTR(space_info, disk_used),
689 BTRFS_ATTR_PTR(space_info, disk_total),
692 ATTRIBUTE_GROUPS(space_info);
694 static void space_info_release(struct kobject *kobj)
696 struct btrfs_space_info *sinfo = to_space_info(kobj);
700 static struct kobj_type space_info_ktype = {
701 .sysfs_ops = &kobj_sysfs_ops,
702 .release = space_info_release,
703 .default_groups = space_info_groups,
706 static const struct attribute *allocation_attrs[] = {
707 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
708 BTRFS_ATTR_PTR(allocation, global_rsv_size),
712 static ssize_t btrfs_label_show(struct kobject *kobj,
713 struct kobj_attribute *a, char *buf)
715 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
716 char *label = fs_info->super_copy->label;
719 spin_lock(&fs_info->super_lock);
720 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
721 spin_unlock(&fs_info->super_lock);
726 static ssize_t btrfs_label_store(struct kobject *kobj,
727 struct kobj_attribute *a,
728 const char *buf, size_t len)
730 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
736 if (sb_rdonly(fs_info->sb))
740 * p_len is the len until the first occurrence of either
743 p_len = strcspn(buf, "\n");
745 if (p_len >= BTRFS_LABEL_SIZE)
748 spin_lock(&fs_info->super_lock);
749 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
750 memcpy(fs_info->super_copy->label, buf, p_len);
751 spin_unlock(&fs_info->super_lock);
754 * We don't want to do full transaction commit from inside sysfs
756 btrfs_set_pending(fs_info, COMMIT);
757 wake_up_process(fs_info->transaction_kthread);
761 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
763 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
764 struct kobj_attribute *a, char *buf)
766 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
768 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
771 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
773 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
774 struct kobj_attribute *a, char *buf)
776 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
778 return scnprintf(buf, PAGE_SIZE, "%u\n",
779 fs_info->super_copy->sectorsize);
782 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
784 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
785 struct kobj_attribute *a, char *buf)
787 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
789 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
792 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
794 static ssize_t quota_override_show(struct kobject *kobj,
795 struct kobj_attribute *a, char *buf)
797 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
800 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
801 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
804 static ssize_t quota_override_store(struct kobject *kobj,
805 struct kobj_attribute *a,
806 const char *buf, size_t len)
808 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
815 if (!capable(CAP_SYS_RESOURCE))
818 err = kstrtoul(buf, 10, &knob);
825 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
827 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
832 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
834 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
835 struct kobj_attribute *a, char *buf)
837 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
839 return scnprintf(buf, PAGE_SIZE, "%pU\n",
840 fs_info->fs_devices->metadata_uuid);
843 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
845 static ssize_t btrfs_checksum_show(struct kobject *kobj,
846 struct kobj_attribute *a, char *buf)
848 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
849 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
851 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
852 btrfs_super_csum_name(csum_type),
853 crypto_shash_driver_name(fs_info->csum_shash));
856 BTRFS_ATTR(, checksum, btrfs_checksum_show);
858 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
859 struct kobj_attribute *a, char *buf)
861 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
864 switch (READ_ONCE(fs_info->exclusive_operation)) {
865 case BTRFS_EXCLOP_NONE:
868 case BTRFS_EXCLOP_BALANCE:
871 case BTRFS_EXCLOP_DEV_ADD:
872 str = "device add\n";
874 case BTRFS_EXCLOP_DEV_REMOVE:
875 str = "device remove\n";
877 case BTRFS_EXCLOP_DEV_REPLACE:
878 str = "device replace\n";
880 case BTRFS_EXCLOP_RESIZE:
883 case BTRFS_EXCLOP_SWAP_ACTIVATE:
884 str = "swap activate\n";
890 return scnprintf(buf, PAGE_SIZE, "%s", str);
892 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
894 static ssize_t btrfs_generation_show(struct kobject *kobj,
895 struct kobj_attribute *a, char *buf)
897 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
899 return scnprintf(buf, PAGE_SIZE, "%llu\n", fs_info->generation);
901 BTRFS_ATTR(, generation, btrfs_generation_show);
904 * Look for an exact string @string in @buffer with possible leading or
905 * trailing whitespace
907 static bool strmatch(const char *buffer, const char *string)
909 const size_t len = strlen(string);
911 /* Skip leading whitespace */
912 buffer = skip_spaces(buffer);
914 /* Match entire string, check if the rest is whitespace or empty */
915 if (strncmp(string, buffer, len) == 0 &&
916 strlen(skip_spaces(buffer + len)) == 0)
922 static const char * const btrfs_read_policy_name[] = { "pid" };
924 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
925 struct kobj_attribute *a, char *buf)
927 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
931 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
932 if (fs_devices->read_policy == i)
933 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
934 (ret == 0 ? "" : " "),
935 btrfs_read_policy_name[i]);
937 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
938 (ret == 0 ? "" : " "),
939 btrfs_read_policy_name[i]);
942 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
947 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
948 struct kobj_attribute *a,
949 const char *buf, size_t len)
951 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
954 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
955 if (strmatch(buf, btrfs_read_policy_name[i])) {
956 if (i != fs_devices->read_policy) {
957 fs_devices->read_policy = i;
958 btrfs_info(fs_devices->fs_info,
959 "read policy set to '%s'",
960 btrfs_read_policy_name[i]);
968 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
970 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
971 struct kobj_attribute *a,
974 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
977 ret = scnprintf(buf, PAGE_SIZE, "%d\n", fs_info->bg_reclaim_threshold);
982 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
983 struct kobj_attribute *a,
984 const char *buf, size_t len)
986 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
990 ret = kstrtoint(buf, 10, &thresh);
994 if (thresh <= 50 || thresh > 100)
997 fs_info->bg_reclaim_threshold = thresh;
1001 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1002 btrfs_bg_reclaim_threshold_store);
1004 static const struct attribute *btrfs_attrs[] = {
1005 BTRFS_ATTR_PTR(, label),
1006 BTRFS_ATTR_PTR(, nodesize),
1007 BTRFS_ATTR_PTR(, sectorsize),
1008 BTRFS_ATTR_PTR(, clone_alignment),
1009 BTRFS_ATTR_PTR(, quota_override),
1010 BTRFS_ATTR_PTR(, metadata_uuid),
1011 BTRFS_ATTR_PTR(, checksum),
1012 BTRFS_ATTR_PTR(, exclusive_operation),
1013 BTRFS_ATTR_PTR(, generation),
1014 BTRFS_ATTR_PTR(, read_policy),
1015 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1019 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1021 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1023 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1024 complete(&fs_devs->kobj_unregister);
1027 static struct kobj_type btrfs_ktype = {
1028 .sysfs_ops = &kobj_sysfs_ops,
1029 .release = btrfs_release_fsid_kobj,
1032 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1034 if (kobj->ktype != &btrfs_ktype)
1036 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1039 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1041 if (kobj->ktype != &btrfs_ktype)
1043 return to_fs_devs(kobj)->fs_info;
1046 #define NUM_FEATURE_BITS 64
1047 #define BTRFS_FEATURE_NAME_MAX 13
1048 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1049 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1051 static const u64 supported_feature_masks[FEAT_MAX] = {
1052 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1053 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1054 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1057 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1061 for (set = 0; set < FEAT_MAX; set++) {
1063 struct attribute *attrs[2];
1064 struct attribute_group agroup = {
1068 u64 features = get_features(fs_info, set);
1069 features &= ~supported_feature_masks[set];
1075 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1076 struct btrfs_feature_attr *fa;
1078 if (!(features & (1ULL << i)))
1081 fa = &btrfs_feature_attrs[set][i];
1082 attrs[0] = &fa->kobj_attr.attr;
1085 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1090 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1098 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1100 if (fs_devs->devinfo_kobj) {
1101 kobject_del(fs_devs->devinfo_kobj);
1102 kobject_put(fs_devs->devinfo_kobj);
1103 fs_devs->devinfo_kobj = NULL;
1106 if (fs_devs->devices_kobj) {
1107 kobject_del(fs_devs->devices_kobj);
1108 kobject_put(fs_devs->devices_kobj);
1109 fs_devs->devices_kobj = NULL;
1112 if (fs_devs->fsid_kobj.state_initialized) {
1113 kobject_del(&fs_devs->fsid_kobj);
1114 kobject_put(&fs_devs->fsid_kobj);
1115 wait_for_completion(&fs_devs->kobj_unregister);
1119 /* when fs_devs is NULL it will remove all fsid kobject */
1120 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1122 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1125 __btrfs_sysfs_remove_fsid(fs_devs);
1129 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1130 __btrfs_sysfs_remove_fsid(fs_devs);
1134 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1136 struct btrfs_device *device;
1137 struct btrfs_fs_devices *seed;
1139 list_for_each_entry(device, &fs_devices->devices, dev_list)
1140 btrfs_sysfs_remove_device(device);
1142 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1143 list_for_each_entry(device, &seed->devices, dev_list)
1144 btrfs_sysfs_remove_device(device);
1148 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1150 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1152 sysfs_remove_link(fsid_kobj, "bdi");
1154 if (fs_info->space_info_kobj) {
1155 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1156 kobject_del(fs_info->space_info_kobj);
1157 kobject_put(fs_info->space_info_kobj);
1159 #ifdef CONFIG_BTRFS_DEBUG
1160 if (fs_info->discard_debug_kobj) {
1161 sysfs_remove_files(fs_info->discard_debug_kobj,
1162 discard_debug_attrs);
1163 kobject_del(fs_info->discard_debug_kobj);
1164 kobject_put(fs_info->discard_debug_kobj);
1166 if (fs_info->debug_kobj) {
1167 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1168 kobject_del(fs_info->debug_kobj);
1169 kobject_put(fs_info->debug_kobj);
1172 addrm_unknown_feature_attrs(fs_info, false);
1173 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1174 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1175 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1178 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1179 [FEAT_COMPAT] = "compat",
1180 [FEAT_COMPAT_RO] = "compat_ro",
1181 [FEAT_INCOMPAT] = "incompat",
1184 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1186 return btrfs_feature_set_names[set];
1189 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1191 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1196 str = kmalloc(bufsize, GFP_KERNEL);
1200 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1203 if (!(flags & (1ULL << i)))
1206 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1207 len += scnprintf(str + len, bufsize - len, "%s%s",
1208 len ? "," : "", name);
1214 static void init_feature_attrs(void)
1216 struct btrfs_feature_attr *fa;
1219 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1220 ARRAY_SIZE(btrfs_feature_attrs));
1221 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1222 ARRAY_SIZE(btrfs_feature_attrs[0]));
1224 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1225 memset(btrfs_unknown_feature_names, 0,
1226 sizeof(btrfs_unknown_feature_names));
1228 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1229 struct btrfs_feature_attr *sfa;
1230 struct attribute *a = btrfs_supported_feature_attrs[i];
1232 sfa = attr_to_btrfs_feature_attr(a);
1233 bit = ilog2(sfa->feature_bit);
1234 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1236 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1239 for (set = 0; set < FEAT_MAX; set++) {
1240 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1241 char *name = btrfs_unknown_feature_names[set][i];
1242 fa = &btrfs_feature_attrs[set][i];
1244 if (fa->kobj_attr.attr.name)
1247 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1248 btrfs_feature_set_names[set], i);
1250 fa->kobj_attr.attr.name = name;
1251 fa->kobj_attr.attr.mode = S_IRUGO;
1252 fa->feature_set = set;
1253 fa->feature_bit = 1ULL << i;
1259 * Create a sysfs entry for a given block group type at path
1260 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1262 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1264 struct btrfs_fs_info *fs_info = cache->fs_info;
1265 struct btrfs_space_info *space_info = cache->space_info;
1266 struct raid_kobject *rkobj;
1267 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1268 unsigned int nofs_flag;
1272 * Setup a NOFS context because kobject_add(), deep in its call chain,
1273 * does GFP_KERNEL allocations, and we are often called in a context
1274 * where if reclaim is triggered we can deadlock (we are either holding
1275 * a transaction handle or some lock required for a transaction
1278 nofs_flag = memalloc_nofs_save();
1280 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1282 memalloc_nofs_restore(nofs_flag);
1283 btrfs_warn(cache->fs_info,
1284 "couldn't alloc memory for raid level kobject");
1288 rkobj->flags = cache->flags;
1289 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1292 * We call this either on mount, or if we've created a block group for a
1293 * new index type while running (i.e. when restriping). The running
1294 * case is tricky because we could race with other threads, so we need
1295 * to have this check to make sure we didn't already init the kobject.
1297 * We don't have to protect on the free side because it only happens on
1300 spin_lock(&space_info->lock);
1301 if (space_info->block_group_kobjs[index]) {
1302 spin_unlock(&space_info->lock);
1303 kobject_put(&rkobj->kobj);
1306 space_info->block_group_kobjs[index] = &rkobj->kobj;
1308 spin_unlock(&space_info->lock);
1310 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1311 btrfs_bg_type_to_raid_name(rkobj->flags));
1312 memalloc_nofs_restore(nofs_flag);
1314 spin_lock(&space_info->lock);
1315 space_info->block_group_kobjs[index] = NULL;
1316 spin_unlock(&space_info->lock);
1317 kobject_put(&rkobj->kobj);
1319 "failed to add kobject for block cache, ignoring");
1325 * Remove sysfs directories for all block group types of a given space info and
1326 * the space info as well
1328 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1332 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1333 struct kobject *kobj;
1335 kobj = space_info->block_group_kobjs[i];
1336 space_info->block_group_kobjs[i] = NULL;
1342 kobject_del(&space_info->kobj);
1343 kobject_put(&space_info->kobj);
1346 static const char *alloc_name(u64 flags)
1349 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1351 case BTRFS_BLOCK_GROUP_METADATA:
1353 case BTRFS_BLOCK_GROUP_DATA:
1355 case BTRFS_BLOCK_GROUP_SYSTEM:
1359 return "invalid-combination";
1364 * Create a sysfs entry for a space info type at path
1365 * /sys/fs/btrfs/UUID/allocation/TYPE
1367 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1368 struct btrfs_space_info *space_info)
1372 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1373 fs_info->space_info_kobj, "%s",
1374 alloc_name(space_info->flags));
1376 kobject_put(&space_info->kobj);
1383 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1385 struct kobject *devices_kobj;
1388 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1389 * fs_info::fs_devices.
1391 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1392 ASSERT(devices_kobj);
1395 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1397 if (device->devid_kobj.state_initialized) {
1398 kobject_del(&device->devid_kobj);
1399 kobject_put(&device->devid_kobj);
1400 wait_for_completion(&device->kobj_unregister);
1404 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1405 struct kobj_attribute *a,
1409 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1412 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1414 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1416 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1418 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1419 struct kobj_attribute *a, char *buf)
1422 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1425 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1427 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1429 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1431 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1432 struct kobj_attribute *a,
1436 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1439 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1441 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1443 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1445 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1446 struct kobj_attribute *a,
1449 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1452 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1453 READ_ONCE(device->scrub_speed_max));
1456 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1457 struct kobj_attribute *a,
1458 const char *buf, size_t len)
1460 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1463 unsigned long long limit;
1465 limit = memparse(buf, &endptr);
1466 WRITE_ONCE(device->scrub_speed_max, limit);
1469 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1470 btrfs_devinfo_scrub_speed_max_store);
1472 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1473 struct kobj_attribute *a, char *buf)
1476 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1479 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1481 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1483 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1485 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1486 struct kobj_attribute *a, char *buf)
1488 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1491 if (!device->dev_stats_valid)
1492 return scnprintf(buf, PAGE_SIZE, "invalid\n");
1495 * Print all at once so we get a snapshot of all values from the same
1496 * time. Keep them in sync and in order of definition of
1497 * btrfs_dev_stat_values.
1499 return scnprintf(buf, PAGE_SIZE,
1503 "corruption_errs %d\n"
1504 "generation_errs %d\n",
1505 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1506 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1507 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1508 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1509 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1511 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1513 static struct attribute *devid_attrs[] = {
1514 BTRFS_ATTR_PTR(devid, error_stats),
1515 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1516 BTRFS_ATTR_PTR(devid, missing),
1517 BTRFS_ATTR_PTR(devid, replace_target),
1518 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1519 BTRFS_ATTR_PTR(devid, writeable),
1522 ATTRIBUTE_GROUPS(devid);
1524 static void btrfs_release_devid_kobj(struct kobject *kobj)
1526 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1529 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1530 complete(&device->kobj_unregister);
1533 static struct kobj_type devid_ktype = {
1534 .sysfs_ops = &kobj_sysfs_ops,
1535 .default_groups = devid_groups,
1536 .release = btrfs_release_devid_kobj,
1539 int btrfs_sysfs_add_device(struct btrfs_device *device)
1542 unsigned int nofs_flag;
1543 struct kobject *devices_kobj;
1544 struct kobject *devinfo_kobj;
1547 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1548 * for the seed fs_devices
1550 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1551 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1552 ASSERT(devices_kobj);
1553 ASSERT(devinfo_kobj);
1555 nofs_flag = memalloc_nofs_save();
1558 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1560 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1562 btrfs_warn(device->fs_info,
1563 "creating sysfs device link for devid %llu failed: %d",
1564 device->devid, ret);
1569 init_completion(&device->kobj_unregister);
1570 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1571 devinfo_kobj, "%llu", device->devid);
1573 kobject_put(&device->devid_kobj);
1574 btrfs_warn(device->fs_info,
1575 "devinfo init for devid %llu failed: %d",
1576 device->devid, ret);
1580 memalloc_nofs_restore(nofs_flag);
1584 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1587 struct btrfs_device *device;
1588 struct btrfs_fs_devices *seed;
1590 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1591 ret = btrfs_sysfs_add_device(device);
1596 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1597 list_for_each_entry(device, &seed->devices, dev_list) {
1598 ret = btrfs_sysfs_add_device(device);
1607 btrfs_sysfs_remove_fs_devices(fs_devices);
1611 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1615 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1617 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1618 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1619 &disk_to_dev(bdev->bd_disk)->kobj);
1622 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1625 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1628 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1631 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1632 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1633 btrfs_warn(fs_devices->fs_info,
1634 "sysfs: failed to create fsid for sprout");
1637 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1641 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1643 if (kobject_rename(&device->devid_kobj, tmp))
1644 btrfs_warn(device->fs_devices->fs_info,
1645 "sysfs: failed to update devid for %llu",
1649 /* /sys/fs/btrfs/ entry */
1650 static struct kset *btrfs_kset;
1654 * /sys/fs/btrfs/UUID
1656 * Can be called by the device discovery thread.
1658 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1662 init_completion(&fs_devs->kobj_unregister);
1663 fs_devs->fsid_kobj.kset = btrfs_kset;
1664 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1665 "%pU", fs_devs->fsid);
1667 kobject_put(&fs_devs->fsid_kobj);
1671 fs_devs->devices_kobj = kobject_create_and_add("devices",
1672 &fs_devs->fsid_kobj);
1673 if (!fs_devs->devices_kobj) {
1674 btrfs_err(fs_devs->fs_info,
1675 "failed to init sysfs device interface");
1676 btrfs_sysfs_remove_fsid(fs_devs);
1680 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1681 &fs_devs->fsid_kobj);
1682 if (!fs_devs->devinfo_kobj) {
1683 btrfs_err(fs_devs->fs_info,
1684 "failed to init sysfs devinfo kobject");
1685 btrfs_sysfs_remove_fsid(fs_devs);
1692 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1695 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1696 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1698 error = btrfs_sysfs_add_fs_devices(fs_devs);
1702 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1704 btrfs_sysfs_remove_fs_devices(fs_devs);
1708 error = sysfs_create_group(fsid_kobj,
1709 &btrfs_feature_attr_group);
1713 #ifdef CONFIG_BTRFS_DEBUG
1714 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1715 if (!fs_info->debug_kobj) {
1720 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1724 /* Discard directory */
1725 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1726 fs_info->debug_kobj);
1727 if (!fs_info->discard_debug_kobj) {
1732 error = sysfs_create_files(fs_info->discard_debug_kobj,
1733 discard_debug_attrs);
1738 error = addrm_unknown_feature_attrs(fs_info, true);
1742 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1746 fs_info->space_info_kobj = kobject_create_and_add("allocation",
1748 if (!fs_info->space_info_kobj) {
1753 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1759 btrfs_sysfs_remove_mounted(fs_info);
1763 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1765 return to_fs_info(kobj->parent->parent);
1768 #define QGROUP_ATTR(_member, _show_name) \
1769 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1770 struct kobj_attribute *a, \
1773 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1774 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1775 struct btrfs_qgroup, kobj); \
1776 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1778 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1780 #define QGROUP_RSV_ATTR(_name, _type) \
1781 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1782 struct kobj_attribute *a, \
1785 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1786 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1787 struct btrfs_qgroup, kobj); \
1788 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1789 &fs_info->qgroup_lock, buf); \
1791 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1793 QGROUP_ATTR(rfer, referenced);
1794 QGROUP_ATTR(excl, exclusive);
1795 QGROUP_ATTR(max_rfer, max_referenced);
1796 QGROUP_ATTR(max_excl, max_exclusive);
1797 QGROUP_ATTR(lim_flags, limit_flags);
1798 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1799 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1800 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1802 static struct attribute *qgroup_attrs[] = {
1803 BTRFS_ATTR_PTR(qgroup, referenced),
1804 BTRFS_ATTR_PTR(qgroup, exclusive),
1805 BTRFS_ATTR_PTR(qgroup, max_referenced),
1806 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1807 BTRFS_ATTR_PTR(qgroup, limit_flags),
1808 BTRFS_ATTR_PTR(qgroup, rsv_data),
1809 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1810 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1813 ATTRIBUTE_GROUPS(qgroup);
1815 static void qgroup_release(struct kobject *kobj)
1817 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1819 memset(&qgroup->kobj, 0, sizeof(*kobj));
1822 static struct kobj_type qgroup_ktype = {
1823 .sysfs_ops = &kobj_sysfs_ops,
1824 .release = qgroup_release,
1825 .default_groups = qgroup_groups,
1828 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1829 struct btrfs_qgroup *qgroup)
1831 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1834 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1836 if (qgroup->kobj.state_initialized)
1841 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1842 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1843 btrfs_qgroup_subvolid(qgroup->qgroupid));
1845 kobject_put(&qgroup->kobj);
1850 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1852 struct btrfs_qgroup *qgroup;
1853 struct btrfs_qgroup *next;
1855 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1858 rbtree_postorder_for_each_entry_safe(qgroup, next,
1859 &fs_info->qgroup_tree, node)
1860 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1861 if (fs_info->qgroups_kobj) {
1862 kobject_del(fs_info->qgroups_kobj);
1863 kobject_put(fs_info->qgroups_kobj);
1864 fs_info->qgroups_kobj = NULL;
1868 /* Called when qgroups get initialized, thus there is no need for locking */
1869 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1871 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1872 struct btrfs_qgroup *qgroup;
1873 struct btrfs_qgroup *next;
1876 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1880 if (fs_info->qgroups_kobj)
1883 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1884 if (!fs_info->qgroups_kobj) {
1888 rbtree_postorder_for_each_entry_safe(qgroup, next,
1889 &fs_info->qgroup_tree, node) {
1890 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1897 btrfs_sysfs_del_qgroups(fs_info);
1901 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1902 struct btrfs_qgroup *qgroup)
1904 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1907 if (qgroup->kobj.state_initialized) {
1908 kobject_del(&qgroup->kobj);
1909 kobject_put(&qgroup->kobj);
1914 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1915 * values in superblock. Call after any changes to incompat/compat_ro flags
1917 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1918 u64 bit, enum btrfs_feature_set set)
1920 struct btrfs_fs_devices *fs_devs;
1921 struct kobject *fsid_kobj;
1922 u64 __maybe_unused features;
1923 int __maybe_unused ret;
1929 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
1930 * safe when called from some contexts (eg. balance)
1932 features = get_features(fs_info, set);
1933 ASSERT(bit & supported_feature_masks[set]);
1935 fs_devs = fs_info->fs_devices;
1936 fsid_kobj = &fs_devs->fsid_kobj;
1938 if (!fsid_kobj->state_initialized)
1942 * FIXME: this is too heavy to update just one value, ideally we'd like
1943 * to use sysfs_update_group but some refactoring is needed first.
1945 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1946 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1949 int __init btrfs_init_sysfs(void)
1953 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1957 init_feature_attrs();
1958 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1961 ret = sysfs_merge_group(&btrfs_kset->kobj,
1962 &btrfs_static_feature_attr_group);
1964 goto out_remove_group;
1966 #ifdef CONFIG_BTRFS_DEBUG
1967 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1975 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1977 kset_unregister(btrfs_kset);
1982 void __cold btrfs_exit_sysfs(void)
1984 sysfs_unmerge_group(&btrfs_kset->kobj,
1985 &btrfs_static_feature_attr_group);
1986 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1987 #ifdef CONFIG_BTRFS_DEBUG
1988 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1990 kset_unregister(btrfs_kset);