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
24 #include <sys/types.h>
28 #include "kerncompat.h"
29 #include "radix-tree.h"
33 #include "transaction.h"
36 #include "print-tree.h"
38 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
40 if (buf->start != btrfs_header_bytenr(buf))
43 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
44 (unsigned long)btrfs_header_fsid(buf),
50 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
52 return crc32c(seed, data, len);
55 void btrfs_csum_final(u32 crc, char *result)
57 *(__le32 *)result = ~cpu_to_le32(crc);
60 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
63 char result[BTRFS_CRC32_SIZE];
67 len = buf->len - BTRFS_CSUM_SIZE;
68 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
69 btrfs_csum_final(crc, result);
72 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
73 printk("checksum verify failed on %llu wanted %X "
74 "found %X\n", (unsigned long long)buf->start,
75 *((int *)result), *((int *)buf));
79 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
84 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
85 u64 bytenr, u32 blocksize)
87 return find_extent_buffer(&root->fs_info->extent_cache,
91 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
92 u64 bytenr, u32 blocksize)
94 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
98 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
102 struct extent_buffer *eb;
104 struct btrfs_multi_bio *multi = NULL;
105 struct btrfs_device *device;
107 eb = btrfs_find_tree_block(root, bytenr, blocksize);
108 if (eb && btrfs_buffer_uptodate(eb)) {
109 free_extent_buffer(eb);
115 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
116 bytenr, &length, &multi, 0);
118 device = multi->stripes[0].dev;
120 blocksize = min(blocksize, (u32)(64 * 1024));
121 readahead(device->fd, multi->stripes[0].physical, blocksize);
126 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
131 struct extent_buffer *eb;
133 struct btrfs_multi_bio *multi = NULL;
134 struct btrfs_device *device;
138 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
142 if (btrfs_buffer_uptodate(eb))
148 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
149 eb->start, &length, &multi, mirror_num);
151 device = multi->stripes[0].dev;
154 eb->dev_bytenr = multi->stripes[0].physical;
156 ret = read_extent_from_disk(eb);
157 if (ret == 0 && check_tree_block(root, eb) == 0 &&
158 csum_tree_block(root, eb, 1) == 0) {
159 btrfs_set_buffer_uptodate(eb);
162 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
164 if (num_copies == 1) {
165 printk("reading %Lu failed only one copy\n", eb->start);
169 if (mirror_num > num_copies) {
170 printk("bailing at mirror %d of %d\n", mirror_num, num_copies);
174 free_extent_buffer(eb);
178 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
179 struct extent_buffer *eb)
184 struct btrfs_multi_bio *multi = NULL;
186 if (check_tree_block(root, eb))
188 if (!btrfs_buffer_uptodate(eb))
191 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
192 csum_tree_block(root, eb, 0);
196 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
197 eb->start, &length, &multi, 0);
199 while(dev_nr < multi->num_stripes) {
201 eb->fd = multi->stripes[dev_nr].dev->fd;
202 eb->dev_bytenr = multi->stripes[dev_nr].physical;
203 multi->stripes[dev_nr].dev->total_ios++;
205 ret = write_extent_to_disk(eb);
212 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
213 u32 stripesize, struct btrfs_root *root,
214 struct btrfs_fs_info *fs_info, u64 objectid)
217 root->commit_root = NULL;
218 root->sectorsize = sectorsize;
219 root->nodesize = nodesize;
220 root->leafsize = leafsize;
221 root->stripesize = stripesize;
223 root->track_dirty = 0;
225 root->fs_info = fs_info;
226 root->objectid = objectid;
227 root->last_trans = 0;
228 root->highest_inode = 0;
229 root->last_inode_alloc = 0;
231 INIT_LIST_HEAD(&root->dirty_list);
232 memset(&root->root_key, 0, sizeof(root->root_key));
233 memset(&root->root_item, 0, sizeof(root->root_item));
234 root->root_key.objectid = objectid;
238 static int update_cowonly_root(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root)
243 struct btrfs_root *tree_root = root->fs_info->tree_root;
245 btrfs_write_dirty_block_groups(trans, root);
247 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
248 if (old_root_bytenr == root->node->start)
250 btrfs_set_root_bytenr(&root->root_item,
252 root->root_item.level = btrfs_header_level(root->node);
253 ret = btrfs_update_root(trans, tree_root,
257 btrfs_write_dirty_block_groups(trans, root);
262 static int commit_tree_roots(struct btrfs_trans_handle *trans,
263 struct btrfs_fs_info *fs_info)
265 struct btrfs_root *root;
266 struct list_head *next;
268 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
269 next = fs_info->dirty_cowonly_roots.next;
271 root = list_entry(next, struct btrfs_root, dirty_list);
272 update_cowonly_root(trans, root);
277 static int __commit_transaction(struct btrfs_trans_handle *trans,
278 struct btrfs_root *root)
282 struct extent_buffer *eb;
283 struct extent_io_tree *tree = &root->fs_info->extent_cache;
287 ret = find_first_extent_bit(tree, 0, &start, &end,
291 while(start <= end) {
292 eb = find_first_extent_buffer(tree, start);
293 BUG_ON(!eb || eb->start != start);
294 ret = write_tree_block(trans, root, eb);
297 clear_extent_buffer_dirty(eb);
298 free_extent_buffer(eb);
304 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
305 struct btrfs_root *root)
308 struct btrfs_root *new_root = NULL;
309 struct btrfs_fs_info *fs_info = root->fs_info;
311 if (root->commit_root == root->node)
314 new_root = malloc(sizeof(*new_root));
317 memcpy(new_root, root, sizeof(*new_root));
318 new_root->node = root->commit_root;
319 root->commit_root = NULL;
321 root->root_key.offset = trans->transid;
322 btrfs_set_root_bytenr(&root->root_item, root->node->start);
323 root->root_item.level = btrfs_header_level(root->node);
324 ret = btrfs_insert_root(trans, fs_info->tree_root,
325 &root->root_key, &root->root_item);
328 btrfs_set_root_refs(&new_root->root_item, 0);
329 ret = btrfs_update_root(trans, root->fs_info->tree_root,
330 &new_root->root_key, &new_root->root_item);
333 ret = commit_tree_roots(trans, fs_info);
335 ret = __commit_transaction(trans, root);
337 write_ctree_super(trans, root);
338 btrfs_finish_extent_commit(trans, fs_info->extent_root,
339 &fs_info->pinned_extents);
340 btrfs_free_transaction(root, trans);
341 fs_info->running_transaction = NULL;
343 trans = btrfs_start_transaction(root, 1);
344 ret = btrfs_drop_snapshot(trans, new_root);
346 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
349 ret = commit_tree_roots(trans, fs_info);
351 ret = __commit_transaction(trans, root);
353 write_ctree_super(trans, root);
354 btrfs_finish_extent_commit(trans, fs_info->extent_root,
355 &fs_info->pinned_extents);
356 btrfs_free_transaction(root, trans);
357 free_extent_buffer(root->commit_root);
358 root->commit_root = NULL;
359 fs_info->running_transaction = NULL;
361 free_extent_buffer(new_root->node);
367 static int find_and_setup_root(struct btrfs_root *tree_root,
368 struct btrfs_fs_info *fs_info,
369 u64 objectid, struct btrfs_root *root)
374 __setup_root(tree_root->nodesize, tree_root->leafsize,
375 tree_root->sectorsize, tree_root->stripesize,
376 root, fs_info, objectid);
377 ret = btrfs_find_last_root(tree_root, objectid,
378 &root->root_item, &root->root_key);
381 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
382 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
388 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
391 free_extent_buffer(root->node);
392 if (root->commit_root)
393 free_extent_buffer(root->commit_root);
399 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
400 struct btrfs_key *location)
402 struct btrfs_root *root;
403 struct btrfs_root *tree_root = fs_info->tree_root;
404 struct btrfs_path *path;
405 struct extent_buffer *l;
409 root = malloc(sizeof(*root));
411 return ERR_PTR(-ENOMEM);
412 memset(root, 0, sizeof(*root));
413 if (location->offset == (u64)-1) {
414 ret = find_and_setup_root(tree_root, fs_info,
415 location->objectid, root);
423 __setup_root(tree_root->nodesize, tree_root->leafsize,
424 tree_root->sectorsize, tree_root->stripesize,
425 root, fs_info, location->objectid);
427 path = btrfs_alloc_path();
429 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
436 read_extent_buffer(l, &root->root_item,
437 btrfs_item_ptr_offset(l, path->slots[0]),
438 sizeof(root->root_item));
439 memcpy(&root->root_key, location, sizeof(*location));
442 btrfs_release_path(root, path);
443 btrfs_free_path(path);
448 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
449 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
457 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
460 struct btrfs_root *root;
461 int flags = O_CREAT | O_RDWR;
466 fp = open(filename, flags, 0600);
470 root = open_ctree_fd(fp, filename, sb_bytenr, writes);
476 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
484 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
485 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
486 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
487 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
488 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
489 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
491 struct btrfs_super_block *disk_super;
492 struct btrfs_fs_devices *fs_devices = NULL;
496 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
498 ret = btrfs_scan_one_device(fp, path, &fs_devices,
499 &total_devs, sb_bytenr);
502 fprintf(stderr, "No valid Btrfs found on %s\n", path);
506 if (total_devs != 1) {
507 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
511 memset(fs_info, 0, sizeof(*fs_info));
512 fs_info->fs_root = root;
513 fs_info->tree_root = tree_root;
514 fs_info->extent_root = extent_root;
515 fs_info->chunk_root = chunk_root;
516 fs_info->dev_root = dev_root;
519 fs_info->readonly = 1;
521 extent_io_tree_init(&fs_info->extent_cache);
522 extent_io_tree_init(&fs_info->free_space_cache);
523 extent_io_tree_init(&fs_info->block_group_cache);
524 extent_io_tree_init(&fs_info->pinned_extents);
525 extent_io_tree_init(&fs_info->pending_del);
526 extent_io_tree_init(&fs_info->extent_ins);
528 cache_tree_init(&fs_info->mapping_tree.cache_tree);
530 mutex_init(&fs_info->fs_mutex);
531 fs_info->fs_devices = fs_devices;
532 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
533 INIT_LIST_HEAD(&fs_info->space_info);
535 __setup_root(4096, 4096, 4096, 4096, tree_root,
536 fs_info, BTRFS_ROOT_TREE_OBJECTID);
539 ret = btrfs_open_devices(fs_devices, O_RDWR);
541 ret = btrfs_open_devices(fs_devices, O_RDONLY);
544 ret = btrfs_bootstrap_super_map(&fs_info->mapping_tree, fs_devices);
546 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
548 BUG_ON(!fs_info->sb_buffer);
549 fs_info->sb_buffer->fd = fs_devices->latest_bdev;
550 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
551 ret = read_extent_from_disk(fs_info->sb_buffer);
553 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
555 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
556 sizeof(fs_info->super_copy));
557 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
558 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
561 disk_super = &fs_info->super_copy;
562 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
563 sizeof(disk_super->magic))) {
564 printk("No valid btrfs found\n");
567 nodesize = btrfs_super_nodesize(disk_super);
568 leafsize = btrfs_super_leafsize(disk_super);
569 sectorsize = btrfs_super_sectorsize(disk_super);
570 stripesize = btrfs_super_stripesize(disk_super);
571 tree_root->nodesize = nodesize;
572 tree_root->leafsize = leafsize;
573 tree_root->sectorsize = sectorsize;
574 tree_root->stripesize = stripesize;
576 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
578 ret = btrfs_read_sys_array(tree_root);
580 blocksize = btrfs_level_size(tree_root,
581 btrfs_super_chunk_root_level(disk_super));
583 __setup_root(nodesize, leafsize, sectorsize, stripesize,
584 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
586 chunk_root->node = read_tree_block(chunk_root,
587 btrfs_super_chunk_root(disk_super),
590 BUG_ON(!chunk_root->node);
592 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
593 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
596 ret = btrfs_read_chunk_tree(chunk_root);
599 blocksize = btrfs_level_size(tree_root,
600 btrfs_super_root_level(disk_super));
602 tree_root->node = read_tree_block(tree_root,
603 btrfs_super_root(disk_super),
605 BUG_ON(!tree_root->node);
606 ret = find_and_setup_root(tree_root, fs_info,
607 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
609 extent_root->track_dirty = 1;
611 ret = find_and_setup_root(tree_root, fs_info,
612 BTRFS_DEV_TREE_OBJECTID, dev_root);
614 dev_root->track_dirty = 1;
616 ret = find_and_setup_root(tree_root, fs_info,
617 BTRFS_FS_TREE_OBJECTID, root);
620 fs_info->generation = btrfs_super_generation(disk_super) + 1;
621 btrfs_read_block_groups(root);
623 fs_info->data_alloc_profile = (u64)-1;
624 fs_info->metadata_alloc_profile = (u64)-1;
625 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
630 int write_all_supers(struct btrfs_root *root)
632 struct list_head *cur;
633 struct list_head *head = &root->fs_info->fs_devices->devices;
634 struct btrfs_device *dev;
635 struct extent_buffer *sb;
636 struct btrfs_dev_item *dev_item;
639 sb = root->fs_info->sb_buffer;
640 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
642 list_for_each(cur, head) {
643 dev = list_entry(cur, struct btrfs_device, dev_list);
644 btrfs_set_device_type(sb, dev_item, dev->type);
645 btrfs_set_device_id(sb, dev_item, dev->devid);
646 btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
647 btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
648 btrfs_set_device_io_align(sb, dev_item, dev->io_align);
649 btrfs_set_device_io_width(sb, dev_item, dev->io_width);
650 btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
651 write_extent_buffer(sb, dev->uuid,
652 (unsigned long)btrfs_device_uuid(dev_item),
655 sb->dev_bytenr = sb->start;
656 btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
657 csum_tree_block(root, sb, 0);
658 ret = write_extent_to_disk(sb);
664 int write_ctree_super(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root)
668 struct btrfs_root *tree_root = root->fs_info->tree_root;
669 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
671 if (root->fs_info->readonly)
674 btrfs_set_super_generation(&root->fs_info->super_copy,
676 btrfs_set_super_root(&root->fs_info->super_copy,
677 tree_root->node->start);
678 btrfs_set_super_root_level(&root->fs_info->super_copy,
679 btrfs_header_level(tree_root->node));
680 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
681 chunk_root->node->start);
682 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
683 btrfs_header_level(chunk_root->node));
684 write_extent_buffer(root->fs_info->sb_buffer,
685 &root->fs_info->super_copy, 0,
686 sizeof(root->fs_info->super_copy));
687 ret = write_all_supers(root);
689 fprintf(stderr, "failed to write new super block err %d\n", ret);
693 static int close_all_devices(struct btrfs_fs_info *fs_info)
695 struct list_head *list;
696 struct list_head *next;
697 struct btrfs_device *device;
701 list = &fs_info->fs_devices->devices;
702 list_for_each(next, list) {
703 device = list_entry(next, struct btrfs_device, dev_list);
709 int close_ctree(struct btrfs_root *root)
712 struct btrfs_trans_handle *trans;
713 struct btrfs_fs_info *fs_info = root->fs_info;
715 trans = btrfs_start_transaction(root, 1);
716 btrfs_commit_transaction(trans, root);
717 trans = btrfs_start_transaction(root, 1);
718 ret = commit_tree_roots(trans, root->fs_info);
720 ret = __commit_transaction(trans, root);
722 write_ctree_super(trans, root);
723 btrfs_free_transaction(root, trans);
724 btrfs_free_block_groups(root->fs_info);
726 free_extent_buffer(root->node);
727 if (root->fs_info->extent_root->node)
728 free_extent_buffer(root->fs_info->extent_root->node);
729 if (root->fs_info->tree_root->node)
730 free_extent_buffer(root->fs_info->tree_root->node);
731 free_extent_buffer(root->commit_root);
732 free_extent_buffer(root->fs_info->sb_buffer);
734 if (root->fs_info->chunk_root->node);
735 free_extent_buffer(root->fs_info->chunk_root->node);
737 if (root->fs_info->dev_root->node);
738 free_extent_buffer(root->fs_info->dev_root->node);
740 close_all_devices(root->fs_info);
741 extent_io_tree_cleanup(&fs_info->extent_cache);
742 extent_io_tree_cleanup(&fs_info->free_space_cache);
743 extent_io_tree_cleanup(&fs_info->block_group_cache);
744 extent_io_tree_cleanup(&fs_info->pinned_extents);
745 extent_io_tree_cleanup(&fs_info->pending_del);
746 extent_io_tree_cleanup(&fs_info->extent_ins);
748 free(fs_info->tree_root);
749 free(fs_info->extent_root);
750 free(fs_info->fs_root);
751 free(fs_info->chunk_root);
752 free(fs_info->dev_root);
758 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
759 struct extent_buffer *eb)
761 return clear_extent_buffer_dirty(eb);
764 int wait_on_tree_block_writeback(struct btrfs_root *root,
765 struct extent_buffer *eb)
770 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
772 set_extent_buffer_dirty(eb);
775 int btrfs_buffer_uptodate(struct extent_buffer *eb)
777 return extent_buffer_uptodate(eb);
780 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
782 return set_extent_buffer_uptodate(eb);