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 <linux/list.h>
13 #include <crypto/hash.h>
19 #include "transaction.h"
22 #include "space-info.h"
23 #include "block-group.h"
27 #include "accessors.h"
31 * --------------------------------------------------------------------------
32 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
33 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and
34 * /sys/fs/btrfs/<uuid>/features
35 * btrfs_attrs /sys/fs/btrfs/<uuid>
36 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid>
37 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation
38 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
39 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>
40 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
41 * discard_attrs /sys/fs/btrfs/<uuid>/discard
43 * When built with BTRFS_CONFIG_DEBUG:
45 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
46 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
49 struct btrfs_feature_attr {
50 struct kobj_attribute kobj_attr;
51 enum btrfs_feature_set feature_set;
55 /* For raid type sysfs entries */
61 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
63 .attr = { .name = __stringify(_name), .mode = _mode }, \
68 #define BTRFS_ATTR_W(_prefix, _name, _store) \
69 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
70 __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
72 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
73 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
74 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
76 #define BTRFS_ATTR(_prefix, _name, _show) \
77 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
78 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
80 #define BTRFS_ATTR_PTR(_prefix, _name) \
81 (&btrfs_attr_##_prefix##_##_name.attr)
83 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
84 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
85 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
86 btrfs_feature_attr_show, \
87 btrfs_feature_attr_store), \
88 .feature_set = _feature_set, \
89 .feature_bit = _feature_prefix ##_## _feature_bit, \
91 #define BTRFS_FEAT_ATTR_PTR(_name) \
92 (&btrfs_attr_features_##_name.kobj_attr.attr)
94 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
95 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
96 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
97 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
98 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
99 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
101 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
102 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
103 static struct kobject *get_btrfs_kobj(struct kobject *kobj);
105 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
107 return container_of(a, struct btrfs_feature_attr, kobj_attr);
110 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
112 return container_of(attr, struct kobj_attribute, attr);
115 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
116 struct attribute *attr)
118 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
121 static u64 get_features(struct btrfs_fs_info *fs_info,
122 enum btrfs_feature_set set)
124 struct btrfs_super_block *disk_super = fs_info->super_copy;
125 if (set == FEAT_COMPAT)
126 return btrfs_super_compat_flags(disk_super);
127 else if (set == FEAT_COMPAT_RO)
128 return btrfs_super_compat_ro_flags(disk_super);
130 return btrfs_super_incompat_flags(disk_super);
133 static void set_features(struct btrfs_fs_info *fs_info,
134 enum btrfs_feature_set set, u64 features)
136 struct btrfs_super_block *disk_super = fs_info->super_copy;
137 if (set == FEAT_COMPAT)
138 btrfs_set_super_compat_flags(disk_super, features);
139 else if (set == FEAT_COMPAT_RO)
140 btrfs_set_super_compat_ro_flags(disk_super, features);
142 btrfs_set_super_incompat_flags(disk_super, features);
145 static int can_modify_feature(struct btrfs_feature_attr *fa)
149 switch (fa->feature_set) {
151 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
152 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
155 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
156 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
159 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
160 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
163 pr_warn("btrfs: sysfs: unknown feature set %d\n",
168 if (set & fa->feature_bit)
170 if (clear & fa->feature_bit)
176 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
177 struct kobj_attribute *a, char *buf)
180 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
181 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
183 u64 features = get_features(fs_info, fa->feature_set);
184 if (features & fa->feature_bit)
187 val = can_modify_feature(fa);
189 return sysfs_emit(buf, "%d\n", val);
192 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
193 struct kobj_attribute *a,
194 const char *buf, size_t count)
196 struct btrfs_fs_info *fs_info;
197 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
198 u64 features, set, clear;
202 fs_info = to_fs_info(kobj);
206 if (sb_rdonly(fs_info->sb))
209 ret = kstrtoul(skip_spaces(buf), 0, &val);
213 if (fa->feature_set == FEAT_COMPAT) {
214 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
215 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
216 } else if (fa->feature_set == FEAT_COMPAT_RO) {
217 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
218 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
220 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
221 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
224 features = get_features(fs_info, fa->feature_set);
227 if ((val && (features & fa->feature_bit)) ||
228 (!val && !(features & fa->feature_bit)))
231 if ((val && !(set & fa->feature_bit)) ||
232 (!val && !(clear & fa->feature_bit))) {
234 "%sabling feature %s on mounted fs is not supported.",
235 val ? "En" : "Dis", fa->kobj_attr.attr.name);
239 btrfs_info(fs_info, "%s %s feature flag",
240 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
242 spin_lock(&fs_info->super_lock);
243 features = get_features(fs_info, fa->feature_set);
245 features |= fa->feature_bit;
247 features &= ~fa->feature_bit;
248 set_features(fs_info, fa->feature_set, features);
249 spin_unlock(&fs_info->super_lock);
252 * We don't want to do full transaction commit from inside sysfs
254 set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
255 wake_up_process(fs_info->transaction_kthread);
260 static umode_t btrfs_feature_visible(struct kobject *kobj,
261 struct attribute *attr, int unused)
263 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
264 umode_t mode = attr->mode;
267 struct btrfs_feature_attr *fa;
270 fa = attr_to_btrfs_feature_attr(attr);
271 features = get_features(fs_info, fa->feature_set);
273 if (can_modify_feature(fa))
275 else if (!(features & fa->feature_bit))
282 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
283 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
284 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
285 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
286 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
287 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
288 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
289 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
290 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
291 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
292 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
293 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
294 #ifdef CONFIG_BLK_DEV_ZONED
295 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
297 #ifdef CONFIG_BTRFS_DEBUG
298 /* Remove once support for extent tree v2 is feature complete */
299 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
301 #ifdef CONFIG_FS_VERITY
302 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
306 * Features which depend on feature bits and may differ between each fs.
308 * /sys/fs/btrfs/features - all available features implemented by this version
309 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
310 * can be changed on a mounted filesystem.
312 static struct attribute *btrfs_supported_feature_attrs[] = {
313 BTRFS_FEAT_ATTR_PTR(default_subvol),
314 BTRFS_FEAT_ATTR_PTR(mixed_groups),
315 BTRFS_FEAT_ATTR_PTR(compress_lzo),
316 BTRFS_FEAT_ATTR_PTR(compress_zstd),
317 BTRFS_FEAT_ATTR_PTR(extended_iref),
318 BTRFS_FEAT_ATTR_PTR(raid56),
319 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
320 BTRFS_FEAT_ATTR_PTR(no_holes),
321 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
322 BTRFS_FEAT_ATTR_PTR(free_space_tree),
323 BTRFS_FEAT_ATTR_PTR(raid1c34),
324 BTRFS_FEAT_ATTR_PTR(block_group_tree),
325 #ifdef CONFIG_BLK_DEV_ZONED
326 BTRFS_FEAT_ATTR_PTR(zoned),
328 #ifdef CONFIG_BTRFS_DEBUG
329 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
331 #ifdef CONFIG_FS_VERITY
332 BTRFS_FEAT_ATTR_PTR(verity),
337 static const struct attribute_group btrfs_feature_attr_group = {
339 .is_visible = btrfs_feature_visible,
340 .attrs = btrfs_supported_feature_attrs,
343 static ssize_t rmdir_subvol_show(struct kobject *kobj,
344 struct kobj_attribute *ka, char *buf)
346 return sysfs_emit(buf, "0\n");
348 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
350 static ssize_t supported_checksums_show(struct kobject *kobj,
351 struct kobj_attribute *a, char *buf)
356 for (i = 0; i < btrfs_get_num_csums(); i++) {
358 * This "trick" only works as long as 'enum btrfs_csum_type' has
361 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
362 btrfs_super_csum_name(i));
366 ret += sysfs_emit_at(buf, ret, "\n");
369 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
371 static ssize_t send_stream_version_show(struct kobject *kobj,
372 struct kobj_attribute *ka, char *buf)
374 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
376 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
378 static const char *rescue_opts[] = {
386 static ssize_t supported_rescue_options_show(struct kobject *kobj,
387 struct kobj_attribute *a,
393 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
394 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
395 ret += sysfs_emit_at(buf, ret, "\n");
398 BTRFS_ATTR(static_feature, supported_rescue_options,
399 supported_rescue_options_show);
401 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
402 struct kobj_attribute *a,
407 /* An artificial limit to only support 4K and PAGE_SIZE */
408 if (PAGE_SIZE > SZ_4K)
409 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
410 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
414 BTRFS_ATTR(static_feature, supported_sectorsizes,
415 supported_sectorsizes_show);
418 * Features which only depend on kernel version.
420 * These are listed in /sys/fs/btrfs/features along with
421 * btrfs_supported_feature_attrs.
423 static struct attribute *btrfs_supported_static_feature_attrs[] = {
424 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
425 BTRFS_ATTR_PTR(static_feature, supported_checksums),
426 BTRFS_ATTR_PTR(static_feature, send_stream_version),
427 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
428 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
432 static const struct attribute_group btrfs_static_feature_attr_group = {
434 .attrs = btrfs_supported_static_feature_attrs,
438 * Discard statistics and tunables
440 #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj))
442 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
443 struct kobj_attribute *a,
446 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
448 return sysfs_emit(buf, "%lld\n",
449 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
451 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
453 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
454 struct kobj_attribute *a,
457 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
459 return sysfs_emit(buf, "%d\n",
460 atomic_read(&fs_info->discard_ctl.discardable_extents));
462 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
464 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
465 struct kobj_attribute *a,
468 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
470 return sysfs_emit(buf, "%llu\n",
471 fs_info->discard_ctl.discard_bitmap_bytes);
473 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
475 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
476 struct kobj_attribute *a,
479 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
481 return sysfs_emit(buf, "%lld\n",
482 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
484 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
486 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
487 struct kobj_attribute *a,
490 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
492 return sysfs_emit(buf, "%llu\n",
493 fs_info->discard_ctl.discard_extent_bytes);
495 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
497 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
498 struct kobj_attribute *a,
501 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
503 return sysfs_emit(buf, "%u\n",
504 READ_ONCE(fs_info->discard_ctl.iops_limit));
507 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
508 struct kobj_attribute *a,
509 const char *buf, size_t len)
511 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
512 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
516 ret = kstrtou32(buf, 10, &iops_limit);
520 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
521 btrfs_discard_calc_delay(discard_ctl);
522 btrfs_discard_schedule_work(discard_ctl, true);
525 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
526 btrfs_discard_iops_limit_store);
528 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
529 struct kobj_attribute *a,
532 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
534 return sysfs_emit(buf, "%u\n",
535 READ_ONCE(fs_info->discard_ctl.kbps_limit));
538 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
539 struct kobj_attribute *a,
540 const char *buf, size_t len)
542 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
543 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
547 ret = kstrtou32(buf, 10, &kbps_limit);
551 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
552 btrfs_discard_schedule_work(discard_ctl, true);
555 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
556 btrfs_discard_kbps_limit_store);
558 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
559 struct kobj_attribute *a,
562 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
564 return sysfs_emit(buf, "%llu\n",
565 READ_ONCE(fs_info->discard_ctl.max_discard_size));
568 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
569 struct kobj_attribute *a,
570 const char *buf, size_t len)
572 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
573 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
574 u64 max_discard_size;
577 ret = kstrtou64(buf, 10, &max_discard_size);
581 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
585 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
586 btrfs_discard_max_discard_size_store);
589 * Per-filesystem stats for discard (when mounted with discard=async).
591 * Path: /sys/fs/btrfs/<uuid>/discard/
593 static const struct attribute *discard_attrs[] = {
594 BTRFS_ATTR_PTR(discard, discardable_bytes),
595 BTRFS_ATTR_PTR(discard, discardable_extents),
596 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
597 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
598 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
599 BTRFS_ATTR_PTR(discard, iops_limit),
600 BTRFS_ATTR_PTR(discard, kbps_limit),
601 BTRFS_ATTR_PTR(discard, max_discard_size),
605 #ifdef CONFIG_BTRFS_DEBUG
608 * Per-filesystem runtime debugging exported via sysfs.
610 * Path: /sys/fs/btrfs/UUID/debug/
612 static const struct attribute *btrfs_debug_mount_attrs[] = {
617 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
619 * Path: /sys/fs/btrfs/debug
621 static struct attribute *btrfs_debug_feature_attrs[] = {
625 static const struct attribute_group btrfs_debug_feature_attr_group = {
627 .attrs = btrfs_debug_feature_attrs,
632 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
640 return sysfs_emit(buf, "%llu\n", val);
643 static ssize_t global_rsv_size_show(struct kobject *kobj,
644 struct kobj_attribute *ka, char *buf)
646 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
647 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
648 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
650 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
652 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
653 struct kobj_attribute *a, char *buf)
655 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
656 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
657 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
659 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
661 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
662 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
664 static ssize_t raid_bytes_show(struct kobject *kobj,
665 struct kobj_attribute *attr, char *buf);
666 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
667 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
669 static ssize_t raid_bytes_show(struct kobject *kobj,
670 struct kobj_attribute *attr, char *buf)
673 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
674 struct btrfs_block_group *block_group;
675 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
678 down_read(&sinfo->groups_sem);
679 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
680 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
681 val += block_group->length;
683 val += block_group->used;
685 up_read(&sinfo->groups_sem);
686 return sysfs_emit(buf, "%llu\n", val);
690 * Allocation information about block group profiles.
692 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
694 static struct attribute *raid_attrs[] = {
695 BTRFS_ATTR_PTR(raid, total_bytes),
696 BTRFS_ATTR_PTR(raid, used_bytes),
699 ATTRIBUTE_GROUPS(raid);
701 static void release_raid_kobj(struct kobject *kobj)
703 kfree(to_raid_kobj(kobj));
706 static const struct kobj_type btrfs_raid_ktype = {
707 .sysfs_ops = &kobj_sysfs_ops,
708 .release = release_raid_kobj,
709 .default_groups = raid_groups,
712 #define SPACE_INFO_ATTR(field) \
713 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
714 struct kobj_attribute *a, \
717 struct btrfs_space_info *sinfo = to_space_info(kobj); \
718 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
720 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
722 static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
723 struct kobj_attribute *a, char *buf)
725 struct btrfs_space_info *sinfo = to_space_info(kobj);
727 return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
731 * Store new chunk size in space info. Can be called on a read-only filesystem.
733 * If the new chunk size value is larger than 10% of free space it is reduced
734 * to match that limit. Alignment must be to 256M and the system chunk size
737 static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
738 struct kobj_attribute *a,
739 const char *buf, size_t len)
741 struct btrfs_space_info *space_info = to_space_info(kobj);
742 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
746 if (!capable(CAP_SYS_ADMIN))
749 if (!fs_info->fs_devices)
752 if (btrfs_is_zoned(fs_info))
755 /* System block type must not be changed. */
756 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
759 val = memparse(buf, &retptr);
760 /* There could be trailing '\n', also catch any typos after the value */
761 retptr = skip_spaces(retptr);
762 if (*retptr != 0 || val == 0)
765 val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
767 /* Limit stripe size to 10% of available space. */
768 val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val);
770 /* Must be multiple of 256M. */
771 val &= ~((u64)SZ_256M - 1);
773 /* Must be at least 256M. */
777 btrfs_update_space_info_chunk_size(space_info, val);
782 static ssize_t btrfs_size_classes_show(struct kobject *kobj,
783 struct kobj_attribute *a, char *buf)
785 struct btrfs_space_info *sinfo = to_space_info(kobj);
786 struct btrfs_block_group *bg;
792 for (int i = 0; i < BTRFS_NR_RAID_TYPES; ++i) {
793 down_read(&sinfo->groups_sem);
794 list_for_each_entry(bg, &sinfo->block_groups[i], list) {
795 if (!btrfs_block_group_should_use_size_class(bg))
797 switch (bg->size_class) {
798 case BTRFS_BG_SZ_NONE:
801 case BTRFS_BG_SZ_SMALL:
804 case BTRFS_BG_SZ_MEDIUM:
807 case BTRFS_BG_SZ_LARGE:
812 up_read(&sinfo->groups_sem);
814 return sysfs_emit(buf, "none %u\n"
818 none, small, medium, large);
821 #ifdef CONFIG_BTRFS_DEBUG
823 * Request chunk allocation with current chunk size.
825 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
826 struct kobj_attribute *a,
827 const char *buf, size_t len)
829 struct btrfs_space_info *space_info = to_space_info(kobj);
830 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
831 struct btrfs_trans_handle *trans;
835 if (!capable(CAP_SYS_ADMIN))
838 if (sb_rdonly(fs_info->sb))
841 ret = kstrtobool(buf, &val);
849 * This is unsafe to be called from sysfs context and may cause
850 * unexpected problems.
852 trans = btrfs_start_transaction(fs_info->tree_root, 0);
854 return PTR_ERR(trans);
855 ret = btrfs_force_chunk_alloc(trans, space_info->flags);
856 btrfs_end_transaction(trans);
863 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
867 SPACE_INFO_ATTR(flags);
868 SPACE_INFO_ATTR(total_bytes);
869 SPACE_INFO_ATTR(bytes_used);
870 SPACE_INFO_ATTR(bytes_pinned);
871 SPACE_INFO_ATTR(bytes_reserved);
872 SPACE_INFO_ATTR(bytes_may_use);
873 SPACE_INFO_ATTR(bytes_readonly);
874 SPACE_INFO_ATTR(bytes_zone_unusable);
875 SPACE_INFO_ATTR(disk_used);
876 SPACE_INFO_ATTR(disk_total);
877 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
878 BTRFS_ATTR(space_info, size_classes, btrfs_size_classes_show);
880 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
881 struct kobj_attribute *a,
884 struct btrfs_space_info *space_info = to_space_info(kobj);
886 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
889 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
890 struct kobj_attribute *a,
891 const char *buf, size_t len)
893 struct btrfs_space_info *space_info = to_space_info(kobj);
897 ret = kstrtoint(buf, 10, &thresh);
901 if (thresh < 0 || thresh > 100)
904 WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
909 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
910 btrfs_sinfo_bg_reclaim_threshold_show,
911 btrfs_sinfo_bg_reclaim_threshold_store);
914 * Allocation information about block group types.
916 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
918 static struct attribute *space_info_attrs[] = {
919 BTRFS_ATTR_PTR(space_info, flags),
920 BTRFS_ATTR_PTR(space_info, total_bytes),
921 BTRFS_ATTR_PTR(space_info, bytes_used),
922 BTRFS_ATTR_PTR(space_info, bytes_pinned),
923 BTRFS_ATTR_PTR(space_info, bytes_reserved),
924 BTRFS_ATTR_PTR(space_info, bytes_may_use),
925 BTRFS_ATTR_PTR(space_info, bytes_readonly),
926 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
927 BTRFS_ATTR_PTR(space_info, disk_used),
928 BTRFS_ATTR_PTR(space_info, disk_total),
929 BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
930 BTRFS_ATTR_PTR(space_info, chunk_size),
931 BTRFS_ATTR_PTR(space_info, size_classes),
932 #ifdef CONFIG_BTRFS_DEBUG
933 BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
937 ATTRIBUTE_GROUPS(space_info);
939 static void space_info_release(struct kobject *kobj)
941 struct btrfs_space_info *sinfo = to_space_info(kobj);
945 static const struct kobj_type space_info_ktype = {
946 .sysfs_ops = &kobj_sysfs_ops,
947 .release = space_info_release,
948 .default_groups = space_info_groups,
952 * Allocation information about block groups.
954 * Path: /sys/fs/btrfs/<uuid>/allocation/
956 static const struct attribute *allocation_attrs[] = {
957 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
958 BTRFS_ATTR_PTR(allocation, global_rsv_size),
962 static ssize_t btrfs_label_show(struct kobject *kobj,
963 struct kobj_attribute *a, char *buf)
965 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
966 char *label = fs_info->super_copy->label;
969 spin_lock(&fs_info->super_lock);
970 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
971 spin_unlock(&fs_info->super_lock);
976 static ssize_t btrfs_label_store(struct kobject *kobj,
977 struct kobj_attribute *a,
978 const char *buf, size_t len)
980 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
986 if (sb_rdonly(fs_info->sb))
990 * p_len is the len until the first occurrence of either
993 p_len = strcspn(buf, "\n");
995 if (p_len >= BTRFS_LABEL_SIZE)
998 spin_lock(&fs_info->super_lock);
999 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
1000 memcpy(fs_info->super_copy->label, buf, p_len);
1001 spin_unlock(&fs_info->super_lock);
1004 * We don't want to do full transaction commit from inside sysfs
1006 set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
1007 wake_up_process(fs_info->transaction_kthread);
1011 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
1013 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
1014 struct kobj_attribute *a, char *buf)
1016 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1018 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
1021 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
1023 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
1024 struct kobj_attribute *a, char *buf)
1026 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1028 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1031 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
1033 static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
1034 struct kobj_attribute *a, char *buf)
1036 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1038 return sysfs_emit(buf,
1040 "last_commit_ms %llu\n"
1041 "max_commit_ms %llu\n"
1042 "total_commit_ms %llu\n",
1043 fs_info->commit_stats.commit_count,
1044 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1045 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1046 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1049 static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1050 struct kobj_attribute *a,
1051 const char *buf, size_t len)
1053 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1060 if (!capable(CAP_SYS_RESOURCE))
1063 ret = kstrtoul(buf, 10, &val);
1069 WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1073 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1075 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1076 struct kobj_attribute *a, char *buf)
1078 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1080 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1083 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1085 static ssize_t quota_override_show(struct kobject *kobj,
1086 struct kobj_attribute *a, char *buf)
1088 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1091 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1092 return sysfs_emit(buf, "%d\n", quota_override);
1095 static ssize_t quota_override_store(struct kobject *kobj,
1096 struct kobj_attribute *a,
1097 const char *buf, size_t len)
1099 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1106 if (!capable(CAP_SYS_RESOURCE))
1109 err = kstrtoul(buf, 10, &knob);
1116 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1118 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1123 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1125 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1126 struct kobj_attribute *a, char *buf)
1128 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1130 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1133 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1135 static ssize_t btrfs_checksum_show(struct kobject *kobj,
1136 struct kobj_attribute *a, char *buf)
1138 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1139 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1141 return sysfs_emit(buf, "%s (%s)\n",
1142 btrfs_super_csum_name(csum_type),
1143 crypto_shash_driver_name(fs_info->csum_shash));
1146 BTRFS_ATTR(, checksum, btrfs_checksum_show);
1148 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1149 struct kobj_attribute *a, char *buf)
1151 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1154 switch (READ_ONCE(fs_info->exclusive_operation)) {
1155 case BTRFS_EXCLOP_NONE:
1158 case BTRFS_EXCLOP_BALANCE:
1161 case BTRFS_EXCLOP_BALANCE_PAUSED:
1162 str = "balance paused\n";
1164 case BTRFS_EXCLOP_DEV_ADD:
1165 str = "device add\n";
1167 case BTRFS_EXCLOP_DEV_REMOVE:
1168 str = "device remove\n";
1170 case BTRFS_EXCLOP_DEV_REPLACE:
1171 str = "device replace\n";
1173 case BTRFS_EXCLOP_RESIZE:
1176 case BTRFS_EXCLOP_SWAP_ACTIVATE:
1177 str = "swap activate\n";
1183 return sysfs_emit(buf, "%s", str);
1185 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1187 static ssize_t btrfs_generation_show(struct kobject *kobj,
1188 struct kobj_attribute *a, char *buf)
1190 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1192 return sysfs_emit(buf, "%llu\n", fs_info->generation);
1194 BTRFS_ATTR(, generation, btrfs_generation_show);
1196 static const char * const btrfs_read_policy_name[] = { "pid" };
1198 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1199 struct kobj_attribute *a, char *buf)
1201 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1205 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1206 if (fs_devices->read_policy == i)
1207 ret += sysfs_emit_at(buf, ret, "%s[%s]",
1208 (ret == 0 ? "" : " "),
1209 btrfs_read_policy_name[i]);
1211 ret += sysfs_emit_at(buf, ret, "%s%s",
1212 (ret == 0 ? "" : " "),
1213 btrfs_read_policy_name[i]);
1216 ret += sysfs_emit_at(buf, ret, "\n");
1221 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1222 struct kobj_attribute *a,
1223 const char *buf, size_t len)
1225 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1228 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1229 if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
1230 if (i != fs_devices->read_policy) {
1231 fs_devices->read_policy = i;
1232 btrfs_info(fs_devices->fs_info,
1233 "read policy set to '%s'",
1234 btrfs_read_policy_name[i]);
1242 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1244 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1245 struct kobj_attribute *a,
1248 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1250 return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1253 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1254 struct kobj_attribute *a,
1255 const char *buf, size_t len)
1257 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1261 ret = kstrtoint(buf, 10, &thresh);
1265 #ifdef CONFIG_BTRFS_DEBUG
1266 if (thresh != 0 && (thresh > 100))
1269 if (thresh != 0 && (thresh <= 50 || thresh > 100))
1273 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1277 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1278 btrfs_bg_reclaim_threshold_store);
1281 * Per-filesystem information and stats.
1283 * Path: /sys/fs/btrfs/<uuid>/
1285 static const struct attribute *btrfs_attrs[] = {
1286 BTRFS_ATTR_PTR(, label),
1287 BTRFS_ATTR_PTR(, nodesize),
1288 BTRFS_ATTR_PTR(, sectorsize),
1289 BTRFS_ATTR_PTR(, clone_alignment),
1290 BTRFS_ATTR_PTR(, quota_override),
1291 BTRFS_ATTR_PTR(, metadata_uuid),
1292 BTRFS_ATTR_PTR(, checksum),
1293 BTRFS_ATTR_PTR(, exclusive_operation),
1294 BTRFS_ATTR_PTR(, generation),
1295 BTRFS_ATTR_PTR(, read_policy),
1296 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1297 BTRFS_ATTR_PTR(, commit_stats),
1301 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1303 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1305 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1306 complete(&fs_devs->kobj_unregister);
1309 static const struct kobj_type btrfs_ktype = {
1310 .sysfs_ops = &kobj_sysfs_ops,
1311 .release = btrfs_release_fsid_kobj,
1314 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1316 if (kobj->ktype != &btrfs_ktype)
1318 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1321 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1323 if (kobj->ktype != &btrfs_ktype)
1325 return to_fs_devs(kobj)->fs_info;
1328 static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1331 if (kobj->ktype == &btrfs_ktype)
1333 kobj = kobj->parent;
1338 #define NUM_FEATURE_BITS 64
1339 #define BTRFS_FEATURE_NAME_MAX 13
1340 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1341 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1343 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1344 ARRAY_SIZE(btrfs_feature_attrs));
1345 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1346 ARRAY_SIZE(btrfs_feature_attrs[0]));
1348 static const u64 supported_feature_masks[FEAT_MAX] = {
1349 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1350 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1351 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1354 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1358 for (set = 0; set < FEAT_MAX; set++) {
1360 struct attribute *attrs[2];
1361 struct attribute_group agroup = {
1365 u64 features = get_features(fs_info, set);
1366 features &= ~supported_feature_masks[set];
1372 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1373 struct btrfs_feature_attr *fa;
1375 if (!(features & (1ULL << i)))
1378 fa = &btrfs_feature_attrs[set][i];
1379 attrs[0] = &fa->kobj_attr.attr;
1382 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1387 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1395 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1397 if (fs_devs->devinfo_kobj) {
1398 kobject_del(fs_devs->devinfo_kobj);
1399 kobject_put(fs_devs->devinfo_kobj);
1400 fs_devs->devinfo_kobj = NULL;
1403 if (fs_devs->devices_kobj) {
1404 kobject_del(fs_devs->devices_kobj);
1405 kobject_put(fs_devs->devices_kobj);
1406 fs_devs->devices_kobj = NULL;
1409 if (fs_devs->fsid_kobj.state_initialized) {
1410 kobject_del(&fs_devs->fsid_kobj);
1411 kobject_put(&fs_devs->fsid_kobj);
1412 wait_for_completion(&fs_devs->kobj_unregister);
1416 /* when fs_devs is NULL it will remove all fsid kobject */
1417 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1419 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1422 __btrfs_sysfs_remove_fsid(fs_devs);
1426 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1427 __btrfs_sysfs_remove_fsid(fs_devs);
1431 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1433 struct btrfs_device *device;
1434 struct btrfs_fs_devices *seed;
1436 list_for_each_entry(device, &fs_devices->devices, dev_list)
1437 btrfs_sysfs_remove_device(device);
1439 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1440 list_for_each_entry(device, &seed->devices, dev_list)
1441 btrfs_sysfs_remove_device(device);
1445 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1447 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1449 sysfs_remove_link(fsid_kobj, "bdi");
1451 if (fs_info->space_info_kobj) {
1452 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1453 kobject_del(fs_info->space_info_kobj);
1454 kobject_put(fs_info->space_info_kobj);
1456 if (fs_info->discard_kobj) {
1457 sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1458 kobject_del(fs_info->discard_kobj);
1459 kobject_put(fs_info->discard_kobj);
1461 #ifdef CONFIG_BTRFS_DEBUG
1462 if (fs_info->debug_kobj) {
1463 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1464 kobject_del(fs_info->debug_kobj);
1465 kobject_put(fs_info->debug_kobj);
1468 addrm_unknown_feature_attrs(fs_info, false);
1469 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1470 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1471 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1474 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1475 [FEAT_COMPAT] = "compat",
1476 [FEAT_COMPAT_RO] = "compat_ro",
1477 [FEAT_INCOMPAT] = "incompat",
1480 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1482 return btrfs_feature_set_names[set];
1485 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1487 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1492 str = kmalloc(bufsize, GFP_KERNEL);
1496 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1499 if (!(flags & (1ULL << i)))
1502 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1503 len += scnprintf(str + len, bufsize - len, "%s%s",
1504 len ? "," : "", name);
1510 static void init_feature_attrs(void)
1512 struct btrfs_feature_attr *fa;
1515 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1516 memset(btrfs_unknown_feature_names, 0,
1517 sizeof(btrfs_unknown_feature_names));
1519 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1520 struct btrfs_feature_attr *sfa;
1521 struct attribute *a = btrfs_supported_feature_attrs[i];
1523 sfa = attr_to_btrfs_feature_attr(a);
1524 bit = ilog2(sfa->feature_bit);
1525 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1527 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1530 for (set = 0; set < FEAT_MAX; set++) {
1531 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1532 char *name = btrfs_unknown_feature_names[set][i];
1533 fa = &btrfs_feature_attrs[set][i];
1535 if (fa->kobj_attr.attr.name)
1538 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1539 btrfs_feature_set_names[set], i);
1541 fa->kobj_attr.attr.name = name;
1542 fa->kobj_attr.attr.mode = S_IRUGO;
1543 fa->feature_set = set;
1544 fa->feature_bit = 1ULL << i;
1550 * Create a sysfs entry for a given block group type at path
1551 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1553 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1555 struct btrfs_fs_info *fs_info = cache->fs_info;
1556 struct btrfs_space_info *space_info = cache->space_info;
1557 struct raid_kobject *rkobj;
1558 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1559 unsigned int nofs_flag;
1563 * Setup a NOFS context because kobject_add(), deep in its call chain,
1564 * does GFP_KERNEL allocations, and we are often called in a context
1565 * where if reclaim is triggered we can deadlock (we are either holding
1566 * a transaction handle or some lock required for a transaction
1569 nofs_flag = memalloc_nofs_save();
1571 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1573 memalloc_nofs_restore(nofs_flag);
1574 btrfs_warn(cache->fs_info,
1575 "couldn't alloc memory for raid level kobject");
1579 rkobj->flags = cache->flags;
1580 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1583 * We call this either on mount, or if we've created a block group for a
1584 * new index type while running (i.e. when restriping). The running
1585 * case is tricky because we could race with other threads, so we need
1586 * to have this check to make sure we didn't already init the kobject.
1588 * We don't have to protect on the free side because it only happens on
1591 spin_lock(&space_info->lock);
1592 if (space_info->block_group_kobjs[index]) {
1593 spin_unlock(&space_info->lock);
1594 kobject_put(&rkobj->kobj);
1597 space_info->block_group_kobjs[index] = &rkobj->kobj;
1599 spin_unlock(&space_info->lock);
1601 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1602 btrfs_bg_type_to_raid_name(rkobj->flags));
1603 memalloc_nofs_restore(nofs_flag);
1605 spin_lock(&space_info->lock);
1606 space_info->block_group_kobjs[index] = NULL;
1607 spin_unlock(&space_info->lock);
1608 kobject_put(&rkobj->kobj);
1610 "failed to add kobject for block cache, ignoring");
1616 * Remove sysfs directories for all block group types of a given space info and
1617 * the space info as well
1619 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1623 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1624 struct kobject *kobj;
1626 kobj = space_info->block_group_kobjs[i];
1627 space_info->block_group_kobjs[i] = NULL;
1633 kobject_del(&space_info->kobj);
1634 kobject_put(&space_info->kobj);
1637 static const char *alloc_name(u64 flags)
1640 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1642 case BTRFS_BLOCK_GROUP_METADATA:
1644 case BTRFS_BLOCK_GROUP_DATA:
1646 case BTRFS_BLOCK_GROUP_SYSTEM:
1650 return "invalid-combination";
1655 * Create a sysfs entry for a space info type at path
1656 * /sys/fs/btrfs/UUID/allocation/TYPE
1658 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1659 struct btrfs_space_info *space_info)
1663 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1664 fs_info->space_info_kobj, "%s",
1665 alloc_name(space_info->flags));
1667 kobject_put(&space_info->kobj);
1674 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1676 struct kobject *devices_kobj;
1679 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1680 * fs_info::fs_devices.
1682 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1683 ASSERT(devices_kobj);
1686 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1688 if (device->devid_kobj.state_initialized) {
1689 kobject_del(&device->devid_kobj);
1690 kobject_put(&device->devid_kobj);
1691 wait_for_completion(&device->kobj_unregister);
1695 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1696 struct kobj_attribute *a,
1700 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1703 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1705 return sysfs_emit(buf, "%d\n", val);
1707 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1709 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1710 struct kobj_attribute *a, char *buf)
1713 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1716 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1718 return sysfs_emit(buf, "%d\n", val);
1720 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1722 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1723 struct kobj_attribute *a,
1727 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1730 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1732 return sysfs_emit(buf, "%d\n", val);
1734 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1736 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1737 struct kobj_attribute *a,
1740 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1743 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1746 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1747 struct kobj_attribute *a,
1748 const char *buf, size_t len)
1750 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1753 unsigned long long limit;
1755 limit = memparse(buf, &endptr);
1756 WRITE_ONCE(device->scrub_speed_max, limit);
1759 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1760 btrfs_devinfo_scrub_speed_max_store);
1762 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1763 struct kobj_attribute *a, char *buf)
1766 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1769 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1771 return sysfs_emit(buf, "%d\n", val);
1773 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1775 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1776 struct kobj_attribute *a, char *buf)
1778 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1781 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1783 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1785 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1786 struct kobj_attribute *a, char *buf)
1788 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1791 if (!device->dev_stats_valid)
1792 return sysfs_emit(buf, "invalid\n");
1795 * Print all at once so we get a snapshot of all values from the same
1796 * time. Keep them in sync and in order of definition of
1797 * btrfs_dev_stat_values.
1799 return sysfs_emit(buf,
1803 "corruption_errs %d\n"
1804 "generation_errs %d\n",
1805 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1806 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1807 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1808 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1809 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1811 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1814 * Information about one device.
1816 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1818 static struct attribute *devid_attrs[] = {
1819 BTRFS_ATTR_PTR(devid, error_stats),
1820 BTRFS_ATTR_PTR(devid, fsid),
1821 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1822 BTRFS_ATTR_PTR(devid, missing),
1823 BTRFS_ATTR_PTR(devid, replace_target),
1824 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1825 BTRFS_ATTR_PTR(devid, writeable),
1828 ATTRIBUTE_GROUPS(devid);
1830 static void btrfs_release_devid_kobj(struct kobject *kobj)
1832 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1835 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1836 complete(&device->kobj_unregister);
1839 static const struct kobj_type devid_ktype = {
1840 .sysfs_ops = &kobj_sysfs_ops,
1841 .default_groups = devid_groups,
1842 .release = btrfs_release_devid_kobj,
1845 int btrfs_sysfs_add_device(struct btrfs_device *device)
1848 unsigned int nofs_flag;
1849 struct kobject *devices_kobj;
1850 struct kobject *devinfo_kobj;
1853 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1854 * for the seed fs_devices
1856 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1857 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1858 ASSERT(devices_kobj);
1859 ASSERT(devinfo_kobj);
1861 nofs_flag = memalloc_nofs_save();
1864 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1866 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1868 btrfs_warn(device->fs_info,
1869 "creating sysfs device link for devid %llu failed: %d",
1870 device->devid, ret);
1875 init_completion(&device->kobj_unregister);
1876 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1877 devinfo_kobj, "%llu", device->devid);
1879 kobject_put(&device->devid_kobj);
1880 btrfs_warn(device->fs_info,
1881 "devinfo init for devid %llu failed: %d",
1882 device->devid, ret);
1886 memalloc_nofs_restore(nofs_flag);
1890 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1893 struct btrfs_device *device;
1894 struct btrfs_fs_devices *seed;
1896 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1897 ret = btrfs_sysfs_add_device(device);
1902 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1903 list_for_each_entry(device, &seed->devices, dev_list) {
1904 ret = btrfs_sysfs_add_device(device);
1913 btrfs_sysfs_remove_fs_devices(fs_devices);
1917 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1921 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1923 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1924 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1925 &disk_to_dev(bdev->bd_disk)->kobj);
1928 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1931 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1934 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1937 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1938 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1939 btrfs_warn(fs_devices->fs_info,
1940 "sysfs: failed to create fsid for sprout");
1943 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1947 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1949 if (kobject_rename(&device->devid_kobj, tmp))
1950 btrfs_warn(device->fs_devices->fs_info,
1951 "sysfs: failed to update devid for %llu",
1955 /* /sys/fs/btrfs/ entry */
1956 static struct kset *btrfs_kset;
1960 * /sys/fs/btrfs/UUID
1962 * Can be called by the device discovery thread.
1964 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1968 init_completion(&fs_devs->kobj_unregister);
1969 fs_devs->fsid_kobj.kset = btrfs_kset;
1970 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1971 "%pU", fs_devs->fsid);
1973 kobject_put(&fs_devs->fsid_kobj);
1977 fs_devs->devices_kobj = kobject_create_and_add("devices",
1978 &fs_devs->fsid_kobj);
1979 if (!fs_devs->devices_kobj) {
1980 btrfs_err(fs_devs->fs_info,
1981 "failed to init sysfs device interface");
1982 btrfs_sysfs_remove_fsid(fs_devs);
1986 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1987 &fs_devs->fsid_kobj);
1988 if (!fs_devs->devinfo_kobj) {
1989 btrfs_err(fs_devs->fs_info,
1990 "failed to init sysfs devinfo kobject");
1991 btrfs_sysfs_remove_fsid(fs_devs);
1998 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
2001 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
2002 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
2004 error = btrfs_sysfs_add_fs_devices(fs_devs);
2008 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
2010 btrfs_sysfs_remove_fs_devices(fs_devs);
2014 error = sysfs_create_group(fsid_kobj,
2015 &btrfs_feature_attr_group);
2019 #ifdef CONFIG_BTRFS_DEBUG
2020 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
2021 if (!fs_info->debug_kobj) {
2026 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
2031 /* Discard directory */
2032 fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
2033 if (!fs_info->discard_kobj) {
2038 error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
2042 error = addrm_unknown_feature_attrs(fs_info, true);
2046 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2050 fs_info->space_info_kobj = kobject_create_and_add("allocation",
2052 if (!fs_info->space_info_kobj) {
2057 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2063 btrfs_sysfs_remove_mounted(fs_info);
2067 static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2068 struct kobj_attribute *a,
2071 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2074 spin_lock(&fs_info->qgroup_lock);
2075 enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
2076 spin_unlock(&fs_info->qgroup_lock);
2078 return sysfs_emit(buf, "%d\n", enabled);
2080 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2082 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2083 struct kobj_attribute *a,
2086 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2089 spin_lock(&fs_info->qgroup_lock);
2090 inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
2091 spin_unlock(&fs_info->qgroup_lock);
2093 return sysfs_emit(buf, "%d\n", inconsistent);
2095 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2097 static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2098 struct kobj_attribute *a,
2101 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2104 spin_lock(&fs_info->qgroup_lock);
2105 result = fs_info->qgroup_drop_subtree_thres;
2106 spin_unlock(&fs_info->qgroup_lock);
2108 return sysfs_emit(buf, "%d\n", result);
2111 static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2112 struct kobj_attribute *a,
2113 const char *buf, size_t len)
2115 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2119 ret = kstrtou8(buf, 10, &new_thres);
2123 if (new_thres > BTRFS_MAX_LEVEL)
2126 spin_lock(&fs_info->qgroup_lock);
2127 fs_info->qgroup_drop_subtree_thres = new_thres;
2128 spin_unlock(&fs_info->qgroup_lock);
2132 BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2133 qgroup_drop_subtree_thres_store);
2136 * Qgroups global info
2138 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2140 static struct attribute *qgroups_attrs[] = {
2141 BTRFS_ATTR_PTR(qgroups, enabled),
2142 BTRFS_ATTR_PTR(qgroups, inconsistent),
2143 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2146 ATTRIBUTE_GROUPS(qgroups);
2148 static void qgroups_release(struct kobject *kobj)
2153 static const struct kobj_type qgroups_ktype = {
2154 .sysfs_ops = &kobj_sysfs_ops,
2155 .default_groups = qgroups_groups,
2156 .release = qgroups_release,
2159 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2161 return to_fs_info(kobj->parent->parent);
2164 #define QGROUP_ATTR(_member, _show_name) \
2165 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
2166 struct kobj_attribute *a, \
2169 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2170 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2171 struct btrfs_qgroup, kobj); \
2172 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
2174 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2176 #define QGROUP_RSV_ATTR(_name, _type) \
2177 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
2178 struct kobj_attribute *a, \
2181 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2182 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2183 struct btrfs_qgroup, kobj); \
2184 return btrfs_show_u64(&qgroup->rsv.values[_type], \
2185 &fs_info->qgroup_lock, buf); \
2187 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2189 QGROUP_ATTR(rfer, referenced);
2190 QGROUP_ATTR(excl, exclusive);
2191 QGROUP_ATTR(max_rfer, max_referenced);
2192 QGROUP_ATTR(max_excl, max_exclusive);
2193 QGROUP_ATTR(lim_flags, limit_flags);
2194 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2195 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2196 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2199 * Qgroup information.
2201 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2203 static struct attribute *qgroup_attrs[] = {
2204 BTRFS_ATTR_PTR(qgroup, referenced),
2205 BTRFS_ATTR_PTR(qgroup, exclusive),
2206 BTRFS_ATTR_PTR(qgroup, max_referenced),
2207 BTRFS_ATTR_PTR(qgroup, max_exclusive),
2208 BTRFS_ATTR_PTR(qgroup, limit_flags),
2209 BTRFS_ATTR_PTR(qgroup, rsv_data),
2210 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2211 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2214 ATTRIBUTE_GROUPS(qgroup);
2216 static void qgroup_release(struct kobject *kobj)
2218 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2220 memset(&qgroup->kobj, 0, sizeof(*kobj));
2223 static const struct kobj_type qgroup_ktype = {
2224 .sysfs_ops = &kobj_sysfs_ops,
2225 .release = qgroup_release,
2226 .default_groups = qgroup_groups,
2229 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2230 struct btrfs_qgroup *qgroup)
2232 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2235 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2237 if (qgroup->kobj.state_initialized)
2242 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2243 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2244 btrfs_qgroup_subvolid(qgroup->qgroupid));
2246 kobject_put(&qgroup->kobj);
2251 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2253 struct btrfs_qgroup *qgroup;
2254 struct btrfs_qgroup *next;
2256 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2259 rbtree_postorder_for_each_entry_safe(qgroup, next,
2260 &fs_info->qgroup_tree, node)
2261 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2262 if (fs_info->qgroups_kobj) {
2263 kobject_del(fs_info->qgroups_kobj);
2264 kobject_put(fs_info->qgroups_kobj);
2265 fs_info->qgroups_kobj = NULL;
2269 /* Called when qgroups get initialized, thus there is no need for locking */
2270 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2272 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2273 struct btrfs_qgroup *qgroup;
2274 struct btrfs_qgroup *next;
2277 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2281 if (fs_info->qgroups_kobj)
2284 fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
2285 if (!fs_info->qgroups_kobj)
2288 ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2289 fsid_kobj, "qgroups");
2293 rbtree_postorder_for_each_entry_safe(qgroup, next,
2294 &fs_info->qgroup_tree, node) {
2295 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2302 btrfs_sysfs_del_qgroups(fs_info);
2306 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2307 struct btrfs_qgroup *qgroup)
2309 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2312 if (qgroup->kobj.state_initialized) {
2313 kobject_del(&qgroup->kobj);
2314 kobject_put(&qgroup->kobj);
2319 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2320 * values in superblock. Call after any changes to incompat/compat_ro flags
2322 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info)
2324 struct kobject *fsid_kobj;
2330 fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2331 if (!fsid_kobj->state_initialized)
2334 ret = sysfs_update_group(fsid_kobj, &btrfs_feature_attr_group);
2337 "failed to update /sys/fs/btrfs/%pU/features: %d",
2338 fs_info->fs_devices->fsid, ret);
2341 int __init btrfs_init_sysfs(void)
2345 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2349 init_feature_attrs();
2350 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2353 ret = sysfs_merge_group(&btrfs_kset->kobj,
2354 &btrfs_static_feature_attr_group);
2356 goto out_remove_group;
2358 #ifdef CONFIG_BTRFS_DEBUG
2359 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2361 sysfs_unmerge_group(&btrfs_kset->kobj,
2362 &btrfs_static_feature_attr_group);
2363 goto out_remove_group;
2370 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2372 kset_unregister(btrfs_kset);
2377 void __cold btrfs_exit_sysfs(void)
2379 sysfs_unmerge_group(&btrfs_kset->kobj,
2380 &btrfs_static_feature_attr_group);
2381 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2382 #ifdef CONFIG_BTRFS_DEBUG
2383 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2385 kset_unregister(btrfs_kset);