btrfs-progs: Introduce change_header_uuid function
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Mon, 11 May 2015 08:08:47 +0000 (16:08 +0800)
committerDavid Sterba <dsterba@suse.cz>
Thu, 14 May 2015 13:41:07 +0000 (15:41 +0200)
This function is used to change fsid and chunk_tree_uuid of a node/leaf.
The function does it without transaction protection.

This is the basis of offline uuid change.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfstune.c
ctree.h
props.c

index 808466f..92837e6 100644 (file)
@@ -98,6 +98,38 @@ static int enable_skinny_metadata(struct btrfs_root *root)
        return 0;
 }
 
+static int change_header_uuid(struct btrfs_root *root, struct extent_buffer *eb)
+{
+       struct btrfs_fs_info *fs_info = root->fs_info;
+       int same_fsid = 1;
+       int same_chunk_tree_uuid = 1;
+       int ret;
+
+       /* Check for whether we need to change fs/chunk id */
+       if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
+               return 0;
+       if (fs_info->new_fsid)
+               same_fsid = !memcmp_extent_buffer(eb, fs_info->new_fsid,
+                                         btrfs_header_fsid(), BTRFS_FSID_SIZE);
+       if (fs_info->new_chunk_tree_uuid)
+               same_chunk_tree_uuid =
+                       !memcmp_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
+                                             btrfs_header_chunk_tree_uuid(eb),
+                                             BTRFS_UUID_SIZE);
+       if (same_fsid && same_chunk_tree_uuid)
+               return 0;
+       if (!same_fsid)
+               write_extent_buffer(eb, fs_info->new_fsid, btrfs_header_fsid(),
+                                   BTRFS_FSID_SIZE);
+       if (!same_chunk_tree_uuid)
+               write_extent_buffer(eb, fs_info->new_chunk_tree_uuid,
+                                   btrfs_header_chunk_tree_uuid(eb),
+                                   BTRFS_UUID_SIZE);
+       ret = write_tree_block(NULL, root, eb);
+
+       return ret;
+}
+
 static void print_usage(void)
 {
        fprintf(stderr, "usage: btrfstune [options] device\n");
diff --git a/ctree.h b/ctree.h
index 98cb1fb..79a3310 100644 (file)
--- a/ctree.h
+++ b/ctree.h
@@ -955,7 +955,9 @@ struct btrfs_device;
 struct btrfs_fs_devices;
 struct btrfs_fs_info {
        u8 fsid[BTRFS_FSID_SIZE];
+       u8 *new_fsid;
        u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
+       u8 *new_chunk_tree_uuid;
        struct btrfs_root *fs_root;
        struct btrfs_root *extent_root;
        struct btrfs_root *tree_root;
diff --git a/props.c b/props.c
index c7c6752..c9e2bd4 100644 (file)
--- a/props.c
+++ b/props.c
@@ -187,7 +187,6 @@ out:
        return ret;
 }
 
-
 const struct prop_handler prop_handlers[] = {
        {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
         prop_read_only},