* [PATCH v2 0/5] Fix incorrectly created uuid tree
@ 2019-01-03 7:32 Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 1/5] btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c Qu Wenruo
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
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(-)
--
2.20.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/5] btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
@ 2019-01-03 7:32 ` Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 2/5] btrfs-progs: mkfs: Create data reloc tree from scratch Qu Wenruo
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
Just as how kernel uses it.
This provides the basis for later uuid creation.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
disk-io.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++
disk-io.h | 4 ++-
free-space-tree.c | 72 -----------------------------------------------
3 files changed, 75 insertions(+), 73 deletions(-)
diff --git a/disk-io.c b/disk-io.c
index 5fafa144c0d3..0668031b974b 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -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);
+}
diff --git a/disk-io.h b/disk-io.h
index 05cbbce6ea4a..c4a6a020dd88 100644
--- a/disk-io.h
+++ b/disk-io.h
@@ -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
diff --git a/free-space-tree.c b/free-space-tree.c
index 6ef57928f1a9..5089fef0652c 100644
--- a/free-space-tree.c
+++ b/free-space-tree.c
@@ -1420,78 +1420,6 @@ out:
return ret;
}
-static 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);
-}
-
#define btrfs_set_fs_compat_ro(__fs_info, opt) \
__btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/5] btrfs-progs: mkfs: Create data reloc tree from scratch
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 1/5] btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c Qu Wenruo
@ 2019-01-03 7:32 ` Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 3/5] btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup() Qu Wenruo
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
For 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
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
disk-io.c | 3 +--
disk-io.h | 1 +
mkfs/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 71 insertions(+), 3 deletions(-)
diff --git a/disk-io.c b/disk-io.c
index 0668031b974b..e3a3c5fbe492 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -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;
diff --git a/disk-io.h b/disk-io.h
index c4a6a020dd88..4138dfbdf8a6 100644
--- a/disk-io.h
+++ b/disk-io.h
@@ -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);
diff --git a/mkfs/main.c b/mkfs/main.c
index b6748f7fe853..0a13e8d83e4d 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -39,6 +39,7 @@
#include "utils.h"
#include "list_sort.h"
#include "help.h"
+#include "rbtree-utils.h"
#include "mkfs/common.h"
#include "mkfs/rootdir.h"
#include "fsfeatures.h"
@@ -709,6 +710,73 @@ static void update_chunk_allocation(struct btrfs_fs_info *fs_info,
}
}
+static int create_data_reloc_tree(struct btrfs_trans_handle *trans)
+{
+ struct btrfs_fs_info *fs_info = trans->fs_info;
+ struct btrfs_inode_item *inode;
+ struct btrfs_root *root;
+ struct btrfs_path path;
+ struct btrfs_key key;
+ u64 ino = BTRFS_FIRST_FREE_OBJECTID;
+ char *name = "..";
+ int ret;
+
+ root = btrfs_create_tree(trans, fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
+ if (IS_ERR(root)) {
+ ret = PTR_ERR(root);
+ goto out;
+ }
+ /* Update dirid as created tree has default dirid 0 */
+ btrfs_set_root_dirid(&root->root_item, ino);
+ ret = btrfs_update_root(trans, fs_info->tree_root, &root->root_key,
+ &root->root_item);
+ if (ret < 0)
+ goto out;
+
+ /* Cache this tree so it can be cleaned up at close_ctree() */
+ ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
+ btrfs_fs_roots_compare_roots);
+ if (ret < 0)
+ goto out;
+
+ /* Insert INODE_ITEM */
+ ret = btrfs_new_inode(trans, root, ino, 0755 | S_IFDIR);
+ if (ret < 0)
+ goto out;
+
+ /* then INODE_REF */
+ ret = btrfs_insert_inode_ref(trans, root, name, strlen(name), ino, ino,
+ 0);
+ if (ret < 0)
+ goto out;
+
+ /* Update nlink of that inode item */
+ key.objectid = ino;
+ key.type = BTRFS_INODE_ITEM_KEY;
+ key.offset = 0;
+ btrfs_init_path(&path);
+
+ ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+ if (ret > 0) {
+ ret = -ENOENT;
+ btrfs_release_path(&path);
+ goto out;
+ }
+ if (ret < 0) {
+ btrfs_release_path(&path);
+ goto out;
+ }
+ inode = btrfs_item_ptr(path.nodes[0], path.slots[0],
+ struct btrfs_inode_item);
+ btrfs_set_inode_nlink(path.nodes[0], inode, 1);
+ btrfs_mark_buffer_dirty(path.nodes[0]);
+ btrfs_release_path(&path);
+ return 0;
+out:
+ btrfs_abort_transaction(trans, ret);
+ return ret;
+}
+
int main(int argc, char **argv)
{
char *file;
@@ -1203,7 +1271,7 @@ raid_groups:
goto out;
}
- ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
+ ret = create_data_reloc_tree(trans);
if (ret) {
error("unable to create data reloc tree: %d", ret);
goto out;
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/5] btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup()
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 1/5] btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 2/5] btrfs-progs: mkfs: Create data reloc tree from scratch Qu Wenruo
@ 2019-01-03 7:32 ` Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 4/5] btrfs-progs: uuid: Port btrfs_uuid_tree_add() function Qu Wenruo
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
Although 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.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
uuid-tree.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 71 insertions(+), 2 deletions(-)
diff --git a/uuid-tree.c b/uuid-tree.c
index 320eb67e1404..e0bc51a5b8dc 100644
--- a/uuid-tree.c
+++ b/uuid-tree.c
@@ -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;
+}
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 4/5] btrfs-progs: uuid: Port btrfs_uuid_tree_add() function
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
` (2 preceding siblings ...)
2019-01-03 7:32 ` [PATCH v2 3/5] btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup() Qu Wenruo
@ 2019-01-03 7:32 ` Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 5/5] btrfs-progs: Create uuid tree with proper contents Qu Wenruo
2019-01-10 21:04 ` [PATCH v2 0/5] Fix incorrectly created uuid tree David Sterba
5 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
This 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.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
ctree.h | 7 +++++-
disk-io.c | 19 +++++++++++++++-
uuid-tree.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/ctree.h b/ctree.h
index f9c49d6041c1..7b9e3f1cef6e 100644
--- a/ctree.h
+++ b/ctree.h
@@ -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 ||
diff --git a/disk-io.c b/disk-io.c
index e3a3c5fbe492..32a239d0444d 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -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)
diff --git a/uuid-tree.c b/uuid-tree.c
index e0bc51a5b8dc..e978b2d599ac 100644
--- a/uuid-tree.c
+++ b/uuid-tree.c
@@ -172,3 +172,68 @@ out:
btrfs_free_path(path);
return ret;
}
+
+int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
+ u64 subid_cpu)
+{
+ struct btrfs_fs_info *fs_info = trans->fs_info;
+ struct btrfs_root *uuid_root = fs_info->uuid_root;
+ int ret;
+ struct btrfs_path *path = NULL;
+ struct btrfs_key key;
+ struct extent_buffer *eb;
+ int slot;
+ unsigned long offset;
+ __le64 subid_le;
+
+ if (!uuid_root) {
+ warning("%s: uuid root is not initialized", __func__);
+ return -EINVAL;
+ }
+
+ ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
+ if (ret != -ENOENT)
+ return ret;
+
+ key.type = type;
+ btrfs_uuid_to_key(uuid, &key.objectid, &key.offset);
+
+ path = btrfs_alloc_path();
+ if (!path) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
+ sizeof(subid_le));
+ if (ret >= 0) {
+ /* Add an item for the type for the first time */
+ eb = path->nodes[0];
+ slot = path->slots[0];
+ offset = btrfs_item_ptr_offset(eb, slot);
+ } else if (ret == -EEXIST) {
+ /*
+ * An item with that type already exists.
+ * Extend the item and store the new subid at the end.
+ */
+ btrfs_extend_item(uuid_root, path, sizeof(subid_le));
+ eb = path->nodes[0];
+ slot = path->slots[0];
+ offset = btrfs_item_ptr_offset(eb, slot);
+ offset += btrfs_item_size_nr(eb, slot) - sizeof(subid_le);
+ } else if (ret < 0) {
+ warning("insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
+ ret, (unsigned long long)key.objectid,
+ (unsigned long long)key.offset, type);
+ goto out;
+ }
+
+ ret = 0;
+ subid_le = cpu_to_le64(subid_cpu);
+ write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
+ btrfs_mark_buffer_dirty(eb);
+
+out:
+ btrfs_free_path(path);
+ return ret;
+}
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 5/5] btrfs-progs: Create uuid tree with proper contents
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
` (3 preceding siblings ...)
2019-01-03 7:32 ` [PATCH v2 4/5] btrfs-progs: uuid: Port btrfs_uuid_tree_add() function Qu Wenruo
@ 2019-01-03 7:32 ` Qu Wenruo
2019-01-10 21:04 ` [PATCH v2 0/5] Fix incorrectly created uuid tree David Sterba
5 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2019-01-03 7:32 UTC (permalink / raw)
To: linux-btrfs
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:
(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.
Fixes: 2a496a5b8b74 ("btrfs-progs: mkfs: precreate the uuid tree")
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
mkfs/main.c | 61 ++++++++++++++++++++++++-----------------------------
1 file changed, 28 insertions(+), 33 deletions(-)
diff --git a/mkfs/main.c b/mkfs/main.c
index 0a13e8d83e4d..16b3432eb4c6 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -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);
--
2.20.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/5] Fix incorrectly created uuid tree
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
` (4 preceding siblings ...)
2019-01-03 7:32 ` [PATCH v2 5/5] btrfs-progs: Create uuid tree with proper contents Qu Wenruo
@ 2019-01-10 21:04 ` David Sterba
5 siblings, 0 replies; 7+ messages in thread
From: David Sterba @ 2019-01-10 21:04 UTC (permalink / raw)
To: Qu Wenruo; +Cc: linux-btrfs
On Thu, Jan 03, 2019 at 03:32:16PM +0800, Qu Wenruo wrote:
> 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: 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
Added to devel, thanks.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2019-01-10 21:04 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-03 7:32 [PATCH v2 0/5] Fix incorrectly created uuid tree Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 1/5] btrfs-progs: Export btrfs_create_tree() and move it to disk-io.c Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 2/5] btrfs-progs: mkfs: Create data reloc tree from scratch Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 3/5] btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup() Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 4/5] btrfs-progs: uuid: Port btrfs_uuid_tree_add() function Qu Wenruo
2019-01-03 7:32 ` [PATCH v2 5/5] btrfs-progs: Create uuid tree with proper contents Qu Wenruo
2019-01-10 21:04 ` [PATCH v2 0/5] Fix incorrectly created uuid tree David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).