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>
13 #include <linux/sched/mm.h>
17 #include "transaction.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_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
197 static struct attribute *btrfs_supported_feature_attrs[] = {
198 BTRFS_FEAT_ATTR_PTR(mixed_backref),
199 BTRFS_FEAT_ATTR_PTR(default_subvol),
200 BTRFS_FEAT_ATTR_PTR(mixed_groups),
201 BTRFS_FEAT_ATTR_PTR(compress_lzo),
202 BTRFS_FEAT_ATTR_PTR(compress_zstd),
203 BTRFS_FEAT_ATTR_PTR(big_metadata),
204 BTRFS_FEAT_ATTR_PTR(extended_iref),
205 BTRFS_FEAT_ATTR_PTR(raid56),
206 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
207 BTRFS_FEAT_ATTR_PTR(no_holes),
208 BTRFS_FEAT_ATTR_PTR(free_space_tree),
213 * Features which depend on feature bits and may differ between each fs.
215 * /sys/fs/btrfs/features lists all available features of this kernel while
216 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
217 * can be changed online.
219 static const struct attribute_group btrfs_feature_attr_group = {
221 .is_visible = btrfs_feature_visible,
222 .attrs = btrfs_supported_feature_attrs,
225 static ssize_t rmdir_subvol_show(struct kobject *kobj,
226 struct kobj_attribute *ka, char *buf)
228 return snprintf(buf, PAGE_SIZE, "0\n");
230 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
232 static struct attribute *btrfs_supported_static_feature_attrs[] = {
233 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
238 * Features which only depend on kernel version.
240 * These are listed in /sys/fs/btrfs/features along with
241 * btrfs_feature_attr_group
243 static const struct attribute_group btrfs_static_feature_attr_group = {
245 .attrs = btrfs_supported_static_feature_attrs,
248 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
256 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
259 static ssize_t global_rsv_size_show(struct kobject *kobj,
260 struct kobj_attribute *ka, char *buf)
262 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
263 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
264 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
266 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
268 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
269 struct kobj_attribute *a, char *buf)
271 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
272 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
273 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
275 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
277 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
278 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
280 static ssize_t raid_bytes_show(struct kobject *kobj,
281 struct kobj_attribute *attr, char *buf);
282 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
283 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
285 static ssize_t raid_bytes_show(struct kobject *kobj,
286 struct kobj_attribute *attr, char *buf)
289 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
290 struct btrfs_block_group_cache *block_group;
291 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
294 down_read(&sinfo->groups_sem);
295 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
296 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
297 val += block_group->key.offset;
299 val += btrfs_block_group_used(&block_group->item);
301 up_read(&sinfo->groups_sem);
302 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
305 static struct attribute *raid_attributes[] = {
306 BTRFS_ATTR_PTR(raid, total_bytes),
307 BTRFS_ATTR_PTR(raid, used_bytes),
311 static void release_raid_kobj(struct kobject *kobj)
313 kfree(to_raid_kobj(kobj));
316 struct kobj_type btrfs_raid_ktype = {
317 .sysfs_ops = &kobj_sysfs_ops,
318 .release = release_raid_kobj,
319 .default_attrs = raid_attributes,
322 #define SPACE_INFO_ATTR(field) \
323 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
324 struct kobj_attribute *a, \
327 struct btrfs_space_info *sinfo = to_space_info(kobj); \
328 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
330 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
332 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
333 struct kobj_attribute *a,
336 struct btrfs_space_info *sinfo = to_space_info(kobj);
337 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
338 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
341 SPACE_INFO_ATTR(flags);
342 SPACE_INFO_ATTR(total_bytes);
343 SPACE_INFO_ATTR(bytes_used);
344 SPACE_INFO_ATTR(bytes_pinned);
345 SPACE_INFO_ATTR(bytes_reserved);
346 SPACE_INFO_ATTR(bytes_may_use);
347 SPACE_INFO_ATTR(bytes_readonly);
348 SPACE_INFO_ATTR(disk_used);
349 SPACE_INFO_ATTR(disk_total);
350 BTRFS_ATTR(space_info, total_bytes_pinned,
351 btrfs_space_info_show_total_bytes_pinned);
353 static struct attribute *space_info_attrs[] = {
354 BTRFS_ATTR_PTR(space_info, flags),
355 BTRFS_ATTR_PTR(space_info, total_bytes),
356 BTRFS_ATTR_PTR(space_info, bytes_used),
357 BTRFS_ATTR_PTR(space_info, bytes_pinned),
358 BTRFS_ATTR_PTR(space_info, bytes_reserved),
359 BTRFS_ATTR_PTR(space_info, bytes_may_use),
360 BTRFS_ATTR_PTR(space_info, bytes_readonly),
361 BTRFS_ATTR_PTR(space_info, disk_used),
362 BTRFS_ATTR_PTR(space_info, disk_total),
363 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
367 static void space_info_release(struct kobject *kobj)
369 struct btrfs_space_info *sinfo = to_space_info(kobj);
370 percpu_counter_destroy(&sinfo->total_bytes_pinned);
374 struct kobj_type space_info_ktype = {
375 .sysfs_ops = &kobj_sysfs_ops,
376 .release = space_info_release,
377 .default_attrs = space_info_attrs,
380 static const struct attribute *allocation_attrs[] = {
381 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
382 BTRFS_ATTR_PTR(allocation, global_rsv_size),
386 static ssize_t btrfs_label_show(struct kobject *kobj,
387 struct kobj_attribute *a, char *buf)
389 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
390 char *label = fs_info->super_copy->label;
393 spin_lock(&fs_info->super_lock);
394 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
395 spin_unlock(&fs_info->super_lock);
400 static ssize_t btrfs_label_store(struct kobject *kobj,
401 struct kobj_attribute *a,
402 const char *buf, size_t len)
404 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
410 if (sb_rdonly(fs_info->sb))
414 * p_len is the len until the first occurrence of either
417 p_len = strcspn(buf, "\n");
419 if (p_len >= BTRFS_LABEL_SIZE)
422 spin_lock(&fs_info->super_lock);
423 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
424 memcpy(fs_info->super_copy->label, buf, p_len);
425 spin_unlock(&fs_info->super_lock);
428 * We don't want to do full transaction commit from inside sysfs
430 btrfs_set_pending(fs_info, COMMIT);
431 wake_up_process(fs_info->transaction_kthread);
435 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
437 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
438 struct kobj_attribute *a, char *buf)
440 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
442 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
445 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
447 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
448 struct kobj_attribute *a, char *buf)
450 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
452 return snprintf(buf, PAGE_SIZE, "%u\n",
453 fs_info->super_copy->sectorsize);
456 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
458 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
459 struct kobj_attribute *a, char *buf)
461 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
463 return snprintf(buf, PAGE_SIZE, "%u\n",
464 fs_info->super_copy->sectorsize);
467 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
469 static ssize_t quota_override_show(struct kobject *kobj,
470 struct kobj_attribute *a, char *buf)
472 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
475 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
476 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
479 static ssize_t quota_override_store(struct kobject *kobj,
480 struct kobj_attribute *a,
481 const char *buf, size_t len)
483 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
490 if (!capable(CAP_SYS_RESOURCE))
493 err = kstrtoul(buf, 10, &knob);
500 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
502 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
507 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
509 static const struct attribute *btrfs_attrs[] = {
510 BTRFS_ATTR_PTR(, label),
511 BTRFS_ATTR_PTR(, nodesize),
512 BTRFS_ATTR_PTR(, sectorsize),
513 BTRFS_ATTR_PTR(, clone_alignment),
514 BTRFS_ATTR_PTR(, quota_override),
518 static void btrfs_release_fsid_kobj(struct kobject *kobj)
520 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
522 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
523 complete(&fs_devs->kobj_unregister);
526 static struct kobj_type btrfs_ktype = {
527 .sysfs_ops = &kobj_sysfs_ops,
528 .release = btrfs_release_fsid_kobj,
531 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
533 if (kobj->ktype != &btrfs_ktype)
535 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
538 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
540 if (kobj->ktype != &btrfs_ktype)
542 return to_fs_devs(kobj)->fs_info;
545 #define NUM_FEATURE_BITS 64
546 #define BTRFS_FEATURE_NAME_MAX 13
547 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
548 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
550 static const u64 supported_feature_masks[FEAT_MAX] = {
551 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
552 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
553 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
556 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
560 for (set = 0; set < FEAT_MAX; set++) {
562 struct attribute *attrs[2];
563 struct attribute_group agroup = {
567 u64 features = get_features(fs_info, set);
568 features &= ~supported_feature_masks[set];
574 for (i = 0; i < NUM_FEATURE_BITS; i++) {
575 struct btrfs_feature_attr *fa;
577 if (!(features & (1ULL << i)))
580 fa = &btrfs_feature_attrs[set][i];
581 attrs[0] = &fa->kobj_attr.attr;
584 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
589 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
597 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
599 if (fs_devs->device_dir_kobj) {
600 kobject_del(fs_devs->device_dir_kobj);
601 kobject_put(fs_devs->device_dir_kobj);
602 fs_devs->device_dir_kobj = NULL;
605 if (fs_devs->fsid_kobj.state_initialized) {
606 kobject_del(&fs_devs->fsid_kobj);
607 kobject_put(&fs_devs->fsid_kobj);
608 wait_for_completion(&fs_devs->kobj_unregister);
612 /* when fs_devs is NULL it will remove all fsid kobject */
613 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
615 struct list_head *fs_uuids = btrfs_get_fs_uuids();
618 __btrfs_sysfs_remove_fsid(fs_devs);
622 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
623 __btrfs_sysfs_remove_fsid(fs_devs);
627 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
629 btrfs_reset_fs_info_ptr(fs_info);
631 if (fs_info->space_info_kobj) {
632 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
633 kobject_del(fs_info->space_info_kobj);
634 kobject_put(fs_info->space_info_kobj);
636 addrm_unknown_feature_attrs(fs_info, false);
637 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
638 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
639 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
642 const char * const btrfs_feature_set_names[FEAT_MAX] = {
643 [FEAT_COMPAT] = "compat",
644 [FEAT_COMPAT_RO] = "compat_ro",
645 [FEAT_INCOMPAT] = "incompat",
648 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
650 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
655 str = kmalloc(bufsize, GFP_KERNEL);
659 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
662 if (!(flags & (1ULL << i)))
665 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
666 len += snprintf(str + len, bufsize - len, "%s%s",
667 len ? "," : "", name);
673 static void init_feature_attrs(void)
675 struct btrfs_feature_attr *fa;
678 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
679 ARRAY_SIZE(btrfs_feature_attrs));
680 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
681 ARRAY_SIZE(btrfs_feature_attrs[0]));
683 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
684 memset(btrfs_unknown_feature_names, 0,
685 sizeof(btrfs_unknown_feature_names));
687 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
688 struct btrfs_feature_attr *sfa;
689 struct attribute *a = btrfs_supported_feature_attrs[i];
691 sfa = attr_to_btrfs_feature_attr(a);
692 bit = ilog2(sfa->feature_bit);
693 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
695 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
698 for (set = 0; set < FEAT_MAX; set++) {
699 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
700 char *name = btrfs_unknown_feature_names[set][i];
701 fa = &btrfs_feature_attrs[set][i];
703 if (fa->kobj_attr.attr.name)
706 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
707 btrfs_feature_set_names[set], i);
709 fa->kobj_attr.attr.name = name;
710 fa->kobj_attr.attr.mode = S_IRUGO;
711 fa->feature_set = set;
712 fa->feature_bit = 1ULL << i;
717 /* when one_device is NULL, it removes all device links */
719 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
720 struct btrfs_device *one_device)
722 struct hd_struct *disk;
723 struct kobject *disk_kobj;
725 if (!fs_devices->device_dir_kobj)
728 if (one_device && one_device->bdev) {
729 disk = one_device->bdev->bd_part;
730 disk_kobj = &part_to_dev(disk)->kobj;
732 sysfs_remove_link(fs_devices->device_dir_kobj,
739 list_for_each_entry(one_device,
740 &fs_devices->devices, dev_list) {
741 if (!one_device->bdev)
743 disk = one_device->bdev->bd_part;
744 disk_kobj = &part_to_dev(disk)->kobj;
746 sysfs_remove_link(fs_devices->device_dir_kobj,
753 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
755 if (!fs_devs->device_dir_kobj)
756 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
757 &fs_devs->fsid_kobj);
759 if (!fs_devs->device_dir_kobj)
765 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
766 struct btrfs_device *one_device)
769 struct btrfs_device *dev;
770 unsigned int nofs_flag;
772 nofs_flag = memalloc_nofs_save();
773 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
774 struct hd_struct *disk;
775 struct kobject *disk_kobj;
780 if (one_device && one_device != dev)
783 disk = dev->bdev->bd_part;
784 disk_kobj = &part_to_dev(disk)->kobj;
786 error = sysfs_create_link(fs_devices->device_dir_kobj,
787 disk_kobj, disk_kobj->name);
791 memalloc_nofs_restore(nofs_flag);
796 /* /sys/fs/btrfs/ entry */
797 static struct kset *btrfs_kset;
799 /* /sys/kernel/debug/btrfs */
800 static struct dentry *btrfs_debugfs_root_dentry;
802 /* Debugging tunables and exported data */
803 u64 btrfs_debugfs_test;
806 * Can be called by the device discovery thread.
807 * And parent can be specified for seed device
809 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
810 struct kobject *parent)
814 init_completion(&fs_devs->kobj_unregister);
815 fs_devs->fsid_kobj.kset = btrfs_kset;
816 error = kobject_init_and_add(&fs_devs->fsid_kobj,
817 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
819 kobject_put(&fs_devs->fsid_kobj);
826 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
829 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
830 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
832 btrfs_set_fs_info_ptr(fs_info);
834 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
838 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
840 btrfs_sysfs_rm_device_link(fs_devs, NULL);
844 error = sysfs_create_group(fsid_kobj,
845 &btrfs_feature_attr_group);
849 error = addrm_unknown_feature_attrs(fs_info, true);
853 fs_info->space_info_kobj = kobject_create_and_add("allocation",
855 if (!fs_info->space_info_kobj) {
860 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
866 btrfs_sysfs_remove_mounted(fs_info);
872 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
873 * values in superblock. Call after any changes to incompat/compat_ro flags
875 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
876 u64 bit, enum btrfs_feature_set set)
878 struct btrfs_fs_devices *fs_devs;
879 struct kobject *fsid_kobj;
886 features = get_features(fs_info, set);
887 ASSERT(bit & supported_feature_masks[set]);
889 fs_devs = fs_info->fs_devices;
890 fsid_kobj = &fs_devs->fsid_kobj;
892 if (!fsid_kobj->state_initialized)
896 * FIXME: this is too heavy to update just one value, ideally we'd like
897 * to use sysfs_update_group but some refactoring is needed first.
899 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
900 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
903 static int btrfs_init_debugfs(void)
905 #ifdef CONFIG_DEBUG_FS
906 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
907 if (!btrfs_debugfs_root_dentry)
911 * Example code, how to export data through debugfs.
913 * file: /sys/kernel/debug/btrfs/test
914 * contents of: btrfs_debugfs_test
916 #ifdef CONFIG_BTRFS_DEBUG
917 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
918 &btrfs_debugfs_test);
925 int __init btrfs_init_sysfs(void)
929 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
933 ret = btrfs_init_debugfs();
937 init_feature_attrs();
938 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
941 ret = sysfs_merge_group(&btrfs_kset->kobj,
942 &btrfs_static_feature_attr_group);
944 goto out_remove_group;
949 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
951 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
953 kset_unregister(btrfs_kset);
958 void __cold btrfs_exit_sysfs(void)
960 sysfs_unmerge_group(&btrfs_kset->kobj,
961 &btrfs_static_feature_attr_group);
962 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
963 kset_unregister(btrfs_kset);
964 debugfs_remove_recursive(btrfs_debugfs_root_dentry);