16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1958176a96SJosef Bacik #include <linux/sched.h> 2058176a96SJosef Bacik #include <linux/slab.h> 2158176a96SJosef Bacik #include <linux/spinlock.h> 2258176a96SJosef Bacik #include <linux/completion.h> 2358176a96SJosef Bacik #include <linux/buffer_head.h> 2458176a96SJosef Bacik #include <linux/module.h> 2558176a96SJosef Bacik #include <linux/kobject.h> 2658176a96SJosef Bacik 27bae45de0SChris Mason #include "ctree.h" 28bae45de0SChris Mason #include "disk-io.h" 29bae45de0SChris Mason #include "transaction.h" 3058176a96SJosef Bacik 3158176a96SJosef Bacik static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf) 3258176a96SJosef Bacik { 3358176a96SJosef Bacik return snprintf(buf, PAGE_SIZE, "%llu\n", 3458176a96SJosef Bacik (unsigned long long)btrfs_root_blocks_used(&root->root_item)); 3558176a96SJosef Bacik } 3658176a96SJosef Bacik 3758176a96SJosef Bacik static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf) 3858176a96SJosef Bacik { 3958176a96SJosef Bacik return snprintf(buf, PAGE_SIZE, "%llu\n", 4058176a96SJosef Bacik (unsigned long long)btrfs_root_block_limit(&root->root_item)); 4158176a96SJosef Bacik } 4258176a96SJosef Bacik 4358176a96SJosef Bacik static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf) 4458176a96SJosef Bacik { 4558176a96SJosef Bacik return snprintf(buf, PAGE_SIZE, "%llu\n", 4658176a96SJosef Bacik (unsigned long long)btrfs_super_blocks_used(fs->disk_super)); 4758176a96SJosef Bacik } 4858176a96SJosef Bacik 4958176a96SJosef Bacik static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf) 5058176a96SJosef Bacik { 5158176a96SJosef Bacik return snprintf(buf, PAGE_SIZE, "%llu\n", 5258176a96SJosef Bacik (unsigned long long)btrfs_super_total_blocks(fs->disk_super)); 5358176a96SJosef Bacik } 5458176a96SJosef Bacik 5558176a96SJosef Bacik static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf) 5658176a96SJosef Bacik { 5758176a96SJosef Bacik return snprintf(buf, PAGE_SIZE, "%llu\n", 5858176a96SJosef Bacik (unsigned long long)btrfs_super_blocksize(fs->disk_super)); 5958176a96SJosef Bacik } 6058176a96SJosef Bacik 6158176a96SJosef Bacik /* this is for root attrs (subvols/snapshots) */ 6258176a96SJosef Bacik struct btrfs_root_attr { 6358176a96SJosef Bacik struct attribute attr; 6458176a96SJosef Bacik ssize_t (*show)(struct btrfs_root *, char *); 6558176a96SJosef Bacik ssize_t (*store)(struct btrfs_root *, const char *, size_t); 6658176a96SJosef Bacik }; 6758176a96SJosef Bacik 6858176a96SJosef Bacik #define ROOT_ATTR(name, mode, show, store) \ 6958176a96SJosef Bacik static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, show, store) 7058176a96SJosef Bacik 7158176a96SJosef Bacik ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL); 7258176a96SJosef Bacik ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL); 7358176a96SJosef Bacik 7458176a96SJosef Bacik static struct attribute *btrfs_root_attrs[] = { 7558176a96SJosef Bacik &btrfs_root_attr_blocks_used.attr, 7658176a96SJosef Bacik &btrfs_root_attr_block_limit.attr, 7758176a96SJosef Bacik NULL, 7858176a96SJosef Bacik }; 7958176a96SJosef Bacik 8058176a96SJosef Bacik /* this is for super attrs (actual full fs) */ 8158176a96SJosef Bacik struct btrfs_super_attr { 8258176a96SJosef Bacik struct attribute attr; 8358176a96SJosef Bacik ssize_t (*show)(struct btrfs_fs_info *, char *); 8458176a96SJosef Bacik ssize_t (*store)(struct btrfs_fs_info *, const char *, size_t); 8558176a96SJosef Bacik }; 8658176a96SJosef Bacik 8758176a96SJosef Bacik #define SUPER_ATTR(name, mode, show, store) \ 8858176a96SJosef Bacik static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, show, store) 8958176a96SJosef Bacik 9058176a96SJosef Bacik SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL); 9158176a96SJosef Bacik SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL); 9258176a96SJosef Bacik SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL); 9358176a96SJosef Bacik 9458176a96SJosef Bacik static struct attribute *btrfs_super_attrs[] = { 9558176a96SJosef Bacik &btrfs_super_attr_blocks_used.attr, 9658176a96SJosef Bacik &btrfs_super_attr_total_blocks.attr, 9758176a96SJosef Bacik &btrfs_super_attr_blocksize.attr, 9858176a96SJosef Bacik NULL, 9958176a96SJosef Bacik }; 10058176a96SJosef Bacik 10158176a96SJosef Bacik static ssize_t btrfs_super_attr_show(struct kobject *kobj, 10258176a96SJosef Bacik struct attribute *attr, char *buf) 10358176a96SJosef Bacik { 10458176a96SJosef Bacik struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, 10558176a96SJosef Bacik super_kobj); 10658176a96SJosef Bacik struct btrfs_super_attr *a = container_of(attr, 10758176a96SJosef Bacik struct btrfs_super_attr, 10858176a96SJosef Bacik attr); 10958176a96SJosef Bacik 11058176a96SJosef Bacik return a->show ? a->show(fs, buf) : 0; 11158176a96SJosef Bacik } 11258176a96SJosef Bacik 11358176a96SJosef Bacik static ssize_t btrfs_super_attr_store(struct kobject *kobj, 11458176a96SJosef Bacik struct attribute *attr, 11558176a96SJosef Bacik const char *buf, size_t len) 11658176a96SJosef Bacik { 11758176a96SJosef Bacik struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, 11858176a96SJosef Bacik super_kobj); 11958176a96SJosef Bacik struct btrfs_super_attr *a = container_of(attr, 12058176a96SJosef Bacik struct btrfs_super_attr, 12158176a96SJosef Bacik attr); 12258176a96SJosef Bacik 12358176a96SJosef Bacik return a->store ? a->store(fs, buf, len) : 0; 12458176a96SJosef Bacik } 12558176a96SJosef Bacik 12658176a96SJosef Bacik static ssize_t btrfs_root_attr_show(struct kobject *kobj, 12758176a96SJosef Bacik struct attribute *attr, char *buf) 12858176a96SJosef Bacik { 12958176a96SJosef Bacik struct btrfs_root *root = container_of(kobj, struct btrfs_root, 13058176a96SJosef Bacik root_kobj); 13158176a96SJosef Bacik struct btrfs_root_attr *a = container_of(attr, 13258176a96SJosef Bacik struct btrfs_root_attr, 13358176a96SJosef Bacik attr); 13458176a96SJosef Bacik 13558176a96SJosef Bacik return a->show ? a->show(root, buf) : 0; 13658176a96SJosef Bacik } 13758176a96SJosef Bacik 13858176a96SJosef Bacik static ssize_t btrfs_root_attr_store(struct kobject *kobj, 13958176a96SJosef Bacik struct attribute *attr, 14058176a96SJosef Bacik const char *buf, size_t len) 14158176a96SJosef Bacik { 14258176a96SJosef Bacik struct btrfs_root *root = container_of(kobj, struct btrfs_root, 14358176a96SJosef Bacik root_kobj); 14458176a96SJosef Bacik struct btrfs_root_attr *a = container_of(attr, 14558176a96SJosef Bacik struct btrfs_root_attr, 14658176a96SJosef Bacik attr); 14758176a96SJosef Bacik return a->store ? a->store(root, buf, len) : 0; 14858176a96SJosef Bacik } 14958176a96SJosef Bacik 15058176a96SJosef Bacik static void btrfs_super_release(struct kobject *kobj) 15158176a96SJosef Bacik { 15258176a96SJosef Bacik struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info, 15358176a96SJosef Bacik super_kobj); 15458176a96SJosef Bacik complete(&fs->kobj_unregister); 15558176a96SJosef Bacik } 15658176a96SJosef Bacik 15758176a96SJosef Bacik static void btrfs_root_release(struct kobject *kobj) 15858176a96SJosef Bacik { 15958176a96SJosef Bacik struct btrfs_root *root = container_of(kobj, struct btrfs_root, 16058176a96SJosef Bacik root_kobj); 16158176a96SJosef Bacik complete(&root->kobj_unregister); 16258176a96SJosef Bacik } 16358176a96SJosef Bacik 16458176a96SJosef Bacik static struct sysfs_ops btrfs_super_attr_ops = { 16558176a96SJosef Bacik .show = btrfs_super_attr_show, 16658176a96SJosef Bacik .store = btrfs_super_attr_store, 16758176a96SJosef Bacik }; 16858176a96SJosef Bacik 16958176a96SJosef Bacik static struct sysfs_ops btrfs_root_attr_ops = { 17058176a96SJosef Bacik .show = btrfs_root_attr_show, 17158176a96SJosef Bacik .store = btrfs_root_attr_store, 17258176a96SJosef Bacik }; 17358176a96SJosef Bacik 17458176a96SJosef Bacik static struct kobj_type btrfs_root_ktype = { 17558176a96SJosef Bacik .default_attrs = btrfs_root_attrs, 17658176a96SJosef Bacik .sysfs_ops = &btrfs_root_attr_ops, 17758176a96SJosef Bacik .release = btrfs_root_release, 17858176a96SJosef Bacik }; 17958176a96SJosef Bacik 18058176a96SJosef Bacik static struct kobj_type btrfs_super_ktype = { 18158176a96SJosef Bacik .default_attrs = btrfs_super_attrs, 18258176a96SJosef Bacik .sysfs_ops = &btrfs_super_attr_ops, 18358176a96SJosef Bacik .release = btrfs_super_release, 18458176a96SJosef Bacik }; 18558176a96SJosef Bacik 18658176a96SJosef Bacik static struct kset btrfs_kset = { 18758176a96SJosef Bacik .kobj = {.name = "btrfs"}, 18858176a96SJosef Bacik }; 18958176a96SJosef Bacik 19058176a96SJosef Bacik int btrfs_sysfs_add_super(struct btrfs_fs_info *fs) 19158176a96SJosef Bacik { 19258176a96SJosef Bacik int error; 19358176a96SJosef Bacik 19458176a96SJosef Bacik fs->super_kobj.kset = &btrfs_kset; 19558176a96SJosef Bacik fs->super_kobj.ktype = &btrfs_super_ktype; 19658176a96SJosef Bacik 19758176a96SJosef Bacik error = kobject_set_name(&fs->super_kobj, "%s", 19858176a96SJosef Bacik fs->sb->s_id); 19958176a96SJosef Bacik if (error) 20058176a96SJosef Bacik goto fail; 20158176a96SJosef Bacik 20258176a96SJosef Bacik error = kobject_register(&fs->super_kobj); 20358176a96SJosef Bacik if (error) 20458176a96SJosef Bacik goto fail; 20558176a96SJosef Bacik 20658176a96SJosef Bacik return 0; 20758176a96SJosef Bacik 20858176a96SJosef Bacik fail: 20958176a96SJosef Bacik printk(KERN_ERR "btrfs: sysfs creation for super failed\n"); 21058176a96SJosef Bacik return error; 21158176a96SJosef Bacik } 21258176a96SJosef Bacik 21358176a96SJosef Bacik int btrfs_sysfs_add_root(struct btrfs_root *root) 21458176a96SJosef Bacik { 21558176a96SJosef Bacik int error; 21658176a96SJosef Bacik 21758176a96SJosef Bacik root->root_kobj.ktype = &btrfs_root_ktype; 21858176a96SJosef Bacik root->root_kobj.parent = &root->fs_info->super_kobj; 21958176a96SJosef Bacik 22058176a96SJosef Bacik error = kobject_set_name(&root->root_kobj, "%s", root->name); 22158176a96SJosef Bacik if (error) { 22258176a96SJosef Bacik goto fail; 22358176a96SJosef Bacik } 22458176a96SJosef Bacik 22558176a96SJosef Bacik error = kobject_register(&root->root_kobj); 22658176a96SJosef Bacik if (error) 22758176a96SJosef Bacik goto fail; 22858176a96SJosef Bacik 22958176a96SJosef Bacik return 0; 23058176a96SJosef Bacik 23158176a96SJosef Bacik fail: 23258176a96SJosef Bacik printk(KERN_ERR "btrfs: sysfs creation for root failed\n"); 23358176a96SJosef Bacik return error; 23458176a96SJosef Bacik } 23558176a96SJosef Bacik 23658176a96SJosef Bacik void btrfs_sysfs_del_root(struct btrfs_root *root) 23758176a96SJosef Bacik { 23858176a96SJosef Bacik kobject_unregister(&root->root_kobj); 23958176a96SJosef Bacik wait_for_completion(&root->kobj_unregister); 24058176a96SJosef Bacik } 24158176a96SJosef Bacik 24258176a96SJosef Bacik void btrfs_sysfs_del_super(struct btrfs_fs_info *fs) 24358176a96SJosef Bacik { 24458176a96SJosef Bacik kobject_unregister(&fs->super_kobj); 24558176a96SJosef Bacik wait_for_completion(&fs->kobj_unregister); 24658176a96SJosef Bacik } 24758176a96SJosef Bacik 24858176a96SJosef Bacik int btrfs_init_sysfs() 24958176a96SJosef Bacik { 25058176a96SJosef Bacik kobj_set_kset_s(&btrfs_kset, fs_subsys); 25158176a96SJosef Bacik return kset_register(&btrfs_kset); 25258176a96SJosef Bacik } 25358176a96SJosef Bacik 25458176a96SJosef Bacik void btrfs_exit_sysfs() 25558176a96SJosef Bacik { 25658176a96SJosef Bacik kset_unregister(&btrfs_kset); 25758176a96SJosef Bacik } 258