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