1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
6 #include <linux/slab.h>
7 #include "btrfs-tests.h"
9 #include "../extent_io.h"
10 #include "../disk-io.h"
12 static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
14 struct btrfs_fs_info *fs_info;
15 struct btrfs_path *path = NULL;
16 struct btrfs_root *root = NULL;
17 struct extent_buffer *eb;
18 struct btrfs_item *item;
19 char *value = "mary had a little lamb";
20 char *split1 = "mary had a little";
21 char *split2 = " lamb";
22 char *split3 = "mary";
23 char *split4 = " had a little";
26 u32 value_len = strlen(value);
29 test_msg("running btrfs_split_item tests");
31 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
33 test_err("could not allocate fs_info");
37 root = btrfs_alloc_dummy_root(fs_info);
39 test_err("could not allocate root");
44 path = btrfs_alloc_path();
46 test_err("could not allocate path");
51 path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
53 test_err("could not allocate dummy buffer");
60 key.type = BTRFS_EXTENT_CSUM_KEY;
63 setup_items_for_insert(root, path, &key, &value_len, value_len,
64 value_len + sizeof(struct btrfs_item), 1);
65 item = btrfs_item_nr(0);
66 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
72 * Passing NULL trans here should be safe because we have plenty of
73 * space in this leaf to split the item without having to split the
76 ret = btrfs_split_item(NULL, root, path, &key, 17);
78 test_err("split item failed %d", ret);
83 * Read the first slot, it should have the original key and contain only
86 btrfs_item_key_to_cpu(eb, &key, 0);
87 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
89 test_err("invalid key at slot 0");
94 item = btrfs_item_nr(0);
95 if (btrfs_item_size(eb, item) != strlen(split1)) {
96 test_err("invalid len in the first split");
101 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
103 if (memcmp(buf, split1, strlen(split1))) {
105 "data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
106 (int)strlen(split1), buf, split1);
111 btrfs_item_key_to_cpu(eb, &key, 1);
112 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
114 test_err("invalid key at slot 1");
119 item = btrfs_item_nr(1);
120 if (btrfs_item_size(eb, item) != strlen(split2)) {
121 test_err("invalid len in the second split");
126 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
128 if (memcmp(buf, split2, strlen(split2))) {
130 "data in the buffer doesn't match what it should in the second split");
136 /* Do it again so we test memmoving the other items in the leaf */
137 ret = btrfs_split_item(NULL, root, path, &key, 4);
139 test_err("second split item failed %d", ret);
143 btrfs_item_key_to_cpu(eb, &key, 0);
144 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
146 test_err("invalid key at slot 0");
151 item = btrfs_item_nr(0);
152 if (btrfs_item_size(eb, item) != strlen(split3)) {
153 test_err("invalid len in the first split");
158 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
160 if (memcmp(buf, split3, strlen(split3))) {
162 "data in the buffer doesn't match what it should in the third split");
167 btrfs_item_key_to_cpu(eb, &key, 1);
168 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
170 test_err("invalid key at slot 1");
175 item = btrfs_item_nr(1);
176 if (btrfs_item_size(eb, item) != strlen(split4)) {
177 test_err("invalid len in the second split");
182 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
184 if (memcmp(buf, split4, strlen(split4))) {
186 "data in the buffer doesn't match what it should in the fourth split");
191 btrfs_item_key_to_cpu(eb, &key, 2);
192 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
194 test_err("invalid key at slot 2");
199 item = btrfs_item_nr(2);
200 if (btrfs_item_size(eb, item) != strlen(split2)) {
201 test_err("invalid len in the second split");
206 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
208 if (memcmp(buf, split2, strlen(split2))) {
210 "data in the buffer doesn't match what it should in the last chunk");
215 btrfs_free_path(path);
216 btrfs_free_dummy_root(root);
217 btrfs_free_dummy_fs_info(fs_info);
221 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
223 test_msg("running extent buffer operation tests");
224 return test_btrfs_split_item(sectorsize, nodesize);