2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #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>
29 #include "transaction.h"
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
32 static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
34 return snprintf(buf, PAGE_SIZE, "%llu\n",
35 (unsigned long long)btrfs_root_used(&root->root_item));
38 static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
40 return snprintf(buf, PAGE_SIZE, "%llu\n",
41 (unsigned long long)btrfs_root_limit(&root->root_item));
44 static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
47 return snprintf(buf, PAGE_SIZE, "%llu\n",
48 (unsigned long long)btrfs_super_bytes_used(&fs->super_copy));
51 static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
53 return snprintf(buf, PAGE_SIZE, "%llu\n",
54 (unsigned long long)btrfs_super_total_bytes(&fs->super_copy));
57 static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
59 return snprintf(buf, PAGE_SIZE, "%llu\n",
60 (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
63 /* this is for root attrs (subvols/snapshots) */
64 struct 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);
70 #define ROOT_ATTR(name, mode, show, store) \
71 static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, show, store)
73 ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL);
74 ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL);
76 static struct attribute *btrfs_root_attrs[] = {
77 &btrfs_root_attr_blocks_used.attr,
78 &btrfs_root_attr_block_limit.attr,
82 /* this is for super attrs (actual full fs) */
83 struct 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);
89 #define SUPER_ATTR(name, mode, show, store) \
90 static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, show, store)
92 SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
93 SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
94 SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
96 static 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,
103 static ssize_t btrfs_super_attr_show(struct kobject *kobj,
104 struct attribute *attr, char *buf)
106 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
108 struct btrfs_super_attr *a = container_of(attr,
109 struct btrfs_super_attr,
112 return a->show ? a->show(fs, buf) : 0;
115 static ssize_t btrfs_super_attr_store(struct kobject *kobj,
116 struct attribute *attr,
117 const char *buf, size_t len)
119 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
121 struct btrfs_super_attr *a = container_of(attr,
122 struct btrfs_super_attr,
125 return a->store ? a->store(fs, buf, len) : 0;
128 static ssize_t btrfs_root_attr_show(struct kobject *kobj,
129 struct attribute *attr, char *buf)
131 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
133 struct btrfs_root_attr *a = container_of(attr,
134 struct btrfs_root_attr,
137 return a->show ? a->show(root, buf) : 0;
140 static ssize_t btrfs_root_attr_store(struct kobject *kobj,
141 struct attribute *attr,
142 const char *buf, size_t len)
144 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
146 struct btrfs_root_attr *a = container_of(attr,
147 struct btrfs_root_attr,
149 return a->store ? a->store(root, buf, len) : 0;
152 static void btrfs_super_release(struct kobject *kobj)
154 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
156 complete(&fs->kobj_unregister);
159 static void btrfs_root_release(struct kobject *kobj)
161 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
163 complete(&root->kobj_unregister);
166 static struct sysfs_ops btrfs_super_attr_ops = {
167 .show = btrfs_super_attr_show,
168 .store = btrfs_super_attr_store,
171 static struct sysfs_ops btrfs_root_attr_ops = {
172 .show = btrfs_root_attr_show,
173 .store = btrfs_root_attr_store,
176 static struct kobj_type btrfs_root_ktype = {
177 .default_attrs = btrfs_root_attrs,
178 .sysfs_ops = &btrfs_root_attr_ops,
179 .release = btrfs_root_release,
182 static struct kobj_type btrfs_super_ktype = {
183 .default_attrs = btrfs_super_attrs,
184 .sysfs_ops = &btrfs_super_attr_ops,
185 .release = btrfs_super_release,
188 /* /sys/fs/btrfs/ entry */
189 static struct kset *btrfs_kset;
191 int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
196 int len = strlen(fs->sb->s_id) + 1;
199 name = kmalloc(len, GFP_NOFS);
205 for (i = 0; i < len; i++) {
207 if (c == '/' || c == '\\')
213 fs->super_kobj.kset = btrfs_kset;
214 error = kobject_init_and_add(&fs->super_kobj, &btrfs_super_ktype,
224 printk(KERN_ERR "btrfs: sysfs creation for super failed\n");
228 int btrfs_sysfs_add_root(struct btrfs_root *root)
232 error = kobject_init_and_add(&root->root_kobj, &btrfs_root_ktype,
233 &root->fs_info->super_kobj,
241 printk(KERN_ERR "btrfs: sysfs creation for root failed\n");
245 void btrfs_sysfs_del_root(struct btrfs_root *root)
247 kobject_put(&root->root_kobj);
248 wait_for_completion(&root->kobj_unregister);
251 void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
253 kobject_put(&fs->super_kobj);
254 wait_for_completion(&fs->kobj_unregister);
257 int btrfs_init_sysfs()
259 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
265 void btrfs_exit_sysfs()
267 kset_unregister(btrfs_kset);
272 int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
277 int btrfs_sysfs_add_root(struct btrfs_root *root)
282 void btrfs_sysfs_del_root(struct btrfs_root *root)
287 void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
292 int btrfs_init_sysfs()
297 void btrfs_exit_sysfs()