projects/linux·blob master · refs

2582 lines · 68.1 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
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 <linux/string_choices.h>
14#include "messages.h"
15#include "ctree.h"
16#include "discard.h"
17#include "disk-io.h"
18#include "send.h"
19#include "transaction.h"
20#include "sysfs.h"
21#include "volumes.h"
22#include "space-info.h"
23#include "block-group.h"
24#include "qgroup.h"
25#include "misc.h"
26#include "fs.h"
27#include "accessors.h"
28
29/*
30 * Structure name Path
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
42 *
43 * When built with BTRFS_CONFIG_DEBUG:
44 *
45 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
46 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
47 */
48
49struct btrfs_feature_attr {
50 struct kobj_attribute kobj_attr;
51 enum btrfs_feature_set feature_set;
52 u64 feature_bit;
53};
54
55/* For raid type sysfs entries */
56struct raid_kobject {
57 u64 flags;
58 struct kobject kobj;
59};
60
61#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
62{ \
63 .attr = { .name = __stringify(_name), .mode = _mode }, \
64 .show = _show, \
65 .store = _store, \
66}
67
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)
71
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)
75
76#define BTRFS_ATTR(_prefix, _name, _show) \
77 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
78 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
79
80#define BTRFS_ATTR_PTR(_prefix, _name) \
81 (&btrfs_attr_##_prefix##_##_name.attr)
82
83#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
84static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
85 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
86 btrfs_feature_attr_show, NULL), \
87 .feature_set = _feature_set, \
88 .feature_bit = _feature_prefix ##_## _feature_bit, \
89}
90#define BTRFS_FEAT_ATTR_PTR(_name) \
91 (&btrfs_attr_features_##_name.kobj_attr.attr)
92
93#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
94 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
95#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
96 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
97#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
98 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
99
100static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
101static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
102static struct kobject *get_btrfs_kobj(struct kobject *kobj);
103
104static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
105{
106 return container_of(a, struct btrfs_feature_attr, kobj_attr);
107}
108
109static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
110{
111 return container_of(attr, struct kobj_attribute, attr);
112}
113
114static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
115 struct attribute *attr)
116{
117 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
118}
119
120static u64 get_features(struct btrfs_fs_info *fs_info,
121 enum btrfs_feature_set set)
122{
123 struct btrfs_super_block *disk_super = fs_info->super_copy;
124 if (set == FEAT_COMPAT)
125 return btrfs_super_compat_flags(disk_super);
126 else if (set == FEAT_COMPAT_RO)
127 return btrfs_super_compat_ro_flags(disk_super);
128 else
129 return btrfs_super_incompat_flags(disk_super);
130}
131
132static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
133 struct kobj_attribute *a, char *buf)
134{
135 int val = 0;
136 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
137 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
138
139 if (fs_info) {
140 u64 features = get_features(fs_info, fa->feature_set);
141 if (features & fa->feature_bit)
142 val = 1;
143 }
144
145 return sysfs_emit(buf, "%d\n", val);
146}
147
148static umode_t btrfs_feature_visible(struct kobject *kobj,
149 struct attribute *attr, int unused)
150{
151 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
152 umode_t mode = attr->mode;
153
154 if (fs_info) {
155 struct btrfs_feature_attr *fa;
156 u64 features;
157
158 fa = attr_to_btrfs_feature_attr(attr);
159 features = get_features(fs_info, fa->feature_set);
160
161 if (!(features & fa->feature_bit))
162 mode = 0;
163 }
164
165 return mode;
166}
167
168BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
169BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
170BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
171BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
172BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
173BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
174BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
175BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
176BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
177BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
178BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
179BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
180BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA);
181#ifdef CONFIG_BLK_DEV_ZONED
182BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
183#endif
184#ifdef CONFIG_BTRFS_EXPERIMENTAL
185/* Remove once support for extent tree v2 is feature complete */
186BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
187/* Remove once support for raid stripe tree is feature complete. */
188BTRFS_FEAT_ATTR_INCOMPAT(raid_stripe_tree, RAID_STRIPE_TREE);
189/* Remove once support for remap tree is feature complete. */
190BTRFS_FEAT_ATTR_INCOMPAT(remap_tree, REMAP_TREE);
191#endif
192#ifdef CONFIG_FS_VERITY
193BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
194#endif
195
196/*
197 * Features which depend on feature bits and may differ between each fs.
198 *
199 * /sys/fs/btrfs/features - all available features implemented by this version
200 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
201 * can be changed on a mounted filesystem.
202 */
203static struct attribute *btrfs_supported_feature_attrs[] = {
204 BTRFS_FEAT_ATTR_PTR(default_subvol),
205 BTRFS_FEAT_ATTR_PTR(mixed_groups),
206 BTRFS_FEAT_ATTR_PTR(compress_lzo),
207 BTRFS_FEAT_ATTR_PTR(compress_zstd),
208 BTRFS_FEAT_ATTR_PTR(extended_iref),
209 BTRFS_FEAT_ATTR_PTR(raid56),
210 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
211 BTRFS_FEAT_ATTR_PTR(no_holes),
212 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
213 BTRFS_FEAT_ATTR_PTR(free_space_tree),
214 BTRFS_FEAT_ATTR_PTR(raid1c34),
215 BTRFS_FEAT_ATTR_PTR(block_group_tree),
216 BTRFS_FEAT_ATTR_PTR(simple_quota),
217#ifdef CONFIG_BLK_DEV_ZONED
218 BTRFS_FEAT_ATTR_PTR(zoned),
219#endif
220#ifdef CONFIG_BTRFS_EXPERIMENTAL
221 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
222 BTRFS_FEAT_ATTR_PTR(raid_stripe_tree),
223 BTRFS_FEAT_ATTR_PTR(remap_tree),
224#endif
225#ifdef CONFIG_FS_VERITY
226 BTRFS_FEAT_ATTR_PTR(verity),
227#endif
228 NULL
229};
230
231static const struct attribute_group btrfs_feature_attr_group = {
232 .name = "features",
233 .is_visible = btrfs_feature_visible,
234 .attrs = btrfs_supported_feature_attrs,
235};
236
237static ssize_t rmdir_subvol_show(struct kobject *kobj,
238 struct kobj_attribute *ka, char *buf)
239{
240 return sysfs_emit(buf, "0\n");
241}
242BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
243
244static ssize_t supported_checksums_show(struct kobject *kobj,
245 struct kobj_attribute *a, char *buf)
246{
247 ssize_t ret = 0;
248 int i;
249
250 for (i = 0; i < btrfs_get_num_csums(); i++) {
251 /*
252 * This "trick" only works as long as 'enum btrfs_csum_type' has
253 * no holes in it
254 */
255 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
256 btrfs_super_csum_name(i));
257
258 }
259
260 ret += sysfs_emit_at(buf, ret, "\n");
261 return ret;
262}
263BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
264
265static ssize_t send_stream_version_show(struct kobject *kobj,
266 struct kobj_attribute *ka, char *buf)
267{
268 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
269}
270BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
271
272static const char *rescue_opts[] = {
273 "usebackuproot",
274 "nologreplay",
275 "ignorebadroots",
276 "ignoredatacsums",
277 "ignoremetacsums",
278 "ignoresuperflags",
279 "all",
280};
281
282static ssize_t supported_rescue_options_show(struct kobject *kobj,
283 struct kobj_attribute *a,
284 char *buf)
285{
286 ssize_t ret = 0;
287 int i;
288
289 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
290 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
291 ret += sysfs_emit_at(buf, ret, "\n");
292 return ret;
293}
294BTRFS_ATTR(static_feature, supported_rescue_options,
295 supported_rescue_options_show);
296
297static ssize_t supported_sectorsizes_show(struct kobject *kobj,
298 struct kobj_attribute *a,
299 char *buf)
300{
301 ssize_t ret = 0;
302 bool has_output = false;
303
304 for (u32 cur = BTRFS_MIN_BLOCKSIZE; cur <= BTRFS_MAX_BLOCKSIZE; cur *= 2) {
305 if (!btrfs_supported_blocksize(cur))
306 continue;
307 if (has_output)
308 ret += sysfs_emit_at(buf, ret, " ");
309 ret += sysfs_emit_at(buf, ret, "%u", cur);
310 has_output = true;
311 }
312 ret += sysfs_emit_at(buf, ret, "\n");
313 return ret;
314}
315BTRFS_ATTR(static_feature, supported_sectorsizes,
316 supported_sectorsizes_show);
317
318static ssize_t acl_show(struct kobject *kobj, struct kobj_attribute *a, char *buf)
319{
320 return sysfs_emit(buf, "%d\n", IS_ENABLED(CONFIG_BTRFS_FS_POSIX_ACL));
321}
322BTRFS_ATTR(static_feature, acl, acl_show);
323
324static ssize_t temp_fsid_supported_show(struct kobject *kobj,
325 struct kobj_attribute *a, char *buf)
326{
327 return sysfs_emit(buf, "0\n");
328}
329BTRFS_ATTR(static_feature, temp_fsid, temp_fsid_supported_show);
330
331/*
332 * Features which only depend on kernel version.
333 *
334 * These are listed in /sys/fs/btrfs/features along with
335 * btrfs_supported_feature_attrs.
336 */
337static struct attribute *btrfs_supported_static_feature_attrs[] = {
338 BTRFS_ATTR_PTR(static_feature, acl),
339 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
340 BTRFS_ATTR_PTR(static_feature, supported_checksums),
341 BTRFS_ATTR_PTR(static_feature, send_stream_version),
342 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
343 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
344 BTRFS_ATTR_PTR(static_feature, temp_fsid),
345 NULL
346};
347
348static const struct attribute_group btrfs_static_feature_attr_group = {
349 .name = "features",
350 .attrs = btrfs_supported_static_feature_attrs,
351};
352
353/*
354 * Discard statistics and tunables
355 */
356#define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj))
357
358static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
359 struct kobj_attribute *a,
360 char *buf)
361{
362 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
363
364 return sysfs_emit(buf, "%lld\n",
365 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
366}
367BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
368
369static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
370 struct kobj_attribute *a,
371 char *buf)
372{
373 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
374
375 return sysfs_emit(buf, "%d\n",
376 atomic_read(&fs_info->discard_ctl.discardable_extents));
377}
378BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
379
380static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
381 struct kobj_attribute *a,
382 char *buf)
383{
384 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
385
386 return sysfs_emit(buf, "%llu\n",
387 fs_info->discard_ctl.discard_bitmap_bytes);
388}
389BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
390
391static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
392 struct kobj_attribute *a,
393 char *buf)
394{
395 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
396
397 return sysfs_emit(buf, "%lld\n",
398 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
399}
400BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
401
402static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
403 struct kobj_attribute *a,
404 char *buf)
405{
406 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
407
408 return sysfs_emit(buf, "%llu\n",
409 fs_info->discard_ctl.discard_extent_bytes);
410}
411BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
412
413static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
414 struct kobj_attribute *a,
415 char *buf)
416{
417 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
418
419 return sysfs_emit(buf, "%u\n",
420 READ_ONCE(fs_info->discard_ctl.iops_limit));
421}
422
423static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
424 struct kobj_attribute *a,
425 const char *buf, size_t len)
426{
427 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
428 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
429 u32 iops_limit;
430 int ret;
431
432 ret = kstrtou32(buf, 10, &iops_limit);
433 if (ret)
434 return -EINVAL;
435
436 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
437 btrfs_discard_calc_delay(discard_ctl);
438 btrfs_discard_schedule_work(discard_ctl, true);
439 return len;
440}
441BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
442 btrfs_discard_iops_limit_store);
443
444static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
445 struct kobj_attribute *a,
446 char *buf)
447{
448 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
449
450 return sysfs_emit(buf, "%u\n",
451 READ_ONCE(fs_info->discard_ctl.kbps_limit));
452}
453
454static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
455 struct kobj_attribute *a,
456 const char *buf, size_t len)
457{
458 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
459 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
460 u32 kbps_limit;
461 int ret;
462
463 ret = kstrtou32(buf, 10, &kbps_limit);
464 if (ret)
465 return -EINVAL;
466
467 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
468 btrfs_discard_schedule_work(discard_ctl, true);
469 return len;
470}
471BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
472 btrfs_discard_kbps_limit_store);
473
474static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
475 struct kobj_attribute *a,
476 char *buf)
477{
478 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
479
480 return sysfs_emit(buf, "%llu\n",
481 READ_ONCE(fs_info->discard_ctl.max_discard_size));
482}
483
484static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
485 struct kobj_attribute *a,
486 const char *buf, size_t len)
487{
488 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
489 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
490 u64 max_discard_size;
491 int ret;
492
493 ret = kstrtou64(buf, 10, &max_discard_size);
494 if (ret)
495 return -EINVAL;
496
497 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
498
499 return len;
500}
501BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
502 btrfs_discard_max_discard_size_store);
503
504/*
505 * Per-filesystem stats for discard (when mounted with discard=async).
506 *
507 * Path: /sys/fs/btrfs/<uuid>/discard/
508 */
509static const struct attribute *discard_attrs[] = {
510 BTRFS_ATTR_PTR(discard, discardable_bytes),
511 BTRFS_ATTR_PTR(discard, discardable_extents),
512 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
513 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
514 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
515 BTRFS_ATTR_PTR(discard, iops_limit),
516 BTRFS_ATTR_PTR(discard, kbps_limit),
517 BTRFS_ATTR_PTR(discard, max_discard_size),
518 NULL,
519};
520
521#ifdef CONFIG_BTRFS_DEBUG
522
523/*
524 * Per-filesystem runtime debugging exported via sysfs.
525 *
526 * Path: /sys/fs/btrfs/UUID/debug/
527 */
528static const struct attribute *btrfs_debug_mount_attrs[] = {
529 NULL,
530};
531
532/*
533 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
534 *
535 * Path: /sys/fs/btrfs/debug
536 */
537static struct attribute *btrfs_debug_feature_attrs[] = {
538 NULL
539};
540
541static const struct attribute_group btrfs_debug_feature_attr_group = {
542 .name = "debug",
543 .attrs = btrfs_debug_feature_attrs,
544};
545
546#endif
547
548static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
549{
550 u64 val;
551 if (lock)
552 spin_lock(lock);
553 val = *value_ptr;
554 if (lock)
555 spin_unlock(lock);
556 return sysfs_emit(buf, "%llu\n", val);
557}
558
559static ssize_t global_rsv_size_show(struct kobject *kobj,
560 struct kobj_attribute *ka, char *buf)
561{
562 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
563 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
564 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
565}
566BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
567
568static ssize_t global_rsv_reserved_show(struct kobject *kobj,
569 struct kobj_attribute *a, char *buf)
570{
571 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
572 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
573 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
574}
575BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
576
577#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
578#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
579
580static ssize_t raid_bytes_show(struct kobject *kobj,
581 struct kobj_attribute *attr, char *buf);
582BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
583BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
584
585static ssize_t raid_bytes_show(struct kobject *kobj,
586 struct kobj_attribute *attr, char *buf)
587
588{
589 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
590 struct btrfs_block_group *block_group;
591 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
592 u64 val = 0;
593
594 down_read(&sinfo->groups_sem);
595 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
596 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
597 val += block_group->length;
598 else
599 val += block_group->used;
600 }
601 up_read(&sinfo->groups_sem);
602 return sysfs_emit(buf, "%llu\n", val);
603}
604
605/*
606 * Allocation information about block group profiles.
607 *
608 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
609 */
610static struct attribute *raid_attrs[] = {
611 BTRFS_ATTR_PTR(raid, total_bytes),
612 BTRFS_ATTR_PTR(raid, used_bytes),
613 NULL
614};
615ATTRIBUTE_GROUPS(raid);
616
617static void release_raid_kobj(struct kobject *kobj)
618{
619 kfree(to_raid_kobj(kobj));
620}
621
622static const struct kobj_type btrfs_raid_ktype = {
623 .sysfs_ops = &kobj_sysfs_ops,
624 .release = release_raid_kobj,
625 .default_groups = raid_groups,
626};
627
628#define SPACE_INFO_ATTR(field) \
629static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
630 struct kobj_attribute *a, \
631 char *buf) \
632{ \
633 struct btrfs_space_info *sinfo = to_space_info(kobj); \
634 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
635} \
636BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
637
638static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
639 struct kobj_attribute *a, char *buf)
640{
641 struct btrfs_space_info *sinfo = to_space_info(kobj);
642
643 return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
644}
645
646/*
647 * Store new chunk size in space info. Can be called on a read-only filesystem.
648 *
649 * If the new chunk size value is larger than 10% of free space it is reduced
650 * to match that limit. Alignment must be to 256M and the system chunk size
651 * cannot be set.
652 */
653static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
654 struct kobj_attribute *a,
655 const char *buf, size_t len)
656{
657 struct btrfs_space_info *space_info = to_space_info(kobj);
658 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
659 char *retptr;
660 u64 val;
661
662 if (!capable(CAP_SYS_ADMIN))
663 return -EPERM;
664
665 if (!fs_info->fs_devices)
666 return -EINVAL;
667
668 if (btrfs_is_zoned(fs_info))
669 return -EINVAL;
670
671 /* System block type must not be changed. */
672 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
673 return -EPERM;
674
675 val = memparse(buf, &retptr);
676 /* There could be trailing '\n', also catch any typos after the value */
677 retptr = skip_spaces(retptr);
678 if (*retptr != 0 || val == 0)
679 return -EINVAL;
680
681 val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
682
683 /* Limit stripe size to 10% of available space. */
684 val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val);
685
686 /* Must be multiple of 256M. */
687 val &= ~((u64)SZ_256M - 1);
688
689 /* Must be at least 256M. */
690 if (val < SZ_256M)
691 return -EINVAL;
692
693 btrfs_update_space_info_chunk_size(space_info, val);
694
695 return len;
696}
697
698static ssize_t btrfs_size_classes_show(struct kobject *kobj,
699 struct kobj_attribute *a, char *buf)
700{
701 struct btrfs_space_info *sinfo = to_space_info(kobj);
702 struct btrfs_block_group *bg;
703 u32 none = 0;
704 u32 small = 0;
705 u32 medium = 0;
706 u32 large = 0;
707
708 for (int i = 0; i < BTRFS_NR_RAID_TYPES; ++i) {
709 down_read(&sinfo->groups_sem);
710 list_for_each_entry(bg, &sinfo->block_groups[i], list) {
711 if (!btrfs_block_group_should_use_size_class(bg))
712 continue;
713 switch (bg->size_class) {
714 case BTRFS_BG_SZ_NONE:
715 none++;
716 break;
717 case BTRFS_BG_SZ_SMALL:
718 small++;
719 break;
720 case BTRFS_BG_SZ_MEDIUM:
721 medium++;
722 break;
723 case BTRFS_BG_SZ_LARGE:
724 large++;
725 break;
726 }
727 }
728 up_read(&sinfo->groups_sem);
729 }
730 return sysfs_emit(buf, "none %u\n"
731 "small %u\n"
732 "medium %u\n"
733 "large %u\n",
734 none, small, medium, large);
735}
736
737#ifdef CONFIG_BTRFS_DEBUG
738/*
739 * Request chunk allocation with current chunk size.
740 */
741static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
742 struct kobj_attribute *a,
743 const char *buf, size_t len)
744{
745 struct btrfs_space_info *space_info = to_space_info(kobj);
746 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
747 struct btrfs_trans_handle *trans;
748 bool val;
749 int ret;
750
751 if (!capable(CAP_SYS_ADMIN))
752 return -EPERM;
753
754 if (sb_rdonly(fs_info->sb))
755 return -EROFS;
756
757 ret = kstrtobool(buf, &val);
758 if (ret)
759 return ret;
760
761 if (!val)
762 return -EINVAL;
763
764 /*
765 * This is unsafe to be called from sysfs context and may cause
766 * unexpected problems.
767 */
768 trans = btrfs_start_transaction(fs_info->tree_root, 0);
769 if (IS_ERR(trans))
770 return PTR_ERR(trans);
771 ret = btrfs_force_chunk_alloc(trans, space_info->flags);
772 btrfs_end_transaction(trans);
773
774 if (ret == 1)
775 return len;
776
777 return -ENOSPC;
778}
779BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
780
781#endif
782
783SPACE_INFO_ATTR(flags);
784SPACE_INFO_ATTR(total_bytes);
785SPACE_INFO_ATTR(bytes_used);
786SPACE_INFO_ATTR(bytes_pinned);
787SPACE_INFO_ATTR(bytes_reserved);
788SPACE_INFO_ATTR(bytes_may_use);
789SPACE_INFO_ATTR(bytes_readonly);
790SPACE_INFO_ATTR(bytes_zone_unusable);
791SPACE_INFO_ATTR(disk_used);
792SPACE_INFO_ATTR(disk_total);
793SPACE_INFO_ATTR(reclaim_count);
794SPACE_INFO_ATTR(reclaim_bytes);
795SPACE_INFO_ATTR(reclaim_errors);
796BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
797BTRFS_ATTR(space_info, size_classes, btrfs_size_classes_show);
798
799static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
800 struct kobj_attribute *a,
801 char *buf)
802{
803 struct btrfs_space_info *space_info = to_space_info(kobj);
804 ssize_t ret;
805
806 spin_lock(&space_info->lock);
807 ret = sysfs_emit(buf, "%d\n", btrfs_calc_reclaim_threshold(space_info));
808 spin_unlock(&space_info->lock);
809 return ret;
810}
811
812static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
813 struct kobj_attribute *a,
814 const char *buf, size_t len)
815{
816 struct btrfs_space_info *space_info = to_space_info(kobj);
817 int thresh;
818 int ret;
819
820 if (READ_ONCE(space_info->dynamic_reclaim))
821 return -EINVAL;
822
823 ret = kstrtoint(buf, 10, &thresh);
824 if (ret)
825 return ret;
826
827 if (thresh < 0 || thresh > 100)
828 return -EINVAL;
829
830 WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
831
832 return len;
833}
834
835BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
836 btrfs_sinfo_bg_reclaim_threshold_show,
837 btrfs_sinfo_bg_reclaim_threshold_store);
838
839static ssize_t btrfs_sinfo_dynamic_reclaim_show(struct kobject *kobj,
840 struct kobj_attribute *a,
841 char *buf)
842{
843 struct btrfs_space_info *space_info = to_space_info(kobj);
844
845 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->dynamic_reclaim));
846}
847
848static ssize_t btrfs_sinfo_dynamic_reclaim_store(struct kobject *kobj,
849 struct kobj_attribute *a,
850 const char *buf, size_t len)
851{
852 struct btrfs_space_info *space_info = to_space_info(kobj);
853 int dynamic_reclaim;
854 int ret;
855
856 ret = kstrtoint(buf, 10, &dynamic_reclaim);
857 if (ret)
858 return ret;
859
860 if (dynamic_reclaim < 0)
861 return -EINVAL;
862
863 WRITE_ONCE(space_info->dynamic_reclaim, dynamic_reclaim != 0);
864
865 return len;
866}
867
868BTRFS_ATTR_RW(space_info, dynamic_reclaim,
869 btrfs_sinfo_dynamic_reclaim_show,
870 btrfs_sinfo_dynamic_reclaim_store);
871
872static ssize_t btrfs_sinfo_periodic_reclaim_show(struct kobject *kobj,
873 struct kobj_attribute *a,
874 char *buf)
875{
876 struct btrfs_space_info *space_info = to_space_info(kobj);
877
878 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->periodic_reclaim));
879}
880
881static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj,
882 struct kobj_attribute *a,
883 const char *buf, size_t len)
884{
885 struct btrfs_space_info *space_info = to_space_info(kobj);
886 int periodic_reclaim;
887 int ret;
888
889 ret = kstrtoint(buf, 10, &periodic_reclaim);
890 if (ret)
891 return ret;
892
893 if (periodic_reclaim < 0)
894 return -EINVAL;
895
896 WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0);
897
898 return len;
899}
900
901BTRFS_ATTR_RW(space_info, periodic_reclaim,
902 btrfs_sinfo_periodic_reclaim_show,
903 btrfs_sinfo_periodic_reclaim_store);
904
905/*
906 * Allocation information about block group types.
907 *
908 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
909 */
910static struct attribute *space_info_attrs[] = {
911 BTRFS_ATTR_PTR(space_info, flags),
912 BTRFS_ATTR_PTR(space_info, total_bytes),
913 BTRFS_ATTR_PTR(space_info, bytes_used),
914 BTRFS_ATTR_PTR(space_info, bytes_pinned),
915 BTRFS_ATTR_PTR(space_info, bytes_reserved),
916 BTRFS_ATTR_PTR(space_info, bytes_may_use),
917 BTRFS_ATTR_PTR(space_info, bytes_readonly),
918 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
919 BTRFS_ATTR_PTR(space_info, disk_used),
920 BTRFS_ATTR_PTR(space_info, disk_total),
921 BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
922 BTRFS_ATTR_PTR(space_info, dynamic_reclaim),
923 BTRFS_ATTR_PTR(space_info, chunk_size),
924 BTRFS_ATTR_PTR(space_info, size_classes),
925 BTRFS_ATTR_PTR(space_info, reclaim_count),
926 BTRFS_ATTR_PTR(space_info, reclaim_bytes),
927 BTRFS_ATTR_PTR(space_info, reclaim_errors),
928 BTRFS_ATTR_PTR(space_info, periodic_reclaim),
929#ifdef CONFIG_BTRFS_DEBUG
930 BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
931#endif
932 NULL,
933};
934ATTRIBUTE_GROUPS(space_info);
935
936static void space_info_release(struct kobject *kobj)
937{
938 struct btrfs_space_info *sinfo = to_space_info(kobj);
939 kfree(sinfo);
940}
941
942static const struct kobj_type space_info_ktype = {
943 .sysfs_ops = &kobj_sysfs_ops,
944 .release = space_info_release,
945 .default_groups = space_info_groups,
946};
947
948/*
949 * Allocation information about block groups.
950 *
951 * Path: /sys/fs/btrfs/<uuid>/allocation/
952 */
953static const struct attribute *allocation_attrs[] = {
954 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
955 BTRFS_ATTR_PTR(allocation, global_rsv_size),
956 NULL,
957};
958
959static ssize_t btrfs_label_show(struct kobject *kobj,
960 struct kobj_attribute *a, char *buf)
961{
962 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
963 char *label = fs_info->super_copy->label;
964 ssize_t ret;
965
966 spin_lock(&fs_info->super_lock);
967 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
968 spin_unlock(&fs_info->super_lock);
969
970 return ret;
971}
972
973static ssize_t btrfs_label_store(struct kobject *kobj,
974 struct kobj_attribute *a,
975 const char *buf, size_t len)
976{
977 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
978 size_t p_len;
979
980 if (!fs_info)
981 return -EPERM;
982
983 if (sb_rdonly(fs_info->sb))
984 return -EROFS;
985
986 /*
987 * p_len is the len until the first occurrence of either
988 * '\n' or '\0'
989 */
990 p_len = strcspn(buf, "\n");
991
992 if (p_len >= BTRFS_LABEL_SIZE)
993 return -EINVAL;
994
995 spin_lock(&fs_info->super_lock);
996 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
997 memcpy(fs_info->super_copy->label, buf, p_len);
998 spin_unlock(&fs_info->super_lock);
999
1000 /*
1001 * We don't want to do full transaction commit from inside sysfs
1002 */
1003 set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
1004 wake_up_process(fs_info->transaction_kthread);
1005
1006 return len;
1007}
1008BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
1009
1010static ssize_t btrfs_nodesize_show(struct kobject *kobj,
1011 struct kobj_attribute *a, char *buf)
1012{
1013 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1014
1015 return sysfs_emit(buf, "%u\n", fs_info->nodesize);
1016}
1017
1018BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
1019
1020static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
1021 struct kobj_attribute *a, char *buf)
1022{
1023 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1024
1025 return sysfs_emit(buf, "%u\n", fs_info->sectorsize);
1026}
1027
1028BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
1029
1030static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
1031 struct kobj_attribute *a, char *buf)
1032{
1033 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1034 u64 now = ktime_get_ns();
1035 u64 start_time = fs_info->commit_stats.critical_section_start_time;
1036 u64 pending = 0;
1037
1038 if (start_time)
1039 pending = now - start_time;
1040
1041 return sysfs_emit(buf,
1042 "commits %llu\n"
1043 "cur_commit_ms %llu\n"
1044 "last_commit_ms %llu\n"
1045 "max_commit_ms %llu\n"
1046 "total_commit_ms %llu\n",
1047 fs_info->commit_stats.commit_count,
1048 div_u64(pending, NSEC_PER_MSEC),
1049 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1050 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1051 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1052}
1053
1054static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1055 struct kobj_attribute *a,
1056 const char *buf, size_t len)
1057{
1058 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1059 unsigned long val;
1060 int ret;
1061
1062 if (!fs_info)
1063 return -EPERM;
1064
1065 if (!capable(CAP_SYS_RESOURCE))
1066 return -EPERM;
1067
1068 ret = kstrtoul(buf, 10, &val);
1069 if (ret)
1070 return ret;
1071 if (val)
1072 return -EINVAL;
1073
1074 WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1075
1076 return len;
1077}
1078BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1079
1080static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1081 struct kobj_attribute *a, char *buf)
1082{
1083 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1084
1085 return sysfs_emit(buf, "%u\n", fs_info->sectorsize);
1086}
1087
1088BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1089
1090static ssize_t quota_override_show(struct kobject *kobj,
1091 struct kobj_attribute *a, char *buf)
1092{
1093 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1094 int quota_override;
1095
1096 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1097 return sysfs_emit(buf, "%d\n", quota_override);
1098}
1099
1100static ssize_t quota_override_store(struct kobject *kobj,
1101 struct kobj_attribute *a,
1102 const char *buf, size_t len)
1103{
1104 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1105 unsigned long knob;
1106 int ret;
1107
1108 if (!fs_info)
1109 return -EPERM;
1110
1111 if (!capable(CAP_SYS_RESOURCE))
1112 return -EPERM;
1113
1114 ret = kstrtoul(buf, 10, &knob);
1115 if (ret)
1116 return ret;
1117 if (knob > 1)
1118 return -EINVAL;
1119
1120 assign_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags, knob);
1121
1122 return len;
1123}
1124
1125BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1126
1127static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1128 struct kobj_attribute *a, char *buf)
1129{
1130 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1131
1132 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1133}
1134
1135BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1136
1137static ssize_t btrfs_checksum_show(struct kobject *kobj,
1138 struct kobj_attribute *a, char *buf)
1139{
1140 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1141 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1142 const char *csum_name = btrfs_super_csum_name(csum_type);
1143
1144 return sysfs_emit(buf, "%s (%s-lib)\n", csum_name, csum_name);
1145}
1146
1147BTRFS_ATTR(, checksum, btrfs_checksum_show);
1148
1149static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1150 struct kobj_attribute *a, char *buf)
1151{
1152 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1153 const char *str;
1154
1155 switch (READ_ONCE(fs_info->exclusive_operation)) {
1156 case BTRFS_EXCLOP_NONE:
1157 str = "none\n";
1158 break;
1159 case BTRFS_EXCLOP_BALANCE:
1160 str = "balance\n";
1161 break;
1162 case BTRFS_EXCLOP_BALANCE_PAUSED:
1163 str = "balance paused\n";
1164 break;
1165 case BTRFS_EXCLOP_DEV_ADD:
1166 str = "device add\n";
1167 break;
1168 case BTRFS_EXCLOP_DEV_REMOVE:
1169 str = "device remove\n";
1170 break;
1171 case BTRFS_EXCLOP_DEV_REPLACE:
1172 str = "device replace\n";
1173 break;
1174 case BTRFS_EXCLOP_RESIZE:
1175 str = "resize\n";
1176 break;
1177 case BTRFS_EXCLOP_SWAP_ACTIVATE:
1178 str = "swap activate\n";
1179 break;
1180 default:
1181 str = "UNKNOWN\n";
1182 break;
1183 }
1184 return sysfs_emit(buf, "%s", str);
1185}
1186BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1187
1188static ssize_t btrfs_generation_show(struct kobject *kobj,
1189 struct kobj_attribute *a, char *buf)
1190{
1191 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1192
1193 return sysfs_emit(buf, "%llu\n", btrfs_get_fs_generation(fs_info));
1194}
1195BTRFS_ATTR(, generation, btrfs_generation_show);
1196
1197static ssize_t btrfs_temp_fsid_show(struct kobject *kobj,
1198 struct kobj_attribute *a, char *buf)
1199{
1200 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1201
1202 return sysfs_emit(buf, "%d\n", fs_info->fs_devices->temp_fsid);
1203}
1204BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show);
1205
1206static const char *btrfs_read_policy_name[] = {
1207 "pid",
1208#ifdef CONFIG_BTRFS_EXPERIMENTAL
1209 "round-robin",
1210 "devid",
1211#endif
1212};
1213
1214#ifdef CONFIG_BTRFS_EXPERIMENTAL
1215
1216/* Global module configuration parameters. */
1217static char *read_policy;
1218char *btrfs_get_mod_read_policy(void)
1219{
1220 return read_policy;
1221}
1222
1223/* Set perms to 0, disable /sys/module/btrfs/parameters/read_policy interface. */
1224module_param(read_policy, charp, 0);
1225MODULE_PARM_DESC(read_policy,
1226"Global read policy: pid (default), round-robin[:<min_contig_read>], devid[:<devid>]");
1227#endif
1228
1229int btrfs_read_policy_to_enum(const char *str, s64 *value_ret)
1230{
1231 char param[32];
1232 char __maybe_unused *value_str;
1233
1234 if (!str || strlen(str) == 0)
1235 return 0;
1236
1237 strscpy(param, str);
1238
1239#ifdef CONFIG_BTRFS_EXPERIMENTAL
1240 /* Separate value from input in policy:value format. */
1241 value_str = strchr(param, ':');
1242 if (value_str) {
1243 char *retptr;
1244
1245 *value_str = 0;
1246 value_str++;
1247 if (!value_ret)
1248 return -EINVAL;
1249
1250 *value_ret = memparse(value_str, &retptr);
1251 /* There could be any trailing typos after the value. */
1252 retptr = skip_spaces(retptr);
1253 if (*retptr != 0 || *value_ret <= 0)
1254 return -EINVAL;
1255 }
1256#endif
1257
1258 return sysfs_match_string(btrfs_read_policy_name, param);
1259}
1260
1261#ifdef CONFIG_BTRFS_EXPERIMENTAL
1262int __init btrfs_read_policy_init(void)
1263{
1264 s64 value;
1265
1266 if (btrfs_read_policy_to_enum(read_policy, &value) == -EINVAL) {
1267 btrfs_err(NULL, "invalid read policy or value %s", read_policy);
1268 return -EINVAL;
1269 }
1270
1271 return 0;
1272}
1273#endif
1274
1275static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1276 struct kobj_attribute *a, char *buf)
1277{
1278 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1279 const enum btrfs_read_policy policy = READ_ONCE(fs_devices->read_policy);
1280 ssize_t ret = 0;
1281 int i;
1282
1283 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1284 if (ret != 0)
1285 ret += sysfs_emit_at(buf, ret, " ");
1286
1287 if (i == policy)
1288 ret += sysfs_emit_at(buf, ret, "[");
1289
1290 ret += sysfs_emit_at(buf, ret, "%s", btrfs_read_policy_name[i]);
1291
1292#ifdef CONFIG_BTRFS_EXPERIMENTAL
1293 if (i == BTRFS_READ_POLICY_RR)
1294 ret += sysfs_emit_at(buf, ret, ":%u",
1295 READ_ONCE(fs_devices->rr_min_contig_read));
1296
1297 if (i == BTRFS_READ_POLICY_DEVID)
1298 ret += sysfs_emit_at(buf, ret, ":%llu",
1299 READ_ONCE(fs_devices->read_devid));
1300#endif
1301 if (i == policy)
1302 ret += sysfs_emit_at(buf, ret, "]");
1303 }
1304
1305 ret += sysfs_emit_at(buf, ret, "\n");
1306
1307 return ret;
1308}
1309
1310static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1311 struct kobj_attribute *a,
1312 const char *buf, size_t len)
1313{
1314 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1315 int index;
1316 s64 value = -1;
1317
1318 index = btrfs_read_policy_to_enum(buf, &value);
1319 if (index < 0)
1320 return -EINVAL;
1321
1322#ifdef CONFIG_BTRFS_EXPERIMENTAL
1323 /* If moving from RR then disable collecting fs stats. */
1324 if (fs_devices->read_policy == BTRFS_READ_POLICY_RR && index != BTRFS_READ_POLICY_RR)
1325 fs_devices->collect_fs_stats = false;
1326
1327 if (index == BTRFS_READ_POLICY_RR) {
1328 if (value != -1) {
1329 const u32 sectorsize = fs_devices->fs_info->sectorsize;
1330
1331 if (!IS_ALIGNED(value, sectorsize)) {
1332 u64 temp_value = round_up(value, sectorsize);
1333
1334 btrfs_debug(fs_devices->fs_info,
1335"read_policy: min contig read %lld should be multiple of sectorsize %u, rounded to %llu",
1336 value, sectorsize, temp_value);
1337 value = temp_value;
1338 }
1339 } else {
1340 value = BTRFS_DEFAULT_RR_MIN_CONTIG_READ;
1341 }
1342
1343 if (index != READ_ONCE(fs_devices->read_policy) ||
1344 value != READ_ONCE(fs_devices->rr_min_contig_read)) {
1345 WRITE_ONCE(fs_devices->read_policy, index);
1346 WRITE_ONCE(fs_devices->rr_min_contig_read, value);
1347
1348 btrfs_info(fs_devices->fs_info, "read policy set to '%s:%lld'",
1349 btrfs_read_policy_name[index], value);
1350 }
1351
1352 fs_devices->collect_fs_stats = true;
1353
1354 return len;
1355 }
1356
1357 if (index == BTRFS_READ_POLICY_DEVID) {
1358 if (value != -1) {
1359 BTRFS_DEV_LOOKUP_ARGS(args);
1360
1361 /* Validate input devid. */
1362 args.devid = value;
1363 if (btrfs_find_device(fs_devices, &args) == NULL)
1364 return -EINVAL;
1365 } else {
1366 /* Set default devid to the devid of the latest device. */
1367 value = fs_devices->latest_dev->devid;
1368 }
1369
1370 if (index != READ_ONCE(fs_devices->read_policy) ||
1371 value != READ_ONCE(fs_devices->read_devid)) {
1372 WRITE_ONCE(fs_devices->read_policy, index);
1373 WRITE_ONCE(fs_devices->read_devid, value);
1374
1375 btrfs_info(fs_devices->fs_info, "read policy set to '%s:%llu'",
1376 btrfs_read_policy_name[index], value);
1377 }
1378
1379 return len;
1380 }
1381#endif
1382 if (index != READ_ONCE(fs_devices->read_policy)) {
1383 WRITE_ONCE(fs_devices->read_policy, index);
1384 btrfs_info(fs_devices->fs_info, "read policy set to '%s'",
1385 btrfs_read_policy_name[index]);
1386 }
1387
1388 return len;
1389}
1390BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1391
1392static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1393 struct kobj_attribute *a,
1394 char *buf)
1395{
1396 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1397
1398 return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1399}
1400
1401static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1402 struct kobj_attribute *a,
1403 const char *buf, size_t len)
1404{
1405 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1406 int thresh;
1407 int ret;
1408
1409 ret = kstrtoint(buf, 10, &thresh);
1410 if (ret)
1411 return ret;
1412
1413#ifdef CONFIG_BTRFS_DEBUG
1414 if (thresh != 0 && (thresh > 100))
1415 return -EINVAL;
1416#else
1417 if (thresh != 0 && (thresh <= 50 || thresh > 100))
1418 return -EINVAL;
1419#endif
1420
1421 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1422
1423 return len;
1424}
1425BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1426 btrfs_bg_reclaim_threshold_store);
1427
1428/*
1429 * Per-filesystem information and stats.
1430 *
1431 * Path: /sys/fs/btrfs/<uuid>/
1432 */
1433static const struct attribute *btrfs_attrs[] = {
1434 BTRFS_ATTR_PTR(, label),
1435 BTRFS_ATTR_PTR(, nodesize),
1436 BTRFS_ATTR_PTR(, sectorsize),
1437 BTRFS_ATTR_PTR(, clone_alignment),
1438 BTRFS_ATTR_PTR(, quota_override),
1439 BTRFS_ATTR_PTR(, metadata_uuid),
1440 BTRFS_ATTR_PTR(, checksum),
1441 BTRFS_ATTR_PTR(, exclusive_operation),
1442 BTRFS_ATTR_PTR(, generation),
1443 BTRFS_ATTR_PTR(, read_policy),
1444 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1445 BTRFS_ATTR_PTR(, commit_stats),
1446 BTRFS_ATTR_PTR(, temp_fsid),
1447 NULL,
1448};
1449
1450static void btrfs_release_fsid_kobj(struct kobject *kobj)
1451{
1452 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1453
1454 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1455 complete(&fs_devs->kobj_unregister);
1456}
1457
1458static const struct kobj_type btrfs_ktype = {
1459 .sysfs_ops = &kobj_sysfs_ops,
1460 .release = btrfs_release_fsid_kobj,
1461};
1462
1463static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1464{
1465 if (kobj->ktype != &btrfs_ktype)
1466 return NULL;
1467 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1468}
1469
1470static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1471{
1472 if (kobj->ktype != &btrfs_ktype)
1473 return NULL;
1474 return to_fs_devs(kobj)->fs_info;
1475}
1476
1477static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1478{
1479 while (kobj) {
1480 if (kobj->ktype == &btrfs_ktype)
1481 return kobj;
1482 kobj = kobj->parent;
1483 }
1484 return NULL;
1485}
1486
1487#define NUM_FEATURE_BITS 64
1488#define BTRFS_FEATURE_NAME_MAX 13
1489static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1490static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1491
1492static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1493 ARRAY_SIZE(btrfs_feature_attrs));
1494static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1495 ARRAY_SIZE(btrfs_feature_attrs[0]));
1496
1497static const u64 supported_feature_masks[FEAT_MAX] = {
1498 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1499 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1500 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1501};
1502
1503static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1504{
1505 int set;
1506
1507 for (set = 0; set < FEAT_MAX; set++) {
1508 int i;
1509 struct attribute *attrs[2];
1510 struct attribute_group agroup = {
1511 .name = "features",
1512 .attrs = attrs,
1513 };
1514 u64 features = get_features(fs_info, set);
1515 features &= ~supported_feature_masks[set];
1516
1517 if (!features)
1518 continue;
1519
1520 attrs[1] = NULL;
1521 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1522 struct btrfs_feature_attr *fa;
1523
1524 if (!(features & (1ULL << i)))
1525 continue;
1526
1527 fa = &btrfs_feature_attrs[set][i];
1528 attrs[0] = &fa->kobj_attr.attr;
1529 if (add) {
1530 int ret;
1531 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1532 &agroup);
1533 if (ret)
1534 return ret;
1535 } else
1536 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1537 &agroup);
1538 }
1539
1540 }
1541 return 0;
1542}
1543
1544static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1545{
1546 if (fs_devs->devinfo_kobj) {
1547 kobject_del(fs_devs->devinfo_kobj);
1548 kobject_put(fs_devs->devinfo_kobj);
1549 fs_devs->devinfo_kobj = NULL;
1550 }
1551
1552 if (fs_devs->devices_kobj) {
1553 kobject_del(fs_devs->devices_kobj);
1554 kobject_put(fs_devs->devices_kobj);
1555 fs_devs->devices_kobj = NULL;
1556 }
1557
1558 if (fs_devs->fsid_kobj.state_initialized) {
1559 kobject_del(&fs_devs->fsid_kobj);
1560 kobject_put(&fs_devs->fsid_kobj);
1561 wait_for_completion(&fs_devs->kobj_unregister);
1562 }
1563}
1564
1565/* when fs_devs is NULL it will remove all fsid kobject */
1566void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1567{
1568 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1569
1570 if (fs_devs) {
1571 __btrfs_sysfs_remove_fsid(fs_devs);
1572 return;
1573 }
1574
1575 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1576 __btrfs_sysfs_remove_fsid(fs_devs);
1577 }
1578}
1579
1580static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1581{
1582 struct btrfs_device *device;
1583 struct btrfs_fs_devices *seed;
1584
1585 list_for_each_entry(device, &fs_devices->devices, dev_list)
1586 btrfs_sysfs_remove_device(device);
1587
1588 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1589 list_for_each_entry(device, &seed->devices, dev_list)
1590 btrfs_sysfs_remove_device(device);
1591 }
1592}
1593
1594void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1595{
1596 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1597
1598 sysfs_remove_link(fsid_kobj, "bdi");
1599
1600 if (fs_info->space_info_kobj) {
1601 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1602 kobject_del(fs_info->space_info_kobj);
1603 kobject_put(fs_info->space_info_kobj);
1604 }
1605 if (fs_info->discard_kobj) {
1606 sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1607 kobject_del(fs_info->discard_kobj);
1608 kobject_put(fs_info->discard_kobj);
1609 }
1610#ifdef CONFIG_BTRFS_DEBUG
1611 if (fs_info->debug_kobj) {
1612 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1613 kobject_del(fs_info->debug_kobj);
1614 kobject_put(fs_info->debug_kobj);
1615 }
1616#endif
1617 addrm_unknown_feature_attrs(fs_info, false);
1618 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1619 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1620 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1621}
1622
1623static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1624 [FEAT_COMPAT] = "compat",
1625 [FEAT_COMPAT_RO] = "compat_ro",
1626 [FEAT_INCOMPAT] = "incompat",
1627};
1628
1629const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1630{
1631 return btrfs_feature_set_names[set];
1632}
1633
1634char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1635{
1636 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1637 int len = 0;
1638 int i;
1639 char *str;
1640
1641 str = kmalloc(bufsize, GFP_KERNEL);
1642 if (!str)
1643 return str;
1644
1645 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1646 const char *name;
1647
1648 if (!(flags & (1ULL << i)))
1649 continue;
1650
1651 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1652 len += scnprintf(str + len, bufsize - len, "%s%s",
1653 len ? "," : "", name);
1654 }
1655
1656 return str;
1657}
1658
1659static void init_feature_attrs(void)
1660{
1661 struct btrfs_feature_attr *fa;
1662 int set, i;
1663
1664 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1665 memset(btrfs_unknown_feature_names, 0,
1666 sizeof(btrfs_unknown_feature_names));
1667
1668 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1669 struct btrfs_feature_attr *sfa;
1670 struct attribute *a = btrfs_supported_feature_attrs[i];
1671 int bit;
1672 sfa = attr_to_btrfs_feature_attr(a);
1673 bit = ilog2(sfa->feature_bit);
1674 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1675
1676 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1677 }
1678
1679 for (set = 0; set < FEAT_MAX; set++) {
1680 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1681 char *name = btrfs_unknown_feature_names[set][i];
1682 fa = &btrfs_feature_attrs[set][i];
1683
1684 if (fa->kobj_attr.attr.name)
1685 continue;
1686
1687 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1688 btrfs_feature_set_names[set], i);
1689
1690 fa->kobj_attr.attr.name = name;
1691 fa->kobj_attr.attr.mode = S_IRUGO;
1692 fa->feature_set = set;
1693 fa->feature_bit = 1ULL << i;
1694 }
1695 }
1696}
1697
1698/*
1699 * Create a sysfs entry for a given block group type at path
1700 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1701 */
1702void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1703{
1704 struct btrfs_fs_info *fs_info = cache->fs_info;
1705 struct btrfs_space_info *space_info = cache->space_info;
1706 struct raid_kobject *rkobj;
1707 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1708 unsigned int nofs_flag;
1709 int ret;
1710
1711 /*
1712 * Setup a NOFS context because kobject_add(), deep in its call chain,
1713 * does GFP_KERNEL allocations, and we are often called in a context
1714 * where if reclaim is triggered we can deadlock (we are either holding
1715 * a transaction handle or some lock required for a transaction
1716 * commit).
1717 */
1718 nofs_flag = memalloc_nofs_save();
1719
1720 rkobj = kzalloc_obj(*rkobj, GFP_NOFS);
1721 if (!rkobj) {
1722 memalloc_nofs_restore(nofs_flag);
1723 btrfs_warn(cache->fs_info,
1724 "couldn't alloc memory for raid level kobject");
1725 return;
1726 }
1727
1728 rkobj->flags = cache->flags;
1729 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1730
1731 /*
1732 * We call this either on mount, or if we've created a block group for a
1733 * new index type while running (i.e. when restriping). The running
1734 * case is tricky because we could race with other threads, so we need
1735 * to have this check to make sure we didn't already init the kobject.
1736 *
1737 * We don't have to protect on the free side because it only happens on
1738 * unmount.
1739 */
1740 spin_lock(&space_info->lock);
1741 if (space_info->block_group_kobjs[index]) {
1742 spin_unlock(&space_info->lock);
1743 kobject_put(&rkobj->kobj);
1744 return;
1745 } else {
1746 space_info->block_group_kobjs[index] = &rkobj->kobj;
1747 }
1748 spin_unlock(&space_info->lock);
1749
1750 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1751 btrfs_bg_type_to_raid_name(rkobj->flags));
1752 memalloc_nofs_restore(nofs_flag);
1753 if (ret) {
1754 spin_lock(&space_info->lock);
1755 space_info->block_group_kobjs[index] = NULL;
1756 spin_unlock(&space_info->lock);
1757 kobject_put(&rkobj->kobj);
1758 btrfs_warn(fs_info,
1759 "failed to add kobject for block cache, ignoring");
1760 return;
1761 }
1762}
1763
1764/*
1765 * Remove sysfs directories for all block group types of a given space info and
1766 * the space info as well
1767 */
1768void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1769{
1770 int i;
1771
1772 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1773 struct kobject *kobj;
1774
1775 kobj = space_info->block_group_kobjs[i];
1776 space_info->block_group_kobjs[i] = NULL;
1777 if (kobj) {
1778 kobject_del(kobj);
1779 kobject_put(kobj);
1780 }
1781 }
1782 kobject_del(&space_info->kobj);
1783 kobject_put(&space_info->kobj);
1784}
1785
1786static const char *alloc_name(struct btrfs_space_info *space_info)
1787{
1788 u64 flags = space_info->flags;
1789
1790 switch (flags) {
1791 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1792 return "mixed";
1793 case BTRFS_BLOCK_GROUP_METADATA:
1794 switch (space_info->subgroup_id) {
1795 case BTRFS_SUB_GROUP_PRIMARY:
1796 return "metadata";
1797 case BTRFS_SUB_GROUP_TREELOG:
1798 return "metadata-treelog";
1799 default:
1800 WARN_ON_ONCE(1);
1801 return "metadata (unknown sub-group)";
1802 }
1803 case BTRFS_BLOCK_GROUP_DATA:
1804 switch (space_info->subgroup_id) {
1805 case BTRFS_SUB_GROUP_PRIMARY:
1806 return "data";
1807 case BTRFS_SUB_GROUP_DATA_RELOC:
1808 return "data-reloc";
1809 default:
1810 WARN_ON_ONCE(1);
1811 return "data (unknown sub-group)";
1812 }
1813 case BTRFS_BLOCK_GROUP_SYSTEM:
1814 ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_PRIMARY);
1815 return "system";
1816 case BTRFS_BLOCK_GROUP_METADATA_REMAP:
1817 return "metadata-remap";
1818 default:
1819 WARN_ON(1);
1820 return "invalid-combination";
1821 }
1822}
1823
1824/*
1825 * Create a sysfs entry for a space info type at path
1826 * /sys/fs/btrfs/UUID/allocation/TYPE
1827 */
1828int btrfs_sysfs_add_space_info_type(struct btrfs_space_info *space_info)
1829{
1830 int ret;
1831
1832 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1833 space_info->fs_info->space_info_kobj, "%s",
1834 alloc_name(space_info));
1835 if (ret) {
1836 kobject_put(&space_info->kobj);
1837 return ret;
1838 }
1839
1840 return 0;
1841}
1842
1843void btrfs_sysfs_remove_device(struct btrfs_device *device)
1844{
1845 struct kobject *devices_kobj;
1846
1847 /*
1848 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1849 * fs_info::fs_devices.
1850 */
1851 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1852 ASSERT(devices_kobj);
1853
1854 if (device->bdev)
1855 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1856
1857 if (device->devid_kobj.state_initialized) {
1858 kobject_del(&device->devid_kobj);
1859 kobject_put(&device->devid_kobj);
1860 wait_for_completion(&device->kobj_unregister);
1861 }
1862}
1863
1864static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1865 struct kobj_attribute *a,
1866 char *buf)
1867{
1868 int val;
1869 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1870 devid_kobj);
1871
1872 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1873
1874 return sysfs_emit(buf, "%d\n", val);
1875}
1876BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1877
1878static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1879 struct kobj_attribute *a, char *buf)
1880{
1881 int val;
1882 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1883 devid_kobj);
1884
1885 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1886
1887 return sysfs_emit(buf, "%d\n", val);
1888}
1889BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1890
1891static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1892 struct kobj_attribute *a,
1893 char *buf)
1894{
1895 int val;
1896 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1897 devid_kobj);
1898
1899 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1900
1901 return sysfs_emit(buf, "%d\n", val);
1902}
1903BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1904
1905static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1906 struct kobj_attribute *a,
1907 char *buf)
1908{
1909 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1910 devid_kobj);
1911
1912 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1913}
1914
1915static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1916 struct kobj_attribute *a,
1917 const char *buf, size_t len)
1918{
1919 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1920 devid_kobj);
1921 char *endptr;
1922 unsigned long long limit;
1923
1924 limit = memparse(buf, &endptr);
1925 /* There could be trailing '\n', also catch any typos after the value. */
1926 endptr = skip_spaces(endptr);
1927 if (*endptr != 0)
1928 return -EINVAL;
1929 WRITE_ONCE(device->scrub_speed_max, limit);
1930 return len;
1931}
1932BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1933 btrfs_devinfo_scrub_speed_max_store);
1934
1935static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1936 struct kobj_attribute *a, char *buf)
1937{
1938 int val;
1939 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1940 devid_kobj);
1941
1942 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1943
1944 return sysfs_emit(buf, "%d\n", val);
1945}
1946BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1947
1948static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1949 struct kobj_attribute *a, char *buf)
1950{
1951 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1952 devid_kobj);
1953
1954 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1955}
1956BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1957
1958static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1959 struct kobj_attribute *a, char *buf)
1960{
1961 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1962 devid_kobj);
1963
1964 if (!device->dev_stats_valid)
1965 return sysfs_emit(buf, "invalid\n");
1966
1967 /*
1968 * Print all at once so we get a snapshot of all values from the same
1969 * time. Keep them in sync and in order of definition of
1970 * btrfs_dev_stat_values.
1971 */
1972 return sysfs_emit(buf,
1973 "write_errs %d\n"
1974 "read_errs %d\n"
1975 "flush_errs %d\n"
1976 "corruption_errs %d\n"
1977 "generation_errs %d\n",
1978 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1979 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1980 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1981 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1982 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1983}
1984BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1985
1986/*
1987 * Information about one device.
1988 *
1989 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1990 */
1991static struct attribute *devid_attrs[] = {
1992 BTRFS_ATTR_PTR(devid, error_stats),
1993 BTRFS_ATTR_PTR(devid, fsid),
1994 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1995 BTRFS_ATTR_PTR(devid, missing),
1996 BTRFS_ATTR_PTR(devid, replace_target),
1997 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1998 BTRFS_ATTR_PTR(devid, writeable),
1999 NULL
2000};
2001ATTRIBUTE_GROUPS(devid);
2002
2003static void btrfs_release_devid_kobj(struct kobject *kobj)
2004{
2005 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
2006 devid_kobj);
2007
2008 memset(&device->devid_kobj, 0, sizeof(struct kobject));
2009 complete(&device->kobj_unregister);
2010}
2011
2012static const struct kobj_type devid_ktype = {
2013 .sysfs_ops = &kobj_sysfs_ops,
2014 .default_groups = devid_groups,
2015 .release = btrfs_release_devid_kobj,
2016};
2017
2018int btrfs_sysfs_add_device(struct btrfs_device *device)
2019{
2020 int ret;
2021 unsigned int nofs_flag;
2022 struct kobject *devices_kobj;
2023 struct kobject *devinfo_kobj;
2024
2025 /*
2026 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
2027 * for the seed fs_devices
2028 */
2029 devices_kobj = device->fs_info->fs_devices->devices_kobj;
2030 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
2031 ASSERT(devices_kobj);
2032 ASSERT(devinfo_kobj);
2033
2034 nofs_flag = memalloc_nofs_save();
2035
2036 if (device->bdev) {
2037 struct kobject *disk_kobj = bdev_kobj(device->bdev);
2038
2039 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
2040 if (ret) {
2041 btrfs_warn(device->fs_info,
2042 "creating sysfs device link for devid %llu failed: %d",
2043 device->devid, ret);
2044 goto out;
2045 }
2046 }
2047
2048 init_completion(&device->kobj_unregister);
2049 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
2050 devinfo_kobj, "%llu", device->devid);
2051 if (ret) {
2052 kobject_put(&device->devid_kobj);
2053 btrfs_warn(device->fs_info,
2054 "devinfo init for devid %llu failed: %d",
2055 device->devid, ret);
2056 }
2057
2058out:
2059 memalloc_nofs_restore(nofs_flag);
2060 return ret;
2061}
2062
2063static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
2064{
2065 int ret;
2066 struct btrfs_device *device;
2067 struct btrfs_fs_devices *seed;
2068
2069 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2070 ret = btrfs_sysfs_add_device(device);
2071 if (ret)
2072 goto fail;
2073 }
2074
2075 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
2076 list_for_each_entry(device, &seed->devices, dev_list) {
2077 ret = btrfs_sysfs_add_device(device);
2078 if (ret)
2079 goto fail;
2080 }
2081 }
2082
2083 return 0;
2084
2085fail:
2086 btrfs_sysfs_remove_fs_devices(fs_devices);
2087 return ret;
2088}
2089
2090void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
2091{
2092 int ret;
2093
2094 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
2095 if (ret)
2096 btrfs_warn(NULL, "sending event %d to kobject: '%s' (%p): failed",
2097 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
2098 &disk_to_dev(bdev->bd_disk)->kobj);
2099}
2100
2101void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
2102
2103{
2104 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2105
2106 /*
2107 * Sprouting changes fsid of the mounted filesystem, rename the fsid
2108 * directory
2109 */
2110 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
2111 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
2112 btrfs_warn(fs_devices->fs_info,
2113 "sysfs: failed to create fsid for sprout");
2114}
2115
2116void btrfs_sysfs_update_devid(struct btrfs_device *device)
2117{
2118 char tmp[24];
2119
2120 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
2121
2122 if (kobject_rename(&device->devid_kobj, tmp))
2123 btrfs_warn(device->fs_devices->fs_info,
2124 "sysfs: failed to update devid for %llu",
2125 device->devid);
2126}
2127
2128/* /sys/fs/btrfs/ entry */
2129static struct kset *btrfs_kset;
2130
2131/*
2132 * Creates:
2133 * /sys/fs/btrfs/UUID
2134 *
2135 * Can be called by the device discovery thread.
2136 */
2137int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
2138{
2139 int ret;
2140
2141 init_completion(&fs_devs->kobj_unregister);
2142 fs_devs->fsid_kobj.kset = btrfs_kset;
2143 ret = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
2144 "%pU", fs_devs->fsid);
2145 if (ret) {
2146 kobject_put(&fs_devs->fsid_kobj);
2147 return ret;
2148 }
2149
2150 fs_devs->devices_kobj = kobject_create_and_add("devices",
2151 &fs_devs->fsid_kobj);
2152 if (!fs_devs->devices_kobj) {
2153 btrfs_err(fs_devs->fs_info,
2154 "failed to init sysfs device interface");
2155 btrfs_sysfs_remove_fsid(fs_devs);
2156 return -ENOMEM;
2157 }
2158
2159 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
2160 &fs_devs->fsid_kobj);
2161 if (!fs_devs->devinfo_kobj) {
2162 btrfs_err(fs_devs->fs_info,
2163 "failed to init sysfs devinfo kobject");
2164 btrfs_sysfs_remove_fsid(fs_devs);
2165 return -ENOMEM;
2166 }
2167
2168 return 0;
2169}
2170
2171int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
2172{
2173 int ret;
2174 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
2175 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
2176
2177 ret = btrfs_sysfs_add_fs_devices(fs_devs);
2178 if (ret)
2179 return ret;
2180
2181 ret = sysfs_create_files(fsid_kobj, btrfs_attrs);
2182 if (ret) {
2183 btrfs_sysfs_remove_fs_devices(fs_devs);
2184 return ret;
2185 }
2186
2187 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2188 if (ret)
2189 goto failure;
2190
2191#ifdef CONFIG_BTRFS_DEBUG
2192 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
2193 if (!fs_info->debug_kobj) {
2194 ret = -ENOMEM;
2195 goto failure;
2196 }
2197
2198 ret = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
2199 if (ret)
2200 goto failure;
2201#endif
2202
2203 /* Discard directory */
2204 fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
2205 if (!fs_info->discard_kobj) {
2206 ret = -ENOMEM;
2207 goto failure;
2208 }
2209
2210 ret = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
2211 if (ret)
2212 goto failure;
2213
2214 ret = addrm_unknown_feature_attrs(fs_info, true);
2215 if (ret)
2216 goto failure;
2217
2218 ret = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2219 if (ret)
2220 goto failure;
2221
2222 fs_info->space_info_kobj = kobject_create_and_add("allocation",
2223 fsid_kobj);
2224 if (!fs_info->space_info_kobj) {
2225 ret = -ENOMEM;
2226 goto failure;
2227 }
2228
2229 ret = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2230 if (ret)
2231 goto failure;
2232
2233 return 0;
2234failure:
2235 btrfs_sysfs_remove_mounted(fs_info);
2236 return ret;
2237}
2238
2239static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2240 struct kobj_attribute *a,
2241 char *buf)
2242{
2243 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2244 bool enabled;
2245
2246 enabled = test_bit(BTRFS_QGROUP_STATUS_BIT_ON, &fs_info->qgroup_flags);
2247
2248 return sysfs_emit(buf, "%d\n", enabled);
2249}
2250BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2251
2252static ssize_t qgroup_mode_show(struct kobject *qgroups_kobj,
2253 struct kobj_attribute *a,
2254 char *buf)
2255{
2256 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2257 ssize_t ret = 0;
2258
2259 spin_lock(&fs_info->qgroup_lock);
2260 ASSERT(btrfs_qgroup_enabled(fs_info));
2261 switch (btrfs_qgroup_mode(fs_info)) {
2262 case BTRFS_QGROUP_MODE_FULL:
2263 ret = sysfs_emit(buf, "qgroup\n");
2264 break;
2265 case BTRFS_QGROUP_MODE_SIMPLE:
2266 ret = sysfs_emit(buf, "squota\n");
2267 break;
2268 default:
2269 btrfs_warn(fs_info, "unexpected qgroup mode %d\n",
2270 btrfs_qgroup_mode(fs_info));
2271 break;
2272 }
2273 spin_unlock(&fs_info->qgroup_lock);
2274
2275 return ret;
2276}
2277BTRFS_ATTR(qgroups, mode, qgroup_mode_show);
2278
2279static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2280 struct kobj_attribute *a,
2281 char *buf)
2282{
2283 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2284 bool inconsistent;
2285
2286 inconsistent = test_bit(BTRFS_QGROUP_STATUS_BIT_INCONSISTENT, &fs_info->qgroup_flags);
2287
2288 return sysfs_emit(buf, "%d\n", inconsistent);
2289}
2290BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2291
2292static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2293 struct kobj_attribute *a,
2294 char *buf)
2295{
2296 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2297 u8 result;
2298
2299 spin_lock(&fs_info->qgroup_lock);
2300 result = fs_info->qgroup_drop_subtree_thres;
2301 spin_unlock(&fs_info->qgroup_lock);
2302
2303 return sysfs_emit(buf, "%d\n", result);
2304}
2305
2306static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2307 struct kobj_attribute *a,
2308 const char *buf, size_t len)
2309{
2310 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2311 u8 new_thres;
2312 int ret;
2313
2314 ret = kstrtou8(buf, 10, &new_thres);
2315 if (ret)
2316 return -EINVAL;
2317
2318 if (new_thres > BTRFS_MAX_LEVEL)
2319 return -EINVAL;
2320
2321 spin_lock(&fs_info->qgroup_lock);
2322 fs_info->qgroup_drop_subtree_thres = new_thres;
2323 spin_unlock(&fs_info->qgroup_lock);
2324
2325 return len;
2326}
2327BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2328 qgroup_drop_subtree_thres_store);
2329
2330/*
2331 * Qgroups global info
2332 *
2333 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2334 */
2335static struct attribute *qgroups_attrs[] = {
2336 BTRFS_ATTR_PTR(qgroups, enabled),
2337 BTRFS_ATTR_PTR(qgroups, inconsistent),
2338 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2339 BTRFS_ATTR_PTR(qgroups, mode),
2340 NULL
2341};
2342ATTRIBUTE_GROUPS(qgroups);
2343
2344static void qgroups_release(struct kobject *kobj)
2345{
2346 kfree(kobj);
2347}
2348
2349static const struct kobj_type qgroups_ktype = {
2350 .sysfs_ops = &kobj_sysfs_ops,
2351 .default_groups = qgroups_groups,
2352 .release = qgroups_release,
2353};
2354
2355static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2356{
2357 return to_fs_info(kobj->parent->parent);
2358}
2359
2360#define QGROUP_ATTR(_member, _show_name) \
2361static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
2362 struct kobj_attribute *a, \
2363 char *buf) \
2364{ \
2365 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2366 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2367 struct btrfs_qgroup, kobj); \
2368 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
2369} \
2370BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2371
2372#define QGROUP_RSV_ATTR(_name, _type) \
2373static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
2374 struct kobj_attribute *a, \
2375 char *buf) \
2376{ \
2377 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2378 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2379 struct btrfs_qgroup, kobj); \
2380 return btrfs_show_u64(&qgroup->rsv.values[_type], \
2381 &fs_info->qgroup_lock, buf); \
2382} \
2383BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2384
2385QGROUP_ATTR(rfer, referenced);
2386QGROUP_ATTR(excl, exclusive);
2387QGROUP_ATTR(max_rfer, max_referenced);
2388QGROUP_ATTR(max_excl, max_exclusive);
2389QGROUP_ATTR(lim_flags, limit_flags);
2390QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2391QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2392QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2393
2394/*
2395 * Qgroup information.
2396 *
2397 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2398 */
2399static struct attribute *qgroup_attrs[] = {
2400 BTRFS_ATTR_PTR(qgroup, referenced),
2401 BTRFS_ATTR_PTR(qgroup, exclusive),
2402 BTRFS_ATTR_PTR(qgroup, max_referenced),
2403 BTRFS_ATTR_PTR(qgroup, max_exclusive),
2404 BTRFS_ATTR_PTR(qgroup, limit_flags),
2405 BTRFS_ATTR_PTR(qgroup, rsv_data),
2406 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2407 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2408 NULL
2409};
2410ATTRIBUTE_GROUPS(qgroup);
2411
2412static void qgroup_release(struct kobject *kobj)
2413{
2414 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2415
2416 memset(&qgroup->kobj, 0, sizeof(*kobj));
2417}
2418
2419static const struct kobj_type qgroup_ktype = {
2420 .sysfs_ops = &kobj_sysfs_ops,
2421 .release = qgroup_release,
2422 .default_groups = qgroup_groups,
2423};
2424
2425int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2426 struct btrfs_qgroup *qgroup)
2427{
2428 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2429 int ret;
2430
2431 if (btrfs_is_testing(fs_info))
2432 return 0;
2433 if (qgroup->kobj.state_initialized)
2434 return 0;
2435 if (!qgroups_kobj)
2436 return -EINVAL;
2437
2438 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2439 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2440 btrfs_qgroup_subvolid(qgroup->qgroupid));
2441 if (ret < 0)
2442 kobject_put(&qgroup->kobj);
2443
2444 return ret;
2445}
2446
2447void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2448{
2449 struct btrfs_qgroup *qgroup;
2450 struct btrfs_qgroup *next;
2451
2452 if (btrfs_is_testing(fs_info))
2453 return;
2454
2455 rbtree_postorder_for_each_entry_safe(qgroup, next,
2456 &fs_info->qgroup_tree, node)
2457 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2458 if (fs_info->qgroups_kobj) {
2459 kobject_del(fs_info->qgroups_kobj);
2460 kobject_put(fs_info->qgroups_kobj);
2461 fs_info->qgroups_kobj = NULL;
2462 }
2463}
2464
2465/* Called when qgroups get initialized, thus there is no need for locking */
2466int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2467{
2468 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2469 struct btrfs_qgroup *qgroup;
2470 struct btrfs_qgroup *next;
2471 int ret = 0;
2472
2473 if (btrfs_is_testing(fs_info))
2474 return 0;
2475
2476 ASSERT(fsid_kobj);
2477 if (fs_info->qgroups_kobj)
2478 return 0;
2479
2480 fs_info->qgroups_kobj = kzalloc_obj(struct kobject);
2481 if (!fs_info->qgroups_kobj)
2482 return -ENOMEM;
2483
2484 ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2485 fsid_kobj, "qgroups");
2486 if (ret < 0)
2487 goto out;
2488
2489 rbtree_postorder_for_each_entry_safe(qgroup, next,
2490 &fs_info->qgroup_tree, node) {
2491 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2492 if (ret < 0)
2493 goto out;
2494 }
2495
2496out:
2497 if (ret < 0)
2498 btrfs_sysfs_del_qgroups(fs_info);
2499 return ret;
2500}
2501
2502void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2503 struct btrfs_qgroup *qgroup)
2504{
2505 if (btrfs_is_testing(fs_info))
2506 return;
2507
2508 if (qgroup->kobj.state_initialized) {
2509 kobject_del(&qgroup->kobj);
2510 kobject_put(&qgroup->kobj);
2511 }
2512}
2513
2514/*
2515 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2516 * values in superblock. Call after any changes to incompat/compat_ro flags
2517 */
2518void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info)
2519{
2520 struct kobject *fsid_kobj;
2521 int ret;
2522
2523 if (!fs_info)
2524 return;
2525
2526 fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2527 if (!fsid_kobj->state_initialized)
2528 return;
2529
2530 ret = sysfs_update_group(fsid_kobj, &btrfs_feature_attr_group);
2531 if (ret < 0)
2532 btrfs_warn(fs_info,
2533 "failed to update /sys/fs/btrfs/%pU/features: %d",
2534 fs_info->fs_devices->fsid, ret);
2535}
2536
2537int __init btrfs_init_sysfs(void)
2538{
2539 int ret;
2540
2541 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2542 if (!btrfs_kset)
2543 return -ENOMEM;
2544
2545 init_feature_attrs();
2546 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2547 if (ret)
2548 goto out2;
2549 ret = sysfs_merge_group(&btrfs_kset->kobj,
2550 &btrfs_static_feature_attr_group);
2551 if (ret)
2552 goto out_remove_group;
2553
2554#ifdef CONFIG_BTRFS_DEBUG
2555 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2556 if (ret) {
2557 sysfs_unmerge_group(&btrfs_kset->kobj,
2558 &btrfs_static_feature_attr_group);
2559 goto out_remove_group;
2560 }
2561#endif
2562
2563 return 0;
2564
2565out_remove_group:
2566 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2567out2:
2568 kset_unregister(btrfs_kset);
2569
2570 return ret;
2571}
2572
2573void __cold btrfs_exit_sysfs(void)
2574{
2575 sysfs_unmerge_group(&btrfs_kset->kobj,
2576 &btrfs_static_feature_attr_group);
2577 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2578#ifdef CONFIG_BTRFS_DEBUG
2579 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2580#endif
2581 kset_unregister(btrfs_kset);
2582}