blob: 562e346994d93242abcfb69b16af4184eaf97c89 [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
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400257char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
258{
259 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
260 int len = 0;
261 int i;
262 char *str;
263
264 str = kmalloc(bufsize, GFP_KERNEL);
265 if (!str)
266 return str;
267
268 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
269 const char *name;
270
271 if (!(flags & (1ULL << i)))
272 continue;
273
274 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
275 len += snprintf(str + len, bufsize - len, "%s%s",
276 len ? "," : "", name);
277 }
278
279 return str;
280}
281
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400282static void init_feature_attrs(void)
283{
284 struct btrfs_feature_attr *fa;
285 int set, i;
286
287 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
288 ARRAY_SIZE(btrfs_feature_attrs));
289 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
290 ARRAY_SIZE(btrfs_feature_attrs[0]));
291
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400292 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
293 memset(btrfs_unknown_feature_names, 0,
294 sizeof(btrfs_unknown_feature_names));
295
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400296 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
297 struct btrfs_feature_attr *sfa;
298 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400299 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400300 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400301 bit = ilog2(sfa->feature_bit);
302 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400303
304 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
305 }
306
307 for (set = 0; set < FEAT_MAX; set++) {
308 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
309 char *name = btrfs_unknown_feature_names[set][i];
310 fa = &btrfs_feature_attrs[set][i];
311
312 if (fa->kobj_attr.attr.name)
313 continue;
314
315 snprintf(name, 13, "%s:%u",
316 btrfs_feature_set_names[set], i);
317
318 fa->kobj_attr.attr.name = name;
319 fa->kobj_attr.attr.mode = S_IRUGO;
320 fa->feature_set = set;
321 fa->feature_bit = 1ULL << i;
322 }
323 }
324}
325
326static u64 supported_feature_masks[3] = {
327 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
328 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
329 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
330};
331
332static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info)
333{
334 int set;
335
336 for (set = 0; set < FEAT_MAX; set++) {
337 int i, count, ret, index = 0;
338 struct attribute **attrs;
339 struct attribute_group agroup = {
340 .name = "features",
341 };
342 u64 features = get_features(fs_info, set);
343 features &= ~supported_feature_masks[set];
344
345 count = hweight64(features);
346
347 if (!count)
348 continue;
349
350 attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL);
351
352 for (i = 0; i < NUM_FEATURE_BITS; i++) {
353 struct btrfs_feature_attr *fa;
354
355 if (!(features & (1ULL << i)))
356 continue;
357
358 fa = &btrfs_feature_attrs[set][i];
359 attrs[index++] = &fa->kobj_attr.attr;
360 }
361
362 attrs[index] = NULL;
363 agroup.attrs = attrs;
364
365 ret = sysfs_merge_group(&fs_info->super_kobj, &agroup);
366 kfree(attrs);
367 if (ret)
368 return ret;
369 }
370 return 0;
371}
372
Jeff Mahoney510d7362013-11-01 13:06:59 -0400373/* /sys/fs/btrfs/ entry */
374static struct kset *btrfs_kset;
375
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400376int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
377{
378 int error;
379
380 init_completion(&fs_info->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400381 fs_info->super_kobj.kset = btrfs_kset;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400382 error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
383 "%pU", fs_info->fsid);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400384
385 error = sysfs_create_group(&fs_info->super_kobj,
386 &btrfs_feature_attr_group);
387 if (error)
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400388 goto failure;
389
390 error = add_unknown_feature_attrs(fs_info);
391 if (error)
392 goto failure;
393
394 return 0;
395failure:
396 btrfs_sysfs_remove_one(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400397 return error;
398}
399
Christoph Hellwigb2141072008-09-05 16:43:31 -0400400int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400401{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400402 int ret;
Greg KHe3fe4e72008-02-20 14:14:16 -0500403 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
404 if (!btrfs_kset)
405 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400406
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400407 init_feature_attrs();
408
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400409 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
410 if (ret) {
411 kset_unregister(btrfs_kset);
412 return ret;
413 }
414
Greg KHe3fe4e72008-02-20 14:14:16 -0500415 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400416}
417
Christoph Hellwigb2141072008-09-05 16:43:31 -0400418void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400419{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400420 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500421 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400422}
Chris Mason55d47412008-02-20 16:02:51 -0500423