blob: 8d9457b5aef5704acc09123d5db7702d530d848f [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#define _XOPEN_SOURCE 500
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include "kerncompat.h"
9#include "radix-tree.h"
10#include "ctree.h"
11#include "disk-io.h"
12
13static int allocated_blocks = 0;
Chris Masoned2ff2c2007-03-01 18:59:40 -050014int cache_max = 10000;
Chris Masoneb60cea2007-02-02 09:18:22 -050015
Chris Mason234b63a2007-03-13 10:46:10 -040016static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050017{
Chris Mason7518a232007-03-12 12:01:18 -040018 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050019 BUG();
Chris Mason7518a232007-03-12 12:01:18 -040020 if (root->node && btrfs_header_parentid(&buf->node.header) !=
21 btrfs_header_parentid(&root->node->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050022 BUG();
23 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050024}
25
Chris Mason234b63a2007-03-13 10:46:10 -040026static int free_some_buffers(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -050027{
28 struct list_head *node, *next;
Chris Mason234b63a2007-03-13 10:46:10 -040029 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -050030 if (root->cache_size < cache_max)
31 return 0;
32 list_for_each_safe(node, next, &root->cache) {
Chris Mason234b63a2007-03-13 10:46:10 -040033 b = list_entry(node, struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -050034 if (b->count == 1) {
35 BUG_ON(!list_empty(&b->dirty));
36 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -040037 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -050038 if (root->cache_size < cache_max)
Chris Mason77ce6842007-03-02 10:06:43 -050039 break;
Chris Masoned2ff2c2007-03-01 18:59:40 -050040 }
41 }
42 return 0;
43}
44
Chris Mason234b63a2007-03-13 10:46:10 -040045struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050046{
Chris Mason234b63a2007-03-13 10:46:10 -040047 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050048 int ret;
Chris Mason123abc82007-03-14 14:14:43 -040049
50 buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
Chris Masoneb60cea2007-02-02 09:18:22 -050051 if (!buf)
52 return buf;
53 allocated_blocks++;
54 buf->blocknr = blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -050055 buf->count = 2;
56 INIT_LIST_HEAD(&buf->dirty);
57 free_some_buffers(root);
Chris Masoneb60cea2007-02-02 09:18:22 -050058 radix_tree_preload(GFP_KERNEL);
59 ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
60 radix_tree_preload_end();
Chris Masoned2ff2c2007-03-01 18:59:40 -050061 list_add_tail(&buf->cache, &root->cache);
62 root->cache_size++;
Chris Masoneb60cea2007-02-02 09:18:22 -050063 if (ret) {
64 free(buf);
65 return NULL;
66 }
67 return buf;
68}
69
Chris Mason234b63a2007-03-13 10:46:10 -040070struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050071{
Chris Mason234b63a2007-03-13 10:46:10 -040072 struct btrfs_buffer *buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050073 buf = radix_tree_lookup(&root->cache_radix, blocknr);
74 if (buf) {
75 buf->count++;
76 } else {
77 buf = alloc_tree_block(root, blocknr);
78 if (!buf) {
79 BUG();
80 return NULL;
81 }
Chris Masoneb60cea2007-02-02 09:18:22 -050082 }
Chris Masoneb60cea2007-02-02 09:18:22 -050083 return buf;
84}
85
Chris Mason234b63a2007-03-13 10:46:10 -040086struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050087{
Chris Mason123abc82007-03-14 14:14:43 -040088 loff_t offset = blocknr * root->blocksize;
Chris Mason234b63a2007-03-13 10:46:10 -040089 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050090 int ret;
91
92 buf = radix_tree_lookup(&root->cache_radix, blocknr);
93 if (buf) {
94 buf->count++;
Chris Mason9a8dd152007-02-23 08:38:36 -050095 } else {
96 buf = alloc_tree_block(root, blocknr);
97 if (!buf)
98 return NULL;
Chris Mason123abc82007-03-14 14:14:43 -040099 ret = pread(root->fp, &buf->node, root->blocksize, offset);
100 if (ret != root->blocksize) {
Chris Mason9a8dd152007-02-23 08:38:36 -0500101 free(buf);
102 return NULL;
103 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500104 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500105 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -0500106 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500107 return buf;
108}
109
Chris Mason234b63a2007-03-13 10:46:10 -0400110int dirty_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500111{
112 if (!list_empty(&buf->dirty))
113 return 0;
114 list_add_tail(&buf->dirty, &root->trans);
115 buf->count++;
116 return 0;
117}
118
Chris Mason234b63a2007-03-13 10:46:10 -0400119int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500120{
121 if (!list_empty(&buf->dirty)) {
122 list_del_init(&buf->dirty);
Chris Mason234b63a2007-03-13 10:46:10 -0400123 btrfs_block_release(root, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500124 }
125 return 0;
126}
127
Chris Mason234b63a2007-03-13 10:46:10 -0400128int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 u64 blocknr = buf->blocknr;
Chris Mason123abc82007-03-14 14:14:43 -0400131 loff_t offset = blocknr * root->blocksize;
Chris Masoneb60cea2007-02-02 09:18:22 -0500132 int ret;
133
Chris Mason7518a232007-03-12 12:01:18 -0400134 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500135 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400136 ret = pwrite(root->fp, &buf->node, root->blocksize, offset);
137 if (ret != root->blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500139 return 0;
140}
141
Chris Mason234b63a2007-03-13 10:46:10 -0400142static int __commit_transaction(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500143{
Chris Mason234b63a2007-03-13 10:46:10 -0400144 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500145 int ret = 0;
146 int wret;
147 while(!list_empty(&root->trans)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400148 b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500149 list_del_init(&b->dirty);
150 wret = write_tree_block(root, b);
151 if (wret)
152 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400153 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500154 }
155 return ret;
156}
157
Chris Mason3768f362007-03-13 16:47:54 -0400158static int commit_extent_and_tree_roots(struct btrfs_root *tree_root,
159 struct btrfs_root *extent_root)
160{
161 int ret;
162 u64 old_extent_block;
163
164 while(1) {
165 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
166 if (old_extent_block == extent_root->node->blocknr)
167 break;
168 btrfs_set_root_blocknr(&extent_root->root_item,
169 extent_root->node->blocknr);
170 ret = btrfs_update_root(tree_root,
171 &extent_root->root_key,
172 &extent_root->root_item);
173 BUG_ON(ret);
174 }
175 __commit_transaction(extent_root);
176 __commit_transaction(tree_root);
177 return 0;
178}
179
Chris Mason234b63a2007-03-13 10:46:10 -0400180int btrfs_commit_transaction(struct btrfs_root *root,
181 struct btrfs_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500182{
Chris Masona28ec192007-03-06 20:08:01 -0500183 int ret = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400184 struct btrfs_buffer *snap = root->commit_root;
185 struct btrfs_key snap_key;
Chris Masona28ec192007-03-06 20:08:01 -0500186
Chris Masoned2ff2c2007-03-01 18:59:40 -0500187 ret = __commit_transaction(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500188 BUG_ON(ret);
Chris Mason3768f362007-03-13 16:47:54 -0400189
190 if (root->commit_root == root->node)
191 return 0;
192
193 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
194 root->root_key.offset++;
195
196 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
197 ret = btrfs_insert_root(root->tree_root, &root->root_key,
198 &root->root_item);
199 BUG_ON(ret);
200
201 ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
202 BUG_ON(ret);
203
Chris Masona28ec192007-03-06 20:08:01 -0500204 write_ctree_super(root, s);
Chris Mason3768f362007-03-13 16:47:54 -0400205 btrfs_finish_extent_commit(root->extent_root);
206 btrfs_finish_extent_commit(root->tree_root);
207
208 root->commit_root = root->node;
209 root->node->count++;
210 ret = btrfs_drop_snapshot(root, snap);
211 BUG_ON(ret);
212
213 ret = btrfs_del_root(root->tree_root, &snap_key);
214 BUG_ON(ret);
215
Chris Masoned2ff2c2007-03-01 18:59:40 -0500216 return ret;
217}
218
Chris Mason123abc82007-03-14 14:14:43 -0400219static int __setup_root(struct btrfs_super_block *super,
220 struct btrfs_root *root, u64 objectid, int fp)
Chris Masond97e63b2007-02-20 16:40:44 -0500221{
Chris Masoned2ff2c2007-03-01 18:59:40 -0500222 INIT_LIST_HEAD(&root->trans);
223 INIT_LIST_HEAD(&root->cache);
Chris Masona28ec192007-03-06 20:08:01 -0500224 root->cache_size = 0;
Chris Masond97e63b2007-02-20 16:40:44 -0500225 root->fp = fp;
Chris Masoncfaa7292007-02-21 17:04:57 -0500226 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500227 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400228 root->blocksize = btrfs_super_blocksize(super);
229 root->ref_cows = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500230 memset(&root->current_insert, 0, sizeof(root->current_insert));
Chris Mason0579da42007-03-07 16:15:30 -0500231 memset(&root->last_insert, 0, sizeof(root->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400232 memset(&root->root_key, 0, sizeof(root->root_key));
233 memset(&root->root_item, 0, sizeof(root->root_item));
234 return 0;
235}
236
Chris Mason123abc82007-03-14 14:14:43 -0400237static int find_and_setup_root(struct btrfs_super_block *super,
238 struct btrfs_root *tree_root, u64 objectid,
239 struct btrfs_root *root, int fp)
Chris Mason3768f362007-03-13 16:47:54 -0400240{
241 int ret;
242
Chris Mason123abc82007-03-14 14:14:43 -0400243 __setup_root(super, root, objectid, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400244 ret = btrfs_find_last_root(tree_root, objectid,
245 &root->root_item, &root->root_key);
246 BUG_ON(ret);
247
248 root->node = read_tree_block(root,
249 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400250 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500251 return 0;
252}
253
Chris Mason234b63a2007-03-13 10:46:10 -0400254struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500255{
Chris Mason234b63a2007-03-13 10:46:10 -0400256 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
257 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
Chris Mason3768f362007-03-13 16:47:54 -0400258 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500259 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500260 int ret;
261
Chris Mason3768f362007-03-13 16:47:54 -0400262 root->extent_root = extent_root;
263 root->tree_root = tree_root;
264
265 extent_root->extent_root = extent_root;
266 extent_root->tree_root = tree_root;
267
268 tree_root->extent_root = extent_root;
269 tree_root->tree_root = tree_root;
270
Chris Masonc6730242007-02-26 10:46:55 -0500271 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500272 if (fp < 0) {
273 free(root);
274 return NULL;
275 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500276 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
Chris Masona28ec192007-03-06 20:08:01 -0500277 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
278 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
Chris Mason9a8dd152007-02-23 08:38:36 -0500279 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Mason3768f362007-03-13 16:47:54 -0400280 INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL);
281 INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);
282
Chris Mason234b63a2007-03-13 10:46:10 -0400283 ret = pread(fp, super, sizeof(struct btrfs_super_block),
Chris Mason123abc82007-03-14 14:14:43 -0400284 BTRFS_SUPER_INFO_OFFSET);
Chris Mason3768f362007-03-13 16:47:54 -0400285 if (ret == 0 || btrfs_super_root(super) == 0) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500286 printf("making new FS!\n");
Chris Mason123abc82007-03-14 14:14:43 -0400287 ret = mkfs(fp, 0, 1024);
Chris Masond97e63b2007-02-20 16:40:44 -0500288 if (ret)
289 return NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400290 ret = pread(fp, super, sizeof(struct btrfs_super_block),
Chris Mason123abc82007-03-14 14:14:43 -0400291 BTRFS_SUPER_INFO_OFFSET);
Chris Mason234b63a2007-03-13 10:46:10 -0400292 if (ret != sizeof(struct btrfs_super_block))
Chris Masond97e63b2007-02-20 16:40:44 -0500293 return NULL;
294 }
295 BUG_ON(ret < 0);
Chris Mason3768f362007-03-13 16:47:54 -0400296
Chris Mason123abc82007-03-14 14:14:43 -0400297 __setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400298 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
299 BUG_ON(!tree_root->node);
300
Chris Mason123abc82007-03-14 14:14:43 -0400301 ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID,
Chris Mason3768f362007-03-13 16:47:54 -0400302 extent_root, fp);
303 BUG_ON(ret);
304
Chris Mason123abc82007-03-14 14:14:43 -0400305 ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID,
Chris Mason3768f362007-03-13 16:47:54 -0400306 root, fp);
307 BUG_ON(ret);
308
Chris Masona28ec192007-03-06 20:08:01 -0500309 root->commit_root = root->node;
310 root->node->count++;
Chris Mason3768f362007-03-13 16:47:54 -0400311 root->ref_cows = 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500312 return root;
313}
314
Chris Mason234b63a2007-03-13 10:46:10 -0400315int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoncfaa7292007-02-21 17:04:57 -0500316{
317 int ret;
Chris Mason3768f362007-03-13 16:47:54 -0400318 btrfs_set_super_root(s, root->tree_root->node->blocknr);
Chris Mason234b63a2007-03-13 10:46:10 -0400319 ret = pwrite(root->fp, s, sizeof(*s),
Chris Mason123abc82007-03-14 14:14:43 -0400320 BTRFS_SUPER_INFO_OFFSET);
Chris Masoncfaa7292007-02-21 17:04:57 -0500321 if (ret != sizeof(*s)) {
322 fprintf(stderr, "failed to write new super block err %d\n", ret);
323 return ret;
324 }
325 return 0;
326}
327
Chris Mason234b63a2007-03-13 10:46:10 -0400328static int drop_cache(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500329{
330 while(!list_empty(&root->cache)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400331 struct btrfs_buffer *b = list_entry(root->cache.next,
332 struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500333 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -0400334 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500335 }
336 return 0;
337}
Chris Mason234b63a2007-03-13 10:46:10 -0400338int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoneb60cea2007-02-02 09:18:22 -0500339{
Chris Mason3768f362007-03-13 16:47:54 -0400340 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400341 btrfs_commit_transaction(root, s);
Chris Mason3768f362007-03-13 16:47:54 -0400342 ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
343 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500344 write_ctree_super(root, s);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500345 drop_cache(root->extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400346 drop_cache(root->tree_root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500347 drop_cache(root);
348 BUG_ON(!list_empty(&root->trans));
349 BUG_ON(!list_empty(&root->extent_root->trans));
Chris Mason3768f362007-03-13 16:47:54 -0400350 BUG_ON(!list_empty(&root->tree_root->trans));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500351
Chris Masoneb60cea2007-02-02 09:18:22 -0500352 close(root->fp);
353 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400354 btrfs_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500355 if (root->extent_root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400356 btrfs_block_release(root->extent_root, root->extent_root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400357 if (root->tree_root->node)
358 btrfs_block_release(root->tree_root, root->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400359 btrfs_block_release(root, root->commit_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500360 free(root);
361 printf("on close %d blocks are allocated\n", allocated_blocks);
362 return 0;
363}
364
Chris Mason234b63a2007-03-13 10:46:10 -0400365void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500366{
Chris Masoneb60cea2007-02-02 09:18:22 -0500367 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500368 if (buf->count < 0)
369 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500370 if (buf->count == 0) {
Chris Mason02217ed2007-03-02 16:08:05 -0500371 BUG_ON(!list_empty(&buf->cache));
372 BUG_ON(!list_empty(&buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500373 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
374 BUG();
375 radix_tree_delete(&root->cache_radix, buf->blocknr);
376 memset(buf, 0, sizeof(*buf));
377 free(buf);
378 BUG_ON(allocated_blocks == 0);
379 allocated_blocks--;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500380 BUG_ON(root->cache_size == 0);
381 root->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500382 }
383}
384