18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2013 Fusion IO.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/slab.h>
78c2ecf20Sopenharmony_ci#include "btrfs-tests.h"
88c2ecf20Sopenharmony_ci#include "../ctree.h"
98c2ecf20Sopenharmony_ci#include "../extent_io.h"
108c2ecf20Sopenharmony_ci#include "../disk-io.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	struct btrfs_fs_info *fs_info;
158c2ecf20Sopenharmony_ci	struct btrfs_path *path = NULL;
168c2ecf20Sopenharmony_ci	struct btrfs_root *root = NULL;
178c2ecf20Sopenharmony_ci	struct extent_buffer *eb;
188c2ecf20Sopenharmony_ci	struct btrfs_item *item;
198c2ecf20Sopenharmony_ci	char *value = "mary had a little lamb";
208c2ecf20Sopenharmony_ci	char *split1 = "mary had a little";
218c2ecf20Sopenharmony_ci	char *split2 = " lamb";
228c2ecf20Sopenharmony_ci	char *split3 = "mary";
238c2ecf20Sopenharmony_ci	char *split4 = " had a little";
248c2ecf20Sopenharmony_ci	char buf[32];
258c2ecf20Sopenharmony_ci	struct btrfs_key key;
268c2ecf20Sopenharmony_ci	u32 value_len = strlen(value);
278c2ecf20Sopenharmony_ci	int ret = 0;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	test_msg("running btrfs_split_item tests");
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
328c2ecf20Sopenharmony_ci	if (!fs_info) {
338c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_FS_INFO);
348c2ecf20Sopenharmony_ci		return -ENOMEM;
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	root = btrfs_alloc_dummy_root(fs_info);
388c2ecf20Sopenharmony_ci	if (IS_ERR(root)) {
398c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_ROOT);
408c2ecf20Sopenharmony_ci		ret = PTR_ERR(root);
418c2ecf20Sopenharmony_ci		goto out;
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	path = btrfs_alloc_path();
458c2ecf20Sopenharmony_ci	if (!path) {
468c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_PATH);
478c2ecf20Sopenharmony_ci		ret = -ENOMEM;
488c2ecf20Sopenharmony_ci		goto out;
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
528c2ecf20Sopenharmony_ci	if (!eb) {
538c2ecf20Sopenharmony_ci		test_std_err(TEST_ALLOC_EXTENT_BUFFER);
548c2ecf20Sopenharmony_ci		ret = -ENOMEM;
558c2ecf20Sopenharmony_ci		goto out;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci	path->slots[0] = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	key.objectid = 0;
608c2ecf20Sopenharmony_ci	key.type = BTRFS_EXTENT_CSUM_KEY;
618c2ecf20Sopenharmony_ci	key.offset = 0;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	setup_items_for_insert(root, path, &key, &value_len, 1);
648c2ecf20Sopenharmony_ci	item = btrfs_item_nr(0);
658c2ecf20Sopenharmony_ci	write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
668c2ecf20Sopenharmony_ci			    value_len);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	key.offset = 3;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * Passing NULL trans here should be safe because we have plenty of
728c2ecf20Sopenharmony_ci	 * space in this leaf to split the item without having to split the
738c2ecf20Sopenharmony_ci	 * leaf.
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	ret = btrfs_split_item(NULL, root, path, &key, 17);
768c2ecf20Sopenharmony_ci	if (ret) {
778c2ecf20Sopenharmony_ci		test_err("split item failed %d", ret);
788c2ecf20Sopenharmony_ci		goto out;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/*
828c2ecf20Sopenharmony_ci	 * Read the first slot, it should have the original key and contain only
838c2ecf20Sopenharmony_ci	 * 'mary had a little'
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(eb, &key, 0);
868c2ecf20Sopenharmony_ci	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
878c2ecf20Sopenharmony_ci	    key.offset != 0) {
888c2ecf20Sopenharmony_ci		test_err("invalid key at slot 0");
898c2ecf20Sopenharmony_ci		ret = -EINVAL;
908c2ecf20Sopenharmony_ci		goto out;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	item = btrfs_item_nr(0);
948c2ecf20Sopenharmony_ci	if (btrfs_item_size(eb, item) != strlen(split1)) {
958c2ecf20Sopenharmony_ci		test_err("invalid len in the first split");
968c2ecf20Sopenharmony_ci		ret = -EINVAL;
978c2ecf20Sopenharmony_ci		goto out;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
1018c2ecf20Sopenharmony_ci			   strlen(split1));
1028c2ecf20Sopenharmony_ci	if (memcmp(buf, split1, strlen(split1))) {
1038c2ecf20Sopenharmony_ci		test_err(
1048c2ecf20Sopenharmony_ci"data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
1058c2ecf20Sopenharmony_ci			 (int)strlen(split1), buf, split1);
1068c2ecf20Sopenharmony_ci		ret = -EINVAL;
1078c2ecf20Sopenharmony_ci		goto out;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(eb, &key, 1);
1118c2ecf20Sopenharmony_ci	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
1128c2ecf20Sopenharmony_ci	    key.offset != 3) {
1138c2ecf20Sopenharmony_ci		test_err("invalid key at slot 1");
1148c2ecf20Sopenharmony_ci		ret = -EINVAL;
1158c2ecf20Sopenharmony_ci		goto out;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	item = btrfs_item_nr(1);
1198c2ecf20Sopenharmony_ci	if (btrfs_item_size(eb, item) != strlen(split2)) {
1208c2ecf20Sopenharmony_ci		test_err("invalid len in the second split");
1218c2ecf20Sopenharmony_ci		ret = -EINVAL;
1228c2ecf20Sopenharmony_ci		goto out;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
1268c2ecf20Sopenharmony_ci			   strlen(split2));
1278c2ecf20Sopenharmony_ci	if (memcmp(buf, split2, strlen(split2))) {
1288c2ecf20Sopenharmony_ci		test_err(
1298c2ecf20Sopenharmony_ci	"data in the buffer doesn't match what it should in the second split");
1308c2ecf20Sopenharmony_ci		ret = -EINVAL;
1318c2ecf20Sopenharmony_ci		goto out;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	key.offset = 1;
1358c2ecf20Sopenharmony_ci	/* Do it again so we test memmoving the other items in the leaf */
1368c2ecf20Sopenharmony_ci	ret = btrfs_split_item(NULL, root, path, &key, 4);
1378c2ecf20Sopenharmony_ci	if (ret) {
1388c2ecf20Sopenharmony_ci		test_err("second split item failed %d", ret);
1398c2ecf20Sopenharmony_ci		goto out;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(eb, &key, 0);
1438c2ecf20Sopenharmony_ci	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
1448c2ecf20Sopenharmony_ci	    key.offset != 0) {
1458c2ecf20Sopenharmony_ci		test_err("invalid key at slot 0");
1468c2ecf20Sopenharmony_ci		ret = -EINVAL;
1478c2ecf20Sopenharmony_ci		goto out;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	item = btrfs_item_nr(0);
1518c2ecf20Sopenharmony_ci	if (btrfs_item_size(eb, item) != strlen(split3)) {
1528c2ecf20Sopenharmony_ci		test_err("invalid len in the first split");
1538c2ecf20Sopenharmony_ci		ret = -EINVAL;
1548c2ecf20Sopenharmony_ci		goto out;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
1588c2ecf20Sopenharmony_ci			   strlen(split3));
1598c2ecf20Sopenharmony_ci	if (memcmp(buf, split3, strlen(split3))) {
1608c2ecf20Sopenharmony_ci		test_err(
1618c2ecf20Sopenharmony_ci	"data in the buffer doesn't match what it should in the third split");
1628c2ecf20Sopenharmony_ci		ret = -EINVAL;
1638c2ecf20Sopenharmony_ci		goto out;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(eb, &key, 1);
1678c2ecf20Sopenharmony_ci	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
1688c2ecf20Sopenharmony_ci	    key.offset != 1) {
1698c2ecf20Sopenharmony_ci		test_err("invalid key at slot 1");
1708c2ecf20Sopenharmony_ci		ret = -EINVAL;
1718c2ecf20Sopenharmony_ci		goto out;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	item = btrfs_item_nr(1);
1758c2ecf20Sopenharmony_ci	if (btrfs_item_size(eb, item) != strlen(split4)) {
1768c2ecf20Sopenharmony_ci		test_err("invalid len in the second split");
1778c2ecf20Sopenharmony_ci		ret = -EINVAL;
1788c2ecf20Sopenharmony_ci		goto out;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
1828c2ecf20Sopenharmony_ci			   strlen(split4));
1838c2ecf20Sopenharmony_ci	if (memcmp(buf, split4, strlen(split4))) {
1848c2ecf20Sopenharmony_ci		test_err(
1858c2ecf20Sopenharmony_ci	"data in the buffer doesn't match what it should in the fourth split");
1868c2ecf20Sopenharmony_ci		ret = -EINVAL;
1878c2ecf20Sopenharmony_ci		goto out;
1888c2ecf20Sopenharmony_ci	}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	btrfs_item_key_to_cpu(eb, &key, 2);
1918c2ecf20Sopenharmony_ci	if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
1928c2ecf20Sopenharmony_ci	    key.offset != 3) {
1938c2ecf20Sopenharmony_ci		test_err("invalid key at slot 2");
1948c2ecf20Sopenharmony_ci		ret = -EINVAL;
1958c2ecf20Sopenharmony_ci		goto out;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	item = btrfs_item_nr(2);
1998c2ecf20Sopenharmony_ci	if (btrfs_item_size(eb, item) != strlen(split2)) {
2008c2ecf20Sopenharmony_ci		test_err("invalid len in the second split");
2018c2ecf20Sopenharmony_ci		ret = -EINVAL;
2028c2ecf20Sopenharmony_ci		goto out;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
2068c2ecf20Sopenharmony_ci			   strlen(split2));
2078c2ecf20Sopenharmony_ci	if (memcmp(buf, split2, strlen(split2))) {
2088c2ecf20Sopenharmony_ci		test_err(
2098c2ecf20Sopenharmony_ci	"data in the buffer doesn't match what it should in the last chunk");
2108c2ecf20Sopenharmony_ci		ret = -EINVAL;
2118c2ecf20Sopenharmony_ci		goto out;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ciout:
2148c2ecf20Sopenharmony_ci	btrfs_free_path(path);
2158c2ecf20Sopenharmony_ci	btrfs_free_dummy_root(root);
2168c2ecf20Sopenharmony_ci	btrfs_free_dummy_fs_info(fs_info);
2178c2ecf20Sopenharmony_ci	return ret;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciint btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	test_msg("running extent buffer operation tests");
2238c2ecf20Sopenharmony_ci	return test_btrfs_split_item(sectorsize, nodesize);
2248c2ecf20Sopenharmony_ci}
225