Implement raid0 when multiple devices are present
[btrfs-progs-unstable/devel.git] / disk-io.c
blob1afe5a6edb0aa571d72265826170a65ef51506c0
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 "volumes.h"
32 #include "transaction.h"
33 #include "crc32c.h"
34 #include "utils.h"
36 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
37 u64 logical)
39 u64 physical;
40 u64 length;
41 struct btrfs_device *device;
42 int ret;
44 ret = btrfs_map_block(&root->fs_info->mapping_tree, logical, &physical,
45 &length, &device);
46 BUG_ON(ret);
47 buf->fd = device->fd;
48 buf->dev_bytenr = physical;
49 return 0;
52 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
54 if (buf->start != btrfs_header_bytenr(buf))
55 BUG();
57 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
58 (unsigned long)btrfs_header_fsid(buf),
59 BTRFS_FSID_SIZE))
60 BUG();
61 return 0;
64 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
66 return crc32c(seed, data, len);
69 void btrfs_csum_final(u32 crc, char *result)
71 *(__le32 *)result = ~cpu_to_le32(crc);
74 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
75 int verify)
77 char result[BTRFS_CRC32_SIZE];
78 u32 len;
79 u32 crc = ~(u32)0;
81 len = buf->len - BTRFS_CSUM_SIZE;
82 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
83 btrfs_csum_final(crc, result);
85 if (verify) {
86 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
87 printk("checksum verify failed on %llu\n", buf->start);
88 return 1;
90 } else {
91 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
93 return 0;
96 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
97 u64 bytenr, u32 blocksize)
99 return find_extent_buffer(&root->fs_info->extent_cache,
100 bytenr, blocksize);
103 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
104 u64 bytenr, u32 blocksize)
106 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
107 blocksize);
110 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
112 return 0;
115 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
116 u32 blocksize)
118 int ret;
119 struct extent_buffer *eb;
121 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
122 if (!eb)
123 return NULL;
124 if (!btrfs_buffer_uptodate(eb)) {
125 btrfs_map_bh_to_logical(root, eb, eb->start);
126 ret = read_extent_from_disk(eb);
127 if (ret) {
128 free_extent_buffer(eb);
129 return NULL;
131 btrfs_set_buffer_uptodate(eb);
133 return eb;
136 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
137 struct extent_buffer *eb)
139 if (check_tree_block(root, eb))
140 BUG();
141 if (!btrfs_buffer_uptodate(eb))
142 BUG();
143 btrfs_map_bh_to_logical(root, eb, eb->start);
144 csum_tree_block(root, eb, 0);
145 return write_extent_to_disk(eb);
148 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
149 u32 stripesize, struct btrfs_root *root,
150 struct btrfs_fs_info *fs_info, u64 objectid)
152 root->node = NULL;
153 root->commit_root = NULL;
154 root->sectorsize = sectorsize;
155 root->nodesize = nodesize;
156 root->leafsize = leafsize;
157 root->stripesize = stripesize;
158 root->ref_cows = 0;
159 root->track_dirty = 0;
161 root->fs_info = fs_info;
162 root->objectid = objectid;
163 root->last_trans = 0;
164 root->highest_inode = 0;
165 root->last_inode_alloc = 0;
167 INIT_LIST_HEAD(&root->dirty_list);
168 memset(&root->root_key, 0, sizeof(root->root_key));
169 memset(&root->root_item, 0, sizeof(root->root_item));
170 root->root_key.objectid = objectid;
171 return 0;
174 static int update_cowonly_root(struct btrfs_trans_handle *trans,
175 struct btrfs_root *root)
177 int ret;
178 u64 old_root_bytenr;
179 struct btrfs_root *tree_root = root->fs_info->tree_root;
181 btrfs_write_dirty_block_groups(trans, root);
182 while(1) {
183 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
184 if (old_root_bytenr == root->node->start)
185 break;
186 btrfs_set_root_bytenr(&root->root_item,
187 root->node->start);
188 root->root_item.level = btrfs_header_level(root->node);
189 ret = btrfs_update_root(trans, tree_root,
190 &root->root_key,
191 &root->root_item);
192 BUG_ON(ret);
193 btrfs_write_dirty_block_groups(trans, root);
195 return 0;
198 static int commit_tree_roots(struct btrfs_trans_handle *trans,
199 struct btrfs_fs_info *fs_info)
201 struct btrfs_root *root;
202 struct list_head *next;
204 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
205 next = fs_info->dirty_cowonly_roots.next;
206 list_del_init(next);
207 root = list_entry(next, struct btrfs_root, dirty_list);
208 update_cowonly_root(trans, root);
210 return 0;
213 static int __commit_transaction(struct btrfs_trans_handle *trans,
214 struct btrfs_root *root)
216 u64 start;
217 u64 end;
218 struct extent_buffer *eb;
219 struct extent_io_tree *tree = &root->fs_info->extent_cache;
220 int ret;
222 while(1) {
223 ret = find_first_extent_bit(tree, 0, &start, &end,
224 EXTENT_DIRTY);
225 if (ret)
226 break;
227 while(start <= end) {
228 eb = find_first_extent_buffer(tree, start);
229 BUG_ON(!eb || eb->start != start);
230 ret = write_tree_block(trans, root, eb);
231 BUG_ON(ret);
232 start += eb->len;
233 clear_extent_buffer_dirty(eb);
234 free_extent_buffer(eb);
237 return 0;
240 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root)
243 int ret = 0;
244 struct btrfs_root *new_root = NULL;
245 struct btrfs_fs_info *fs_info = root->fs_info;
247 if (root->commit_root == root->node)
248 goto commit_tree;
250 new_root = malloc(sizeof(*new_root));
251 if (!new_root)
252 return -ENOMEM;
253 memcpy(new_root, root, sizeof(*new_root));
254 new_root->node = root->commit_root;
255 root->commit_root = NULL;
257 root->root_key.offset = trans->transid;
258 btrfs_set_root_bytenr(&root->root_item, root->node->start);
259 root->root_item.level = btrfs_header_level(root->node);
260 ret = btrfs_insert_root(trans, fs_info->tree_root,
261 &root->root_key, &root->root_item);
262 BUG_ON(ret);
264 btrfs_set_root_refs(&new_root->root_item, 0);
265 ret = btrfs_update_root(trans, root->fs_info->tree_root,
266 &new_root->root_key, &new_root->root_item);
267 BUG_ON(ret);
269 ret = commit_tree_roots(trans, fs_info);
270 BUG_ON(ret);
271 ret = __commit_transaction(trans, root);
272 BUG_ON(ret);
273 write_ctree_super(trans, root);
274 btrfs_finish_extent_commit(trans, fs_info->extent_root,
275 &fs_info->pinned_extents);
276 btrfs_free_transaction(root, trans);
277 fs_info->running_transaction = NULL;
279 trans = btrfs_start_transaction(root, 1);
280 ret = btrfs_drop_snapshot(trans, new_root);
281 BUG_ON(ret);
282 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
283 BUG_ON(ret);
284 commit_tree:
285 ret = commit_tree_roots(trans, fs_info);
286 BUG_ON(ret);
287 ret = __commit_transaction(trans, root);
288 BUG_ON(ret);
289 write_ctree_super(trans, root);
290 btrfs_finish_extent_commit(trans, fs_info->extent_root,
291 &fs_info->pinned_extents);
292 btrfs_free_transaction(root, trans);
293 free_extent_buffer(root->commit_root);
294 root->commit_root = NULL;
295 fs_info->running_transaction = NULL;
296 if (new_root) {
297 free_extent_buffer(new_root->node);
298 free(new_root);
300 return 0;
303 static int find_and_setup_root(struct btrfs_root *tree_root,
304 struct btrfs_fs_info *fs_info,
305 u64 objectid, struct btrfs_root *root)
307 int ret;
308 u32 blocksize;
310 __setup_root(tree_root->nodesize, tree_root->leafsize,
311 tree_root->sectorsize, tree_root->stripesize,
312 root, fs_info, objectid);
313 ret = btrfs_find_last_root(tree_root, objectid,
314 &root->root_item, &root->root_key);
315 BUG_ON(ret);
317 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
318 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
319 blocksize);
320 BUG_ON(!root->node);
321 return 0;
324 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
326 if (root->node)
327 free_extent_buffer(root->node);
328 if (root->commit_root)
329 free_extent_buffer(root->commit_root);
331 free(root);
332 return 0;
335 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
336 struct btrfs_key *location)
338 struct btrfs_root *root;
339 struct btrfs_root *tree_root = fs_info->tree_root;
340 struct btrfs_path *path;
341 struct extent_buffer *l;
342 u32 blocksize;
343 int ret = 0;
345 root = malloc(sizeof(*root));
346 if (!root)
347 return ERR_PTR(-ENOMEM);
348 memset(root, 0, sizeof(*root));
349 if (location->offset == (u64)-1) {
350 ret = find_and_setup_root(tree_root, fs_info,
351 location->objectid, root);
352 if (ret) {
353 free(root);
354 return ERR_PTR(ret);
356 goto insert;
359 __setup_root(tree_root->nodesize, tree_root->leafsize,
360 tree_root->sectorsize, tree_root->stripesize,
361 root, fs_info, location->objectid);
363 path = btrfs_alloc_path();
364 BUG_ON(!path);
365 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
366 if (ret != 0) {
367 if (ret > 0)
368 ret = -ENOENT;
369 goto out;
371 l = path->nodes[0];
372 read_extent_buffer(l, &root->root_item,
373 btrfs_item_ptr_offset(l, path->slots[0]),
374 sizeof(root->root_item));
375 memcpy(&root->root_key, location, sizeof(*location));
376 ret = 0;
377 out:
378 btrfs_release_path(root, path);
379 btrfs_free_path(path);
380 if (ret) {
381 free(root);
382 return ERR_PTR(ret);
384 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
385 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
386 blocksize);
387 BUG_ON(!root->node);
388 insert:
389 root->ref_cows = 1;
390 return root;
393 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
395 int fp;
397 fp = open(filename, O_CREAT | O_RDWR, 0600);
398 if (fp < 0) {
399 return NULL;
401 return open_ctree_fd(fp, filename, sb_bytenr);
404 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
406 u32 sectorsize;
407 u32 nodesize;
408 u32 leafsize;
409 u32 blocksize;
410 u32 stripesize;
411 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
412 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
413 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
414 struct btrfs_root *chunk_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 int ret;
418 struct btrfs_super_block *disk_super;
419 struct btrfs_fs_devices *fs_devices = NULL;
420 u64 total_devs;
422 if (sb_bytenr == 0)
423 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
425 ret = btrfs_scan_one_device(fp, path, &fs_devices,
426 &total_devs, sb_bytenr);
428 if (ret) {
429 fprintf(stderr, "No valid Btrfs found on %s\n", path);
430 return NULL;
432 fprintf(stderr, "found Btrfs on %s with %Lu devices\n", path,
433 total_devs);
435 if (total_devs != 1) {
436 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
437 BUG_ON(ret);
440 fs_info->fp = fs_devices->lowest_bdev;
441 fs_info->running_transaction = NULL;
442 fs_info->fs_root = root;
443 fs_info->tree_root = tree_root;
444 fs_info->extent_root = extent_root;
445 fs_info->extent_ops = NULL;
446 fs_info->priv_data = NULL;
447 fs_info->chunk_root = chunk_root;
448 fs_info->dev_root = dev_root;
449 fs_info->force_system_allocs = 0;
451 extent_io_tree_init(&fs_info->extent_cache);
452 extent_io_tree_init(&fs_info->free_space_cache);
453 extent_io_tree_init(&fs_info->block_group_cache);
454 extent_io_tree_init(&fs_info->pinned_extents);
455 extent_io_tree_init(&fs_info->pending_del);
456 extent_io_tree_init(&fs_info->extent_ins);
458 cache_tree_init(&fs_info->mapping_tree.cache_tree);
460 mutex_init(&fs_info->fs_mutex);
461 fs_info->fs_devices = fs_devices;
462 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
463 INIT_LIST_HEAD(&fs_info->space_info);
465 __setup_root(4096, 4096, 4096, 4096, tree_root,
466 fs_info, BTRFS_ROOT_TREE_OBJECTID);
468 ret = btrfs_open_devices(fs_devices, O_RDWR);
469 BUG_ON(ret);
471 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
472 4096);
473 BUG_ON(!fs_info->sb_buffer);
474 fs_info->sb_buffer->fd = fs_devices->lowest_bdev;
475 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
476 ret = read_extent_from_disk(fs_info->sb_buffer);
477 BUG_ON(ret);
478 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
480 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
481 sizeof(fs_info->super_copy));
482 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
483 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
484 BTRFS_FSID_SIZE);
486 disk_super = &fs_info->super_copy;
487 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
488 sizeof(disk_super->magic))) {
489 printk("No valid btrfs found\n");
490 BUG_ON(1);
492 nodesize = btrfs_super_nodesize(disk_super);
493 leafsize = btrfs_super_leafsize(disk_super);
494 sectorsize = btrfs_super_sectorsize(disk_super);
495 stripesize = btrfs_super_stripesize(disk_super);
496 tree_root->nodesize = nodesize;
497 tree_root->leafsize = leafsize;
498 tree_root->sectorsize = sectorsize;
499 tree_root->stripesize = stripesize;
501 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
502 BUG_ON(ret);
503 ret = btrfs_read_sys_array(tree_root);
504 BUG_ON(ret);
505 blocksize = btrfs_level_size(tree_root,
506 btrfs_super_chunk_root_level(disk_super));
508 __setup_root(nodesize, leafsize, sectorsize, stripesize,
509 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
511 chunk_root->node = read_tree_block(chunk_root,
512 btrfs_super_chunk_root(disk_super),
513 blocksize);
515 BUG_ON(!chunk_root->node);
517 ret = btrfs_read_chunk_tree(chunk_root);
518 BUG_ON(ret);
520 blocksize = btrfs_level_size(tree_root,
521 btrfs_super_root_level(disk_super));
523 tree_root->node = read_tree_block(tree_root,
524 btrfs_super_root(disk_super),
525 blocksize);
526 BUG_ON(!tree_root->node);
527 ret = find_and_setup_root(tree_root, fs_info,
528 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
529 BUG_ON(ret);
530 extent_root->track_dirty = 1;
532 ret = find_and_setup_root(tree_root, fs_info,
533 BTRFS_DEV_TREE_OBJECTID, dev_root);
534 BUG_ON(ret);
535 dev_root->track_dirty = 1;
537 ret = find_and_setup_root(tree_root, fs_info,
538 BTRFS_FS_TREE_OBJECTID, root);
539 BUG_ON(ret);
540 root->ref_cows = 1;
541 fs_info->generation = btrfs_super_generation(disk_super) + 1;
542 btrfs_read_block_groups(root);
543 return root;
546 int write_ctree_super(struct btrfs_trans_handle *trans,
547 struct btrfs_root *root)
549 int ret;
550 struct btrfs_root *tree_root = root->fs_info->tree_root;
551 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
552 btrfs_set_super_generation(&root->fs_info->super_copy,
553 trans->transid);
554 btrfs_set_super_root(&root->fs_info->super_copy,
555 tree_root->node->start);
556 btrfs_set_super_root_level(&root->fs_info->super_copy,
557 btrfs_header_level(tree_root->node));
558 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
559 chunk_root->node->start);
560 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
561 btrfs_header_level(chunk_root->node));
562 write_extent_buffer(root->fs_info->sb_buffer,
563 &root->fs_info->super_copy, 0,
564 sizeof(root->fs_info->super_copy));
565 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
566 if (ret)
567 fprintf(stderr, "failed to write new super block err %d\n", ret);
568 return ret;
571 static int close_all_devices(struct btrfs_fs_info *fs_info)
573 struct list_head *list;
574 struct list_head *next;
575 struct btrfs_device *device;
577 return 0;
579 list = &fs_info->fs_devices->devices;
580 list_for_each(next, list) {
581 device = list_entry(next, struct btrfs_device, dev_list);
582 // close(device->fd);
584 return 0;
587 int close_ctree(struct btrfs_root *root)
589 int ret;
590 struct btrfs_trans_handle *trans;
591 struct btrfs_fs_info *fs_info = root->fs_info;
593 trans = btrfs_start_transaction(root, 1);
594 btrfs_commit_transaction(trans, root);
595 trans = btrfs_start_transaction(root, 1);
596 ret = commit_tree_roots(trans, root->fs_info);
597 BUG_ON(ret);
598 ret = __commit_transaction(trans, root);
599 BUG_ON(ret);
600 write_ctree_super(trans, root);
601 btrfs_free_transaction(root, trans);
602 btrfs_free_block_groups(root->fs_info);
603 close(root->fs_info->fp);
604 if (root->node)
605 free_extent_buffer(root->node);
606 if (root->fs_info->extent_root->node)
607 free_extent_buffer(root->fs_info->extent_root->node);
608 if (root->fs_info->tree_root->node)
609 free_extent_buffer(root->fs_info->tree_root->node);
610 free_extent_buffer(root->commit_root);
611 free_extent_buffer(root->fs_info->sb_buffer);
613 if (root->fs_info->chunk_root->node);
614 free_extent_buffer(root->fs_info->chunk_root->node);
616 if (root->fs_info->dev_root->node);
617 free_extent_buffer(root->fs_info->dev_root->node);
619 close_all_devices(root->fs_info);
620 extent_io_tree_cleanup(&fs_info->extent_cache);
621 extent_io_tree_cleanup(&fs_info->free_space_cache);
622 extent_io_tree_cleanup(&fs_info->block_group_cache);
623 extent_io_tree_cleanup(&fs_info->pinned_extents);
624 extent_io_tree_cleanup(&fs_info->pending_del);
625 extent_io_tree_cleanup(&fs_info->extent_ins);
627 free(fs_info->tree_root);
628 free(fs_info->extent_root);
629 free(fs_info->fs_root);
630 free(fs_info->chunk_root);
631 free(fs_info->dev_root);
632 free(fs_info);
634 return 0;
637 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
638 struct extent_buffer *eb)
640 return clear_extent_buffer_dirty(eb);
643 int wait_on_tree_block_writeback(struct btrfs_root *root,
644 struct extent_buffer *eb)
646 return 0;
649 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
651 set_extent_buffer_dirty(eb);
654 int btrfs_buffer_uptodate(struct extent_buffer *eb)
656 return extent_buffer_uptodate(eb);
659 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
661 return set_extent_buffer_uptodate(eb);