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 static 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\n",
74 (unsigned long long)buf->start);
78 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
83 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
84 u64 bytenr, u32 blocksize)
86 return find_extent_buffer(&root->fs_info->extent_cache,
90 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
91 u64 bytenr, u32 blocksize)
93 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
97 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
102 struct extent_buffer *eb;
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);
114 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
115 bytenr, &physical, &length, &device,
119 blocksize = min(blocksize, (u32)(64 * 1024));
120 readahead(device->fd, physical, blocksize);
124 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
130 struct extent_buffer *eb;
133 struct btrfs_device *device;
135 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
139 if (btrfs_buffer_uptodate(eb))
143 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
144 eb->start, &physical, &length, &device,
149 eb->dev_bytenr = physical;
150 ret = read_extent_from_disk(eb);
152 free_extent_buffer(eb);
155 btrfs_set_buffer_uptodate(eb);
159 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
160 struct extent_buffer *eb)
167 struct btrfs_device *device;
169 if (check_tree_block(root, eb))
171 if (!btrfs_buffer_uptodate(eb))
174 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
175 csum_tree_block(root, eb, 0);
178 while(dev_nr < total_devs) {
179 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
180 dev_nr, eb->start, &physical, &length,
181 &device, &total_devs);
184 eb->dev_bytenr = physical;
187 ret = write_extent_to_disk(eb);
193 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
194 u32 stripesize, struct btrfs_root *root,
195 struct btrfs_fs_info *fs_info, u64 objectid)
198 root->commit_root = NULL;
199 root->sectorsize = sectorsize;
200 root->nodesize = nodesize;
201 root->leafsize = leafsize;
202 root->stripesize = stripesize;
204 root->track_dirty = 0;
206 root->fs_info = fs_info;
207 root->objectid = objectid;
208 root->last_trans = 0;
209 root->highest_inode = 0;
210 root->last_inode_alloc = 0;
212 INIT_LIST_HEAD(&root->dirty_list);
213 memset(&root->root_key, 0, sizeof(root->root_key));
214 memset(&root->root_item, 0, sizeof(root->root_item));
215 root->root_key.objectid = objectid;
219 static int update_cowonly_root(struct btrfs_trans_handle *trans,
220 struct btrfs_root *root)
224 struct btrfs_root *tree_root = root->fs_info->tree_root;
226 btrfs_write_dirty_block_groups(trans, root);
228 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
229 if (old_root_bytenr == root->node->start)
231 btrfs_set_root_bytenr(&root->root_item,
233 root->root_item.level = btrfs_header_level(root->node);
234 ret = btrfs_update_root(trans, tree_root,
238 btrfs_write_dirty_block_groups(trans, root);
243 static int commit_tree_roots(struct btrfs_trans_handle *trans,
244 struct btrfs_fs_info *fs_info)
246 struct btrfs_root *root;
247 struct list_head *next;
249 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
250 next = fs_info->dirty_cowonly_roots.next;
252 root = list_entry(next, struct btrfs_root, dirty_list);
253 update_cowonly_root(trans, root);
258 static int __commit_transaction(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root)
263 struct extent_buffer *eb;
264 struct extent_io_tree *tree = &root->fs_info->extent_cache;
268 ret = find_first_extent_bit(tree, 0, &start, &end,
272 while(start <= end) {
273 eb = find_first_extent_buffer(tree, start);
274 BUG_ON(!eb || eb->start != start);
275 ret = write_tree_block(trans, root, eb);
278 clear_extent_buffer_dirty(eb);
279 free_extent_buffer(eb);
285 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
286 struct btrfs_root *root)
289 struct btrfs_root *new_root = NULL;
290 struct btrfs_fs_info *fs_info = root->fs_info;
292 if (root->commit_root == root->node)
295 new_root = malloc(sizeof(*new_root));
298 memcpy(new_root, root, sizeof(*new_root));
299 new_root->node = root->commit_root;
300 root->commit_root = NULL;
302 root->root_key.offset = trans->transid;
303 btrfs_set_root_bytenr(&root->root_item, root->node->start);
304 root->root_item.level = btrfs_header_level(root->node);
305 ret = btrfs_insert_root(trans, fs_info->tree_root,
306 &root->root_key, &root->root_item);
309 btrfs_set_root_refs(&new_root->root_item, 0);
310 ret = btrfs_update_root(trans, root->fs_info->tree_root,
311 &new_root->root_key, &new_root->root_item);
314 ret = commit_tree_roots(trans, fs_info);
316 ret = __commit_transaction(trans, root);
318 write_ctree_super(trans, root);
319 btrfs_finish_extent_commit(trans, fs_info->extent_root,
320 &fs_info->pinned_extents);
321 btrfs_free_transaction(root, trans);
322 fs_info->running_transaction = NULL;
324 trans = btrfs_start_transaction(root, 1);
325 ret = btrfs_drop_snapshot(trans, new_root);
327 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
330 ret = commit_tree_roots(trans, fs_info);
332 ret = __commit_transaction(trans, root);
334 write_ctree_super(trans, root);
335 btrfs_finish_extent_commit(trans, fs_info->extent_root,
336 &fs_info->pinned_extents);
337 btrfs_free_transaction(root, trans);
338 free_extent_buffer(root->commit_root);
339 root->commit_root = NULL;
340 fs_info->running_transaction = NULL;
342 free_extent_buffer(new_root->node);
348 static int find_and_setup_root(struct btrfs_root *tree_root,
349 struct btrfs_fs_info *fs_info,
350 u64 objectid, struct btrfs_root *root)
355 __setup_root(tree_root->nodesize, tree_root->leafsize,
356 tree_root->sectorsize, tree_root->stripesize,
357 root, fs_info, objectid);
358 ret = btrfs_find_last_root(tree_root, objectid,
359 &root->root_item, &root->root_key);
362 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
363 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
369 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
372 free_extent_buffer(root->node);
373 if (root->commit_root)
374 free_extent_buffer(root->commit_root);
380 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
381 struct btrfs_key *location)
383 struct btrfs_root *root;
384 struct btrfs_root *tree_root = fs_info->tree_root;
385 struct btrfs_path *path;
386 struct extent_buffer *l;
390 root = malloc(sizeof(*root));
392 return ERR_PTR(-ENOMEM);
393 memset(root, 0, sizeof(*root));
394 if (location->offset == (u64)-1) {
395 ret = find_and_setup_root(tree_root, fs_info,
396 location->objectid, root);
404 __setup_root(tree_root->nodesize, tree_root->leafsize,
405 tree_root->sectorsize, tree_root->stripesize,
406 root, fs_info, location->objectid);
408 path = btrfs_alloc_path();
410 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
417 read_extent_buffer(l, &root->root_item,
418 btrfs_item_ptr_offset(l, path->slots[0]),
419 sizeof(root->root_item));
420 memcpy(&root->root_key, location, sizeof(*location));
423 btrfs_release_path(root, path);
424 btrfs_free_path(path);
429 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
430 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
438 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
442 fp = open(filename, O_CREAT | O_RDWR, 0600);
446 return open_ctree_fd(fp, filename, sb_bytenr);
449 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
456 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
457 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
458 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
459 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
460 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
461 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
463 struct btrfs_super_block *disk_super;
464 struct btrfs_fs_devices *fs_devices = NULL;
468 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
470 ret = btrfs_scan_one_device(fp, path, &fs_devices,
471 &total_devs, sb_bytenr);
474 fprintf(stderr, "No valid Btrfs found on %s\n", path);
477 fprintf(stderr, "found Btrfs on %s with %lu devices\n", path,
478 (unsigned long)total_devs);
480 if (total_devs != 1) {
481 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
485 memset(fs_info, 0, sizeof(*fs_info));
486 fs_info->fp = fs_devices->lowest_bdev;
487 fs_info->fs_root = root;
488 fs_info->tree_root = tree_root;
489 fs_info->extent_root = extent_root;
490 fs_info->chunk_root = chunk_root;
491 fs_info->dev_root = dev_root;
493 extent_io_tree_init(&fs_info->extent_cache);
494 extent_io_tree_init(&fs_info->free_space_cache);
495 extent_io_tree_init(&fs_info->block_group_cache);
496 extent_io_tree_init(&fs_info->pinned_extents);
497 extent_io_tree_init(&fs_info->pending_del);
498 extent_io_tree_init(&fs_info->extent_ins);
500 cache_tree_init(&fs_info->mapping_tree.cache_tree);
502 mutex_init(&fs_info->fs_mutex);
503 fs_info->fs_devices = fs_devices;
504 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
505 INIT_LIST_HEAD(&fs_info->space_info);
507 __setup_root(4096, 4096, 4096, 4096, tree_root,
508 fs_info, BTRFS_ROOT_TREE_OBJECTID);
510 ret = btrfs_open_devices(fs_devices, O_RDWR);
513 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
515 BUG_ON(!fs_info->sb_buffer);
516 fs_info->sb_buffer->fd = fs_devices->lowest_bdev;
517 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
518 ret = read_extent_from_disk(fs_info->sb_buffer);
520 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
522 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
523 sizeof(fs_info->super_copy));
524 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
525 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
528 disk_super = &fs_info->super_copy;
529 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
530 sizeof(disk_super->magic))) {
531 printk("No valid btrfs found\n");
534 nodesize = btrfs_super_nodesize(disk_super);
535 leafsize = btrfs_super_leafsize(disk_super);
536 sectorsize = btrfs_super_sectorsize(disk_super);
537 stripesize = btrfs_super_stripesize(disk_super);
538 tree_root->nodesize = nodesize;
539 tree_root->leafsize = leafsize;
540 tree_root->sectorsize = sectorsize;
541 tree_root->stripesize = stripesize;
543 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
545 ret = btrfs_read_sys_array(tree_root);
547 blocksize = btrfs_level_size(tree_root,
548 btrfs_super_chunk_root_level(disk_super));
550 __setup_root(nodesize, leafsize, sectorsize, stripesize,
551 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
553 chunk_root->node = read_tree_block(chunk_root,
554 btrfs_super_chunk_root(disk_super),
557 BUG_ON(!chunk_root->node);
559 ret = btrfs_read_chunk_tree(chunk_root);
562 blocksize = btrfs_level_size(tree_root,
563 btrfs_super_root_level(disk_super));
565 tree_root->node = read_tree_block(tree_root,
566 btrfs_super_root(disk_super),
568 BUG_ON(!tree_root->node);
569 ret = find_and_setup_root(tree_root, fs_info,
570 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
572 extent_root->track_dirty = 1;
574 ret = find_and_setup_root(tree_root, fs_info,
575 BTRFS_DEV_TREE_OBJECTID, dev_root);
577 dev_root->track_dirty = 1;
579 ret = find_and_setup_root(tree_root, fs_info,
580 BTRFS_FS_TREE_OBJECTID, root);
583 fs_info->generation = btrfs_super_generation(disk_super) + 1;
584 btrfs_read_block_groups(root);
586 fs_info->data_alloc_profile = (u64)-1;
587 fs_info->metadata_alloc_profile = (u64)-1;
588 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
593 int write_ctree_super(struct btrfs_trans_handle *trans,
594 struct btrfs_root *root)
597 struct btrfs_root *tree_root = root->fs_info->tree_root;
598 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
599 btrfs_set_super_generation(&root->fs_info->super_copy,
601 btrfs_set_super_root(&root->fs_info->super_copy,
602 tree_root->node->start);
603 btrfs_set_super_root_level(&root->fs_info->super_copy,
604 btrfs_header_level(tree_root->node));
605 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
606 chunk_root->node->start);
607 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
608 btrfs_header_level(chunk_root->node));
609 write_extent_buffer(root->fs_info->sb_buffer,
610 &root->fs_info->super_copy, 0,
611 sizeof(root->fs_info->super_copy));
612 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
614 fprintf(stderr, "failed to write new super block err %d\n", ret);
618 static int close_all_devices(struct btrfs_fs_info *fs_info)
620 struct list_head *list;
621 struct list_head *next;
622 struct btrfs_device *device;
626 list = &fs_info->fs_devices->devices;
627 list_for_each(next, list) {
628 device = list_entry(next, struct btrfs_device, dev_list);
629 // close(device->fd);
634 int close_ctree(struct btrfs_root *root)
637 struct btrfs_trans_handle *trans;
638 struct btrfs_fs_info *fs_info = root->fs_info;
640 trans = btrfs_start_transaction(root, 1);
641 btrfs_commit_transaction(trans, root);
642 trans = btrfs_start_transaction(root, 1);
643 ret = commit_tree_roots(trans, root->fs_info);
645 ret = __commit_transaction(trans, root);
647 write_ctree_super(trans, root);
648 btrfs_free_transaction(root, trans);
649 btrfs_free_block_groups(root->fs_info);
650 close(root->fs_info->fp);
652 free_extent_buffer(root->node);
653 if (root->fs_info->extent_root->node)
654 free_extent_buffer(root->fs_info->extent_root->node);
655 if (root->fs_info->tree_root->node)
656 free_extent_buffer(root->fs_info->tree_root->node);
657 free_extent_buffer(root->commit_root);
658 free_extent_buffer(root->fs_info->sb_buffer);
660 if (root->fs_info->chunk_root->node);
661 free_extent_buffer(root->fs_info->chunk_root->node);
663 if (root->fs_info->dev_root->node);
664 free_extent_buffer(root->fs_info->dev_root->node);
666 close_all_devices(root->fs_info);
667 extent_io_tree_cleanup(&fs_info->extent_cache);
668 extent_io_tree_cleanup(&fs_info->free_space_cache);
669 extent_io_tree_cleanup(&fs_info->block_group_cache);
670 extent_io_tree_cleanup(&fs_info->pinned_extents);
671 extent_io_tree_cleanup(&fs_info->pending_del);
672 extent_io_tree_cleanup(&fs_info->extent_ins);
674 free(fs_info->tree_root);
675 free(fs_info->extent_root);
676 free(fs_info->fs_root);
677 free(fs_info->chunk_root);
678 free(fs_info->dev_root);
684 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
685 struct extent_buffer *eb)
687 return clear_extent_buffer_dirty(eb);
690 int wait_on_tree_block_writeback(struct btrfs_root *root,
691 struct extent_buffer *eb)
696 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
698 set_extent_buffer_dirty(eb);
701 int btrfs_buffer_uptodate(struct extent_buffer *eb)
703 return extent_buffer_uptodate(eb);
706 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
708 return set_extent_buffer_uptodate(eb);