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