blob: c3c223ae66918d9e244fc909c4d6b7836490ef95 [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
31static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
32{
33 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040034 (unsigned long long)btrfs_root_used(&root->root_item));
Josef Bacik58176a92007-08-29 15:47:34 -040035}
36
37static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
38{
39 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040040 (unsigned long long)btrfs_root_limit(&root->root_item));
Josef Bacik58176a92007-08-29 15:47:34 -040041}
42
43static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
44{
Chris Masondb945352007-10-15 16:15:53 -040045
Josef Bacik58176a92007-08-29 15:47:34 -040046 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Masondb945352007-10-15 16:15:53 -040047 (unsigned long long)btrfs_super_bytes_used(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040048}
49
50static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
51{
52 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Masondb945352007-10-15 16:15:53 -040053 (unsigned long long)btrfs_super_total_bytes(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040054}
55
56static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
57{
58 return snprintf(buf, PAGE_SIZE, "%llu\n",
Chris Mason5f39d392007-10-15 16:14:19 -040059 (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
Josef Bacik58176a92007-08-29 15:47:34 -040060}
61
62/* this is for root attrs (subvols/snapshots) */
63struct btrfs_root_attr {
64 struct attribute attr;
65 ssize_t (*show)(struct btrfs_root *, char *);
66 ssize_t (*store)(struct btrfs_root *, const char *, size_t);
67};
68
69#define ROOT_ATTR(name, mode, show, store) \
Chris Masond3977122009-01-05 21:25:51 -050070static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, \
71 show, store)
Josef Bacik58176a92007-08-29 15:47:34 -040072
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) \
Chris Masond3977122009-01-05 21:25:51 -050090static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, \
91 show, store)
Josef Bacik58176a92007-08-29 15:47:34 -040092
93SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
94SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
95SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
96
97static struct attribute *btrfs_super_attrs[] = {
98 &btrfs_super_attr_blocks_used.attr,
99 &btrfs_super_attr_total_blocks.attr,
100 &btrfs_super_attr_blocksize.attr,
101 NULL,
102};
103
104static ssize_t btrfs_super_attr_show(struct kobject *kobj,
105 struct attribute *attr, char *buf)
106{
107 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
108 super_kobj);
109 struct btrfs_super_attr *a = container_of(attr,
110 struct btrfs_super_attr,
111 attr);
112
113 return a->show ? a->show(fs, buf) : 0;
114}
115
116static ssize_t btrfs_super_attr_store(struct kobject *kobj,
117 struct attribute *attr,
118 const char *buf, size_t len)
119{
120 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
121 super_kobj);
122 struct btrfs_super_attr *a = container_of(attr,
123 struct btrfs_super_attr,
124 attr);
125
126 return a->store ? a->store(fs, buf, len) : 0;
127}
128
129static ssize_t btrfs_root_attr_show(struct kobject *kobj,
130 struct attribute *attr, char *buf)
131{
132 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
133 root_kobj);
134 struct btrfs_root_attr *a = container_of(attr,
135 struct btrfs_root_attr,
136 attr);
137
138 return a->show ? a->show(root, buf) : 0;
139}
140
141static ssize_t btrfs_root_attr_store(struct kobject *kobj,
142 struct attribute *attr,
143 const char *buf, size_t len)
144{
145 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
146 root_kobj);
147 struct btrfs_root_attr *a = container_of(attr,
148 struct btrfs_root_attr,
149 attr);
150 return a->store ? a->store(root, buf, len) : 0;
151}
152
153static void btrfs_super_release(struct kobject *kobj)
154{
155 struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
156 super_kobj);
157 complete(&fs->kobj_unregister);
158}
159
160static void btrfs_root_release(struct kobject *kobj)
161{
162 struct btrfs_root *root = container_of(kobj, struct btrfs_root,
163 root_kobj);
164 complete(&root->kobj_unregister);
165}
166
Emese Revfy52cf25d2010-01-19 02:58:23 +0100167static const struct sysfs_ops btrfs_super_attr_ops = {
Josef Bacik58176a92007-08-29 15:47:34 -0400168 .show = btrfs_super_attr_show,
169 .store = btrfs_super_attr_store,
170};
171
Emese Revfy52cf25d2010-01-19 02:58:23 +0100172static const struct sysfs_ops btrfs_root_attr_ops = {
Josef Bacik58176a92007-08-29 15:47:34 -0400173 .show = btrfs_root_attr_show,
174 .store = btrfs_root_attr_store,
175};
176
Greg KHe3fe4e72008-02-20 14:14:16 -0500177/* /sys/fs/btrfs/ entry */
178static struct kset *btrfs_kset;
Josef Bacik58176a92007-08-29 15:47:34 -0400179
Christoph Hellwigb2141072008-09-05 16:43:31 -0400180int btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400181{
Greg KHe3fe4e72008-02-20 14:14:16 -0500182 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
183 if (!btrfs_kset)
184 return -ENOMEM;
185 return 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400186}
187
Christoph Hellwigb2141072008-09-05 16:43:31 -0400188void btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400189{
Greg KHe3fe4e72008-02-20 14:14:16 -0500190 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -0400191}
Chris Mason55d47412008-02-20 16:02:51 -0500192