2007-06-12 13:07:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "kerncompat.h"
|
2007-03-01 20:16:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-01-07 19:57:12 +00:00
|
|
|
#include <fcntl.h>
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/print-tree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/transaction.h"
|
2007-03-01 20:16:26 +00:00
|
|
|
|
|
|
|
/* for testing only */
|
2013-08-09 20:20:47 +00:00
|
|
|
static int next_key(int i, int max_key) {
|
2007-03-01 20:16:26 +00:00
|
|
|
return rand() % max_key;
|
2007-03-10 11:35:47 +00:00
|
|
|
// return i;
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
int main(int argc, char **argv) {
|
2007-03-12 20:22:34 +00:00
|
|
|
struct btrfs_key ins;
|
|
|
|
struct btrfs_key last = { (u64)-1, 0, 0};
|
2007-03-01 20:16:26 +00:00
|
|
|
char *buf;
|
|
|
|
int i;
|
|
|
|
int num;
|
|
|
|
int ret;
|
2008-04-15 15:32:17 +00:00
|
|
|
int run_size = 300000;
|
2007-03-01 20:16:26 +00:00
|
|
|
int max_key = 100000000;
|
2008-04-15 15:32:17 +00:00
|
|
|
int tree_size = 2;
|
2023-08-23 14:32:39 +00:00
|
|
|
struct btrfs_path path = { 0 };
|
2007-03-13 14:46:10 +00:00
|
|
|
struct btrfs_root *root;
|
2007-03-16 20:20:31 +00:00
|
|
|
struct btrfs_trans_handle *trans;
|
2007-03-01 20:16:26 +00:00
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("ERROR: btree-test image\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Parameters:\n");
|
|
|
|
printf(" run_size\t%d\n", run_size);
|
|
|
|
printf(" max_key\t%d\n", max_key);
|
|
|
|
printf(" tree_size\t%d\n", tree_size);
|
|
|
|
printf(" file\t%s\n", argv[1]);
|
|
|
|
|
2015-09-29 17:10:36 +00:00
|
|
|
buf = calloc(1, 512);
|
2007-04-04 17:27:22 +00:00
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
root = open_ctree(argv[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
|
2013-01-21 15:57:25 +00:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "Open ctree failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-16 20:20:31 +00:00
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 14:48:16 +00:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2007-03-01 20:16:26 +00:00
|
|
|
srand(55);
|
2016-09-13 09:58:21 +00:00
|
|
|
ins.type = BTRFS_STRING_ITEM_KEY;
|
2007-03-01 20:16:26 +00:00
|
|
|
for (i = 0; i < run_size; i++) {
|
|
|
|
num = next_key(i, max_key);
|
|
|
|
// num = i;
|
|
|
|
sprintf(buf, "string-%d", num);
|
|
|
|
if (i % 10000 == 0)
|
|
|
|
fprintf(stderr, "insert %d:%d\n", num, i);
|
|
|
|
ins.objectid = num;
|
|
|
|
ins.offset = 0;
|
2007-04-04 17:27:22 +00:00
|
|
|
ret = btrfs_insert_item(trans, root, &ins, buf, 512);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (!ret)
|
|
|
|
tree_size++;
|
2007-03-10 11:35:47 +00:00
|
|
|
if (i == run_size - 5) {
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 14:48:16 +00:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2007-03-10 11:35:47 +00:00
|
|
|
}
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
close_ctree(root);
|
|
|
|
exit(1);
|
2023-10-23 22:42:52 +00:00
|
|
|
root = open_ctree(argv[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
|
2013-01-21 15:57:25 +00:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "Open ctree failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-01 20:16:26 +00:00
|
|
|
printf("starting search\n");
|
|
|
|
srand(55);
|
|
|
|
for (i = 0; i < run_size; i++) {
|
|
|
|
num = next_key(i, max_key);
|
|
|
|
ins.objectid = num;
|
|
|
|
if (i % 10000 == 0)
|
|
|
|
fprintf(stderr, "search %d:%d\n", num, i);
|
2008-04-15 15:32:17 +00:00
|
|
|
ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (ret) {
|
2023-10-23 22:27:41 +00:00
|
|
|
btrfs_print_tree(root->node, BTRFS_PRINT_TREE_BFS);
|
2007-03-01 20:16:26 +00:00
|
|
|
printf("unable to find %d\n", num);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-03 00:52:43 +00:00
|
|
|
btrfs_release_path(&path);
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
2008-04-15 15:32:17 +00:00
|
|
|
close_ctree(root);
|
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
root = open_ctree(argv[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
|
2013-01-21 15:57:25 +00:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "Open ctree failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-08-11 22:46:49 +00:00
|
|
|
printf("node %p level %d total ptrs %u free spc %lu\n", root->node,
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_header_level(root->node),
|
|
|
|
btrfs_header_nritems(root->node),
|
2018-01-26 07:26:01 +00:00
|
|
|
(unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) -
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_header_nritems(root->node));
|
2007-03-01 20:16:26 +00:00
|
|
|
printf("all searches good, deleting some items\n");
|
|
|
|
i = 0;
|
|
|
|
srand(55);
|
2008-04-15 15:32:17 +00:00
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 14:48:16 +00:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2007-03-01 20:16:26 +00:00
|
|
|
for (i = 0 ; i < run_size/4; i++) {
|
|
|
|
num = next_key(i, max_key);
|
|
|
|
ins.objectid = num;
|
2007-03-16 20:20:31 +00:00
|
|
|
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (!ret) {
|
|
|
|
if (i % 10000 == 0)
|
|
|
|
fprintf(stderr, "del %d:%d\n", num, i);
|
2007-03-16 20:20:31 +00:00
|
|
|
ret = btrfs_del_item(trans, root, &path);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (ret != 0)
|
|
|
|
BUG();
|
|
|
|
tree_size--;
|
|
|
|
}
|
2013-08-03 00:52:43 +00:00
|
|
|
btrfs_release_path(&path);
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
close_ctree(root);
|
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
root = open_ctree(argv[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
|
2013-01-21 15:57:25 +00:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "Open ctree failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-04-15 15:32:17 +00:00
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 14:48:16 +00:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2007-03-01 20:16:26 +00:00
|
|
|
srand(128);
|
|
|
|
for (i = 0; i < run_size; i++) {
|
|
|
|
num = next_key(i, max_key);
|
|
|
|
sprintf(buf, "string-%d", num);
|
|
|
|
ins.objectid = num;
|
|
|
|
if (i % 10000 == 0)
|
|
|
|
fprintf(stderr, "insert %d:%d\n", num, i);
|
2007-04-04 17:27:22 +00:00
|
|
|
ret = btrfs_insert_item(trans, root, &ins, buf, 512);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (!ret)
|
|
|
|
tree_size++;
|
|
|
|
}
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
close_ctree(root);
|
|
|
|
|
2023-10-23 22:42:52 +00:00
|
|
|
root = open_ctree(argv[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
|
2013-01-21 15:57:25 +00:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "Open ctree failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-01 20:16:26 +00:00
|
|
|
srand(128);
|
|
|
|
printf("starting search2\n");
|
|
|
|
for (i = 0; i < run_size; i++) {
|
|
|
|
num = next_key(i, max_key);
|
|
|
|
ins.objectid = num;
|
|
|
|
if (i % 10000 == 0)
|
|
|
|
fprintf(stderr, "search %d:%d\n", num, i);
|
2008-04-15 15:32:17 +00:00
|
|
|
ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (ret) {
|
2023-10-23 22:27:41 +00:00
|
|
|
btrfs_print_tree(root->node, BTRFS_PRINT_TREE_BFS);
|
2007-03-01 20:16:26 +00:00
|
|
|
printf("unable to find %d\n", num);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-03 00:52:43 +00:00
|
|
|
btrfs_release_path(&path);
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
|
|
|
printf("starting big long delete run\n");
|
2008-04-15 15:32:17 +00:00
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2017-08-28 14:48:16 +00:00
|
|
|
BUG_ON(IS_ERR(trans));
|
2008-04-15 15:32:17 +00:00
|
|
|
while(root->node && btrfs_header_nritems(root->node) > 0) {
|
|
|
|
struct extent_buffer *leaf;
|
2007-03-01 20:16:26 +00:00
|
|
|
int slot;
|
|
|
|
ins.objectid = (u64)-1;
|
2007-03-16 20:20:31 +00:00
|
|
|
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (ret == 0)
|
|
|
|
BUG();
|
|
|
|
|
2008-04-15 15:32:17 +00:00
|
|
|
leaf = path.nodes[0];
|
2007-03-01 20:16:26 +00:00
|
|
|
slot = path.slots[0];
|
2008-04-15 15:32:17 +00:00
|
|
|
if (slot != btrfs_header_nritems(leaf))
|
2007-03-01 20:16:26 +00:00
|
|
|
BUG();
|
|
|
|
while(path.slots[0] > 0) {
|
|
|
|
path.slots[0] -= 1;
|
|
|
|
slot = path.slots[0];
|
2008-04-15 15:32:17 +00:00
|
|
|
leaf = path.nodes[0];
|
|
|
|
|
|
|
|
btrfs_item_key_to_cpu(leaf, &last, slot);
|
2007-03-01 20:16:26 +00:00
|
|
|
|
|
|
|
if (tree_size % 10000 == 0)
|
|
|
|
printf("big del %d:%d\n", tree_size, i);
|
2007-03-16 20:20:31 +00:00
|
|
|
ret = btrfs_del_item(trans, root, &path);
|
2007-03-01 20:16:26 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
printf("del_item returned %d\n", ret);
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
tree_size--;
|
|
|
|
}
|
2013-08-03 00:52:43 +00:00
|
|
|
btrfs_release_path(&path);
|
2007-03-01 20:16:26 +00:00
|
|
|
}
|
2007-03-07 01:08:01 +00:00
|
|
|
/*
|
|
|
|
printf("previous tree:\n");
|
2007-03-13 14:46:10 +00:00
|
|
|
btrfs_print_tree(root, root->commit_root);
|
2007-03-07 01:08:01 +00:00
|
|
|
printf("map before commit\n");
|
2007-03-13 14:46:10 +00:00
|
|
|
btrfs_print_tree(root->extent_root, root->extent_root->node);
|
2007-03-07 01:08:01 +00:00
|
|
|
*/
|
2008-04-15 15:32:17 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
2007-03-01 20:16:26 +00:00
|
|
|
printf("tree size is now %d\n", tree_size);
|
2007-03-07 01:08:01 +00:00
|
|
|
printf("root %p commit root %p\n", root->node, root->commit_root);
|
2023-10-23 22:27:41 +00:00
|
|
|
btrfs_print_tree(root->node, BTRFS_PRINT_TREE_BFS);
|
2008-04-15 15:32:17 +00:00
|
|
|
close_ctree(root);
|
2007-03-01 20:16:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|