Fix inode link count checks in btrfsck
[btrfs-progs-unstable.git] / quick-test.c
blobfa6fd8385c2d3cd548c1104152ba1673d7571439
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
26 #include "print-tree.h"
27 #include "transaction.h"
29 /* for testing only */
30 int next_key(int i, int max_key) {
31 return rand() % max_key;
32 // return i;
35 int main(int ac, char **av) {
36 struct btrfs_key ins;
37 struct btrfs_key last = { (u64)-1, 0, 0};
38 char *buf;
39 int i;
40 int num;
41 int ret;
42 int run_size = 300000;
43 int max_key = 100000000;
44 int tree_size = 2;
45 struct btrfs_path path;
46 struct btrfs_root *root;
47 struct btrfs_trans_handle *trans;
49 buf = malloc(512);
50 memset(buf, 0, 512);
52 radix_tree_init();
54 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
55 trans = btrfs_start_transaction(root, 1);
56 srand(55);
57 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
58 for (i = 0; i < run_size; i++) {
59 num = next_key(i, max_key);
60 // num = i;
61 sprintf(buf, "string-%d", num);
62 if (i % 10000 == 0)
63 fprintf(stderr, "insert %d:%d\n", num, i);
64 ins.objectid = num;
65 ins.offset = 0;
66 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
67 if (!ret)
68 tree_size++;
69 if (i == run_size - 5) {
70 btrfs_commit_transaction(trans, root);
71 trans = btrfs_start_transaction(root, 1);
74 btrfs_commit_transaction(trans, root);
75 close_ctree(root);
76 exit(1);
77 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
78 printf("starting search\n");
79 srand(55);
80 for (i = 0; i < run_size; i++) {
81 num = next_key(i, max_key);
82 ins.objectid = num;
83 btrfs_init_path(&path);
84 if (i % 10000 == 0)
85 fprintf(stderr, "search %d:%d\n", num, i);
86 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
87 if (ret) {
88 btrfs_print_tree(root, root->node, 1);
89 printf("unable to find %d\n", num);
90 exit(1);
92 btrfs_release_path(root, &path);
94 close_ctree(root);
96 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
97 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
98 btrfs_header_level(root->node),
99 btrfs_header_nritems(root->node),
100 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) -
101 btrfs_header_nritems(root->node));
102 printf("all searches good, deleting some items\n");
103 i = 0;
104 srand(55);
105 trans = btrfs_start_transaction(root, 1);
106 for (i = 0 ; i < run_size/4; i++) {
107 num = next_key(i, max_key);
108 ins.objectid = num;
109 btrfs_init_path(&path);
110 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
111 if (!ret) {
112 if (i % 10000 == 0)
113 fprintf(stderr, "del %d:%d\n", num, i);
114 ret = btrfs_del_item(trans, root, &path);
115 if (ret != 0)
116 BUG();
117 tree_size--;
119 btrfs_release_path(root, &path);
121 btrfs_commit_transaction(trans, root);
122 close_ctree(root);
124 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
125 trans = btrfs_start_transaction(root, 1);
126 srand(128);
127 for (i = 0; i < run_size; i++) {
128 num = next_key(i, max_key);
129 sprintf(buf, "string-%d", num);
130 ins.objectid = num;
131 if (i % 10000 == 0)
132 fprintf(stderr, "insert %d:%d\n", num, i);
133 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
134 if (!ret)
135 tree_size++;
137 btrfs_commit_transaction(trans, root);
138 close_ctree(root);
140 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
141 srand(128);
142 printf("starting search2\n");
143 for (i = 0; i < run_size; i++) {
144 num = next_key(i, max_key);
145 ins.objectid = num;
146 btrfs_init_path(&path);
147 if (i % 10000 == 0)
148 fprintf(stderr, "search %d:%d\n", num, i);
149 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
150 if (ret) {
151 btrfs_print_tree(root, root->node, 1);
152 printf("unable to find %d\n", num);
153 exit(1);
155 btrfs_release_path(root, &path);
157 printf("starting big long delete run\n");
158 trans = btrfs_start_transaction(root, 1);
159 while(root->node && btrfs_header_nritems(root->node) > 0) {
160 struct extent_buffer *leaf;
161 int slot;
162 ins.objectid = (u64)-1;
163 btrfs_init_path(&path);
164 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
165 if (ret == 0)
166 BUG();
168 leaf = path.nodes[0];
169 slot = path.slots[0];
170 if (slot != btrfs_header_nritems(leaf))
171 BUG();
172 while(path.slots[0] > 0) {
173 path.slots[0] -= 1;
174 slot = path.slots[0];
175 leaf = path.nodes[0];
177 btrfs_item_key_to_cpu(leaf, &last, slot);
179 if (tree_size % 10000 == 0)
180 printf("big del %d:%d\n", tree_size, i);
181 ret = btrfs_del_item(trans, root, &path);
182 if (ret != 0) {
183 printf("del_item returned %d\n", ret);
184 BUG();
186 tree_size--;
188 btrfs_release_path(root, &path);
191 printf("previous tree:\n");
192 btrfs_print_tree(root, root->commit_root);
193 printf("map before commit\n");
194 btrfs_print_tree(root->extent_root, root->extent_root->node);
196 btrfs_commit_transaction(trans, root);
197 printf("tree size is now %d\n", tree_size);
198 printf("root %p commit root %p\n", root->node, root->commit_root);
199 btrfs_print_tree(root, root->node, 1);
200 close_ctree(root);
201 return 0;