2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
22 #include <sys/types.h>
25 #include "kerncompat.h"
26 #include "radix-tree.h"
29 #include "print-tree.h"
31 #include "transaction.h"
34 struct btrfs_super_block super;
35 static u64 dir_oid = 0;
36 static u64 file_oid = 33778;
38 static int find_num(struct radix_tree_root *root, unsigned long *num_ret,
41 unsigned long num = rand();
46 ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
51 } else if (ret != 0 && num == res[0]) {
53 if (ret > 1 && num == res[1]) {
62 static void initial_inode_init(struct btrfs_root *root,
63 struct btrfs_inode_item *inode_item)
65 memset(inode_item, 0, sizeof(*inode_item));
66 btrfs_set_inode_generation(inode_item, root->fs_info->generation);
67 btrfs_set_inode_mode(inode_item, S_IFREG | 0700);
70 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
71 struct radix_tree_root *radix)
77 struct btrfs_path path;
78 struct btrfs_key inode_map;
79 struct btrfs_inode_item inode_item;
81 find_num(radix, &oid, 0);
82 sprintf(buf, "str-%lu", oid);
84 ret = btrfs_find_free_objectid(trans, root, dir_oid + 1, &objectid);
88 inode_map.objectid = objectid;
90 btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
93 initial_inode_init(root, &inode_item);
94 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
97 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
98 &inode_map, BTRFS_FT_UNKNOWN);
102 radix_tree_preload(GFP_KERNEL);
103 ret = radix_tree_insert(radix, oid, (void *)oid);
104 radix_tree_preload_end();
113 * if we got an EEXIST, it may be due to hash collision, double
116 btrfs_init_path(&path);
117 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
121 if (!btrfs_match_dir_item_name(root, &path, buf, strlen(buf))) {
122 struct btrfs_dir_item *di;
128 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
129 struct btrfs_dir_item);
130 found = (char *)(di + 1);
131 found_len = btrfs_dir_name_len(di);
132 myhash = btrfs_name_hash(buf, strlen(buf));
133 foundhash = btrfs_name_hash(found, found_len);
134 if (myhash != foundhash)
136 btrfs_release_path(root, &path);
140 btrfs_release_path(root, &path);
142 printf("failed to insert %lu ret %d\n", oid, ret);
146 static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
147 *root, struct radix_tree_root *radix)
152 struct btrfs_key key;
154 ret = find_num(radix, &oid, 1);
157 sprintf(buf, "str-%lu", oid);
159 key.objectid = file_oid;
161 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
163 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
164 &key, BTRFS_FT_UNKNOWN);
165 if (ret != -EEXIST) {
166 printf("insert on %s gave us %d\n", buf, ret);
172 static int del_dir_item(struct btrfs_trans_handle *trans,
173 struct btrfs_root *root,
174 struct radix_tree_root *radix,
175 unsigned long radix_index,
176 struct btrfs_path *path)
181 struct btrfs_dir_item *di;
183 /* find the inode number of the file */
184 di = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
185 struct btrfs_dir_item);
186 file_objectid = btrfs_disk_key_objectid(&di->location);
188 /* delete the directory item */
189 ret = btrfs_del_item(trans, root, path);
192 btrfs_release_path(root, path);
194 /* delete the inode */
195 btrfs_init_path(path);
196 ret = btrfs_lookup_inode(trans, root, path, file_objectid, -1);
199 ret = btrfs_del_item(trans, root, path);
202 btrfs_release_path(root, path);
204 if (root->fs_info->last_inode_alloc > file_objectid)
205 root->fs_info->last_inode_alloc = file_objectid;
206 ptr = radix_tree_delete(radix, radix_index);
213 btrfs_release_path(root, path);
215 printf("failed to delete %lu %d\n", radix_index, ret);
219 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
220 struct radix_tree_root *radix)
225 struct btrfs_path path;
227 ret = find_num(radix, &oid, 1);
230 sprintf(buf, "str-%lu", oid);
231 btrfs_init_path(&path);
232 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
237 ret = del_dir_item(trans, root, radix, oid, &path);
242 btrfs_release_path(root, &path);
243 printf("failed to delete %lu %d\n", oid, ret);
247 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
248 *root, struct radix_tree_root *radix)
250 struct btrfs_path path;
255 struct btrfs_dir_item *di;
257 ret = find_num(radix, &oid, 1);
260 sprintf(buf, "str-%lu", oid);
261 btrfs_init_path(&path);
262 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
265 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
266 struct btrfs_dir_item);
267 objectid = btrfs_disk_key_objectid(&di->location);
269 btrfs_release_path(root, &path);
271 printf("unable to find key %lu\n", oid);
277 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
278 *root, struct radix_tree_root *radix)
280 struct btrfs_path path;
285 ret = find_num(radix, &oid, 0);
288 sprintf(buf, "str-%lu", oid);
289 btrfs_init_path(&path);
290 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
292 btrfs_release_path(root, &path);
294 printf("able to find key that should not exist %lu\n", oid);
300 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
301 *root, struct radix_tree_root *radix, int nr)
303 struct btrfs_path path;
304 struct btrfs_key key;
305 unsigned long found = 0;
311 struct btrfs_dir_item *di;
313 key.offset = (u64)-1;
315 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
316 key.objectid = dir_oid;
318 btrfs_init_path(&path);
319 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
321 btrfs_release_path(root, &path);
325 if (path.slots[0] == 0) {
326 btrfs_release_path(root, &path);
331 slot = path.slots[0];
332 di = btrfs_item_ptr(&path.nodes[0]->leaf, slot,
333 struct btrfs_dir_item);
334 found_len = btrfs_dir_name_len(di);
335 memcpy(buf, (char *)(di + 1), found_len);
336 BUG_ON(found_len > 128);
337 buf[found_len] = '\0';
338 found = atoi(buf + 4);
339 ret = del_dir_item(trans, root, radix, found, &path);
343 "failed to remove %lu from tree\n",
351 fprintf(stderr, "failed to delete from the radix %lu\n", found);
355 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
356 struct radix_tree_root *radix, int count)
360 for (i = 0; i < count; i++) {
361 ret = ins_one(trans, root, radix);
363 fprintf(stderr, "fill failed\n");
367 ret = btrfs_commit_transaction(trans, root, &super);
369 fprintf(stderr, "fill commit failed\n");
373 if (i && i % 10000 == 0) {
374 printf("bigfill %d\n", i);
383 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
384 struct radix_tree_root *radix)
387 int nr = rand() % 5000;
388 static int run_nr = 0;
390 /* do the bulk op much less frequently */
393 ret = empty_tree(trans, root, radix, nr);
396 ret = fill_tree(trans, root, radix, nr);
403 int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
404 radix_tree_root *radix) =
405 { ins_one, insert_dup, del_one, lookup_item,
406 lookup_enoent, bulk_op };
408 void sigstopper(int ignored)
411 fprintf(stderr, "caught exit signal, stopping\n");
414 int print_usage(void)
416 printf("usage: tester [-ih] [-c count] [-f count]\n");
417 printf("\t -c count -- iteration count after filling\n");
418 printf("\t -f count -- run this many random inserts before starting\n");
419 printf("\t -i -- only do initial fill\n");
420 printf("\t -h -- this help text\n");
423 int main(int ac, char **av)
425 RADIX_TREE(radix, GFP_KERNEL);
426 struct btrfs_root *root;
431 int iterations = 20000;
432 int init_fill_count = 800000;
434 int initial_only = 0;
435 struct btrfs_trans_handle *trans;
438 root = open_ctree(av[ac-1], &super, 0);
441 fprintf(stderr, "Open ctree failed\n");
445 trans = btrfs_start_transaction(root, 1);
447 dir_oid = btrfs_super_root_dir(&super);
449 signal(SIGTERM, sigstopper);
450 signal(SIGINT, sigstopper);
452 for (i = 1 ; i < ac - 1; i++) {
453 if (strcmp(av[i], "-i") == 0) {
455 } else if (strcmp(av[i], "-c") == 0) {
456 iterations = atoi(av[i+1]);
458 } else if (strcmp(av[i], "-f") == 0) {
459 init_fill_count = atoi(av[i+1]);
465 printf("initial fill\n");
466 ret = fill_tree(trans, root, &radix, init_fill_count);
467 printf("starting run\n");
472 if (initial_only == 1) {
475 for (i = 0; i < iterations; i++) {
476 op = rand() % ARRAY_SIZE(ops);
477 count = rand() % 128;
482 if (i && i % 5000 == 0) {
483 printf("open & close, root level %d nritems %d\n",
484 btrfs_header_level(&root->node->node.header),
485 btrfs_header_nritems(&root->node->node.header));
486 close_ctree(root, &super);
487 root = open_ctree("dbfile", &super, 0);
490 fprintf(stderr, "Open ctree failed\n");
495 ret = ops[op](trans, root, &radix);
497 fprintf(stderr, "op %d failed %d:%d\n",
499 btrfs_print_tree(root, root->node, 1);
500 fprintf(stderr, "op %d failed %d:%d\n",
505 if (ops[op] == bulk_op)
507 if (keep_running == 0) {
514 close_ctree(root, &super);