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>
15 #include "transaction.h"
18 #include "space-info.h"
19 #include "block-group.h"
21 struct btrfs_feature_attr {
22 struct kobj_attribute kobj_attr;
23 enum btrfs_feature_set feature_set;
27 /* For raid type sysfs entries */
33 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
35 .attr = { .name = __stringify(_name), .mode = _mode }, \
40 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
41 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
42 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
44 #define BTRFS_ATTR(_prefix, _name, _show) \
45 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
46 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
48 #define BTRFS_ATTR_PTR(_prefix, _name) \
49 (&btrfs_attr_##_prefix##_##_name.attr)
51 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
52 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
53 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
54 btrfs_feature_attr_show, \
55 btrfs_feature_attr_store), \
56 .feature_set = _feature_set, \
57 .feature_bit = _feature_prefix ##_## _feature_bit, \
59 #define BTRFS_FEAT_ATTR_PTR(_name) \
60 (&btrfs_attr_features_##_name.kobj_attr.attr)
62 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
63 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
64 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
65 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
66 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
69 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
70 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
72 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
74 return container_of(a, struct btrfs_feature_attr, kobj_attr);
77 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
79 return container_of(attr, struct kobj_attribute, attr);
82 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
83 struct attribute *attr)
85 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
88 static u64 get_features(struct btrfs_fs_info *fs_info,
89 enum btrfs_feature_set set)
91 struct btrfs_super_block *disk_super = fs_info->super_copy;
92 if (set == FEAT_COMPAT)
93 return btrfs_super_compat_flags(disk_super);
94 else if (set == FEAT_COMPAT_RO)
95 return btrfs_super_compat_ro_flags(disk_super);
97 return btrfs_super_incompat_flags(disk_super);
100 static void set_features(struct btrfs_fs_info *fs_info,
101 enum btrfs_feature_set set, u64 features)
103 struct btrfs_super_block *disk_super = fs_info->super_copy;
104 if (set == FEAT_COMPAT)
105 btrfs_set_super_compat_flags(disk_super, features);
106 else if (set == FEAT_COMPAT_RO)
107 btrfs_set_super_compat_ro_flags(disk_super, features);
109 btrfs_set_super_incompat_flags(disk_super, features);
112 static int can_modify_feature(struct btrfs_feature_attr *fa)
116 switch (fa->feature_set) {
118 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
119 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
122 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
123 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
126 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
127 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
130 pr_warn("btrfs: sysfs: unknown feature set %d\n",
135 if (set & fa->feature_bit)
137 if (clear & fa->feature_bit)
143 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
144 struct kobj_attribute *a, char *buf)
147 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
148 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
150 u64 features = get_features(fs_info, fa->feature_set);
151 if (features & fa->feature_bit)
154 val = can_modify_feature(fa);
156 return snprintf(buf, PAGE_SIZE, "%d\n", val);
159 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
160 struct kobj_attribute *a,
161 const char *buf, size_t count)
163 struct btrfs_fs_info *fs_info;
164 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
165 u64 features, set, clear;
169 fs_info = to_fs_info(kobj);
173 if (sb_rdonly(fs_info->sb))
176 ret = kstrtoul(skip_spaces(buf), 0, &val);
180 if (fa->feature_set == FEAT_COMPAT) {
181 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
182 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
183 } else if (fa->feature_set == FEAT_COMPAT_RO) {
184 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
185 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
187 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
188 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
191 features = get_features(fs_info, fa->feature_set);
194 if ((val && (features & fa->feature_bit)) ||
195 (!val && !(features & fa->feature_bit)))
198 if ((val && !(set & fa->feature_bit)) ||
199 (!val && !(clear & fa->feature_bit))) {
201 "%sabling feature %s on mounted fs is not supported.",
202 val ? "En" : "Dis", fa->kobj_attr.attr.name);
206 btrfs_info(fs_info, "%s %s feature flag",
207 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
209 spin_lock(&fs_info->super_lock);
210 features = get_features(fs_info, fa->feature_set);
212 features |= fa->feature_bit;
214 features &= ~fa->feature_bit;
215 set_features(fs_info, fa->feature_set, features);
216 spin_unlock(&fs_info->super_lock);
219 * We don't want to do full transaction commit from inside sysfs
221 btrfs_set_pending(fs_info, COMMIT);
222 wake_up_process(fs_info->transaction_kthread);
227 static umode_t btrfs_feature_visible(struct kobject *kobj,
228 struct attribute *attr, int unused)
230 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
231 umode_t mode = attr->mode;
234 struct btrfs_feature_attr *fa;
237 fa = attr_to_btrfs_feature_attr(attr);
238 features = get_features(fs_info, fa->feature_set);
240 if (can_modify_feature(fa))
242 else if (!(features & fa->feature_bit))
249 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
250 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
251 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
252 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
253 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
254 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
255 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
256 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
257 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
258 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
259 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
260 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
262 static struct attribute *btrfs_supported_feature_attrs[] = {
263 BTRFS_FEAT_ATTR_PTR(mixed_backref),
264 BTRFS_FEAT_ATTR_PTR(default_subvol),
265 BTRFS_FEAT_ATTR_PTR(mixed_groups),
266 BTRFS_FEAT_ATTR_PTR(compress_lzo),
267 BTRFS_FEAT_ATTR_PTR(compress_zstd),
268 BTRFS_FEAT_ATTR_PTR(big_metadata),
269 BTRFS_FEAT_ATTR_PTR(extended_iref),
270 BTRFS_FEAT_ATTR_PTR(raid56),
271 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
272 BTRFS_FEAT_ATTR_PTR(no_holes),
273 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
274 BTRFS_FEAT_ATTR_PTR(free_space_tree),
279 * Features which depend on feature bits and may differ between each fs.
281 * /sys/fs/btrfs/features lists all available features of this kernel while
282 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
283 * can be changed online.
285 static const struct attribute_group btrfs_feature_attr_group = {
287 .is_visible = btrfs_feature_visible,
288 .attrs = btrfs_supported_feature_attrs,
291 static ssize_t rmdir_subvol_show(struct kobject *kobj,
292 struct kobj_attribute *ka, char *buf)
294 return snprintf(buf, PAGE_SIZE, "0\n");
296 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
298 static struct attribute *btrfs_supported_static_feature_attrs[] = {
299 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
304 * Features which only depend on kernel version.
306 * These are listed in /sys/fs/btrfs/features along with
307 * btrfs_feature_attr_group
309 static const struct attribute_group btrfs_static_feature_attr_group = {
311 .attrs = btrfs_supported_static_feature_attrs,
314 #ifdef CONFIG_BTRFS_DEBUG
317 * Runtime debugging exported via sysfs
319 * /sys/fs/btrfs/debug - applies to module or all filesystems
320 * /sys/fs/btrfs/UUID - applies only to the given filesystem
322 static struct attribute *btrfs_debug_feature_attrs[] = {
326 static const struct attribute_group btrfs_debug_feature_attr_group = {
328 .attrs = btrfs_debug_feature_attrs,
333 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
341 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
344 static ssize_t global_rsv_size_show(struct kobject *kobj,
345 struct kobj_attribute *ka, char *buf)
347 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
348 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
349 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
351 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
353 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
354 struct kobj_attribute *a, char *buf)
356 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
357 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
358 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
360 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
362 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
363 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
365 static ssize_t raid_bytes_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf);
367 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
368 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
370 static ssize_t raid_bytes_show(struct kobject *kobj,
371 struct kobj_attribute *attr, char *buf)
374 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
375 struct btrfs_block_group_cache *block_group;
376 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
379 down_read(&sinfo->groups_sem);
380 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
381 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
382 val += block_group->key.offset;
384 val += btrfs_block_group_used(&block_group->item);
386 up_read(&sinfo->groups_sem);
387 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
390 static struct attribute *raid_attrs[] = {
391 BTRFS_ATTR_PTR(raid, total_bytes),
392 BTRFS_ATTR_PTR(raid, used_bytes),
395 ATTRIBUTE_GROUPS(raid);
397 static void release_raid_kobj(struct kobject *kobj)
399 kfree(to_raid_kobj(kobj));
402 static struct kobj_type btrfs_raid_ktype = {
403 .sysfs_ops = &kobj_sysfs_ops,
404 .release = release_raid_kobj,
405 .default_groups = raid_groups,
408 #define SPACE_INFO_ATTR(field) \
409 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
410 struct kobj_attribute *a, \
413 struct btrfs_space_info *sinfo = to_space_info(kobj); \
414 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
416 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
418 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
419 struct kobj_attribute *a,
422 struct btrfs_space_info *sinfo = to_space_info(kobj);
423 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
424 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
427 SPACE_INFO_ATTR(flags);
428 SPACE_INFO_ATTR(total_bytes);
429 SPACE_INFO_ATTR(bytes_used);
430 SPACE_INFO_ATTR(bytes_pinned);
431 SPACE_INFO_ATTR(bytes_reserved);
432 SPACE_INFO_ATTR(bytes_may_use);
433 SPACE_INFO_ATTR(bytes_readonly);
434 SPACE_INFO_ATTR(disk_used);
435 SPACE_INFO_ATTR(disk_total);
436 BTRFS_ATTR(space_info, total_bytes_pinned,
437 btrfs_space_info_show_total_bytes_pinned);
439 static struct attribute *space_info_attrs[] = {
440 BTRFS_ATTR_PTR(space_info, flags),
441 BTRFS_ATTR_PTR(space_info, total_bytes),
442 BTRFS_ATTR_PTR(space_info, bytes_used),
443 BTRFS_ATTR_PTR(space_info, bytes_pinned),
444 BTRFS_ATTR_PTR(space_info, bytes_reserved),
445 BTRFS_ATTR_PTR(space_info, bytes_may_use),
446 BTRFS_ATTR_PTR(space_info, bytes_readonly),
447 BTRFS_ATTR_PTR(space_info, disk_used),
448 BTRFS_ATTR_PTR(space_info, disk_total),
449 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
452 ATTRIBUTE_GROUPS(space_info);
454 static void space_info_release(struct kobject *kobj)
456 struct btrfs_space_info *sinfo = to_space_info(kobj);
457 percpu_counter_destroy(&sinfo->total_bytes_pinned);
461 static struct kobj_type space_info_ktype = {
462 .sysfs_ops = &kobj_sysfs_ops,
463 .release = space_info_release,
464 .default_groups = space_info_groups,
467 static const struct attribute *allocation_attrs[] = {
468 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
469 BTRFS_ATTR_PTR(allocation, global_rsv_size),
473 static ssize_t btrfs_label_show(struct kobject *kobj,
474 struct kobj_attribute *a, char *buf)
476 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
477 char *label = fs_info->super_copy->label;
480 spin_lock(&fs_info->super_lock);
481 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
482 spin_unlock(&fs_info->super_lock);
487 static ssize_t btrfs_label_store(struct kobject *kobj,
488 struct kobj_attribute *a,
489 const char *buf, size_t len)
491 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
497 if (sb_rdonly(fs_info->sb))
501 * p_len is the len until the first occurrence of either
504 p_len = strcspn(buf, "\n");
506 if (p_len >= BTRFS_LABEL_SIZE)
509 spin_lock(&fs_info->super_lock);
510 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
511 memcpy(fs_info->super_copy->label, buf, p_len);
512 spin_unlock(&fs_info->super_lock);
515 * We don't want to do full transaction commit from inside sysfs
517 btrfs_set_pending(fs_info, COMMIT);
518 wake_up_process(fs_info->transaction_kthread);
522 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
524 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
525 struct kobj_attribute *a, char *buf)
527 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
529 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
532 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
534 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
535 struct kobj_attribute *a, char *buf)
537 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
539 return snprintf(buf, PAGE_SIZE, "%u\n",
540 fs_info->super_copy->sectorsize);
543 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
545 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
546 struct kobj_attribute *a, char *buf)
548 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
550 return snprintf(buf, PAGE_SIZE, "%u\n",
551 fs_info->super_copy->sectorsize);
554 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
556 static ssize_t quota_override_show(struct kobject *kobj,
557 struct kobj_attribute *a, char *buf)
559 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
562 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
563 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
566 static ssize_t quota_override_store(struct kobject *kobj,
567 struct kobj_attribute *a,
568 const char *buf, size_t len)
570 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
577 if (!capable(CAP_SYS_RESOURCE))
580 err = kstrtoul(buf, 10, &knob);
587 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
589 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
594 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
596 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
597 struct kobj_attribute *a, char *buf)
599 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
601 return snprintf(buf, PAGE_SIZE, "%pU\n",
602 fs_info->fs_devices->metadata_uuid);
605 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
607 static const struct attribute *btrfs_attrs[] = {
608 BTRFS_ATTR_PTR(, label),
609 BTRFS_ATTR_PTR(, nodesize),
610 BTRFS_ATTR_PTR(, sectorsize),
611 BTRFS_ATTR_PTR(, clone_alignment),
612 BTRFS_ATTR_PTR(, quota_override),
613 BTRFS_ATTR_PTR(, metadata_uuid),
617 static void btrfs_release_fsid_kobj(struct kobject *kobj)
619 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
621 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
622 complete(&fs_devs->kobj_unregister);
625 static struct kobj_type btrfs_ktype = {
626 .sysfs_ops = &kobj_sysfs_ops,
627 .release = btrfs_release_fsid_kobj,
630 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
632 if (kobj->ktype != &btrfs_ktype)
634 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
637 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
639 if (kobj->ktype != &btrfs_ktype)
641 return to_fs_devs(kobj)->fs_info;
644 #define NUM_FEATURE_BITS 64
645 #define BTRFS_FEATURE_NAME_MAX 13
646 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
647 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
649 static const u64 supported_feature_masks[FEAT_MAX] = {
650 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
651 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
652 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
655 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
659 for (set = 0; set < FEAT_MAX; set++) {
661 struct attribute *attrs[2];
662 struct attribute_group agroup = {
666 u64 features = get_features(fs_info, set);
667 features &= ~supported_feature_masks[set];
673 for (i = 0; i < NUM_FEATURE_BITS; i++) {
674 struct btrfs_feature_attr *fa;
676 if (!(features & (1ULL << i)))
679 fa = &btrfs_feature_attrs[set][i];
680 attrs[0] = &fa->kobj_attr.attr;
683 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
688 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
696 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
698 if (fs_devs->device_dir_kobj) {
699 kobject_del(fs_devs->device_dir_kobj);
700 kobject_put(fs_devs->device_dir_kobj);
701 fs_devs->device_dir_kobj = NULL;
704 if (fs_devs->fsid_kobj.state_initialized) {
705 kobject_del(&fs_devs->fsid_kobj);
706 kobject_put(&fs_devs->fsid_kobj);
707 wait_for_completion(&fs_devs->kobj_unregister);
711 /* when fs_devs is NULL it will remove all fsid kobject */
712 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
714 struct list_head *fs_uuids = btrfs_get_fs_uuids();
717 __btrfs_sysfs_remove_fsid(fs_devs);
721 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
722 __btrfs_sysfs_remove_fsid(fs_devs);
726 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
728 btrfs_reset_fs_info_ptr(fs_info);
730 if (fs_info->space_info_kobj) {
731 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
732 kobject_del(fs_info->space_info_kobj);
733 kobject_put(fs_info->space_info_kobj);
735 addrm_unknown_feature_attrs(fs_info, false);
736 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
737 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
738 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
741 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
742 [FEAT_COMPAT] = "compat",
743 [FEAT_COMPAT_RO] = "compat_ro",
744 [FEAT_INCOMPAT] = "incompat",
747 const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
749 return btrfs_feature_set_names[set];
752 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
754 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
759 str = kmalloc(bufsize, GFP_KERNEL);
763 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
766 if (!(flags & (1ULL << i)))
769 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
770 len += snprintf(str + len, bufsize - len, "%s%s",
771 len ? "," : "", name);
777 static void init_feature_attrs(void)
779 struct btrfs_feature_attr *fa;
782 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
783 ARRAY_SIZE(btrfs_feature_attrs));
784 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
785 ARRAY_SIZE(btrfs_feature_attrs[0]));
787 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
788 memset(btrfs_unknown_feature_names, 0,
789 sizeof(btrfs_unknown_feature_names));
791 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
792 struct btrfs_feature_attr *sfa;
793 struct attribute *a = btrfs_supported_feature_attrs[i];
795 sfa = attr_to_btrfs_feature_attr(a);
796 bit = ilog2(sfa->feature_bit);
797 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
799 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
802 for (set = 0; set < FEAT_MAX; set++) {
803 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
804 char *name = btrfs_unknown_feature_names[set][i];
805 fa = &btrfs_feature_attrs[set][i];
807 if (fa->kobj_attr.attr.name)
810 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
811 btrfs_feature_set_names[set], i);
813 fa->kobj_attr.attr.name = name;
814 fa->kobj_attr.attr.mode = S_IRUGO;
815 fa->feature_set = set;
816 fa->feature_bit = 1ULL << i;
822 * Create a sysfs entry for a given block group type at path
823 * /sys/fs/btrfs/UUID/allocation/data/TYPE
825 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
827 struct btrfs_fs_info *fs_info = cache->fs_info;
828 struct btrfs_space_info *space_info = cache->space_info;
829 struct raid_kobject *rkobj;
830 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
831 unsigned int nofs_flag;
835 * Setup a NOFS context because kobject_add(), deep in its call chain,
836 * does GFP_KERNEL allocations, and we are often called in a context
837 * where if reclaim is triggered we can deadlock (we are either holding
838 * a transaction handle or some lock required for a transaction
841 nofs_flag = memalloc_nofs_save();
843 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
845 memalloc_nofs_restore(nofs_flag);
846 btrfs_warn(cache->fs_info,
847 "couldn't alloc memory for raid level kobject");
851 rkobj->flags = cache->flags;
852 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
853 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
854 btrfs_bg_type_to_raid_name(rkobj->flags));
855 memalloc_nofs_restore(nofs_flag);
857 kobject_put(&rkobj->kobj);
859 "failed to add kobject for block cache, ignoring");
863 space_info->block_group_kobjs[index] = &rkobj->kobj;
867 * Remove sysfs directories for all block group types of a given space info and
868 * the space info as well
870 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
874 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
875 struct kobject *kobj;
877 kobj = space_info->block_group_kobjs[i];
878 space_info->block_group_kobjs[i] = NULL;
884 kobject_del(&space_info->kobj);
885 kobject_put(&space_info->kobj);
888 static const char *alloc_name(u64 flags)
891 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
893 case BTRFS_BLOCK_GROUP_METADATA:
895 case BTRFS_BLOCK_GROUP_DATA:
897 case BTRFS_BLOCK_GROUP_SYSTEM:
901 return "invalid-combination";
906 * Create a sysfs entry for a space info type at path
907 * /sys/fs/btrfs/UUID/allocation/TYPE
909 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
910 struct btrfs_space_info *space_info)
914 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
915 fs_info->space_info_kobj, "%s",
916 alloc_name(space_info->flags));
918 kobject_put(&space_info->kobj);
925 /* when one_device is NULL, it removes all device links */
927 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
928 struct btrfs_device *one_device)
930 struct hd_struct *disk;
931 struct kobject *disk_kobj;
933 if (!fs_devices->device_dir_kobj)
936 if (one_device && one_device->bdev) {
937 disk = one_device->bdev->bd_part;
938 disk_kobj = &part_to_dev(disk)->kobj;
940 sysfs_remove_link(fs_devices->device_dir_kobj,
947 list_for_each_entry(one_device,
948 &fs_devices->devices, dev_list) {
949 if (!one_device->bdev)
951 disk = one_device->bdev->bd_part;
952 disk_kobj = &part_to_dev(disk)->kobj;
954 sysfs_remove_link(fs_devices->device_dir_kobj,
961 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
963 if (!fs_devs->device_dir_kobj)
964 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
965 &fs_devs->fsid_kobj);
967 if (!fs_devs->device_dir_kobj)
973 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
974 struct btrfs_device *one_device)
977 struct btrfs_device *dev;
979 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
980 struct hd_struct *disk;
981 struct kobject *disk_kobj;
986 if (one_device && one_device != dev)
989 disk = dev->bdev->bd_part;
990 disk_kobj = &part_to_dev(disk)->kobj;
992 error = sysfs_create_link(fs_devices->device_dir_kobj,
993 disk_kobj, disk_kobj->name);
1001 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1005 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1007 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1008 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1009 &disk_to_dev(bdev->bd_disk)->kobj);
1012 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1015 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1018 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1021 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1022 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1023 btrfs_warn(fs_devices->fs_info,
1024 "sysfs: failed to create fsid for sprout");
1027 /* /sys/fs/btrfs/ entry */
1028 static struct kset *btrfs_kset;
1031 * Can be called by the device discovery thread.
1032 * And parent can be specified for seed device
1034 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
1035 struct kobject *parent)
1039 init_completion(&fs_devs->kobj_unregister);
1040 fs_devs->fsid_kobj.kset = btrfs_kset;
1041 error = kobject_init_and_add(&fs_devs->fsid_kobj,
1042 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
1044 kobject_put(&fs_devs->fsid_kobj);
1051 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1054 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1055 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1057 btrfs_set_fs_info_ptr(fs_info);
1059 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
1063 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1065 btrfs_sysfs_rm_device_link(fs_devs, NULL);
1069 error = sysfs_create_group(fsid_kobj,
1070 &btrfs_feature_attr_group);
1074 #ifdef CONFIG_BTRFS_DEBUG
1075 error = sysfs_create_group(fsid_kobj,
1076 &btrfs_debug_feature_attr_group);
1081 error = addrm_unknown_feature_attrs(fs_info, true);
1085 fs_info->space_info_kobj = kobject_create_and_add("allocation",
1087 if (!fs_info->space_info_kobj) {
1092 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1098 btrfs_sysfs_remove_mounted(fs_info);
1104 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1105 * values in superblock. Call after any changes to incompat/compat_ro flags
1107 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1108 u64 bit, enum btrfs_feature_set set)
1110 struct btrfs_fs_devices *fs_devs;
1111 struct kobject *fsid_kobj;
1118 features = get_features(fs_info, set);
1119 ASSERT(bit & supported_feature_masks[set]);
1121 fs_devs = fs_info->fs_devices;
1122 fsid_kobj = &fs_devs->fsid_kobj;
1124 if (!fsid_kobj->state_initialized)
1128 * FIXME: this is too heavy to update just one value, ideally we'd like
1129 * to use sysfs_update_group but some refactoring is needed first.
1131 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1132 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1135 int __init btrfs_init_sysfs(void)
1139 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1143 init_feature_attrs();
1144 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1147 ret = sysfs_merge_group(&btrfs_kset->kobj,
1148 &btrfs_static_feature_attr_group);
1150 goto out_remove_group;
1152 #ifdef CONFIG_BTRFS_DEBUG
1153 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1161 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1163 kset_unregister(btrfs_kset);
1168 void __cold btrfs_exit_sysfs(void)
1170 sysfs_unmerge_group(&btrfs_kset->kobj,
1171 &btrfs_static_feature_attr_group);
1172 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1173 kset_unregister(btrfs_kset);