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"
35 int btrfs_open_device(struct btrfs_device *dev)
37 dev->fd = open(dev->name, O_RDWR, 0600);
42 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
47 struct btrfs_device *device;
50 ret = btrfs_map_block(&root->fs_info->mapping_tree, logical, &physical,
54 buf->dev_bytenr = physical;
58 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
60 if (buf->start != btrfs_header_bytenr(buf))
63 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
64 (unsigned long)btrfs_header_fsid(buf),
70 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
72 return crc32c(seed, data, len);
75 void btrfs_csum_final(u32 crc, char *result)
77 *(__le32 *)result = ~cpu_to_le32(crc);
80 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
83 char result[BTRFS_CRC32_SIZE];
87 len = buf->len - BTRFS_CSUM_SIZE;
88 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
89 btrfs_csum_final(crc, result);
92 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
93 printk("checksum verify failed on %llu\n", buf->start);
97 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
102 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
103 u64 bytenr, u32 blocksize)
105 return find_extent_buffer(&root->fs_info->extent_cache,
109 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
110 u64 bytenr, u32 blocksize)
112 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
116 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
121 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
125 struct extent_buffer *eb;
127 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
130 if (!btrfs_buffer_uptodate(eb)) {
131 btrfs_map_bh_to_logical(root, eb, eb->start);
132 ret = read_extent_from_disk(eb);
134 free_extent_buffer(eb);
137 btrfs_set_buffer_uptodate(eb);
142 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
143 struct extent_buffer *eb)
145 if (check_tree_block(root, eb))
147 if (!btrfs_buffer_uptodate(eb))
149 btrfs_map_bh_to_logical(root, eb, eb->start);
150 csum_tree_block(root, eb, 0);
151 return write_extent_to_disk(eb);
154 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
155 u32 stripesize, struct btrfs_root *root,
156 struct btrfs_fs_info *fs_info, u64 objectid)
159 root->commit_root = NULL;
160 root->sectorsize = sectorsize;
161 root->nodesize = nodesize;
162 root->leafsize = leafsize;
163 root->stripesize = stripesize;
165 root->track_dirty = 0;
167 root->fs_info = fs_info;
168 root->objectid = objectid;
169 root->last_trans = 0;
170 root->highest_inode = 0;
171 root->last_inode_alloc = 0;
173 INIT_LIST_HEAD(&root->dirty_list);
174 memset(&root->root_key, 0, sizeof(root->root_key));
175 memset(&root->root_item, 0, sizeof(root->root_item));
176 root->root_key.objectid = objectid;
180 static int update_cowonly_root(struct btrfs_trans_handle *trans,
181 struct btrfs_root *root)
185 struct btrfs_root *tree_root = root->fs_info->tree_root;
187 btrfs_write_dirty_block_groups(trans, root);
189 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
190 if (old_root_bytenr == root->node->start)
192 btrfs_set_root_bytenr(&root->root_item,
194 root->root_item.level = btrfs_header_level(root->node);
195 ret = btrfs_update_root(trans, tree_root,
199 btrfs_write_dirty_block_groups(trans, root);
204 static int commit_tree_roots(struct btrfs_trans_handle *trans,
205 struct btrfs_fs_info *fs_info)
207 struct btrfs_root *root;
208 struct list_head *next;
210 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
211 next = fs_info->dirty_cowonly_roots.next;
213 root = list_entry(next, struct btrfs_root, dirty_list);
214 update_cowonly_root(trans, root);
219 static int __commit_transaction(struct btrfs_trans_handle *trans,
220 struct btrfs_root *root)
224 struct extent_buffer *eb;
225 struct extent_io_tree *tree = &root->fs_info->extent_cache;
229 ret = find_first_extent_bit(tree, 0, &start, &end,
233 while(start <= end) {
234 eb = find_first_extent_buffer(tree, start);
235 BUG_ON(!eb || eb->start != start);
236 ret = write_tree_block(trans, root, eb);
239 clear_extent_buffer_dirty(eb);
240 free_extent_buffer(eb);
246 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root)
250 struct btrfs_root *new_root = NULL;
251 struct btrfs_fs_info *fs_info = root->fs_info;
253 if (root->commit_root == root->node)
256 new_root = malloc(sizeof(*new_root));
259 memcpy(new_root, root, sizeof(*new_root));
260 new_root->node = root->commit_root;
261 root->commit_root = NULL;
263 root->root_key.offset = trans->transid;
264 btrfs_set_root_bytenr(&root->root_item, root->node->start);
265 root->root_item.level = btrfs_header_level(root->node);
266 ret = btrfs_insert_root(trans, fs_info->tree_root,
267 &root->root_key, &root->root_item);
270 btrfs_set_root_refs(&new_root->root_item, 0);
271 ret = btrfs_update_root(trans, root->fs_info->tree_root,
272 &new_root->root_key, &new_root->root_item);
275 ret = commit_tree_roots(trans, fs_info);
277 ret = __commit_transaction(trans, root);
279 write_ctree_super(trans, root);
280 btrfs_finish_extent_commit(trans, fs_info->extent_root,
281 &fs_info->pinned_extents);
282 btrfs_free_transaction(root, trans);
283 fs_info->running_transaction = NULL;
285 trans = btrfs_start_transaction(root, 1);
286 ret = btrfs_drop_snapshot(trans, new_root);
288 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
291 ret = commit_tree_roots(trans, fs_info);
293 ret = __commit_transaction(trans, root);
295 write_ctree_super(trans, root);
296 btrfs_finish_extent_commit(trans, fs_info->extent_root,
297 &fs_info->pinned_extents);
298 btrfs_free_transaction(root, trans);
299 free_extent_buffer(root->commit_root);
300 root->commit_root = NULL;
301 fs_info->running_transaction = NULL;
303 free_extent_buffer(new_root->node);
309 static int find_and_setup_root(struct btrfs_root *tree_root,
310 struct btrfs_fs_info *fs_info,
311 u64 objectid, struct btrfs_root *root)
316 __setup_root(tree_root->nodesize, tree_root->leafsize,
317 tree_root->sectorsize, tree_root->stripesize,
318 root, fs_info, objectid);
319 ret = btrfs_find_last_root(tree_root, objectid,
320 &root->root_item, &root->root_key);
323 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
324 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
330 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
333 free_extent_buffer(root->node);
334 if (root->commit_root)
335 free_extent_buffer(root->commit_root);
341 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
342 struct btrfs_key *location)
344 struct btrfs_root *root;
345 struct btrfs_root *tree_root = fs_info->tree_root;
346 struct btrfs_path *path;
347 struct extent_buffer *l;
351 root = malloc(sizeof(*root));
353 return ERR_PTR(-ENOMEM);
354 memset(root, 0, sizeof(*root));
355 if (location->offset == (u64)-1) {
356 ret = find_and_setup_root(tree_root, fs_info,
357 location->objectid, root);
365 __setup_root(tree_root->nodesize, tree_root->leafsize,
366 tree_root->sectorsize, tree_root->stripesize,
367 root, fs_info, location->objectid);
369 path = btrfs_alloc_path();
371 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
378 read_extent_buffer(l, &root->root_item,
379 btrfs_item_ptr_offset(l, path->slots[0]),
380 sizeof(root->root_item));
381 memcpy(&root->root_key, location, sizeof(*location));
384 btrfs_release_path(root, path);
385 btrfs_free_path(path);
390 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
391 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
399 struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr)
403 fp = open(filename, O_CREAT | O_RDWR, 0600);
407 return open_ctree_fd(fp, sb_bytenr);
410 struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr)
417 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
418 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
419 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
420 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
421 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
422 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
424 struct btrfs_super_block *disk_super;
427 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
430 fs_info->running_transaction = NULL;
431 fs_info->fs_root = root;
432 fs_info->tree_root = tree_root;
433 fs_info->extent_root = extent_root;
434 fs_info->extent_ops = NULL;
435 fs_info->priv_data = NULL;
436 fs_info->chunk_root = chunk_root;
437 fs_info->dev_root = dev_root;
438 fs_info->force_system_allocs = 0;
440 extent_io_tree_init(&fs_info->extent_cache);
441 extent_io_tree_init(&fs_info->free_space_cache);
442 extent_io_tree_init(&fs_info->block_group_cache);
443 extent_io_tree_init(&fs_info->pinned_extents);
444 extent_io_tree_init(&fs_info->pending_del);
445 extent_io_tree_init(&fs_info->extent_ins);
447 cache_tree_init(&fs_info->mapping_tree.cache_tree);
449 mutex_init(&fs_info->fs_mutex);
450 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
451 INIT_LIST_HEAD(&fs_info->devices);
452 INIT_LIST_HEAD(&fs_info->space_info);
454 __setup_root(4096, 4096, 4096, 4096, tree_root,
455 fs_info, BTRFS_ROOT_TREE_OBJECTID);
457 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
459 BUG_ON(!fs_info->sb_buffer);
460 fs_info->sb_buffer->fd = fp;
461 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
462 ret = read_extent_from_disk(fs_info->sb_buffer);
464 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
466 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
467 sizeof(fs_info->super_copy));
468 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
469 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
472 disk_super = &fs_info->super_copy;
473 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
474 sizeof(disk_super->magic))) {
475 printk("No valid btrfs found\n");
478 nodesize = btrfs_super_nodesize(disk_super);
479 leafsize = btrfs_super_leafsize(disk_super);
480 sectorsize = btrfs_super_sectorsize(disk_super);
481 stripesize = btrfs_super_stripesize(disk_super);
482 tree_root->nodesize = nodesize;
483 tree_root->leafsize = leafsize;
484 tree_root->sectorsize = sectorsize;
485 tree_root->stripesize = stripesize;
487 ret = btrfs_read_sys_array(tree_root);
489 blocksize = btrfs_level_size(tree_root,
490 btrfs_super_chunk_root_level(disk_super));
492 __setup_root(nodesize, leafsize, sectorsize, stripesize,
493 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
494 chunk_root->node = read_tree_block(chunk_root,
495 btrfs_super_chunk_root(disk_super),
498 BUG_ON(!chunk_root->node);
499 ret = btrfs_read_chunk_tree(chunk_root);
502 blocksize = btrfs_level_size(tree_root,
503 btrfs_super_root_level(disk_super));
505 tree_root->node = read_tree_block(tree_root,
506 btrfs_super_root(disk_super),
508 BUG_ON(!tree_root->node);
509 ret = find_and_setup_root(tree_root, fs_info,
510 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
512 extent_root->track_dirty = 1;
514 ret = find_and_setup_root(tree_root, fs_info,
515 BTRFS_DEV_TREE_OBJECTID, dev_root);
517 dev_root->track_dirty = 1;
519 ret = find_and_setup_root(tree_root, fs_info,
520 BTRFS_FS_TREE_OBJECTID, root);
523 fs_info->generation = btrfs_super_generation(disk_super) + 1;
524 btrfs_read_block_groups(root);
528 int write_ctree_super(struct btrfs_trans_handle *trans,
529 struct btrfs_root *root)
532 struct btrfs_root *tree_root = root->fs_info->tree_root;
533 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
534 btrfs_set_super_generation(&root->fs_info->super_copy,
536 btrfs_set_super_root(&root->fs_info->super_copy,
537 tree_root->node->start);
538 btrfs_set_super_root_level(&root->fs_info->super_copy,
539 btrfs_header_level(tree_root->node));
540 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
541 chunk_root->node->start);
542 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
543 btrfs_header_level(chunk_root->node));
544 write_extent_buffer(root->fs_info->sb_buffer,
545 &root->fs_info->super_copy, 0,
546 sizeof(root->fs_info->super_copy));
547 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
549 fprintf(stderr, "failed to write new super block err %d\n", ret);
553 static int close_all_devices(struct btrfs_fs_info *fs_info)
555 struct list_head *list;
556 struct list_head *next;
557 struct btrfs_device *device;
559 list = &fs_info->devices;
560 while(!list_empty(list)) {
563 device = list_entry(next, struct btrfs_device, dev_list);
571 int close_ctree(struct btrfs_root *root)
574 struct btrfs_trans_handle *trans;
575 struct btrfs_fs_info *fs_info = root->fs_info;
577 trans = btrfs_start_transaction(root, 1);
578 btrfs_commit_transaction(trans, root);
579 trans = btrfs_start_transaction(root, 1);
580 ret = commit_tree_roots(trans, root->fs_info);
582 ret = __commit_transaction(trans, root);
584 write_ctree_super(trans, root);
585 btrfs_free_transaction(root, trans);
586 btrfs_free_block_groups(root->fs_info);
587 close(root->fs_info->fp);
589 free_extent_buffer(root->node);
590 if (root->fs_info->extent_root->node)
591 free_extent_buffer(root->fs_info->extent_root->node);
592 if (root->fs_info->tree_root->node)
593 free_extent_buffer(root->fs_info->tree_root->node);
594 free_extent_buffer(root->commit_root);
595 free_extent_buffer(root->fs_info->sb_buffer);
597 if (root->fs_info->chunk_root->node);
598 free_extent_buffer(root->fs_info->chunk_root->node);
600 if (root->fs_info->dev_root->node);
601 free_extent_buffer(root->fs_info->dev_root->node);
603 close_all_devices(root->fs_info);
604 extent_io_tree_cleanup(&fs_info->extent_cache);
605 extent_io_tree_cleanup(&fs_info->free_space_cache);
606 extent_io_tree_cleanup(&fs_info->block_group_cache);
607 extent_io_tree_cleanup(&fs_info->pinned_extents);
608 extent_io_tree_cleanup(&fs_info->pending_del);
609 extent_io_tree_cleanup(&fs_info->extent_ins);
611 free(fs_info->tree_root);
612 free(fs_info->extent_root);
613 free(fs_info->fs_root);
614 free(fs_info->chunk_root);
615 free(fs_info->dev_root);
621 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
622 struct extent_buffer *eb)
624 return clear_extent_buffer_dirty(eb);
627 int wait_on_tree_block_writeback(struct btrfs_root *root,
628 struct extent_buffer *eb)
633 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
635 set_extent_buffer_dirty(eb);
638 int btrfs_buffer_uptodate(struct extent_buffer *eb)
640 return extent_buffer_uptodate(eb);
643 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
645 return set_extent_buffer_uptodate(eb);