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,
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);
208 if (ret == 0 && check_tree_block(root, eb) == 0 &&
209 csum_tree_block(root, eb, 1) == 0 &&
210 verify_parent_transid(eb->tree, eb, parent_transid) == 0) {
211 btrfs_set_buffer_uptodate(eb);
214 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
216 if (num_copies == 1) {
220 if (mirror_num > num_copies) {
224 free_extent_buffer(eb);
228 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
229 struct extent_buffer *eb)
234 struct btrfs_multi_bio *multi = NULL;
236 if (check_tree_block(root, eb))
238 if (!btrfs_buffer_uptodate(eb, trans->transid))
241 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
242 csum_tree_block(root, eb, 0);
246 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
247 eb->start, &length, &multi, 0);
249 while(dev_nr < multi->num_stripes) {
251 eb->fd = multi->stripes[dev_nr].dev->fd;
252 eb->dev_bytenr = multi->stripes[dev_nr].physical;
253 multi->stripes[dev_nr].dev->total_ios++;
255 ret = write_extent_to_disk(eb);
262 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
263 u32 stripesize, struct btrfs_root *root,
264 struct btrfs_fs_info *fs_info, u64 objectid)
267 root->commit_root = NULL;
268 root->sectorsize = sectorsize;
269 root->nodesize = nodesize;
270 root->leafsize = leafsize;
271 root->stripesize = stripesize;
273 root->track_dirty = 0;
275 root->fs_info = fs_info;
276 root->objectid = objectid;
277 root->last_trans = 0;
278 root->highest_inode = 0;
279 root->last_inode_alloc = 0;
281 INIT_LIST_HEAD(&root->dirty_list);
282 memset(&root->root_key, 0, sizeof(root->root_key));
283 memset(&root->root_item, 0, sizeof(root->root_item));
284 root->root_key.objectid = objectid;
288 static int update_cowonly_root(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root)
293 struct btrfs_root *tree_root = root->fs_info->tree_root;
295 btrfs_write_dirty_block_groups(trans, root);
297 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
298 if (old_root_bytenr == root->node->start)
300 btrfs_set_root_bytenr(&root->root_item,
302 btrfs_set_root_generation(&root->root_item,
304 root->root_item.level = btrfs_header_level(root->node);
305 ret = btrfs_update_root(trans, tree_root,
309 btrfs_write_dirty_block_groups(trans, root);
314 static int commit_tree_roots(struct btrfs_trans_handle *trans,
315 struct btrfs_fs_info *fs_info)
317 struct btrfs_root *root;
318 struct list_head *next;
319 struct extent_buffer *eb;
321 if (fs_info->readonly)
324 eb = fs_info->tree_root->node;
325 extent_buffer_get(eb);
326 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
327 free_extent_buffer(eb);
329 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
330 next = fs_info->dirty_cowonly_roots.next;
332 root = list_entry(next, struct btrfs_root, dirty_list);
333 update_cowonly_root(trans, root);
338 static int __commit_transaction(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root)
343 struct extent_buffer *eb;
344 struct extent_io_tree *tree = &root->fs_info->extent_cache;
348 ret = find_first_extent_bit(tree, 0, &start, &end,
352 while(start <= end) {
353 eb = find_first_extent_buffer(tree, start);
354 BUG_ON(!eb || eb->start != start);
355 ret = write_tree_block(trans, root, eb);
358 clear_extent_buffer_dirty(eb);
359 free_extent_buffer(eb);
365 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root)
369 struct btrfs_fs_info *fs_info = root->fs_info;
371 if (root->commit_root == root->node)
374 free_extent_buffer(root->commit_root);
375 root->commit_root = NULL;
377 btrfs_set_root_bytenr(&root->root_item, root->node->start);
378 btrfs_set_root_generation(&root->root_item, trans->transid);
379 root->root_item.level = btrfs_header_level(root->node);
380 ret = btrfs_update_root(trans, root->fs_info->tree_root,
381 &root->root_key, &root->root_item);
384 ret = commit_tree_roots(trans, fs_info);
386 ret = __commit_transaction(trans, root);
388 write_ctree_super(trans, root);
389 btrfs_finish_extent_commit(trans, fs_info->extent_root,
390 &fs_info->pinned_extents);
391 btrfs_free_transaction(root, trans);
392 free_extent_buffer(root->commit_root);
393 root->commit_root = NULL;
394 fs_info->running_transaction = NULL;
398 static int find_and_setup_root(struct btrfs_root *tree_root,
399 struct btrfs_fs_info *fs_info,
400 u64 objectid, struct btrfs_root *root)
406 __setup_root(tree_root->nodesize, tree_root->leafsize,
407 tree_root->sectorsize, tree_root->stripesize,
408 root, fs_info, objectid);
409 ret = btrfs_find_last_root(tree_root, objectid,
410 &root->root_item, &root->root_key);
413 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
414 generation = btrfs_root_generation(&root->root_item);
415 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
416 blocksize, generation);
421 static int find_and_setup_log_root(struct btrfs_root *tree_root,
422 struct btrfs_fs_info *fs_info,
423 struct btrfs_super_block *disk_super)
426 u64 blocknr = btrfs_super_log_root(disk_super);
427 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
432 blocksize = btrfs_level_size(tree_root,
433 btrfs_super_log_root_level(disk_super));
435 __setup_root(tree_root->nodesize, tree_root->leafsize,
436 tree_root->sectorsize, tree_root->stripesize,
437 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
439 log_root->node = read_tree_block(tree_root, blocknr,
441 btrfs_super_generation(disk_super) + 1);
443 fs_info->log_root_tree = log_root;
444 BUG_ON(!log_root->node);
449 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
450 struct btrfs_root *root)
453 free_extent_buffer(root->node);
454 if (root->commit_root)
455 free_extent_buffer(root->commit_root);
460 static int free_fs_roots(struct btrfs_fs_info *fs_info)
462 struct cache_extent *cache;
463 struct btrfs_root *root;
466 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
469 root = container_of(cache, struct btrfs_root, cache);
470 remove_cache_extent(&fs_info->fs_root_cache, cache);
471 btrfs_free_fs_root(fs_info, root);
476 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
477 struct btrfs_key *location)
479 struct btrfs_root *root;
480 struct btrfs_root *tree_root = fs_info->tree_root;
481 struct btrfs_path *path;
482 struct extent_buffer *l;
487 root = malloc(sizeof(*root));
489 return ERR_PTR(-ENOMEM);
490 memset(root, 0, sizeof(*root));
491 if (location->offset == (u64)-1) {
492 ret = find_and_setup_root(tree_root, fs_info,
493 location->objectid, root);
501 __setup_root(tree_root->nodesize, tree_root->leafsize,
502 tree_root->sectorsize, tree_root->stripesize,
503 root, fs_info, location->objectid);
505 path = btrfs_alloc_path();
507 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
514 read_extent_buffer(l, &root->root_item,
515 btrfs_item_ptr_offset(l, path->slots[0]),
516 sizeof(root->root_item));
517 memcpy(&root->root_key, location, sizeof(*location));
520 btrfs_release_path(root, path);
521 btrfs_free_path(path);
526 generation = btrfs_root_generation(&root->root_item);
527 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
528 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
529 blocksize, generation);
536 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
537 struct btrfs_key *location)
539 struct btrfs_root *root;
540 struct cache_extent *cache;
543 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
544 return fs_info->tree_root;
545 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
546 return fs_info->extent_root;
547 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
548 return fs_info->chunk_root;
549 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
550 return fs_info->dev_root;
551 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
552 return fs_info->csum_root;
554 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
555 location->offset != (u64)-1);
557 cache = find_cache_extent(&fs_info->fs_root_cache,
558 location->objectid, 1);
560 return container_of(cache, struct btrfs_root, cache);
562 root = btrfs_read_fs_root_no_cache(fs_info, location);
566 root->cache.start = location->objectid;
567 root->cache.size = 1;
568 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
574 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
577 struct btrfs_root *root;
578 int flags = O_CREAT | O_RDWR;
583 fp = open(filename, flags, 0600);
585 fprintf (stderr, "Could not open %s\n", filename);
588 root = open_ctree_fd(fp, filename, sb_bytenr, writes);
594 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
603 struct btrfs_key key;
604 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
605 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
606 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
607 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
608 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
609 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
611 struct btrfs_super_block *disk_super;
612 struct btrfs_fs_devices *fs_devices = NULL;
617 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
619 ret = btrfs_scan_one_device(fp, path, &fs_devices,
620 &total_devs, sb_bytenr);
623 fprintf(stderr, "No valid Btrfs found on %s\n", path);
627 if (total_devs != 1) {
628 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
632 memset(fs_info, 0, sizeof(*fs_info));
633 fs_info->tree_root = tree_root;
634 fs_info->extent_root = extent_root;
635 fs_info->chunk_root = chunk_root;
636 fs_info->dev_root = dev_root;
637 fs_info->csum_root = csum_root;
640 fs_info->readonly = 1;
642 extent_io_tree_init(&fs_info->extent_cache);
643 extent_io_tree_init(&fs_info->free_space_cache);
644 extent_io_tree_init(&fs_info->block_group_cache);
645 extent_io_tree_init(&fs_info->pinned_extents);
646 extent_io_tree_init(&fs_info->pending_del);
647 extent_io_tree_init(&fs_info->extent_ins);
648 cache_tree_init(&fs_info->fs_root_cache);
650 cache_tree_init(&fs_info->mapping_tree.cache_tree);
652 mutex_init(&fs_info->fs_mutex);
653 fs_info->fs_devices = fs_devices;
654 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
655 INIT_LIST_HEAD(&fs_info->space_info);
657 __setup_root(4096, 4096, 4096, 4096, tree_root,
658 fs_info, BTRFS_ROOT_TREE_OBJECTID);
661 ret = btrfs_open_devices(fs_devices, O_RDWR);
663 ret = btrfs_open_devices(fs_devices, O_RDONLY);
666 fs_info->super_bytenr = sb_bytenr;
667 disk_super = &fs_info->super_copy;
668 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
669 disk_super, sb_bytenr);
671 printk("No valid btrfs found\n");
675 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
678 features = btrfs_super_incompat_flags(disk_super) &
679 ~BTRFS_FEATURE_INCOMPAT_SUPP;
681 printk("couldn't open because of unsupported "
682 "option features (%Lx).\n", features);
686 features = btrfs_super_incompat_flags(disk_super);
687 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
688 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
689 btrfs_set_super_incompat_flags(disk_super, features);
692 features = btrfs_super_compat_ro_flags(disk_super) &
693 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
694 if (writes && features) {
695 printk("couldn't open RDWR because of unsupported "
696 "option features (%Lx).\n", features);
700 nodesize = btrfs_super_nodesize(disk_super);
701 leafsize = btrfs_super_leafsize(disk_super);
702 sectorsize = btrfs_super_sectorsize(disk_super);
703 stripesize = btrfs_super_stripesize(disk_super);
704 tree_root->nodesize = nodesize;
705 tree_root->leafsize = leafsize;
706 tree_root->sectorsize = sectorsize;
707 tree_root->stripesize = stripesize;
709 ret = btrfs_read_sys_array(tree_root);
711 blocksize = btrfs_level_size(tree_root,
712 btrfs_super_chunk_root_level(disk_super));
713 generation = btrfs_super_chunk_root_generation(disk_super);
715 __setup_root(nodesize, leafsize, sectorsize, stripesize,
716 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
718 chunk_root->node = read_tree_block(chunk_root,
719 btrfs_super_chunk_root(disk_super),
720 blocksize, generation);
722 BUG_ON(!chunk_root->node);
724 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
725 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
728 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
729 ret = btrfs_read_chunk_tree(chunk_root);
733 blocksize = btrfs_level_size(tree_root,
734 btrfs_super_root_level(disk_super));
735 generation = btrfs_super_generation(disk_super);
737 tree_root->node = read_tree_block(tree_root,
738 btrfs_super_root(disk_super),
739 blocksize, generation);
740 BUG_ON(!tree_root->node);
741 ret = find_and_setup_root(tree_root, fs_info,
742 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
744 extent_root->track_dirty = 1;
746 ret = find_and_setup_root(tree_root, fs_info,
747 BTRFS_DEV_TREE_OBJECTID, dev_root);
749 dev_root->track_dirty = 1;
751 ret = find_and_setup_root(tree_root, fs_info,
752 BTRFS_CSUM_TREE_OBJECTID, csum_root);
754 csum_root->track_dirty = 1;
758 find_and_setup_log_root(tree_root, fs_info, disk_super);
760 fs_info->generation = generation + 1;
761 btrfs_read_block_groups(fs_info->tree_root);
763 key.objectid = BTRFS_FS_TREE_OBJECTID;
764 key.type = BTRFS_ROOT_ITEM_KEY;
765 key.offset = (u64)-1;
766 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
768 fs_info->data_alloc_profile = (u64)-1;
769 fs_info->metadata_alloc_profile = (u64)-1;
770 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
772 return fs_info->fs_root;
775 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
777 u8 fsid[BTRFS_FSID_SIZE];
778 struct btrfs_super_block buf;
784 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
785 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
786 if (ret < sizeof(buf))
789 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
790 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
794 memcpy(sb, &buf, sizeof(*sb));
798 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
799 bytenr = btrfs_sb_offset(i);
800 ret = pread64(fd, &buf, sizeof(buf), bytenr);
801 if (ret < sizeof(buf))
804 if (btrfs_super_bytenr(&buf) != bytenr ||
805 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
810 memcpy(fsid, buf.fsid, sizeof(fsid));
811 else if (memcmp(fsid, buf.fsid, sizeof(fsid)))
814 if (btrfs_super_generation(&buf) > transid) {
815 memcpy(sb, &buf, sizeof(*sb));
816 transid = btrfs_super_generation(&buf);
820 return transid > 0 ? 0 : -1;
823 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
824 struct btrfs_device *device)
830 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
831 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
834 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
835 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
836 btrfs_csum_final(crc, (char *)&sb->csum[0]);
838 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
839 root->fs_info->super_bytenr);
840 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
844 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
845 bytenr = btrfs_sb_offset(i);
846 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
849 btrfs_set_super_bytenr(sb, bytenr);
852 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
853 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
854 btrfs_csum_final(crc, (char *)&sb->csum[0]);
856 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
857 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
862 int write_all_supers(struct btrfs_root *root)
864 struct list_head *cur;
865 struct list_head *head = &root->fs_info->fs_devices->devices;
866 struct btrfs_device *dev;
867 struct btrfs_super_block *sb;
868 struct btrfs_dev_item *dev_item;
872 sb = &root->fs_info->super_copy;
873 dev_item = &sb->dev_item;
874 list_for_each(cur, head) {
875 dev = list_entry(cur, struct btrfs_device, dev_list);
879 btrfs_set_stack_device_generation(dev_item, 0);
880 btrfs_set_stack_device_type(dev_item, dev->type);
881 btrfs_set_stack_device_id(dev_item, dev->devid);
882 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
883 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
884 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
885 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
886 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
887 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
888 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
890 flags = btrfs_super_flags(sb);
891 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
893 ret = write_dev_supers(root, sb, dev);
899 int write_ctree_super(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root)
903 struct btrfs_root *tree_root = root->fs_info->tree_root;
904 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
906 if (root->fs_info->readonly)
909 btrfs_set_super_generation(&root->fs_info->super_copy,
911 btrfs_set_super_root(&root->fs_info->super_copy,
912 tree_root->node->start);
913 btrfs_set_super_root_level(&root->fs_info->super_copy,
914 btrfs_header_level(tree_root->node));
915 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
916 chunk_root->node->start);
917 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
918 btrfs_header_level(chunk_root->node));
919 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
920 btrfs_header_generation(chunk_root->node));
922 ret = write_all_supers(root);
924 fprintf(stderr, "failed to write new super block err %d\n", ret);
928 static int close_all_devices(struct btrfs_fs_info *fs_info)
930 struct list_head *list;
931 struct list_head *next;
932 struct btrfs_device *device;
936 list = &fs_info->fs_devices->devices;
937 list_for_each(next, list) {
938 device = list_entry(next, struct btrfs_device, dev_list);
944 int close_ctree(struct btrfs_root *root)
947 struct btrfs_trans_handle *trans;
948 struct btrfs_fs_info *fs_info = root->fs_info;
950 trans = btrfs_start_transaction(root, 1);
951 btrfs_commit_transaction(trans, root);
952 trans = btrfs_start_transaction(root, 1);
953 ret = commit_tree_roots(trans, fs_info);
955 ret = __commit_transaction(trans, root);
957 write_ctree_super(trans, root);
958 btrfs_free_transaction(root, trans);
959 btrfs_free_block_groups(fs_info);
961 free_fs_roots(fs_info);
963 if (fs_info->extent_root->node)
964 free_extent_buffer(fs_info->extent_root->node);
965 if (fs_info->tree_root->node)
966 free_extent_buffer(fs_info->tree_root->node);
967 if (fs_info->chunk_root->node)
968 free_extent_buffer(fs_info->chunk_root->node);
969 if (fs_info->dev_root->node)
970 free_extent_buffer(fs_info->dev_root->node);
971 if (fs_info->csum_root->node)
972 free_extent_buffer(fs_info->csum_root->node);
974 if (fs_info->log_root_tree) {
975 if (fs_info->log_root_tree->node)
976 free_extent_buffer(fs_info->log_root_tree->node);
977 free(fs_info->log_root_tree);
980 close_all_devices(fs_info);
981 extent_io_tree_cleanup(&fs_info->extent_cache);
982 extent_io_tree_cleanup(&fs_info->free_space_cache);
983 extent_io_tree_cleanup(&fs_info->block_group_cache);
984 extent_io_tree_cleanup(&fs_info->pinned_extents);
985 extent_io_tree_cleanup(&fs_info->pending_del);
986 extent_io_tree_cleanup(&fs_info->extent_ins);
988 free(fs_info->tree_root);
989 free(fs_info->extent_root);
990 free(fs_info->chunk_root);
991 free(fs_info->dev_root);
992 free(fs_info->csum_root);
998 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
999 struct extent_buffer *eb)
1001 return clear_extent_buffer_dirty(eb);
1004 int wait_on_tree_block_writeback(struct btrfs_root *root,
1005 struct extent_buffer *eb)
1010 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1012 set_extent_buffer_dirty(eb);
1015 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1019 ret = extent_buffer_uptodate(buf);
1023 ret = verify_parent_transid(buf->tree, buf, parent_transid);
1027 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1029 return set_extent_buffer_uptodate(eb);