extent-buffer-tests.c 5.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2013 Fusion IO.  All rights reserved.
 */

#include <linux/slab.h>
#include "btrfs-tests.h"
#include "../ctree.h"
#include "../extent_io.h"
#include "../disk-io.h"

12
static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
13
{
14 15 16
	struct btrfs_fs_info *fs_info;
	struct btrfs_path *path = NULL;
	struct btrfs_root *root = NULL;
17 18 19 20 21 22 23 24 25 26 27 28
	struct extent_buffer *eb;
	struct btrfs_item *item;
	char *value = "mary had a little lamb";
	char *split1 = "mary had a little";
	char *split2 = " lamb";
	char *split3 = "mary";
	char *split4 = " had a little";
	char buf[32];
	struct btrfs_key key;
	u32 value_len = strlen(value);
	int ret = 0;

29
	test_msg("running btrfs_split_item tests");
30

31
	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
32
	if (!fs_info) {
33
		test_std_err(TEST_ALLOC_FS_INFO);
34 35 36
		return -ENOMEM;
	}

37
	root = btrfs_alloc_dummy_root(fs_info);
38
	if (IS_ERR(root)) {
39
		test_err("could not allocate root");
40 41
		ret = PTR_ERR(root);
		goto out;
42 43 44 45
	}

	path = btrfs_alloc_path();
	if (!path) {
46
		test_err("could not allocate path");
47 48
		ret = -ENOMEM;
		goto out;
49 50
	}

51
	path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
52
	if (!eb) {
53
		test_err("could not allocate dummy buffer");
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		ret = -ENOMEM;
		goto out;
	}
	path->slots[0] = 0;

	key.objectid = 0;
	key.type = BTRFS_EXTENT_CSUM_KEY;
	key.offset = 0;

	setup_items_for_insert(root, path, &key, &value_len, value_len,
			       value_len + sizeof(struct btrfs_item), 1);
	item = btrfs_item_nr(0);
	write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
			    value_len);

	key.offset = 3;

	/*
	 * Passing NULL trans here should be safe because we have plenty of
	 * space in this leaf to split the item without having to split the
	 * leaf.
	 */
	ret = btrfs_split_item(NULL, root, path, &key, 17);
	if (ret) {
78
		test_err("split item failed %d", ret);
79 80 81 82 83 84 85 86 87 88
		goto out;
	}

	/*
	 * Read the first slot, it should have the original key and contain only
	 * 'mary had a little'
	 */
	btrfs_item_key_to_cpu(eb, &key, 0);
	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
	    key.offset != 0) {
89
		test_err("invalid key at slot 0");
90 91 92 93 94 95
		ret = -EINVAL;
		goto out;
	}

	item = btrfs_item_nr(0);
	if (btrfs_item_size(eb, item) != strlen(split1)) {
96
		test_err("invalid len in the first split");
97 98 99 100 101 102 103
		ret = -EINVAL;
		goto out;
	}

	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
			   strlen(split1));
	if (memcmp(buf, split1, strlen(split1))) {
104 105
		test_err(
"data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
106 107 108 109 110 111 112 113
			 (int)strlen(split1), buf, split1);
		ret = -EINVAL;
		goto out;
	}

	btrfs_item_key_to_cpu(eb, &key, 1);
	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
	    key.offset != 3) {
114
		test_err("invalid key at slot 1");
115 116 117 118 119 120
		ret = -EINVAL;
		goto out;
	}

	item = btrfs_item_nr(1);
	if (btrfs_item_size(eb, item) != strlen(split2)) {
121
		test_err("invalid len in the second split");
122 123 124 125 126 127 128
		ret = -EINVAL;
		goto out;
	}

	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
			   strlen(split2));
	if (memcmp(buf, split2, strlen(split2))) {
129 130
		test_err(
	"data in the buffer doesn't match what it should in the second split");
131 132 133 134 135 136 137 138
		ret = -EINVAL;
		goto out;
	}

	key.offset = 1;
	/* Do it again so we test memmoving the other items in the leaf */
	ret = btrfs_split_item(NULL, root, path, &key, 4);
	if (ret) {
139
		test_err("second split item failed %d", ret);
140 141 142 143 144 145
		goto out;
	}

	btrfs_item_key_to_cpu(eb, &key, 0);
	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
	    key.offset != 0) {
146
		test_err("invalid key at slot 0");
147 148 149 150 151 152
		ret = -EINVAL;
		goto out;
	}

	item = btrfs_item_nr(0);
	if (btrfs_item_size(eb, item) != strlen(split3)) {
153
		test_err("invalid len in the first split");
154 155 156 157 158 159 160
		ret = -EINVAL;
		goto out;
	}

	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
			   strlen(split3));
	if (memcmp(buf, split3, strlen(split3))) {
161 162
		test_err(
	"data in the buffer doesn't match what it should in the third split");
163 164 165 166 167 168 169
		ret = -EINVAL;
		goto out;
	}

	btrfs_item_key_to_cpu(eb, &key, 1);
	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
	    key.offset != 1) {
170
		test_err("invalid key at slot 1");
171 172 173 174 175 176
		ret = -EINVAL;
		goto out;
	}

	item = btrfs_item_nr(1);
	if (btrfs_item_size(eb, item) != strlen(split4)) {
177
		test_err("invalid len in the second split");
178 179 180 181 182 183 184
		ret = -EINVAL;
		goto out;
	}

	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
			   strlen(split4));
	if (memcmp(buf, split4, strlen(split4))) {
185 186
		test_err(
	"data in the buffer doesn't match what it should in the fourth split");
187 188 189 190 191 192 193
		ret = -EINVAL;
		goto out;
	}

	btrfs_item_key_to_cpu(eb, &key, 2);
	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
	    key.offset != 3) {
194
		test_err("invalid key at slot 2");
195 196 197 198 199 200
		ret = -EINVAL;
		goto out;
	}

	item = btrfs_item_nr(2);
	if (btrfs_item_size(eb, item) != strlen(split2)) {
201
		test_err("invalid len in the second split");
202 203 204 205 206 207 208
		ret = -EINVAL;
		goto out;
	}

	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
			   strlen(split2));
	if (memcmp(buf, split2, strlen(split2))) {
209 210
		test_err(
	"data in the buffer doesn't match what it should in the last chunk");
211 212 213 214 215
		ret = -EINVAL;
		goto out;
	}
out:
	btrfs_free_path(path);
216 217
	btrfs_free_dummy_root(root);
	btrfs_free_dummy_fs_info(fs_info);
218 219 220
	return ret;
}

221
int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
222
{
223
	test_msg("running extent buffer operation tests");
224
	return test_btrfs_split_item(sectorsize, nodesize);
225
}