Linux 4.18.10
[linux/fpc-iii.git] / fs / btrfs / sysfs.c
blob4a4e960c7c665f56a9f418b2806d5a2904e0eb42
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/buffer_head.h>
11 #include <linux/kobject.h>
12 #include <linux/bug.h>
13 #include <linux/genhd.h>
14 #include <linux/debugfs.h>
16 #include "ctree.h"
17 #include "disk-io.h"
18 #include "transaction.h"
19 #include "sysfs.h"
20 #include "volumes.h"
22 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
23 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
25 static u64 get_features(struct btrfs_fs_info *fs_info,
26 enum btrfs_feature_set set)
28 struct btrfs_super_block *disk_super = fs_info->super_copy;
29 if (set == FEAT_COMPAT)
30 return btrfs_super_compat_flags(disk_super);
31 else if (set == FEAT_COMPAT_RO)
32 return btrfs_super_compat_ro_flags(disk_super);
33 else
34 return btrfs_super_incompat_flags(disk_super);
37 static void set_features(struct btrfs_fs_info *fs_info,
38 enum btrfs_feature_set set, u64 features)
40 struct btrfs_super_block *disk_super = fs_info->super_copy;
41 if (set == FEAT_COMPAT)
42 btrfs_set_super_compat_flags(disk_super, features);
43 else if (set == FEAT_COMPAT_RO)
44 btrfs_set_super_compat_ro_flags(disk_super, features);
45 else
46 btrfs_set_super_incompat_flags(disk_super, features);
49 static int can_modify_feature(struct btrfs_feature_attr *fa)
51 int val = 0;
52 u64 set, clear;
53 switch (fa->feature_set) {
54 case FEAT_COMPAT:
55 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
56 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
57 break;
58 case FEAT_COMPAT_RO:
59 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
60 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
61 break;
62 case FEAT_INCOMPAT:
63 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
64 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
65 break;
66 default:
67 pr_warn("btrfs: sysfs: unknown feature set %d\n",
68 fa->feature_set);
69 return 0;
72 if (set & fa->feature_bit)
73 val |= 1;
74 if (clear & fa->feature_bit)
75 val |= 2;
77 return val;
80 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
81 struct kobj_attribute *a, char *buf)
83 int val = 0;
84 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
85 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
86 if (fs_info) {
87 u64 features = get_features(fs_info, fa->feature_set);
88 if (features & fa->feature_bit)
89 val = 1;
90 } else
91 val = can_modify_feature(fa);
93 return snprintf(buf, PAGE_SIZE, "%d\n", val);
96 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
97 struct kobj_attribute *a,
98 const char *buf, size_t count)
100 struct btrfs_fs_info *fs_info;
101 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
102 u64 features, set, clear;
103 unsigned long val;
104 int ret;
106 fs_info = to_fs_info(kobj);
107 if (!fs_info)
108 return -EPERM;
110 if (sb_rdonly(fs_info->sb))
111 return -EROFS;
113 ret = kstrtoul(skip_spaces(buf), 0, &val);
114 if (ret)
115 return ret;
117 if (fa->feature_set == FEAT_COMPAT) {
118 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
119 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
120 } else if (fa->feature_set == FEAT_COMPAT_RO) {
121 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
122 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
123 } else {
124 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
125 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
128 features = get_features(fs_info, fa->feature_set);
130 /* Nothing to do */
131 if ((val && (features & fa->feature_bit)) ||
132 (!val && !(features & fa->feature_bit)))
133 return count;
135 if ((val && !(set & fa->feature_bit)) ||
136 (!val && !(clear & fa->feature_bit))) {
137 btrfs_info(fs_info,
138 "%sabling feature %s on mounted fs is not supported.",
139 val ? "En" : "Dis", fa->kobj_attr.attr.name);
140 return -EPERM;
143 btrfs_info(fs_info, "%s %s feature flag",
144 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
146 spin_lock(&fs_info->super_lock);
147 features = get_features(fs_info, fa->feature_set);
148 if (val)
149 features |= fa->feature_bit;
150 else
151 features &= ~fa->feature_bit;
152 set_features(fs_info, fa->feature_set, features);
153 spin_unlock(&fs_info->super_lock);
156 * We don't want to do full transaction commit from inside sysfs
158 btrfs_set_pending(fs_info, COMMIT);
159 wake_up_process(fs_info->transaction_kthread);
161 return count;
164 static umode_t btrfs_feature_visible(struct kobject *kobj,
165 struct attribute *attr, int unused)
167 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
168 umode_t mode = attr->mode;
170 if (fs_info) {
171 struct btrfs_feature_attr *fa;
172 u64 features;
174 fa = attr_to_btrfs_feature_attr(attr);
175 features = get_features(fs_info, fa->feature_set);
177 if (can_modify_feature(fa))
178 mode |= S_IWUSR;
179 else if (!(features & fa->feature_bit))
180 mode = 0;
183 return mode;
186 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
187 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
188 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
189 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
190 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
191 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
192 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
193 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
194 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
195 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
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(free_space_tree),
210 NULL
214 * Features which depend on feature bits and may differ between each fs.
216 * /sys/fs/btrfs/features lists all available features of this kernel while
217 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
218 * can be changed online.
220 static const struct attribute_group btrfs_feature_attr_group = {
221 .name = "features",
222 .is_visible = btrfs_feature_visible,
223 .attrs = btrfs_supported_feature_attrs,
226 static ssize_t rmdir_subvol_show(struct kobject *kobj,
227 struct kobj_attribute *ka, char *buf)
229 return snprintf(buf, PAGE_SIZE, "0\n");
231 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
233 static struct attribute *btrfs_supported_static_feature_attrs[] = {
234 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
235 NULL
239 * Features which only depend on kernel version.
241 * These are listed in /sys/fs/btrfs/features along with
242 * btrfs_feature_attr_group
244 static const struct attribute_group btrfs_static_feature_attr_group = {
245 .name = "features",
246 .attrs = btrfs_supported_static_feature_attrs,
249 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
251 u64 val;
252 if (lock)
253 spin_lock(lock);
254 val = *value_ptr;
255 if (lock)
256 spin_unlock(lock);
257 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
260 static ssize_t global_rsv_size_show(struct kobject *kobj,
261 struct kobj_attribute *ka, char *buf)
263 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
264 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
265 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
267 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
269 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
270 struct kobj_attribute *a, char *buf)
272 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
273 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
274 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
276 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
278 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
279 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
281 static ssize_t raid_bytes_show(struct kobject *kobj,
282 struct kobj_attribute *attr, char *buf);
283 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
284 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
286 static ssize_t raid_bytes_show(struct kobject *kobj,
287 struct kobj_attribute *attr, char *buf)
290 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
291 struct btrfs_block_group_cache *block_group;
292 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
293 u64 val = 0;
295 down_read(&sinfo->groups_sem);
296 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
297 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
298 val += block_group->key.offset;
299 else
300 val += btrfs_block_group_used(&block_group->item);
302 up_read(&sinfo->groups_sem);
303 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
306 static struct attribute *raid_attributes[] = {
307 BTRFS_ATTR_PTR(raid, total_bytes),
308 BTRFS_ATTR_PTR(raid, used_bytes),
309 NULL
312 static void release_raid_kobj(struct kobject *kobj)
314 kfree(to_raid_kobj(kobj));
317 struct kobj_type btrfs_raid_ktype = {
318 .sysfs_ops = &kobj_sysfs_ops,
319 .release = release_raid_kobj,
320 .default_attrs = raid_attributes,
323 #define SPACE_INFO_ATTR(field) \
324 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
325 struct kobj_attribute *a, \
326 char *buf) \
328 struct btrfs_space_info *sinfo = to_space_info(kobj); \
329 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
331 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
333 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
334 struct kobj_attribute *a,
335 char *buf)
337 struct btrfs_space_info *sinfo = to_space_info(kobj);
338 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
339 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
342 SPACE_INFO_ATTR(flags);
343 SPACE_INFO_ATTR(total_bytes);
344 SPACE_INFO_ATTR(bytes_used);
345 SPACE_INFO_ATTR(bytes_pinned);
346 SPACE_INFO_ATTR(bytes_reserved);
347 SPACE_INFO_ATTR(bytes_may_use);
348 SPACE_INFO_ATTR(bytes_readonly);
349 SPACE_INFO_ATTR(disk_used);
350 SPACE_INFO_ATTR(disk_total);
351 BTRFS_ATTR(space_info, total_bytes_pinned,
352 btrfs_space_info_show_total_bytes_pinned);
354 static struct attribute *space_info_attrs[] = {
355 BTRFS_ATTR_PTR(space_info, flags),
356 BTRFS_ATTR_PTR(space_info, total_bytes),
357 BTRFS_ATTR_PTR(space_info, bytes_used),
358 BTRFS_ATTR_PTR(space_info, bytes_pinned),
359 BTRFS_ATTR_PTR(space_info, bytes_reserved),
360 BTRFS_ATTR_PTR(space_info, bytes_may_use),
361 BTRFS_ATTR_PTR(space_info, bytes_readonly),
362 BTRFS_ATTR_PTR(space_info, disk_used),
363 BTRFS_ATTR_PTR(space_info, disk_total),
364 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
365 NULL,
368 static void space_info_release(struct kobject *kobj)
370 struct btrfs_space_info *sinfo = to_space_info(kobj);
371 percpu_counter_destroy(&sinfo->total_bytes_pinned);
372 kfree(sinfo);
375 struct kobj_type space_info_ktype = {
376 .sysfs_ops = &kobj_sysfs_ops,
377 .release = space_info_release,
378 .default_attrs = space_info_attrs,
381 static const struct attribute *allocation_attrs[] = {
382 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
383 BTRFS_ATTR_PTR(allocation, global_rsv_size),
384 NULL,
387 static ssize_t btrfs_label_show(struct kobject *kobj,
388 struct kobj_attribute *a, char *buf)
390 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
391 char *label = fs_info->super_copy->label;
392 ssize_t ret;
394 spin_lock(&fs_info->super_lock);
395 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
396 spin_unlock(&fs_info->super_lock);
398 return ret;
401 static ssize_t btrfs_label_store(struct kobject *kobj,
402 struct kobj_attribute *a,
403 const char *buf, size_t len)
405 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
406 size_t p_len;
408 if (!fs_info)
409 return -EPERM;
411 if (sb_rdonly(fs_info->sb))
412 return -EROFS;
415 * p_len is the len until the first occurrence of either
416 * '\n' or '\0'
418 p_len = strcspn(buf, "\n");
420 if (p_len >= BTRFS_LABEL_SIZE)
421 return -EINVAL;
423 spin_lock(&fs_info->super_lock);
424 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
425 memcpy(fs_info->super_copy->label, buf, p_len);
426 spin_unlock(&fs_info->super_lock);
429 * We don't want to do full transaction commit from inside sysfs
431 btrfs_set_pending(fs_info, COMMIT);
432 wake_up_process(fs_info->transaction_kthread);
434 return len;
436 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
438 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
439 struct kobj_attribute *a, char *buf)
441 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
443 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
446 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
448 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
449 struct kobj_attribute *a, char *buf)
451 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
453 return snprintf(buf, PAGE_SIZE, "%u\n",
454 fs_info->super_copy->sectorsize);
457 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
459 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
460 struct kobj_attribute *a, char *buf)
462 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
464 return snprintf(buf, PAGE_SIZE, "%u\n",
465 fs_info->super_copy->sectorsize);
468 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
470 static ssize_t quota_override_show(struct kobject *kobj,
471 struct kobj_attribute *a, char *buf)
473 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
474 int quota_override;
476 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
477 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
480 static ssize_t quota_override_store(struct kobject *kobj,
481 struct kobj_attribute *a,
482 const char *buf, size_t len)
484 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
485 unsigned long knob;
486 int err;
488 if (!fs_info)
489 return -EPERM;
491 if (!capable(CAP_SYS_RESOURCE))
492 return -EPERM;
494 err = kstrtoul(buf, 10, &knob);
495 if (err)
496 return err;
497 if (knob > 1)
498 return -EINVAL;
500 if (knob)
501 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
502 else
503 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
505 return len;
508 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
510 static const struct attribute *btrfs_attrs[] = {
511 BTRFS_ATTR_PTR(, label),
512 BTRFS_ATTR_PTR(, nodesize),
513 BTRFS_ATTR_PTR(, sectorsize),
514 BTRFS_ATTR_PTR(, clone_alignment),
515 BTRFS_ATTR_PTR(, quota_override),
516 NULL,
519 static void btrfs_release_fsid_kobj(struct kobject *kobj)
521 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
523 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
524 complete(&fs_devs->kobj_unregister);
527 static struct kobj_type btrfs_ktype = {
528 .sysfs_ops = &kobj_sysfs_ops,
529 .release = btrfs_release_fsid_kobj,
532 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
534 if (kobj->ktype != &btrfs_ktype)
535 return NULL;
536 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
539 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
541 if (kobj->ktype != &btrfs_ktype)
542 return NULL;
543 return to_fs_devs(kobj)->fs_info;
546 #define NUM_FEATURE_BITS 64
547 #define BTRFS_FEATURE_NAME_MAX 13
548 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
549 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
551 static const u64 supported_feature_masks[FEAT_MAX] = {
552 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
553 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
554 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
557 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
559 int set;
561 for (set = 0; set < FEAT_MAX; set++) {
562 int i;
563 struct attribute *attrs[2];
564 struct attribute_group agroup = {
565 .name = "features",
566 .attrs = attrs,
568 u64 features = get_features(fs_info, set);
569 features &= ~supported_feature_masks[set];
571 if (!features)
572 continue;
574 attrs[1] = NULL;
575 for (i = 0; i < NUM_FEATURE_BITS; i++) {
576 struct btrfs_feature_attr *fa;
578 if (!(features & (1ULL << i)))
579 continue;
581 fa = &btrfs_feature_attrs[set][i];
582 attrs[0] = &fa->kobj_attr.attr;
583 if (add) {
584 int ret;
585 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
586 &agroup);
587 if (ret)
588 return ret;
589 } else
590 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
591 &agroup);
595 return 0;
598 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
600 if (fs_devs->device_dir_kobj) {
601 kobject_del(fs_devs->device_dir_kobj);
602 kobject_put(fs_devs->device_dir_kobj);
603 fs_devs->device_dir_kobj = NULL;
606 if (fs_devs->fsid_kobj.state_initialized) {
607 kobject_del(&fs_devs->fsid_kobj);
608 kobject_put(&fs_devs->fsid_kobj);
609 wait_for_completion(&fs_devs->kobj_unregister);
613 /* when fs_devs is NULL it will remove all fsid kobject */
614 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
616 struct list_head *fs_uuids = btrfs_get_fs_uuids();
618 if (fs_devs) {
619 __btrfs_sysfs_remove_fsid(fs_devs);
620 return;
623 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
624 __btrfs_sysfs_remove_fsid(fs_devs);
628 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
630 btrfs_reset_fs_info_ptr(fs_info);
632 if (fs_info->space_info_kobj) {
633 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
634 kobject_del(fs_info->space_info_kobj);
635 kobject_put(fs_info->space_info_kobj);
637 addrm_unknown_feature_attrs(fs_info, false);
638 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
639 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
640 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
643 const char * const btrfs_feature_set_names[FEAT_MAX] = {
644 [FEAT_COMPAT] = "compat",
645 [FEAT_COMPAT_RO] = "compat_ro",
646 [FEAT_INCOMPAT] = "incompat",
649 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
651 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
652 int len = 0;
653 int i;
654 char *str;
656 str = kmalloc(bufsize, GFP_KERNEL);
657 if (!str)
658 return str;
660 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
661 const char *name;
663 if (!(flags & (1ULL << i)))
664 continue;
666 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
667 len += snprintf(str + len, bufsize - len, "%s%s",
668 len ? "," : "", name);
671 return str;
674 static void init_feature_attrs(void)
676 struct btrfs_feature_attr *fa;
677 int set, i;
679 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
680 ARRAY_SIZE(btrfs_feature_attrs));
681 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
682 ARRAY_SIZE(btrfs_feature_attrs[0]));
684 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
685 memset(btrfs_unknown_feature_names, 0,
686 sizeof(btrfs_unknown_feature_names));
688 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
689 struct btrfs_feature_attr *sfa;
690 struct attribute *a = btrfs_supported_feature_attrs[i];
691 int bit;
692 sfa = attr_to_btrfs_feature_attr(a);
693 bit = ilog2(sfa->feature_bit);
694 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
696 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
699 for (set = 0; set < FEAT_MAX; set++) {
700 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
701 char *name = btrfs_unknown_feature_names[set][i];
702 fa = &btrfs_feature_attrs[set][i];
704 if (fa->kobj_attr.attr.name)
705 continue;
707 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
708 btrfs_feature_set_names[set], i);
710 fa->kobj_attr.attr.name = name;
711 fa->kobj_attr.attr.mode = S_IRUGO;
712 fa->feature_set = set;
713 fa->feature_bit = 1ULL << i;
718 /* when one_device is NULL, it removes all device links */
720 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
721 struct btrfs_device *one_device)
723 struct hd_struct *disk;
724 struct kobject *disk_kobj;
726 if (!fs_devices->device_dir_kobj)
727 return -EINVAL;
729 if (one_device && one_device->bdev) {
730 disk = one_device->bdev->bd_part;
731 disk_kobj = &part_to_dev(disk)->kobj;
733 sysfs_remove_link(fs_devices->device_dir_kobj,
734 disk_kobj->name);
737 if (one_device)
738 return 0;
740 list_for_each_entry(one_device,
741 &fs_devices->devices, dev_list) {
742 if (!one_device->bdev)
743 continue;
744 disk = one_device->bdev->bd_part;
745 disk_kobj = &part_to_dev(disk)->kobj;
747 sysfs_remove_link(fs_devices->device_dir_kobj,
748 disk_kobj->name);
751 return 0;
754 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
756 if (!fs_devs->device_dir_kobj)
757 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
758 &fs_devs->fsid_kobj);
760 if (!fs_devs->device_dir_kobj)
761 return -ENOMEM;
763 return 0;
766 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
767 struct btrfs_device *one_device)
769 int error = 0;
770 struct btrfs_device *dev;
772 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
773 struct hd_struct *disk;
774 struct kobject *disk_kobj;
776 if (!dev->bdev)
777 continue;
779 if (one_device && one_device != dev)
780 continue;
782 disk = dev->bdev->bd_part;
783 disk_kobj = &part_to_dev(disk)->kobj;
785 error = sysfs_create_link(fs_devices->device_dir_kobj,
786 disk_kobj, disk_kobj->name);
787 if (error)
788 break;
791 return error;
794 /* /sys/fs/btrfs/ entry */
795 static struct kset *btrfs_kset;
797 /* /sys/kernel/debug/btrfs */
798 static struct dentry *btrfs_debugfs_root_dentry;
800 /* Debugging tunables and exported data */
801 u64 btrfs_debugfs_test;
804 * Can be called by the device discovery thread.
805 * And parent can be specified for seed device
807 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
808 struct kobject *parent)
810 int error;
812 init_completion(&fs_devs->kobj_unregister);
813 fs_devs->fsid_kobj.kset = btrfs_kset;
814 error = kobject_init_and_add(&fs_devs->fsid_kobj,
815 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
816 return error;
819 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
821 int error;
822 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
823 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
825 btrfs_set_fs_info_ptr(fs_info);
827 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
828 if (error)
829 return error;
831 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
832 if (error) {
833 btrfs_sysfs_rm_device_link(fs_devs, NULL);
834 return error;
837 error = sysfs_create_group(fsid_kobj,
838 &btrfs_feature_attr_group);
839 if (error)
840 goto failure;
842 error = addrm_unknown_feature_attrs(fs_info, true);
843 if (error)
844 goto failure;
846 fs_info->space_info_kobj = kobject_create_and_add("allocation",
847 fsid_kobj);
848 if (!fs_info->space_info_kobj) {
849 error = -ENOMEM;
850 goto failure;
853 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
854 if (error)
855 goto failure;
857 return 0;
858 failure:
859 btrfs_sysfs_remove_mounted(fs_info);
860 return error;
865 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
866 * values in superblock. Call after any changes to incompat/compat_ro flags
868 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
869 u64 bit, enum btrfs_feature_set set)
871 struct btrfs_fs_devices *fs_devs;
872 struct kobject *fsid_kobj;
873 u64 features;
874 int ret;
876 if (!fs_info)
877 return;
879 features = get_features(fs_info, set);
880 ASSERT(bit & supported_feature_masks[set]);
882 fs_devs = fs_info->fs_devices;
883 fsid_kobj = &fs_devs->fsid_kobj;
885 if (!fsid_kobj->state_initialized)
886 return;
889 * FIXME: this is too heavy to update just one value, ideally we'd like
890 * to use sysfs_update_group but some refactoring is needed first.
892 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
893 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
896 static int btrfs_init_debugfs(void)
898 #ifdef CONFIG_DEBUG_FS
899 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
900 if (!btrfs_debugfs_root_dentry)
901 return -ENOMEM;
904 * Example code, how to export data through debugfs.
906 * file: /sys/kernel/debug/btrfs/test
907 * contents of: btrfs_debugfs_test
909 #ifdef CONFIG_BTRFS_DEBUG
910 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
911 &btrfs_debugfs_test);
912 #endif
914 #endif
915 return 0;
918 int __init btrfs_init_sysfs(void)
920 int ret;
922 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
923 if (!btrfs_kset)
924 return -ENOMEM;
926 ret = btrfs_init_debugfs();
927 if (ret)
928 goto out1;
930 init_feature_attrs();
931 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
932 if (ret)
933 goto out2;
934 ret = sysfs_merge_group(&btrfs_kset->kobj,
935 &btrfs_static_feature_attr_group);
936 if (ret)
937 goto out_remove_group;
939 return 0;
941 out_remove_group:
942 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
943 out2:
944 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
945 out1:
946 kset_unregister(btrfs_kset);
948 return ret;
951 void __cold btrfs_exit_sysfs(void)
953 sysfs_unmerge_group(&btrfs_kset->kobj,
954 &btrfs_static_feature_attr_group);
955 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
956 kset_unregister(btrfs_kset);
957 debugfs_remove_recursive(btrfs_debugfs_root_dentry);