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