blob: ec631539111130d52a9604ea81d4ee669205dbbd [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>
Jeff Mahoney29e5be22013-11-01 13:07:05 -040026#include <linux/genhd.h>
Josef Bacik58176a92007-08-29 15:47:34 -040027
Chris Masonbae45de2007-04-04 21:22:22 -040028#include "ctree.h"
29#include "disk-io.h"
30#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040031#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040032#include "volumes.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040033
Jeff Mahoney510d7362013-11-01 13:06:59 -040034static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040035
Jeff Mahoney510d7362013-11-01 13:06:59 -040036static u64 get_features(struct btrfs_fs_info *fs_info,
37 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040038{
Jeff Mahoney510d7362013-11-01 13:06:59 -040039 struct btrfs_super_block *disk_super = fs_info->super_copy;
40 if (set == FEAT_COMPAT)
41 return btrfs_super_compat_flags(disk_super);
42 else if (set == FEAT_COMPAT_RO)
43 return btrfs_super_compat_ro_flags(disk_super);
44 else
45 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040046}
47
Jeff Mahoneyba631942013-11-01 13:07:01 -040048static void set_features(struct btrfs_fs_info *fs_info,
49 enum btrfs_feature_set set, u64 features)
50{
51 struct btrfs_super_block *disk_super = fs_info->super_copy;
52 if (set == FEAT_COMPAT)
53 btrfs_set_super_compat_flags(disk_super, features);
54 else if (set == FEAT_COMPAT_RO)
55 btrfs_set_super_compat_ro_flags(disk_super, features);
56 else
57 btrfs_set_super_incompat_flags(disk_super, features);
58}
59
60static int can_modify_feature(struct btrfs_feature_attr *fa)
61{
62 int val = 0;
63 u64 set, clear;
64 switch (fa->feature_set) {
65 case FEAT_COMPAT:
66 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
67 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
68 break;
69 case FEAT_COMPAT_RO:
70 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
71 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
72 break;
73 case FEAT_INCOMPAT:
74 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
75 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
76 break;
77 default:
78 BUG();
79 }
80
81 if (set & fa->feature_bit)
82 val |= 1;
83 if (clear & fa->feature_bit)
84 val |= 2;
85
86 return val;
87}
88
Jeff Mahoney079b72b2013-11-01 13:06:57 -040089static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
90 struct kobj_attribute *a, char *buf)
91{
Jeff Mahoney510d7362013-11-01 13:06:59 -040092 int val = 0;
93 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -040094 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -040095 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -040096 u64 features = get_features(fs_info, fa->feature_set);
97 if (features & fa->feature_bit)
98 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -040099 } else
100 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400101
102 return snprintf(buf, PAGE_SIZE, "%d\n", val);
103}
104
Jeff Mahoneyba631942013-11-01 13:07:01 -0400105static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
106 struct kobj_attribute *a,
107 const char *buf, size_t count)
108{
109 struct btrfs_fs_info *fs_info;
110 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
111 struct btrfs_trans_handle *trans;
112 u64 features, set, clear;
113 unsigned long val;
114 int ret;
115
116 fs_info = to_fs_info(kobj);
117 if (!fs_info)
118 return -EPERM;
119
120 ret = kstrtoul(skip_spaces(buf), 0, &val);
121 if (ret)
122 return ret;
123
124 if (fa->feature_set == FEAT_COMPAT) {
125 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
126 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
127 } else if (fa->feature_set == FEAT_COMPAT_RO) {
128 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
129 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
130 } else {
131 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
132 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
133 }
134
135 features = get_features(fs_info, fa->feature_set);
136
137 /* Nothing to do */
138 if ((val && (features & fa->feature_bit)) ||
139 (!val && !(features & fa->feature_bit)))
140 return count;
141
142 if ((val && !(set & fa->feature_bit)) ||
143 (!val && !(clear & fa->feature_bit))) {
144 btrfs_info(fs_info,
145 "%sabling feature %s on mounted fs is not supported.",
146 val ? "En" : "Dis", fa->kobj_attr.attr.name);
147 return -EPERM;
148 }
149
150 btrfs_info(fs_info, "%s %s feature flag",
151 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
152
153 trans = btrfs_start_transaction(fs_info->fs_root, 1);
154 if (IS_ERR(trans))
155 return PTR_ERR(trans);
156
157 spin_lock(&fs_info->super_lock);
158 features = get_features(fs_info, fa->feature_set);
159 if (val)
160 features |= fa->feature_bit;
161 else
162 features &= ~fa->feature_bit;
163 set_features(fs_info, fa->feature_set, features);
164 spin_unlock(&fs_info->super_lock);
165
166 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
167 if (ret)
168 return ret;
169
170 return count;
171}
172
Jeff Mahoney510d7362013-11-01 13:06:59 -0400173static umode_t btrfs_feature_visible(struct kobject *kobj,
174 struct attribute *attr, int unused)
175{
176 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
177 umode_t mode = attr->mode;
178
179 if (fs_info) {
180 struct btrfs_feature_attr *fa;
181 u64 features;
182
183 fa = attr_to_btrfs_feature_attr(attr);
184 features = get_features(fs_info, fa->feature_set);
185
Jeff Mahoneyba631942013-11-01 13:07:01 -0400186 if (can_modify_feature(fa))
187 mode |= S_IWUSR;
188 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400189 mode = 0;
190 }
191
192 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400193}
194
195BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
196BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
197BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
198BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
199BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2);
200BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
201BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
202BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
203BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
204
205static struct attribute *btrfs_supported_feature_attrs[] = {
206 BTRFS_FEAT_ATTR_PTR(mixed_backref),
207 BTRFS_FEAT_ATTR_PTR(default_subvol),
208 BTRFS_FEAT_ATTR_PTR(mixed_groups),
209 BTRFS_FEAT_ATTR_PTR(compress_lzo),
210 BTRFS_FEAT_ATTR_PTR(compress_lzov2),
211 BTRFS_FEAT_ATTR_PTR(big_metadata),
212 BTRFS_FEAT_ATTR_PTR(extended_iref),
213 BTRFS_FEAT_ATTR_PTR(raid56),
214 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
215 NULL
216};
217
218static const struct attribute_group btrfs_feature_attr_group = {
219 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400220 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400221 .attrs = btrfs_supported_feature_attrs,
222};
Josef Bacik58176a92007-08-29 15:47:34 -0400223
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400224static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
225{
226 u64 val;
227 if (lock)
228 spin_lock(lock);
229 val = *value_ptr;
230 if (lock)
231 spin_unlock(lock);
232 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
233}
234
235static ssize_t global_rsv_size_show(struct kobject *kobj,
236 struct kobj_attribute *ka, char *buf)
237{
238 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
239 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
240 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
241}
242BTRFS_ATTR(global_rsv_size, 0444, global_rsv_size_show);
243
244static ssize_t global_rsv_reserved_show(struct kobject *kobj,
245 struct kobj_attribute *a, char *buf)
246{
247 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
248 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
249 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
250}
251BTRFS_ATTR(global_rsv_reserved, 0444, global_rsv_reserved_show);
252
253#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
254
255static ssize_t raid_bytes_show(struct kobject *kobj,
256 struct kobj_attribute *attr, char *buf);
257BTRFS_RAID_ATTR(total_bytes, raid_bytes_show);
258BTRFS_RAID_ATTR(used_bytes, raid_bytes_show);
259
260static ssize_t raid_bytes_show(struct kobject *kobj,
261 struct kobj_attribute *attr, char *buf)
262
263{
264 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
265 struct btrfs_block_group_cache *block_group;
266 int index = kobj - sinfo->block_group_kobjs;
267 u64 val = 0;
268
269 down_read(&sinfo->groups_sem);
270 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
271 if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes))
272 val += block_group->key.offset;
273 else
274 val += btrfs_block_group_used(&block_group->item);
275 }
276 up_read(&sinfo->groups_sem);
277 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
278}
279
280static struct attribute *raid_attributes[] = {
281 BTRFS_RAID_ATTR_PTR(total_bytes),
282 BTRFS_RAID_ATTR_PTR(used_bytes),
283 NULL
284};
285
286static void release_raid_kobj(struct kobject *kobj)
287{
288 kobject_put(kobj->parent);
289}
290
291struct kobj_type btrfs_raid_ktype = {
292 .sysfs_ops = &kobj_sysfs_ops,
293 .release = release_raid_kobj,
294 .default_attrs = raid_attributes,
295};
296
297#define SPACE_INFO_ATTR(field) \
298static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
299 struct kobj_attribute *a, \
300 char *buf) \
301{ \
302 struct btrfs_space_info *sinfo = to_space_info(kobj); \
303 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
304} \
305BTRFS_ATTR(field, 0444, btrfs_space_info_show_##field)
306
307static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
308 struct kobj_attribute *a,
309 char *buf)
310{
311 struct btrfs_space_info *sinfo = to_space_info(kobj);
312 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
313 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
314}
315
316SPACE_INFO_ATTR(flags);
317SPACE_INFO_ATTR(total_bytes);
318SPACE_INFO_ATTR(bytes_used);
319SPACE_INFO_ATTR(bytes_pinned);
320SPACE_INFO_ATTR(bytes_reserved);
321SPACE_INFO_ATTR(bytes_may_use);
322SPACE_INFO_ATTR(disk_used);
323SPACE_INFO_ATTR(disk_total);
324BTRFS_ATTR(total_bytes_pinned, 0444, btrfs_space_info_show_total_bytes_pinned);
325
326static struct attribute *space_info_attrs[] = {
327 BTRFS_ATTR_PTR(flags),
328 BTRFS_ATTR_PTR(total_bytes),
329 BTRFS_ATTR_PTR(bytes_used),
330 BTRFS_ATTR_PTR(bytes_pinned),
331 BTRFS_ATTR_PTR(bytes_reserved),
332 BTRFS_ATTR_PTR(bytes_may_use),
333 BTRFS_ATTR_PTR(disk_used),
334 BTRFS_ATTR_PTR(disk_total),
335 BTRFS_ATTR_PTR(total_bytes_pinned),
336 NULL,
337};
338
339static void space_info_release(struct kobject *kobj)
340{
341 struct btrfs_space_info *sinfo = to_space_info(kobj);
342 percpu_counter_destroy(&sinfo->total_bytes_pinned);
343 kfree(sinfo);
344}
345
346struct kobj_type space_info_ktype = {
347 .sysfs_ops = &kobj_sysfs_ops,
348 .release = space_info_release,
349 .default_attrs = space_info_attrs,
350};
351
352static const struct attribute *allocation_attrs[] = {
353 BTRFS_ATTR_PTR(global_rsv_reserved),
354 BTRFS_ATTR_PTR(global_rsv_size),
355 NULL,
356};
357
Jeff Mahoney510d7362013-11-01 13:06:59 -0400358static void btrfs_release_super_kobj(struct kobject *kobj)
359{
360 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
361 complete(&fs_info->kobj_unregister);
362}
363
364static struct kobj_type btrfs_ktype = {
365 .sysfs_ops = &kobj_sysfs_ops,
366 .release = btrfs_release_super_kobj,
367};
368
369static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
370{
371 if (kobj->ktype != &btrfs_ktype)
372 return NULL;
373 return container_of(kobj, struct btrfs_fs_info, super_kobj);
374}
Josef Bacik58176a92007-08-29 15:47:34 -0400375
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400376void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
377{
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400378 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400379 kobject_del(fs_info->device_dir_kobj);
380 kobject_put(fs_info->device_dir_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400381 kobject_del(fs_info->space_info_kobj);
382 kobject_put(fs_info->space_info_kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400383 kobject_del(&fs_info->super_kobj);
384 kobject_put(&fs_info->super_kobj);
385 wait_for_completion(&fs_info->kobj_unregister);
386}
387
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400388const char * const btrfs_feature_set_names[3] = {
389 [FEAT_COMPAT] = "compat",
390 [FEAT_COMPAT_RO] = "compat_ro",
391 [FEAT_INCOMPAT] = "incompat",
392};
393
394#define NUM_FEATURE_BITS 64
395static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
396static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
397
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400398char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
399{
400 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
401 int len = 0;
402 int i;
403 char *str;
404
405 str = kmalloc(bufsize, GFP_KERNEL);
406 if (!str)
407 return str;
408
409 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
410 const char *name;
411
412 if (!(flags & (1ULL << i)))
413 continue;
414
415 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
416 len += snprintf(str + len, bufsize - len, "%s%s",
417 len ? "," : "", name);
418 }
419
420 return str;
421}
422
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400423static void init_feature_attrs(void)
424{
425 struct btrfs_feature_attr *fa;
426 int set, i;
427
428 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
429 ARRAY_SIZE(btrfs_feature_attrs));
430 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
431 ARRAY_SIZE(btrfs_feature_attrs[0]));
432
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400433 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
434 memset(btrfs_unknown_feature_names, 0,
435 sizeof(btrfs_unknown_feature_names));
436
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400437 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
438 struct btrfs_feature_attr *sfa;
439 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400440 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400441 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400442 bit = ilog2(sfa->feature_bit);
443 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400444
445 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
446 }
447
448 for (set = 0; set < FEAT_MAX; set++) {
449 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
450 char *name = btrfs_unknown_feature_names[set][i];
451 fa = &btrfs_feature_attrs[set][i];
452
453 if (fa->kobj_attr.attr.name)
454 continue;
455
456 snprintf(name, 13, "%s:%u",
457 btrfs_feature_set_names[set], i);
458
459 fa->kobj_attr.attr.name = name;
460 fa->kobj_attr.attr.mode = S_IRUGO;
461 fa->feature_set = set;
462 fa->feature_bit = 1ULL << i;
463 }
464 }
465}
466
467static u64 supported_feature_masks[3] = {
468 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
469 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
470 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
471};
472
473static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info)
474{
475 int set;
476
477 for (set = 0; set < FEAT_MAX; set++) {
478 int i, count, ret, index = 0;
479 struct attribute **attrs;
480 struct attribute_group agroup = {
481 .name = "features",
482 };
483 u64 features = get_features(fs_info, set);
484 features &= ~supported_feature_masks[set];
485
486 count = hweight64(features);
487
488 if (!count)
489 continue;
490
491 attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL);
492
493 for (i = 0; i < NUM_FEATURE_BITS; i++) {
494 struct btrfs_feature_attr *fa;
495
496 if (!(features & (1ULL << i)))
497 continue;
498
499 fa = &btrfs_feature_attrs[set][i];
500 attrs[index++] = &fa->kobj_attr.attr;
501 }
502
503 attrs[index] = NULL;
504 agroup.attrs = attrs;
505
506 ret = sysfs_merge_group(&fs_info->super_kobj, &agroup);
507 kfree(attrs);
508 if (ret)
509 return ret;
510 }
511 return 0;
512}
513
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400514static int add_device_membership(struct btrfs_fs_info *fs_info)
515{
516 int error = 0;
517 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
518 struct btrfs_device *dev;
519
520 fs_info->device_dir_kobj = kobject_create_and_add("devices",
521 &fs_info->super_kobj);
522 if (!fs_info->device_dir_kobj)
523 return -ENOMEM;
524
525 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
526 struct hd_struct *disk = dev->bdev->bd_part;
527 struct kobject *disk_kobj = &part_to_dev(disk)->kobj;
528
529 error = sysfs_create_link(fs_info->device_dir_kobj,
530 disk_kobj, disk_kobj->name);
531 if (error)
532 break;
533 }
534
535 return error;
536}
537
Jeff Mahoney510d7362013-11-01 13:06:59 -0400538/* /sys/fs/btrfs/ entry */
539static struct kset *btrfs_kset;
540
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400541int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
542{
543 int error;
544
545 init_completion(&fs_info->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400546 fs_info->super_kobj.kset = btrfs_kset;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400547 error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
548 "%pU", fs_info->fsid);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400549
550 error = sysfs_create_group(&fs_info->super_kobj,
551 &btrfs_feature_attr_group);
552 if (error)
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400553 goto failure;
554
555 error = add_unknown_feature_attrs(fs_info);
556 if (error)
557 goto failure;
558
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400559 error = add_device_membership(fs_info);
560 if (error)
561 goto failure;
562
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400563 fs_info->space_info_kobj = kobject_create_and_add("allocation",
564 &fs_info->super_kobj);
565 if (!fs_info->space_info_kobj) {
566 error = -ENOMEM;
567 goto failure;
568 }
569
570 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
571 if (error)
572 goto failure;
573
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400574 return 0;
575failure:
576 btrfs_sysfs_remove_one(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400577 return error;
578}
579
Christoph Hellwigb2141072008-09-05 16:43:31 -0400580int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400581{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400582 int ret;
Greg KHe3fe4e72008-02-20 14:14:16 -0500583 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
584 if (!btrfs_kset)
585 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400586
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400587 init_feature_attrs();
588
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400589 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
590 if (ret) {
591 kset_unregister(btrfs_kset);
592 return ret;
593 }
594
Greg KHe3fe4e72008-02-20 14:14:16 -0500595 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400596}
597
Christoph Hellwigb2141072008-09-05 16:43:31 -0400598void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400599{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400600 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500601 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400602}
Chris Mason55d47412008-02-20 16:02:51 -0500603