btrfs: use proper endianness accessors for super_copy
authorAnand Jain <anand.jain@oracle.com>
Thu, 22 Feb 2018 13:58:42 +0000 (21:58 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 9 Mar 2018 06:41:05 +0000 (22:41 -0800)
commit 3c181c12c431fe33b669410d663beb9cceefcd1b upstream.

The fs_info::super_copy is a byte copy of the on-disk structure and all
members must use the accessor macros/functions to obtain the right
value.  This was missing in update_super_roots and in sysfs readers.

Moving between opposite endianness hosts will report bogus numbers in
sysfs, and mount may fail as the root will not be restored correctly. If
the filesystem is always used on a same endian host, this will not be a
problem.

Fix this by using the btrfs_set_super...() functions to set
fs_info::super_copy values, and for the sysfs, use the cached
fs_info::nodesize/sectorsize values.

CC: stable@vger.kernel.org
Fixes: df93589a17378 ("btrfs: export more from FS_INFO to sysfs")
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/sysfs.c
fs/btrfs/transaction.c

index 883881b16c86f7539b1de2eb9243c951e112f408..4447e0fe9b559e31200f43aa340f74f5b89cb31c 100644 (file)
@@ -422,7 +422,7 @@ static ssize_t btrfs_nodesize_show(struct kobject *kobj,
 {
        struct btrfs_fs_info *fs_info = to_fs_info(kobj);
 
-       return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
+       return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->nodesize);
 }
 
 BTRFS_ATTR(nodesize, btrfs_nodesize_show);
@@ -432,8 +432,7 @@ static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
 {
        struct btrfs_fs_info *fs_info = to_fs_info(kobj);
 
-       return snprintf(buf, PAGE_SIZE, "%u\n",
-                       fs_info->super_copy->sectorsize);
+       return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->sectorsize);
 }
 
 BTRFS_ATTR(sectorsize, btrfs_sectorsize_show);
@@ -443,8 +442,7 @@ static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
 {
        struct btrfs_fs_info *fs_info = to_fs_info(kobj);
 
-       return snprintf(buf, PAGE_SIZE, "%u\n",
-                       fs_info->super_copy->sectorsize);
+       return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->sectorsize);
 }
 
 BTRFS_ATTR(clone_alignment, btrfs_clone_alignment_show);
index f615d59b0489e2af52a711b896f758e13d05bd46..46bda13e5727b53aa2e1ae249fdce98cc51e1d42 100644 (file)
@@ -1722,19 +1722,23 @@ static void update_super_roots(struct btrfs_fs_info *fs_info)
 
        super = fs_info->super_copy;
 
+       /* update latest btrfs_super_block::chunk_root refs */
        root_item = &fs_info->chunk_root->root_item;
-       super->chunk_root = root_item->bytenr;
-       super->chunk_root_generation = root_item->generation;
-       super->chunk_root_level = root_item->level;
+       btrfs_set_super_chunk_root(super, root_item->bytenr);
+       btrfs_set_super_chunk_root_generation(super, root_item->generation);
+       btrfs_set_super_chunk_root_level(super, root_item->level);
 
+       /* update latest btrfs_super_block::root refs */
        root_item = &fs_info->tree_root->root_item;
-       super->root = root_item->bytenr;
-       super->generation = root_item->generation;
-       super->root_level = root_item->level;
+       btrfs_set_super_root(super, root_item->bytenr);
+       btrfs_set_super_generation(super, root_item->generation);
+       btrfs_set_super_root_level(super, root_item->level);
+
        if (btrfs_test_opt(fs_info, SPACE_CACHE))
-               super->cache_generation = root_item->generation;
+               btrfs_set_super_cache_generation(super, root_item->generation);
        if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
-               super->uuid_tree_generation = root_item->generation;
+               btrfs_set_super_uuid_tree_generation(super,
+                                                    root_item->generation);
 }
 
 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)