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