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
), *((int *)buf
));
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
,
127 struct extent_buffer
*eb
;
129 struct btrfs_multi_bio
*multi
= NULL
;
130 struct btrfs_device
*device
;
132 eb
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
133 if (eb
&& btrfs_buffer_uptodate(eb
, parent_transid
)) {
134 free_extent_buffer(eb
);
140 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
141 bytenr
, &length
, &multi
, 0);
143 device
= multi
->stripes
[0].dev
;
145 blocksize
= min(blocksize
, (u32
)(64 * 1024));
146 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
151 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
152 struct extent_buffer
*eb
, u64 parent_transid
)
156 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
159 if (extent_buffer_uptodate(eb
) &&
160 btrfs_header_generation(eb
) == parent_transid
) {
164 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
165 (unsigned long long)eb
->start
,
166 (unsigned long long)parent_transid
,
167 (unsigned long long)btrfs_header_generation(eb
));
170 clear_extent_buffer_uptodate(io_tree
, eb
);
176 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
177 u32 blocksize
, u64 parent_transid
)
181 struct extent_buffer
*eb
;
183 struct btrfs_multi_bio
*multi
= NULL
;
184 struct btrfs_device
*device
;
188 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
192 if (btrfs_buffer_uptodate(eb
, parent_transid
))
198 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
199 eb
->start
, &length
, &multi
, mirror_num
);
201 device
= multi
->stripes
[0].dev
;
204 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
206 ret
= read_extent_from_disk(eb
);
207 if (ret
== 0 && check_tree_block(root
, eb
) == 0 &&
208 csum_tree_block(root
, eb
, 1) == 0 &&
209 verify_parent_transid(eb
->tree
, eb
, parent_transid
) == 0) {
210 btrfs_set_buffer_uptodate(eb
);
213 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
215 if (num_copies
== 1) {
219 if (mirror_num
> num_copies
) {
223 free_extent_buffer(eb
);
227 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
228 struct extent_buffer
*eb
)
233 struct btrfs_multi_bio
*multi
= NULL
;
235 if (check_tree_block(root
, eb
))
237 if (!btrfs_buffer_uptodate(eb
, trans
->transid
))
240 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
241 csum_tree_block(root
, eb
, 0);
245 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, WRITE
,
246 eb
->start
, &length
, &multi
, 0);
248 while(dev_nr
< multi
->num_stripes
) {
250 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
251 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
252 multi
->stripes
[dev_nr
].dev
->total_ios
++;
254 ret
= write_extent_to_disk(eb
);
261 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
262 u32 stripesize
, struct btrfs_root
*root
,
263 struct btrfs_fs_info
*fs_info
, u64 objectid
)
266 root
->commit_root
= NULL
;
267 root
->sectorsize
= sectorsize
;
268 root
->nodesize
= nodesize
;
269 root
->leafsize
= leafsize
;
270 root
->stripesize
= stripesize
;
272 root
->track_dirty
= 0;
274 root
->fs_info
= fs_info
;
275 root
->objectid
= objectid
;
276 root
->last_trans
= 0;
277 root
->highest_inode
= 0;
278 root
->last_inode_alloc
= 0;
280 INIT_LIST_HEAD(&root
->dirty_list
);
281 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
282 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
283 root
->root_key
.objectid
= objectid
;
287 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
288 struct btrfs_root
*root
)
292 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
294 btrfs_write_dirty_block_groups(trans
, root
);
296 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
297 if (old_root_bytenr
== root
->node
->start
)
299 btrfs_set_root_bytenr(&root
->root_item
,
301 btrfs_set_root_generation(&root
->root_item
,
303 root
->root_item
.level
= btrfs_header_level(root
->node
);
304 ret
= btrfs_update_root(trans
, tree_root
,
308 btrfs_write_dirty_block_groups(trans
, root
);
313 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
314 struct btrfs_fs_info
*fs_info
)
316 struct btrfs_root
*root
;
317 struct list_head
*next
;
318 struct extent_buffer
*eb
;
320 if (fs_info
->readonly
)
323 eb
= fs_info
->tree_root
->node
;
324 extent_buffer_get(eb
);
325 btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
326 free_extent_buffer(eb
);
328 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
329 next
= fs_info
->dirty_cowonly_roots
.next
;
331 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
332 update_cowonly_root(trans
, root
);
337 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
338 struct btrfs_root
*root
)
342 struct extent_buffer
*eb
;
343 struct extent_io_tree
*tree
= &root
->fs_info
->extent_cache
;
347 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
351 while(start
<= end
) {
352 eb
= find_first_extent_buffer(tree
, start
);
353 BUG_ON(!eb
|| eb
->start
!= start
);
354 ret
= write_tree_block(trans
, root
, eb
);
357 clear_extent_buffer_dirty(eb
);
358 free_extent_buffer(eb
);
364 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
365 struct btrfs_root
*root
)
368 struct btrfs_root
*new_root
= NULL
;
369 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
371 if (root
->commit_root
== root
->node
)
374 new_root
= malloc(sizeof(*new_root
));
377 memcpy(new_root
, root
, sizeof(*new_root
));
378 new_root
->node
= root
->commit_root
;
379 root
->commit_root
= NULL
;
381 root
->root_key
.offset
= trans
->transid
;
382 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
383 btrfs_set_root_generation(&root
->root_item
, root
->root_key
.offset
);
384 root
->root_item
.level
= btrfs_header_level(root
->node
);
385 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
,
386 &root
->root_key
, &root
->root_item
);
389 btrfs_set_root_refs(&new_root
->root_item
, 0);
390 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
391 &new_root
->root_key
, &new_root
->root_item
);
394 ret
= commit_tree_roots(trans
, fs_info
);
396 ret
= __commit_transaction(trans
, root
);
398 write_ctree_super(trans
, root
);
399 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
400 &fs_info
->pinned_extents
);
401 btrfs_free_transaction(root
, trans
);
402 fs_info
->running_transaction
= NULL
;
404 trans
= btrfs_start_transaction(root
, 1);
405 ret
= btrfs_drop_snapshot(trans
, new_root
);
407 ret
= btrfs_del_root(trans
, fs_info
->tree_root
, &new_root
->root_key
);
410 ret
= commit_tree_roots(trans
, fs_info
);
412 ret
= __commit_transaction(trans
, root
);
414 write_ctree_super(trans
, root
);
415 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
416 &fs_info
->pinned_extents
);
417 btrfs_free_transaction(root
, trans
);
418 free_extent_buffer(root
->commit_root
);
419 root
->commit_root
= NULL
;
420 fs_info
->running_transaction
= NULL
;
422 free_extent_buffer(new_root
->node
);
428 static int find_and_setup_root(struct btrfs_root
*tree_root
,
429 struct btrfs_fs_info
*fs_info
,
430 u64 objectid
, struct btrfs_root
*root
)
436 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
437 tree_root
->sectorsize
, tree_root
->stripesize
,
438 root
, fs_info
, objectid
);
439 ret
= btrfs_find_last_root(tree_root
, objectid
,
440 &root
->root_item
, &root
->root_key
);
443 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
444 generation
= btrfs_root_generation(&root
->root_item
);
445 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
446 blocksize
, generation
);
451 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
454 free_extent_buffer(root
->node
);
455 if (root
->commit_root
)
456 free_extent_buffer(root
->commit_root
);
462 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
463 struct btrfs_key
*location
)
465 struct btrfs_root
*root
;
466 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
467 struct btrfs_path
*path
;
468 struct extent_buffer
*l
;
473 root
= malloc(sizeof(*root
));
475 return ERR_PTR(-ENOMEM
);
476 memset(root
, 0, sizeof(*root
));
477 if (location
->offset
== (u64
)-1) {
478 ret
= find_and_setup_root(tree_root
, fs_info
,
479 location
->objectid
, root
);
487 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
488 tree_root
->sectorsize
, tree_root
->stripesize
,
489 root
, fs_info
, location
->objectid
);
491 path
= btrfs_alloc_path();
493 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
500 read_extent_buffer(l
, &root
->root_item
,
501 btrfs_item_ptr_offset(l
, path
->slots
[0]),
502 sizeof(root
->root_item
));
503 memcpy(&root
->root_key
, location
, sizeof(*location
));
506 btrfs_release_path(root
, path
);
507 btrfs_free_path(path
);
512 generation
= btrfs_root_generation(&root
->root_item
);
513 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
514 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
515 blocksize
, generation
);
522 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
, int writes
)
525 struct btrfs_root
*root
;
526 int flags
= O_CREAT
| O_RDWR
;
531 fp
= open(filename
, flags
, 0600);
535 root
= open_ctree_fd(fp
, filename
, sb_bytenr
, writes
);
541 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
,
550 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
551 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
552 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
553 struct btrfs_root
*chunk_root
= malloc(sizeof(struct btrfs_root
));
554 struct btrfs_root
*dev_root
= malloc(sizeof(struct btrfs_root
));
555 struct btrfs_root
*csum_root
= malloc(sizeof(struct btrfs_root
));
556 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
558 struct btrfs_super_block
*disk_super
;
559 struct btrfs_fs_devices
*fs_devices
= NULL
;
563 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
565 ret
= btrfs_scan_one_device(fp
, path
, &fs_devices
,
566 &total_devs
, sb_bytenr
);
569 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
573 if (total_devs
!= 1) {
574 ret
= btrfs_scan_for_fsid(fs_devices
, total_devs
, 1);
578 memset(fs_info
, 0, sizeof(*fs_info
));
579 fs_info
->fs_root
= root
;
580 fs_info
->tree_root
= tree_root
;
581 fs_info
->extent_root
= extent_root
;
582 fs_info
->chunk_root
= chunk_root
;
583 fs_info
->dev_root
= dev_root
;
584 fs_info
->csum_root
= csum_root
;
587 fs_info
->readonly
= 1;
589 extent_io_tree_init(&fs_info
->extent_cache
);
590 extent_io_tree_init(&fs_info
->free_space_cache
);
591 extent_io_tree_init(&fs_info
->block_group_cache
);
592 extent_io_tree_init(&fs_info
->pinned_extents
);
593 extent_io_tree_init(&fs_info
->pending_del
);
594 extent_io_tree_init(&fs_info
->extent_ins
);
596 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
598 mutex_init(&fs_info
->fs_mutex
);
599 fs_info
->fs_devices
= fs_devices
;
600 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
601 INIT_LIST_HEAD(&fs_info
->space_info
);
603 __setup_root(4096, 4096, 4096, 4096, tree_root
,
604 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
607 ret
= btrfs_open_devices(fs_devices
, O_RDWR
);
609 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
612 fs_info
->super_bytenr
= sb_bytenr
;
613 disk_super
= &fs_info
->super_copy
;
614 ret
= btrfs_read_dev_super(fs_devices
->latest_bdev
,
615 disk_super
, sb_bytenr
);
617 printk("No valid btrfs found\n");
621 memcpy(fs_info
->fsid
, &disk_super
->fsid
, BTRFS_FSID_SIZE
);
623 nodesize
= btrfs_super_nodesize(disk_super
);
624 leafsize
= btrfs_super_leafsize(disk_super
);
625 sectorsize
= btrfs_super_sectorsize(disk_super
);
626 stripesize
= btrfs_super_stripesize(disk_super
);
627 tree_root
->nodesize
= nodesize
;
628 tree_root
->leafsize
= leafsize
;
629 tree_root
->sectorsize
= sectorsize
;
630 tree_root
->stripesize
= stripesize
;
632 ret
= btrfs_read_sys_array(tree_root
);
634 blocksize
= btrfs_level_size(tree_root
,
635 btrfs_super_chunk_root_level(disk_super
));
636 generation
= btrfs_super_chunk_root_generation(disk_super
);
638 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
639 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
641 chunk_root
->node
= read_tree_block(chunk_root
,
642 btrfs_super_chunk_root(disk_super
),
643 blocksize
, generation
);
645 BUG_ON(!chunk_root
->node
);
647 read_extent_buffer(chunk_root
->node
, fs_info
->chunk_tree_uuid
,
648 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root
->node
),
651 if (!(btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_METADUMP
)) {
652 ret
= btrfs_read_chunk_tree(chunk_root
);
656 blocksize
= btrfs_level_size(tree_root
,
657 btrfs_super_root_level(disk_super
));
658 generation
= btrfs_super_generation(disk_super
);
660 tree_root
->node
= read_tree_block(tree_root
,
661 btrfs_super_root(disk_super
),
662 blocksize
, generation
);
663 BUG_ON(!tree_root
->node
);
664 ret
= find_and_setup_root(tree_root
, fs_info
,
665 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
667 extent_root
->track_dirty
= 1;
669 ret
= find_and_setup_root(tree_root
, fs_info
,
670 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
672 dev_root
->track_dirty
= 1;
674 ret
= find_and_setup_root(tree_root
, fs_info
,
675 BTRFS_CSUM_TREE_OBJECTID
, csum_root
);
677 csum_root
->track_dirty
= 1;
679 ret
= find_and_setup_root(tree_root
, fs_info
,
680 BTRFS_FS_TREE_OBJECTID
, root
);
683 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
684 btrfs_read_block_groups(root
);
686 fs_info
->data_alloc_profile
= (u64
)-1;
687 fs_info
->metadata_alloc_profile
= (u64
)-1;
688 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
693 int btrfs_read_dev_super(int fd
, struct btrfs_super_block
*sb
, u64 sb_bytenr
)
695 struct btrfs_super_block buf
;
701 if (sb_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
702 ret
= pread64(fd
, &buf
, sizeof(buf
), sb_bytenr
);
703 if (ret
< sizeof(buf
))
706 if (btrfs_super_bytenr(&buf
) != sb_bytenr
||
707 strncmp((char *)(&buf
.magic
), BTRFS_MAGIC
,
711 memcpy(sb
, &buf
, sizeof(*sb
));
715 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
716 bytenr
= btrfs_sb_offset(i
);
717 ret
= pread64(fd
, &buf
, sizeof(buf
), bytenr
);
718 if (ret
< sizeof(buf
))
721 if (btrfs_super_bytenr(&buf
) != bytenr
||
722 strncmp((char *)(&buf
.magic
), BTRFS_MAGIC
,
726 if (btrfs_super_generation(&buf
) > transid
) {
727 memcpy(sb
, &buf
, sizeof(*sb
));
728 transid
= btrfs_super_generation(&buf
);
732 return transid
> 0 ? 0 : -1;
735 int write_dev_supers(struct btrfs_root
*root
, struct btrfs_super_block
*sb
,
736 struct btrfs_device
*device
)
742 if (root
->fs_info
->super_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
743 btrfs_set_super_bytenr(sb
, root
->fs_info
->super_bytenr
);
746 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
747 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
748 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
750 ret
= pwrite64(device
->fd
, sb
, BTRFS_SUPER_INFO_SIZE
,
751 root
->fs_info
->super_bytenr
);
752 BUG_ON(ret
!= BTRFS_SUPER_INFO_SIZE
);
756 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
757 bytenr
= btrfs_sb_offset(i
);
758 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>= device
->total_bytes
)
761 btrfs_set_super_bytenr(sb
, bytenr
);
764 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
765 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
766 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
768 ret
= pwrite64(device
->fd
, sb
, BTRFS_SUPER_INFO_SIZE
, bytenr
);
769 BUG_ON(ret
!= BTRFS_SUPER_INFO_SIZE
);
774 int write_all_supers(struct btrfs_root
*root
)
776 struct list_head
*cur
;
777 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
778 struct btrfs_device
*dev
;
779 struct btrfs_super_block
*sb
;
780 struct btrfs_dev_item
*dev_item
;
784 sb
= &root
->fs_info
->super_copy
;
785 dev_item
= &sb
->dev_item
;
786 list_for_each(cur
, head
) {
787 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
791 btrfs_set_stack_device_generation(dev_item
, 0);
792 btrfs_set_stack_device_type(dev_item
, dev
->type
);
793 btrfs_set_stack_device_id(dev_item
, dev
->devid
);
794 btrfs_set_stack_device_total_bytes(dev_item
, dev
->total_bytes
);
795 btrfs_set_stack_device_bytes_used(dev_item
, dev
->bytes_used
);
796 btrfs_set_stack_device_io_align(dev_item
, dev
->io_align
);
797 btrfs_set_stack_device_io_width(dev_item
, dev
->io_width
);
798 btrfs_set_stack_device_sector_size(dev_item
, dev
->sector_size
);
799 memcpy(dev_item
->uuid
, dev
->uuid
, BTRFS_UUID_SIZE
);
800 memcpy(dev_item
->fsid
, dev
->fs_devices
->fsid
, BTRFS_UUID_SIZE
);
802 flags
= btrfs_super_flags(sb
);
803 btrfs_set_super_flags(sb
, flags
| BTRFS_HEADER_FLAG_WRITTEN
);
805 ret
= write_dev_supers(root
, sb
, dev
);
811 int write_ctree_super(struct btrfs_trans_handle
*trans
,
812 struct btrfs_root
*root
)
815 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
816 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
818 if (root
->fs_info
->readonly
)
821 btrfs_set_super_generation(&root
->fs_info
->super_copy
,
823 btrfs_set_super_root(&root
->fs_info
->super_copy
,
824 tree_root
->node
->start
);
825 btrfs_set_super_root_level(&root
->fs_info
->super_copy
,
826 btrfs_header_level(tree_root
->node
));
827 btrfs_set_super_chunk_root(&root
->fs_info
->super_copy
,
828 chunk_root
->node
->start
);
829 btrfs_set_super_chunk_root_level(&root
->fs_info
->super_copy
,
830 btrfs_header_level(chunk_root
->node
));
831 btrfs_set_super_chunk_root_generation(&root
->fs_info
->super_copy
,
832 btrfs_header_generation(chunk_root
->node
));
834 ret
= write_all_supers(root
);
836 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
840 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
842 struct list_head
*list
;
843 struct list_head
*next
;
844 struct btrfs_device
*device
;
848 list
= &fs_info
->fs_devices
->devices
;
849 list_for_each(next
, list
) {
850 device
= list_entry(next
, struct btrfs_device
, dev_list
);
856 int close_ctree(struct btrfs_root
*root
)
859 struct btrfs_trans_handle
*trans
;
860 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
862 trans
= btrfs_start_transaction(root
, 1);
863 btrfs_commit_transaction(trans
, root
);
864 trans
= btrfs_start_transaction(root
, 1);
865 ret
= commit_tree_roots(trans
, root
->fs_info
);
867 ret
= __commit_transaction(trans
, root
);
869 write_ctree_super(trans
, root
);
870 btrfs_free_transaction(root
, trans
);
871 btrfs_free_block_groups(root
->fs_info
);
873 free_extent_buffer(root
->node
);
874 if (root
->fs_info
->extent_root
->node
)
875 free_extent_buffer(root
->fs_info
->extent_root
->node
);
876 if (root
->fs_info
->tree_root
->node
)
877 free_extent_buffer(root
->fs_info
->tree_root
->node
);
878 free_extent_buffer(root
->commit_root
);
880 if (root
->fs_info
->chunk_root
->node
)
881 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
882 if (root
->fs_info
->dev_root
->node
)
883 free_extent_buffer(root
->fs_info
->dev_root
->node
);
884 if (root
->fs_info
->csum_root
->node
)
885 free_extent_buffer(root
->fs_info
->csum_root
->node
);
887 close_all_devices(root
->fs_info
);
888 extent_io_tree_cleanup(&fs_info
->extent_cache
);
889 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
890 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
891 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
892 extent_io_tree_cleanup(&fs_info
->pending_del
);
893 extent_io_tree_cleanup(&fs_info
->extent_ins
);
895 free(fs_info
->tree_root
);
896 free(fs_info
->extent_root
);
897 free(fs_info
->fs_root
);
898 free(fs_info
->chunk_root
);
899 free(fs_info
->dev_root
);
900 free(fs_info
->csum_root
);
906 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
907 struct extent_buffer
*eb
)
909 return clear_extent_buffer_dirty(eb
);
912 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
913 struct extent_buffer
*eb
)
918 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
920 set_extent_buffer_dirty(eb
);
923 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
927 ret
= extent_buffer_uptodate(buf
);
931 ret
= verify_parent_transid(buf
->tree
, buf
, parent_transid
);
935 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
937 return set_extent_buffer_uptodate(eb
);