^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) STRATO AG 2013. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) key->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) key->objectid = get_unaligned_le64(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) key->offset = get_unaligned_le64(uuid + sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u8 type, u64 subid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (WARN_ON_ONCE(!uuid_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) btrfs_uuid_to_key(uuid, type, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) offset = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!IS_ALIGNED(item_size, sizeof(u64))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) btrfs_warn(uuid_root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) "uuid item with illegal size %lu!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) (unsigned long)item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) while (item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __le64 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) read_extent_buffer(eb, &data, offset, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (le64_to_cpu(data) == subid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) offset += sizeof(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) item_size -= sizeof(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u64 subid_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct btrfs_root *uuid_root = fs_info->uuid_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __le64 subid_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (WARN_ON_ONCE(!uuid_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) btrfs_uuid_to_key(uuid, type, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) sizeof(subid_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Add an item for the type for the first time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) offset = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * An item with that type already exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Extend the item and store the new subid at the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) btrfs_extend_item(path, sizeof(subid_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) offset = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) offset += btrfs_item_size_nr(eb, slot) - sizeof(subid_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret, (unsigned long long)key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) (unsigned long long)key.offset, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) subid_le = cpu_to_le64(subid_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) btrfs_mark_buffer_dirty(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u64 subid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct btrfs_root *uuid_root = fs_info->uuid_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned long move_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned long move_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned long move_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (WARN_ON_ONCE(!uuid_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) btrfs_uuid_to_key(uuid, type, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) btrfs_warn(fs_info, "error %d while searching for uuid item!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) eb = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) offset = btrfs_item_ptr_offset(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!IS_ALIGNED(item_size, sizeof(u64))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) btrfs_warn(fs_info, "uuid item with illegal size %lu!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) (unsigned long)item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) while (item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) __le64 read_subid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (le64_to_cpu(read_subid) == subid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) offset += sizeof(read_subid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) item_size -= sizeof(read_subid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) item_size = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (item_size == sizeof(subid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ret = btrfs_del_item(trans, uuid_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) move_dst = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) move_src = offset + sizeof(subid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) memmove_extent_buffer(eb, move_dst, move_src, move_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) btrfs_truncate_item(path, item_size - sizeof(subid), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) u64 subid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* 1 - for the uuid item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) trans = btrfs_start_transaction(uuid_root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = btrfs_uuid_tree_remove(trans, uuid, type, subid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Check if there's an matching subvolume for given UUID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * 0 check succeeded, the entry is not outdated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * > 0 if the check failed, the caller should remove the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * < 0 if an error occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) u8 *uuid, u8 type, u64 subvolid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct btrfs_root *subvol_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (type != BTRFS_UUID_KEY_SUBVOL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (IS_ERR(subvol_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = PTR_ERR(subvol_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case BTRFS_UUID_KEY_SUBVOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (memcmp(uuid, subvol_root->root_item.received_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) BTRFS_UUID_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) btrfs_put_root(subvol_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct btrfs_root *root = fs_info->uuid_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) key.type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) again_search_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (btrfs_fs_closing(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (key.type != BTRFS_UUID_KEY_SUBVOL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) offset = btrfs_item_ptr_offset(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) item_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!IS_ALIGNED(item_size, sizeof(u64))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) "uuid item with illegal size %lu!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) (unsigned long)item_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) while (item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) u8 uuid[BTRFS_UUID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) __le64 subid_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) u64 subid_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) put_unaligned_le64(key.objectid, uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) put_unaligned_le64(key.offset, uuid + sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) read_extent_buffer(leaf, &subid_le, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) sizeof(subid_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) subid_cpu = le64_to_cpu(subid_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) key.type, subid_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ret = btrfs_uuid_iter_rem(root, uuid, key.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) subid_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * this might look inefficient, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * justification is that it is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * exception that check_func returns 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * and that in the regular case only one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * entry per UUID exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto again_search_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (ret < 0 && ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) key.offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto again_search_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) item_size -= sizeof(subid_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) offset += sizeof(subid_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = btrfs_next_item(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }