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>
12 #include <crypto/hash.h>
18 #include "transaction.h"
21 #include "space-info.h"
22 #include "block-group.h"
28 * --------------------------------------------------------------------------
29 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
30 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and
31 * /sys/fs/btrfs/<uuid>/features
32 * btrfs_attrs /sys/fs/btrfs/<uuid>
33 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid>
34 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation
35 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
36 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>
37 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
38 * discard_attrs /sys/fs/btrfs/<uuid>/discard
40 * When built with BTRFS_CONFIG_DEBUG:
42 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
43 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
46 struct btrfs_feature_attr {
47 struct kobj_attribute kobj_attr;
48 enum btrfs_feature_set feature_set;
52 /* For raid type sysfs entries */
58 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
60 .attr = { .name = __stringify(_name), .mode = _mode }, \
65 #define BTRFS_ATTR_W(_prefix, _name, _store) \
66 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
67 __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
69 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
70 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
71 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
73 #define BTRFS_ATTR(_prefix, _name, _show) \
74 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
75 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
77 #define BTRFS_ATTR_PTR(_prefix, _name) \
78 (&btrfs_attr_##_prefix##_##_name.attr)
80 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
81 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
82 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
83 btrfs_feature_attr_show, \
84 btrfs_feature_attr_store), \
85 .feature_set = _feature_set, \
86 .feature_bit = _feature_prefix ##_## _feature_bit, \
88 #define BTRFS_FEAT_ATTR_PTR(_name) \
89 (&btrfs_attr_features_##_name.kobj_attr.attr)
91 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
92 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
93 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
94 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
95 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
96 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
98 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
99 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
100 static struct kobject *get_btrfs_kobj(struct kobject *kobj);
102 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
104 return container_of(a, struct btrfs_feature_attr, kobj_attr);
107 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
109 return container_of(attr, struct kobj_attribute, attr);
112 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
113 struct attribute *attr)
115 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
118 static u64 get_features(struct btrfs_fs_info *fs_info,
119 enum btrfs_feature_set set)
121 struct btrfs_super_block *disk_super = fs_info->super_copy;
122 if (set == FEAT_COMPAT)
123 return btrfs_super_compat_flags(disk_super);
124 else if (set == FEAT_COMPAT_RO)
125 return btrfs_super_compat_ro_flags(disk_super);
127 return btrfs_super_incompat_flags(disk_super);
130 static void set_features(struct btrfs_fs_info *fs_info,
131 enum btrfs_feature_set set, u64 features)
133 struct btrfs_super_block *disk_super = fs_info->super_copy;
134 if (set == FEAT_COMPAT)
135 btrfs_set_super_compat_flags(disk_super, features);
136 else if (set == FEAT_COMPAT_RO)
137 btrfs_set_super_compat_ro_flags(disk_super, features);
139 btrfs_set_super_incompat_flags(disk_super, features);
142 static int can_modify_feature(struct btrfs_feature_attr *fa)
146 switch (fa->feature_set) {
148 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
149 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
152 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
153 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
156 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
157 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
160 pr_warn("btrfs: sysfs: unknown feature set %d\n",
165 if (set & fa->feature_bit)
167 if (clear & fa->feature_bit)
173 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
174 struct kobj_attribute *a, char *buf)
177 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
178 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
180 u64 features = get_features(fs_info, fa->feature_set);
181 if (features & fa->feature_bit)
184 val = can_modify_feature(fa);
186 return sysfs_emit(buf, "%d\n", val);
189 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
190 struct kobj_attribute *a,
191 const char *buf, size_t count)
193 struct btrfs_fs_info *fs_info;
194 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
195 u64 features, set, clear;
199 fs_info = to_fs_info(kobj);
203 if (sb_rdonly(fs_info->sb))
206 ret = kstrtoul(skip_spaces(buf), 0, &val);
210 if (fa->feature_set == FEAT_COMPAT) {
211 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
212 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
213 } else if (fa->feature_set == FEAT_COMPAT_RO) {
214 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
215 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
217 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
218 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
221 features = get_features(fs_info, fa->feature_set);
224 if ((val && (features & fa->feature_bit)) ||
225 (!val && !(features & fa->feature_bit)))
228 if ((val && !(set & fa->feature_bit)) ||
229 (!val && !(clear & fa->feature_bit))) {
231 "%sabling feature %s on mounted fs is not supported.",
232 val ? "En" : "Dis", fa->kobj_attr.attr.name);
236 btrfs_info(fs_info, "%s %s feature flag",
237 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
239 spin_lock(&fs_info->super_lock);
240 features = get_features(fs_info, fa->feature_set);
242 features |= fa->feature_bit;
244 features &= ~fa->feature_bit;
245 set_features(fs_info, fa->feature_set, features);
246 spin_unlock(&fs_info->super_lock);
249 * We don't want to do full transaction commit from inside sysfs
251 btrfs_set_pending(fs_info, COMMIT);
252 wake_up_process(fs_info->transaction_kthread);
257 static umode_t btrfs_feature_visible(struct kobject *kobj,
258 struct attribute *attr, int unused)
260 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
261 umode_t mode = attr->mode;
264 struct btrfs_feature_attr *fa;
267 fa = attr_to_btrfs_feature_attr(attr);
268 features = get_features(fs_info, fa->feature_set);
270 if (can_modify_feature(fa))
272 else if (!(features & fa->feature_bit))
279 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
280 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
281 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
282 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
283 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
284 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
285 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
286 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
287 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
288 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
289 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
290 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
291 #ifdef CONFIG_BLK_DEV_ZONED
292 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
294 #ifdef CONFIG_BTRFS_DEBUG
295 /* Remove once support for extent tree v2 is feature complete */
296 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
298 #ifdef CONFIG_FS_VERITY
299 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
303 * Features which depend on feature bits and may differ between each fs.
305 * /sys/fs/btrfs/features - all available features implemented by this version
306 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
307 * can be changed on a mounted filesystem.
309 static struct attribute *btrfs_supported_feature_attrs[] = {
310 BTRFS_FEAT_ATTR_PTR(default_subvol),
311 BTRFS_FEAT_ATTR_PTR(mixed_groups),
312 BTRFS_FEAT_ATTR_PTR(compress_lzo),
313 BTRFS_FEAT_ATTR_PTR(compress_zstd),
314 BTRFS_FEAT_ATTR_PTR(extended_iref),
315 BTRFS_FEAT_ATTR_PTR(raid56),
316 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
317 BTRFS_FEAT_ATTR_PTR(no_holes),
318 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
319 BTRFS_FEAT_ATTR_PTR(free_space_tree),
320 BTRFS_FEAT_ATTR_PTR(raid1c34),
321 BTRFS_FEAT_ATTR_PTR(block_group_tree),
322 #ifdef CONFIG_BLK_DEV_ZONED
323 BTRFS_FEAT_ATTR_PTR(zoned),
325 #ifdef CONFIG_BTRFS_DEBUG
326 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
328 #ifdef CONFIG_FS_VERITY
329 BTRFS_FEAT_ATTR_PTR(verity),
334 static const struct attribute_group btrfs_feature_attr_group = {
336 .is_visible = btrfs_feature_visible,
337 .attrs = btrfs_supported_feature_attrs,
340 static ssize_t rmdir_subvol_show(struct kobject *kobj,
341 struct kobj_attribute *ka, char *buf)
343 return sysfs_emit(buf, "0\n");
345 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
347 static ssize_t supported_checksums_show(struct kobject *kobj,
348 struct kobj_attribute *a, char *buf)
353 for (i = 0; i < btrfs_get_num_csums(); i++) {
355 * This "trick" only works as long as 'enum btrfs_csum_type' has
358 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
359 btrfs_super_csum_name(i));
363 ret += sysfs_emit_at(buf, ret, "\n");
366 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
368 static ssize_t send_stream_version_show(struct kobject *kobj,
369 struct kobj_attribute *ka, char *buf)
371 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
373 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
375 static const char *rescue_opts[] = {
383 static ssize_t supported_rescue_options_show(struct kobject *kobj,
384 struct kobj_attribute *a,
390 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
391 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
392 ret += sysfs_emit_at(buf, ret, "\n");
395 BTRFS_ATTR(static_feature, supported_rescue_options,
396 supported_rescue_options_show);
398 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
399 struct kobj_attribute *a,
404 /* An artificial limit to only support 4K and PAGE_SIZE */
405 if (PAGE_SIZE > SZ_4K)
406 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
407 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
411 BTRFS_ATTR(static_feature, supported_sectorsizes,
412 supported_sectorsizes_show);
415 * Features which only depend on kernel version.
417 * These are listed in /sys/fs/btrfs/features along with
418 * btrfs_supported_feature_attrs.
420 static struct attribute *btrfs_supported_static_feature_attrs[] = {
421 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
422 BTRFS_ATTR_PTR(static_feature, supported_checksums),
423 BTRFS_ATTR_PTR(static_feature, send_stream_version),
424 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
425 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
429 static const struct attribute_group btrfs_static_feature_attr_group = {
431 .attrs = btrfs_supported_static_feature_attrs,
435 * Discard statistics and tunables
437 #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj))
439 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
440 struct kobj_attribute *a,
443 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
445 return sysfs_emit(buf, "%lld\n",
446 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
448 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
450 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
451 struct kobj_attribute *a,
454 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
456 return sysfs_emit(buf, "%d\n",
457 atomic_read(&fs_info->discard_ctl.discardable_extents));
459 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
461 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
462 struct kobj_attribute *a,
465 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
467 return sysfs_emit(buf, "%llu\n",
468 fs_info->discard_ctl.discard_bitmap_bytes);
470 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
472 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
473 struct kobj_attribute *a,
476 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
478 return sysfs_emit(buf, "%lld\n",
479 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
481 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
483 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
484 struct kobj_attribute *a,
487 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
489 return sysfs_emit(buf, "%llu\n",
490 fs_info->discard_ctl.discard_extent_bytes);
492 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
494 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
495 struct kobj_attribute *a,
498 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
500 return sysfs_emit(buf, "%u\n",
501 READ_ONCE(fs_info->discard_ctl.iops_limit));
504 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
505 struct kobj_attribute *a,
506 const char *buf, size_t len)
508 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
509 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
513 ret = kstrtou32(buf, 10, &iops_limit);
517 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
518 btrfs_discard_calc_delay(discard_ctl);
519 btrfs_discard_schedule_work(discard_ctl, true);
522 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
523 btrfs_discard_iops_limit_store);
525 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
526 struct kobj_attribute *a,
529 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
531 return sysfs_emit(buf, "%u\n",
532 READ_ONCE(fs_info->discard_ctl.kbps_limit));
535 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
536 struct kobj_attribute *a,
537 const char *buf, size_t len)
539 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
540 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
544 ret = kstrtou32(buf, 10, &kbps_limit);
548 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
549 btrfs_discard_schedule_work(discard_ctl, true);
552 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
553 btrfs_discard_kbps_limit_store);
555 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
556 struct kobj_attribute *a,
559 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
561 return sysfs_emit(buf, "%llu\n",
562 READ_ONCE(fs_info->discard_ctl.max_discard_size));
565 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
566 struct kobj_attribute *a,
567 const char *buf, size_t len)
569 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
570 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
571 u64 max_discard_size;
574 ret = kstrtou64(buf, 10, &max_discard_size);
578 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
582 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
583 btrfs_discard_max_discard_size_store);
586 * Per-filesystem stats for discard (when mounted with discard=async).
588 * Path: /sys/fs/btrfs/<uuid>/discard/
590 static const struct attribute *discard_attrs[] = {
591 BTRFS_ATTR_PTR(discard, discardable_bytes),
592 BTRFS_ATTR_PTR(discard, discardable_extents),
593 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
594 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
595 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
596 BTRFS_ATTR_PTR(discard, iops_limit),
597 BTRFS_ATTR_PTR(discard, kbps_limit),
598 BTRFS_ATTR_PTR(discard, max_discard_size),
602 #ifdef CONFIG_BTRFS_DEBUG
605 * Per-filesystem runtime debugging exported via sysfs.
607 * Path: /sys/fs/btrfs/UUID/debug/
609 static const struct attribute *btrfs_debug_mount_attrs[] = {
614 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
616 * Path: /sys/fs/btrfs/debug
618 static struct attribute *btrfs_debug_feature_attrs[] = {
622 static const struct attribute_group btrfs_debug_feature_attr_group = {
624 .attrs = btrfs_debug_feature_attrs,
629 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
637 return sysfs_emit(buf, "%llu\n", val);
640 static ssize_t global_rsv_size_show(struct kobject *kobj,
641 struct kobj_attribute *ka, char *buf)
643 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
644 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
645 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
647 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
649 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
650 struct kobj_attribute *a, char *buf)
652 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
653 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
654 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
656 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
658 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
659 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
661 static ssize_t raid_bytes_show(struct kobject *kobj,
662 struct kobj_attribute *attr, char *buf);
663 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
664 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
666 static ssize_t raid_bytes_show(struct kobject *kobj,
667 struct kobj_attribute *attr, char *buf)
670 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
671 struct btrfs_block_group *block_group;
672 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
675 down_read(&sinfo->groups_sem);
676 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
677 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
678 val += block_group->length;
680 val += block_group->used;
682 up_read(&sinfo->groups_sem);
683 return sysfs_emit(buf, "%llu\n", val);
687 * Allocation information about block group profiles.
689 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
691 static struct attribute *raid_attrs[] = {
692 BTRFS_ATTR_PTR(raid, total_bytes),
693 BTRFS_ATTR_PTR(raid, used_bytes),
696 ATTRIBUTE_GROUPS(raid);
698 static void release_raid_kobj(struct kobject *kobj)
700 kfree(to_raid_kobj(kobj));
703 static struct kobj_type btrfs_raid_ktype = {
704 .sysfs_ops = &kobj_sysfs_ops,
705 .release = release_raid_kobj,
706 .default_groups = raid_groups,
709 #define SPACE_INFO_ATTR(field) \
710 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
711 struct kobj_attribute *a, \
714 struct btrfs_space_info *sinfo = to_space_info(kobj); \
715 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
717 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
719 static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
720 struct kobj_attribute *a, char *buf)
722 struct btrfs_space_info *sinfo = to_space_info(kobj);
724 return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
728 * Store new chunk size in space info. Can be called on a read-only filesystem.
730 * If the new chunk size value is larger than 10% of free space it is reduced
731 * to match that limit. Alignment must be to 256M and the system chunk size
734 static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
735 struct kobj_attribute *a,
736 const char *buf, size_t len)
738 struct btrfs_space_info *space_info = to_space_info(kobj);
739 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
743 if (!capable(CAP_SYS_ADMIN))
746 if (!fs_info->fs_devices)
749 if (btrfs_is_zoned(fs_info))
752 /* System block type must not be changed. */
753 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
756 val = memparse(buf, &retptr);
757 /* There could be trailing '\n', also catch any typos after the value */
758 retptr = skip_spaces(retptr);
759 if (*retptr != 0 || val == 0)
762 val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
764 /* Limit stripe size to 10% of available space. */
765 val = min(div_factor(fs_info->fs_devices->total_rw_bytes, 1), val);
767 /* Must be multiple of 256M. */
768 val &= ~((u64)SZ_256M - 1);
770 /* Must be at least 256M. */
774 btrfs_update_space_info_chunk_size(space_info, val);
779 #ifdef CONFIG_BTRFS_DEBUG
781 * Request chunk allocation with current chunk size.
783 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
784 struct kobj_attribute *a,
785 const char *buf, size_t len)
787 struct btrfs_space_info *space_info = to_space_info(kobj);
788 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
789 struct btrfs_trans_handle *trans;
793 if (!capable(CAP_SYS_ADMIN))
796 if (sb_rdonly(fs_info->sb))
799 ret = kstrtobool(buf, &val);
807 * This is unsafe to be called from sysfs context and may cause
808 * unexpected problems.
810 trans = btrfs_start_transaction(fs_info->tree_root, 0);
812 return PTR_ERR(trans);
813 ret = btrfs_force_chunk_alloc(trans, space_info->flags);
814 btrfs_end_transaction(trans);
821 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
825 SPACE_INFO_ATTR(flags);
826 SPACE_INFO_ATTR(total_bytes);
827 SPACE_INFO_ATTR(bytes_used);
828 SPACE_INFO_ATTR(bytes_pinned);
829 SPACE_INFO_ATTR(bytes_reserved);
830 SPACE_INFO_ATTR(bytes_may_use);
831 SPACE_INFO_ATTR(bytes_readonly);
832 SPACE_INFO_ATTR(bytes_zone_unusable);
833 SPACE_INFO_ATTR(disk_used);
834 SPACE_INFO_ATTR(disk_total);
835 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
837 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
838 struct kobj_attribute *a,
841 struct btrfs_space_info *space_info = to_space_info(kobj);
843 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
846 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
847 struct kobj_attribute *a,
848 const char *buf, size_t len)
850 struct btrfs_space_info *space_info = to_space_info(kobj);
854 ret = kstrtoint(buf, 10, &thresh);
858 if (thresh < 0 || thresh > 100)
861 WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
866 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
867 btrfs_sinfo_bg_reclaim_threshold_show,
868 btrfs_sinfo_bg_reclaim_threshold_store);
871 * Allocation information about block group types.
873 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
875 static struct attribute *space_info_attrs[] = {
876 BTRFS_ATTR_PTR(space_info, flags),
877 BTRFS_ATTR_PTR(space_info, total_bytes),
878 BTRFS_ATTR_PTR(space_info, bytes_used),
879 BTRFS_ATTR_PTR(space_info, bytes_pinned),
880 BTRFS_ATTR_PTR(space_info, bytes_reserved),
881 BTRFS_ATTR_PTR(space_info, bytes_may_use),
882 BTRFS_ATTR_PTR(space_info, bytes_readonly),
883 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
884 BTRFS_ATTR_PTR(space_info, disk_used),
885 BTRFS_ATTR_PTR(space_info, disk_total),
886 BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
887 BTRFS_ATTR_PTR(space_info, chunk_size),
888 #ifdef CONFIG_BTRFS_DEBUG
889 BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
893 ATTRIBUTE_GROUPS(space_info);
895 static void space_info_release(struct kobject *kobj)
897 struct btrfs_space_info *sinfo = to_space_info(kobj);
901 static struct kobj_type space_info_ktype = {
902 .sysfs_ops = &kobj_sysfs_ops,
903 .release = space_info_release,
904 .default_groups = space_info_groups,
908 * Allocation information about block groups.
910 * Path: /sys/fs/btrfs/<uuid>/allocation/
912 static const struct attribute *allocation_attrs[] = {
913 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
914 BTRFS_ATTR_PTR(allocation, global_rsv_size),
918 static ssize_t btrfs_label_show(struct kobject *kobj,
919 struct kobj_attribute *a, char *buf)
921 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
922 char *label = fs_info->super_copy->label;
925 spin_lock(&fs_info->super_lock);
926 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
927 spin_unlock(&fs_info->super_lock);
932 static ssize_t btrfs_label_store(struct kobject *kobj,
933 struct kobj_attribute *a,
934 const char *buf, size_t len)
936 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
942 if (sb_rdonly(fs_info->sb))
946 * p_len is the len until the first occurrence of either
949 p_len = strcspn(buf, "\n");
951 if (p_len >= BTRFS_LABEL_SIZE)
954 spin_lock(&fs_info->super_lock);
955 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
956 memcpy(fs_info->super_copy->label, buf, p_len);
957 spin_unlock(&fs_info->super_lock);
960 * We don't want to do full transaction commit from inside sysfs
962 btrfs_set_pending(fs_info, COMMIT);
963 wake_up_process(fs_info->transaction_kthread);
967 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
969 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
970 struct kobj_attribute *a, char *buf)
972 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
974 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
977 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
979 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
980 struct kobj_attribute *a, char *buf)
982 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
984 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
987 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
989 static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
990 struct kobj_attribute *a, char *buf)
992 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
994 return sysfs_emit(buf,
996 "last_commit_ms %llu\n"
997 "max_commit_ms %llu\n"
998 "total_commit_ms %llu\n",
999 fs_info->commit_stats.commit_count,
1000 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1001 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1002 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1005 static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1006 struct kobj_attribute *a,
1007 const char *buf, size_t len)
1009 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1016 if (!capable(CAP_SYS_RESOURCE))
1019 ret = kstrtoul(buf, 10, &val);
1025 WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1029 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1031 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1032 struct kobj_attribute *a, char *buf)
1034 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1036 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1039 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1041 static ssize_t quota_override_show(struct kobject *kobj,
1042 struct kobj_attribute *a, char *buf)
1044 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1047 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1048 return sysfs_emit(buf, "%d\n", quota_override);
1051 static ssize_t quota_override_store(struct kobject *kobj,
1052 struct kobj_attribute *a,
1053 const char *buf, size_t len)
1055 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1062 if (!capable(CAP_SYS_RESOURCE))
1065 err = kstrtoul(buf, 10, &knob);
1072 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1074 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1079 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1081 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1082 struct kobj_attribute *a, char *buf)
1084 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1086 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1089 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1091 static ssize_t btrfs_checksum_show(struct kobject *kobj,
1092 struct kobj_attribute *a, char *buf)
1094 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1095 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1097 return sysfs_emit(buf, "%s (%s)\n",
1098 btrfs_super_csum_name(csum_type),
1099 crypto_shash_driver_name(fs_info->csum_shash));
1102 BTRFS_ATTR(, checksum, btrfs_checksum_show);
1104 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1105 struct kobj_attribute *a, char *buf)
1107 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1110 switch (READ_ONCE(fs_info->exclusive_operation)) {
1111 case BTRFS_EXCLOP_NONE:
1114 case BTRFS_EXCLOP_BALANCE:
1117 case BTRFS_EXCLOP_BALANCE_PAUSED:
1118 str = "balance paused\n";
1120 case BTRFS_EXCLOP_DEV_ADD:
1121 str = "device add\n";
1123 case BTRFS_EXCLOP_DEV_REMOVE:
1124 str = "device remove\n";
1126 case BTRFS_EXCLOP_DEV_REPLACE:
1127 str = "device replace\n";
1129 case BTRFS_EXCLOP_RESIZE:
1132 case BTRFS_EXCLOP_SWAP_ACTIVATE:
1133 str = "swap activate\n";
1139 return sysfs_emit(buf, "%s", str);
1141 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1143 static ssize_t btrfs_generation_show(struct kobject *kobj,
1144 struct kobj_attribute *a, char *buf)
1146 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1148 return sysfs_emit(buf, "%llu\n", fs_info->generation);
1150 BTRFS_ATTR(, generation, btrfs_generation_show);
1152 static const char * const btrfs_read_policy_name[] = { "pid" };
1154 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1155 struct kobj_attribute *a, char *buf)
1157 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1161 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1162 if (fs_devices->read_policy == i)
1163 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
1164 (ret == 0 ? "" : " "),
1165 btrfs_read_policy_name[i]);
1167 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
1168 (ret == 0 ? "" : " "),
1169 btrfs_read_policy_name[i]);
1172 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
1177 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1178 struct kobj_attribute *a,
1179 const char *buf, size_t len)
1181 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1184 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1185 if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
1186 if (i != fs_devices->read_policy) {
1187 fs_devices->read_policy = i;
1188 btrfs_info(fs_devices->fs_info,
1189 "read policy set to '%s'",
1190 btrfs_read_policy_name[i]);
1198 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1200 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1201 struct kobj_attribute *a,
1204 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1206 return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1209 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1210 struct kobj_attribute *a,
1211 const char *buf, size_t len)
1213 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1217 ret = kstrtoint(buf, 10, &thresh);
1221 if (thresh != 0 && (thresh <= 50 || thresh > 100))
1224 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1228 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1229 btrfs_bg_reclaim_threshold_store);
1232 * Per-filesystem information and stats.
1234 * Path: /sys/fs/btrfs/<uuid>/
1236 static const struct attribute *btrfs_attrs[] = {
1237 BTRFS_ATTR_PTR(, label),
1238 BTRFS_ATTR_PTR(, nodesize),
1239 BTRFS_ATTR_PTR(, sectorsize),
1240 BTRFS_ATTR_PTR(, clone_alignment),
1241 BTRFS_ATTR_PTR(, quota_override),
1242 BTRFS_ATTR_PTR(, metadata_uuid),
1243 BTRFS_ATTR_PTR(, checksum),
1244 BTRFS_ATTR_PTR(, exclusive_operation),
1245 BTRFS_ATTR_PTR(, generation),
1246 BTRFS_ATTR_PTR(, read_policy),
1247 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1248 BTRFS_ATTR_PTR(, commit_stats),
1252 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1254 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1256 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1257 complete(&fs_devs->kobj_unregister);
1260 static struct kobj_type btrfs_ktype = {
1261 .sysfs_ops = &kobj_sysfs_ops,
1262 .release = btrfs_release_fsid_kobj,
1265 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1267 if (kobj->ktype != &btrfs_ktype)
1269 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1272 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1274 if (kobj->ktype != &btrfs_ktype)
1276 return to_fs_devs(kobj)->fs_info;
1279 static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1282 if (kobj->ktype == &btrfs_ktype)
1284 kobj = kobj->parent;
1289 #define NUM_FEATURE_BITS 64
1290 #define BTRFS_FEATURE_NAME_MAX 13
1291 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1292 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1294 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1295 ARRAY_SIZE(btrfs_feature_attrs));
1296 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1297 ARRAY_SIZE(btrfs_feature_attrs[0]));
1299 static const u64 supported_feature_masks[FEAT_MAX] = {
1300 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1301 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1302 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1305 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1309 for (set = 0; set < FEAT_MAX; set++) {
1311 struct attribute *attrs[2];
1312 struct attribute_group agroup = {
1316 u64 features = get_features(fs_info, set);
1317 features &= ~supported_feature_masks[set];
1323 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1324 struct btrfs_feature_attr *fa;
1326 if (!(features & (1ULL << i)))
1329 fa = &btrfs_feature_attrs[set][i];
1330 attrs[0] = &fa->kobj_attr.attr;
1333 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1338 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1346 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1348 if (fs_devs->devinfo_kobj) {
1349 kobject_del(fs_devs->devinfo_kobj);
1350 kobject_put(fs_devs->devinfo_kobj);
1351 fs_devs->devinfo_kobj = NULL;
1354 if (fs_devs->devices_kobj) {
1355 kobject_del(fs_devs->devices_kobj);
1356 kobject_put(fs_devs->devices_kobj);
1357 fs_devs->devices_kobj = NULL;
1360 if (fs_devs->fsid_kobj.state_initialized) {
1361 kobject_del(&fs_devs->fsid_kobj);
1362 kobject_put(&fs_devs->fsid_kobj);
1363 wait_for_completion(&fs_devs->kobj_unregister);
1367 /* when fs_devs is NULL it will remove all fsid kobject */
1368 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1370 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1373 __btrfs_sysfs_remove_fsid(fs_devs);
1377 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1378 __btrfs_sysfs_remove_fsid(fs_devs);
1382 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1384 struct btrfs_device *device;
1385 struct btrfs_fs_devices *seed;
1387 list_for_each_entry(device, &fs_devices->devices, dev_list)
1388 btrfs_sysfs_remove_device(device);
1390 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1391 list_for_each_entry(device, &seed->devices, dev_list)
1392 btrfs_sysfs_remove_device(device);
1396 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1398 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1400 sysfs_remove_link(fsid_kobj, "bdi");
1402 if (fs_info->space_info_kobj) {
1403 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1404 kobject_del(fs_info->space_info_kobj);
1405 kobject_put(fs_info->space_info_kobj);
1407 if (fs_info->discard_kobj) {
1408 sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1409 kobject_del(fs_info->discard_kobj);
1410 kobject_put(fs_info->discard_kobj);
1412 #ifdef CONFIG_BTRFS_DEBUG
1413 if (fs_info->debug_kobj) {
1414 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1415 kobject_del(fs_info->debug_kobj);
1416 kobject_put(fs_info->debug_kobj);
1419 addrm_unknown_feature_attrs(fs_info, false);
1420 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1421 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1422 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1425 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1426 [FEAT_COMPAT] = "compat",
1427 [FEAT_COMPAT_RO] = "compat_ro",
1428 [FEAT_INCOMPAT] = "incompat",
1431 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1433 return btrfs_feature_set_names[set];
1436 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1438 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1443 str = kmalloc(bufsize, GFP_KERNEL);
1447 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1450 if (!(flags & (1ULL << i)))
1453 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1454 len += scnprintf(str + len, bufsize - len, "%s%s",
1455 len ? "," : "", name);
1461 static void init_feature_attrs(void)
1463 struct btrfs_feature_attr *fa;
1466 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1467 memset(btrfs_unknown_feature_names, 0,
1468 sizeof(btrfs_unknown_feature_names));
1470 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1471 struct btrfs_feature_attr *sfa;
1472 struct attribute *a = btrfs_supported_feature_attrs[i];
1474 sfa = attr_to_btrfs_feature_attr(a);
1475 bit = ilog2(sfa->feature_bit);
1476 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1478 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1481 for (set = 0; set < FEAT_MAX; set++) {
1482 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1483 char *name = btrfs_unknown_feature_names[set][i];
1484 fa = &btrfs_feature_attrs[set][i];
1486 if (fa->kobj_attr.attr.name)
1489 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1490 btrfs_feature_set_names[set], i);
1492 fa->kobj_attr.attr.name = name;
1493 fa->kobj_attr.attr.mode = S_IRUGO;
1494 fa->feature_set = set;
1495 fa->feature_bit = 1ULL << i;
1501 * Create a sysfs entry for a given block group type at path
1502 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1504 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1506 struct btrfs_fs_info *fs_info = cache->fs_info;
1507 struct btrfs_space_info *space_info = cache->space_info;
1508 struct raid_kobject *rkobj;
1509 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1510 unsigned int nofs_flag;
1514 * Setup a NOFS context because kobject_add(), deep in its call chain,
1515 * does GFP_KERNEL allocations, and we are often called in a context
1516 * where if reclaim is triggered we can deadlock (we are either holding
1517 * a transaction handle or some lock required for a transaction
1520 nofs_flag = memalloc_nofs_save();
1522 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1524 memalloc_nofs_restore(nofs_flag);
1525 btrfs_warn(cache->fs_info,
1526 "couldn't alloc memory for raid level kobject");
1530 rkobj->flags = cache->flags;
1531 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1534 * We call this either on mount, or if we've created a block group for a
1535 * new index type while running (i.e. when restriping). The running
1536 * case is tricky because we could race with other threads, so we need
1537 * to have this check to make sure we didn't already init the kobject.
1539 * We don't have to protect on the free side because it only happens on
1542 spin_lock(&space_info->lock);
1543 if (space_info->block_group_kobjs[index]) {
1544 spin_unlock(&space_info->lock);
1545 kobject_put(&rkobj->kobj);
1548 space_info->block_group_kobjs[index] = &rkobj->kobj;
1550 spin_unlock(&space_info->lock);
1552 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1553 btrfs_bg_type_to_raid_name(rkobj->flags));
1554 memalloc_nofs_restore(nofs_flag);
1556 spin_lock(&space_info->lock);
1557 space_info->block_group_kobjs[index] = NULL;
1558 spin_unlock(&space_info->lock);
1559 kobject_put(&rkobj->kobj);
1561 "failed to add kobject for block cache, ignoring");
1567 * Remove sysfs directories for all block group types of a given space info and
1568 * the space info as well
1570 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1574 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1575 struct kobject *kobj;
1577 kobj = space_info->block_group_kobjs[i];
1578 space_info->block_group_kobjs[i] = NULL;
1584 kobject_del(&space_info->kobj);
1585 kobject_put(&space_info->kobj);
1588 static const char *alloc_name(u64 flags)
1591 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1593 case BTRFS_BLOCK_GROUP_METADATA:
1595 case BTRFS_BLOCK_GROUP_DATA:
1597 case BTRFS_BLOCK_GROUP_SYSTEM:
1601 return "invalid-combination";
1606 * Create a sysfs entry for a space info type at path
1607 * /sys/fs/btrfs/UUID/allocation/TYPE
1609 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1610 struct btrfs_space_info *space_info)
1614 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1615 fs_info->space_info_kobj, "%s",
1616 alloc_name(space_info->flags));
1618 kobject_put(&space_info->kobj);
1625 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1627 struct kobject *devices_kobj;
1630 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1631 * fs_info::fs_devices.
1633 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1634 ASSERT(devices_kobj);
1637 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1639 if (device->devid_kobj.state_initialized) {
1640 kobject_del(&device->devid_kobj);
1641 kobject_put(&device->devid_kobj);
1642 wait_for_completion(&device->kobj_unregister);
1646 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1647 struct kobj_attribute *a,
1651 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1654 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1656 return sysfs_emit(buf, "%d\n", val);
1658 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1660 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1661 struct kobj_attribute *a, char *buf)
1664 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1667 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1669 return sysfs_emit(buf, "%d\n", val);
1671 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1673 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1674 struct kobj_attribute *a,
1678 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1681 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1683 return sysfs_emit(buf, "%d\n", val);
1685 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1687 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1688 struct kobj_attribute *a,
1691 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1694 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1697 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1698 struct kobj_attribute *a,
1699 const char *buf, size_t len)
1701 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1704 unsigned long long limit;
1706 limit = memparse(buf, &endptr);
1707 WRITE_ONCE(device->scrub_speed_max, limit);
1710 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1711 btrfs_devinfo_scrub_speed_max_store);
1713 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1714 struct kobj_attribute *a, char *buf)
1717 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1720 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1722 return sysfs_emit(buf, "%d\n", val);
1724 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1726 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1727 struct kobj_attribute *a, char *buf)
1729 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1732 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1734 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1736 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1737 struct kobj_attribute *a, char *buf)
1739 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1742 if (!device->dev_stats_valid)
1743 return sysfs_emit(buf, "invalid\n");
1746 * Print all at once so we get a snapshot of all values from the same
1747 * time. Keep them in sync and in order of definition of
1748 * btrfs_dev_stat_values.
1750 return sysfs_emit(buf,
1754 "corruption_errs %d\n"
1755 "generation_errs %d\n",
1756 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1757 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1758 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1759 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1760 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1762 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1765 * Information about one device.
1767 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1769 static struct attribute *devid_attrs[] = {
1770 BTRFS_ATTR_PTR(devid, error_stats),
1771 BTRFS_ATTR_PTR(devid, fsid),
1772 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1773 BTRFS_ATTR_PTR(devid, missing),
1774 BTRFS_ATTR_PTR(devid, replace_target),
1775 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1776 BTRFS_ATTR_PTR(devid, writeable),
1779 ATTRIBUTE_GROUPS(devid);
1781 static void btrfs_release_devid_kobj(struct kobject *kobj)
1783 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1786 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1787 complete(&device->kobj_unregister);
1790 static struct kobj_type devid_ktype = {
1791 .sysfs_ops = &kobj_sysfs_ops,
1792 .default_groups = devid_groups,
1793 .release = btrfs_release_devid_kobj,
1796 int btrfs_sysfs_add_device(struct btrfs_device *device)
1799 unsigned int nofs_flag;
1800 struct kobject *devices_kobj;
1801 struct kobject *devinfo_kobj;
1804 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1805 * for the seed fs_devices
1807 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1808 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1809 ASSERT(devices_kobj);
1810 ASSERT(devinfo_kobj);
1812 nofs_flag = memalloc_nofs_save();
1815 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1817 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1819 btrfs_warn(device->fs_info,
1820 "creating sysfs device link for devid %llu failed: %d",
1821 device->devid, ret);
1826 init_completion(&device->kobj_unregister);
1827 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1828 devinfo_kobj, "%llu", device->devid);
1830 kobject_put(&device->devid_kobj);
1831 btrfs_warn(device->fs_info,
1832 "devinfo init for devid %llu failed: %d",
1833 device->devid, ret);
1837 memalloc_nofs_restore(nofs_flag);
1841 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1844 struct btrfs_device *device;
1845 struct btrfs_fs_devices *seed;
1847 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1848 ret = btrfs_sysfs_add_device(device);
1853 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1854 list_for_each_entry(device, &seed->devices, dev_list) {
1855 ret = btrfs_sysfs_add_device(device);
1864 btrfs_sysfs_remove_fs_devices(fs_devices);
1868 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1872 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1874 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1875 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1876 &disk_to_dev(bdev->bd_disk)->kobj);
1879 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1882 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1885 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1888 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1889 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1890 btrfs_warn(fs_devices->fs_info,
1891 "sysfs: failed to create fsid for sprout");
1894 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1898 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1900 if (kobject_rename(&device->devid_kobj, tmp))
1901 btrfs_warn(device->fs_devices->fs_info,
1902 "sysfs: failed to update devid for %llu",
1906 /* /sys/fs/btrfs/ entry */
1907 static struct kset *btrfs_kset;
1911 * /sys/fs/btrfs/UUID
1913 * Can be called by the device discovery thread.
1915 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1919 init_completion(&fs_devs->kobj_unregister);
1920 fs_devs->fsid_kobj.kset = btrfs_kset;
1921 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1922 "%pU", fs_devs->fsid);
1924 kobject_put(&fs_devs->fsid_kobj);
1928 fs_devs->devices_kobj = kobject_create_and_add("devices",
1929 &fs_devs->fsid_kobj);
1930 if (!fs_devs->devices_kobj) {
1931 btrfs_err(fs_devs->fs_info,
1932 "failed to init sysfs device interface");
1933 btrfs_sysfs_remove_fsid(fs_devs);
1937 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1938 &fs_devs->fsid_kobj);
1939 if (!fs_devs->devinfo_kobj) {
1940 btrfs_err(fs_devs->fs_info,
1941 "failed to init sysfs devinfo kobject");
1942 btrfs_sysfs_remove_fsid(fs_devs);
1949 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1952 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1953 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1955 error = btrfs_sysfs_add_fs_devices(fs_devs);
1959 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1961 btrfs_sysfs_remove_fs_devices(fs_devs);
1965 error = sysfs_create_group(fsid_kobj,
1966 &btrfs_feature_attr_group);
1970 #ifdef CONFIG_BTRFS_DEBUG
1971 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1972 if (!fs_info->debug_kobj) {
1977 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1982 /* Discard directory */
1983 fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
1984 if (!fs_info->discard_kobj) {
1989 error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
1993 error = addrm_unknown_feature_attrs(fs_info, true);
1997 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2001 fs_info->space_info_kobj = kobject_create_and_add("allocation",
2003 if (!fs_info->space_info_kobj) {
2008 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2014 btrfs_sysfs_remove_mounted(fs_info);
2018 static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2019 struct kobj_attribute *a,
2022 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2025 spin_lock(&fs_info->qgroup_lock);
2026 enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
2027 spin_unlock(&fs_info->qgroup_lock);
2029 return sysfs_emit(buf, "%d\n", enabled);
2031 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2033 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2034 struct kobj_attribute *a,
2037 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2040 spin_lock(&fs_info->qgroup_lock);
2041 inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
2042 spin_unlock(&fs_info->qgroup_lock);
2044 return sysfs_emit(buf, "%d\n", inconsistent);
2046 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2048 static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2049 struct kobj_attribute *a,
2052 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2055 spin_lock(&fs_info->qgroup_lock);
2056 result = fs_info->qgroup_drop_subtree_thres;
2057 spin_unlock(&fs_info->qgroup_lock);
2059 return sysfs_emit(buf, "%d\n", result);
2062 static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2063 struct kobj_attribute *a,
2064 const char *buf, size_t len)
2066 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2070 ret = kstrtou8(buf, 10, &new_thres);
2074 if (new_thres > BTRFS_MAX_LEVEL)
2077 spin_lock(&fs_info->qgroup_lock);
2078 fs_info->qgroup_drop_subtree_thres = new_thres;
2079 spin_unlock(&fs_info->qgroup_lock);
2083 BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2084 qgroup_drop_subtree_thres_store);
2087 * Qgroups global info
2089 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2091 static struct attribute *qgroups_attrs[] = {
2092 BTRFS_ATTR_PTR(qgroups, enabled),
2093 BTRFS_ATTR_PTR(qgroups, inconsistent),
2094 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2097 ATTRIBUTE_GROUPS(qgroups);
2099 static void qgroups_release(struct kobject *kobj)
2104 static struct kobj_type qgroups_ktype = {
2105 .sysfs_ops = &kobj_sysfs_ops,
2106 .default_groups = qgroups_groups,
2107 .release = qgroups_release,
2110 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2112 return to_fs_info(kobj->parent->parent);
2115 #define QGROUP_ATTR(_member, _show_name) \
2116 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
2117 struct kobj_attribute *a, \
2120 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2121 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2122 struct btrfs_qgroup, kobj); \
2123 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
2125 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2127 #define QGROUP_RSV_ATTR(_name, _type) \
2128 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
2129 struct kobj_attribute *a, \
2132 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2133 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2134 struct btrfs_qgroup, kobj); \
2135 return btrfs_show_u64(&qgroup->rsv.values[_type], \
2136 &fs_info->qgroup_lock, buf); \
2138 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2140 QGROUP_ATTR(rfer, referenced);
2141 QGROUP_ATTR(excl, exclusive);
2142 QGROUP_ATTR(max_rfer, max_referenced);
2143 QGROUP_ATTR(max_excl, max_exclusive);
2144 QGROUP_ATTR(lim_flags, limit_flags);
2145 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2146 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2147 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2150 * Qgroup information.
2152 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2154 static struct attribute *qgroup_attrs[] = {
2155 BTRFS_ATTR_PTR(qgroup, referenced),
2156 BTRFS_ATTR_PTR(qgroup, exclusive),
2157 BTRFS_ATTR_PTR(qgroup, max_referenced),
2158 BTRFS_ATTR_PTR(qgroup, max_exclusive),
2159 BTRFS_ATTR_PTR(qgroup, limit_flags),
2160 BTRFS_ATTR_PTR(qgroup, rsv_data),
2161 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2162 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2165 ATTRIBUTE_GROUPS(qgroup);
2167 static void qgroup_release(struct kobject *kobj)
2169 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2171 memset(&qgroup->kobj, 0, sizeof(*kobj));
2174 static struct kobj_type qgroup_ktype = {
2175 .sysfs_ops = &kobj_sysfs_ops,
2176 .release = qgroup_release,
2177 .default_groups = qgroup_groups,
2180 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2181 struct btrfs_qgroup *qgroup)
2183 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2186 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2188 if (qgroup->kobj.state_initialized)
2193 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2194 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2195 btrfs_qgroup_subvolid(qgroup->qgroupid));
2197 kobject_put(&qgroup->kobj);
2202 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2204 struct btrfs_qgroup *qgroup;
2205 struct btrfs_qgroup *next;
2207 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2210 rbtree_postorder_for_each_entry_safe(qgroup, next,
2211 &fs_info->qgroup_tree, node)
2212 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2213 if (fs_info->qgroups_kobj) {
2214 kobject_del(fs_info->qgroups_kobj);
2215 kobject_put(fs_info->qgroups_kobj);
2216 fs_info->qgroups_kobj = NULL;
2220 /* Called when qgroups get initialized, thus there is no need for locking */
2221 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2223 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2224 struct btrfs_qgroup *qgroup;
2225 struct btrfs_qgroup *next;
2228 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2232 if (fs_info->qgroups_kobj)
2235 fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
2236 if (!fs_info->qgroups_kobj)
2239 ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2240 fsid_kobj, "qgroups");
2244 rbtree_postorder_for_each_entry_safe(qgroup, next,
2245 &fs_info->qgroup_tree, node) {
2246 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2253 btrfs_sysfs_del_qgroups(fs_info);
2257 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2258 struct btrfs_qgroup *qgroup)
2260 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2263 if (qgroup->kobj.state_initialized) {
2264 kobject_del(&qgroup->kobj);
2265 kobject_put(&qgroup->kobj);
2270 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2271 * values in superblock. Call after any changes to incompat/compat_ro flags
2273 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
2274 u64 bit, enum btrfs_feature_set set)
2276 struct btrfs_fs_devices *fs_devs;
2277 struct kobject *fsid_kobj;
2278 u64 __maybe_unused features;
2279 int __maybe_unused ret;
2285 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2286 * safe when called from some contexts (eg. balance)
2288 features = get_features(fs_info, set);
2289 ASSERT(bit & supported_feature_masks[set]);
2291 fs_devs = fs_info->fs_devices;
2292 fsid_kobj = &fs_devs->fsid_kobj;
2294 if (!fsid_kobj->state_initialized)
2298 * FIXME: this is too heavy to update just one value, ideally we'd like
2299 * to use sysfs_update_group but some refactoring is needed first.
2301 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2302 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2305 int __init btrfs_init_sysfs(void)
2309 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2313 init_feature_attrs();
2314 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2317 ret = sysfs_merge_group(&btrfs_kset->kobj,
2318 &btrfs_static_feature_attr_group);
2320 goto out_remove_group;
2322 #ifdef CONFIG_BTRFS_DEBUG
2323 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2331 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2333 kset_unregister(btrfs_kset);
2338 void __cold btrfs_exit_sysfs(void)
2340 sysfs_unmerge_group(&btrfs_kset->kobj,
2341 &btrfs_static_feature_attr_group);
2342 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2343 #ifdef CONFIG_BTRFS_DEBUG
2344 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2346 kset_unregister(btrfs_kset);