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/kobject.h>
12 #include <linux/bug.h>
16 #include "transaction.h"
19 #include "space-info.h"
20 #include "block-group.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
);
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
);
46 btrfs_set_super_incompat_flags(disk_super
, features
);
49 static int can_modify_feature(struct btrfs_feature_attr
*fa
)
53 switch (fa
->feature_set
) {
55 set
= BTRFS_FEATURE_COMPAT_SAFE_SET
;
56 clear
= BTRFS_FEATURE_COMPAT_SAFE_CLEAR
;
59 set
= BTRFS_FEATURE_COMPAT_RO_SAFE_SET
;
60 clear
= BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR
;
63 set
= BTRFS_FEATURE_INCOMPAT_SAFE_SET
;
64 clear
= BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR
;
67 pr_warn("btrfs: sysfs: unknown feature set %d\n",
72 if (set
& fa
->feature_bit
)
74 if (clear
& fa
->feature_bit
)
80 static ssize_t
btrfs_feature_attr_show(struct kobject
*kobj
,
81 struct kobj_attribute
*a
, char *buf
)
84 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
85 struct btrfs_feature_attr
*fa
= to_btrfs_feature_attr(a
);
87 u64 features
= get_features(fs_info
, fa
->feature_set
);
88 if (features
& fa
->feature_bit
)
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
;
106 fs_info
= to_fs_info(kobj
);
110 if (sb_rdonly(fs_info
->sb
))
113 ret
= kstrtoul(skip_spaces(buf
), 0, &val
);
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
;
124 set
= BTRFS_FEATURE_INCOMPAT_SAFE_SET
;
125 clear
= BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR
;
128 features
= get_features(fs_info
, fa
->feature_set
);
131 if ((val
&& (features
& fa
->feature_bit
)) ||
132 (!val
&& !(features
& fa
->feature_bit
)))
135 if ((val
&& !(set
& fa
->feature_bit
)) ||
136 (!val
&& !(clear
& fa
->feature_bit
))) {
138 "%sabling feature %s on mounted fs is not supported.",
139 val
? "En" : "Dis", fa
->kobj_attr
.attr
.name
);
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
);
149 features
|= fa
->feature_bit
;
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
);
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
;
171 struct btrfs_feature_attr
*fa
;
174 fa
= attr_to_btrfs_feature_attr(attr
);
175 features
= get_features(fs_info
, fa
->feature_set
);
177 if (can_modify_feature(fa
))
179 else if (!(features
& fa
->feature_bit
))
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_INCOMPAT(metadata_uuid
, METADATA_UUID
);
197 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree
, FREE_SPACE_TREE
);
199 static struct attribute
*btrfs_supported_feature_attrs
[] = {
200 BTRFS_FEAT_ATTR_PTR(mixed_backref
),
201 BTRFS_FEAT_ATTR_PTR(default_subvol
),
202 BTRFS_FEAT_ATTR_PTR(mixed_groups
),
203 BTRFS_FEAT_ATTR_PTR(compress_lzo
),
204 BTRFS_FEAT_ATTR_PTR(compress_zstd
),
205 BTRFS_FEAT_ATTR_PTR(big_metadata
),
206 BTRFS_FEAT_ATTR_PTR(extended_iref
),
207 BTRFS_FEAT_ATTR_PTR(raid56
),
208 BTRFS_FEAT_ATTR_PTR(skinny_metadata
),
209 BTRFS_FEAT_ATTR_PTR(no_holes
),
210 BTRFS_FEAT_ATTR_PTR(metadata_uuid
),
211 BTRFS_FEAT_ATTR_PTR(free_space_tree
),
216 * Features which depend on feature bits and may differ between each fs.
218 * /sys/fs/btrfs/features lists all available features of this kernel while
219 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
220 * can be changed online.
222 static const struct attribute_group btrfs_feature_attr_group
= {
224 .is_visible
= btrfs_feature_visible
,
225 .attrs
= btrfs_supported_feature_attrs
,
228 static ssize_t
rmdir_subvol_show(struct kobject
*kobj
,
229 struct kobj_attribute
*ka
, char *buf
)
231 return snprintf(buf
, PAGE_SIZE
, "0\n");
233 BTRFS_ATTR(static_feature
, rmdir_subvol
, rmdir_subvol_show
);
235 static struct attribute
*btrfs_supported_static_feature_attrs
[] = {
236 BTRFS_ATTR_PTR(static_feature
, rmdir_subvol
),
241 * Features which only depend on kernel version.
243 * These are listed in /sys/fs/btrfs/features along with
244 * btrfs_feature_attr_group
246 static const struct attribute_group btrfs_static_feature_attr_group
= {
248 .attrs
= btrfs_supported_static_feature_attrs
,
251 #ifdef CONFIG_BTRFS_DEBUG
254 * Runtime debugging exported via sysfs
256 * /sys/fs/btrfs/debug - applies to module or all filesystems
257 * /sys/fs/btrfs/UUID - applies only to the given filesystem
259 static struct attribute
*btrfs_debug_feature_attrs
[] = {
263 static const struct attribute_group btrfs_debug_feature_attr_group
= {
265 .attrs
= btrfs_debug_feature_attrs
,
270 static ssize_t
btrfs_show_u64(u64
*value_ptr
, spinlock_t
*lock
, char *buf
)
278 return snprintf(buf
, PAGE_SIZE
, "%llu\n", val
);
281 static ssize_t
global_rsv_size_show(struct kobject
*kobj
,
282 struct kobj_attribute
*ka
, char *buf
)
284 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
285 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
286 return btrfs_show_u64(&block_rsv
->size
, &block_rsv
->lock
, buf
);
288 BTRFS_ATTR(allocation
, global_rsv_size
, global_rsv_size_show
);
290 static ssize_t
global_rsv_reserved_show(struct kobject
*kobj
,
291 struct kobj_attribute
*a
, char *buf
)
293 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
294 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
295 return btrfs_show_u64(&block_rsv
->reserved
, &block_rsv
->lock
, buf
);
297 BTRFS_ATTR(allocation
, global_rsv_reserved
, global_rsv_reserved_show
);
299 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
300 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
302 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
303 struct kobj_attribute
*attr
, char *buf
);
304 BTRFS_ATTR(raid
, total_bytes
, raid_bytes_show
);
305 BTRFS_ATTR(raid
, used_bytes
, raid_bytes_show
);
307 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
308 struct kobj_attribute
*attr
, char *buf
)
311 struct btrfs_space_info
*sinfo
= to_space_info(kobj
->parent
);
312 struct btrfs_block_group_cache
*block_group
;
313 int index
= btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj
)->flags
);
316 down_read(&sinfo
->groups_sem
);
317 list_for_each_entry(block_group
, &sinfo
->block_groups
[index
], list
) {
318 if (&attr
->attr
== BTRFS_ATTR_PTR(raid
, total_bytes
))
319 val
+= block_group
->key
.offset
;
321 val
+= btrfs_block_group_used(&block_group
->item
);
323 up_read(&sinfo
->groups_sem
);
324 return snprintf(buf
, PAGE_SIZE
, "%llu\n", val
);
327 static struct attribute
*raid_attrs
[] = {
328 BTRFS_ATTR_PTR(raid
, total_bytes
),
329 BTRFS_ATTR_PTR(raid
, used_bytes
),
332 ATTRIBUTE_GROUPS(raid
);
334 static void release_raid_kobj(struct kobject
*kobj
)
336 kfree(to_raid_kobj(kobj
));
339 static struct kobj_type btrfs_raid_ktype
= {
340 .sysfs_ops
= &kobj_sysfs_ops
,
341 .release
= release_raid_kobj
,
342 .default_groups
= raid_groups
,
345 #define SPACE_INFO_ATTR(field) \
346 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
347 struct kobj_attribute *a, \
350 struct btrfs_space_info *sinfo = to_space_info(kobj); \
351 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
353 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
355 static ssize_t
btrfs_space_info_show_total_bytes_pinned(struct kobject
*kobj
,
356 struct kobj_attribute
*a
,
359 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
360 s64 val
= percpu_counter_sum(&sinfo
->total_bytes_pinned
);
361 return snprintf(buf
, PAGE_SIZE
, "%lld\n", val
);
364 SPACE_INFO_ATTR(flags
);
365 SPACE_INFO_ATTR(total_bytes
);
366 SPACE_INFO_ATTR(bytes_used
);
367 SPACE_INFO_ATTR(bytes_pinned
);
368 SPACE_INFO_ATTR(bytes_reserved
);
369 SPACE_INFO_ATTR(bytes_may_use
);
370 SPACE_INFO_ATTR(bytes_readonly
);
371 SPACE_INFO_ATTR(disk_used
);
372 SPACE_INFO_ATTR(disk_total
);
373 BTRFS_ATTR(space_info
, total_bytes_pinned
,
374 btrfs_space_info_show_total_bytes_pinned
);
376 static struct attribute
*space_info_attrs
[] = {
377 BTRFS_ATTR_PTR(space_info
, flags
),
378 BTRFS_ATTR_PTR(space_info
, total_bytes
),
379 BTRFS_ATTR_PTR(space_info
, bytes_used
),
380 BTRFS_ATTR_PTR(space_info
, bytes_pinned
),
381 BTRFS_ATTR_PTR(space_info
, bytes_reserved
),
382 BTRFS_ATTR_PTR(space_info
, bytes_may_use
),
383 BTRFS_ATTR_PTR(space_info
, bytes_readonly
),
384 BTRFS_ATTR_PTR(space_info
, disk_used
),
385 BTRFS_ATTR_PTR(space_info
, disk_total
),
386 BTRFS_ATTR_PTR(space_info
, total_bytes_pinned
),
389 ATTRIBUTE_GROUPS(space_info
);
391 static void space_info_release(struct kobject
*kobj
)
393 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
394 percpu_counter_destroy(&sinfo
->total_bytes_pinned
);
398 static struct kobj_type space_info_ktype
= {
399 .sysfs_ops
= &kobj_sysfs_ops
,
400 .release
= space_info_release
,
401 .default_groups
= space_info_groups
,
404 static const struct attribute
*allocation_attrs
[] = {
405 BTRFS_ATTR_PTR(allocation
, global_rsv_reserved
),
406 BTRFS_ATTR_PTR(allocation
, global_rsv_size
),
410 static ssize_t
btrfs_label_show(struct kobject
*kobj
,
411 struct kobj_attribute
*a
, char *buf
)
413 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
414 char *label
= fs_info
->super_copy
->label
;
417 spin_lock(&fs_info
->super_lock
);
418 ret
= snprintf(buf
, PAGE_SIZE
, label
[0] ? "%s\n" : "%s", label
);
419 spin_unlock(&fs_info
->super_lock
);
424 static ssize_t
btrfs_label_store(struct kobject
*kobj
,
425 struct kobj_attribute
*a
,
426 const char *buf
, size_t len
)
428 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
434 if (sb_rdonly(fs_info
->sb
))
438 * p_len is the len until the first occurrence of either
441 p_len
= strcspn(buf
, "\n");
443 if (p_len
>= BTRFS_LABEL_SIZE
)
446 spin_lock(&fs_info
->super_lock
);
447 memset(fs_info
->super_copy
->label
, 0, BTRFS_LABEL_SIZE
);
448 memcpy(fs_info
->super_copy
->label
, buf
, p_len
);
449 spin_unlock(&fs_info
->super_lock
);
452 * We don't want to do full transaction commit from inside sysfs
454 btrfs_set_pending(fs_info
, COMMIT
);
455 wake_up_process(fs_info
->transaction_kthread
);
459 BTRFS_ATTR_RW(, label
, btrfs_label_show
, btrfs_label_store
);
461 static ssize_t
btrfs_nodesize_show(struct kobject
*kobj
,
462 struct kobj_attribute
*a
, char *buf
)
464 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
466 return snprintf(buf
, PAGE_SIZE
, "%u\n", fs_info
->super_copy
->nodesize
);
469 BTRFS_ATTR(, nodesize
, btrfs_nodesize_show
);
471 static ssize_t
btrfs_sectorsize_show(struct kobject
*kobj
,
472 struct kobj_attribute
*a
, char *buf
)
474 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
476 return snprintf(buf
, PAGE_SIZE
, "%u\n",
477 fs_info
->super_copy
->sectorsize
);
480 BTRFS_ATTR(, sectorsize
, btrfs_sectorsize_show
);
482 static ssize_t
btrfs_clone_alignment_show(struct kobject
*kobj
,
483 struct kobj_attribute
*a
, char *buf
)
485 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
487 return snprintf(buf
, PAGE_SIZE
, "%u\n",
488 fs_info
->super_copy
->sectorsize
);
491 BTRFS_ATTR(, clone_alignment
, btrfs_clone_alignment_show
);
493 static ssize_t
quota_override_show(struct kobject
*kobj
,
494 struct kobj_attribute
*a
, char *buf
)
496 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
499 quota_override
= test_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
500 return snprintf(buf
, PAGE_SIZE
, "%d\n", quota_override
);
503 static ssize_t
quota_override_store(struct kobject
*kobj
,
504 struct kobj_attribute
*a
,
505 const char *buf
, size_t len
)
507 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
514 if (!capable(CAP_SYS_RESOURCE
))
517 err
= kstrtoul(buf
, 10, &knob
);
524 set_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
526 clear_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
531 BTRFS_ATTR_RW(, quota_override
, quota_override_show
, quota_override_store
);
533 static ssize_t
btrfs_metadata_uuid_show(struct kobject
*kobj
,
534 struct kobj_attribute
*a
, char *buf
)
536 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
538 return snprintf(buf
, PAGE_SIZE
, "%pU\n",
539 fs_info
->fs_devices
->metadata_uuid
);
542 BTRFS_ATTR(, metadata_uuid
, btrfs_metadata_uuid_show
);
544 static const struct attribute
*btrfs_attrs
[] = {
545 BTRFS_ATTR_PTR(, label
),
546 BTRFS_ATTR_PTR(, nodesize
),
547 BTRFS_ATTR_PTR(, sectorsize
),
548 BTRFS_ATTR_PTR(, clone_alignment
),
549 BTRFS_ATTR_PTR(, quota_override
),
550 BTRFS_ATTR_PTR(, metadata_uuid
),
554 static void btrfs_release_fsid_kobj(struct kobject
*kobj
)
556 struct btrfs_fs_devices
*fs_devs
= to_fs_devs(kobj
);
558 memset(&fs_devs
->fsid_kobj
, 0, sizeof(struct kobject
));
559 complete(&fs_devs
->kobj_unregister
);
562 static struct kobj_type btrfs_ktype
= {
563 .sysfs_ops
= &kobj_sysfs_ops
,
564 .release
= btrfs_release_fsid_kobj
,
567 static inline struct btrfs_fs_devices
*to_fs_devs(struct kobject
*kobj
)
569 if (kobj
->ktype
!= &btrfs_ktype
)
571 return container_of(kobj
, struct btrfs_fs_devices
, fsid_kobj
);
574 static inline struct btrfs_fs_info
*to_fs_info(struct kobject
*kobj
)
576 if (kobj
->ktype
!= &btrfs_ktype
)
578 return to_fs_devs(kobj
)->fs_info
;
581 #define NUM_FEATURE_BITS 64
582 #define BTRFS_FEATURE_NAME_MAX 13
583 static char btrfs_unknown_feature_names
[FEAT_MAX
][NUM_FEATURE_BITS
][BTRFS_FEATURE_NAME_MAX
];
584 static struct btrfs_feature_attr btrfs_feature_attrs
[FEAT_MAX
][NUM_FEATURE_BITS
];
586 static const u64 supported_feature_masks
[FEAT_MAX
] = {
587 [FEAT_COMPAT
] = BTRFS_FEATURE_COMPAT_SUPP
,
588 [FEAT_COMPAT_RO
] = BTRFS_FEATURE_COMPAT_RO_SUPP
,
589 [FEAT_INCOMPAT
] = BTRFS_FEATURE_INCOMPAT_SUPP
,
592 static int addrm_unknown_feature_attrs(struct btrfs_fs_info
*fs_info
, bool add
)
596 for (set
= 0; set
< FEAT_MAX
; set
++) {
598 struct attribute
*attrs
[2];
599 struct attribute_group agroup
= {
603 u64 features
= get_features(fs_info
, set
);
604 features
&= ~supported_feature_masks
[set
];
610 for (i
= 0; i
< NUM_FEATURE_BITS
; i
++) {
611 struct btrfs_feature_attr
*fa
;
613 if (!(features
& (1ULL << i
)))
616 fa
= &btrfs_feature_attrs
[set
][i
];
617 attrs
[0] = &fa
->kobj_attr
.attr
;
620 ret
= sysfs_merge_group(&fs_info
->fs_devices
->fsid_kobj
,
625 sysfs_unmerge_group(&fs_info
->fs_devices
->fsid_kobj
,
633 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
635 if (fs_devs
->device_dir_kobj
) {
636 kobject_del(fs_devs
->device_dir_kobj
);
637 kobject_put(fs_devs
->device_dir_kobj
);
638 fs_devs
->device_dir_kobj
= NULL
;
641 if (fs_devs
->fsid_kobj
.state_initialized
) {
642 kobject_del(&fs_devs
->fsid_kobj
);
643 kobject_put(&fs_devs
->fsid_kobj
);
644 wait_for_completion(&fs_devs
->kobj_unregister
);
648 /* when fs_devs is NULL it will remove all fsid kobject */
649 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
651 struct list_head
*fs_uuids
= btrfs_get_fs_uuids();
654 __btrfs_sysfs_remove_fsid(fs_devs
);
658 list_for_each_entry(fs_devs
, fs_uuids
, fs_list
) {
659 __btrfs_sysfs_remove_fsid(fs_devs
);
663 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info
*fs_info
)
665 btrfs_reset_fs_info_ptr(fs_info
);
667 if (fs_info
->space_info_kobj
) {
668 sysfs_remove_files(fs_info
->space_info_kobj
, allocation_attrs
);
669 kobject_del(fs_info
->space_info_kobj
);
670 kobject_put(fs_info
->space_info_kobj
);
672 addrm_unknown_feature_attrs(fs_info
, false);
673 sysfs_remove_group(&fs_info
->fs_devices
->fsid_kobj
, &btrfs_feature_attr_group
);
674 sysfs_remove_files(&fs_info
->fs_devices
->fsid_kobj
, btrfs_attrs
);
675 btrfs_sysfs_rm_device_link(fs_info
->fs_devices
, NULL
);
678 static const char * const btrfs_feature_set_names
[FEAT_MAX
] = {
679 [FEAT_COMPAT
] = "compat",
680 [FEAT_COMPAT_RO
] = "compat_ro",
681 [FEAT_INCOMPAT
] = "incompat",
684 const char * const btrfs_feature_set_name(enum btrfs_feature_set set
)
686 return btrfs_feature_set_names
[set
];
689 char *btrfs_printable_features(enum btrfs_feature_set set
, u64 flags
)
691 size_t bufsize
= 4096; /* safe max, 64 names * 64 bytes */
696 str
= kmalloc(bufsize
, GFP_KERNEL
);
700 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
703 if (!(flags
& (1ULL << i
)))
706 name
= btrfs_feature_attrs
[set
][i
].kobj_attr
.attr
.name
;
707 len
+= snprintf(str
+ len
, bufsize
- len
, "%s%s",
708 len
? "," : "", name
);
714 static void init_feature_attrs(void)
716 struct btrfs_feature_attr
*fa
;
719 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names
) !=
720 ARRAY_SIZE(btrfs_feature_attrs
));
721 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names
[0]) !=
722 ARRAY_SIZE(btrfs_feature_attrs
[0]));
724 memset(btrfs_feature_attrs
, 0, sizeof(btrfs_feature_attrs
));
725 memset(btrfs_unknown_feature_names
, 0,
726 sizeof(btrfs_unknown_feature_names
));
728 for (i
= 0; btrfs_supported_feature_attrs
[i
]; i
++) {
729 struct btrfs_feature_attr
*sfa
;
730 struct attribute
*a
= btrfs_supported_feature_attrs
[i
];
732 sfa
= attr_to_btrfs_feature_attr(a
);
733 bit
= ilog2(sfa
->feature_bit
);
734 fa
= &btrfs_feature_attrs
[sfa
->feature_set
][bit
];
736 fa
->kobj_attr
.attr
.name
= sfa
->kobj_attr
.attr
.name
;
739 for (set
= 0; set
< FEAT_MAX
; set
++) {
740 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
741 char *name
= btrfs_unknown_feature_names
[set
][i
];
742 fa
= &btrfs_feature_attrs
[set
][i
];
744 if (fa
->kobj_attr
.attr
.name
)
747 snprintf(name
, BTRFS_FEATURE_NAME_MAX
, "%s:%u",
748 btrfs_feature_set_names
[set
], i
);
750 fa
->kobj_attr
.attr
.name
= name
;
751 fa
->kobj_attr
.attr
.mode
= S_IRUGO
;
752 fa
->feature_set
= set
;
753 fa
->feature_bit
= 1ULL << i
;
759 * Create a sysfs entry for a given block group type at path
760 * /sys/fs/btrfs/UUID/allocation/data/TYPE
762 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache
*cache
)
764 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
765 struct btrfs_space_info
*space_info
= cache
->space_info
;
766 struct raid_kobject
*rkobj
;
767 const int index
= btrfs_bg_flags_to_raid_index(cache
->flags
);
768 unsigned int nofs_flag
;
772 * Setup a NOFS context because kobject_add(), deep in its call chain,
773 * does GFP_KERNEL allocations, and we are often called in a context
774 * where if reclaim is triggered we can deadlock (we are either holding
775 * a transaction handle or some lock required for a transaction
778 nofs_flag
= memalloc_nofs_save();
780 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
782 memalloc_nofs_restore(nofs_flag
);
783 btrfs_warn(cache
->fs_info
,
784 "couldn't alloc memory for raid level kobject");
788 rkobj
->flags
= cache
->flags
;
789 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
790 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
, "%s",
791 btrfs_bg_type_to_raid_name(rkobj
->flags
));
792 memalloc_nofs_restore(nofs_flag
);
794 kobject_put(&rkobj
->kobj
);
796 "failed to add kobject for block cache, ignoring");
800 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
803 static const char *alloc_name(u64 flags
)
806 case BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
:
808 case BTRFS_BLOCK_GROUP_METADATA
:
810 case BTRFS_BLOCK_GROUP_DATA
:
812 case BTRFS_BLOCK_GROUP_SYSTEM
:
816 return "invalid-combination";
821 * Create a sysfs entry for a space info type at path
822 * /sys/fs/btrfs/UUID/allocation/TYPE
824 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info
*fs_info
,
825 struct btrfs_space_info
*space_info
)
829 ret
= kobject_init_and_add(&space_info
->kobj
, &space_info_ktype
,
830 fs_info
->space_info_kobj
, "%s",
831 alloc_name(space_info
->flags
));
833 kobject_put(&space_info
->kobj
);
840 /* when one_device is NULL, it removes all device links */
842 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices
*fs_devices
,
843 struct btrfs_device
*one_device
)
845 struct hd_struct
*disk
;
846 struct kobject
*disk_kobj
;
848 if (!fs_devices
->device_dir_kobj
)
851 if (one_device
&& one_device
->bdev
) {
852 disk
= one_device
->bdev
->bd_part
;
853 disk_kobj
= &part_to_dev(disk
)->kobj
;
855 sysfs_remove_link(fs_devices
->device_dir_kobj
,
862 list_for_each_entry(one_device
,
863 &fs_devices
->devices
, dev_list
) {
864 if (!one_device
->bdev
)
866 disk
= one_device
->bdev
->bd_part
;
867 disk_kobj
= &part_to_dev(disk
)->kobj
;
869 sysfs_remove_link(fs_devices
->device_dir_kobj
,
876 int btrfs_sysfs_add_device(struct btrfs_fs_devices
*fs_devs
)
878 if (!fs_devs
->device_dir_kobj
)
879 fs_devs
->device_dir_kobj
= kobject_create_and_add("devices",
880 &fs_devs
->fsid_kobj
);
882 if (!fs_devs
->device_dir_kobj
)
888 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices
*fs_devices
,
889 struct btrfs_device
*one_device
)
892 struct btrfs_device
*dev
;
894 list_for_each_entry(dev
, &fs_devices
->devices
, dev_list
) {
895 struct hd_struct
*disk
;
896 struct kobject
*disk_kobj
;
901 if (one_device
&& one_device
!= dev
)
904 disk
= dev
->bdev
->bd_part
;
905 disk_kobj
= &part_to_dev(disk
)->kobj
;
907 error
= sysfs_create_link(fs_devices
->device_dir_kobj
,
908 disk_kobj
, disk_kobj
->name
);
916 /* /sys/fs/btrfs/ entry */
917 static struct kset
*btrfs_kset
;
920 * Can be called by the device discovery thread.
921 * And parent can be specified for seed device
923 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices
*fs_devs
,
924 struct kobject
*parent
)
928 init_completion(&fs_devs
->kobj_unregister
);
929 fs_devs
->fsid_kobj
.kset
= btrfs_kset
;
930 error
= kobject_init_and_add(&fs_devs
->fsid_kobj
,
931 &btrfs_ktype
, parent
, "%pU", fs_devs
->fsid
);
933 kobject_put(&fs_devs
->fsid_kobj
);
940 int btrfs_sysfs_add_mounted(struct btrfs_fs_info
*fs_info
)
943 struct btrfs_fs_devices
*fs_devs
= fs_info
->fs_devices
;
944 struct kobject
*fsid_kobj
= &fs_devs
->fsid_kobj
;
946 btrfs_set_fs_info_ptr(fs_info
);
948 error
= btrfs_sysfs_add_device_link(fs_devs
, NULL
);
952 error
= sysfs_create_files(fsid_kobj
, btrfs_attrs
);
954 btrfs_sysfs_rm_device_link(fs_devs
, NULL
);
958 error
= sysfs_create_group(fsid_kobj
,
959 &btrfs_feature_attr_group
);
963 #ifdef CONFIG_BTRFS_DEBUG
964 error
= sysfs_create_group(fsid_kobj
,
965 &btrfs_debug_feature_attr_group
);
970 error
= addrm_unknown_feature_attrs(fs_info
, true);
974 fs_info
->space_info_kobj
= kobject_create_and_add("allocation",
976 if (!fs_info
->space_info_kobj
) {
981 error
= sysfs_create_files(fs_info
->space_info_kobj
, allocation_attrs
);
987 btrfs_sysfs_remove_mounted(fs_info
);
993 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
994 * values in superblock. Call after any changes to incompat/compat_ro flags
996 void btrfs_sysfs_feature_update(struct btrfs_fs_info
*fs_info
,
997 u64 bit
, enum btrfs_feature_set set
)
999 struct btrfs_fs_devices
*fs_devs
;
1000 struct kobject
*fsid_kobj
;
1007 features
= get_features(fs_info
, set
);
1008 ASSERT(bit
& supported_feature_masks
[set
]);
1010 fs_devs
= fs_info
->fs_devices
;
1011 fsid_kobj
= &fs_devs
->fsid_kobj
;
1013 if (!fsid_kobj
->state_initialized
)
1017 * FIXME: this is too heavy to update just one value, ideally we'd like
1018 * to use sysfs_update_group but some refactoring is needed first.
1020 sysfs_remove_group(fsid_kobj
, &btrfs_feature_attr_group
);
1021 ret
= sysfs_create_group(fsid_kobj
, &btrfs_feature_attr_group
);
1024 int __init
btrfs_init_sysfs(void)
1028 btrfs_kset
= kset_create_and_add("btrfs", NULL
, fs_kobj
);
1032 init_feature_attrs();
1033 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1036 ret
= sysfs_merge_group(&btrfs_kset
->kobj
,
1037 &btrfs_static_feature_attr_group
);
1039 goto out_remove_group
;
1041 #ifdef CONFIG_BTRFS_DEBUG
1042 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_debug_feature_attr_group
);
1050 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1052 kset_unregister(btrfs_kset
);
1057 void __cold
btrfs_exit_sysfs(void)
1059 sysfs_unmerge_group(&btrfs_kset
->kobj
,
1060 &btrfs_static_feature_attr_group
);
1061 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1062 kset_unregister(btrfs_kset
);