]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/btrfs/sysfs.c
btrfs: sysfs: make UUID/debug have its own kobject
[linux.git] / fs / btrfs / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5
6 #include <linux/sched.h>
7 #include <linux/sched/mm.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/completion.h>
11 #include <linux/bug.h>
12 #include <crypto/hash.h>
13
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "sysfs.h"
18 #include "volumes.h"
19 #include "space-info.h"
20 #include "block-group.h"
21
22 struct btrfs_feature_attr {
23         struct kobj_attribute kobj_attr;
24         enum btrfs_feature_set feature_set;
25         u64 feature_bit;
26 };
27
28 /* For raid type sysfs entries */
29 struct raid_kobject {
30         u64 flags;
31         struct kobject kobj;
32 };
33
34 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)                   \
35 {                                                                       \
36         .attr   = { .name = __stringify(_name), .mode = _mode },        \
37         .show   = _show,                                                \
38         .store  = _store,                                               \
39 }
40
41 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store)                    \
42         static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
43                         __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
44
45 #define BTRFS_ATTR(_prefix, _name, _show)                               \
46         static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
47                         __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
48
49 #define BTRFS_ATTR_PTR(_prefix, _name)                                  \
50         (&btrfs_attr_##_prefix##_##_name.attr)
51
52 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
53 static struct btrfs_feature_attr btrfs_attr_features_##_name = {             \
54         .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,                        \
55                                       btrfs_feature_attr_show,               \
56                                       btrfs_feature_attr_store),             \
57         .feature_set    = _feature_set,                                      \
58         .feature_bit    = _feature_prefix ##_## _feature_bit,                \
59 }
60 #define BTRFS_FEAT_ATTR_PTR(_name)                                           \
61         (&btrfs_attr_features_##_name.kobj_attr.attr)
62
63 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
64         BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
65 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
66         BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
67 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
68         BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
69
70 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
71 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
72
73 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
74 {
75         return container_of(a, struct btrfs_feature_attr, kobj_attr);
76 }
77
78 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
79 {
80         return container_of(attr, struct kobj_attribute, attr);
81 }
82
83 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
84                 struct attribute *attr)
85 {
86         return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
87 }
88
89 static u64 get_features(struct btrfs_fs_info *fs_info,
90                         enum btrfs_feature_set set)
91 {
92         struct btrfs_super_block *disk_super = fs_info->super_copy;
93         if (set == FEAT_COMPAT)
94                 return btrfs_super_compat_flags(disk_super);
95         else if (set == FEAT_COMPAT_RO)
96                 return btrfs_super_compat_ro_flags(disk_super);
97         else
98                 return btrfs_super_incompat_flags(disk_super);
99 }
100
101 static void set_features(struct btrfs_fs_info *fs_info,
102                          enum btrfs_feature_set set, u64 features)
103 {
104         struct btrfs_super_block *disk_super = fs_info->super_copy;
105         if (set == FEAT_COMPAT)
106                 btrfs_set_super_compat_flags(disk_super, features);
107         else if (set == FEAT_COMPAT_RO)
108                 btrfs_set_super_compat_ro_flags(disk_super, features);
109         else
110                 btrfs_set_super_incompat_flags(disk_super, features);
111 }
112
113 static int can_modify_feature(struct btrfs_feature_attr *fa)
114 {
115         int val = 0;
116         u64 set, clear;
117         switch (fa->feature_set) {
118         case FEAT_COMPAT:
119                 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
120                 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
121                 break;
122         case FEAT_COMPAT_RO:
123                 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
124                 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
125                 break;
126         case FEAT_INCOMPAT:
127                 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
128                 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
129                 break;
130         default:
131                 pr_warn("btrfs: sysfs: unknown feature set %d\n",
132                                 fa->feature_set);
133                 return 0;
134         }
135
136         if (set & fa->feature_bit)
137                 val |= 1;
138         if (clear & fa->feature_bit)
139                 val |= 2;
140
141         return val;
142 }
143
144 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
145                                        struct kobj_attribute *a, char *buf)
146 {
147         int val = 0;
148         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
149         struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
150         if (fs_info) {
151                 u64 features = get_features(fs_info, fa->feature_set);
152                 if (features & fa->feature_bit)
153                         val = 1;
154         } else
155                 val = can_modify_feature(fa);
156
157         return snprintf(buf, PAGE_SIZE, "%d\n", val);
158 }
159
160 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
161                                         struct kobj_attribute *a,
162                                         const char *buf, size_t count)
163 {
164         struct btrfs_fs_info *fs_info;
165         struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
166         u64 features, set, clear;
167         unsigned long val;
168         int ret;
169
170         fs_info = to_fs_info(kobj);
171         if (!fs_info)
172                 return -EPERM;
173
174         if (sb_rdonly(fs_info->sb))
175                 return -EROFS;
176
177         ret = kstrtoul(skip_spaces(buf), 0, &val);
178         if (ret)
179                 return ret;
180
181         if (fa->feature_set == FEAT_COMPAT) {
182                 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
183                 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
184         } else if (fa->feature_set == FEAT_COMPAT_RO) {
185                 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
186                 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
187         } else {
188                 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
189                 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
190         }
191
192         features = get_features(fs_info, fa->feature_set);
193
194         /* Nothing to do */
195         if ((val && (features & fa->feature_bit)) ||
196             (!val && !(features & fa->feature_bit)))
197                 return count;
198
199         if ((val && !(set & fa->feature_bit)) ||
200             (!val && !(clear & fa->feature_bit))) {
201                 btrfs_info(fs_info,
202                         "%sabling feature %s on mounted fs is not supported.",
203                         val ? "En" : "Dis", fa->kobj_attr.attr.name);
204                 return -EPERM;
205         }
206
207         btrfs_info(fs_info, "%s %s feature flag",
208                    val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
209
210         spin_lock(&fs_info->super_lock);
211         features = get_features(fs_info, fa->feature_set);
212         if (val)
213                 features |= fa->feature_bit;
214         else
215                 features &= ~fa->feature_bit;
216         set_features(fs_info, fa->feature_set, features);
217         spin_unlock(&fs_info->super_lock);
218
219         /*
220          * We don't want to do full transaction commit from inside sysfs
221          */
222         btrfs_set_pending(fs_info, COMMIT);
223         wake_up_process(fs_info->transaction_kthread);
224
225         return count;
226 }
227
228 static umode_t btrfs_feature_visible(struct kobject *kobj,
229                                      struct attribute *attr, int unused)
230 {
231         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
232         umode_t mode = attr->mode;
233
234         if (fs_info) {
235                 struct btrfs_feature_attr *fa;
236                 u64 features;
237
238                 fa = attr_to_btrfs_feature_attr(attr);
239                 features = get_features(fs_info, fa->feature_set);
240
241                 if (can_modify_feature(fa))
242                         mode |= S_IWUSR;
243                 else if (!(features & fa->feature_bit))
244                         mode = 0;
245         }
246
247         return mode;
248 }
249
250 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
251 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
252 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
253 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
254 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
255 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
256 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
257 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
258 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
259 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
260 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
261 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
262 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
263
264 static struct attribute *btrfs_supported_feature_attrs[] = {
265         BTRFS_FEAT_ATTR_PTR(mixed_backref),
266         BTRFS_FEAT_ATTR_PTR(default_subvol),
267         BTRFS_FEAT_ATTR_PTR(mixed_groups),
268         BTRFS_FEAT_ATTR_PTR(compress_lzo),
269         BTRFS_FEAT_ATTR_PTR(compress_zstd),
270         BTRFS_FEAT_ATTR_PTR(big_metadata),
271         BTRFS_FEAT_ATTR_PTR(extended_iref),
272         BTRFS_FEAT_ATTR_PTR(raid56),
273         BTRFS_FEAT_ATTR_PTR(skinny_metadata),
274         BTRFS_FEAT_ATTR_PTR(no_holes),
275         BTRFS_FEAT_ATTR_PTR(metadata_uuid),
276         BTRFS_FEAT_ATTR_PTR(free_space_tree),
277         BTRFS_FEAT_ATTR_PTR(raid1c34),
278         NULL
279 };
280
281 /*
282  * Features which depend on feature bits and may differ between each fs.
283  *
284  * /sys/fs/btrfs/features lists all available features of this kernel while
285  * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
286  * can be changed online.
287  */
288 static const struct attribute_group btrfs_feature_attr_group = {
289         .name = "features",
290         .is_visible = btrfs_feature_visible,
291         .attrs = btrfs_supported_feature_attrs,
292 };
293
294 static ssize_t rmdir_subvol_show(struct kobject *kobj,
295                                  struct kobj_attribute *ka, char *buf)
296 {
297         return snprintf(buf, PAGE_SIZE, "0\n");
298 }
299 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
300
301 static ssize_t supported_checksums_show(struct kobject *kobj,
302                                         struct kobj_attribute *a, char *buf)
303 {
304         ssize_t ret = 0;
305         int i;
306
307         for (i = 0; i < btrfs_get_num_csums(); i++) {
308                 /*
309                  * This "trick" only works as long as 'enum btrfs_csum_type' has
310                  * no holes in it
311                  */
312                 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
313                                 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
314
315         }
316
317         ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
318         return ret;
319 }
320 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
321
322 static struct attribute *btrfs_supported_static_feature_attrs[] = {
323         BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
324         BTRFS_ATTR_PTR(static_feature, supported_checksums),
325         NULL
326 };
327
328 /*
329  * Features which only depend on kernel version.
330  *
331  * These are listed in /sys/fs/btrfs/features along with
332  * btrfs_feature_attr_group
333  */
334 static const struct attribute_group btrfs_static_feature_attr_group = {
335         .name = "features",
336         .attrs = btrfs_supported_static_feature_attrs,
337 };
338
339 #ifdef CONFIG_BTRFS_DEBUG
340
341 /*
342  * Runtime debugging exported via sysfs
343  *
344  * /sys/fs/btrfs/debug - applies to module or all filesystems
345  * /sys/fs/btrfs/UUID  - applies only to the given filesystem
346  */
347 static const struct attribute *btrfs_debug_mount_attrs[] = {
348         NULL,
349 };
350
351 static struct attribute *btrfs_debug_feature_attrs[] = {
352         NULL
353 };
354
355 static const struct attribute_group btrfs_debug_feature_attr_group = {
356         .name = "debug",
357         .attrs = btrfs_debug_feature_attrs,
358 };
359
360 #endif
361
362 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
363 {
364         u64 val;
365         if (lock)
366                 spin_lock(lock);
367         val = *value_ptr;
368         if (lock)
369                 spin_unlock(lock);
370         return snprintf(buf, PAGE_SIZE, "%llu\n", val);
371 }
372
373 static ssize_t global_rsv_size_show(struct kobject *kobj,
374                                     struct kobj_attribute *ka, char *buf)
375 {
376         struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
377         struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
378         return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
379 }
380 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
381
382 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
383                                         struct kobj_attribute *a, char *buf)
384 {
385         struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
386         struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
387         return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
388 }
389 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
390
391 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
392 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
393
394 static ssize_t raid_bytes_show(struct kobject *kobj,
395                                struct kobj_attribute *attr, char *buf);
396 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
397 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
398
399 static ssize_t raid_bytes_show(struct kobject *kobj,
400                                struct kobj_attribute *attr, char *buf)
401
402 {
403         struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
404         struct btrfs_block_group *block_group;
405         int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
406         u64 val = 0;
407
408         down_read(&sinfo->groups_sem);
409         list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
410                 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
411                         val += block_group->length;
412                 else
413                         val += block_group->used;
414         }
415         up_read(&sinfo->groups_sem);
416         return snprintf(buf, PAGE_SIZE, "%llu\n", val);
417 }
418
419 static struct attribute *raid_attrs[] = {
420         BTRFS_ATTR_PTR(raid, total_bytes),
421         BTRFS_ATTR_PTR(raid, used_bytes),
422         NULL
423 };
424 ATTRIBUTE_GROUPS(raid);
425
426 static void release_raid_kobj(struct kobject *kobj)
427 {
428         kfree(to_raid_kobj(kobj));
429 }
430
431 static struct kobj_type btrfs_raid_ktype = {
432         .sysfs_ops = &kobj_sysfs_ops,
433         .release = release_raid_kobj,
434         .default_groups = raid_groups,
435 };
436
437 #define SPACE_INFO_ATTR(field)                                          \
438 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,      \
439                                              struct kobj_attribute *a,  \
440                                              char *buf)                 \
441 {                                                                       \
442         struct btrfs_space_info *sinfo = to_space_info(kobj);           \
443         return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);        \
444 }                                                                       \
445 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
446
447 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
448                                                        struct kobj_attribute *a,
449                                                        char *buf)
450 {
451         struct btrfs_space_info *sinfo = to_space_info(kobj);
452         s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
453         return snprintf(buf, PAGE_SIZE, "%lld\n", val);
454 }
455
456 SPACE_INFO_ATTR(flags);
457 SPACE_INFO_ATTR(total_bytes);
458 SPACE_INFO_ATTR(bytes_used);
459 SPACE_INFO_ATTR(bytes_pinned);
460 SPACE_INFO_ATTR(bytes_reserved);
461 SPACE_INFO_ATTR(bytes_may_use);
462 SPACE_INFO_ATTR(bytes_readonly);
463 SPACE_INFO_ATTR(disk_used);
464 SPACE_INFO_ATTR(disk_total);
465 BTRFS_ATTR(space_info, total_bytes_pinned,
466            btrfs_space_info_show_total_bytes_pinned);
467
468 static struct attribute *space_info_attrs[] = {
469         BTRFS_ATTR_PTR(space_info, flags),
470         BTRFS_ATTR_PTR(space_info, total_bytes),
471         BTRFS_ATTR_PTR(space_info, bytes_used),
472         BTRFS_ATTR_PTR(space_info, bytes_pinned),
473         BTRFS_ATTR_PTR(space_info, bytes_reserved),
474         BTRFS_ATTR_PTR(space_info, bytes_may_use),
475         BTRFS_ATTR_PTR(space_info, bytes_readonly),
476         BTRFS_ATTR_PTR(space_info, disk_used),
477         BTRFS_ATTR_PTR(space_info, disk_total),
478         BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
479         NULL,
480 };
481 ATTRIBUTE_GROUPS(space_info);
482
483 static void space_info_release(struct kobject *kobj)
484 {
485         struct btrfs_space_info *sinfo = to_space_info(kobj);
486         percpu_counter_destroy(&sinfo->total_bytes_pinned);
487         kfree(sinfo);
488 }
489
490 static struct kobj_type space_info_ktype = {
491         .sysfs_ops = &kobj_sysfs_ops,
492         .release = space_info_release,
493         .default_groups = space_info_groups,
494 };
495
496 static const struct attribute *allocation_attrs[] = {
497         BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
498         BTRFS_ATTR_PTR(allocation, global_rsv_size),
499         NULL,
500 };
501
502 static ssize_t btrfs_label_show(struct kobject *kobj,
503                                 struct kobj_attribute *a, char *buf)
504 {
505         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
506         char *label = fs_info->super_copy->label;
507         ssize_t ret;
508
509         spin_lock(&fs_info->super_lock);
510         ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
511         spin_unlock(&fs_info->super_lock);
512
513         return ret;
514 }
515
516 static ssize_t btrfs_label_store(struct kobject *kobj,
517                                  struct kobj_attribute *a,
518                                  const char *buf, size_t len)
519 {
520         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
521         size_t p_len;
522
523         if (!fs_info)
524                 return -EPERM;
525
526         if (sb_rdonly(fs_info->sb))
527                 return -EROFS;
528
529         /*
530          * p_len is the len until the first occurrence of either
531          * '\n' or '\0'
532          */
533         p_len = strcspn(buf, "\n");
534
535         if (p_len >= BTRFS_LABEL_SIZE)
536                 return -EINVAL;
537
538         spin_lock(&fs_info->super_lock);
539         memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
540         memcpy(fs_info->super_copy->label, buf, p_len);
541         spin_unlock(&fs_info->super_lock);
542
543         /*
544          * We don't want to do full transaction commit from inside sysfs
545          */
546         btrfs_set_pending(fs_info, COMMIT);
547         wake_up_process(fs_info->transaction_kthread);
548
549         return len;
550 }
551 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
552
553 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
554                                 struct kobj_attribute *a, char *buf)
555 {
556         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
557
558         return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
559 }
560
561 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
562
563 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
564                                 struct kobj_attribute *a, char *buf)
565 {
566         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
567
568         return snprintf(buf, PAGE_SIZE, "%u\n",
569                         fs_info->super_copy->sectorsize);
570 }
571
572 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
573
574 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
575                                 struct kobj_attribute *a, char *buf)
576 {
577         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
578
579         return snprintf(buf, PAGE_SIZE, "%u\n",
580                         fs_info->super_copy->sectorsize);
581 }
582
583 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
584
585 static ssize_t quota_override_show(struct kobject *kobj,
586                                    struct kobj_attribute *a, char *buf)
587 {
588         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
589         int quota_override;
590
591         quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
592         return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
593 }
594
595 static ssize_t quota_override_store(struct kobject *kobj,
596                                     struct kobj_attribute *a,
597                                     const char *buf, size_t len)
598 {
599         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
600         unsigned long knob;
601         int err;
602
603         if (!fs_info)
604                 return -EPERM;
605
606         if (!capable(CAP_SYS_RESOURCE))
607                 return -EPERM;
608
609         err = kstrtoul(buf, 10, &knob);
610         if (err)
611                 return err;
612         if (knob > 1)
613                 return -EINVAL;
614
615         if (knob)
616                 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
617         else
618                 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
619
620         return len;
621 }
622
623 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
624
625 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
626                                 struct kobj_attribute *a, char *buf)
627 {
628         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
629
630         return snprintf(buf, PAGE_SIZE, "%pU\n",
631                         fs_info->fs_devices->metadata_uuid);
632 }
633
634 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
635
636 static ssize_t btrfs_checksum_show(struct kobject *kobj,
637                                    struct kobj_attribute *a, char *buf)
638 {
639         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
640         u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
641
642         return snprintf(buf, PAGE_SIZE, "%s (%s)\n",
643                         btrfs_super_csum_name(csum_type),
644                         crypto_shash_driver_name(fs_info->csum_shash));
645 }
646
647 BTRFS_ATTR(, checksum, btrfs_checksum_show);
648
649 static const struct attribute *btrfs_attrs[] = {
650         BTRFS_ATTR_PTR(, label),
651         BTRFS_ATTR_PTR(, nodesize),
652         BTRFS_ATTR_PTR(, sectorsize),
653         BTRFS_ATTR_PTR(, clone_alignment),
654         BTRFS_ATTR_PTR(, quota_override),
655         BTRFS_ATTR_PTR(, metadata_uuid),
656         BTRFS_ATTR_PTR(, checksum),
657         NULL,
658 };
659
660 static void btrfs_release_fsid_kobj(struct kobject *kobj)
661 {
662         struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
663
664         memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
665         complete(&fs_devs->kobj_unregister);
666 }
667
668 static struct kobj_type btrfs_ktype = {
669         .sysfs_ops      = &kobj_sysfs_ops,
670         .release        = btrfs_release_fsid_kobj,
671 };
672
673 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
674 {
675         if (kobj->ktype != &btrfs_ktype)
676                 return NULL;
677         return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
678 }
679
680 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
681 {
682         if (kobj->ktype != &btrfs_ktype)
683                 return NULL;
684         return to_fs_devs(kobj)->fs_info;
685 }
686
687 #define NUM_FEATURE_BITS 64
688 #define BTRFS_FEATURE_NAME_MAX 13
689 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
690 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
691
692 static const u64 supported_feature_masks[FEAT_MAX] = {
693         [FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
694         [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
695         [FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
696 };
697
698 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
699 {
700         int set;
701
702         for (set = 0; set < FEAT_MAX; set++) {
703                 int i;
704                 struct attribute *attrs[2];
705                 struct attribute_group agroup = {
706                         .name = "features",
707                         .attrs = attrs,
708                 };
709                 u64 features = get_features(fs_info, set);
710                 features &= ~supported_feature_masks[set];
711
712                 if (!features)
713                         continue;
714
715                 attrs[1] = NULL;
716                 for (i = 0; i < NUM_FEATURE_BITS; i++) {
717                         struct btrfs_feature_attr *fa;
718
719                         if (!(features & (1ULL << i)))
720                                 continue;
721
722                         fa = &btrfs_feature_attrs[set][i];
723                         attrs[0] = &fa->kobj_attr.attr;
724                         if (add) {
725                                 int ret;
726                                 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
727                                                         &agroup);
728                                 if (ret)
729                                         return ret;
730                         } else
731                                 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
732                                                     &agroup);
733                 }
734
735         }
736         return 0;
737 }
738
739 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
740 {
741         if (fs_devs->devices_kobj) {
742                 kobject_del(fs_devs->devices_kobj);
743                 kobject_put(fs_devs->devices_kobj);
744                 fs_devs->devices_kobj = NULL;
745         }
746
747         if (fs_devs->fsid_kobj.state_initialized) {
748                 kobject_del(&fs_devs->fsid_kobj);
749                 kobject_put(&fs_devs->fsid_kobj);
750                 wait_for_completion(&fs_devs->kobj_unregister);
751         }
752 }
753
754 /* when fs_devs is NULL it will remove all fsid kobject */
755 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
756 {
757         struct list_head *fs_uuids = btrfs_get_fs_uuids();
758
759         if (fs_devs) {
760                 __btrfs_sysfs_remove_fsid(fs_devs);
761                 return;
762         }
763
764         list_for_each_entry(fs_devs, fs_uuids, fs_list) {
765                 __btrfs_sysfs_remove_fsid(fs_devs);
766         }
767 }
768
769 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
770 {
771         btrfs_reset_fs_info_ptr(fs_info);
772
773         if (fs_info->space_info_kobj) {
774                 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
775                 kobject_del(fs_info->space_info_kobj);
776                 kobject_put(fs_info->space_info_kobj);
777         }
778 #ifdef CONFIG_BTRFS_DEBUG
779         if (fs_info->debug_kobj) {
780                 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
781                 kobject_del(fs_info->debug_kobj);
782                 kobject_put(fs_info->debug_kobj);
783         }
784 #endif
785         addrm_unknown_feature_attrs(fs_info, false);
786         sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
787         sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
788         btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
789 }
790
791 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
792         [FEAT_COMPAT]    = "compat",
793         [FEAT_COMPAT_RO] = "compat_ro",
794         [FEAT_INCOMPAT]  = "incompat",
795 };
796
797 const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
798 {
799         return btrfs_feature_set_names[set];
800 }
801
802 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
803 {
804         size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
805         int len = 0;
806         int i;
807         char *str;
808
809         str = kmalloc(bufsize, GFP_KERNEL);
810         if (!str)
811                 return str;
812
813         for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
814                 const char *name;
815
816                 if (!(flags & (1ULL << i)))
817                         continue;
818
819                 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
820                 len += snprintf(str + len, bufsize - len, "%s%s",
821                                 len ? "," : "", name);
822         }
823
824         return str;
825 }
826
827 static void init_feature_attrs(void)
828 {
829         struct btrfs_feature_attr *fa;
830         int set, i;
831
832         BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
833                      ARRAY_SIZE(btrfs_feature_attrs));
834         BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
835                      ARRAY_SIZE(btrfs_feature_attrs[0]));
836
837         memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
838         memset(btrfs_unknown_feature_names, 0,
839                sizeof(btrfs_unknown_feature_names));
840
841         for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
842                 struct btrfs_feature_attr *sfa;
843                 struct attribute *a = btrfs_supported_feature_attrs[i];
844                 int bit;
845                 sfa = attr_to_btrfs_feature_attr(a);
846                 bit = ilog2(sfa->feature_bit);
847                 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
848
849                 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
850         }
851
852         for (set = 0; set < FEAT_MAX; set++) {
853                 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
854                         char *name = btrfs_unknown_feature_names[set][i];
855                         fa = &btrfs_feature_attrs[set][i];
856
857                         if (fa->kobj_attr.attr.name)
858                                 continue;
859
860                         snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
861                                  btrfs_feature_set_names[set], i);
862
863                         fa->kobj_attr.attr.name = name;
864                         fa->kobj_attr.attr.mode = S_IRUGO;
865                         fa->feature_set = set;
866                         fa->feature_bit = 1ULL << i;
867                 }
868         }
869 }
870
871 /*
872  * Create a sysfs entry for a given block group type at path
873  * /sys/fs/btrfs/UUID/allocation/data/TYPE
874  */
875 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
876 {
877         struct btrfs_fs_info *fs_info = cache->fs_info;
878         struct btrfs_space_info *space_info = cache->space_info;
879         struct raid_kobject *rkobj;
880         const int index = btrfs_bg_flags_to_raid_index(cache->flags);
881         unsigned int nofs_flag;
882         int ret;
883
884         /*
885          * Setup a NOFS context because kobject_add(), deep in its call chain,
886          * does GFP_KERNEL allocations, and we are often called in a context
887          * where if reclaim is triggered we can deadlock (we are either holding
888          * a transaction handle or some lock required for a transaction
889          * commit).
890          */
891         nofs_flag = memalloc_nofs_save();
892
893         rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
894         if (!rkobj) {
895                 memalloc_nofs_restore(nofs_flag);
896                 btrfs_warn(cache->fs_info,
897                                 "couldn't alloc memory for raid level kobject");
898                 return;
899         }
900
901         rkobj->flags = cache->flags;
902         kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
903         ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
904                           btrfs_bg_type_to_raid_name(rkobj->flags));
905         memalloc_nofs_restore(nofs_flag);
906         if (ret) {
907                 kobject_put(&rkobj->kobj);
908                 btrfs_warn(fs_info,
909                         "failed to add kobject for block cache, ignoring");
910                 return;
911         }
912
913         space_info->block_group_kobjs[index] = &rkobj->kobj;
914 }
915
916 /*
917  * Remove sysfs directories for all block group types of a given space info and
918  * the space info as well
919  */
920 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
921 {
922         int i;
923
924         for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
925                 struct kobject *kobj;
926
927                 kobj = space_info->block_group_kobjs[i];
928                 space_info->block_group_kobjs[i] = NULL;
929                 if (kobj) {
930                         kobject_del(kobj);
931                         kobject_put(kobj);
932                 }
933         }
934         kobject_del(&space_info->kobj);
935         kobject_put(&space_info->kobj);
936 }
937
938 static const char *alloc_name(u64 flags)
939 {
940         switch (flags) {
941         case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
942                 return "mixed";
943         case BTRFS_BLOCK_GROUP_METADATA:
944                 return "metadata";
945         case BTRFS_BLOCK_GROUP_DATA:
946                 return "data";
947         case BTRFS_BLOCK_GROUP_SYSTEM:
948                 return "system";
949         default:
950                 WARN_ON(1);
951                 return "invalid-combination";
952         };
953 }
954
955 /*
956  * Create a sysfs entry for a space info type at path
957  * /sys/fs/btrfs/UUID/allocation/TYPE
958  */
959 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
960                                     struct btrfs_space_info *space_info)
961 {
962         int ret;
963
964         ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
965                                    fs_info->space_info_kobj, "%s",
966                                    alloc_name(space_info->flags));
967         if (ret) {
968                 kobject_put(&space_info->kobj);
969                 return ret;
970         }
971
972         return 0;
973 }
974
975 /* when one_device is NULL, it removes all device links */
976
977 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
978                 struct btrfs_device *one_device)
979 {
980         struct hd_struct *disk;
981         struct kobject *disk_kobj;
982
983         if (!fs_devices->devices_kobj)
984                 return -EINVAL;
985
986         if (one_device && one_device->bdev) {
987                 disk = one_device->bdev->bd_part;
988                 disk_kobj = &part_to_dev(disk)->kobj;
989
990                 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
991         }
992
993         if (one_device)
994                 return 0;
995
996         list_for_each_entry(one_device,
997                         &fs_devices->devices, dev_list) {
998                 if (!one_device->bdev)
999                         continue;
1000                 disk = one_device->bdev->bd_part;
1001                 disk_kobj = &part_to_dev(disk)->kobj;
1002
1003                 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
1004         }
1005
1006         return 0;
1007 }
1008
1009 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
1010                                 struct btrfs_device *one_device)
1011 {
1012         int error = 0;
1013         struct btrfs_device *dev;
1014
1015         list_for_each_entry(dev, &fs_devices->devices, dev_list) {
1016                 struct hd_struct *disk;
1017                 struct kobject *disk_kobj;
1018
1019                 if (!dev->bdev)
1020                         continue;
1021
1022                 if (one_device && one_device != dev)
1023                         continue;
1024
1025                 disk = dev->bdev->bd_part;
1026                 disk_kobj = &part_to_dev(disk)->kobj;
1027
1028                 error = sysfs_create_link(fs_devices->devices_kobj,
1029                                           disk_kobj, disk_kobj->name);
1030                 if (error)
1031                         break;
1032         }
1033
1034         return error;
1035 }
1036
1037 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1038 {
1039         int ret;
1040
1041         ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1042         if (ret)
1043                 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1044                         action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1045                         &disk_to_dev(bdev->bd_disk)->kobj);
1046 }
1047
1048 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1049                                     const u8 *fsid)
1050 {
1051         char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1052
1053         /*
1054          * Sprouting changes fsid of the mounted filesystem, rename the fsid
1055          * directory
1056          */
1057         snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1058         if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1059                 btrfs_warn(fs_devices->fs_info,
1060                                 "sysfs: failed to create fsid for sprout");
1061 }
1062
1063 /* /sys/fs/btrfs/ entry */
1064 static struct kset *btrfs_kset;
1065
1066 /*
1067  * Creates:
1068  *              /sys/fs/btrfs/UUID
1069  *
1070  * Can be called by the device discovery thread.
1071  */
1072 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1073 {
1074         int error;
1075
1076         init_completion(&fs_devs->kobj_unregister);
1077         fs_devs->fsid_kobj.kset = btrfs_kset;
1078         error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1079                                      "%pU", fs_devs->fsid);
1080         if (error) {
1081                 kobject_put(&fs_devs->fsid_kobj);
1082                 return error;
1083         }
1084
1085         fs_devs->devices_kobj = kobject_create_and_add("devices",
1086                                                        &fs_devs->fsid_kobj);
1087         if (!fs_devs->devices_kobj) {
1088                 btrfs_err(fs_devs->fs_info,
1089                           "failed to init sysfs device interface");
1090                 kobject_put(&fs_devs->fsid_kobj);
1091                 return -ENOMEM;
1092         }
1093
1094         return 0;
1095 }
1096
1097 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1098 {
1099         int error;
1100         struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1101         struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1102
1103         btrfs_set_fs_info_ptr(fs_info);
1104
1105         error = btrfs_sysfs_add_device_link(fs_devs, NULL);
1106         if (error)
1107                 return error;
1108
1109         error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1110         if (error) {
1111                 btrfs_sysfs_rm_device_link(fs_devs, NULL);
1112                 return error;
1113         }
1114
1115         error = sysfs_create_group(fsid_kobj,
1116                                    &btrfs_feature_attr_group);
1117         if (error)
1118                 goto failure;
1119
1120 #ifdef CONFIG_BTRFS_DEBUG
1121         fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1122         if (!fs_info->debug_kobj) {
1123                 error = -ENOMEM;
1124                 goto failure;
1125         }
1126
1127         error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1128         if (error)
1129                 goto failure;
1130 #endif
1131
1132         error = addrm_unknown_feature_attrs(fs_info, true);
1133         if (error)
1134                 goto failure;
1135
1136         fs_info->space_info_kobj = kobject_create_and_add("allocation",
1137                                                   fsid_kobj);
1138         if (!fs_info->space_info_kobj) {
1139                 error = -ENOMEM;
1140                 goto failure;
1141         }
1142
1143         error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1144         if (error)
1145                 goto failure;
1146
1147         return 0;
1148 failure:
1149         btrfs_sysfs_remove_mounted(fs_info);
1150         return error;
1151 }
1152
1153
1154 /*
1155  * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1156  * values in superblock. Call after any changes to incompat/compat_ro flags
1157  */
1158 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1159                 u64 bit, enum btrfs_feature_set set)
1160 {
1161         struct btrfs_fs_devices *fs_devs;
1162         struct kobject *fsid_kobj;
1163         u64 features;
1164         int ret;
1165
1166         if (!fs_info)
1167                 return;
1168
1169         features = get_features(fs_info, set);
1170         ASSERT(bit & supported_feature_masks[set]);
1171
1172         fs_devs = fs_info->fs_devices;
1173         fsid_kobj = &fs_devs->fsid_kobj;
1174
1175         if (!fsid_kobj->state_initialized)
1176                 return;
1177
1178         /*
1179          * FIXME: this is too heavy to update just one value, ideally we'd like
1180          * to use sysfs_update_group but some refactoring is needed first.
1181          */
1182         sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1183         ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1184 }
1185
1186 int __init btrfs_init_sysfs(void)
1187 {
1188         int ret;
1189
1190         btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1191         if (!btrfs_kset)
1192                 return -ENOMEM;
1193
1194         init_feature_attrs();
1195         ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1196         if (ret)
1197                 goto out2;
1198         ret = sysfs_merge_group(&btrfs_kset->kobj,
1199                                 &btrfs_static_feature_attr_group);
1200         if (ret)
1201                 goto out_remove_group;
1202
1203 #ifdef CONFIG_BTRFS_DEBUG
1204         ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1205         if (ret)
1206                 goto out2;
1207 #endif
1208
1209         return 0;
1210
1211 out_remove_group:
1212         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1213 out2:
1214         kset_unregister(btrfs_kset);
1215
1216         return ret;
1217 }
1218
1219 void __cold btrfs_exit_sysfs(void)
1220 {
1221         sysfs_unmerge_group(&btrfs_kset->kobj,
1222                             &btrfs_static_feature_attr_group);
1223         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1224 #ifdef CONFIG_BTRFS_DEBUG
1225         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1226 #endif
1227         kset_unregister(btrfs_kset);
1228 }
1229