1
2
3
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 <crypto/hash.h>
13
14#include "ctree.h"
15#include "discard.h"
16#include "disk-io.h"
17#include "send.h"
18#include "transaction.h"
19#include "sysfs.h"
20#include "volumes.h"
21#include "space-info.h"
22#include "block-group.h"
23#include "qgroup.h"
24
25struct btrfs_feature_attr {
26 struct kobj_attribute kobj_attr;
27 enum btrfs_feature_set feature_set;
28 u64 feature_bit;
29};
30
31
32struct raid_kobject {
33 u64 flags;
34 struct kobject kobj;
35};
36
37#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
38{ \
39 .attr = { .name = __stringify(_name), .mode = _mode }, \
40 .show = _show, \
41 .store = _store, \
42}
43
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)
47
48#define BTRFS_ATTR(_prefix, _name, _show) \
49 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
50 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
51
52#define BTRFS_ATTR_PTR(_prefix, _name) \
53 (&btrfs_attr_##_prefix##_##_name.attr)
54
55#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
56static 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, \
62}
63#define BTRFS_FEAT_ATTR_PTR(_name) \
64 (&btrfs_attr_features_##_name.kobj_attr.attr)
65
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)
72
73static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
74static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
75
76static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
77{
78 return container_of(a, struct btrfs_feature_attr, kobj_attr);
79}
80
81static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
82{
83 return container_of(attr, struct kobj_attribute, attr);
84}
85
86static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
87 struct attribute *attr)
88{
89 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
90}
91
92static u64 get_features(struct btrfs_fs_info *fs_info,
93 enum btrfs_feature_set set)
94{
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);
100 else
101 return btrfs_super_incompat_flags(disk_super);
102}
103
104static void set_features(struct btrfs_fs_info *fs_info,
105 enum btrfs_feature_set set, u64 features)
106{
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);
112 else
113 btrfs_set_super_incompat_flags(disk_super, features);
114}
115
116static int can_modify_feature(struct btrfs_feature_attr *fa)
117{
118 int val = 0;
119 u64 set, clear;
120 switch (fa->feature_set) {
121 case FEAT_COMPAT:
122 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
123 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
124 break;
125 case FEAT_COMPAT_RO:
126 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
127 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
128 break;
129 case FEAT_INCOMPAT:
130 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
131 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
132 break;
133 default:
134 pr_warn("btrfs: sysfs: unknown feature set %d\n",
135 fa->feature_set);
136 return 0;
137 }
138
139 if (set & fa->feature_bit)
140 val |= 1;
141 if (clear & fa->feature_bit)
142 val |= 2;
143
144 return val;
145}
146
147static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
148 struct kobj_attribute *a, char *buf)
149{
150 int val = 0;
151 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
152 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
153 if (fs_info) {
154 u64 features = get_features(fs_info, fa->feature_set);
155 if (features & fa->feature_bit)
156 val = 1;
157 } else
158 val = can_modify_feature(fa);
159
160 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
161}
162
163static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
164 struct kobj_attribute *a,
165 const char *buf, size_t count)
166{
167 struct btrfs_fs_info *fs_info;
168 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
169 u64 features, set, clear;
170 unsigned long val;
171 int ret;
172
173 fs_info = to_fs_info(kobj);
174 if (!fs_info)
175 return -EPERM;
176
177 if (sb_rdonly(fs_info->sb))
178 return -EROFS;
179
180 ret = kstrtoul(skip_spaces(buf), 0, &val);
181 if (ret)
182 return ret;
183
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;
190 } else {
191 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
192 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
193 }
194
195 features = get_features(fs_info, fa->feature_set);
196
197
198 if ((val && (features & fa->feature_bit)) ||
199 (!val && !(features & fa->feature_bit)))
200 return count;
201
202 if ((val && !(set & fa->feature_bit)) ||
203 (!val && !(clear & fa->feature_bit))) {
204 btrfs_info(fs_info,
205 "%sabling feature %s on mounted fs is not supported.",
206 val ? "En" : "Dis", fa->kobj_attr.attr.name);
207 return -EPERM;
208 }
209
210 btrfs_info(fs_info, "%s %s feature flag",
211 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
212
213 spin_lock(&fs_info->super_lock);
214 features = get_features(fs_info, fa->feature_set);
215 if (val)
216 features |= fa->feature_bit;
217 else
218 features &= ~fa->feature_bit;
219 set_features(fs_info, fa->feature_set, features);
220 spin_unlock(&fs_info->super_lock);
221
222
223
224
225 btrfs_set_pending(fs_info, COMMIT);
226 wake_up_process(fs_info->transaction_kthread);
227
228 return count;
229}
230
231static umode_t btrfs_feature_visible(struct kobject *kobj,
232 struct attribute *attr, int unused)
233{
234 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
235 umode_t mode = attr->mode;
236
237 if (fs_info) {
238 struct btrfs_feature_attr *fa;
239 u64 features;
240
241 fa = attr_to_btrfs_feature_attr(attr);
242 features = get_features(fs_info, fa->feature_set);
243
244 if (can_modify_feature(fa))
245 mode |= S_IWUSR;
246 else if (!(features & fa->feature_bit))
247 mode = 0;
248 }
249
250 return mode;
251}
252
253BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
254BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
255BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
256BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
257BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
258BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
259BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
260BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
261BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
262BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
263BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
264BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
265BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
266
267#ifdef CONFIG_BTRFS_DEBUG
268BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
269#endif
270
271static 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),
287#endif
288 NULL
289};
290
291
292
293
294
295
296
297
298static const struct attribute_group btrfs_feature_attr_group = {
299 .name = "features",
300 .is_visible = btrfs_feature_visible,
301 .attrs = btrfs_supported_feature_attrs,
302};
303
304static ssize_t rmdir_subvol_show(struct kobject *kobj,
305 struct kobj_attribute *ka, char *buf)
306{
307 return scnprintf(buf, PAGE_SIZE, "0\n");
308}
309BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
310
311static ssize_t supported_checksums_show(struct kobject *kobj,
312 struct kobj_attribute *a, char *buf)
313{
314 ssize_t ret = 0;
315 int i;
316
317 for (i = 0; i < btrfs_get_num_csums(); i++) {
318
319
320
321
322 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
323 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
324
325 }
326
327 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
328 return ret;
329}
330BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
331
332static ssize_t send_stream_version_show(struct kobject *kobj,
333 struct kobj_attribute *ka, char *buf)
334{
335 return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
336}
337BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
338
339static const char *rescue_opts[] = {
340 "usebackuproot",
341 "nologreplay",
342 "ignorebadroots",
343 "ignoredatacsums",
344 "all",
345};
346
347static ssize_t supported_rescue_options_show(struct kobject *kobj,
348 struct kobj_attribute *a,
349 char *buf)
350{
351 ssize_t ret = 0;
352 int i;
353
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");
358 return ret;
359}
360BTRFS_ATTR(static_feature, supported_rescue_options,
361 supported_rescue_options_show);
362
363static 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),
368 NULL
369};
370
371
372
373
374
375
376
377static const struct attribute_group btrfs_static_feature_attr_group = {
378 .name = "features",
379 .attrs = btrfs_supported_static_feature_attrs,
380};
381
382#ifdef CONFIG_BTRFS_DEBUG
383
384
385
386
387#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
388
389static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
390 struct kobj_attribute *a,
391 char *buf)
392{
393 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
394
395 return scnprintf(buf, PAGE_SIZE, "%lld\n",
396 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
397}
398BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
399
400static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
401 struct kobj_attribute *a,
402 char *buf)
403{
404 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
405
406 return scnprintf(buf, PAGE_SIZE, "%d\n",
407 atomic_read(&fs_info->discard_ctl.discardable_extents));
408}
409BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
410
411static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
412 struct kobj_attribute *a,
413 char *buf)
414{
415 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
416
417 return scnprintf(buf, PAGE_SIZE, "%lld\n",
418 fs_info->discard_ctl.discard_bitmap_bytes);
419}
420BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
421
422static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
423 struct kobj_attribute *a,
424 char *buf)
425{
426 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
427
428 return scnprintf(buf, PAGE_SIZE, "%lld\n",
429 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
430}
431BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
432
433static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
434 struct kobj_attribute *a,
435 char *buf)
436{
437 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
438
439 return scnprintf(buf, PAGE_SIZE, "%lld\n",
440 fs_info->discard_ctl.discard_extent_bytes);
441}
442BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
443
444static ssize_t btrfs_discard_iops_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 scnprintf(buf, PAGE_SIZE, "%u\n",
451 READ_ONCE(fs_info->discard_ctl.iops_limit));
452}
453
454static ssize_t btrfs_discard_iops_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 iops_limit;
461 int ret;
462
463 ret = kstrtou32(buf, 10, &iops_limit);
464 if (ret)
465 return -EINVAL;
466
467 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
468 btrfs_discard_calc_delay(discard_ctl);
469 btrfs_discard_schedule_work(discard_ctl, true);
470 return len;
471}
472BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
473 btrfs_discard_iops_limit_store);
474
475static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
476 struct kobj_attribute *a,
477 char *buf)
478{
479 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
480
481 return scnprintf(buf, PAGE_SIZE, "%u\n",
482 READ_ONCE(fs_info->discard_ctl.kbps_limit));
483}
484
485static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
486 struct kobj_attribute *a,
487 const char *buf, size_t len)
488{
489 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
490 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
491 u32 kbps_limit;
492 int ret;
493
494 ret = kstrtou32(buf, 10, &kbps_limit);
495 if (ret)
496 return -EINVAL;
497
498 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
499 btrfs_discard_schedule_work(discard_ctl, true);
500 return len;
501}
502BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
503 btrfs_discard_kbps_limit_store);
504
505static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
506 struct kobj_attribute *a,
507 char *buf)
508{
509 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
510
511 return scnprintf(buf, PAGE_SIZE, "%llu\n",
512 READ_ONCE(fs_info->discard_ctl.max_discard_size));
513}
514
515static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
516 struct kobj_attribute *a,
517 const char *buf, size_t len)
518{
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;
522 int ret;
523
524 ret = kstrtou64(buf, 10, &max_discard_size);
525 if (ret)
526 return -EINVAL;
527
528 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
529
530 return len;
531}
532BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
533 btrfs_discard_max_discard_size_store);
534
535static 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),
544 NULL,
545};
546
547
548
549
550
551
552
553static const struct attribute *btrfs_debug_mount_attrs[] = {
554 NULL,
555};
556
557static struct attribute *btrfs_debug_feature_attrs[] = {
558 NULL
559};
560
561static const struct attribute_group btrfs_debug_feature_attr_group = {
562 .name = "debug",
563 .attrs = btrfs_debug_feature_attrs,
564};
565
566#endif
567
568static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
569{
570 u64 val;
571 if (lock)
572 spin_lock(lock);
573 val = *value_ptr;
574 if (lock)
575 spin_unlock(lock);
576 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
577}
578
579static ssize_t global_rsv_size_show(struct kobject *kobj,
580 struct kobj_attribute *ka, char *buf)
581{
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);
585}
586BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
587
588static ssize_t global_rsv_reserved_show(struct kobject *kobj,
589 struct kobj_attribute *a, char *buf)
590{
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);
594}
595BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
596
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)
599
600static ssize_t raid_bytes_show(struct kobject *kobj,
601 struct kobj_attribute *attr, char *buf);
602BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
603BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
604
605static ssize_t raid_bytes_show(struct kobject *kobj,
606 struct kobj_attribute *attr, char *buf)
607
608{
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);
612 u64 val = 0;
613
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;
618 else
619 val += block_group->used;
620 }
621 up_read(&sinfo->groups_sem);
622 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
623}
624
625static struct attribute *raid_attrs[] = {
626 BTRFS_ATTR_PTR(raid, total_bytes),
627 BTRFS_ATTR_PTR(raid, used_bytes),
628 NULL
629};
630ATTRIBUTE_GROUPS(raid);
631
632static void release_raid_kobj(struct kobject *kobj)
633{
634 kfree(to_raid_kobj(kobj));
635}
636
637static struct kobj_type btrfs_raid_ktype = {
638 .sysfs_ops = &kobj_sysfs_ops,
639 .release = release_raid_kobj,
640 .default_groups = raid_groups,
641};
642
643#define SPACE_INFO_ATTR(field) \
644static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
645 struct kobj_attribute *a, \
646 char *buf) \
647{ \
648 struct btrfs_space_info *sinfo = to_space_info(kobj); \
649 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
650} \
651BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
652
653static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
654 struct kobj_attribute *a,
655 char *buf)
656{
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);
660}
661
662SPACE_INFO_ATTR(flags);
663SPACE_INFO_ATTR(total_bytes);
664SPACE_INFO_ATTR(bytes_used);
665SPACE_INFO_ATTR(bytes_pinned);
666SPACE_INFO_ATTR(bytes_reserved);
667SPACE_INFO_ATTR(bytes_may_use);
668SPACE_INFO_ATTR(bytes_readonly);
669SPACE_INFO_ATTR(bytes_zone_unusable);
670SPACE_INFO_ATTR(disk_used);
671SPACE_INFO_ATTR(disk_total);
672BTRFS_ATTR(space_info, total_bytes_pinned,
673 btrfs_space_info_show_total_bytes_pinned);
674
675static struct attribute *space_info_attrs[] = {
676 BTRFS_ATTR_PTR(space_info, flags),
677 BTRFS_ATTR_PTR(space_info, total_bytes),
678 BTRFS_ATTR_PTR(space_info, bytes_used),
679 BTRFS_ATTR_PTR(space_info, bytes_pinned),
680 BTRFS_ATTR_PTR(space_info, bytes_reserved),
681 BTRFS_ATTR_PTR(space_info, bytes_may_use),
682 BTRFS_ATTR_PTR(space_info, bytes_readonly),
683 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
684 BTRFS_ATTR_PTR(space_info, disk_used),
685 BTRFS_ATTR_PTR(space_info, disk_total),
686 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
687 NULL,
688};
689ATTRIBUTE_GROUPS(space_info);
690
691static void space_info_release(struct kobject *kobj)
692{
693 struct btrfs_space_info *sinfo = to_space_info(kobj);
694 percpu_counter_destroy(&sinfo->total_bytes_pinned);
695 kfree(sinfo);
696}
697
698static struct kobj_type space_info_ktype = {
699 .sysfs_ops = &kobj_sysfs_ops,
700 .release = space_info_release,
701 .default_groups = space_info_groups,
702};
703
704static const struct attribute *allocation_attrs[] = {
705 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
706 BTRFS_ATTR_PTR(allocation, global_rsv_size),
707 NULL,
708};
709
710static ssize_t btrfs_label_show(struct kobject *kobj,
711 struct kobj_attribute *a, char *buf)
712{
713 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
714 char *label = fs_info->super_copy->label;
715 ssize_t ret;
716
717 spin_lock(&fs_info->super_lock);
718 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
719 spin_unlock(&fs_info->super_lock);
720
721 return ret;
722}
723
724static ssize_t btrfs_label_store(struct kobject *kobj,
725 struct kobj_attribute *a,
726 const char *buf, size_t len)
727{
728 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
729 size_t p_len;
730
731 if (!fs_info)
732 return -EPERM;
733
734 if (sb_rdonly(fs_info->sb))
735 return -EROFS;
736
737
738
739
740
741 p_len = strcspn(buf, "\n");
742
743 if (p_len >= BTRFS_LABEL_SIZE)
744 return -EINVAL;
745
746 spin_lock(&fs_info->super_lock);
747 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
748 memcpy(fs_info->super_copy->label, buf, p_len);
749 spin_unlock(&fs_info->super_lock);
750
751
752
753
754 btrfs_set_pending(fs_info, COMMIT);
755 wake_up_process(fs_info->transaction_kthread);
756
757 return len;
758}
759BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
760
761static ssize_t btrfs_nodesize_show(struct kobject *kobj,
762 struct kobj_attribute *a, char *buf)
763{
764 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
765
766 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
767}
768
769BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
770
771static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
772 struct kobj_attribute *a, char *buf)
773{
774 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
775
776 return scnprintf(buf, PAGE_SIZE, "%u\n",
777 fs_info->super_copy->sectorsize);
778}
779
780BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
781
782static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
783 struct kobj_attribute *a, char *buf)
784{
785 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
786
787 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
788}
789
790BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
791
792static ssize_t quota_override_show(struct kobject *kobj,
793 struct kobj_attribute *a, char *buf)
794{
795 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
796 int quota_override;
797
798 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
799 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
800}
801
802static ssize_t quota_override_store(struct kobject *kobj,
803 struct kobj_attribute *a,
804 const char *buf, size_t len)
805{
806 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
807 unsigned long knob;
808 int err;
809
810 if (!fs_info)
811 return -EPERM;
812
813 if (!capable(CAP_SYS_RESOURCE))
814 return -EPERM;
815
816 err = kstrtoul(buf, 10, &knob);
817 if (err)
818 return err;
819 if (knob > 1)
820 return -EINVAL;
821
822 if (knob)
823 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
824 else
825 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
826
827 return len;
828}
829
830BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
831
832static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
833 struct kobj_attribute *a, char *buf)
834{
835 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
836
837 return scnprintf(buf, PAGE_SIZE, "%pU\n",
838 fs_info->fs_devices->metadata_uuid);
839}
840
841BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
842
843static ssize_t btrfs_checksum_show(struct kobject *kobj,
844 struct kobj_attribute *a, char *buf)
845{
846 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
847 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
848
849 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
850 btrfs_super_csum_name(csum_type),
851 crypto_shash_driver_name(fs_info->csum_shash));
852}
853
854BTRFS_ATTR(, checksum, btrfs_checksum_show);
855
856static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
857 struct kobj_attribute *a, char *buf)
858{
859 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
860 const char *str;
861
862 switch (READ_ONCE(fs_info->exclusive_operation)) {
863 case BTRFS_EXCLOP_NONE:
864 str = "none\n";
865 break;
866 case BTRFS_EXCLOP_BALANCE:
867 str = "balance\n";
868 break;
869 case BTRFS_EXCLOP_DEV_ADD:
870 str = "device add\n";
871 break;
872 case BTRFS_EXCLOP_DEV_REMOVE:
873 str = "device remove\n";
874 break;
875 case BTRFS_EXCLOP_DEV_REPLACE:
876 str = "device replace\n";
877 break;
878 case BTRFS_EXCLOP_RESIZE:
879 str = "resize\n";
880 break;
881 case BTRFS_EXCLOP_SWAP_ACTIVATE:
882 str = "swap activate\n";
883 break;
884 default:
885 str = "UNKNOWN\n";
886 break;
887 }
888 return scnprintf(buf, PAGE_SIZE, "%s", str);
889}
890BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
891
892static ssize_t btrfs_generation_show(struct kobject *kobj,
893 struct kobj_attribute *a, char *buf)
894{
895 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
896
897 return scnprintf(buf, PAGE_SIZE, "%llu\n", fs_info->generation);
898}
899BTRFS_ATTR(, generation, btrfs_generation_show);
900
901
902
903
904
905static bool strmatch(const char *buffer, const char *string)
906{
907 const size_t len = strlen(string);
908
909
910 buffer = skip_spaces(buffer);
911
912
913 if (strncmp(string, buffer, len) == 0 &&
914 strlen(skip_spaces(buffer + len)) == 0)
915 return true;
916
917 return false;
918}
919
920static const char * const btrfs_read_policy_name[] = { "pid" };
921
922static ssize_t btrfs_read_policy_show(struct kobject *kobj,
923 struct kobj_attribute *a, char *buf)
924{
925 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
926 ssize_t ret = 0;
927 int i;
928
929 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
930 if (fs_devices->read_policy == i)
931 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
932 (ret == 0 ? "" : " "),
933 btrfs_read_policy_name[i]);
934 else
935 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
936 (ret == 0 ? "" : " "),
937 btrfs_read_policy_name[i]);
938 }
939
940 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
941
942 return ret;
943}
944
945static ssize_t btrfs_read_policy_store(struct kobject *kobj,
946 struct kobj_attribute *a,
947 const char *buf, size_t len)
948{
949 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
950 int i;
951
952 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
953 if (strmatch(buf, btrfs_read_policy_name[i])) {
954 if (i != fs_devices->read_policy) {
955 fs_devices->read_policy = i;
956 btrfs_info(fs_devices->fs_info,
957 "read policy set to '%s'",
958 btrfs_read_policy_name[i]);
959 }
960 return len;
961 }
962 }
963
964 return -EINVAL;
965}
966BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
967
968static const struct attribute *btrfs_attrs[] = {
969 BTRFS_ATTR_PTR(, label),
970 BTRFS_ATTR_PTR(, nodesize),
971 BTRFS_ATTR_PTR(, sectorsize),
972 BTRFS_ATTR_PTR(, clone_alignment),
973 BTRFS_ATTR_PTR(, quota_override),
974 BTRFS_ATTR_PTR(, metadata_uuid),
975 BTRFS_ATTR_PTR(, checksum),
976 BTRFS_ATTR_PTR(, exclusive_operation),
977 BTRFS_ATTR_PTR(, generation),
978 BTRFS_ATTR_PTR(, read_policy),
979 NULL,
980};
981
982static void btrfs_release_fsid_kobj(struct kobject *kobj)
983{
984 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
985
986 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
987 complete(&fs_devs->kobj_unregister);
988}
989
990static struct kobj_type btrfs_ktype = {
991 .sysfs_ops = &kobj_sysfs_ops,
992 .release = btrfs_release_fsid_kobj,
993};
994
995static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
996{
997 if (kobj->ktype != &btrfs_ktype)
998 return NULL;
999 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1000}
1001
1002static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1003{
1004 if (kobj->ktype != &btrfs_ktype)
1005 return NULL;
1006 return to_fs_devs(kobj)->fs_info;
1007}
1008
1009#define NUM_FEATURE_BITS 64
1010#define BTRFS_FEATURE_NAME_MAX 13
1011static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1012static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1013
1014static const u64 supported_feature_masks[FEAT_MAX] = {
1015 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1016 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1017 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1018};
1019
1020static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1021{
1022 int set;
1023
1024 for (set = 0; set < FEAT_MAX; set++) {
1025 int i;
1026 struct attribute *attrs[2];
1027 struct attribute_group agroup = {
1028 .name = "features",
1029 .attrs = attrs,
1030 };
1031 u64 features = get_features(fs_info, set);
1032 features &= ~supported_feature_masks[set];
1033
1034 if (!features)
1035 continue;
1036
1037 attrs[1] = NULL;
1038 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1039 struct btrfs_feature_attr *fa;
1040
1041 if (!(features & (1ULL << i)))
1042 continue;
1043
1044 fa = &btrfs_feature_attrs[set][i];
1045 attrs[0] = &fa->kobj_attr.attr;
1046 if (add) {
1047 int ret;
1048 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1049 &agroup);
1050 if (ret)
1051 return ret;
1052 } else
1053 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1054 &agroup);
1055 }
1056
1057 }
1058 return 0;
1059}
1060
1061static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1062{
1063 if (fs_devs->devinfo_kobj) {
1064 kobject_del(fs_devs->devinfo_kobj);
1065 kobject_put(fs_devs->devinfo_kobj);
1066 fs_devs->devinfo_kobj = NULL;
1067 }
1068
1069 if (fs_devs->devices_kobj) {
1070 kobject_del(fs_devs->devices_kobj);
1071 kobject_put(fs_devs->devices_kobj);
1072 fs_devs->devices_kobj = NULL;
1073 }
1074
1075 if (fs_devs->fsid_kobj.state_initialized) {
1076 kobject_del(&fs_devs->fsid_kobj);
1077 kobject_put(&fs_devs->fsid_kobj);
1078 wait_for_completion(&fs_devs->kobj_unregister);
1079 }
1080}
1081
1082
1083void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1084{
1085 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1086
1087 if (fs_devs) {
1088 __btrfs_sysfs_remove_fsid(fs_devs);
1089 return;
1090 }
1091
1092 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1093 __btrfs_sysfs_remove_fsid(fs_devs);
1094 }
1095}
1096
1097static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1098{
1099 struct btrfs_device *device;
1100 struct btrfs_fs_devices *seed;
1101
1102 list_for_each_entry(device, &fs_devices->devices, dev_list)
1103 btrfs_sysfs_remove_device(device);
1104
1105 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1106 list_for_each_entry(device, &seed->devices, dev_list)
1107 btrfs_sysfs_remove_device(device);
1108 }
1109}
1110
1111void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1112{
1113 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1114
1115 sysfs_remove_link(fsid_kobj, "bdi");
1116
1117 if (fs_info->space_info_kobj) {
1118 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1119 kobject_del(fs_info->space_info_kobj);
1120 kobject_put(fs_info->space_info_kobj);
1121 }
1122#ifdef CONFIG_BTRFS_DEBUG
1123 if (fs_info->discard_debug_kobj) {
1124 sysfs_remove_files(fs_info->discard_debug_kobj,
1125 discard_debug_attrs);
1126 kobject_del(fs_info->discard_debug_kobj);
1127 kobject_put(fs_info->discard_debug_kobj);
1128 }
1129 if (fs_info->debug_kobj) {
1130 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1131 kobject_del(fs_info->debug_kobj);
1132 kobject_put(fs_info->debug_kobj);
1133 }
1134#endif
1135 addrm_unknown_feature_attrs(fs_info, false);
1136 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1137 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1138 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1139}
1140
1141static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1142 [FEAT_COMPAT] = "compat",
1143 [FEAT_COMPAT_RO] = "compat_ro",
1144 [FEAT_INCOMPAT] = "incompat",
1145};
1146
1147const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1148{
1149 return btrfs_feature_set_names[set];
1150}
1151
1152char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1153{
1154 size_t bufsize = 4096;
1155 int len = 0;
1156 int i;
1157 char *str;
1158
1159 str = kmalloc(bufsize, GFP_KERNEL);
1160 if (!str)
1161 return str;
1162
1163 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1164 const char *name;
1165
1166 if (!(flags & (1ULL << i)))
1167 continue;
1168
1169 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1170 len += scnprintf(str + len, bufsize - len, "%s%s",
1171 len ? "," : "", name);
1172 }
1173
1174 return str;
1175}
1176
1177static void init_feature_attrs(void)
1178{
1179 struct btrfs_feature_attr *fa;
1180 int set, i;
1181
1182 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1183 ARRAY_SIZE(btrfs_feature_attrs));
1184 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1185 ARRAY_SIZE(btrfs_feature_attrs[0]));
1186
1187 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1188 memset(btrfs_unknown_feature_names, 0,
1189 sizeof(btrfs_unknown_feature_names));
1190
1191 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1192 struct btrfs_feature_attr *sfa;
1193 struct attribute *a = btrfs_supported_feature_attrs[i];
1194 int bit;
1195 sfa = attr_to_btrfs_feature_attr(a);
1196 bit = ilog2(sfa->feature_bit);
1197 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1198
1199 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1200 }
1201
1202 for (set = 0; set < FEAT_MAX; set++) {
1203 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1204 char *name = btrfs_unknown_feature_names[set][i];
1205 fa = &btrfs_feature_attrs[set][i];
1206
1207 if (fa->kobj_attr.attr.name)
1208 continue;
1209
1210 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1211 btrfs_feature_set_names[set], i);
1212
1213 fa->kobj_attr.attr.name = name;
1214 fa->kobj_attr.attr.mode = S_IRUGO;
1215 fa->feature_set = set;
1216 fa->feature_bit = 1ULL << i;
1217 }
1218 }
1219}
1220
1221
1222
1223
1224
1225void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1226{
1227 struct btrfs_fs_info *fs_info = cache->fs_info;
1228 struct btrfs_space_info *space_info = cache->space_info;
1229 struct raid_kobject *rkobj;
1230 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1231 unsigned int nofs_flag;
1232 int ret;
1233
1234
1235
1236
1237
1238
1239
1240
1241 nofs_flag = memalloc_nofs_save();
1242
1243 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1244 if (!rkobj) {
1245 memalloc_nofs_restore(nofs_flag);
1246 btrfs_warn(cache->fs_info,
1247 "couldn't alloc memory for raid level kobject");
1248 return;
1249 }
1250
1251 rkobj->flags = cache->flags;
1252 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263 spin_lock(&space_info->lock);
1264 if (space_info->block_group_kobjs[index]) {
1265 spin_unlock(&space_info->lock);
1266 kobject_put(&rkobj->kobj);
1267 return;
1268 } else {
1269 space_info->block_group_kobjs[index] = &rkobj->kobj;
1270 }
1271 spin_unlock(&space_info->lock);
1272
1273 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1274 btrfs_bg_type_to_raid_name(rkobj->flags));
1275 memalloc_nofs_restore(nofs_flag);
1276 if (ret) {
1277 spin_lock(&space_info->lock);
1278 space_info->block_group_kobjs[index] = NULL;
1279 spin_unlock(&space_info->lock);
1280 kobject_put(&rkobj->kobj);
1281 btrfs_warn(fs_info,
1282 "failed to add kobject for block cache, ignoring");
1283 return;
1284 }
1285}
1286
1287
1288
1289
1290
1291void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1292{
1293 int i;
1294
1295 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1296 struct kobject *kobj;
1297
1298 kobj = space_info->block_group_kobjs[i];
1299 space_info->block_group_kobjs[i] = NULL;
1300 if (kobj) {
1301 kobject_del(kobj);
1302 kobject_put(kobj);
1303 }
1304 }
1305 kobject_del(&space_info->kobj);
1306 kobject_put(&space_info->kobj);
1307}
1308
1309static const char *alloc_name(u64 flags)
1310{
1311 switch (flags) {
1312 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1313 return "mixed";
1314 case BTRFS_BLOCK_GROUP_METADATA:
1315 return "metadata";
1316 case BTRFS_BLOCK_GROUP_DATA:
1317 return "data";
1318 case BTRFS_BLOCK_GROUP_SYSTEM:
1319 return "system";
1320 default:
1321 WARN_ON(1);
1322 return "invalid-combination";
1323 }
1324}
1325
1326
1327
1328
1329
1330int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1331 struct btrfs_space_info *space_info)
1332{
1333 int ret;
1334
1335 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1336 fs_info->space_info_kobj, "%s",
1337 alloc_name(space_info->flags));
1338 if (ret) {
1339 kobject_put(&space_info->kobj);
1340 return ret;
1341 }
1342
1343 return 0;
1344}
1345
1346void btrfs_sysfs_remove_device(struct btrfs_device *device)
1347{
1348 struct kobject *devices_kobj;
1349
1350
1351
1352
1353
1354 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1355 ASSERT(devices_kobj);
1356
1357 if (device->bdev)
1358 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1359
1360 if (device->devid_kobj.state_initialized) {
1361 kobject_del(&device->devid_kobj);
1362 kobject_put(&device->devid_kobj);
1363 wait_for_completion(&device->kobj_unregister);
1364 }
1365}
1366
1367static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1368 struct kobj_attribute *a,
1369 char *buf)
1370{
1371 int val;
1372 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1373 devid_kobj);
1374
1375 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1376
1377 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1378}
1379BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1380
1381static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1382 struct kobj_attribute *a, char *buf)
1383{
1384 int val;
1385 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1386 devid_kobj);
1387
1388 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1389
1390 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1391}
1392BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1393
1394static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1395 struct kobj_attribute *a,
1396 char *buf)
1397{
1398 int val;
1399 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1400 devid_kobj);
1401
1402 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1403
1404 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1405}
1406BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1407
1408static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1409 struct kobj_attribute *a, char *buf)
1410{
1411 int val;
1412 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1413 devid_kobj);
1414
1415 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1416
1417 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1418}
1419BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1420
1421static struct attribute *devid_attrs[] = {
1422 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1423 BTRFS_ATTR_PTR(devid, missing),
1424 BTRFS_ATTR_PTR(devid, replace_target),
1425 BTRFS_ATTR_PTR(devid, writeable),
1426 NULL
1427};
1428ATTRIBUTE_GROUPS(devid);
1429
1430static void btrfs_release_devid_kobj(struct kobject *kobj)
1431{
1432 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1433 devid_kobj);
1434
1435 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1436 complete(&device->kobj_unregister);
1437}
1438
1439static struct kobj_type devid_ktype = {
1440 .sysfs_ops = &kobj_sysfs_ops,
1441 .default_groups = devid_groups,
1442 .release = btrfs_release_devid_kobj,
1443};
1444
1445int btrfs_sysfs_add_device(struct btrfs_device *device)
1446{
1447 int ret;
1448 unsigned int nofs_flag;
1449 struct kobject *devices_kobj;
1450 struct kobject *devinfo_kobj;
1451
1452
1453
1454
1455
1456 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1457 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1458 ASSERT(devices_kobj);
1459 ASSERT(devinfo_kobj);
1460
1461 nofs_flag = memalloc_nofs_save();
1462
1463 if (device->bdev) {
1464 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1465
1466 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1467 if (ret) {
1468 btrfs_warn(device->fs_info,
1469 "creating sysfs device link for devid %llu failed: %d",
1470 device->devid, ret);
1471 goto out;
1472 }
1473 }
1474
1475 init_completion(&device->kobj_unregister);
1476 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1477 devinfo_kobj, "%llu", device->devid);
1478 if (ret) {
1479 kobject_put(&device->devid_kobj);
1480 btrfs_warn(device->fs_info,
1481 "devinfo init for devid %llu failed: %d",
1482 device->devid, ret);
1483 }
1484
1485out:
1486 memalloc_nofs_restore(nofs_flag);
1487 return ret;
1488}
1489
1490static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1491{
1492 int ret;
1493 struct btrfs_device *device;
1494 struct btrfs_fs_devices *seed;
1495
1496 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1497 ret = btrfs_sysfs_add_device(device);
1498 if (ret)
1499 goto fail;
1500 }
1501
1502 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1503 list_for_each_entry(device, &seed->devices, dev_list) {
1504 ret = btrfs_sysfs_add_device(device);
1505 if (ret)
1506 goto fail;
1507 }
1508 }
1509
1510 return 0;
1511
1512fail:
1513 btrfs_sysfs_remove_fs_devices(fs_devices);
1514 return ret;
1515}
1516
1517void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1518{
1519 int ret;
1520
1521 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1522 if (ret)
1523 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1524 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1525 &disk_to_dev(bdev->bd_disk)->kobj);
1526}
1527
1528void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1529
1530{
1531 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1532
1533
1534
1535
1536
1537 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1538 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1539 btrfs_warn(fs_devices->fs_info,
1540 "sysfs: failed to create fsid for sprout");
1541}
1542
1543void btrfs_sysfs_update_devid(struct btrfs_device *device)
1544{
1545 char tmp[24];
1546
1547 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1548
1549 if (kobject_rename(&device->devid_kobj, tmp))
1550 btrfs_warn(device->fs_devices->fs_info,
1551 "sysfs: failed to update devid for %llu",
1552 device->devid);
1553}
1554
1555
1556static struct kset *btrfs_kset;
1557
1558
1559
1560
1561
1562
1563
1564int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1565{
1566 int error;
1567
1568 init_completion(&fs_devs->kobj_unregister);
1569 fs_devs->fsid_kobj.kset = btrfs_kset;
1570 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1571 "%pU", fs_devs->fsid);
1572 if (error) {
1573 kobject_put(&fs_devs->fsid_kobj);
1574 return error;
1575 }
1576
1577 fs_devs->devices_kobj = kobject_create_and_add("devices",
1578 &fs_devs->fsid_kobj);
1579 if (!fs_devs->devices_kobj) {
1580 btrfs_err(fs_devs->fs_info,
1581 "failed to init sysfs device interface");
1582 btrfs_sysfs_remove_fsid(fs_devs);
1583 return -ENOMEM;
1584 }
1585
1586 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1587 &fs_devs->fsid_kobj);
1588 if (!fs_devs->devinfo_kobj) {
1589 btrfs_err(fs_devs->fs_info,
1590 "failed to init sysfs devinfo kobject");
1591 btrfs_sysfs_remove_fsid(fs_devs);
1592 return -ENOMEM;
1593 }
1594
1595 return 0;
1596}
1597
1598int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1599{
1600 int error;
1601 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1602 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1603
1604 error = btrfs_sysfs_add_fs_devices(fs_devs);
1605 if (error)
1606 return error;
1607
1608 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1609 if (error) {
1610 btrfs_sysfs_remove_fs_devices(fs_devs);
1611 return error;
1612 }
1613
1614 error = sysfs_create_group(fsid_kobj,
1615 &btrfs_feature_attr_group);
1616 if (error)
1617 goto failure;
1618
1619#ifdef CONFIG_BTRFS_DEBUG
1620 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1621 if (!fs_info->debug_kobj) {
1622 error = -ENOMEM;
1623 goto failure;
1624 }
1625
1626 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1627 if (error)
1628 goto failure;
1629
1630
1631 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1632 fs_info->debug_kobj);
1633 if (!fs_info->discard_debug_kobj) {
1634 error = -ENOMEM;
1635 goto failure;
1636 }
1637
1638 error = sysfs_create_files(fs_info->discard_debug_kobj,
1639 discard_debug_attrs);
1640 if (error)
1641 goto failure;
1642#endif
1643
1644 error = addrm_unknown_feature_attrs(fs_info, true);
1645 if (error)
1646 goto failure;
1647
1648 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1649 if (error)
1650 goto failure;
1651
1652 fs_info->space_info_kobj = kobject_create_and_add("allocation",
1653 fsid_kobj);
1654 if (!fs_info->space_info_kobj) {
1655 error = -ENOMEM;
1656 goto failure;
1657 }
1658
1659 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1660 if (error)
1661 goto failure;
1662
1663 return 0;
1664failure:
1665 btrfs_sysfs_remove_mounted(fs_info);
1666 return error;
1667}
1668
1669static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1670{
1671 return to_fs_info(kobj->parent->parent);
1672}
1673
1674#define QGROUP_ATTR(_member, _show_name) \
1675static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1676 struct kobj_attribute *a, \
1677 char *buf) \
1678{ \
1679 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1680 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1681 struct btrfs_qgroup, kobj); \
1682 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1683} \
1684BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1685
1686#define QGROUP_RSV_ATTR(_name, _type) \
1687static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1688 struct kobj_attribute *a, \
1689 char *buf) \
1690{ \
1691 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1692 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1693 struct btrfs_qgroup, kobj); \
1694 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1695 &fs_info->qgroup_lock, buf); \
1696} \
1697BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1698
1699QGROUP_ATTR(rfer, referenced);
1700QGROUP_ATTR(excl, exclusive);
1701QGROUP_ATTR(max_rfer, max_referenced);
1702QGROUP_ATTR(max_excl, max_exclusive);
1703QGROUP_ATTR(lim_flags, limit_flags);
1704QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1705QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1706QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1707
1708static struct attribute *qgroup_attrs[] = {
1709 BTRFS_ATTR_PTR(qgroup, referenced),
1710 BTRFS_ATTR_PTR(qgroup, exclusive),
1711 BTRFS_ATTR_PTR(qgroup, max_referenced),
1712 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1713 BTRFS_ATTR_PTR(qgroup, limit_flags),
1714 BTRFS_ATTR_PTR(qgroup, rsv_data),
1715 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1716 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1717 NULL
1718};
1719ATTRIBUTE_GROUPS(qgroup);
1720
1721static void qgroup_release(struct kobject *kobj)
1722{
1723 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1724
1725 memset(&qgroup->kobj, 0, sizeof(*kobj));
1726}
1727
1728static struct kobj_type qgroup_ktype = {
1729 .sysfs_ops = &kobj_sysfs_ops,
1730 .release = qgroup_release,
1731 .default_groups = qgroup_groups,
1732};
1733
1734int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1735 struct btrfs_qgroup *qgroup)
1736{
1737 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1738 int ret;
1739
1740 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1741 return 0;
1742 if (qgroup->kobj.state_initialized)
1743 return 0;
1744 if (!qgroups_kobj)
1745 return -EINVAL;
1746
1747 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1748 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1749 btrfs_qgroup_subvolid(qgroup->qgroupid));
1750 if (ret < 0)
1751 kobject_put(&qgroup->kobj);
1752
1753 return ret;
1754}
1755
1756void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1757{
1758 struct btrfs_qgroup *qgroup;
1759 struct btrfs_qgroup *next;
1760
1761 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1762 return;
1763
1764 rbtree_postorder_for_each_entry_safe(qgroup, next,
1765 &fs_info->qgroup_tree, node)
1766 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1767 if (fs_info->qgroups_kobj) {
1768 kobject_del(fs_info->qgroups_kobj);
1769 kobject_put(fs_info->qgroups_kobj);
1770 fs_info->qgroups_kobj = NULL;
1771 }
1772}
1773
1774
1775int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1776{
1777 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1778 struct btrfs_qgroup *qgroup;
1779 struct btrfs_qgroup *next;
1780 int ret = 0;
1781
1782 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1783 return 0;
1784
1785 ASSERT(fsid_kobj);
1786 if (fs_info->qgroups_kobj)
1787 return 0;
1788
1789 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1790 if (!fs_info->qgroups_kobj) {
1791 ret = -ENOMEM;
1792 goto out;
1793 }
1794 rbtree_postorder_for_each_entry_safe(qgroup, next,
1795 &fs_info->qgroup_tree, node) {
1796 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1797 if (ret < 0)
1798 goto out;
1799 }
1800
1801out:
1802 if (ret < 0)
1803 btrfs_sysfs_del_qgroups(fs_info);
1804 return ret;
1805}
1806
1807void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1808 struct btrfs_qgroup *qgroup)
1809{
1810 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1811 return;
1812
1813 if (qgroup->kobj.state_initialized) {
1814 kobject_del(&qgroup->kobj);
1815 kobject_put(&qgroup->kobj);
1816 }
1817}
1818
1819
1820
1821
1822
1823void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1824 u64 bit, enum btrfs_feature_set set)
1825{
1826 struct btrfs_fs_devices *fs_devs;
1827 struct kobject *fsid_kobj;
1828 u64 __maybe_unused features;
1829 int __maybe_unused ret;
1830
1831 if (!fs_info)
1832 return;
1833
1834
1835
1836
1837
1838 features = get_features(fs_info, set);
1839 ASSERT(bit & supported_feature_masks[set]);
1840
1841 fs_devs = fs_info->fs_devices;
1842 fsid_kobj = &fs_devs->fsid_kobj;
1843
1844 if (!fsid_kobj->state_initialized)
1845 return;
1846
1847
1848
1849
1850
1851 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1852 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1853}
1854
1855int __init btrfs_init_sysfs(void)
1856{
1857 int ret;
1858
1859 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1860 if (!btrfs_kset)
1861 return -ENOMEM;
1862
1863 init_feature_attrs();
1864 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1865 if (ret)
1866 goto out2;
1867 ret = sysfs_merge_group(&btrfs_kset->kobj,
1868 &btrfs_static_feature_attr_group);
1869 if (ret)
1870 goto out_remove_group;
1871
1872#ifdef CONFIG_BTRFS_DEBUG
1873 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1874 if (ret)
1875 goto out2;
1876#endif
1877
1878 return 0;
1879
1880out_remove_group:
1881 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1882out2:
1883 kset_unregister(btrfs_kset);
1884
1885 return ret;
1886}
1887
1888void __cold btrfs_exit_sysfs(void)
1889{
1890 sysfs_unmerge_group(&btrfs_kset->kobj,
1891 &btrfs_static_feature_attr_group);
1892 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1893#ifdef CONFIG_BTRFS_DEBUG
1894 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1895#endif
1896 kset_unregister(btrfs_kset);
1897}
1898
1899