7 #include "kerncompat.h"
8 #include "radix-tree.h"
11 #include "print-tree.h"
13 #include "transaction.h"
16 struct btrfs_super_block super;
17 static u64 dir_oid = 0;
18 static u64 file_oid = 33778;
20 static int find_num(struct radix_tree_root *root, unsigned long *num_ret,
23 unsigned long num = rand();
28 ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
33 } else if (ret != 0 && num == res[0]) {
35 if (ret > 1 && num == res[1]) {
44 static void initial_inode_init(struct btrfs_root *root,
45 struct btrfs_inode_item *inode_item)
47 memset(inode_item, 0, sizeof(*inode_item));
48 btrfs_set_inode_generation(inode_item, root->fs_info->generation);
49 btrfs_set_inode_mode(inode_item, S_IFREG | 0700);
52 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
53 struct radix_tree_root *radix)
59 struct btrfs_path path;
60 struct btrfs_key inode_map;
61 struct btrfs_inode_item inode_item;
63 find_num(radix, &oid, 0);
64 sprintf(buf, "str-%lu", oid);
66 ret = btrfs_find_free_objectid(trans, root, dir_oid + 1, &objectid);
70 inode_map.objectid = objectid;
72 btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
75 initial_inode_init(root, &inode_item);
76 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
79 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
80 &inode_map, BTRFS_FT_UNKNOWN);
84 radix_tree_preload(GFP_KERNEL);
85 ret = radix_tree_insert(radix, oid, (void *)oid);
86 radix_tree_preload_end();
95 * if we got an EEXIST, it may be due to hash collision, double
98 btrfs_init_path(&path);
99 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
103 if (!btrfs_match_dir_item_name(root, &path, buf, strlen(buf))) {
104 struct btrfs_dir_item *di;
110 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
111 struct btrfs_dir_item);
112 found = (char *)(di + 1);
113 found_len = btrfs_dir_name_len(di);
114 btrfs_name_hash(buf, strlen(buf), &myhash);
115 btrfs_name_hash(found, found_len, &foundhash);
116 if (myhash != foundhash)
118 btrfs_release_path(root, &path);
122 btrfs_release_path(root, &path);
124 printf("failed to insert %lu ret %d\n", oid, ret);
128 static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
129 *root, struct radix_tree_root *radix)
134 struct btrfs_key key;
136 ret = find_num(radix, &oid, 1);
139 sprintf(buf, "str-%lu", oid);
141 key.objectid = file_oid;
143 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
145 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
146 &key, BTRFS_FT_UNKNOWN);
147 if (ret != -EEXIST) {
148 printf("insert on %s gave us %d\n", buf, ret);
154 static int del_dir_item(struct btrfs_trans_handle *trans,
155 struct btrfs_root *root,
156 struct radix_tree_root *radix,
157 unsigned long radix_index,
158 struct btrfs_path *path)
163 struct btrfs_dir_item *di;
165 /* find the inode number of the file */
166 di = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
167 struct btrfs_dir_item);
168 file_objectid = btrfs_disk_key_objectid(&di->location);
170 /* delete the directory item */
171 ret = btrfs_del_item(trans, root, path);
174 btrfs_release_path(root, path);
176 /* delete the inode */
177 btrfs_init_path(path);
178 ret = btrfs_lookup_inode(trans, root, path, file_objectid, -1);
181 ret = btrfs_del_item(trans, root, path);
184 btrfs_release_path(root, path);
186 if (root->fs_info->last_inode_alloc > file_objectid)
187 root->fs_info->last_inode_alloc = file_objectid;
188 ptr = radix_tree_delete(radix, radix_index);
195 btrfs_release_path(root, path);
197 printf("failed to delete %lu %d\n", radix_index, ret);
201 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
202 struct radix_tree_root *radix)
207 struct btrfs_path path;
209 ret = find_num(radix, &oid, 1);
212 sprintf(buf, "str-%lu", oid);
213 btrfs_init_path(&path);
214 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
219 ret = del_dir_item(trans, root, radix, oid, &path);
224 btrfs_release_path(root, &path);
225 printf("failed to delete %lu %d\n", oid, ret);
229 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
230 *root, struct radix_tree_root *radix)
232 struct btrfs_path path;
237 struct btrfs_dir_item *di;
239 ret = find_num(radix, &oid, 1);
242 sprintf(buf, "str-%lu", oid);
243 btrfs_init_path(&path);
244 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
247 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
248 struct btrfs_dir_item);
249 objectid = btrfs_disk_key_objectid(&di->location);
251 btrfs_release_path(root, &path);
253 printf("unable to find key %lu\n", oid);
259 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
260 *root, struct radix_tree_root *radix)
262 struct btrfs_path path;
267 ret = find_num(radix, &oid, 0);
270 sprintf(buf, "str-%lu", oid);
271 btrfs_init_path(&path);
272 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
274 btrfs_release_path(root, &path);
276 printf("able to find key that should not exist %lu\n", oid);
282 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
283 *root, struct radix_tree_root *radix, int nr)
285 struct btrfs_path path;
286 struct btrfs_key key;
287 unsigned long found = 0;
293 struct btrfs_dir_item *di;
295 key.offset = (u64)-1;
297 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
298 key.objectid = dir_oid;
300 btrfs_init_path(&path);
301 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
303 btrfs_release_path(root, &path);
307 if (path.slots[0] == 0) {
308 btrfs_release_path(root, &path);
313 slot = path.slots[0];
314 di = btrfs_item_ptr(&path.nodes[0]->leaf, slot,
315 struct btrfs_dir_item);
316 found_len = btrfs_dir_name_len(di);
317 memcpy(buf, (char *)(di + 1), found_len);
318 BUG_ON(found_len > 128);
319 buf[found_len] = '\0';
320 found = atoi(buf + 4);
321 ret = del_dir_item(trans, root, radix, found, &path);
325 "failed to remove %lu from tree\n",
333 fprintf(stderr, "failed to delete from the radix %lu\n", found);
337 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
338 struct radix_tree_root *radix, int count)
342 for (i = 0; i < count; i++) {
343 ret = ins_one(trans, root, radix);
345 fprintf(stderr, "fill failed\n");
349 ret = btrfs_commit_transaction(trans, root, &super);
351 fprintf(stderr, "fill commit failed\n");
355 if (i && i % 10000 == 0) {
356 printf("bigfill %d\n", i);
365 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
366 struct radix_tree_root *radix)
369 int nr = rand() % 5000;
370 static int run_nr = 0;
372 /* do the bulk op much less frequently */
375 ret = empty_tree(trans, root, radix, nr);
378 ret = fill_tree(trans, root, radix, nr);
385 int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
386 radix_tree_root *radix) =
387 { ins_one, insert_dup, del_one, lookup_item,
388 lookup_enoent, bulk_op };
390 void sigstopper(int ignored)
393 fprintf(stderr, "caught exit signal, stopping\n");
396 int print_usage(void)
398 printf("usage: tester [-ih] [-c count] [-f count]\n");
399 printf("\t -c count -- iteration count after filling\n");
400 printf("\t -f count -- run this many random inserts before starting\n");
401 printf("\t -i -- only do initial fill\n");
402 printf("\t -h -- this help text\n");
405 int main(int ac, char **av)
407 RADIX_TREE(radix, GFP_KERNEL);
408 struct btrfs_root *root;
413 int iterations = 20000;
414 int init_fill_count = 800000;
416 int initial_only = 0;
417 struct btrfs_trans_handle *trans;
420 root = open_ctree(av[ac-1], &super);
421 trans = btrfs_start_transaction(root, 1);
423 dir_oid = btrfs_super_root_dir(&super);
425 signal(SIGTERM, sigstopper);
426 signal(SIGINT, sigstopper);
428 for (i = 1 ; i < ac - 1; i++) {
429 if (strcmp(av[i], "-i") == 0) {
431 } else if (strcmp(av[i], "-c") == 0) {
432 iterations = atoi(av[i+1]);
434 } else if (strcmp(av[i], "-f") == 0) {
435 init_fill_count = atoi(av[i+1]);
441 printf("initial fill\n");
442 ret = fill_tree(trans, root, &radix, init_fill_count);
443 printf("starting run\n");
448 if (initial_only == 1) {
451 for (i = 0; i < iterations; i++) {
452 op = rand() % ARRAY_SIZE(ops);
453 count = rand() % 128;
458 if (i && i % 5000 == 0) {
459 printf("open & close, root level %d nritems %d\n",
460 btrfs_header_level(&root->node->node.header),
461 btrfs_header_nritems(&root->node->node.header));
462 close_ctree(root, &super);
463 root = open_ctree("dbfile", &super);
466 ret = ops[op](trans, root, &radix);
468 fprintf(stderr, "op %d failed %d:%d\n",
470 btrfs_print_tree(root, root->node);
471 fprintf(stderr, "op %d failed %d:%d\n",
476 if (ops[op] == bulk_op)
478 if (keep_running == 0) {
485 close_ctree(root, &super);