Linux 4.19.133
[linux/fpc-iii.git] / fs / btrfs / sysfs.c
blobaefb0169d46d75145e4c257adf8da11cc03e4b70
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
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>
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "sysfs.h"
18 #include "volumes.h"
20 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
21 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
23 static u64 get_features(struct btrfs_fs_info *fs_info,
24 enum btrfs_feature_set set)
26 struct btrfs_super_block *disk_super = fs_info->super_copy;
27 if (set == FEAT_COMPAT)
28 return btrfs_super_compat_flags(disk_super);
29 else if (set == FEAT_COMPAT_RO)
30 return btrfs_super_compat_ro_flags(disk_super);
31 else
32 return btrfs_super_incompat_flags(disk_super);
35 static void set_features(struct btrfs_fs_info *fs_info,
36 enum btrfs_feature_set set, u64 features)
38 struct btrfs_super_block *disk_super = fs_info->super_copy;
39 if (set == FEAT_COMPAT)
40 btrfs_set_super_compat_flags(disk_super, features);
41 else if (set == FEAT_COMPAT_RO)
42 btrfs_set_super_compat_ro_flags(disk_super, features);
43 else
44 btrfs_set_super_incompat_flags(disk_super, features);
47 static int can_modify_feature(struct btrfs_feature_attr *fa)
49 int val = 0;
50 u64 set, clear;
51 switch (fa->feature_set) {
52 case FEAT_COMPAT:
53 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
54 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
55 break;
56 case FEAT_COMPAT_RO:
57 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
58 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
59 break;
60 case FEAT_INCOMPAT:
61 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
62 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
63 break;
64 default:
65 pr_warn("btrfs: sysfs: unknown feature set %d\n",
66 fa->feature_set);
67 return 0;
70 if (set & fa->feature_bit)
71 val |= 1;
72 if (clear & fa->feature_bit)
73 val |= 2;
75 return val;
78 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
79 struct kobj_attribute *a, char *buf)
81 int val = 0;
82 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
83 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
84 if (fs_info) {
85 u64 features = get_features(fs_info, fa->feature_set);
86 if (features & fa->feature_bit)
87 val = 1;
88 } else
89 val = can_modify_feature(fa);
91 return snprintf(buf, PAGE_SIZE, "%d\n", val);
94 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
95 struct kobj_attribute *a,
96 const char *buf, size_t count)
98 struct btrfs_fs_info *fs_info;
99 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
100 u64 features, set, clear;
101 unsigned long val;
102 int ret;
104 fs_info = to_fs_info(kobj);
105 if (!fs_info)
106 return -EPERM;
108 if (sb_rdonly(fs_info->sb))
109 return -EROFS;
111 ret = kstrtoul(skip_spaces(buf), 0, &val);
112 if (ret)
113 return ret;
115 if (fa->feature_set == FEAT_COMPAT) {
116 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
117 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
118 } else if (fa->feature_set == FEAT_COMPAT_RO) {
119 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
120 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
121 } else {
122 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
123 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
126 features = get_features(fs_info, fa->feature_set);
128 /* Nothing to do */
129 if ((val && (features & fa->feature_bit)) ||
130 (!val && !(features & fa->feature_bit)))
131 return count;
133 if ((val && !(set & fa->feature_bit)) ||
134 (!val && !(clear & fa->feature_bit))) {
135 btrfs_info(fs_info,
136 "%sabling feature %s on mounted fs is not supported.",
137 val ? "En" : "Dis", fa->kobj_attr.attr.name);
138 return -EPERM;
141 btrfs_info(fs_info, "%s %s feature flag",
142 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
144 spin_lock(&fs_info->super_lock);
145 features = get_features(fs_info, fa->feature_set);
146 if (val)
147 features |= fa->feature_bit;
148 else
149 features &= ~fa->feature_bit;
150 set_features(fs_info, fa->feature_set, features);
151 spin_unlock(&fs_info->super_lock);
154 * We don't want to do full transaction commit from inside sysfs
156 btrfs_set_pending(fs_info, COMMIT);
157 wake_up_process(fs_info->transaction_kthread);
159 return count;
162 static umode_t btrfs_feature_visible(struct kobject *kobj,
163 struct attribute *attr, int unused)
165 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
166 umode_t mode = attr->mode;
168 if (fs_info) {
169 struct btrfs_feature_attr *fa;
170 u64 features;
172 fa = attr_to_btrfs_feature_attr(attr);
173 features = get_features(fs_info, fa->feature_set);
175 if (can_modify_feature(fa))
176 mode |= S_IWUSR;
177 else if (!(features & fa->feature_bit))
178 mode = 0;
181 return mode;
184 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
185 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
186 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
187 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
188 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
189 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
190 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
191 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
192 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
193 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
194 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
196 static struct attribute *btrfs_supported_feature_attrs[] = {
197 BTRFS_FEAT_ATTR_PTR(mixed_backref),
198 BTRFS_FEAT_ATTR_PTR(default_subvol),
199 BTRFS_FEAT_ATTR_PTR(mixed_groups),
200 BTRFS_FEAT_ATTR_PTR(compress_lzo),
201 BTRFS_FEAT_ATTR_PTR(compress_zstd),
202 BTRFS_FEAT_ATTR_PTR(big_metadata),
203 BTRFS_FEAT_ATTR_PTR(extended_iref),
204 BTRFS_FEAT_ATTR_PTR(raid56),
205 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
206 BTRFS_FEAT_ATTR_PTR(no_holes),
207 BTRFS_FEAT_ATTR_PTR(free_space_tree),
208 NULL
212 * Features which depend on feature bits and may differ between each fs.
214 * /sys/fs/btrfs/features lists all available features of this kernel while
215 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
216 * can be changed online.
218 static const struct attribute_group btrfs_feature_attr_group = {
219 .name = "features",
220 .is_visible = btrfs_feature_visible,
221 .attrs = btrfs_supported_feature_attrs,
224 static ssize_t rmdir_subvol_show(struct kobject *kobj,
225 struct kobj_attribute *ka, char *buf)
227 return snprintf(buf, PAGE_SIZE, "0\n");
229 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
231 static struct attribute *btrfs_supported_static_feature_attrs[] = {
232 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
233 NULL
237 * Features which only depend on kernel version.
239 * These are listed in /sys/fs/btrfs/features along with
240 * btrfs_feature_attr_group
242 static const struct attribute_group btrfs_static_feature_attr_group = {
243 .name = "features",
244 .attrs = btrfs_supported_static_feature_attrs,
247 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
249 u64 val;
250 if (lock)
251 spin_lock(lock);
252 val = *value_ptr;
253 if (lock)
254 spin_unlock(lock);
255 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
258 static ssize_t global_rsv_size_show(struct kobject *kobj,
259 struct kobj_attribute *ka, char *buf)
261 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
262 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
263 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
265 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
267 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
268 struct kobj_attribute *a, char *buf)
270 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
271 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
272 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
274 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
276 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
277 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
279 static ssize_t raid_bytes_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf);
281 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
282 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
284 static ssize_t raid_bytes_show(struct kobject *kobj,
285 struct kobj_attribute *attr, char *buf)
288 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
289 struct btrfs_block_group_cache *block_group;
290 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
291 u64 val = 0;
293 down_read(&sinfo->groups_sem);
294 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
295 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
296 val += block_group->key.offset;
297 else
298 val += btrfs_block_group_used(&block_group->item);
300 up_read(&sinfo->groups_sem);
301 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
304 static struct attribute *raid_attributes[] = {
305 BTRFS_ATTR_PTR(raid, total_bytes),
306 BTRFS_ATTR_PTR(raid, used_bytes),
307 NULL
310 static void release_raid_kobj(struct kobject *kobj)
312 kfree(to_raid_kobj(kobj));
315 struct kobj_type btrfs_raid_ktype = {
316 .sysfs_ops = &kobj_sysfs_ops,
317 .release = release_raid_kobj,
318 .default_attrs = raid_attributes,
321 #define SPACE_INFO_ATTR(field) \
322 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
323 struct kobj_attribute *a, \
324 char *buf) \
326 struct btrfs_space_info *sinfo = to_space_info(kobj); \
327 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
329 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
331 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
332 struct kobj_attribute *a,
333 char *buf)
335 struct btrfs_space_info *sinfo = to_space_info(kobj);
336 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
337 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
340 SPACE_INFO_ATTR(flags);
341 SPACE_INFO_ATTR(total_bytes);
342 SPACE_INFO_ATTR(bytes_used);
343 SPACE_INFO_ATTR(bytes_pinned);
344 SPACE_INFO_ATTR(bytes_reserved);
345 SPACE_INFO_ATTR(bytes_may_use);
346 SPACE_INFO_ATTR(bytes_readonly);
347 SPACE_INFO_ATTR(disk_used);
348 SPACE_INFO_ATTR(disk_total);
349 BTRFS_ATTR(space_info, total_bytes_pinned,
350 btrfs_space_info_show_total_bytes_pinned);
352 static struct attribute *space_info_attrs[] = {
353 BTRFS_ATTR_PTR(space_info, flags),
354 BTRFS_ATTR_PTR(space_info, total_bytes),
355 BTRFS_ATTR_PTR(space_info, bytes_used),
356 BTRFS_ATTR_PTR(space_info, bytes_pinned),
357 BTRFS_ATTR_PTR(space_info, bytes_reserved),
358 BTRFS_ATTR_PTR(space_info, bytes_may_use),
359 BTRFS_ATTR_PTR(space_info, bytes_readonly),
360 BTRFS_ATTR_PTR(space_info, disk_used),
361 BTRFS_ATTR_PTR(space_info, disk_total),
362 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
363 NULL,
366 static void space_info_release(struct kobject *kobj)
368 struct btrfs_space_info *sinfo = to_space_info(kobj);
369 percpu_counter_destroy(&sinfo->total_bytes_pinned);
370 kfree(sinfo);
373 struct kobj_type space_info_ktype = {
374 .sysfs_ops = &kobj_sysfs_ops,
375 .release = space_info_release,
376 .default_attrs = space_info_attrs,
379 static const struct attribute *allocation_attrs[] = {
380 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
381 BTRFS_ATTR_PTR(allocation, global_rsv_size),
382 NULL,
385 static ssize_t btrfs_label_show(struct kobject *kobj,
386 struct kobj_attribute *a, char *buf)
388 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
389 char *label = fs_info->super_copy->label;
390 ssize_t ret;
392 spin_lock(&fs_info->super_lock);
393 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
394 spin_unlock(&fs_info->super_lock);
396 return ret;
399 static ssize_t btrfs_label_store(struct kobject *kobj,
400 struct kobj_attribute *a,
401 const char *buf, size_t len)
403 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
404 size_t p_len;
406 if (!fs_info)
407 return -EPERM;
409 if (sb_rdonly(fs_info->sb))
410 return -EROFS;
413 * p_len is the len until the first occurrence of either
414 * '\n' or '\0'
416 p_len = strcspn(buf, "\n");
418 if (p_len >= BTRFS_LABEL_SIZE)
419 return -EINVAL;
421 spin_lock(&fs_info->super_lock);
422 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
423 memcpy(fs_info->super_copy->label, buf, p_len);
424 spin_unlock(&fs_info->super_lock);
427 * We don't want to do full transaction commit from inside sysfs
429 btrfs_set_pending(fs_info, COMMIT);
430 wake_up_process(fs_info->transaction_kthread);
432 return len;
434 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
436 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
437 struct kobj_attribute *a, char *buf)
439 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
441 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
444 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
446 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
447 struct kobj_attribute *a, char *buf)
449 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
451 return snprintf(buf, PAGE_SIZE, "%u\n",
452 fs_info->super_copy->sectorsize);
455 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
457 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
458 struct kobj_attribute *a, char *buf)
460 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
462 return snprintf(buf, PAGE_SIZE, "%u\n",
463 fs_info->super_copy->sectorsize);
466 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
468 static ssize_t quota_override_show(struct kobject *kobj,
469 struct kobj_attribute *a, char *buf)
471 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
472 int quota_override;
474 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
475 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
478 static ssize_t quota_override_store(struct kobject *kobj,
479 struct kobj_attribute *a,
480 const char *buf, size_t len)
482 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
483 unsigned long knob;
484 int err;
486 if (!fs_info)
487 return -EPERM;
489 if (!capable(CAP_SYS_RESOURCE))
490 return -EPERM;
492 err = kstrtoul(buf, 10, &knob);
493 if (err)
494 return err;
495 if (knob > 1)
496 return -EINVAL;
498 if (knob)
499 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
500 else
501 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
503 return len;
506 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
508 static const struct attribute *btrfs_attrs[] = {
509 BTRFS_ATTR_PTR(, label),
510 BTRFS_ATTR_PTR(, nodesize),
511 BTRFS_ATTR_PTR(, sectorsize),
512 BTRFS_ATTR_PTR(, clone_alignment),
513 BTRFS_ATTR_PTR(, quota_override),
514 NULL,
517 static void btrfs_release_fsid_kobj(struct kobject *kobj)
519 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
521 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
522 complete(&fs_devs->kobj_unregister);
525 static struct kobj_type btrfs_ktype = {
526 .sysfs_ops = &kobj_sysfs_ops,
527 .release = btrfs_release_fsid_kobj,
530 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
532 if (kobj->ktype != &btrfs_ktype)
533 return NULL;
534 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
537 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
539 if (kobj->ktype != &btrfs_ktype)
540 return NULL;
541 return to_fs_devs(kobj)->fs_info;
544 #define NUM_FEATURE_BITS 64
545 #define BTRFS_FEATURE_NAME_MAX 13
546 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
547 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
549 static const u64 supported_feature_masks[FEAT_MAX] = {
550 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
551 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
552 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
555 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
557 int set;
559 for (set = 0; set < FEAT_MAX; set++) {
560 int i;
561 struct attribute *attrs[2];
562 struct attribute_group agroup = {
563 .name = "features",
564 .attrs = attrs,
566 u64 features = get_features(fs_info, set);
567 features &= ~supported_feature_masks[set];
569 if (!features)
570 continue;
572 attrs[1] = NULL;
573 for (i = 0; i < NUM_FEATURE_BITS; i++) {
574 struct btrfs_feature_attr *fa;
576 if (!(features & (1ULL << i)))
577 continue;
579 fa = &btrfs_feature_attrs[set][i];
580 attrs[0] = &fa->kobj_attr.attr;
581 if (add) {
582 int ret;
583 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
584 &agroup);
585 if (ret)
586 return ret;
587 } else
588 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
589 &agroup);
593 return 0;
596 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
598 if (fs_devs->device_dir_kobj) {
599 kobject_del(fs_devs->device_dir_kobj);
600 kobject_put(fs_devs->device_dir_kobj);
601 fs_devs->device_dir_kobj = NULL;
604 if (fs_devs->fsid_kobj.state_initialized) {
605 kobject_del(&fs_devs->fsid_kobj);
606 kobject_put(&fs_devs->fsid_kobj);
607 wait_for_completion(&fs_devs->kobj_unregister);
611 /* when fs_devs is NULL it will remove all fsid kobject */
612 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
614 struct list_head *fs_uuids = btrfs_get_fs_uuids();
616 if (fs_devs) {
617 __btrfs_sysfs_remove_fsid(fs_devs);
618 return;
621 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
622 __btrfs_sysfs_remove_fsid(fs_devs);
626 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
628 btrfs_reset_fs_info_ptr(fs_info);
630 if (fs_info->space_info_kobj) {
631 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
632 kobject_del(fs_info->space_info_kobj);
633 kobject_put(fs_info->space_info_kobj);
635 addrm_unknown_feature_attrs(fs_info, false);
636 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
637 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
638 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
641 const char * const btrfs_feature_set_names[FEAT_MAX] = {
642 [FEAT_COMPAT] = "compat",
643 [FEAT_COMPAT_RO] = "compat_ro",
644 [FEAT_INCOMPAT] = "incompat",
647 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
649 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
650 int len = 0;
651 int i;
652 char *str;
654 str = kmalloc(bufsize, GFP_KERNEL);
655 if (!str)
656 return str;
658 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
659 const char *name;
661 if (!(flags & (1ULL << i)))
662 continue;
664 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
665 len += snprintf(str + len, bufsize - len, "%s%s",
666 len ? "," : "", name);
669 return str;
672 static void init_feature_attrs(void)
674 struct btrfs_feature_attr *fa;
675 int set, i;
677 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
678 ARRAY_SIZE(btrfs_feature_attrs));
679 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
680 ARRAY_SIZE(btrfs_feature_attrs[0]));
682 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
683 memset(btrfs_unknown_feature_names, 0,
684 sizeof(btrfs_unknown_feature_names));
686 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
687 struct btrfs_feature_attr *sfa;
688 struct attribute *a = btrfs_supported_feature_attrs[i];
689 int bit;
690 sfa = attr_to_btrfs_feature_attr(a);
691 bit = ilog2(sfa->feature_bit);
692 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
694 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
697 for (set = 0; set < FEAT_MAX; set++) {
698 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
699 char *name = btrfs_unknown_feature_names[set][i];
700 fa = &btrfs_feature_attrs[set][i];
702 if (fa->kobj_attr.attr.name)
703 continue;
705 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
706 btrfs_feature_set_names[set], i);
708 fa->kobj_attr.attr.name = name;
709 fa->kobj_attr.attr.mode = S_IRUGO;
710 fa->feature_set = set;
711 fa->feature_bit = 1ULL << i;
716 /* when one_device is NULL, it removes all device links */
718 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
719 struct btrfs_device *one_device)
721 struct hd_struct *disk;
722 struct kobject *disk_kobj;
724 if (!fs_devices->device_dir_kobj)
725 return -EINVAL;
727 if (one_device && one_device->bdev) {
728 disk = one_device->bdev->bd_part;
729 disk_kobj = &part_to_dev(disk)->kobj;
731 sysfs_remove_link(fs_devices->device_dir_kobj,
732 disk_kobj->name);
735 if (one_device)
736 return 0;
738 list_for_each_entry(one_device,
739 &fs_devices->devices, dev_list) {
740 if (!one_device->bdev)
741 continue;
742 disk = one_device->bdev->bd_part;
743 disk_kobj = &part_to_dev(disk)->kobj;
745 sysfs_remove_link(fs_devices->device_dir_kobj,
746 disk_kobj->name);
749 return 0;
752 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
754 if (!fs_devs->device_dir_kobj)
755 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
756 &fs_devs->fsid_kobj);
758 if (!fs_devs->device_dir_kobj)
759 return -ENOMEM;
761 return 0;
764 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
765 struct btrfs_device *one_device)
767 int error = 0;
768 struct btrfs_device *dev;
770 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
771 struct hd_struct *disk;
772 struct kobject *disk_kobj;
774 if (!dev->bdev)
775 continue;
777 if (one_device && one_device != dev)
778 continue;
780 disk = dev->bdev->bd_part;
781 disk_kobj = &part_to_dev(disk)->kobj;
783 error = sysfs_create_link(fs_devices->device_dir_kobj,
784 disk_kobj, disk_kobj->name);
785 if (error)
786 break;
789 return error;
792 /* /sys/fs/btrfs/ entry */
793 static struct kset *btrfs_kset;
795 /* /sys/kernel/debug/btrfs */
796 static struct dentry *btrfs_debugfs_root_dentry;
798 /* Debugging tunables and exported data */
799 u64 btrfs_debugfs_test;
802 * Can be called by the device discovery thread.
803 * And parent can be specified for seed device
805 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
806 struct kobject *parent)
808 int error;
810 init_completion(&fs_devs->kobj_unregister);
811 fs_devs->fsid_kobj.kset = btrfs_kset;
812 error = kobject_init_and_add(&fs_devs->fsid_kobj,
813 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
814 if (error) {
815 kobject_put(&fs_devs->fsid_kobj);
816 return error;
819 return 0;
822 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
824 int error;
825 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
826 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
828 btrfs_set_fs_info_ptr(fs_info);
830 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
831 if (error)
832 return error;
834 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
835 if (error) {
836 btrfs_sysfs_rm_device_link(fs_devs, NULL);
837 return error;
840 error = sysfs_create_group(fsid_kobj,
841 &btrfs_feature_attr_group);
842 if (error)
843 goto failure;
845 error = addrm_unknown_feature_attrs(fs_info, true);
846 if (error)
847 goto failure;
849 fs_info->space_info_kobj = kobject_create_and_add("allocation",
850 fsid_kobj);
851 if (!fs_info->space_info_kobj) {
852 error = -ENOMEM;
853 goto failure;
856 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
857 if (error)
858 goto failure;
860 return 0;
861 failure:
862 btrfs_sysfs_remove_mounted(fs_info);
863 return error;
868 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
869 * values in superblock. Call after any changes to incompat/compat_ro flags
871 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
872 u64 bit, enum btrfs_feature_set set)
874 struct btrfs_fs_devices *fs_devs;
875 struct kobject *fsid_kobj;
876 u64 features;
877 int ret;
879 if (!fs_info)
880 return;
882 features = get_features(fs_info, set);
883 ASSERT(bit & supported_feature_masks[set]);
885 fs_devs = fs_info->fs_devices;
886 fsid_kobj = &fs_devs->fsid_kobj;
888 if (!fsid_kobj->state_initialized)
889 return;
892 * FIXME: this is too heavy to update just one value, ideally we'd like
893 * to use sysfs_update_group but some refactoring is needed first.
895 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
896 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
899 static int btrfs_init_debugfs(void)
901 #ifdef CONFIG_DEBUG_FS
902 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
903 if (!btrfs_debugfs_root_dentry)
904 return -ENOMEM;
907 * Example code, how to export data through debugfs.
909 * file: /sys/kernel/debug/btrfs/test
910 * contents of: btrfs_debugfs_test
912 #ifdef CONFIG_BTRFS_DEBUG
913 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
914 &btrfs_debugfs_test);
915 #endif
917 #endif
918 return 0;
921 int __init btrfs_init_sysfs(void)
923 int ret;
925 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
926 if (!btrfs_kset)
927 return -ENOMEM;
929 ret = btrfs_init_debugfs();
930 if (ret)
931 goto out1;
933 init_feature_attrs();
934 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
935 if (ret)
936 goto out2;
937 ret = sysfs_merge_group(&btrfs_kset->kobj,
938 &btrfs_static_feature_attr_group);
939 if (ret)
940 goto out_remove_group;
942 return 0;
944 out_remove_group:
945 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
946 out2:
947 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
948 out1:
949 kset_unregister(btrfs_kset);
951 return ret;
954 void __cold btrfs_exit_sysfs(void)
956 sysfs_unmerge_group(&btrfs_kset->kobj,
957 &btrfs_static_feature_attr_group);
958 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
959 kset_unregister(btrfs_kset);
960 debugfs_remove_recursive(btrfs_debugfs_root_dentry);