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