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"
11 #include "../accessors.h"
13 static int test_btrfs_split_item(u32 sectorsize
, u32 nodesize
)
15 struct btrfs_fs_info
*fs_info
;
16 struct btrfs_path
*path
= NULL
;
17 struct btrfs_root
*root
= NULL
;
18 struct extent_buffer
*eb
;
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_std_err(TEST_ALLOC_FS_INFO
);
37 root
= btrfs_alloc_dummy_root(fs_info
);
39 test_std_err(TEST_ALLOC_ROOT
);
44 path
= btrfs_alloc_path();
46 test_std_err(TEST_ALLOC_PATH
);
51 eb
= alloc_dummy_extent_buffer(fs_info
, nodesize
);
54 test_std_err(TEST_ALLOC_EXTENT_BUFFER
);
61 key
.type
= BTRFS_EXTENT_CSUM_KEY
;
65 * Passing a NULL trans handle is fine here, we have a dummy root eb
66 * and the tree is a single node (level 0).
68 btrfs_setup_item_for_insert(NULL
, root
, path
, &key
, value_len
);
69 write_extent_buffer(eb
, value
, btrfs_item_ptr_offset(eb
, 0),
75 * Passing NULL trans here should be safe because we have plenty of
76 * space in this leaf to split the item without having to split the
79 ret
= btrfs_split_item(NULL
, root
, path
, &key
, 17);
81 test_err("split item failed %d", ret
);
86 * Read the first slot, it should have the original key and contain only
89 btrfs_item_key_to_cpu(eb
, &key
, 0);
90 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
92 test_err("invalid key at slot 0");
97 if (btrfs_item_size(eb
, 0) != strlen(split1
)) {
98 test_err("invalid len in the first split");
103 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 0),
105 if (memcmp(buf
, split1
, strlen(split1
))) {
107 "data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
108 (int)strlen(split1
), buf
, split1
);
113 btrfs_item_key_to_cpu(eb
, &key
, 1);
114 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
116 test_err("invalid key at slot 1");
121 if (btrfs_item_size(eb
, 1) != strlen(split2
)) {
122 test_err("invalid len in the second split");
127 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 1),
129 if (memcmp(buf
, split2
, strlen(split2
))) {
131 "data in the buffer doesn't match what it should in the second split");
137 /* Do it again so we test memmoving the other items in the leaf */
138 ret
= btrfs_split_item(NULL
, root
, path
, &key
, 4);
140 test_err("second split item failed %d", ret
);
144 btrfs_item_key_to_cpu(eb
, &key
, 0);
145 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
147 test_err("invalid key at slot 0");
152 if (btrfs_item_size(eb
, 0) != 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 if (btrfs_item_size(eb
, 1) != strlen(split4
)) {
176 test_err("invalid len in the second split");
181 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 1),
183 if (memcmp(buf
, split4
, strlen(split4
))) {
185 "data in the buffer doesn't match what it should in the fourth split");
190 btrfs_item_key_to_cpu(eb
, &key
, 2);
191 if (key
.objectid
!= 0 || key
.type
!= BTRFS_EXTENT_CSUM_KEY
||
193 test_err("invalid key at slot 2");
198 if (btrfs_item_size(eb
, 2) != strlen(split2
)) {
199 test_err("invalid len in the second split");
204 read_extent_buffer(eb
, buf
, btrfs_item_ptr_offset(eb
, 2),
206 if (memcmp(buf
, split2
, strlen(split2
))) {
208 "data in the buffer doesn't match what it should in the last chunk");
213 btrfs_free_path(path
);
214 btrfs_free_dummy_root(root
);
215 btrfs_free_dummy_fs_info(fs_info
);
219 int btrfs_test_extent_buffer_operations(u32 sectorsize
, u32 nodesize
)
221 test_msg("running extent buffer operation tests");
222 return test_btrfs_split_item(sectorsize
, nodesize
);