blob: bacaa38ea82efe977e2ce08e8f984683c168d09f [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 Mason9f5fae22007-03-20 14:38:32 -040031 if (root->fs_info->cache_size < cache_max)
Chris Masoned2ff2c2007-03-01 18:59:40 -050032 return 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040033 list_for_each_safe(node, next, &root->fs_info->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 Mason9f5fae22007-03-20 14:38:32 -040039 if (root->fs_info->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);
Chris Mason9f5fae22007-03-20 14:38:32 -040060 ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -050061 radix_tree_preload_end();
Chris Mason9f5fae22007-03-20 14:38:32 -040062 list_add_tail(&buf->cache, &root->fs_info->cache);
63 root->fs_info->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 Mason9f5fae22007-03-20 14:38:32 -040074 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
Chris Mason9a8dd152007-02-23 08:38:36 -050075 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
Chris Mason9f5fae22007-03-20 14:38:32 -040093 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
Chris Masoneb60cea2007-02-02 09:18:22 -050094 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 Mason9f5fae22007-03-20 14:38:32 -0400100 ret = pread(root->fs_info->fp, &buf->node, root->blocksize,
101 offset);
Chris Mason123abc82007-03-14 14:14:43 -0400102 if (ret != root->blocksize) {
Chris Mason9a8dd152007-02-23 08:38:36 -0500103 free(buf);
104 return NULL;
105 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500106 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500107 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -0500108 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500109 return buf;
110}
111
Chris Masone089f052007-03-16 16:20:31 -0400112int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
113 struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500114{
115 if (!list_empty(&buf->dirty))
116 return 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400117 list_add_tail(&buf->dirty, &root->fs_info->trans);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500118 buf->count++;
119 return 0;
120}
121
Chris Masone089f052007-03-16 16:20:31 -0400122int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
123 struct btrfs_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500124{
125 if (!list_empty(&buf->dirty)) {
126 list_del_init(&buf->dirty);
Chris Mason234b63a2007-03-13 10:46:10 -0400127 btrfs_block_release(root, buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500128 }
129 return 0;
130}
131
Chris Masone089f052007-03-16 16:20:31 -0400132int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
133 struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500134{
135 u64 blocknr = buf->blocknr;
Chris Mason123abc82007-03-14 14:14:43 -0400136 loff_t offset = blocknr * root->blocksize;
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 int ret;
138
Chris Mason7518a232007-03-12 12:01:18 -0400139 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
Chris Masoneb60cea2007-02-02 09:18:22 -0500140 BUG();
Chris Mason9f5fae22007-03-20 14:38:32 -0400141 ret = pwrite(root->fs_info->fp, &buf->node, root->blocksize, offset);
Chris Mason123abc82007-03-14 14:14:43 -0400142 if (ret != root->blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -0500143 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 return 0;
145}
146
Chris Masone089f052007-03-16 16:20:31 -0400147static int __commit_transaction(struct btrfs_trans_handle *trans, struct
148 btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500149{
Chris Mason234b63a2007-03-13 10:46:10 -0400150 struct btrfs_buffer *b;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500151 int ret = 0;
152 int wret;
Chris Mason9f5fae22007-03-20 14:38:32 -0400153 while(!list_empty(&root->fs_info->trans)) {
154 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
155 dirty);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500156 list_del_init(&b->dirty);
Chris Masone089f052007-03-16 16:20:31 -0400157 wret = write_tree_block(trans, root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500158 if (wret)
159 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400160 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500161 }
162 return ret;
163}
164
Chris Mason9f5fae22007-03-20 14:38:32 -0400165static int commit_tree_roots(struct btrfs_trans_handle *trans,
166 struct btrfs_fs_info *fs_info)
Chris Mason3768f362007-03-13 16:47:54 -0400167{
168 int ret;
169 u64 old_extent_block;
Chris Mason9f5fae22007-03-20 14:38:32 -0400170 struct btrfs_root *tree_root = fs_info->tree_root;
171 struct btrfs_root *extent_root = fs_info->extent_root;
172 struct btrfs_root *inode_root = fs_info->inode_root;
Chris Mason3768f362007-03-13 16:47:54 -0400173
Chris Mason9f5fae22007-03-20 14:38:32 -0400174 btrfs_set_root_blocknr(&inode_root->root_item,
175 inode_root->node->blocknr);
176 ret = btrfs_update_root(trans, tree_root,
177 &inode_root->root_key,
178 &inode_root->root_item);
179 BUG_ON(ret);
Chris Mason3768f362007-03-13 16:47:54 -0400180 while(1) {
181 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
182 if (old_extent_block == extent_root->node->blocknr)
183 break;
184 btrfs_set_root_blocknr(&extent_root->root_item,
185 extent_root->node->blocknr);
Chris Masone089f052007-03-16 16:20:31 -0400186 ret = btrfs_update_root(trans, tree_root,
Chris Mason3768f362007-03-13 16:47:54 -0400187 &extent_root->root_key,
188 &extent_root->root_item);
189 BUG_ON(ret);
190 }
Chris Mason3768f362007-03-13 16:47:54 -0400191 return 0;
192}
193
Chris Masone089f052007-03-16 16:20:31 -0400194int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
195 btrfs_root *root, struct btrfs_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500196{
Chris Masona28ec192007-03-06 20:08:01 -0500197 int ret = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400198 struct btrfs_buffer *snap = root->commit_root;
199 struct btrfs_key snap_key;
Chris Masona28ec192007-03-06 20:08:01 -0500200
Chris Mason3768f362007-03-13 16:47:54 -0400201 if (root->commit_root == root->node)
202 return 0;
203
204 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
205 root->root_key.offset++;
206
207 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -0400208 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
209 &root->root_key, &root->root_item);
Chris Mason3768f362007-03-13 16:47:54 -0400210 BUG_ON(ret);
211
Chris Mason9f5fae22007-03-20 14:38:32 -0400212 ret = commit_tree_roots(trans, root->fs_info);
213 BUG_ON(ret);
214
215 ret = __commit_transaction(trans, root);
Chris Mason3768f362007-03-13 16:47:54 -0400216 BUG_ON(ret);
217
Chris Masone089f052007-03-16 16:20:31 -0400218 write_ctree_super(trans, root, s);
Chris Mason9f5fae22007-03-20 14:38:32 -0400219 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
220 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
Chris Mason3768f362007-03-13 16:47:54 -0400221
222 root->commit_root = root->node;
223 root->node->count++;
Chris Masone089f052007-03-16 16:20:31 -0400224 ret = btrfs_drop_snapshot(trans, root, snap);
Chris Mason3768f362007-03-13 16:47:54 -0400225 BUG_ON(ret);
226
Chris Mason9f5fae22007-03-20 14:38:32 -0400227 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
Chris Mason3768f362007-03-13 16:47:54 -0400228 BUG_ON(ret);
Chris Mason293ffd52007-03-20 15:57:25 -0400229 root->fs_info->generation = root->root_key.offset + 1;
Chris Mason3768f362007-03-13 16:47:54 -0400230
Chris Masoned2ff2c2007-03-01 18:59:40 -0500231 return ret;
232}
233
Chris Mason123abc82007-03-14 14:14:43 -0400234static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400235 struct btrfs_root *root,
236 struct btrfs_fs_info *fs_info,
237 u64 objectid, int fp)
Chris Masond97e63b2007-02-20 16:40:44 -0500238{
Chris Masoncfaa7292007-02-21 17:04:57 -0500239 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500240 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400241 root->blocksize = btrfs_super_blocksize(super);
242 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400243 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400244 memset(&root->root_key, 0, sizeof(root->root_key));
245 memset(&root->root_item, 0, sizeof(root->root_item));
246 return 0;
247}
248
Chris Mason123abc82007-03-14 14:14:43 -0400249static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400250 struct btrfs_root *tree_root,
251 struct btrfs_fs_info *fs_info,
252 u64 objectid,
Chris Mason123abc82007-03-14 14:14:43 -0400253 struct btrfs_root *root, int fp)
Chris Mason3768f362007-03-13 16:47:54 -0400254{
255 int ret;
256
Chris Mason9f5fae22007-03-20 14:38:32 -0400257 __setup_root(super, root, fs_info, objectid, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400258 ret = btrfs_find_last_root(tree_root, objectid,
259 &root->root_item, &root->root_key);
260 BUG_ON(ret);
261
262 root->node = read_tree_block(root,
263 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400264 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500265 return 0;
266}
267
Chris Mason234b63a2007-03-13 10:46:10 -0400268struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500269{
Chris Mason234b63a2007-03-13 10:46:10 -0400270 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
271 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
Chris Mason3768f362007-03-13 16:47:54 -0400272 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
Chris Mason9f5fae22007-03-20 14:38:32 -0400273 struct btrfs_root *inode_root = malloc(sizeof(struct btrfs_root));
274 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
Chris Masoneb60cea2007-02-02 09:18:22 -0500275 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500276 int ret;
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 Mason9f5fae22007-03-20 14:38:32 -0400283 INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
284 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
285 INIT_LIST_HEAD(&fs_info->trans);
286 INIT_LIST_HEAD(&fs_info->cache);
287 fs_info->cache_size = 0;
288 fs_info->fp = fp;
289 fs_info->running_transaction = NULL;
290 fs_info->fs_root = root;
291 fs_info->tree_root = tree_root;
292 fs_info->extent_root = extent_root;
293 fs_info->inode_root = inode_root;
294 fs_info->last_inode_alloc = 0;
295 fs_info->last_inode_alloc_dirid = 0;
296 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
297 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400298
Chris Mason234b63a2007-03-13 10:46:10 -0400299 ret = pread(fp, super, sizeof(struct btrfs_super_block),
Chris Mason123abc82007-03-14 14:14:43 -0400300 BTRFS_SUPER_INFO_OFFSET);
Chris Mason3768f362007-03-13 16:47:54 -0400301 if (ret == 0 || btrfs_super_root(super) == 0) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500302 printf("making new FS!\n");
Chris Mason123abc82007-03-14 14:14:43 -0400303 ret = mkfs(fp, 0, 1024);
Chris Masond97e63b2007-02-20 16:40:44 -0500304 if (ret)
305 return NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400306 ret = pread(fp, super, sizeof(struct btrfs_super_block),
Chris Mason123abc82007-03-14 14:14:43 -0400307 BTRFS_SUPER_INFO_OFFSET);
Chris Mason234b63a2007-03-13 10:46:10 -0400308 if (ret != sizeof(struct btrfs_super_block))
Chris Masond97e63b2007-02-20 16:40:44 -0500309 return NULL;
310 }
311 BUG_ON(ret < 0);
Chris Mason3768f362007-03-13 16:47:54 -0400312
Chris Mason9f5fae22007-03-20 14:38:32 -0400313 __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400314 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
315 BUG_ON(!tree_root->node);
316
Chris Mason9f5fae22007-03-20 14:38:32 -0400317 ret = find_and_setup_root(super, tree_root, fs_info,
318 BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400319 BUG_ON(ret);
320
Chris Mason9f5fae22007-03-20 14:38:32 -0400321 ret = find_and_setup_root(super, tree_root, fs_info,
322 BTRFS_INODE_MAP_OBJECTID, inode_root, fp);
323 BUG_ON(ret);
324
325 ret = find_and_setup_root(super, tree_root, fs_info,
326 BTRFS_FS_TREE_OBJECTID, root, fp);
Chris Mason3768f362007-03-13 16:47:54 -0400327 BUG_ON(ret);
328
Chris Masona28ec192007-03-06 20:08:01 -0500329 root->commit_root = root->node;
330 root->node->count++;
Chris Mason3768f362007-03-13 16:47:54 -0400331 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400332 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500333 return root;
334}
335
Chris Masone089f052007-03-16 16:20:31 -0400336int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
337 *root, struct btrfs_super_block *s)
Chris Masoncfaa7292007-02-21 17:04:57 -0500338{
339 int ret;
Chris Mason9f5fae22007-03-20 14:38:32 -0400340 btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
341 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
Chris Mason123abc82007-03-14 14:14:43 -0400342 BTRFS_SUPER_INFO_OFFSET);
Chris Masoncfaa7292007-02-21 17:04:57 -0500343 if (ret != sizeof(*s)) {
344 fprintf(stderr, "failed to write new super block err %d\n", ret);
345 return ret;
346 }
347 return 0;
348}
349
Chris Mason234b63a2007-03-13 10:46:10 -0400350static int drop_cache(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500351{
Chris Mason9f5fae22007-03-20 14:38:32 -0400352 while(!list_empty(&root->fs_info->cache)) {
353 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
354 struct btrfs_buffer,
355 cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500356 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -0400357 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500358 }
359 return 0;
360}
Chris Mason234b63a2007-03-13 10:46:10 -0400361int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
Chris Masoneb60cea2007-02-02 09:18:22 -0500362{
Chris Mason3768f362007-03-13 16:47:54 -0400363 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400364 struct btrfs_trans_handle *trans;
365
Chris Mason9f5fae22007-03-20 14:38:32 -0400366 trans = root->fs_info->running_transaction;
Chris Masone089f052007-03-16 16:20:31 -0400367 btrfs_commit_transaction(trans, root, s);
Chris Mason9f5fae22007-03-20 14:38:32 -0400368 ret = commit_tree_roots(trans, root->fs_info);
369 BUG_ON(ret);
370 ret = __commit_transaction(trans, root);
Chris Mason3768f362007-03-13 16:47:54 -0400371 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -0400372 write_ctree_super(trans, root, s);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500373 drop_cache(root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400374 BUG_ON(!list_empty(&root->fs_info->trans));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500375
Chris Mason9f5fae22007-03-20 14:38:32 -0400376 close(root->fs_info->fp);
Chris Masoneb60cea2007-02-02 09:18:22 -0500377 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400378 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400379 if (root->fs_info->extent_root->node)
380 btrfs_block_release(root->fs_info->extent_root,
381 root->fs_info->extent_root->node);
382 if (root->fs_info->inode_root->node)
383 btrfs_block_release(root->fs_info->inode_root,
384 root->fs_info->inode_root->node);
385 if (root->fs_info->tree_root->node)
386 btrfs_block_release(root->fs_info->tree_root,
387 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400388 btrfs_block_release(root, root->commit_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500389 free(root);
390 printf("on close %d blocks are allocated\n", allocated_blocks);
391 return 0;
392}
393
Chris Mason234b63a2007-03-13 10:46:10 -0400394void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500395{
Chris Masoneb60cea2007-02-02 09:18:22 -0500396 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500397 if (buf->count < 0)
398 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500399 if (buf->count == 0) {
Chris Mason02217ed2007-03-02 16:08:05 -0500400 BUG_ON(!list_empty(&buf->cache));
401 BUG_ON(!list_empty(&buf->dirty));
Chris Mason9f5fae22007-03-20 14:38:32 -0400402 if (!radix_tree_lookup(&root->fs_info->cache_radix,
403 buf->blocknr))
Chris Masoneb60cea2007-02-02 09:18:22 -0500404 BUG();
Chris Mason9f5fae22007-03-20 14:38:32 -0400405 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
Chris Masoneb60cea2007-02-02 09:18:22 -0500406 memset(buf, 0, sizeof(*buf));
407 free(buf);
408 BUG_ON(allocated_blocks == 0);
409 allocated_blocks--;
Chris Mason9f5fae22007-03-20 14:38:32 -0400410 BUG_ON(root->fs_info->cache_size == 0);
411 root->fs_info->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500412 }
413}
414