xref: /openbmc/linux/fs/btrfs/sysfs.c (revision df93589a)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
1958176a96SJosef Bacik #include <linux/sched.h>
2058176a96SJosef Bacik #include <linux/slab.h>
2158176a96SJosef Bacik #include <linux/spinlock.h>
2258176a96SJosef Bacik #include <linux/completion.h>
2358176a96SJosef Bacik #include <linux/buffer_head.h>
2458176a96SJosef Bacik #include <linux/kobject.h>
2579da4fa4SJeff Mahoney #include <linux/bug.h>
2629e5be24SJeff Mahoney #include <linux/genhd.h>
271bae3098SDavid Sterba #include <linux/debugfs.h>
2858176a96SJosef Bacik 
29bae45de0SChris Mason #include "ctree.h"
30bae45de0SChris Mason #include "disk-io.h"
31bae45de0SChris Mason #include "transaction.h"
32079b72bcSJeff Mahoney #include "sysfs.h"
3329e5be24SJeff Mahoney #include "volumes.h"
34079b72bcSJeff Mahoney 
35510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
365ac1d209SJeff Mahoney 
37510d7360SJeff Mahoney static u64 get_features(struct btrfs_fs_info *fs_info,
38510d7360SJeff Mahoney 			enum btrfs_feature_set set)
395ac1d209SJeff Mahoney {
40510d7360SJeff Mahoney 	struct btrfs_super_block *disk_super = fs_info->super_copy;
41510d7360SJeff Mahoney 	if (set == FEAT_COMPAT)
42510d7360SJeff Mahoney 		return btrfs_super_compat_flags(disk_super);
43510d7360SJeff Mahoney 	else if (set == FEAT_COMPAT_RO)
44510d7360SJeff Mahoney 		return btrfs_super_compat_ro_flags(disk_super);
45510d7360SJeff Mahoney 	else
46510d7360SJeff Mahoney 		return btrfs_super_incompat_flags(disk_super);
475ac1d209SJeff Mahoney }
485ac1d209SJeff Mahoney 
49ba631941SJeff Mahoney static void set_features(struct btrfs_fs_info *fs_info,
50ba631941SJeff Mahoney 			 enum btrfs_feature_set set, u64 features)
51ba631941SJeff Mahoney {
52ba631941SJeff Mahoney 	struct btrfs_super_block *disk_super = fs_info->super_copy;
53ba631941SJeff Mahoney 	if (set == FEAT_COMPAT)
54ba631941SJeff Mahoney 		btrfs_set_super_compat_flags(disk_super, features);
55ba631941SJeff Mahoney 	else if (set == FEAT_COMPAT_RO)
56ba631941SJeff Mahoney 		btrfs_set_super_compat_ro_flags(disk_super, features);
57ba631941SJeff Mahoney 	else
58ba631941SJeff Mahoney 		btrfs_set_super_incompat_flags(disk_super, features);
59ba631941SJeff Mahoney }
60ba631941SJeff Mahoney 
61ba631941SJeff Mahoney static int can_modify_feature(struct btrfs_feature_attr *fa)
62ba631941SJeff Mahoney {
63ba631941SJeff Mahoney 	int val = 0;
64ba631941SJeff Mahoney 	u64 set, clear;
65ba631941SJeff Mahoney 	switch (fa->feature_set) {
66ba631941SJeff Mahoney 	case FEAT_COMPAT:
67ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
68ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
69ba631941SJeff Mahoney 		break;
70ba631941SJeff Mahoney 	case FEAT_COMPAT_RO:
71ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
72ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
73ba631941SJeff Mahoney 		break;
74ba631941SJeff Mahoney 	case FEAT_INCOMPAT:
75ba631941SJeff Mahoney 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
76ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
77ba631941SJeff Mahoney 		break;
78ba631941SJeff Mahoney 	default:
79cc37bb04SDavid Sterba 		printk(KERN_WARNING "btrfs: sysfs: unknown feature set %d\n",
80cc37bb04SDavid Sterba 				fa->feature_set);
81cc37bb04SDavid Sterba 		return 0;
82ba631941SJeff Mahoney 	}
83ba631941SJeff Mahoney 
84ba631941SJeff Mahoney 	if (set & fa->feature_bit)
85ba631941SJeff Mahoney 		val |= 1;
86ba631941SJeff Mahoney 	if (clear & fa->feature_bit)
87ba631941SJeff Mahoney 		val |= 2;
88ba631941SJeff Mahoney 
89ba631941SJeff Mahoney 	return val;
90ba631941SJeff Mahoney }
91ba631941SJeff Mahoney 
92079b72bcSJeff Mahoney static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
93079b72bcSJeff Mahoney 				       struct kobj_attribute *a, char *buf)
94079b72bcSJeff Mahoney {
95510d7360SJeff Mahoney 	int val = 0;
96510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
97510d7360SJeff Mahoney 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
98ba631941SJeff Mahoney 	if (fs_info) {
99510d7360SJeff Mahoney 		u64 features = get_features(fs_info, fa->feature_set);
100510d7360SJeff Mahoney 		if (features & fa->feature_bit)
101510d7360SJeff Mahoney 			val = 1;
102ba631941SJeff Mahoney 	} else
103ba631941SJeff Mahoney 		val = can_modify_feature(fa);
104510d7360SJeff Mahoney 
105510d7360SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
106510d7360SJeff Mahoney }
107510d7360SJeff Mahoney 
108ba631941SJeff Mahoney static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
109ba631941SJeff Mahoney 					struct kobj_attribute *a,
110ba631941SJeff Mahoney 					const char *buf, size_t count)
111ba631941SJeff Mahoney {
112ba631941SJeff Mahoney 	struct btrfs_fs_info *fs_info;
113ba631941SJeff Mahoney 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
114ba631941SJeff Mahoney 	struct btrfs_trans_handle *trans;
115ba631941SJeff Mahoney 	u64 features, set, clear;
116ba631941SJeff Mahoney 	unsigned long val;
117ba631941SJeff Mahoney 	int ret;
118ba631941SJeff Mahoney 
119ba631941SJeff Mahoney 	fs_info = to_fs_info(kobj);
120ba631941SJeff Mahoney 	if (!fs_info)
121ba631941SJeff Mahoney 		return -EPERM;
122ba631941SJeff Mahoney 
123ba631941SJeff Mahoney 	ret = kstrtoul(skip_spaces(buf), 0, &val);
124ba631941SJeff Mahoney 	if (ret)
125ba631941SJeff Mahoney 		return ret;
126ba631941SJeff Mahoney 
127ba631941SJeff Mahoney 	if (fa->feature_set == FEAT_COMPAT) {
128ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
129ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
130ba631941SJeff Mahoney 	} else if (fa->feature_set == FEAT_COMPAT_RO) {
131ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
132ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
133ba631941SJeff Mahoney 	} else {
134ba631941SJeff Mahoney 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
135ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
136ba631941SJeff Mahoney 	}
137ba631941SJeff Mahoney 
138ba631941SJeff Mahoney 	features = get_features(fs_info, fa->feature_set);
139ba631941SJeff Mahoney 
140ba631941SJeff Mahoney 	/* Nothing to do */
141ba631941SJeff Mahoney 	if ((val && (features & fa->feature_bit)) ||
142ba631941SJeff Mahoney 	    (!val && !(features & fa->feature_bit)))
143ba631941SJeff Mahoney 		return count;
144ba631941SJeff Mahoney 
145ba631941SJeff Mahoney 	if ((val && !(set & fa->feature_bit)) ||
146ba631941SJeff Mahoney 	    (!val && !(clear & fa->feature_bit))) {
147ba631941SJeff Mahoney 		btrfs_info(fs_info,
148ba631941SJeff Mahoney 			"%sabling feature %s on mounted fs is not supported.",
149ba631941SJeff Mahoney 			val ? "En" : "Dis", fa->kobj_attr.attr.name);
150ba631941SJeff Mahoney 		return -EPERM;
151ba631941SJeff Mahoney 	}
152ba631941SJeff Mahoney 
153ba631941SJeff Mahoney 	btrfs_info(fs_info, "%s %s feature flag",
154ba631941SJeff Mahoney 		   val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
155ba631941SJeff Mahoney 
15643d87fa2SDavid Sterba 	trans = btrfs_start_transaction(fs_info->fs_root, 0);
157ba631941SJeff Mahoney 	if (IS_ERR(trans))
158ba631941SJeff Mahoney 		return PTR_ERR(trans);
159ba631941SJeff Mahoney 
160ba631941SJeff Mahoney 	spin_lock(&fs_info->super_lock);
161ba631941SJeff Mahoney 	features = get_features(fs_info, fa->feature_set);
162ba631941SJeff Mahoney 	if (val)
163ba631941SJeff Mahoney 		features |= fa->feature_bit;
164ba631941SJeff Mahoney 	else
165ba631941SJeff Mahoney 		features &= ~fa->feature_bit;
166ba631941SJeff Mahoney 	set_features(fs_info, fa->feature_set, features);
167ba631941SJeff Mahoney 	spin_unlock(&fs_info->super_lock);
168ba631941SJeff Mahoney 
169ba631941SJeff Mahoney 	ret = btrfs_commit_transaction(trans, fs_info->fs_root);
170ba631941SJeff Mahoney 	if (ret)
171ba631941SJeff Mahoney 		return ret;
172ba631941SJeff Mahoney 
173ba631941SJeff Mahoney 	return count;
174ba631941SJeff Mahoney }
175ba631941SJeff Mahoney 
176510d7360SJeff Mahoney static umode_t btrfs_feature_visible(struct kobject *kobj,
177510d7360SJeff Mahoney 				     struct attribute *attr, int unused)
178510d7360SJeff Mahoney {
179510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
180510d7360SJeff Mahoney 	umode_t mode = attr->mode;
181510d7360SJeff Mahoney 
182510d7360SJeff Mahoney 	if (fs_info) {
183510d7360SJeff Mahoney 		struct btrfs_feature_attr *fa;
184510d7360SJeff Mahoney 		u64 features;
185510d7360SJeff Mahoney 
186510d7360SJeff Mahoney 		fa = attr_to_btrfs_feature_attr(attr);
187510d7360SJeff Mahoney 		features = get_features(fs_info, fa->feature_set);
188510d7360SJeff Mahoney 
189ba631941SJeff Mahoney 		if (can_modify_feature(fa))
190ba631941SJeff Mahoney 			mode |= S_IWUSR;
191ba631941SJeff Mahoney 		else if (!(features & fa->feature_bit))
192510d7360SJeff Mahoney 			mode = 0;
193510d7360SJeff Mahoney 	}
194510d7360SJeff Mahoney 
195510d7360SJeff Mahoney 	return mode;
196079b72bcSJeff Mahoney }
197079b72bcSJeff Mahoney 
198079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
199079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
200079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
201079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
202079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
203079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
204079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
205079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
206c736c095SDavid Sterba BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
207079b72bcSJeff Mahoney 
208079b72bcSJeff Mahoney static struct attribute *btrfs_supported_feature_attrs[] = {
209079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(mixed_backref),
210079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(default_subvol),
211079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(mixed_groups),
212079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(compress_lzo),
213079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(big_metadata),
214079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(extended_iref),
215079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(raid56),
216079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(skinny_metadata),
217c736c095SDavid Sterba 	BTRFS_FEAT_ATTR_PTR(no_holes),
218079b72bcSJeff Mahoney 	NULL
219079b72bcSJeff Mahoney };
220079b72bcSJeff Mahoney 
221079b72bcSJeff Mahoney static const struct attribute_group btrfs_feature_attr_group = {
222079b72bcSJeff Mahoney 	.name = "features",
223510d7360SJeff Mahoney 	.is_visible = btrfs_feature_visible,
224079b72bcSJeff Mahoney 	.attrs = btrfs_supported_feature_attrs,
225079b72bcSJeff Mahoney };
22658176a96SJosef Bacik 
2276ab0a202SJeff Mahoney static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
2286ab0a202SJeff Mahoney {
2296ab0a202SJeff Mahoney 	u64 val;
2306ab0a202SJeff Mahoney 	if (lock)
2316ab0a202SJeff Mahoney 		spin_lock(lock);
2326ab0a202SJeff Mahoney 	val = *value_ptr;
2336ab0a202SJeff Mahoney 	if (lock)
2346ab0a202SJeff Mahoney 		spin_unlock(lock);
2356ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%llu\n", val);
2366ab0a202SJeff Mahoney }
2376ab0a202SJeff Mahoney 
2386ab0a202SJeff Mahoney static ssize_t global_rsv_size_show(struct kobject *kobj,
2396ab0a202SJeff Mahoney 				    struct kobj_attribute *ka, char *buf)
2406ab0a202SJeff Mahoney {
2416ab0a202SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
2426ab0a202SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2436ab0a202SJeff Mahoney 	return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
2446ab0a202SJeff Mahoney }
2456ab0a202SJeff Mahoney BTRFS_ATTR(global_rsv_size, 0444, global_rsv_size_show);
2466ab0a202SJeff Mahoney 
2476ab0a202SJeff Mahoney static ssize_t global_rsv_reserved_show(struct kobject *kobj,
2486ab0a202SJeff Mahoney 					struct kobj_attribute *a, char *buf)
2496ab0a202SJeff Mahoney {
2506ab0a202SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
2516ab0a202SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2526ab0a202SJeff Mahoney 	return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
2536ab0a202SJeff Mahoney }
2546ab0a202SJeff Mahoney BTRFS_ATTR(global_rsv_reserved, 0444, global_rsv_reserved_show);
2556ab0a202SJeff Mahoney 
2566ab0a202SJeff Mahoney #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
2576ab0a202SJeff Mahoney 
2586ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj,
2596ab0a202SJeff Mahoney 			       struct kobj_attribute *attr, char *buf);
2606ab0a202SJeff Mahoney BTRFS_RAID_ATTR(total_bytes, raid_bytes_show);
2616ab0a202SJeff Mahoney BTRFS_RAID_ATTR(used_bytes, raid_bytes_show);
2626ab0a202SJeff Mahoney 
2636ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj,
2646ab0a202SJeff Mahoney 			       struct kobj_attribute *attr, char *buf)
2656ab0a202SJeff Mahoney 
2666ab0a202SJeff Mahoney {
2676ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
2686ab0a202SJeff Mahoney 	struct btrfs_block_group_cache *block_group;
2696ab0a202SJeff Mahoney 	int index = kobj - sinfo->block_group_kobjs;
2706ab0a202SJeff Mahoney 	u64 val = 0;
2716ab0a202SJeff Mahoney 
2726ab0a202SJeff Mahoney 	down_read(&sinfo->groups_sem);
2736ab0a202SJeff Mahoney 	list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
2746ab0a202SJeff Mahoney 		if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes))
2756ab0a202SJeff Mahoney 			val += block_group->key.offset;
2766ab0a202SJeff Mahoney 		else
2776ab0a202SJeff Mahoney 			val += btrfs_block_group_used(&block_group->item);
2786ab0a202SJeff Mahoney 	}
2796ab0a202SJeff Mahoney 	up_read(&sinfo->groups_sem);
2806ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%llu\n", val);
2816ab0a202SJeff Mahoney }
2826ab0a202SJeff Mahoney 
2836ab0a202SJeff Mahoney static struct attribute *raid_attributes[] = {
2846ab0a202SJeff Mahoney 	BTRFS_RAID_ATTR_PTR(total_bytes),
2856ab0a202SJeff Mahoney 	BTRFS_RAID_ATTR_PTR(used_bytes),
2866ab0a202SJeff Mahoney 	NULL
2876ab0a202SJeff Mahoney };
2886ab0a202SJeff Mahoney 
2896ab0a202SJeff Mahoney static void release_raid_kobj(struct kobject *kobj)
2906ab0a202SJeff Mahoney {
2916ab0a202SJeff Mahoney 	kobject_put(kobj->parent);
2926ab0a202SJeff Mahoney }
2936ab0a202SJeff Mahoney 
2946ab0a202SJeff Mahoney struct kobj_type btrfs_raid_ktype = {
2956ab0a202SJeff Mahoney 	.sysfs_ops = &kobj_sysfs_ops,
2966ab0a202SJeff Mahoney 	.release = release_raid_kobj,
2976ab0a202SJeff Mahoney 	.default_attrs = raid_attributes,
2986ab0a202SJeff Mahoney };
2996ab0a202SJeff Mahoney 
3006ab0a202SJeff Mahoney #define SPACE_INFO_ATTR(field)						\
3016ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,	\
3026ab0a202SJeff Mahoney 					     struct kobj_attribute *a,	\
3036ab0a202SJeff Mahoney 					     char *buf)			\
3046ab0a202SJeff Mahoney {									\
3056ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);		\
3066ab0a202SJeff Mahoney 	return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);	\
3076ab0a202SJeff Mahoney }									\
3086ab0a202SJeff Mahoney BTRFS_ATTR(field, 0444, btrfs_space_info_show_##field)
3096ab0a202SJeff Mahoney 
3106ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
3116ab0a202SJeff Mahoney 						       struct kobj_attribute *a,
3126ab0a202SJeff Mahoney 						       char *buf)
3136ab0a202SJeff Mahoney {
3146ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);
3156ab0a202SJeff Mahoney 	s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
3166ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%lld\n", val);
3176ab0a202SJeff Mahoney }
3186ab0a202SJeff Mahoney 
3196ab0a202SJeff Mahoney SPACE_INFO_ATTR(flags);
3206ab0a202SJeff Mahoney SPACE_INFO_ATTR(total_bytes);
3216ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_used);
3226ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_pinned);
3236ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_reserved);
3246ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_may_use);
3256ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_used);
3266ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_total);
3276ab0a202SJeff Mahoney BTRFS_ATTR(total_bytes_pinned, 0444, btrfs_space_info_show_total_bytes_pinned);
3286ab0a202SJeff Mahoney 
3296ab0a202SJeff Mahoney static struct attribute *space_info_attrs[] = {
3306ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(flags),
3316ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(total_bytes),
3326ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_used),
3336ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_pinned),
3346ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_reserved),
3356ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_may_use),
3366ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(disk_used),
3376ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(disk_total),
3386ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(total_bytes_pinned),
3396ab0a202SJeff Mahoney 	NULL,
3406ab0a202SJeff Mahoney };
3416ab0a202SJeff Mahoney 
3426ab0a202SJeff Mahoney static void space_info_release(struct kobject *kobj)
3436ab0a202SJeff Mahoney {
3446ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);
3456ab0a202SJeff Mahoney 	percpu_counter_destroy(&sinfo->total_bytes_pinned);
3466ab0a202SJeff Mahoney 	kfree(sinfo);
3476ab0a202SJeff Mahoney }
3486ab0a202SJeff Mahoney 
3496ab0a202SJeff Mahoney struct kobj_type space_info_ktype = {
3506ab0a202SJeff Mahoney 	.sysfs_ops = &kobj_sysfs_ops,
3516ab0a202SJeff Mahoney 	.release = space_info_release,
3526ab0a202SJeff Mahoney 	.default_attrs = space_info_attrs,
3536ab0a202SJeff Mahoney };
3546ab0a202SJeff Mahoney 
3556ab0a202SJeff Mahoney static const struct attribute *allocation_attrs[] = {
3566ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(global_rsv_reserved),
3576ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(global_rsv_size),
3586ab0a202SJeff Mahoney 	NULL,
3596ab0a202SJeff Mahoney };
3606ab0a202SJeff Mahoney 
361f8ba9c11SJeff Mahoney static ssize_t btrfs_label_show(struct kobject *kobj,
362f8ba9c11SJeff Mahoney 				struct kobj_attribute *a, char *buf)
363f8ba9c11SJeff Mahoney {
364f8ba9c11SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
365f8ba9c11SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label);
366f8ba9c11SJeff Mahoney }
367f8ba9c11SJeff Mahoney 
368f8ba9c11SJeff Mahoney static ssize_t btrfs_label_store(struct kobject *kobj,
369f8ba9c11SJeff Mahoney 				 struct kobj_attribute *a,
370f8ba9c11SJeff Mahoney 				 const char *buf, size_t len)
371f8ba9c11SJeff Mahoney {
372f8ba9c11SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
373f8ba9c11SJeff Mahoney 	struct btrfs_trans_handle *trans;
374f8ba9c11SJeff Mahoney 	struct btrfs_root *root = fs_info->fs_root;
375f8ba9c11SJeff Mahoney 	int ret;
376f8ba9c11SJeff Mahoney 
377f8ba9c11SJeff Mahoney 	if (len >= BTRFS_LABEL_SIZE) {
378efe120a0SFrank Holton 		pr_err("BTRFS: unable to set label with more than %d bytes\n",
379f8ba9c11SJeff Mahoney 		       BTRFS_LABEL_SIZE - 1);
380f8ba9c11SJeff Mahoney 		return -EINVAL;
381f8ba9c11SJeff Mahoney 	}
382f8ba9c11SJeff Mahoney 
383f8ba9c11SJeff Mahoney 	trans = btrfs_start_transaction(root, 0);
384f8ba9c11SJeff Mahoney 	if (IS_ERR(trans))
385f8ba9c11SJeff Mahoney 		return PTR_ERR(trans);
386f8ba9c11SJeff Mahoney 
387f8ba9c11SJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
388f8ba9c11SJeff Mahoney 	strcpy(fs_info->super_copy->label, buf);
389f8ba9c11SJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
390f8ba9c11SJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
391f8ba9c11SJeff Mahoney 
392f8ba9c11SJeff Mahoney 	if (!ret)
393f8ba9c11SJeff Mahoney 		return len;
394f8ba9c11SJeff Mahoney 
395f8ba9c11SJeff Mahoney 	return ret;
396f8ba9c11SJeff Mahoney }
397f8ba9c11SJeff Mahoney BTRFS_ATTR_RW(label, 0644, btrfs_label_show, btrfs_label_store);
398f8ba9c11SJeff Mahoney 
399df93589aSDavid Sterba static ssize_t btrfs_no_store(struct kobject *kobj,
400df93589aSDavid Sterba 				 struct kobj_attribute *a,
401df93589aSDavid Sterba 				 const char *buf, size_t len)
402df93589aSDavid Sterba {
403df93589aSDavid Sterba 	return -EPERM;
404df93589aSDavid Sterba }
405df93589aSDavid Sterba 
406df93589aSDavid Sterba static ssize_t btrfs_nodesize_show(struct kobject *kobj,
407df93589aSDavid Sterba 				struct kobj_attribute *a, char *buf)
408df93589aSDavid Sterba {
409df93589aSDavid Sterba 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
410df93589aSDavid Sterba 
411df93589aSDavid Sterba 	return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
412df93589aSDavid Sterba }
413df93589aSDavid Sterba 
414df93589aSDavid Sterba BTRFS_ATTR_RW(nodesize, 0444, btrfs_nodesize_show, btrfs_no_store);
415df93589aSDavid Sterba 
416df93589aSDavid Sterba static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
417df93589aSDavid Sterba 				struct kobj_attribute *a, char *buf)
418df93589aSDavid Sterba {
419df93589aSDavid Sterba 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
420df93589aSDavid Sterba 
421df93589aSDavid Sterba 	return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
422df93589aSDavid Sterba }
423df93589aSDavid Sterba 
424df93589aSDavid Sterba BTRFS_ATTR_RW(sectorsize, 0444, btrfs_sectorsize_show, btrfs_no_store);
425df93589aSDavid Sterba 
426df93589aSDavid Sterba static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
427df93589aSDavid Sterba 				struct kobj_attribute *a, char *buf)
428df93589aSDavid Sterba {
429df93589aSDavid Sterba 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
430df93589aSDavid Sterba 
431df93589aSDavid Sterba 	return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
432df93589aSDavid Sterba }
433df93589aSDavid Sterba 
434df93589aSDavid Sterba BTRFS_ATTR_RW(clone_alignment, 0444, btrfs_clone_alignment_show, btrfs_no_store);
435df93589aSDavid Sterba 
436f8ba9c11SJeff Mahoney static struct attribute *btrfs_attrs[] = {
437f8ba9c11SJeff Mahoney 	BTRFS_ATTR_PTR(label),
438df93589aSDavid Sterba 	BTRFS_ATTR_PTR(nodesize),
439df93589aSDavid Sterba 	BTRFS_ATTR_PTR(sectorsize),
440df93589aSDavid Sterba 	BTRFS_ATTR_PTR(clone_alignment),
441f8ba9c11SJeff Mahoney 	NULL,
442f8ba9c11SJeff Mahoney };
443f8ba9c11SJeff Mahoney 
444510d7360SJeff Mahoney static void btrfs_release_super_kobj(struct kobject *kobj)
445510d7360SJeff Mahoney {
446510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
447510d7360SJeff Mahoney 	complete(&fs_info->kobj_unregister);
448510d7360SJeff Mahoney }
449510d7360SJeff Mahoney 
450510d7360SJeff Mahoney static struct kobj_type btrfs_ktype = {
451510d7360SJeff Mahoney 	.sysfs_ops	= &kobj_sysfs_ops,
452510d7360SJeff Mahoney 	.release	= btrfs_release_super_kobj,
453f8ba9c11SJeff Mahoney 	.default_attrs	= btrfs_attrs,
454510d7360SJeff Mahoney };
455510d7360SJeff Mahoney 
456510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
457510d7360SJeff Mahoney {
458510d7360SJeff Mahoney 	if (kobj->ktype != &btrfs_ktype)
459510d7360SJeff Mahoney 		return NULL;
460510d7360SJeff Mahoney 	return container_of(kobj, struct btrfs_fs_info, super_kobj);
461510d7360SJeff Mahoney }
46258176a96SJosef Bacik 
463e453d989SJeff Mahoney #define NUM_FEATURE_BITS 64
464e453d989SJeff Mahoney static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
465e453d989SJeff Mahoney static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
466e453d989SJeff Mahoney 
467e453d989SJeff Mahoney static u64 supported_feature_masks[3] = {
468e453d989SJeff Mahoney 	[FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
469e453d989SJeff Mahoney 	[FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
470e453d989SJeff Mahoney 	[FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
471e453d989SJeff Mahoney };
472e453d989SJeff Mahoney 
473e453d989SJeff Mahoney static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
4745ac1d209SJeff Mahoney {
475e453d989SJeff Mahoney 	int set;
476e453d989SJeff Mahoney 
477e453d989SJeff Mahoney 	for (set = 0; set < FEAT_MAX; set++) {
478e453d989SJeff Mahoney 		int i;
479e453d989SJeff Mahoney 		struct attribute *attrs[2];
480e453d989SJeff Mahoney 		struct attribute_group agroup = {
481e453d989SJeff Mahoney 			.name = "features",
482e453d989SJeff Mahoney 			.attrs = attrs,
483e453d989SJeff Mahoney 		};
484e453d989SJeff Mahoney 		u64 features = get_features(fs_info, set);
485e453d989SJeff Mahoney 		features &= ~supported_feature_masks[set];
486e453d989SJeff Mahoney 
487e453d989SJeff Mahoney 		if (!features)
488e453d989SJeff Mahoney 			continue;
489e453d989SJeff Mahoney 
490e453d989SJeff Mahoney 		attrs[1] = NULL;
491e453d989SJeff Mahoney 		for (i = 0; i < NUM_FEATURE_BITS; i++) {
492e453d989SJeff Mahoney 			struct btrfs_feature_attr *fa;
493e453d989SJeff Mahoney 
494e453d989SJeff Mahoney 			if (!(features & (1ULL << i)))
495e453d989SJeff Mahoney 				continue;
496e453d989SJeff Mahoney 
497e453d989SJeff Mahoney 			fa = &btrfs_feature_attrs[set][i];
498e453d989SJeff Mahoney 			attrs[0] = &fa->kobj_attr.attr;
499e453d989SJeff Mahoney 			if (add) {
500e453d989SJeff Mahoney 				int ret;
501e453d989SJeff Mahoney 				ret = sysfs_merge_group(&fs_info->super_kobj,
502e453d989SJeff Mahoney 							&agroup);
503e453d989SJeff Mahoney 				if (ret)
504e453d989SJeff Mahoney 					return ret;
505e453d989SJeff Mahoney 			} else
506e453d989SJeff Mahoney 				sysfs_unmerge_group(&fs_info->super_kobj,
507e453d989SJeff Mahoney 						    &agroup);
508e453d989SJeff Mahoney 		}
509e453d989SJeff Mahoney 
510e453d989SJeff Mahoney 	}
511e453d989SJeff Mahoney 	return 0;
512e453d989SJeff Mahoney }
513e453d989SJeff Mahoney 
514e453d989SJeff Mahoney static void __btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
515e453d989SJeff Mahoney {
5165ac1d209SJeff Mahoney 	kobject_del(&fs_info->super_kobj);
5175ac1d209SJeff Mahoney 	kobject_put(&fs_info->super_kobj);
5185ac1d209SJeff Mahoney 	wait_for_completion(&fs_info->kobj_unregister);
5195ac1d209SJeff Mahoney }
5205ac1d209SJeff Mahoney 
521e453d989SJeff Mahoney void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
522e453d989SJeff Mahoney {
523e453d989SJeff Mahoney 	if (fs_info->space_info_kobj) {
524e453d989SJeff Mahoney 		sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
525e453d989SJeff Mahoney 		kobject_del(fs_info->space_info_kobj);
526e453d989SJeff Mahoney 		kobject_put(fs_info->space_info_kobj);
527e453d989SJeff Mahoney 	}
528e453d989SJeff Mahoney 	kobject_del(fs_info->device_dir_kobj);
529e453d989SJeff Mahoney 	kobject_put(fs_info->device_dir_kobj);
530e453d989SJeff Mahoney 	addrm_unknown_feature_attrs(fs_info, false);
531e453d989SJeff Mahoney 	sysfs_remove_group(&fs_info->super_kobj, &btrfs_feature_attr_group);
532e453d989SJeff Mahoney 	__btrfs_sysfs_remove_one(fs_info);
533e453d989SJeff Mahoney }
534e453d989SJeff Mahoney 
53579da4fa4SJeff Mahoney const char * const btrfs_feature_set_names[3] = {
53679da4fa4SJeff Mahoney 	[FEAT_COMPAT]	 = "compat",
53779da4fa4SJeff Mahoney 	[FEAT_COMPAT_RO] = "compat_ro",
53879da4fa4SJeff Mahoney 	[FEAT_INCOMPAT]	 = "incompat",
53979da4fa4SJeff Mahoney };
54079da4fa4SJeff Mahoney 
5413b02a68aSJeff Mahoney char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
5423b02a68aSJeff Mahoney {
5433b02a68aSJeff Mahoney 	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
5443b02a68aSJeff Mahoney 	int len = 0;
5453b02a68aSJeff Mahoney 	int i;
5463b02a68aSJeff Mahoney 	char *str;
5473b02a68aSJeff Mahoney 
5483b02a68aSJeff Mahoney 	str = kmalloc(bufsize, GFP_KERNEL);
5493b02a68aSJeff Mahoney 	if (!str)
5503b02a68aSJeff Mahoney 		return str;
5513b02a68aSJeff Mahoney 
5523b02a68aSJeff Mahoney 	for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
5533b02a68aSJeff Mahoney 		const char *name;
5543b02a68aSJeff Mahoney 
5553b02a68aSJeff Mahoney 		if (!(flags & (1ULL << i)))
5563b02a68aSJeff Mahoney 			continue;
5573b02a68aSJeff Mahoney 
5583b02a68aSJeff Mahoney 		name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
5593b02a68aSJeff Mahoney 		len += snprintf(str + len, bufsize - len, "%s%s",
5603b02a68aSJeff Mahoney 				len ? "," : "", name);
5613b02a68aSJeff Mahoney 	}
5623b02a68aSJeff Mahoney 
5633b02a68aSJeff Mahoney 	return str;
5643b02a68aSJeff Mahoney }
5653b02a68aSJeff Mahoney 
56679da4fa4SJeff Mahoney static void init_feature_attrs(void)
56779da4fa4SJeff Mahoney {
56879da4fa4SJeff Mahoney 	struct btrfs_feature_attr *fa;
56979da4fa4SJeff Mahoney 	int set, i;
57079da4fa4SJeff Mahoney 
57179da4fa4SJeff Mahoney 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
57279da4fa4SJeff Mahoney 		     ARRAY_SIZE(btrfs_feature_attrs));
57379da4fa4SJeff Mahoney 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
57479da4fa4SJeff Mahoney 		     ARRAY_SIZE(btrfs_feature_attrs[0]));
57579da4fa4SJeff Mahoney 
5763b02a68aSJeff Mahoney 	memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
5773b02a68aSJeff Mahoney 	memset(btrfs_unknown_feature_names, 0,
5783b02a68aSJeff Mahoney 	       sizeof(btrfs_unknown_feature_names));
5793b02a68aSJeff Mahoney 
58079da4fa4SJeff Mahoney 	for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
58179da4fa4SJeff Mahoney 		struct btrfs_feature_attr *sfa;
58279da4fa4SJeff Mahoney 		struct attribute *a = btrfs_supported_feature_attrs[i];
5833b02a68aSJeff Mahoney 		int bit;
58479da4fa4SJeff Mahoney 		sfa = attr_to_btrfs_feature_attr(a);
5853b02a68aSJeff Mahoney 		bit = ilog2(sfa->feature_bit);
5863b02a68aSJeff Mahoney 		fa = &btrfs_feature_attrs[sfa->feature_set][bit];
58779da4fa4SJeff Mahoney 
58879da4fa4SJeff Mahoney 		fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
58979da4fa4SJeff Mahoney 	}
59079da4fa4SJeff Mahoney 
59179da4fa4SJeff Mahoney 	for (set = 0; set < FEAT_MAX; set++) {
59279da4fa4SJeff Mahoney 		for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
59379da4fa4SJeff Mahoney 			char *name = btrfs_unknown_feature_names[set][i];
59479da4fa4SJeff Mahoney 			fa = &btrfs_feature_attrs[set][i];
59579da4fa4SJeff Mahoney 
59679da4fa4SJeff Mahoney 			if (fa->kobj_attr.attr.name)
59779da4fa4SJeff Mahoney 				continue;
59879da4fa4SJeff Mahoney 
59979da4fa4SJeff Mahoney 			snprintf(name, 13, "%s:%u",
60079da4fa4SJeff Mahoney 				 btrfs_feature_set_names[set], i);
60179da4fa4SJeff Mahoney 
60279da4fa4SJeff Mahoney 			fa->kobj_attr.attr.name = name;
60379da4fa4SJeff Mahoney 			fa->kobj_attr.attr.mode = S_IRUGO;
60479da4fa4SJeff Mahoney 			fa->feature_set = set;
60579da4fa4SJeff Mahoney 			fa->feature_bit = 1ULL << i;
60679da4fa4SJeff Mahoney 		}
60779da4fa4SJeff Mahoney 	}
60879da4fa4SJeff Mahoney }
60979da4fa4SJeff Mahoney 
61029e5be24SJeff Mahoney static int add_device_membership(struct btrfs_fs_info *fs_info)
61129e5be24SJeff Mahoney {
61229e5be24SJeff Mahoney 	int error = 0;
61329e5be24SJeff Mahoney 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
61429e5be24SJeff Mahoney 	struct btrfs_device *dev;
61529e5be24SJeff Mahoney 
61629e5be24SJeff Mahoney 	fs_info->device_dir_kobj = kobject_create_and_add("devices",
61729e5be24SJeff Mahoney 						&fs_info->super_kobj);
61829e5be24SJeff Mahoney 	if (!fs_info->device_dir_kobj)
61929e5be24SJeff Mahoney 		return -ENOMEM;
62029e5be24SJeff Mahoney 
62129e5be24SJeff Mahoney 	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
622f085381eSAnand Jain 		struct hd_struct *disk;
623f085381eSAnand Jain 		struct kobject *disk_kobj;
624f085381eSAnand Jain 
625f085381eSAnand Jain 		if (!dev->bdev)
626f085381eSAnand Jain 			continue;
627f085381eSAnand Jain 
628f085381eSAnand Jain 		disk = dev->bdev->bd_part;
629f085381eSAnand Jain 		disk_kobj = &part_to_dev(disk)->kobj;
63029e5be24SJeff Mahoney 
63129e5be24SJeff Mahoney 		error = sysfs_create_link(fs_info->device_dir_kobj,
63229e5be24SJeff Mahoney 					  disk_kobj, disk_kobj->name);
63329e5be24SJeff Mahoney 		if (error)
63429e5be24SJeff Mahoney 			break;
63529e5be24SJeff Mahoney 	}
63629e5be24SJeff Mahoney 
63729e5be24SJeff Mahoney 	return error;
63829e5be24SJeff Mahoney }
63929e5be24SJeff Mahoney 
640510d7360SJeff Mahoney /* /sys/fs/btrfs/ entry */
641510d7360SJeff Mahoney static struct kset *btrfs_kset;
642510d7360SJeff Mahoney 
6431bae3098SDavid Sterba /* /sys/kernel/debug/btrfs */
6441bae3098SDavid Sterba static struct dentry *btrfs_debugfs_root_dentry;
6451bae3098SDavid Sterba 
6461bae3098SDavid Sterba /* Debugging tunables and exported data */
6471bae3098SDavid Sterba u64 btrfs_debugfs_test;
6481bae3098SDavid Sterba 
6495ac1d209SJeff Mahoney int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
6505ac1d209SJeff Mahoney {
6515ac1d209SJeff Mahoney 	int error;
6525ac1d209SJeff Mahoney 
6535ac1d209SJeff Mahoney 	init_completion(&fs_info->kobj_unregister);
654510d7360SJeff Mahoney 	fs_info->super_kobj.kset = btrfs_kset;
6555ac1d209SJeff Mahoney 	error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
6565ac1d209SJeff Mahoney 				     "%pU", fs_info->fsid);
657e453d989SJeff Mahoney 	if (error)
658e453d989SJeff Mahoney 		return error;
659510d7360SJeff Mahoney 
660510d7360SJeff Mahoney 	error = sysfs_create_group(&fs_info->super_kobj,
661510d7360SJeff Mahoney 				   &btrfs_feature_attr_group);
662e453d989SJeff Mahoney 	if (error) {
663e453d989SJeff Mahoney 		__btrfs_sysfs_remove_one(fs_info);
664e453d989SJeff Mahoney 		return error;
665e453d989SJeff Mahoney 	}
66679da4fa4SJeff Mahoney 
667e453d989SJeff Mahoney 	error = addrm_unknown_feature_attrs(fs_info, true);
66879da4fa4SJeff Mahoney 	if (error)
66979da4fa4SJeff Mahoney 		goto failure;
67079da4fa4SJeff Mahoney 
67129e5be24SJeff Mahoney 	error = add_device_membership(fs_info);
67229e5be24SJeff Mahoney 	if (error)
67329e5be24SJeff Mahoney 		goto failure;
67429e5be24SJeff Mahoney 
6756ab0a202SJeff Mahoney 	fs_info->space_info_kobj = kobject_create_and_add("allocation",
6766ab0a202SJeff Mahoney 						  &fs_info->super_kobj);
6776ab0a202SJeff Mahoney 	if (!fs_info->space_info_kobj) {
6786ab0a202SJeff Mahoney 		error = -ENOMEM;
6796ab0a202SJeff Mahoney 		goto failure;
6806ab0a202SJeff Mahoney 	}
6816ab0a202SJeff Mahoney 
6826ab0a202SJeff Mahoney 	error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
6836ab0a202SJeff Mahoney 	if (error)
6846ab0a202SJeff Mahoney 		goto failure;
6856ab0a202SJeff Mahoney 
68679da4fa4SJeff Mahoney 	return 0;
68779da4fa4SJeff Mahoney failure:
688510d7360SJeff Mahoney 	btrfs_sysfs_remove_one(fs_info);
6895ac1d209SJeff Mahoney 	return error;
6905ac1d209SJeff Mahoney }
6915ac1d209SJeff Mahoney 
6921bae3098SDavid Sterba static int btrfs_init_debugfs(void)
6931bae3098SDavid Sterba {
6941bae3098SDavid Sterba #ifdef CONFIG_DEBUG_FS
6951bae3098SDavid Sterba 	btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
6961bae3098SDavid Sterba 	if (!btrfs_debugfs_root_dentry)
6971bae3098SDavid Sterba 		return -ENOMEM;
6981bae3098SDavid Sterba 
6991bae3098SDavid Sterba 	debugfs_create_u64("test", S_IRUGO | S_IWUGO, btrfs_debugfs_root_dentry,
7001bae3098SDavid Sterba 			&btrfs_debugfs_test);
7011bae3098SDavid Sterba #endif
7021bae3098SDavid Sterba 	return 0;
7031bae3098SDavid Sterba }
7041bae3098SDavid Sterba 
705b214107eSChristoph Hellwig int btrfs_init_sysfs(void)
70658176a96SJosef Bacik {
707079b72bcSJeff Mahoney 	int ret;
7081bae3098SDavid Sterba 
709e3fe4e71SGreg KH 	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
710e3fe4e71SGreg KH 	if (!btrfs_kset)
711e3fe4e71SGreg KH 		return -ENOMEM;
712079b72bcSJeff Mahoney 
7131bae3098SDavid Sterba 	ret = btrfs_init_debugfs();
7141bae3098SDavid Sterba 	if (ret)
715079b72bcSJeff Mahoney 		return ret;
716079b72bcSJeff Mahoney 
7171bae3098SDavid Sterba 	init_feature_attrs();
7181bae3098SDavid Sterba 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
7191bae3098SDavid Sterba 
7201bae3098SDavid Sterba 	return ret;
72158176a96SJosef Bacik }
72258176a96SJosef Bacik 
723b214107eSChristoph Hellwig void btrfs_exit_sysfs(void)
72458176a96SJosef Bacik {
725079b72bcSJeff Mahoney 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
726e3fe4e71SGreg KH 	kset_unregister(btrfs_kset);
7271bae3098SDavid Sterba 	debugfs_remove_recursive(btrfs_debugfs_root_dentry);
72858176a96SJosef Bacik }
72955d47414SChris Mason 
730