xref: /openbmc/linux/fs/btrfs/sysfs.c (revision 34b7074d)
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 #include "messages.h"
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 #include "misc.h"
25 #include "fs.h"
26 #include "accessors.h"
27 
28 /*
29  * Structure name                       Path
30  * --------------------------------------------------------------------------
31  * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
32  * btrfs_supported_feature_attrs	/sys/fs/btrfs/features and
33  *					/sys/fs/btrfs/<uuid>/features
34  * btrfs_attrs				/sys/fs/btrfs/<uuid>
35  * devid_attrs				/sys/fs/btrfs/<uuid>/devinfo/<devid>
36  * allocation_attrs			/sys/fs/btrfs/<uuid>/allocation
37  * qgroup_attrs				/sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
38  * space_info_attrs			/sys/fs/btrfs/<uuid>/allocation/<bg-type>
39  * raid_attrs				/sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
40  * discard_attrs			/sys/fs/btrfs/<uuid>/discard
41  *
42  * When built with BTRFS_CONFIG_DEBUG:
43  *
44  * btrfs_debug_feature_attrs		/sys/fs/btrfs/debug
45  * btrfs_debug_mount_attrs		/sys/fs/btrfs/<uuid>/debug
46  */
47 
48 struct btrfs_feature_attr {
49 	struct kobj_attribute kobj_attr;
50 	enum btrfs_feature_set feature_set;
51 	u64 feature_bit;
52 };
53 
54 /* For raid type sysfs entries */
55 struct raid_kobject {
56 	u64 flags;
57 	struct kobject kobj;
58 };
59 
60 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)			\
61 {									\
62 	.attr	= { .name = __stringify(_name), .mode = _mode },	\
63 	.show	= _show,						\
64 	.store	= _store,						\
65 }
66 
67 #define BTRFS_ATTR_W(_prefix, _name, _store)			        \
68 	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
69 			__INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
70 
71 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store)			\
72 	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
73 			__INIT_KOBJ_ATTR(_name, 0644, _show, _store)
74 
75 #define BTRFS_ATTR(_prefix, _name, _show)				\
76 	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
77 			__INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
78 
79 #define BTRFS_ATTR_PTR(_prefix, _name)					\
80 	(&btrfs_attr_##_prefix##_##_name.attr)
81 
82 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
83 static struct btrfs_feature_attr btrfs_attr_features_##_name = {	     \
84 	.kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,			     \
85 				      btrfs_feature_attr_show,		     \
86 				      btrfs_feature_attr_store),	     \
87 	.feature_set	= _feature_set,					     \
88 	.feature_bit	= _feature_prefix ##_## _feature_bit,		     \
89 }
90 #define BTRFS_FEAT_ATTR_PTR(_name)					     \
91 	(&btrfs_attr_features_##_name.kobj_attr.attr)
92 
93 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
94 	BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
95 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
96 	BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
97 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
98 	BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
99 
100 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
101 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
102 static struct kobject *get_btrfs_kobj(struct kobject *kobj);
103 
104 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
105 {
106 	return container_of(a, struct btrfs_feature_attr, kobj_attr);
107 }
108 
109 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
110 {
111 	return container_of(attr, struct kobj_attribute, attr);
112 }
113 
114 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
115 		struct attribute *attr)
116 {
117 	return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
118 }
119 
120 static u64 get_features(struct btrfs_fs_info *fs_info,
121 			enum btrfs_feature_set set)
122 {
123 	struct btrfs_super_block *disk_super = fs_info->super_copy;
124 	if (set == FEAT_COMPAT)
125 		return btrfs_super_compat_flags(disk_super);
126 	else if (set == FEAT_COMPAT_RO)
127 		return btrfs_super_compat_ro_flags(disk_super);
128 	else
129 		return btrfs_super_incompat_flags(disk_super);
130 }
131 
132 static void set_features(struct btrfs_fs_info *fs_info,
133 			 enum btrfs_feature_set set, u64 features)
134 {
135 	struct btrfs_super_block *disk_super = fs_info->super_copy;
136 	if (set == FEAT_COMPAT)
137 		btrfs_set_super_compat_flags(disk_super, features);
138 	else if (set == FEAT_COMPAT_RO)
139 		btrfs_set_super_compat_ro_flags(disk_super, features);
140 	else
141 		btrfs_set_super_incompat_flags(disk_super, features);
142 }
143 
144 static int can_modify_feature(struct btrfs_feature_attr *fa)
145 {
146 	int val = 0;
147 	u64 set, clear;
148 	switch (fa->feature_set) {
149 	case FEAT_COMPAT:
150 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
151 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
152 		break;
153 	case FEAT_COMPAT_RO:
154 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
155 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
156 		break;
157 	case FEAT_INCOMPAT:
158 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
159 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
160 		break;
161 	default:
162 		pr_warn("btrfs: sysfs: unknown feature set %d\n",
163 				fa->feature_set);
164 		return 0;
165 	}
166 
167 	if (set & fa->feature_bit)
168 		val |= 1;
169 	if (clear & fa->feature_bit)
170 		val |= 2;
171 
172 	return val;
173 }
174 
175 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
176 				       struct kobj_attribute *a, char *buf)
177 {
178 	int val = 0;
179 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
180 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
181 	if (fs_info) {
182 		u64 features = get_features(fs_info, fa->feature_set);
183 		if (features & fa->feature_bit)
184 			val = 1;
185 	} else
186 		val = can_modify_feature(fa);
187 
188 	return sysfs_emit(buf, "%d\n", val);
189 }
190 
191 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
192 					struct kobj_attribute *a,
193 					const char *buf, size_t count)
194 {
195 	struct btrfs_fs_info *fs_info;
196 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
197 	u64 features, set, clear;
198 	unsigned long val;
199 	int ret;
200 
201 	fs_info = to_fs_info(kobj);
202 	if (!fs_info)
203 		return -EPERM;
204 
205 	if (sb_rdonly(fs_info->sb))
206 		return -EROFS;
207 
208 	ret = kstrtoul(skip_spaces(buf), 0, &val);
209 	if (ret)
210 		return ret;
211 
212 	if (fa->feature_set == FEAT_COMPAT) {
213 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
214 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
215 	} else if (fa->feature_set == FEAT_COMPAT_RO) {
216 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
217 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
218 	} else {
219 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
220 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
221 	}
222 
223 	features = get_features(fs_info, fa->feature_set);
224 
225 	/* Nothing to do */
226 	if ((val && (features & fa->feature_bit)) ||
227 	    (!val && !(features & fa->feature_bit)))
228 		return count;
229 
230 	if ((val && !(set & fa->feature_bit)) ||
231 	    (!val && !(clear & fa->feature_bit))) {
232 		btrfs_info(fs_info,
233 			"%sabling feature %s on mounted fs is not supported.",
234 			val ? "En" : "Dis", fa->kobj_attr.attr.name);
235 		return -EPERM;
236 	}
237 
238 	btrfs_info(fs_info, "%s %s feature flag",
239 		   val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
240 
241 	spin_lock(&fs_info->super_lock);
242 	features = get_features(fs_info, fa->feature_set);
243 	if (val)
244 		features |= fa->feature_bit;
245 	else
246 		features &= ~fa->feature_bit;
247 	set_features(fs_info, fa->feature_set, features);
248 	spin_unlock(&fs_info->super_lock);
249 
250 	/*
251 	 * We don't want to do full transaction commit from inside sysfs
252 	 */
253 	set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
254 	wake_up_process(fs_info->transaction_kthread);
255 
256 	return count;
257 }
258 
259 static umode_t btrfs_feature_visible(struct kobject *kobj,
260 				     struct attribute *attr, int unused)
261 {
262 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
263 	umode_t mode = attr->mode;
264 
265 	if (fs_info) {
266 		struct btrfs_feature_attr *fa;
267 		u64 features;
268 
269 		fa = attr_to_btrfs_feature_attr(attr);
270 		features = get_features(fs_info, fa->feature_set);
271 
272 		if (can_modify_feature(fa))
273 			mode |= S_IWUSR;
274 		else if (!(features & fa->feature_bit))
275 			mode = 0;
276 	}
277 
278 	return mode;
279 }
280 
281 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
282 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
283 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
284 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
285 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
286 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
287 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
288 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
289 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
290 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
291 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
292 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
293 #ifdef CONFIG_BLK_DEV_ZONED
294 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
295 #endif
296 #ifdef CONFIG_BTRFS_DEBUG
297 /* Remove once support for extent tree v2 is feature complete */
298 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
299 #endif
300 #ifdef CONFIG_FS_VERITY
301 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
302 #endif
303 
304 /*
305  * Features which depend on feature bits and may differ between each fs.
306  *
307  * /sys/fs/btrfs/features      - all available features implemented by this version
308  * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
309  *                               can be changed on a mounted filesystem.
310  */
311 static struct attribute *btrfs_supported_feature_attrs[] = {
312 	BTRFS_FEAT_ATTR_PTR(default_subvol),
313 	BTRFS_FEAT_ATTR_PTR(mixed_groups),
314 	BTRFS_FEAT_ATTR_PTR(compress_lzo),
315 	BTRFS_FEAT_ATTR_PTR(compress_zstd),
316 	BTRFS_FEAT_ATTR_PTR(extended_iref),
317 	BTRFS_FEAT_ATTR_PTR(raid56),
318 	BTRFS_FEAT_ATTR_PTR(skinny_metadata),
319 	BTRFS_FEAT_ATTR_PTR(no_holes),
320 	BTRFS_FEAT_ATTR_PTR(metadata_uuid),
321 	BTRFS_FEAT_ATTR_PTR(free_space_tree),
322 	BTRFS_FEAT_ATTR_PTR(raid1c34),
323 	BTRFS_FEAT_ATTR_PTR(block_group_tree),
324 #ifdef CONFIG_BLK_DEV_ZONED
325 	BTRFS_FEAT_ATTR_PTR(zoned),
326 #endif
327 #ifdef CONFIG_BTRFS_DEBUG
328 	BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
329 #endif
330 #ifdef CONFIG_FS_VERITY
331 	BTRFS_FEAT_ATTR_PTR(verity),
332 #endif
333 	NULL
334 };
335 
336 static const struct attribute_group btrfs_feature_attr_group = {
337 	.name = "features",
338 	.is_visible = btrfs_feature_visible,
339 	.attrs = btrfs_supported_feature_attrs,
340 };
341 
342 static ssize_t rmdir_subvol_show(struct kobject *kobj,
343 				 struct kobj_attribute *ka, char *buf)
344 {
345 	return sysfs_emit(buf, "0\n");
346 }
347 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
348 
349 static ssize_t supported_checksums_show(struct kobject *kobj,
350 					struct kobj_attribute *a, char *buf)
351 {
352 	ssize_t ret = 0;
353 	int i;
354 
355 	for (i = 0; i < btrfs_get_num_csums(); i++) {
356 		/*
357 		 * This "trick" only works as long as 'enum btrfs_csum_type' has
358 		 * no holes in it
359 		 */
360 		ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
361 				     btrfs_super_csum_name(i));
362 
363 	}
364 
365 	ret += sysfs_emit_at(buf, ret, "\n");
366 	return ret;
367 }
368 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
369 
370 static ssize_t send_stream_version_show(struct kobject *kobj,
371 					struct kobj_attribute *ka, char *buf)
372 {
373 	return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
374 }
375 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
376 
377 static const char *rescue_opts[] = {
378 	"usebackuproot",
379 	"nologreplay",
380 	"ignorebadroots",
381 	"ignoredatacsums",
382 	"all",
383 };
384 
385 static ssize_t supported_rescue_options_show(struct kobject *kobj,
386 					     struct kobj_attribute *a,
387 					     char *buf)
388 {
389 	ssize_t ret = 0;
390 	int i;
391 
392 	for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
393 		ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
394 	ret += sysfs_emit_at(buf, ret, "\n");
395 	return ret;
396 }
397 BTRFS_ATTR(static_feature, supported_rescue_options,
398 	   supported_rescue_options_show);
399 
400 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
401 					  struct kobj_attribute *a,
402 					  char *buf)
403 {
404 	ssize_t ret = 0;
405 
406 	/* An artificial limit to only support 4K and PAGE_SIZE */
407 	if (PAGE_SIZE > SZ_4K)
408 		ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
409 	ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
410 
411 	return ret;
412 }
413 BTRFS_ATTR(static_feature, supported_sectorsizes,
414 	   supported_sectorsizes_show);
415 
416 /*
417  * Features which only depend on kernel version.
418  *
419  * These are listed in /sys/fs/btrfs/features along with
420  * btrfs_supported_feature_attrs.
421  */
422 static struct attribute *btrfs_supported_static_feature_attrs[] = {
423 	BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
424 	BTRFS_ATTR_PTR(static_feature, supported_checksums),
425 	BTRFS_ATTR_PTR(static_feature, send_stream_version),
426 	BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
427 	BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
428 	NULL
429 };
430 
431 static const struct attribute_group btrfs_static_feature_attr_group = {
432 	.name = "features",
433 	.attrs = btrfs_supported_static_feature_attrs,
434 };
435 
436 /*
437  * Discard statistics and tunables
438  */
439 #define discard_to_fs_info(_kobj)	to_fs_info(get_btrfs_kobj(_kobj))
440 
441 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
442 					    struct kobj_attribute *a,
443 					    char *buf)
444 {
445 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
446 
447 	return sysfs_emit(buf, "%lld\n",
448 			atomic64_read(&fs_info->discard_ctl.discardable_bytes));
449 }
450 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
451 
452 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
453 					      struct kobj_attribute *a,
454 					      char *buf)
455 {
456 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
457 
458 	return sysfs_emit(buf, "%d\n",
459 			atomic_read(&fs_info->discard_ctl.discardable_extents));
460 }
461 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
462 
463 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
464 					       struct kobj_attribute *a,
465 					       char *buf)
466 {
467 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
468 
469 	return sysfs_emit(buf, "%llu\n",
470 			  fs_info->discard_ctl.discard_bitmap_bytes);
471 }
472 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
473 
474 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
475 					      struct kobj_attribute *a,
476 					      char *buf)
477 {
478 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
479 
480 	return sysfs_emit(buf, "%lld\n",
481 		atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
482 }
483 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
484 
485 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
486 					       struct kobj_attribute *a,
487 					       char *buf)
488 {
489 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
490 
491 	return sysfs_emit(buf, "%llu\n",
492 			  fs_info->discard_ctl.discard_extent_bytes);
493 }
494 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
495 
496 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
497 					     struct kobj_attribute *a,
498 					     char *buf)
499 {
500 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
501 
502 	return sysfs_emit(buf, "%u\n",
503 			  READ_ONCE(fs_info->discard_ctl.iops_limit));
504 }
505 
506 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
507 					      struct kobj_attribute *a,
508 					      const char *buf, size_t len)
509 {
510 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
511 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
512 	u32 iops_limit;
513 	int ret;
514 
515 	ret = kstrtou32(buf, 10, &iops_limit);
516 	if (ret)
517 		return -EINVAL;
518 
519 	WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
520 	btrfs_discard_calc_delay(discard_ctl);
521 	btrfs_discard_schedule_work(discard_ctl, true);
522 	return len;
523 }
524 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
525 	      btrfs_discard_iops_limit_store);
526 
527 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
528 					     struct kobj_attribute *a,
529 					     char *buf)
530 {
531 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
532 
533 	return sysfs_emit(buf, "%u\n",
534 			  READ_ONCE(fs_info->discard_ctl.kbps_limit));
535 }
536 
537 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
538 					      struct kobj_attribute *a,
539 					      const char *buf, size_t len)
540 {
541 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
542 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
543 	u32 kbps_limit;
544 	int ret;
545 
546 	ret = kstrtou32(buf, 10, &kbps_limit);
547 	if (ret)
548 		return -EINVAL;
549 
550 	WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
551 	btrfs_discard_schedule_work(discard_ctl, true);
552 	return len;
553 }
554 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
555 	      btrfs_discard_kbps_limit_store);
556 
557 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
558 						   struct kobj_attribute *a,
559 						   char *buf)
560 {
561 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
562 
563 	return sysfs_emit(buf, "%llu\n",
564 			  READ_ONCE(fs_info->discard_ctl.max_discard_size));
565 }
566 
567 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
568 						    struct kobj_attribute *a,
569 						    const char *buf, size_t len)
570 {
571 	struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
572 	struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
573 	u64 max_discard_size;
574 	int ret;
575 
576 	ret = kstrtou64(buf, 10, &max_discard_size);
577 	if (ret)
578 		return -EINVAL;
579 
580 	WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
581 
582 	return len;
583 }
584 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
585 	      btrfs_discard_max_discard_size_store);
586 
587 /*
588  * Per-filesystem stats for discard (when mounted with discard=async).
589  *
590  * Path: /sys/fs/btrfs/<uuid>/discard/
591  */
592 static const struct attribute *discard_attrs[] = {
593 	BTRFS_ATTR_PTR(discard, discardable_bytes),
594 	BTRFS_ATTR_PTR(discard, discardable_extents),
595 	BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
596 	BTRFS_ATTR_PTR(discard, discard_bytes_saved),
597 	BTRFS_ATTR_PTR(discard, discard_extent_bytes),
598 	BTRFS_ATTR_PTR(discard, iops_limit),
599 	BTRFS_ATTR_PTR(discard, kbps_limit),
600 	BTRFS_ATTR_PTR(discard, max_discard_size),
601 	NULL,
602 };
603 
604 #ifdef CONFIG_BTRFS_DEBUG
605 
606 /*
607  * Per-filesystem runtime debugging exported via sysfs.
608  *
609  * Path: /sys/fs/btrfs/UUID/debug/
610  */
611 static const struct attribute *btrfs_debug_mount_attrs[] = {
612 	NULL,
613 };
614 
615 /*
616  * Runtime debugging exported via sysfs, applies to all mounted filesystems.
617  *
618  * Path: /sys/fs/btrfs/debug
619  */
620 static struct attribute *btrfs_debug_feature_attrs[] = {
621 	NULL
622 };
623 
624 static const struct attribute_group btrfs_debug_feature_attr_group = {
625 	.name = "debug",
626 	.attrs = btrfs_debug_feature_attrs,
627 };
628 
629 #endif
630 
631 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
632 {
633 	u64 val;
634 	if (lock)
635 		spin_lock(lock);
636 	val = *value_ptr;
637 	if (lock)
638 		spin_unlock(lock);
639 	return sysfs_emit(buf, "%llu\n", val);
640 }
641 
642 static ssize_t global_rsv_size_show(struct kobject *kobj,
643 				    struct kobj_attribute *ka, char *buf)
644 {
645 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
646 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
647 	return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
648 }
649 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
650 
651 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
652 					struct kobj_attribute *a, char *buf)
653 {
654 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
655 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
656 	return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
657 }
658 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
659 
660 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
661 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
662 
663 static ssize_t raid_bytes_show(struct kobject *kobj,
664 			       struct kobj_attribute *attr, char *buf);
665 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
666 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
667 
668 static ssize_t raid_bytes_show(struct kobject *kobj,
669 			       struct kobj_attribute *attr, char *buf)
670 
671 {
672 	struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
673 	struct btrfs_block_group *block_group;
674 	int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
675 	u64 val = 0;
676 
677 	down_read(&sinfo->groups_sem);
678 	list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
679 		if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
680 			val += block_group->length;
681 		else
682 			val += block_group->used;
683 	}
684 	up_read(&sinfo->groups_sem);
685 	return sysfs_emit(buf, "%llu\n", val);
686 }
687 
688 /*
689  * Allocation information about block group profiles.
690  *
691  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
692  */
693 static struct attribute *raid_attrs[] = {
694 	BTRFS_ATTR_PTR(raid, total_bytes),
695 	BTRFS_ATTR_PTR(raid, used_bytes),
696 	NULL
697 };
698 ATTRIBUTE_GROUPS(raid);
699 
700 static void release_raid_kobj(struct kobject *kobj)
701 {
702 	kfree(to_raid_kobj(kobj));
703 }
704 
705 static struct kobj_type btrfs_raid_ktype = {
706 	.sysfs_ops = &kobj_sysfs_ops,
707 	.release = release_raid_kobj,
708 	.default_groups = raid_groups,
709 };
710 
711 #define SPACE_INFO_ATTR(field)						\
712 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,	\
713 					     struct kobj_attribute *a,	\
714 					     char *buf)			\
715 {									\
716 	struct btrfs_space_info *sinfo = to_space_info(kobj);		\
717 	return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);	\
718 }									\
719 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
720 
721 static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
722 				     struct kobj_attribute *a, char *buf)
723 {
724 	struct btrfs_space_info *sinfo = to_space_info(kobj);
725 
726 	return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
727 }
728 
729 /*
730  * Store new chunk size in space info. Can be called on a read-only filesystem.
731  *
732  * If the new chunk size value is larger than 10% of free space it is reduced
733  * to match that limit. Alignment must be to 256M and the system chunk size
734  * cannot be set.
735  */
736 static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
737 				      struct kobj_attribute *a,
738 				      const char *buf, size_t len)
739 {
740 	struct btrfs_space_info *space_info = to_space_info(kobj);
741 	struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
742 	char *retptr;
743 	u64 val;
744 
745 	if (!capable(CAP_SYS_ADMIN))
746 		return -EPERM;
747 
748 	if (!fs_info->fs_devices)
749 		return -EINVAL;
750 
751 	if (btrfs_is_zoned(fs_info))
752 		return -EINVAL;
753 
754 	/* System block type must not be changed. */
755 	if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
756 		return -EPERM;
757 
758 	val = memparse(buf, &retptr);
759 	/* There could be trailing '\n', also catch any typos after the value */
760 	retptr = skip_spaces(retptr);
761 	if (*retptr != 0 || val == 0)
762 		return -EINVAL;
763 
764 	val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
765 
766 	/* Limit stripe size to 10% of available space. */
767 	val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val);
768 
769 	/* Must be multiple of 256M. */
770 	val &= ~((u64)SZ_256M - 1);
771 
772 	/* Must be at least 256M. */
773 	if (val < SZ_256M)
774 		return -EINVAL;
775 
776 	btrfs_update_space_info_chunk_size(space_info, val);
777 
778 	return len;
779 }
780 
781 #ifdef CONFIG_BTRFS_DEBUG
782 /*
783  * Request chunk allocation with current chunk size.
784  */
785 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
786 					     struct kobj_attribute *a,
787 					     const char *buf, size_t len)
788 {
789 	struct btrfs_space_info *space_info = to_space_info(kobj);
790 	struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
791 	struct btrfs_trans_handle *trans;
792 	bool val;
793 	int ret;
794 
795 	if (!capable(CAP_SYS_ADMIN))
796 		return -EPERM;
797 
798 	if (sb_rdonly(fs_info->sb))
799 		return -EROFS;
800 
801 	ret = kstrtobool(buf, &val);
802 	if (ret)
803 		return ret;
804 
805 	if (!val)
806 		return -EINVAL;
807 
808 	/*
809 	 * This is unsafe to be called from sysfs context and may cause
810 	 * unexpected problems.
811 	 */
812 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
813 	if (IS_ERR(trans))
814 		return PTR_ERR(trans);
815 	ret = btrfs_force_chunk_alloc(trans, space_info->flags);
816 	btrfs_end_transaction(trans);
817 
818 	if (ret == 1)
819 		return len;
820 
821 	return -ENOSPC;
822 }
823 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
824 
825 #endif
826 
827 SPACE_INFO_ATTR(flags);
828 SPACE_INFO_ATTR(total_bytes);
829 SPACE_INFO_ATTR(bytes_used);
830 SPACE_INFO_ATTR(bytes_pinned);
831 SPACE_INFO_ATTR(bytes_reserved);
832 SPACE_INFO_ATTR(bytes_may_use);
833 SPACE_INFO_ATTR(bytes_readonly);
834 SPACE_INFO_ATTR(bytes_zone_unusable);
835 SPACE_INFO_ATTR(disk_used);
836 SPACE_INFO_ATTR(disk_total);
837 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
838 
839 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
840 						     struct kobj_attribute *a,
841 						     char *buf)
842 {
843 	struct btrfs_space_info *space_info = to_space_info(kobj);
844 
845 	return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
846 }
847 
848 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
849 						      struct kobj_attribute *a,
850 						      const char *buf, size_t len)
851 {
852 	struct btrfs_space_info *space_info = to_space_info(kobj);
853 	int thresh;
854 	int ret;
855 
856 	ret = kstrtoint(buf, 10, &thresh);
857 	if (ret)
858 		return ret;
859 
860 	if (thresh < 0 || thresh > 100)
861 		return -EINVAL;
862 
863 	WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
864 
865 	return len;
866 }
867 
868 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
869 	      btrfs_sinfo_bg_reclaim_threshold_show,
870 	      btrfs_sinfo_bg_reclaim_threshold_store);
871 
872 /*
873  * Allocation information about block group types.
874  *
875  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
876  */
877 static struct attribute *space_info_attrs[] = {
878 	BTRFS_ATTR_PTR(space_info, flags),
879 	BTRFS_ATTR_PTR(space_info, total_bytes),
880 	BTRFS_ATTR_PTR(space_info, bytes_used),
881 	BTRFS_ATTR_PTR(space_info, bytes_pinned),
882 	BTRFS_ATTR_PTR(space_info, bytes_reserved),
883 	BTRFS_ATTR_PTR(space_info, bytes_may_use),
884 	BTRFS_ATTR_PTR(space_info, bytes_readonly),
885 	BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
886 	BTRFS_ATTR_PTR(space_info, disk_used),
887 	BTRFS_ATTR_PTR(space_info, disk_total),
888 	BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
889 	BTRFS_ATTR_PTR(space_info, chunk_size),
890 #ifdef CONFIG_BTRFS_DEBUG
891 	BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
892 #endif
893 	NULL,
894 };
895 ATTRIBUTE_GROUPS(space_info);
896 
897 static void space_info_release(struct kobject *kobj)
898 {
899 	struct btrfs_space_info *sinfo = to_space_info(kobj);
900 	kfree(sinfo);
901 }
902 
903 static struct kobj_type space_info_ktype = {
904 	.sysfs_ops = &kobj_sysfs_ops,
905 	.release = space_info_release,
906 	.default_groups = space_info_groups,
907 };
908 
909 /*
910  * Allocation information about block groups.
911  *
912  * Path: /sys/fs/btrfs/<uuid>/allocation/
913  */
914 static const struct attribute *allocation_attrs[] = {
915 	BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
916 	BTRFS_ATTR_PTR(allocation, global_rsv_size),
917 	NULL,
918 };
919 
920 static ssize_t btrfs_label_show(struct kobject *kobj,
921 				struct kobj_attribute *a, char *buf)
922 {
923 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
924 	char *label = fs_info->super_copy->label;
925 	ssize_t ret;
926 
927 	spin_lock(&fs_info->super_lock);
928 	ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
929 	spin_unlock(&fs_info->super_lock);
930 
931 	return ret;
932 }
933 
934 static ssize_t btrfs_label_store(struct kobject *kobj,
935 				 struct kobj_attribute *a,
936 				 const char *buf, size_t len)
937 {
938 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
939 	size_t p_len;
940 
941 	if (!fs_info)
942 		return -EPERM;
943 
944 	if (sb_rdonly(fs_info->sb))
945 		return -EROFS;
946 
947 	/*
948 	 * p_len is the len until the first occurrence of either
949 	 * '\n' or '\0'
950 	 */
951 	p_len = strcspn(buf, "\n");
952 
953 	if (p_len >= BTRFS_LABEL_SIZE)
954 		return -EINVAL;
955 
956 	spin_lock(&fs_info->super_lock);
957 	memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
958 	memcpy(fs_info->super_copy->label, buf, p_len);
959 	spin_unlock(&fs_info->super_lock);
960 
961 	/*
962 	 * We don't want to do full transaction commit from inside sysfs
963 	 */
964 	set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
965 	wake_up_process(fs_info->transaction_kthread);
966 
967 	return len;
968 }
969 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
970 
971 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
972 				struct kobj_attribute *a, char *buf)
973 {
974 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
975 
976 	return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
977 }
978 
979 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
980 
981 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
982 				struct kobj_attribute *a, char *buf)
983 {
984 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
985 
986 	return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
987 }
988 
989 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
990 
991 static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
992 				       struct kobj_attribute *a, char *buf)
993 {
994 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
995 
996 	return sysfs_emit(buf,
997 		"commits %llu\n"
998 		"last_commit_ms %llu\n"
999 		"max_commit_ms %llu\n"
1000 		"total_commit_ms %llu\n",
1001 		fs_info->commit_stats.commit_count,
1002 		div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1003 		div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1004 		div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1005 }
1006 
1007 static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1008 					struct kobj_attribute *a,
1009 					const char *buf, size_t len)
1010 {
1011 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1012 	unsigned long val;
1013 	int ret;
1014 
1015 	if (!fs_info)
1016 		return -EPERM;
1017 
1018 	if (!capable(CAP_SYS_RESOURCE))
1019 		return -EPERM;
1020 
1021 	ret = kstrtoul(buf, 10, &val);
1022 	if (ret)
1023 		return ret;
1024 	if (val)
1025 		return -EINVAL;
1026 
1027 	WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1028 
1029 	return len;
1030 }
1031 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1032 
1033 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1034 				struct kobj_attribute *a, char *buf)
1035 {
1036 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1037 
1038 	return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1039 }
1040 
1041 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1042 
1043 static ssize_t quota_override_show(struct kobject *kobj,
1044 				   struct kobj_attribute *a, char *buf)
1045 {
1046 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1047 	int quota_override;
1048 
1049 	quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1050 	return sysfs_emit(buf, "%d\n", quota_override);
1051 }
1052 
1053 static ssize_t quota_override_store(struct kobject *kobj,
1054 				    struct kobj_attribute *a,
1055 				    const char *buf, size_t len)
1056 {
1057 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1058 	unsigned long knob;
1059 	int err;
1060 
1061 	if (!fs_info)
1062 		return -EPERM;
1063 
1064 	if (!capable(CAP_SYS_RESOURCE))
1065 		return -EPERM;
1066 
1067 	err = kstrtoul(buf, 10, &knob);
1068 	if (err)
1069 		return err;
1070 	if (knob > 1)
1071 		return -EINVAL;
1072 
1073 	if (knob)
1074 		set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1075 	else
1076 		clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1077 
1078 	return len;
1079 }
1080 
1081 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1082 
1083 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1084 				struct kobj_attribute *a, char *buf)
1085 {
1086 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1087 
1088 	return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1089 }
1090 
1091 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1092 
1093 static ssize_t btrfs_checksum_show(struct kobject *kobj,
1094 				   struct kobj_attribute *a, char *buf)
1095 {
1096 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1097 	u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1098 
1099 	return sysfs_emit(buf, "%s (%s)\n",
1100 			  btrfs_super_csum_name(csum_type),
1101 			  crypto_shash_driver_name(fs_info->csum_shash));
1102 }
1103 
1104 BTRFS_ATTR(, checksum, btrfs_checksum_show);
1105 
1106 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1107 		struct kobj_attribute *a, char *buf)
1108 {
1109 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1110 	const char *str;
1111 
1112 	switch (READ_ONCE(fs_info->exclusive_operation)) {
1113 		case  BTRFS_EXCLOP_NONE:
1114 			str = "none\n";
1115 			break;
1116 		case BTRFS_EXCLOP_BALANCE:
1117 			str = "balance\n";
1118 			break;
1119 		case BTRFS_EXCLOP_BALANCE_PAUSED:
1120 			str = "balance paused\n";
1121 			break;
1122 		case BTRFS_EXCLOP_DEV_ADD:
1123 			str = "device add\n";
1124 			break;
1125 		case BTRFS_EXCLOP_DEV_REMOVE:
1126 			str = "device remove\n";
1127 			break;
1128 		case BTRFS_EXCLOP_DEV_REPLACE:
1129 			str = "device replace\n";
1130 			break;
1131 		case BTRFS_EXCLOP_RESIZE:
1132 			str = "resize\n";
1133 			break;
1134 		case BTRFS_EXCLOP_SWAP_ACTIVATE:
1135 			str = "swap activate\n";
1136 			break;
1137 		default:
1138 			str = "UNKNOWN\n";
1139 			break;
1140 	}
1141 	return sysfs_emit(buf, "%s", str);
1142 }
1143 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1144 
1145 static ssize_t btrfs_generation_show(struct kobject *kobj,
1146 				     struct kobj_attribute *a, char *buf)
1147 {
1148 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1149 
1150 	return sysfs_emit(buf, "%llu\n", fs_info->generation);
1151 }
1152 BTRFS_ATTR(, generation, btrfs_generation_show);
1153 
1154 static const char * const btrfs_read_policy_name[] = { "pid" };
1155 
1156 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1157 				      struct kobj_attribute *a, char *buf)
1158 {
1159 	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1160 	ssize_t ret = 0;
1161 	int i;
1162 
1163 	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1164 		if (fs_devices->read_policy == i)
1165 			ret += sysfs_emit_at(buf, ret, "%s[%s]",
1166 					 (ret == 0 ? "" : " "),
1167 					 btrfs_read_policy_name[i]);
1168 		else
1169 			ret += sysfs_emit_at(buf, ret, "%s%s",
1170 					 (ret == 0 ? "" : " "),
1171 					 btrfs_read_policy_name[i]);
1172 	}
1173 
1174 	ret += sysfs_emit_at(buf, ret, "\n");
1175 
1176 	return ret;
1177 }
1178 
1179 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1180 				       struct kobj_attribute *a,
1181 				       const char *buf, size_t len)
1182 {
1183 	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1184 	int i;
1185 
1186 	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1187 		if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
1188 			if (i != fs_devices->read_policy) {
1189 				fs_devices->read_policy = i;
1190 				btrfs_info(fs_devices->fs_info,
1191 					   "read policy set to '%s'",
1192 					   btrfs_read_policy_name[i]);
1193 			}
1194 			return len;
1195 		}
1196 	}
1197 
1198 	return -EINVAL;
1199 }
1200 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1201 
1202 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1203 					       struct kobj_attribute *a,
1204 					       char *buf)
1205 {
1206 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1207 
1208 	return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1209 }
1210 
1211 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1212 						struct kobj_attribute *a,
1213 						const char *buf, size_t len)
1214 {
1215 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1216 	int thresh;
1217 	int ret;
1218 
1219 	ret = kstrtoint(buf, 10, &thresh);
1220 	if (ret)
1221 		return ret;
1222 
1223 	if (thresh != 0 && (thresh <= 50 || thresh > 100))
1224 		return -EINVAL;
1225 
1226 	WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1227 
1228 	return len;
1229 }
1230 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1231 	      btrfs_bg_reclaim_threshold_store);
1232 
1233 /*
1234  * Per-filesystem information and stats.
1235  *
1236  * Path: /sys/fs/btrfs/<uuid>/
1237  */
1238 static const struct attribute *btrfs_attrs[] = {
1239 	BTRFS_ATTR_PTR(, label),
1240 	BTRFS_ATTR_PTR(, nodesize),
1241 	BTRFS_ATTR_PTR(, sectorsize),
1242 	BTRFS_ATTR_PTR(, clone_alignment),
1243 	BTRFS_ATTR_PTR(, quota_override),
1244 	BTRFS_ATTR_PTR(, metadata_uuid),
1245 	BTRFS_ATTR_PTR(, checksum),
1246 	BTRFS_ATTR_PTR(, exclusive_operation),
1247 	BTRFS_ATTR_PTR(, generation),
1248 	BTRFS_ATTR_PTR(, read_policy),
1249 	BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1250 	BTRFS_ATTR_PTR(, commit_stats),
1251 	NULL,
1252 };
1253 
1254 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1255 {
1256 	struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1257 
1258 	memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1259 	complete(&fs_devs->kobj_unregister);
1260 }
1261 
1262 static struct kobj_type btrfs_ktype = {
1263 	.sysfs_ops	= &kobj_sysfs_ops,
1264 	.release	= btrfs_release_fsid_kobj,
1265 };
1266 
1267 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1268 {
1269 	if (kobj->ktype != &btrfs_ktype)
1270 		return NULL;
1271 	return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1272 }
1273 
1274 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1275 {
1276 	if (kobj->ktype != &btrfs_ktype)
1277 		return NULL;
1278 	return to_fs_devs(kobj)->fs_info;
1279 }
1280 
1281 static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1282 {
1283 	while (kobj) {
1284 		if (kobj->ktype == &btrfs_ktype)
1285 			return kobj;
1286 		kobj = kobj->parent;
1287 	}
1288 	return NULL;
1289 }
1290 
1291 #define NUM_FEATURE_BITS 64
1292 #define BTRFS_FEATURE_NAME_MAX 13
1293 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1294 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1295 
1296 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1297 	      ARRAY_SIZE(btrfs_feature_attrs));
1298 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1299 	      ARRAY_SIZE(btrfs_feature_attrs[0]));
1300 
1301 static const u64 supported_feature_masks[FEAT_MAX] = {
1302 	[FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
1303 	[FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1304 	[FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
1305 };
1306 
1307 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1308 {
1309 	int set;
1310 
1311 	for (set = 0; set < FEAT_MAX; set++) {
1312 		int i;
1313 		struct attribute *attrs[2];
1314 		struct attribute_group agroup = {
1315 			.name = "features",
1316 			.attrs = attrs,
1317 		};
1318 		u64 features = get_features(fs_info, set);
1319 		features &= ~supported_feature_masks[set];
1320 
1321 		if (!features)
1322 			continue;
1323 
1324 		attrs[1] = NULL;
1325 		for (i = 0; i < NUM_FEATURE_BITS; i++) {
1326 			struct btrfs_feature_attr *fa;
1327 
1328 			if (!(features & (1ULL << i)))
1329 				continue;
1330 
1331 			fa = &btrfs_feature_attrs[set][i];
1332 			attrs[0] = &fa->kobj_attr.attr;
1333 			if (add) {
1334 				int ret;
1335 				ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1336 							&agroup);
1337 				if (ret)
1338 					return ret;
1339 			} else
1340 				sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1341 						    &agroup);
1342 		}
1343 
1344 	}
1345 	return 0;
1346 }
1347 
1348 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1349 {
1350 	if (fs_devs->devinfo_kobj) {
1351 		kobject_del(fs_devs->devinfo_kobj);
1352 		kobject_put(fs_devs->devinfo_kobj);
1353 		fs_devs->devinfo_kobj = NULL;
1354 	}
1355 
1356 	if (fs_devs->devices_kobj) {
1357 		kobject_del(fs_devs->devices_kobj);
1358 		kobject_put(fs_devs->devices_kobj);
1359 		fs_devs->devices_kobj = NULL;
1360 	}
1361 
1362 	if (fs_devs->fsid_kobj.state_initialized) {
1363 		kobject_del(&fs_devs->fsid_kobj);
1364 		kobject_put(&fs_devs->fsid_kobj);
1365 		wait_for_completion(&fs_devs->kobj_unregister);
1366 	}
1367 }
1368 
1369 /* when fs_devs is NULL it will remove all fsid kobject */
1370 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1371 {
1372 	struct list_head *fs_uuids = btrfs_get_fs_uuids();
1373 
1374 	if (fs_devs) {
1375 		__btrfs_sysfs_remove_fsid(fs_devs);
1376 		return;
1377 	}
1378 
1379 	list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1380 		__btrfs_sysfs_remove_fsid(fs_devs);
1381 	}
1382 }
1383 
1384 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1385 {
1386 	struct btrfs_device *device;
1387 	struct btrfs_fs_devices *seed;
1388 
1389 	list_for_each_entry(device, &fs_devices->devices, dev_list)
1390 		btrfs_sysfs_remove_device(device);
1391 
1392 	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1393 		list_for_each_entry(device, &seed->devices, dev_list)
1394 			btrfs_sysfs_remove_device(device);
1395 	}
1396 }
1397 
1398 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1399 {
1400 	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1401 
1402 	sysfs_remove_link(fsid_kobj, "bdi");
1403 
1404 	if (fs_info->space_info_kobj) {
1405 		sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1406 		kobject_del(fs_info->space_info_kobj);
1407 		kobject_put(fs_info->space_info_kobj);
1408 	}
1409 	if (fs_info->discard_kobj) {
1410 		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1411 		kobject_del(fs_info->discard_kobj);
1412 		kobject_put(fs_info->discard_kobj);
1413 	}
1414 #ifdef CONFIG_BTRFS_DEBUG
1415 	if (fs_info->debug_kobj) {
1416 		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1417 		kobject_del(fs_info->debug_kobj);
1418 		kobject_put(fs_info->debug_kobj);
1419 	}
1420 #endif
1421 	addrm_unknown_feature_attrs(fs_info, false);
1422 	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1423 	sysfs_remove_files(fsid_kobj, btrfs_attrs);
1424 	btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1425 }
1426 
1427 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1428 	[FEAT_COMPAT]	 = "compat",
1429 	[FEAT_COMPAT_RO] = "compat_ro",
1430 	[FEAT_INCOMPAT]	 = "incompat",
1431 };
1432 
1433 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1434 {
1435 	return btrfs_feature_set_names[set];
1436 }
1437 
1438 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1439 {
1440 	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1441 	int len = 0;
1442 	int i;
1443 	char *str;
1444 
1445 	str = kmalloc(bufsize, GFP_KERNEL);
1446 	if (!str)
1447 		return str;
1448 
1449 	for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1450 		const char *name;
1451 
1452 		if (!(flags & (1ULL << i)))
1453 			continue;
1454 
1455 		name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1456 		len += scnprintf(str + len, bufsize - len, "%s%s",
1457 				len ? "," : "", name);
1458 	}
1459 
1460 	return str;
1461 }
1462 
1463 static void init_feature_attrs(void)
1464 {
1465 	struct btrfs_feature_attr *fa;
1466 	int set, i;
1467 
1468 	memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1469 	memset(btrfs_unknown_feature_names, 0,
1470 	       sizeof(btrfs_unknown_feature_names));
1471 
1472 	for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1473 		struct btrfs_feature_attr *sfa;
1474 		struct attribute *a = btrfs_supported_feature_attrs[i];
1475 		int bit;
1476 		sfa = attr_to_btrfs_feature_attr(a);
1477 		bit = ilog2(sfa->feature_bit);
1478 		fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1479 
1480 		fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1481 	}
1482 
1483 	for (set = 0; set < FEAT_MAX; set++) {
1484 		for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1485 			char *name = btrfs_unknown_feature_names[set][i];
1486 			fa = &btrfs_feature_attrs[set][i];
1487 
1488 			if (fa->kobj_attr.attr.name)
1489 				continue;
1490 
1491 			snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1492 				 btrfs_feature_set_names[set], i);
1493 
1494 			fa->kobj_attr.attr.name = name;
1495 			fa->kobj_attr.attr.mode = S_IRUGO;
1496 			fa->feature_set = set;
1497 			fa->feature_bit = 1ULL << i;
1498 		}
1499 	}
1500 }
1501 
1502 /*
1503  * Create a sysfs entry for a given block group type at path
1504  * /sys/fs/btrfs/UUID/allocation/data/TYPE
1505  */
1506 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1507 {
1508 	struct btrfs_fs_info *fs_info = cache->fs_info;
1509 	struct btrfs_space_info *space_info = cache->space_info;
1510 	struct raid_kobject *rkobj;
1511 	const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1512 	unsigned int nofs_flag;
1513 	int ret;
1514 
1515 	/*
1516 	 * Setup a NOFS context because kobject_add(), deep in its call chain,
1517 	 * does GFP_KERNEL allocations, and we are often called in a context
1518 	 * where if reclaim is triggered we can deadlock (we are either holding
1519 	 * a transaction handle or some lock required for a transaction
1520 	 * commit).
1521 	 */
1522 	nofs_flag = memalloc_nofs_save();
1523 
1524 	rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1525 	if (!rkobj) {
1526 		memalloc_nofs_restore(nofs_flag);
1527 		btrfs_warn(cache->fs_info,
1528 				"couldn't alloc memory for raid level kobject");
1529 		return;
1530 	}
1531 
1532 	rkobj->flags = cache->flags;
1533 	kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1534 
1535 	/*
1536 	 * We call this either on mount, or if we've created a block group for a
1537 	 * new index type while running (i.e. when restriping).  The running
1538 	 * case is tricky because we could race with other threads, so we need
1539 	 * to have this check to make sure we didn't already init the kobject.
1540 	 *
1541 	 * We don't have to protect on the free side because it only happens on
1542 	 * unmount.
1543 	 */
1544 	spin_lock(&space_info->lock);
1545 	if (space_info->block_group_kobjs[index]) {
1546 		spin_unlock(&space_info->lock);
1547 		kobject_put(&rkobj->kobj);
1548 		return;
1549 	} else {
1550 		space_info->block_group_kobjs[index] = &rkobj->kobj;
1551 	}
1552 	spin_unlock(&space_info->lock);
1553 
1554 	ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1555 			  btrfs_bg_type_to_raid_name(rkobj->flags));
1556 	memalloc_nofs_restore(nofs_flag);
1557 	if (ret) {
1558 		spin_lock(&space_info->lock);
1559 		space_info->block_group_kobjs[index] = NULL;
1560 		spin_unlock(&space_info->lock);
1561 		kobject_put(&rkobj->kobj);
1562 		btrfs_warn(fs_info,
1563 			"failed to add kobject for block cache, ignoring");
1564 		return;
1565 	}
1566 }
1567 
1568 /*
1569  * Remove sysfs directories for all block group types of a given space info and
1570  * the space info as well
1571  */
1572 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1573 {
1574 	int i;
1575 
1576 	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1577 		struct kobject *kobj;
1578 
1579 		kobj = space_info->block_group_kobjs[i];
1580 		space_info->block_group_kobjs[i] = NULL;
1581 		if (kobj) {
1582 			kobject_del(kobj);
1583 			kobject_put(kobj);
1584 		}
1585 	}
1586 	kobject_del(&space_info->kobj);
1587 	kobject_put(&space_info->kobj);
1588 }
1589 
1590 static const char *alloc_name(u64 flags)
1591 {
1592 	switch (flags) {
1593 	case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1594 		return "mixed";
1595 	case BTRFS_BLOCK_GROUP_METADATA:
1596 		return "metadata";
1597 	case BTRFS_BLOCK_GROUP_DATA:
1598 		return "data";
1599 	case BTRFS_BLOCK_GROUP_SYSTEM:
1600 		return "system";
1601 	default:
1602 		WARN_ON(1);
1603 		return "invalid-combination";
1604 	}
1605 }
1606 
1607 /*
1608  * Create a sysfs entry for a space info type at path
1609  * /sys/fs/btrfs/UUID/allocation/TYPE
1610  */
1611 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1612 				    struct btrfs_space_info *space_info)
1613 {
1614 	int ret;
1615 
1616 	ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1617 				   fs_info->space_info_kobj, "%s",
1618 				   alloc_name(space_info->flags));
1619 	if (ret) {
1620 		kobject_put(&space_info->kobj);
1621 		return ret;
1622 	}
1623 
1624 	return 0;
1625 }
1626 
1627 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1628 {
1629 	struct kobject *devices_kobj;
1630 
1631 	/*
1632 	 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1633 	 * fs_info::fs_devices.
1634 	 */
1635 	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1636 	ASSERT(devices_kobj);
1637 
1638 	if (device->bdev)
1639 		sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1640 
1641 	if (device->devid_kobj.state_initialized) {
1642 		kobject_del(&device->devid_kobj);
1643 		kobject_put(&device->devid_kobj);
1644 		wait_for_completion(&device->kobj_unregister);
1645 	}
1646 }
1647 
1648 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1649 					         struct kobj_attribute *a,
1650 					         char *buf)
1651 {
1652 	int val;
1653 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1654 						   devid_kobj);
1655 
1656 	val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1657 
1658 	return sysfs_emit(buf, "%d\n", val);
1659 }
1660 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1661 
1662 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1663 					struct kobj_attribute *a, char *buf)
1664 {
1665 	int val;
1666 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1667 						   devid_kobj);
1668 
1669 	val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1670 
1671 	return sysfs_emit(buf, "%d\n", val);
1672 }
1673 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1674 
1675 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1676 					         struct kobj_attribute *a,
1677 					         char *buf)
1678 {
1679 	int val;
1680 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1681 						   devid_kobj);
1682 
1683 	val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1684 
1685 	return sysfs_emit(buf, "%d\n", val);
1686 }
1687 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1688 
1689 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1690 					     struct kobj_attribute *a,
1691 					     char *buf)
1692 {
1693 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1694 						   devid_kobj);
1695 
1696 	return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1697 }
1698 
1699 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1700 					      struct kobj_attribute *a,
1701 					      const char *buf, size_t len)
1702 {
1703 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1704 						   devid_kobj);
1705 	char *endptr;
1706 	unsigned long long limit;
1707 
1708 	limit = memparse(buf, &endptr);
1709 	WRITE_ONCE(device->scrub_speed_max, limit);
1710 	return len;
1711 }
1712 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1713 	      btrfs_devinfo_scrub_speed_max_store);
1714 
1715 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1716 					    struct kobj_attribute *a, char *buf)
1717 {
1718 	int val;
1719 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1720 						   devid_kobj);
1721 
1722 	val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1723 
1724 	return sysfs_emit(buf, "%d\n", val);
1725 }
1726 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1727 
1728 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1729 				       struct kobj_attribute *a, char *buf)
1730 {
1731 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1732 						   devid_kobj);
1733 
1734 	return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1735 }
1736 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1737 
1738 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1739 		struct kobj_attribute *a, char *buf)
1740 {
1741 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1742 						   devid_kobj);
1743 
1744 	if (!device->dev_stats_valid)
1745 		return sysfs_emit(buf, "invalid\n");
1746 
1747 	/*
1748 	 * Print all at once so we get a snapshot of all values from the same
1749 	 * time. Keep them in sync and in order of definition of
1750 	 * btrfs_dev_stat_values.
1751 	 */
1752 	return sysfs_emit(buf,
1753 		"write_errs %d\n"
1754 		"read_errs %d\n"
1755 		"flush_errs %d\n"
1756 		"corruption_errs %d\n"
1757 		"generation_errs %d\n",
1758 		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1759 		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1760 		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1761 		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1762 		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1763 }
1764 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1765 
1766 /*
1767  * Information about one device.
1768  *
1769  * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1770  */
1771 static struct attribute *devid_attrs[] = {
1772 	BTRFS_ATTR_PTR(devid, error_stats),
1773 	BTRFS_ATTR_PTR(devid, fsid),
1774 	BTRFS_ATTR_PTR(devid, in_fs_metadata),
1775 	BTRFS_ATTR_PTR(devid, missing),
1776 	BTRFS_ATTR_PTR(devid, replace_target),
1777 	BTRFS_ATTR_PTR(devid, scrub_speed_max),
1778 	BTRFS_ATTR_PTR(devid, writeable),
1779 	NULL
1780 };
1781 ATTRIBUTE_GROUPS(devid);
1782 
1783 static void btrfs_release_devid_kobj(struct kobject *kobj)
1784 {
1785 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1786 						   devid_kobj);
1787 
1788 	memset(&device->devid_kobj, 0, sizeof(struct kobject));
1789 	complete(&device->kobj_unregister);
1790 }
1791 
1792 static struct kobj_type devid_ktype = {
1793 	.sysfs_ops	= &kobj_sysfs_ops,
1794 	.default_groups = devid_groups,
1795 	.release	= btrfs_release_devid_kobj,
1796 };
1797 
1798 int btrfs_sysfs_add_device(struct btrfs_device *device)
1799 {
1800 	int ret;
1801 	unsigned int nofs_flag;
1802 	struct kobject *devices_kobj;
1803 	struct kobject *devinfo_kobj;
1804 
1805 	/*
1806 	 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1807 	 * for the seed fs_devices
1808 	 */
1809 	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1810 	devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1811 	ASSERT(devices_kobj);
1812 	ASSERT(devinfo_kobj);
1813 
1814 	nofs_flag = memalloc_nofs_save();
1815 
1816 	if (device->bdev) {
1817 		struct kobject *disk_kobj = bdev_kobj(device->bdev);
1818 
1819 		ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1820 		if (ret) {
1821 			btrfs_warn(device->fs_info,
1822 				"creating sysfs device link for devid %llu failed: %d",
1823 				device->devid, ret);
1824 			goto out;
1825 		}
1826 	}
1827 
1828 	init_completion(&device->kobj_unregister);
1829 	ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1830 				   devinfo_kobj, "%llu", device->devid);
1831 	if (ret) {
1832 		kobject_put(&device->devid_kobj);
1833 		btrfs_warn(device->fs_info,
1834 			   "devinfo init for devid %llu failed: %d",
1835 			   device->devid, ret);
1836 	}
1837 
1838 out:
1839 	memalloc_nofs_restore(nofs_flag);
1840 	return ret;
1841 }
1842 
1843 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1844 {
1845 	int ret;
1846 	struct btrfs_device *device;
1847 	struct btrfs_fs_devices *seed;
1848 
1849 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1850 		ret = btrfs_sysfs_add_device(device);
1851 		if (ret)
1852 			goto fail;
1853 	}
1854 
1855 	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1856 		list_for_each_entry(device, &seed->devices, dev_list) {
1857 			ret = btrfs_sysfs_add_device(device);
1858 			if (ret)
1859 				goto fail;
1860 		}
1861 	}
1862 
1863 	return 0;
1864 
1865 fail:
1866 	btrfs_sysfs_remove_fs_devices(fs_devices);
1867 	return ret;
1868 }
1869 
1870 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1871 {
1872 	int ret;
1873 
1874 	ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1875 	if (ret)
1876 		pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1877 			action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1878 			&disk_to_dev(bdev->bd_disk)->kobj);
1879 }
1880 
1881 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1882 
1883 {
1884 	char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1885 
1886 	/*
1887 	 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1888 	 * directory
1889 	 */
1890 	snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1891 	if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1892 		btrfs_warn(fs_devices->fs_info,
1893 				"sysfs: failed to create fsid for sprout");
1894 }
1895 
1896 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1897 {
1898 	char tmp[24];
1899 
1900 	snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1901 
1902 	if (kobject_rename(&device->devid_kobj, tmp))
1903 		btrfs_warn(device->fs_devices->fs_info,
1904 			   "sysfs: failed to update devid for %llu",
1905 			   device->devid);
1906 }
1907 
1908 /* /sys/fs/btrfs/ entry */
1909 static struct kset *btrfs_kset;
1910 
1911 /*
1912  * Creates:
1913  *		/sys/fs/btrfs/UUID
1914  *
1915  * Can be called by the device discovery thread.
1916  */
1917 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1918 {
1919 	int error;
1920 
1921 	init_completion(&fs_devs->kobj_unregister);
1922 	fs_devs->fsid_kobj.kset = btrfs_kset;
1923 	error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1924 				     "%pU", fs_devs->fsid);
1925 	if (error) {
1926 		kobject_put(&fs_devs->fsid_kobj);
1927 		return error;
1928 	}
1929 
1930 	fs_devs->devices_kobj = kobject_create_and_add("devices",
1931 						       &fs_devs->fsid_kobj);
1932 	if (!fs_devs->devices_kobj) {
1933 		btrfs_err(fs_devs->fs_info,
1934 			  "failed to init sysfs device interface");
1935 		btrfs_sysfs_remove_fsid(fs_devs);
1936 		return -ENOMEM;
1937 	}
1938 
1939 	fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1940 						       &fs_devs->fsid_kobj);
1941 	if (!fs_devs->devinfo_kobj) {
1942 		btrfs_err(fs_devs->fs_info,
1943 			  "failed to init sysfs devinfo kobject");
1944 		btrfs_sysfs_remove_fsid(fs_devs);
1945 		return -ENOMEM;
1946 	}
1947 
1948 	return 0;
1949 }
1950 
1951 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1952 {
1953 	int error;
1954 	struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1955 	struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1956 
1957 	error = btrfs_sysfs_add_fs_devices(fs_devs);
1958 	if (error)
1959 		return error;
1960 
1961 	error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1962 	if (error) {
1963 		btrfs_sysfs_remove_fs_devices(fs_devs);
1964 		return error;
1965 	}
1966 
1967 	error = sysfs_create_group(fsid_kobj,
1968 				   &btrfs_feature_attr_group);
1969 	if (error)
1970 		goto failure;
1971 
1972 #ifdef CONFIG_BTRFS_DEBUG
1973 	fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1974 	if (!fs_info->debug_kobj) {
1975 		error = -ENOMEM;
1976 		goto failure;
1977 	}
1978 
1979 	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1980 	if (error)
1981 		goto failure;
1982 #endif
1983 
1984 	/* Discard directory */
1985 	fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
1986 	if (!fs_info->discard_kobj) {
1987 		error = -ENOMEM;
1988 		goto failure;
1989 	}
1990 
1991 	error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
1992 	if (error)
1993 		goto failure;
1994 
1995 	error = addrm_unknown_feature_attrs(fs_info, true);
1996 	if (error)
1997 		goto failure;
1998 
1999 	error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2000 	if (error)
2001 		goto failure;
2002 
2003 	fs_info->space_info_kobj = kobject_create_and_add("allocation",
2004 						  fsid_kobj);
2005 	if (!fs_info->space_info_kobj) {
2006 		error = -ENOMEM;
2007 		goto failure;
2008 	}
2009 
2010 	error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2011 	if (error)
2012 		goto failure;
2013 
2014 	return 0;
2015 failure:
2016 	btrfs_sysfs_remove_mounted(fs_info);
2017 	return error;
2018 }
2019 
2020 static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2021 				   struct kobj_attribute *a,
2022 				   char *buf)
2023 {
2024 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2025 	bool enabled;
2026 
2027 	spin_lock(&fs_info->qgroup_lock);
2028 	enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
2029 	spin_unlock(&fs_info->qgroup_lock);
2030 
2031 	return sysfs_emit(buf, "%d\n", enabled);
2032 }
2033 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2034 
2035 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2036 					struct kobj_attribute *a,
2037 					char *buf)
2038 {
2039 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2040 	bool inconsistent;
2041 
2042 	spin_lock(&fs_info->qgroup_lock);
2043 	inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
2044 	spin_unlock(&fs_info->qgroup_lock);
2045 
2046 	return sysfs_emit(buf, "%d\n", inconsistent);
2047 }
2048 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2049 
2050 static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2051 					      struct kobj_attribute *a,
2052 					      char *buf)
2053 {
2054 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2055 	u8 result;
2056 
2057 	spin_lock(&fs_info->qgroup_lock);
2058 	result = fs_info->qgroup_drop_subtree_thres;
2059 	spin_unlock(&fs_info->qgroup_lock);
2060 
2061 	return sysfs_emit(buf, "%d\n", result);
2062 }
2063 
2064 static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2065 					       struct kobj_attribute *a,
2066 					       const char *buf, size_t len)
2067 {
2068 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2069 	u8 new_thres;
2070 	int ret;
2071 
2072 	ret = kstrtou8(buf, 10, &new_thres);
2073 	if (ret)
2074 		return -EINVAL;
2075 
2076 	if (new_thres > BTRFS_MAX_LEVEL)
2077 		return -EINVAL;
2078 
2079 	spin_lock(&fs_info->qgroup_lock);
2080 	fs_info->qgroup_drop_subtree_thres = new_thres;
2081 	spin_unlock(&fs_info->qgroup_lock);
2082 
2083 	return len;
2084 }
2085 BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2086 	      qgroup_drop_subtree_thres_store);
2087 
2088 /*
2089  * Qgroups global info
2090  *
2091  * Path: /sys/fs/btrfs/<uuid>/qgroups/
2092  */
2093 static struct attribute *qgroups_attrs[] = {
2094 	BTRFS_ATTR_PTR(qgroups, enabled),
2095 	BTRFS_ATTR_PTR(qgroups, inconsistent),
2096 	BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2097 	NULL
2098 };
2099 ATTRIBUTE_GROUPS(qgroups);
2100 
2101 static void qgroups_release(struct kobject *kobj)
2102 {
2103 	kfree(kobj);
2104 }
2105 
2106 static struct kobj_type qgroups_ktype = {
2107 	.sysfs_ops = &kobj_sysfs_ops,
2108 	.default_groups = qgroups_groups,
2109 	.release = qgroups_release,
2110 };
2111 
2112 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2113 {
2114 	return to_fs_info(kobj->parent->parent);
2115 }
2116 
2117 #define QGROUP_ATTR(_member, _show_name)					\
2118 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj,		\
2119 					   struct kobj_attribute *a,		\
2120 					   char *buf)				\
2121 {										\
2122 	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
2123 	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
2124 			struct btrfs_qgroup, kobj);				\
2125 	return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf);	\
2126 }										\
2127 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2128 
2129 #define QGROUP_RSV_ATTR(_name, _type)						\
2130 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj,	\
2131 					     struct kobj_attribute *a,		\
2132 					     char *buf)				\
2133 {										\
2134 	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
2135 	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
2136 			struct btrfs_qgroup, kobj);				\
2137 	return btrfs_show_u64(&qgroup->rsv.values[_type],			\
2138 			&fs_info->qgroup_lock, buf);				\
2139 }										\
2140 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2141 
2142 QGROUP_ATTR(rfer, referenced);
2143 QGROUP_ATTR(excl, exclusive);
2144 QGROUP_ATTR(max_rfer, max_referenced);
2145 QGROUP_ATTR(max_excl, max_exclusive);
2146 QGROUP_ATTR(lim_flags, limit_flags);
2147 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2148 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2149 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2150 
2151 /*
2152  * Qgroup information.
2153  *
2154  * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2155  */
2156 static struct attribute *qgroup_attrs[] = {
2157 	BTRFS_ATTR_PTR(qgroup, referenced),
2158 	BTRFS_ATTR_PTR(qgroup, exclusive),
2159 	BTRFS_ATTR_PTR(qgroup, max_referenced),
2160 	BTRFS_ATTR_PTR(qgroup, max_exclusive),
2161 	BTRFS_ATTR_PTR(qgroup, limit_flags),
2162 	BTRFS_ATTR_PTR(qgroup, rsv_data),
2163 	BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2164 	BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2165 	NULL
2166 };
2167 ATTRIBUTE_GROUPS(qgroup);
2168 
2169 static void qgroup_release(struct kobject *kobj)
2170 {
2171 	struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2172 
2173 	memset(&qgroup->kobj, 0, sizeof(*kobj));
2174 }
2175 
2176 static struct kobj_type qgroup_ktype = {
2177 	.sysfs_ops = &kobj_sysfs_ops,
2178 	.release = qgroup_release,
2179 	.default_groups = qgroup_groups,
2180 };
2181 
2182 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2183 				struct btrfs_qgroup *qgroup)
2184 {
2185 	struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2186 	int ret;
2187 
2188 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2189 		return 0;
2190 	if (qgroup->kobj.state_initialized)
2191 		return 0;
2192 	if (!qgroups_kobj)
2193 		return -EINVAL;
2194 
2195 	ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2196 			"%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2197 			btrfs_qgroup_subvolid(qgroup->qgroupid));
2198 	if (ret < 0)
2199 		kobject_put(&qgroup->kobj);
2200 
2201 	return ret;
2202 }
2203 
2204 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2205 {
2206 	struct btrfs_qgroup *qgroup;
2207 	struct btrfs_qgroup *next;
2208 
2209 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2210 		return;
2211 
2212 	rbtree_postorder_for_each_entry_safe(qgroup, next,
2213 					     &fs_info->qgroup_tree, node)
2214 		btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2215 	if (fs_info->qgroups_kobj) {
2216 		kobject_del(fs_info->qgroups_kobj);
2217 		kobject_put(fs_info->qgroups_kobj);
2218 		fs_info->qgroups_kobj = NULL;
2219 	}
2220 }
2221 
2222 /* Called when qgroups get initialized, thus there is no need for locking */
2223 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2224 {
2225 	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2226 	struct btrfs_qgroup *qgroup;
2227 	struct btrfs_qgroup *next;
2228 	int ret = 0;
2229 
2230 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2231 		return 0;
2232 
2233 	ASSERT(fsid_kobj);
2234 	if (fs_info->qgroups_kobj)
2235 		return 0;
2236 
2237 	fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
2238 	if (!fs_info->qgroups_kobj)
2239 		return -ENOMEM;
2240 
2241 	ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2242 				   fsid_kobj, "qgroups");
2243 	if (ret < 0)
2244 		goto out;
2245 
2246 	rbtree_postorder_for_each_entry_safe(qgroup, next,
2247 					     &fs_info->qgroup_tree, node) {
2248 		ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2249 		if (ret < 0)
2250 			goto out;
2251 	}
2252 
2253 out:
2254 	if (ret < 0)
2255 		btrfs_sysfs_del_qgroups(fs_info);
2256 	return ret;
2257 }
2258 
2259 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2260 				struct btrfs_qgroup *qgroup)
2261 {
2262 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2263 		return;
2264 
2265 	if (qgroup->kobj.state_initialized) {
2266 		kobject_del(&qgroup->kobj);
2267 		kobject_put(&qgroup->kobj);
2268 	}
2269 }
2270 
2271 /*
2272  * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2273  * values in superblock. Call after any changes to incompat/compat_ro flags
2274  */
2275 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
2276 		u64 bit, enum btrfs_feature_set set)
2277 {
2278 	struct btrfs_fs_devices *fs_devs;
2279 	struct kobject *fsid_kobj;
2280 	u64 __maybe_unused features;
2281 	int __maybe_unused ret;
2282 
2283 	if (!fs_info)
2284 		return;
2285 
2286 	/*
2287 	 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2288 	 * safe when called from some contexts (eg. balance)
2289 	 */
2290 	features = get_features(fs_info, set);
2291 	ASSERT(bit & supported_feature_masks[set]);
2292 
2293 	fs_devs = fs_info->fs_devices;
2294 	fsid_kobj = &fs_devs->fsid_kobj;
2295 
2296 	if (!fsid_kobj->state_initialized)
2297 		return;
2298 
2299 	/*
2300 	 * FIXME: this is too heavy to update just one value, ideally we'd like
2301 	 * to use sysfs_update_group but some refactoring is needed first.
2302 	 */
2303 	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2304 	ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2305 }
2306 
2307 int __init btrfs_init_sysfs(void)
2308 {
2309 	int ret;
2310 
2311 	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2312 	if (!btrfs_kset)
2313 		return -ENOMEM;
2314 
2315 	init_feature_attrs();
2316 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2317 	if (ret)
2318 		goto out2;
2319 	ret = sysfs_merge_group(&btrfs_kset->kobj,
2320 				&btrfs_static_feature_attr_group);
2321 	if (ret)
2322 		goto out_remove_group;
2323 
2324 #ifdef CONFIG_BTRFS_DEBUG
2325 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2326 	if (ret) {
2327 		sysfs_unmerge_group(&btrfs_kset->kobj,
2328 				    &btrfs_static_feature_attr_group);
2329 		goto out_remove_group;
2330 	}
2331 #endif
2332 
2333 	return 0;
2334 
2335 out_remove_group:
2336 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2337 out2:
2338 	kset_unregister(btrfs_kset);
2339 
2340 	return ret;
2341 }
2342 
2343 void __cold btrfs_exit_sysfs(void)
2344 {
2345 	sysfs_unmerge_group(&btrfs_kset->kobj,
2346 			    &btrfs_static_feature_attr_group);
2347 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2348 #ifdef CONFIG_BTRFS_DEBUG
2349 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2350 #endif
2351 	kset_unregister(btrfs_kset);
2352 }
2353