diff mbox

[5/9] btrfs: add per-super attributes to sysfs

Message ID 20130916182018.459541510@suse.com
State New, archived
Headers show

Commit Message

Jeff Mahoney Sept. 16, 2013, 6:19 p.m. UTC
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@suse.com>
---
 fs/btrfs/ctree.h |    2 +
 fs/btrfs/super.c |   13 +++++++++++-
 fs/btrfs/sysfs.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h |   20 +++++++++++++++++++
 4 files changed, 90 insertions(+), 1 deletion(-)



--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

--- a/fs/btrfs/ctree.h	2013-09-16 13:49:06.656768298 -0400
+++ b/fs/btrfs/ctree.h	2013-09-16 13:49:25.464512784 -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-16 13:49:06.660768245 -0400
+++ b/fs/btrfs/super.c	2013-09-16 13:49:25.464512784 -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-16 13:49:06.668768135 -0400
+++ b/fs/btrfs/sysfs.c	2013-09-16 13:49:25.464512784 -0400
@@ -27,6 +27,7 @@ 
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
+#include "sysfs.h"
 
 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
@@ -67,8 +68,63 @@  static struct kobj_type btrfs_supp_feat_
 	.release	= kobj_completion_release,
 };
 
+static struct attribute *btrfs_attrs[] = {
+	NULL,
+};
+
+#define super_kobj_to_fs_info(kobj) \
+	container_of(kobj, struct btrfs_fs_info, super_kc.kc_kobj)
+
+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 = super_kobj_to_fs_info(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 = super_kobj_to_fs_info(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	= kobj_completion_release,
+};
+
 /* /sys/fs/btrfs/ entry */
 static struct kset *btrfs_kset;
+
+int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
+{
+	int error;
+
+	kobj_completion_init(&fs_info->super_kc, &btrfs_ktype);
+	fs_info->super_kc.kc_kobj.kset = btrfs_kset;
+	error = kobject_add(&fs_info->super_kc.kc_kobj, NULL,
+			    "%pU", fs_info->fsid);
+
+	return error;
+}
+
+void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
+{
+	kobj_completion_del_and_wait(&fs_info->super_kc);
+}
+
 static struct kobj_completion btrfs_features;
 
 int btrfs_init_sysfs(void)
--- a/fs/btrfs/sysfs.h	2013-09-16 13:49:06.668768135 -0400
+++ b/fs/btrfs/sysfs.h	2013-09-16 13:49:25.464512784 -0400
@@ -8,6 +8,25 @@  enum btrfs_feature_set {
 	FEAT_MAX
 };
 
+struct btrfs_attr {
+	struct attribute attr;
+	ssize_t (*show)(struct btrfs_attr *, struct btrfs_fs_info *, char *);
+	ssize_t (*store)(struct btrfs_attr *, struct btrfs_fs_info *,
+			 const char *, size_t);
+};
+
+#define __INIT_BTRFS_ATTR(_name, _mode, _show, _store)			\
+{									\
+	.attr	= { .name = __stringify(_name), .mode = _mode },	\
+	.show	= _show,						\
+	.store	= _store,						\
+}
+
+#define BTRFS_ATTR(_name, _mode, _show, _store)				\
+static struct btrfs_attr btrfs_attr_##_name =				\
+			__INIT_BTRFS_ATTR(_name, _mode, _show, _store)
+#define BTRFS_ATTR_LIST(_name)    (&btrfs_attr_##_name.attr),
+
 struct btrfs_feature_attr {
 	struct attribute attr;			/* global show, no store */
 	enum btrfs_feature_set feature_set;
@@ -30,6 +49,7 @@  static struct btrfs_feature_attr btrfs_a
 	BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
 
 /* convert from attribute */
+#define to_btrfs_attr(a) container_of(a, struct btrfs_attr, attr)
 #define to_btrfs_feature_attr(a) \
 			container_of(a, struct btrfs_feature_attr, attr)
 #endif /* _BTRFS_SYSFS_H_ */