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
23 #include <sys/types.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
32 #include "transaction.h"
36 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
41 struct btrfs_device *device;
44 ret = btrfs_map_block(&root->fs_info->mapping_tree, logical, &physical,
48 buf->dev_bytenr = physical;
52 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
54 if (buf->start != btrfs_header_bytenr(buf))
57 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
58 (unsigned long)btrfs_header_fsid(buf),
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,
77 char result[BTRFS_CRC32_SIZE];
81 len = buf->len - BTRFS_CSUM_SIZE;
82 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
83 btrfs_csum_final(crc, result);
86 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
87 printk("checksum verify failed on %llu\n",
88 (unsigned long long)buf->start);
92 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
97 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
98 u64 bytenr, u32 blocksize)
100 return find_extent_buffer(&root->fs_info->extent_cache,
104 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
105 u64 bytenr, u32 blocksize)
107 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
111 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
116 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
120 struct extent_buffer *eb;
122 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
125 if (!btrfs_buffer_uptodate(eb)) {
126 btrfs_map_bh_to_logical(root, eb, eb->start);
127 ret = read_extent_from_disk(eb);
129 free_extent_buffer(eb);
132 btrfs_set_buffer_uptodate(eb);
137 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
138 struct extent_buffer *eb)
140 if (check_tree_block(root, eb))
142 if (!btrfs_buffer_uptodate(eb))
145 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
146 btrfs_map_bh_to_logical(root, eb, eb->start);
147 csum_tree_block(root, eb, 0);
148 return write_extent_to_disk(eb);
151 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
152 u32 stripesize, struct btrfs_root *root,
153 struct btrfs_fs_info *fs_info, u64 objectid)
156 root->commit_root = NULL;
157 root->sectorsize = sectorsize;
158 root->nodesize = nodesize;
159 root->leafsize = leafsize;
160 root->stripesize = stripesize;
162 root->track_dirty = 0;
164 root->fs_info = fs_info;
165 root->objectid = objectid;
166 root->last_trans = 0;
167 root->highest_inode = 0;
168 root->last_inode_alloc = 0;
170 INIT_LIST_HEAD(&root->dirty_list);
171 memset(&root->root_key, 0, sizeof(root->root_key));
172 memset(&root->root_item, 0, sizeof(root->root_item));
173 root->root_key.objectid = objectid;
177 static int update_cowonly_root(struct btrfs_trans_handle *trans,
178 struct btrfs_root *root)
182 struct btrfs_root *tree_root = root->fs_info->tree_root;
184 btrfs_write_dirty_block_groups(trans, root);
186 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
187 if (old_root_bytenr == root->node->start)
189 btrfs_set_root_bytenr(&root->root_item,
191 root->root_item.level = btrfs_header_level(root->node);
192 ret = btrfs_update_root(trans, tree_root,
196 btrfs_write_dirty_block_groups(trans, root);
201 static int commit_tree_roots(struct btrfs_trans_handle *trans,
202 struct btrfs_fs_info *fs_info)
204 struct btrfs_root *root;
205 struct list_head *next;
207 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
208 next = fs_info->dirty_cowonly_roots.next;
210 root = list_entry(next, struct btrfs_root, dirty_list);
211 update_cowonly_root(trans, root);
216 static int __commit_transaction(struct btrfs_trans_handle *trans,
217 struct btrfs_root *root)
221 struct extent_buffer *eb;
222 struct extent_io_tree *tree = &root->fs_info->extent_cache;
226 ret = find_first_extent_bit(tree, 0, &start, &end,
230 while(start <= end) {
231 eb = find_first_extent_buffer(tree, start);
232 BUG_ON(!eb || eb->start != start);
233 ret = write_tree_block(trans, root, eb);
236 clear_extent_buffer_dirty(eb);
237 free_extent_buffer(eb);
243 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root)
247 struct btrfs_root *new_root = NULL;
248 struct btrfs_fs_info *fs_info = root->fs_info;
250 if (root->commit_root == root->node)
253 new_root = malloc(sizeof(*new_root));
256 memcpy(new_root, root, sizeof(*new_root));
257 new_root->node = root->commit_root;
258 root->commit_root = NULL;
260 root->root_key.offset = trans->transid;
261 btrfs_set_root_bytenr(&root->root_item, root->node->start);
262 root->root_item.level = btrfs_header_level(root->node);
263 ret = btrfs_insert_root(trans, fs_info->tree_root,
264 &root->root_key, &root->root_item);
267 btrfs_set_root_refs(&new_root->root_item, 0);
268 ret = btrfs_update_root(trans, root->fs_info->tree_root,
269 &new_root->root_key, &new_root->root_item);
272 ret = commit_tree_roots(trans, fs_info);
274 ret = __commit_transaction(trans, root);
276 write_ctree_super(trans, root);
277 btrfs_finish_extent_commit(trans, fs_info->extent_root,
278 &fs_info->pinned_extents);
279 btrfs_free_transaction(root, trans);
280 fs_info->running_transaction = NULL;
282 trans = btrfs_start_transaction(root, 1);
283 ret = btrfs_drop_snapshot(trans, new_root);
285 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
288 ret = commit_tree_roots(trans, fs_info);
290 ret = __commit_transaction(trans, root);
292 write_ctree_super(trans, root);
293 btrfs_finish_extent_commit(trans, fs_info->extent_root,
294 &fs_info->pinned_extents);
295 btrfs_free_transaction(root, trans);
296 free_extent_buffer(root->commit_root);
297 root->commit_root = NULL;
298 fs_info->running_transaction = NULL;
300 free_extent_buffer(new_root->node);
306 static int find_and_setup_root(struct btrfs_root *tree_root,
307 struct btrfs_fs_info *fs_info,
308 u64 objectid, struct btrfs_root *root)
313 __setup_root(tree_root->nodesize, tree_root->leafsize,
314 tree_root->sectorsize, tree_root->stripesize,
315 root, fs_info, objectid);
316 ret = btrfs_find_last_root(tree_root, objectid,
317 &root->root_item, &root->root_key);
320 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
321 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
327 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
330 free_extent_buffer(root->node);
331 if (root->commit_root)
332 free_extent_buffer(root->commit_root);
338 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
339 struct btrfs_key *location)
341 struct btrfs_root *root;
342 struct btrfs_root *tree_root = fs_info->tree_root;
343 struct btrfs_path *path;
344 struct extent_buffer *l;
348 root = malloc(sizeof(*root));
350 return ERR_PTR(-ENOMEM);
351 memset(root, 0, sizeof(*root));
352 if (location->offset == (u64)-1) {
353 ret = find_and_setup_root(tree_root, fs_info,
354 location->objectid, root);
362 __setup_root(tree_root->nodesize, tree_root->leafsize,
363 tree_root->sectorsize, tree_root->stripesize,
364 root, fs_info, location->objectid);
366 path = btrfs_alloc_path();
368 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
375 read_extent_buffer(l, &root->root_item,
376 btrfs_item_ptr_offset(l, path->slots[0]),
377 sizeof(root->root_item));
378 memcpy(&root->root_key, location, sizeof(*location));
381 btrfs_release_path(root, path);
382 btrfs_free_path(path);
387 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
388 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
396 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
400 fp = open(filename, O_CREAT | O_RDWR, 0600);
404 return open_ctree_fd(fp, filename, sb_bytenr);
407 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
414 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
415 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
416 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
417 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
418 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
419 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
421 struct btrfs_super_block *disk_super;
422 struct btrfs_fs_devices *fs_devices = NULL;
426 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
428 ret = btrfs_scan_one_device(fp, path, &fs_devices,
429 &total_devs, sb_bytenr);
432 fprintf(stderr, "No valid Btrfs found on %s\n", path);
435 fprintf(stderr, "found Btrfs on %s with %lu devices\n", path,
436 (unsigned long)total_devs);
438 if (total_devs != 1) {
439 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
443 fs_info->fp = fs_devices->lowest_bdev;
444 fs_info->running_transaction = NULL;
445 fs_info->fs_root = root;
446 fs_info->tree_root = tree_root;
447 fs_info->extent_root = extent_root;
448 fs_info->extent_ops = NULL;
449 fs_info->priv_data = NULL;
450 fs_info->chunk_root = chunk_root;
451 fs_info->dev_root = dev_root;
452 fs_info->force_system_allocs = 0;
454 extent_io_tree_init(&fs_info->extent_cache);
455 extent_io_tree_init(&fs_info->free_space_cache);
456 extent_io_tree_init(&fs_info->block_group_cache);
457 extent_io_tree_init(&fs_info->pinned_extents);
458 extent_io_tree_init(&fs_info->pending_del);
459 extent_io_tree_init(&fs_info->extent_ins);
461 cache_tree_init(&fs_info->mapping_tree.cache_tree);
463 mutex_init(&fs_info->fs_mutex);
464 fs_info->fs_devices = fs_devices;
465 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
466 INIT_LIST_HEAD(&fs_info->space_info);
468 __setup_root(4096, 4096, 4096, 4096, tree_root,
469 fs_info, BTRFS_ROOT_TREE_OBJECTID);
471 ret = btrfs_open_devices(fs_devices, O_RDWR);
474 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
476 BUG_ON(!fs_info->sb_buffer);
477 fs_info->sb_buffer->fd = fs_devices->lowest_bdev;
478 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
479 ret = read_extent_from_disk(fs_info->sb_buffer);
481 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
483 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
484 sizeof(fs_info->super_copy));
485 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
486 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
489 disk_super = &fs_info->super_copy;
490 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
491 sizeof(disk_super->magic))) {
492 printk("No valid btrfs found\n");
495 nodesize = btrfs_super_nodesize(disk_super);
496 leafsize = btrfs_super_leafsize(disk_super);
497 sectorsize = btrfs_super_sectorsize(disk_super);
498 stripesize = btrfs_super_stripesize(disk_super);
499 tree_root->nodesize = nodesize;
500 tree_root->leafsize = leafsize;
501 tree_root->sectorsize = sectorsize;
502 tree_root->stripesize = stripesize;
504 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
506 ret = btrfs_read_sys_array(tree_root);
508 blocksize = btrfs_level_size(tree_root,
509 btrfs_super_chunk_root_level(disk_super));
511 __setup_root(nodesize, leafsize, sectorsize, stripesize,
512 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
514 chunk_root->node = read_tree_block(chunk_root,
515 btrfs_super_chunk_root(disk_super),
518 BUG_ON(!chunk_root->node);
520 ret = btrfs_read_chunk_tree(chunk_root);
523 blocksize = btrfs_level_size(tree_root,
524 btrfs_super_root_level(disk_super));
526 tree_root->node = read_tree_block(tree_root,
527 btrfs_super_root(disk_super),
529 BUG_ON(!tree_root->node);
530 ret = find_and_setup_root(tree_root, fs_info,
531 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
533 extent_root->track_dirty = 1;
535 ret = find_and_setup_root(tree_root, fs_info,
536 BTRFS_DEV_TREE_OBJECTID, dev_root);
538 dev_root->track_dirty = 1;
540 ret = find_and_setup_root(tree_root, fs_info,
541 BTRFS_FS_TREE_OBJECTID, root);
544 fs_info->generation = btrfs_super_generation(disk_super) + 1;
545 btrfs_read_block_groups(root);
549 int write_ctree_super(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root)
553 struct btrfs_root *tree_root = root->fs_info->tree_root;
554 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
555 btrfs_set_super_generation(&root->fs_info->super_copy,
557 btrfs_set_super_root(&root->fs_info->super_copy,
558 tree_root->node->start);
559 btrfs_set_super_root_level(&root->fs_info->super_copy,
560 btrfs_header_level(tree_root->node));
561 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
562 chunk_root->node->start);
563 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
564 btrfs_header_level(chunk_root->node));
565 write_extent_buffer(root->fs_info->sb_buffer,
566 &root->fs_info->super_copy, 0,
567 sizeof(root->fs_info->super_copy));
568 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
570 fprintf(stderr, "failed to write new super block err %d\n", ret);
574 static int close_all_devices(struct btrfs_fs_info *fs_info)
576 struct list_head *list;
577 struct list_head *next;
578 struct btrfs_device *device;
582 list = &fs_info->fs_devices->devices;
583 list_for_each(next, list) {
584 device = list_entry(next, struct btrfs_device, dev_list);
585 // close(device->fd);
590 int close_ctree(struct btrfs_root *root)
593 struct btrfs_trans_handle *trans;
594 struct btrfs_fs_info *fs_info = root->fs_info;
596 trans = btrfs_start_transaction(root, 1);
597 btrfs_commit_transaction(trans, root);
598 trans = btrfs_start_transaction(root, 1);
599 ret = commit_tree_roots(trans, root->fs_info);
601 ret = __commit_transaction(trans, root);
603 write_ctree_super(trans, root);
604 btrfs_free_transaction(root, trans);
605 btrfs_free_block_groups(root->fs_info);
606 close(root->fs_info->fp);
608 free_extent_buffer(root->node);
609 if (root->fs_info->extent_root->node)
610 free_extent_buffer(root->fs_info->extent_root->node);
611 if (root->fs_info->tree_root->node)
612 free_extent_buffer(root->fs_info->tree_root->node);
613 free_extent_buffer(root->commit_root);
614 free_extent_buffer(root->fs_info->sb_buffer);
616 if (root->fs_info->chunk_root->node);
617 free_extent_buffer(root->fs_info->chunk_root->node);
619 if (root->fs_info->dev_root->node);
620 free_extent_buffer(root->fs_info->dev_root->node);
622 close_all_devices(root->fs_info);
623 extent_io_tree_cleanup(&fs_info->extent_cache);
624 extent_io_tree_cleanup(&fs_info->free_space_cache);
625 extent_io_tree_cleanup(&fs_info->block_group_cache);
626 extent_io_tree_cleanup(&fs_info->pinned_extents);
627 extent_io_tree_cleanup(&fs_info->pending_del);
628 extent_io_tree_cleanup(&fs_info->extent_ins);
630 free(fs_info->tree_root);
631 free(fs_info->extent_root);
632 free(fs_info->fs_root);
633 free(fs_info->chunk_root);
634 free(fs_info->dev_root);
640 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
641 struct extent_buffer *eb)
643 return clear_extent_buffer_dirty(eb);
646 int wait_on_tree_block_writeback(struct btrfs_root *root,
647 struct extent_buffer *eb)
652 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
654 set_extent_buffer_dirty(eb);
657 int btrfs_buffer_uptodate(struct extent_buffer *eb)
659 return extent_buffer_uptodate(eb);
662 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
664 return set_extent_buffer_uptodate(eb);