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