Btrfs: add transaction.h to the Makefile
[linux-2.6-block.git] / fs / btrfs / disk-io.c
CommitLineData
eb60ceac
CM
1#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"
e089f05c 12#include "transaction.h"
eb60ceac
CM
13
14static int allocated_blocks = 0;
ed2ff2cb 15int cache_max = 10000;
eb60ceac 16
234b63a0 17static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
eb60ceac 18{
7518a238 19 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
9a8dd150 20 BUG();
7518a238
CM
21 if (root->node && btrfs_header_parentid(&buf->node.header) !=
22 btrfs_header_parentid(&root->node->node.header))
9a8dd150
CM
23 BUG();
24 return 0;
eb60ceac
CM
25}
26
234b63a0 27static int free_some_buffers(struct btrfs_root *root)
ed2ff2cb
CM
28{
29 struct list_head *node, *next;
234b63a0 30 struct btrfs_buffer *b;
ed2ff2cb
CM
31 if (root->cache_size < cache_max)
32 return 0;
33 list_for_each_safe(node, next, &root->cache) {
234b63a0 34 b = list_entry(node, struct btrfs_buffer, cache);
ed2ff2cb
CM
35 if (b->count == 1) {
36 BUG_ON(!list_empty(&b->dirty));
37 list_del_init(&b->cache);
234b63a0 38 btrfs_block_release(root, b);
ed2ff2cb 39 if (root->cache_size < cache_max)
77ce6846 40 break;
ed2ff2cb
CM
41 }
42 }
43 return 0;
44}
45
234b63a0 46struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 47{
234b63a0 48 struct btrfs_buffer *buf;
eb60ceac 49 int ret;
123abc88
CM
50
51 buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
eb60ceac
CM
52 if (!buf)
53 return buf;
54 allocated_blocks++;
55 buf->blocknr = blocknr;
ed2ff2cb
CM
56 buf->count = 2;
57 INIT_LIST_HEAD(&buf->dirty);
58 free_some_buffers(root);
eb60ceac
CM
59 radix_tree_preload(GFP_KERNEL);
60 ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
61 radix_tree_preload_end();
ed2ff2cb
CM
62 list_add_tail(&buf->cache, &root->cache);
63 root->cache_size++;
eb60ceac
CM
64 if (ret) {
65 free(buf);
66 return NULL;
67 }
68 return buf;
69}
70
234b63a0 71struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 72{
234b63a0 73 struct btrfs_buffer *buf;
9a8dd150
CM
74 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 }
eb60ceac 83 }
eb60ceac
CM
84 return buf;
85}
86
234b63a0 87struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 88{
123abc88 89 loff_t offset = blocknr * root->blocksize;
234b63a0 90 struct btrfs_buffer *buf;
eb60ceac
CM
91 int ret;
92
93 buf = radix_tree_lookup(&root->cache_radix, blocknr);
94 if (buf) {
95 buf->count++;
9a8dd150
CM
96 } else {
97 buf = alloc_tree_block(root, blocknr);
98 if (!buf)
99 return NULL;
123abc88
CM
100 ret = pread(root->fp, &buf->node, root->blocksize, offset);
101 if (ret != root->blocksize) {
9a8dd150
CM
102 free(buf);
103 return NULL;
104 }
eb60ceac 105 }
9a8dd150 106 if (check_tree_block(root, buf))
cfaa7295 107 BUG();
eb60ceac
CM
108 return buf;
109}
110
e089f05c
CM
111int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
112 struct btrfs_buffer *buf)
ed2ff2cb
CM
113{
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
e089f05c
CM
121int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
122 struct btrfs_buffer *buf)
ed2ff2cb
CM
123{
124 if (!list_empty(&buf->dirty)) {
125 list_del_init(&buf->dirty);
234b63a0 126 btrfs_block_release(root, buf);
ed2ff2cb
CM
127 }
128 return 0;
129}
130
e089f05c
CM
131int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
132 struct btrfs_buffer *buf)
eb60ceac
CM
133{
134 u64 blocknr = buf->blocknr;
123abc88 135 loff_t offset = blocknr * root->blocksize;
eb60ceac
CM
136 int ret;
137
7518a238 138 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
eb60ceac 139 BUG();
123abc88
CM
140 ret = pwrite(root->fp, &buf->node, root->blocksize, offset);
141 if (ret != root->blocksize)
eb60ceac 142 return ret;
eb60ceac
CM
143 return 0;
144}
145
e089f05c
CM
146static int __commit_transaction(struct btrfs_trans_handle *trans, struct
147 btrfs_root *root)
ed2ff2cb 148{
234b63a0 149 struct btrfs_buffer *b;
ed2ff2cb
CM
150 int ret = 0;
151 int wret;
152 while(!list_empty(&root->trans)) {
234b63a0 153 b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
ed2ff2cb 154 list_del_init(&b->dirty);
e089f05c 155 wret = write_tree_block(trans, root, b);
ed2ff2cb
CM
156 if (wret)
157 ret = wret;
234b63a0 158 btrfs_block_release(root, b);
ed2ff2cb
CM
159 }
160 return ret;
161}
162
e089f05c
CM
163static int commit_extent_and_tree_roots(struct btrfs_trans_handle *trans,
164 struct btrfs_root *tree_root, struct
165 btrfs_root *extent_root)
3768f368
CM
166{
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);
e089f05c 176 ret = btrfs_update_root(trans, tree_root,
3768f368
CM
177 &extent_root->root_key,
178 &extent_root->root_item);
179 BUG_ON(ret);
180 }
e089f05c
CM
181 __commit_transaction(trans, extent_root);
182 __commit_transaction(trans, tree_root);
3768f368
CM
183 return 0;
184}
185
e089f05c
CM
186int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
187 btrfs_root *root, struct btrfs_super_block *s)
ed2ff2cb 188{
a28ec197 189 int ret = 0;
3768f368
CM
190 struct btrfs_buffer *snap = root->commit_root;
191 struct btrfs_key snap_key;
a28ec197 192
e089f05c 193 ret = __commit_transaction(trans, root);
ed2ff2cb 194 BUG_ON(ret);
3768f368
CM
195
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);
e089f05c 203 ret = btrfs_insert_root(trans, root->tree_root, &root->root_key,
3768f368
CM
204 &root->root_item);
205 BUG_ON(ret);
206
e089f05c
CM
207 ret = commit_extent_and_tree_roots(trans, root->tree_root,
208 root->extent_root);
3768f368
CM
209 BUG_ON(ret);
210
e089f05c
CM
211 write_ctree_super(trans, root, s);
212 btrfs_finish_extent_commit(trans, root->extent_root);
213 btrfs_finish_extent_commit(trans, root->tree_root);
3768f368
CM
214
215 root->commit_root = root->node;
216 root->node->count++;
e089f05c 217 ret = btrfs_drop_snapshot(trans, root, snap);
3768f368
CM
218 BUG_ON(ret);
219
e089f05c 220 ret = btrfs_del_root(trans, root->tree_root, &snap_key);
3768f368
CM
221 BUG_ON(ret);
222
ed2ff2cb
CM
223 return ret;
224}
225
123abc88
CM
226static int __setup_root(struct btrfs_super_block *super,
227 struct btrfs_root *root, u64 objectid, int fp)
d97e63b6 228{
ed2ff2cb
CM
229 INIT_LIST_HEAD(&root->trans);
230 INIT_LIST_HEAD(&root->cache);
a28ec197 231 root->cache_size = 0;
d97e63b6 232 root->fp = fp;
cfaa7295 233 root->node = NULL;
a28ec197 234 root->commit_root = NULL;
123abc88
CM
235 root->blocksize = btrfs_super_blocksize(super);
236 root->ref_cows = 0;
a28ec197 237 memset(&root->current_insert, 0, sizeof(root->current_insert));
0579da42 238 memset(&root->last_insert, 0, sizeof(root->last_insert));
3768f368
CM
239 memset(&root->root_key, 0, sizeof(root->root_key));
240 memset(&root->root_item, 0, sizeof(root->root_item));
241 return 0;
242}
243
123abc88
CM
244static 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)
3768f368
CM
247{
248 int ret;
249
123abc88 250 __setup_root(super, root, objectid, fp);
3768f368
CM
251 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));
3768f368 257 BUG_ON(!root->node);
d97e63b6
CM
258 return 0;
259}
260
234b63a0 261struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
eb60ceac 262{
234b63a0
CM
263 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
264 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
3768f368 265 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
eb60ceac 266 int fp;
eb60ceac
CM
267 int ret;
268
3768f368
CM
269 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
c673024a 278 fp = open(filename, O_CREAT | O_RDWR, 0600);
eb60ceac
CM
279 if (fp < 0) {
280 free(root);
281 return NULL;
282 }
9a8dd150 283 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
a28ec197
CM
284 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
285 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
9a8dd150 286 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
3768f368
CM
287 INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL);
288 INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);
289
234b63a0 290 ret = pread(fp, super, sizeof(struct btrfs_super_block),
123abc88 291 BTRFS_SUPER_INFO_OFFSET);
3768f368 292 if (ret == 0 || btrfs_super_root(super) == 0) {
5c680ed6 293 printf("making new FS!\n");
123abc88 294 ret = mkfs(fp, 0, 1024);
d97e63b6
CM
295 if (ret)
296 return NULL;
234b63a0 297 ret = pread(fp, super, sizeof(struct btrfs_super_block),
123abc88 298 BTRFS_SUPER_INFO_OFFSET);
234b63a0 299 if (ret != sizeof(struct btrfs_super_block))
d97e63b6
CM
300 return NULL;
301 }
302 BUG_ON(ret < 0);
3768f368 303
123abc88 304 __setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
3768f368
CM
305 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
306 BUG_ON(!tree_root->node);
307
123abc88 308 ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID,
3768f368
CM
309 extent_root, fp);
310 BUG_ON(ret);
311
123abc88 312 ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID,
3768f368
CM
313 root, fp);
314 BUG_ON(ret);
315
a28ec197
CM
316 root->commit_root = root->node;
317 root->node->count++;
3768f368 318 root->ref_cows = 1;
eb60ceac
CM
319 return root;
320}
321
e089f05c
CM
322int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
323 *root, struct btrfs_super_block *s)
eb60ceac
CM
324{
325 int ret;
3768f368 326 btrfs_set_super_root(s, root->tree_root->node->blocknr);
234b63a0 327 ret = pwrite(root->fp, s, sizeof(*s),
123abc88 328 BTRFS_SUPER_INFO_OFFSET);
cfaa7295
CM
329 if (ret != sizeof(*s)) {
330 fprintf(stderr, "failed to write new super block err %d\n", ret);
eb60ceac 331 return ret;
cfaa7295
CM
332 }
333 return 0;
334}
335
234b63a0 336static int drop_cache(struct btrfs_root *root)
ed2ff2cb
CM
337{
338 while(!list_empty(&root->cache)) {
234b63a0
CM
339 struct btrfs_buffer *b = list_entry(root->cache.next,
340 struct btrfs_buffer, cache);
ed2ff2cb 341 list_del_init(&b->cache);
234b63a0 342 btrfs_block_release(root, b);
ed2ff2cb
CM
343 }
344 return 0;
345}
234b63a0 346int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
cfaa7295 347{
3768f368 348 int ret;
e089f05c
CM
349 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);
3768f368 355 BUG_ON(ret);
e089f05c 356 write_ctree_super(trans, root, s);
ed2ff2cb 357 drop_cache(root->extent_root);
3768f368 358 drop_cache(root->tree_root);
ed2ff2cb
CM
359 drop_cache(root);
360 BUG_ON(!list_empty(&root->trans));
361 BUG_ON(!list_empty(&root->extent_root->trans));
3768f368 362 BUG_ON(!list_empty(&root->tree_root->trans));
ed2ff2cb 363
cfaa7295
CM
364 close(root->fp);
365 if (root->node)
234b63a0 366 btrfs_block_release(root, root->node);
cfaa7295 367 if (root->extent_root->node)
234b63a0 368 btrfs_block_release(root->extent_root, root->extent_root->node);
3768f368
CM
369 if (root->tree_root->node)
370 btrfs_block_release(root->tree_root, root->tree_root->node);
234b63a0 371 btrfs_block_release(root, root->commit_root);
cfaa7295
CM
372 free(root);
373 printf("on close %d blocks are allocated\n", allocated_blocks);
eb60ceac
CM
374 return 0;
375}
376
234b63a0 377void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
eb60ceac
CM
378{
379 buf->count--;
cfaa7295
CM
380 if (buf->count < 0)
381 BUG();
eb60ceac 382 if (buf->count == 0) {
02217ed2
CM
383 BUG_ON(!list_empty(&buf->cache));
384 BUG_ON(!list_empty(&buf->dirty));
eb60ceac
CM
385 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--;
ed2ff2cb
CM
392 BUG_ON(root->cache_size == 0);
393 root->cache_size--;
eb60ceac
CM
394 }
395}
396