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