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