1 // SPDX-License-Identifier: GPL-2.0+
3 * BTRFS filesystem implementation for U-Boot
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
8 #include <linux/kernel.h>
15 static const struct btrfs_csum {
19 [BTRFS_CSUM_TYPE_CRC32] = { 4, "crc32c" },
20 [BTRFS_CSUM_TYPE_XXHASH] = { 8, "xxhash64" },
21 [BTRFS_CSUM_TYPE_SHA256] = { 32, "sha256" },
22 [BTRFS_CSUM_TYPE_BLAKE2] = { 32, "blake2" },
25 u16 btrfs_super_csum_size(const struct btrfs_super_block *sb)
27 const u16 csum_type = btrfs_super_csum_type(sb);
29 return btrfs_csums[csum_type].size;
32 const char *btrfs_super_csum_name(u16 csum_type)
34 return btrfs_csums[csum_type].name;
37 size_t btrfs_super_num_csums(void)
39 return ARRAY_SIZE(btrfs_csums);
42 u16 btrfs_csum_type_size(u16 csum_type)
44 return btrfs_csums[csum_type].size;
47 struct btrfs_path *btrfs_alloc_path(void)
49 struct btrfs_path *path;
50 path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
54 void btrfs_free_path(struct btrfs_path *p)
58 btrfs_release_path(p);
62 void btrfs_release_path(struct btrfs_path *p)
65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
68 free_extent_buffer(p->nodes[i]);
70 memset(p, 0, sizeof(*p));
73 int __btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
75 if (a->objectid > b->objectid)
77 if (a->objectid < b->objectid)
79 if (a->type > b->type)
81 if (a->type < b->type)
83 if (a->offset > b->offset)
85 if (a->offset < b->offset)
90 int btrfs_comp_keys_type(struct btrfs_key *a, struct btrfs_key *b)
92 if (a->objectid > b->objectid)
94 if (a->objectid < b->objectid)
96 if (a->type > b->type)
98 if (a->type < b->type)
104 * search for key in the extent_buffer. The items start at offset p,
105 * and they are item_size apart. There are 'max' items in p.
107 * the slot in the array is returned via slot, and it points to
108 * the place where you would insert key if it is not found in
111 * slot may point to max if the key is bigger than all of the keys
113 static int __generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
116 int low = 0, high = max, mid, ret;
117 struct btrfs_key *tmp;
120 mid = (low + high) / 2;
122 tmp = (struct btrfs_key *) ((u8 *) addr + mid*item_size);
123 ret = __btrfs_comp_keys(tmp, key);
127 } else if (ret > 0) {
139 int __btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
145 if (p->header.level) {
147 size = sizeof(struct btrfs_key_ptr);
149 addr = p->leaf.items;
150 size = sizeof(struct btrfs_item);
153 return __generic_bin_search(addr, size, key, p->header.nritems, slot);
156 static void clear_path(struct __btrfs_path *p)
160 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
166 void __btrfs_free_path(struct __btrfs_path *p)
170 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
178 static int read_tree_node(u64 physical, union btrfs_tree_node **buf)
180 ALLOC_CACHE_ALIGN_BUFFER(struct btrfs_header, hdr,
181 sizeof(struct btrfs_header));
182 unsigned long size, offset = sizeof(*hdr);
183 union btrfs_tree_node *res;
186 if (!btrfs_devread(physical, sizeof(*hdr), hdr))
189 btrfs_header_to_cpu(hdr);
192 size = sizeof(struct btrfs_node)
193 + hdr->nritems * sizeof(struct btrfs_key_ptr);
195 size = btrfs_info.sb.nodesize;
197 res = malloc_cache_aligned(size);
199 debug("%s: malloc failed\n", __func__);
203 if (!btrfs_devread(physical + offset, size - offset,
204 ((u8 *) res) + offset)) {
209 memcpy(&res->header, hdr, sizeof(*hdr));
211 for (i = 0; i < hdr->nritems; ++i)
212 btrfs_key_ptr_to_cpu(&res->node.ptrs[i]);
214 for (i = 0; i < hdr->nritems; ++i)
215 btrfs_item_to_cpu(&res->leaf.items[i]);
222 int btrfs_search_tree(const struct __btrfs_root *root, struct btrfs_key *key,
223 struct __btrfs_path *p)
227 u64 logical, physical;
228 union btrfs_tree_node *buf;
232 logical = root->bytenr;
234 for (i = 0; i < BTRFS_MAX_LEVEL; ++i) {
235 physical = btrfs_map_logical_to_physical(logical);
236 if (physical == -1ULL)
239 if (read_tree_node(physical, &buf))
242 lvl = buf->header.level;
243 if (i && prev_lvl != lvl + 1) {
244 printf("%s: invalid level in header at %llu\n",
250 ret = __btrfs_bin_search(buf, key, &slot);
253 if (ret && slot > 0 && lvl)
256 p->slots[lvl] = slot;
260 logical = buf->node.ptrs[slot].blockptr;
263 * The path might be invalid if:
264 * cur leaf max < searched value < next leaf min
266 * Jump to the next valid element if it exists.
268 if (slot >= buf->header.nritems)
269 if (btrfs_next_slot(p) < 0)
277 __btrfs_free_path(p);
281 static int jump_leaf(struct __btrfs_path *path, int dir)
283 struct __btrfs_path p;
285 int level = 1, from_level, i;
287 dir = dir >= 0 ? 1 : -1;
291 while (level < BTRFS_MAX_LEVEL) {
295 slot = p.slots[level];
296 if ((dir > 0 && slot + dir >= p.nodes[level]->header.nritems)
297 || (dir < 0 && !slot))
303 if (level == BTRFS_MAX_LEVEL)
306 p.slots[level] = slot + dir;
311 u64 logical, physical;
313 slot = p.slots[level + 1];
314 logical = p.nodes[level + 1]->node.ptrs[slot].blockptr;
315 physical = btrfs_map_logical_to_physical(logical);
316 if (physical == -1ULL)
319 if (read_tree_node(physical, &p.nodes[level]))
325 p.slots[level] = p.nodes[level]->header.nritems - 1;
329 /* Free rewritten nodes in path */
330 for (i = 0; i <= from_level; ++i)
331 free(path->nodes[i]);
337 /* Free rewritten nodes in p */
338 for (i = level + 1; i <= from_level; ++i)
343 int btrfs_prev_slot(struct __btrfs_path *p)
346 return jump_leaf(p, -1);
352 int btrfs_next_slot(struct __btrfs_path *p)
354 struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
356 if (p->slots[0] + 1 >= leaf->header.nritems)
357 return jump_leaf(p, 1);
363 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
365 if (k1->objectid > k2->objectid)
367 if (k1->objectid < k2->objectid)
369 if (k1->type > k2->type)
371 if (k1->type < k2->type)
373 if (k1->offset > k2->offset)
375 if (k1->offset < k2->offset)
380 static int btrfs_comp_keys(struct btrfs_disk_key *disk,
381 const struct btrfs_key *k2)
385 btrfs_disk_key_to_cpu(&k1, disk);
386 return btrfs_comp_cpu_keys(&k1, k2);
389 enum btrfs_tree_block_status
390 btrfs_check_node(struct btrfs_fs_info *fs_info,
391 struct btrfs_disk_key *parent_key, struct extent_buffer *buf)
394 struct btrfs_key cpukey;
395 struct btrfs_disk_key key;
396 u32 nritems = btrfs_header_nritems(buf);
397 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
399 if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(fs_info))
402 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
403 if (parent_key && parent_key->type) {
404 btrfs_node_key(buf, &key, 0);
405 if (memcmp(parent_key, &key, sizeof(key)))
408 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
409 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
410 btrfs_node_key(buf, &key, i);
411 btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
412 if (btrfs_comp_keys(&key, &cpukey) >= 0)
415 return BTRFS_TREE_BLOCK_CLEAN;
420 enum btrfs_tree_block_status
421 btrfs_check_leaf(struct btrfs_fs_info *fs_info,
422 struct btrfs_disk_key *parent_key, struct extent_buffer *buf)
425 struct btrfs_key cpukey;
426 struct btrfs_disk_key key;
427 u32 nritems = btrfs_header_nritems(buf);
428 enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
430 if (nritems * sizeof(struct btrfs_item) > buf->len) {
431 fprintf(stderr, "invalid number of items %llu\n",
432 (unsigned long long)buf->start);
436 if (btrfs_header_level(buf) != 0) {
437 ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
438 fprintf(stderr, "leaf is not a leaf %llu\n",
439 (unsigned long long)btrfs_header_bytenr(buf));
442 if (btrfs_leaf_free_space(buf) < 0) {
443 ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
444 fprintf(stderr, "leaf free space incorrect %llu %d\n",
445 (unsigned long long)btrfs_header_bytenr(buf),
446 btrfs_leaf_free_space(buf));
451 return BTRFS_TREE_BLOCK_CLEAN;
453 btrfs_item_key(buf, &key, 0);
454 if (parent_key && parent_key->type &&
455 memcmp(parent_key, &key, sizeof(key))) {
456 ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
457 fprintf(stderr, "leaf parent key incorrect %llu\n",
458 (unsigned long long)btrfs_header_bytenr(buf));
461 for (i = 0; nritems > 1 && i < nritems - 1; i++) {
462 btrfs_item_key(buf, &key, i);
463 btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
464 if (btrfs_comp_keys(&key, &cpukey) >= 0) {
465 ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
466 fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
469 if (btrfs_item_offset_nr(buf, i) !=
470 btrfs_item_end_nr(buf, i + 1)) {
471 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
472 fprintf(stderr, "incorrect offsets %u %u\n",
473 btrfs_item_offset_nr(buf, i),
474 btrfs_item_end_nr(buf, i + 1));
477 if (i == 0 && btrfs_item_end_nr(buf, i) !=
478 BTRFS_LEAF_DATA_SIZE(fs_info)) {
479 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
480 fprintf(stderr, "bad item end %u wanted %u\n",
481 btrfs_item_end_nr(buf, i),
482 (unsigned)BTRFS_LEAF_DATA_SIZE(fs_info));
487 for (i = 0; i < nritems; i++) {
488 if (btrfs_item_end_nr(buf, i) >
489 BTRFS_LEAF_DATA_SIZE(fs_info)) {
490 btrfs_item_key(buf, &key, 0);
491 ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
492 fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
493 (unsigned long long)btrfs_item_end_nr(buf, i),
494 (unsigned long long)BTRFS_LEAF_DATA_SIZE(
500 return BTRFS_TREE_BLOCK_CLEAN;
505 static int noinline check_block(struct btrfs_fs_info *fs_info,
506 struct btrfs_path *path, int level)
508 struct btrfs_disk_key key;
509 struct btrfs_disk_key *key_ptr = NULL;
510 struct extent_buffer *parent;
511 enum btrfs_tree_block_status ret;
513 if (path->nodes[level + 1]) {
514 parent = path->nodes[level + 1];
515 btrfs_node_key(parent, &key, path->slots[level + 1]);
519 ret = btrfs_check_leaf(fs_info, key_ptr, path->nodes[0]);
521 ret = btrfs_check_node(fs_info, key_ptr, path->nodes[level]);
522 if (ret == BTRFS_TREE_BLOCK_CLEAN)
528 * search for key in the extent_buffer. The items start at offset p,
529 * and they are item_size apart. There are 'max' items in p.
531 * the slot in the array is returned via slot, and it points to
532 * the place where you would insert key if it is not found in
535 * slot may point to max if the key is bigger than all of the keys
537 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
538 int item_size, const struct btrfs_key *key,
545 unsigned long offset;
546 struct btrfs_disk_key *tmp;
549 mid = (low + high) / 2;
550 offset = p + mid * item_size;
552 tmp = (struct btrfs_disk_key *)(eb->data + offset);
553 ret = btrfs_comp_keys(tmp, key);
569 * simple bin_search frontend that does the right thing for
572 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
575 if (btrfs_header_level(eb) == 0)
576 return generic_bin_search(eb,
577 offsetof(struct btrfs_leaf, items),
578 sizeof(struct btrfs_item),
579 key, btrfs_header_nritems(eb),
582 return generic_bin_search(eb,
583 offsetof(struct btrfs_node, ptrs),
584 sizeof(struct btrfs_key_ptr),
585 key, btrfs_header_nritems(eb),
589 struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
590 struct extent_buffer *parent, int slot)
592 struct extent_buffer *ret;
593 int level = btrfs_header_level(parent);
597 if (slot >= btrfs_header_nritems(parent))
603 ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
604 btrfs_node_ptr_generation(parent, slot));
605 if (!extent_buffer_uptodate(ret))
606 return ERR_PTR(-EIO);
608 if (btrfs_header_level(ret) != level - 1) {
609 error("child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
610 btrfs_header_bytenr(parent), slot,
611 btrfs_header_level(parent), btrfs_header_level(ret));
612 free_extent_buffer(ret);
613 return ERR_PTR(-EIO);
618 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
619 u64 iobjectid, u64 ioff, u8 key_type,
620 struct btrfs_key *found_key)
623 struct btrfs_key key;
624 struct extent_buffer *eb;
625 struct btrfs_path *path;
628 key.objectid = iobjectid;
631 if (found_path == NULL) {
632 path = btrfs_alloc_path();
638 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
639 if ((ret < 0) || (found_key == NULL))
643 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
644 ret = btrfs_next_leaf(fs_root, path);
650 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
651 if (found_key->type != key.type ||
652 found_key->objectid != key.objectid) {
658 if (path != found_path)
659 btrfs_free_path(path);
664 * look for key in the tree. path is filled in with nodes along the way
665 * if key is found, we return zero and you can find the item in the leaf
666 * level of the path (level 0)
668 * If the key isn't found, the path points to the slot where it should
669 * be inserted, and 1 is returned. If there are other errors during the
670 * search a negative error number is returned.
672 * if ins_len > 0, nodes and leaves will be split as we walk down the
673 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
676 * NOTE: This version has no COW ability, thus we expect trans == NULL,
677 * ins_len == 0 and cow == 0.
679 int btrfs_search_slot(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root, const struct btrfs_key *key,
681 struct btrfs_path *p, int ins_len, int cow)
683 struct extent_buffer *b;
687 struct btrfs_fs_info *fs_info = root->fs_info;
690 assert(trans == NULL && ins_len == 0 && cow == 0);
691 lowest_level = p->lowest_level;
692 WARN_ON(lowest_level && ins_len > 0);
693 WARN_ON(p->nodes[0] != NULL);
696 extent_buffer_get(b);
698 level = btrfs_header_level(b);
702 wret = btrfs_cow_block(trans, root, b,
707 free_extent_buffer(b);
712 BUG_ON(!cow && ins_len);
713 if (level != btrfs_header_level(b))
715 level = btrfs_header_level(b);
717 ret = check_block(fs_info, p, level);
720 ret = btrfs_bin_search(b, key, &slot);
724 p->slots[level] = slot;
726 if ((p->search_for_split || ins_len > 0) &&
727 btrfs_header_nritems(b) >=
728 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
729 int sret = split_node(trans, root, p, level);
734 slot = p->slots[level];
735 } else if (ins_len < 0) {
736 int sret = balance_level(trans, root, p,
742 btrfs_release_path(p);
745 slot = p->slots[level];
746 BUG_ON(btrfs_header_nritems(b) == 1);
749 /* this is only true while dropping a snapshot */
750 if (level == lowest_level)
753 b = read_node_slot(fs_info, b, slot);
754 if (!extent_buffer_uptodate(b))
757 p->slots[level] = slot;
760 ins_len > btrfs_leaf_free_space(b)) {
761 int sret = split_leaf(trans, root, key,
762 p, ins_len, ret == 0);
775 * Helper to use instead of search slot if no exact match is needed but
776 * instead the next or previous item should be returned.
777 * When find_higher is true, the next higher item is returned, the next lower
779 * When return_any and find_higher are both true, and no higher item is found,
780 * return the next lower instead.
781 * When return_any is true and find_higher is false, and no lower item is found,
782 * return the next higher instead.
783 * It returns 0 if any item is found, 1 if none is found (tree empty), and
786 int btrfs_search_slot_for_read(struct btrfs_root *root,
787 const struct btrfs_key *key,
788 struct btrfs_path *p, int find_higher,
792 struct extent_buffer *leaf;
795 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
799 * A return value of 1 means the path is at the position where the item
800 * should be inserted. Normally this is the next bigger item, but in
801 * case the previous item is the last in a leaf, path points to the
802 * first free slot in the previous leaf, i.e. at an invalid item.
807 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
808 ret = btrfs_next_leaf(root, p);
814 * No higher item found, return the next lower instead
818 btrfs_release_path(p);
822 if (p->slots[0] == 0) {
823 ret = btrfs_prev_leaf(root, p);
828 if (p->slots[0] == btrfs_header_nritems(leaf))
835 * No lower item found, return the next higher instead
839 btrfs_release_path(p);
849 * how many bytes are required to store the items in a leaf. start
850 * and nr indicate which items in the leaf to check. This totals up the
851 * space used both by the item structs and the item data
853 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
856 int nritems = btrfs_header_nritems(l);
857 int end = min(nritems, start + nr) - 1;
861 data_len = btrfs_item_end_nr(l, start);
862 data_len = data_len - btrfs_item_offset_nr(l, end);
863 data_len += sizeof(struct btrfs_item) * nr;
864 WARN_ON(data_len < 0);
869 * The space between the end of the leaf items and
870 * the start of the leaf data. IOW, how much room
871 * the leaf has left for both items and data
873 int btrfs_leaf_free_space(struct extent_buffer *leaf)
875 int nritems = btrfs_header_nritems(leaf);
879 BUG_ON(leaf->fs_info && leaf->fs_info->nodesize != leaf->len);
880 leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
881 ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
883 printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
884 ret, leaf_data_size, leaf_space_used(leaf, 0, nritems),
891 * walk up the tree as far as required to find the previous leaf.
892 * returns 0 if it found something or 1 if there are no lesser leaves.
893 * returns < 0 on io errors.
895 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
899 struct extent_buffer *c;
900 struct extent_buffer *next = NULL;
901 struct btrfs_fs_info *fs_info = root->fs_info;
903 while(level < BTRFS_MAX_LEVEL) {
904 if (!path->nodes[level])
907 slot = path->slots[level];
908 c = path->nodes[level];
911 if (level == BTRFS_MAX_LEVEL)
917 next = read_node_slot(fs_info, c, slot);
918 if (!extent_buffer_uptodate(next)) {
920 return PTR_ERR(next);
925 path->slots[level] = slot;
928 c = path->nodes[level];
929 free_extent_buffer(c);
930 slot = btrfs_header_nritems(next);
933 path->nodes[level] = next;
934 path->slots[level] = slot;
937 next = read_node_slot(fs_info, next, slot);
938 if (!extent_buffer_uptodate(next)) {
940 return PTR_ERR(next);
948 * Walk up the tree as far as necessary to find the next sibling tree block.
949 * More generic version of btrfs_next_leaf(), as it could find sibling nodes
950 * if @path->lowest_level is not 0.
952 * returns 0 if it found something or 1 if there are no greater leaves.
953 * returns < 0 on io errors.
955 int btrfs_next_sibling_tree_block(struct btrfs_fs_info *fs_info,
956 struct btrfs_path *path)
959 int level = path->lowest_level + 1;
960 struct extent_buffer *c;
961 struct extent_buffer *next = NULL;
963 BUG_ON(path->lowest_level + 1 >= BTRFS_MAX_LEVEL);
965 if (!path->nodes[level])
968 slot = path->slots[level] + 1;
969 c = path->nodes[level];
970 if (slot >= btrfs_header_nritems(c)) {
972 if (level == BTRFS_MAX_LEVEL)
977 next = read_node_slot(fs_info, c, slot);
978 if (!extent_buffer_uptodate(next))
981 } while (level < BTRFS_MAX_LEVEL);
982 path->slots[level] = slot;
985 c = path->nodes[level];
986 free_extent_buffer(c);
987 path->nodes[level] = next;
988 path->slots[level] = 0;
989 if (level == path->lowest_level)
991 next = read_node_slot(fs_info, next, 0);
992 if (!extent_buffer_uptodate(next))
998 int btrfs_previous_item(struct btrfs_root *root,
999 struct btrfs_path *path, u64 min_objectid,
1002 struct btrfs_key found_key;
1003 struct extent_buffer *leaf;
1008 if (path->slots[0] == 0) {
1009 ret = btrfs_prev_leaf(root, path);
1015 leaf = path->nodes[0];
1016 nritems = btrfs_header_nritems(leaf);
1019 if (path->slots[0] == nritems)
1022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1023 if (found_key.objectid < min_objectid)
1025 if (found_key.type == type)
1027 if (found_key.objectid == min_objectid &&
1028 found_key.type < type)