]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/btrfs/sysfs.c
btrfs: track discardable extents for async discard
[mirror_ubuntu-hirsute-kernel.git] / fs / btrfs / sysfs.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
58176a96 6#include <linux/sched.h>
32a9991f 7#include <linux/sched/mm.h>
58176a96
JB
8#include <linux/slab.h>
9#include <linux/spinlock.h>
10#include <linux/completion.h>
79da4fa4 11#include <linux/bug.h>
41e6d2a8 12#include <crypto/hash.h>
58176a96 13
bae45de0 14#include "ctree.h"
dfb79ddb 15#include "discard.h"
bae45de0
CM
16#include "disk-io.h"
17#include "transaction.h"
079b72bc 18#include "sysfs.h"
29e5be24 19#include "volumes.h"
8719aaae 20#include "space-info.h"
aac0023c 21#include "block-group.h"
079b72bc 22
9188db61
DS
23struct btrfs_feature_attr {
24 struct kobj_attribute kobj_attr;
25 enum btrfs_feature_set feature_set;
26 u64 feature_bit;
27};
28
29/* For raid type sysfs entries */
30struct raid_kobject {
31 u64 flags;
32 struct kobject kobj;
33};
34
35#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
36{ \
37 .attr = { .name = __stringify(_name), .mode = _mode }, \
38 .show = _show, \
39 .store = _store, \
40}
41
42#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
43 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
44 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
45
46#define BTRFS_ATTR(_prefix, _name, _show) \
47 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
48 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
49
50#define BTRFS_ATTR_PTR(_prefix, _name) \
51 (&btrfs_attr_##_prefix##_##_name.attr)
52
53#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
54static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
55 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
56 btrfs_feature_attr_show, \
57 btrfs_feature_attr_store), \
58 .feature_set = _feature_set, \
59 .feature_bit = _feature_prefix ##_## _feature_bit, \
60}
61#define BTRFS_FEAT_ATTR_PTR(_name) \
62 (&btrfs_attr_features_##_name.kobj_attr.attr)
63
64#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
65 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
66#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
68#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
69 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
70
510d7360 71static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
2e7910d6 72static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
5ac1d209 73
8f52316c
DS
74static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
75{
76 return container_of(a, struct btrfs_feature_attr, kobj_attr);
77}
78
79static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
80{
81 return container_of(attr, struct kobj_attribute, attr);
82}
83
84static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
85 struct attribute *attr)
86{
87 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
88}
89
510d7360
JM
90static u64 get_features(struct btrfs_fs_info *fs_info,
91 enum btrfs_feature_set set)
5ac1d209 92{
510d7360
JM
93 struct btrfs_super_block *disk_super = fs_info->super_copy;
94 if (set == FEAT_COMPAT)
95 return btrfs_super_compat_flags(disk_super);
96 else if (set == FEAT_COMPAT_RO)
97 return btrfs_super_compat_ro_flags(disk_super);
98 else
99 return btrfs_super_incompat_flags(disk_super);
5ac1d209
JM
100}
101
ba631941
JM
102static void set_features(struct btrfs_fs_info *fs_info,
103 enum btrfs_feature_set set, u64 features)
104{
105 struct btrfs_super_block *disk_super = fs_info->super_copy;
106 if (set == FEAT_COMPAT)
107 btrfs_set_super_compat_flags(disk_super, features);
108 else if (set == FEAT_COMPAT_RO)
109 btrfs_set_super_compat_ro_flags(disk_super, features);
110 else
111 btrfs_set_super_incompat_flags(disk_super, features);
112}
113
114static int can_modify_feature(struct btrfs_feature_attr *fa)
115{
116 int val = 0;
117 u64 set, clear;
118 switch (fa->feature_set) {
119 case FEAT_COMPAT:
120 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
121 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
122 break;
123 case FEAT_COMPAT_RO:
124 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
125 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
126 break;
127 case FEAT_INCOMPAT:
128 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
129 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
130 break;
131 default:
62e85577 132 pr_warn("btrfs: sysfs: unknown feature set %d\n",
cc37bb04
DS
133 fa->feature_set);
134 return 0;
ba631941
JM
135 }
136
137 if (set & fa->feature_bit)
138 val |= 1;
139 if (clear & fa->feature_bit)
140 val |= 2;
141
142 return val;
143}
144
510d7360
JM
145static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
146 struct kobj_attribute *a, char *buf)
5ac1d209 147{
510d7360 148 int val = 0;
5ac1d209 149 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
ba631941 150 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
510d7360 151 if (fs_info) {
510d7360
JM
152 u64 features = get_features(fs_info, fa->feature_set);
153 if (features & fa->feature_bit)
154 val = 1;
ba631941
JM
155 } else
156 val = can_modify_feature(fa);
510d7360
JM
157
158 return snprintf(buf, PAGE_SIZE, "%d\n", val);
5ac1d209
JM
159}
160
ba631941
JM
161static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
162 struct kobj_attribute *a,
163 const char *buf, size_t count)
164{
165 struct btrfs_fs_info *fs_info;
166 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
ba631941
JM
167 u64 features, set, clear;
168 unsigned long val;
169 int ret;
170
171 fs_info = to_fs_info(kobj);
172 if (!fs_info)
173 return -EPERM;
174
bc98a42c 175 if (sb_rdonly(fs_info->sb))
ee611138
DS
176 return -EROFS;
177
ba631941
JM
178 ret = kstrtoul(skip_spaces(buf), 0, &val);
179 if (ret)
180 return ret;
181
182 if (fa->feature_set == FEAT_COMPAT) {
183 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
184 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
185 } else if (fa->feature_set == FEAT_COMPAT_RO) {
186 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
187 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
188 } else {
189 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
190 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
191 }
192
193 features = get_features(fs_info, fa->feature_set);
194
195 /* Nothing to do */
196 if ((val && (features & fa->feature_bit)) ||
197 (!val && !(features & fa->feature_bit)))
198 return count;
199
200 if ((val && !(set & fa->feature_bit)) ||
201 (!val && !(clear & fa->feature_bit))) {
202 btrfs_info(fs_info,
203 "%sabling feature %s on mounted fs is not supported.",
204 val ? "En" : "Dis", fa->kobj_attr.attr.name);
205 return -EPERM;
206 }
207
208 btrfs_info(fs_info, "%s %s feature flag",
209 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
210
ba631941
JM
211 spin_lock(&fs_info->super_lock);
212 features = get_features(fs_info, fa->feature_set);
213 if (val)
214 features |= fa->feature_bit;
215 else
216 features &= ~fa->feature_bit;
217 set_features(fs_info, fa->feature_set, features);
218 spin_unlock(&fs_info->super_lock);
219
0eae2747
DS
220 /*
221 * We don't want to do full transaction commit from inside sysfs
222 */
223 btrfs_set_pending(fs_info, COMMIT);
224 wake_up_process(fs_info->transaction_kthread);
ba631941
JM
225
226 return count;
227}
228
510d7360
JM
229static umode_t btrfs_feature_visible(struct kobject *kobj,
230 struct attribute *attr, int unused)
079b72bc 231{
510d7360
JM
232 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
233 umode_t mode = attr->mode;
234
235 if (fs_info) {
236 struct btrfs_feature_attr *fa;
237 u64 features;
238
239 fa = attr_to_btrfs_feature_attr(attr);
240 features = get_features(fs_info, fa->feature_set);
241
ba631941
JM
242 if (can_modify_feature(fa))
243 mode |= S_IWUSR;
244 else if (!(features & fa->feature_bit))
510d7360
JM
245 mode = 0;
246 }
247
248 return mode;
079b72bc
JM
249}
250
251BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
252BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
253BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
254BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
5c1aab1d 255BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
079b72bc
JM
256BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
257BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
258BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
259BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
c736c095 260BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
56f20f40 261BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
3b5bb73b 262BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
cfbb825c 263BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
079b72bc
JM
264
265static struct attribute *btrfs_supported_feature_attrs[] = {
266 BTRFS_FEAT_ATTR_PTR(mixed_backref),
267 BTRFS_FEAT_ATTR_PTR(default_subvol),
268 BTRFS_FEAT_ATTR_PTR(mixed_groups),
269 BTRFS_FEAT_ATTR_PTR(compress_lzo),
5c1aab1d 270 BTRFS_FEAT_ATTR_PTR(compress_zstd),
079b72bc
JM
271 BTRFS_FEAT_ATTR_PTR(big_metadata),
272 BTRFS_FEAT_ATTR_PTR(extended_iref),
273 BTRFS_FEAT_ATTR_PTR(raid56),
274 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
c736c095 275 BTRFS_FEAT_ATTR_PTR(no_holes),
56f20f40 276 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
3b5bb73b 277 BTRFS_FEAT_ATTR_PTR(free_space_tree),
cfbb825c 278 BTRFS_FEAT_ATTR_PTR(raid1c34),
079b72bc
JM
279 NULL
280};
281
f902bd3a
MT
282/*
283 * Features which depend on feature bits and may differ between each fs.
284 *
285 * /sys/fs/btrfs/features lists all available features of this kernel while
286 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
287 * can be changed online.
288 */
079b72bc
JM
289static const struct attribute_group btrfs_feature_attr_group = {
290 .name = "features",
510d7360 291 .is_visible = btrfs_feature_visible,
079b72bc
JM
292 .attrs = btrfs_supported_feature_attrs,
293};
58176a96 294
f902bd3a
MT
295static ssize_t rmdir_subvol_show(struct kobject *kobj,
296 struct kobj_attribute *ka, char *buf)
297{
298 return snprintf(buf, PAGE_SIZE, "0\n");
299}
300BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
301
f7cea56c
DS
302static ssize_t supported_checksums_show(struct kobject *kobj,
303 struct kobj_attribute *a, char *buf)
304{
305 ssize_t ret = 0;
306 int i;
307
308 for (i = 0; i < btrfs_get_num_csums(); i++) {
309 /*
310 * This "trick" only works as long as 'enum btrfs_csum_type' has
311 * no holes in it
312 */
313 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
314 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
315
316 }
317
318 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
319 return ret;
320}
321BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
322
f902bd3a
MT
323static struct attribute *btrfs_supported_static_feature_attrs[] = {
324 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
f7cea56c 325 BTRFS_ATTR_PTR(static_feature, supported_checksums),
f902bd3a
MT
326 NULL
327};
328
329/*
330 * Features which only depend on kernel version.
331 *
332 * These are listed in /sys/fs/btrfs/features along with
333 * btrfs_feature_attr_group
334 */
335static const struct attribute_group btrfs_static_feature_attr_group = {
336 .name = "features",
337 .attrs = btrfs_supported_static_feature_attrs,
338};
339
6e369feb
DS
340#ifdef CONFIG_BTRFS_DEBUG
341
e4faab84
DZ
342/*
343 * Discard statistics and tunables
344 */
dfb79ddb
DZ
345#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
346
347static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
348 struct kobj_attribute *a,
349 char *buf)
350{
351 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
352
353 return snprintf(buf, PAGE_SIZE, "%d\n",
354 atomic_read(&fs_info->discard_ctl.discardable_extents));
355}
356BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
357
e4faab84 358static const struct attribute *discard_debug_attrs[] = {
dfb79ddb 359 BTRFS_ATTR_PTR(discard, discardable_extents),
e4faab84
DZ
360 NULL,
361};
362
6e369feb
DS
363/*
364 * Runtime debugging exported via sysfs
365 *
366 * /sys/fs/btrfs/debug - applies to module or all filesystems
367 * /sys/fs/btrfs/UUID - applies only to the given filesystem
368 */
93945cb4
DZ
369static const struct attribute *btrfs_debug_mount_attrs[] = {
370 NULL,
371};
372
6e369feb
DS
373static struct attribute *btrfs_debug_feature_attrs[] = {
374 NULL
375};
376
377static const struct attribute_group btrfs_debug_feature_attr_group = {
378 .name = "debug",
379 .attrs = btrfs_debug_feature_attrs,
380};
381
382#endif
383
6ab0a202
JM
384static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
385{
386 u64 val;
387 if (lock)
388 spin_lock(lock);
389 val = *value_ptr;
390 if (lock)
391 spin_unlock(lock);
392 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
393}
394
395static ssize_t global_rsv_size_show(struct kobject *kobj,
396 struct kobj_attribute *ka, char *buf)
397{
398 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
399 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
400 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
401}
a969f4cc 402BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
6ab0a202
JM
403
404static ssize_t global_rsv_reserved_show(struct kobject *kobj,
405 struct kobj_attribute *a, char *buf)
406{
407 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
408 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
409 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
410}
a969f4cc 411BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
6ab0a202
JM
412
413#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
c1895442 414#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
6ab0a202
JM
415
416static ssize_t raid_bytes_show(struct kobject *kobj,
417 struct kobj_attribute *attr, char *buf);
a969f4cc
HK
418BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
419BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
6ab0a202
JM
420
421static ssize_t raid_bytes_show(struct kobject *kobj,
422 struct kobj_attribute *attr, char *buf)
423
424{
425 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
32da5386 426 struct btrfs_block_group *block_group;
75cb379d 427 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
6ab0a202
JM
428 u64 val = 0;
429
430 down_read(&sinfo->groups_sem);
431 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
a969f4cc 432 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
b3470b5d 433 val += block_group->length;
6ab0a202 434 else
bf38be65 435 val += block_group->used;
6ab0a202
JM
436 }
437 up_read(&sinfo->groups_sem);
438 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
439}
440
7c7e3014 441static struct attribute *raid_attrs[] = {
a969f4cc
HK
442 BTRFS_ATTR_PTR(raid, total_bytes),
443 BTRFS_ATTR_PTR(raid, used_bytes),
6ab0a202
JM
444 NULL
445};
7c7e3014 446ATTRIBUTE_GROUPS(raid);
6ab0a202
JM
447
448static void release_raid_kobj(struct kobject *kobj)
449{
c1895442 450 kfree(to_raid_kobj(kobj));
6ab0a202
JM
451}
452
536ea45c 453static struct kobj_type btrfs_raid_ktype = {
6ab0a202
JM
454 .sysfs_ops = &kobj_sysfs_ops,
455 .release = release_raid_kobj,
7c7e3014 456 .default_groups = raid_groups,
6ab0a202
JM
457};
458
459#define SPACE_INFO_ATTR(field) \
460static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
461 struct kobj_attribute *a, \
462 char *buf) \
463{ \
464 struct btrfs_space_info *sinfo = to_space_info(kobj); \
465 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
466} \
a969f4cc 467BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
6ab0a202
JM
468
469static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
470 struct kobj_attribute *a,
471 char *buf)
472{
473 struct btrfs_space_info *sinfo = to_space_info(kobj);
474 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
475 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
476}
477
478SPACE_INFO_ATTR(flags);
479SPACE_INFO_ATTR(total_bytes);
480SPACE_INFO_ATTR(bytes_used);
481SPACE_INFO_ATTR(bytes_pinned);
482SPACE_INFO_ATTR(bytes_reserved);
483SPACE_INFO_ATTR(bytes_may_use);
c1fd5c30 484SPACE_INFO_ATTR(bytes_readonly);
6ab0a202
JM
485SPACE_INFO_ATTR(disk_used);
486SPACE_INFO_ATTR(disk_total);
a969f4cc
HK
487BTRFS_ATTR(space_info, total_bytes_pinned,
488 btrfs_space_info_show_total_bytes_pinned);
6ab0a202
JM
489
490static struct attribute *space_info_attrs[] = {
a969f4cc
HK
491 BTRFS_ATTR_PTR(space_info, flags),
492 BTRFS_ATTR_PTR(space_info, total_bytes),
493 BTRFS_ATTR_PTR(space_info, bytes_used),
494 BTRFS_ATTR_PTR(space_info, bytes_pinned),
495 BTRFS_ATTR_PTR(space_info, bytes_reserved),
496 BTRFS_ATTR_PTR(space_info, bytes_may_use),
497 BTRFS_ATTR_PTR(space_info, bytes_readonly),
498 BTRFS_ATTR_PTR(space_info, disk_used),
499 BTRFS_ATTR_PTR(space_info, disk_total),
500 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
6ab0a202
JM
501 NULL,
502};
7c7e3014 503ATTRIBUTE_GROUPS(space_info);
6ab0a202
JM
504
505static void space_info_release(struct kobject *kobj)
506{
507 struct btrfs_space_info *sinfo = to_space_info(kobj);
508 percpu_counter_destroy(&sinfo->total_bytes_pinned);
509 kfree(sinfo);
510}
511
27992d01 512static struct kobj_type space_info_ktype = {
6ab0a202
JM
513 .sysfs_ops = &kobj_sysfs_ops,
514 .release = space_info_release,
7c7e3014 515 .default_groups = space_info_groups,
6ab0a202
JM
516};
517
518static const struct attribute *allocation_attrs[] = {
a969f4cc
HK
519 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
520 BTRFS_ATTR_PTR(allocation, global_rsv_size),
6ab0a202
JM
521 NULL,
522};
523
f8ba9c11
JM
524static ssize_t btrfs_label_show(struct kobject *kobj,
525 struct kobj_attribute *a, char *buf)
526{
527 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
48fcc3ff 528 char *label = fs_info->super_copy->label;
ee17fc80
DS
529 ssize_t ret;
530
531 spin_lock(&fs_info->super_lock);
532 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
533 spin_unlock(&fs_info->super_lock);
534
535 return ret;
f8ba9c11
JM
536}
537
538static ssize_t btrfs_label_store(struct kobject *kobj,
539 struct kobj_attribute *a,
540 const char *buf, size_t len)
541{
542 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
48fcc3ff 543 size_t p_len;
f8ba9c11 544
66ac9fe7
DS
545 if (!fs_info)
546 return -EPERM;
547
bc98a42c 548 if (sb_rdonly(fs_info->sb))
79aec2b8
AJ
549 return -EROFS;
550
48fcc3ff
ST
551 /*
552 * p_len is the len until the first occurrence of either
553 * '\n' or '\0'
554 */
555 p_len = strcspn(buf, "\n");
556
557 if (p_len >= BTRFS_LABEL_SIZE)
f8ba9c11 558 return -EINVAL;
f8ba9c11 559
a6f69dc8 560 spin_lock(&fs_info->super_lock);
48fcc3ff
ST
561 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
562 memcpy(fs_info->super_copy->label, buf, p_len);
a6f69dc8 563 spin_unlock(&fs_info->super_lock);
f8ba9c11 564
a6f69dc8
DS
565 /*
566 * We don't want to do full transaction commit from inside sysfs
567 */
568 btrfs_set_pending(fs_info, COMMIT);
569 wake_up_process(fs_info->transaction_kthread);
f8ba9c11 570
a6f69dc8 571 return len;
f8ba9c11 572}
a969f4cc 573BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
f8ba9c11 574
df93589a
DS
575static ssize_t btrfs_nodesize_show(struct kobject *kobj,
576 struct kobj_attribute *a, char *buf)
577{
578 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
579
093e037c 580 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
df93589a
DS
581}
582
a969f4cc 583BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
df93589a
DS
584
585static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
586 struct kobj_attribute *a, char *buf)
587{
588 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
589
093e037c
DS
590 return snprintf(buf, PAGE_SIZE, "%u\n",
591 fs_info->super_copy->sectorsize);
df93589a
DS
592}
593
a969f4cc 594BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
df93589a
DS
595
596static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
597 struct kobj_attribute *a, char *buf)
598{
599 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
600
093e037c
DS
601 return snprintf(buf, PAGE_SIZE, "%u\n",
602 fs_info->super_copy->sectorsize);
df93589a
DS
603}
604
a969f4cc 605BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
df93589a 606
2723480a
SD
607static ssize_t quota_override_show(struct kobject *kobj,
608 struct kobj_attribute *a, char *buf)
609{
610 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
611 int quota_override;
612
613 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
614 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
615}
616
617static ssize_t quota_override_store(struct kobject *kobj,
618 struct kobj_attribute *a,
619 const char *buf, size_t len)
620{
621 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
622 unsigned long knob;
623 int err;
624
625 if (!fs_info)
626 return -EPERM;
627
628 if (!capable(CAP_SYS_RESOURCE))
629 return -EPERM;
630
631 err = kstrtoul(buf, 10, &knob);
632 if (err)
633 return err;
634 if (knob > 1)
635 return -EINVAL;
636
637 if (knob)
638 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
639 else
640 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
641
642 return len;
643}
644
a969f4cc 645BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
2723480a 646
56f20f40
NB
647static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
648 struct kobj_attribute *a, char *buf)
649{
650 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
651
652 return snprintf(buf, PAGE_SIZE, "%pU\n",
653 fs_info->fs_devices->metadata_uuid);
654}
655
656BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
657
41e6d2a8
JT
658static ssize_t btrfs_checksum_show(struct kobject *kobj,
659 struct kobj_attribute *a, char *buf)
660{
661 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
662 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
663
664 return snprintf(buf, PAGE_SIZE, "%s (%s)\n",
665 btrfs_super_csum_name(csum_type),
666 crypto_shash_driver_name(fs_info->csum_shash));
667}
668
669BTRFS_ATTR(, checksum, btrfs_checksum_show);
670
0dd2906f 671static const struct attribute *btrfs_attrs[] = {
a969f4cc
HK
672 BTRFS_ATTR_PTR(, label),
673 BTRFS_ATTR_PTR(, nodesize),
674 BTRFS_ATTR_PTR(, sectorsize),
675 BTRFS_ATTR_PTR(, clone_alignment),
676 BTRFS_ATTR_PTR(, quota_override),
56f20f40 677 BTRFS_ATTR_PTR(, metadata_uuid),
41e6d2a8 678 BTRFS_ATTR_PTR(, checksum),
f8ba9c11
JM
679 NULL,
680};
681
c1b7e474 682static void btrfs_release_fsid_kobj(struct kobject *kobj)
510d7360 683{
2e7910d6 684 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
248d200d 685
c1b7e474 686 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
2e7910d6 687 complete(&fs_devs->kobj_unregister);
510d7360
JM
688}
689
690static struct kobj_type btrfs_ktype = {
691 .sysfs_ops = &kobj_sysfs_ops,
c1b7e474 692 .release = btrfs_release_fsid_kobj,
510d7360
JM
693};
694
2e7910d6
AJ
695static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
696{
697 if (kobj->ktype != &btrfs_ktype)
698 return NULL;
c1b7e474 699 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
2e7910d6
AJ
700}
701
510d7360
JM
702static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
703{
704 if (kobj->ktype != &btrfs_ktype)
705 return NULL;
2e7910d6 706 return to_fs_devs(kobj)->fs_info;
510d7360 707}
58176a96 708
e453d989 709#define NUM_FEATURE_BITS 64
6c52157f
TM
710#define BTRFS_FEATURE_NAME_MAX 13
711static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
712static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
e453d989 713
6c52157f 714static const u64 supported_feature_masks[FEAT_MAX] = {
e453d989
JM
715 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
716 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
717 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
718};
719
720static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
721{
722 int set;
723
724 for (set = 0; set < FEAT_MAX; set++) {
725 int i;
726 struct attribute *attrs[2];
727 struct attribute_group agroup = {
728 .name = "features",
729 .attrs = attrs,
730 };
731 u64 features = get_features(fs_info, set);
732 features &= ~supported_feature_masks[set];
733
734 if (!features)
735 continue;
736
737 attrs[1] = NULL;
738 for (i = 0; i < NUM_FEATURE_BITS; i++) {
739 struct btrfs_feature_attr *fa;
740
741 if (!(features & (1ULL << i)))
742 continue;
743
744 fa = &btrfs_feature_attrs[set][i];
745 attrs[0] = &fa->kobj_attr.attr;
746 if (add) {
747 int ret;
c1b7e474 748 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
e453d989
JM
749 &agroup);
750 if (ret)
751 return ret;
752 } else
c1b7e474 753 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
e453d989
JM
754 &agroup);
755 }
756
757 }
758 return 0;
759}
760
2e3e1281 761static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
5ac1d209 762{
b5501504
AJ
763 if (fs_devs->devices_kobj) {
764 kobject_del(fs_devs->devices_kobj);
765 kobject_put(fs_devs->devices_kobj);
766 fs_devs->devices_kobj = NULL;
aaf13305
AJ
767 }
768
c1b7e474
AJ
769 if (fs_devs->fsid_kobj.state_initialized) {
770 kobject_del(&fs_devs->fsid_kobj);
771 kobject_put(&fs_devs->fsid_kobj);
f90fc547
AJ
772 wait_for_completion(&fs_devs->kobj_unregister);
773 }
5ac1d209
JM
774}
775
2e3e1281 776/* when fs_devs is NULL it will remove all fsid kobject */
1d1c1be3 777void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
2e3e1281
AJ
778{
779 struct list_head *fs_uuids = btrfs_get_fs_uuids();
780
781 if (fs_devs) {
782 __btrfs_sysfs_remove_fsid(fs_devs);
783 return;
784 }
785
c4babc5e 786 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
2e3e1281
AJ
787 __btrfs_sysfs_remove_fsid(fs_devs);
788 }
789}
790
6618a59b 791void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
e453d989 792{
5a13f430
AJ
793 btrfs_reset_fs_info_ptr(fs_info);
794
e453d989
JM
795 if (fs_info->space_info_kobj) {
796 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
797 kobject_del(fs_info->space_info_kobj);
798 kobject_put(fs_info->space_info_kobj);
799 }
71e8978e 800#ifdef CONFIG_BTRFS_DEBUG
e4faab84
DZ
801 if (fs_info->discard_debug_kobj) {
802 sysfs_remove_files(fs_info->discard_debug_kobj,
803 discard_debug_attrs);
804 kobject_del(fs_info->discard_debug_kobj);
805 kobject_put(fs_info->discard_debug_kobj);
806 }
93945cb4
DZ
807 if (fs_info->debug_kobj) {
808 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
809 kobject_del(fs_info->debug_kobj);
810 kobject_put(fs_info->debug_kobj);
811 }
71e8978e 812#endif
e453d989 813 addrm_unknown_feature_attrs(fs_info, false);
c1b7e474
AJ
814 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
815 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
32576040 816 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
e453d989
JM
817}
818
f10152bc 819static const char * const btrfs_feature_set_names[FEAT_MAX] = {
79da4fa4
JM
820 [FEAT_COMPAT] = "compat",
821 [FEAT_COMPAT_RO] = "compat_ro",
822 [FEAT_INCOMPAT] = "incompat",
823};
824
f10152bc
DS
825const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
826{
827 return btrfs_feature_set_names[set];
828}
829
3b02a68a
JM
830char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
831{
832 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
833 int len = 0;
834 int i;
835 char *str;
836
837 str = kmalloc(bufsize, GFP_KERNEL);
838 if (!str)
839 return str;
840
841 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
842 const char *name;
843
844 if (!(flags & (1ULL << i)))
845 continue;
846
847 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
848 len += snprintf(str + len, bufsize - len, "%s%s",
849 len ? "," : "", name);
850 }
851
852 return str;
853}
854
79da4fa4
JM
855static void init_feature_attrs(void)
856{
857 struct btrfs_feature_attr *fa;
858 int set, i;
859
860 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
861 ARRAY_SIZE(btrfs_feature_attrs));
862 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
863 ARRAY_SIZE(btrfs_feature_attrs[0]));
864
3b02a68a
JM
865 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
866 memset(btrfs_unknown_feature_names, 0,
867 sizeof(btrfs_unknown_feature_names));
868
79da4fa4
JM
869 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
870 struct btrfs_feature_attr *sfa;
871 struct attribute *a = btrfs_supported_feature_attrs[i];
3b02a68a 872 int bit;
79da4fa4 873 sfa = attr_to_btrfs_feature_attr(a);
3b02a68a
JM
874 bit = ilog2(sfa->feature_bit);
875 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
79da4fa4
JM
876
877 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
878 }
879
880 for (set = 0; set < FEAT_MAX; set++) {
881 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
882 char *name = btrfs_unknown_feature_names[set][i];
883 fa = &btrfs_feature_attrs[set][i];
884
885 if (fa->kobj_attr.attr.name)
886 continue;
887
6c52157f 888 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
79da4fa4
JM
889 btrfs_feature_set_names[set], i);
890
891 fa->kobj_attr.attr.name = name;
892 fa->kobj_attr.attr.mode = S_IRUGO;
893 fa->feature_set = set;
894 fa->feature_bit = 1ULL << i;
895 }
896 }
897}
898
32a9991f
DS
899/*
900 * Create a sysfs entry for a given block group type at path
901 * /sys/fs/btrfs/UUID/allocation/data/TYPE
902 */
32da5386 903void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
32a9991f
DS
904{
905 struct btrfs_fs_info *fs_info = cache->fs_info;
906 struct btrfs_space_info *space_info = cache->space_info;
907 struct raid_kobject *rkobj;
908 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
909 unsigned int nofs_flag;
910 int ret;
911
912 /*
913 * Setup a NOFS context because kobject_add(), deep in its call chain,
914 * does GFP_KERNEL allocations, and we are often called in a context
915 * where if reclaim is triggered we can deadlock (we are either holding
916 * a transaction handle or some lock required for a transaction
917 * commit).
918 */
919 nofs_flag = memalloc_nofs_save();
920
921 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
922 if (!rkobj) {
923 memalloc_nofs_restore(nofs_flag);
924 btrfs_warn(cache->fs_info,
925 "couldn't alloc memory for raid level kobject");
926 return;
927 }
928
929 rkobj->flags = cache->flags;
930 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
931 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
932 btrfs_bg_type_to_raid_name(rkobj->flags));
933 memalloc_nofs_restore(nofs_flag);
934 if (ret) {
935 kobject_put(&rkobj->kobj);
936 btrfs_warn(fs_info,
937 "failed to add kobject for block cache, ignoring");
938 return;
939 }
940
941 space_info->block_group_kobjs[index] = &rkobj->kobj;
942}
943
b5865bab
DS
944/*
945 * Remove sysfs directories for all block group types of a given space info and
946 * the space info as well
947 */
948void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
949{
950 int i;
951
952 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
953 struct kobject *kobj;
954
955 kobj = space_info->block_group_kobjs[i];
956 space_info->block_group_kobjs[i] = NULL;
957 if (kobj) {
958 kobject_del(kobj);
959 kobject_put(kobj);
960 }
961 }
962 kobject_del(&space_info->kobj);
963 kobject_put(&space_info->kobj);
964}
965
b882327a
DS
966static const char *alloc_name(u64 flags)
967{
968 switch (flags) {
969 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
970 return "mixed";
971 case BTRFS_BLOCK_GROUP_METADATA:
972 return "metadata";
973 case BTRFS_BLOCK_GROUP_DATA:
974 return "data";
975 case BTRFS_BLOCK_GROUP_SYSTEM:
976 return "system";
977 default:
978 WARN_ON(1);
979 return "invalid-combination";
980 };
981}
982
983/*
984 * Create a sysfs entry for a space info type at path
985 * /sys/fs/btrfs/UUID/allocation/TYPE
986 */
987int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
988 struct btrfs_space_info *space_info)
989{
990 int ret;
991
992 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
993 fs_info->space_info_kobj, "%s",
994 alloc_name(space_info->flags));
995 if (ret) {
996 kobject_put(&space_info->kobj);
997 return ret;
998 }
999
1000 return 0;
1001}
1002
e7e1aa9c
AJ
1003/* when one_device is NULL, it removes all device links */
1004
32576040 1005int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
99994cde
AJ
1006 struct btrfs_device *one_device)
1007{
1008 struct hd_struct *disk;
1009 struct kobject *disk_kobj;
1010
b5501504 1011 if (!fs_devices->devices_kobj)
99994cde
AJ
1012 return -EINVAL;
1013
87fa3bb0 1014 if (one_device && one_device->bdev) {
99994cde
AJ
1015 disk = one_device->bdev->bd_part;
1016 disk_kobj = &part_to_dev(disk)->kobj;
1017
b5501504 1018 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
99994cde
AJ
1019 }
1020
e7e1aa9c
AJ
1021 if (one_device)
1022 return 0;
1023
1024 list_for_each_entry(one_device,
6c14a164 1025 &fs_devices->devices, dev_list) {
e7e1aa9c
AJ
1026 if (!one_device->bdev)
1027 continue;
1028 disk = one_device->bdev->bd_part;
1029 disk_kobj = &part_to_dev(disk)->kobj;
1030
b5501504 1031 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
e7e1aa9c
AJ
1032 }
1033
99994cde
AJ
1034 return 0;
1035}
1036
e3bd6973 1037int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
1ba43816 1038 struct btrfs_device *one_device)
00c921c2
AJ
1039{
1040 int error = 0;
00c921c2
AJ
1041 struct btrfs_device *dev;
1042
29e5be24 1043 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
f085381e
AJ
1044 struct hd_struct *disk;
1045 struct kobject *disk_kobj;
1046
1047 if (!dev->bdev)
1048 continue;
1049
0d39376a
AJ
1050 if (one_device && one_device != dev)
1051 continue;
1052
f085381e
AJ
1053 disk = dev->bdev->bd_part;
1054 disk_kobj = &part_to_dev(disk)->kobj;
29e5be24 1055
b5501504 1056 error = sysfs_create_link(fs_devices->devices_kobj,
29e5be24
JM
1057 disk_kobj, disk_kobj->name);
1058 if (error)
1059 break;
1060 }
1061
1062 return error;
1063}
1064
5b28692e
DS
1065void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1066{
1067 int ret;
1068
1069 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1070 if (ret)
1071 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1072 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1073 &disk_to_dev(bdev->bd_disk)->kobj);
1074}
1075
f93c3997
DS
1076void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1077 const u8 *fsid)
1078{
1079 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1080
1081 /*
1082 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1083 * directory
1084 */
1085 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1086 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1087 btrfs_warn(fs_devices->fs_info,
1088 "sysfs: failed to create fsid for sprout");
1089}
1090
510d7360
JM
1091/* /sys/fs/btrfs/ entry */
1092static struct kset *btrfs_kset;
1093
72059215 1094/*
c6761a9e
AJ
1095 * Creates:
1096 * /sys/fs/btrfs/UUID
1097 *
72059215 1098 * Can be called by the device discovery thread.
72059215 1099 */
c6761a9e 1100int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
5ac1d209
JM
1101{
1102 int error;
1103
2e7910d6 1104 init_completion(&fs_devs->kobj_unregister);
c1b7e474 1105 fs_devs->fsid_kobj.kset = btrfs_kset;
c6761a9e
AJ
1106 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1107 "%pU", fs_devs->fsid);
e3277335
TH
1108 if (error) {
1109 kobject_put(&fs_devs->fsid_kobj);
1110 return error;
1111 }
1112
bc036bb3
AJ
1113 fs_devs->devices_kobj = kobject_create_and_add("devices",
1114 &fs_devs->fsid_kobj);
1115 if (!fs_devs->devices_kobj) {
1116 btrfs_err(fs_devs->fs_info,
1117 "failed to init sysfs device interface");
1118 kobject_put(&fs_devs->fsid_kobj);
1119 return -ENOMEM;
1120 }
1121
e3277335 1122 return 0;
72059215
AJ
1123}
1124
96f3136e 1125int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
72059215
AJ
1126{
1127 int error;
2e7910d6 1128 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
c1b7e474 1129 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
72059215 1130
5a13f430
AJ
1131 btrfs_set_fs_info_ptr(fs_info);
1132
e3bd6973 1133 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
b7c35e81 1134 if (error)
aaf13305 1135 return error;
aaf13305 1136
c1b7e474 1137 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
e453d989 1138 if (error) {
32576040 1139 btrfs_sysfs_rm_device_link(fs_devs, NULL);
e453d989
JM
1140 return error;
1141 }
79da4fa4 1142
c1b7e474 1143 error = sysfs_create_group(fsid_kobj,
0dd2906f
AJ
1144 &btrfs_feature_attr_group);
1145 if (error)
1146 goto failure;
1147
6e369feb 1148#ifdef CONFIG_BTRFS_DEBUG
93945cb4
DZ
1149 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1150 if (!fs_info->debug_kobj) {
1151 error = -ENOMEM;
1152 goto failure;
1153 }
1154
1155 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
6e369feb
DS
1156 if (error)
1157 goto failure;
e4faab84
DZ
1158
1159 /* Discard directory */
1160 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1161 fs_info->debug_kobj);
1162 if (!fs_info->discard_debug_kobj) {
1163 error = -ENOMEM;
1164 goto failure;
1165 }
1166
1167 error = sysfs_create_files(fs_info->discard_debug_kobj,
1168 discard_debug_attrs);
1169 if (error)
1170 goto failure;
6e369feb
DS
1171#endif
1172
e453d989 1173 error = addrm_unknown_feature_attrs(fs_info, true);
79da4fa4
JM
1174 if (error)
1175 goto failure;
1176
6ab0a202 1177 fs_info->space_info_kobj = kobject_create_and_add("allocation",
c1b7e474 1178 fsid_kobj);
6ab0a202
JM
1179 if (!fs_info->space_info_kobj) {
1180 error = -ENOMEM;
1181 goto failure;
1182 }
1183
1184 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1185 if (error)
1186 goto failure;
1187
79da4fa4
JM
1188 return 0;
1189failure:
6618a59b 1190 btrfs_sysfs_remove_mounted(fs_info);
5ac1d209
JM
1191 return error;
1192}
1193
444e7516
DS
1194
1195/*
1196 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1197 * values in superblock. Call after any changes to incompat/compat_ro flags
1198 */
1199void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1200 u64 bit, enum btrfs_feature_set set)
1201{
1202 struct btrfs_fs_devices *fs_devs;
1203 struct kobject *fsid_kobj;
1204 u64 features;
1205 int ret;
1206
1207 if (!fs_info)
1208 return;
1209
1210 features = get_features(fs_info, set);
1211 ASSERT(bit & supported_feature_masks[set]);
1212
1213 fs_devs = fs_info->fs_devices;
1214 fsid_kobj = &fs_devs->fsid_kobj;
1215
bf609206
DS
1216 if (!fsid_kobj->state_initialized)
1217 return;
1218
444e7516
DS
1219 /*
1220 * FIXME: this is too heavy to update just one value, ideally we'd like
1221 * to use sysfs_update_group but some refactoring is needed first.
1222 */
1223 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1224 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1225}
1226
f5c29bd9 1227int __init btrfs_init_sysfs(void)
58176a96 1228{
079b72bc 1229 int ret;
1bae3098 1230
e3fe4e71
GKH
1231 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1232 if (!btrfs_kset)
1233 return -ENOMEM;
079b72bc 1234
1bae3098 1235 init_feature_attrs();
079b72bc 1236 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
001a648d
FM
1237 if (ret)
1238 goto out2;
f902bd3a
MT
1239 ret = sysfs_merge_group(&btrfs_kset->kobj,
1240 &btrfs_static_feature_attr_group);
1241 if (ret)
1242 goto out_remove_group;
001a648d 1243
6e369feb
DS
1244#ifdef CONFIG_BTRFS_DEBUG
1245 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1246 if (ret)
1247 goto out2;
1248#endif
1249
001a648d 1250 return 0;
f902bd3a
MT
1251
1252out_remove_group:
1253 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
001a648d 1254out2:
001a648d 1255 kset_unregister(btrfs_kset);
079b72bc 1256
1bae3098 1257 return ret;
58176a96
JB
1258}
1259
e67c718b 1260void __cold btrfs_exit_sysfs(void)
58176a96 1261{
f902bd3a
MT
1262 sysfs_unmerge_group(&btrfs_kset->kobj,
1263 &btrfs_static_feature_attr_group);
079b72bc 1264 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
71e8978e
DZ
1265#ifdef CONFIG_BTRFS_DEBUG
1266 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1267#endif
e3fe4e71 1268 kset_unregister(btrfs_kset);
58176a96 1269}
55d47414 1270