This patch adds per-super attributes to sysfs.
It doesn't publish any attributes yet, but does the proper lifetime
handling as well as the basic infrastructure to add new attributes.
Signed-off-by: Jeff Mahoney <jeffm@xxxxxxxx>
---
fs/btrfs/ctree.h | 2 +
fs/btrfs/super.c | 13 +++++++++++-
fs/btrfs/sysfs.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/sysfs.h | 19 ++++++++++++++++++
4 files changed, 91 insertions(+), 1 deletion(-)
--- a/fs/btrfs/ctree.h 2013-09-10 00:09:12.990087784 -0400
+++ b/fs/btrfs/ctree.h 2013-09-10 00:09:35.521794520 -0400
@@ -3694,6 +3694,8 @@ int btrfs_defrag_leaves(struct btrfs_tra
/* sysfs.c */
int btrfs_init_sysfs(void);
void btrfs_exit_sysfs(void);
+int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info);
+void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info);
/* xattr.c */
ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
--- a/fs/btrfs/super.c 2013-09-10 00:09:12.994087730 -0400
+++ b/fs/btrfs/super.c 2013-09-10 00:09:35.525794464 -0400
@@ -301,6 +301,8 @@ void __btrfs_panic(struct btrfs_fs_info
static void btrfs_put_super(struct super_block *sb)
{
+ btrfs_sysfs_remove_one(btrfs_sb(sb));
+
(void)close_ctree(btrfs_sb(sb)->tree_root);
/* FIXME: need to fix VFS to return error? */
/* AV: return it _where_? ->put_super() can be triggered by any number
@@ -1143,8 +1145,17 @@ static struct dentry *btrfs_mount(struct
}
root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
- if (IS_ERR(root))
+ if (IS_ERR(root)) {
deactivate_locked_super(s);
+ return root;
+ }
+
+ error = btrfs_sysfs_add_one(fs_info);
+ if (error) {
+ dput(root);
+ deactivate_locked_super(s);
+ return ERR_PTR(error);
+ }
return root;
--- a/fs/btrfs/sysfs.c 2013-09-10 00:09:13.002087628 -0400
+++ b/fs/btrfs/sysfs.c 2013-09-10 00:09:49.501616538 -0400
@@ -61,6 +61,64 @@ static struct attribute *btrfs_supp_feat
NULL
};
+static struct attribute *btrfs_attrs[] = {
+ NULL,
+};
+
+static void btrfs_fs_info_release(struct kobject *kobj)
+{
+ struct btrfs_fs_info *fs_info;
+ fs_info = container_of(kobj, struct btrfs_fs_info, super_kobj);
+ complete(&fs_info->kobj_unregister);
+}
+
+static ssize_t btrfs_attr_show(struct kobject *kobj,
+ struct attribute *attr, char *buf)
+{
+ struct btrfs_attr *a = container_of(attr, struct btrfs_attr, attr);
+ struct btrfs_fs_info *fs_info;
+ fs_info = container_of(kobj, struct btrfs_fs_info, super_kobj);
+
+ return a->show ? a->show(a, fs_info, buf) : 0;
+}
+
+static ssize_t btrfs_attr_store(struct kobject *kobj,
+ struct attribute *attr,
+ const char *buf, size_t len)
+{
+ struct btrfs_attr *a = container_of(attr, struct btrfs_attr, attr);
+ struct btrfs_fs_info *fs_info;
+ fs_info = container_of(kobj, struct btrfs_fs_info, super_kobj);
+
+ return a->store ? a->store(a, fs_info, buf, len) : 0;
+}
+
+static const struct sysfs_ops btrfs_attr_ops = {
+ .show = btrfs_attr_show,
+ .store = btrfs_attr_store,
+};
+
+static struct kobj_type btrfs_ktype = {
+ .default_attrs = btrfs_attrs,
+ .sysfs_ops = &btrfs_attr_ops,
+ .release = btrfs_fs_info_release,
+};
+
+int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
+{
+ init_completion(&fs_info->kobj_unregister);
+ fs_info->super_kobj.kset = btrfs_kset;
+ return kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL,
+ "%pU", fs_info->fsid);
+}
+
+void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
+{
+ kobject_del(&fs_info->super_kobj);