blob: 61af5d8446e397df031d9961cc54210ac2b16558 [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>
24#include <linux/module.h>
25#include <linux/kobject.h>
26
Chris Masonbae45de2007-04-04 21:22:22 -040027#include "ctree.h"
28#include "disk-io.h"
29#include "transaction.h"
Josef Bacik58176a92007-08-29 15:47:34 -040030
Chris Mason55d47412008-02-20 16:02:51 -050031#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
Josef Bacik58176a92007-08-29 15:47:34 -040032static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
33{
34 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040035 (unsigned long long)btrfs_root_used(&root->root_item));
Josef Bacik58176a92007-08-29 15:47:34 -040036}
37
38static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
39{
40 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040041 (unsigned long long)btrfs_root_limit(&root->root_item));
Josef Bacik58176a92007-08-29 15:47:34 -040042}
43
44static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
45{
Chris Masondb945352007-10-15 16:15:53 -040046
Josef Bacik58176a92007-08-29 15:47:34 -040047 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Masondb945352007-10-15 16:15:53 -040048 (unsigned long long)btrfs_super_bytes_used(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040049}
50
51static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
52{
53 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Masondb945352007-10-15 16:15:53 -040054 (unsigned long long)btrfs_super_total_bytes(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040055}
56
57static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
58{
59 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040060 (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040061}
62
63/* this is for root attrs (subvols/snapshots) */
64struct btrfs_root_attr {
65 struct attribute attr;
66 ssize_t (*show)(struct btrfs_root *, char *);
67 ssize_t (*store)(struct btrfs_root *, const char *, size_t);
68};
69
70#define ROOT_ATTR(name, mode, show, store) \
71static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, show, store)
72
73ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL);
74ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL);
75
76static struct attribute *btrfs_root_attrs[] = {
77 &btrfs_root_attr_blocks_used.attr,
78 &btrfs_root_attr_block_limit.attr,
79 NULL,
80};
81
82/* this is for super attrs (actual full fs) */
83struct btrfs_super_attr {
84 struct attribute attr;
85 ssize_t (*show)(struct btrfs_fs_info *, char *);
86 ssize_t (*store)(struct btrfs_fs_info *, const char *, size_t);
87};
88
89#define SUPER_ATTR(name, mode, show, store) \
90static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, show, store)
91
92SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
93SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
94SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
95
96static struct attribute *btrfs_super_attrs[] = {
97 &btrfs_super_attr_blocks_used.attr,
98 &btrfs_super_attr_total_blocks.attr,
99 &btrfs_super_attr_blocksize.attr,
100 NULL,
101};
102
103static ssize_t btrfs_super_attr_show(struct kobject *kobj,
104 struct attribute *attr, char *buf)
105{
106 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
107 super_kobj);
108 struct btrfs_super_attr *a = container_of(attr,
109 struct btrfs_super_attr,
110 attr);
111
112 return a->show ? a->show(fs, buf) : 0;
113}
114
115static ssize_t btrfs_super_attr_store(struct kobject *kobj,
116 struct attribute *attr,
117 const char *buf, size_t len)
118{
119 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
120 super_kobj);
121 struct btrfs_super_attr *a = container_of(attr,
122 struct btrfs_super_attr,
123 attr);
124
125 return a->store ? a->store(fs, buf, len) : 0;
126}
127
128static ssize_t btrfs_root_attr_show(struct kobject *kobj,
129 struct attribute *attr, char *buf)
130{
131 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
132 root_kobj);
133 struct btrfs_root_attr *a = container_of(attr,
134 struct btrfs_root_attr,
135 attr);
136
137 return a->show ? a->show(root, buf) : 0;
138}
139
140static ssize_t btrfs_root_attr_store(struct kobject *kobj,
141 struct attribute *attr,
142 const char *buf, size_t len)
143{
144 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
145 root_kobj);
146 struct btrfs_root_attr *a = container_of(attr,
147 struct btrfs_root_attr,
148 attr);
149 return a->store ? a->store(root, buf, len) : 0;
150}
151
152static void btrfs_super_release(struct kobject *kobj)
153{
154 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
155 super_kobj);
156 complete(&fs->kobj_unregister);
157}
158
159static void btrfs_root_release(struct kobject *kobj)
160{
161 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
162 root_kobj);
163 complete(&root->kobj_unregister);
164}
165
166static struct sysfs_ops btrfs_super_attr_ops = {
167 .show = btrfs_super_attr_show,
168 .store = btrfs_super_attr_store,
169};
170
171static struct sysfs_ops btrfs_root_attr_ops = {
172 .show = btrfs_root_attr_show,
173 .store = btrfs_root_attr_store,
174};
175
176static struct kobj_type btrfs_root_ktype = {
177 .default_attrs = btrfs_root_attrs,
178 .sysfs_ops = &btrfs_root_attr_ops,
179 .release = btrfs_root_release,
180};
181
182static struct kobj_type btrfs_super_ktype = {
183 .default_attrs = btrfs_super_attrs,
184 .sysfs_ops = &btrfs_super_attr_ops,
185 .release = btrfs_super_release,
186};
187
Greg KHe3fe4e72008-02-20 14:14:16 -0500188/* /sys/fs/btrfs/ entry */
189static struct kset *btrfs_kset;
Josef Bacik58176a92007-08-29 15:47:34 -0400190
191int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
192{
193 int error;
Chris Mason6da6aba2007-12-18 16:15:09 -0500194 char *name;
195 char c;
196 int len = strlen(fs->sb->s_id) + 1;
197 int i;
198
199 name = kmalloc(len, GFP_NOFS);
200 if (!name) {
201 error = -ENOMEM;
202 goto fail;
203 }
204
205 for (i = 0; i < len; i++) {
206 c = fs->sb->s_id[i];
207 if (c == '/' || c == '\\')
208 c = '!';
209 name[i] = c;
210 }
211 name[len] = '\0';
Josef Bacik58176a92007-08-29 15:47:34 -0400212
Greg KHe3fe4e72008-02-20 14:14:16 -0500213 fs->super_kobj.kset = btrfs_kset;
214 error = kobject_init_and_add(&fs->super_kobj, &btrfs_super_ktype,
215 NULL, "%s", name);
Josef Bacik58176a92007-08-29 15:47:34 -0400216 if (error)
217 goto fail;
218
Chris Mason6da6aba2007-12-18 16:15:09 -0500219 kfree(name);
Josef Bacik58176a92007-08-29 15:47:34 -0400220 return 0;
221
222fail:
Chris Mason6da6aba2007-12-18 16:15:09 -0500223 kfree(name);
Josef Bacik58176a92007-08-29 15:47:34 -0400224 printk(KERN_ERR "btrfs: sysfs creation for super failed\n");
225 return error;
226}
227
228int btrfs_sysfs_add_root(struct btrfs_root *root)
229{
230 int error;
231
Greg KHe3fe4e72008-02-20 14:14:16 -0500232 error = kobject_init_and_add(&root->root_kobj, &btrfs_root_ktype,
233 &root->fs_info->super_kobj,
234 "%s", root->name);
Josef Bacik58176a92007-08-29 15:47:34 -0400235 if (error)
236 goto fail;
237
238 return 0;
239
240fail:
241 printk(KERN_ERR "btrfs: sysfs creation for root failed\n");
242 return error;
243}
244
245void btrfs_sysfs_del_root(struct btrfs_root *root)
246{
Greg KHe3fe4e72008-02-20 14:14:16 -0500247 kobject_put(&root->root_kobj);
Josef Bacik58176a92007-08-29 15:47:34 -0400248 wait_for_completion(&root->kobj_unregister);
249}
250
251void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
252{
Greg KHe3fe4e72008-02-20 14:14:16 -0500253 kobject_put(&fs->super_kobj);
Josef Bacik58176a92007-08-29 15:47:34 -0400254 wait_for_completion(&fs->kobj_unregister);
255}
256
Christoph Hellwigb2141072008-09-05 16:43:31 -0400257int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400258{
Greg KHe3fe4e72008-02-20 14:14:16 -0500259 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
260 if (!btrfs_kset)
261 return -ENOMEM;
262 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400263}
264
Christoph Hellwigb2141072008-09-05 16:43:31 -0400265void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400266{
Greg KHe3fe4e72008-02-20 14:14:16 -0500267 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400268}
Chris Mason55d47412008-02-20 16:02:51 -0500269
270#else
271
272int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
273{
274 return 0;
275}
276
277int btrfs_sysfs_add_root(struct btrfs_root *root)
278{
279 return 0;
280}
281
282void btrfs_sysfs_del_root(struct btrfs_root *root)
283{
284 return;
285}
286
287void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
288{
289 return;
290}
291
Christoph Hellwigb2141072008-09-05 16:43:31 -0400292int btrfs_init_sysfs(void)
Chris Mason55d47412008-02-20 16:02:51 -0500293{
294 return 0;
295}
296
Christoph Hellwigb2141072008-09-05 16:43:31 -0400297void btrfs_exit_sysfs(void)
Chris Mason55d47412008-02-20 16:02:51 -0500298{
299 return;
300}
301#endif