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