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 ret
= btrfs_insert_inode_map(trans
, root
, objectid
, &inode_map
);
79 initial_inode_init(root
, &inode_item
);
80 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
83 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
88 radix_tree_preload(GFP_KERNEL
);
89 ret
= radix_tree_insert(radix
, oid
, (void *)oid
);
90 radix_tree_preload_end();
99 * if we got an EEXIST, it may be due to hash collision, double
102 btrfs_init_path(&path
);
103 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
107 if (!btrfs_match_dir_item_name(root
, &path
, buf
, strlen(buf
))) {
108 struct btrfs_dir_item
*di
;
114 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
115 struct btrfs_dir_item
);
116 found
= (char *)(di
+ 1);
117 found_len
= btrfs_dir_name_len(di
);
118 btrfs_name_hash(buf
, strlen(buf
), &myhash
);
119 btrfs_name_hash(found
, found_len
, &foundhash
);
120 if (myhash
!= foundhash
)
122 btrfs_release_path(root
, &path
);
126 btrfs_release_path(root
, &path
);
128 printf("failed to insert %lu ret %d\n", oid
, ret
);
132 static int insert_dup(struct btrfs_trans_handle
*trans
, struct btrfs_root
133 *root
, struct radix_tree_root
*radix
)
138 struct btrfs_key key
;
140 ret
= find_num(radix
, &oid
, 1);
143 sprintf(buf
, "str-%lu", oid
);
145 key
.objectid
= file_oid
;
147 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
149 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
151 if (ret
!= -EEXIST
) {
152 printf("insert on %s gave us %d\n", buf
, ret
);
158 static int del_dir_item(struct btrfs_trans_handle
*trans
,
159 struct btrfs_root
*root
,
160 struct radix_tree_root
*radix
,
161 unsigned long radix_index
,
162 struct btrfs_path
*path
)
167 struct btrfs_dir_item
*di
;
169 /* find the inode number of the file */
170 di
= btrfs_item_ptr(&path
->nodes
[0]->leaf
, path
->slots
[0],
171 struct btrfs_dir_item
);
172 file_objectid
= btrfs_disk_key_objectid(&di
->location
);
174 /* delete the directory item */
175 ret
= btrfs_del_item(trans
, root
, path
);
178 btrfs_release_path(root
, path
);
180 /* delete the inode */
181 btrfs_init_path(path
);
182 ret
= btrfs_lookup_inode(trans
, root
, path
, file_objectid
, -1);
185 ret
= btrfs_del_item(trans
, root
, path
);
188 btrfs_release_path(root
, path
);
190 /* delete the inode mapping */
191 btrfs_init_path(path
);
192 ret
= btrfs_lookup_inode_map(trans
, root
, path
, file_objectid
, -1);
195 ret
= btrfs_del_item(trans
, root
->fs_info
->inode_root
, path
);
199 if (root
->fs_info
->last_inode_alloc
> file_objectid
)
200 root
->fs_info
->last_inode_alloc
= file_objectid
;
201 btrfs_release_path(root
, path
);
202 ptr
= radix_tree_delete(radix
, radix_index
);
209 btrfs_release_path(root
, path
);
211 printf("failed to delete %lu %d\n", radix_index
, ret
);
215 static int del_one(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
216 struct radix_tree_root
*radix
)
221 struct btrfs_path path
;
223 ret
= find_num(radix
, &oid
, 1);
226 sprintf(buf
, "str-%lu", oid
);
227 btrfs_init_path(&path
);
228 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
233 ret
= del_dir_item(trans
, root
, radix
, oid
, &path
);
238 btrfs_release_path(root
, &path
);
239 printf("failed to delete %lu %d\n", oid
, ret
);
243 static int lookup_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
244 *root
, struct radix_tree_root
*radix
)
246 struct btrfs_path path
;
251 struct btrfs_dir_item
*di
;
253 ret
= find_num(radix
, &oid
, 1);
256 sprintf(buf
, "str-%lu", oid
);
257 btrfs_init_path(&path
);
258 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
261 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
262 struct btrfs_dir_item
);
263 objectid
= btrfs_disk_key_objectid(&di
->location
);
264 btrfs_release_path(root
, &path
);
265 btrfs_init_path(&path
);
266 ret
= btrfs_lookup_inode_map(trans
, root
, &path
, objectid
, 0);
268 btrfs_release_path(root
, &path
);
270 printf("unable to find key %lu\n", oid
);
276 static int lookup_enoent(struct btrfs_trans_handle
*trans
, struct btrfs_root
277 *root
, struct radix_tree_root
*radix
)
279 struct btrfs_path path
;
284 ret
= find_num(radix
, &oid
, 0);
287 sprintf(buf
, "str-%lu", oid
);
288 btrfs_init_path(&path
);
289 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
291 btrfs_release_path(root
, &path
);
293 printf("able to find key that should not exist %lu\n", oid
);
299 static int empty_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
300 *root
, struct radix_tree_root
*radix
, int nr
)
302 struct btrfs_path path
;
303 struct btrfs_key key
;
304 unsigned long found
= 0;
310 struct btrfs_dir_item
*di
;
312 key
.offset
= (u64
)-1;
314 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
315 key
.objectid
= dir_oid
;
317 btrfs_init_path(&path
);
318 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, -1, 1);
320 btrfs_release_path(root
, &path
);
324 if (path
.slots
[0] == 0) {
325 btrfs_release_path(root
, &path
);
330 slot
= path
.slots
[0];
331 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, slot
,
332 struct btrfs_dir_item
);
333 found_len
= btrfs_dir_name_len(di
);
334 memcpy(buf
, (char *)(di
+ 1), found_len
);
335 BUG_ON(found_len
> 128);
336 buf
[found_len
] = '\0';
337 found
= atoi(buf
+ 4);
338 ret
= del_dir_item(trans
, root
, radix
, found
, &path
);
342 "failed to remove %lu from tree\n",
350 fprintf(stderr
, "failed to delete from the radix %lu\n", found
);
354 static int fill_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
355 struct radix_tree_root
*radix
, int count
)
359 for (i
= 0; i
< count
; i
++) {
360 ret
= ins_one(trans
, root
, radix
);
362 fprintf(stderr
, "fill failed\n");
366 ret
= btrfs_commit_transaction(trans
, root
, &super
);
368 fprintf(stderr
, "fill commit failed\n");
372 if (i
&& i
% 10000 == 0) {
373 printf("bigfill %d\n", i
);
382 static int bulk_op(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
383 struct radix_tree_root
*radix
)
386 int nr
= rand() % 5000;
387 static int run_nr
= 0;
389 /* do the bulk op much less frequently */
392 ret
= empty_tree(trans
, root
, radix
, nr
);
395 ret
= fill_tree(trans
, root
, radix
, nr
);
402 int (*ops
[])(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
, struct
403 radix_tree_root
*radix
) =
404 { ins_one
, insert_dup
, del_one
, lookup_item
,
405 lookup_enoent
, bulk_op
};
407 void sigstopper(int ignored
)
410 fprintf(stderr
, "caught exit signal, stopping\n");
413 int print_usage(void)
415 printf("usage: tester [-ih] [-c count] [-f count]\n");
416 printf("\t -c count -- iteration count after filling\n");
417 printf("\t -f count -- run this many random inserts before starting\n");
418 printf("\t -i -- only do initial fill\n");
419 printf("\t -h -- this help text\n");
422 int main(int ac
, char **av
)
424 RADIX_TREE(radix
, GFP_KERNEL
);
425 struct btrfs_root
*root
;
430 int iterations
= 20000;
431 int init_fill_count
= 800000;
433 int initial_only
= 0;
434 struct btrfs_trans_handle
*trans
;
437 root
= open_ctree(av
[ac
-1], &super
);
438 trans
= btrfs_start_transaction(root
, 1);
440 dir_oid
= btrfs_super_root_dir(&super
);
442 signal(SIGTERM
, sigstopper
);
443 signal(SIGINT
, sigstopper
);
445 for (i
= 1 ; i
< ac
- 1; i
++) {
446 if (strcmp(av
[i
], "-i") == 0) {
448 } else if (strcmp(av
[i
], "-c") == 0) {
449 iterations
= atoi(av
[i
+1]);
451 } else if (strcmp(av
[i
], "-f") == 0) {
452 init_fill_count
= atoi(av
[i
+1]);
458 printf("initial fill\n");
459 ret
= fill_tree(trans
, root
, &radix
, init_fill_count
);
460 printf("starting run\n");
465 if (initial_only
== 1) {
468 for (i
= 0; i
< iterations
; i
++) {
469 op
= rand() % ARRAY_SIZE(ops
);
470 count
= rand() % 128;
475 if (i
&& i
% 5000 == 0) {
476 printf("open & close, root level %d nritems %d\n",
477 btrfs_header_level(&root
->node
->node
.header
),
478 btrfs_header_nritems(&root
->node
->node
.header
));
479 close_ctree(root
, &super
);
480 root
= open_ctree("dbfile", &super
);
483 ret
= ops
[op
](trans
, root
, &radix
);
485 fprintf(stderr
, "op %d failed %d:%d\n",
487 btrfs_print_tree(root
, root
->node
);
488 fprintf(stderr
, "op %d failed %d:%d\n",
493 if (ops
[op
] == bulk_op
)
495 if (keep_running
== 0) {
502 close_ctree(root
, &super
);