i386 fixes from axboe
[btrfs-progs-unstable.git] / disk-io.c
blobcb5d14203cbce746f0a1cd47b1835844418b05f0
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 600
20 #define __USE_XOPEN2K
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "transaction.h"
33 static int allocated_blocks = 0;
34 int cache_max = 10000;
36 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh,
37 u64 logical)
39 bh->fd = root->fs_info->fp;
40 bh->dev_blocknr = logical;
41 return 0;
44 static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
46 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
47 BUG();
48 if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
49 sizeof(buf->node.header.fsid)))
50 BUG();
51 return 0;
54 static int free_some_buffers(struct btrfs_root *root)
56 struct list_head *node, *next;
57 struct btrfs_buffer *b;
58 if (root->fs_info->cache_size < cache_max)
59 return 0;
60 list_for_each_safe(node, next, &root->fs_info->cache) {
61 b = list_entry(node, struct btrfs_buffer, cache);
62 if (b->count == 1) {
63 BUG_ON(!list_empty(&b->dirty));
64 list_del_init(&b->cache);
65 btrfs_block_release(root, b);
66 if (root->fs_info->cache_size < cache_max)
67 break;
70 return 0;
73 struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
75 struct btrfs_buffer *buf;
76 int ret;
78 buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
79 if (!buf)
80 return buf;
81 allocated_blocks++;
82 buf->blocknr = blocknr;
83 buf->count = 2;
84 INIT_LIST_HEAD(&buf->dirty);
85 free_some_buffers(root);
86 radix_tree_preload(GFP_KERNEL);
87 ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
88 radix_tree_preload_end();
89 list_add_tail(&buf->cache, &root->fs_info->cache);
90 root->fs_info->cache_size++;
91 if (ret) {
92 free(buf);
93 return NULL;
95 return buf;
98 struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
100 struct btrfs_buffer *buf;
101 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
102 if (buf) {
103 buf->count++;
104 } else {
105 buf = alloc_tree_block(root, blocknr);
106 if (!buf) {
107 BUG();
108 return NULL;
111 return buf;
114 struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
116 struct btrfs_buffer *buf;
117 int ret;
118 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
119 if (buf) {
120 buf->count++;
121 if (check_tree_block(root, buf))
122 BUG();
123 } else {
124 buf = alloc_tree_block(root, blocknr);
125 if (!buf)
126 return NULL;
127 btrfs_map_bh_to_logical(root, buf, blocknr);
128 ret = pread(buf->fd, &buf->node, root->blocksize,
129 buf->dev_blocknr * root->blocksize);
130 if (ret != root->blocksize) {
131 free(buf);
132 return NULL;
134 if (check_tree_block(root, buf))
135 BUG();
137 return buf;
140 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
141 struct btrfs_buffer *buf)
143 if (!list_empty(&buf->dirty))
144 return 0;
145 list_add_tail(&buf->dirty, &root->fs_info->trans);
146 buf->count++;
147 if (check_tree_block(root, buf))
148 BUG();
149 return 0;
152 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
153 struct btrfs_buffer *buf)
155 if (!list_empty(&buf->dirty)) {
156 list_del_init(&buf->dirty);
157 btrfs_block_release(root, buf);
159 return 0;
162 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
163 struct btrfs_buffer *buf)
165 int ret;
167 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
168 BUG();
169 btrfs_map_bh_to_logical(root, buf, buf->blocknr);
170 if (check_tree_block(root, buf))
171 BUG();
172 ret = pwrite(buf->fd, &buf->node, root->blocksize,
173 buf->dev_blocknr * root->blocksize);
174 if (ret != root->blocksize)
175 return ret;
176 return 0;
179 static int __commit_transaction(struct btrfs_trans_handle *trans, struct
180 btrfs_root *root)
182 struct btrfs_buffer *b;
183 int ret = 0;
184 int wret;
185 while(!list_empty(&root->fs_info->trans)) {
186 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
187 dirty);
188 list_del_init(&b->dirty);
189 wret = write_tree_block(trans, root, b);
190 if (wret)
191 ret = wret;
192 btrfs_block_release(root, b);
194 return ret;
197 static int commit_tree_roots(struct btrfs_trans_handle *trans,
198 struct btrfs_fs_info *fs_info)
200 int ret;
201 u64 old_extent_block;
202 struct btrfs_root *tree_root = fs_info->tree_root;
203 struct btrfs_root *extent_root = fs_info->extent_root;
205 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
206 while(1) {
207 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
208 if (old_extent_block == extent_root->node->blocknr)
209 break;
210 btrfs_set_root_blocknr(&extent_root->root_item,
211 extent_root->node->blocknr);
212 ret = btrfs_update_root(trans, tree_root,
213 &extent_root->root_key,
214 &extent_root->root_item);
215 BUG_ON(ret);
216 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
218 return 0;
221 int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
222 btrfs_root *root, struct btrfs_super_block *s)
224 int ret = 0;
225 struct btrfs_buffer *snap = root->commit_root;
226 struct btrfs_key snap_key;
228 if (root->commit_root == root->node)
229 return 0;
231 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
232 root->root_key.offset++;
234 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
235 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
236 &root->root_key, &root->root_item);
237 BUG_ON(ret);
239 ret = commit_tree_roots(trans, root->fs_info);
240 BUG_ON(ret);
242 ret = __commit_transaction(trans, root);
243 BUG_ON(ret);
245 write_ctree_super(trans, root, s);
246 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
247 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
249 root->commit_root = root->node;
250 root->node->count++;
251 ret = btrfs_drop_snapshot(trans, root, snap);
252 BUG_ON(ret);
254 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
255 BUG_ON(ret);
256 root->fs_info->generation = root->root_key.offset + 1;
258 return ret;
261 static int __setup_root(struct btrfs_super_block *super,
262 struct btrfs_root *root,
263 struct btrfs_fs_info *fs_info,
264 u64 objectid, int fp)
266 root->node = NULL;
267 root->commit_root = NULL;
268 root->blocksize = btrfs_super_blocksize(super);
269 root->ref_cows = 0;
270 root->fs_info = fs_info;
271 memset(&root->root_key, 0, sizeof(root->root_key));
272 memset(&root->root_item, 0, sizeof(root->root_item));
273 root->root_key.objectid = objectid;
274 return 0;
277 static int find_and_setup_root(struct btrfs_super_block *super,
278 struct btrfs_root *tree_root,
279 struct btrfs_fs_info *fs_info,
280 u64 objectid,
281 struct btrfs_root *root, int fp)
283 int ret;
285 __setup_root(super, root, fs_info, objectid, fp);
286 ret = btrfs_find_last_root(tree_root, objectid,
287 &root->root_item, &root->root_key);
288 BUG_ON(ret);
290 root->node = read_tree_block(root,
291 btrfs_root_blocknr(&root->root_item));
292 BUG_ON(!root->node);
293 return 0;
296 struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
298 int fp;
300 fp = open(filename, O_CREAT | O_RDWR, 0600);
301 if (fp < 0) {
302 return NULL;
304 return open_ctree_fd(fp, super);
307 struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
309 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
310 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
311 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
312 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
313 int ret;
315 INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
316 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
317 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
318 INIT_LIST_HEAD(&fs_info->trans);
319 INIT_LIST_HEAD(&fs_info->cache);
320 fs_info->cache_size = 0;
321 fs_info->fp = fp;
322 fs_info->running_transaction = NULL;
323 fs_info->fs_root = root;
324 fs_info->tree_root = tree_root;
325 fs_info->extent_root = extent_root;
326 fs_info->last_inode_alloc = 0;
327 fs_info->last_inode_alloc_dirid = 0;
328 fs_info->disk_super = super;
329 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
330 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
332 ret = pread(fp, super, sizeof(struct btrfs_super_block),
333 BTRFS_SUPER_INFO_OFFSET);
334 if (ret == 0 || btrfs_super_root(super) == 0) {
335 BUG();
336 return NULL;
338 BUG_ON(ret < 0);
340 __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
341 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
342 BUG_ON(!tree_root->node);
344 ret = find_and_setup_root(super, tree_root, fs_info,
345 BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
346 BUG_ON(ret);
348 ret = find_and_setup_root(super, tree_root, fs_info,
349 BTRFS_FS_TREE_OBJECTID, root, fp);
350 BUG_ON(ret);
352 root->commit_root = root->node;
353 root->node->count++;
354 root->ref_cows = 1;
355 root->fs_info->generation = root->root_key.offset + 1;
356 btrfs_read_block_groups(root);
357 return root;
360 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
361 *root, struct btrfs_super_block *s)
363 int ret;
364 btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
365 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
366 BTRFS_SUPER_INFO_OFFSET);
367 if (ret != sizeof(*s)) {
368 fprintf(stderr, "failed to write new super block err %d\n", ret);
369 return ret;
371 return 0;
374 static int drop_cache(struct btrfs_root *root)
376 while(!list_empty(&root->fs_info->cache)) {
377 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
378 struct btrfs_buffer,
379 cache);
380 list_del_init(&b->cache);
381 btrfs_block_release(root, b);
383 return 0;
386 int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
388 int ret;
389 struct btrfs_trans_handle *trans;
391 trans = root->fs_info->running_transaction;
392 btrfs_commit_transaction(trans, root, s);
393 ret = commit_tree_roots(trans, root->fs_info);
394 BUG_ON(ret);
395 ret = __commit_transaction(trans, root);
396 BUG_ON(ret);
397 write_ctree_super(trans, root, s);
398 drop_cache(root);
399 BUG_ON(!list_empty(&root->fs_info->trans));
401 btrfs_free_block_groups(root->fs_info);
402 close(root->fs_info->fp);
403 if (root->node)
404 btrfs_block_release(root, root->node);
405 if (root->fs_info->extent_root->node)
406 btrfs_block_release(root->fs_info->extent_root,
407 root->fs_info->extent_root->node);
408 if (root->fs_info->tree_root->node)
409 btrfs_block_release(root->fs_info->tree_root,
410 root->fs_info->tree_root->node);
411 btrfs_block_release(root, root->commit_root);
412 free(root);
413 printf("on close %d blocks are allocated\n", allocated_blocks);
414 return 0;
417 void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
419 buf->count--;
420 if (buf->count < 0)
421 BUG();
422 if (buf->count == 0) {
423 BUG_ON(!list_empty(&buf->cache));
424 BUG_ON(!list_empty(&buf->dirty));
425 if (!radix_tree_lookup(&root->fs_info->cache_radix,
426 buf->blocknr))
427 BUG();
428 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
429 memset(buf, 0, sizeof(*buf));
430 free(buf);
431 BUG_ON(allocated_blocks == 0);
432 allocated_blocks--;
433 BUG_ON(root->fs_info->cache_size == 0);
434 root->fs_info->cache_size--;