bring back the inode number directory index
[btrfs-progs-unstable.git] / disk-io.c
blob49bda1486dcb1f9c64086841921f0129cb6be3b1
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"
12 #include "transaction.h"
14 static int allocated_blocks = 0;
15 int cache_max = 10000;
17 struct dev_lookup {
18 u64 block_start;
19 u64 num_blocks;
20 u64 device_id;
21 int fd;
24 int btrfs_insert_dev_radix(struct btrfs_root *root,
25 int fd,
26 u64 device_id,
27 u64 block_start,
28 u64 num_blocks)
30 struct dev_lookup *lookup;
31 int ret;
33 lookup = malloc(sizeof(*lookup));
34 if (!lookup)
35 return -ENOMEM;
36 lookup->block_start = block_start;
37 lookup->num_blocks = num_blocks;
38 lookup->fd = fd;
39 lookup->device_id = device_id;
40 printf("inserting into dev radix %Lu %Lu\n", block_start, num_blocks);
42 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
43 num_blocks - 1, lookup);
44 return ret;
47 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh,
48 u64 logical)
50 struct dev_lookup *lookup[2];
52 int ret;
54 root = root->fs_info->dev_root;
55 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
56 (void **)lookup,
57 (unsigned long)logical,
58 ARRAY_SIZE(lookup));
59 if (ret == 0 || lookup[0]->block_start > logical ||
60 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
61 ret = -1;
62 goto out;
64 bh->fd = lookup[0]->fd;
65 bh->dev_blocknr = logical - lookup[0]->block_start;
66 ret = 0;
67 out:
68 return ret;
71 static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
73 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
74 BUG();
75 if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
76 sizeof(buf->node.header.fsid)))
77 BUG();
78 return 0;
81 static int free_some_buffers(struct btrfs_root *root)
83 struct list_head *node, *next;
84 struct btrfs_buffer *b;
85 if (root->fs_info->cache_size < cache_max)
86 return 0;
87 list_for_each_safe(node, next, &root->fs_info->cache) {
88 b = list_entry(node, struct btrfs_buffer, cache);
89 if (b->count == 1) {
90 BUG_ON(!list_empty(&b->dirty));
91 list_del_init(&b->cache);
92 btrfs_block_release(root, b);
93 if (root->fs_info->cache_size < cache_max)
94 break;
97 return 0;
100 struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
102 struct btrfs_buffer *buf;
103 int ret;
105 buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
106 if (!buf)
107 return buf;
108 allocated_blocks++;
109 buf->blocknr = blocknr;
110 buf->count = 2;
111 INIT_LIST_HEAD(&buf->dirty);
112 free_some_buffers(root);
113 radix_tree_preload(GFP_KERNEL);
114 ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
115 radix_tree_preload_end();
116 list_add_tail(&buf->cache, &root->fs_info->cache);
117 root->fs_info->cache_size++;
118 if (ret) {
119 free(buf);
120 return NULL;
122 return buf;
125 struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
127 struct btrfs_buffer *buf;
128 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
129 if (buf) {
130 buf->count++;
131 } else {
132 buf = alloc_tree_block(root, blocknr);
133 if (!buf) {
134 BUG();
135 return NULL;
138 return buf;
141 struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
143 struct btrfs_buffer *buf;
144 int ret;
145 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
146 if (buf) {
147 buf->count++;
148 } else {
149 buf = alloc_tree_block(root, blocknr);
150 if (!buf)
151 return NULL;
152 btrfs_map_bh_to_logical(root, buf, blocknr);
153 ret = pread(buf->fd, &buf->node, root->blocksize,
154 buf->dev_blocknr * root->blocksize);
155 if (ret != root->blocksize) {
156 free(buf);
157 return NULL;
160 if (check_tree_block(root, buf))
161 BUG();
162 return buf;
165 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
166 struct btrfs_buffer *buf)
168 if (!list_empty(&buf->dirty))
169 return 0;
170 list_add_tail(&buf->dirty, &root->fs_info->trans);
171 buf->count++;
172 return 0;
175 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
176 struct btrfs_buffer *buf)
178 if (!list_empty(&buf->dirty)) {
179 list_del_init(&buf->dirty);
180 btrfs_block_release(root, buf);
182 return 0;
185 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
186 struct btrfs_buffer *buf)
188 int ret;
190 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
191 BUG();
192 btrfs_map_bh_to_logical(root, buf, buf->blocknr);
193 ret = pwrite(buf->fd, &buf->node, root->blocksize,
194 buf->dev_blocknr * root->blocksize);
195 if (ret != root->blocksize)
196 return ret;
197 return 0;
200 static int __commit_transaction(struct btrfs_trans_handle *trans, struct
201 btrfs_root *root)
203 struct btrfs_buffer *b;
204 int ret = 0;
205 int wret;
206 while(!list_empty(&root->fs_info->trans)) {
207 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
208 dirty);
209 list_del_init(&b->dirty);
210 wret = write_tree_block(trans, root, b);
211 if (wret)
212 ret = wret;
213 btrfs_block_release(root, b);
215 return ret;
218 static int commit_tree_roots(struct btrfs_trans_handle *trans,
219 struct btrfs_fs_info *fs_info)
221 int ret;
222 u64 old_extent_block;
223 struct btrfs_root *tree_root = fs_info->tree_root;
224 struct btrfs_root *extent_root = fs_info->extent_root;
226 if (btrfs_super_device_root(fs_info->disk_super) !=
227 fs_info->dev_root->node->blocknr) {
228 btrfs_set_super_device_root(fs_info->disk_super,
229 fs_info->dev_root->node->blocknr);
231 while(1) {
232 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
233 if (old_extent_block == extent_root->node->blocknr)
234 break;
235 btrfs_set_root_blocknr(&extent_root->root_item,
236 extent_root->node->blocknr);
237 ret = btrfs_update_root(trans, tree_root,
238 &extent_root->root_key,
239 &extent_root->root_item);
240 BUG_ON(ret);
242 return 0;
245 int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
246 btrfs_root *root, struct btrfs_super_block *s)
248 int ret = 0;
249 struct btrfs_buffer *snap = root->commit_root;
250 struct btrfs_key snap_key;
252 if (root->commit_root == root->node)
253 return 0;
255 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
256 root->root_key.offset++;
258 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
259 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
260 &root->root_key, &root->root_item);
261 BUG_ON(ret);
263 ret = commit_tree_roots(trans, root->fs_info);
264 BUG_ON(ret);
266 ret = __commit_transaction(trans, root);
267 BUG_ON(ret);
269 write_ctree_super(trans, root, s);
270 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
271 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
273 root->commit_root = root->node;
274 root->node->count++;
275 ret = btrfs_drop_snapshot(trans, root, snap);
276 BUG_ON(ret);
278 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
279 BUG_ON(ret);
280 root->fs_info->generation = root->root_key.offset + 1;
282 return ret;
285 static int __setup_root(struct btrfs_super_block *super,
286 struct btrfs_root *root,
287 struct btrfs_fs_info *fs_info,
288 u64 objectid, int fp)
290 root->node = NULL;
291 root->commit_root = NULL;
292 root->blocksize = btrfs_super_blocksize(super);
293 root->ref_cows = 0;
294 root->fs_info = fs_info;
295 memset(&root->root_key, 0, sizeof(root->root_key));
296 memset(&root->root_item, 0, sizeof(root->root_item));
297 return 0;
300 static int find_and_setup_root(struct btrfs_super_block *super,
301 struct btrfs_root *tree_root,
302 struct btrfs_fs_info *fs_info,
303 u64 objectid,
304 struct btrfs_root *root, int fp)
306 int ret;
308 __setup_root(super, root, fs_info, objectid, fp);
309 ret = btrfs_find_last_root(tree_root, objectid,
310 &root->root_item, &root->root_key);
311 BUG_ON(ret);
313 root->node = read_tree_block(root,
314 btrfs_root_blocknr(&root->root_item));
315 BUG_ON(!root->node);
316 return 0;
319 int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
320 u64 block_start, u64 num_blocks,
321 char *filename, int name_len)
323 char *null_filename;
324 int fd;
325 int ret;
327 null_filename = malloc(name_len + 1);
328 if (!null_filename)
329 return -ENOMEM;
330 memcpy(null_filename, filename, name_len);
331 null_filename[name_len] = '\0';
333 fd = open(null_filename, O_RDWR);
334 if (fd < 0) {
335 ret = -1;
336 goto out;
338 ret = btrfs_insert_dev_radix(root, fd, device_id,
339 block_start, num_blocks);
340 BUG_ON(ret);
341 ret = 0;
342 out:
343 free(null_filename);
344 return ret;
347 static int read_device_info(struct btrfs_root *root)
349 struct btrfs_path path;
350 int ret;
351 struct btrfs_key key;
352 struct btrfs_leaf *leaf;
353 struct btrfs_device_item *dev_item;
354 int nritems;
355 int slot;
357 root = root->fs_info->dev_root;
359 btrfs_init_path(&path);
360 key.objectid = 0;
361 key.offset = 0;
362 key.flags = 0;
363 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
365 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
366 leaf = &path.nodes[0]->leaf;
367 nritems = btrfs_header_nritems(&leaf->header);
368 while(1) {
369 slot = path.slots[0];
370 if (slot >= nritems) {
371 ret = btrfs_next_leaf(root, &path);
372 if (ret)
373 break;
374 leaf = &path.nodes[0]->leaf;
375 nritems = btrfs_header_nritems(&leaf->header);
376 slot = path.slots[0];
378 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
379 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
380 path.slots[0]++;
381 continue;
383 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
384 if (btrfs_device_id(dev_item) !=
385 btrfs_super_device_id(root->fs_info->disk_super)) {
386 printf("found key %Lu %Lu\n", key.objectid, key.offset);
387 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
388 key.objectid, key.offset,
389 (char *)(dev_item + 1),
390 btrfs_device_pathlen(dev_item));
391 BUG_ON(ret);
393 path.slots[0]++;
395 btrfs_release_path(root, &path);
396 return 0;
399 struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
401 int fp;
403 fp = open(filename, O_CREAT | O_RDWR, 0600);
404 if (fp < 0) {
405 return NULL;
407 return open_ctree_fd(fp, super);
410 struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
412 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
413 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
414 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
415 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
416 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
417 struct dev_lookup *dev_lookup;
418 int ret;
420 INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
421 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
422 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_KERNEL);
423 INIT_LIST_HEAD(&fs_info->trans);
424 INIT_LIST_HEAD(&fs_info->cache);
425 fs_info->cache_size = 0;
426 fs_info->fp = fp;
427 fs_info->running_transaction = NULL;
428 fs_info->fs_root = root;
429 fs_info->tree_root = tree_root;
430 fs_info->extent_root = extent_root;
431 fs_info->dev_root = dev_root;
432 fs_info->last_inode_alloc = 0;
433 fs_info->last_inode_alloc_dirid = 0;
434 fs_info->disk_super = super;
435 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
436 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
438 ret = pread(fp, super, sizeof(struct btrfs_super_block),
439 BTRFS_SUPER_INFO_OFFSET);
440 if (ret == 0 || btrfs_super_root(super) == 0) {
441 BUG();
442 return NULL;
444 BUG_ON(ret < 0);
445 __setup_root(super, dev_root, fs_info, BTRFS_DEV_TREE_OBJECTID, fp);
447 dev_lookup = malloc(sizeof(*dev_lookup));
448 dev_lookup->fd = fp;
449 dev_lookup->device_id = btrfs_super_device_id(super);
450 dev_lookup->block_start = btrfs_super_device_block_start(super);
451 dev_lookup->num_blocks = btrfs_super_device_num_blocks(super);
452 ret = radix_tree_insert(&fs_info->dev_radix,
453 dev_lookup->block_start +
454 dev_lookup->num_blocks - 1, dev_lookup);
455 BUG_ON(ret);
457 dev_root->node = read_tree_block(dev_root,
458 btrfs_super_device_root(super));
460 ret = read_device_info(dev_root);
461 BUG_ON(ret);
463 __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
464 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
465 BUG_ON(!tree_root->node);
467 ret = find_and_setup_root(super, tree_root, fs_info,
468 BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
469 BUG_ON(ret);
471 ret = find_and_setup_root(super, tree_root, fs_info,
472 BTRFS_FS_TREE_OBJECTID, root, fp);
473 BUG_ON(ret);
475 root->commit_root = root->node;
476 root->node->count++;
477 root->ref_cows = 1;
478 root->fs_info->generation = root->root_key.offset + 1;
479 return root;
482 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
483 *root, struct btrfs_super_block *s)
485 int ret;
486 btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
487 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
488 BTRFS_SUPER_INFO_OFFSET);
489 if (ret != sizeof(*s)) {
490 fprintf(stderr, "failed to write new super block err %d\n", ret);
491 return ret;
493 return 0;
496 static int drop_cache(struct btrfs_root *root)
498 while(!list_empty(&root->fs_info->cache)) {
499 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
500 struct btrfs_buffer,
501 cache);
502 list_del_init(&b->cache);
503 btrfs_block_release(root, b);
505 return 0;
508 static int free_dev_radix(struct btrfs_fs_info *fs_info)
510 struct dev_lookup *lookup[8];
511 int ret;
512 int i;
513 while(1) {
514 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
515 (void **)lookup, 0,
516 ARRAY_SIZE(lookup));
517 if (!ret)
518 break;
519 for (i = 0; i < ret; i++) {
520 if (lookup[i]->device_id !=
521 btrfs_super_device_id(fs_info->disk_super))
522 close(lookup[i]->fd);
523 radix_tree_delete(&fs_info->dev_radix,
524 lookup[i]->block_start +
525 lookup[i]->num_blocks - 1);
526 free(lookup[i]);
529 return 0;
532 int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
534 int ret;
535 struct btrfs_trans_handle *trans;
537 trans = root->fs_info->running_transaction;
538 btrfs_commit_transaction(trans, root, s);
539 ret = commit_tree_roots(trans, root->fs_info);
540 BUG_ON(ret);
541 ret = __commit_transaction(trans, root);
542 BUG_ON(ret);
543 write_ctree_super(trans, root, s);
544 drop_cache(root);
545 BUG_ON(!list_empty(&root->fs_info->trans));
547 free_dev_radix(root->fs_info);
548 close(root->fs_info->fp);
549 if (root->node)
550 btrfs_block_release(root, root->node);
551 if (root->fs_info->extent_root->node)
552 btrfs_block_release(root->fs_info->extent_root,
553 root->fs_info->extent_root->node);
554 if (root->fs_info->tree_root->node)
555 btrfs_block_release(root->fs_info->tree_root,
556 root->fs_info->tree_root->node);
557 if (root->fs_info->dev_root->node)
558 btrfs_block_release(root->fs_info->dev_root,
559 root->fs_info->dev_root->node);
560 btrfs_block_release(root, root->commit_root);
561 free(root);
562 printf("on close %d blocks are allocated\n", allocated_blocks);
563 return 0;
566 void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
568 buf->count--;
569 if (buf->count < 0)
570 BUG();
571 if (buf->count == 0) {
572 BUG_ON(!list_empty(&buf->cache));
573 BUG_ON(!list_empty(&buf->dirty));
574 if (!radix_tree_lookup(&root->fs_info->cache_radix,
575 buf->blocknr))
576 BUG();
577 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
578 memset(buf, 0, sizeof(*buf));
579 free(buf);
580 BUG_ON(allocated_blocks == 0);
581 allocated_blocks--;
582 BUG_ON(root->fs_info->cache_size == 0);
583 root->fs_info->cache_size--;