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