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