blob: 832cf62151ffbf43998e98efe0457f6181a8e432 [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>
25
Chris Masonbae45de2007-04-04 21:22:22 -040026#include "ctree.h"
27#include "disk-io.h"
28#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040029#include "sysfs.h"
30
Jeff Mahoney510d7362013-11-01 13:06:59 -040031static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040032
Jeff Mahoney510d7362013-11-01 13:06:59 -040033static u64 get_features(struct btrfs_fs_info *fs_info,
34 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040035{
Jeff Mahoney510d7362013-11-01 13:06:59 -040036 struct btrfs_super_block *disk_super = fs_info->super_copy;
37 if (set == FEAT_COMPAT)
38 return btrfs_super_compat_flags(disk_super);
39 else if (set == FEAT_COMPAT_RO)
40 return btrfs_super_compat_ro_flags(disk_super);
41 else
42 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040043}
44
Jeff Mahoney079b72b2013-11-01 13:06:57 -040045static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
46 struct kobj_attribute *a, char *buf)
47{
Jeff Mahoney510d7362013-11-01 13:06:59 -040048 int val = 0;
49 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
50 if (fs_info) {
51 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
52 u64 features = get_features(fs_info, fa->feature_set);
53 if (features & fa->feature_bit)
54 val = 1;
55 }
56
57 return snprintf(buf, PAGE_SIZE, "%d\n", val);
58}
59
60static umode_t btrfs_feature_visible(struct kobject *kobj,
61 struct attribute *attr, int unused)
62{
63 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
64 umode_t mode = attr->mode;
65
66 if (fs_info) {
67 struct btrfs_feature_attr *fa;
68 u64 features;
69
70 fa = attr_to_btrfs_feature_attr(attr);
71 features = get_features(fs_info, fa->feature_set);
72
73 if (!(features & fa->feature_bit))
74 mode = 0;
75 }
76
77 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -040078}
79
80BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
81BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
82BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
83BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
84BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2);
85BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
86BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
87BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
88BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
89
90static struct attribute *btrfs_supported_feature_attrs[] = {
91 BTRFS_FEAT_ATTR_PTR(mixed_backref),
92 BTRFS_FEAT_ATTR_PTR(default_subvol),
93 BTRFS_FEAT_ATTR_PTR(mixed_groups),
94 BTRFS_FEAT_ATTR_PTR(compress_lzo),
95 BTRFS_FEAT_ATTR_PTR(compress_lzov2),
96 BTRFS_FEAT_ATTR_PTR(big_metadata),
97 BTRFS_FEAT_ATTR_PTR(extended_iref),
98 BTRFS_FEAT_ATTR_PTR(raid56),
99 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
100 NULL
101};
102
103static const struct attribute_group btrfs_feature_attr_group = {
104 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400105 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400106 .attrs = btrfs_supported_feature_attrs,
107};
Josef Bacik58176a92007-08-29 15:47:34 -0400108
Jeff Mahoney510d7362013-11-01 13:06:59 -0400109static void btrfs_release_super_kobj(struct kobject *kobj)
110{
111 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
112 complete(&fs_info->kobj_unregister);
113}
114
115static struct kobj_type btrfs_ktype = {
116 .sysfs_ops = &kobj_sysfs_ops,
117 .release = btrfs_release_super_kobj,
118};
119
120static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
121{
122 if (kobj->ktype != &btrfs_ktype)
123 return NULL;
124 return container_of(kobj, struct btrfs_fs_info, super_kobj);
125}
Josef Bacik58176a92007-08-29 15:47:34 -0400126
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400127void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
128{
129 kobject_del(&fs_info->super_kobj);
130 kobject_put(&fs_info->super_kobj);
131 wait_for_completion(&fs_info->kobj_unregister);
132}
133
Jeff Mahoney510d7362013-11-01 13:06:59 -0400134/* /sys/fs/btrfs/ entry */
135static struct kset *btrfs_kset;
136
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400137int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
138{
139 int error;
140
141 init_completion(&fs_info->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400142 fs_info->super_kobj.kset = btrfs_kset;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400143 error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
144 "%pU", fs_info->fsid);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400145
146 error = sysfs_create_group(&fs_info->super_kobj,
147 &btrfs_feature_attr_group);
148 if (error)
149 btrfs_sysfs_remove_one(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400150 return error;
151}
152
Christoph Hellwigb2141072008-09-05 16:43:31 -0400153int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400154{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400155 int ret;
Greg KHe3fe4e72008-02-20 14:14:16 -0500156 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
157 if (!btrfs_kset)
158 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400159
160 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
161 if (ret) {
162 kset_unregister(btrfs_kset);
163 return ret;
164 }
165
Greg KHe3fe4e72008-02-20 14:14:16 -0500166 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400167}
168
Christoph Hellwigb2141072008-09-05 16:43:31 -0400169void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400170{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400171 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500172 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400173}
Chris Mason55d47412008-02-20 16:02:51 -0500174