xref: /openbmc/linux/fs/btrfs/sysfs.c (revision f8ba9c11)
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>
2758176a96SJosef Bacik 
28bae45de0SChris Mason #include "ctree.h"
29bae45de0SChris Mason #include "disk-io.h"
30bae45de0SChris Mason #include "transaction.h"
31079b72bcSJeff Mahoney #include "sysfs.h"
3229e5be24SJeff Mahoney #include "volumes.h"
33079b72bcSJeff Mahoney 
34510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
355ac1d209SJeff Mahoney 
36510d7360SJeff Mahoney static u64 get_features(struct btrfs_fs_info *fs_info,
37510d7360SJeff Mahoney 			enum btrfs_feature_set set)
385ac1d209SJeff Mahoney {
39510d7360SJeff Mahoney 	struct btrfs_super_block *disk_super = fs_info->super_copy;
40510d7360SJeff Mahoney 	if (set == FEAT_COMPAT)
41510d7360SJeff Mahoney 		return btrfs_super_compat_flags(disk_super);
42510d7360SJeff Mahoney 	else if (set == FEAT_COMPAT_RO)
43510d7360SJeff Mahoney 		return btrfs_super_compat_ro_flags(disk_super);
44510d7360SJeff Mahoney 	else
45510d7360SJeff Mahoney 		return btrfs_super_incompat_flags(disk_super);
465ac1d209SJeff Mahoney }
475ac1d209SJeff Mahoney 
48ba631941SJeff Mahoney static void set_features(struct btrfs_fs_info *fs_info,
49ba631941SJeff Mahoney 			 enum btrfs_feature_set set, u64 features)
50ba631941SJeff Mahoney {
51ba631941SJeff Mahoney 	struct btrfs_super_block *disk_super = fs_info->super_copy;
52ba631941SJeff Mahoney 	if (set == FEAT_COMPAT)
53ba631941SJeff Mahoney 		btrfs_set_super_compat_flags(disk_super, features);
54ba631941SJeff Mahoney 	else if (set == FEAT_COMPAT_RO)
55ba631941SJeff Mahoney 		btrfs_set_super_compat_ro_flags(disk_super, features);
56ba631941SJeff Mahoney 	else
57ba631941SJeff Mahoney 		btrfs_set_super_incompat_flags(disk_super, features);
58ba631941SJeff Mahoney }
59ba631941SJeff Mahoney 
60ba631941SJeff Mahoney static int can_modify_feature(struct btrfs_feature_attr *fa)
61ba631941SJeff Mahoney {
62ba631941SJeff Mahoney 	int val = 0;
63ba631941SJeff Mahoney 	u64 set, clear;
64ba631941SJeff Mahoney 	switch (fa->feature_set) {
65ba631941SJeff Mahoney 	case FEAT_COMPAT:
66ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
67ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
68ba631941SJeff Mahoney 		break;
69ba631941SJeff Mahoney 	case FEAT_COMPAT_RO:
70ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
71ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
72ba631941SJeff Mahoney 		break;
73ba631941SJeff Mahoney 	case FEAT_INCOMPAT:
74ba631941SJeff Mahoney 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
75ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
76ba631941SJeff Mahoney 		break;
77ba631941SJeff Mahoney 	default:
78ba631941SJeff Mahoney 		BUG();
79ba631941SJeff Mahoney 	}
80ba631941SJeff Mahoney 
81ba631941SJeff Mahoney 	if (set & fa->feature_bit)
82ba631941SJeff Mahoney 		val |= 1;
83ba631941SJeff Mahoney 	if (clear & fa->feature_bit)
84ba631941SJeff Mahoney 		val |= 2;
85ba631941SJeff Mahoney 
86ba631941SJeff Mahoney 	return val;
87ba631941SJeff Mahoney }
88ba631941SJeff Mahoney 
89079b72bcSJeff Mahoney static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
90079b72bcSJeff Mahoney 				       struct kobj_attribute *a, char *buf)
91079b72bcSJeff Mahoney {
92510d7360SJeff Mahoney 	int val = 0;
93510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
94510d7360SJeff Mahoney 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
95ba631941SJeff Mahoney 	if (fs_info) {
96510d7360SJeff Mahoney 		u64 features = get_features(fs_info, fa->feature_set);
97510d7360SJeff Mahoney 		if (features & fa->feature_bit)
98510d7360SJeff Mahoney 			val = 1;
99ba631941SJeff Mahoney 	} else
100ba631941SJeff Mahoney 		val = can_modify_feature(fa);
101510d7360SJeff Mahoney 
102510d7360SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
103510d7360SJeff Mahoney }
104510d7360SJeff Mahoney 
105ba631941SJeff Mahoney static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
106ba631941SJeff Mahoney 					struct kobj_attribute *a,
107ba631941SJeff Mahoney 					const char *buf, size_t count)
108ba631941SJeff Mahoney {
109ba631941SJeff Mahoney 	struct btrfs_fs_info *fs_info;
110ba631941SJeff Mahoney 	struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
111ba631941SJeff Mahoney 	struct btrfs_trans_handle *trans;
112ba631941SJeff Mahoney 	u64 features, set, clear;
113ba631941SJeff Mahoney 	unsigned long val;
114ba631941SJeff Mahoney 	int ret;
115ba631941SJeff Mahoney 
116ba631941SJeff Mahoney 	fs_info = to_fs_info(kobj);
117ba631941SJeff Mahoney 	if (!fs_info)
118ba631941SJeff Mahoney 		return -EPERM;
119ba631941SJeff Mahoney 
120ba631941SJeff Mahoney 	ret = kstrtoul(skip_spaces(buf), 0, &val);
121ba631941SJeff Mahoney 	if (ret)
122ba631941SJeff Mahoney 		return ret;
123ba631941SJeff Mahoney 
124ba631941SJeff Mahoney 	if (fa->feature_set == FEAT_COMPAT) {
125ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
126ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
127ba631941SJeff Mahoney 	} else if (fa->feature_set == FEAT_COMPAT_RO) {
128ba631941SJeff Mahoney 		set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
129ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
130ba631941SJeff Mahoney 	} else {
131ba631941SJeff Mahoney 		set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
132ba631941SJeff Mahoney 		clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
133ba631941SJeff Mahoney 	}
134ba631941SJeff Mahoney 
135ba631941SJeff Mahoney 	features = get_features(fs_info, fa->feature_set);
136ba631941SJeff Mahoney 
137ba631941SJeff Mahoney 	/* Nothing to do */
138ba631941SJeff Mahoney 	if ((val && (features & fa->feature_bit)) ||
139ba631941SJeff Mahoney 	    (!val && !(features & fa->feature_bit)))
140ba631941SJeff Mahoney 		return count;
141ba631941SJeff Mahoney 
142ba631941SJeff Mahoney 	if ((val && !(set & fa->feature_bit)) ||
143ba631941SJeff Mahoney 	    (!val && !(clear & fa->feature_bit))) {
144ba631941SJeff Mahoney 		btrfs_info(fs_info,
145ba631941SJeff Mahoney 			"%sabling feature %s on mounted fs is not supported.",
146ba631941SJeff Mahoney 			val ? "En" : "Dis", fa->kobj_attr.attr.name);
147ba631941SJeff Mahoney 		return -EPERM;
148ba631941SJeff Mahoney 	}
149ba631941SJeff Mahoney 
150ba631941SJeff Mahoney 	btrfs_info(fs_info, "%s %s feature flag",
151ba631941SJeff Mahoney 		   val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
152ba631941SJeff Mahoney 
153ba631941SJeff Mahoney 	trans = btrfs_start_transaction(fs_info->fs_root, 1);
154ba631941SJeff Mahoney 	if (IS_ERR(trans))
155ba631941SJeff Mahoney 		return PTR_ERR(trans);
156ba631941SJeff Mahoney 
157ba631941SJeff Mahoney 	spin_lock(&fs_info->super_lock);
158ba631941SJeff Mahoney 	features = get_features(fs_info, fa->feature_set);
159ba631941SJeff Mahoney 	if (val)
160ba631941SJeff Mahoney 		features |= fa->feature_bit;
161ba631941SJeff Mahoney 	else
162ba631941SJeff Mahoney 		features &= ~fa->feature_bit;
163ba631941SJeff Mahoney 	set_features(fs_info, fa->feature_set, features);
164ba631941SJeff Mahoney 	spin_unlock(&fs_info->super_lock);
165ba631941SJeff Mahoney 
166ba631941SJeff Mahoney 	ret = btrfs_commit_transaction(trans, fs_info->fs_root);
167ba631941SJeff Mahoney 	if (ret)
168ba631941SJeff Mahoney 		return ret;
169ba631941SJeff Mahoney 
170ba631941SJeff Mahoney 	return count;
171ba631941SJeff Mahoney }
172ba631941SJeff Mahoney 
173510d7360SJeff Mahoney static umode_t btrfs_feature_visible(struct kobject *kobj,
174510d7360SJeff Mahoney 				     struct attribute *attr, int unused)
175510d7360SJeff Mahoney {
176510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
177510d7360SJeff Mahoney 	umode_t mode = attr->mode;
178510d7360SJeff Mahoney 
179510d7360SJeff Mahoney 	if (fs_info) {
180510d7360SJeff Mahoney 		struct btrfs_feature_attr *fa;
181510d7360SJeff Mahoney 		u64 features;
182510d7360SJeff Mahoney 
183510d7360SJeff Mahoney 		fa = attr_to_btrfs_feature_attr(attr);
184510d7360SJeff Mahoney 		features = get_features(fs_info, fa->feature_set);
185510d7360SJeff Mahoney 
186ba631941SJeff Mahoney 		if (can_modify_feature(fa))
187ba631941SJeff Mahoney 			mode |= S_IWUSR;
188ba631941SJeff Mahoney 		else if (!(features & fa->feature_bit))
189510d7360SJeff Mahoney 			mode = 0;
190510d7360SJeff Mahoney 	}
191510d7360SJeff Mahoney 
192510d7360SJeff Mahoney 	return mode;
193079b72bcSJeff Mahoney }
194079b72bcSJeff Mahoney 
195079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
196079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
197079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
198079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
199079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2);
200079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
201079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
202079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
203079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
204079b72bcSJeff Mahoney 
205079b72bcSJeff Mahoney static struct attribute *btrfs_supported_feature_attrs[] = {
206079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(mixed_backref),
207079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(default_subvol),
208079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(mixed_groups),
209079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(compress_lzo),
210079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(compress_lzov2),
211079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(big_metadata),
212079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(extended_iref),
213079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(raid56),
214079b72bcSJeff Mahoney 	BTRFS_FEAT_ATTR_PTR(skinny_metadata),
215079b72bcSJeff Mahoney 	NULL
216079b72bcSJeff Mahoney };
217079b72bcSJeff Mahoney 
218079b72bcSJeff Mahoney static const struct attribute_group btrfs_feature_attr_group = {
219079b72bcSJeff Mahoney 	.name = "features",
220510d7360SJeff Mahoney 	.is_visible = btrfs_feature_visible,
221079b72bcSJeff Mahoney 	.attrs = btrfs_supported_feature_attrs,
222079b72bcSJeff Mahoney };
22358176a96SJosef Bacik 
2246ab0a202SJeff Mahoney static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
2256ab0a202SJeff Mahoney {
2266ab0a202SJeff Mahoney 	u64 val;
2276ab0a202SJeff Mahoney 	if (lock)
2286ab0a202SJeff Mahoney 		spin_lock(lock);
2296ab0a202SJeff Mahoney 	val = *value_ptr;
2306ab0a202SJeff Mahoney 	if (lock)
2316ab0a202SJeff Mahoney 		spin_unlock(lock);
2326ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%llu\n", val);
2336ab0a202SJeff Mahoney }
2346ab0a202SJeff Mahoney 
2356ab0a202SJeff Mahoney static ssize_t global_rsv_size_show(struct kobject *kobj,
2366ab0a202SJeff Mahoney 				    struct kobj_attribute *ka, char *buf)
2376ab0a202SJeff Mahoney {
2386ab0a202SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
2396ab0a202SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2406ab0a202SJeff Mahoney 	return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
2416ab0a202SJeff Mahoney }
2426ab0a202SJeff Mahoney BTRFS_ATTR(global_rsv_size, 0444, global_rsv_size_show);
2436ab0a202SJeff Mahoney 
2446ab0a202SJeff Mahoney static ssize_t global_rsv_reserved_show(struct kobject *kobj,
2456ab0a202SJeff Mahoney 					struct kobj_attribute *a, char *buf)
2466ab0a202SJeff Mahoney {
2476ab0a202SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
2486ab0a202SJeff Mahoney 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2496ab0a202SJeff Mahoney 	return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
2506ab0a202SJeff Mahoney }
2516ab0a202SJeff Mahoney BTRFS_ATTR(global_rsv_reserved, 0444, global_rsv_reserved_show);
2526ab0a202SJeff Mahoney 
2536ab0a202SJeff Mahoney #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
2546ab0a202SJeff Mahoney 
2556ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj,
2566ab0a202SJeff Mahoney 			       struct kobj_attribute *attr, char *buf);
2576ab0a202SJeff Mahoney BTRFS_RAID_ATTR(total_bytes, raid_bytes_show);
2586ab0a202SJeff Mahoney BTRFS_RAID_ATTR(used_bytes, raid_bytes_show);
2596ab0a202SJeff Mahoney 
2606ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj,
2616ab0a202SJeff Mahoney 			       struct kobj_attribute *attr, char *buf)
2626ab0a202SJeff Mahoney 
2636ab0a202SJeff Mahoney {
2646ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
2656ab0a202SJeff Mahoney 	struct btrfs_block_group_cache *block_group;
2666ab0a202SJeff Mahoney 	int index = kobj - sinfo->block_group_kobjs;
2676ab0a202SJeff Mahoney 	u64 val = 0;
2686ab0a202SJeff Mahoney 
2696ab0a202SJeff Mahoney 	down_read(&sinfo->groups_sem);
2706ab0a202SJeff Mahoney 	list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
2716ab0a202SJeff Mahoney 		if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes))
2726ab0a202SJeff Mahoney 			val += block_group->key.offset;
2736ab0a202SJeff Mahoney 		else
2746ab0a202SJeff Mahoney 			val += btrfs_block_group_used(&block_group->item);
2756ab0a202SJeff Mahoney 	}
2766ab0a202SJeff Mahoney 	up_read(&sinfo->groups_sem);
2776ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%llu\n", val);
2786ab0a202SJeff Mahoney }
2796ab0a202SJeff Mahoney 
2806ab0a202SJeff Mahoney static struct attribute *raid_attributes[] = {
2816ab0a202SJeff Mahoney 	BTRFS_RAID_ATTR_PTR(total_bytes),
2826ab0a202SJeff Mahoney 	BTRFS_RAID_ATTR_PTR(used_bytes),
2836ab0a202SJeff Mahoney 	NULL
2846ab0a202SJeff Mahoney };
2856ab0a202SJeff Mahoney 
2866ab0a202SJeff Mahoney static void release_raid_kobj(struct kobject *kobj)
2876ab0a202SJeff Mahoney {
2886ab0a202SJeff Mahoney 	kobject_put(kobj->parent);
2896ab0a202SJeff Mahoney }
2906ab0a202SJeff Mahoney 
2916ab0a202SJeff Mahoney struct kobj_type btrfs_raid_ktype = {
2926ab0a202SJeff Mahoney 	.sysfs_ops = &kobj_sysfs_ops,
2936ab0a202SJeff Mahoney 	.release = release_raid_kobj,
2946ab0a202SJeff Mahoney 	.default_attrs = raid_attributes,
2956ab0a202SJeff Mahoney };
2966ab0a202SJeff Mahoney 
2976ab0a202SJeff Mahoney #define SPACE_INFO_ATTR(field)						\
2986ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,	\
2996ab0a202SJeff Mahoney 					     struct kobj_attribute *a,	\
3006ab0a202SJeff Mahoney 					     char *buf)			\
3016ab0a202SJeff Mahoney {									\
3026ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);		\
3036ab0a202SJeff Mahoney 	return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);	\
3046ab0a202SJeff Mahoney }									\
3056ab0a202SJeff Mahoney BTRFS_ATTR(field, 0444, btrfs_space_info_show_##field)
3066ab0a202SJeff Mahoney 
3076ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
3086ab0a202SJeff Mahoney 						       struct kobj_attribute *a,
3096ab0a202SJeff Mahoney 						       char *buf)
3106ab0a202SJeff Mahoney {
3116ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);
3126ab0a202SJeff Mahoney 	s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
3136ab0a202SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%lld\n", val);
3146ab0a202SJeff Mahoney }
3156ab0a202SJeff Mahoney 
3166ab0a202SJeff Mahoney SPACE_INFO_ATTR(flags);
3176ab0a202SJeff Mahoney SPACE_INFO_ATTR(total_bytes);
3186ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_used);
3196ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_pinned);
3206ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_reserved);
3216ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_may_use);
3226ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_used);
3236ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_total);
3246ab0a202SJeff Mahoney BTRFS_ATTR(total_bytes_pinned, 0444, btrfs_space_info_show_total_bytes_pinned);
3256ab0a202SJeff Mahoney 
3266ab0a202SJeff Mahoney static struct attribute *space_info_attrs[] = {
3276ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(flags),
3286ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(total_bytes),
3296ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_used),
3306ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_pinned),
3316ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_reserved),
3326ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(bytes_may_use),
3336ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(disk_used),
3346ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(disk_total),
3356ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(total_bytes_pinned),
3366ab0a202SJeff Mahoney 	NULL,
3376ab0a202SJeff Mahoney };
3386ab0a202SJeff Mahoney 
3396ab0a202SJeff Mahoney static void space_info_release(struct kobject *kobj)
3406ab0a202SJeff Mahoney {
3416ab0a202SJeff Mahoney 	struct btrfs_space_info *sinfo = to_space_info(kobj);
3426ab0a202SJeff Mahoney 	percpu_counter_destroy(&sinfo->total_bytes_pinned);
3436ab0a202SJeff Mahoney 	kfree(sinfo);
3446ab0a202SJeff Mahoney }
3456ab0a202SJeff Mahoney 
3466ab0a202SJeff Mahoney struct kobj_type space_info_ktype = {
3476ab0a202SJeff Mahoney 	.sysfs_ops = &kobj_sysfs_ops,
3486ab0a202SJeff Mahoney 	.release = space_info_release,
3496ab0a202SJeff Mahoney 	.default_attrs = space_info_attrs,
3506ab0a202SJeff Mahoney };
3516ab0a202SJeff Mahoney 
3526ab0a202SJeff Mahoney static const struct attribute *allocation_attrs[] = {
3536ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(global_rsv_reserved),
3546ab0a202SJeff Mahoney 	BTRFS_ATTR_PTR(global_rsv_size),
3556ab0a202SJeff Mahoney 	NULL,
3566ab0a202SJeff Mahoney };
3576ab0a202SJeff Mahoney 
358f8ba9c11SJeff Mahoney static ssize_t btrfs_label_show(struct kobject *kobj,
359f8ba9c11SJeff Mahoney 				struct kobj_attribute *a, char *buf)
360f8ba9c11SJeff Mahoney {
361f8ba9c11SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
362f8ba9c11SJeff Mahoney 	return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label);
363f8ba9c11SJeff Mahoney }
364f8ba9c11SJeff Mahoney 
365f8ba9c11SJeff Mahoney static ssize_t btrfs_label_store(struct kobject *kobj,
366f8ba9c11SJeff Mahoney 				 struct kobj_attribute *a,
367f8ba9c11SJeff Mahoney 				 const char *buf, size_t len)
368f8ba9c11SJeff Mahoney {
369f8ba9c11SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
370f8ba9c11SJeff Mahoney 	struct btrfs_trans_handle *trans;
371f8ba9c11SJeff Mahoney 	struct btrfs_root *root = fs_info->fs_root;
372f8ba9c11SJeff Mahoney 	int ret;
373f8ba9c11SJeff Mahoney 
374f8ba9c11SJeff Mahoney 	if (len >= BTRFS_LABEL_SIZE) {
375f8ba9c11SJeff Mahoney 		pr_err("btrfs: unable to set label with more than %d bytes\n",
376f8ba9c11SJeff Mahoney 		       BTRFS_LABEL_SIZE - 1);
377f8ba9c11SJeff Mahoney 		return -EINVAL;
378f8ba9c11SJeff Mahoney 	}
379f8ba9c11SJeff Mahoney 
380f8ba9c11SJeff Mahoney 	trans = btrfs_start_transaction(root, 0);
381f8ba9c11SJeff Mahoney 	if (IS_ERR(trans))
382f8ba9c11SJeff Mahoney 		return PTR_ERR(trans);
383f8ba9c11SJeff Mahoney 
384f8ba9c11SJeff Mahoney 	spin_lock(&root->fs_info->super_lock);
385f8ba9c11SJeff Mahoney 	strcpy(fs_info->super_copy->label, buf);
386f8ba9c11SJeff Mahoney 	spin_unlock(&root->fs_info->super_lock);
387f8ba9c11SJeff Mahoney 	ret = btrfs_commit_transaction(trans, root);
388f8ba9c11SJeff Mahoney 
389f8ba9c11SJeff Mahoney 	if (!ret)
390f8ba9c11SJeff Mahoney 		return len;
391f8ba9c11SJeff Mahoney 
392f8ba9c11SJeff Mahoney 	return ret;
393f8ba9c11SJeff Mahoney }
394f8ba9c11SJeff Mahoney BTRFS_ATTR_RW(label, 0644, btrfs_label_show, btrfs_label_store);
395f8ba9c11SJeff Mahoney 
396f8ba9c11SJeff Mahoney static struct attribute *btrfs_attrs[] = {
397f8ba9c11SJeff Mahoney 	BTRFS_ATTR_PTR(label),
398f8ba9c11SJeff Mahoney 	NULL,
399f8ba9c11SJeff Mahoney };
400f8ba9c11SJeff Mahoney 
401510d7360SJeff Mahoney static void btrfs_release_super_kobj(struct kobject *kobj)
402510d7360SJeff Mahoney {
403510d7360SJeff Mahoney 	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
404510d7360SJeff Mahoney 	complete(&fs_info->kobj_unregister);
405510d7360SJeff Mahoney }
406510d7360SJeff Mahoney 
407510d7360SJeff Mahoney static struct kobj_type btrfs_ktype = {
408510d7360SJeff Mahoney 	.sysfs_ops	= &kobj_sysfs_ops,
409510d7360SJeff Mahoney 	.release	= btrfs_release_super_kobj,
410f8ba9c11SJeff Mahoney 	.default_attrs	= btrfs_attrs,
411510d7360SJeff Mahoney };
412510d7360SJeff Mahoney 
413510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
414510d7360SJeff Mahoney {
415510d7360SJeff Mahoney 	if (kobj->ktype != &btrfs_ktype)
416510d7360SJeff Mahoney 		return NULL;
417510d7360SJeff Mahoney 	return container_of(kobj, struct btrfs_fs_info, super_kobj);
418510d7360SJeff Mahoney }
41958176a96SJosef Bacik 
4205ac1d209SJeff Mahoney void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
4215ac1d209SJeff Mahoney {
4226ab0a202SJeff Mahoney 	sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
42329e5be24SJeff Mahoney 	kobject_del(fs_info->device_dir_kobj);
42429e5be24SJeff Mahoney 	kobject_put(fs_info->device_dir_kobj);
4256ab0a202SJeff Mahoney 	kobject_del(fs_info->space_info_kobj);
4266ab0a202SJeff Mahoney 	kobject_put(fs_info->space_info_kobj);
4275ac1d209SJeff Mahoney 	kobject_del(&fs_info->super_kobj);
4285ac1d209SJeff Mahoney 	kobject_put(&fs_info->super_kobj);
4295ac1d209SJeff Mahoney 	wait_for_completion(&fs_info->kobj_unregister);
4305ac1d209SJeff Mahoney }
4315ac1d209SJeff Mahoney 
43279da4fa4SJeff Mahoney const char * const btrfs_feature_set_names[3] = {
43379da4fa4SJeff Mahoney 	[FEAT_COMPAT]	 = "compat",
43479da4fa4SJeff Mahoney 	[FEAT_COMPAT_RO] = "compat_ro",
43579da4fa4SJeff Mahoney 	[FEAT_INCOMPAT]	 = "incompat",
43679da4fa4SJeff Mahoney };
43779da4fa4SJeff Mahoney 
43879da4fa4SJeff Mahoney #define NUM_FEATURE_BITS 64
43979da4fa4SJeff Mahoney static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
44079da4fa4SJeff Mahoney static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
44179da4fa4SJeff Mahoney 
4423b02a68aSJeff Mahoney char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
4433b02a68aSJeff Mahoney {
4443b02a68aSJeff Mahoney 	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
4453b02a68aSJeff Mahoney 	int len = 0;
4463b02a68aSJeff Mahoney 	int i;
4473b02a68aSJeff Mahoney 	char *str;
4483b02a68aSJeff Mahoney 
4493b02a68aSJeff Mahoney 	str = kmalloc(bufsize, GFP_KERNEL);
4503b02a68aSJeff Mahoney 	if (!str)
4513b02a68aSJeff Mahoney 		return str;
4523b02a68aSJeff Mahoney 
4533b02a68aSJeff Mahoney 	for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
4543b02a68aSJeff Mahoney 		const char *name;
4553b02a68aSJeff Mahoney 
4563b02a68aSJeff Mahoney 		if (!(flags & (1ULL << i)))
4573b02a68aSJeff Mahoney 			continue;
4583b02a68aSJeff Mahoney 
4593b02a68aSJeff Mahoney 		name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
4603b02a68aSJeff Mahoney 		len += snprintf(str + len, bufsize - len, "%s%s",
4613b02a68aSJeff Mahoney 				len ? "," : "", name);
4623b02a68aSJeff Mahoney 	}
4633b02a68aSJeff Mahoney 
4643b02a68aSJeff Mahoney 	return str;
4653b02a68aSJeff Mahoney }
4663b02a68aSJeff Mahoney 
46779da4fa4SJeff Mahoney static void init_feature_attrs(void)
46879da4fa4SJeff Mahoney {
46979da4fa4SJeff Mahoney 	struct btrfs_feature_attr *fa;
47079da4fa4SJeff Mahoney 	int set, i;
47179da4fa4SJeff Mahoney 
47279da4fa4SJeff Mahoney 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
47379da4fa4SJeff Mahoney 		     ARRAY_SIZE(btrfs_feature_attrs));
47479da4fa4SJeff Mahoney 	BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
47579da4fa4SJeff Mahoney 		     ARRAY_SIZE(btrfs_feature_attrs[0]));
47679da4fa4SJeff Mahoney 
4773b02a68aSJeff Mahoney 	memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
4783b02a68aSJeff Mahoney 	memset(btrfs_unknown_feature_names, 0,
4793b02a68aSJeff Mahoney 	       sizeof(btrfs_unknown_feature_names));
4803b02a68aSJeff Mahoney 
48179da4fa4SJeff Mahoney 	for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
48279da4fa4SJeff Mahoney 		struct btrfs_feature_attr *sfa;
48379da4fa4SJeff Mahoney 		struct attribute *a = btrfs_supported_feature_attrs[i];
4843b02a68aSJeff Mahoney 		int bit;
48579da4fa4SJeff Mahoney 		sfa = attr_to_btrfs_feature_attr(a);
4863b02a68aSJeff Mahoney 		bit = ilog2(sfa->feature_bit);
4873b02a68aSJeff Mahoney 		fa = &btrfs_feature_attrs[sfa->feature_set][bit];
48879da4fa4SJeff Mahoney 
48979da4fa4SJeff Mahoney 		fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
49079da4fa4SJeff Mahoney 	}
49179da4fa4SJeff Mahoney 
49279da4fa4SJeff Mahoney 	for (set = 0; set < FEAT_MAX; set++) {
49379da4fa4SJeff Mahoney 		for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
49479da4fa4SJeff Mahoney 			char *name = btrfs_unknown_feature_names[set][i];
49579da4fa4SJeff Mahoney 			fa = &btrfs_feature_attrs[set][i];
49679da4fa4SJeff Mahoney 
49779da4fa4SJeff Mahoney 			if (fa->kobj_attr.attr.name)
49879da4fa4SJeff Mahoney 				continue;
49979da4fa4SJeff Mahoney 
50079da4fa4SJeff Mahoney 			snprintf(name, 13, "%s:%u",
50179da4fa4SJeff Mahoney 				 btrfs_feature_set_names[set], i);
50279da4fa4SJeff Mahoney 
50379da4fa4SJeff Mahoney 			fa->kobj_attr.attr.name = name;
50479da4fa4SJeff Mahoney 			fa->kobj_attr.attr.mode = S_IRUGO;
50579da4fa4SJeff Mahoney 			fa->feature_set = set;
50679da4fa4SJeff Mahoney 			fa->feature_bit = 1ULL << i;
50779da4fa4SJeff Mahoney 		}
50879da4fa4SJeff Mahoney 	}
50979da4fa4SJeff Mahoney }
51079da4fa4SJeff Mahoney 
51179da4fa4SJeff Mahoney static u64 supported_feature_masks[3] = {
51279da4fa4SJeff Mahoney 	[FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
51379da4fa4SJeff Mahoney 	[FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
51479da4fa4SJeff Mahoney 	[FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
51579da4fa4SJeff Mahoney };
51679da4fa4SJeff Mahoney 
51779da4fa4SJeff Mahoney static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info)
51879da4fa4SJeff Mahoney {
51979da4fa4SJeff Mahoney 	int set;
52079da4fa4SJeff Mahoney 
52179da4fa4SJeff Mahoney 	for (set = 0; set < FEAT_MAX; set++) {
52279da4fa4SJeff Mahoney 		int i, count, ret, index = 0;
52379da4fa4SJeff Mahoney 		struct attribute **attrs;
52479da4fa4SJeff Mahoney 		struct attribute_group agroup = {
52579da4fa4SJeff Mahoney 			.name = "features",
52679da4fa4SJeff Mahoney 		};
52779da4fa4SJeff Mahoney 		u64 features = get_features(fs_info, set);
52879da4fa4SJeff Mahoney 		features &= ~supported_feature_masks[set];
52979da4fa4SJeff Mahoney 
53079da4fa4SJeff Mahoney 		count = hweight64(features);
53179da4fa4SJeff Mahoney 
53279da4fa4SJeff Mahoney 		if (!count)
53379da4fa4SJeff Mahoney 			continue;
53479da4fa4SJeff Mahoney 
53579da4fa4SJeff Mahoney 		attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL);
53679da4fa4SJeff Mahoney 
53779da4fa4SJeff Mahoney 		for (i = 0; i < NUM_FEATURE_BITS; i++) {
53879da4fa4SJeff Mahoney 			struct btrfs_feature_attr *fa;
53979da4fa4SJeff Mahoney 
54079da4fa4SJeff Mahoney 			if (!(features & (1ULL << i)))
54179da4fa4SJeff Mahoney 				continue;
54279da4fa4SJeff Mahoney 
54379da4fa4SJeff Mahoney 			fa = &btrfs_feature_attrs[set][i];
54479da4fa4SJeff Mahoney 			attrs[index++] = &fa->kobj_attr.attr;
54579da4fa4SJeff Mahoney 		}
54679da4fa4SJeff Mahoney 
54779da4fa4SJeff Mahoney 		attrs[index] = NULL;
54879da4fa4SJeff Mahoney 		agroup.attrs = attrs;
54979da4fa4SJeff Mahoney 
55079da4fa4SJeff Mahoney 		ret = sysfs_merge_group(&fs_info->super_kobj, &agroup);
55179da4fa4SJeff Mahoney 		kfree(attrs);
55279da4fa4SJeff Mahoney 		if (ret)
55379da4fa4SJeff Mahoney 			return ret;
55479da4fa4SJeff Mahoney 	}
55579da4fa4SJeff Mahoney 	return 0;
55679da4fa4SJeff Mahoney }
55779da4fa4SJeff Mahoney 
55829e5be24SJeff Mahoney static int add_device_membership(struct btrfs_fs_info *fs_info)
55929e5be24SJeff Mahoney {
56029e5be24SJeff Mahoney 	int error = 0;
56129e5be24SJeff Mahoney 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
56229e5be24SJeff Mahoney 	struct btrfs_device *dev;
56329e5be24SJeff Mahoney 
56429e5be24SJeff Mahoney 	fs_info->device_dir_kobj = kobject_create_and_add("devices",
56529e5be24SJeff Mahoney 						&fs_info->super_kobj);
56629e5be24SJeff Mahoney 	if (!fs_info->device_dir_kobj)
56729e5be24SJeff Mahoney 		return -ENOMEM;
56829e5be24SJeff Mahoney 
56929e5be24SJeff Mahoney 	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
57029e5be24SJeff Mahoney 		struct hd_struct *disk = dev->bdev->bd_part;
57129e5be24SJeff Mahoney 		struct kobject *disk_kobj = &part_to_dev(disk)->kobj;
57229e5be24SJeff Mahoney 
57329e5be24SJeff Mahoney 		error = sysfs_create_link(fs_info->device_dir_kobj,
57429e5be24SJeff Mahoney 					  disk_kobj, disk_kobj->name);
57529e5be24SJeff Mahoney 		if (error)
57629e5be24SJeff Mahoney 			break;
57729e5be24SJeff Mahoney 	}
57829e5be24SJeff Mahoney 
57929e5be24SJeff Mahoney 	return error;
58029e5be24SJeff Mahoney }
58129e5be24SJeff Mahoney 
582510d7360SJeff Mahoney /* /sys/fs/btrfs/ entry */
583510d7360SJeff Mahoney static struct kset *btrfs_kset;
584510d7360SJeff Mahoney 
5855ac1d209SJeff Mahoney int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
5865ac1d209SJeff Mahoney {
5875ac1d209SJeff Mahoney 	int error;
5885ac1d209SJeff Mahoney 
5895ac1d209SJeff Mahoney 	init_completion(&fs_info->kobj_unregister);
590510d7360SJeff Mahoney 	fs_info->super_kobj.kset = btrfs_kset;
5915ac1d209SJeff Mahoney 	error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
5925ac1d209SJeff Mahoney 				     "%pU", fs_info->fsid);
593510d7360SJeff Mahoney 
594510d7360SJeff Mahoney 	error = sysfs_create_group(&fs_info->super_kobj,
595510d7360SJeff Mahoney 				   &btrfs_feature_attr_group);
596510d7360SJeff Mahoney 	if (error)
59779da4fa4SJeff Mahoney 		goto failure;
59879da4fa4SJeff Mahoney 
59979da4fa4SJeff Mahoney 	error = add_unknown_feature_attrs(fs_info);
60079da4fa4SJeff Mahoney 	if (error)
60179da4fa4SJeff Mahoney 		goto failure;
60279da4fa4SJeff Mahoney 
60329e5be24SJeff Mahoney 	error = add_device_membership(fs_info);
60429e5be24SJeff Mahoney 	if (error)
60529e5be24SJeff Mahoney 		goto failure;
60629e5be24SJeff Mahoney 
6076ab0a202SJeff Mahoney 	fs_info->space_info_kobj = kobject_create_and_add("allocation",
6086ab0a202SJeff Mahoney 						  &fs_info->super_kobj);
6096ab0a202SJeff Mahoney 	if (!fs_info->space_info_kobj) {
6106ab0a202SJeff Mahoney 		error = -ENOMEM;
6116ab0a202SJeff Mahoney 		goto failure;
6126ab0a202SJeff Mahoney 	}
6136ab0a202SJeff Mahoney 
6146ab0a202SJeff Mahoney 	error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
6156ab0a202SJeff Mahoney 	if (error)
6166ab0a202SJeff Mahoney 		goto failure;
6176ab0a202SJeff Mahoney 
61879da4fa4SJeff Mahoney 	return 0;
61979da4fa4SJeff Mahoney failure:
620510d7360SJeff Mahoney 	btrfs_sysfs_remove_one(fs_info);
6215ac1d209SJeff Mahoney 	return error;
6225ac1d209SJeff Mahoney }
6235ac1d209SJeff Mahoney 
624b214107eSChristoph Hellwig int btrfs_init_sysfs(void)
62558176a96SJosef Bacik {
626079b72bcSJeff Mahoney 	int ret;
627e3fe4e71SGreg KH 	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
628e3fe4e71SGreg KH 	if (!btrfs_kset)
629e3fe4e71SGreg KH 		return -ENOMEM;
630079b72bcSJeff Mahoney 
63179da4fa4SJeff Mahoney 	init_feature_attrs();
63279da4fa4SJeff Mahoney 
633079b72bcSJeff Mahoney 	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
634079b72bcSJeff Mahoney 	if (ret) {
635079b72bcSJeff Mahoney 		kset_unregister(btrfs_kset);
636079b72bcSJeff Mahoney 		return ret;
637079b72bcSJeff Mahoney 	}
638079b72bcSJeff Mahoney 
639e3fe4e71SGreg KH 	return 0;
64058176a96SJosef Bacik }
64158176a96SJosef Bacik 
642b214107eSChristoph Hellwig void btrfs_exit_sysfs(void)
64358176a96SJosef Bacik {
644079b72bcSJeff Mahoney 	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
645e3fe4e71SGreg KH 	kset_unregister(btrfs_kset);
64658176a96SJosef Bacik }
64755d47414SChris Mason 
648