blob: 25ce07908ee3f89cb94679932a16d33963785f26 [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"
Chris Masone089f052007-03-16 16:20:31 -040012#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050013
14static int allocated_blocks = 0;
Chris Masoned2ff2c2007-03-01 18:59:40 -050015int cache_max = 10000;
Chris Masoneb60cea2007-02-02 09:18:22 -050016
Chris Mason234b63a2007-03-13 10:46:10 -040017static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050018{
Chris Mason7518a232007-03-12 12:01:18 -040019 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050020 BUG();
Chris Mason7518a232007-03-12 12:01:18 -040021 if (root->node && btrfs_header_parentid(&buf->node.header) !=
22 btrfs_header_parentid(&root->node->node.header))
Chris Mason9a8dd152007-02-23 08:38:36 -050023 BUG();
24 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050025}
26
Chris Mason234b63a2007-03-13 10:46:10 -040027static int free_some_buffers(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -050028{
29 struct list_head *node, *next;
Chris Mason234b63a2007-03-13 10:46:10 -040030 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -050031 if (root->cache_size < cache_max)
32 return 0;
33 list_for_each_safe(node, next, &root->cache) {
Chris Mason234b63a2007-03-13 10:46:10 -040034 b = list_entry(node, struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -050035 if (b->count == 1) {
36 BUG_ON(!list_empty(&b->dirty));
37 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -040038 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -050039 if (root->cache_size < cache_max)
Chris Mason77ce6842007-03-02 10:06:43 -050040 break;
Chris Masoned2ff2c2007-03-01 18:59:40 -050041 }
42 }
43 return 0;
44}
45
Chris Mason234b63a2007-03-13 10:46:10 -040046struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050047{
Chris Mason234b63a2007-03-13 10:46:10 -040048 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050049 int ret;
Chris Mason123abc82007-03-14 14:14:43 -040050
51 buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
Chris Masoneb60cea2007-02-02 09:18:22 -050052 if (!buf)
53 return buf;
54 allocated_blocks++;
55 buf->blocknr = blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -050056 buf->count = 2;
57 INIT_LIST_HEAD(&buf->dirty);
58 free_some_buffers(root);
Chris Masoneb60cea2007-02-02 09:18:22 -050059 radix_tree_preload(GFP_KERNEL);
60 ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
61 radix_tree_preload_end();
Chris Masoned2ff2c2007-03-01 18:59:40 -050062 list_add_tail(&buf->cache, &root->cache);
63 root->cache_size++;
Chris Masoneb60cea2007-02-02 09:18:22 -050064 if (ret) {
65 free(buf);
66 return NULL;
67 }
68 return buf;
69}
70
Chris Mason234b63a2007-03-13 10:46:10 -040071struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050072{
Chris Mason234b63a2007-03-13 10:46:10 -040073 struct btrfs_buffer *buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050074 buf = radix_tree_lookup(&root->cache_radix, blocknr);
75 if (buf) {
76 buf->count++;
77 } else {
78 buf = alloc_tree_block(root, blocknr);
79 if (!buf) {
80 BUG();
81 return NULL;
82 }
Chris Masoneb60cea2007-02-02 09:18:22 -050083 }
Chris Masoneb60cea2007-02-02 09:18:22 -050084 return buf;
85}
86
Chris Mason234b63a2007-03-13 10:46:10 -040087struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050088{
Chris Mason123abc82007-03-14 14:14:43 -040089 loff_t offset = blocknr * root->blocksize;
Chris Mason234b63a2007-03-13 10:46:10 -040090 struct btrfs_buffer *buf;
Chris Masoneb60cea2007-02-02 09:18:22 -050091 int ret;
92
93 buf = radix_tree_lookup(&root->cache_radix, blocknr);
94 if (buf) {
95 buf->count++;
Chris Mason9a8dd152007-02-23 08:38:36 -050096 } else {
97 buf = alloc_tree_block(root, blocknr);
98 if (!buf)
99 return NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400100 ret = pread(root->fp, &buf->node, root->blocksize, offset);
101 if (ret != root->blocksize) {
Chris Mason9a8dd152007-02-23 08:38:36 -0500102 free(buf);
103 return NULL;
104 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500105 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500106 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -0500107 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500108 return buf;
109}
110
Chris Masone089f052007-03-16 16:20:31 -0400111int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
112 struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500113{
114 if (!list_empty(&buf->dirty))
115 return 0;
116 list_add_tail(&buf->dirty, &root->trans);
117 buf->count++;
118 return 0;
119}
120
Chris Masone089f052007-03-16 16:20:31 -0400121int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
122 struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500123{
124 if (!list_empty(&buf->dirty)) {
125 list_del_init(&buf->dirty);
Chris Mason234b63a2007-03-13 10:46:10 -0400126 btrfs_block_release(root, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500127 }
128 return 0;
129}
130
Chris Masone089f052007-03-16 16:20:31 -0400131int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
132 struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
134 u64 blocknr = buf->blocknr;
Chris Mason123abc82007-03-14 14:14:43 -0400135 loff_t offset = blocknr * root->blocksize;
Chris Masoneb60cea2007-02-02 09:18:22 -0500136 int ret;
137
Chris Mason7518a232007-03-12 12:01:18 -0400138 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500139 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400140 ret = pwrite(root->fp, &buf->node, root->blocksize, offset);
141 if (ret != root->blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500143 return 0;
144}
145
Chris Masone089f052007-03-16 16:20:31 -0400146static int __commit_transaction(struct btrfs_trans_handle *trans, struct
147 btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500148{
Chris Mason234b63a2007-03-13 10:46:10 -0400149 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500150 int ret = 0;
151 int wret;
152 while(!list_empty(&root->trans)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400153 b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500154 list_del_init(&b->dirty);
Chris Masone089f052007-03-16 16:20:31 -0400155 wret = write_tree_block(trans, root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500156 if (wret)
157 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400158 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500159 }
160 return ret;
161}
162
Chris Masone089f052007-03-16 16:20:31 -0400163static int commit_extent_and_tree_roots(struct btrfs_trans_handle *trans,
164 struct btrfs_root *tree_root, struct
165 btrfs_root *extent_root)
Chris Mason3768f362007-03-13 16:47:54 -0400166{
167 int ret;
168 u64 old_extent_block;
169
170 while(1) {
171 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
172 if (old_extent_block == extent_root->node->blocknr)
173 break;
174 btrfs_set_root_blocknr(&extent_root->root_item,
175 extent_root->node->blocknr);
Chris Masone089f052007-03-16 16:20:31 -0400176 ret = btrfs_update_root(trans, tree_root,
Chris Mason3768f362007-03-13 16:47:54 -0400177 &extent_root->root_key,
178 &extent_root->root_item);
179 BUG_ON(ret);
180 }
Chris Masone089f052007-03-16 16:20:31 -0400181 __commit_transaction(trans, extent_root);
182 __commit_transaction(trans, tree_root);
Chris Mason3768f362007-03-13 16:47:54 -0400183 return 0;
184}
185
Chris Masone089f052007-03-16 16:20:31 -0400186int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
187 btrfs_root *root, struct btrfs_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500188{
Chris Masona28ec192007-03-06 20:08:01 -0500189 int ret = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400190 struct btrfs_buffer *snap = root->commit_root;
191 struct btrfs_key snap_key;
Chris Masona28ec192007-03-06 20:08:01 -0500192
Chris Masone089f052007-03-16 16:20:31 -0400193 ret = __commit_transaction(trans, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500194 BUG_ON(ret);
Chris Mason3768f362007-03-13 16:47:54 -0400195
196 if (root->commit_root == root->node)
197 return 0;
198
199 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
200 root->root_key.offset++;
201
202 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
Chris Masone089f052007-03-16 16:20:31 -0400203 ret = btrfs_insert_root(trans, root->tree_root, &root->root_key,
Chris Mason3768f362007-03-13 16:47:54 -0400204 &root->root_item);
205 BUG_ON(ret);
206
Chris Masone089f052007-03-16 16:20:31 -0400207 ret = commit_extent_and_tree_roots(trans, root->tree_root,
208 root->extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400209 BUG_ON(ret);
210
Chris Masone089f052007-03-16 16:20:31 -0400211 write_ctree_super(trans, root, s);
212 btrfs_finish_extent_commit(trans, root->extent_root);
213 btrfs_finish_extent_commit(trans, root->tree_root);
Chris Mason3768f362007-03-13 16:47:54 -0400214
215 root->commit_root = root->node;
216 root->node->count++;
Chris Masone089f052007-03-16 16:20:31 -0400217 ret = btrfs_drop_snapshot(trans, root, snap);
Chris Mason3768f362007-03-13 16:47:54 -0400218 BUG_ON(ret);
219
Chris Masone089f052007-03-16 16:20:31 -0400220 ret = btrfs_del_root(trans, root->tree_root, &snap_key);
Chris Mason3768f362007-03-13 16:47:54 -0400221 BUG_ON(ret);
222
Chris Masoned2ff2c2007-03-01 18:59:40 -0500223 return ret;
224}
225
Chris Mason123abc82007-03-14 14:14:43 -0400226static int __setup_root(struct btrfs_super_block *super,
227 struct btrfs_root *root, u64 objectid, int fp)
Chris Masond97e63b2007-02-20 16:40:44 -0500228{
Chris Masoned2ff2c2007-03-01 18:59:40 -0500229 INIT_LIST_HEAD(&root->trans);
230 INIT_LIST_HEAD(&root->cache);
Chris Masona28ec192007-03-06 20:08:01 -0500231 root->cache_size = 0;
Chris Masond97e63b2007-02-20 16:40:44 -0500232 root->fp = fp;
Chris Masoncfaa7292007-02-21 17:04:57 -0500233 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500234 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400235 root->blocksize = btrfs_super_blocksize(super);
236 root->ref_cows = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500237 memset(&root->current_insert, 0, sizeof(root->current_insert));
Chris Mason0579da42007-03-07 16:15:30 -0500238 memset(&root->last_insert, 0, sizeof(root->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400239 memset(&root->root_key, 0, sizeof(root->root_key));
240 memset(&root->root_item, 0, sizeof(root->root_item));
241 return 0;
242}
243
Chris Mason123abc82007-03-14 14:14:43 -0400244static int find_and_setup_root(struct btrfs_super_block *super,
245 struct btrfs_root *tree_root, u64 objectid,
246 struct btrfs_root *root, int fp)
Chris Mason3768f362007-03-13 16:47:54 -0400247{
248 int ret;
249
Chris Mason123abc82007-03-14 14:14:43 -0400250 __setup_root(super, root, objectid, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400251 ret = btrfs_find_last_root(tree_root, objectid,
252 &root->root_item, &root->root_key);
253 BUG_ON(ret);
254
255 root->node = read_tree_block(root,
256 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400257 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500258 return 0;
259}
260
Chris Mason234b63a2007-03-13 10:46:10 -0400261struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500262{
Chris Mason234b63a2007-03-13 10:46:10 -0400263 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
264 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
Chris Mason3768f362007-03-13 16:47:54 -0400265 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500266 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500267 int ret;
268
Chris Mason3768f362007-03-13 16:47:54 -0400269 root->extent_root = extent_root;
270 root->tree_root = tree_root;
271
272 extent_root->extent_root = extent_root;
273 extent_root->tree_root = tree_root;
274
275 tree_root->extent_root = extent_root;
276 tree_root->tree_root = tree_root;
277
Chris Masonc6730242007-02-26 10:46:55 -0500278 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500279 if (fp < 0) {
280 free(root);
281 return NULL;
282 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500283 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
Chris Masona28ec192007-03-06 20:08:01 -0500284 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
285 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
Chris Mason9a8dd152007-02-23 08:38:36 -0500286 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Mason3768f362007-03-13 16:47:54 -0400287 INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL);
288 INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);
289
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 Mason3768f362007-03-13 16:47:54 -0400292 if (ret == 0 || btrfs_super_root(super) == 0) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500293 printf("making new FS!\n");
Chris Mason123abc82007-03-14 14:14:43 -0400294 ret = mkfs(fp, 0, 1024);
Chris Masond97e63b2007-02-20 16:40:44 -0500295 if (ret)
296 return NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400297 ret = pread(fp, super, sizeof(struct btrfs_super_block),
Chris Mason123abc82007-03-14 14:14:43 -0400298 BTRFS_SUPER_INFO_OFFSET);
Chris Mason234b63a2007-03-13 10:46:10 -0400299 if (ret != sizeof(struct btrfs_super_block))
Chris Masond97e63b2007-02-20 16:40:44 -0500300 return NULL;
301 }
302 BUG_ON(ret < 0);
Chris Mason3768f362007-03-13 16:47:54 -0400303
Chris Mason123abc82007-03-14 14:14:43 -0400304 __setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400305 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
306 BUG_ON(!tree_root->node);
307
Chris Mason123abc82007-03-14 14:14:43 -0400308 ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID,
Chris Mason3768f362007-03-13 16:47:54 -0400309 extent_root, fp);
310 BUG_ON(ret);
311
Chris Mason123abc82007-03-14 14:14:43 -0400312 ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID,
Chris Mason3768f362007-03-13 16:47:54 -0400313 root, fp);
314 BUG_ON(ret);
315
Chris Masona28ec192007-03-06 20:08:01 -0500316 root->commit_root = root->node;
317 root->node->count++;
Chris Mason3768f362007-03-13 16:47:54 -0400318 root->ref_cows = 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500319 return root;
320}
321
Chris Masone089f052007-03-16 16:20:31 -0400322int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
323 *root, struct btrfs_super_block *s)
Chris Masoncfaa7292007-02-21 17:04:57 -0500324{
325 int ret;
Chris Mason3768f362007-03-13 16:47:54 -0400326 btrfs_set_super_root(s, root->tree_root->node->blocknr);
Chris Mason234b63a2007-03-13 10:46:10 -0400327 ret = pwrite(root->fp, s, sizeof(*s),
Chris Mason123abc82007-03-14 14:14:43 -0400328 BTRFS_SUPER_INFO_OFFSET);
Chris Masoncfaa7292007-02-21 17:04:57 -0500329 if (ret != sizeof(*s)) {
330 fprintf(stderr, "failed to write new super block err %d\n", ret);
331 return ret;
332 }
333 return 0;
334}
335
Chris Mason234b63a2007-03-13 10:46:10 -0400336static int drop_cache(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500337{
338 while(!list_empty(&root->cache)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400339 struct btrfs_buffer *b = list_entry(root->cache.next,
340 struct btrfs_buffer, cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500341 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -0400342 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500343 }
344 return 0;
345}
Chris Mason234b63a2007-03-13 10:46:10 -0400346int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoneb60cea2007-02-02 09:18:22 -0500347{
Chris Mason3768f362007-03-13 16:47:54 -0400348 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400349 struct btrfs_trans_handle *trans;
350
351 trans = root->running_transaction;
352 btrfs_commit_transaction(trans, root, s);
353 ret = commit_extent_and_tree_roots(trans, root->tree_root,
354 root->extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400355 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -0400356 write_ctree_super(trans, root, s);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500357 drop_cache(root->extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400358 drop_cache(root->tree_root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500359 drop_cache(root);
360 BUG_ON(!list_empty(&root->trans));
361 BUG_ON(!list_empty(&root->extent_root->trans));
Chris Mason3768f362007-03-13 16:47:54 -0400362 BUG_ON(!list_empty(&root->tree_root->trans));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500363
Chris Masoneb60cea2007-02-02 09:18:22 -0500364 close(root->fp);
365 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400366 btrfs_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500367 if (root->extent_root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400368 btrfs_block_release(root->extent_root, root->extent_root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400369 if (root->tree_root->node)
370 btrfs_block_release(root->tree_root, root->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400371 btrfs_block_release(root, root->commit_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500372 free(root);
373 printf("on close %d blocks are allocated\n", allocated_blocks);
374 return 0;
375}
376
Chris Mason234b63a2007-03-13 10:46:10 -0400377void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500378{
Chris Masoneb60cea2007-02-02 09:18:22 -0500379 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500380 if (buf->count < 0)
381 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500382 if (buf->count == 0) {
Chris Mason02217ed2007-03-02 16:08:05 -0500383 BUG_ON(!list_empty(&buf->cache));
384 BUG_ON(!list_empty(&buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500385 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
386 BUG();
387 radix_tree_delete(&root->cache_radix, buf->blocknr);
388 memset(buf, 0, sizeof(*buf));
389 free(buf);
390 BUG_ON(allocated_blocks == 0);
391 allocated_blocks--;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500392 BUG_ON(root->cache_size == 0);
393 root->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500394 }
395}
396