^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) 2007 Oracle. 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/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/uuid.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) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "space-info.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Read a root item from the tree. In case we detect a root item smaller then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * sizeof(root_item), we know it's an old version of the root structure and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * initialize all new fields to zero. The same happens if we detect mismatching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * generation numbers as then we know the root was once mounted with an older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * kernel that was not aware of the root item structure change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct btrfs_root_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int need_reset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) len = btrfs_item_size_nr(eb, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) min_t(u32, len, sizeof(*item)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (len < sizeof(*item))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) need_reset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!need_reset && btrfs_root_generation(item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) != btrfs_root_generation_v2(item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (btrfs_root_generation_v2(item) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) btrfs_warn(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) "mismatching generation and generation_v2 found in root item. This root was probably mounted with an older kernel. Resetting all new fields.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) need_reset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (need_reset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) memset(&item->generation_v2, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) sizeof(*item) - offsetof(struct btrfs_root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) generation_v2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) generate_random_guid(item->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * btrfs_find_root - lookup the root by the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * root: the root of the root tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * search_key: the key to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * path: the path we search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * root_item: the root item of the tree we look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * root_key: the root key of the tree we look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * If ->offset of 'search_key' is -1ULL, it means we are not sure the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * of the search key, just lookup the root with the highest offset for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * given objectid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * If we find something return 0, otherwise > 0, < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct btrfs_path *path, struct btrfs_root_item *root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct btrfs_key *root_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ret = btrfs_search_slot(NULL, root, search_key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (search_key->offset != -1ULL) { /* the search key is exact */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) BUG_ON(ret == 0); /* Logical error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (path->slots[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) btrfs_item_key_to_cpu(l, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (found_key.objectid != search_key->objectid ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) found_key.type != BTRFS_ROOT_ITEM_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (root_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) btrfs_read_root_item(l, slot, root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (root_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) memcpy(root_key, &found_key, sizeof(found_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void btrfs_set_root_node(struct btrfs_root_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct extent_buffer *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) btrfs_set_root_bytenr(item, node->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) btrfs_set_root_level(item, btrfs_header_level(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) btrfs_set_root_generation(item, btrfs_header_generation(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * copy the data in 'item' into the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *root, struct btrfs_key *key, struct btrfs_root_item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) u32 old_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ret = btrfs_search_slot(trans, root, key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) btrfs_crit(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "unable to find root key (%llu %u %llu) in tree %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) key->objectid, key->type, key->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ptr = btrfs_item_ptr_offset(l, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) old_len = btrfs_item_size_nr(l, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * If this is the first time we update the root item which originated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * from an older kernel, we need to enlarge the item size to make room
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * for the added fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (old_len < sizeof(*item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ret = btrfs_search_slot(trans, root, key, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = btrfs_del_item(trans, root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = btrfs_insert_empty_item(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) key, sizeof(*item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ptr = btrfs_item_ptr_offset(l, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Update generation_v2 so at the next mount we know the new root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * fields are valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) write_extent_buffer(l, item, ptr, sizeof(*item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) const struct btrfs_key *key, struct btrfs_root_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Make sure generation v1 and v2 match. See update_root for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return btrfs_insert_item(trans, root, key, item, sizeof(*item));
^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) int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct btrfs_root *tree_root = fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) key.objectid = BTRFS_ORPHAN_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) key.type = BTRFS_ORPHAN_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u64 root_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (path->slots[0] >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ret = btrfs_next_leaf(tree_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) key.type != BTRFS_ORPHAN_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) root_objectid = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) key.offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) root = btrfs_get_fs_root(fs_info, root_objectid, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = PTR_ERR_OR_ZERO(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (err && err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) } else if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) trans = btrfs_join_transaction(tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) err = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) btrfs_handle_fs_error(fs_info, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) "Failed to start trans to delete orphan item");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) err = btrfs_del_orphan_item(trans, tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) root_objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) btrfs_handle_fs_error(fs_info, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) "Failed to delete root orphan item");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (btrfs_root_refs(&root->root_item) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) set_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) btrfs_add_dead_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* drop the root item for 'key' from the tree root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int btrfs_del_root(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) const struct btrfs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct btrfs_root *root = trans->fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = btrfs_search_slot(trans, root, key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) BUG_ON(ret != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = btrfs_del_item(trans, root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) u64 ref_id, u64 dirid, u64 *sequence, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct btrfs_root *tree_root = trans->fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct btrfs_root_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) key.objectid = root_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) key.type = BTRFS_ROOT_BACKREF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) key.offset = ref_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ref = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ptr = (unsigned long)(ref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if ((btrfs_root_ref_dirid(leaf, ref) != dirid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) (btrfs_root_ref_name_len(leaf, ref) != name_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) memcmp_extent_buffer(leaf, name, ptr, name_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) *sequence = btrfs_root_ref_sequence(leaf, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = btrfs_del_item(trans, tree_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (key.type == BTRFS_ROOT_BACKREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) key.objectid = ref_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) key.offset = root_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * or BTRFS_ROOT_BACKREF_KEY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * The dirid, sequence, name and name_len refer to the directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * that is referencing the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * For a forward ref, the root_id is the id of the tree referencing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * the root and ref_id is the id of the subvol or snapshot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * For a back ref the root_id is the id of the subvol or snapshot and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * ref_id is the id of the tree referencing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Will return 0, -ENOMEM, or anything from the CoW path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) u64 ref_id, u64 dirid, u64 sequence, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct btrfs_root *tree_root = trans->fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct btrfs_root_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unsigned long ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) key.objectid = root_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) key.type = BTRFS_ROOT_BACKREF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) key.offset = ref_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) sizeof(*ref) + name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) btrfs_set_root_ref_dirid(leaf, ref, dirid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) btrfs_set_root_ref_sequence(leaf, ref, sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) btrfs_set_root_ref_name_len(leaf, ref, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ptr = (unsigned long)(ref + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) write_extent_buffer(leaf, name, ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (key.type == BTRFS_ROOT_BACKREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) key.objectid = ref_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) key.offset = root_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * Old btrfs forgets to init root_item->flags and root_item->byte_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * for subvolumes. To work around this problem, we steal a bit from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * root_item->inode_item->flags, and use it to indicate if those fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * have been properly initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) btrfs_set_root_flags(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) btrfs_set_root_limit(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) void btrfs_update_root_times(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) struct btrfs_root_item *item = &root->root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct timespec64 ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ktime_get_real_ts64(&ct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) spin_lock(&root->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) btrfs_set_root_ctransid(item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) spin_unlock(&root->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * root: the root of the parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * rsv: block reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * items: the number of items that we need do reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * use_global_rsv: allow fallback to the global block reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * This function is used to reserve the space for snapshot/subvolume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * creation and deletion. Those operations are different with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * common file/directory operations, they change two fs/file trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * and root tree, the number of items that the qgroup reserves is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * different with the free space reservation. So we can not use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * the space reservation mechanism in start_transaction().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct btrfs_block_rsv *rsv, int items,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) bool use_global_rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) u64 qgroup_num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /* One for parent inode, two for dir entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) qgroup_num_bytes = 3 * fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ret = btrfs_qgroup_reserve_meta_prealloc(root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) qgroup_num_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) num_bytes = btrfs_calc_insert_metadata_size(fs_info, items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) rsv->space_info = btrfs_find_space_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) BTRFS_BLOCK_GROUP_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) ret = btrfs_block_rsv_add(root, rsv, num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) BTRFS_RESERVE_FLUSH_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (ret == -ENOSPC && use_global_rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (ret && qgroup_num_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) spin_lock(&rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) rsv->qgroup_rsv_reserved += qgroup_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) spin_unlock(&rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) void btrfs_subvolume_release_metadata(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct btrfs_block_rsv *rsv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) u64 qgroup_to_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) btrfs_block_rsv_release(fs_info, rsv, (u64)-1, &qgroup_to_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) btrfs_qgroup_convert_reserved_meta(root, qgroup_to_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }