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/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/kobject.h>
11 #include <linux/bug.h>
12 #include <linux/debugfs.h>
13 #include <linux/sched/mm.h>
14
15 #include "ctree.h"
16 #include "disk-io.h"
17 #include "transaction.h"
18 #include "sysfs.h"
19 #include "volumes.h"
20
21 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
22 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
23
get_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set)24 static u64 get_features(struct btrfs_fs_info *fs_info,
25 enum btrfs_feature_set set)
26 {
27 struct btrfs_super_block *disk_super = fs_info->super_copy;
28 if (set == FEAT_COMPAT)
29 return btrfs_super_compat_flags(disk_super);
30 else if (set == FEAT_COMPAT_RO)
31 return btrfs_super_compat_ro_flags(disk_super);
32 else
33 return btrfs_super_incompat_flags(disk_super);
34 }
35
set_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set,u64 features)36 static void set_features(struct btrfs_fs_info *fs_info,
37 enum btrfs_feature_set set, u64 features)
38 {
39 struct btrfs_super_block *disk_super = fs_info->super_copy;
40 if (set == FEAT_COMPAT)
41 btrfs_set_super_compat_flags(disk_super, features);
42 else if (set == FEAT_COMPAT_RO)
43 btrfs_set_super_compat_ro_flags(disk_super, features);
44 else
45 btrfs_set_super_incompat_flags(disk_super, features);
46 }
47
can_modify_feature(struct btrfs_feature_attr * fa)48 static int can_modify_feature(struct btrfs_feature_attr *fa)
49 {
50 int val = 0;
51 u64 set, clear;
52 switch (fa->feature_set) {
53 case FEAT_COMPAT:
54 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
55 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
56 break;
57 case FEAT_COMPAT_RO:
58 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
59 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
60 break;
61 case FEAT_INCOMPAT:
62 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
63 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
64 break;
65 default:
66 pr_warn("btrfs: sysfs: unknown feature set %d\n",
67 fa->feature_set);
68 return 0;
69 }
70
71 if (set & fa->feature_bit)
72 val |= 1;
73 if (clear & fa->feature_bit)
74 val |= 2;
75
76 return val;
77 }
78
btrfs_feature_attr_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)79 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
80 struct kobj_attribute *a, char *buf)
81 {
82 int val = 0;
83 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
84 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
85 if (fs_info) {
86 u64 features = get_features(fs_info, fa->feature_set);
87 if (features & fa->feature_bit)
88 val = 1;
89 } else
90 val = can_modify_feature(fa);
91
92 return snprintf(buf, PAGE_SIZE, "%d\n", val);
93 }
94
btrfs_feature_attr_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t count)95 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
96 struct kobj_attribute *a,
97 const char *buf, size_t count)
98 {
99 struct btrfs_fs_info *fs_info;
100 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
101 u64 features, set, clear;
102 unsigned long val;
103 int ret;
104
105 fs_info = to_fs_info(kobj);
106 if (!fs_info)
107 return -EPERM;
108
109 if (sb_rdonly(fs_info->sb))
110 return -EROFS;
111
112 ret = kstrtoul(skip_spaces(buf), 0, &val);
113 if (ret)
114 return ret;
115
116 if (fa->feature_set == FEAT_COMPAT) {
117 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
118 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
119 } else if (fa->feature_set == FEAT_COMPAT_RO) {
120 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
121 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
122 } else {
123 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
124 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
125 }
126
127 features = get_features(fs_info, fa->feature_set);
128
129 /* Nothing to do */
130 if ((val && (features & fa->feature_bit)) ||
131 (!val && !(features & fa->feature_bit)))
132 return count;
133
134 if ((val && !(set & fa->feature_bit)) ||
135 (!val && !(clear & fa->feature_bit))) {
136 btrfs_info(fs_info,
137 "%sabling feature %s on mounted fs is not supported.",
138 val ? "En" : "Dis", fa->kobj_attr.attr.name);
139 return -EPERM;
140 }
141
142 btrfs_info(fs_info, "%s %s feature flag",
143 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
144
145 spin_lock(&fs_info->super_lock);
146 features = get_features(fs_info, fa->feature_set);
147 if (val)
148 features |= fa->feature_bit;
149 else
150 features &= ~fa->feature_bit;
151 set_features(fs_info, fa->feature_set, features);
152 spin_unlock(&fs_info->super_lock);
153
154 /*
155 * We don't want to do full transaction commit from inside sysfs
156 */
157 btrfs_set_pending(fs_info, COMMIT);
158 wake_up_process(fs_info->transaction_kthread);
159
160 return count;
161 }
162
btrfs_feature_visible(struct kobject * kobj,struct attribute * attr,int unused)163 static umode_t btrfs_feature_visible(struct kobject *kobj,
164 struct attribute *attr, int unused)
165 {
166 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
167 umode_t mode = attr->mode;
168
169 if (fs_info) {
170 struct btrfs_feature_attr *fa;
171 u64 features;
172
173 fa = attr_to_btrfs_feature_attr(attr);
174 features = get_features(fs_info, fa->feature_set);
175
176 if (can_modify_feature(fa))
177 mode |= S_IWUSR;
178 else if (!(features & fa->feature_bit))
179 mode = 0;
180 }
181
182 return mode;
183 }
184
185 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
186 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
187 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
188 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
189 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
190 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
191 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
192 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
193 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
194 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
195 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
196
197 static struct attribute *btrfs_supported_feature_attrs[] = {
198 BTRFS_FEAT_ATTR_PTR(mixed_backref),
199 BTRFS_FEAT_ATTR_PTR(default_subvol),
200 BTRFS_FEAT_ATTR_PTR(mixed_groups),
201 BTRFS_FEAT_ATTR_PTR(compress_lzo),
202 BTRFS_FEAT_ATTR_PTR(compress_zstd),
203 BTRFS_FEAT_ATTR_PTR(big_metadata),
204 BTRFS_FEAT_ATTR_PTR(extended_iref),
205 BTRFS_FEAT_ATTR_PTR(raid56),
206 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
207 BTRFS_FEAT_ATTR_PTR(no_holes),
208 BTRFS_FEAT_ATTR_PTR(free_space_tree),
209 NULL
210 };
211
212 /*
213 * Features which depend on feature bits and may differ between each fs.
214 *
215 * /sys/fs/btrfs/features lists all available features of this kernel while
216 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
217 * can be changed online.
218 */
219 static const struct attribute_group btrfs_feature_attr_group = {
220 .name = "features",
221 .is_visible = btrfs_feature_visible,
222 .attrs = btrfs_supported_feature_attrs,
223 };
224
rmdir_subvol_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)225 static ssize_t rmdir_subvol_show(struct kobject *kobj,
226 struct kobj_attribute *ka, char *buf)
227 {
228 return snprintf(buf, PAGE_SIZE, "0\n");
229 }
230 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
231
232 static struct attribute *btrfs_supported_static_feature_attrs[] = {
233 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
234 NULL
235 };
236
237 /*
238 * Features which only depend on kernel version.
239 *
240 * These are listed in /sys/fs/btrfs/features along with
241 * btrfs_feature_attr_group
242 */
243 static const struct attribute_group btrfs_static_feature_attr_group = {
244 .name = "features",
245 .attrs = btrfs_supported_static_feature_attrs,
246 };
247
btrfs_show_u64(u64 * value_ptr,spinlock_t * lock,char * buf)248 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
249 {
250 u64 val;
251 if (lock)
252 spin_lock(lock);
253 val = *value_ptr;
254 if (lock)
255 spin_unlock(lock);
256 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
257 }
258
global_rsv_size_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)259 static ssize_t global_rsv_size_show(struct kobject *kobj,
260 struct kobj_attribute *ka, char *buf)
261 {
262 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
263 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
264 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
265 }
266 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
267
global_rsv_reserved_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)268 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
269 struct kobj_attribute *a, char *buf)
270 {
271 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
272 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
273 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
274 }
275 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
276
277 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
278 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
279
280 static ssize_t raid_bytes_show(struct kobject *kobj,
281 struct kobj_attribute *attr, char *buf);
282 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
283 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
284
raid_bytes_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)285 static ssize_t raid_bytes_show(struct kobject *kobj,
286 struct kobj_attribute *attr, char *buf)
287
288 {
289 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
290 struct btrfs_block_group_cache *block_group;
291 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
292 u64 val = 0;
293
294 down_read(&sinfo->groups_sem);
295 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
296 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
297 val += block_group->key.offset;
298 else
299 val += btrfs_block_group_used(&block_group->item);
300 }
301 up_read(&sinfo->groups_sem);
302 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
303 }
304
305 static struct attribute *raid_attributes[] = {
306 BTRFS_ATTR_PTR(raid, total_bytes),
307 BTRFS_ATTR_PTR(raid, used_bytes),
308 NULL
309 };
310
release_raid_kobj(struct kobject * kobj)311 static void release_raid_kobj(struct kobject *kobj)
312 {
313 kfree(to_raid_kobj(kobj));
314 }
315
316 struct kobj_type btrfs_raid_ktype = {
317 .sysfs_ops = &kobj_sysfs_ops,
318 .release = release_raid_kobj,
319 .default_attrs = raid_attributes,
320 };
321
322 #define SPACE_INFO_ATTR(field) \
323 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
324 struct kobj_attribute *a, \
325 char *buf) \
326 { \
327 struct btrfs_space_info *sinfo = to_space_info(kobj); \
328 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
329 } \
330 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
331
btrfs_space_info_show_total_bytes_pinned(struct kobject * kobj,struct kobj_attribute * a,char * buf)332 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
333 struct kobj_attribute *a,
334 char *buf)
335 {
336 struct btrfs_space_info *sinfo = to_space_info(kobj);
337 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
338 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
339 }
340
341 SPACE_INFO_ATTR(flags);
342 SPACE_INFO_ATTR(total_bytes);
343 SPACE_INFO_ATTR(bytes_used);
344 SPACE_INFO_ATTR(bytes_pinned);
345 SPACE_INFO_ATTR(bytes_reserved);
346 SPACE_INFO_ATTR(bytes_may_use);
347 SPACE_INFO_ATTR(bytes_readonly);
348 SPACE_INFO_ATTR(disk_used);
349 SPACE_INFO_ATTR(disk_total);
350 BTRFS_ATTR(space_info, total_bytes_pinned,
351 btrfs_space_info_show_total_bytes_pinned);
352
353 static struct attribute *space_info_attrs[] = {
354 BTRFS_ATTR_PTR(space_info, flags),
355 BTRFS_ATTR_PTR(space_info, total_bytes),
356 BTRFS_ATTR_PTR(space_info, bytes_used),
357 BTRFS_ATTR_PTR(space_info, bytes_pinned),
358 BTRFS_ATTR_PTR(space_info, bytes_reserved),
359 BTRFS_ATTR_PTR(space_info, bytes_may_use),
360 BTRFS_ATTR_PTR(space_info, bytes_readonly),
361 BTRFS_ATTR_PTR(space_info, disk_used),
362 BTRFS_ATTR_PTR(space_info, disk_total),
363 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
364 NULL,
365 };
366
space_info_release(struct kobject * kobj)367 static void space_info_release(struct kobject *kobj)
368 {
369 struct btrfs_space_info *sinfo = to_space_info(kobj);
370 percpu_counter_destroy(&sinfo->total_bytes_pinned);
371 kfree(sinfo);
372 }
373
374 struct kobj_type space_info_ktype = {
375 .sysfs_ops = &kobj_sysfs_ops,
376 .release = space_info_release,
377 .default_attrs = space_info_attrs,
378 };
379
380 static const struct attribute *allocation_attrs[] = {
381 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
382 BTRFS_ATTR_PTR(allocation, global_rsv_size),
383 NULL,
384 };
385
btrfs_label_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)386 static ssize_t btrfs_label_show(struct kobject *kobj,
387 struct kobj_attribute *a, char *buf)
388 {
389 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
390 char *label = fs_info->super_copy->label;
391 ssize_t ret;
392
393 spin_lock(&fs_info->super_lock);
394 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
395 spin_unlock(&fs_info->super_lock);
396
397 return ret;
398 }
399
btrfs_label_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)400 static ssize_t btrfs_label_store(struct kobject *kobj,
401 struct kobj_attribute *a,
402 const char *buf, size_t len)
403 {
404 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
405 size_t p_len;
406
407 if (!fs_info)
408 return -EPERM;
409
410 if (sb_rdonly(fs_info->sb))
411 return -EROFS;
412
413 /*
414 * p_len is the len until the first occurrence of either
415 * '\n' or '\0'
416 */
417 p_len = strcspn(buf, "\n");
418
419 if (p_len >= BTRFS_LABEL_SIZE)
420 return -EINVAL;
421
422 spin_lock(&fs_info->super_lock);
423 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
424 memcpy(fs_info->super_copy->label, buf, p_len);
425 spin_unlock(&fs_info->super_lock);
426
427 /*
428 * We don't want to do full transaction commit from inside sysfs
429 */
430 btrfs_set_pending(fs_info, COMMIT);
431 wake_up_process(fs_info->transaction_kthread);
432
433 return len;
434 }
435 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
436
btrfs_nodesize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)437 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
438 struct kobj_attribute *a, char *buf)
439 {
440 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
441
442 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
443 }
444
445 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
446
btrfs_sectorsize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)447 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
448 struct kobj_attribute *a, char *buf)
449 {
450 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
451
452 return snprintf(buf, PAGE_SIZE, "%u\n",
453 fs_info->super_copy->sectorsize);
454 }
455
456 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
457
btrfs_clone_alignment_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)458 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
459 struct kobj_attribute *a, char *buf)
460 {
461 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
462
463 return snprintf(buf, PAGE_SIZE, "%u\n",
464 fs_info->super_copy->sectorsize);
465 }
466
467 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
468
quota_override_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)469 static ssize_t quota_override_show(struct kobject *kobj,
470 struct kobj_attribute *a, char *buf)
471 {
472 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
473 int quota_override;
474
475 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
476 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
477 }
478
quota_override_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)479 static ssize_t quota_override_store(struct kobject *kobj,
480 struct kobj_attribute *a,
481 const char *buf, size_t len)
482 {
483 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
484 unsigned long knob;
485 int err;
486
487 if (!fs_info)
488 return -EPERM;
489
490 if (!capable(CAP_SYS_RESOURCE))
491 return -EPERM;
492
493 err = kstrtoul(buf, 10, &knob);
494 if (err)
495 return err;
496 if (knob > 1)
497 return -EINVAL;
498
499 if (knob)
500 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
501 else
502 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
503
504 return len;
505 }
506
507 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
508
509 static const struct attribute *btrfs_attrs[] = {
510 BTRFS_ATTR_PTR(, label),
511 BTRFS_ATTR_PTR(, nodesize),
512 BTRFS_ATTR_PTR(, sectorsize),
513 BTRFS_ATTR_PTR(, clone_alignment),
514 BTRFS_ATTR_PTR(, quota_override),
515 NULL,
516 };
517
btrfs_release_fsid_kobj(struct kobject * kobj)518 static void btrfs_release_fsid_kobj(struct kobject *kobj)
519 {
520 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
521
522 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
523 complete(&fs_devs->kobj_unregister);
524 }
525
526 static struct kobj_type btrfs_ktype = {
527 .sysfs_ops = &kobj_sysfs_ops,
528 .release = btrfs_release_fsid_kobj,
529 };
530
to_fs_devs(struct kobject * kobj)531 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
532 {
533 if (kobj->ktype != &btrfs_ktype)
534 return NULL;
535 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
536 }
537
to_fs_info(struct kobject * kobj)538 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
539 {
540 if (kobj->ktype != &btrfs_ktype)
541 return NULL;
542 return to_fs_devs(kobj)->fs_info;
543 }
544
545 #define NUM_FEATURE_BITS 64
546 #define BTRFS_FEATURE_NAME_MAX 13
547 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
548 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
549
550 static const u64 supported_feature_masks[FEAT_MAX] = {
551 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
552 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
553 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
554 };
555
addrm_unknown_feature_attrs(struct btrfs_fs_info * fs_info,bool add)556 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
557 {
558 int set;
559
560 for (set = 0; set < FEAT_MAX; set++) {
561 int i;
562 struct attribute *attrs[2];
563 struct attribute_group agroup = {
564 .name = "features",
565 .attrs = attrs,
566 };
567 u64 features = get_features(fs_info, set);
568 features &= ~supported_feature_masks[set];
569
570 if (!features)
571 continue;
572
573 attrs[1] = NULL;
574 for (i = 0; i < NUM_FEATURE_BITS; i++) {
575 struct btrfs_feature_attr *fa;
576
577 if (!(features & (1ULL << i)))
578 continue;
579
580 fa = &btrfs_feature_attrs[set][i];
581 attrs[0] = &fa->kobj_attr.attr;
582 if (add) {
583 int ret;
584 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
585 &agroup);
586 if (ret)
587 return ret;
588 } else
589 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
590 &agroup);
591 }
592
593 }
594 return 0;
595 }
596
__btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)597 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
598 {
599 if (fs_devs->device_dir_kobj) {
600 kobject_del(fs_devs->device_dir_kobj);
601 kobject_put(fs_devs->device_dir_kobj);
602 fs_devs->device_dir_kobj = NULL;
603 }
604
605 if (fs_devs->fsid_kobj.state_initialized) {
606 kobject_del(&fs_devs->fsid_kobj);
607 kobject_put(&fs_devs->fsid_kobj);
608 wait_for_completion(&fs_devs->kobj_unregister);
609 }
610 }
611
612 /* when fs_devs is NULL it will remove all fsid kobject */
btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)613 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
614 {
615 struct list_head *fs_uuids = btrfs_get_fs_uuids();
616
617 if (fs_devs) {
618 __btrfs_sysfs_remove_fsid(fs_devs);
619 return;
620 }
621
622 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
623 __btrfs_sysfs_remove_fsid(fs_devs);
624 }
625 }
626
btrfs_sysfs_remove_mounted(struct btrfs_fs_info * fs_info)627 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
628 {
629 btrfs_reset_fs_info_ptr(fs_info);
630
631 if (fs_info->space_info_kobj) {
632 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
633 kobject_del(fs_info->space_info_kobj);
634 kobject_put(fs_info->space_info_kobj);
635 }
636 addrm_unknown_feature_attrs(fs_info, false);
637 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
638 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
639 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
640 }
641
642 const char * const btrfs_feature_set_names[FEAT_MAX] = {
643 [FEAT_COMPAT] = "compat",
644 [FEAT_COMPAT_RO] = "compat_ro",
645 [FEAT_INCOMPAT] = "incompat",
646 };
647
btrfs_printable_features(enum btrfs_feature_set set,u64 flags)648 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
649 {
650 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
651 int len = 0;
652 int i;
653 char *str;
654
655 str = kmalloc(bufsize, GFP_KERNEL);
656 if (!str)
657 return str;
658
659 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
660 const char *name;
661
662 if (!(flags & (1ULL << i)))
663 continue;
664
665 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
666 len += snprintf(str + len, bufsize - len, "%s%s",
667 len ? "," : "", name);
668 }
669
670 return str;
671 }
672
init_feature_attrs(void)673 static void init_feature_attrs(void)
674 {
675 struct btrfs_feature_attr *fa;
676 int set, i;
677
678 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
679 ARRAY_SIZE(btrfs_feature_attrs));
680 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
681 ARRAY_SIZE(btrfs_feature_attrs[0]));
682
683 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
684 memset(btrfs_unknown_feature_names, 0,
685 sizeof(btrfs_unknown_feature_names));
686
687 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
688 struct btrfs_feature_attr *sfa;
689 struct attribute *a = btrfs_supported_feature_attrs[i];
690 int bit;
691 sfa = attr_to_btrfs_feature_attr(a);
692 bit = ilog2(sfa->feature_bit);
693 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
694
695 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
696 }
697
698 for (set = 0; set < FEAT_MAX; set++) {
699 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
700 char *name = btrfs_unknown_feature_names[set][i];
701 fa = &btrfs_feature_attrs[set][i];
702
703 if (fa->kobj_attr.attr.name)
704 continue;
705
706 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
707 btrfs_feature_set_names[set], i);
708
709 fa->kobj_attr.attr.name = name;
710 fa->kobj_attr.attr.mode = S_IRUGO;
711 fa->feature_set = set;
712 fa->feature_bit = 1ULL << i;
713 }
714 }
715 }
716
717 /* when one_device is NULL, it removes all device links */
718
btrfs_sysfs_rm_device_link(struct btrfs_fs_devices * fs_devices,struct btrfs_device * one_device)719 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
720 struct btrfs_device *one_device)
721 {
722 struct hd_struct *disk;
723 struct kobject *disk_kobj;
724
725 if (!fs_devices->device_dir_kobj)
726 return -EINVAL;
727
728 if (one_device && one_device->bdev) {
729 disk = one_device->bdev->bd_part;
730 disk_kobj = &part_to_dev(disk)->kobj;
731
732 sysfs_remove_link(fs_devices->device_dir_kobj,
733 disk_kobj->name);
734 }
735
736 if (one_device)
737 return 0;
738
739 list_for_each_entry(one_device,
740 &fs_devices->devices, dev_list) {
741 if (!one_device->bdev)
742 continue;
743 disk = one_device->bdev->bd_part;
744 disk_kobj = &part_to_dev(disk)->kobj;
745
746 sysfs_remove_link(fs_devices->device_dir_kobj,
747 disk_kobj->name);
748 }
749
750 return 0;
751 }
752
btrfs_sysfs_add_device(struct btrfs_fs_devices * fs_devs)753 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
754 {
755 if (!fs_devs->device_dir_kobj)
756 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
757 &fs_devs->fsid_kobj);
758
759 if (!fs_devs->device_dir_kobj)
760 return -ENOMEM;
761
762 return 0;
763 }
764
btrfs_sysfs_add_device_link(struct btrfs_fs_devices * fs_devices,struct btrfs_device * one_device)765 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
766 struct btrfs_device *one_device)
767 {
768 int error = 0;
769 struct btrfs_device *dev;
770 unsigned int nofs_flag;
771
772 nofs_flag = memalloc_nofs_save();
773 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
774 struct hd_struct *disk;
775 struct kobject *disk_kobj;
776
777 if (!dev->bdev)
778 continue;
779
780 if (one_device && one_device != dev)
781 continue;
782
783 disk = dev->bdev->bd_part;
784 disk_kobj = &part_to_dev(disk)->kobj;
785
786 error = sysfs_create_link(fs_devices->device_dir_kobj,
787 disk_kobj, disk_kobj->name);
788 if (error)
789 break;
790 }
791 memalloc_nofs_restore(nofs_flag);
792
793 return error;
794 }
795
796 /* /sys/fs/btrfs/ entry */
797 static struct kset *btrfs_kset;
798
799 /* /sys/kernel/debug/btrfs */
800 static struct dentry *btrfs_debugfs_root_dentry;
801
802 /* Debugging tunables and exported data */
803 u64 btrfs_debugfs_test;
804
805 /*
806 * Can be called by the device discovery thread.
807 * And parent can be specified for seed device
808 */
btrfs_sysfs_add_fsid(struct btrfs_fs_devices * fs_devs,struct kobject * parent)809 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
810 struct kobject *parent)
811 {
812 int error;
813
814 init_completion(&fs_devs->kobj_unregister);
815 fs_devs->fsid_kobj.kset = btrfs_kset;
816 error = kobject_init_and_add(&fs_devs->fsid_kobj,
817 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
818 if (error) {
819 kobject_put(&fs_devs->fsid_kobj);
820 return error;
821 }
822
823 return 0;
824 }
825
btrfs_sysfs_add_mounted(struct btrfs_fs_info * fs_info)826 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
827 {
828 int error;
829 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
830 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
831
832 btrfs_set_fs_info_ptr(fs_info);
833
834 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
835 if (error)
836 return error;
837
838 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
839 if (error) {
840 btrfs_sysfs_rm_device_link(fs_devs, NULL);
841 return error;
842 }
843
844 error = sysfs_create_group(fsid_kobj,
845 &btrfs_feature_attr_group);
846 if (error)
847 goto failure;
848
849 error = addrm_unknown_feature_attrs(fs_info, true);
850 if (error)
851 goto failure;
852
853 fs_info->space_info_kobj = kobject_create_and_add("allocation",
854 fsid_kobj);
855 if (!fs_info->space_info_kobj) {
856 error = -ENOMEM;
857 goto failure;
858 }
859
860 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
861 if (error)
862 goto failure;
863
864 return 0;
865 failure:
866 btrfs_sysfs_remove_mounted(fs_info);
867 return error;
868 }
869
870
871 /*
872 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
873 * values in superblock. Call after any changes to incompat/compat_ro flags
874 */
btrfs_sysfs_feature_update(struct btrfs_fs_info * fs_info,u64 bit,enum btrfs_feature_set set)875 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
876 u64 bit, enum btrfs_feature_set set)
877 {
878 struct btrfs_fs_devices *fs_devs;
879 struct kobject *fsid_kobj;
880 u64 features;
881 int ret;
882
883 if (!fs_info)
884 return;
885
886 features = get_features(fs_info, set);
887 ASSERT(bit & supported_feature_masks[set]);
888
889 fs_devs = fs_info->fs_devices;
890 fsid_kobj = &fs_devs->fsid_kobj;
891
892 if (!fsid_kobj->state_initialized)
893 return;
894
895 /*
896 * FIXME: this is too heavy to update just one value, ideally we'd like
897 * to use sysfs_update_group but some refactoring is needed first.
898 */
899 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
900 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
901 }
902
btrfs_init_debugfs(void)903 static int btrfs_init_debugfs(void)
904 {
905 #ifdef CONFIG_DEBUG_FS
906 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
907 if (!btrfs_debugfs_root_dentry)
908 return -ENOMEM;
909
910 /*
911 * Example code, how to export data through debugfs.
912 *
913 * file: /sys/kernel/debug/btrfs/test
914 * contents of: btrfs_debugfs_test
915 */
916 #ifdef CONFIG_BTRFS_DEBUG
917 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
918 &btrfs_debugfs_test);
919 #endif
920
921 #endif
922 return 0;
923 }
924
btrfs_init_sysfs(void)925 int __init btrfs_init_sysfs(void)
926 {
927 int ret;
928
929 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
930 if (!btrfs_kset)
931 return -ENOMEM;
932
933 ret = btrfs_init_debugfs();
934 if (ret)
935 goto out1;
936
937 init_feature_attrs();
938 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
939 if (ret)
940 goto out2;
941 ret = sysfs_merge_group(&btrfs_kset->kobj,
942 &btrfs_static_feature_attr_group);
943 if (ret)
944 goto out_remove_group;
945
946 return 0;
947
948 out_remove_group:
949 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
950 out2:
951 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
952 out1:
953 kset_unregister(btrfs_kset);
954
955 return ret;
956 }
957
btrfs_exit_sysfs(void)958 void __cold btrfs_exit_sysfs(void)
959 {
960 sysfs_unmerge_group(&btrfs_kset->kobj,
961 &btrfs_static_feature_attr_group);
962 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
963 kset_unregister(btrfs_kset);
964 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
965 }
966
967