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
);
417 static ssize_t
acl_show(struct kobject
*kobj
, struct kobj_attribute
*a
, char *buf
)
419 return sysfs_emit(buf
, "%d\n", !!IS_ENABLED(CONFIG_BTRFS_FS_POSIX_ACL
));
421 BTRFS_ATTR(static_feature
, acl
, acl_show
);
424 * Features which only depend on kernel version.
426 * These are listed in /sys/fs/btrfs/features along with
427 * btrfs_supported_feature_attrs.
429 static struct attribute
*btrfs_supported_static_feature_attrs
[] = {
430 BTRFS_ATTR_PTR(static_feature
, acl
),
431 BTRFS_ATTR_PTR(static_feature
, rmdir_subvol
),
432 BTRFS_ATTR_PTR(static_feature
, supported_checksums
),
433 BTRFS_ATTR_PTR(static_feature
, send_stream_version
),
434 BTRFS_ATTR_PTR(static_feature
, supported_rescue_options
),
435 BTRFS_ATTR_PTR(static_feature
, supported_sectorsizes
),
439 static const struct attribute_group btrfs_static_feature_attr_group
= {
441 .attrs
= btrfs_supported_static_feature_attrs
,
445 * Discard statistics and tunables
447 #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj))
449 static ssize_t
btrfs_discardable_bytes_show(struct kobject
*kobj
,
450 struct kobj_attribute
*a
,
453 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
455 return sysfs_emit(buf
, "%lld\n",
456 atomic64_read(&fs_info
->discard_ctl
.discardable_bytes
));
458 BTRFS_ATTR(discard
, discardable_bytes
, btrfs_discardable_bytes_show
);
460 static ssize_t
btrfs_discardable_extents_show(struct kobject
*kobj
,
461 struct kobj_attribute
*a
,
464 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
466 return sysfs_emit(buf
, "%d\n",
467 atomic_read(&fs_info
->discard_ctl
.discardable_extents
));
469 BTRFS_ATTR(discard
, discardable_extents
, btrfs_discardable_extents_show
);
471 static ssize_t
btrfs_discard_bitmap_bytes_show(struct kobject
*kobj
,
472 struct kobj_attribute
*a
,
475 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
477 return sysfs_emit(buf
, "%llu\n",
478 fs_info
->discard_ctl
.discard_bitmap_bytes
);
480 BTRFS_ATTR(discard
, discard_bitmap_bytes
, btrfs_discard_bitmap_bytes_show
);
482 static ssize_t
btrfs_discard_bytes_saved_show(struct kobject
*kobj
,
483 struct kobj_attribute
*a
,
486 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
488 return sysfs_emit(buf
, "%lld\n",
489 atomic64_read(&fs_info
->discard_ctl
.discard_bytes_saved
));
491 BTRFS_ATTR(discard
, discard_bytes_saved
, btrfs_discard_bytes_saved_show
);
493 static ssize_t
btrfs_discard_extent_bytes_show(struct kobject
*kobj
,
494 struct kobj_attribute
*a
,
497 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
499 return sysfs_emit(buf
, "%llu\n",
500 fs_info
->discard_ctl
.discard_extent_bytes
);
502 BTRFS_ATTR(discard
, discard_extent_bytes
, btrfs_discard_extent_bytes_show
);
504 static ssize_t
btrfs_discard_iops_limit_show(struct kobject
*kobj
,
505 struct kobj_attribute
*a
,
508 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
510 return sysfs_emit(buf
, "%u\n",
511 READ_ONCE(fs_info
->discard_ctl
.iops_limit
));
514 static ssize_t
btrfs_discard_iops_limit_store(struct kobject
*kobj
,
515 struct kobj_attribute
*a
,
516 const char *buf
, size_t len
)
518 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
519 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
523 ret
= kstrtou32(buf
, 10, &iops_limit
);
527 WRITE_ONCE(discard_ctl
->iops_limit
, iops_limit
);
528 btrfs_discard_calc_delay(discard_ctl
);
529 btrfs_discard_schedule_work(discard_ctl
, true);
532 BTRFS_ATTR_RW(discard
, iops_limit
, btrfs_discard_iops_limit_show
,
533 btrfs_discard_iops_limit_store
);
535 static ssize_t
btrfs_discard_kbps_limit_show(struct kobject
*kobj
,
536 struct kobj_attribute
*a
,
539 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
541 return sysfs_emit(buf
, "%u\n",
542 READ_ONCE(fs_info
->discard_ctl
.kbps_limit
));
545 static ssize_t
btrfs_discard_kbps_limit_store(struct kobject
*kobj
,
546 struct kobj_attribute
*a
,
547 const char *buf
, size_t len
)
549 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
550 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
554 ret
= kstrtou32(buf
, 10, &kbps_limit
);
558 WRITE_ONCE(discard_ctl
->kbps_limit
, kbps_limit
);
559 btrfs_discard_schedule_work(discard_ctl
, true);
562 BTRFS_ATTR_RW(discard
, kbps_limit
, btrfs_discard_kbps_limit_show
,
563 btrfs_discard_kbps_limit_store
);
565 static ssize_t
btrfs_discard_max_discard_size_show(struct kobject
*kobj
,
566 struct kobj_attribute
*a
,
569 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
571 return sysfs_emit(buf
, "%llu\n",
572 READ_ONCE(fs_info
->discard_ctl
.max_discard_size
));
575 static ssize_t
btrfs_discard_max_discard_size_store(struct kobject
*kobj
,
576 struct kobj_attribute
*a
,
577 const char *buf
, size_t len
)
579 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
580 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
581 u64 max_discard_size
;
584 ret
= kstrtou64(buf
, 10, &max_discard_size
);
588 WRITE_ONCE(discard_ctl
->max_discard_size
, max_discard_size
);
592 BTRFS_ATTR_RW(discard
, max_discard_size
, btrfs_discard_max_discard_size_show
,
593 btrfs_discard_max_discard_size_store
);
596 * Per-filesystem stats for discard (when mounted with discard=async).
598 * Path: /sys/fs/btrfs/<uuid>/discard/
600 static const struct attribute
*discard_attrs
[] = {
601 BTRFS_ATTR_PTR(discard
, discardable_bytes
),
602 BTRFS_ATTR_PTR(discard
, discardable_extents
),
603 BTRFS_ATTR_PTR(discard
, discard_bitmap_bytes
),
604 BTRFS_ATTR_PTR(discard
, discard_bytes_saved
),
605 BTRFS_ATTR_PTR(discard
, discard_extent_bytes
),
606 BTRFS_ATTR_PTR(discard
, iops_limit
),
607 BTRFS_ATTR_PTR(discard
, kbps_limit
),
608 BTRFS_ATTR_PTR(discard
, max_discard_size
),
612 #ifdef CONFIG_BTRFS_DEBUG
615 * Per-filesystem runtime debugging exported via sysfs.
617 * Path: /sys/fs/btrfs/UUID/debug/
619 static const struct attribute
*btrfs_debug_mount_attrs
[] = {
624 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
626 * Path: /sys/fs/btrfs/debug
628 static struct attribute
*btrfs_debug_feature_attrs
[] = {
632 static const struct attribute_group btrfs_debug_feature_attr_group
= {
634 .attrs
= btrfs_debug_feature_attrs
,
639 static ssize_t
btrfs_show_u64(u64
*value_ptr
, spinlock_t
*lock
, char *buf
)
647 return sysfs_emit(buf
, "%llu\n", val
);
650 static ssize_t
global_rsv_size_show(struct kobject
*kobj
,
651 struct kobj_attribute
*ka
, char *buf
)
653 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
654 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
655 return btrfs_show_u64(&block_rsv
->size
, &block_rsv
->lock
, buf
);
657 BTRFS_ATTR(allocation
, global_rsv_size
, global_rsv_size_show
);
659 static ssize_t
global_rsv_reserved_show(struct kobject
*kobj
,
660 struct kobj_attribute
*a
, char *buf
)
662 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
663 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
664 return btrfs_show_u64(&block_rsv
->reserved
, &block_rsv
->lock
, buf
);
666 BTRFS_ATTR(allocation
, global_rsv_reserved
, global_rsv_reserved_show
);
668 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
669 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
671 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
672 struct kobj_attribute
*attr
, char *buf
);
673 BTRFS_ATTR(raid
, total_bytes
, raid_bytes_show
);
674 BTRFS_ATTR(raid
, used_bytes
, raid_bytes_show
);
676 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
677 struct kobj_attribute
*attr
, char *buf
)
680 struct btrfs_space_info
*sinfo
= to_space_info(kobj
->parent
);
681 struct btrfs_block_group
*block_group
;
682 int index
= btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj
)->flags
);
685 down_read(&sinfo
->groups_sem
);
686 list_for_each_entry(block_group
, &sinfo
->block_groups
[index
], list
) {
687 if (&attr
->attr
== BTRFS_ATTR_PTR(raid
, total_bytes
))
688 val
+= block_group
->length
;
690 val
+= block_group
->used
;
692 up_read(&sinfo
->groups_sem
);
693 return sysfs_emit(buf
, "%llu\n", val
);
697 * Allocation information about block group profiles.
699 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
701 static struct attribute
*raid_attrs
[] = {
702 BTRFS_ATTR_PTR(raid
, total_bytes
),
703 BTRFS_ATTR_PTR(raid
, used_bytes
),
706 ATTRIBUTE_GROUPS(raid
);
708 static void release_raid_kobj(struct kobject
*kobj
)
710 kfree(to_raid_kobj(kobj
));
713 static const struct kobj_type btrfs_raid_ktype
= {
714 .sysfs_ops
= &kobj_sysfs_ops
,
715 .release
= release_raid_kobj
,
716 .default_groups
= raid_groups
,
719 #define SPACE_INFO_ATTR(field) \
720 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
721 struct kobj_attribute *a, \
724 struct btrfs_space_info *sinfo = to_space_info(kobj); \
725 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
727 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
729 static ssize_t
btrfs_chunk_size_show(struct kobject
*kobj
,
730 struct kobj_attribute
*a
, char *buf
)
732 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
734 return sysfs_emit(buf
, "%llu\n", READ_ONCE(sinfo
->chunk_size
));
738 * Store new chunk size in space info. Can be called on a read-only filesystem.
740 * If the new chunk size value is larger than 10% of free space it is reduced
741 * to match that limit. Alignment must be to 256M and the system chunk size
744 static ssize_t
btrfs_chunk_size_store(struct kobject
*kobj
,
745 struct kobj_attribute
*a
,
746 const char *buf
, size_t len
)
748 struct btrfs_space_info
*space_info
= to_space_info(kobj
);
749 struct btrfs_fs_info
*fs_info
= to_fs_info(get_btrfs_kobj(kobj
));
753 if (!capable(CAP_SYS_ADMIN
))
756 if (!fs_info
->fs_devices
)
759 if (btrfs_is_zoned(fs_info
))
762 /* System block type must not be changed. */
763 if (space_info
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
766 val
= memparse(buf
, &retptr
);
767 /* There could be trailing '\n', also catch any typos after the value */
768 retptr
= skip_spaces(retptr
);
769 if (*retptr
!= 0 || val
== 0)
772 val
= min(val
, BTRFS_MAX_DATA_CHUNK_SIZE
);
774 /* Limit stripe size to 10% of available space. */
775 val
= min(mult_perc(fs_info
->fs_devices
->total_rw_bytes
, 10), val
);
777 /* Must be multiple of 256M. */
778 val
&= ~((u64
)SZ_256M
- 1);
780 /* Must be at least 256M. */
784 btrfs_update_space_info_chunk_size(space_info
, val
);
789 static ssize_t
btrfs_size_classes_show(struct kobject
*kobj
,
790 struct kobj_attribute
*a
, char *buf
)
792 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
793 struct btrfs_block_group
*bg
;
799 for (int i
= 0; i
< BTRFS_NR_RAID_TYPES
; ++i
) {
800 down_read(&sinfo
->groups_sem
);
801 list_for_each_entry(bg
, &sinfo
->block_groups
[i
], list
) {
802 if (!btrfs_block_group_should_use_size_class(bg
))
804 switch (bg
->size_class
) {
805 case BTRFS_BG_SZ_NONE
:
808 case BTRFS_BG_SZ_SMALL
:
811 case BTRFS_BG_SZ_MEDIUM
:
814 case BTRFS_BG_SZ_LARGE
:
819 up_read(&sinfo
->groups_sem
);
821 return sysfs_emit(buf
, "none %u\n"
825 none
, small
, medium
, large
);
828 #ifdef CONFIG_BTRFS_DEBUG
830 * Request chunk allocation with current chunk size.
832 static ssize_t
btrfs_force_chunk_alloc_store(struct kobject
*kobj
,
833 struct kobj_attribute
*a
,
834 const char *buf
, size_t len
)
836 struct btrfs_space_info
*space_info
= to_space_info(kobj
);
837 struct btrfs_fs_info
*fs_info
= to_fs_info(get_btrfs_kobj(kobj
));
838 struct btrfs_trans_handle
*trans
;
842 if (!capable(CAP_SYS_ADMIN
))
845 if (sb_rdonly(fs_info
->sb
))
848 ret
= kstrtobool(buf
, &val
);
856 * This is unsafe to be called from sysfs context and may cause
857 * unexpected problems.
859 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
861 return PTR_ERR(trans
);
862 ret
= btrfs_force_chunk_alloc(trans
, space_info
->flags
);
863 btrfs_end_transaction(trans
);
870 BTRFS_ATTR_W(space_info
, force_chunk_alloc
, btrfs_force_chunk_alloc_store
);
874 SPACE_INFO_ATTR(flags
);
875 SPACE_INFO_ATTR(total_bytes
);
876 SPACE_INFO_ATTR(bytes_used
);
877 SPACE_INFO_ATTR(bytes_pinned
);
878 SPACE_INFO_ATTR(bytes_reserved
);
879 SPACE_INFO_ATTR(bytes_may_use
);
880 SPACE_INFO_ATTR(bytes_readonly
);
881 SPACE_INFO_ATTR(bytes_zone_unusable
);
882 SPACE_INFO_ATTR(disk_used
);
883 SPACE_INFO_ATTR(disk_total
);
884 BTRFS_ATTR_RW(space_info
, chunk_size
, btrfs_chunk_size_show
, btrfs_chunk_size_store
);
885 BTRFS_ATTR(space_info
, size_classes
, btrfs_size_classes_show
);
887 static ssize_t
btrfs_sinfo_bg_reclaim_threshold_show(struct kobject
*kobj
,
888 struct kobj_attribute
*a
,
891 struct btrfs_space_info
*space_info
= to_space_info(kobj
);
893 return sysfs_emit(buf
, "%d\n", READ_ONCE(space_info
->bg_reclaim_threshold
));
896 static ssize_t
btrfs_sinfo_bg_reclaim_threshold_store(struct kobject
*kobj
,
897 struct kobj_attribute
*a
,
898 const char *buf
, size_t len
)
900 struct btrfs_space_info
*space_info
= to_space_info(kobj
);
904 ret
= kstrtoint(buf
, 10, &thresh
);
908 if (thresh
< 0 || thresh
> 100)
911 WRITE_ONCE(space_info
->bg_reclaim_threshold
, thresh
);
916 BTRFS_ATTR_RW(space_info
, bg_reclaim_threshold
,
917 btrfs_sinfo_bg_reclaim_threshold_show
,
918 btrfs_sinfo_bg_reclaim_threshold_store
);
921 * Allocation information about block group types.
923 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
925 static struct attribute
*space_info_attrs
[] = {
926 BTRFS_ATTR_PTR(space_info
, flags
),
927 BTRFS_ATTR_PTR(space_info
, total_bytes
),
928 BTRFS_ATTR_PTR(space_info
, bytes_used
),
929 BTRFS_ATTR_PTR(space_info
, bytes_pinned
),
930 BTRFS_ATTR_PTR(space_info
, bytes_reserved
),
931 BTRFS_ATTR_PTR(space_info
, bytes_may_use
),
932 BTRFS_ATTR_PTR(space_info
, bytes_readonly
),
933 BTRFS_ATTR_PTR(space_info
, bytes_zone_unusable
),
934 BTRFS_ATTR_PTR(space_info
, disk_used
),
935 BTRFS_ATTR_PTR(space_info
, disk_total
),
936 BTRFS_ATTR_PTR(space_info
, bg_reclaim_threshold
),
937 BTRFS_ATTR_PTR(space_info
, chunk_size
),
938 BTRFS_ATTR_PTR(space_info
, size_classes
),
939 #ifdef CONFIG_BTRFS_DEBUG
940 BTRFS_ATTR_PTR(space_info
, force_chunk_alloc
),
944 ATTRIBUTE_GROUPS(space_info
);
946 static void space_info_release(struct kobject
*kobj
)
948 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
952 static const struct kobj_type space_info_ktype
= {
953 .sysfs_ops
= &kobj_sysfs_ops
,
954 .release
= space_info_release
,
955 .default_groups
= space_info_groups
,
959 * Allocation information about block groups.
961 * Path: /sys/fs/btrfs/<uuid>/allocation/
963 static const struct attribute
*allocation_attrs
[] = {
964 BTRFS_ATTR_PTR(allocation
, global_rsv_reserved
),
965 BTRFS_ATTR_PTR(allocation
, global_rsv_size
),
969 static ssize_t
btrfs_label_show(struct kobject
*kobj
,
970 struct kobj_attribute
*a
, char *buf
)
972 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
973 char *label
= fs_info
->super_copy
->label
;
976 spin_lock(&fs_info
->super_lock
);
977 ret
= sysfs_emit(buf
, label
[0] ? "%s\n" : "%s", label
);
978 spin_unlock(&fs_info
->super_lock
);
983 static ssize_t
btrfs_label_store(struct kobject
*kobj
,
984 struct kobj_attribute
*a
,
985 const char *buf
, size_t len
)
987 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
993 if (sb_rdonly(fs_info
->sb
))
997 * p_len is the len until the first occurrence of either
1000 p_len
= strcspn(buf
, "\n");
1002 if (p_len
>= BTRFS_LABEL_SIZE
)
1005 spin_lock(&fs_info
->super_lock
);
1006 memset(fs_info
->super_copy
->label
, 0, BTRFS_LABEL_SIZE
);
1007 memcpy(fs_info
->super_copy
->label
, buf
, p_len
);
1008 spin_unlock(&fs_info
->super_lock
);
1011 * We don't want to do full transaction commit from inside sysfs
1013 set_bit(BTRFS_FS_NEED_TRANS_COMMIT
, &fs_info
->flags
);
1014 wake_up_process(fs_info
->transaction_kthread
);
1018 BTRFS_ATTR_RW(, label
, btrfs_label_show
, btrfs_label_store
);
1020 static ssize_t
btrfs_nodesize_show(struct kobject
*kobj
,
1021 struct kobj_attribute
*a
, char *buf
)
1023 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1025 return sysfs_emit(buf
, "%u\n", fs_info
->super_copy
->nodesize
);
1028 BTRFS_ATTR(, nodesize
, btrfs_nodesize_show
);
1030 static ssize_t
btrfs_sectorsize_show(struct kobject
*kobj
,
1031 struct kobj_attribute
*a
, char *buf
)
1033 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1035 return sysfs_emit(buf
, "%u\n", fs_info
->super_copy
->sectorsize
);
1038 BTRFS_ATTR(, sectorsize
, btrfs_sectorsize_show
);
1040 static ssize_t
btrfs_commit_stats_show(struct kobject
*kobj
,
1041 struct kobj_attribute
*a
, char *buf
)
1043 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1045 return sysfs_emit(buf
,
1047 "last_commit_ms %llu\n"
1048 "max_commit_ms %llu\n"
1049 "total_commit_ms %llu\n",
1050 fs_info
->commit_stats
.commit_count
,
1051 div_u64(fs_info
->commit_stats
.last_commit_dur
, NSEC_PER_MSEC
),
1052 div_u64(fs_info
->commit_stats
.max_commit_dur
, NSEC_PER_MSEC
),
1053 div_u64(fs_info
->commit_stats
.total_commit_dur
, NSEC_PER_MSEC
));
1056 static ssize_t
btrfs_commit_stats_store(struct kobject
*kobj
,
1057 struct kobj_attribute
*a
,
1058 const char *buf
, size_t len
)
1060 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1067 if (!capable(CAP_SYS_RESOURCE
))
1070 ret
= kstrtoul(buf
, 10, &val
);
1076 WRITE_ONCE(fs_info
->commit_stats
.max_commit_dur
, 0);
1080 BTRFS_ATTR_RW(, commit_stats
, btrfs_commit_stats_show
, btrfs_commit_stats_store
);
1082 static ssize_t
btrfs_clone_alignment_show(struct kobject
*kobj
,
1083 struct kobj_attribute
*a
, char *buf
)
1085 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1087 return sysfs_emit(buf
, "%u\n", fs_info
->super_copy
->sectorsize
);
1090 BTRFS_ATTR(, clone_alignment
, btrfs_clone_alignment_show
);
1092 static ssize_t
quota_override_show(struct kobject
*kobj
,
1093 struct kobj_attribute
*a
, char *buf
)
1095 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1098 quota_override
= test_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
1099 return sysfs_emit(buf
, "%d\n", quota_override
);
1102 static ssize_t
quota_override_store(struct kobject
*kobj
,
1103 struct kobj_attribute
*a
,
1104 const char *buf
, size_t len
)
1106 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1113 if (!capable(CAP_SYS_RESOURCE
))
1116 err
= kstrtoul(buf
, 10, &knob
);
1123 set_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
1125 clear_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
1130 BTRFS_ATTR_RW(, quota_override
, quota_override_show
, quota_override_store
);
1132 static ssize_t
btrfs_metadata_uuid_show(struct kobject
*kobj
,
1133 struct kobj_attribute
*a
, char *buf
)
1135 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1137 return sysfs_emit(buf
, "%pU\n", fs_info
->fs_devices
->metadata_uuid
);
1140 BTRFS_ATTR(, metadata_uuid
, btrfs_metadata_uuid_show
);
1142 static ssize_t
btrfs_checksum_show(struct kobject
*kobj
,
1143 struct kobj_attribute
*a
, char *buf
)
1145 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1146 u16 csum_type
= btrfs_super_csum_type(fs_info
->super_copy
);
1148 return sysfs_emit(buf
, "%s (%s)\n",
1149 btrfs_super_csum_name(csum_type
),
1150 crypto_shash_driver_name(fs_info
->csum_shash
));
1153 BTRFS_ATTR(, checksum
, btrfs_checksum_show
);
1155 static ssize_t
btrfs_exclusive_operation_show(struct kobject
*kobj
,
1156 struct kobj_attribute
*a
, char *buf
)
1158 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1161 switch (READ_ONCE(fs_info
->exclusive_operation
)) {
1162 case BTRFS_EXCLOP_NONE
:
1165 case BTRFS_EXCLOP_BALANCE
:
1168 case BTRFS_EXCLOP_BALANCE_PAUSED
:
1169 str
= "balance paused\n";
1171 case BTRFS_EXCLOP_DEV_ADD
:
1172 str
= "device add\n";
1174 case BTRFS_EXCLOP_DEV_REMOVE
:
1175 str
= "device remove\n";
1177 case BTRFS_EXCLOP_DEV_REPLACE
:
1178 str
= "device replace\n";
1180 case BTRFS_EXCLOP_RESIZE
:
1183 case BTRFS_EXCLOP_SWAP_ACTIVATE
:
1184 str
= "swap activate\n";
1190 return sysfs_emit(buf
, "%s", str
);
1192 BTRFS_ATTR(, exclusive_operation
, btrfs_exclusive_operation_show
);
1194 static ssize_t
btrfs_generation_show(struct kobject
*kobj
,
1195 struct kobj_attribute
*a
, char *buf
)
1197 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1199 return sysfs_emit(buf
, "%llu\n", fs_info
->generation
);
1201 BTRFS_ATTR(, generation
, btrfs_generation_show
);
1203 static const char * const btrfs_read_policy_name
[] = { "pid" };
1205 static ssize_t
btrfs_read_policy_show(struct kobject
*kobj
,
1206 struct kobj_attribute
*a
, char *buf
)
1208 struct btrfs_fs_devices
*fs_devices
= to_fs_devs(kobj
);
1212 for (i
= 0; i
< BTRFS_NR_READ_POLICY
; i
++) {
1213 if (fs_devices
->read_policy
== i
)
1214 ret
+= sysfs_emit_at(buf
, ret
, "%s[%s]",
1215 (ret
== 0 ? "" : " "),
1216 btrfs_read_policy_name
[i
]);
1218 ret
+= sysfs_emit_at(buf
, ret
, "%s%s",
1219 (ret
== 0 ? "" : " "),
1220 btrfs_read_policy_name
[i
]);
1223 ret
+= sysfs_emit_at(buf
, ret
, "\n");
1228 static ssize_t
btrfs_read_policy_store(struct kobject
*kobj
,
1229 struct kobj_attribute
*a
,
1230 const char *buf
, size_t len
)
1232 struct btrfs_fs_devices
*fs_devices
= to_fs_devs(kobj
);
1235 for (i
= 0; i
< BTRFS_NR_READ_POLICY
; i
++) {
1236 if (sysfs_streq(buf
, btrfs_read_policy_name
[i
])) {
1237 if (i
!= fs_devices
->read_policy
) {
1238 fs_devices
->read_policy
= i
;
1239 btrfs_info(fs_devices
->fs_info
,
1240 "read policy set to '%s'",
1241 btrfs_read_policy_name
[i
]);
1249 BTRFS_ATTR_RW(, read_policy
, btrfs_read_policy_show
, btrfs_read_policy_store
);
1251 static ssize_t
btrfs_bg_reclaim_threshold_show(struct kobject
*kobj
,
1252 struct kobj_attribute
*a
,
1255 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1257 return sysfs_emit(buf
, "%d\n", READ_ONCE(fs_info
->bg_reclaim_threshold
));
1260 static ssize_t
btrfs_bg_reclaim_threshold_store(struct kobject
*kobj
,
1261 struct kobj_attribute
*a
,
1262 const char *buf
, size_t len
)
1264 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
1268 ret
= kstrtoint(buf
, 10, &thresh
);
1272 #ifdef CONFIG_BTRFS_DEBUG
1273 if (thresh
!= 0 && (thresh
> 100))
1276 if (thresh
!= 0 && (thresh
<= 50 || thresh
> 100))
1280 WRITE_ONCE(fs_info
->bg_reclaim_threshold
, thresh
);
1284 BTRFS_ATTR_RW(, bg_reclaim_threshold
, btrfs_bg_reclaim_threshold_show
,
1285 btrfs_bg_reclaim_threshold_store
);
1288 * Per-filesystem information and stats.
1290 * Path: /sys/fs/btrfs/<uuid>/
1292 static const struct attribute
*btrfs_attrs
[] = {
1293 BTRFS_ATTR_PTR(, label
),
1294 BTRFS_ATTR_PTR(, nodesize
),
1295 BTRFS_ATTR_PTR(, sectorsize
),
1296 BTRFS_ATTR_PTR(, clone_alignment
),
1297 BTRFS_ATTR_PTR(, quota_override
),
1298 BTRFS_ATTR_PTR(, metadata_uuid
),
1299 BTRFS_ATTR_PTR(, checksum
),
1300 BTRFS_ATTR_PTR(, exclusive_operation
),
1301 BTRFS_ATTR_PTR(, generation
),
1302 BTRFS_ATTR_PTR(, read_policy
),
1303 BTRFS_ATTR_PTR(, bg_reclaim_threshold
),
1304 BTRFS_ATTR_PTR(, commit_stats
),
1308 static void btrfs_release_fsid_kobj(struct kobject
*kobj
)
1310 struct btrfs_fs_devices
*fs_devs
= to_fs_devs(kobj
);
1312 memset(&fs_devs
->fsid_kobj
, 0, sizeof(struct kobject
));
1313 complete(&fs_devs
->kobj_unregister
);
1316 static const struct kobj_type btrfs_ktype
= {
1317 .sysfs_ops
= &kobj_sysfs_ops
,
1318 .release
= btrfs_release_fsid_kobj
,
1321 static inline struct btrfs_fs_devices
*to_fs_devs(struct kobject
*kobj
)
1323 if (kobj
->ktype
!= &btrfs_ktype
)
1325 return container_of(kobj
, struct btrfs_fs_devices
, fsid_kobj
);
1328 static inline struct btrfs_fs_info
*to_fs_info(struct kobject
*kobj
)
1330 if (kobj
->ktype
!= &btrfs_ktype
)
1332 return to_fs_devs(kobj
)->fs_info
;
1335 static struct kobject
*get_btrfs_kobj(struct kobject
*kobj
)
1338 if (kobj
->ktype
== &btrfs_ktype
)
1340 kobj
= kobj
->parent
;
1345 #define NUM_FEATURE_BITS 64
1346 #define BTRFS_FEATURE_NAME_MAX 13
1347 static char btrfs_unknown_feature_names
[FEAT_MAX
][NUM_FEATURE_BITS
][BTRFS_FEATURE_NAME_MAX
];
1348 static struct btrfs_feature_attr btrfs_feature_attrs
[FEAT_MAX
][NUM_FEATURE_BITS
];
1350 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names
) ==
1351 ARRAY_SIZE(btrfs_feature_attrs
));
1352 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names
[0]) ==
1353 ARRAY_SIZE(btrfs_feature_attrs
[0]));
1355 static const u64 supported_feature_masks
[FEAT_MAX
] = {
1356 [FEAT_COMPAT
] = BTRFS_FEATURE_COMPAT_SUPP
,
1357 [FEAT_COMPAT_RO
] = BTRFS_FEATURE_COMPAT_RO_SUPP
,
1358 [FEAT_INCOMPAT
] = BTRFS_FEATURE_INCOMPAT_SUPP
,
1361 static int addrm_unknown_feature_attrs(struct btrfs_fs_info
*fs_info
, bool add
)
1365 for (set
= 0; set
< FEAT_MAX
; set
++) {
1367 struct attribute
*attrs
[2];
1368 struct attribute_group agroup
= {
1372 u64 features
= get_features(fs_info
, set
);
1373 features
&= ~supported_feature_masks
[set
];
1379 for (i
= 0; i
< NUM_FEATURE_BITS
; i
++) {
1380 struct btrfs_feature_attr
*fa
;
1382 if (!(features
& (1ULL << i
)))
1385 fa
= &btrfs_feature_attrs
[set
][i
];
1386 attrs
[0] = &fa
->kobj_attr
.attr
;
1389 ret
= sysfs_merge_group(&fs_info
->fs_devices
->fsid_kobj
,
1394 sysfs_unmerge_group(&fs_info
->fs_devices
->fsid_kobj
,
1402 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
1404 if (fs_devs
->devinfo_kobj
) {
1405 kobject_del(fs_devs
->devinfo_kobj
);
1406 kobject_put(fs_devs
->devinfo_kobj
);
1407 fs_devs
->devinfo_kobj
= NULL
;
1410 if (fs_devs
->devices_kobj
) {
1411 kobject_del(fs_devs
->devices_kobj
);
1412 kobject_put(fs_devs
->devices_kobj
);
1413 fs_devs
->devices_kobj
= NULL
;
1416 if (fs_devs
->fsid_kobj
.state_initialized
) {
1417 kobject_del(&fs_devs
->fsid_kobj
);
1418 kobject_put(&fs_devs
->fsid_kobj
);
1419 wait_for_completion(&fs_devs
->kobj_unregister
);
1423 /* when fs_devs is NULL it will remove all fsid kobject */
1424 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
1426 struct list_head
*fs_uuids
= btrfs_get_fs_uuids();
1429 __btrfs_sysfs_remove_fsid(fs_devs
);
1433 list_for_each_entry(fs_devs
, fs_uuids
, fs_list
) {
1434 __btrfs_sysfs_remove_fsid(fs_devs
);
1438 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1440 struct btrfs_device
*device
;
1441 struct btrfs_fs_devices
*seed
;
1443 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
1444 btrfs_sysfs_remove_device(device
);
1446 list_for_each_entry(seed
, &fs_devices
->seed_list
, seed_list
) {
1447 list_for_each_entry(device
, &seed
->devices
, dev_list
)
1448 btrfs_sysfs_remove_device(device
);
1452 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info
*fs_info
)
1454 struct kobject
*fsid_kobj
= &fs_info
->fs_devices
->fsid_kobj
;
1456 sysfs_remove_link(fsid_kobj
, "bdi");
1458 if (fs_info
->space_info_kobj
) {
1459 sysfs_remove_files(fs_info
->space_info_kobj
, allocation_attrs
);
1460 kobject_del(fs_info
->space_info_kobj
);
1461 kobject_put(fs_info
->space_info_kobj
);
1463 if (fs_info
->discard_kobj
) {
1464 sysfs_remove_files(fs_info
->discard_kobj
, discard_attrs
);
1465 kobject_del(fs_info
->discard_kobj
);
1466 kobject_put(fs_info
->discard_kobj
);
1468 #ifdef CONFIG_BTRFS_DEBUG
1469 if (fs_info
->debug_kobj
) {
1470 sysfs_remove_files(fs_info
->debug_kobj
, btrfs_debug_mount_attrs
);
1471 kobject_del(fs_info
->debug_kobj
);
1472 kobject_put(fs_info
->debug_kobj
);
1475 addrm_unknown_feature_attrs(fs_info
, false);
1476 sysfs_remove_group(fsid_kobj
, &btrfs_feature_attr_group
);
1477 sysfs_remove_files(fsid_kobj
, btrfs_attrs
);
1478 btrfs_sysfs_remove_fs_devices(fs_info
->fs_devices
);
1481 static const char * const btrfs_feature_set_names
[FEAT_MAX
] = {
1482 [FEAT_COMPAT
] = "compat",
1483 [FEAT_COMPAT_RO
] = "compat_ro",
1484 [FEAT_INCOMPAT
] = "incompat",
1487 const char *btrfs_feature_set_name(enum btrfs_feature_set set
)
1489 return btrfs_feature_set_names
[set
];
1492 char *btrfs_printable_features(enum btrfs_feature_set set
, u64 flags
)
1494 size_t bufsize
= 4096; /* safe max, 64 names * 64 bytes */
1499 str
= kmalloc(bufsize
, GFP_KERNEL
);
1503 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
1506 if (!(flags
& (1ULL << i
)))
1509 name
= btrfs_feature_attrs
[set
][i
].kobj_attr
.attr
.name
;
1510 len
+= scnprintf(str
+ len
, bufsize
- len
, "%s%s",
1511 len
? "," : "", name
);
1517 static void init_feature_attrs(void)
1519 struct btrfs_feature_attr
*fa
;
1522 memset(btrfs_feature_attrs
, 0, sizeof(btrfs_feature_attrs
));
1523 memset(btrfs_unknown_feature_names
, 0,
1524 sizeof(btrfs_unknown_feature_names
));
1526 for (i
= 0; btrfs_supported_feature_attrs
[i
]; i
++) {
1527 struct btrfs_feature_attr
*sfa
;
1528 struct attribute
*a
= btrfs_supported_feature_attrs
[i
];
1530 sfa
= attr_to_btrfs_feature_attr(a
);
1531 bit
= ilog2(sfa
->feature_bit
);
1532 fa
= &btrfs_feature_attrs
[sfa
->feature_set
][bit
];
1534 fa
->kobj_attr
.attr
.name
= sfa
->kobj_attr
.attr
.name
;
1537 for (set
= 0; set
< FEAT_MAX
; set
++) {
1538 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
1539 char *name
= btrfs_unknown_feature_names
[set
][i
];
1540 fa
= &btrfs_feature_attrs
[set
][i
];
1542 if (fa
->kobj_attr
.attr
.name
)
1545 snprintf(name
, BTRFS_FEATURE_NAME_MAX
, "%s:%u",
1546 btrfs_feature_set_names
[set
], i
);
1548 fa
->kobj_attr
.attr
.name
= name
;
1549 fa
->kobj_attr
.attr
.mode
= S_IRUGO
;
1550 fa
->feature_set
= set
;
1551 fa
->feature_bit
= 1ULL << i
;
1557 * Create a sysfs entry for a given block group type at path
1558 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1560 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group
*cache
)
1562 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
1563 struct btrfs_space_info
*space_info
= cache
->space_info
;
1564 struct raid_kobject
*rkobj
;
1565 const int index
= btrfs_bg_flags_to_raid_index(cache
->flags
);
1566 unsigned int nofs_flag
;
1570 * Setup a NOFS context because kobject_add(), deep in its call chain,
1571 * does GFP_KERNEL allocations, and we are often called in a context
1572 * where if reclaim is triggered we can deadlock (we are either holding
1573 * a transaction handle or some lock required for a transaction
1576 nofs_flag
= memalloc_nofs_save();
1578 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
1580 memalloc_nofs_restore(nofs_flag
);
1581 btrfs_warn(cache
->fs_info
,
1582 "couldn't alloc memory for raid level kobject");
1586 rkobj
->flags
= cache
->flags
;
1587 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
1590 * We call this either on mount, or if we've created a block group for a
1591 * new index type while running (i.e. when restriping). The running
1592 * case is tricky because we could race with other threads, so we need
1593 * to have this check to make sure we didn't already init the kobject.
1595 * We don't have to protect on the free side because it only happens on
1598 spin_lock(&space_info
->lock
);
1599 if (space_info
->block_group_kobjs
[index
]) {
1600 spin_unlock(&space_info
->lock
);
1601 kobject_put(&rkobj
->kobj
);
1604 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
1606 spin_unlock(&space_info
->lock
);
1608 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
, "%s",
1609 btrfs_bg_type_to_raid_name(rkobj
->flags
));
1610 memalloc_nofs_restore(nofs_flag
);
1612 spin_lock(&space_info
->lock
);
1613 space_info
->block_group_kobjs
[index
] = NULL
;
1614 spin_unlock(&space_info
->lock
);
1615 kobject_put(&rkobj
->kobj
);
1617 "failed to add kobject for block cache, ignoring");
1623 * Remove sysfs directories for all block group types of a given space info and
1624 * the space info as well
1626 void btrfs_sysfs_remove_space_info(struct btrfs_space_info
*space_info
)
1630 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1631 struct kobject
*kobj
;
1633 kobj
= space_info
->block_group_kobjs
[i
];
1634 space_info
->block_group_kobjs
[i
] = NULL
;
1640 kobject_del(&space_info
->kobj
);
1641 kobject_put(&space_info
->kobj
);
1644 static const char *alloc_name(u64 flags
)
1647 case BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
:
1649 case BTRFS_BLOCK_GROUP_METADATA
:
1651 case BTRFS_BLOCK_GROUP_DATA
:
1653 case BTRFS_BLOCK_GROUP_SYSTEM
:
1657 return "invalid-combination";
1662 * Create a sysfs entry for a space info type at path
1663 * /sys/fs/btrfs/UUID/allocation/TYPE
1665 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info
*fs_info
,
1666 struct btrfs_space_info
*space_info
)
1670 ret
= kobject_init_and_add(&space_info
->kobj
, &space_info_ktype
,
1671 fs_info
->space_info_kobj
, "%s",
1672 alloc_name(space_info
->flags
));
1674 kobject_put(&space_info
->kobj
);
1681 void btrfs_sysfs_remove_device(struct btrfs_device
*device
)
1683 struct kobject
*devices_kobj
;
1686 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1687 * fs_info::fs_devices.
1689 devices_kobj
= device
->fs_info
->fs_devices
->devices_kobj
;
1690 ASSERT(devices_kobj
);
1693 sysfs_remove_link(devices_kobj
, bdev_kobj(device
->bdev
)->name
);
1695 if (device
->devid_kobj
.state_initialized
) {
1696 kobject_del(&device
->devid_kobj
);
1697 kobject_put(&device
->devid_kobj
);
1698 wait_for_completion(&device
->kobj_unregister
);
1702 static ssize_t
btrfs_devinfo_in_fs_metadata_show(struct kobject
*kobj
,
1703 struct kobj_attribute
*a
,
1707 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1710 val
= !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
1712 return sysfs_emit(buf
, "%d\n", val
);
1714 BTRFS_ATTR(devid
, in_fs_metadata
, btrfs_devinfo_in_fs_metadata_show
);
1716 static ssize_t
btrfs_devinfo_missing_show(struct kobject
*kobj
,
1717 struct kobj_attribute
*a
, char *buf
)
1720 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1723 val
= !!test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
1725 return sysfs_emit(buf
, "%d\n", val
);
1727 BTRFS_ATTR(devid
, missing
, btrfs_devinfo_missing_show
);
1729 static ssize_t
btrfs_devinfo_replace_target_show(struct kobject
*kobj
,
1730 struct kobj_attribute
*a
,
1734 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1737 val
= !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
1739 return sysfs_emit(buf
, "%d\n", val
);
1741 BTRFS_ATTR(devid
, replace_target
, btrfs_devinfo_replace_target_show
);
1743 static ssize_t
btrfs_devinfo_scrub_speed_max_show(struct kobject
*kobj
,
1744 struct kobj_attribute
*a
,
1747 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1750 return sysfs_emit(buf
, "%llu\n", READ_ONCE(device
->scrub_speed_max
));
1753 static ssize_t
btrfs_devinfo_scrub_speed_max_store(struct kobject
*kobj
,
1754 struct kobj_attribute
*a
,
1755 const char *buf
, size_t len
)
1757 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1760 unsigned long long limit
;
1762 limit
= memparse(buf
, &endptr
);
1763 WRITE_ONCE(device
->scrub_speed_max
, limit
);
1766 BTRFS_ATTR_RW(devid
, scrub_speed_max
, btrfs_devinfo_scrub_speed_max_show
,
1767 btrfs_devinfo_scrub_speed_max_store
);
1769 static ssize_t
btrfs_devinfo_writeable_show(struct kobject
*kobj
,
1770 struct kobj_attribute
*a
, char *buf
)
1773 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1776 val
= !!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1778 return sysfs_emit(buf
, "%d\n", val
);
1780 BTRFS_ATTR(devid
, writeable
, btrfs_devinfo_writeable_show
);
1782 static ssize_t
btrfs_devinfo_fsid_show(struct kobject
*kobj
,
1783 struct kobj_attribute
*a
, char *buf
)
1785 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1788 return sysfs_emit(buf
, "%pU\n", device
->fs_devices
->fsid
);
1790 BTRFS_ATTR(devid
, fsid
, btrfs_devinfo_fsid_show
);
1792 static ssize_t
btrfs_devinfo_error_stats_show(struct kobject
*kobj
,
1793 struct kobj_attribute
*a
, char *buf
)
1795 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1798 if (!device
->dev_stats_valid
)
1799 return sysfs_emit(buf
, "invalid\n");
1802 * Print all at once so we get a snapshot of all values from the same
1803 * time. Keep them in sync and in order of definition of
1804 * btrfs_dev_stat_values.
1806 return sysfs_emit(buf
,
1810 "corruption_errs %d\n"
1811 "generation_errs %d\n",
1812 btrfs_dev_stat_read(device
, BTRFS_DEV_STAT_WRITE_ERRS
),
1813 btrfs_dev_stat_read(device
, BTRFS_DEV_STAT_READ_ERRS
),
1814 btrfs_dev_stat_read(device
, BTRFS_DEV_STAT_FLUSH_ERRS
),
1815 btrfs_dev_stat_read(device
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
1816 btrfs_dev_stat_read(device
, BTRFS_DEV_STAT_GENERATION_ERRS
));
1818 BTRFS_ATTR(devid
, error_stats
, btrfs_devinfo_error_stats_show
);
1821 * Information about one device.
1823 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1825 static struct attribute
*devid_attrs
[] = {
1826 BTRFS_ATTR_PTR(devid
, error_stats
),
1827 BTRFS_ATTR_PTR(devid
, fsid
),
1828 BTRFS_ATTR_PTR(devid
, in_fs_metadata
),
1829 BTRFS_ATTR_PTR(devid
, missing
),
1830 BTRFS_ATTR_PTR(devid
, replace_target
),
1831 BTRFS_ATTR_PTR(devid
, scrub_speed_max
),
1832 BTRFS_ATTR_PTR(devid
, writeable
),
1835 ATTRIBUTE_GROUPS(devid
);
1837 static void btrfs_release_devid_kobj(struct kobject
*kobj
)
1839 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1842 memset(&device
->devid_kobj
, 0, sizeof(struct kobject
));
1843 complete(&device
->kobj_unregister
);
1846 static const struct kobj_type devid_ktype
= {
1847 .sysfs_ops
= &kobj_sysfs_ops
,
1848 .default_groups
= devid_groups
,
1849 .release
= btrfs_release_devid_kobj
,
1852 int btrfs_sysfs_add_device(struct btrfs_device
*device
)
1855 unsigned int nofs_flag
;
1856 struct kobject
*devices_kobj
;
1857 struct kobject
*devinfo_kobj
;
1860 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1861 * for the seed fs_devices
1863 devices_kobj
= device
->fs_info
->fs_devices
->devices_kobj
;
1864 devinfo_kobj
= device
->fs_info
->fs_devices
->devinfo_kobj
;
1865 ASSERT(devices_kobj
);
1866 ASSERT(devinfo_kobj
);
1868 nofs_flag
= memalloc_nofs_save();
1871 struct kobject
*disk_kobj
= bdev_kobj(device
->bdev
);
1873 ret
= sysfs_create_link(devices_kobj
, disk_kobj
, disk_kobj
->name
);
1875 btrfs_warn(device
->fs_info
,
1876 "creating sysfs device link for devid %llu failed: %d",
1877 device
->devid
, ret
);
1882 init_completion(&device
->kobj_unregister
);
1883 ret
= kobject_init_and_add(&device
->devid_kobj
, &devid_ktype
,
1884 devinfo_kobj
, "%llu", device
->devid
);
1886 kobject_put(&device
->devid_kobj
);
1887 btrfs_warn(device
->fs_info
,
1888 "devinfo init for devid %llu failed: %d",
1889 device
->devid
, ret
);
1893 memalloc_nofs_restore(nofs_flag
);
1897 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1900 struct btrfs_device
*device
;
1901 struct btrfs_fs_devices
*seed
;
1903 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1904 ret
= btrfs_sysfs_add_device(device
);
1909 list_for_each_entry(seed
, &fs_devices
->seed_list
, seed_list
) {
1910 list_for_each_entry(device
, &seed
->devices
, dev_list
) {
1911 ret
= btrfs_sysfs_add_device(device
);
1920 btrfs_sysfs_remove_fs_devices(fs_devices
);
1924 void btrfs_kobject_uevent(struct block_device
*bdev
, enum kobject_action action
)
1928 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
1930 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1931 action
, kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
1932 &disk_to_dev(bdev
->bd_disk
)->kobj
);
1935 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices
*fs_devices
)
1938 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
1941 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1944 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU", fs_devices
->fsid
);
1945 if (kobject_rename(&fs_devices
->fsid_kobj
, fsid_buf
))
1946 btrfs_warn(fs_devices
->fs_info
,
1947 "sysfs: failed to create fsid for sprout");
1950 void btrfs_sysfs_update_devid(struct btrfs_device
*device
)
1954 snprintf(tmp
, sizeof(tmp
), "%llu", device
->devid
);
1956 if (kobject_rename(&device
->devid_kobj
, tmp
))
1957 btrfs_warn(device
->fs_devices
->fs_info
,
1958 "sysfs: failed to update devid for %llu",
1962 /* /sys/fs/btrfs/ entry */
1963 static struct kset
*btrfs_kset
;
1967 * /sys/fs/btrfs/UUID
1969 * Can be called by the device discovery thread.
1971 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices
*fs_devs
)
1975 init_completion(&fs_devs
->kobj_unregister
);
1976 fs_devs
->fsid_kobj
.kset
= btrfs_kset
;
1977 error
= kobject_init_and_add(&fs_devs
->fsid_kobj
, &btrfs_ktype
, NULL
,
1978 "%pU", fs_devs
->fsid
);
1980 kobject_put(&fs_devs
->fsid_kobj
);
1984 fs_devs
->devices_kobj
= kobject_create_and_add("devices",
1985 &fs_devs
->fsid_kobj
);
1986 if (!fs_devs
->devices_kobj
) {
1987 btrfs_err(fs_devs
->fs_info
,
1988 "failed to init sysfs device interface");
1989 btrfs_sysfs_remove_fsid(fs_devs
);
1993 fs_devs
->devinfo_kobj
= kobject_create_and_add("devinfo",
1994 &fs_devs
->fsid_kobj
);
1995 if (!fs_devs
->devinfo_kobj
) {
1996 btrfs_err(fs_devs
->fs_info
,
1997 "failed to init sysfs devinfo kobject");
1998 btrfs_sysfs_remove_fsid(fs_devs
);
2005 int btrfs_sysfs_add_mounted(struct btrfs_fs_info
*fs_info
)
2008 struct btrfs_fs_devices
*fs_devs
= fs_info
->fs_devices
;
2009 struct kobject
*fsid_kobj
= &fs_devs
->fsid_kobj
;
2011 error
= btrfs_sysfs_add_fs_devices(fs_devs
);
2015 error
= sysfs_create_files(fsid_kobj
, btrfs_attrs
);
2017 btrfs_sysfs_remove_fs_devices(fs_devs
);
2021 error
= sysfs_create_group(fsid_kobj
,
2022 &btrfs_feature_attr_group
);
2026 #ifdef CONFIG_BTRFS_DEBUG
2027 fs_info
->debug_kobj
= kobject_create_and_add("debug", fsid_kobj
);
2028 if (!fs_info
->debug_kobj
) {
2033 error
= sysfs_create_files(fs_info
->debug_kobj
, btrfs_debug_mount_attrs
);
2038 /* Discard directory */
2039 fs_info
->discard_kobj
= kobject_create_and_add("discard", fsid_kobj
);
2040 if (!fs_info
->discard_kobj
) {
2045 error
= sysfs_create_files(fs_info
->discard_kobj
, discard_attrs
);
2049 error
= addrm_unknown_feature_attrs(fs_info
, true);
2053 error
= sysfs_create_link(fsid_kobj
, &fs_info
->sb
->s_bdi
->dev
->kobj
, "bdi");
2057 fs_info
->space_info_kobj
= kobject_create_and_add("allocation",
2059 if (!fs_info
->space_info_kobj
) {
2064 error
= sysfs_create_files(fs_info
->space_info_kobj
, allocation_attrs
);
2070 btrfs_sysfs_remove_mounted(fs_info
);
2074 static ssize_t
qgroup_enabled_show(struct kobject
*qgroups_kobj
,
2075 struct kobj_attribute
*a
,
2078 struct btrfs_fs_info
*fs_info
= to_fs_info(qgroups_kobj
->parent
);
2081 spin_lock(&fs_info
->qgroup_lock
);
2082 enabled
= fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_ON
;
2083 spin_unlock(&fs_info
->qgroup_lock
);
2085 return sysfs_emit(buf
, "%d\n", enabled
);
2087 BTRFS_ATTR(qgroups
, enabled
, qgroup_enabled_show
);
2089 static ssize_t
qgroup_inconsistent_show(struct kobject
*qgroups_kobj
,
2090 struct kobj_attribute
*a
,
2093 struct btrfs_fs_info
*fs_info
= to_fs_info(qgroups_kobj
->parent
);
2096 spin_lock(&fs_info
->qgroup_lock
);
2097 inconsistent
= (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
);
2098 spin_unlock(&fs_info
->qgroup_lock
);
2100 return sysfs_emit(buf
, "%d\n", inconsistent
);
2102 BTRFS_ATTR(qgroups
, inconsistent
, qgroup_inconsistent_show
);
2104 static ssize_t
qgroup_drop_subtree_thres_show(struct kobject
*qgroups_kobj
,
2105 struct kobj_attribute
*a
,
2108 struct btrfs_fs_info
*fs_info
= to_fs_info(qgroups_kobj
->parent
);
2111 spin_lock(&fs_info
->qgroup_lock
);
2112 result
= fs_info
->qgroup_drop_subtree_thres
;
2113 spin_unlock(&fs_info
->qgroup_lock
);
2115 return sysfs_emit(buf
, "%d\n", result
);
2118 static ssize_t
qgroup_drop_subtree_thres_store(struct kobject
*qgroups_kobj
,
2119 struct kobj_attribute
*a
,
2120 const char *buf
, size_t len
)
2122 struct btrfs_fs_info
*fs_info
= to_fs_info(qgroups_kobj
->parent
);
2126 ret
= kstrtou8(buf
, 10, &new_thres
);
2130 if (new_thres
> BTRFS_MAX_LEVEL
)
2133 spin_lock(&fs_info
->qgroup_lock
);
2134 fs_info
->qgroup_drop_subtree_thres
= new_thres
;
2135 spin_unlock(&fs_info
->qgroup_lock
);
2139 BTRFS_ATTR_RW(qgroups
, drop_subtree_threshold
, qgroup_drop_subtree_thres_show
,
2140 qgroup_drop_subtree_thres_store
);
2143 * Qgroups global info
2145 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2147 static struct attribute
*qgroups_attrs
[] = {
2148 BTRFS_ATTR_PTR(qgroups
, enabled
),
2149 BTRFS_ATTR_PTR(qgroups
, inconsistent
),
2150 BTRFS_ATTR_PTR(qgroups
, drop_subtree_threshold
),
2153 ATTRIBUTE_GROUPS(qgroups
);
2155 static void qgroups_release(struct kobject
*kobj
)
2160 static const struct kobj_type qgroups_ktype
= {
2161 .sysfs_ops
= &kobj_sysfs_ops
,
2162 .default_groups
= qgroups_groups
,
2163 .release
= qgroups_release
,
2166 static inline struct btrfs_fs_info
*qgroup_kobj_to_fs_info(struct kobject
*kobj
)
2168 return to_fs_info(kobj
->parent
->parent
);
2171 #define QGROUP_ATTR(_member, _show_name) \
2172 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
2173 struct kobj_attribute *a, \
2176 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2177 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2178 struct btrfs_qgroup, kobj); \
2179 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
2181 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2183 #define QGROUP_RSV_ATTR(_name, _type) \
2184 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
2185 struct kobj_attribute *a, \
2188 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2189 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2190 struct btrfs_qgroup, kobj); \
2191 return btrfs_show_u64(&qgroup->rsv.values[_type], \
2192 &fs_info->qgroup_lock, buf); \
2194 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2196 QGROUP_ATTR(rfer
, referenced
);
2197 QGROUP_ATTR(excl
, exclusive
);
2198 QGROUP_ATTR(max_rfer
, max_referenced
);
2199 QGROUP_ATTR(max_excl
, max_exclusive
);
2200 QGROUP_ATTR(lim_flags
, limit_flags
);
2201 QGROUP_RSV_ATTR(data
, BTRFS_QGROUP_RSV_DATA
);
2202 QGROUP_RSV_ATTR(meta_pertrans
, BTRFS_QGROUP_RSV_META_PERTRANS
);
2203 QGROUP_RSV_ATTR(meta_prealloc
, BTRFS_QGROUP_RSV_META_PREALLOC
);
2206 * Qgroup information.
2208 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2210 static struct attribute
*qgroup_attrs
[] = {
2211 BTRFS_ATTR_PTR(qgroup
, referenced
),
2212 BTRFS_ATTR_PTR(qgroup
, exclusive
),
2213 BTRFS_ATTR_PTR(qgroup
, max_referenced
),
2214 BTRFS_ATTR_PTR(qgroup
, max_exclusive
),
2215 BTRFS_ATTR_PTR(qgroup
, limit_flags
),
2216 BTRFS_ATTR_PTR(qgroup
, rsv_data
),
2217 BTRFS_ATTR_PTR(qgroup
, rsv_meta_pertrans
),
2218 BTRFS_ATTR_PTR(qgroup
, rsv_meta_prealloc
),
2221 ATTRIBUTE_GROUPS(qgroup
);
2223 static void qgroup_release(struct kobject
*kobj
)
2225 struct btrfs_qgroup
*qgroup
= container_of(kobj
, struct btrfs_qgroup
, kobj
);
2227 memset(&qgroup
->kobj
, 0, sizeof(*kobj
));
2230 static const struct kobj_type qgroup_ktype
= {
2231 .sysfs_ops
= &kobj_sysfs_ops
,
2232 .release
= qgroup_release
,
2233 .default_groups
= qgroup_groups
,
2236 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info
*fs_info
,
2237 struct btrfs_qgroup
*qgroup
)
2239 struct kobject
*qgroups_kobj
= fs_info
->qgroups_kobj
;
2242 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
2244 if (qgroup
->kobj
.state_initialized
)
2249 ret
= kobject_init_and_add(&qgroup
->kobj
, &qgroup_ktype
, qgroups_kobj
,
2250 "%hu_%llu", btrfs_qgroup_level(qgroup
->qgroupid
),
2251 btrfs_qgroup_subvolid(qgroup
->qgroupid
));
2253 kobject_put(&qgroup
->kobj
);
2258 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info
*fs_info
)
2260 struct btrfs_qgroup
*qgroup
;
2261 struct btrfs_qgroup
*next
;
2263 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
2266 rbtree_postorder_for_each_entry_safe(qgroup
, next
,
2267 &fs_info
->qgroup_tree
, node
)
2268 btrfs_sysfs_del_one_qgroup(fs_info
, qgroup
);
2269 if (fs_info
->qgroups_kobj
) {
2270 kobject_del(fs_info
->qgroups_kobj
);
2271 kobject_put(fs_info
->qgroups_kobj
);
2272 fs_info
->qgroups_kobj
= NULL
;
2276 /* Called when qgroups get initialized, thus there is no need for locking */
2277 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info
*fs_info
)
2279 struct kobject
*fsid_kobj
= &fs_info
->fs_devices
->fsid_kobj
;
2280 struct btrfs_qgroup
*qgroup
;
2281 struct btrfs_qgroup
*next
;
2284 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
2288 if (fs_info
->qgroups_kobj
)
2291 fs_info
->qgroups_kobj
= kzalloc(sizeof(struct kobject
), GFP_KERNEL
);
2292 if (!fs_info
->qgroups_kobj
)
2295 ret
= kobject_init_and_add(fs_info
->qgroups_kobj
, &qgroups_ktype
,
2296 fsid_kobj
, "qgroups");
2300 rbtree_postorder_for_each_entry_safe(qgroup
, next
,
2301 &fs_info
->qgroup_tree
, node
) {
2302 ret
= btrfs_sysfs_add_one_qgroup(fs_info
, qgroup
);
2309 btrfs_sysfs_del_qgroups(fs_info
);
2313 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info
*fs_info
,
2314 struct btrfs_qgroup
*qgroup
)
2316 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
2319 if (qgroup
->kobj
.state_initialized
) {
2320 kobject_del(&qgroup
->kobj
);
2321 kobject_put(&qgroup
->kobj
);
2326 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2327 * values in superblock. Call after any changes to incompat/compat_ro flags
2329 void btrfs_sysfs_feature_update(struct btrfs_fs_info
*fs_info
)
2331 struct kobject
*fsid_kobj
;
2337 fsid_kobj
= &fs_info
->fs_devices
->fsid_kobj
;
2338 if (!fsid_kobj
->state_initialized
)
2341 ret
= sysfs_update_group(fsid_kobj
, &btrfs_feature_attr_group
);
2344 "failed to update /sys/fs/btrfs/%pU/features: %d",
2345 fs_info
->fs_devices
->fsid
, ret
);
2348 int __init
btrfs_init_sysfs(void)
2352 btrfs_kset
= kset_create_and_add("btrfs", NULL
, fs_kobj
);
2356 init_feature_attrs();
2357 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
2360 ret
= sysfs_merge_group(&btrfs_kset
->kobj
,
2361 &btrfs_static_feature_attr_group
);
2363 goto out_remove_group
;
2365 #ifdef CONFIG_BTRFS_DEBUG
2366 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_debug_feature_attr_group
);
2368 sysfs_unmerge_group(&btrfs_kset
->kobj
,
2369 &btrfs_static_feature_attr_group
);
2370 goto out_remove_group
;
2377 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
2379 kset_unregister(btrfs_kset
);
2384 void __cold
btrfs_exit_sysfs(void)
2386 sysfs_unmerge_group(&btrfs_kset
->kobj
,
2387 &btrfs_static_feature_attr_group
);
2388 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
2389 #ifdef CONFIG_BTRFS_DEBUG
2390 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_debug_feature_attr_group
);
2392 kset_unregister(btrfs_kset
);