Blob Blame History Raw
From: "Luis R. Rodriguez" <mcgrof@suse.com>
Date: Wed, 16 Jul 2014 14:16:29 -0700
Subject: [PATCH 1/2] fs/super.c: add new super block sub devices
 super_block_dev
References: bnc#865869
Patch-mainline: submitted, but not accepted

Modern filesystems are using the get_anon_bdev() for internal
notions of volumes, snapshots for a single super block but never
exposing them directly to the VFS layer. While this works its
leaves the VFS layer growing dumb over what filesystems are doing.
This creates a new super block subdevice which we can use to start
stuffing in information about the underlying bdev's and its
associated super block to start off with. This at least now lets
us implement proper support for ustat() once filesystems are
modified to use this data structure and respective helpers.

Update: 16 Feb 2017 jeffm
- Removed loop iteration to remove element
- Added initializer
- List element connectedness determines validity

Signed-off-by: Luis R. Rodriguez <mcgrof@suse.com>
---
 fs/super.c         |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 include/linux/fs.h |   20 +++++++++++++++++
 2 files changed, 77 insertions(+), 2 deletions(-)

--- a/fs/super.c
+++ b/fs/super.c
@@ -154,6 +154,59 @@ static void destroy_super_rcu(struct rcu
 	schedule_work(&s->destroy_work);
 }
 
+static bool super_dev_match(struct super_block *sb, dev_t dev)
+{
+	struct super_block_dev *sbdev;
+
+	if (sb->s_dev == dev)
+		return true;
+
+	if (list_empty(&sb->s_sbdevs))
+		return false;
+
+	list_for_each_entry(sbdev, &sb->s_sbdevs, entry)
+		if (sbdev->anon_dev == dev)
+			return true;
+
+	return false;
+}
+
+/* To be used only by btrfs */
+int insert_anon_sbdev(struct super_block *sb, struct super_block_dev *sbdev)
+{
+	int ret;
+
+	ret = get_anon_bdev(&sbdev->anon_dev);
+	if (ret)
+		return ret;
+
+	sbdev->sb = sb;
+
+	spin_lock(&sb_lock);
+	list_add_tail(&sbdev->entry, &sb->s_sbdevs);
+	spin_unlock(&sb_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(insert_anon_sbdev);
+
+/* To be used only by btrfs */
+void remove_anon_sbdev(struct super_block_dev *sbdev)
+{
+	bool remove = false;
+
+	spin_lock(&sb_lock);
+	if (!list_empty(&sbdev->entry)) {
+		remove = true;
+		list_del_init(&sbdev->entry);
+	}
+	spin_unlock(&sb_lock);
+
+	if (remove)
+		free_anon_bdev(sbdev->anon_dev);
+}
+EXPORT_SYMBOL_GPL(remove_anon_sbdev);
+
 /**
  *	destroy_super	-	frees a superblock
  *	@s: superblock to free
@@ -166,6 +219,7 @@ static void destroy_super(struct super_b
 	list_lru_destroy(&s->s_inode_lru);
 	security_sb_free(s);
 	WARN_ON(!list_empty(&s->s_mounts));
+	WARN_ON(!list_empty(&s->s_sbdevs));
 	put_user_ns(s->s_user_ns);
 	kfree(s->s_subtype);
 	kfree(s->s_options);
@@ -215,6 +269,7 @@ static struct super_block *alloc_super(s
 	spin_lock_init(&s->s_inode_list_lock);
 	INIT_LIST_HEAD(&s->s_inodes_wb);
 	spin_lock_init(&s->s_inode_wblist_lock);
+	INIT_LIST_HEAD(&s->s_sbdevs);
 
 	if (list_lru_init_memcg(&s->s_dentry_lru))
 		goto fail;
@@ -770,7 +825,7 @@ rescan:
 	list_for_each_entry(sb, &super_blocks, s_list) {
 		if (hlist_unhashed(&sb->s_instances))
 			continue;
-		if (sb->s_dev ==  dev) {
+		if (super_dev_match(sb, dev)) {
 			sb->s_count++;
 			spin_unlock(&sb_lock);
 			down_read(&sb->s_umount);
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1284,6 +1284,14 @@ struct sb_writers {
 	struct percpu_rw_semaphore	rw_sem[SB_FREEZE_LEVELS];
 };
 
+/* we can expand this to help the VFS layer with modern filesystems */
+/* To be used only used by btrfs */
+struct super_block_dev {
+	struct super_block	*sb;
+	struct list_head	entry;		/* For struct sb->s_sbdevs */
+	dev_t			anon_dev;
+};
+
 struct super_block {
 	struct list_head	s_list;		/* Keep this first */
 	dev_t			s_dev;		/* search index; _not_ kdev_t */
@@ -1310,6 +1318,7 @@ struct super_block {
 	const struct fscrypt_operations	*s_cop;
 
 	struct hlist_bl_head	s_anon;		/* anonymous dentries for (nfs) exporting */
+	struct list_head	s_sbdevs;	/* internal fs dev_t */
 	struct list_head	s_mounts;	/* list of mounts; _not_ for fs use */
 	struct block_device	*s_bdev;
 	struct backing_dev_info *s_bdi;
@@ -2043,6 +2052,17 @@ void deactivate_locked_super(struct supe
 int set_anon_super(struct super_block *s, void *data);
 int get_anon_bdev(dev_t *);
 void free_anon_bdev(dev_t);
+
+/* These two are to be used only by btrfs */
+int insert_anon_sbdev(struct super_block *sb, struct super_block_dev *sbdev);
+void remove_anon_sbdev(struct super_block_dev *sbdev);
+static inline void init_anon_sbdev(struct super_block_dev *sbdev)
+{
+	sbdev->sb = NULL;
+	INIT_LIST_HEAD(&sbdev->entry);
+	sbdev->anon_dev = 0;
+}
+
 struct super_block *sget_userns(struct file_system_type *type,
 			int (*test)(struct super_block *,void *),
 			int (*set)(struct super_block *,void *),