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