Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 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> |
| 26 | |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "transaction.h" |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 30 | |
| 31 | static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf) |
| 32 | { |
| 33 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 34 | (unsigned long long)btrfs_root_used(&root->root_item)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf) |
| 38 | { |
| 39 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 40 | (unsigned long long)btrfs_root_limit(&root->root_item)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf) |
| 44 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 45 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 46 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 47 | (unsigned long long)btrfs_super_bytes_used(&fs->super_copy)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf) |
| 51 | { |
| 52 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 53 | (unsigned long long)btrfs_super_total_bytes(&fs->super_copy)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf) |
| 57 | { |
| 58 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 59 | (unsigned long long)btrfs_super_sectorsize(&fs->super_copy)); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* this is for root attrs (subvols/snapshots) */ |
| 63 | struct 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) \ |
| 70 | static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, show, store) |
| 71 | |
| 72 | ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL); |
| 73 | ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL); |
| 74 | |
| 75 | static struct attribute *btrfs_root_attrs[] = { |
| 76 | &btrfs_root_attr_blocks_used.attr, |
| 77 | &btrfs_root_attr_block_limit.attr, |
| 78 | NULL, |
| 79 | }; |
| 80 | |
| 81 | /* this is for super attrs (actual full fs) */ |
| 82 | struct btrfs_super_attr { |
| 83 | struct attribute attr; |
| 84 | ssize_t (*show)(struct btrfs_fs_info *, char *); |
| 85 | ssize_t (*store)(struct btrfs_fs_info *, const char *, size_t); |
| 86 | }; |
| 87 | |
| 88 | #define SUPER_ATTR(name, mode, show, store) \ |
| 89 | static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, show, store) |
| 90 | |
| 91 | SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL); |
| 92 | SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL); |
| 93 | SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL); |
| 94 | |
| 95 | static struct attribute *btrfs_super_attrs[] = { |
| 96 | &btrfs_super_attr_blocks_used.attr, |
| 97 | &btrfs_super_attr_total_blocks.attr, |
| 98 | &btrfs_super_attr_blocksize.attr, |
| 99 | NULL, |
| 100 | }; |
| 101 | |
| 102 | static ssize_t btrfs_super_attr_show(struct kobject *kobj, |
| 103 | struct attribute *attr, char *buf) |
| 104 | { |
| 105 | struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, |
| 106 | super_kobj); |
| 107 | struct btrfs_super_attr *a = container_of(attr, |
| 108 | struct btrfs_super_attr, |
| 109 | attr); |
| 110 | |
| 111 | return a->show ? a->show(fs, buf) : 0; |
| 112 | } |
| 113 | |
| 114 | static ssize_t btrfs_super_attr_store(struct kobject *kobj, |
| 115 | struct attribute *attr, |
| 116 | const char *buf, size_t len) |
| 117 | { |
| 118 | struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, |
| 119 | super_kobj); |
| 120 | struct btrfs_super_attr *a = container_of(attr, |
| 121 | struct btrfs_super_attr, |
| 122 | attr); |
| 123 | |
| 124 | return a->store ? a->store(fs, buf, len) : 0; |
| 125 | } |
| 126 | |
| 127 | static ssize_t btrfs_root_attr_show(struct kobject *kobj, |
| 128 | struct attribute *attr, char *buf) |
| 129 | { |
| 130 | struct btrfs_root *root = container_of(kobj, struct btrfs_root, |
| 131 | root_kobj); |
| 132 | struct btrfs_root_attr *a = container_of(attr, |
| 133 | struct btrfs_root_attr, |
| 134 | attr); |
| 135 | |
| 136 | return a->show ? a->show(root, buf) : 0; |
| 137 | } |
| 138 | |
| 139 | static ssize_t btrfs_root_attr_store(struct kobject *kobj, |
| 140 | struct attribute *attr, |
| 141 | const char *buf, size_t len) |
| 142 | { |
| 143 | struct btrfs_root *root = container_of(kobj, struct btrfs_root, |
| 144 | root_kobj); |
| 145 | struct btrfs_root_attr *a = container_of(attr, |
| 146 | struct btrfs_root_attr, |
| 147 | attr); |
| 148 | return a->store ? a->store(root, buf, len) : 0; |
| 149 | } |
| 150 | |
| 151 | static void btrfs_super_release(struct kobject *kobj) |
| 152 | { |
| 153 | struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, |
| 154 | super_kobj); |
| 155 | complete(&fs->kobj_unregister); |
| 156 | } |
| 157 | |
| 158 | static void btrfs_root_release(struct kobject *kobj) |
| 159 | { |
| 160 | struct btrfs_root *root = container_of(kobj, struct btrfs_root, |
| 161 | root_kobj); |
| 162 | complete(&root->kobj_unregister); |
| 163 | } |
| 164 | |
| 165 | static struct sysfs_ops btrfs_super_attr_ops = { |
| 166 | .show = btrfs_super_attr_show, |
| 167 | .store = btrfs_super_attr_store, |
| 168 | }; |
| 169 | |
| 170 | static struct sysfs_ops btrfs_root_attr_ops = { |
| 171 | .show = btrfs_root_attr_show, |
| 172 | .store = btrfs_root_attr_store, |
| 173 | }; |
| 174 | |
| 175 | static struct kobj_type btrfs_root_ktype = { |
| 176 | .default_attrs = btrfs_root_attrs, |
| 177 | .sysfs_ops = &btrfs_root_attr_ops, |
| 178 | .release = btrfs_root_release, |
| 179 | }; |
| 180 | |
| 181 | static struct kobj_type btrfs_super_ktype = { |
| 182 | .default_attrs = btrfs_super_attrs, |
| 183 | .sysfs_ops = &btrfs_super_attr_ops, |
| 184 | .release = btrfs_super_release, |
| 185 | }; |
| 186 | |
| 187 | static struct kset btrfs_kset = { |
| 188 | .kobj = {.name = "btrfs"}, |
| 189 | }; |
| 190 | |
| 191 | int btrfs_sysfs_add_super(struct btrfs_fs_info *fs) |
| 192 | { |
| 193 | int error; |
| 194 | |
| 195 | fs->super_kobj.kset = &btrfs_kset; |
| 196 | fs->super_kobj.ktype = &btrfs_super_ktype; |
| 197 | |
| 198 | error = kobject_set_name(&fs->super_kobj, "%s", |
| 199 | fs->sb->s_id); |
| 200 | if (error) |
| 201 | goto fail; |
| 202 | |
| 203 | error = kobject_register(&fs->super_kobj); |
| 204 | if (error) |
| 205 | goto fail; |
| 206 | |
| 207 | return 0; |
| 208 | |
| 209 | fail: |
| 210 | printk(KERN_ERR "btrfs: sysfs creation for super failed\n"); |
| 211 | return error; |
| 212 | } |
| 213 | |
| 214 | int btrfs_sysfs_add_root(struct btrfs_root *root) |
| 215 | { |
| 216 | int error; |
| 217 | |
| 218 | root->root_kobj.ktype = &btrfs_root_ktype; |
| 219 | root->root_kobj.parent = &root->fs_info->super_kobj; |
| 220 | |
| 221 | error = kobject_set_name(&root->root_kobj, "%s", root->name); |
| 222 | if (error) { |
| 223 | goto fail; |
| 224 | } |
| 225 | |
| 226 | error = kobject_register(&root->root_kobj); |
| 227 | if (error) |
| 228 | goto fail; |
| 229 | |
| 230 | return 0; |
| 231 | |
| 232 | fail: |
| 233 | printk(KERN_ERR "btrfs: sysfs creation for root failed\n"); |
| 234 | return error; |
| 235 | } |
| 236 | |
| 237 | void btrfs_sysfs_del_root(struct btrfs_root *root) |
| 238 | { |
| 239 | kobject_unregister(&root->root_kobj); |
| 240 | wait_for_completion(&root->kobj_unregister); |
| 241 | } |
| 242 | |
| 243 | void btrfs_sysfs_del_super(struct btrfs_fs_info *fs) |
| 244 | { |
| 245 | kobject_unregister(&fs->super_kobj); |
| 246 | wait_for_completion(&fs->kobj_unregister); |
| 247 | } |
| 248 | |
| 249 | int btrfs_init_sysfs() |
| 250 | { |
| 251 | kobj_set_kset_s(&btrfs_kset, fs_subsys); |
| 252 | return kset_register(&btrfs_kset); |
| 253 | } |
| 254 | |
| 255 | void btrfs_exit_sysfs() |
| 256 | { |
| 257 | kset_unregister(&btrfs_kset); |
| 258 | } |