2012-07-25 21:08:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Alexander Block. 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.
|
|
|
|
*/
|
|
|
|
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kerncompat.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include <sys/ioctl.h>
|
2013-06-26 15:17:57 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2015-06-12 11:18:44 +00:00
|
|
|
#include <limits.h>
|
2015-06-12 15:06:48 +00:00
|
|
|
#include <errno.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kernel-shared/accessors.h"
|
|
|
|
#include "kernel-shared/uapi/btrfs_tree.h"
|
2023-04-19 21:17:12 +00:00
|
|
|
#include "kernel-shared/uapi/btrfs.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2024-07-26 08:40:50 +00:00
|
|
|
#include "kernel-shared/uuid-tree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "common/send-utils.h"
|
2021-09-08 09:16:43 +00:00
|
|
|
#include "common/messages.h"
|
|
|
|
#include "common/utils.h"
|
2024-03-13 22:56:11 +00:00
|
|
|
#include "common/tree-search.h"
|
2012-07-25 21:08:25 +00:00
|
|
|
|
2013-04-23 10:18:55 +00:00
|
|
|
static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
|
|
|
|
u64 subvol_id);
|
|
|
|
|
2013-06-26 15:17:57 +00:00
|
|
|
static int btrfs_get_root_id_by_sub_path(int mnt_fd, const char *sub_path,
|
|
|
|
u64 *root_id)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int subvol_fd;
|
|
|
|
|
|
|
|
subvol_fd = openat(mnt_fd, sub_path, O_RDONLY);
|
|
|
|
if (subvol_fd < 0) {
|
|
|
|
ret = -errno;
|
2022-09-16 17:29:25 +00:00
|
|
|
error("open %s failed: %m", sub_path);
|
2013-06-26 15:17:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:16:43 +00:00
|
|
|
ret = lookup_path_rootid(subvol_fd, root_id);
|
|
|
|
if (ret) {
|
|
|
|
errno = -ret;
|
|
|
|
error("cannot resolve rootid for path: %m");
|
|
|
|
}
|
2013-06-26 15:17:57 +00:00
|
|
|
close(subvol_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int btrfs_read_root_item_raw(int mnt_fd, u64 root_id, size_t buf_len,
|
|
|
|
u32 *read_len, void *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
2024-03-13 22:56:11 +00:00
|
|
|
struct btrfs_tree_search_args args;
|
|
|
|
struct btrfs_ioctl_search_key *sk;
|
2013-06-26 15:17:57 +00:00
|
|
|
unsigned long off = 0;
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*read_len = 0;
|
|
|
|
memset(&args, 0, sizeof(args));
|
2024-03-13 22:56:11 +00:00
|
|
|
sk = btrfs_tree_search_sk(&args);
|
2013-06-26 15:17:57 +00:00
|
|
|
sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there may be more than one ROOT_ITEM key if there are
|
|
|
|
* snapshots pending deletion, we have to loop through
|
|
|
|
* them.
|
|
|
|
*/
|
|
|
|
sk->min_objectid = root_id;
|
2024-04-19 23:27:41 +00:00
|
|
|
sk->min_type = BTRFS_ROOT_ITEM_KEY;
|
2013-06-26 15:17:57 +00:00
|
|
|
sk->max_objectid = root_id;
|
|
|
|
sk->max_type = BTRFS_ROOT_ITEM_KEY;
|
|
|
|
sk->max_offset = (u64)-1;
|
|
|
|
sk->max_transid = (u64)-1;
|
|
|
|
sk->nr_items = 4096;
|
|
|
|
|
|
|
|
while (1) {
|
2024-03-13 22:56:11 +00:00
|
|
|
ret = btrfs_tree_search_ioctl(mnt_fd, &args);
|
2013-06-26 15:17:57 +00:00
|
|
|
if (ret < 0) {
|
2022-09-16 17:29:25 +00:00
|
|
|
error("can't perform the search: %m");
|
2013-06-26 15:17:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* the ioctl returns the number of item it found in nr_items */
|
|
|
|
if (sk->nr_items == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
for (i = 0; i < sk->nr_items; i++) {
|
|
|
|
struct btrfs_root_item *item;
|
2024-03-14 14:58:56 +00:00
|
|
|
struct btrfs_ioctl_search_header sh;
|
2013-06-26 15:17:57 +00:00
|
|
|
|
2024-03-14 14:58:56 +00:00
|
|
|
memcpy(&sh, btrfs_tree_search_data(&args, off), sizeof(sh));
|
|
|
|
off += sizeof(sh);
|
2024-03-13 22:56:11 +00:00
|
|
|
|
|
|
|
item = btrfs_tree_search_data(&args, off);
|
2024-03-14 14:58:56 +00:00
|
|
|
off += sh.len;
|
2013-06-26 15:17:57 +00:00
|
|
|
|
2024-03-14 14:58:56 +00:00
|
|
|
sk->min_objectid = sh.objectid;
|
|
|
|
sk->min_type = sh.type;
|
|
|
|
sk->min_offset = sh.offset;
|
2013-06-26 15:17:57 +00:00
|
|
|
|
2024-03-14 14:58:56 +00:00
|
|
|
if (sh.objectid > root_id)
|
2013-06-26 15:17:57 +00:00
|
|
|
break;
|
|
|
|
|
2024-03-14 14:58:56 +00:00
|
|
|
if (sh.objectid == root_id && sh.type == BTRFS_ROOT_ITEM_KEY) {
|
|
|
|
if (sh.len > buf_len) {
|
2013-06-26 15:17:57 +00:00
|
|
|
/* btrfs-progs is too old for kernel */
|
2022-09-16 17:29:25 +00:00
|
|
|
error(
|
|
|
|
"buf for read_root_item_raw() is too small, get newer btrfs tools");
|
2013-06-26 15:17:57 +00:00
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
2024-03-14 14:58:56 +00:00
|
|
|
memcpy(buf, item, sh.len);
|
|
|
|
*read_len = sh.len;
|
2013-06-26 15:17:57 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sk->min_offset < (u64)-1)
|
|
|
|
sk->min_offset++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (sk->min_type != BTRFS_ROOT_ITEM_KEY ||
|
|
|
|
sk->min_objectid != root_id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found ? 0 : -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a root item from the tree. In case we detect a root item smaller then
|
|
|
|
* sizeof(root_item), we know it's an old version of the root structure and
|
|
|
|
* initialize all new fields to zero. The same happens if we detect mismatching
|
|
|
|
* generation numbers as then we know the root was once mounted with an older
|
|
|
|
* kernel that was not aware of the root item structure change.
|
|
|
|
*/
|
|
|
|
static int btrfs_read_root_item(int mnt_fd, u64 root_id,
|
|
|
|
struct btrfs_root_item *item)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 read_len;
|
|
|
|
|
|
|
|
ret = btrfs_read_root_item_raw(mnt_fd, root_id, sizeof(*item),
|
|
|
|
&read_len, item);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (read_len < sizeof(*item) ||
|
btrfs-progs: send: add gcc9 workaround for root item reset
Gcc version 9.1.1 reports:
In file included from /usr/include/string.h:494,
from kerncompat.h:25,
from ctree.h:26,
from send-utils.c:26:
In function ‘memset’,
inlined from ‘btrfs_read_root_item’ at send-utils.c:165:3,
inlined from ‘subvol_uuid_search2’ at send-utils.c:494:8:
/usr/include/bits/string_fortified.h:71:10: warning: ‘__builtin_memset’ offset [248, 439] from the object at ‘root_item’ is out of the bounds of referenced subobject ‘generation_v2’ with type ‘long long unsigned int’ at offset 239 [-Warray-bounds]
71 | return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
That's correct in case the intent is to overwrite just the generation_v2
member, but we want to zero the rest of the root item structure starting
from the generation_v2. No typecasts can obscure that from gcc so the
starting address is calculated as base pointer + member offset.
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-02 10:50:14 +00:00
|
|
|
btrfs_root_generation(item) != btrfs_root_generation_v2(item)) {
|
|
|
|
/*
|
|
|
|
* Workaround for gcc9 that warns that memset over
|
|
|
|
* generation_v2 overflows, which is what we want but would
|
|
|
|
* be otherwise a bug
|
|
|
|
*
|
|
|
|
* The below is &item->generation_v2
|
|
|
|
*/
|
|
|
|
char *start = (char *)item + offsetof(struct btrfs_root_item,
|
|
|
|
generation_v2);
|
|
|
|
|
|
|
|
memset(start, 0,
|
2013-06-26 15:17:57 +00:00
|
|
|
sizeof(*item) - offsetof(struct btrfs_root_item,
|
|
|
|
generation_v2));
|
btrfs-progs: send: add gcc9 workaround for root item reset
Gcc version 9.1.1 reports:
In file included from /usr/include/string.h:494,
from kerncompat.h:25,
from ctree.h:26,
from send-utils.c:26:
In function ‘memset’,
inlined from ‘btrfs_read_root_item’ at send-utils.c:165:3,
inlined from ‘subvol_uuid_search2’ at send-utils.c:494:8:
/usr/include/bits/string_fortified.h:71:10: warning: ‘__builtin_memset’ offset [248, 439] from the object at ‘root_item’ is out of the bounds of referenced subobject ‘generation_v2’ with type ‘long long unsigned int’ at offset 239 [-Warray-bounds]
71 | return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
That's correct in case the intent is to overwrite just the generation_v2
member, but we want to zero the rest of the root item structure starting
from the generation_v2. No typecasts can obscure that from gcc so the
starting address is calculated as base pointer + member offset.
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-02 10:50:14 +00:00
|
|
|
}
|
2013-06-26 15:17:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-23 10:18:55 +00:00
|
|
|
int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id)
|
|
|
|
{
|
|
|
|
if (path_len < 1)
|
|
|
|
return -EOVERFLOW;
|
|
|
|
path[0] = '\0';
|
|
|
|
path_len--;
|
|
|
|
path[path_len] = '\0';
|
|
|
|
return btrfs_subvolid_resolve_sub(fd, path, &path_len, subvol_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
|
|
|
|
u64 subvol_id)
|
|
|
|
{
|
|
|
|
int ret;
|
2024-03-13 22:56:11 +00:00
|
|
|
struct btrfs_tree_search_args args;
|
|
|
|
struct btrfs_ioctl_search_key *sk;
|
2013-04-23 10:18:55 +00:00
|
|
|
struct btrfs_ioctl_ino_lookup_args ino_lookup_arg;
|
2024-03-14 14:58:56 +00:00
|
|
|
struct btrfs_ioctl_search_header sh;
|
2013-04-23 10:18:55 +00:00
|
|
|
struct btrfs_root_ref *backref_item;
|
|
|
|
|
|
|
|
if (subvol_id == BTRFS_FS_TREE_OBJECTID) {
|
|
|
|
if (*path_len < 1)
|
|
|
|
return -EOVERFLOW;
|
|
|
|
*path = '\0';
|
|
|
|
(*path_len)--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:56:11 +00:00
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
sk = btrfs_tree_search_sk(&args);
|
|
|
|
sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
|
|
|
|
sk->min_objectid = subvol_id;
|
|
|
|
sk->min_type = BTRFS_ROOT_BACKREF_KEY;
|
2024-04-19 23:27:41 +00:00
|
|
|
sk->max_objectid = subvol_id;
|
2024-03-13 22:56:11 +00:00
|
|
|
sk->max_type = BTRFS_ROOT_BACKREF_KEY;
|
|
|
|
sk->max_offset = (u64)-1;
|
|
|
|
sk->max_transid = (u64)-1;
|
|
|
|
sk->nr_items = 1;
|
|
|
|
ret = btrfs_tree_search_ioctl(fd, &args);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2013-04-23 10:18:55 +00:00
|
|
|
fprintf(stderr,
|
2018-01-07 21:54:21 +00:00
|
|
|
"ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %m\n",
|
2023-10-23 20:34:33 +00:00
|
|
|
subvol_id, ret);
|
2013-04-23 10:18:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-03-13 22:56:11 +00:00
|
|
|
if (sk->nr_items < 1) {
|
2023-10-23 20:34:33 +00:00
|
|
|
fprintf(stderr, "failed to lookup subvol_id %llu!\n", subvol_id);
|
2013-04-23 10:18:55 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2024-03-14 14:58:56 +00:00
|
|
|
memcpy(&sh, btrfs_tree_search_data(&args, 0), sizeof(sh));
|
|
|
|
backref_item = btrfs_tree_search_data(&args, sizeof(sh));
|
|
|
|
if (sh.offset != BTRFS_FS_TREE_OBJECTID) {
|
2013-04-23 10:18:55 +00:00
|
|
|
int sub_ret;
|
|
|
|
|
2024-03-14 14:58:56 +00:00
|
|
|
sub_ret = btrfs_subvolid_resolve_sub(fd, path, path_len, sh.offset);
|
2013-04-23 10:18:55 +00:00
|
|
|
if (sub_ret)
|
|
|
|
return sub_ret;
|
|
|
|
if (*path_len < 1)
|
|
|
|
return -EOVERFLOW;
|
|
|
|
strcat(path, "/");
|
|
|
|
(*path_len)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (btrfs_stack_root_ref_dirid(backref_item) !=
|
|
|
|
BTRFS_FIRST_FREE_OBJECTID) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
memset(&ino_lookup_arg, 0, sizeof(ino_lookup_arg));
|
2024-03-14 14:58:56 +00:00
|
|
|
ino_lookup_arg.treeid = sh.offset;
|
2013-04-23 10:18:55 +00:00
|
|
|
ino_lookup_arg.objectid =
|
|
|
|
btrfs_stack_root_ref_dirid(backref_item);
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg);
|
2016-01-12 12:35:50 +00:00
|
|
|
if (ret < 0) {
|
2013-04-23 10:18:55 +00:00
|
|
|
fprintf(stderr,
|
2018-01-07 21:54:21 +00:00
|
|
|
"ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %m\n",
|
|
|
|
ret);
|
2013-04-23 10:18:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(ino_lookup_arg.name);
|
|
|
|
if (*path_len < len)
|
|
|
|
return -EOVERFLOW;
|
|
|
|
strcat(path, ino_lookup_arg.name);
|
|
|
|
(*path_len) -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*path_len < btrfs_stack_root_ref_name_len(backref_item))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
strncat(path, (char *)(backref_item + 1),
|
|
|
|
btrfs_stack_root_ref_name_len(backref_item));
|
|
|
|
(*path_len) -= btrfs_stack_root_ref_name_len(backref_item);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:06:50 +00:00
|
|
|
struct subvol_info *subvol_uuid_search(int mnt_fd,
|
2012-07-25 21:08:25 +00:00
|
|
|
u64 root_id, const u8 *uuid, u64 transid,
|
|
|
|
const char *path,
|
2016-12-12 16:45:25 +00:00
|
|
|
enum subvol_search_type type)
|
2012-07-25 21:08:25 +00:00
|
|
|
{
|
2013-06-26 15:17:57 +00:00
|
|
|
int ret = 0;
|
2012-07-25 21:08:25 +00:00
|
|
|
struct btrfs_root_item root_item;
|
2013-06-26 15:17:57 +00:00
|
|
|
struct subvol_info *info = NULL;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case subvol_search_by_received_uuid:
|
2021-09-30 18:06:50 +00:00
|
|
|
ret = btrfs_lookup_uuid_received_subvol_item(mnt_fd, uuid,
|
2013-06-26 15:17:57 +00:00
|
|
|
&root_id);
|
|
|
|
break;
|
|
|
|
case subvol_search_by_uuid:
|
2021-09-30 18:06:50 +00:00
|
|
|
ret = btrfs_lookup_uuid_subvol_item(mnt_fd, uuid, &root_id);
|
2013-06-26 15:17:57 +00:00
|
|
|
break;
|
|
|
|
case subvol_search_by_root_id:
|
|
|
|
break;
|
|
|
|
case subvol_search_by_path:
|
2021-09-30 18:06:50 +00:00
|
|
|
ret = btrfs_get_root_id_by_sub_path(mnt_fd, path, &root_id);
|
2013-06-26 15:17:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-07-25 21:08:25 +00:00
|
|
|
|
2013-06-26 15:17:57 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2021-09-30 18:06:50 +00:00
|
|
|
ret = btrfs_read_root_item(mnt_fd, root_id, &root_item);
|
2013-06-26 15:17:57 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
info = calloc(1, sizeof(*info));
|
2016-12-10 13:47:44 +00:00
|
|
|
if (!info) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-26 15:17:57 +00:00
|
|
|
info->root_id = root_id;
|
|
|
|
memcpy(info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
|
|
|
|
memcpy(info->received_uuid, root_item.received_uuid, BTRFS_UUID_SIZE);
|
|
|
|
memcpy(info->parent_uuid, root_item.parent_uuid, BTRFS_UUID_SIZE);
|
|
|
|
info->ctransid = btrfs_root_ctransid(&root_item);
|
|
|
|
info->otransid = btrfs_root_otransid(&root_item);
|
|
|
|
info->stransid = btrfs_root_stransid(&root_item);
|
|
|
|
info->rtransid = btrfs_root_rtransid(&root_item);
|
|
|
|
if (type == subvol_search_by_path) {
|
|
|
|
info->path = strdup(path);
|
2016-12-12 16:53:31 +00:00
|
|
|
if (!info->path) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-26 15:17:57 +00:00
|
|
|
} else {
|
2015-06-12 11:18:44 +00:00
|
|
|
info->path = malloc(PATH_MAX);
|
2016-12-10 13:47:42 +00:00
|
|
|
if (!info->path) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-09-30 18:06:50 +00:00
|
|
|
ret = btrfs_subvolid_resolve(mnt_fd, info->path,
|
2015-06-12 11:18:44 +00:00
|
|
|
PATH_MAX, root_id);
|
2012-07-25 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2016-12-10 13:47:44 +00:00
|
|
|
if (ret) {
|
|
|
|
if (info) {
|
|
|
|
free(info->path);
|
|
|
|
free(info);
|
|
|
|
}
|
2016-12-10 13:47:43 +00:00
|
|
|
return ERR_PTR(ret);
|
2013-06-26 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
2012-07-25 21:08:25 +00:00
|
|
|
}
|