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