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
;
74 ret
= btrfs_insert_inode_map(trans
, root
, objectid
, &inode_map
);
78 initial_inode_init(root
, &inode_item
);
79 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
82 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
87 radix_tree_preload(GFP_KERNEL
);
88 ret
= radix_tree_insert(radix
, oid
, (void *)oid
);
89 radix_tree_preload_end();
98 * if we got an EEXIST, it may be due to hash collision, double
101 btrfs_init_path(&path
);
102 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
106 if (!btrfs_match_dir_item_name(root
, &path
, buf
, strlen(buf
))) {
107 struct btrfs_dir_item
*di
;
113 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
114 struct btrfs_dir_item
);
115 found
= (char *)(di
+ 1);
116 found_len
= btrfs_dir_name_len(di
);
117 btrfs_name_hash(buf
, strlen(buf
), &myhash
);
118 btrfs_name_hash(found
, found_len
, &foundhash
);
119 if (myhash
!= foundhash
)
121 btrfs_release_path(root
, &path
);
125 btrfs_release_path(root
, &path
);
127 printf("failed to insert %lu ret %d\n", oid
, ret
);
131 static int insert_dup(struct btrfs_trans_handle
*trans
, struct btrfs_root
132 *root
, struct radix_tree_root
*radix
)
138 ret
= find_num(radix
, &oid
, 1);
141 sprintf(buf
, "str-%lu", oid
);
143 ret
= btrfs_insert_dir_item(trans
, root
, buf
, strlen(buf
), dir_oid
,
145 if (ret
!= -EEXIST
) {
146 printf("insert on %s gave us %d\n", buf
, ret
);
152 static int del_dir_item(struct btrfs_trans_handle
*trans
,
153 struct btrfs_root
*root
,
154 struct radix_tree_root
*radix
,
155 unsigned long radix_index
,
156 struct btrfs_path
*path
)
161 struct btrfs_dir_item
*di
;
163 /* find the inode number of the file */
164 di
= btrfs_item_ptr(&path
->nodes
[0]->leaf
, path
->slots
[0],
165 struct btrfs_dir_item
);
166 file_objectid
= btrfs_dir_objectid(di
);
168 /* delete the directory item */
169 ret
= btrfs_del_item(trans
, root
, path
);
172 btrfs_release_path(root
, path
);
174 /* delete the inode */
175 btrfs_init_path(path
);
176 ret
= btrfs_lookup_inode(trans
, root
, path
, file_objectid
, -1);
179 ret
= btrfs_del_item(trans
, root
, path
);
182 btrfs_release_path(root
, path
);
184 /* delete the inode mapping */
185 btrfs_init_path(path
);
186 ret
= btrfs_lookup_inode_map(trans
, root
, path
, file_objectid
, -1);
189 ret
= btrfs_del_item(trans
, root
->fs_info
->inode_root
, path
);
193 if (root
->fs_info
->last_inode_alloc
> file_objectid
)
194 root
->fs_info
->last_inode_alloc
= file_objectid
;
195 btrfs_release_path(root
, path
);
196 ptr
= radix_tree_delete(radix
, radix_index
);
203 btrfs_release_path(root
, path
);
205 printf("failed to delete %lu %d\n", radix_index
, ret
);
209 static int del_one(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
210 struct radix_tree_root
*radix
)
215 struct btrfs_path path
;
217 ret
= find_num(radix
, &oid
, 1);
220 sprintf(buf
, "str-%lu", oid
);
221 btrfs_init_path(&path
);
222 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
227 ret
= del_dir_item(trans
, root
, radix
, oid
, &path
);
232 btrfs_release_path(root
, &path
);
233 printf("failed to delete %lu %d\n", oid
, ret
);
237 static int lookup_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
238 *root
, struct radix_tree_root
*radix
)
240 struct btrfs_path path
;
245 struct btrfs_dir_item
*di
;
247 ret
= find_num(radix
, &oid
, 1);
250 sprintf(buf
, "str-%lu", oid
);
251 btrfs_init_path(&path
);
252 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
255 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, path
.slots
[0],
256 struct btrfs_dir_item
);
257 objectid
= btrfs_dir_objectid(di
);
258 btrfs_release_path(root
, &path
);
259 btrfs_init_path(&path
);
260 ret
= btrfs_lookup_inode_map(trans
, root
, &path
, objectid
, 0);
262 btrfs_release_path(root
, &path
);
264 printf("unable to find key %lu\n", oid
);
270 static int lookup_enoent(struct btrfs_trans_handle
*trans
, struct btrfs_root
271 *root
, struct radix_tree_root
*radix
)
273 struct btrfs_path path
;
278 ret
= find_num(radix
, &oid
, 0);
281 sprintf(buf
, "str-%lu", oid
);
282 btrfs_init_path(&path
);
283 ret
= btrfs_lookup_dir_item(trans
, root
, &path
, dir_oid
, buf
,
285 btrfs_release_path(root
, &path
);
287 printf("able to find key that should not exist %lu\n", oid
);
293 static int empty_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
294 *root
, struct radix_tree_root
*radix
, int nr
)
296 struct btrfs_path path
;
297 struct btrfs_key key
;
298 unsigned long found
= 0;
304 struct btrfs_dir_item
*di
;
306 key
.offset
= (u64
)-1;
308 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
309 key
.objectid
= dir_oid
;
311 btrfs_init_path(&path
);
312 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, -1, 1);
314 btrfs_release_path(root
, &path
);
318 if (path
.slots
[0] == 0) {
319 btrfs_release_path(root
, &path
);
324 slot
= path
.slots
[0];
325 di
= btrfs_item_ptr(&path
.nodes
[0]->leaf
, slot
,
326 struct btrfs_dir_item
);
327 found_len
= btrfs_dir_name_len(di
);
328 memcpy(buf
, (char *)(di
+ 1), found_len
);
329 BUG_ON(found_len
> 128);
330 buf
[found_len
] = '\0';
331 found
= atoi(buf
+ 4);
332 ret
= del_dir_item(trans
, root
, radix
, found
, &path
);
336 "failed to remove %lu from tree\n",
344 fprintf(stderr
, "failed to delete from the radix %lu\n", found
);
348 static int fill_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
349 struct radix_tree_root
*radix
, int count
)
353 for (i
= 0; i
< count
; i
++) {
354 ret
= ins_one(trans
, root
, radix
);
356 fprintf(stderr
, "fill failed\n");
360 ret
= btrfs_commit_transaction(trans
, root
, &super
);
362 fprintf(stderr
, "fill commit failed\n");
366 if (i
&& i
% 10000 == 0) {
367 printf("bigfill %d\n", i
);
376 static int bulk_op(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
377 struct radix_tree_root
*radix
)
380 int nr
= rand() % 5000;
381 static int run_nr
= 0;
383 /* do the bulk op much less frequently */
386 ret
= empty_tree(trans
, root
, radix
, nr
);
389 ret
= fill_tree(trans
, root
, radix
, nr
);
396 int (*ops
[])(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
, struct
397 radix_tree_root
*radix
) =
398 { ins_one
, insert_dup
, del_one
, lookup_item
,
399 lookup_enoent
, bulk_op
};
401 void sigstopper(int ignored
)
404 fprintf(stderr
, "caught exit signal, stopping\n");
407 int print_usage(void)
409 printf("usage: tester [-ih] [-c count] [-f count]\n");
410 printf("\t -c count -- iteration count after filling\n");
411 printf("\t -f count -- run this many random inserts before starting\n");
412 printf("\t -i -- only do initial fill\n");
413 printf("\t -h -- this help text\n");
416 int main(int ac
, char **av
)
418 RADIX_TREE(radix
, GFP_KERNEL
);
419 struct btrfs_root
*root
;
424 int iterations
= 20000;
425 int init_fill_count
= 800000;
427 int initial_only
= 0;
428 struct btrfs_trans_handle
*trans
;
431 root
= open_ctree(av
[ac
-1], &super
);
432 trans
= btrfs_start_transaction(root
, 1);
434 dir_oid
= btrfs_super_root_dir(&super
);
436 signal(SIGTERM
, sigstopper
);
437 signal(SIGINT
, sigstopper
);
439 for (i
= 1 ; i
< ac
- 1; i
++) {
440 if (strcmp(av
[i
], "-i") == 0) {
442 } else if (strcmp(av
[i
], "-c") == 0) {
443 iterations
= atoi(av
[i
+1]);
445 } else if (strcmp(av
[i
], "-f") == 0) {
446 init_fill_count
= atoi(av
[i
+1]);
452 printf("initial fill\n");
453 ret
= fill_tree(trans
, root
, &radix
, init_fill_count
);
454 printf("starting run\n");
459 if (initial_only
== 1) {
462 for (i
= 0; i
< iterations
; i
++) {
463 op
= rand() % ARRAY_SIZE(ops
);
464 count
= rand() % 128;
469 if (i
&& i
% 5000 == 0) {
470 printf("open & close, root level %d nritems %d\n",
471 btrfs_header_level(&root
->node
->node
.header
),
472 btrfs_header_nritems(&root
->node
->node
.header
));
473 close_ctree(root
, &super
);
474 root
= open_ctree("dbfile", &super
);
477 ret
= ops
[op
](trans
, root
, &radix
);
479 fprintf(stderr
, "op %d failed %d:%d\n",
481 btrfs_print_tree(root
, root
->node
);
482 fprintf(stderr
, "op %d failed %d:%d\n",
487 if (ops
[op
] == bulk_op
)
489 if (keep_running
== 0) {
496 close_ctree(root
, &super
);