[PATCH 2/4] btrfs: detect uninitialized btrfs_root::anon_dev for user visible subvolumes

Qu Wenruo
2020-06-16T02:17:51Z

btrfs_root::anon_dev is an indicator for different subvolume namespaces.
Thus it should always be initialized to an anonymous block device.

Add a safe net to catch such uninitialized values.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/inode.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 00647e8cf2df..195aac71fe32 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -8697,6 +8697,7 @@ static int btrfs_getattr(const struct path *path, struct kstat *stat,
{
u64 delalloc_bytes;
struct inode *inode = d_inode(path->dentry);
+ struct btrfs_root *root = BTRFS_I(inode)->root;
u32 blocksize = inode->i_sb->s_blocksize;
u32 bi_flags = BTRFS_I(inode)->flags;

@@ -8718,7 +8719,13 @@ static int btrfs_getattr(const struct path *path, struct kstat *stat,
STATX_ATTR_NODUMP);

generic_fillattr(inode, stat);
- stat->dev = BTRFS_I(inode)->root->anon_dev;
+ if (unlikely(!root->anon_dev)) {
+ WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
+ btrfs_warn(root->fs_info,
+ "uninitialized btrfs_root::anon_dev detected, root=%llu",
+ root->root_key.objectid);
+ }
+ stat->dev = root->anon_dev;

spin_lock(&BTRFS_I(inode)->lock);
delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
--
2.27.0