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