1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/kobject.h>
11 #include <linux/bug.h>
12 #include <linux/debugfs.h>
16 #include "transaction.h"
19 #include "space-info.h"
21 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
22 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
24 static u64 get_features(struct btrfs_fs_info *fs_info,
25 enum btrfs_feature_set set)
27 struct btrfs_super_block *disk_super = fs_info->super_copy;
28 if (set == FEAT_COMPAT)
29 return btrfs_super_compat_flags(disk_super);
30 else if (set == FEAT_COMPAT_RO)
31 return btrfs_super_compat_ro_flags(disk_super);
33 return btrfs_super_incompat_flags(disk_super);
36 static void set_features(struct btrfs_fs_info *fs_info,
37 enum btrfs_feature_set set, u64 features)
39 struct btrfs_super_block *disk_super = fs_info->super_copy;
40 if (set == FEAT_COMPAT)
41 btrfs_set_super_compat_flags(disk_super, features);
42 else if (set == FEAT_COMPAT_RO)
43 btrfs_set_super_compat_ro_flags(disk_super, features);
45 btrfs_set_super_incompat_flags(disk_super, features);
48 static int can_modify_feature(struct btrfs_feature_attr *fa)
52 switch (fa->feature_set) {
54 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
55 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
58 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
59 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
62 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
63 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
66 pr_warn("btrfs: sysfs: unknown feature set %d\n",
71 if (set & fa->feature_bit)
73 if (clear & fa->feature_bit)
79 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
80 struct kobj_attribute *a, char *buf)
83 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
84 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
86 u64 features = get_features(fs_info, fa->feature_set);
87 if (features & fa->feature_bit)
90 val = can_modify_feature(fa);
92 return snprintf(buf, PAGE_SIZE, "%d\n", val);
95 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
96 struct kobj_attribute *a,
97 const char *buf, size_t count)
99 struct btrfs_fs_info *fs_info;
100 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
101 u64 features, set, clear;
105 fs_info = to_fs_info(kobj);
109 if (sb_rdonly(fs_info->sb))
112 ret = kstrtoul(skip_spaces(buf), 0, &val);
116 if (fa->feature_set == FEAT_COMPAT) {
117 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
118 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
119 } else if (fa->feature_set == FEAT_COMPAT_RO) {
120 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
121 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
123 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
124 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
127 features = get_features(fs_info, fa->feature_set);
130 if ((val && (features & fa->feature_bit)) ||
131 (!val && !(features & fa->feature_bit)))
134 if ((val && !(set & fa->feature_bit)) ||
135 (!val && !(clear & fa->feature_bit))) {
137 "%sabling feature %s on mounted fs is not supported.",
138 val ? "En" : "Dis", fa->kobj_attr.attr.name);
142 btrfs_info(fs_info, "%s %s feature flag",
143 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
145 spin_lock(&fs_info->super_lock);
146 features = get_features(fs_info, fa->feature_set);
148 features |= fa->feature_bit;
150 features &= ~fa->feature_bit;
151 set_features(fs_info, fa->feature_set, features);
152 spin_unlock(&fs_info->super_lock);
155 * We don't want to do full transaction commit from inside sysfs
157 btrfs_set_pending(fs_info, COMMIT);
158 wake_up_process(fs_info->transaction_kthread);
163 static umode_t btrfs_feature_visible(struct kobject *kobj,
164 struct attribute *attr, int unused)
166 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
167 umode_t mode = attr->mode;
170 struct btrfs_feature_attr *fa;
173 fa = attr_to_btrfs_feature_attr(attr);
174 features = get_features(fs_info, fa->feature_set);
176 if (can_modify_feature(fa))
178 else if (!(features & fa->feature_bit))
185 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
186 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
187 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
188 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
189 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
190 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
191 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
192 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
193 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
194 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
195 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
196 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
198 static struct attribute *btrfs_supported_feature_attrs[] = {
199 BTRFS_FEAT_ATTR_PTR(mixed_backref),
200 BTRFS_FEAT_ATTR_PTR(default_subvol),
201 BTRFS_FEAT_ATTR_PTR(mixed_groups),
202 BTRFS_FEAT_ATTR_PTR(compress_lzo),
203 BTRFS_FEAT_ATTR_PTR(compress_zstd),
204 BTRFS_FEAT_ATTR_PTR(big_metadata),
205 BTRFS_FEAT_ATTR_PTR(extended_iref),
206 BTRFS_FEAT_ATTR_PTR(raid56),
207 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
208 BTRFS_FEAT_ATTR_PTR(no_holes),
209 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
210 BTRFS_FEAT_ATTR_PTR(free_space_tree),
215 * Features which depend on feature bits and may differ between each fs.
217 * /sys/fs/btrfs/features lists all available features of this kernel while
218 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
219 * can be changed online.
221 static const struct attribute_group btrfs_feature_attr_group = {
223 .is_visible = btrfs_feature_visible,
224 .attrs = btrfs_supported_feature_attrs,
227 static ssize_t rmdir_subvol_show(struct kobject *kobj,
228 struct kobj_attribute *ka, char *buf)
230 return snprintf(buf, PAGE_SIZE, "0\n");
232 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
234 static struct attribute *btrfs_supported_static_feature_attrs[] = {
235 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
240 * Features which only depend on kernel version.
242 * These are listed in /sys/fs/btrfs/features along with
243 * btrfs_feature_attr_group
245 static const struct attribute_group btrfs_static_feature_attr_group = {
247 .attrs = btrfs_supported_static_feature_attrs,
250 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
258 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
261 static ssize_t global_rsv_size_show(struct kobject *kobj,
262 struct kobj_attribute *ka, char *buf)
264 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
265 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
266 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
268 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
270 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
271 struct kobj_attribute *a, char *buf)
273 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
274 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
275 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
277 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
279 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
280 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
282 static ssize_t raid_bytes_show(struct kobject *kobj,
283 struct kobj_attribute *attr, char *buf);
284 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
285 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
287 static ssize_t raid_bytes_show(struct kobject *kobj,
288 struct kobj_attribute *attr, char *buf)
291 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
292 struct btrfs_block_group_cache *block_group;
293 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
296 down_read(&sinfo->groups_sem);
297 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
298 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
299 val += block_group->key.offset;
301 val += btrfs_block_group_used(&block_group->item);
303 up_read(&sinfo->groups_sem);
304 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
307 static struct attribute *raid_attrs[] = {
308 BTRFS_ATTR_PTR(raid, total_bytes),
309 BTRFS_ATTR_PTR(raid, used_bytes),
312 ATTRIBUTE_GROUPS(raid);
314 static void release_raid_kobj(struct kobject *kobj)
316 kfree(to_raid_kobj(kobj));
319 struct kobj_type btrfs_raid_ktype = {
320 .sysfs_ops = &kobj_sysfs_ops,
321 .release = release_raid_kobj,
322 .default_groups = raid_groups,
325 #define SPACE_INFO_ATTR(field) \
326 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
327 struct kobj_attribute *a, \
330 struct btrfs_space_info *sinfo = to_space_info(kobj); \
331 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
333 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
335 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
336 struct kobj_attribute *a,
339 struct btrfs_space_info *sinfo = to_space_info(kobj);
340 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
341 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
344 SPACE_INFO_ATTR(flags);
345 SPACE_INFO_ATTR(total_bytes);
346 SPACE_INFO_ATTR(bytes_used);
347 SPACE_INFO_ATTR(bytes_pinned);
348 SPACE_INFO_ATTR(bytes_reserved);
349 SPACE_INFO_ATTR(bytes_may_use);
350 SPACE_INFO_ATTR(bytes_readonly);
351 SPACE_INFO_ATTR(disk_used);
352 SPACE_INFO_ATTR(disk_total);
353 BTRFS_ATTR(space_info, total_bytes_pinned,
354 btrfs_space_info_show_total_bytes_pinned);
356 static struct attribute *space_info_attrs[] = {
357 BTRFS_ATTR_PTR(space_info, flags),
358 BTRFS_ATTR_PTR(space_info, total_bytes),
359 BTRFS_ATTR_PTR(space_info, bytes_used),
360 BTRFS_ATTR_PTR(space_info, bytes_pinned),
361 BTRFS_ATTR_PTR(space_info, bytes_reserved),
362 BTRFS_ATTR_PTR(space_info, bytes_may_use),
363 BTRFS_ATTR_PTR(space_info, bytes_readonly),
364 BTRFS_ATTR_PTR(space_info, disk_used),
365 BTRFS_ATTR_PTR(space_info, disk_total),
366 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
369 ATTRIBUTE_GROUPS(space_info);
371 static void space_info_release(struct kobject *kobj)
373 struct btrfs_space_info *sinfo = to_space_info(kobj);
374 percpu_counter_destroy(&sinfo->total_bytes_pinned);
378 struct kobj_type space_info_ktype = {
379 .sysfs_ops = &kobj_sysfs_ops,
380 .release = space_info_release,
381 .default_groups = space_info_groups,
384 static const struct attribute *allocation_attrs[] = {
385 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
386 BTRFS_ATTR_PTR(allocation, global_rsv_size),
390 static ssize_t btrfs_label_show(struct kobject *kobj,
391 struct kobj_attribute *a, char *buf)
393 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
394 char *label = fs_info->super_copy->label;
397 spin_lock(&fs_info->super_lock);
398 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
399 spin_unlock(&fs_info->super_lock);
404 static ssize_t btrfs_label_store(struct kobject *kobj,
405 struct kobj_attribute *a,
406 const char *buf, size_t len)
408 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
414 if (sb_rdonly(fs_info->sb))
418 * p_len is the len until the first occurrence of either
421 p_len = strcspn(buf, "\n");
423 if (p_len >= BTRFS_LABEL_SIZE)
426 spin_lock(&fs_info->super_lock);
427 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
428 memcpy(fs_info->super_copy->label, buf, p_len);
429 spin_unlock(&fs_info->super_lock);
432 * We don't want to do full transaction commit from inside sysfs
434 btrfs_set_pending(fs_info, COMMIT);
435 wake_up_process(fs_info->transaction_kthread);
439 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
441 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
442 struct kobj_attribute *a, char *buf)
444 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
446 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
449 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
451 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
452 struct kobj_attribute *a, char *buf)
454 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
456 return snprintf(buf, PAGE_SIZE, "%u\n",
457 fs_info->super_copy->sectorsize);
460 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
462 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
463 struct kobj_attribute *a, char *buf)
465 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
467 return snprintf(buf, PAGE_SIZE, "%u\n",
468 fs_info->super_copy->sectorsize);
471 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
473 static ssize_t quota_override_show(struct kobject *kobj,
474 struct kobj_attribute *a, char *buf)
476 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
479 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
480 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
483 static ssize_t quota_override_store(struct kobject *kobj,
484 struct kobj_attribute *a,
485 const char *buf, size_t len)
487 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
494 if (!capable(CAP_SYS_RESOURCE))
497 err = kstrtoul(buf, 10, &knob);
504 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
506 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
511 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
513 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
514 struct kobj_attribute *a, char *buf)
516 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
518 return snprintf(buf, PAGE_SIZE, "%pU\n",
519 fs_info->fs_devices->metadata_uuid);
522 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
524 static const struct attribute *btrfs_attrs[] = {
525 BTRFS_ATTR_PTR(, label),
526 BTRFS_ATTR_PTR(, nodesize),
527 BTRFS_ATTR_PTR(, sectorsize),
528 BTRFS_ATTR_PTR(, clone_alignment),
529 BTRFS_ATTR_PTR(, quota_override),
530 BTRFS_ATTR_PTR(, metadata_uuid),
534 static void btrfs_release_fsid_kobj(struct kobject *kobj)
536 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
538 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
539 complete(&fs_devs->kobj_unregister);
542 static struct kobj_type btrfs_ktype = {
543 .sysfs_ops = &kobj_sysfs_ops,
544 .release = btrfs_release_fsid_kobj,
547 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
549 if (kobj->ktype != &btrfs_ktype)
551 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
554 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
556 if (kobj->ktype != &btrfs_ktype)
558 return to_fs_devs(kobj)->fs_info;
561 #define NUM_FEATURE_BITS 64
562 #define BTRFS_FEATURE_NAME_MAX 13
563 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
564 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
566 static const u64 supported_feature_masks[FEAT_MAX] = {
567 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
568 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
569 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
572 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
576 for (set = 0; set < FEAT_MAX; set++) {
578 struct attribute *attrs[2];
579 struct attribute_group agroup = {
583 u64 features = get_features(fs_info, set);
584 features &= ~supported_feature_masks[set];
590 for (i = 0; i < NUM_FEATURE_BITS; i++) {
591 struct btrfs_feature_attr *fa;
593 if (!(features & (1ULL << i)))
596 fa = &btrfs_feature_attrs[set][i];
597 attrs[0] = &fa->kobj_attr.attr;
600 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
605 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
613 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
615 if (fs_devs->device_dir_kobj) {
616 kobject_del(fs_devs->device_dir_kobj);
617 kobject_put(fs_devs->device_dir_kobj);
618 fs_devs->device_dir_kobj = NULL;
621 if (fs_devs->fsid_kobj.state_initialized) {
622 kobject_del(&fs_devs->fsid_kobj);
623 kobject_put(&fs_devs->fsid_kobj);
624 wait_for_completion(&fs_devs->kobj_unregister);
628 /* when fs_devs is NULL it will remove all fsid kobject */
629 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
631 struct list_head *fs_uuids = btrfs_get_fs_uuids();
634 __btrfs_sysfs_remove_fsid(fs_devs);
638 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
639 __btrfs_sysfs_remove_fsid(fs_devs);
643 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
645 btrfs_reset_fs_info_ptr(fs_info);
647 if (fs_info->space_info_kobj) {
648 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
649 kobject_del(fs_info->space_info_kobj);
650 kobject_put(fs_info->space_info_kobj);
652 addrm_unknown_feature_attrs(fs_info, false);
653 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
654 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
655 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
658 const char * const btrfs_feature_set_names[FEAT_MAX] = {
659 [FEAT_COMPAT] = "compat",
660 [FEAT_COMPAT_RO] = "compat_ro",
661 [FEAT_INCOMPAT] = "incompat",
664 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
666 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
671 str = kmalloc(bufsize, GFP_KERNEL);
675 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
678 if (!(flags & (1ULL << i)))
681 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
682 len += snprintf(str + len, bufsize - len, "%s%s",
683 len ? "," : "", name);
689 static void init_feature_attrs(void)
691 struct btrfs_feature_attr *fa;
694 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
695 ARRAY_SIZE(btrfs_feature_attrs));
696 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
697 ARRAY_SIZE(btrfs_feature_attrs[0]));
699 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
700 memset(btrfs_unknown_feature_names, 0,
701 sizeof(btrfs_unknown_feature_names));
703 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
704 struct btrfs_feature_attr *sfa;
705 struct attribute *a = btrfs_supported_feature_attrs[i];
707 sfa = attr_to_btrfs_feature_attr(a);
708 bit = ilog2(sfa->feature_bit);
709 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
711 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
714 for (set = 0; set < FEAT_MAX; set++) {
715 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
716 char *name = btrfs_unknown_feature_names[set][i];
717 fa = &btrfs_feature_attrs[set][i];
719 if (fa->kobj_attr.attr.name)
722 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
723 btrfs_feature_set_names[set], i);
725 fa->kobj_attr.attr.name = name;
726 fa->kobj_attr.attr.mode = S_IRUGO;
727 fa->feature_set = set;
728 fa->feature_bit = 1ULL << i;
733 /* when one_device is NULL, it removes all device links */
735 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
736 struct btrfs_device *one_device)
738 struct hd_struct *disk;
739 struct kobject *disk_kobj;
741 if (!fs_devices->device_dir_kobj)
744 if (one_device && one_device->bdev) {
745 disk = one_device->bdev->bd_part;
746 disk_kobj = &part_to_dev(disk)->kobj;
748 sysfs_remove_link(fs_devices->device_dir_kobj,
755 list_for_each_entry(one_device,
756 &fs_devices->devices, dev_list) {
757 if (!one_device->bdev)
759 disk = one_device->bdev->bd_part;
760 disk_kobj = &part_to_dev(disk)->kobj;
762 sysfs_remove_link(fs_devices->device_dir_kobj,
769 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
771 if (!fs_devs->device_dir_kobj)
772 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
773 &fs_devs->fsid_kobj);
775 if (!fs_devs->device_dir_kobj)
781 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
782 struct btrfs_device *one_device)
785 struct btrfs_device *dev;
787 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
788 struct hd_struct *disk;
789 struct kobject *disk_kobj;
794 if (one_device && one_device != dev)
797 disk = dev->bdev->bd_part;
798 disk_kobj = &part_to_dev(disk)->kobj;
800 error = sysfs_create_link(fs_devices->device_dir_kobj,
801 disk_kobj, disk_kobj->name);
809 /* /sys/fs/btrfs/ entry */
810 static struct kset *btrfs_kset;
812 /* /sys/kernel/debug/btrfs */
813 static struct dentry *btrfs_debugfs_root_dentry;
815 /* Debugging tunables and exported data */
816 u64 btrfs_debugfs_test;
819 * Can be called by the device discovery thread.
820 * And parent can be specified for seed device
822 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
823 struct kobject *parent)
827 init_completion(&fs_devs->kobj_unregister);
828 fs_devs->fsid_kobj.kset = btrfs_kset;
829 error = kobject_init_and_add(&fs_devs->fsid_kobj,
830 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
832 kobject_put(&fs_devs->fsid_kobj);
839 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
842 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
843 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
845 btrfs_set_fs_info_ptr(fs_info);
847 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
851 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
853 btrfs_sysfs_rm_device_link(fs_devs, NULL);
857 error = sysfs_create_group(fsid_kobj,
858 &btrfs_feature_attr_group);
862 error = addrm_unknown_feature_attrs(fs_info, true);
866 fs_info->space_info_kobj = kobject_create_and_add("allocation",
868 if (!fs_info->space_info_kobj) {
873 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
879 btrfs_sysfs_remove_mounted(fs_info);
885 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
886 * values in superblock. Call after any changes to incompat/compat_ro flags
888 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
889 u64 bit, enum btrfs_feature_set set)
891 struct btrfs_fs_devices *fs_devs;
892 struct kobject *fsid_kobj;
899 features = get_features(fs_info, set);
900 ASSERT(bit & supported_feature_masks[set]);
902 fs_devs = fs_info->fs_devices;
903 fsid_kobj = &fs_devs->fsid_kobj;
905 if (!fsid_kobj->state_initialized)
909 * FIXME: this is too heavy to update just one value, ideally we'd like
910 * to use sysfs_update_group but some refactoring is needed first.
912 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
913 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
916 static void btrfs_init_debugfs(void)
918 #ifdef CONFIG_DEBUG_FS
919 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
922 * Example code, how to export data through debugfs.
924 * file: /sys/kernel/debug/btrfs/test
925 * contents of: btrfs_debugfs_test
927 #ifdef CONFIG_BTRFS_DEBUG
928 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
929 &btrfs_debugfs_test);
935 int __init btrfs_init_sysfs(void)
939 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
943 btrfs_init_debugfs();
945 init_feature_attrs();
946 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
949 ret = sysfs_merge_group(&btrfs_kset->kobj,
950 &btrfs_static_feature_attr_group);
952 goto out_remove_group;
957 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
959 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
960 kset_unregister(btrfs_kset);
965 void __cold btrfs_exit_sysfs(void)
967 sysfs_unmerge_group(&btrfs_kset->kobj,
968 &btrfs_static_feature_attr_group);
969 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
970 kset_unregister(btrfs_kset);
971 debugfs_remove_recursive(btrfs_debugfs_root_dentry);