1 // SPDX-License-Identifier: GPL-2.0+
3 * BTRFS filesystem implementation for U-Boot
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
11 int btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
13 if (a->objectid > b->objectid)
15 if (a->objectid < b->objectid)
17 if (a->type > b->type)
19 if (a->type < b->type)
21 if (a->offset > b->offset)
23 if (a->offset < b->offset)
28 int btrfs_comp_keys_type(struct btrfs_key *a, struct btrfs_key *b)
30 if (a->objectid > b->objectid)
32 if (a->objectid < b->objectid)
34 if (a->type > b->type)
36 if (a->type < b->type)
41 static int generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
44 int low = 0, high = max, mid, ret;
45 struct btrfs_key *tmp;
48 mid = (low + high) / 2;
50 tmp = (struct btrfs_key *) ((u8 *) addr + mid*item_size);
51 ret = btrfs_comp_keys(tmp, key);
67 int btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
73 if (p->header.level) {
75 size = sizeof(struct btrfs_key_ptr);
78 size = sizeof(struct btrfs_item);
81 return generic_bin_search(addr, size, key, p->header.nritems, slot);
84 static void clear_path(struct btrfs_path *p)
88 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
94 void btrfs_free_path(struct btrfs_path *p)
98 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
106 static int read_tree_node(u64 physical, union btrfs_tree_node **buf)
108 struct btrfs_header hdr;
109 unsigned long size, offset = sizeof(hdr);
110 union btrfs_tree_node *res;
113 if (!btrfs_devread(physical, sizeof(hdr), &hdr))
116 btrfs_header_to_cpu(&hdr);
119 size = sizeof(struct btrfs_node)
120 + hdr.nritems * sizeof(struct btrfs_key_ptr);
122 size = btrfs_info.sb.nodesize;
126 debug("%s: malloc failed\n", __func__);
130 if (!btrfs_devread(physical + offset, size - offset,
131 ((u8 *) res) + offset)) {
138 for (i = 0; i < hdr.nritems; ++i)
139 btrfs_key_ptr_to_cpu(&res->node.ptrs[i]);
141 for (i = 0; i < hdr.nritems; ++i)
142 btrfs_item_to_cpu(&res->leaf.items[i]);
149 int btrfs_search_tree(const struct btrfs_root *root, struct btrfs_key *key,
150 struct btrfs_path *p)
154 u64 logical, physical;
155 union btrfs_tree_node *buf;
159 logical = root->bytenr;
161 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
162 physical = btrfs_map_logical_to_physical(logical);
163 if (physical == -1ULL)
166 if (read_tree_node(physical, &buf))
169 lvl = buf->header.level;
170 if (i && prev_lvl != lvl + 1) {
171 printf("%s: invalid level in header at %llu\n",
177 ret = btrfs_bin_search(buf, key, &slot);
180 if (ret && slot > 0 && lvl)
183 p->slots[lvl] = slot;
187 logical = buf->node.ptrs[slot].blockptr;
198 static int jump_leaf(struct btrfs_path *path, int dir)
202 int level = 1, from_level, i;
204 dir = dir >= 0 ? 1 : -1;
208 while (level < BTRFS_MAX_LEVEL) {
212 slot = p.slots[level];
213 if ((dir > 0 && slot + dir >= p.nodes[level]->header.nritems)
214 || (dir < 0 && !slot))
220 if (level == BTRFS_MAX_LEVEL)
223 p.slots[level] = slot + dir;
228 u64 logical, physical;
230 slot = p.slots[level + 1];
231 logical = p.nodes[level + 1]->node.ptrs[slot].blockptr;
232 physical = btrfs_map_logical_to_physical(logical);
233 if (physical == -1ULL)
236 if (read_tree_node(physical, &p.nodes[level]))
242 p.slots[level] = p.nodes[level]->header.nritems - 1;
246 /* Free rewritten nodes in path */
247 for (i = 0; i <= from_level; ++i)
248 free(path->nodes[i]);
254 /* Free rewritten nodes in p */
255 for (i = level + 1; i <= from_level; ++i)
260 int btrfs_prev_slot(struct btrfs_path *p)
263 return jump_leaf(p, -1);
269 int btrfs_next_slot(struct btrfs_path *p)
271 struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
273 if (p->slots[0] >= leaf->header.nritems)
274 return jump_leaf(p, 1);