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"
25 struct btrfs_feature_attr
{
26 struct kobj_attribute kobj_attr
;
27 enum btrfs_feature_set feature_set
;
31 /* For raid type sysfs entries */
37 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
39 .attr = { .name = __stringify(_name), .mode = _mode }, \
44 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
45 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
46 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
48 #define BTRFS_ATTR(_prefix, _name, _show) \
49 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
50 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
52 #define BTRFS_ATTR_PTR(_prefix, _name) \
53 (&btrfs_attr_##_prefix##_##_name.attr)
55 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
56 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
57 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
58 btrfs_feature_attr_show, \
59 btrfs_feature_attr_store), \
60 .feature_set = _feature_set, \
61 .feature_bit = _feature_prefix ##_## _feature_bit, \
63 #define BTRFS_FEAT_ATTR_PTR(_name) \
64 (&btrfs_attr_features_##_name.kobj_attr.attr)
66 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
68 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
69 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
70 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
71 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
73 static inline struct btrfs_fs_info
*to_fs_info(struct kobject
*kobj
);
74 static inline struct btrfs_fs_devices
*to_fs_devs(struct kobject
*kobj
);
76 static struct btrfs_feature_attr
*to_btrfs_feature_attr(struct kobj_attribute
*a
)
78 return container_of(a
, struct btrfs_feature_attr
, kobj_attr
);
81 static struct kobj_attribute
*attr_to_btrfs_attr(struct attribute
*attr
)
83 return container_of(attr
, struct kobj_attribute
, attr
);
86 static struct btrfs_feature_attr
*attr_to_btrfs_feature_attr(
87 struct attribute
*attr
)
89 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr
));
92 static u64
get_features(struct btrfs_fs_info
*fs_info
,
93 enum btrfs_feature_set set
)
95 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
96 if (set
== FEAT_COMPAT
)
97 return btrfs_super_compat_flags(disk_super
);
98 else if (set
== FEAT_COMPAT_RO
)
99 return btrfs_super_compat_ro_flags(disk_super
);
101 return btrfs_super_incompat_flags(disk_super
);
104 static void set_features(struct btrfs_fs_info
*fs_info
,
105 enum btrfs_feature_set set
, u64 features
)
107 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
108 if (set
== FEAT_COMPAT
)
109 btrfs_set_super_compat_flags(disk_super
, features
);
110 else if (set
== FEAT_COMPAT_RO
)
111 btrfs_set_super_compat_ro_flags(disk_super
, features
);
113 btrfs_set_super_incompat_flags(disk_super
, features
);
116 static int can_modify_feature(struct btrfs_feature_attr
*fa
)
120 switch (fa
->feature_set
) {
122 set
= BTRFS_FEATURE_COMPAT_SAFE_SET
;
123 clear
= BTRFS_FEATURE_COMPAT_SAFE_CLEAR
;
126 set
= BTRFS_FEATURE_COMPAT_RO_SAFE_SET
;
127 clear
= BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR
;
130 set
= BTRFS_FEATURE_INCOMPAT_SAFE_SET
;
131 clear
= BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR
;
134 pr_warn("btrfs: sysfs: unknown feature set %d\n",
139 if (set
& fa
->feature_bit
)
141 if (clear
& fa
->feature_bit
)
147 static ssize_t
btrfs_feature_attr_show(struct kobject
*kobj
,
148 struct kobj_attribute
*a
, char *buf
)
151 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
152 struct btrfs_feature_attr
*fa
= to_btrfs_feature_attr(a
);
154 u64 features
= get_features(fs_info
, fa
->feature_set
);
155 if (features
& fa
->feature_bit
)
158 val
= can_modify_feature(fa
);
160 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
163 static ssize_t
btrfs_feature_attr_store(struct kobject
*kobj
,
164 struct kobj_attribute
*a
,
165 const char *buf
, size_t count
)
167 struct btrfs_fs_info
*fs_info
;
168 struct btrfs_feature_attr
*fa
= to_btrfs_feature_attr(a
);
169 u64 features
, set
, clear
;
173 fs_info
= to_fs_info(kobj
);
177 if (sb_rdonly(fs_info
->sb
))
180 ret
= kstrtoul(skip_spaces(buf
), 0, &val
);
184 if (fa
->feature_set
== FEAT_COMPAT
) {
185 set
= BTRFS_FEATURE_COMPAT_SAFE_SET
;
186 clear
= BTRFS_FEATURE_COMPAT_SAFE_CLEAR
;
187 } else if (fa
->feature_set
== FEAT_COMPAT_RO
) {
188 set
= BTRFS_FEATURE_COMPAT_RO_SAFE_SET
;
189 clear
= BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR
;
191 set
= BTRFS_FEATURE_INCOMPAT_SAFE_SET
;
192 clear
= BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR
;
195 features
= get_features(fs_info
, fa
->feature_set
);
198 if ((val
&& (features
& fa
->feature_bit
)) ||
199 (!val
&& !(features
& fa
->feature_bit
)))
202 if ((val
&& !(set
& fa
->feature_bit
)) ||
203 (!val
&& !(clear
& fa
->feature_bit
))) {
205 "%sabling feature %s on mounted fs is not supported.",
206 val
? "En" : "Dis", fa
->kobj_attr
.attr
.name
);
210 btrfs_info(fs_info
, "%s %s feature flag",
211 val
? "Setting" : "Clearing", fa
->kobj_attr
.attr
.name
);
213 spin_lock(&fs_info
->super_lock
);
214 features
= get_features(fs_info
, fa
->feature_set
);
216 features
|= fa
->feature_bit
;
218 features
&= ~fa
->feature_bit
;
219 set_features(fs_info
, fa
->feature_set
, features
);
220 spin_unlock(&fs_info
->super_lock
);
223 * We don't want to do full transaction commit from inside sysfs
225 btrfs_set_pending(fs_info
, COMMIT
);
226 wake_up_process(fs_info
->transaction_kthread
);
231 static umode_t
btrfs_feature_visible(struct kobject
*kobj
,
232 struct attribute
*attr
, int unused
)
234 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
235 umode_t mode
= attr
->mode
;
238 struct btrfs_feature_attr
*fa
;
241 fa
= attr_to_btrfs_feature_attr(attr
);
242 features
= get_features(fs_info
, fa
->feature_set
);
244 if (can_modify_feature(fa
))
246 else if (!(features
& fa
->feature_bit
))
253 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref
, MIXED_BACKREF
);
254 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol
, DEFAULT_SUBVOL
);
255 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups
, MIXED_GROUPS
);
256 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo
, COMPRESS_LZO
);
257 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd
, COMPRESS_ZSTD
);
258 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata
, BIG_METADATA
);
259 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref
, EXTENDED_IREF
);
260 BTRFS_FEAT_ATTR_INCOMPAT(raid56
, RAID56
);
261 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata
, SKINNY_METADATA
);
262 BTRFS_FEAT_ATTR_INCOMPAT(no_holes
, NO_HOLES
);
263 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid
, METADATA_UUID
);
264 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree
, FREE_SPACE_TREE
);
265 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34
, RAID1C34
);
266 /* Remove once support for zoned allocation is feature complete */
267 #ifdef CONFIG_BTRFS_DEBUG
268 BTRFS_FEAT_ATTR_INCOMPAT(zoned
, ZONED
);
271 static struct attribute
*btrfs_supported_feature_attrs
[] = {
272 BTRFS_FEAT_ATTR_PTR(mixed_backref
),
273 BTRFS_FEAT_ATTR_PTR(default_subvol
),
274 BTRFS_FEAT_ATTR_PTR(mixed_groups
),
275 BTRFS_FEAT_ATTR_PTR(compress_lzo
),
276 BTRFS_FEAT_ATTR_PTR(compress_zstd
),
277 BTRFS_FEAT_ATTR_PTR(big_metadata
),
278 BTRFS_FEAT_ATTR_PTR(extended_iref
),
279 BTRFS_FEAT_ATTR_PTR(raid56
),
280 BTRFS_FEAT_ATTR_PTR(skinny_metadata
),
281 BTRFS_FEAT_ATTR_PTR(no_holes
),
282 BTRFS_FEAT_ATTR_PTR(metadata_uuid
),
283 BTRFS_FEAT_ATTR_PTR(free_space_tree
),
284 BTRFS_FEAT_ATTR_PTR(raid1c34
),
285 #ifdef CONFIG_BTRFS_DEBUG
286 BTRFS_FEAT_ATTR_PTR(zoned
),
292 * Features which depend on feature bits and may differ between each fs.
294 * /sys/fs/btrfs/features lists all available features of this kernel while
295 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
296 * can be changed online.
298 static const struct attribute_group btrfs_feature_attr_group
= {
300 .is_visible
= btrfs_feature_visible
,
301 .attrs
= btrfs_supported_feature_attrs
,
304 static ssize_t
rmdir_subvol_show(struct kobject
*kobj
,
305 struct kobj_attribute
*ka
, char *buf
)
307 return scnprintf(buf
, PAGE_SIZE
, "0\n");
309 BTRFS_ATTR(static_feature
, rmdir_subvol
, rmdir_subvol_show
);
311 static ssize_t
supported_checksums_show(struct kobject
*kobj
,
312 struct kobj_attribute
*a
, char *buf
)
317 for (i
= 0; i
< btrfs_get_num_csums(); i
++) {
319 * This "trick" only works as long as 'enum btrfs_csum_type' has
322 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s%s",
323 (i
== 0 ? "" : " "), btrfs_super_csum_name(i
));
327 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
330 BTRFS_ATTR(static_feature
, supported_checksums
, supported_checksums_show
);
332 static ssize_t
send_stream_version_show(struct kobject
*kobj
,
333 struct kobj_attribute
*ka
, char *buf
)
335 return snprintf(buf
, PAGE_SIZE
, "%d\n", BTRFS_SEND_STREAM_VERSION
);
337 BTRFS_ATTR(static_feature
, send_stream_version
, send_stream_version_show
);
339 static const char *rescue_opts
[] = {
347 static ssize_t
supported_rescue_options_show(struct kobject
*kobj
,
348 struct kobj_attribute
*a
,
354 for (i
= 0; i
< ARRAY_SIZE(rescue_opts
); i
++)
355 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s%s",
356 (i
? " " : ""), rescue_opts
[i
]);
357 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
360 BTRFS_ATTR(static_feature
, supported_rescue_options
,
361 supported_rescue_options_show
);
363 static struct attribute
*btrfs_supported_static_feature_attrs
[] = {
364 BTRFS_ATTR_PTR(static_feature
, rmdir_subvol
),
365 BTRFS_ATTR_PTR(static_feature
, supported_checksums
),
366 BTRFS_ATTR_PTR(static_feature
, send_stream_version
),
367 BTRFS_ATTR_PTR(static_feature
, supported_rescue_options
),
372 * Features which only depend on kernel version.
374 * These are listed in /sys/fs/btrfs/features along with
375 * btrfs_feature_attr_group
377 static const struct attribute_group btrfs_static_feature_attr_group
= {
379 .attrs
= btrfs_supported_static_feature_attrs
,
382 #ifdef CONFIG_BTRFS_DEBUG
385 * Discard statistics and tunables
387 #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
389 static ssize_t
btrfs_discardable_bytes_show(struct kobject
*kobj
,
390 struct kobj_attribute
*a
,
393 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
395 return scnprintf(buf
, PAGE_SIZE
, "%lld\n",
396 atomic64_read(&fs_info
->discard_ctl
.discardable_bytes
));
398 BTRFS_ATTR(discard
, discardable_bytes
, btrfs_discardable_bytes_show
);
400 static ssize_t
btrfs_discardable_extents_show(struct kobject
*kobj
,
401 struct kobj_attribute
*a
,
404 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
406 return scnprintf(buf
, PAGE_SIZE
, "%d\n",
407 atomic_read(&fs_info
->discard_ctl
.discardable_extents
));
409 BTRFS_ATTR(discard
, discardable_extents
, btrfs_discardable_extents_show
);
411 static ssize_t
btrfs_discard_bitmap_bytes_show(struct kobject
*kobj
,
412 struct kobj_attribute
*a
,
415 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
417 return scnprintf(buf
, PAGE_SIZE
, "%lld\n",
418 fs_info
->discard_ctl
.discard_bitmap_bytes
);
420 BTRFS_ATTR(discard
, discard_bitmap_bytes
, btrfs_discard_bitmap_bytes_show
);
422 static ssize_t
btrfs_discard_bytes_saved_show(struct kobject
*kobj
,
423 struct kobj_attribute
*a
,
426 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
428 return scnprintf(buf
, PAGE_SIZE
, "%lld\n",
429 atomic64_read(&fs_info
->discard_ctl
.discard_bytes_saved
));
431 BTRFS_ATTR(discard
, discard_bytes_saved
, btrfs_discard_bytes_saved_show
);
433 static ssize_t
btrfs_discard_extent_bytes_show(struct kobject
*kobj
,
434 struct kobj_attribute
*a
,
437 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
439 return scnprintf(buf
, PAGE_SIZE
, "%lld\n",
440 fs_info
->discard_ctl
.discard_extent_bytes
);
442 BTRFS_ATTR(discard
, discard_extent_bytes
, btrfs_discard_extent_bytes_show
);
444 static ssize_t
btrfs_discard_iops_limit_show(struct kobject
*kobj
,
445 struct kobj_attribute
*a
,
448 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
450 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
451 READ_ONCE(fs_info
->discard_ctl
.iops_limit
));
454 static ssize_t
btrfs_discard_iops_limit_store(struct kobject
*kobj
,
455 struct kobj_attribute
*a
,
456 const char *buf
, size_t len
)
458 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
459 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
463 ret
= kstrtou32(buf
, 10, &iops_limit
);
467 WRITE_ONCE(discard_ctl
->iops_limit
, iops_limit
);
468 btrfs_discard_calc_delay(discard_ctl
);
469 btrfs_discard_schedule_work(discard_ctl
, true);
472 BTRFS_ATTR_RW(discard
, iops_limit
, btrfs_discard_iops_limit_show
,
473 btrfs_discard_iops_limit_store
);
475 static ssize_t
btrfs_discard_kbps_limit_show(struct kobject
*kobj
,
476 struct kobj_attribute
*a
,
479 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
481 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
482 READ_ONCE(fs_info
->discard_ctl
.kbps_limit
));
485 static ssize_t
btrfs_discard_kbps_limit_store(struct kobject
*kobj
,
486 struct kobj_attribute
*a
,
487 const char *buf
, size_t len
)
489 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
490 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
494 ret
= kstrtou32(buf
, 10, &kbps_limit
);
498 WRITE_ONCE(discard_ctl
->kbps_limit
, kbps_limit
);
499 btrfs_discard_schedule_work(discard_ctl
, true);
502 BTRFS_ATTR_RW(discard
, kbps_limit
, btrfs_discard_kbps_limit_show
,
503 btrfs_discard_kbps_limit_store
);
505 static ssize_t
btrfs_discard_max_discard_size_show(struct kobject
*kobj
,
506 struct kobj_attribute
*a
,
509 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
511 return scnprintf(buf
, PAGE_SIZE
, "%llu\n",
512 READ_ONCE(fs_info
->discard_ctl
.max_discard_size
));
515 static ssize_t
btrfs_discard_max_discard_size_store(struct kobject
*kobj
,
516 struct kobj_attribute
*a
,
517 const char *buf
, size_t len
)
519 struct btrfs_fs_info
*fs_info
= discard_to_fs_info(kobj
);
520 struct btrfs_discard_ctl
*discard_ctl
= &fs_info
->discard_ctl
;
521 u64 max_discard_size
;
524 ret
= kstrtou64(buf
, 10, &max_discard_size
);
528 WRITE_ONCE(discard_ctl
->max_discard_size
, max_discard_size
);
532 BTRFS_ATTR_RW(discard
, max_discard_size
, btrfs_discard_max_discard_size_show
,
533 btrfs_discard_max_discard_size_store
);
535 static const struct attribute
*discard_debug_attrs
[] = {
536 BTRFS_ATTR_PTR(discard
, discardable_bytes
),
537 BTRFS_ATTR_PTR(discard
, discardable_extents
),
538 BTRFS_ATTR_PTR(discard
, discard_bitmap_bytes
),
539 BTRFS_ATTR_PTR(discard
, discard_bytes_saved
),
540 BTRFS_ATTR_PTR(discard
, discard_extent_bytes
),
541 BTRFS_ATTR_PTR(discard
, iops_limit
),
542 BTRFS_ATTR_PTR(discard
, kbps_limit
),
543 BTRFS_ATTR_PTR(discard
, max_discard_size
),
548 * Runtime debugging exported via sysfs
550 * /sys/fs/btrfs/debug - applies to module or all filesystems
551 * /sys/fs/btrfs/UUID - applies only to the given filesystem
553 static const struct attribute
*btrfs_debug_mount_attrs
[] = {
557 static struct attribute
*btrfs_debug_feature_attrs
[] = {
561 static const struct attribute_group btrfs_debug_feature_attr_group
= {
563 .attrs
= btrfs_debug_feature_attrs
,
568 static ssize_t
btrfs_show_u64(u64
*value_ptr
, spinlock_t
*lock
, char *buf
)
576 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", val
);
579 static ssize_t
global_rsv_size_show(struct kobject
*kobj
,
580 struct kobj_attribute
*ka
, char *buf
)
582 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
583 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
584 return btrfs_show_u64(&block_rsv
->size
, &block_rsv
->lock
, buf
);
586 BTRFS_ATTR(allocation
, global_rsv_size
, global_rsv_size_show
);
588 static ssize_t
global_rsv_reserved_show(struct kobject
*kobj
,
589 struct kobj_attribute
*a
, char *buf
)
591 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
->parent
);
592 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
593 return btrfs_show_u64(&block_rsv
->reserved
, &block_rsv
->lock
, buf
);
595 BTRFS_ATTR(allocation
, global_rsv_reserved
, global_rsv_reserved_show
);
597 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
598 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
600 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
601 struct kobj_attribute
*attr
, char *buf
);
602 BTRFS_ATTR(raid
, total_bytes
, raid_bytes_show
);
603 BTRFS_ATTR(raid
, used_bytes
, raid_bytes_show
);
605 static ssize_t
raid_bytes_show(struct kobject
*kobj
,
606 struct kobj_attribute
*attr
, char *buf
)
609 struct btrfs_space_info
*sinfo
= to_space_info(kobj
->parent
);
610 struct btrfs_block_group
*block_group
;
611 int index
= btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj
)->flags
);
614 down_read(&sinfo
->groups_sem
);
615 list_for_each_entry(block_group
, &sinfo
->block_groups
[index
], list
) {
616 if (&attr
->attr
== BTRFS_ATTR_PTR(raid
, total_bytes
))
617 val
+= block_group
->length
;
619 val
+= block_group
->used
;
621 up_read(&sinfo
->groups_sem
);
622 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", val
);
625 static struct attribute
*raid_attrs
[] = {
626 BTRFS_ATTR_PTR(raid
, total_bytes
),
627 BTRFS_ATTR_PTR(raid
, used_bytes
),
630 ATTRIBUTE_GROUPS(raid
);
632 static void release_raid_kobj(struct kobject
*kobj
)
634 kfree(to_raid_kobj(kobj
));
637 static struct kobj_type btrfs_raid_ktype
= {
638 .sysfs_ops
= &kobj_sysfs_ops
,
639 .release
= release_raid_kobj
,
640 .default_groups
= raid_groups
,
643 #define SPACE_INFO_ATTR(field) \
644 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
645 struct kobj_attribute *a, \
648 struct btrfs_space_info *sinfo = to_space_info(kobj); \
649 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
651 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
653 static ssize_t
btrfs_space_info_show_total_bytes_pinned(struct kobject
*kobj
,
654 struct kobj_attribute
*a
,
657 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
658 s64 val
= percpu_counter_sum(&sinfo
->total_bytes_pinned
);
659 return scnprintf(buf
, PAGE_SIZE
, "%lld\n", val
);
662 SPACE_INFO_ATTR(flags
);
663 SPACE_INFO_ATTR(total_bytes
);
664 SPACE_INFO_ATTR(bytes_used
);
665 SPACE_INFO_ATTR(bytes_pinned
);
666 SPACE_INFO_ATTR(bytes_reserved
);
667 SPACE_INFO_ATTR(bytes_may_use
);
668 SPACE_INFO_ATTR(bytes_readonly
);
669 SPACE_INFO_ATTR(disk_used
);
670 SPACE_INFO_ATTR(disk_total
);
671 BTRFS_ATTR(space_info
, total_bytes_pinned
,
672 btrfs_space_info_show_total_bytes_pinned
);
674 static struct attribute
*space_info_attrs
[] = {
675 BTRFS_ATTR_PTR(space_info
, flags
),
676 BTRFS_ATTR_PTR(space_info
, total_bytes
),
677 BTRFS_ATTR_PTR(space_info
, bytes_used
),
678 BTRFS_ATTR_PTR(space_info
, bytes_pinned
),
679 BTRFS_ATTR_PTR(space_info
, bytes_reserved
),
680 BTRFS_ATTR_PTR(space_info
, bytes_may_use
),
681 BTRFS_ATTR_PTR(space_info
, bytes_readonly
),
682 BTRFS_ATTR_PTR(space_info
, disk_used
),
683 BTRFS_ATTR_PTR(space_info
, disk_total
),
684 BTRFS_ATTR_PTR(space_info
, total_bytes_pinned
),
687 ATTRIBUTE_GROUPS(space_info
);
689 static void space_info_release(struct kobject
*kobj
)
691 struct btrfs_space_info
*sinfo
= to_space_info(kobj
);
692 percpu_counter_destroy(&sinfo
->total_bytes_pinned
);
696 static struct kobj_type space_info_ktype
= {
697 .sysfs_ops
= &kobj_sysfs_ops
,
698 .release
= space_info_release
,
699 .default_groups
= space_info_groups
,
702 static const struct attribute
*allocation_attrs
[] = {
703 BTRFS_ATTR_PTR(allocation
, global_rsv_reserved
),
704 BTRFS_ATTR_PTR(allocation
, global_rsv_size
),
708 static ssize_t
btrfs_label_show(struct kobject
*kobj
,
709 struct kobj_attribute
*a
, char *buf
)
711 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
712 char *label
= fs_info
->super_copy
->label
;
715 spin_lock(&fs_info
->super_lock
);
716 ret
= scnprintf(buf
, PAGE_SIZE
, label
[0] ? "%s\n" : "%s", label
);
717 spin_unlock(&fs_info
->super_lock
);
722 static ssize_t
btrfs_label_store(struct kobject
*kobj
,
723 struct kobj_attribute
*a
,
724 const char *buf
, size_t len
)
726 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
732 if (sb_rdonly(fs_info
->sb
))
736 * p_len is the len until the first occurrence of either
739 p_len
= strcspn(buf
, "\n");
741 if (p_len
>= BTRFS_LABEL_SIZE
)
744 spin_lock(&fs_info
->super_lock
);
745 memset(fs_info
->super_copy
->label
, 0, BTRFS_LABEL_SIZE
);
746 memcpy(fs_info
->super_copy
->label
, buf
, p_len
);
747 spin_unlock(&fs_info
->super_lock
);
750 * We don't want to do full transaction commit from inside sysfs
752 btrfs_set_pending(fs_info
, COMMIT
);
753 wake_up_process(fs_info
->transaction_kthread
);
757 BTRFS_ATTR_RW(, label
, btrfs_label_show
, btrfs_label_store
);
759 static ssize_t
btrfs_nodesize_show(struct kobject
*kobj
,
760 struct kobj_attribute
*a
, char *buf
)
762 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
764 return scnprintf(buf
, PAGE_SIZE
, "%u\n", fs_info
->super_copy
->nodesize
);
767 BTRFS_ATTR(, nodesize
, btrfs_nodesize_show
);
769 static ssize_t
btrfs_sectorsize_show(struct kobject
*kobj
,
770 struct kobj_attribute
*a
, char *buf
)
772 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
774 return scnprintf(buf
, PAGE_SIZE
, "%u\n",
775 fs_info
->super_copy
->sectorsize
);
778 BTRFS_ATTR(, sectorsize
, btrfs_sectorsize_show
);
780 static ssize_t
btrfs_clone_alignment_show(struct kobject
*kobj
,
781 struct kobj_attribute
*a
, char *buf
)
783 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
785 return scnprintf(buf
, PAGE_SIZE
, "%u\n", fs_info
->super_copy
->sectorsize
);
788 BTRFS_ATTR(, clone_alignment
, btrfs_clone_alignment_show
);
790 static ssize_t
quota_override_show(struct kobject
*kobj
,
791 struct kobj_attribute
*a
, char *buf
)
793 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
796 quota_override
= test_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
797 return scnprintf(buf
, PAGE_SIZE
, "%d\n", quota_override
);
800 static ssize_t
quota_override_store(struct kobject
*kobj
,
801 struct kobj_attribute
*a
,
802 const char *buf
, size_t len
)
804 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
811 if (!capable(CAP_SYS_RESOURCE
))
814 err
= kstrtoul(buf
, 10, &knob
);
821 set_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
823 clear_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
);
828 BTRFS_ATTR_RW(, quota_override
, quota_override_show
, quota_override_store
);
830 static ssize_t
btrfs_metadata_uuid_show(struct kobject
*kobj
,
831 struct kobj_attribute
*a
, char *buf
)
833 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
835 return scnprintf(buf
, PAGE_SIZE
, "%pU\n",
836 fs_info
->fs_devices
->metadata_uuid
);
839 BTRFS_ATTR(, metadata_uuid
, btrfs_metadata_uuid_show
);
841 static ssize_t
btrfs_checksum_show(struct kobject
*kobj
,
842 struct kobj_attribute
*a
, char *buf
)
844 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
845 u16 csum_type
= btrfs_super_csum_type(fs_info
->super_copy
);
847 return scnprintf(buf
, PAGE_SIZE
, "%s (%s)\n",
848 btrfs_super_csum_name(csum_type
),
849 crypto_shash_driver_name(fs_info
->csum_shash
));
852 BTRFS_ATTR(, checksum
, btrfs_checksum_show
);
854 static ssize_t
btrfs_exclusive_operation_show(struct kobject
*kobj
,
855 struct kobj_attribute
*a
, char *buf
)
857 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
860 switch (READ_ONCE(fs_info
->exclusive_operation
)) {
861 case BTRFS_EXCLOP_NONE
:
864 case BTRFS_EXCLOP_BALANCE
:
867 case BTRFS_EXCLOP_DEV_ADD
:
868 str
= "device add\n";
870 case BTRFS_EXCLOP_DEV_REMOVE
:
871 str
= "device remove\n";
873 case BTRFS_EXCLOP_DEV_REPLACE
:
874 str
= "device replace\n";
876 case BTRFS_EXCLOP_RESIZE
:
879 case BTRFS_EXCLOP_SWAP_ACTIVATE
:
880 str
= "swap activate\n";
886 return scnprintf(buf
, PAGE_SIZE
, "%s", str
);
888 BTRFS_ATTR(, exclusive_operation
, btrfs_exclusive_operation_show
);
890 static ssize_t
btrfs_generation_show(struct kobject
*kobj
,
891 struct kobj_attribute
*a
, char *buf
)
893 struct btrfs_fs_info
*fs_info
= to_fs_info(kobj
);
895 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", fs_info
->generation
);
897 BTRFS_ATTR(, generation
, btrfs_generation_show
);
900 * Look for an exact string @string in @buffer with possible leading or
901 * trailing whitespace
903 static bool strmatch(const char *buffer
, const char *string
)
905 const size_t len
= strlen(string
);
907 /* Skip leading whitespace */
908 buffer
= skip_spaces(buffer
);
910 /* Match entire string, check if the rest is whitespace or empty */
911 if (strncmp(string
, buffer
, len
) == 0 &&
912 strlen(skip_spaces(buffer
+ len
)) == 0)
918 static const char * const btrfs_read_policy_name
[] = { "pid" };
920 static ssize_t
btrfs_read_policy_show(struct kobject
*kobj
,
921 struct kobj_attribute
*a
, char *buf
)
923 struct btrfs_fs_devices
*fs_devices
= to_fs_devs(kobj
);
927 for (i
= 0; i
< BTRFS_NR_READ_POLICY
; i
++) {
928 if (fs_devices
->read_policy
== i
)
929 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s[%s]",
930 (ret
== 0 ? "" : " "),
931 btrfs_read_policy_name
[i
]);
933 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s%s",
934 (ret
== 0 ? "" : " "),
935 btrfs_read_policy_name
[i
]);
938 ret
+= scnprintf(buf
+ ret
, PAGE_SIZE
- ret
, "\n");
943 static ssize_t
btrfs_read_policy_store(struct kobject
*kobj
,
944 struct kobj_attribute
*a
,
945 const char *buf
, size_t len
)
947 struct btrfs_fs_devices
*fs_devices
= to_fs_devs(kobj
);
950 for (i
= 0; i
< BTRFS_NR_READ_POLICY
; i
++) {
951 if (strmatch(buf
, btrfs_read_policy_name
[i
])) {
952 if (i
!= fs_devices
->read_policy
) {
953 fs_devices
->read_policy
= i
;
954 btrfs_info(fs_devices
->fs_info
,
955 "read policy set to '%s'",
956 btrfs_read_policy_name
[i
]);
964 BTRFS_ATTR_RW(, read_policy
, btrfs_read_policy_show
, btrfs_read_policy_store
);
966 static const struct attribute
*btrfs_attrs
[] = {
967 BTRFS_ATTR_PTR(, label
),
968 BTRFS_ATTR_PTR(, nodesize
),
969 BTRFS_ATTR_PTR(, sectorsize
),
970 BTRFS_ATTR_PTR(, clone_alignment
),
971 BTRFS_ATTR_PTR(, quota_override
),
972 BTRFS_ATTR_PTR(, metadata_uuid
),
973 BTRFS_ATTR_PTR(, checksum
),
974 BTRFS_ATTR_PTR(, exclusive_operation
),
975 BTRFS_ATTR_PTR(, generation
),
976 BTRFS_ATTR_PTR(, read_policy
),
980 static void btrfs_release_fsid_kobj(struct kobject
*kobj
)
982 struct btrfs_fs_devices
*fs_devs
= to_fs_devs(kobj
);
984 memset(&fs_devs
->fsid_kobj
, 0, sizeof(struct kobject
));
985 complete(&fs_devs
->kobj_unregister
);
988 static struct kobj_type btrfs_ktype
= {
989 .sysfs_ops
= &kobj_sysfs_ops
,
990 .release
= btrfs_release_fsid_kobj
,
993 static inline struct btrfs_fs_devices
*to_fs_devs(struct kobject
*kobj
)
995 if (kobj
->ktype
!= &btrfs_ktype
)
997 return container_of(kobj
, struct btrfs_fs_devices
, fsid_kobj
);
1000 static inline struct btrfs_fs_info
*to_fs_info(struct kobject
*kobj
)
1002 if (kobj
->ktype
!= &btrfs_ktype
)
1004 return to_fs_devs(kobj
)->fs_info
;
1007 #define NUM_FEATURE_BITS 64
1008 #define BTRFS_FEATURE_NAME_MAX 13
1009 static char btrfs_unknown_feature_names
[FEAT_MAX
][NUM_FEATURE_BITS
][BTRFS_FEATURE_NAME_MAX
];
1010 static struct btrfs_feature_attr btrfs_feature_attrs
[FEAT_MAX
][NUM_FEATURE_BITS
];
1012 static const u64 supported_feature_masks
[FEAT_MAX
] = {
1013 [FEAT_COMPAT
] = BTRFS_FEATURE_COMPAT_SUPP
,
1014 [FEAT_COMPAT_RO
] = BTRFS_FEATURE_COMPAT_RO_SUPP
,
1015 [FEAT_INCOMPAT
] = BTRFS_FEATURE_INCOMPAT_SUPP
,
1018 static int addrm_unknown_feature_attrs(struct btrfs_fs_info
*fs_info
, bool add
)
1022 for (set
= 0; set
< FEAT_MAX
; set
++) {
1024 struct attribute
*attrs
[2];
1025 struct attribute_group agroup
= {
1029 u64 features
= get_features(fs_info
, set
);
1030 features
&= ~supported_feature_masks
[set
];
1036 for (i
= 0; i
< NUM_FEATURE_BITS
; i
++) {
1037 struct btrfs_feature_attr
*fa
;
1039 if (!(features
& (1ULL << i
)))
1042 fa
= &btrfs_feature_attrs
[set
][i
];
1043 attrs
[0] = &fa
->kobj_attr
.attr
;
1046 ret
= sysfs_merge_group(&fs_info
->fs_devices
->fsid_kobj
,
1051 sysfs_unmerge_group(&fs_info
->fs_devices
->fsid_kobj
,
1059 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
1061 if (fs_devs
->devinfo_kobj
) {
1062 kobject_del(fs_devs
->devinfo_kobj
);
1063 kobject_put(fs_devs
->devinfo_kobj
);
1064 fs_devs
->devinfo_kobj
= NULL
;
1067 if (fs_devs
->devices_kobj
) {
1068 kobject_del(fs_devs
->devices_kobj
);
1069 kobject_put(fs_devs
->devices_kobj
);
1070 fs_devs
->devices_kobj
= NULL
;
1073 if (fs_devs
->fsid_kobj
.state_initialized
) {
1074 kobject_del(&fs_devs
->fsid_kobj
);
1075 kobject_put(&fs_devs
->fsid_kobj
);
1076 wait_for_completion(&fs_devs
->kobj_unregister
);
1080 /* when fs_devs is NULL it will remove all fsid kobject */
1081 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices
*fs_devs
)
1083 struct list_head
*fs_uuids
= btrfs_get_fs_uuids();
1086 __btrfs_sysfs_remove_fsid(fs_devs
);
1090 list_for_each_entry(fs_devs
, fs_uuids
, fs_list
) {
1091 __btrfs_sysfs_remove_fsid(fs_devs
);
1095 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1097 struct btrfs_device
*device
;
1098 struct btrfs_fs_devices
*seed
;
1100 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
1101 btrfs_sysfs_remove_device(device
);
1103 list_for_each_entry(seed
, &fs_devices
->seed_list
, seed_list
) {
1104 list_for_each_entry(device
, &seed
->devices
, dev_list
)
1105 btrfs_sysfs_remove_device(device
);
1109 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info
*fs_info
)
1111 struct kobject
*fsid_kobj
= &fs_info
->fs_devices
->fsid_kobj
;
1113 sysfs_remove_link(fsid_kobj
, "bdi");
1115 if (fs_info
->space_info_kobj
) {
1116 sysfs_remove_files(fs_info
->space_info_kobj
, allocation_attrs
);
1117 kobject_del(fs_info
->space_info_kobj
);
1118 kobject_put(fs_info
->space_info_kobj
);
1120 #ifdef CONFIG_BTRFS_DEBUG
1121 if (fs_info
->discard_debug_kobj
) {
1122 sysfs_remove_files(fs_info
->discard_debug_kobj
,
1123 discard_debug_attrs
);
1124 kobject_del(fs_info
->discard_debug_kobj
);
1125 kobject_put(fs_info
->discard_debug_kobj
);
1127 if (fs_info
->debug_kobj
) {
1128 sysfs_remove_files(fs_info
->debug_kobj
, btrfs_debug_mount_attrs
);
1129 kobject_del(fs_info
->debug_kobj
);
1130 kobject_put(fs_info
->debug_kobj
);
1133 addrm_unknown_feature_attrs(fs_info
, false);
1134 sysfs_remove_group(fsid_kobj
, &btrfs_feature_attr_group
);
1135 sysfs_remove_files(fsid_kobj
, btrfs_attrs
);
1136 btrfs_sysfs_remove_fs_devices(fs_info
->fs_devices
);
1139 static const char * const btrfs_feature_set_names
[FEAT_MAX
] = {
1140 [FEAT_COMPAT
] = "compat",
1141 [FEAT_COMPAT_RO
] = "compat_ro",
1142 [FEAT_INCOMPAT
] = "incompat",
1145 const char *btrfs_feature_set_name(enum btrfs_feature_set set
)
1147 return btrfs_feature_set_names
[set
];
1150 char *btrfs_printable_features(enum btrfs_feature_set set
, u64 flags
)
1152 size_t bufsize
= 4096; /* safe max, 64 names * 64 bytes */
1157 str
= kmalloc(bufsize
, GFP_KERNEL
);
1161 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
1164 if (!(flags
& (1ULL << i
)))
1167 name
= btrfs_feature_attrs
[set
][i
].kobj_attr
.attr
.name
;
1168 len
+= scnprintf(str
+ len
, bufsize
- len
, "%s%s",
1169 len
? "," : "", name
);
1175 static void init_feature_attrs(void)
1177 struct btrfs_feature_attr
*fa
;
1180 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names
) !=
1181 ARRAY_SIZE(btrfs_feature_attrs
));
1182 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names
[0]) !=
1183 ARRAY_SIZE(btrfs_feature_attrs
[0]));
1185 memset(btrfs_feature_attrs
, 0, sizeof(btrfs_feature_attrs
));
1186 memset(btrfs_unknown_feature_names
, 0,
1187 sizeof(btrfs_unknown_feature_names
));
1189 for (i
= 0; btrfs_supported_feature_attrs
[i
]; i
++) {
1190 struct btrfs_feature_attr
*sfa
;
1191 struct attribute
*a
= btrfs_supported_feature_attrs
[i
];
1193 sfa
= attr_to_btrfs_feature_attr(a
);
1194 bit
= ilog2(sfa
->feature_bit
);
1195 fa
= &btrfs_feature_attrs
[sfa
->feature_set
][bit
];
1197 fa
->kobj_attr
.attr
.name
= sfa
->kobj_attr
.attr
.name
;
1200 for (set
= 0; set
< FEAT_MAX
; set
++) {
1201 for (i
= 0; i
< ARRAY_SIZE(btrfs_feature_attrs
[set
]); i
++) {
1202 char *name
= btrfs_unknown_feature_names
[set
][i
];
1203 fa
= &btrfs_feature_attrs
[set
][i
];
1205 if (fa
->kobj_attr
.attr
.name
)
1208 snprintf(name
, BTRFS_FEATURE_NAME_MAX
, "%s:%u",
1209 btrfs_feature_set_names
[set
], i
);
1211 fa
->kobj_attr
.attr
.name
= name
;
1212 fa
->kobj_attr
.attr
.mode
= S_IRUGO
;
1213 fa
->feature_set
= set
;
1214 fa
->feature_bit
= 1ULL << i
;
1220 * Create a sysfs entry for a given block group type at path
1221 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1223 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group
*cache
)
1225 struct btrfs_fs_info
*fs_info
= cache
->fs_info
;
1226 struct btrfs_space_info
*space_info
= cache
->space_info
;
1227 struct raid_kobject
*rkobj
;
1228 const int index
= btrfs_bg_flags_to_raid_index(cache
->flags
);
1229 unsigned int nofs_flag
;
1233 * Setup a NOFS context because kobject_add(), deep in its call chain,
1234 * does GFP_KERNEL allocations, and we are often called in a context
1235 * where if reclaim is triggered we can deadlock (we are either holding
1236 * a transaction handle or some lock required for a transaction
1239 nofs_flag
= memalloc_nofs_save();
1241 rkobj
= kzalloc(sizeof(*rkobj
), GFP_NOFS
);
1243 memalloc_nofs_restore(nofs_flag
);
1244 btrfs_warn(cache
->fs_info
,
1245 "couldn't alloc memory for raid level kobject");
1249 rkobj
->flags
= cache
->flags
;
1250 kobject_init(&rkobj
->kobj
, &btrfs_raid_ktype
);
1253 * We call this either on mount, or if we've created a block group for a
1254 * new index type while running (i.e. when restriping). The running
1255 * case is tricky because we could race with other threads, so we need
1256 * to have this check to make sure we didn't already init the kobject.
1258 * We don't have to protect on the free side because it only happens on
1261 spin_lock(&space_info
->lock
);
1262 if (space_info
->block_group_kobjs
[index
]) {
1263 spin_unlock(&space_info
->lock
);
1264 kobject_put(&rkobj
->kobj
);
1267 space_info
->block_group_kobjs
[index
] = &rkobj
->kobj
;
1269 spin_unlock(&space_info
->lock
);
1271 ret
= kobject_add(&rkobj
->kobj
, &space_info
->kobj
, "%s",
1272 btrfs_bg_type_to_raid_name(rkobj
->flags
));
1273 memalloc_nofs_restore(nofs_flag
);
1275 spin_lock(&space_info
->lock
);
1276 space_info
->block_group_kobjs
[index
] = NULL
;
1277 spin_unlock(&space_info
->lock
);
1278 kobject_put(&rkobj
->kobj
);
1280 "failed to add kobject for block cache, ignoring");
1286 * Remove sysfs directories for all block group types of a given space info and
1287 * the space info as well
1289 void btrfs_sysfs_remove_space_info(struct btrfs_space_info
*space_info
)
1293 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1294 struct kobject
*kobj
;
1296 kobj
= space_info
->block_group_kobjs
[i
];
1297 space_info
->block_group_kobjs
[i
] = NULL
;
1303 kobject_del(&space_info
->kobj
);
1304 kobject_put(&space_info
->kobj
);
1307 static const char *alloc_name(u64 flags
)
1310 case BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
:
1312 case BTRFS_BLOCK_GROUP_METADATA
:
1314 case BTRFS_BLOCK_GROUP_DATA
:
1316 case BTRFS_BLOCK_GROUP_SYSTEM
:
1320 return "invalid-combination";
1325 * Create a sysfs entry for a space info type at path
1326 * /sys/fs/btrfs/UUID/allocation/TYPE
1328 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info
*fs_info
,
1329 struct btrfs_space_info
*space_info
)
1333 ret
= kobject_init_and_add(&space_info
->kobj
, &space_info_ktype
,
1334 fs_info
->space_info_kobj
, "%s",
1335 alloc_name(space_info
->flags
));
1337 kobject_put(&space_info
->kobj
);
1344 void btrfs_sysfs_remove_device(struct btrfs_device
*device
)
1346 struct kobject
*devices_kobj
;
1349 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1350 * fs_info::fs_devices.
1352 devices_kobj
= device
->fs_info
->fs_devices
->devices_kobj
;
1353 ASSERT(devices_kobj
);
1356 sysfs_remove_link(devices_kobj
, bdev_kobj(device
->bdev
)->name
);
1358 if (device
->devid_kobj
.state_initialized
) {
1359 kobject_del(&device
->devid_kobj
);
1360 kobject_put(&device
->devid_kobj
);
1361 wait_for_completion(&device
->kobj_unregister
);
1365 static ssize_t
btrfs_devinfo_in_fs_metadata_show(struct kobject
*kobj
,
1366 struct kobj_attribute
*a
,
1370 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1373 val
= !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
1375 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
1377 BTRFS_ATTR(devid
, in_fs_metadata
, btrfs_devinfo_in_fs_metadata_show
);
1379 static ssize_t
btrfs_devinfo_missing_show(struct kobject
*kobj
,
1380 struct kobj_attribute
*a
, char *buf
)
1383 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1386 val
= !!test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
1388 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
1390 BTRFS_ATTR(devid
, missing
, btrfs_devinfo_missing_show
);
1392 static ssize_t
btrfs_devinfo_replace_target_show(struct kobject
*kobj
,
1393 struct kobj_attribute
*a
,
1397 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1400 val
= !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
1402 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
1404 BTRFS_ATTR(devid
, replace_target
, btrfs_devinfo_replace_target_show
);
1406 static ssize_t
btrfs_devinfo_writeable_show(struct kobject
*kobj
,
1407 struct kobj_attribute
*a
, char *buf
)
1410 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1413 val
= !!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1415 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
1417 BTRFS_ATTR(devid
, writeable
, btrfs_devinfo_writeable_show
);
1419 static struct attribute
*devid_attrs
[] = {
1420 BTRFS_ATTR_PTR(devid
, in_fs_metadata
),
1421 BTRFS_ATTR_PTR(devid
, missing
),
1422 BTRFS_ATTR_PTR(devid
, replace_target
),
1423 BTRFS_ATTR_PTR(devid
, writeable
),
1426 ATTRIBUTE_GROUPS(devid
);
1428 static void btrfs_release_devid_kobj(struct kobject
*kobj
)
1430 struct btrfs_device
*device
= container_of(kobj
, struct btrfs_device
,
1433 memset(&device
->devid_kobj
, 0, sizeof(struct kobject
));
1434 complete(&device
->kobj_unregister
);
1437 static struct kobj_type devid_ktype
= {
1438 .sysfs_ops
= &kobj_sysfs_ops
,
1439 .default_groups
= devid_groups
,
1440 .release
= btrfs_release_devid_kobj
,
1443 int btrfs_sysfs_add_device(struct btrfs_device
*device
)
1446 unsigned int nofs_flag
;
1447 struct kobject
*devices_kobj
;
1448 struct kobject
*devinfo_kobj
;
1451 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1452 * for the seed fs_devices
1454 devices_kobj
= device
->fs_info
->fs_devices
->devices_kobj
;
1455 devinfo_kobj
= device
->fs_info
->fs_devices
->devinfo_kobj
;
1456 ASSERT(devices_kobj
);
1457 ASSERT(devinfo_kobj
);
1459 nofs_flag
= memalloc_nofs_save();
1462 struct kobject
*disk_kobj
= bdev_kobj(device
->bdev
);
1464 ret
= sysfs_create_link(devices_kobj
, disk_kobj
, disk_kobj
->name
);
1466 btrfs_warn(device
->fs_info
,
1467 "creating sysfs device link for devid %llu failed: %d",
1468 device
->devid
, ret
);
1473 init_completion(&device
->kobj_unregister
);
1474 ret
= kobject_init_and_add(&device
->devid_kobj
, &devid_ktype
,
1475 devinfo_kobj
, "%llu", device
->devid
);
1477 kobject_put(&device
->devid_kobj
);
1478 btrfs_warn(device
->fs_info
,
1479 "devinfo init for devid %llu failed: %d",
1480 device
->devid
, ret
);
1484 memalloc_nofs_restore(nofs_flag
);
1488 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1491 struct btrfs_device
*device
;
1492 struct btrfs_fs_devices
*seed
;
1494 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1495 ret
= btrfs_sysfs_add_device(device
);
1500 list_for_each_entry(seed
, &fs_devices
->seed_list
, seed_list
) {
1501 list_for_each_entry(device
, &seed
->devices
, dev_list
) {
1502 ret
= btrfs_sysfs_add_device(device
);
1511 btrfs_sysfs_remove_fs_devices(fs_devices
);
1515 void btrfs_kobject_uevent(struct block_device
*bdev
, enum kobject_action action
)
1519 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
1521 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1522 action
, kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
1523 &disk_to_dev(bdev
->bd_disk
)->kobj
);
1526 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices
*fs_devices
)
1529 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
1532 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1535 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU", fs_devices
->fsid
);
1536 if (kobject_rename(&fs_devices
->fsid_kobj
, fsid_buf
))
1537 btrfs_warn(fs_devices
->fs_info
,
1538 "sysfs: failed to create fsid for sprout");
1541 void btrfs_sysfs_update_devid(struct btrfs_device
*device
)
1545 snprintf(tmp
, sizeof(tmp
), "%llu", device
->devid
);
1547 if (kobject_rename(&device
->devid_kobj
, tmp
))
1548 btrfs_warn(device
->fs_devices
->fs_info
,
1549 "sysfs: failed to update devid for %llu",
1553 /* /sys/fs/btrfs/ entry */
1554 static struct kset
*btrfs_kset
;
1558 * /sys/fs/btrfs/UUID
1560 * Can be called by the device discovery thread.
1562 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices
*fs_devs
)
1566 init_completion(&fs_devs
->kobj_unregister
);
1567 fs_devs
->fsid_kobj
.kset
= btrfs_kset
;
1568 error
= kobject_init_and_add(&fs_devs
->fsid_kobj
, &btrfs_ktype
, NULL
,
1569 "%pU", fs_devs
->fsid
);
1571 kobject_put(&fs_devs
->fsid_kobj
);
1575 fs_devs
->devices_kobj
= kobject_create_and_add("devices",
1576 &fs_devs
->fsid_kobj
);
1577 if (!fs_devs
->devices_kobj
) {
1578 btrfs_err(fs_devs
->fs_info
,
1579 "failed to init sysfs device interface");
1580 btrfs_sysfs_remove_fsid(fs_devs
);
1584 fs_devs
->devinfo_kobj
= kobject_create_and_add("devinfo",
1585 &fs_devs
->fsid_kobj
);
1586 if (!fs_devs
->devinfo_kobj
) {
1587 btrfs_err(fs_devs
->fs_info
,
1588 "failed to init sysfs devinfo kobject");
1589 btrfs_sysfs_remove_fsid(fs_devs
);
1596 int btrfs_sysfs_add_mounted(struct btrfs_fs_info
*fs_info
)
1599 struct btrfs_fs_devices
*fs_devs
= fs_info
->fs_devices
;
1600 struct kobject
*fsid_kobj
= &fs_devs
->fsid_kobj
;
1602 error
= btrfs_sysfs_add_fs_devices(fs_devs
);
1606 error
= sysfs_create_files(fsid_kobj
, btrfs_attrs
);
1608 btrfs_sysfs_remove_fs_devices(fs_devs
);
1612 error
= sysfs_create_group(fsid_kobj
,
1613 &btrfs_feature_attr_group
);
1617 #ifdef CONFIG_BTRFS_DEBUG
1618 fs_info
->debug_kobj
= kobject_create_and_add("debug", fsid_kobj
);
1619 if (!fs_info
->debug_kobj
) {
1624 error
= sysfs_create_files(fs_info
->debug_kobj
, btrfs_debug_mount_attrs
);
1628 /* Discard directory */
1629 fs_info
->discard_debug_kobj
= kobject_create_and_add("discard",
1630 fs_info
->debug_kobj
);
1631 if (!fs_info
->discard_debug_kobj
) {
1636 error
= sysfs_create_files(fs_info
->discard_debug_kobj
,
1637 discard_debug_attrs
);
1642 error
= addrm_unknown_feature_attrs(fs_info
, true);
1646 error
= sysfs_create_link(fsid_kobj
, &fs_info
->sb
->s_bdi
->dev
->kobj
, "bdi");
1650 fs_info
->space_info_kobj
= kobject_create_and_add("allocation",
1652 if (!fs_info
->space_info_kobj
) {
1657 error
= sysfs_create_files(fs_info
->space_info_kobj
, allocation_attrs
);
1663 btrfs_sysfs_remove_mounted(fs_info
);
1667 static inline struct btrfs_fs_info
*qgroup_kobj_to_fs_info(struct kobject
*kobj
)
1669 return to_fs_info(kobj
->parent
->parent
);
1672 #define QGROUP_ATTR(_member, _show_name) \
1673 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1674 struct kobj_attribute *a, \
1677 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1678 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1679 struct btrfs_qgroup, kobj); \
1680 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1682 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1684 #define QGROUP_RSV_ATTR(_name, _type) \
1685 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1686 struct kobj_attribute *a, \
1689 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1690 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1691 struct btrfs_qgroup, kobj); \
1692 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1693 &fs_info->qgroup_lock, buf); \
1695 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1697 QGROUP_ATTR(rfer
, referenced
);
1698 QGROUP_ATTR(excl
, exclusive
);
1699 QGROUP_ATTR(max_rfer
, max_referenced
);
1700 QGROUP_ATTR(max_excl
, max_exclusive
);
1701 QGROUP_ATTR(lim_flags
, limit_flags
);
1702 QGROUP_RSV_ATTR(data
, BTRFS_QGROUP_RSV_DATA
);
1703 QGROUP_RSV_ATTR(meta_pertrans
, BTRFS_QGROUP_RSV_META_PERTRANS
);
1704 QGROUP_RSV_ATTR(meta_prealloc
, BTRFS_QGROUP_RSV_META_PREALLOC
);
1706 static struct attribute
*qgroup_attrs
[] = {
1707 BTRFS_ATTR_PTR(qgroup
, referenced
),
1708 BTRFS_ATTR_PTR(qgroup
, exclusive
),
1709 BTRFS_ATTR_PTR(qgroup
, max_referenced
),
1710 BTRFS_ATTR_PTR(qgroup
, max_exclusive
),
1711 BTRFS_ATTR_PTR(qgroup
, limit_flags
),
1712 BTRFS_ATTR_PTR(qgroup
, rsv_data
),
1713 BTRFS_ATTR_PTR(qgroup
, rsv_meta_pertrans
),
1714 BTRFS_ATTR_PTR(qgroup
, rsv_meta_prealloc
),
1717 ATTRIBUTE_GROUPS(qgroup
);
1719 static void qgroup_release(struct kobject
*kobj
)
1721 struct btrfs_qgroup
*qgroup
= container_of(kobj
, struct btrfs_qgroup
, kobj
);
1723 memset(&qgroup
->kobj
, 0, sizeof(*kobj
));
1726 static struct kobj_type qgroup_ktype
= {
1727 .sysfs_ops
= &kobj_sysfs_ops
,
1728 .release
= qgroup_release
,
1729 .default_groups
= qgroup_groups
,
1732 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info
*fs_info
,
1733 struct btrfs_qgroup
*qgroup
)
1735 struct kobject
*qgroups_kobj
= fs_info
->qgroups_kobj
;
1738 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
1740 if (qgroup
->kobj
.state_initialized
)
1745 ret
= kobject_init_and_add(&qgroup
->kobj
, &qgroup_ktype
, qgroups_kobj
,
1746 "%hu_%llu", btrfs_qgroup_level(qgroup
->qgroupid
),
1747 btrfs_qgroup_subvolid(qgroup
->qgroupid
));
1749 kobject_put(&qgroup
->kobj
);
1754 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info
*fs_info
)
1756 struct btrfs_qgroup
*qgroup
;
1757 struct btrfs_qgroup
*next
;
1759 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
1762 rbtree_postorder_for_each_entry_safe(qgroup
, next
,
1763 &fs_info
->qgroup_tree
, node
)
1764 btrfs_sysfs_del_one_qgroup(fs_info
, qgroup
);
1765 if (fs_info
->qgroups_kobj
) {
1766 kobject_del(fs_info
->qgroups_kobj
);
1767 kobject_put(fs_info
->qgroups_kobj
);
1768 fs_info
->qgroups_kobj
= NULL
;
1772 /* Called when qgroups get initialized, thus there is no need for locking */
1773 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info
*fs_info
)
1775 struct kobject
*fsid_kobj
= &fs_info
->fs_devices
->fsid_kobj
;
1776 struct btrfs_qgroup
*qgroup
;
1777 struct btrfs_qgroup
*next
;
1780 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
1784 if (fs_info
->qgroups_kobj
)
1787 fs_info
->qgroups_kobj
= kobject_create_and_add("qgroups", fsid_kobj
);
1788 if (!fs_info
->qgroups_kobj
) {
1792 rbtree_postorder_for_each_entry_safe(qgroup
, next
,
1793 &fs_info
->qgroup_tree
, node
) {
1794 ret
= btrfs_sysfs_add_one_qgroup(fs_info
, qgroup
);
1801 btrfs_sysfs_del_qgroups(fs_info
);
1805 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info
*fs_info
,
1806 struct btrfs_qgroup
*qgroup
)
1808 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO
, &fs_info
->fs_state
))
1811 if (qgroup
->kobj
.state_initialized
) {
1812 kobject_del(&qgroup
->kobj
);
1813 kobject_put(&qgroup
->kobj
);
1818 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1819 * values in superblock. Call after any changes to incompat/compat_ro flags
1821 void btrfs_sysfs_feature_update(struct btrfs_fs_info
*fs_info
,
1822 u64 bit
, enum btrfs_feature_set set
)
1824 struct btrfs_fs_devices
*fs_devs
;
1825 struct kobject
*fsid_kobj
;
1826 u64 __maybe_unused features
;
1827 int __maybe_unused ret
;
1833 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
1834 * safe when called from some contexts (eg. balance)
1836 features
= get_features(fs_info
, set
);
1837 ASSERT(bit
& supported_feature_masks
[set
]);
1839 fs_devs
= fs_info
->fs_devices
;
1840 fsid_kobj
= &fs_devs
->fsid_kobj
;
1842 if (!fsid_kobj
->state_initialized
)
1846 * FIXME: this is too heavy to update just one value, ideally we'd like
1847 * to use sysfs_update_group but some refactoring is needed first.
1849 sysfs_remove_group(fsid_kobj
, &btrfs_feature_attr_group
);
1850 ret
= sysfs_create_group(fsid_kobj
, &btrfs_feature_attr_group
);
1853 int __init
btrfs_init_sysfs(void)
1857 btrfs_kset
= kset_create_and_add("btrfs", NULL
, fs_kobj
);
1861 init_feature_attrs();
1862 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1865 ret
= sysfs_merge_group(&btrfs_kset
->kobj
,
1866 &btrfs_static_feature_attr_group
);
1868 goto out_remove_group
;
1870 #ifdef CONFIG_BTRFS_DEBUG
1871 ret
= sysfs_create_group(&btrfs_kset
->kobj
, &btrfs_debug_feature_attr_group
);
1879 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1881 kset_unregister(btrfs_kset
);
1886 void __cold
btrfs_exit_sysfs(void)
1888 sysfs_unmerge_group(&btrfs_kset
->kobj
,
1889 &btrfs_static_feature_attr_group
);
1890 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_feature_attr_group
);
1891 #ifdef CONFIG_BTRFS_DEBUG
1892 sysfs_remove_group(&btrfs_kset
->kobj
, &btrfs_debug_feature_attr_group
);
1894 kset_unregister(btrfs_kset
);