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