5 #include "kerncompat.h"
6 #include "radix-tree.h"
9 #include "print-tree.h"
11 #include "transaction.h"
14 struct btrfs_super_block super;
15 static u64 dir_oid = 44556;
16 static u64 file_oid = 33778;
18 static int find_num(struct radix_tree_root *root, unsigned long *num_ret,
21 unsigned long num = rand();
26 ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
31 } else if (ret != 0 && num == res[0]) {
33 if (ret > 1 && num == res[1]) {
42 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
43 struct radix_tree_root *radix)
49 struct btrfs_path path;
50 struct btrfs_key inode_map;
52 find_num(radix, &oid, 0);
53 sprintf(buf, "str-%lu", oid);
55 ret = btrfs_find_free_objectid(trans, root, dir_oid + 1, &objectid);
59 inode_map.objectid = objectid;
63 ret = btrfs_insert_inode_map(trans, root, objectid, &inode_map);
66 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
71 radix_tree_preload(GFP_KERNEL);
72 ret = radix_tree_insert(radix, oid, (void *)oid);
73 radix_tree_preload_end();
82 * if we got an EEXIST, it may be due to hash collision, double
85 btrfs_init_path(&path);
86 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
90 if (!btrfs_match_dir_item_name(root, &path, buf, strlen(buf))) {
91 struct btrfs_dir_item *di;
97 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
98 struct btrfs_dir_item);
99 found = (char *)(di + 1);
100 found_len = btrfs_dir_name_len(di);
101 btrfs_name_hash(buf, strlen(buf), &myhash);
102 btrfs_name_hash(found, found_len, &foundhash);
103 if (myhash != foundhash)
105 btrfs_release_path(root, &path);
109 btrfs_release_path(root, &path);
111 printf("failed to insert %lu ret %d\n", oid, ret);
115 static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
116 *root, struct radix_tree_root *radix)
122 ret = find_num(radix, &oid, 1);
125 sprintf(buf, "str-%lu", oid);
127 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
129 if (ret != -EEXIST) {
130 printf("insert on %s gave us %d\n", buf, ret);
136 static int del_dir_item(struct btrfs_trans_handle *trans,
137 struct btrfs_root *root,
138 struct radix_tree_root *radix,
139 unsigned long radix_index,
140 struct btrfs_path *path)
145 struct btrfs_dir_item *di;
146 struct btrfs_path map_path;
148 /* find the inode number of the file */
149 di = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
150 struct btrfs_dir_item);
151 file_objectid = btrfs_dir_objectid(di);
153 /* delete the directory item */
154 ret = btrfs_del_item(trans, root, path);
158 /* delete the inode mapping */
159 btrfs_init_path(&map_path);
160 ret = btrfs_lookup_inode_map(trans, root, &map_path, file_objectid, -1);
163 ret = btrfs_del_item(trans, root->fs_info->inode_root, &map_path);
167 if (root->fs_info->last_inode_alloc > file_objectid)
168 root->fs_info->last_inode_alloc = file_objectid;
169 btrfs_release_path(root, &map_path);
170 ptr = radix_tree_delete(radix, radix_index);
177 btrfs_release_path(root, &map_path);
179 printf("failed to delete %lu %d\n", radix_index, ret);
183 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
184 struct radix_tree_root *radix)
189 struct btrfs_path path;
191 ret = find_num(radix, &oid, 1);
194 sprintf(buf, "str-%lu", oid);
195 btrfs_init_path(&path);
196 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
201 ret = del_dir_item(trans, root, radix, oid, &path);
204 btrfs_release_path(root, &path);
207 btrfs_release_path(root, &path);
208 printf("failed to delete %lu %d\n", oid, ret);
212 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
213 *root, struct radix_tree_root *radix)
215 struct btrfs_path path;
220 struct btrfs_dir_item *di;
222 ret = find_num(radix, &oid, 1);
225 sprintf(buf, "str-%lu", oid);
226 btrfs_init_path(&path);
227 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
230 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
231 struct btrfs_dir_item);
232 objectid = btrfs_dir_objectid(di);
233 btrfs_release_path(root, &path);
234 btrfs_init_path(&path);
235 ret = btrfs_lookup_inode_map(trans, root, &path, objectid, 0);
237 btrfs_release_path(root, &path);
239 printf("unable to find key %lu\n", oid);
245 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
246 *root, struct radix_tree_root *radix)
248 struct btrfs_path path;
253 ret = find_num(radix, &oid, 0);
256 sprintf(buf, "str-%lu", oid);
257 btrfs_init_path(&path);
258 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
260 btrfs_release_path(root, &path);
262 printf("able to find key that should not exist %lu\n", oid);
268 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
269 *root, struct radix_tree_root *radix, int nr)
271 struct btrfs_path path;
272 struct btrfs_key key;
273 unsigned long found = 0;
279 struct btrfs_dir_item *di;
281 key.offset = (u64)-1;
283 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
284 key.objectid = dir_oid;
286 btrfs_init_path(&path);
287 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
289 btrfs_release_path(root, &path);
293 if (path.slots[0] == 0) {
294 btrfs_release_path(root, &path);
299 slot = path.slots[0];
300 di = btrfs_item_ptr(&path.nodes[0]->leaf, slot,
301 struct btrfs_dir_item);
302 found_len = btrfs_dir_name_len(di);
303 memcpy(buf, (char *)(di + 1), found_len);
304 BUG_ON(found_len > 128);
305 buf[found_len] = '\0';
306 found = atoi(buf + 4);
307 ret = del_dir_item(trans, root, radix, found, &path);
311 "failed to remove %lu from tree\n",
315 btrfs_release_path(root, &path);
320 fprintf(stderr, "failed to delete from the radix %lu\n", found);
324 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
325 struct radix_tree_root *radix, int count)
329 for (i = 0; i < count; i++) {
330 ret = ins_one(trans, root, radix);
332 fprintf(stderr, "fill failed\n");
336 ret = btrfs_commit_transaction(trans, root, &super);
338 fprintf(stderr, "fill commit failed\n");
342 if (i && i % 10000 == 0) {
343 printf("bigfill %d\n", i);
352 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
353 struct radix_tree_root *radix)
356 int nr = rand() % 5000;
357 static int run_nr = 0;
359 /* do the bulk op much less frequently */
362 ret = empty_tree(trans, root, radix, nr);
365 ret = fill_tree(trans, root, radix, nr);
372 int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
373 radix_tree_root *radix) =
374 { ins_one, insert_dup, del_one, lookup_item,
375 lookup_enoent, bulk_op };
377 void sigstopper(int ignored)
380 fprintf(stderr, "caught exit signal, stopping\n");
383 int print_usage(void)
385 printf("usage: tester [-ih] [-c count] [-f count]\n");
386 printf("\t -c count -- iteration count after filling\n");
387 printf("\t -f count -- run this many random inserts before starting\n");
388 printf("\t -i -- only do initial fill\n");
389 printf("\t -h -- this help text\n");
392 int main(int ac, char **av)
394 RADIX_TREE(radix, GFP_KERNEL);
395 struct btrfs_root *root;
400 int iterations = 20000;
401 int init_fill_count = 800000;
403 int initial_only = 0;
404 struct btrfs_trans_handle *trans;
407 printf("removing old tree\n");
409 root = open_ctree("dbfile", &super);
410 trans = btrfs_start_transaction(root, 1);
412 signal(SIGTERM, sigstopper);
413 signal(SIGINT, sigstopper);
415 for (i = 1 ; i < ac ; i++) {
416 if (strcmp(av[i], "-i") == 0) {
418 } else if (strcmp(av[i], "-c") == 0) {
419 iterations = atoi(av[i+1]);
421 } else if (strcmp(av[i], "-f") == 0) {
422 init_fill_count = atoi(av[i+1]);
428 printf("initial fill\n");
429 ret = fill_tree(trans, root, &radix, init_fill_count);
430 printf("starting run\n");
435 if (initial_only == 1) {
438 for (i = 0; i < iterations; i++) {
439 op = rand() % ARRAY_SIZE(ops);
440 count = rand() % 128;
445 if (i && i % 5000 == 0) {
446 printf("open & close, root level %d nritems %d\n",
447 btrfs_header_level(&root->node->node.header),
448 btrfs_header_nritems(&root->node->node.header));
449 close_ctree(root, &super);
450 root = open_ctree("dbfile", &super);
453 ret = ops[op](trans, root, &radix);
455 fprintf(stderr, "op %d failed %d:%d\n",
457 btrfs_print_tree(root, root->node);
458 fprintf(stderr, "op %d failed %d:%d\n",
463 if (ops[op] == bulk_op)
465 if (keep_running == 0) {
472 close_ctree(root, &super);