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"
27 * --------------------------------------------------------------------------
28 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
29 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and
30 * /sys/fs/btrfs/<uuid>/features
31 * btrfs_attrs /sys/fs/btrfs/<uuid>
32 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid>
33 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation
34 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
35 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>
36 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
38 * When built with BTRFS_CONFIG_DEBUG:
40 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
41 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
42 * discard_debug_attrs /sys/fs/btrfs/<uuid>/debug/discard
45 struct btrfs_feature_attr {
46 struct kobj_attribute kobj_attr;
47 enum btrfs_feature_set feature_set;
51 /* For raid type sysfs entries */
57 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
59 .attr = { .name = __stringify(_name), .mode = _mode }, \
64 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
65 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
66 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
68 #define BTRFS_ATTR(_prefix, _name, _show) \
69 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
70 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
72 #define BTRFS_ATTR_PTR(_prefix, _name) \
73 (&btrfs_attr_##_prefix##_##_name.attr)
75 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
76 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
77 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
78 btrfs_feature_attr_show, \
79 btrfs_feature_attr_store), \
80 .feature_set = _feature_set, \
81 .feature_bit = _feature_prefix ##_## _feature_bit, \
83 #define BTRFS_FEAT_ATTR_PTR(_name) \
84 (&btrfs_attr_features_##_name.kobj_attr.attr)
86 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
87 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
88 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
89 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
90 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
91 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
93 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
94 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
96 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
98 return container_of(a, struct btrfs_feature_attr, kobj_attr);
101 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
103 return container_of(attr, struct kobj_attribute, attr);
106 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
107 struct attribute *attr)
109 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
112 static u64 get_features(struct btrfs_fs_info *fs_info,
113 enum btrfs_feature_set set)
115 struct btrfs_super_block *disk_super = fs_info->super_copy;
116 if (set == FEAT_COMPAT)
117 return btrfs_super_compat_flags(disk_super);
118 else if (set == FEAT_COMPAT_RO)
119 return btrfs_super_compat_ro_flags(disk_super);
121 return btrfs_super_incompat_flags(disk_super);
124 static void set_features(struct btrfs_fs_info *fs_info,
125 enum btrfs_feature_set set, u64 features)
127 struct btrfs_super_block *disk_super = fs_info->super_copy;
128 if (set == FEAT_COMPAT)
129 btrfs_set_super_compat_flags(disk_super, features);
130 else if (set == FEAT_COMPAT_RO)
131 btrfs_set_super_compat_ro_flags(disk_super, features);
133 btrfs_set_super_incompat_flags(disk_super, features);
136 static int can_modify_feature(struct btrfs_feature_attr *fa)
140 switch (fa->feature_set) {
142 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
143 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
146 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
147 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
150 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
151 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
154 pr_warn("btrfs: sysfs: unknown feature set %d\n",
159 if (set & fa->feature_bit)
161 if (clear & fa->feature_bit)
167 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
168 struct kobj_attribute *a, char *buf)
171 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
172 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
174 u64 features = get_features(fs_info, fa->feature_set);
175 if (features & fa->feature_bit)
178 val = can_modify_feature(fa);
180 return sysfs_emit(buf, "%d\n", val);
183 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
184 struct kobj_attribute *a,
185 const char *buf, size_t count)
187 struct btrfs_fs_info *fs_info;
188 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
189 u64 features, set, clear;
193 fs_info = to_fs_info(kobj);
197 if (sb_rdonly(fs_info->sb))
200 ret = kstrtoul(skip_spaces(buf), 0, &val);
204 if (fa->feature_set == FEAT_COMPAT) {
205 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
206 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
207 } else if (fa->feature_set == FEAT_COMPAT_RO) {
208 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
209 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
211 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
212 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
215 features = get_features(fs_info, fa->feature_set);
218 if ((val && (features & fa->feature_bit)) ||
219 (!val && !(features & fa->feature_bit)))
222 if ((val && !(set & fa->feature_bit)) ||
223 (!val && !(clear & fa->feature_bit))) {
225 "%sabling feature %s on mounted fs is not supported.",
226 val ? "En" : "Dis", fa->kobj_attr.attr.name);
230 btrfs_info(fs_info, "%s %s feature flag",
231 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
233 spin_lock(&fs_info->super_lock);
234 features = get_features(fs_info, fa->feature_set);
236 features |= fa->feature_bit;
238 features &= ~fa->feature_bit;
239 set_features(fs_info, fa->feature_set, features);
240 spin_unlock(&fs_info->super_lock);
243 * We don't want to do full transaction commit from inside sysfs
245 btrfs_set_pending(fs_info, COMMIT);
246 wake_up_process(fs_info->transaction_kthread);
251 static umode_t btrfs_feature_visible(struct kobject *kobj,
252 struct attribute *attr, int unused)
254 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
255 umode_t mode = attr->mode;
258 struct btrfs_feature_attr *fa;
261 fa = attr_to_btrfs_feature_attr(attr);
262 features = get_features(fs_info, fa->feature_set);
264 if (can_modify_feature(fa))
266 else if (!(features & fa->feature_bit))
273 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
274 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
275 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
276 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
277 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
278 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
279 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
280 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
281 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
282 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
283 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
284 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
285 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
286 #ifdef CONFIG_BTRFS_DEBUG
287 /* Remove once support for zoned allocation is feature complete */
288 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
289 /* Remove once support for extent tree v2 is feature complete */
290 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
292 #ifdef CONFIG_FS_VERITY
293 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
297 * Features which depend on feature bits and may differ between each fs.
299 * /sys/fs/btrfs/features - all available features implemeted by this version
300 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
301 * can be changed on a mounted filesystem.
303 static struct attribute *btrfs_supported_feature_attrs[] = {
304 BTRFS_FEAT_ATTR_PTR(mixed_backref),
305 BTRFS_FEAT_ATTR_PTR(default_subvol),
306 BTRFS_FEAT_ATTR_PTR(mixed_groups),
307 BTRFS_FEAT_ATTR_PTR(compress_lzo),
308 BTRFS_FEAT_ATTR_PTR(compress_zstd),
309 BTRFS_FEAT_ATTR_PTR(big_metadata),
310 BTRFS_FEAT_ATTR_PTR(extended_iref),
311 BTRFS_FEAT_ATTR_PTR(raid56),
312 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
313 BTRFS_FEAT_ATTR_PTR(no_holes),
314 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
315 BTRFS_FEAT_ATTR_PTR(free_space_tree),
316 BTRFS_FEAT_ATTR_PTR(raid1c34),
317 #ifdef CONFIG_BTRFS_DEBUG
318 BTRFS_FEAT_ATTR_PTR(zoned),
319 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
321 #ifdef CONFIG_FS_VERITY
322 BTRFS_FEAT_ATTR_PTR(verity),
327 static const struct attribute_group btrfs_feature_attr_group = {
329 .is_visible = btrfs_feature_visible,
330 .attrs = btrfs_supported_feature_attrs,
333 static ssize_t rmdir_subvol_show(struct kobject *kobj,
334 struct kobj_attribute *ka, char *buf)
336 return sysfs_emit(buf, "0\n");
338 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
340 static ssize_t supported_checksums_show(struct kobject *kobj,
341 struct kobj_attribute *a, char *buf)
346 for (i = 0; i < btrfs_get_num_csums(); i++) {
348 * This "trick" only works as long as 'enum btrfs_csum_type' has
351 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
352 btrfs_super_csum_name(i));
356 ret += sysfs_emit_at(buf, ret, "\n");
359 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
361 static ssize_t send_stream_version_show(struct kobject *kobj,
362 struct kobj_attribute *ka, char *buf)
364 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
366 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
368 static const char *rescue_opts[] = {
376 static ssize_t supported_rescue_options_show(struct kobject *kobj,
377 struct kobj_attribute *a,
383 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
384 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
385 ret += sysfs_emit_at(buf, ret, "\n");
388 BTRFS_ATTR(static_feature, supported_rescue_options,
389 supported_rescue_options_show);
391 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
392 struct kobj_attribute *a,
397 /* 4K sector size is also supported with 64K page size */
398 if (PAGE_SIZE == SZ_64K)
399 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
401 /* Only sectorsize == PAGE_SIZE is now supported */
402 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
406 BTRFS_ATTR(static_feature, supported_sectorsizes,
407 supported_sectorsizes_show);
410 * Features which only depend on kernel version.
412 * These are listed in /sys/fs/btrfs/features along with
413 * btrfs_supported_feature_attrs.
415 static struct attribute *btrfs_supported_static_feature_attrs[] = {
416 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
417 BTRFS_ATTR_PTR(static_feature, supported_checksums),
418 BTRFS_ATTR_PTR(static_feature, send_stream_version),
419 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
420 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
424 static const struct attribute_group btrfs_static_feature_attr_group = {
426 .attrs = btrfs_supported_static_feature_attrs,
429 #ifdef CONFIG_BTRFS_DEBUG
432 * Discard statistics and tunables
434 #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
436 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
437 struct kobj_attribute *a,
440 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
442 return sysfs_emit(buf, "%lld\n",
443 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
445 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
447 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
448 struct kobj_attribute *a,
451 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
453 return sysfs_emit(buf, "%d\n",
454 atomic_read(&fs_info->discard_ctl.discardable_extents));
456 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
458 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
459 struct kobj_attribute *a,
462 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
464 return sysfs_emit(buf, "%llu\n",
465 fs_info->discard_ctl.discard_bitmap_bytes);
467 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
469 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
470 struct kobj_attribute *a,
473 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
475 return sysfs_emit(buf, "%lld\n",
476 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
478 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
480 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
481 struct kobj_attribute *a,
484 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
486 return sysfs_emit(buf, "%llu\n",
487 fs_info->discard_ctl.discard_extent_bytes);
489 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
491 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
492 struct kobj_attribute *a,
495 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
497 return sysfs_emit(buf, "%u\n",
498 READ_ONCE(fs_info->discard_ctl.iops_limit));
501 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
502 struct kobj_attribute *a,
503 const char *buf, size_t len)
505 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
506 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
510 ret = kstrtou32(buf, 10, &iops_limit);
514 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
515 btrfs_discard_calc_delay(discard_ctl);
516 btrfs_discard_schedule_work(discard_ctl, true);
519 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
520 btrfs_discard_iops_limit_store);
522 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
523 struct kobj_attribute *a,
526 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
528 return sysfs_emit(buf, "%u\n",
529 READ_ONCE(fs_info->discard_ctl.kbps_limit));
532 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
533 struct kobj_attribute *a,
534 const char *buf, size_t len)
536 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
537 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
541 ret = kstrtou32(buf, 10, &kbps_limit);
545 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
546 btrfs_discard_schedule_work(discard_ctl, true);
549 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
550 btrfs_discard_kbps_limit_store);
552 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
553 struct kobj_attribute *a,
556 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
558 return sysfs_emit(buf, "%llu\n",
559 READ_ONCE(fs_info->discard_ctl.max_discard_size));
562 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
563 struct kobj_attribute *a,
564 const char *buf, size_t len)
566 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
567 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
568 u64 max_discard_size;
571 ret = kstrtou64(buf, 10, &max_discard_size);
575 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
579 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
580 btrfs_discard_max_discard_size_store);
583 * Per-filesystem debugging of discard (when mounted with discard=async).
585 * Path: /sys/fs/btrfs/<uuid>/debug/discard/
587 static const struct attribute *discard_debug_attrs[] = {
588 BTRFS_ATTR_PTR(discard, discardable_bytes),
589 BTRFS_ATTR_PTR(discard, discardable_extents),
590 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
591 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
592 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
593 BTRFS_ATTR_PTR(discard, iops_limit),
594 BTRFS_ATTR_PTR(discard, kbps_limit),
595 BTRFS_ATTR_PTR(discard, max_discard_size),
600 * Per-filesystem runtime debugging exported via sysfs.
602 * Path: /sys/fs/btrfs/UUID/debug/
604 static const struct attribute *btrfs_debug_mount_attrs[] = {
609 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
611 * Path: /sys/fs/btrfs/debug
613 static struct attribute *btrfs_debug_feature_attrs[] = {
617 static const struct attribute_group btrfs_debug_feature_attr_group = {
619 .attrs = btrfs_debug_feature_attrs,
624 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
632 return sysfs_emit(buf, "%llu\n", val);
635 static ssize_t global_rsv_size_show(struct kobject *kobj,
636 struct kobj_attribute *ka, char *buf)
638 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
639 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
640 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
642 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
644 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
645 struct kobj_attribute *a, char *buf)
647 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
648 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
649 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
651 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
653 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
654 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
656 static ssize_t raid_bytes_show(struct kobject *kobj,
657 struct kobj_attribute *attr, char *buf);
658 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
659 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
661 static ssize_t raid_bytes_show(struct kobject *kobj,
662 struct kobj_attribute *attr, char *buf)
665 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
666 struct btrfs_block_group *block_group;
667 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
670 down_read(&sinfo->groups_sem);
671 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
672 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
673 val += block_group->length;
675 val += block_group->used;
677 up_read(&sinfo->groups_sem);
678 return sysfs_emit(buf, "%llu\n", val);
682 * Allocation information about block group profiles.
684 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
686 static struct attribute *raid_attrs[] = {
687 BTRFS_ATTR_PTR(raid, total_bytes),
688 BTRFS_ATTR_PTR(raid, used_bytes),
691 ATTRIBUTE_GROUPS(raid);
693 static void release_raid_kobj(struct kobject *kobj)
695 kfree(to_raid_kobj(kobj));
698 static struct kobj_type btrfs_raid_ktype = {
699 .sysfs_ops = &kobj_sysfs_ops,
700 .release = release_raid_kobj,
701 .default_groups = raid_groups,
704 #define SPACE_INFO_ATTR(field) \
705 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
706 struct kobj_attribute *a, \
709 struct btrfs_space_info *sinfo = to_space_info(kobj); \
710 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
712 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
714 SPACE_INFO_ATTR(flags);
715 SPACE_INFO_ATTR(total_bytes);
716 SPACE_INFO_ATTR(bytes_used);
717 SPACE_INFO_ATTR(bytes_pinned);
718 SPACE_INFO_ATTR(bytes_reserved);
719 SPACE_INFO_ATTR(bytes_may_use);
720 SPACE_INFO_ATTR(bytes_readonly);
721 SPACE_INFO_ATTR(bytes_zone_unusable);
722 SPACE_INFO_ATTR(disk_used);
723 SPACE_INFO_ATTR(disk_total);
726 * Allocation information about block group types.
728 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
730 static struct attribute *space_info_attrs[] = {
731 BTRFS_ATTR_PTR(space_info, flags),
732 BTRFS_ATTR_PTR(space_info, total_bytes),
733 BTRFS_ATTR_PTR(space_info, bytes_used),
734 BTRFS_ATTR_PTR(space_info, bytes_pinned),
735 BTRFS_ATTR_PTR(space_info, bytes_reserved),
736 BTRFS_ATTR_PTR(space_info, bytes_may_use),
737 BTRFS_ATTR_PTR(space_info, bytes_readonly),
738 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
739 BTRFS_ATTR_PTR(space_info, disk_used),
740 BTRFS_ATTR_PTR(space_info, disk_total),
743 ATTRIBUTE_GROUPS(space_info);
745 static void space_info_release(struct kobject *kobj)
747 struct btrfs_space_info *sinfo = to_space_info(kobj);
751 static struct kobj_type space_info_ktype = {
752 .sysfs_ops = &kobj_sysfs_ops,
753 .release = space_info_release,
754 .default_groups = space_info_groups,
758 * Allocation information about block groups.
760 * Path: /sys/fs/btrfs/<uuid>/allocation/
762 static const struct attribute *allocation_attrs[] = {
763 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
764 BTRFS_ATTR_PTR(allocation, global_rsv_size),
768 static ssize_t btrfs_label_show(struct kobject *kobj,
769 struct kobj_attribute *a, char *buf)
771 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
772 char *label = fs_info->super_copy->label;
775 spin_lock(&fs_info->super_lock);
776 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
777 spin_unlock(&fs_info->super_lock);
782 static ssize_t btrfs_label_store(struct kobject *kobj,
783 struct kobj_attribute *a,
784 const char *buf, size_t len)
786 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
792 if (sb_rdonly(fs_info->sb))
796 * p_len is the len until the first occurrence of either
799 p_len = strcspn(buf, "\n");
801 if (p_len >= BTRFS_LABEL_SIZE)
804 spin_lock(&fs_info->super_lock);
805 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
806 memcpy(fs_info->super_copy->label, buf, p_len);
807 spin_unlock(&fs_info->super_lock);
810 * We don't want to do full transaction commit from inside sysfs
812 btrfs_set_pending(fs_info, COMMIT);
813 wake_up_process(fs_info->transaction_kthread);
817 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
819 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
820 struct kobj_attribute *a, char *buf)
822 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
824 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
827 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
829 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
830 struct kobj_attribute *a, char *buf)
832 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
834 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
837 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
839 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
840 struct kobj_attribute *a, char *buf)
842 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
844 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
847 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
849 static ssize_t quota_override_show(struct kobject *kobj,
850 struct kobj_attribute *a, char *buf)
852 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
855 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
856 return sysfs_emit(buf, "%d\n", quota_override);
859 static ssize_t quota_override_store(struct kobject *kobj,
860 struct kobj_attribute *a,
861 const char *buf, size_t len)
863 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
870 if (!capable(CAP_SYS_RESOURCE))
873 err = kstrtoul(buf, 10, &knob);
880 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
882 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
887 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
889 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
890 struct kobj_attribute *a, char *buf)
892 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
894 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
897 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
899 static ssize_t btrfs_checksum_show(struct kobject *kobj,
900 struct kobj_attribute *a, char *buf)
902 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
903 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
905 return sysfs_emit(buf, "%s (%s)\n",
906 btrfs_super_csum_name(csum_type),
907 crypto_shash_driver_name(fs_info->csum_shash));
910 BTRFS_ATTR(, checksum, btrfs_checksum_show);
912 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
913 struct kobj_attribute *a, char *buf)
915 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
918 switch (READ_ONCE(fs_info->exclusive_operation)) {
919 case BTRFS_EXCLOP_NONE:
922 case BTRFS_EXCLOP_BALANCE:
925 case BTRFS_EXCLOP_BALANCE_PAUSED:
926 str = "balance paused\n";
928 case BTRFS_EXCLOP_DEV_ADD:
929 str = "device add\n";
931 case BTRFS_EXCLOP_DEV_REMOVE:
932 str = "device remove\n";
934 case BTRFS_EXCLOP_DEV_REPLACE:
935 str = "device replace\n";
937 case BTRFS_EXCLOP_RESIZE:
940 case BTRFS_EXCLOP_SWAP_ACTIVATE:
941 str = "swap activate\n";
947 return sysfs_emit(buf, "%s", str);
949 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
951 static ssize_t btrfs_generation_show(struct kobject *kobj,
952 struct kobj_attribute *a, char *buf)
954 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
956 return sysfs_emit(buf, "%llu\n", fs_info->generation);
958 BTRFS_ATTR(, generation, btrfs_generation_show);
961 * Look for an exact string @string in @buffer with possible leading or
962 * trailing whitespace
964 static bool strmatch(const char *buffer, const char *string)
966 const size_t len = strlen(string);
968 /* Skip leading whitespace */
969 buffer = skip_spaces(buffer);
971 /* Match entire string, check if the rest is whitespace or empty */
972 if (strncmp(string, buffer, len) == 0 &&
973 strlen(skip_spaces(buffer + len)) == 0)
979 static const char * const btrfs_read_policy_name[] = { "pid" };
981 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
982 struct kobj_attribute *a, char *buf)
984 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
988 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
989 if (fs_devices->read_policy == i)
990 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
991 (ret == 0 ? "" : " "),
992 btrfs_read_policy_name[i]);
994 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
995 (ret == 0 ? "" : " "),
996 btrfs_read_policy_name[i]);
999 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
1004 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1005 struct kobj_attribute *a,
1006 const char *buf, size_t len)
1008 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1011 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1012 if (strmatch(buf, btrfs_read_policy_name[i])) {
1013 if (i != fs_devices->read_policy) {
1014 fs_devices->read_policy = i;
1015 btrfs_info(fs_devices->fs_info,
1016 "read policy set to '%s'",
1017 btrfs_read_policy_name[i]);
1025 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1027 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1028 struct kobj_attribute *a,
1031 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1034 ret = sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1039 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1040 struct kobj_attribute *a,
1041 const char *buf, size_t len)
1043 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1047 ret = kstrtoint(buf, 10, &thresh);
1051 if (thresh != 0 && (thresh <= 50 || thresh > 100))
1054 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1058 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1059 btrfs_bg_reclaim_threshold_store);
1062 * Per-filesystem information and stats.
1064 * Path: /sys/fs/btrfs/<uuid>/
1066 static const struct attribute *btrfs_attrs[] = {
1067 BTRFS_ATTR_PTR(, label),
1068 BTRFS_ATTR_PTR(, nodesize),
1069 BTRFS_ATTR_PTR(, sectorsize),
1070 BTRFS_ATTR_PTR(, clone_alignment),
1071 BTRFS_ATTR_PTR(, quota_override),
1072 BTRFS_ATTR_PTR(, metadata_uuid),
1073 BTRFS_ATTR_PTR(, checksum),
1074 BTRFS_ATTR_PTR(, exclusive_operation),
1075 BTRFS_ATTR_PTR(, generation),
1076 BTRFS_ATTR_PTR(, read_policy),
1077 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1081 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1083 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1085 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1086 complete(&fs_devs->kobj_unregister);
1089 static struct kobj_type btrfs_ktype = {
1090 .sysfs_ops = &kobj_sysfs_ops,
1091 .release = btrfs_release_fsid_kobj,
1094 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1096 if (kobj->ktype != &btrfs_ktype)
1098 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1101 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1103 if (kobj->ktype != &btrfs_ktype)
1105 return to_fs_devs(kobj)->fs_info;
1108 #define NUM_FEATURE_BITS 64
1109 #define BTRFS_FEATURE_NAME_MAX 13
1110 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1111 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1113 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1114 ARRAY_SIZE(btrfs_feature_attrs));
1115 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1116 ARRAY_SIZE(btrfs_feature_attrs[0]));
1118 static const u64 supported_feature_masks[FEAT_MAX] = {
1119 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1120 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1121 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1124 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1128 for (set = 0; set < FEAT_MAX; set++) {
1130 struct attribute *attrs[2];
1131 struct attribute_group agroup = {
1135 u64 features = get_features(fs_info, set);
1136 features &= ~supported_feature_masks[set];
1142 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1143 struct btrfs_feature_attr *fa;
1145 if (!(features & (1ULL << i)))
1148 fa = &btrfs_feature_attrs[set][i];
1149 attrs[0] = &fa->kobj_attr.attr;
1152 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1157 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1165 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1167 if (fs_devs->devinfo_kobj) {
1168 kobject_del(fs_devs->devinfo_kobj);
1169 kobject_put(fs_devs->devinfo_kobj);
1170 fs_devs->devinfo_kobj = NULL;
1173 if (fs_devs->devices_kobj) {
1174 kobject_del(fs_devs->devices_kobj);
1175 kobject_put(fs_devs->devices_kobj);
1176 fs_devs->devices_kobj = NULL;
1179 if (fs_devs->fsid_kobj.state_initialized) {
1180 kobject_del(&fs_devs->fsid_kobj);
1181 kobject_put(&fs_devs->fsid_kobj);
1182 wait_for_completion(&fs_devs->kobj_unregister);
1186 /* when fs_devs is NULL it will remove all fsid kobject */
1187 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1189 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1192 __btrfs_sysfs_remove_fsid(fs_devs);
1196 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1197 __btrfs_sysfs_remove_fsid(fs_devs);
1201 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1203 struct btrfs_device *device;
1204 struct btrfs_fs_devices *seed;
1206 list_for_each_entry(device, &fs_devices->devices, dev_list)
1207 btrfs_sysfs_remove_device(device);
1209 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1210 list_for_each_entry(device, &seed->devices, dev_list)
1211 btrfs_sysfs_remove_device(device);
1215 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1217 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1219 sysfs_remove_link(fsid_kobj, "bdi");
1221 if (fs_info->space_info_kobj) {
1222 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1223 kobject_del(fs_info->space_info_kobj);
1224 kobject_put(fs_info->space_info_kobj);
1226 #ifdef CONFIG_BTRFS_DEBUG
1227 if (fs_info->discard_debug_kobj) {
1228 sysfs_remove_files(fs_info->discard_debug_kobj,
1229 discard_debug_attrs);
1230 kobject_del(fs_info->discard_debug_kobj);
1231 kobject_put(fs_info->discard_debug_kobj);
1233 if (fs_info->debug_kobj) {
1234 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1235 kobject_del(fs_info->debug_kobj);
1236 kobject_put(fs_info->debug_kobj);
1239 addrm_unknown_feature_attrs(fs_info, false);
1240 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1241 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1242 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1245 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1246 [FEAT_COMPAT] = "compat",
1247 [FEAT_COMPAT_RO] = "compat_ro",
1248 [FEAT_INCOMPAT] = "incompat",
1251 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1253 return btrfs_feature_set_names[set];
1256 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1258 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1263 str = kmalloc(bufsize, GFP_KERNEL);
1267 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1270 if (!(flags & (1ULL << i)))
1273 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1274 len += scnprintf(str + len, bufsize - len, "%s%s",
1275 len ? "," : "", name);
1281 static void init_feature_attrs(void)
1283 struct btrfs_feature_attr *fa;
1286 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1287 memset(btrfs_unknown_feature_names, 0,
1288 sizeof(btrfs_unknown_feature_names));
1290 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1291 struct btrfs_feature_attr *sfa;
1292 struct attribute *a = btrfs_supported_feature_attrs[i];
1294 sfa = attr_to_btrfs_feature_attr(a);
1295 bit = ilog2(sfa->feature_bit);
1296 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1298 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1301 for (set = 0; set < FEAT_MAX; set++) {
1302 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1303 char *name = btrfs_unknown_feature_names[set][i];
1304 fa = &btrfs_feature_attrs[set][i];
1306 if (fa->kobj_attr.attr.name)
1309 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1310 btrfs_feature_set_names[set], i);
1312 fa->kobj_attr.attr.name = name;
1313 fa->kobj_attr.attr.mode = S_IRUGO;
1314 fa->feature_set = set;
1315 fa->feature_bit = 1ULL << i;
1321 * Create a sysfs entry for a given block group type at path
1322 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1324 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1326 struct btrfs_fs_info *fs_info = cache->fs_info;
1327 struct btrfs_space_info *space_info = cache->space_info;
1328 struct raid_kobject *rkobj;
1329 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1330 unsigned int nofs_flag;
1334 * Setup a NOFS context because kobject_add(), deep in its call chain,
1335 * does GFP_KERNEL allocations, and we are often called in a context
1336 * where if reclaim is triggered we can deadlock (we are either holding
1337 * a transaction handle or some lock required for a transaction
1340 nofs_flag = memalloc_nofs_save();
1342 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1344 memalloc_nofs_restore(nofs_flag);
1345 btrfs_warn(cache->fs_info,
1346 "couldn't alloc memory for raid level kobject");
1350 rkobj->flags = cache->flags;
1351 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1354 * We call this either on mount, or if we've created a block group for a
1355 * new index type while running (i.e. when restriping). The running
1356 * case is tricky because we could race with other threads, so we need
1357 * to have this check to make sure we didn't already init the kobject.
1359 * We don't have to protect on the free side because it only happens on
1362 spin_lock(&space_info->lock);
1363 if (space_info->block_group_kobjs[index]) {
1364 spin_unlock(&space_info->lock);
1365 kobject_put(&rkobj->kobj);
1368 space_info->block_group_kobjs[index] = &rkobj->kobj;
1370 spin_unlock(&space_info->lock);
1372 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1373 btrfs_bg_type_to_raid_name(rkobj->flags));
1374 memalloc_nofs_restore(nofs_flag);
1376 spin_lock(&space_info->lock);
1377 space_info->block_group_kobjs[index] = NULL;
1378 spin_unlock(&space_info->lock);
1379 kobject_put(&rkobj->kobj);
1381 "failed to add kobject for block cache, ignoring");
1387 * Remove sysfs directories for all block group types of a given space info and
1388 * the space info as well
1390 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1394 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1395 struct kobject *kobj;
1397 kobj = space_info->block_group_kobjs[i];
1398 space_info->block_group_kobjs[i] = NULL;
1404 kobject_del(&space_info->kobj);
1405 kobject_put(&space_info->kobj);
1408 static const char *alloc_name(u64 flags)
1411 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1413 case BTRFS_BLOCK_GROUP_METADATA:
1415 case BTRFS_BLOCK_GROUP_DATA:
1417 case BTRFS_BLOCK_GROUP_SYSTEM:
1421 return "invalid-combination";
1426 * Create a sysfs entry for a space info type at path
1427 * /sys/fs/btrfs/UUID/allocation/TYPE
1429 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1430 struct btrfs_space_info *space_info)
1434 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1435 fs_info->space_info_kobj, "%s",
1436 alloc_name(space_info->flags));
1438 kobject_put(&space_info->kobj);
1445 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1447 struct kobject *devices_kobj;
1450 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1451 * fs_info::fs_devices.
1453 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1454 ASSERT(devices_kobj);
1457 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1459 if (device->devid_kobj.state_initialized) {
1460 kobject_del(&device->devid_kobj);
1461 kobject_put(&device->devid_kobj);
1462 wait_for_completion(&device->kobj_unregister);
1466 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1467 struct kobj_attribute *a,
1471 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1474 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1476 return sysfs_emit(buf, "%d\n", val);
1478 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1480 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1481 struct kobj_attribute *a, char *buf)
1484 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1487 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1489 return sysfs_emit(buf, "%d\n", val);
1491 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1493 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1494 struct kobj_attribute *a,
1498 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1501 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1503 return sysfs_emit(buf, "%d\n", val);
1505 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1507 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1508 struct kobj_attribute *a,
1511 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1514 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1517 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1518 struct kobj_attribute *a,
1519 const char *buf, size_t len)
1521 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1524 unsigned long long limit;
1526 limit = memparse(buf, &endptr);
1527 WRITE_ONCE(device->scrub_speed_max, limit);
1530 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1531 btrfs_devinfo_scrub_speed_max_store);
1533 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1534 struct kobj_attribute *a, char *buf)
1537 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1540 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1542 return sysfs_emit(buf, "%d\n", val);
1544 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1546 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1547 struct kobj_attribute *a, char *buf)
1549 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1552 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1554 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1556 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1557 struct kobj_attribute *a, char *buf)
1559 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1562 if (!device->dev_stats_valid)
1563 return sysfs_emit(buf, "invalid\n");
1566 * Print all at once so we get a snapshot of all values from the same
1567 * time. Keep them in sync and in order of definition of
1568 * btrfs_dev_stat_values.
1570 return sysfs_emit(buf,
1574 "corruption_errs %d\n"
1575 "generation_errs %d\n",
1576 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1577 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1578 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1579 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1580 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1582 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1585 * Information about one device.
1587 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1589 static struct attribute *devid_attrs[] = {
1590 BTRFS_ATTR_PTR(devid, error_stats),
1591 BTRFS_ATTR_PTR(devid, fsid),
1592 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1593 BTRFS_ATTR_PTR(devid, missing),
1594 BTRFS_ATTR_PTR(devid, replace_target),
1595 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1596 BTRFS_ATTR_PTR(devid, writeable),
1599 ATTRIBUTE_GROUPS(devid);
1601 static void btrfs_release_devid_kobj(struct kobject *kobj)
1603 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1606 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1607 complete(&device->kobj_unregister);
1610 static struct kobj_type devid_ktype = {
1611 .sysfs_ops = &kobj_sysfs_ops,
1612 .default_groups = devid_groups,
1613 .release = btrfs_release_devid_kobj,
1616 int btrfs_sysfs_add_device(struct btrfs_device *device)
1619 unsigned int nofs_flag;
1620 struct kobject *devices_kobj;
1621 struct kobject *devinfo_kobj;
1624 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1625 * for the seed fs_devices
1627 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1628 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1629 ASSERT(devices_kobj);
1630 ASSERT(devinfo_kobj);
1632 nofs_flag = memalloc_nofs_save();
1635 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1637 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1639 btrfs_warn(device->fs_info,
1640 "creating sysfs device link for devid %llu failed: %d",
1641 device->devid, ret);
1646 init_completion(&device->kobj_unregister);
1647 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1648 devinfo_kobj, "%llu", device->devid);
1650 kobject_put(&device->devid_kobj);
1651 btrfs_warn(device->fs_info,
1652 "devinfo init for devid %llu failed: %d",
1653 device->devid, ret);
1657 memalloc_nofs_restore(nofs_flag);
1661 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1664 struct btrfs_device *device;
1665 struct btrfs_fs_devices *seed;
1667 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1668 ret = btrfs_sysfs_add_device(device);
1673 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1674 list_for_each_entry(device, &seed->devices, dev_list) {
1675 ret = btrfs_sysfs_add_device(device);
1684 btrfs_sysfs_remove_fs_devices(fs_devices);
1688 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1692 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1694 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1695 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1696 &disk_to_dev(bdev->bd_disk)->kobj);
1699 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1702 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1705 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1708 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1709 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1710 btrfs_warn(fs_devices->fs_info,
1711 "sysfs: failed to create fsid for sprout");
1714 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1718 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1720 if (kobject_rename(&device->devid_kobj, tmp))
1721 btrfs_warn(device->fs_devices->fs_info,
1722 "sysfs: failed to update devid for %llu",
1726 /* /sys/fs/btrfs/ entry */
1727 static struct kset *btrfs_kset;
1731 * /sys/fs/btrfs/UUID
1733 * Can be called by the device discovery thread.
1735 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1739 init_completion(&fs_devs->kobj_unregister);
1740 fs_devs->fsid_kobj.kset = btrfs_kset;
1741 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1742 "%pU", fs_devs->fsid);
1744 kobject_put(&fs_devs->fsid_kobj);
1748 fs_devs->devices_kobj = kobject_create_and_add("devices",
1749 &fs_devs->fsid_kobj);
1750 if (!fs_devs->devices_kobj) {
1751 btrfs_err(fs_devs->fs_info,
1752 "failed to init sysfs device interface");
1753 btrfs_sysfs_remove_fsid(fs_devs);
1757 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1758 &fs_devs->fsid_kobj);
1759 if (!fs_devs->devinfo_kobj) {
1760 btrfs_err(fs_devs->fs_info,
1761 "failed to init sysfs devinfo kobject");
1762 btrfs_sysfs_remove_fsid(fs_devs);
1769 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1772 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1773 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1775 error = btrfs_sysfs_add_fs_devices(fs_devs);
1779 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1781 btrfs_sysfs_remove_fs_devices(fs_devs);
1785 error = sysfs_create_group(fsid_kobj,
1786 &btrfs_feature_attr_group);
1790 #ifdef CONFIG_BTRFS_DEBUG
1791 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1792 if (!fs_info->debug_kobj) {
1797 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1801 /* Discard directory */
1802 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1803 fs_info->debug_kobj);
1804 if (!fs_info->discard_debug_kobj) {
1809 error = sysfs_create_files(fs_info->discard_debug_kobj,
1810 discard_debug_attrs);
1815 error = addrm_unknown_feature_attrs(fs_info, true);
1819 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1823 fs_info->space_info_kobj = kobject_create_and_add("allocation",
1825 if (!fs_info->space_info_kobj) {
1830 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1836 btrfs_sysfs_remove_mounted(fs_info);
1840 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1842 return to_fs_info(kobj->parent->parent);
1845 #define QGROUP_ATTR(_member, _show_name) \
1846 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1847 struct kobj_attribute *a, \
1850 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1851 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1852 struct btrfs_qgroup, kobj); \
1853 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1855 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1857 #define QGROUP_RSV_ATTR(_name, _type) \
1858 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1859 struct kobj_attribute *a, \
1862 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1863 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1864 struct btrfs_qgroup, kobj); \
1865 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1866 &fs_info->qgroup_lock, buf); \
1868 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1870 QGROUP_ATTR(rfer, referenced);
1871 QGROUP_ATTR(excl, exclusive);
1872 QGROUP_ATTR(max_rfer, max_referenced);
1873 QGROUP_ATTR(max_excl, max_exclusive);
1874 QGROUP_ATTR(lim_flags, limit_flags);
1875 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1876 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1877 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1880 * Qgroup information.
1882 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
1884 static struct attribute *qgroup_attrs[] = {
1885 BTRFS_ATTR_PTR(qgroup, referenced),
1886 BTRFS_ATTR_PTR(qgroup, exclusive),
1887 BTRFS_ATTR_PTR(qgroup, max_referenced),
1888 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1889 BTRFS_ATTR_PTR(qgroup, limit_flags),
1890 BTRFS_ATTR_PTR(qgroup, rsv_data),
1891 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1892 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1895 ATTRIBUTE_GROUPS(qgroup);
1897 static void qgroup_release(struct kobject *kobj)
1899 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1901 memset(&qgroup->kobj, 0, sizeof(*kobj));
1904 static struct kobj_type qgroup_ktype = {
1905 .sysfs_ops = &kobj_sysfs_ops,
1906 .release = qgroup_release,
1907 .default_groups = qgroup_groups,
1910 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1911 struct btrfs_qgroup *qgroup)
1913 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1916 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1918 if (qgroup->kobj.state_initialized)
1923 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1924 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1925 btrfs_qgroup_subvolid(qgroup->qgroupid));
1927 kobject_put(&qgroup->kobj);
1932 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1934 struct btrfs_qgroup *qgroup;
1935 struct btrfs_qgroup *next;
1937 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1940 rbtree_postorder_for_each_entry_safe(qgroup, next,
1941 &fs_info->qgroup_tree, node)
1942 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1943 if (fs_info->qgroups_kobj) {
1944 kobject_del(fs_info->qgroups_kobj);
1945 kobject_put(fs_info->qgroups_kobj);
1946 fs_info->qgroups_kobj = NULL;
1950 /* Called when qgroups get initialized, thus there is no need for locking */
1951 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1953 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1954 struct btrfs_qgroup *qgroup;
1955 struct btrfs_qgroup *next;
1958 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1962 if (fs_info->qgroups_kobj)
1965 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1966 if (!fs_info->qgroups_kobj) {
1970 rbtree_postorder_for_each_entry_safe(qgroup, next,
1971 &fs_info->qgroup_tree, node) {
1972 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1979 btrfs_sysfs_del_qgroups(fs_info);
1983 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1984 struct btrfs_qgroup *qgroup)
1986 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1989 if (qgroup->kobj.state_initialized) {
1990 kobject_del(&qgroup->kobj);
1991 kobject_put(&qgroup->kobj);
1996 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1997 * values in superblock. Call after any changes to incompat/compat_ro flags
1999 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
2000 u64 bit, enum btrfs_feature_set set)
2002 struct btrfs_fs_devices *fs_devs;
2003 struct kobject *fsid_kobj;
2004 u64 __maybe_unused features;
2005 int __maybe_unused ret;
2011 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2012 * safe when called from some contexts (eg. balance)
2014 features = get_features(fs_info, set);
2015 ASSERT(bit & supported_feature_masks[set]);
2017 fs_devs = fs_info->fs_devices;
2018 fsid_kobj = &fs_devs->fsid_kobj;
2020 if (!fsid_kobj->state_initialized)
2024 * FIXME: this is too heavy to update just one value, ideally we'd like
2025 * to use sysfs_update_group but some refactoring is needed first.
2027 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2028 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2031 int __init btrfs_init_sysfs(void)
2035 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2039 init_feature_attrs();
2040 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2043 ret = sysfs_merge_group(&btrfs_kset->kobj,
2044 &btrfs_static_feature_attr_group);
2046 goto out_remove_group;
2048 #ifdef CONFIG_BTRFS_DEBUG
2049 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2057 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2059 kset_unregister(btrfs_kset);
2064 void __cold btrfs_exit_sysfs(void)
2066 sysfs_unmerge_group(&btrfs_kset->kobj,
2067 &btrfs_static_feature_attr_group);
2068 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2069 #ifdef CONFIG_BTRFS_DEBUG
2070 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2072 kset_unregister(btrfs_kset);