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(void)
27 struct btrfs_path
*path
;
28 struct btrfs_root
*root
;
29 struct extent_buffer
*eb
;
30 struct btrfs_item
*item
;
31 char *value
= "mary had a little lamb";
32 char *split1
= "mary had a little";
33 char *split2
= " lamb";
34 char *split3
= "mary";
35 char *split4
= " had a little";
38 u32 value_len
= strlen(value
);
41 test_msg("Running btrfs_split_item tests\n");
43 root
= btrfs_alloc_dummy_root();
45 test_msg("Could not allocate root\n");
49 path
= btrfs_alloc_path();
51 test_msg("Could not allocate path\n");
56 path
->nodes
[0] = eb
= alloc_dummy_extent_buffer(0, 4096);
58 test_msg("Could not allocate dummy buffer\n");
65 key
.type
= BTRFS_EXTENT_CSUM_KEY
;
68 setup_items_for_insert(root
, path
, &key
, &value_len
, value_len
,
69 value_len
+ sizeof(struct btrfs_item
), 1);
70 item
= btrfs_item_nr(0);
71 write_extent_buffer(eb
, value
, btrfs_item_ptr_offset(eb
, 0),
77 * Passing NULL trans here should be safe because we have plenty of
78 * space in this leaf to split the item without having to split the
81 ret
= btrfs_split_item(NULL
, root
, path
, &key
, 17);
83 test_msg("Split item failed %d\n", ret
);
88 * Read the first slot, it should have the original key and contain only
91 btrfs_item_key_to_cpu(eb
, &key
, 0);
92 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
94 test_msg("Invalid key at slot 0\n");
99 item
= btrfs_item_nr(0);
100 if (btrfs_item_size(eb
, item
) != strlen(split1
)) {
101 test_msg("Invalid len in the first split\n");
106 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 0),
108 if (memcmp(buf
, split1
, strlen(split1
))) {
109 test_msg("Data in the buffer doesn't match what it should "
110 "in the first split have='%.*s' want '%s'\n",
111 (int)strlen(split1
), buf
, split1
);
116 btrfs_item_key_to_cpu(eb
, &key
, 1);
117 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
119 test_msg("Invalid key at slot 1\n");
124 item
= btrfs_item_nr(1);
125 if (btrfs_item_size(eb
, item
) != strlen(split2
)) {
126 test_msg("Invalid len in the second split\n");
131 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 1),
133 if (memcmp(buf
, split2
, strlen(split2
))) {
134 test_msg("Data in the buffer doesn't match what it should "
135 "in the second split\n");
141 /* Do it again so we test memmoving the other items in the leaf */
142 ret
= btrfs_split_item(NULL
, root
, path
, &key
, 4);
144 test_msg("Second split item failed %d\n", ret
);
148 btrfs_item_key_to_cpu(eb
, &key
, 0);
149 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
151 test_msg("Invalid key at slot 0\n");
156 item
= btrfs_item_nr(0);
157 if (btrfs_item_size(eb
, item
) != strlen(split3
)) {
158 test_msg("Invalid len in the first split\n");
163 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 0),
165 if (memcmp(buf
, split3
, strlen(split3
))) {
166 test_msg("Data in the buffer doesn't match what it should "
167 "in the third split");
172 btrfs_item_key_to_cpu(eb
, &key
, 1);
173 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
175 test_msg("Invalid key at slot 1\n");
180 item
= btrfs_item_nr(1);
181 if (btrfs_item_size(eb
, item
) != strlen(split4
)) {
182 test_msg("Invalid len in the second split\n");
187 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 1),
189 if (memcmp(buf
, split4
, strlen(split4
))) {
190 test_msg("Data in the buffer doesn't match what it should "
191 "in the fourth split\n");
196 btrfs_item_key_to_cpu(eb
, &key
, 2);
197 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
199 test_msg("Invalid key at slot 2\n");
204 item
= btrfs_item_nr(2);
205 if (btrfs_item_size(eb
, item
) != strlen(split2
)) {
206 test_msg("Invalid len in the second split\n");
211 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 2),
213 if (memcmp(buf
, split2
, strlen(split2
))) {
214 test_msg("Data in the buffer doesn't match what it should "
215 "in the last chunk\n");
220 btrfs_free_path(path
);
225 int btrfs_test_extent_buffer_operations(void)
227 test_msg("Running extent buffer operation tests");
228 return test_btrfs_split_item();