2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-09-03 15:00:28 +00:00
|
|
|
/*
|
|
|
|
* BTRFS filesystem implementation for U-Boot
|
|
|
|
*
|
|
|
|
* 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "btrfs.h"
|
|
|
|
|
2020-06-24 16:02:56 +00:00
|
|
|
static void read_root_item(struct __btrfs_path *p, struct btrfs_root_item *item)
|
2017-09-03 15:00:28 +00:00
|
|
|
{
|
|
|
|
u32 len;
|
|
|
|
int reset = 0;
|
|
|
|
|
|
|
|
len = btrfs_path_item_size(p);
|
|
|
|
memcpy(item, btrfs_path_item_ptr(p, struct btrfs_root_item), len);
|
|
|
|
btrfs_root_item_to_cpu(item);
|
|
|
|
|
|
|
|
if (len < sizeof(*item))
|
|
|
|
reset = 1;
|
|
|
|
if (!reset && item->generation != item->generation_v2) {
|
|
|
|
if (item->generation_v2 != 0)
|
|
|
|
printf("%s: generation != generation_v2 in root item",
|
|
|
|
__func__);
|
|
|
|
reset = 1;
|
|
|
|
}
|
|
|
|
if (reset) {
|
|
|
|
memset(&item->generation_v2, 0,
|
|
|
|
sizeof(*item) - offsetof(struct btrfs_root_item,
|
|
|
|
generation_v2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:02:57 +00:00
|
|
|
int btrfs_find_root(u64 objectid, struct __btrfs_root *root,
|
2017-09-03 15:00:28 +00:00
|
|
|
struct btrfs_root_item *root_item)
|
|
|
|
{
|
2020-06-24 16:02:56 +00:00
|
|
|
struct __btrfs_path path;
|
2017-09-03 15:00:28 +00:00
|
|
|
struct btrfs_root_item my_root_item;
|
|
|
|
|
|
|
|
if (!btrfs_search_tree_key_type(&btrfs_info.tree_root, objectid,
|
|
|
|
BTRFS_ROOT_ITEM_KEY, &path))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!root_item)
|
|
|
|
root_item = &my_root_item;
|
|
|
|
read_root_item(&path, root_item);
|
|
|
|
|
|
|
|
if (root) {
|
|
|
|
root->objectid = objectid;
|
|
|
|
root->bytenr = root_item->bytenr;
|
|
|
|
root->root_dirid = root_item->root_dirid;
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:02:56 +00:00
|
|
|
__btrfs_free_path(&path);
|
2017-09-03 15:00:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 btrfs_lookup_root_ref(u64 subvolid, struct btrfs_root_ref *refp, char *name)
|
|
|
|
{
|
2020-06-24 16:02:56 +00:00
|
|
|
struct __btrfs_path path;
|
2017-09-03 15:00:28 +00:00
|
|
|
struct btrfs_key *key;
|
|
|
|
struct btrfs_root_ref *ref;
|
|
|
|
u64 res = -1ULL;
|
|
|
|
|
|
|
|
key = btrfs_search_tree_key_type(&btrfs_info.tree_root, subvolid,
|
|
|
|
BTRFS_ROOT_BACKREF_KEY, &path);
|
|
|
|
|
|
|
|
if (!key)
|
|
|
|
return -1ULL;
|
|
|
|
|
|
|
|
ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
|
|
|
|
btrfs_root_ref_to_cpu(ref);
|
|
|
|
|
|
|
|
if (refp)
|
|
|
|
*refp = *ref;
|
|
|
|
|
|
|
|
if (name) {
|
2020-06-24 16:02:47 +00:00
|
|
|
if (ref->name_len > BTRFS_NAME_LEN) {
|
2017-09-03 15:00:28 +00:00
|
|
|
printf("%s: volume name too long: %u\n", __func__,
|
|
|
|
ref->name_len);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(name, ref + 1, ref->name_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = key->offset;
|
|
|
|
out:
|
2020-06-24 16:02:56 +00:00
|
|
|
__btrfs_free_path(&path);
|
2017-09-03 15:00:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|