btrfs-progs: cross-port btrfs_uuid_tree_add() from kernel
The modification is minimal: - Replace WARN_ON() with UASSERT() - Remove the @trans parameter for btrfs_extend_item() and btrfs_mark_buffer_dirty() As progs version doesn't need a transaction handler. - Remove the btrfs_uuid_tree_add() in mkfs/main.c Signed-off-by: Qu Wenruo <wqu@suse.com>
This commit is contained in:
parent
8efa8092aa
commit
99dc37bcfe
|
@ -30,6 +30,7 @@
|
||||||
#include "kernel-shared/messages.h"
|
#include "kernel-shared/messages.h"
|
||||||
#include "kernel-shared/transaction.h"
|
#include "kernel-shared/transaction.h"
|
||||||
#include "kernel-shared/uuid-tree.h"
|
#include "kernel-shared/uuid-tree.h"
|
||||||
|
#include "kernel-shared/disk-io.h"
|
||||||
#include "common/messages.h"
|
#include "common/messages.h"
|
||||||
#include "common/utils.h"
|
#include "common/utils.h"
|
||||||
|
|
||||||
|
@ -199,3 +200,121 @@ out:
|
||||||
btrfs_free_path(path);
|
btrfs_free_path(path);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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, const 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;
|
||||||
|
|
||||||
|
UASSERT(uuid_root);
|
||||||
|
|
||||||
|
path = btrfs_alloc_path();
|
||||||
|
if (!path) {
|
||||||
|
ret = -ENOMEM;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
btrfs_uuid_to_key(uuid, type, &key);
|
||||||
|
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(eb, slot);
|
||||||
|
offset = btrfs_item_ptr_offset(eb, slot);
|
||||||
|
ret = -ENOENT;
|
||||||
|
|
||||||
|
if (!IS_ALIGNED(item_size, sizeof(u64))) {
|
||||||
|
btrfs_warn(uuid_root->fs_info,
|
||||||
|
"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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const 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;
|
||||||
|
|
||||||
|
ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
|
||||||
|
if (ret != -ENOENT)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
UASSERT(uuid_root);
|
||||||
|
btrfs_uuid_to_key(uuid, type, &key);
|
||||||
|
|
||||||
|
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(path, sizeof(subid_le));
|
||||||
|
eb = path->nodes[0];
|
||||||
|
slot = path->slots[0];
|
||||||
|
offset = btrfs_item_ptr_offset(eb, slot);
|
||||||
|
offset += btrfs_item_size(eb, slot) - sizeof(subid_le);
|
||||||
|
} else {
|
||||||
|
btrfs_warn(fs_info,
|
||||||
|
"insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
|
||||||
|
ret, key.objectid, 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;
|
||||||
|
}
|
||||||
|
|
|
@ -29,5 +29,7 @@ int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
|
||||||
int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
|
int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
|
||||||
u64 subid);
|
u64 subid);
|
||||||
void btrfs_uuid_to_key(const u8 *uuid, u8 type, struct btrfs_key *key);
|
void btrfs_uuid_to_key(const u8 *uuid, u8 type, struct btrfs_key *key);
|
||||||
|
int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 type,
|
||||||
|
u64 subid_cpu);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
56
mkfs/main.c
56
mkfs/main.c
|
@ -736,62 +736,6 @@ static void update_chunk_allocation(struct btrfs_fs_info *fs_info,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid,
|
|
||||||
u8 type, u64 subvol_id_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 subvol_id_le;
|
|
||||||
|
|
||||||
btrfs_uuid_to_key(uuid, type, &key);
|
|
||||||
|
|
||||||
path = btrfs_alloc_path();
|
|
||||||
if (!path) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = btrfs_insert_empty_item(trans, uuid_root, path, &key, sizeof(subvol_id_le));
|
|
||||||
if (ret < 0 && ret != -EEXIST) {
|
|
||||||
warning(
|
|
||||||
"inserting uuid item failed (0x%016llx, 0x%016llx) type %u: %d",
|
|
||||||
key.objectid, key.offset, type, ret);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
/*
|
|
||||||
* ret == -EEXIST case, an item with that type already exists.
|
|
||||||
* Extend the item and store the new subvol_id at the end.
|
|
||||||
*/
|
|
||||||
btrfs_extend_item(path, sizeof(subvol_id_le));
|
|
||||||
eb = path->nodes[0];
|
|
||||||
slot = path->slots[0];
|
|
||||||
offset = btrfs_item_ptr_offset(eb, slot);
|
|
||||||
offset += btrfs_item_size(eb, slot) - sizeof(subvol_id_le);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
subvol_id_le = cpu_to_le64(subvol_id_cpu);
|
|
||||||
write_extent_buffer(eb, &subvol_id_le, offset, sizeof(subvol_id_le));
|
|
||||||
btrfs_mark_buffer_dirty(eb);
|
|
||||||
|
|
||||||
out:
|
|
||||||
btrfs_free_path(path);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int create_uuid_tree(struct btrfs_trans_handle *trans)
|
static int create_uuid_tree(struct btrfs_trans_handle *trans)
|
||||||
{
|
{
|
||||||
struct btrfs_fs_info *fs_info = trans->fs_info;
|
struct btrfs_fs_info *fs_info = trans->fs_info;
|
||||||
|
|
Loading…
Reference in New Issue