2 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/slab.h>
20 #include "btrfs-tests.h"
22 #include "../extent_io.h"
23 #include "../disk-io.h"
25 static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
27 struct btrfs_fs_info *fs_info;
28 struct btrfs_path *path = NULL;
29 struct btrfs_root *root = NULL;
30 struct extent_buffer *eb;
31 struct btrfs_item *item;
32 char *value = "mary had a little lamb";
33 char *split1 = "mary had a little";
34 char *split2 = " lamb";
35 char *split3 = "mary";
36 char *split4 = " had a little";
39 u32 value_len = strlen(value);
42 test_msg("Running btrfs_split_item tests\n");
44 fs_info = btrfs_alloc_dummy_fs_info();
46 test_msg("Could not allocate fs_info\n");
50 root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
52 test_msg("Could not allocate root\n");
57 path = btrfs_alloc_path();
59 test_msg("Could not allocate path\n");
64 path->nodes[0] = eb = alloc_dummy_extent_buffer(NULL, nodesize,
67 test_msg("Could not allocate dummy buffer\n");
74 key.type = BTRFS_EXTENT_CSUM_KEY;
77 setup_items_for_insert(root, path, &key, &value_len, value_len,
78 value_len + sizeof(struct btrfs_item), 1);
79 item = btrfs_item_nr(0);
80 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
86 * Passing NULL trans here should be safe because we have plenty of
87 * space in this leaf to split the item without having to split the
90 ret = btrfs_split_item(NULL, root, path, &key, 17);
92 test_msg("Split item failed %d\n", ret);
97 * Read the first slot, it should have the original key and contain only
100 btrfs_item_key_to_cpu(eb, &key, 0);
101 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
103 test_msg("Invalid key at slot 0\n");
108 item = btrfs_item_nr(0);
109 if (btrfs_item_size(eb, item) != strlen(split1)) {
110 test_msg("Invalid len in the first split\n");
115 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
117 if (memcmp(buf, split1, strlen(split1))) {
118 test_msg("Data in the buffer doesn't match what it should "
119 "in the first split have='%.*s' want '%s'\n",
120 (int)strlen(split1), buf, split1);
125 btrfs_item_key_to_cpu(eb, &key, 1);
126 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
128 test_msg("Invalid key at slot 1\n");
133 item = btrfs_item_nr(1);
134 if (btrfs_item_size(eb, item) != strlen(split2)) {
135 test_msg("Invalid len in the second split\n");
140 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
142 if (memcmp(buf, split2, strlen(split2))) {
143 test_msg("Data in the buffer doesn't match what it should "
144 "in the second split\n");
150 /* Do it again so we test memmoving the other items in the leaf */
151 ret = btrfs_split_item(NULL, root, path, &key, 4);
153 test_msg("Second split item failed %d\n", ret);
157 btrfs_item_key_to_cpu(eb, &key, 0);
158 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
160 test_msg("Invalid key at slot 0\n");
165 item = btrfs_item_nr(0);
166 if (btrfs_item_size(eb, item) != strlen(split3)) {
167 test_msg("Invalid len in the first split\n");
172 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
174 if (memcmp(buf, split3, strlen(split3))) {
175 test_msg("Data in the buffer doesn't match what it should "
176 "in the third split");
181 btrfs_item_key_to_cpu(eb, &key, 1);
182 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
184 test_msg("Invalid key at slot 1\n");
189 item = btrfs_item_nr(1);
190 if (btrfs_item_size(eb, item) != strlen(split4)) {
191 test_msg("Invalid len in the second split\n");
196 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
198 if (memcmp(buf, split4, strlen(split4))) {
199 test_msg("Data in the buffer doesn't match what it should "
200 "in the fourth split\n");
205 btrfs_item_key_to_cpu(eb, &key, 2);
206 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
208 test_msg("Invalid key at slot 2\n");
213 item = btrfs_item_nr(2);
214 if (btrfs_item_size(eb, item) != strlen(split2)) {
215 test_msg("Invalid len in the second split\n");
220 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
222 if (memcmp(buf, split2, strlen(split2))) {
223 test_msg("Data in the buffer doesn't match what it should "
224 "in the last chunk\n");
229 btrfs_free_path(path);
230 btrfs_free_dummy_root(root);
231 btrfs_free_dummy_fs_info(fs_info);
235 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
237 test_msg("Running extent buffer operation tests\n");
238 return test_btrfs_split_item(sectorsize, nodesize);