xref: /openbmc/linux/fs/btrfs/sysfs.c (revision 84b102f5)
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 "discard.h"
16 #include "disk-io.h"
17 #include "send.h"
18 #include "transaction.h"
19 #include "sysfs.h"
20 #include "volumes.h"
21 #include "space-info.h"
22 #include "block-group.h"
23 #include "qgroup.h"
24 
25 struct btrfs_feature_attr {
26 	struct kobj_attribute kobj_attr;
27 	enum btrfs_feature_set feature_set;
28 	u64 feature_bit;
29 };
30 
31 /* For raid type sysfs entries */
32 struct raid_kobject {
33 	u64 flags;
34 	struct kobject kobj;
35 };
36 
37 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)			\
38 {									\
39 	.attr	= { .name = __stringify(_name), .mode = _mode },	\
40 	.show	= _show,						\
41 	.store	= _store,						\
42 }
43 
44 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store)			\
45 	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
46 			__INIT_KOBJ_ATTR(_name, 0644, _show, _store)
47 
48 #define BTRFS_ATTR(_prefix, _name, _show)				\
49 	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
50 			__INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
51 
52 #define BTRFS_ATTR_PTR(_prefix, _name)					\
53 	(&btrfs_attr_##_prefix##_##_name.attr)
54 
55 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
56 static struct btrfs_feature_attr btrfs_attr_features_##_name = {	     \
57 	.kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,			     \
58 				      btrfs_feature_attr_show,		     \
59 				      btrfs_feature_attr_store),	     \
60 	.feature_set	= _feature_set,					     \
61 	.feature_bit	= _feature_prefix ##_## _feature_bit,		     \
62 }
63 #define BTRFS_FEAT_ATTR_PTR(_name)					     \
64 	(&btrfs_attr_features_##_name.kobj_attr.attr)
65 
66 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
67 	BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
68 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
69 	BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
70 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
71 	BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
72 
73 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
74 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
75 
76 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
77 {
78 	return container_of(a, struct btrfs_feature_attr, kobj_attr);
79 }
80 
81 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
82 {
83 	return container_of(attr, struct kobj_attribute, attr);
84 }
85 
86 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
87 		struct attribute *attr)
88 {
89 	return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
90 }
91 
92 static u64 get_features(struct btrfs_fs_info *fs_info,
93 			enum btrfs_feature_set set)
94 {
95 	struct btrfs_super_block *disk_super = fs_info->super_copy;
96 	if (set == FEAT_COMPAT)
97 		return btrfs_super_compat_flags(disk_super);
98 	else if (set == FEAT_COMPAT_RO)
99 		return btrfs_super_compat_ro_flags(disk_super);
100 	else
101 		return btrfs_super_incompat_flags(disk_super);
102 }
103 
104 static void set_features(struct btrfs_fs_info *fs_info,
105 			 enum btrfs_feature_set set, u64 features)
106 {
107 	struct btrfs_super_block *disk_super = fs_info->super_copy;
108 	if (set == FEAT_COMPAT)
109 		btrfs_set_super_compat_flags(disk_super, features);
110 	else if (set == FEAT_COMPAT_RO)
111 		btrfs_set_super_compat_ro_flags(disk_super, features);
112 	else
113 		btrfs_set_super_incompat_flags(disk_super, features);
114 }
115 
116 static int can_modify_feature(struct btrfs_feature_attr *fa)
117 {
118 	int val = 0;
119 	u64 set, clear;
120 	switch (fa->feature_set) {
121 	case FEAT_COMPAT:
122 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
123 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
124 		break;
125 	case FEAT_COMPAT_RO:
126 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
127 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
128 		break;
129 	case FEAT_INCOMPAT:
130 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
131 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
132 		break;
133 	default:
134 		pr_warn("btrfs: sysfs: unknown feature set %d\n",
135 				fa->feature_set);
136 		return 0;
137 	}
138 
139 	if (set & fa->feature_bit)
140 		val |= 1;
141 	if (clear & fa->feature_bit)
142 		val |= 2;
143 
144 	return val;
145 }
146 
147 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
148 				       struct kobj_attribute *a, char *buf)
149 {
150 	int val = 0;
151 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
152 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
153 	if (fs_info) {
154 		u64 features = get_features(fs_info, fa->feature_set);
155 		if (features & fa->feature_bit)
156 			val = 1;
157 	} else
158 		val = can_modify_feature(fa);
159 
160 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
161 }
162 
163 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
164 					struct kobj_attribute *a,
165 					const char *buf, size_t count)
166 {
167 	struct btrfs_fs_info *fs_info;
168 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
169 	u64 features, set, clear;
170 	unsigned long val;
171 	int ret;
172 
173 	fs_info = to_fs_info(kobj);
174 	if (!fs_info)
175 		return -EPERM;
176 
177 	if (sb_rdonly(fs_info->sb))
178 		return -EROFS;
179 
180 	ret = kstrtoul(skip_spaces(buf), 0, &val);
181 	if (ret)
182 		return ret;
183 
184 	if (fa->feature_set == FEAT_COMPAT) {
185 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
186 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
187 	} else if (fa->feature_set == FEAT_COMPAT_RO) {
188 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
189 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
190 	} else {
191 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
192 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
193 	}
194 
195 	features = get_features(fs_info, fa->feature_set);
196 
197 	/* Nothing to do */
198 	if ((val && (features & fa->feature_bit)) ||
199 	    (!val && !(features & fa->feature_bit)))
200 		return count;
201 
202 	if ((val && !(set & fa->feature_bit)) ||
203 	    (!val && !(clear & fa->feature_bit))) {
204 		btrfs_info(fs_info,
205 			"%sabling feature %s on mounted fs is not supported.",
206 			val ? "En" : "Dis", fa->kobj_attr.attr.name);
207 		return -EPERM;
208 	}
209 
210 	btrfs_info(fs_info, "%s %s feature flag",
211 		   val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
212 
213 	spin_lock(&fs_info->super_lock);
214 	features = get_features(fs_info, fa->feature_set);
215 	if (val)
216 		features |= fa->feature_bit;
217 	else
218 		features &= ~fa->feature_bit;
219 	set_features(fs_info, fa->feature_set, features);
220 	spin_unlock(&fs_info->super_lock);
221 
222 	/*
223 	 * We don't want to do full transaction commit from inside sysfs
224 	 */
225 	btrfs_set_pending(fs_info, COMMIT);
226 	wake_up_process(fs_info->transaction_kthread);
227 
228 	return count;
229 }
230 
231 static umode_t btrfs_feature_visible(struct kobject *kobj,
232 				     struct attribute *attr, int unused)
233 {
234 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
235 	umode_t mode = attr->mode;
236 
237 	if (fs_info) {
238 		struct btrfs_feature_attr *fa;
239 		u64 features;
240 
241 		fa = attr_to_btrfs_feature_attr(attr);
242 		features = get_features(fs_info, fa->feature_set);
243 
244 		if (can_modify_feature(fa))
245 			mode |= S_IWUSR;
246 		else if (!(features & fa->feature_bit))
247 			mode = 0;
248 	}
249 
250 	return mode;
251 }
252 
253 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
254 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
255 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
256 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
257 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
258 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
259 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
260 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
261 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
262 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
263 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
264 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
265 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
266 /* Remove once support for zoned allocation is feature complete */
267 #ifdef CONFIG_BTRFS_DEBUG
268 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
269 #endif
270 
271 static struct attribute *btrfs_supported_feature_attrs[] = {
272 	BTRFS_FEAT_ATTR_PTR(mixed_backref),
273 	BTRFS_FEAT_ATTR_PTR(default_subvol),
274 	BTRFS_FEAT_ATTR_PTR(mixed_groups),
275 	BTRFS_FEAT_ATTR_PTR(compress_lzo),
276 	BTRFS_FEAT_ATTR_PTR(compress_zstd),
277 	BTRFS_FEAT_ATTR_PTR(big_metadata),
278 	BTRFS_FEAT_ATTR_PTR(extended_iref),
279 	BTRFS_FEAT_ATTR_PTR(raid56),
280 	BTRFS_FEAT_ATTR_PTR(skinny_metadata),
281 	BTRFS_FEAT_ATTR_PTR(no_holes),
282 	BTRFS_FEAT_ATTR_PTR(metadata_uuid),
283 	BTRFS_FEAT_ATTR_PTR(free_space_tree),
284 	BTRFS_FEAT_ATTR_PTR(raid1c34),
285 #ifdef CONFIG_BTRFS_DEBUG
286 	BTRFS_FEAT_ATTR_PTR(zoned),
287 #endif
288 	NULL
289 };
290 
291 /*
292  * Features which depend on feature bits and may differ between each fs.
293  *
294  * /sys/fs/btrfs/features lists all available features of this kernel while
295  * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
296  * can be changed online.
297  */
298 static const struct attribute_group btrfs_feature_attr_group = {
299 	.name = "features",
300 	.is_visible = btrfs_feature_visible,
301 	.attrs = btrfs_supported_feature_attrs,
302 };
303 
304 static ssize_t rmdir_subvol_show(struct kobject *kobj,
305 				 struct kobj_attribute *ka, char *buf)
306 {
307 	return scnprintf(buf, PAGE_SIZE, "0\n");
308 }
309 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
310 
311 static ssize_t supported_checksums_show(struct kobject *kobj,
312 					struct kobj_attribute *a, char *buf)
313 {
314 	ssize_t ret = 0;
315 	int i;
316 
317 	for (i = 0; i < btrfs_get_num_csums(); i++) {
318 		/*
319 		 * This "trick" only works as long as 'enum btrfs_csum_type' has
320 		 * no holes in it
321 		 */
322 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
323 				(i == 0 ? "" : " "), btrfs_super_csum_name(i));
324 
325 	}
326 
327 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
328 	return ret;
329 }
330 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
331 
332 static ssize_t send_stream_version_show(struct kobject *kobj,
333 					struct kobj_attribute *ka, char *buf)
334 {
335 	return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
336 }
337 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
338 
339 static const char *rescue_opts[] = {
340 	"usebackuproot",
341 	"nologreplay",
342 	"ignorebadroots",
343 	"ignoredatacsums",
344 	"all",
345 };
346 
347 static ssize_t supported_rescue_options_show(struct kobject *kobj,
348 					     struct kobj_attribute *a,
349 					     char *buf)
350 {
351 	ssize_t ret = 0;
352 	int i;
353 
354 	for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
355 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
356 				 (i ? " " : ""), rescue_opts[i]);
357 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
358 	return ret;
359 }
360 BTRFS_ATTR(static_feature, supported_rescue_options,
361 	   supported_rescue_options_show);
362 
363 static struct attribute *btrfs_supported_static_feature_attrs[] = {
364 	BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
365 	BTRFS_ATTR_PTR(static_feature, supported_checksums),
366 	BTRFS_ATTR_PTR(static_feature, send_stream_version),
367 	BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
368 	NULL
369 };
370 
371 /*
372  * Features which only depend on kernel version.
373  *
374  * These are listed in /sys/fs/btrfs/features along with
375  * btrfs_feature_attr_group
376  */
377 static const struct attribute_group btrfs_static_feature_attr_group = {
378 	.name = "features",
379 	.attrs = btrfs_supported_static_feature_attrs,
380 };
381 
382 #ifdef CONFIG_BTRFS_DEBUG
383 
384 /*
385  * Discard statistics and tunables
386  */
387 #define discard_to_fs_info(_kobj)	to_fs_info((_kobj)->parent->parent)
388 
389 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
390 					    struct kobj_attribute *a,
391 					    char *buf)
392 {
393 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
394 
395 	return scnprintf(buf, PAGE_SIZE, "%lld\n",
396 			atomic64_read(&fs_info->discard_ctl.discardable_bytes));
397 }
398 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
399 
400 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
401 					      struct kobj_attribute *a,
402 					      char *buf)
403 {
404 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
405 
406 	return scnprintf(buf, PAGE_SIZE, "%d\n",
407 			atomic_read(&fs_info->discard_ctl.discardable_extents));
408 }
409 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
410 
411 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
412 					       struct kobj_attribute *a,
413 					       char *buf)
414 {
415 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
416 
417 	return scnprintf(buf, PAGE_SIZE, "%lld\n",
418 			fs_info->discard_ctl.discard_bitmap_bytes);
419 }
420 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
421 
422 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
423 					      struct kobj_attribute *a,
424 					      char *buf)
425 {
426 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
427 
428 	return scnprintf(buf, PAGE_SIZE, "%lld\n",
429 		atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
430 }
431 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
432 
433 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
434 					       struct kobj_attribute *a,
435 					       char *buf)
436 {
437 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
438 
439 	return scnprintf(buf, PAGE_SIZE, "%lld\n",
440 			fs_info->discard_ctl.discard_extent_bytes);
441 }
442 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
443 
444 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
445 					     struct kobj_attribute *a,
446 					     char *buf)
447 {
448 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
449 
450 	return scnprintf(buf, PAGE_SIZE, "%u\n",
451 			READ_ONCE(fs_info->discard_ctl.iops_limit));
452 }
453 
454 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
455 					      struct kobj_attribute *a,
456 					      const char *buf, size_t len)
457 {
458 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
459 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
460 	u32 iops_limit;
461 	int ret;
462 
463 	ret = kstrtou32(buf, 10, &iops_limit);
464 	if (ret)
465 		return -EINVAL;
466 
467 	WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
468 	btrfs_discard_calc_delay(discard_ctl);
469 	btrfs_discard_schedule_work(discard_ctl, true);
470 	return len;
471 }
472 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
473 	      btrfs_discard_iops_limit_store);
474 
475 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
476 					     struct kobj_attribute *a,
477 					     char *buf)
478 {
479 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
480 
481 	return scnprintf(buf, PAGE_SIZE, "%u\n",
482 			READ_ONCE(fs_info->discard_ctl.kbps_limit));
483 }
484 
485 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
486 					      struct kobj_attribute *a,
487 					      const char *buf, size_t len)
488 {
489 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
490 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
491 	u32 kbps_limit;
492 	int ret;
493 
494 	ret = kstrtou32(buf, 10, &kbps_limit);
495 	if (ret)
496 		return -EINVAL;
497 
498 	WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
499 	btrfs_discard_schedule_work(discard_ctl, true);
500 	return len;
501 }
502 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
503 	      btrfs_discard_kbps_limit_store);
504 
505 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
506 						   struct kobj_attribute *a,
507 						   char *buf)
508 {
509 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
510 
511 	return scnprintf(buf, PAGE_SIZE, "%llu\n",
512 			READ_ONCE(fs_info->discard_ctl.max_discard_size));
513 }
514 
515 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
516 						    struct kobj_attribute *a,
517 						    const char *buf, size_t len)
518 {
519 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
520 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
521 	u64 max_discard_size;
522 	int ret;
523 
524 	ret = kstrtou64(buf, 10, &max_discard_size);
525 	if (ret)
526 		return -EINVAL;
527 
528 	WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
529 
530 	return len;
531 }
532 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
533 	      btrfs_discard_max_discard_size_store);
534 
535 static const struct attribute *discard_debug_attrs[] = {
536 	BTRFS_ATTR_PTR(discard, discardable_bytes),
537 	BTRFS_ATTR_PTR(discard, discardable_extents),
538 	BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
539 	BTRFS_ATTR_PTR(discard, discard_bytes_saved),
540 	BTRFS_ATTR_PTR(discard, discard_extent_bytes),
541 	BTRFS_ATTR_PTR(discard, iops_limit),
542 	BTRFS_ATTR_PTR(discard, kbps_limit),
543 	BTRFS_ATTR_PTR(discard, max_discard_size),
544 	NULL,
545 };
546 
547 /*
548  * Runtime debugging exported via sysfs
549  *
550  * /sys/fs/btrfs/debug - applies to module or all filesystems
551  * /sys/fs/btrfs/UUID  - applies only to the given filesystem
552  */
553 static const struct attribute *btrfs_debug_mount_attrs[] = {
554 	NULL,
555 };
556 
557 static struct attribute *btrfs_debug_feature_attrs[] = {
558 	NULL
559 };
560 
561 static const struct attribute_group btrfs_debug_feature_attr_group = {
562 	.name = "debug",
563 	.attrs = btrfs_debug_feature_attrs,
564 };
565 
566 #endif
567 
568 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
569 {
570 	u64 val;
571 	if (lock)
572 		spin_lock(lock);
573 	val = *value_ptr;
574 	if (lock)
575 		spin_unlock(lock);
576 	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
577 }
578 
579 static ssize_t global_rsv_size_show(struct kobject *kobj,
580 				    struct kobj_attribute *ka, char *buf)
581 {
582 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
583 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
584 	return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
585 }
586 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
587 
588 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
589 					struct kobj_attribute *a, char *buf)
590 {
591 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
592 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
593 	return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
594 }
595 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
596 
597 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
598 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
599 
600 static ssize_t raid_bytes_show(struct kobject *kobj,
601 			       struct kobj_attribute *attr, char *buf);
602 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
603 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
604 
605 static ssize_t raid_bytes_show(struct kobject *kobj,
606 			       struct kobj_attribute *attr, char *buf)
607 
608 {
609 	struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
610 	struct btrfs_block_group *block_group;
611 	int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
612 	u64 val = 0;
613 
614 	down_read(&sinfo->groups_sem);
615 	list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
616 		if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
617 			val += block_group->length;
618 		else
619 			val += block_group->used;
620 	}
621 	up_read(&sinfo->groups_sem);
622 	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
623 }
624 
625 static struct attribute *raid_attrs[] = {
626 	BTRFS_ATTR_PTR(raid, total_bytes),
627 	BTRFS_ATTR_PTR(raid, used_bytes),
628 	NULL
629 };
630 ATTRIBUTE_GROUPS(raid);
631 
632 static void release_raid_kobj(struct kobject *kobj)
633 {
634 	kfree(to_raid_kobj(kobj));
635 }
636 
637 static struct kobj_type btrfs_raid_ktype = {
638 	.sysfs_ops = &kobj_sysfs_ops,
639 	.release = release_raid_kobj,
640 	.default_groups = raid_groups,
641 };
642 
643 #define SPACE_INFO_ATTR(field)						\
644 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,	\
645 					     struct kobj_attribute *a,	\
646 					     char *buf)			\
647 {									\
648 	struct btrfs_space_info *sinfo = to_space_info(kobj);		\
649 	return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);	\
650 }									\
651 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
652 
653 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
654 						       struct kobj_attribute *a,
655 						       char *buf)
656 {
657 	struct btrfs_space_info *sinfo = to_space_info(kobj);
658 	s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
659 	return scnprintf(buf, PAGE_SIZE, "%lld\n", val);
660 }
661 
662 SPACE_INFO_ATTR(flags);
663 SPACE_INFO_ATTR(total_bytes);
664 SPACE_INFO_ATTR(bytes_used);
665 SPACE_INFO_ATTR(bytes_pinned);
666 SPACE_INFO_ATTR(bytes_reserved);
667 SPACE_INFO_ATTR(bytes_may_use);
668 SPACE_INFO_ATTR(bytes_readonly);
669 SPACE_INFO_ATTR(disk_used);
670 SPACE_INFO_ATTR(disk_total);
671 BTRFS_ATTR(space_info, total_bytes_pinned,
672 	   btrfs_space_info_show_total_bytes_pinned);
673 
674 static struct attribute *space_info_attrs[] = {
675 	BTRFS_ATTR_PTR(space_info, flags),
676 	BTRFS_ATTR_PTR(space_info, total_bytes),
677 	BTRFS_ATTR_PTR(space_info, bytes_used),
678 	BTRFS_ATTR_PTR(space_info, bytes_pinned),
679 	BTRFS_ATTR_PTR(space_info, bytes_reserved),
680 	BTRFS_ATTR_PTR(space_info, bytes_may_use),
681 	BTRFS_ATTR_PTR(space_info, bytes_readonly),
682 	BTRFS_ATTR_PTR(space_info, disk_used),
683 	BTRFS_ATTR_PTR(space_info, disk_total),
684 	BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
685 	NULL,
686 };
687 ATTRIBUTE_GROUPS(space_info);
688 
689 static void space_info_release(struct kobject *kobj)
690 {
691 	struct btrfs_space_info *sinfo = to_space_info(kobj);
692 	percpu_counter_destroy(&sinfo->total_bytes_pinned);
693 	kfree(sinfo);
694 }
695 
696 static struct kobj_type space_info_ktype = {
697 	.sysfs_ops = &kobj_sysfs_ops,
698 	.release = space_info_release,
699 	.default_groups = space_info_groups,
700 };
701 
702 static const struct attribute *allocation_attrs[] = {
703 	BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
704 	BTRFS_ATTR_PTR(allocation, global_rsv_size),
705 	NULL,
706 };
707 
708 static ssize_t btrfs_label_show(struct kobject *kobj,
709 				struct kobj_attribute *a, char *buf)
710 {
711 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
712 	char *label = fs_info->super_copy->label;
713 	ssize_t ret;
714 
715 	spin_lock(&fs_info->super_lock);
716 	ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
717 	spin_unlock(&fs_info->super_lock);
718 
719 	return ret;
720 }
721 
722 static ssize_t btrfs_label_store(struct kobject *kobj,
723 				 struct kobj_attribute *a,
724 				 const char *buf, size_t len)
725 {
726 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
727 	size_t p_len;
728 
729 	if (!fs_info)
730 		return -EPERM;
731 
732 	if (sb_rdonly(fs_info->sb))
733 		return -EROFS;
734 
735 	/*
736 	 * p_len is the len until the first occurrence of either
737 	 * '\n' or '\0'
738 	 */
739 	p_len = strcspn(buf, "\n");
740 
741 	if (p_len >= BTRFS_LABEL_SIZE)
742 		return -EINVAL;
743 
744 	spin_lock(&fs_info->super_lock);
745 	memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
746 	memcpy(fs_info->super_copy->label, buf, p_len);
747 	spin_unlock(&fs_info->super_lock);
748 
749 	/*
750 	 * We don't want to do full transaction commit from inside sysfs
751 	 */
752 	btrfs_set_pending(fs_info, COMMIT);
753 	wake_up_process(fs_info->transaction_kthread);
754 
755 	return len;
756 }
757 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
758 
759 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
760 				struct kobj_attribute *a, char *buf)
761 {
762 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
763 
764 	return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
765 }
766 
767 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
768 
769 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
770 				struct kobj_attribute *a, char *buf)
771 {
772 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
773 
774 	return scnprintf(buf, PAGE_SIZE, "%u\n",
775 			 fs_info->super_copy->sectorsize);
776 }
777 
778 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
779 
780 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
781 				struct kobj_attribute *a, char *buf)
782 {
783 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
784 
785 	return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
786 }
787 
788 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
789 
790 static ssize_t quota_override_show(struct kobject *kobj,
791 				   struct kobj_attribute *a, char *buf)
792 {
793 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
794 	int quota_override;
795 
796 	quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
797 	return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
798 }
799 
800 static ssize_t quota_override_store(struct kobject *kobj,
801 				    struct kobj_attribute *a,
802 				    const char *buf, size_t len)
803 {
804 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
805 	unsigned long knob;
806 	int err;
807 
808 	if (!fs_info)
809 		return -EPERM;
810 
811 	if (!capable(CAP_SYS_RESOURCE))
812 		return -EPERM;
813 
814 	err = kstrtoul(buf, 10, &knob);
815 	if (err)
816 		return err;
817 	if (knob > 1)
818 		return -EINVAL;
819 
820 	if (knob)
821 		set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
822 	else
823 		clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
824 
825 	return len;
826 }
827 
828 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
829 
830 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
831 				struct kobj_attribute *a, char *buf)
832 {
833 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
834 
835 	return scnprintf(buf, PAGE_SIZE, "%pU\n",
836 			fs_info->fs_devices->metadata_uuid);
837 }
838 
839 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
840 
841 static ssize_t btrfs_checksum_show(struct kobject *kobj,
842 				   struct kobj_attribute *a, char *buf)
843 {
844 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
845 	u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
846 
847 	return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
848 			btrfs_super_csum_name(csum_type),
849 			crypto_shash_driver_name(fs_info->csum_shash));
850 }
851 
852 BTRFS_ATTR(, checksum, btrfs_checksum_show);
853 
854 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
855 		struct kobj_attribute *a, char *buf)
856 {
857 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
858 	const char *str;
859 
860 	switch (READ_ONCE(fs_info->exclusive_operation)) {
861 		case  BTRFS_EXCLOP_NONE:
862 			str = "none\n";
863 			break;
864 		case BTRFS_EXCLOP_BALANCE:
865 			str = "balance\n";
866 			break;
867 		case BTRFS_EXCLOP_DEV_ADD:
868 			str = "device add\n";
869 			break;
870 		case BTRFS_EXCLOP_DEV_REMOVE:
871 			str = "device remove\n";
872 			break;
873 		case BTRFS_EXCLOP_DEV_REPLACE:
874 			str = "device replace\n";
875 			break;
876 		case BTRFS_EXCLOP_RESIZE:
877 			str = "resize\n";
878 			break;
879 		case BTRFS_EXCLOP_SWAP_ACTIVATE:
880 			str = "swap activate\n";
881 			break;
882 		default:
883 			str = "UNKNOWN\n";
884 			break;
885 	}
886 	return scnprintf(buf, PAGE_SIZE, "%s", str);
887 }
888 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
889 
890 static ssize_t btrfs_generation_show(struct kobject *kobj,
891 				     struct kobj_attribute *a, char *buf)
892 {
893 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
894 
895 	return scnprintf(buf, PAGE_SIZE, "%llu\n", fs_info->generation);
896 }
897 BTRFS_ATTR(, generation, btrfs_generation_show);
898 
899 /*
900  * Look for an exact string @string in @buffer with possible leading or
901  * trailing whitespace
902  */
903 static bool strmatch(const char *buffer, const char *string)
904 {
905 	const size_t len = strlen(string);
906 
907 	/* Skip leading whitespace */
908 	buffer = skip_spaces(buffer);
909 
910 	/* Match entire string, check if the rest is whitespace or empty */
911 	if (strncmp(string, buffer, len) == 0 &&
912 	    strlen(skip_spaces(buffer + len)) == 0)
913 		return true;
914 
915 	return false;
916 }
917 
918 static const char * const btrfs_read_policy_name[] = { "pid" };
919 
920 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
921 				      struct kobj_attribute *a, char *buf)
922 {
923 	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
924 	ssize_t ret = 0;
925 	int i;
926 
927 	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
928 		if (fs_devices->read_policy == i)
929 			ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
930 					 (ret == 0 ? "" : " "),
931 					 btrfs_read_policy_name[i]);
932 		else
933 			ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
934 					 (ret == 0 ? "" : " "),
935 					 btrfs_read_policy_name[i]);
936 	}
937 
938 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
939 
940 	return ret;
941 }
942 
943 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
944 				       struct kobj_attribute *a,
945 				       const char *buf, size_t len)
946 {
947 	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
948 	int i;
949 
950 	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
951 		if (strmatch(buf, btrfs_read_policy_name[i])) {
952 			if (i != fs_devices->read_policy) {
953 				fs_devices->read_policy = i;
954 				btrfs_info(fs_devices->fs_info,
955 					   "read policy set to '%s'",
956 					   btrfs_read_policy_name[i]);
957 			}
958 			return len;
959 		}
960 	}
961 
962 	return -EINVAL;
963 }
964 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
965 
966 static const struct attribute *btrfs_attrs[] = {
967 	BTRFS_ATTR_PTR(, label),
968 	BTRFS_ATTR_PTR(, nodesize),
969 	BTRFS_ATTR_PTR(, sectorsize),
970 	BTRFS_ATTR_PTR(, clone_alignment),
971 	BTRFS_ATTR_PTR(, quota_override),
972 	BTRFS_ATTR_PTR(, metadata_uuid),
973 	BTRFS_ATTR_PTR(, checksum),
974 	BTRFS_ATTR_PTR(, exclusive_operation),
975 	BTRFS_ATTR_PTR(, generation),
976 	BTRFS_ATTR_PTR(, read_policy),
977 	NULL,
978 };
979 
980 static void btrfs_release_fsid_kobj(struct kobject *kobj)
981 {
982 	struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
983 
984 	memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
985 	complete(&fs_devs->kobj_unregister);
986 }
987 
988 static struct kobj_type btrfs_ktype = {
989 	.sysfs_ops	= &kobj_sysfs_ops,
990 	.release	= btrfs_release_fsid_kobj,
991 };
992 
993 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
994 {
995 	if (kobj->ktype != &btrfs_ktype)
996 		return NULL;
997 	return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
998 }
999 
1000 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1001 {
1002 	if (kobj->ktype != &btrfs_ktype)
1003 		return NULL;
1004 	return to_fs_devs(kobj)->fs_info;
1005 }
1006 
1007 #define NUM_FEATURE_BITS 64
1008 #define BTRFS_FEATURE_NAME_MAX 13
1009 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1010 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1011 
1012 static const u64 supported_feature_masks[FEAT_MAX] = {
1013 	[FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
1014 	[FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1015 	[FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
1016 };
1017 
1018 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1019 {
1020 	int set;
1021 
1022 	for (set = 0; set < FEAT_MAX; set++) {
1023 		int i;
1024 		struct attribute *attrs[2];
1025 		struct attribute_group agroup = {
1026 			.name = "features",
1027 			.attrs = attrs,
1028 		};
1029 		u64 features = get_features(fs_info, set);
1030 		features &= ~supported_feature_masks[set];
1031 
1032 		if (!features)
1033 			continue;
1034 
1035 		attrs[1] = NULL;
1036 		for (i = 0; i < NUM_FEATURE_BITS; i++) {
1037 			struct btrfs_feature_attr *fa;
1038 
1039 			if (!(features & (1ULL << i)))
1040 				continue;
1041 
1042 			fa = &btrfs_feature_attrs[set][i];
1043 			attrs[0] = &fa->kobj_attr.attr;
1044 			if (add) {
1045 				int ret;
1046 				ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1047 							&agroup);
1048 				if (ret)
1049 					return ret;
1050 			} else
1051 				sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1052 						    &agroup);
1053 		}
1054 
1055 	}
1056 	return 0;
1057 }
1058 
1059 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1060 {
1061 	if (fs_devs->devinfo_kobj) {
1062 		kobject_del(fs_devs->devinfo_kobj);
1063 		kobject_put(fs_devs->devinfo_kobj);
1064 		fs_devs->devinfo_kobj = NULL;
1065 	}
1066 
1067 	if (fs_devs->devices_kobj) {
1068 		kobject_del(fs_devs->devices_kobj);
1069 		kobject_put(fs_devs->devices_kobj);
1070 		fs_devs->devices_kobj = NULL;
1071 	}
1072 
1073 	if (fs_devs->fsid_kobj.state_initialized) {
1074 		kobject_del(&fs_devs->fsid_kobj);
1075 		kobject_put(&fs_devs->fsid_kobj);
1076 		wait_for_completion(&fs_devs->kobj_unregister);
1077 	}
1078 }
1079 
1080 /* when fs_devs is NULL it will remove all fsid kobject */
1081 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1082 {
1083 	struct list_head *fs_uuids = btrfs_get_fs_uuids();
1084 
1085 	if (fs_devs) {
1086 		__btrfs_sysfs_remove_fsid(fs_devs);
1087 		return;
1088 	}
1089 
1090 	list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1091 		__btrfs_sysfs_remove_fsid(fs_devs);
1092 	}
1093 }
1094 
1095 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1096 {
1097 	struct btrfs_device *device;
1098 	struct btrfs_fs_devices *seed;
1099 
1100 	list_for_each_entry(device, &fs_devices->devices, dev_list)
1101 		btrfs_sysfs_remove_device(device);
1102 
1103 	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1104 		list_for_each_entry(device, &seed->devices, dev_list)
1105 			btrfs_sysfs_remove_device(device);
1106 	}
1107 }
1108 
1109 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1110 {
1111 	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1112 
1113 	sysfs_remove_link(fsid_kobj, "bdi");
1114 
1115 	if (fs_info->space_info_kobj) {
1116 		sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1117 		kobject_del(fs_info->space_info_kobj);
1118 		kobject_put(fs_info->space_info_kobj);
1119 	}
1120 #ifdef CONFIG_BTRFS_DEBUG
1121 	if (fs_info->discard_debug_kobj) {
1122 		sysfs_remove_files(fs_info->discard_debug_kobj,
1123 				   discard_debug_attrs);
1124 		kobject_del(fs_info->discard_debug_kobj);
1125 		kobject_put(fs_info->discard_debug_kobj);
1126 	}
1127 	if (fs_info->debug_kobj) {
1128 		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1129 		kobject_del(fs_info->debug_kobj);
1130 		kobject_put(fs_info->debug_kobj);
1131 	}
1132 #endif
1133 	addrm_unknown_feature_attrs(fs_info, false);
1134 	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1135 	sysfs_remove_files(fsid_kobj, btrfs_attrs);
1136 	btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1137 }
1138 
1139 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1140 	[FEAT_COMPAT]	 = "compat",
1141 	[FEAT_COMPAT_RO] = "compat_ro",
1142 	[FEAT_INCOMPAT]	 = "incompat",
1143 };
1144 
1145 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1146 {
1147 	return btrfs_feature_set_names[set];
1148 }
1149 
1150 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1151 {
1152 	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1153 	int len = 0;
1154 	int i;
1155 	char *str;
1156 
1157 	str = kmalloc(bufsize, GFP_KERNEL);
1158 	if (!str)
1159 		return str;
1160 
1161 	for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1162 		const char *name;
1163 
1164 		if (!(flags & (1ULL << i)))
1165 			continue;
1166 
1167 		name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1168 		len += scnprintf(str + len, bufsize - len, "%s%s",
1169 				len ? "," : "", name);
1170 	}
1171 
1172 	return str;
1173 }
1174 
1175 static void init_feature_attrs(void)
1176 {
1177 	struct btrfs_feature_attr *fa;
1178 	int set, i;
1179 
1180 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1181 		     ARRAY_SIZE(btrfs_feature_attrs));
1182 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1183 		     ARRAY_SIZE(btrfs_feature_attrs[0]));
1184 
1185 	memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1186 	memset(btrfs_unknown_feature_names, 0,
1187 	       sizeof(btrfs_unknown_feature_names));
1188 
1189 	for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1190 		struct btrfs_feature_attr *sfa;
1191 		struct attribute *a = btrfs_supported_feature_attrs[i];
1192 		int bit;
1193 		sfa = attr_to_btrfs_feature_attr(a);
1194 		bit = ilog2(sfa->feature_bit);
1195 		fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1196 
1197 		fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1198 	}
1199 
1200 	for (set = 0; set < FEAT_MAX; set++) {
1201 		for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1202 			char *name = btrfs_unknown_feature_names[set][i];
1203 			fa = &btrfs_feature_attrs[set][i];
1204 
1205 			if (fa->kobj_attr.attr.name)
1206 				continue;
1207 
1208 			snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1209 				 btrfs_feature_set_names[set], i);
1210 
1211 			fa->kobj_attr.attr.name = name;
1212 			fa->kobj_attr.attr.mode = S_IRUGO;
1213 			fa->feature_set = set;
1214 			fa->feature_bit = 1ULL << i;
1215 		}
1216 	}
1217 }
1218 
1219 /*
1220  * Create a sysfs entry for a given block group type at path
1221  * /sys/fs/btrfs/UUID/allocation/data/TYPE
1222  */
1223 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1224 {
1225 	struct btrfs_fs_info *fs_info = cache->fs_info;
1226 	struct btrfs_space_info *space_info = cache->space_info;
1227 	struct raid_kobject *rkobj;
1228 	const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1229 	unsigned int nofs_flag;
1230 	int ret;
1231 
1232 	/*
1233 	 * Setup a NOFS context because kobject_add(), deep in its call chain,
1234 	 * does GFP_KERNEL allocations, and we are often called in a context
1235 	 * where if reclaim is triggered we can deadlock (we are either holding
1236 	 * a transaction handle or some lock required for a transaction
1237 	 * commit).
1238 	 */
1239 	nofs_flag = memalloc_nofs_save();
1240 
1241 	rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1242 	if (!rkobj) {
1243 		memalloc_nofs_restore(nofs_flag);
1244 		btrfs_warn(cache->fs_info,
1245 				"couldn't alloc memory for raid level kobject");
1246 		return;
1247 	}
1248 
1249 	rkobj->flags = cache->flags;
1250 	kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1251 
1252 	/*
1253 	 * We call this either on mount, or if we've created a block group for a
1254 	 * new index type while running (i.e. when restriping).  The running
1255 	 * case is tricky because we could race with other threads, so we need
1256 	 * to have this check to make sure we didn't already init the kobject.
1257 	 *
1258 	 * We don't have to protect on the free side because it only happens on
1259 	 * unmount.
1260 	 */
1261 	spin_lock(&space_info->lock);
1262 	if (space_info->block_group_kobjs[index]) {
1263 		spin_unlock(&space_info->lock);
1264 		kobject_put(&rkobj->kobj);
1265 		return;
1266 	} else {
1267 		space_info->block_group_kobjs[index] = &rkobj->kobj;
1268 	}
1269 	spin_unlock(&space_info->lock);
1270 
1271 	ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1272 			  btrfs_bg_type_to_raid_name(rkobj->flags));
1273 	memalloc_nofs_restore(nofs_flag);
1274 	if (ret) {
1275 		spin_lock(&space_info->lock);
1276 		space_info->block_group_kobjs[index] = NULL;
1277 		spin_unlock(&space_info->lock);
1278 		kobject_put(&rkobj->kobj);
1279 		btrfs_warn(fs_info,
1280 			"failed to add kobject for block cache, ignoring");
1281 		return;
1282 	}
1283 }
1284 
1285 /*
1286  * Remove sysfs directories for all block group types of a given space info and
1287  * the space info as well
1288  */
1289 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1290 {
1291 	int i;
1292 
1293 	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1294 		struct kobject *kobj;
1295 
1296 		kobj = space_info->block_group_kobjs[i];
1297 		space_info->block_group_kobjs[i] = NULL;
1298 		if (kobj) {
1299 			kobject_del(kobj);
1300 			kobject_put(kobj);
1301 		}
1302 	}
1303 	kobject_del(&space_info->kobj);
1304 	kobject_put(&space_info->kobj);
1305 }
1306 
1307 static const char *alloc_name(u64 flags)
1308 {
1309 	switch (flags) {
1310 	case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1311 		return "mixed";
1312 	case BTRFS_BLOCK_GROUP_METADATA:
1313 		return "metadata";
1314 	case BTRFS_BLOCK_GROUP_DATA:
1315 		return "data";
1316 	case BTRFS_BLOCK_GROUP_SYSTEM:
1317 		return "system";
1318 	default:
1319 		WARN_ON(1);
1320 		return "invalid-combination";
1321 	}
1322 }
1323 
1324 /*
1325  * Create a sysfs entry for a space info type at path
1326  * /sys/fs/btrfs/UUID/allocation/TYPE
1327  */
1328 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1329 				    struct btrfs_space_info *space_info)
1330 {
1331 	int ret;
1332 
1333 	ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1334 				   fs_info->space_info_kobj, "%s",
1335 				   alloc_name(space_info->flags));
1336 	if (ret) {
1337 		kobject_put(&space_info->kobj);
1338 		return ret;
1339 	}
1340 
1341 	return 0;
1342 }
1343 
1344 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1345 {
1346 	struct kobject *devices_kobj;
1347 
1348 	/*
1349 	 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1350 	 * fs_info::fs_devices.
1351 	 */
1352 	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1353 	ASSERT(devices_kobj);
1354 
1355 	if (device->bdev)
1356 		sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1357 
1358 	if (device->devid_kobj.state_initialized) {
1359 		kobject_del(&device->devid_kobj);
1360 		kobject_put(&device->devid_kobj);
1361 		wait_for_completion(&device->kobj_unregister);
1362 	}
1363 }
1364 
1365 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1366 					         struct kobj_attribute *a,
1367 					         char *buf)
1368 {
1369 	int val;
1370 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1371 						   devid_kobj);
1372 
1373 	val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1374 
1375 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1376 }
1377 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1378 
1379 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1380 					struct kobj_attribute *a, char *buf)
1381 {
1382 	int val;
1383 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1384 						   devid_kobj);
1385 
1386 	val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1387 
1388 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1389 }
1390 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1391 
1392 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1393 					         struct kobj_attribute *a,
1394 					         char *buf)
1395 {
1396 	int val;
1397 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1398 						   devid_kobj);
1399 
1400 	val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1401 
1402 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1403 }
1404 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1405 
1406 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1407 					    struct kobj_attribute *a, char *buf)
1408 {
1409 	int val;
1410 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1411 						   devid_kobj);
1412 
1413 	val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1414 
1415 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1416 }
1417 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1418 
1419 static struct attribute *devid_attrs[] = {
1420 	BTRFS_ATTR_PTR(devid, in_fs_metadata),
1421 	BTRFS_ATTR_PTR(devid, missing),
1422 	BTRFS_ATTR_PTR(devid, replace_target),
1423 	BTRFS_ATTR_PTR(devid, writeable),
1424 	NULL
1425 };
1426 ATTRIBUTE_GROUPS(devid);
1427 
1428 static void btrfs_release_devid_kobj(struct kobject *kobj)
1429 {
1430 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1431 						   devid_kobj);
1432 
1433 	memset(&device->devid_kobj, 0, sizeof(struct kobject));
1434 	complete(&device->kobj_unregister);
1435 }
1436 
1437 static struct kobj_type devid_ktype = {
1438 	.sysfs_ops	= &kobj_sysfs_ops,
1439 	.default_groups = devid_groups,
1440 	.release	= btrfs_release_devid_kobj,
1441 };
1442 
1443 int btrfs_sysfs_add_device(struct btrfs_device *device)
1444 {
1445 	int ret;
1446 	unsigned int nofs_flag;
1447 	struct kobject *devices_kobj;
1448 	struct kobject *devinfo_kobj;
1449 
1450 	/*
1451 	 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1452 	 * for the seed fs_devices
1453 	 */
1454 	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1455 	devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1456 	ASSERT(devices_kobj);
1457 	ASSERT(devinfo_kobj);
1458 
1459 	nofs_flag = memalloc_nofs_save();
1460 
1461 	if (device->bdev) {
1462 		struct kobject *disk_kobj = bdev_kobj(device->bdev);
1463 
1464 		ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1465 		if (ret) {
1466 			btrfs_warn(device->fs_info,
1467 				"creating sysfs device link for devid %llu failed: %d",
1468 				device->devid, ret);
1469 			goto out;
1470 		}
1471 	}
1472 
1473 	init_completion(&device->kobj_unregister);
1474 	ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1475 				   devinfo_kobj, "%llu", device->devid);
1476 	if (ret) {
1477 		kobject_put(&device->devid_kobj);
1478 		btrfs_warn(device->fs_info,
1479 			   "devinfo init for devid %llu failed: %d",
1480 			   device->devid, ret);
1481 	}
1482 
1483 out:
1484 	memalloc_nofs_restore(nofs_flag);
1485 	return ret;
1486 }
1487 
1488 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1489 {
1490 	int ret;
1491 	struct btrfs_device *device;
1492 	struct btrfs_fs_devices *seed;
1493 
1494 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1495 		ret = btrfs_sysfs_add_device(device);
1496 		if (ret)
1497 			goto fail;
1498 	}
1499 
1500 	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1501 		list_for_each_entry(device, &seed->devices, dev_list) {
1502 			ret = btrfs_sysfs_add_device(device);
1503 			if (ret)
1504 				goto fail;
1505 		}
1506 	}
1507 
1508 	return 0;
1509 
1510 fail:
1511 	btrfs_sysfs_remove_fs_devices(fs_devices);
1512 	return ret;
1513 }
1514 
1515 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1516 {
1517 	int ret;
1518 
1519 	ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1520 	if (ret)
1521 		pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1522 			action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1523 			&disk_to_dev(bdev->bd_disk)->kobj);
1524 }
1525 
1526 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1527 
1528 {
1529 	char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1530 
1531 	/*
1532 	 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1533 	 * directory
1534 	 */
1535 	snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1536 	if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1537 		btrfs_warn(fs_devices->fs_info,
1538 				"sysfs: failed to create fsid for sprout");
1539 }
1540 
1541 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1542 {
1543 	char tmp[24];
1544 
1545 	snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1546 
1547 	if (kobject_rename(&device->devid_kobj, tmp))
1548 		btrfs_warn(device->fs_devices->fs_info,
1549 			   "sysfs: failed to update devid for %llu",
1550 			   device->devid);
1551 }
1552 
1553 /* /sys/fs/btrfs/ entry */
1554 static struct kset *btrfs_kset;
1555 
1556 /*
1557  * Creates:
1558  *		/sys/fs/btrfs/UUID
1559  *
1560  * Can be called by the device discovery thread.
1561  */
1562 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1563 {
1564 	int error;
1565 
1566 	init_completion(&fs_devs->kobj_unregister);
1567 	fs_devs->fsid_kobj.kset = btrfs_kset;
1568 	error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1569 				     "%pU", fs_devs->fsid);
1570 	if (error) {
1571 		kobject_put(&fs_devs->fsid_kobj);
1572 		return error;
1573 	}
1574 
1575 	fs_devs->devices_kobj = kobject_create_and_add("devices",
1576 						       &fs_devs->fsid_kobj);
1577 	if (!fs_devs->devices_kobj) {
1578 		btrfs_err(fs_devs->fs_info,
1579 			  "failed to init sysfs device interface");
1580 		btrfs_sysfs_remove_fsid(fs_devs);
1581 		return -ENOMEM;
1582 	}
1583 
1584 	fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1585 						       &fs_devs->fsid_kobj);
1586 	if (!fs_devs->devinfo_kobj) {
1587 		btrfs_err(fs_devs->fs_info,
1588 			  "failed to init sysfs devinfo kobject");
1589 		btrfs_sysfs_remove_fsid(fs_devs);
1590 		return -ENOMEM;
1591 	}
1592 
1593 	return 0;
1594 }
1595 
1596 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1597 {
1598 	int error;
1599 	struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1600 	struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1601 
1602 	error = btrfs_sysfs_add_fs_devices(fs_devs);
1603 	if (error)
1604 		return error;
1605 
1606 	error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1607 	if (error) {
1608 		btrfs_sysfs_remove_fs_devices(fs_devs);
1609 		return error;
1610 	}
1611 
1612 	error = sysfs_create_group(fsid_kobj,
1613 				   &btrfs_feature_attr_group);
1614 	if (error)
1615 		goto failure;
1616 
1617 #ifdef CONFIG_BTRFS_DEBUG
1618 	fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1619 	if (!fs_info->debug_kobj) {
1620 		error = -ENOMEM;
1621 		goto failure;
1622 	}
1623 
1624 	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1625 	if (error)
1626 		goto failure;
1627 
1628 	/* Discard directory */
1629 	fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1630 						     fs_info->debug_kobj);
1631 	if (!fs_info->discard_debug_kobj) {
1632 		error = -ENOMEM;
1633 		goto failure;
1634 	}
1635 
1636 	error = sysfs_create_files(fs_info->discard_debug_kobj,
1637 				   discard_debug_attrs);
1638 	if (error)
1639 		goto failure;
1640 #endif
1641 
1642 	error = addrm_unknown_feature_attrs(fs_info, true);
1643 	if (error)
1644 		goto failure;
1645 
1646 	error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1647 	if (error)
1648 		goto failure;
1649 
1650 	fs_info->space_info_kobj = kobject_create_and_add("allocation",
1651 						  fsid_kobj);
1652 	if (!fs_info->space_info_kobj) {
1653 		error = -ENOMEM;
1654 		goto failure;
1655 	}
1656 
1657 	error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1658 	if (error)
1659 		goto failure;
1660 
1661 	return 0;
1662 failure:
1663 	btrfs_sysfs_remove_mounted(fs_info);
1664 	return error;
1665 }
1666 
1667 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1668 {
1669 	return to_fs_info(kobj->parent->parent);
1670 }
1671 
1672 #define QGROUP_ATTR(_member, _show_name)					\
1673 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj,		\
1674 					   struct kobj_attribute *a,		\
1675 					   char *buf)				\
1676 {										\
1677 	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
1678 	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
1679 			struct btrfs_qgroup, kobj);				\
1680 	return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf);	\
1681 }										\
1682 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1683 
1684 #define QGROUP_RSV_ATTR(_name, _type)						\
1685 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj,	\
1686 					     struct kobj_attribute *a,		\
1687 					     char *buf)				\
1688 {										\
1689 	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
1690 	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
1691 			struct btrfs_qgroup, kobj);				\
1692 	return btrfs_show_u64(&qgroup->rsv.values[_type],			\
1693 			&fs_info->qgroup_lock, buf);				\
1694 }										\
1695 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1696 
1697 QGROUP_ATTR(rfer, referenced);
1698 QGROUP_ATTR(excl, exclusive);
1699 QGROUP_ATTR(max_rfer, max_referenced);
1700 QGROUP_ATTR(max_excl, max_exclusive);
1701 QGROUP_ATTR(lim_flags, limit_flags);
1702 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1703 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1704 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1705 
1706 static struct attribute *qgroup_attrs[] = {
1707 	BTRFS_ATTR_PTR(qgroup, referenced),
1708 	BTRFS_ATTR_PTR(qgroup, exclusive),
1709 	BTRFS_ATTR_PTR(qgroup, max_referenced),
1710 	BTRFS_ATTR_PTR(qgroup, max_exclusive),
1711 	BTRFS_ATTR_PTR(qgroup, limit_flags),
1712 	BTRFS_ATTR_PTR(qgroup, rsv_data),
1713 	BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1714 	BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1715 	NULL
1716 };
1717 ATTRIBUTE_GROUPS(qgroup);
1718 
1719 static void qgroup_release(struct kobject *kobj)
1720 {
1721 	struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1722 
1723 	memset(&qgroup->kobj, 0, sizeof(*kobj));
1724 }
1725 
1726 static struct kobj_type qgroup_ktype = {
1727 	.sysfs_ops = &kobj_sysfs_ops,
1728 	.release = qgroup_release,
1729 	.default_groups = qgroup_groups,
1730 };
1731 
1732 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1733 				struct btrfs_qgroup *qgroup)
1734 {
1735 	struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1736 	int ret;
1737 
1738 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1739 		return 0;
1740 	if (qgroup->kobj.state_initialized)
1741 		return 0;
1742 	if (!qgroups_kobj)
1743 		return -EINVAL;
1744 
1745 	ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1746 			"%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1747 			btrfs_qgroup_subvolid(qgroup->qgroupid));
1748 	if (ret < 0)
1749 		kobject_put(&qgroup->kobj);
1750 
1751 	return ret;
1752 }
1753 
1754 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1755 {
1756 	struct btrfs_qgroup *qgroup;
1757 	struct btrfs_qgroup *next;
1758 
1759 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1760 		return;
1761 
1762 	rbtree_postorder_for_each_entry_safe(qgroup, next,
1763 					     &fs_info->qgroup_tree, node)
1764 		btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1765 	if (fs_info->qgroups_kobj) {
1766 		kobject_del(fs_info->qgroups_kobj);
1767 		kobject_put(fs_info->qgroups_kobj);
1768 		fs_info->qgroups_kobj = NULL;
1769 	}
1770 }
1771 
1772 /* Called when qgroups get initialized, thus there is no need for locking */
1773 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1774 {
1775 	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1776 	struct btrfs_qgroup *qgroup;
1777 	struct btrfs_qgroup *next;
1778 	int ret = 0;
1779 
1780 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1781 		return 0;
1782 
1783 	ASSERT(fsid_kobj);
1784 	if (fs_info->qgroups_kobj)
1785 		return 0;
1786 
1787 	fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1788 	if (!fs_info->qgroups_kobj) {
1789 		ret = -ENOMEM;
1790 		goto out;
1791 	}
1792 	rbtree_postorder_for_each_entry_safe(qgroup, next,
1793 					     &fs_info->qgroup_tree, node) {
1794 		ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1795 		if (ret < 0)
1796 			goto out;
1797 	}
1798 
1799 out:
1800 	if (ret < 0)
1801 		btrfs_sysfs_del_qgroups(fs_info);
1802 	return ret;
1803 }
1804 
1805 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1806 				struct btrfs_qgroup *qgroup)
1807 {
1808 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1809 		return;
1810 
1811 	if (qgroup->kobj.state_initialized) {
1812 		kobject_del(&qgroup->kobj);
1813 		kobject_put(&qgroup->kobj);
1814 	}
1815 }
1816 
1817 /*
1818  * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1819  * values in superblock. Call after any changes to incompat/compat_ro flags
1820  */
1821 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1822 		u64 bit, enum btrfs_feature_set set)
1823 {
1824 	struct btrfs_fs_devices *fs_devs;
1825 	struct kobject *fsid_kobj;
1826 	u64 __maybe_unused features;
1827 	int __maybe_unused ret;
1828 
1829 	if (!fs_info)
1830 		return;
1831 
1832 	/*
1833 	 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
1834 	 * safe when called from some contexts (eg. balance)
1835 	 */
1836 	features = get_features(fs_info, set);
1837 	ASSERT(bit & supported_feature_masks[set]);
1838 
1839 	fs_devs = fs_info->fs_devices;
1840 	fsid_kobj = &fs_devs->fsid_kobj;
1841 
1842 	if (!fsid_kobj->state_initialized)
1843 		return;
1844 
1845 	/*
1846 	 * FIXME: this is too heavy to update just one value, ideally we'd like
1847 	 * to use sysfs_update_group but some refactoring is needed first.
1848 	 */
1849 	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1850 	ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1851 }
1852 
1853 int __init btrfs_init_sysfs(void)
1854 {
1855 	int ret;
1856 
1857 	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1858 	if (!btrfs_kset)
1859 		return -ENOMEM;
1860 
1861 	init_feature_attrs();
1862 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1863 	if (ret)
1864 		goto out2;
1865 	ret = sysfs_merge_group(&btrfs_kset->kobj,
1866 				&btrfs_static_feature_attr_group);
1867 	if (ret)
1868 		goto out_remove_group;
1869 
1870 #ifdef CONFIG_BTRFS_DEBUG
1871 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1872 	if (ret)
1873 		goto out2;
1874 #endif
1875 
1876 	return 0;
1877 
1878 out_remove_group:
1879 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1880 out2:
1881 	kset_unregister(btrfs_kset);
1882 
1883 	return ret;
1884 }
1885 
1886 void __cold btrfs_exit_sysfs(void)
1887 {
1888 	sysfs_unmerge_group(&btrfs_kset->kobj,
1889 			    &btrfs_static_feature_attr_group);
1890 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1891 #ifdef CONFIG_BTRFS_DEBUG
1892 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1893 #endif
1894 	kset_unregister(btrfs_kset);
1895 }
1896 
1897