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(struct btrfs_root
*root
, struct extent_buffer
*buf
,
73 char result
[BTRFS_CRC32_SIZE
];
77 len
= buf
->len
- BTRFS_CSUM_SIZE
;
78 crc
= crc32c(crc
, buf
->data
+ BTRFS_CSUM_SIZE
, len
);
79 btrfs_csum_final(crc
, result
);
82 if (memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
83 printk("checksum verify failed on %llu wanted %X "
84 "found %X\n", (unsigned long long)buf
->start
,
85 *((int *)result
), *((int *)buf
));
89 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
94 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
95 u64 bytenr
, u32 blocksize
)
97 return find_extent_buffer(&root
->fs_info
->extent_cache
,
101 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
102 u64 bytenr
, u32 blocksize
)
104 return alloc_extent_buffer(&root
->fs_info
->extent_cache
, bytenr
,
108 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
,
113 struct extent_buffer
*eb
;
115 struct btrfs_multi_bio
*multi
= NULL
;
116 struct btrfs_device
*device
;
118 eb
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
119 if (eb
&& btrfs_buffer_uptodate(eb
, parent_transid
)) {
120 free_extent_buffer(eb
);
126 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
127 bytenr
, &length
, &multi
, 0);
129 device
= multi
->stripes
[0].dev
;
131 blocksize
= min(blocksize
, (u32
)(64 * 1024));
132 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
137 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
138 struct extent_buffer
*eb
, u64 parent_transid
)
142 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
145 if (extent_buffer_uptodate(eb
) &&
146 btrfs_header_generation(eb
) == parent_transid
) {
150 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
151 (unsigned long long)eb
->start
,
152 (unsigned long long)parent_transid
,
153 (unsigned long long)btrfs_header_generation(eb
));
156 clear_extent_buffer_uptodate(io_tree
, eb
);
162 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
163 u32 blocksize
, u64 parent_transid
)
167 struct extent_buffer
*eb
;
169 struct btrfs_multi_bio
*multi
= NULL
;
170 struct btrfs_device
*device
;
174 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
178 if (btrfs_buffer_uptodate(eb
, parent_transid
))
184 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
185 eb
->start
, &length
, &multi
, mirror_num
);
187 device
= multi
->stripes
[0].dev
;
190 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
192 ret
= read_extent_from_disk(eb
);
193 if (ret
== 0 && check_tree_block(root
, eb
) == 0 &&
194 csum_tree_block(root
, eb
, 1) == 0 &&
195 verify_parent_transid(eb
->tree
, eb
, parent_transid
) == 0) {
196 btrfs_set_buffer_uptodate(eb
);
199 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
201 if (num_copies
== 1) {
205 if (mirror_num
> num_copies
) {
209 free_extent_buffer(eb
);
213 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
214 struct extent_buffer
*eb
)
219 struct btrfs_multi_bio
*multi
= NULL
;
221 if (check_tree_block(root
, eb
))
223 if (!btrfs_buffer_uptodate(eb
, trans
->transid
))
226 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
227 csum_tree_block(root
, eb
, 0);
231 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, WRITE
,
232 eb
->start
, &length
, &multi
, 0);
234 while(dev_nr
< multi
->num_stripes
) {
236 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
237 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
238 multi
->stripes
[dev_nr
].dev
->total_ios
++;
240 ret
= write_extent_to_disk(eb
);
247 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
248 u32 stripesize
, struct btrfs_root
*root
,
249 struct btrfs_fs_info
*fs_info
, u64 objectid
)
252 root
->commit_root
= NULL
;
253 root
->sectorsize
= sectorsize
;
254 root
->nodesize
= nodesize
;
255 root
->leafsize
= leafsize
;
256 root
->stripesize
= stripesize
;
258 root
->track_dirty
= 0;
260 root
->fs_info
= fs_info
;
261 root
->objectid
= objectid
;
262 root
->last_trans
= 0;
263 root
->highest_inode
= 0;
264 root
->last_inode_alloc
= 0;
266 INIT_LIST_HEAD(&root
->dirty_list
);
267 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
268 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
269 root
->root_key
.objectid
= objectid
;
273 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
274 struct btrfs_root
*root
)
278 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
280 btrfs_write_dirty_block_groups(trans
, root
);
282 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
283 if (old_root_bytenr
== root
->node
->start
)
285 btrfs_set_root_bytenr(&root
->root_item
,
287 btrfs_set_root_generation(&root
->root_item
,
289 root
->root_item
.level
= btrfs_header_level(root
->node
);
290 ret
= btrfs_update_root(trans
, tree_root
,
294 btrfs_write_dirty_block_groups(trans
, root
);
299 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
300 struct btrfs_fs_info
*fs_info
)
302 struct btrfs_root
*root
;
303 struct list_head
*next
;
304 struct extent_buffer
*eb
;
306 if (fs_info
->readonly
)
309 eb
= fs_info
->tree_root
->node
;
310 extent_buffer_get(eb
);
311 btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
312 free_extent_buffer(eb
);
314 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
315 next
= fs_info
->dirty_cowonly_roots
.next
;
317 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
318 update_cowonly_root(trans
, root
);
323 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
324 struct btrfs_root
*root
)
328 struct extent_buffer
*eb
;
329 struct extent_io_tree
*tree
= &root
->fs_info
->extent_cache
;
333 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
337 while(start
<= end
) {
338 eb
= find_first_extent_buffer(tree
, start
);
339 BUG_ON(!eb
|| eb
->start
!= start
);
340 ret
= write_tree_block(trans
, root
, eb
);
343 clear_extent_buffer_dirty(eb
);
344 free_extent_buffer(eb
);
350 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
351 struct btrfs_root
*root
)
354 struct btrfs_root
*new_root
= NULL
;
355 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
357 if (root
->commit_root
== root
->node
)
360 new_root
= malloc(sizeof(*new_root
));
363 memcpy(new_root
, root
, sizeof(*new_root
));
364 new_root
->node
= root
->commit_root
;
365 root
->commit_root
= NULL
;
367 root
->root_key
.offset
= trans
->transid
;
368 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
369 btrfs_set_root_generation(&root
->root_item
, root
->root_key
.offset
);
370 root
->root_item
.level
= btrfs_header_level(root
->node
);
371 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
,
372 &root
->root_key
, &root
->root_item
);
375 btrfs_set_root_refs(&new_root
->root_item
, 0);
376 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
377 &new_root
->root_key
, &new_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 fs_info
->running_transaction
= NULL
;
390 trans
= btrfs_start_transaction(root
, 1);
391 ret
= btrfs_drop_snapshot(trans
, new_root
);
393 ret
= btrfs_del_root(trans
, fs_info
->tree_root
, &new_root
->root_key
);
396 ret
= commit_tree_roots(trans
, fs_info
);
398 ret
= __commit_transaction(trans
, root
);
400 write_ctree_super(trans
, root
);
401 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
402 &fs_info
->pinned_extents
);
403 btrfs_free_transaction(root
, trans
);
404 free_extent_buffer(root
->commit_root
);
405 root
->commit_root
= NULL
;
406 fs_info
->running_transaction
= NULL
;
408 free_extent_buffer(new_root
->node
);
414 static int find_and_setup_root(struct btrfs_root
*tree_root
,
415 struct btrfs_fs_info
*fs_info
,
416 u64 objectid
, struct btrfs_root
*root
)
422 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
423 tree_root
->sectorsize
, tree_root
->stripesize
,
424 root
, fs_info
, objectid
);
425 ret
= btrfs_find_last_root(tree_root
, objectid
,
426 &root
->root_item
, &root
->root_key
);
429 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
430 generation
= btrfs_root_generation(&root
->root_item
);
431 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
432 blocksize
, generation
);
437 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
440 free_extent_buffer(root
->node
);
441 if (root
->commit_root
)
442 free_extent_buffer(root
->commit_root
);
448 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
449 struct btrfs_key
*location
)
451 struct btrfs_root
*root
;
452 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
453 struct btrfs_path
*path
;
454 struct extent_buffer
*l
;
459 root
= malloc(sizeof(*root
));
461 return ERR_PTR(-ENOMEM
);
462 memset(root
, 0, sizeof(*root
));
463 if (location
->offset
== (u64
)-1) {
464 ret
= find_and_setup_root(tree_root
, fs_info
,
465 location
->objectid
, root
);
473 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
474 tree_root
->sectorsize
, tree_root
->stripesize
,
475 root
, fs_info
, location
->objectid
);
477 path
= btrfs_alloc_path();
479 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
486 read_extent_buffer(l
, &root
->root_item
,
487 btrfs_item_ptr_offset(l
, path
->slots
[0]),
488 sizeof(root
->root_item
));
489 memcpy(&root
->root_key
, location
, sizeof(*location
));
492 btrfs_release_path(root
, path
);
493 btrfs_free_path(path
);
498 generation
= btrfs_root_generation(&root
->root_item
);
499 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
500 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
501 blocksize
, generation
);
508 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
, int writes
)
511 struct btrfs_root
*root
;
512 int flags
= O_CREAT
| O_RDWR
;
517 fp
= open(filename
, flags
, 0600);
521 root
= open_ctree_fd(fp
, filename
, sb_bytenr
, writes
);
527 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
,
536 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
537 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
538 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
539 struct btrfs_root
*chunk_root
= malloc(sizeof(struct btrfs_root
));
540 struct btrfs_root
*dev_root
= malloc(sizeof(struct btrfs_root
));
541 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
543 struct btrfs_super_block
*disk_super
;
544 struct btrfs_fs_devices
*fs_devices
= NULL
;
548 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
550 ret
= btrfs_scan_one_device(fp
, path
, &fs_devices
,
551 &total_devs
, sb_bytenr
);
554 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
558 if (total_devs
!= 1) {
559 ret
= btrfs_scan_for_fsid(fs_devices
, total_devs
, 1);
563 memset(fs_info
, 0, sizeof(*fs_info
));
564 fs_info
->fs_root
= root
;
565 fs_info
->tree_root
= tree_root
;
566 fs_info
->extent_root
= extent_root
;
567 fs_info
->chunk_root
= chunk_root
;
568 fs_info
->dev_root
= dev_root
;
571 fs_info
->readonly
= 1;
573 extent_io_tree_init(&fs_info
->extent_cache
);
574 extent_io_tree_init(&fs_info
->free_space_cache
);
575 extent_io_tree_init(&fs_info
->block_group_cache
);
576 extent_io_tree_init(&fs_info
->pinned_extents
);
577 extent_io_tree_init(&fs_info
->pending_del
);
578 extent_io_tree_init(&fs_info
->extent_ins
);
580 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
582 mutex_init(&fs_info
->fs_mutex
);
583 fs_info
->fs_devices
= fs_devices
;
584 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
585 INIT_LIST_HEAD(&fs_info
->space_info
);
587 __setup_root(4096, 4096, 4096, 4096, tree_root
,
588 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
591 ret
= btrfs_open_devices(fs_devices
, O_RDWR
);
593 ret
= btrfs_open_devices(fs_devices
, O_RDONLY
);
596 ret
= btrfs_bootstrap_super_map(&fs_info
->mapping_tree
, fs_devices
);
598 fs_info
->sb_buffer
= btrfs_find_create_tree_block(tree_root
, sb_bytenr
,
600 BUG_ON(!fs_info
->sb_buffer
);
601 fs_info
->sb_buffer
->fd
= fs_devices
->latest_bdev
;
602 fs_info
->sb_buffer
->dev_bytenr
= sb_bytenr
;
603 ret
= read_extent_from_disk(fs_info
->sb_buffer
);
605 btrfs_set_buffer_uptodate(fs_info
->sb_buffer
);
607 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
608 sizeof(fs_info
->super_copy
));
609 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
610 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
613 disk_super
= &fs_info
->super_copy
;
614 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
615 sizeof(disk_super
->magic
))) {
616 printk("No valid btrfs found\n");
619 nodesize
= btrfs_super_nodesize(disk_super
);
620 leafsize
= btrfs_super_leafsize(disk_super
);
621 sectorsize
= btrfs_super_sectorsize(disk_super
);
622 stripesize
= btrfs_super_stripesize(disk_super
);
623 tree_root
->nodesize
= nodesize
;
624 tree_root
->leafsize
= leafsize
;
625 tree_root
->sectorsize
= sectorsize
;
626 tree_root
->stripesize
= stripesize
;
628 ret
= btrfs_read_super_device(tree_root
, fs_info
->sb_buffer
);
630 ret
= btrfs_read_sys_array(tree_root
);
632 blocksize
= btrfs_level_size(tree_root
,
633 btrfs_super_chunk_root_level(disk_super
));
634 generation
= btrfs_super_chunk_root_generation(disk_super
);
636 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
637 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
639 chunk_root
->node
= read_tree_block(chunk_root
,
640 btrfs_super_chunk_root(disk_super
),
641 blocksize
, generation
);
643 BUG_ON(!chunk_root
->node
);
645 read_extent_buffer(chunk_root
->node
, fs_info
->chunk_tree_uuid
,
646 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root
->node
),
649 ret
= btrfs_read_chunk_tree(chunk_root
);
652 blocksize
= btrfs_level_size(tree_root
,
653 btrfs_super_root_level(disk_super
));
654 generation
= btrfs_super_generation(disk_super
);
656 tree_root
->node
= read_tree_block(tree_root
,
657 btrfs_super_root(disk_super
),
658 blocksize
, generation
);
659 BUG_ON(!tree_root
->node
);
660 ret
= find_and_setup_root(tree_root
, fs_info
,
661 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
663 extent_root
->track_dirty
= 1;
665 ret
= find_and_setup_root(tree_root
, fs_info
,
666 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
668 dev_root
->track_dirty
= 1;
670 ret
= find_and_setup_root(tree_root
, fs_info
,
671 BTRFS_FS_TREE_OBJECTID
, root
);
674 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
675 btrfs_read_block_groups(root
);
677 fs_info
->data_alloc_profile
= (u64
)-1;
678 fs_info
->metadata_alloc_profile
= (u64
)-1;
679 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
684 int write_all_supers(struct btrfs_root
*root
)
686 struct list_head
*cur
;
687 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
688 struct btrfs_device
*dev
;
689 struct extent_buffer
*sb
;
690 struct btrfs_dev_item
*dev_item
;
693 sb
= root
->fs_info
->sb_buffer
;
694 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
696 list_for_each(cur
, head
) {
697 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
701 btrfs_set_device_generation(sb
, dev_item
, 0);
702 btrfs_set_device_type(sb
, dev_item
, dev
->type
);
703 btrfs_set_device_id(sb
, dev_item
, dev
->devid
);
704 btrfs_set_device_total_bytes(sb
, dev_item
, dev
->total_bytes
);
705 btrfs_set_device_bytes_used(sb
, dev_item
, dev
->bytes_used
);
706 btrfs_set_device_io_align(sb
, dev_item
, dev
->io_align
);
707 btrfs_set_device_io_width(sb
, dev_item
, dev
->io_width
);
708 btrfs_set_device_sector_size(sb
, dev_item
, dev
->sector_size
);
709 write_extent_buffer(sb
, dev
->uuid
,
710 (unsigned long)btrfs_device_uuid(dev_item
),
712 write_extent_buffer(sb
, dev
->fs_devices
->fsid
,
713 (unsigned long)btrfs_device_fsid(dev_item
),
716 sb
->dev_bytenr
= sb
->start
;
717 btrfs_set_header_flag(sb
, BTRFS_HEADER_FLAG_WRITTEN
);
718 csum_tree_block(root
, sb
, 0);
719 ret
= write_extent_to_disk(sb
);
725 int write_ctree_super(struct btrfs_trans_handle
*trans
,
726 struct btrfs_root
*root
)
729 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
730 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
732 if (root
->fs_info
->readonly
)
735 btrfs_set_super_generation(&root
->fs_info
->super_copy
,
737 btrfs_set_super_root(&root
->fs_info
->super_copy
,
738 tree_root
->node
->start
);
739 btrfs_set_super_root_level(&root
->fs_info
->super_copy
,
740 btrfs_header_level(tree_root
->node
));
741 btrfs_set_super_chunk_root(&root
->fs_info
->super_copy
,
742 chunk_root
->node
->start
);
743 btrfs_set_super_chunk_root_level(&root
->fs_info
->super_copy
,
744 btrfs_header_level(chunk_root
->node
));
745 btrfs_set_super_chunk_root_generation(&root
->fs_info
->super_copy
,
746 btrfs_header_generation(chunk_root
->node
));
747 write_extent_buffer(root
->fs_info
->sb_buffer
,
748 &root
->fs_info
->super_copy
, 0,
749 sizeof(root
->fs_info
->super_copy
));
750 ret
= write_all_supers(root
);
752 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
756 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
758 struct list_head
*list
;
759 struct list_head
*next
;
760 struct btrfs_device
*device
;
764 list
= &fs_info
->fs_devices
->devices
;
765 list_for_each(next
, list
) {
766 device
= list_entry(next
, struct btrfs_device
, dev_list
);
772 int close_ctree(struct btrfs_root
*root
)
775 struct btrfs_trans_handle
*trans
;
776 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
778 trans
= btrfs_start_transaction(root
, 1);
779 btrfs_commit_transaction(trans
, root
);
780 trans
= btrfs_start_transaction(root
, 1);
781 ret
= commit_tree_roots(trans
, root
->fs_info
);
783 ret
= __commit_transaction(trans
, root
);
785 write_ctree_super(trans
, root
);
786 btrfs_free_transaction(root
, trans
);
787 btrfs_free_block_groups(root
->fs_info
);
789 free_extent_buffer(root
->node
);
790 if (root
->fs_info
->extent_root
->node
)
791 free_extent_buffer(root
->fs_info
->extent_root
->node
);
792 if (root
->fs_info
->tree_root
->node
)
793 free_extent_buffer(root
->fs_info
->tree_root
->node
);
794 free_extent_buffer(root
->commit_root
);
795 free_extent_buffer(root
->fs_info
->sb_buffer
);
797 if (root
->fs_info
->chunk_root
->node
);
798 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
800 if (root
->fs_info
->dev_root
->node
);
801 free_extent_buffer(root
->fs_info
->dev_root
->node
);
803 close_all_devices(root
->fs_info
);
804 extent_io_tree_cleanup(&fs_info
->extent_cache
);
805 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
806 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
807 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
808 extent_io_tree_cleanup(&fs_info
->pending_del
);
809 extent_io_tree_cleanup(&fs_info
->extent_ins
);
811 free(fs_info
->tree_root
);
812 free(fs_info
->extent_root
);
813 free(fs_info
->fs_root
);
814 free(fs_info
->chunk_root
);
815 free(fs_info
->dev_root
);
821 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
822 struct extent_buffer
*eb
)
824 return clear_extent_buffer_dirty(eb
);
827 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
828 struct extent_buffer
*eb
)
833 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
835 set_extent_buffer_dirty(eb
);
838 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
842 ret
= extent_buffer_uptodate(buf
);
846 ret
= verify_parent_transid(buf
->tree
, buf
, parent_transid
);
850 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
852 return set_extent_buffer_uptodate(eb
);