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 static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
33 return snprintf(buf, PAGE_SIZE, "%llu\n",
34 (unsigned long long)btrfs_root_blocks_used(&root->root_item));
37 static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
39 return snprintf(buf, PAGE_SIZE, "%llu\n",
40 (unsigned long long)btrfs_root_block_limit(&root->root_item));
43 static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
45 return snprintf(buf, PAGE_SIZE, "%llu\n",
46 (unsigned long long)btrfs_super_blocks_used(fs->disk_super));
49 static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
51 return snprintf(buf, PAGE_SIZE, "%llu\n",
52 (unsigned long long)btrfs_super_total_blocks(fs->disk_super));
55 static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
57 return snprintf(buf, PAGE_SIZE, "%llu\n",
58 (unsigned long long)btrfs_super_blocksize(fs->disk_super));
61 /* this is for root attrs (subvols/snapshots) */
62 struct btrfs_root_attr {
63 struct attribute attr;
64 ssize_t (*show)(struct btrfs_root *, char *);
65 ssize_t (*store)(struct btrfs_root *, const char *, size_t);
68 #define ROOT_ATTR(name, mode, show, store) \
69 static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, show, store)
71 ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL);
72 ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL);
74 static struct attribute *btrfs_root_attrs[] = {
75 &btrfs_root_attr_blocks_used.attr,
76 &btrfs_root_attr_block_limit.attr,
80 /* this is for super attrs (actual full fs) */
81 struct btrfs_super_attr {
82 struct attribute attr;
83 ssize_t (*show)(struct btrfs_fs_info *, char *);
84 ssize_t (*store)(struct btrfs_fs_info *, const char *, size_t);
87 #define SUPER_ATTR(name, mode, show, store) \
88 static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, show, store)
90 SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
91 SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
92 SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
94 static struct attribute *btrfs_super_attrs[] = {
95 &btrfs_super_attr_blocks_used.attr,
96 &btrfs_super_attr_total_blocks.attr,
97 &btrfs_super_attr_blocksize.attr,
101 static ssize_t btrfs_super_attr_show(struct kobject *kobj,
102 struct attribute *attr, char *buf)
104 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
106 struct btrfs_super_attr *a = container_of(attr,
107 struct btrfs_super_attr,
110 return a->show ? a->show(fs, buf) : 0;
113 static ssize_t btrfs_super_attr_store(struct kobject *kobj,
114 struct attribute *attr,
115 const char *buf, size_t len)
117 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
119 struct btrfs_super_attr *a = container_of(attr,
120 struct btrfs_super_attr,
123 return a->store ? a->store(fs, buf, len) : 0;
126 static ssize_t btrfs_root_attr_show(struct kobject *kobj,
127 struct attribute *attr, char *buf)
129 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
131 struct btrfs_root_attr *a = container_of(attr,
132 struct btrfs_root_attr,
135 return a->show ? a->show(root, buf) : 0;
138 static ssize_t btrfs_root_attr_store(struct kobject *kobj,
139 struct attribute *attr,
140 const char *buf, size_t len)
142 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
144 struct btrfs_root_attr *a = container_of(attr,
145 struct btrfs_root_attr,
147 return a->store ? a->store(root, buf, len) : 0;
150 static void btrfs_super_release(struct kobject *kobj)
152 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
154 complete(&fs->kobj_unregister);
157 static void btrfs_root_release(struct kobject *kobj)
159 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
161 complete(&root->kobj_unregister);
164 static struct sysfs_ops btrfs_super_attr_ops = {
165 .show = btrfs_super_attr_show,
166 .store = btrfs_super_attr_store,
169 static struct sysfs_ops btrfs_root_attr_ops = {
170 .show = btrfs_root_attr_show,
171 .store = btrfs_root_attr_store,
174 static struct kobj_type btrfs_root_ktype = {
175 .default_attrs = btrfs_root_attrs,
176 .sysfs_ops = &btrfs_root_attr_ops,
177 .release = btrfs_root_release,
180 static struct kobj_type btrfs_super_ktype = {
181 .default_attrs = btrfs_super_attrs,
182 .sysfs_ops = &btrfs_super_attr_ops,
183 .release = btrfs_super_release,
186 static struct kset btrfs_kset = {
187 .kobj = {.name = "btrfs"},
190 int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
194 fs->super_kobj.kset = &btrfs_kset;
195 fs->super_kobj.ktype = &btrfs_super_ktype;
197 error = kobject_set_name(&fs->super_kobj, "%s",
202 error = kobject_register(&fs->super_kobj);
209 printk(KERN_ERR "btrfs: sysfs creation for super failed\n");
213 int btrfs_sysfs_add_root(struct btrfs_root *root)
217 root->root_kobj.ktype = &btrfs_root_ktype;
218 root->root_kobj.parent = &root->fs_info->super_kobj;
220 error = kobject_set_name(&root->root_kobj, "%s", root->name);
225 error = kobject_register(&root->root_kobj);
232 printk(KERN_ERR "btrfs: sysfs creation for root failed\n");
236 void btrfs_sysfs_del_root(struct btrfs_root *root)
238 kobject_unregister(&root->root_kobj);
239 wait_for_completion(&root->kobj_unregister);
242 void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
244 kobject_unregister(&fs->super_kobj);
245 wait_for_completion(&fs->kobj_unregister);
248 int btrfs_init_sysfs()
250 kobj_set_kset_s(&btrfs_kset, fs_subsys);
251 return kset_register(&btrfs_kset);
254 void btrfs_exit_sysfs()
256 kset_unregister(&btrfs_kset);