2013-06-26 15:17:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) STRATO AG 2013. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
2021-09-06 14:23:14 +00:00
|
|
|
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kerncompat.h"
|
2013-06-26 15:17:56 +00:00
|
|
|
#include <sys/ioctl.h>
|
2023-08-28 20:12:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "kernel-lib/bitops.h"
|
|
|
|
#include "kernel-shared/accessors.h"
|
|
|
|
#include "kernel-shared/extent_io.h"
|
|
|
|
#include "kernel-shared/uapi/btrfs.h"
|
|
|
|
#include "kernel-shared/uapi/btrfs_tree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2023-06-27 14:15:35 +00:00
|
|
|
#include "kernel-shared/messages.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/transaction.h"
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "common/messages.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/utils.h"
|
2013-06-26 15:17:56 +00:00
|
|
|
|
2019-06-13 18:25:26 +00:00
|
|
|
void btrfs_uuid_to_key(const u8 *uuid, struct btrfs_key *key)
|
2013-06-26 15:17:56 +00:00
|
|
|
{
|
2019-06-13 18:25:26 +00:00
|
|
|
u64 tmp;
|
|
|
|
|
|
|
|
tmp = get_unaligned_le64(uuid);
|
|
|
|
put_unaligned_64(tmp, &key->objectid);
|
|
|
|
tmp = get_unaligned_le64(uuid + sizeof(u64));
|
|
|
|
put_unaligned_64(tmp, &key->offset);
|
2013-06-26 15:17:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 07:32:19 +00:00
|
|
|
/*
|
|
|
|
* Search uuid tree - mounted
|
|
|
|
*
|
|
|
|
* return -ENOENT for !found, < 0 for errors, or 0 if an item was found
|
|
|
|
*/
|
2013-06-26 15:17:56 +00:00
|
|
|
static int btrfs_uuid_tree_lookup_any(int fd, const u8 *uuid, u8 type,
|
|
|
|
u64 *subid)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct btrfs_ioctl_search_args search_arg;
|
|
|
|
struct btrfs_ioctl_search_header *search_header;
|
|
|
|
u32 item_size;
|
2013-08-14 23:16:39 +00:00
|
|
|
__le64 lesubid;
|
2019-06-13 18:25:26 +00:00
|
|
|
struct btrfs_key key;
|
2013-06-26 15:17:56 +00:00
|
|
|
|
2019-06-13 18:25:26 +00:00
|
|
|
key.type = type;
|
|
|
|
btrfs_uuid_to_key(uuid, &key);
|
2013-06-26 15:17:56 +00:00
|
|
|
|
|
|
|
memset(&search_arg, 0, sizeof(search_arg));
|
|
|
|
search_arg.key.tree_id = BTRFS_UUID_TREE_OBJECTID;
|
2019-06-13 18:25:26 +00:00
|
|
|
search_arg.key.min_objectid = key.objectid;
|
|
|
|
search_arg.key.max_objectid = key.objectid;
|
2013-06-26 15:17:56 +00:00
|
|
|
search_arg.key.min_type = type;
|
|
|
|
search_arg.key.max_type = type;
|
2019-06-13 18:25:26 +00:00
|
|
|
search_arg.key.min_offset = key.offset;
|
|
|
|
search_arg.key.max_offset = key.offset;
|
2013-06-26 15:17:56 +00:00
|
|
|
search_arg.key.max_transid = (u64)-1;
|
|
|
|
search_arg.key.nr_items = 1;
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr,
|
2018-01-07 21:54:21 +00:00
|
|
|
"ioctl(BTRFS_IOC_TREE_SEARCH, uuid, key %016llx, UUID_KEY, %016llx) ret=%d, error: %m\n",
|
2019-06-13 18:25:26 +00:00
|
|
|
(unsigned long long)key.objectid,
|
|
|
|
(unsigned long long)key.offset, ret);
|
2013-06-26 15:17:56 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search_arg.key.nr_items < 1) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
search_header = (struct btrfs_ioctl_search_header *)(search_arg.buf);
|
2016-05-03 14:20:52 +00:00
|
|
|
item_size = btrfs_search_header_len(search_header);
|
2013-06-26 15:17:56 +00:00
|
|
|
if ((item_size & (sizeof(u64) - 1)) || item_size == 0) {
|
|
|
|
printf("btrfs: uuid item with illegal size %lu!\n",
|
|
|
|
(unsigned long)item_size);
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return first stored id */
|
2013-08-14 23:16:39 +00:00
|
|
|
memcpy(&lesubid, search_header + 1, sizeof(lesubid));
|
|
|
|
*subid = le64_to_cpu(lesubid);
|
2013-06-26 15:17:56 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_lookup_uuid_subvol_item(int fd, const u8 *uuid, u64 *subvol_id)
|
|
|
|
{
|
|
|
|
return btrfs_uuid_tree_lookup_any(fd, uuid, BTRFS_UUID_KEY_SUBVOL,
|
|
|
|
subvol_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
|
|
|
|
u64 *subvol_id)
|
|
|
|
{
|
|
|
|
return btrfs_uuid_tree_lookup_any(fd, uuid,
|
|
|
|
BTRFS_UUID_KEY_RECEIVED_SUBVOL,
|
|
|
|
subvol_id);
|
|
|
|
}
|
2021-09-14 09:05:55 +00:00
|
|
|
|
|
|
|
int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
|
2023-06-27 14:15:35 +00:00
|
|
|
u64 subid)
|
2021-09-14 09:05:55 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
u32 item_size;
|
|
|
|
unsigned long move_dst;
|
|
|
|
unsigned long move_src;
|
|
|
|
unsigned long move_len;
|
|
|
|
|
|
|
|
if (!uuid_root) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
btrfs_uuid_to_key(uuid, &key);
|
|
|
|
key.type = type;
|
|
|
|
|
|
|
|
path = btrfs_alloc_path();
|
|
|
|
if (!path) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
|
|
|
|
if (ret < 0) {
|
2023-06-27 14:15:35 +00:00
|
|
|
btrfs_warn(fs_info, "error %d while searching for uuid item!",
|
|
|
|
ret);
|
2021-09-14 09:05:55 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
eb = path->nodes[0];
|
|
|
|
slot = path->slots[0];
|
|
|
|
offset = btrfs_item_ptr_offset(eb, slot);
|
2022-02-22 22:26:20 +00:00
|
|
|
item_size = btrfs_item_size(eb, slot);
|
2021-09-14 09:05:55 +00:00
|
|
|
if (!IS_ALIGNED(item_size, sizeof(u64))) {
|
2023-06-27 14:15:35 +00:00
|
|
|
btrfs_warn(fs_info, "uuid item with illegal size %lu!",
|
|
|
|
(unsigned long)item_size);
|
2021-09-14 09:05:55 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
while (item_size) {
|
|
|
|
__le64 read_subid;
|
|
|
|
|
|
|
|
read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
|
|
|
|
if (le64_to_cpu(read_subid) == subid)
|
|
|
|
break;
|
|
|
|
offset += sizeof(read_subid);
|
|
|
|
item_size -= sizeof(read_subid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item_size) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:26:20 +00:00
|
|
|
item_size = btrfs_item_size(eb, slot);
|
2021-09-14 09:05:55 +00:00
|
|
|
if (item_size == sizeof(subid)) {
|
|
|
|
ret = btrfs_del_item(trans, uuid_root, path);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
move_dst = offset;
|
|
|
|
move_src = offset + sizeof(subid);
|
|
|
|
move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
|
|
|
|
memmove_extent_buffer(eb, move_dst, move_src, move_len);
|
|
|
|
btrfs_truncate_item(path, item_size - sizeof(subid), 1);
|
|
|
|
|
|
|
|
out:
|
|
|
|
btrfs_free_path(path);
|
|
|
|
return ret;
|
|
|
|
}
|