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