1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2013 Fusion IO. All rights reserved.
4 */
5
6 #include <linux/slab.h>
7 #include "btrfs-tests.h"
8 #include "../ctree.h"
9 #include "../extent_io.h"
10 #include "../disk-io.h"
11
test_btrfs_split_item(u32 sectorsize,u32 nodesize)12 static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
13 {
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 char *value = "mary had a little lamb";
19 char *split1 = "mary had a little";
20 char *split2 = " lamb";
21 char *split3 = "mary";
22 char *split4 = " had a little";
23 char buf[32];
24 struct btrfs_key key;
25 u32 value_len = strlen(value);
26 int ret = 0;
27
28 test_msg("running btrfs_split_item tests");
29
30 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
31 if (!fs_info) {
32 test_std_err(TEST_ALLOC_FS_INFO);
33 return -ENOMEM;
34 }
35
36 root = btrfs_alloc_dummy_root(fs_info);
37 if (IS_ERR(root)) {
38 test_std_err(TEST_ALLOC_ROOT);
39 ret = PTR_ERR(root);
40 goto out;
41 }
42
43 path = btrfs_alloc_path();
44 if (!path) {
45 test_std_err(TEST_ALLOC_PATH);
46 ret = -ENOMEM;
47 goto out;
48 }
49
50 path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
51 if (!eb) {
52 test_std_err(TEST_ALLOC_EXTENT_BUFFER);
53 ret = -ENOMEM;
54 goto out;
55 }
56 path->slots[0] = 0;
57
58 key.objectid = 0;
59 key.type = BTRFS_EXTENT_CSUM_KEY;
60 key.offset = 0;
61
62 btrfs_setup_item_for_insert(root, path, &key, value_len);
63 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
64 value_len);
65
66 key.offset = 3;
67
68 /*
69 * Passing NULL trans here should be safe because we have plenty of
70 * space in this leaf to split the item without having to split the
71 * leaf.
72 */
73 ret = btrfs_split_item(NULL, root, path, &key, 17);
74 if (ret) {
75 test_err("split item failed %d", ret);
76 goto out;
77 }
78
79 /*
80 * Read the first slot, it should have the original key and contain only
81 * 'mary had a little'
82 */
83 btrfs_item_key_to_cpu(eb, &key, 0);
84 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
85 key.offset != 0) {
86 test_err("invalid key at slot 0");
87 ret = -EINVAL;
88 goto out;
89 }
90
91 if (btrfs_item_size(eb, 0) != strlen(split1)) {
92 test_err("invalid len in the first split");
93 ret = -EINVAL;
94 goto out;
95 }
96
97 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
98 strlen(split1));
99 if (memcmp(buf, split1, strlen(split1))) {
100 test_err(
101 "data in the buffer doesn't match what it should in the first split have='%.*s' want '%s'",
102 (int)strlen(split1), buf, split1);
103 ret = -EINVAL;
104 goto out;
105 }
106
107 btrfs_item_key_to_cpu(eb, &key, 1);
108 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
109 key.offset != 3) {
110 test_err("invalid key at slot 1");
111 ret = -EINVAL;
112 goto out;
113 }
114
115 if (btrfs_item_size(eb, 1) != strlen(split2)) {
116 test_err("invalid len in the second split");
117 ret = -EINVAL;
118 goto out;
119 }
120
121 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
122 strlen(split2));
123 if (memcmp(buf, split2, strlen(split2))) {
124 test_err(
125 "data in the buffer doesn't match what it should in the second split");
126 ret = -EINVAL;
127 goto out;
128 }
129
130 key.offset = 1;
131 /* Do it again so we test memmoving the other items in the leaf */
132 ret = btrfs_split_item(NULL, root, path, &key, 4);
133 if (ret) {
134 test_err("second split item failed %d", ret);
135 goto out;
136 }
137
138 btrfs_item_key_to_cpu(eb, &key, 0);
139 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
140 key.offset != 0) {
141 test_err("invalid key at slot 0");
142 ret = -EINVAL;
143 goto out;
144 }
145
146 if (btrfs_item_size(eb, 0) != strlen(split3)) {
147 test_err("invalid len in the first split");
148 ret = -EINVAL;
149 goto out;
150 }
151
152 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0),
153 strlen(split3));
154 if (memcmp(buf, split3, strlen(split3))) {
155 test_err(
156 "data in the buffer doesn't match what it should in the third split");
157 ret = -EINVAL;
158 goto out;
159 }
160
161 btrfs_item_key_to_cpu(eb, &key, 1);
162 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
163 key.offset != 1) {
164 test_err("invalid key at slot 1");
165 ret = -EINVAL;
166 goto out;
167 }
168
169 if (btrfs_item_size(eb, 1) != strlen(split4)) {
170 test_err("invalid len in the second split");
171 ret = -EINVAL;
172 goto out;
173 }
174
175 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1),
176 strlen(split4));
177 if (memcmp(buf, split4, strlen(split4))) {
178 test_err(
179 "data in the buffer doesn't match what it should in the fourth split");
180 ret = -EINVAL;
181 goto out;
182 }
183
184 btrfs_item_key_to_cpu(eb, &key, 2);
185 if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY ||
186 key.offset != 3) {
187 test_err("invalid key at slot 2");
188 ret = -EINVAL;
189 goto out;
190 }
191
192 if (btrfs_item_size(eb, 2) != strlen(split2)) {
193 test_err("invalid len in the second split");
194 ret = -EINVAL;
195 goto out;
196 }
197
198 read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2),
199 strlen(split2));
200 if (memcmp(buf, split2, strlen(split2))) {
201 test_err(
202 "data in the buffer doesn't match what it should in the last chunk");
203 ret = -EINVAL;
204 goto out;
205 }
206 out:
207 btrfs_free_path(path);
208 btrfs_free_dummy_root(root);
209 btrfs_free_dummy_fs_info(fs_info);
210 return ret;
211 }
212
btrfs_test_extent_buffer_operations(u32 sectorsize,u32 nodesize)213 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
214 {
215 test_msg("running extent buffer operation tests");
216 return test_btrfs_split_item(sectorsize, nodesize);
217 }
218