Btrfs-progs: add type root to label property
[platform/upstream/btrfs-progs.git] / quick-test.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
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"
28
29 /* for testing only */
30 static int next_key(int i, int max_key) {
31         return rand() % max_key;
32         // return i;
33 }
34
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;
48
49         buf = malloc(512);
50         memset(buf, 0, 512);
51
52         radix_tree_init();
53
54         root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
55         if (!root) {
56                 fprintf(stderr, "Open ctree failed\n");
57                 exit(1);
58         }
59         trans = btrfs_start_transaction(root, 1);
60         srand(55);
61         btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
62         for (i = 0; i < run_size; i++) {
63                 num = next_key(i, max_key);
64                 // num = i;
65                 sprintf(buf, "string-%d", num);
66                 if (i % 10000 == 0)
67                         fprintf(stderr, "insert %d:%d\n", num, i);
68                 ins.objectid = num;
69                 ins.offset = 0;
70                 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
71                 if (!ret)
72                         tree_size++;
73                 if (i == run_size - 5) {
74                         btrfs_commit_transaction(trans, root);
75                         trans = btrfs_start_transaction(root, 1);
76                 }
77         }
78         btrfs_commit_transaction(trans, root);
79         close_ctree(root);
80         exit(1);
81         root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
82         if (!root) {
83                 fprintf(stderr, "Open ctree failed\n");
84                 exit(1);
85         }
86         printf("starting search\n");
87         srand(55);
88         for (i = 0; i < run_size; i++) {
89                 num = next_key(i, max_key);
90                 ins.objectid = num;
91                 btrfs_init_path(&path);
92                 if (i % 10000 == 0)
93                         fprintf(stderr, "search %d:%d\n", num, i);
94                 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
95                 if (ret) {
96                         btrfs_print_tree(root, root->node, 1);
97                         printf("unable to find %d\n", num);
98                         exit(1);
99                 }
100                 btrfs_release_path(&path);
101         }
102         close_ctree(root);
103
104         root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
105         if (!root) {
106                 fprintf(stderr, "Open ctree failed\n");
107                 exit(1);
108         }
109         printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
110                 btrfs_header_level(root->node),
111                 btrfs_header_nritems(root->node),
112                 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) -
113                 btrfs_header_nritems(root->node));
114         printf("all searches good, deleting some items\n");
115         i = 0;
116         srand(55);
117         trans = btrfs_start_transaction(root, 1);
118         for (i = 0 ; i < run_size/4; i++) {
119                 num = next_key(i, max_key);
120                 ins.objectid = num;
121                 btrfs_init_path(&path);
122                 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
123                 if (!ret) {
124                         if (i % 10000 == 0)
125                                 fprintf(stderr, "del %d:%d\n", num, i);
126                         ret = btrfs_del_item(trans, root, &path);
127                         if (ret != 0)
128                                 BUG();
129                         tree_size--;
130                 }
131                 btrfs_release_path(&path);
132         }
133         btrfs_commit_transaction(trans, root);
134         close_ctree(root);
135
136         root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
137         if (!root) {
138                 fprintf(stderr, "Open ctree failed\n");
139                 exit(1);
140         }
141         trans = btrfs_start_transaction(root, 1);
142         srand(128);
143         for (i = 0; i < run_size; i++) {
144                 num = next_key(i, max_key);
145                 sprintf(buf, "string-%d", num);
146                 ins.objectid = num;
147                 if (i % 10000 == 0)
148                         fprintf(stderr, "insert %d:%d\n", num, i);
149                 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
150                 if (!ret)
151                         tree_size++;
152         }
153         btrfs_commit_transaction(trans, root);
154         close_ctree(root);
155
156         root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
157         if (!root) {
158                 fprintf(stderr, "Open ctree failed\n");
159                 exit(1);
160         }
161         srand(128);
162         printf("starting search2\n");
163         for (i = 0; i < run_size; i++) {
164                 num = next_key(i, max_key);
165                 ins.objectid = num;
166                 btrfs_init_path(&path);
167                 if (i % 10000 == 0)
168                         fprintf(stderr, "search %d:%d\n", num, i);
169                 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
170                 if (ret) {
171                         btrfs_print_tree(root, root->node, 1);
172                         printf("unable to find %d\n", num);
173                         exit(1);
174                 }
175                 btrfs_release_path(&path);
176         }
177         printf("starting big long delete run\n");
178         trans = btrfs_start_transaction(root, 1);
179         while(root->node && btrfs_header_nritems(root->node) > 0) {
180                 struct extent_buffer *leaf;
181                 int slot;
182                 ins.objectid = (u64)-1;
183                 btrfs_init_path(&path);
184                 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
185                 if (ret == 0)
186                         BUG();
187
188                 leaf = path.nodes[0];
189                 slot = path.slots[0];
190                 if (slot != btrfs_header_nritems(leaf))
191                         BUG();
192                 while(path.slots[0] > 0) {
193                         path.slots[0] -= 1;
194                         slot = path.slots[0];
195                         leaf = path.nodes[0];
196
197                         btrfs_item_key_to_cpu(leaf, &last, slot);
198
199                         if (tree_size % 10000 == 0)
200                                 printf("big del %d:%d\n", tree_size, i);
201                         ret = btrfs_del_item(trans, root, &path);
202                         if (ret != 0) {
203                                 printf("del_item returned %d\n", ret);
204                                 BUG();
205                         }
206                         tree_size--;
207                 }
208                 btrfs_release_path(&path);
209         }
210         /*
211         printf("previous tree:\n");
212         btrfs_print_tree(root, root->commit_root);
213         printf("map before commit\n");
214         btrfs_print_tree(root->extent_root, root->extent_root->node);
215         */
216         btrfs_commit_transaction(trans, root);
217         printf("tree size is now %d\n", tree_size);
218         printf("root %p commit root %p\n", root->node, root->commit_root);
219         btrfs_print_tree(root, root->node, 1);
220         close_ctree(root);
221         return 0;
222 }