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 void initial_inode_init(struct btrfs_root *root,
43 struct btrfs_inode_item *inode_item)
45 memset(inode_item, 0, sizeof(*inode_item));
46 btrfs_set_inode_generation(inode_item, root->fs_info->generation);
49 static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
50 struct radix_tree_root *radix)
56 struct btrfs_path path;
57 struct btrfs_key inode_map;
58 struct btrfs_inode_item inode_item;
60 find_num(radix, &oid, 0);
61 sprintf(buf, "str-%lu", oid);
63 ret = btrfs_find_free_objectid(trans, root, dir_oid + 1, &objectid);
67 inode_map.objectid = objectid;
71 ret = btrfs_insert_inode_map(trans, root, objectid, &inode_map);
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,
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)
135 ret = find_num(radix, &oid, 1);
138 sprintf(buf, "str-%lu", oid);
140 ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
142 if (ret != -EEXIST) {
143 printf("insert on %s gave us %d\n", buf, ret);
149 static int del_dir_item(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct radix_tree_root *radix,
152 unsigned long radix_index,
153 struct btrfs_path *path)
158 struct btrfs_dir_item *di;
160 /* find the inode number of the file */
161 di = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
162 struct btrfs_dir_item);
163 file_objectid = btrfs_dir_objectid(di);
165 /* delete the directory item */
166 ret = btrfs_del_item(trans, root, path);
169 btrfs_release_path(root, path);
171 /* delete the inode */
172 btrfs_init_path(path);
173 ret = btrfs_lookup_inode(trans, root, path, file_objectid, -1);
176 ret = btrfs_del_item(trans, root, path);
179 btrfs_release_path(root, path);
181 /* delete the inode mapping */
182 btrfs_init_path(path);
183 ret = btrfs_lookup_inode_map(trans, root, path, file_objectid, -1);
186 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
190 if (root->fs_info->last_inode_alloc > file_objectid)
191 root->fs_info->last_inode_alloc = file_objectid;
192 btrfs_release_path(root, path);
193 ptr = radix_tree_delete(radix, radix_index);
200 btrfs_release_path(root, path);
202 printf("failed to delete %lu %d\n", radix_index, ret);
206 static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
207 struct radix_tree_root *radix)
212 struct btrfs_path path;
214 ret = find_num(radix, &oid, 1);
217 sprintf(buf, "str-%lu", oid);
218 btrfs_init_path(&path);
219 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
224 ret = del_dir_item(trans, root, radix, oid, &path);
229 btrfs_release_path(root, &path);
230 printf("failed to delete %lu %d\n", oid, ret);
234 static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
235 *root, struct radix_tree_root *radix)
237 struct btrfs_path path;
242 struct btrfs_dir_item *di;
244 ret = find_num(radix, &oid, 1);
247 sprintf(buf, "str-%lu", oid);
248 btrfs_init_path(&path);
249 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
252 di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
253 struct btrfs_dir_item);
254 objectid = btrfs_dir_objectid(di);
255 btrfs_release_path(root, &path);
256 btrfs_init_path(&path);
257 ret = btrfs_lookup_inode_map(trans, root, &path, objectid, 0);
259 btrfs_release_path(root, &path);
261 printf("unable to find key %lu\n", oid);
267 static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
268 *root, struct radix_tree_root *radix)
270 struct btrfs_path path;
275 ret = find_num(radix, &oid, 0);
278 sprintf(buf, "str-%lu", oid);
279 btrfs_init_path(&path);
280 ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
282 btrfs_release_path(root, &path);
284 printf("able to find key that should not exist %lu\n", oid);
290 static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
291 *root, struct radix_tree_root *radix, int nr)
293 struct btrfs_path path;
294 struct btrfs_key key;
295 unsigned long found = 0;
301 struct btrfs_dir_item *di;
303 key.offset = (u64)-1;
305 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
306 key.objectid = dir_oid;
308 btrfs_init_path(&path);
309 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
311 btrfs_release_path(root, &path);
315 if (path.slots[0] == 0) {
316 btrfs_release_path(root, &path);
321 slot = path.slots[0];
322 di = btrfs_item_ptr(&path.nodes[0]->leaf, slot,
323 struct btrfs_dir_item);
324 found_len = btrfs_dir_name_len(di);
325 memcpy(buf, (char *)(di + 1), found_len);
326 BUG_ON(found_len > 128);
327 buf[found_len] = '\0';
328 found = atoi(buf + 4);
329 ret = del_dir_item(trans, root, radix, found, &path);
333 "failed to remove %lu from tree\n",
341 fprintf(stderr, "failed to delete from the radix %lu\n", found);
345 static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
346 struct radix_tree_root *radix, int count)
350 for (i = 0; i < count; i++) {
351 ret = ins_one(trans, root, radix);
353 fprintf(stderr, "fill failed\n");
357 ret = btrfs_commit_transaction(trans, root, &super);
359 fprintf(stderr, "fill commit failed\n");
363 if (i && i % 10000 == 0) {
364 printf("bigfill %d\n", i);
373 static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
374 struct radix_tree_root *radix)
377 int nr = rand() % 5000;
378 static int run_nr = 0;
380 /* do the bulk op much less frequently */
383 ret = empty_tree(trans, root, radix, nr);
386 ret = fill_tree(trans, root, radix, nr);
393 int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
394 radix_tree_root *radix) =
395 { ins_one, insert_dup, del_one, lookup_item,
396 lookup_enoent, bulk_op };
398 void sigstopper(int ignored)
401 fprintf(stderr, "caught exit signal, stopping\n");
404 int print_usage(void)
406 printf("usage: tester [-ih] [-c count] [-f count]\n");
407 printf("\t -c count -- iteration count after filling\n");
408 printf("\t -f count -- run this many random inserts before starting\n");
409 printf("\t -i -- only do initial fill\n");
410 printf("\t -h -- this help text\n");
413 int main(int ac, char **av)
415 RADIX_TREE(radix, GFP_KERNEL);
416 struct btrfs_root *root;
421 int iterations = 20000;
422 int init_fill_count = 800000;
424 int initial_only = 0;
425 struct btrfs_trans_handle *trans;
428 printf("removing old tree\n");
430 root = open_ctree("dbfile", &super);
431 trans = btrfs_start_transaction(root, 1);
433 signal(SIGTERM, sigstopper);
434 signal(SIGINT, sigstopper);
436 for (i = 1 ; i < ac ; i++) {
437 if (strcmp(av[i], "-i") == 0) {
439 } else if (strcmp(av[i], "-c") == 0) {
440 iterations = atoi(av[i+1]);
442 } else if (strcmp(av[i], "-f") == 0) {
443 init_fill_count = atoi(av[i+1]);
449 printf("initial fill\n");
450 ret = fill_tree(trans, root, &radix, init_fill_count);
451 printf("starting run\n");
456 if (initial_only == 1) {
459 for (i = 0; i < iterations; i++) {
460 op = rand() % ARRAY_SIZE(ops);
461 count = rand() % 128;
466 if (i && i % 5000 == 0) {
467 printf("open & close, root level %d nritems %d\n",
468 btrfs_header_level(&root->node->node.header),
469 btrfs_header_nritems(&root->node->node.header));
470 close_ctree(root, &super);
471 root = open_ctree("dbfile", &super);
474 ret = ops[op](trans, root, &radix);
476 fprintf(stderr, "op %d failed %d:%d\n",
478 btrfs_print_tree(root, root->node);
479 fprintf(stderr, "op %d failed %d:%d\n",
484 if (ops[op] == bulk_op)
486 if (keep_running == 0) {
493 close_ctree(root, &super);