blob: 7d340f352b7725406ca274386ebbb3e64d17c061 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Josef Bacik58176a92007-08-29 15:47:34 -040019#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/completion.h>
23#include <linux/buffer_head.h>
Josef Bacik58176a92007-08-29 15:47:34 -040024#include <linux/kobject.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040025#include <linux/bug.h>
Josef Bacik58176a92007-08-29 15:47:34 -040026
Chris Masonbae45de2007-04-04 21:22:22 -040027#include "ctree.h"
28#include "disk-io.h"
29#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040030#include "sysfs.h"
31
Jeff Mahoney510d7362013-11-01 13:06:59 -040032static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040033
Jeff Mahoney510d7362013-11-01 13:06:59 -040034static u64 get_features(struct btrfs_fs_info *fs_info,
35 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040036{
Jeff Mahoney510d7362013-11-01 13:06:59 -040037 struct btrfs_super_block *disk_super = fs_info->super_copy;
38 if (set == FEAT_COMPAT)
39 return btrfs_super_compat_flags(disk_super);
40 else if (set == FEAT_COMPAT_RO)
41 return btrfs_super_compat_ro_flags(disk_super);
42 else
43 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040044}
45
Jeff Mahoneyba631942013-11-01 13:07:01 -040046static void set_features(struct btrfs_fs_info *fs_info,
47 enum btrfs_feature_set set, u64 features)
48{
49 struct btrfs_super_block *disk_super = fs_info->super_copy;
50 if (set == FEAT_COMPAT)
51 btrfs_set_super_compat_flags(disk_super, features);
52 else if (set == FEAT_COMPAT_RO)
53 btrfs_set_super_compat_ro_flags(disk_super, features);
54 else
55 btrfs_set_super_incompat_flags(disk_super, features);
56}
57
58static int can_modify_feature(struct btrfs_feature_attr *fa)
59{
60 int val = 0;
61 u64 set, clear;
62 switch (fa->feature_set) {
63 case FEAT_COMPAT:
64 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
65 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
66 break;
67 case FEAT_COMPAT_RO:
68 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
69 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
70 break;
71 case FEAT_INCOMPAT:
72 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
73 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
74 break;
75 default:
76 BUG();
77 }
78
79 if (set & fa->feature_bit)
80 val |= 1;
81 if (clear & fa->feature_bit)
82 val |= 2;
83
84 return val;
85}
86
Jeff Mahoney079b72b2013-11-01 13:06:57 -040087static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
88 struct kobj_attribute *a, char *buf)
89{
Jeff Mahoney510d7362013-11-01 13:06:59 -040090 int val = 0;
91 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -040092 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -040093 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -040094 u64 features = get_features(fs_info, fa->feature_set);
95 if (features & fa->feature_bit)
96 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -040097 } else
98 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -040099
100 return snprintf(buf, PAGE_SIZE, "%d\n", val);
101}
102
Jeff Mahoneyba631942013-11-01 13:07:01 -0400103static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
104 struct kobj_attribute *a,
105 const char *buf, size_t count)
106{
107 struct btrfs_fs_info *fs_info;
108 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
109 struct btrfs_trans_handle *trans;
110 u64 features, set, clear;
111 unsigned long val;
112 int ret;
113
114 fs_info = to_fs_info(kobj);
115 if (!fs_info)
116 return -EPERM;
117
118 ret = kstrtoul(skip_spaces(buf), 0, &val);
119 if (ret)
120 return ret;
121
122 if (fa->feature_set == FEAT_COMPAT) {
123 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
124 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
125 } else if (fa->feature_set == FEAT_COMPAT_RO) {
126 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
127 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
128 } else {
129 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
130 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
131 }
132
133 features = get_features(fs_info, fa->feature_set);
134
135 /* Nothing to do */
136 if ((val && (features & fa->feature_bit)) ||
137 (!val && !(features & fa->feature_bit)))
138 return count;
139
140 if ((val && !(set & fa->feature_bit)) ||
141 (!val && !(clear & fa->feature_bit))) {
142 btrfs_info(fs_info,
143 "%sabling feature %s on mounted fs is not supported.",
144 val ? "En" : "Dis", fa->kobj_attr.attr.name);
145 return -EPERM;
146 }
147
148 btrfs_info(fs_info, "%s %s feature flag",
149 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
150
151 trans = btrfs_start_transaction(fs_info->fs_root, 1);
152 if (IS_ERR(trans))
153 return PTR_ERR(trans);
154
155 spin_lock(&fs_info->super_lock);
156 features = get_features(fs_info, fa->feature_set);
157 if (val)
158 features |= fa->feature_bit;
159 else
160 features &= ~fa->feature_bit;
161 set_features(fs_info, fa->feature_set, features);
162 spin_unlock(&fs_info->super_lock);
163
164 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
165 if (ret)
166 return ret;
167
168 return count;
169}
170
Jeff Mahoney510d7362013-11-01 13:06:59 -0400171static umode_t btrfs_feature_visible(struct kobject *kobj,
172 struct attribute *attr, int unused)
173{
174 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
175 umode_t mode = attr->mode;
176
177 if (fs_info) {
178 struct btrfs_feature_attr *fa;
179 u64 features;
180
181 fa = attr_to_btrfs_feature_attr(attr);
182 features = get_features(fs_info, fa->feature_set);
183
Jeff Mahoneyba631942013-11-01 13:07:01 -0400184 if (can_modify_feature(fa))
185 mode |= S_IWUSR;
186 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400187 mode = 0;
188 }
189
190 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400191}
192
193BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
194BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
195BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
196BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
197BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2);
198BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
199BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
200BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
201BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
202
203static struct attribute *btrfs_supported_feature_attrs[] = {
204 BTRFS_FEAT_ATTR_PTR(mixed_backref),
205 BTRFS_FEAT_ATTR_PTR(default_subvol),
206 BTRFS_FEAT_ATTR_PTR(mixed_groups),
207 BTRFS_FEAT_ATTR_PTR(compress_lzo),
208 BTRFS_FEAT_ATTR_PTR(compress_lzov2),
209 BTRFS_FEAT_ATTR_PTR(big_metadata),
210 BTRFS_FEAT_ATTR_PTR(extended_iref),
211 BTRFS_FEAT_ATTR_PTR(raid56),
212 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
213 NULL
214};
215
216static const struct attribute_group btrfs_feature_attr_group = {
217 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400218 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400219 .attrs = btrfs_supported_feature_attrs,
220};
Josef Bacik58176a92007-08-29 15:47:34 -0400221
Jeff Mahoney510d7362013-11-01 13:06:59 -0400222static void btrfs_release_super_kobj(struct kobject *kobj)
223{
224 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
225 complete(&fs_info->kobj_unregister);
226}
227
228static struct kobj_type btrfs_ktype = {
229 .sysfs_ops = &kobj_sysfs_ops,
230 .release = btrfs_release_super_kobj,
231};
232
233static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
234{
235 if (kobj->ktype != &btrfs_ktype)
236 return NULL;
237 return container_of(kobj, struct btrfs_fs_info, super_kobj);
238}
Josef Bacik58176a92007-08-29 15:47:34 -0400239
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400240void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
241{
242 kobject_del(&fs_info->super_kobj);
243 kobject_put(&fs_info->super_kobj);
244 wait_for_completion(&fs_info->kobj_unregister);
245}
246
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400247const char * const btrfs_feature_set_names[3] = {
248 [FEAT_COMPAT] = "compat",
249 [FEAT_COMPAT_RO] = "compat_ro",
250 [FEAT_INCOMPAT] = "incompat",
251};
252
253#define NUM_FEATURE_BITS 64
254static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
255static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
256
257static void init_feature_attrs(void)
258{
259 struct btrfs_feature_attr *fa;
260 int set, i;
261
262 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
263 ARRAY_SIZE(btrfs_feature_attrs));
264 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
265 ARRAY_SIZE(btrfs_feature_attrs[0]));
266
267 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
268 struct btrfs_feature_attr *sfa;
269 struct attribute *a = btrfs_supported_feature_attrs[i];
270 sfa = attr_to_btrfs_feature_attr(a);
271 fa = &btrfs_feature_attrs[sfa->feature_set][sfa->feature_bit];
272
273 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
274 }
275
276 for (set = 0; set < FEAT_MAX; set++) {
277 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
278 char *name = btrfs_unknown_feature_names[set][i];
279 fa = &btrfs_feature_attrs[set][i];
280
281 if (fa->kobj_attr.attr.name)
282 continue;
283
284 snprintf(name, 13, "%s:%u",
285 btrfs_feature_set_names[set], i);
286
287 fa->kobj_attr.attr.name = name;
288 fa->kobj_attr.attr.mode = S_IRUGO;
289 fa->feature_set = set;
290 fa->feature_bit = 1ULL << i;
291 }
292 }
293}
294
295static u64 supported_feature_masks[3] = {
296 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
297 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
298 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
299};
300
301static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info)
302{
303 int set;
304
305 for (set = 0; set < FEAT_MAX; set++) {
306 int i, count, ret, index = 0;
307 struct attribute **attrs;
308 struct attribute_group agroup = {
309 .name = "features",
310 };
311 u64 features = get_features(fs_info, set);
312 features &= ~supported_feature_masks[set];
313
314 count = hweight64(features);
315
316 if (!count)
317 continue;
318
319 attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL);
320
321 for (i = 0; i < NUM_FEATURE_BITS; i++) {
322 struct btrfs_feature_attr *fa;
323
324 if (!(features & (1ULL << i)))
325 continue;
326
327 fa = &btrfs_feature_attrs[set][i];
328 attrs[index++] = &fa->kobj_attr.attr;
329 }
330
331 attrs[index] = NULL;
332 agroup.attrs = attrs;
333
334 ret = sysfs_merge_group(&fs_info->super_kobj, &agroup);
335 kfree(attrs);
336 if (ret)
337 return ret;
338 }
339 return 0;
340}
341
Jeff Mahoney510d7362013-11-01 13:06:59 -0400342/* /sys/fs/btrfs/ entry */
343static struct kset *btrfs_kset;
344
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400345int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
346{
347 int error;
348
349 init_completion(&fs_info->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400350 fs_info->super_kobj.kset = btrfs_kset;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400351 error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
352 "%pU", fs_info->fsid);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400353
354 error = sysfs_create_group(&fs_info->super_kobj,
355 &btrfs_feature_attr_group);
356 if (error)
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400357 goto failure;
358
359 error = add_unknown_feature_attrs(fs_info);
360 if (error)
361 goto failure;
362
363 return 0;
364failure:
365 btrfs_sysfs_remove_one(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400366 return error;
367}
368
Christoph Hellwigb2141072008-09-05 16:43:31 -0400369int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400370{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400371 int ret;
Greg KHe3fe4e72008-02-20 14:14:16 -0500372 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
373 if (!btrfs_kset)
374 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400375
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400376 init_feature_attrs();
377
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400378 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
379 if (ret) {
380 kset_unregister(btrfs_kset);
381 return ret;
382 }
383
Greg KHe3fe4e72008-02-20 14:14:16 -0500384 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400385}
386
Christoph Hellwigb2141072008-09-05 16:43:31 -0400387void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400388{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400389 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500390 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400391}
Chris Mason55d47412008-02-20 16:02:51 -0500392