Linux Audio

Check our new training course

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