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
)
101 struct extent_buffer
*eb
;
103 struct btrfs_multi_bio
*multi
= NULL
;
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
);
114 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
115 bytenr
, &length
, &multi
);
117 device
= multi
->stripes
[0].dev
;
119 blocksize
= min(blocksize
, (u32
)(64 * 1024));
120 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
125 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
130 struct extent_buffer
*eb
;
132 struct btrfs_multi_bio
*multi
= NULL
;
133 struct btrfs_device
*device
;
135 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
139 if (btrfs_buffer_uptodate(eb
))
144 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
145 eb
->start
, &length
, &multi
);
147 device
= multi
->stripes
[0].dev
;
150 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
151 ret
= read_extent_from_disk(eb
);
153 free_extent_buffer(eb
);
156 btrfs_set_buffer_uptodate(eb
);
161 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
162 struct extent_buffer
*eb
)
167 struct btrfs_multi_bio
*multi
= NULL
;
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);
179 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, WRITE
,
180 eb
->start
, &length
, &multi
);
181 while(dev_nr
< multi
->num_stripes
) {
183 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
184 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
185 multi
->stripes
[dev_nr
].dev
->total_ios
++;
187 ret
= write_extent_to_disk(eb
);
194 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
195 u32 stripesize
, struct btrfs_root
*root
,
196 struct btrfs_fs_info
*fs_info
, u64 objectid
)
199 root
->commit_root
= NULL
;
200 root
->sectorsize
= sectorsize
;
201 root
->nodesize
= nodesize
;
202 root
->leafsize
= leafsize
;
203 root
->stripesize
= stripesize
;
205 root
->track_dirty
= 0;
207 root
->fs_info
= fs_info
;
208 root
->objectid
= objectid
;
209 root
->last_trans
= 0;
210 root
->highest_inode
= 0;
211 root
->last_inode_alloc
= 0;
213 INIT_LIST_HEAD(&root
->dirty_list
);
214 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
215 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
216 root
->root_key
.objectid
= objectid
;
220 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
221 struct btrfs_root
*root
)
225 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
227 btrfs_write_dirty_block_groups(trans
, root
);
229 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
230 if (old_root_bytenr
== root
->node
->start
)
232 btrfs_set_root_bytenr(&root
->root_item
,
234 root
->root_item
.level
= btrfs_header_level(root
->node
);
235 ret
= btrfs_update_root(trans
, tree_root
,
239 btrfs_write_dirty_block_groups(trans
, root
);
244 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
245 struct btrfs_fs_info
*fs_info
)
247 struct btrfs_root
*root
;
248 struct list_head
*next
;
250 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
251 next
= fs_info
->dirty_cowonly_roots
.next
;
253 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
254 update_cowonly_root(trans
, root
);
259 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
260 struct btrfs_root
*root
)
264 struct extent_buffer
*eb
;
265 struct extent_io_tree
*tree
= &root
->fs_info
->extent_cache
;
269 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
273 while(start
<= end
) {
274 eb
= find_first_extent_buffer(tree
, start
);
275 BUG_ON(!eb
|| eb
->start
!= start
);
276 ret
= write_tree_block(trans
, root
, eb
);
279 clear_extent_buffer_dirty(eb
);
280 free_extent_buffer(eb
);
286 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
287 struct btrfs_root
*root
)
290 struct btrfs_root
*new_root
= NULL
;
291 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
293 if (root
->commit_root
== root
->node
)
296 new_root
= malloc(sizeof(*new_root
));
299 memcpy(new_root
, root
, sizeof(*new_root
));
300 new_root
->node
= root
->commit_root
;
301 root
->commit_root
= NULL
;
303 root
->root_key
.offset
= trans
->transid
;
304 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
305 root
->root_item
.level
= btrfs_header_level(root
->node
);
306 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
,
307 &root
->root_key
, &root
->root_item
);
310 btrfs_set_root_refs(&new_root
->root_item
, 0);
311 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
312 &new_root
->root_key
, &new_root
->root_item
);
315 ret
= commit_tree_roots(trans
, fs_info
);
317 ret
= __commit_transaction(trans
, root
);
319 write_ctree_super(trans
, root
);
320 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
321 &fs_info
->pinned_extents
);
322 btrfs_free_transaction(root
, trans
);
323 fs_info
->running_transaction
= NULL
;
325 trans
= btrfs_start_transaction(root
, 1);
326 ret
= btrfs_drop_snapshot(trans
, new_root
);
328 ret
= btrfs_del_root(trans
, fs_info
->tree_root
, &new_root
->root_key
);
331 ret
= commit_tree_roots(trans
, fs_info
);
333 ret
= __commit_transaction(trans
, root
);
335 write_ctree_super(trans
, root
);
336 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
337 &fs_info
->pinned_extents
);
338 btrfs_free_transaction(root
, trans
);
339 free_extent_buffer(root
->commit_root
);
340 root
->commit_root
= NULL
;
341 fs_info
->running_transaction
= NULL
;
343 free_extent_buffer(new_root
->node
);
349 static int find_and_setup_root(struct btrfs_root
*tree_root
,
350 struct btrfs_fs_info
*fs_info
,
351 u64 objectid
, struct btrfs_root
*root
)
356 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
357 tree_root
->sectorsize
, tree_root
->stripesize
,
358 root
, fs_info
, objectid
);
359 ret
= btrfs_find_last_root(tree_root
, objectid
,
360 &root
->root_item
, &root
->root_key
);
363 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
364 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
370 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
373 free_extent_buffer(root
->node
);
374 if (root
->commit_root
)
375 free_extent_buffer(root
->commit_root
);
381 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
382 struct btrfs_key
*location
)
384 struct btrfs_root
*root
;
385 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
386 struct btrfs_path
*path
;
387 struct extent_buffer
*l
;
391 root
= malloc(sizeof(*root
));
393 return ERR_PTR(-ENOMEM
);
394 memset(root
, 0, sizeof(*root
));
395 if (location
->offset
== (u64
)-1) {
396 ret
= find_and_setup_root(tree_root
, fs_info
,
397 location
->objectid
, root
);
405 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
406 tree_root
->sectorsize
, tree_root
->stripesize
,
407 root
, fs_info
, location
->objectid
);
409 path
= btrfs_alloc_path();
411 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
418 read_extent_buffer(l
, &root
->root_item
,
419 btrfs_item_ptr_offset(l
, path
->slots
[0]),
420 sizeof(root
->root_item
));
421 memcpy(&root
->root_key
, location
, sizeof(*location
));
424 btrfs_release_path(root
, path
);
425 btrfs_free_path(path
);
430 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
431 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
439 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
)
443 fp
= open(filename
, O_CREAT
| O_RDWR
, 0600);
447 return open_ctree_fd(fp
, filename
, sb_bytenr
);
450 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
)
457 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
458 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
459 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
460 struct btrfs_root
*chunk_root
= malloc(sizeof(struct btrfs_root
));
461 struct btrfs_root
*dev_root
= malloc(sizeof(struct btrfs_root
));
462 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
464 struct btrfs_super_block
*disk_super
;
465 struct btrfs_fs_devices
*fs_devices
= NULL
;
469 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
471 ret
= btrfs_scan_one_device(fp
, path
, &fs_devices
,
472 &total_devs
, sb_bytenr
);
475 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
478 fprintf(stderr
, "found Btrfs on %s with %lu devices\n", path
,
479 (unsigned long)total_devs
);
481 if (total_devs
!= 1) {
482 ret
= btrfs_scan_for_fsid(fs_devices
, total_devs
, 1);
486 memset(fs_info
, 0, sizeof(*fs_info
));
487 fs_info
->fp
= fs_devices
->lowest_bdev
;
488 fs_info
->fs_root
= root
;
489 fs_info
->tree_root
= tree_root
;
490 fs_info
->extent_root
= extent_root
;
491 fs_info
->chunk_root
= chunk_root
;
492 fs_info
->dev_root
= dev_root
;
494 extent_io_tree_init(&fs_info
->extent_cache
);
495 extent_io_tree_init(&fs_info
->free_space_cache
);
496 extent_io_tree_init(&fs_info
->block_group_cache
);
497 extent_io_tree_init(&fs_info
->pinned_extents
);
498 extent_io_tree_init(&fs_info
->pending_del
);
499 extent_io_tree_init(&fs_info
->extent_ins
);
501 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
503 mutex_init(&fs_info
->fs_mutex
);
504 fs_info
->fs_devices
= fs_devices
;
505 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
506 INIT_LIST_HEAD(&fs_info
->space_info
);
508 __setup_root(4096, 4096, 4096, 4096, tree_root
,
509 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
511 ret
= btrfs_open_devices(fs_devices
, O_RDWR
);
514 fs_info
->sb_buffer
= btrfs_find_create_tree_block(tree_root
, sb_bytenr
,
516 BUG_ON(!fs_info
->sb_buffer
);
517 fs_info
->sb_buffer
->fd
= fs_devices
->lowest_bdev
;
518 fs_info
->sb_buffer
->dev_bytenr
= sb_bytenr
;
519 ret
= read_extent_from_disk(fs_info
->sb_buffer
);
521 btrfs_set_buffer_uptodate(fs_info
->sb_buffer
);
523 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
524 sizeof(fs_info
->super_copy
));
525 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
526 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
529 disk_super
= &fs_info
->super_copy
;
530 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
531 sizeof(disk_super
->magic
))) {
532 printk("No valid btrfs found\n");
535 nodesize
= btrfs_super_nodesize(disk_super
);
536 leafsize
= btrfs_super_leafsize(disk_super
);
537 sectorsize
= btrfs_super_sectorsize(disk_super
);
538 stripesize
= btrfs_super_stripesize(disk_super
);
539 tree_root
->nodesize
= nodesize
;
540 tree_root
->leafsize
= leafsize
;
541 tree_root
->sectorsize
= sectorsize
;
542 tree_root
->stripesize
= stripesize
;
544 ret
= btrfs_read_super_device(tree_root
, fs_info
->sb_buffer
);
546 ret
= btrfs_read_sys_array(tree_root
);
548 blocksize
= btrfs_level_size(tree_root
,
549 btrfs_super_chunk_root_level(disk_super
));
551 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
552 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
554 chunk_root
->node
= read_tree_block(chunk_root
,
555 btrfs_super_chunk_root(disk_super
),
558 BUG_ON(!chunk_root
->node
);
560 ret
= btrfs_read_chunk_tree(chunk_root
);
563 blocksize
= btrfs_level_size(tree_root
,
564 btrfs_super_root_level(disk_super
));
566 tree_root
->node
= read_tree_block(tree_root
,
567 btrfs_super_root(disk_super
),
569 BUG_ON(!tree_root
->node
);
570 ret
= find_and_setup_root(tree_root
, fs_info
,
571 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
573 extent_root
->track_dirty
= 1;
575 ret
= find_and_setup_root(tree_root
, fs_info
,
576 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
578 dev_root
->track_dirty
= 1;
580 ret
= find_and_setup_root(tree_root
, fs_info
,
581 BTRFS_FS_TREE_OBJECTID
, root
);
584 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
585 btrfs_read_block_groups(root
);
587 fs_info
->data_alloc_profile
= (u64
)-1;
588 fs_info
->metadata_alloc_profile
= (u64
)-1;
589 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
594 int write_ctree_super(struct btrfs_trans_handle
*trans
,
595 struct btrfs_root
*root
)
598 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
599 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
600 btrfs_set_super_generation(&root
->fs_info
->super_copy
,
602 btrfs_set_super_root(&root
->fs_info
->super_copy
,
603 tree_root
->node
->start
);
604 btrfs_set_super_root_level(&root
->fs_info
->super_copy
,
605 btrfs_header_level(tree_root
->node
));
606 btrfs_set_super_chunk_root(&root
->fs_info
->super_copy
,
607 chunk_root
->node
->start
);
608 btrfs_set_super_chunk_root_level(&root
->fs_info
->super_copy
,
609 btrfs_header_level(chunk_root
->node
));
610 write_extent_buffer(root
->fs_info
->sb_buffer
,
611 &root
->fs_info
->super_copy
, 0,
612 sizeof(root
->fs_info
->super_copy
));
613 ret
= write_tree_block(trans
, root
, root
->fs_info
->sb_buffer
);
615 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
619 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
621 struct list_head
*list
;
622 struct list_head
*next
;
623 struct btrfs_device
*device
;
627 list
= &fs_info
->fs_devices
->devices
;
628 list_for_each(next
, list
) {
629 device
= list_entry(next
, struct btrfs_device
, dev_list
);
630 // close(device->fd);
635 int close_ctree(struct btrfs_root
*root
)
638 struct btrfs_trans_handle
*trans
;
639 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
641 trans
= btrfs_start_transaction(root
, 1);
642 btrfs_commit_transaction(trans
, root
);
643 trans
= btrfs_start_transaction(root
, 1);
644 ret
= commit_tree_roots(trans
, root
->fs_info
);
646 ret
= __commit_transaction(trans
, root
);
648 write_ctree_super(trans
, root
);
649 btrfs_free_transaction(root
, trans
);
650 btrfs_free_block_groups(root
->fs_info
);
651 close(root
->fs_info
->fp
);
653 free_extent_buffer(root
->node
);
654 if (root
->fs_info
->extent_root
->node
)
655 free_extent_buffer(root
->fs_info
->extent_root
->node
);
656 if (root
->fs_info
->tree_root
->node
)
657 free_extent_buffer(root
->fs_info
->tree_root
->node
);
658 free_extent_buffer(root
->commit_root
);
659 free_extent_buffer(root
->fs_info
->sb_buffer
);
661 if (root
->fs_info
->chunk_root
->node
);
662 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
664 if (root
->fs_info
->dev_root
->node
);
665 free_extent_buffer(root
->fs_info
->dev_root
->node
);
667 close_all_devices(root
->fs_info
);
668 extent_io_tree_cleanup(&fs_info
->extent_cache
);
669 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
670 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
671 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
672 extent_io_tree_cleanup(&fs_info
->pending_del
);
673 extent_io_tree_cleanup(&fs_info
->extent_ins
);
675 free(fs_info
->tree_root
);
676 free(fs_info
->extent_root
);
677 free(fs_info
->fs_root
);
678 free(fs_info
->chunk_root
);
679 free(fs_info
->dev_root
);
685 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
686 struct extent_buffer
*eb
)
688 return clear_extent_buffer_dirty(eb
);
691 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
692 struct extent_buffer
*eb
)
697 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
699 set_extent_buffer_dirty(eb
);
702 int btrfs_buffer_uptodate(struct extent_buffer
*eb
)
704 return extent_buffer_uptodate(eb
);
707 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
709 return set_extent_buffer_uptodate(eb
);