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
)
41 struct btrfs_fs_devices
*fs_devices
;
44 if (buf
->start
!= btrfs_header_bytenr(buf
))
47 fs_devices
= root
->fs_info
->fs_devices
;
49 if (!memcmp_extent_buffer(buf
, fs_devices
->fsid
,
50 (unsigned long)btrfs_header_fsid(buf
),
55 fs_devices
= fs_devices
->seed
;
60 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
62 return crc32c(seed
, data
, len
);
65 void btrfs_csum_final(u32 crc
, char *result
)
67 *(__le32
*)result
= ~cpu_to_le32(crc
);
70 int csum_tree_block_size(struct extent_buffer
*buf
, u16 csum_size
,
77 result
= malloc(csum_size
* sizeof(char));
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, csum_size
)) {
87 printk("checksum verify failed on %llu wanted %X "
88 "found %X\n", (unsigned long long)buf
->start
,
89 *((int *)result
), *((char *)buf
->data
));
94 write_extent_buffer(buf
, result
, 0, csum_size
);
100 int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
104 btrfs_super_csum_size(&root
->fs_info
->super_copy
);
105 return csum_tree_block_size(buf
, csum_size
, verify
);
108 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
109 u64 bytenr
, u32 blocksize
)
111 return find_extent_buffer(&root
->fs_info
->extent_cache
,
115 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
116 u64 bytenr
, u32 blocksize
)
118 return alloc_extent_buffer(&root
->fs_info
->extent_cache
, bytenr
,
122 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
,
126 struct extent_buffer
*eb
;
128 struct btrfs_multi_bio
*multi
= NULL
;
129 struct btrfs_device
*device
;
131 eb
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
132 if (eb
&& btrfs_buffer_uptodate(eb
, parent_transid
)) {
133 free_extent_buffer(eb
);
138 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
139 bytenr
, &length
, &multi
, 0);
141 device
= multi
->stripes
[0].dev
;
143 blocksize
= min(blocksize
, (u32
)(64 * 1024));
144 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
149 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
150 struct extent_buffer
*eb
, u64 parent_transid
)
154 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
157 if (extent_buffer_uptodate(eb
) &&
158 btrfs_header_generation(eb
) == parent_transid
) {
162 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
163 (unsigned long long)eb
->start
,
164 (unsigned long long)parent_transid
,
165 (unsigned long long)btrfs_header_generation(eb
));
168 clear_extent_buffer_uptodate(io_tree
, eb
);
174 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
175 u32 blocksize
, u64 parent_transid
)
178 struct extent_buffer
*eb
;
180 struct btrfs_multi_bio
*multi
= NULL
;
181 struct btrfs_device
*device
;
185 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
189 if (btrfs_buffer_uptodate(eb
, parent_transid
))
194 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
195 eb
->start
, &length
, &multi
, mirror_num
);
197 device
= multi
->stripes
[0].dev
;
200 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
202 ret
= read_extent_from_disk(eb
);
204 if (ret
== 0 && check_tree_block(root
, eb
) == 0 &&
205 csum_tree_block(root
, eb
, 1) == 0 &&
206 verify_parent_transid(eb
->tree
, eb
, parent_transid
) == 0) {
207 btrfs_set_buffer_uptodate(eb
);
210 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
212 if (num_copies
== 1) {
216 if (mirror_num
> num_copies
) {
220 free_extent_buffer(eb
);
224 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
225 struct extent_buffer
*eb
)
230 struct btrfs_multi_bio
*multi
= NULL
;
232 if (check_tree_block(root
, eb
))
234 if (!btrfs_buffer_uptodate(eb
, trans
->transid
))
237 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
238 csum_tree_block(root
, eb
, 0);
242 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, WRITE
,
243 eb
->start
, &length
, &multi
, 0);
245 while(dev_nr
< multi
->num_stripes
) {
247 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
248 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
249 multi
->stripes
[dev_nr
].dev
->total_ios
++;
251 ret
= write_extent_to_disk(eb
);
258 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
259 u32 stripesize
, struct btrfs_root
*root
,
260 struct btrfs_fs_info
*fs_info
, u64 objectid
)
263 root
->commit_root
= NULL
;
264 root
->sectorsize
= sectorsize
;
265 root
->nodesize
= nodesize
;
266 root
->leafsize
= leafsize
;
267 root
->stripesize
= stripesize
;
269 root
->track_dirty
= 0;
271 root
->fs_info
= fs_info
;
272 root
->objectid
= objectid
;
273 root
->last_trans
= 0;
274 root
->highest_inode
= 0;
275 root
->last_inode_alloc
= 0;
277 INIT_LIST_HEAD(&root
->dirty_list
);
278 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
279 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
280 root
->root_key
.objectid
= objectid
;
284 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
285 struct btrfs_root
*root
)
289 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
291 btrfs_write_dirty_block_groups(trans
, root
);
293 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
294 if (old_root_bytenr
== root
->node
->start
)
296 btrfs_set_root_bytenr(&root
->root_item
,
298 btrfs_set_root_generation(&root
->root_item
,
300 root
->root_item
.level
= btrfs_header_level(root
->node
);
301 ret
= btrfs_update_root(trans
, tree_root
,
305 btrfs_write_dirty_block_groups(trans
, root
);
310 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
311 struct btrfs_fs_info
*fs_info
)
313 struct btrfs_root
*root
;
314 struct list_head
*next
;
315 struct extent_buffer
*eb
;
317 if (fs_info
->readonly
)
320 eb
= fs_info
->tree_root
->node
;
321 extent_buffer_get(eb
);
322 btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
323 free_extent_buffer(eb
);
325 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
326 next
= fs_info
->dirty_cowonly_roots
.next
;
328 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
329 update_cowonly_root(trans
, root
);
334 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
335 struct btrfs_root
*root
)
339 struct extent_buffer
*eb
;
340 struct extent_io_tree
*tree
= &root
->fs_info
->extent_cache
;
344 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
348 while(start
<= end
) {
349 eb
= find_first_extent_buffer(tree
, start
);
350 BUG_ON(!eb
|| eb
->start
!= start
);
351 ret
= write_tree_block(trans
, root
, eb
);
354 clear_extent_buffer_dirty(eb
);
355 free_extent_buffer(eb
);
361 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
362 struct btrfs_root
*root
)
365 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
367 if (root
->commit_root
== root
->node
)
370 free_extent_buffer(root
->commit_root
);
371 root
->commit_root
= NULL
;
373 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
374 btrfs_set_root_generation(&root
->root_item
, trans
->transid
);
375 root
->root_item
.level
= btrfs_header_level(root
->node
);
376 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
377 &root
->root_key
, &root
->root_item
);
380 ret
= commit_tree_roots(trans
, fs_info
);
382 ret
= __commit_transaction(trans
, root
);
384 write_ctree_super(trans
, root
);
385 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
386 &fs_info
->pinned_extents
);
387 btrfs_free_transaction(root
, trans
);
388 free_extent_buffer(root
->commit_root
);
389 root
->commit_root
= NULL
;
390 fs_info
->running_transaction
= NULL
;
394 static int find_and_setup_root(struct btrfs_root
*tree_root
,
395 struct btrfs_fs_info
*fs_info
,
396 u64 objectid
, struct btrfs_root
*root
)
402 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
403 tree_root
->sectorsize
, tree_root
->stripesize
,
404 root
, fs_info
, objectid
);
405 ret
= btrfs_find_last_root(tree_root
, objectid
,
406 &root
->root_item
, &root
->root_key
);
409 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
410 generation
= btrfs_root_generation(&root
->root_item
);
411 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
412 blocksize
, generation
);
417 static int find_and_setup_log_root(struct btrfs_root
*tree_root
,
418 struct btrfs_fs_info
*fs_info
,
419 struct btrfs_super_block
*disk_super
)
422 u64 blocknr
= btrfs_super_log_root(disk_super
);
423 struct btrfs_root
*log_root
= malloc(sizeof(struct btrfs_root
));
428 blocksize
= btrfs_level_size(tree_root
,
429 btrfs_super_log_root_level(disk_super
));
431 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
432 tree_root
->sectorsize
, tree_root
->stripesize
,
433 log_root
, fs_info
, BTRFS_TREE_LOG_OBJECTID
);
435 log_root
->node
= read_tree_block(tree_root
, blocknr
,
437 btrfs_super_generation(disk_super
) + 1);
439 fs_info
->log_root_tree
= log_root
;
440 BUG_ON(!log_root
->node
);
445 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
,
446 struct btrfs_root
*root
)
449 free_extent_buffer(root
->node
);
450 if (root
->commit_root
)
451 free_extent_buffer(root
->commit_root
);
456 static int free_fs_roots(struct btrfs_fs_info
*fs_info
)
458 struct cache_extent
*cache
;
459 struct btrfs_root
*root
;
462 cache
= find_first_cache_extent(&fs_info
->fs_root_cache
, 0);
465 root
= container_of(cache
, struct btrfs_root
, cache
);
466 remove_cache_extent(&fs_info
->fs_root_cache
, cache
);
467 btrfs_free_fs_root(fs_info
, root
);
472 struct btrfs_root
*btrfs_read_fs_root_no_cache(struct btrfs_fs_info
*fs_info
,
473 struct btrfs_key
*location
)
475 struct btrfs_root
*root
;
476 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
477 struct btrfs_path
*path
;
478 struct extent_buffer
*l
;
483 root
= malloc(sizeof(*root
));
485 return ERR_PTR(-ENOMEM
);
486 memset(root
, 0, sizeof(*root
));
487 if (location
->offset
== (u64
)-1) {
488 ret
= find_and_setup_root(tree_root
, fs_info
,
489 location
->objectid
, root
);
497 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
498 tree_root
->sectorsize
, tree_root
->stripesize
,
499 root
, fs_info
, location
->objectid
);
501 path
= btrfs_alloc_path();
503 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
510 read_extent_buffer(l
, &root
->root_item
,
511 btrfs_item_ptr_offset(l
, path
->slots
[0]),
512 sizeof(root
->root_item
));
513 memcpy(&root
->root_key
, location
, sizeof(*location
));
516 btrfs_release_path(root
, path
);
517 btrfs_free_path(path
);
522 generation
= btrfs_root_generation(&root
->root_item
);
523 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
524 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
525 blocksize
, generation
);
532 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
533 struct btrfs_key
*location
)
535 struct btrfs_root
*root
;
536 struct cache_extent
*cache
;
539 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
540 return fs_info
->tree_root
;
541 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
542 return fs_info
->extent_root
;
543 if (location
->objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
544 return fs_info
->chunk_root
;
545 if (location
->objectid
== BTRFS_DEV_TREE_OBJECTID
)
546 return fs_info
->dev_root
;
547 if (location
->objectid
== BTRFS_CSUM_TREE_OBJECTID
)
548 return fs_info
->csum_root
;
550 BUG_ON(location
->objectid
== BTRFS_TREE_RELOC_OBJECTID
||
551 location
->offset
!= (u64
)-1);
553 cache
= find_cache_extent(&fs_info
->fs_root_cache
,
554 location
->objectid
, 1);
556 return container_of(cache
, struct btrfs_root
, cache
);
558 root
= btrfs_read_fs_root_no_cache(fs_info
, location
);
562 root
->cache
.start
= location
->objectid
;
563 root
->cache
.size
= 1;
564 ret
= insert_existing_cache_extent(&fs_info
->fs_root_cache
,
570 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
, int writes
)
573 struct btrfs_root
*root
;
574 int flags
= O_CREAT
| O_RDWR
;
579 fp
= open(filename
, flags
, 0600);
581 fprintf (stderr
, "Could not open %s\n", filename
);
584 root
= open_ctree_fd(fp
, filename
, sb_bytenr
, writes
);
590 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
,
599 struct btrfs_key key
;
600 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
601 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
602 struct btrfs_root
*chunk_root
= malloc(sizeof(struct btrfs_root
));
603 struct btrfs_root
*dev_root
= malloc(sizeof(struct btrfs_root
));
604 struct btrfs_root
*csum_root
= malloc(sizeof(struct btrfs_root
));
605 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
607 struct btrfs_super_block
*disk_super
;
608 struct btrfs_fs_devices
*fs_devices
= NULL
;
613 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
615 ret
= btrfs_scan_one_device(fp
, path
, &fs_devices
,
616 &total_devs
, sb_bytenr
);
619 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
623 if (total_devs
!= 1) {
624 ret
= btrfs_scan_for_fsid(fs_devices
, total_devs
, 1);
628 memset(fs_info
, 0, sizeof(*fs_info
));
629 fs_info
->tree_root
= tree_root
;
630 fs_info
->extent_root
= extent_root
;
631 fs_info
->chunk_root
= chunk_root
;
632 fs_info
->dev_root
= dev_root
;
633 fs_info
->csum_root
= csum_root
;
636 fs_info
->readonly
= 1;
638 extent_io_tree_init(&fs_info
->extent_cache
);
639 extent_io_tree_init(&fs_info
->free_space_cache
);
640 extent_io_tree_init(&fs_info
->block_group_cache
);
641 extent_io_tree_init(&fs_info
->pinned_extents
);
642 extent_io_tree_init(&fs_info
->pending_del
);
643 extent_io_tree_init(&fs_info
->extent_ins
);
644 cache_tree_init(&fs_info
->fs_root_cache
);
646 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
648 mutex_init(&fs_info
->fs_mutex
);
649 fs_info
->fs_devices
= fs_devices
;
650 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
651 INIT_LIST_HEAD(&fs_info
->space_info
);
653 __setup_root(4096, 4096, 4096, 4096, tree_root
,
654 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
657 ret
= btrfs_open_devices(fs_devices
, O_RDWR
);
659 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
662 fs_info
->super_bytenr
= sb_bytenr
;
663 disk_super
= &fs_info
->super_copy
;
664 ret
= btrfs_read_dev_super(fs_devices
->latest_bdev
,
665 disk_super
, sb_bytenr
);
667 printk("No valid btrfs found\n");
671 memcpy(fs_info
->fsid
, &disk_super
->fsid
, BTRFS_FSID_SIZE
);
674 features
= btrfs_super_incompat_flags(disk_super
) &
675 ~BTRFS_FEATURE_INCOMPAT_SUPP
;
677 printk("couldn't open because of unsupported "
678 "option features (%Lx).\n",
679 (unsigned long long)features
);
683 features
= btrfs_super_incompat_flags(disk_super
);
684 if (!(features
& BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
)) {
685 features
|= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
;
686 btrfs_set_super_incompat_flags(disk_super
, features
);
689 features
= btrfs_super_compat_ro_flags(disk_super
) &
690 ~BTRFS_FEATURE_COMPAT_RO_SUPP
;
691 if (writes
&& features
) {
692 printk("couldn't open RDWR because of unsupported "
693 "option features (%Lx).\n",
694 (unsigned long long)features
);
698 nodesize
= btrfs_super_nodesize(disk_super
);
699 leafsize
= btrfs_super_leafsize(disk_super
);
700 sectorsize
= btrfs_super_sectorsize(disk_super
);
701 stripesize
= btrfs_super_stripesize(disk_super
);
702 tree_root
->nodesize
= nodesize
;
703 tree_root
->leafsize
= leafsize
;
704 tree_root
->sectorsize
= sectorsize
;
705 tree_root
->stripesize
= stripesize
;
707 ret
= btrfs_read_sys_array(tree_root
);
709 blocksize
= btrfs_level_size(tree_root
,
710 btrfs_super_chunk_root_level(disk_super
));
711 generation
= btrfs_super_chunk_root_generation(disk_super
);
713 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
714 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
716 chunk_root
->node
= read_tree_block(chunk_root
,
717 btrfs_super_chunk_root(disk_super
),
718 blocksize
, generation
);
720 BUG_ON(!chunk_root
->node
);
722 read_extent_buffer(chunk_root
->node
, fs_info
->chunk_tree_uuid
,
723 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root
->node
),
726 if (!(btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_METADUMP
)) {
727 ret
= btrfs_read_chunk_tree(chunk_root
);
731 blocksize
= btrfs_level_size(tree_root
,
732 btrfs_super_root_level(disk_super
));
733 generation
= btrfs_super_generation(disk_super
);
735 tree_root
->node
= read_tree_block(tree_root
,
736 btrfs_super_root(disk_super
),
737 blocksize
, generation
);
738 BUG_ON(!tree_root
->node
);
739 ret
= find_and_setup_root(tree_root
, fs_info
,
740 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
742 extent_root
->track_dirty
= 1;
744 ret
= find_and_setup_root(tree_root
, fs_info
,
745 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
747 dev_root
->track_dirty
= 1;
749 ret
= find_and_setup_root(tree_root
, fs_info
,
750 BTRFS_CSUM_TREE_OBJECTID
, csum_root
);
752 csum_root
->track_dirty
= 1;
756 find_and_setup_log_root(tree_root
, fs_info
, disk_super
);
758 fs_info
->generation
= generation
+ 1;
759 btrfs_read_block_groups(fs_info
->tree_root
);
761 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
762 key
.type
= BTRFS_ROOT_ITEM_KEY
;
763 key
.offset
= (u64
)-1;
764 fs_info
->fs_root
= btrfs_read_fs_root(fs_info
, &key
);
766 fs_info
->data_alloc_profile
= (u64
)-1;
767 fs_info
->metadata_alloc_profile
= (u64
)-1;
768 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
770 return fs_info
->fs_root
;
773 int btrfs_read_dev_super(int fd
, struct btrfs_super_block
*sb
, u64 sb_bytenr
)
775 u8 fsid
[BTRFS_FSID_SIZE
];
776 struct btrfs_super_block buf
;
782 if (sb_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
783 ret
= pread64(fd
, &buf
, sizeof(buf
), sb_bytenr
);
784 if (ret
< sizeof(buf
))
787 if (btrfs_super_bytenr(&buf
) != sb_bytenr
||
788 strncmp((char *)(&buf
.magic
), BTRFS_MAGIC
,
792 memcpy(sb
, &buf
, sizeof(*sb
));
796 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
797 bytenr
= btrfs_sb_offset(i
);
798 ret
= pread64(fd
, &buf
, sizeof(buf
), bytenr
);
799 if (ret
< sizeof(buf
))
802 if (btrfs_super_bytenr(&buf
) != bytenr
||
803 strncmp((char *)(&buf
.magic
), BTRFS_MAGIC
,
808 memcpy(fsid
, buf
.fsid
, sizeof(fsid
));
809 else if (memcmp(fsid
, buf
.fsid
, sizeof(fsid
)))
812 if (btrfs_super_generation(&buf
) > transid
) {
813 memcpy(sb
, &buf
, sizeof(*sb
));
814 transid
= btrfs_super_generation(&buf
);
818 return transid
> 0 ? 0 : -1;
821 int write_dev_supers(struct btrfs_root
*root
, struct btrfs_super_block
*sb
,
822 struct btrfs_device
*device
)
828 if (root
->fs_info
->super_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
829 btrfs_set_super_bytenr(sb
, root
->fs_info
->super_bytenr
);
830 printk("speiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n");
832 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
833 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
834 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
836 ret
= pwrite64(device
->fd
, sb
, BTRFS_SUPER_INFO_SIZE
,
837 root
->fs_info
->super_bytenr
);
838 BUG_ON(ret
!= BTRFS_SUPER_INFO_SIZE
);
842 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
843 bytenr
= btrfs_sb_offset(i
);
844 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>= device
->total_bytes
)
847 btrfs_set_super_bytenr(sb
, bytenr
);
850 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
851 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
852 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
854 ret
= pwrite64(device
->fd
, sb
, BTRFS_SUPER_INFO_SIZE
, bytenr
);
855 BUG_ON(ret
!= BTRFS_SUPER_INFO_SIZE
);
860 int write_all_supers(struct btrfs_root
*root
)
862 struct list_head
*cur
;
863 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
864 struct btrfs_device
*dev
;
865 struct btrfs_super_block
*sb
;
866 struct btrfs_dev_item
*dev_item
;
870 sb
= &root
->fs_info
->super_copy
;
871 dev_item
= &sb
->dev_item
;
872 list_for_each(cur
, head
) {
873 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
877 btrfs_set_stack_device_generation(dev_item
, 0);
878 btrfs_set_stack_device_type(dev_item
, dev
->type
);
879 btrfs_set_stack_device_id(dev_item
, dev
->devid
);
880 btrfs_set_stack_device_total_bytes(dev_item
, dev
->total_bytes
);
881 btrfs_set_stack_device_bytes_used(dev_item
, dev
->bytes_used
);
882 btrfs_set_stack_device_io_align(dev_item
, dev
->io_align
);
883 btrfs_set_stack_device_io_width(dev_item
, dev
->io_width
);
884 btrfs_set_stack_device_sector_size(dev_item
, dev
->sector_size
);
885 memcpy(dev_item
->uuid
, dev
->uuid
, BTRFS_UUID_SIZE
);
886 memcpy(dev_item
->fsid
, dev
->fs_devices
->fsid
, BTRFS_UUID_SIZE
);
888 flags
= btrfs_super_flags(sb
);
889 btrfs_set_super_flags(sb
, flags
| BTRFS_HEADER_FLAG_WRITTEN
);
891 ret
= write_dev_supers(root
, sb
, dev
);
897 int write_ctree_super(struct btrfs_trans_handle
*trans
,
898 struct btrfs_root
*root
)
901 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
902 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
904 if (root
->fs_info
->readonly
)
907 btrfs_set_super_generation(&root
->fs_info
->super_copy
,
909 btrfs_set_super_root(&root
->fs_info
->super_copy
,
910 tree_root
->node
->start
);
911 btrfs_set_super_root_level(&root
->fs_info
->super_copy
,
912 btrfs_header_level(tree_root
->node
));
913 btrfs_set_super_chunk_root(&root
->fs_info
->super_copy
,
914 chunk_root
->node
->start
);
915 btrfs_set_super_chunk_root_level(&root
->fs_info
->super_copy
,
916 btrfs_header_level(chunk_root
->node
));
917 btrfs_set_super_chunk_root_generation(&root
->fs_info
->super_copy
,
918 btrfs_header_generation(chunk_root
->node
));
920 ret
= write_all_supers(root
);
922 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
926 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
928 struct list_head
*list
;
929 struct list_head
*next
;
930 struct btrfs_device
*device
;
934 list
= &fs_info
->fs_devices
->devices
;
935 list_for_each(next
, list
) {
936 device
= list_entry(next
, struct btrfs_device
, dev_list
);
942 int close_ctree(struct btrfs_root
*root
)
945 struct btrfs_trans_handle
*trans
;
946 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
948 trans
= btrfs_start_transaction(root
, 1);
949 btrfs_commit_transaction(trans
, root
);
950 trans
= btrfs_start_transaction(root
, 1);
951 ret
= commit_tree_roots(trans
, fs_info
);
953 ret
= __commit_transaction(trans
, root
);
955 write_ctree_super(trans
, root
);
956 btrfs_free_transaction(root
, trans
);
957 btrfs_free_block_groups(fs_info
);
959 free_fs_roots(fs_info
);
961 if (fs_info
->extent_root
->node
)
962 free_extent_buffer(fs_info
->extent_root
->node
);
963 if (fs_info
->tree_root
->node
)
964 free_extent_buffer(fs_info
->tree_root
->node
);
965 if (fs_info
->chunk_root
->node
)
966 free_extent_buffer(fs_info
->chunk_root
->node
);
967 if (fs_info
->dev_root
->node
)
968 free_extent_buffer(fs_info
->dev_root
->node
);
969 if (fs_info
->csum_root
->node
)
970 free_extent_buffer(fs_info
->csum_root
->node
);
972 if (fs_info
->log_root_tree
) {
973 if (fs_info
->log_root_tree
->node
)
974 free_extent_buffer(fs_info
->log_root_tree
->node
);
975 free(fs_info
->log_root_tree
);
978 close_all_devices(fs_info
);
979 extent_io_tree_cleanup(&fs_info
->extent_cache
);
980 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
981 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
982 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
983 extent_io_tree_cleanup(&fs_info
->pending_del
);
984 extent_io_tree_cleanup(&fs_info
->extent_ins
);
986 free(fs_info
->tree_root
);
987 free(fs_info
->extent_root
);
988 free(fs_info
->chunk_root
);
989 free(fs_info
->dev_root
);
990 free(fs_info
->csum_root
);
996 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
997 struct extent_buffer
*eb
)
999 return clear_extent_buffer_dirty(eb
);
1002 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
1003 struct extent_buffer
*eb
)
1008 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
1010 set_extent_buffer_dirty(eb
);
1013 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
1017 ret
= extent_buffer_uptodate(buf
);
1021 ret
= verify_parent_transid(buf
->tree
, buf
, parent_transid
);
1025 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
1027 return set_extent_buffer_uptodate(eb
);