Fix incorrectly created uuid tree
v2 cover Fix incorrectly created uuid tree
This patchset can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/uuid_tree_support
Which is based on v4.19.1 tag.
Commit 2a496a5b8b74 ("btrfs-progs: mkfs: precreate the uuid tree") creates uuid tree at mkfs time.
However it doesn't populate uuid tree correctly nor just create an empty root.
It uses create_tree(), which just copies the content of fs root, containing meaningless INODE_ITEM:
v4.15 mkfs (no uuid tree creation) + kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30572544 items 1 free space 16250 generation 7 owner UUID_TREE leaf 30572544 flags 0x1(WRITTEN) backref revision 1
fs uuid 33ecddef-fc86-481a-93ce-846b01c11376
chunk uuid 9e58f646-b0da-43ca-9c7d-8bbe3e120246
item 0 key (0x92457c59d31491be UUID_KEY_SUBVOL 0xef908b5e79aa76a1) itemoff 16275 itemsize 8 subvol_id 5
v4.19.1 mkfs (incorrect one), no kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30507008 items 2 free space 16061 generation 4 owner UUID_TREE leaf 30507008 flags 0x1(WRITTEN) backref revision 1
fs uuid 162f5333-9b5d-4217-877c-ddaeaa79398e
chunk uuid 7bc2c5c6-a6d2-4eec-a513-142b549c6541
item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160 generation 3 transid 0 size 0 nbytes 16384 block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0 sequence 0 flags 0x0(none) item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12 index 0 namelen 2 name: ..
This patchset will fix it by populuating uuid tree properly, as kernel tree checker now doesn't accept empty uuid tree.
w/ this patchset, no kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30507008 items 1 free space 16250 generation 4 owner UUID_TREE leaf 30507008 flags 0x1(WRITTEN) backref revision 1
fs uuid ae53079e-dbbc-409b-a565-5326c7b27731
chunk uuid b5fb1bea-f20d-4af1-80f8-6ca3f0038d67
item 0 key (0x334ba6b032d89c07 UUID_KEY_SUBVOL 0x86cde09cb78bcca0) itemoff 16275 itemsize 8 subvol_id 5
changelog:
v2:
- Don't use btrfs_copy_root() to create empty root. Instead, use btrfs_create_tree() to create an empty tree and fill it from scratch.
- Rename functions:
create_inode_root() -> create_data_reloc_root()
- Better patch order
Qu Wenruo (5):
btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c
btrfs-progs: mkfs: Create data reloc tree from scratch
btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup()
btrfs-progs: uuid: Port btrfs_uuid_tree_add() function
btrfs-progs: Create uuid tree with proper contents
ctree.h | 7 ++- disk-io.c | 94 ++++++++++++++++++++++++++++++- disk-io.h | 5 +- free-space-tree.c | 72 ------------------------ mkfs/main.c | 131 +++++++++++++++++++++++++++++++------------ uuid-tree.c | 138 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 334 insertions(+), 113 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/5 btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c
Just as how kernel uses it.
This provides the basis for later uuid creation.
--- disk-io.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++ disk-io.h | 4 ++- free-space-tree.c | 72 ----------------------------------------------- 3 files changed, 75 insertions(+), 73 deletions(-)
disk-io.c +72 -0
@@ -1723,3 +1723,75 @@ int btrfs_set_buffer_uptodate(struct extent_buffer *eb) {return set_extent_buffer_uptodate (eb); } + +struct btrfs_root *btrfs_create_tree (struct btrfs_trans_handle *trans, +struct btrfs_fs_info *fs_info, +u64 objectid) +{ +struct extent_buffer *leaf; +struct btrfs_root *tree_root = fs_info->tree_root; +struct btrfs_root *root; +struct btrfs_key key; +int ret =0 ; + + root =kzalloc (sizeof (*root),GFP_KERNEL ); +if (!root) +return ERR_PTR (-ENOMEM ); + +btrfs_setup_root (root, fs_info, objectid); + root->root_key.objectid = objectid; + root->root_key.type =BTRFS_ROOT_ITEM_KEY ; + root->root_key.offset =0 ; + + leaf =btrfs_alloc_free_block (trans, root, fs_info->nodesize, objectid, +NULL ,0 ,0 ,0 ); +if (IS_ERR (leaf)) { + ret =PTR_ERR (leaf); + leaf =NULL ; + goto fail; + } + +memset_extent_buffer (leaf,0 ,0 ,sizeof (struct btrfs_header )); +btrfs_set_header_bytenr (leaf, leaf->start); +btrfs_set_header_generation (leaf, trans->transid); +btrfs_set_header_backref_rev (leaf,BTRFS_MIXED_BACKREF_REV ); +btrfs_set_header_owner (leaf, objectid); + root->node = leaf; +write_extent_buffer (leaf, fs_info->fsid,btrfs_header_fsid (),BTRFS_FSID_SIZE ); +write_extent_buffer (leaf, fs_info->chunk_tree_uuid, +btrfs_header_chunk_tree_uuid (leaf), +BTRFS_UUID_SIZE ); +btrfs_mark_buffer_dirty (leaf); + +extent_buffer_get (root->node); + root->commit_root = root->node; + root->track_dirty =1 ; + + root->root_item.flags =0 ; + root->root_item.byte_limit =0 ; +btrfs_set_root_bytenr (&root->root_item, leaf->start); +btrfs_set_root_generation (&root->root_item, trans->transid); +btrfs_set_root_level (&root->root_item,0 ); +btrfs_set_root_refs (&root->root_item,1 ); +btrfs_set_root_used (&root->root_item, leaf->len); +btrfs_set_root_last_snapshot (&root->root_item,0 ); +btrfs_set_root_dirid (&root->root_item,0 ); +memset (root->root_item.uuid,0 ,BTRFS_UUID_SIZE ); + root->root_item.drop_level =0 ; + + key.objectid = objectid; + key.type =BTRFS_ROOT_ITEM_KEY ; + key.offset =0 ; + ret =btrfs_insert_root (trans, tree_root, &key, &root->root_item); +if (ret) + goto fail; + +return root; + +fail: +if (leaf) +free_extent_buffer (leaf); + +kfree (root); +return ERR_PTR (ret); +}
disk-io.h +3 -1
@@ -197,5 +197,7 @@ int write_tree_block(struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info, struct extent_buffer *eb);int write_and_map_eb (struct btrfs_fs_info *fs_info,struct extent_buffer *eb); - +struct btrfs_root *btrfs_create_tree (struct btrfs_trans_handle *trans, +struct btrfs_fs_info *fs_info, +u64 objectid);#endif
free-space-tree.c +0 -72
78 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 2/5 btrfs-progs: mkfs: Create data reloc tree from scratch
In reply to Qu WenruoFor data reloc tree creation, we copy its content from fs tree just for its INODE_ITEM, INODE_REF and dirid.
This hides the detail and is not obvious for why we're copying from fs root.
This patch will create data reloc tree from scratch by:
- Create root
Including root item and new tree root.
- Change dirid to BTRFS_FIRST_FREE_OBJECTID
- Insert root INODE_ITEM and INODE_REF
--- disk-io.c | 3 +-- disk-io.h | 1 + mkfs/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-)
disk-io.c +1 -2
@@ -638,8 +638,7 @@ static int btrfs_fs_roots_compare_objectids(struct rb_node *node,return 0 ; } -static int btrfs_fs_roots_compare_roots (struct rb_node *node1, -struct rb_node *node2) +int btrfs_fs_roots_compare_roots (struct rb_node *node1,struct rb_node *node2) {struct btrfs_root *root;
disk-io.h +1 -0
@@ -197,6 +197,7 @@ int write_tree_block(struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info, struct extent_buffer *eb);int write_and_map_eb (struct btrfs_fs_info *fs_info,struct extent_buffer *eb); +int btrfs_fs_roots_compare_roots (struct rb_node *node1,struct rb_node *node2);struct btrfs_root *btrfs_create_tree (struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info,u64 objectid);
mkfs/main.c +69 -1
88 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 3/5 btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup()
In reply to Qu WenruoAlthough we have btrfs_uuid_tree_lookup_any(), it's an online function utilizing tree search ioctl, not an offline search function.
This patch will port kernel btrfs_uuid_tree_lookup() into btrfs-progs for later proper uuid tree initialization.
--- uuid-tree.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-)
uuid-tree.c +71 -2
@@ -23,6 +23,7 @@#include "transaction.h" #include "disk-io.h" #include "print-tree.h" +#include "utils.h" static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid,
@@ -32,8 +33,11 @@ static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, *key_offset =get_unaligned_le64 (uuid +sizeof (u64)); } - -/* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */ +/* +* Search uuid tree of a *MOUNTED* btrfs (online) +* +* return -ENOENT for !found, < 0 for errors, or 0 if an item was found +*/ static int btrfs_uuid_tree_lookup_any (int fd,const u8 *uuid,u8 type,u64 *subid) {
@@ -103,3 +107,68 @@ int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,BTRFS_UUID_KEY_RECEIVED_SUBVOL , subvol_id); } + +/* +* Search uuid tree of an *UNMOUNTED* btrfs (offline) +* +* return -ENOENT for !found, < 0 for errors, or 0 if an item was found +*/ +static int btrfs_uuid_tree_lookup (struct btrfs_root *uuid_root,u8 *uuid, +u8 type,u64 subid) +{ +int ret; +struct btrfs_path *path =NULL ; +struct extent_buffer *eb; +int slot; +u32 item_size; +unsigned long offset; +struct btrfs_key key; + +if (!uuid_root) { + ret = -ENOENT ; + goto out; + } + + path =btrfs_alloc_path (); +if (!path) { + ret = -ENOMEM ; + goto out; + } + +btrfs_uuid_to_key (uuid, &key.objectid, &key.offset); + key.type = type; + ret =btrfs_search_slot (NULL , uuid_root, &key, path,0 ,0 ); +if (ret <0 ) { + goto out; + }else if (ret >0 ) { + ret = -ENOENT ; + goto out; + } + + eb = path->nodes[0 ]; + slot = path->slots[0 ]; + item_size =btrfs_item_size_nr (eb, slot); + offset =btrfs_item_ptr_offset (eb, slot); + ret = -ENOENT ; + +if (!IS_ALIGNED (item_size,sizeof (u64))) { +warning ("uuid item with illegal size %lu!" , + (unsigned long )item_size); + goto out; + } +while (item_size) { +__le64 data; + +read_extent_buffer (eb, &data, offset,sizeof (data)); +if (le64_to_cpu (data) == subid) { + ret =0 ; +break ; + } + offset +=sizeof (data); + item_size -=sizeof (data); + } + +out: +btrfs_free_path (path); +return ret; +}
Signature · 1 line
2.20.1
PATCH v2 4/5 btrfs-progs: uuid: Port btrfs_uuid_tree_add() function
In reply to Qu WenruoThis function provide the offline ability to add new uuid tree entry.
Also port fs_info->uuid and its initialization and cleanup code to support uuid tree.
--- ctree.h | 7 +++++- disk-io.c | 19 +++++++++++++++- uuid-tree.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-)
ctree.h +6 -1
@@ -1101,6 +1101,7 @@ struct btrfs_fs_info {struct btrfs_root *csum_root;struct btrfs_root *quota_root;struct btrfs_root *free_space_root; +struct btrfs_root *uuid_root;struct rb_root fs_root_tree;
@@ -2774,11 +2775,15 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u64 isize); -/* uuid-tree.c */ +/* uuid-tree.c, mounted fs (online) interface */ int btrfs_lookup_uuid_subvol_item (int fd,const u8 *uuid,u64 *subvol_id);int btrfs_lookup_uuid_received_subvol_item (int fd,const u8 *uuid,u64 *subvol_id); +/* uuid-tree.c, unmounted fs (offline) interface */ +int btrfs_uuid_tree_add (struct btrfs_trans_handle *trans,u8 *uuid,u8 type, +u64 subid_cpu); +static inline int is_fstree (u64 rootid) { if (rootid == BTRFS_FS_TREE_OBJECTID ||
disk-io.c +18 -1
@@ -664,6 +664,9 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,return fs_info->dev_root;if (location->objectid ==BTRFS_CSUM_TREE_OBJECTID )return fs_info->csum_root; +if (location->objectid ==BTRFS_UUID_TREE_OBJECTID ) +return fs_info->uuid_root ? fs_info->uuid_root : +ERR_PTR (-ENOENT );if (location->objectid ==BTRFS_QUOTA_TREE_OBJECTID )return fs_info->quota_enabled ? fs_info->quota_root :ERR_PTR (-ENOENT );
@@ -700,6 +703,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)free (fs_info->dev_root);free (fs_info->csum_root);free (fs_info->free_space_root); +free (fs_info->uuid_root);free (fs_info->super_copy);free (fs_info->log_root_tree);free (fs_info);
@@ -720,12 +724,14 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr) fs_info->csum_root =calloc (1 ,sizeof (struct btrfs_root )); fs_info->quota_root =calloc (1 ,sizeof (struct btrfs_root )); fs_info->free_space_root =calloc (1 ,sizeof (struct btrfs_root )); + fs_info->uuid_root =calloc (1 ,sizeof (struct btrfs_root )); fs_info->super_copy =calloc (1 ,BTRFS_SUPER_INFO_SIZE );if (!fs_info->tree_root || !fs_info->extent_root || !fs_info->chunk_root || !fs_info->dev_root || !fs_info->csum_root || !fs_info->quota_root || - !fs_info->free_space_root || !fs_info->super_copy) + !fs_info->free_space_root || !fs_info->uuid_root || + !fs_info->super_copy) goto free_all;extent_io_tree_init (&fs_info->extent_cache);
@@ -895,6 +901,15 @@ int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,return ret; fs_info->csum_root->track_dirty =1 ; + ret =find_and_setup_root (root, fs_info,BTRFS_UUID_TREE_OBJECTID , + fs_info->uuid_root); +if (ret) { +free (fs_info->uuid_root); + fs_info->uuid_root =NULL ; + }else { + fs_info->uuid_root->track_dirty =1 ; + } + ret =find_and_setup_root (root, fs_info,BTRFS_QUOTA_TREE_OBJECTID , fs_info->quota_root);if (ret) {
@@ -963,6 +978,8 @@ void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)free_extent_buffer (fs_info->log_root_tree->node);if (fs_info->chunk_root)free_extent_buffer (fs_info->chunk_root->node); +if (fs_info->uuid_root) +free_extent_buffer (fs_info->uuid_root->node); }static void free_map_lookup (struct cache_extent *ce)
uuid-tree.c +65 -0
68 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 5/5 btrfs-progs: Create uuid tree with proper contents
In reply to Qu WenruoCommit 2a496a5b8b74 ("btrfs-progs: mkfs: precreate the uuid tree") creates uuid tree at mkfs time.
However it doesn't populate uuid tree correctly nor just create an empty root.
It uses create_tree(), which just copies the content of fs root, containing meaningless INODE_ITEM:
v4.15 mkfs (no uuid tree creation) + kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30572544 items 1 free space 16250 generation 7 owner UUID_TREE leaf 30572544 flags 0x1(WRITTEN) backref revision 1
fs uuid 33ecddef-fc86-481a-93ce-846b01c11376
chunk uuid 9e58f646-b0da-43ca-9c7d-8bbe3e120246
item 0 key (0x92457c59d31491be UUID_KEY_SUBVOL 0xef908b5e79aa76a1) itemoff 16275 itemsize 8 subvol_id 5
v4.19.1 mkfs (incorrect one), no kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30507008 items 2 free space 16061 generation 4 owner UUID_TREE leaf 30507008 flags 0x1(WRITTEN) backref revision 1
fs uuid 162f5333-9b5d-4217-877c-ddaeaa79398e
chunk uuid 7bc2c5c6-a6d2-4eec-a513-142b549c6541
item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160 generation 3 transid 0 size 0 nbytes 16384 block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0 sequence 0 flags 0x0(none) item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12 index 0 namelen 2 name: ..
This patchset will fix it by populuating uuid tree properly:
(NOTE: due to tree-checker, kernel doesn't accept empty uuid tree, so we
can only fix it by populating uuid tree correctly)
w/ this patchset, no kernel mount:
uuid tree key (UUID_TREE ROOT_ITEM 0)
leaf 30507008 items 1 free space 16250 generation 4 owner UUID_TREE leaf 30507008 flags 0x1(WRITTEN) backref revision 1
fs uuid ae53079e-dbbc-409b-a565-5326c7b27731
chunk uuid b5fb1bea-f20d-4af1-80f8-6ca3f0038d67
item 0 key (0x334ba6b032d89c07 UUID_KEY_SUBVOL 0x86cde09cb78bcca0) itemoff 16275 itemsize 8 subvol_id 5
For kernel, except tree-checker needs an non-empty uuid tree, both all above behavior won't cause problem, but it's always better to keep a good standardized behavior.
--- mkfs/main.c | 61 ++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 33 deletions(-)
mkfs/main.c +28 -33
@@ -310,38 +310,6 @@ static int create_raid_groups(struct btrfs_trans_handle *trans,return ret; } -static int create_tree (struct btrfs_trans_handle *trans, -struct btrfs_root *root,u64 objectid) -{ -struct btrfs_key location; -struct btrfs_root_item root_item; -struct extent_buffer *tmp; -u8 uuid[BTRFS_UUID_SIZE ] = {0 }; -int ret; - - ret =btrfs_copy_root (trans, root, root->node, &tmp, objectid); -if (ret) -return ret; - -memcpy (&root_item, &root->root_item,sizeof (root_item)); -btrfs_set_root_bytenr (&root_item, tmp->start); -btrfs_set_root_level (&root_item,btrfs_header_level (tmp)); -btrfs_set_root_generation (&root_item, trans->transid); -/* clear uuid and o/ctime of source tree */ -memcpy (root_item.uuid, uuid,BTRFS_UUID_SIZE ); -btrfs_set_stack_timespec_sec (&root_item.otime,0 ); -btrfs_set_stack_timespec_sec (&root_item.ctime,0 ); -free_extent_buffer (tmp); - - location.objectid = objectid; - location.type =BTRFS_ROOT_ITEM_KEY ; - location.offset =0 ; - ret =btrfs_insert_root (trans, root->fs_info->tree_root, - &location, &root_item); - -return ret; -} -static void print_usage (int ret) {printf ("Usage: mkfs.btrfs [options] dev [ dev ... ]\n" );
@@ -777,6 +745,33 @@ out:return ret; } +static int create_uuid_tree (struct btrfs_trans_handle *trans) +{ +struct btrfs_fs_info *fs_info = trans->fs_info; +struct btrfs_root *root; +int ret; + +ASSERT (fs_info->uuid_root ==NULL ); + root =btrfs_create_tree (trans, fs_info,BTRFS_UUID_TREE_OBJECTID ); +if (IS_ERR (root)) { + ret =PTR_ERR (root); + goto out; + } + +add_root_to_dirty_list (root); + fs_info->uuid_root = root; + ret =btrfs_uuid_tree_add (trans, fs_info->fs_root->root_item.uuid, +BTRFS_UUID_KEY_SUBVOL , + fs_info->fs_root->root_key.objectid); +if (ret <0 ) + goto out; +return 0 ; + +out: +btrfs_abort_transaction (trans, ret); +return ret; +} +int main (int argc,char **argv) {char *file;
@@ -1277,7 +1272,7 @@ raid_groups: goto out; } - ret =create_tree (trans, root,BTRFS_UUID_TREE_OBJECTID ); + ret =create_uuid_tree (trans);if (ret)warning ("unable to create uuid tree, will be created after mount: %d" , ret);
Signature · 1 line
2.20.1
Quoted 10 lines · Qu Wenruo
This patchset can be fetched from github:
https://github.com/adam900710/btrfs-progs/tree/uuid_tree_support
Which is based on v4.19.1 tag.Qu Wenruo (5):
btrfs-progs: Exportbtrfs_create_tree()and move it todisk-io.c
btrfs-progs: mkfs: Create data reloc tree from scratch
btrfs-progs: uuid: Port kernelbtrfs_uuid_tree_lookup()
btrfs-progs: uuid: Portbtrfs_uuid_tree_add()function
btrfs-progs: Create uuid tree with proper contents
Added to devel, thanks.