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"
37 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
39 if (buf->start != btrfs_header_bytenr(buf))
42 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
43 (unsigned long)btrfs_header_fsid(buf),
49 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
51 return crc32c(seed, data, len);
54 void btrfs_csum_final(u32 crc, char *result)
56 *(__le32 *)result = ~cpu_to_le32(crc);
59 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
62 char result[BTRFS_CRC32_SIZE];
66 len = buf->len - BTRFS_CSUM_SIZE;
67 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
68 btrfs_csum_final(crc, result);
71 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
72 printk("checksum verify failed on %llu\n",
73 (unsigned long long)buf->start);
77 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
82 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
83 u64 bytenr, u32 blocksize)
85 return find_extent_buffer(&root->fs_info->extent_cache,
89 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
90 u64 bytenr, u32 blocksize)
92 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
96 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
101 struct extent_buffer *eb;
104 struct btrfs_device *device;
106 eb = btrfs_find_tree_block(root, bytenr, blocksize);
107 if (eb && btrfs_buffer_uptodate(eb)) {
108 free_extent_buffer(eb);
113 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
114 bytenr, &physical, &length, &device,
118 blocksize = min(blocksize, (u32)(64 * 1024));
119 readahead(device->fd, physical, blocksize);
123 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
129 struct extent_buffer *eb;
132 struct btrfs_device *device;
134 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
138 if (btrfs_buffer_uptodate(eb))
142 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
143 eb->start, &physical, &length, &device,
148 eb->dev_bytenr = physical;
149 ret = read_extent_from_disk(eb);
151 free_extent_buffer(eb);
154 btrfs_set_buffer_uptodate(eb);
158 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
159 struct extent_buffer *eb)
166 struct btrfs_device *device;
168 if (check_tree_block(root, eb))
170 if (!btrfs_buffer_uptodate(eb))
173 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
174 csum_tree_block(root, eb, 0);
177 while(dev_nr < total_devs) {
178 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
179 dev_nr, eb->start, &physical, &length,
180 &device, &total_devs);
183 eb->dev_bytenr = physical;
186 ret = write_extent_to_disk(eb);
192 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
193 u32 stripesize, struct btrfs_root *root,
194 struct btrfs_fs_info *fs_info, u64 objectid)
197 root->commit_root = NULL;
198 root->sectorsize = sectorsize;
199 root->nodesize = nodesize;
200 root->leafsize = leafsize;
201 root->stripesize = stripesize;
203 root->track_dirty = 0;
205 root->fs_info = fs_info;
206 root->objectid = objectid;
207 root->last_trans = 0;
208 root->highest_inode = 0;
209 root->last_inode_alloc = 0;
211 INIT_LIST_HEAD(&root->dirty_list);
212 memset(&root->root_key, 0, sizeof(root->root_key));
213 memset(&root->root_item, 0, sizeof(root->root_item));
214 root->root_key.objectid = objectid;
218 static int update_cowonly_root(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root)
223 struct btrfs_root *tree_root = root->fs_info->tree_root;
225 btrfs_write_dirty_block_groups(trans, root);
227 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
228 if (old_root_bytenr == root->node->start)
230 btrfs_set_root_bytenr(&root->root_item,
232 root->root_item.level = btrfs_header_level(root->node);
233 ret = btrfs_update_root(trans, tree_root,
237 btrfs_write_dirty_block_groups(trans, root);
242 static int commit_tree_roots(struct btrfs_trans_handle *trans,
243 struct btrfs_fs_info *fs_info)
245 struct btrfs_root *root;
246 struct list_head *next;
248 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
249 next = fs_info->dirty_cowonly_roots.next;
251 root = list_entry(next, struct btrfs_root, dirty_list);
252 update_cowonly_root(trans, root);
257 static int __commit_transaction(struct btrfs_trans_handle *trans,
258 struct btrfs_root *root)
262 struct extent_buffer *eb;
263 struct extent_io_tree *tree = &root->fs_info->extent_cache;
267 ret = find_first_extent_bit(tree, 0, &start, &end,
271 while(start <= end) {
272 eb = find_first_extent_buffer(tree, start);
273 BUG_ON(!eb || eb->start != start);
274 ret = write_tree_block(trans, root, eb);
277 clear_extent_buffer_dirty(eb);
278 free_extent_buffer(eb);
284 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root)
288 struct btrfs_root *new_root = NULL;
289 struct btrfs_fs_info *fs_info = root->fs_info;
291 if (root->commit_root == root->node)
294 new_root = malloc(sizeof(*new_root));
297 memcpy(new_root, root, sizeof(*new_root));
298 new_root->node = root->commit_root;
299 root->commit_root = NULL;
301 root->root_key.offset = trans->transid;
302 btrfs_set_root_bytenr(&root->root_item, root->node->start);
303 root->root_item.level = btrfs_header_level(root->node);
304 ret = btrfs_insert_root(trans, fs_info->tree_root,
305 &root->root_key, &root->root_item);
308 btrfs_set_root_refs(&new_root->root_item, 0);
309 ret = btrfs_update_root(trans, root->fs_info->tree_root,
310 &new_root->root_key, &new_root->root_item);
313 ret = commit_tree_roots(trans, fs_info);
315 ret = __commit_transaction(trans, root);
317 write_ctree_super(trans, root);
318 btrfs_finish_extent_commit(trans, fs_info->extent_root,
319 &fs_info->pinned_extents);
320 btrfs_free_transaction(root, trans);
321 fs_info->running_transaction = NULL;
323 trans = btrfs_start_transaction(root, 1);
324 ret = btrfs_drop_snapshot(trans, new_root);
326 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
329 ret = commit_tree_roots(trans, fs_info);
331 ret = __commit_transaction(trans, root);
333 write_ctree_super(trans, root);
334 btrfs_finish_extent_commit(trans, fs_info->extent_root,
335 &fs_info->pinned_extents);
336 btrfs_free_transaction(root, trans);
337 free_extent_buffer(root->commit_root);
338 root->commit_root = NULL;
339 fs_info->running_transaction = NULL;
341 free_extent_buffer(new_root->node);
347 static int find_and_setup_root(struct btrfs_root *tree_root,
348 struct btrfs_fs_info *fs_info,
349 u64 objectid, struct btrfs_root *root)
354 __setup_root(tree_root->nodesize, tree_root->leafsize,
355 tree_root->sectorsize, tree_root->stripesize,
356 root, fs_info, objectid);
357 ret = btrfs_find_last_root(tree_root, objectid,
358 &root->root_item, &root->root_key);
361 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
362 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
368 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
371 free_extent_buffer(root->node);
372 if (root->commit_root)
373 free_extent_buffer(root->commit_root);
379 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
380 struct btrfs_key *location)
382 struct btrfs_root *root;
383 struct btrfs_root *tree_root = fs_info->tree_root;
384 struct btrfs_path *path;
385 struct extent_buffer *l;
389 root = malloc(sizeof(*root));
391 return ERR_PTR(-ENOMEM);
392 memset(root, 0, sizeof(*root));
393 if (location->offset == (u64)-1) {
394 ret = find_and_setup_root(tree_root, fs_info,
395 location->objectid, root);
403 __setup_root(tree_root->nodesize, tree_root->leafsize,
404 tree_root->sectorsize, tree_root->stripesize,
405 root, fs_info, location->objectid);
407 path = btrfs_alloc_path();
409 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
416 read_extent_buffer(l, &root->root_item,
417 btrfs_item_ptr_offset(l, path->slots[0]),
418 sizeof(root->root_item));
419 memcpy(&root->root_key, location, sizeof(*location));
422 btrfs_release_path(root, path);
423 btrfs_free_path(path);
428 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
429 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
437 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
441 fp = open(filename, O_CREAT | O_RDWR, 0600);
445 return open_ctree_fd(fp, filename, sb_bytenr);
448 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
455 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
456 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
457 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
458 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
459 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
460 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
462 struct btrfs_super_block *disk_super;
463 struct btrfs_fs_devices *fs_devices = NULL;
467 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
469 ret = btrfs_scan_one_device(fp, path, &fs_devices,
470 &total_devs, sb_bytenr);
473 fprintf(stderr, "No valid Btrfs found on %s\n", path);
476 fprintf(stderr, "found Btrfs on %s with %lu devices\n", path,
477 (unsigned long)total_devs);
479 if (total_devs != 1) {
480 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
484 fs_info->fp = fs_devices->lowest_bdev;
485 fs_info->running_transaction = NULL;
486 fs_info->fs_root = root;
487 fs_info->tree_root = tree_root;
488 fs_info->extent_root = extent_root;
489 fs_info->extent_ops = NULL;
490 fs_info->priv_data = NULL;
491 fs_info->chunk_root = chunk_root;
492 fs_info->dev_root = dev_root;
493 fs_info->force_system_allocs = 0;
495 extent_io_tree_init(&fs_info->extent_cache);
496 extent_io_tree_init(&fs_info->free_space_cache);
497 extent_io_tree_init(&fs_info->block_group_cache);
498 extent_io_tree_init(&fs_info->pinned_extents);
499 extent_io_tree_init(&fs_info->pending_del);
500 extent_io_tree_init(&fs_info->extent_ins);
502 cache_tree_init(&fs_info->mapping_tree.cache_tree);
504 mutex_init(&fs_info->fs_mutex);
505 fs_info->fs_devices = fs_devices;
506 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
507 INIT_LIST_HEAD(&fs_info->space_info);
509 __setup_root(4096, 4096, 4096, 4096, tree_root,
510 fs_info, BTRFS_ROOT_TREE_OBJECTID);
512 ret = btrfs_open_devices(fs_devices, O_RDWR);
515 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
517 BUG_ON(!fs_info->sb_buffer);
518 fs_info->sb_buffer->fd = fs_devices->lowest_bdev;
519 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
520 ret = read_extent_from_disk(fs_info->sb_buffer);
522 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
524 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
525 sizeof(fs_info->super_copy));
526 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
527 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
530 disk_super = &fs_info->super_copy;
531 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
532 sizeof(disk_super->magic))) {
533 printk("No valid btrfs found\n");
536 nodesize = btrfs_super_nodesize(disk_super);
537 leafsize = btrfs_super_leafsize(disk_super);
538 sectorsize = btrfs_super_sectorsize(disk_super);
539 stripesize = btrfs_super_stripesize(disk_super);
540 tree_root->nodesize = nodesize;
541 tree_root->leafsize = leafsize;
542 tree_root->sectorsize = sectorsize;
543 tree_root->stripesize = stripesize;
545 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
547 ret = btrfs_read_sys_array(tree_root);
549 blocksize = btrfs_level_size(tree_root,
550 btrfs_super_chunk_root_level(disk_super));
552 __setup_root(nodesize, leafsize, sectorsize, stripesize,
553 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
555 chunk_root->node = read_tree_block(chunk_root,
556 btrfs_super_chunk_root(disk_super),
559 BUG_ON(!chunk_root->node);
561 ret = btrfs_read_chunk_tree(chunk_root);
564 blocksize = btrfs_level_size(tree_root,
565 btrfs_super_root_level(disk_super));
567 tree_root->node = read_tree_block(tree_root,
568 btrfs_super_root(disk_super),
570 BUG_ON(!tree_root->node);
571 ret = find_and_setup_root(tree_root, fs_info,
572 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
574 extent_root->track_dirty = 1;
576 ret = find_and_setup_root(tree_root, fs_info,
577 BTRFS_DEV_TREE_OBJECTID, dev_root);
579 dev_root->track_dirty = 1;
581 ret = find_and_setup_root(tree_root, fs_info,
582 BTRFS_FS_TREE_OBJECTID, root);
585 fs_info->generation = btrfs_super_generation(disk_super) + 1;
586 btrfs_read_block_groups(root);
590 int write_ctree_super(struct btrfs_trans_handle *trans,
591 struct btrfs_root *root)
594 struct btrfs_root *tree_root = root->fs_info->tree_root;
595 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
596 btrfs_set_super_generation(&root->fs_info->super_copy,
598 btrfs_set_super_root(&root->fs_info->super_copy,
599 tree_root->node->start);
600 btrfs_set_super_root_level(&root->fs_info->super_copy,
601 btrfs_header_level(tree_root->node));
602 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
603 chunk_root->node->start);
604 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
605 btrfs_header_level(chunk_root->node));
606 write_extent_buffer(root->fs_info->sb_buffer,
607 &root->fs_info->super_copy, 0,
608 sizeof(root->fs_info->super_copy));
609 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
611 fprintf(stderr, "failed to write new super block err %d\n", ret);
615 static int close_all_devices(struct btrfs_fs_info *fs_info)
617 struct list_head *list;
618 struct list_head *next;
619 struct btrfs_device *device;
623 list = &fs_info->fs_devices->devices;
624 list_for_each(next, list) {
625 device = list_entry(next, struct btrfs_device, dev_list);
626 // close(device->fd);
631 int close_ctree(struct btrfs_root *root)
634 struct btrfs_trans_handle *trans;
635 struct btrfs_fs_info *fs_info = root->fs_info;
637 trans = btrfs_start_transaction(root, 1);
638 btrfs_commit_transaction(trans, root);
639 trans = btrfs_start_transaction(root, 1);
640 ret = commit_tree_roots(trans, root->fs_info);
642 ret = __commit_transaction(trans, root);
644 write_ctree_super(trans, root);
645 btrfs_free_transaction(root, trans);
646 btrfs_free_block_groups(root->fs_info);
647 close(root->fs_info->fp);
649 free_extent_buffer(root->node);
650 if (root->fs_info->extent_root->node)
651 free_extent_buffer(root->fs_info->extent_root->node);
652 if (root->fs_info->tree_root->node)
653 free_extent_buffer(root->fs_info->tree_root->node);
654 free_extent_buffer(root->commit_root);
655 free_extent_buffer(root->fs_info->sb_buffer);
657 if (root->fs_info->chunk_root->node);
658 free_extent_buffer(root->fs_info->chunk_root->node);
660 if (root->fs_info->dev_root->node);
661 free_extent_buffer(root->fs_info->dev_root->node);
663 close_all_devices(root->fs_info);
664 extent_io_tree_cleanup(&fs_info->extent_cache);
665 extent_io_tree_cleanup(&fs_info->free_space_cache);
666 extent_io_tree_cleanup(&fs_info->block_group_cache);
667 extent_io_tree_cleanup(&fs_info->pinned_extents);
668 extent_io_tree_cleanup(&fs_info->pending_del);
669 extent_io_tree_cleanup(&fs_info->extent_ins);
671 free(fs_info->tree_root);
672 free(fs_info->extent_root);
673 free(fs_info->fs_root);
674 free(fs_info->chunk_root);
675 free(fs_info->dev_root);
681 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
682 struct extent_buffer *eb)
684 return clear_extent_buffer_dirty(eb);
687 int wait_on_tree_block_writeback(struct btrfs_root *root,
688 struct extent_buffer *eb)
693 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
695 set_extent_buffer_dirty(eb);
698 int btrfs_buffer_uptodate(struct extent_buffer *eb)
700 return extent_buffer_uptodate(eb);
703 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
705 return set_extent_buffer_uptodate(eb);