+ printf("able to find key that should not exist %llu\n",
+ (unsigned long long)key.objectid);
+ return -EEXIST;
+}
+
+static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
+ *root, struct radix_tree_root *radix, int nr)
+{
+ struct btrfs_path path;
+ struct btrfs_key key;
+ unsigned long found = 0;
+ int ret;
+ int slot;
+ int *ptr;
+ int count = 0;
+
+ key.offset = 0;
+ key.flags = 0;
+ key.type = BTRFS_STRING_ITEM_KEY;
+ key.objectid = (unsigned long)-1;
+ while(nr-- >= 0) {
+ btrfs_init_path(&path);
+ ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
+ if (ret < 0) {
+ btrfs_release_path(&path);
+ return ret;
+ }
+ if (ret != 0) {
+ if (path.slots[0] == 0) {
+ btrfs_release_path(&path);
+ break;
+ }
+ path.slots[0] -= 1;
+ }
+ slot = path.slots[0];
+ found = btrfs_disk_key_objectid(
+ &path.nodes[0]->leaf.items[slot].key);
+ ret = btrfs_del_item(trans, root, &path);
+ count++;
+ if (ret) {
+ fprintf(stderr,
+ "failed to remove %lu from tree\n",
+ found);
+ return ret;
+ }
+ btrfs_release_path(&path);
+ ptr = radix_tree_delete(radix, found);
+ if (!ptr)
+ goto error;
+ if (!keep_running)
+ break;
+ }
+ return 0;
+error:
+ fprintf(stderr, "failed to delete from the radix %lu\n", found);
+ return -ENOENT;
+}
+
+static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+ struct radix_tree_root *radix, int count)
+{
+ int i;
+ int ret = 0;
+ for (i = 0; i < count; i++) {
+ ret = ins_one(trans, root, radix);
+ if (ret) {
+ fprintf(stderr, "fill failed\n");
+ goto out;
+ }
+ if (i % 1000 == 0) {
+ ret = btrfs_commit_transaction(trans, root, &super);
+ if (ret) {
+ fprintf(stderr, "fill commit failed\n");
+ return ret;
+ }
+ }
+ if (i && i % 10000 == 0) {
+ printf("bigfill %d\n", i);
+ }
+ if (!keep_running)
+ break;
+ }
+out:
+ return ret;
+}
+
+static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+ struct radix_tree_root *radix)
+{
+ int ret;
+ int nr = rand() % 5000;
+ static int run_nr = 0;
+
+ /* do the bulk op much less frequently */
+ if (run_nr++ % 100)
+ return 0;
+ ret = empty_tree(trans, root, radix, nr);
+ if (ret)
+ return ret;
+ ret = fill_tree(trans, root, radix, nr);
+ if (ret)
+ return ret;
+ return 0;