^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) 2013 Fusion IO. 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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "btrfs-tests.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 "../extent_io.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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct btrfs_root *root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct extent_buffer *eb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) char *value = "mary had a little lamb";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) char *split1 = "mary had a little";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) char *split2 = " lamb";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) char *split3 = "mary";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) char *split4 = " had a little";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) char buf[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 value_len = strlen(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) test_msg("running btrfs_split_item tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!fs_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) test_std_err(TEST_ALLOC_FS_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) root = btrfs_alloc_dummy_root(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) test_std_err(TEST_ALLOC_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ret = PTR_ERR(root);
^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) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) test_std_err(TEST_ALLOC_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) goto out;
^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) path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) test_std_err(TEST_ALLOC_EXTENT_BUFFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) path->slots[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) key.objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) key.type = BTRFS_EXTENT_CSUM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) setup_items_for_insert(root, path, &key, &value_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) item = btrfs_item_nr(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) key.offset = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Passing NULL trans here should be safe because we have plenty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * space in this leaf to split the item without having to split the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * leaf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = btrfs_split_item(NULL, root, path, &key, 17);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) test_err("split item failed %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^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) * Read the first slot, it should have the original key and contain only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * 'mary had a little'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) btrfs_item_key_to_cpu(eb, &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) key.offset != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) test_err("invalid key at slot 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) item = btrfs_item_nr(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (btrfs_item_size(eb, item) != strlen(split1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) test_err("invalid len in the first split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) strlen(split1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (memcmp(buf, split1, strlen(split1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) "data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (int)strlen(split1), buf, split1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) btrfs_item_key_to_cpu(eb, &key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) key.offset != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) test_err("invalid key at slot 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) item = btrfs_item_nr(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (btrfs_item_size(eb, item) != strlen(split2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) test_err("invalid len in the second split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) strlen(split2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (memcmp(buf, split2, strlen(split2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "data in the buffer doesn't match what it should in the second split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) key.offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* Do it again so we test memmoving the other items in the leaf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = btrfs_split_item(NULL, root, path, &key, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) test_err("second split item failed %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) btrfs_item_key_to_cpu(eb, &key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) key.offset != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) test_err("invalid key at slot 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) item = btrfs_item_nr(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (btrfs_item_size(eb, item) != strlen(split3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) test_err("invalid len in the first split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) strlen(split3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (memcmp(buf, split3, strlen(split3))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) "data in the buffer doesn't match what it should in the third split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) btrfs_item_key_to_cpu(eb, &key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) key.offset != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) test_err("invalid key at slot 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) item = btrfs_item_nr(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (btrfs_item_size(eb, item) != strlen(split4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) test_err("invalid len in the second split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) strlen(split4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (memcmp(buf, split4, strlen(split4))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) "data in the buffer doesn't match what it should in the fourth split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) btrfs_item_key_to_cpu(eb, &key, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) key.offset != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) test_err("invalid key at slot 2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = -EINVAL;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) item = btrfs_item_nr(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (btrfs_item_size(eb, item) != strlen(split2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) test_err("invalid len in the second split");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) strlen(split2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (memcmp(buf, split2, strlen(split2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) "data in the buffer doesn't match what it should in the last chunk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) btrfs_free_dummy_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) btrfs_free_dummy_fs_info(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) test_msg("running extent buffer operation tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return test_btrfs_split_item(sectorsize, nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }