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