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(nodesize, sectorsize);
46 test_msg("Could not allocate fs_info\n");
50 root = btrfs_alloc_dummy_root(fs_info);
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(fs_info, nodesize);
66 test_msg("Could not allocate dummy buffer\n");
73 key.type = BTRFS_EXTENT_CSUM_KEY;
76 setup_items_for_insert(root, path, &key, &value_len, value_len,
77 value_len + sizeof(struct btrfs_item), 1);
78 item = btrfs_item_nr(0);
79 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
85 * Passing NULL trans here should be safe because we have plenty of
86 * space in this leaf to split the item without having to split the
89 ret = btrfs_split_item(NULL, root, path, &key, 17);
91 test_msg("Split item failed %d\n", ret);
96 * Read the first slot, it should have the original key and contain only
99 btrfs_item_key_to_cpu(eb, &key, 0);
100 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
102 test_msg("Invalid key at slot 0\n");
107 item = btrfs_item_nr(0);
108 if (btrfs_item_size(eb, item) != strlen(split1)) {
109 test_msg("Invalid len in the first split\n");
114 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
116 if (memcmp(buf, split1, strlen(split1))) {
117 test_msg("Data in the buffer doesn't match what it should "
118 "in the first split have='%.*s' want '%s'\n",
119 (int)strlen(split1), buf, split1);
124 btrfs_item_key_to_cpu(eb, &key, 1);
125 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
127 test_msg("Invalid key at slot 1\n");
132 item = btrfs_item_nr(1);
133 if (btrfs_item_size(eb, item) != strlen(split2)) {
134 test_msg("Invalid len in the second split\n");
139 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
141 if (memcmp(buf, split2, strlen(split2))) {
142 test_msg("Data in the buffer doesn't match what it should "
143 "in the second split\n");
149 /* Do it again so we test memmoving the other items in the leaf */
150 ret = btrfs_split_item(NULL, root, path, &key, 4);
152 test_msg("Second split item failed %d\n", ret);
156 btrfs_item_key_to_cpu(eb, &key, 0);
157 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
159 test_msg("Invalid key at slot 0\n");
164 item = btrfs_item_nr(0);
165 if (btrfs_item_size(eb, item) != strlen(split3)) {
166 test_msg("Invalid len in the first split\n");
171 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
173 if (memcmp(buf, split3, strlen(split3))) {
174 test_msg("Data in the buffer doesn't match what it should "
175 "in the third split");
180 btrfs_item_key_to_cpu(eb, &key, 1);
181 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
183 test_msg("Invalid key at slot 1\n");
188 item = btrfs_item_nr(1);
189 if (btrfs_item_size(eb, item) != strlen(split4)) {
190 test_msg("Invalid len in the second split\n");
195 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
197 if (memcmp(buf, split4, strlen(split4))) {
198 test_msg("Data in the buffer doesn't match what it should "
199 "in the fourth split\n");
204 btrfs_item_key_to_cpu(eb, &key, 2);
205 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
207 test_msg("Invalid key at slot 2\n");
212 item = btrfs_item_nr(2);
213 if (btrfs_item_size(eb, item) != strlen(split2)) {
214 test_msg("Invalid len in the second split\n");
219 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
221 if (memcmp(buf, split2, strlen(split2))) {
222 test_msg("Data in the buffer doesn't match what it should "
223 "in the last chunk\n");
228 btrfs_free_path(path);
229 btrfs_free_dummy_root(root);
230 btrfs_free_dummy_fs_info(fs_info);
234 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
236 test_msg("Running extent buffer operation tests\n");
237 return test_btrfs_split_item(sectorsize, nodesize);