btrfs-debug-tree: add -d option to print only the device mapping
[btrfs-progs-unstable.git] / disk-io.c
bloba6e1000d11d51245f2571023f72a2649200b4b99
1 /*
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
20 #define __USE_XOPEN2K
21 #define _GNU_SOURCE 1
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "kerncompat.h"
29 #include "radix-tree.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "volumes.h"
33 #include "transaction.h"
34 #include "crc32c.h"
35 #include "utils.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;
42 int ret = 1;
44 if (buf->start != btrfs_header_bytenr(buf))
45 return ret;
47 fs_devices = root->fs_info->fs_devices;
48 while (fs_devices) {
49 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
50 (unsigned long)btrfs_header_fsid(buf),
51 BTRFS_FSID_SIZE)) {
52 ret = 0;
53 break;
55 fs_devices = fs_devices->seed;
57 return ret;
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,
71 int verify)
73 char *result;
74 u32 len;
75 u32 crc = ~(u32)0;
77 result = malloc(csum_size * sizeof(char));
78 if (!result)
79 return 1;
81 len = buf->len - BTRFS_CSUM_SIZE;
82 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
83 btrfs_csum_final(crc, result);
85 if (verify) {
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));
90 free(result);
91 return 1;
93 } else {
94 write_extent_buffer(buf, result, 0, csum_size);
96 free(result);
97 return 0;
100 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
101 int verify)
103 u16 csum_size =
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,
112 bytenr, blocksize);
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,
119 blocksize);
122 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
123 u64 parent_transid)
125 int ret;
126 int dev_nr;
127 struct extent_buffer *eb;
128 u64 length;
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);
135 return 0;
138 dev_nr = 0;
139 length = blocksize;
140 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
141 bytenr, &length, &multi, 0);
142 BUG_ON(ret);
143 device = multi->stripes[0].dev;
144 device->total_ios++;
145 blocksize = min(blocksize, (u32)(64 * 1024));
146 readahead(device->fd, multi->stripes[0].physical, blocksize);
147 kfree(multi);
148 return 0;
151 static int verify_parent_transid(struct extent_io_tree *io_tree,
152 struct extent_buffer *eb, u64 parent_transid)
154 int ret;
156 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
157 return 0;
159 if (extent_buffer_uptodate(eb) &&
160 btrfs_header_generation(eb) == parent_transid) {
161 ret = 0;
162 goto out;
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));
168 ret = 1;
169 out:
170 clear_extent_buffer_uptodate(io_tree, eb);
171 return ret;
176 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
177 u32 blocksize, u64 parent_transid)
179 int ret;
180 int dev_nr;
181 struct extent_buffer *eb;
182 u64 length;
183 struct btrfs_multi_bio *multi = NULL;
184 struct btrfs_device *device;
185 int mirror_num = 0;
186 int num_copies;
188 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
189 if (!eb)
190 return NULL;
192 if (btrfs_buffer_uptodate(eb, parent_transid))
193 return eb;
195 dev_nr = 0;
196 length = blocksize;
197 while (1) {
198 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
199 eb->start, &length, &multi, mirror_num);
200 BUG_ON(ret);
201 device = multi->stripes[0].dev;
202 eb->fd = device->fd;
203 device->total_ios++;
204 eb->dev_bytenr = multi->stripes[0].physical;
205 kfree(multi);
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);
211 return eb;
213 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
214 eb->start, eb->len);
215 if (num_copies == 1) {
216 break;
218 mirror_num++;
219 if (mirror_num > num_copies) {
220 break;
223 free_extent_buffer(eb);
224 return NULL;
227 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
228 struct extent_buffer *eb)
230 int ret;
231 int dev_nr;
232 u64 length;
233 struct btrfs_multi_bio *multi = NULL;
235 if (check_tree_block(root, eb))
236 BUG();
237 if (!btrfs_buffer_uptodate(eb, trans->transid))
238 BUG();
240 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
241 csum_tree_block(root, eb, 0);
243 dev_nr = 0;
244 length = eb->len;
245 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
246 eb->start, &length, &multi, 0);
248 while(dev_nr < multi->num_stripes) {
249 BUG_ON(ret);
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++;
253 dev_nr++;
254 ret = write_extent_to_disk(eb);
255 BUG_ON(ret);
257 kfree(multi);
258 return 0;
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)
265 root->node = NULL;
266 root->commit_root = NULL;
267 root->sectorsize = sectorsize;
268 root->nodesize = nodesize;
269 root->leafsize = leafsize;
270 root->stripesize = stripesize;
271 root->ref_cows = 0;
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;
284 return 0;
287 static int update_cowonly_root(struct btrfs_trans_handle *trans,
288 struct btrfs_root *root)
290 int ret;
291 u64 old_root_bytenr;
292 struct btrfs_root *tree_root = root->fs_info->tree_root;
294 btrfs_write_dirty_block_groups(trans, root);
295 while(1) {
296 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
297 if (old_root_bytenr == root->node->start)
298 break;
299 btrfs_set_root_bytenr(&root->root_item,
300 root->node->start);
301 btrfs_set_root_generation(&root->root_item,
302 trans->transid);
303 root->root_item.level = btrfs_header_level(root->node);
304 ret = btrfs_update_root(trans, tree_root,
305 &root->root_key,
306 &root->root_item);
307 BUG_ON(ret);
308 btrfs_write_dirty_block_groups(trans, root);
310 return 0;
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)
321 return 0;
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;
330 list_del_init(next);
331 root = list_entry(next, struct btrfs_root, dirty_list);
332 update_cowonly_root(trans, root);
334 return 0;
337 static int __commit_transaction(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root)
340 u64 start;
341 u64 end;
342 struct extent_buffer *eb;
343 struct extent_io_tree *tree = &root->fs_info->extent_cache;
344 int ret;
346 while(1) {
347 ret = find_first_extent_bit(tree, 0, &start, &end,
348 EXTENT_DIRTY);
349 if (ret)
350 break;
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);
355 BUG_ON(ret);
356 start += eb->len;
357 clear_extent_buffer_dirty(eb);
358 free_extent_buffer(eb);
361 return 0;
364 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
365 struct btrfs_root *root)
367 int ret = 0;
368 struct btrfs_fs_info *fs_info = root->fs_info;
370 if (root->commit_root == root->node)
371 goto commit_tree;
373 free_extent_buffer(root->commit_root);
374 root->commit_root = NULL;
376 btrfs_set_root_bytenr(&root->root_item, root->node->start);
377 btrfs_set_root_generation(&root->root_item, trans->transid);
378 root->root_item.level = btrfs_header_level(root->node);
379 ret = btrfs_update_root(trans, root->fs_info->tree_root,
380 &root->root_key, &root->root_item);
381 BUG_ON(ret);
382 commit_tree:
383 ret = commit_tree_roots(trans, fs_info);
384 BUG_ON(ret);
385 ret = __commit_transaction(trans, root);
386 BUG_ON(ret);
387 write_ctree_super(trans, root);
388 btrfs_finish_extent_commit(trans, fs_info->extent_root,
389 &fs_info->pinned_extents);
390 btrfs_free_transaction(root, trans);
391 free_extent_buffer(root->commit_root);
392 root->commit_root = NULL;
393 fs_info->running_transaction = NULL;
394 return 0;
397 static int find_and_setup_root(struct btrfs_root *tree_root,
398 struct btrfs_fs_info *fs_info,
399 u64 objectid, struct btrfs_root *root)
401 int ret;
402 u32 blocksize;
403 u64 generation;
405 __setup_root(tree_root->nodesize, tree_root->leafsize,
406 tree_root->sectorsize, tree_root->stripesize,
407 root, fs_info, objectid);
408 ret = btrfs_find_last_root(tree_root, objectid,
409 &root->root_item, &root->root_key);
410 BUG_ON(ret);
412 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
413 generation = btrfs_root_generation(&root->root_item);
414 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
415 blocksize, generation);
416 BUG_ON(!root->node);
417 return 0;
420 static int find_and_setup_log_root(struct btrfs_root *tree_root,
421 struct btrfs_fs_info *fs_info,
422 struct btrfs_super_block *disk_super)
424 u32 blocksize;
425 u64 blocknr = btrfs_super_log_root(disk_super);
426 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
428 if (blocknr == 0)
429 return 0;
431 blocksize = btrfs_level_size(tree_root,
432 btrfs_super_log_root_level(disk_super));
434 __setup_root(tree_root->nodesize, tree_root->leafsize,
435 tree_root->sectorsize, tree_root->stripesize,
436 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
438 log_root->node = read_tree_block(tree_root, blocknr,
439 blocksize,
440 btrfs_super_generation(disk_super) + 1);
442 fs_info->log_root_tree = log_root;
443 BUG_ON(!log_root->node);
444 return 0;
448 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
449 struct btrfs_root *root)
451 if (root->node)
452 free_extent_buffer(root->node);
453 if (root->commit_root)
454 free_extent_buffer(root->commit_root);
455 kfree(root);
456 return 0;
459 static int free_fs_roots(struct btrfs_fs_info *fs_info)
461 struct cache_extent *cache;
462 struct btrfs_root *root;
464 while (1) {
465 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
466 if (!cache)
467 break;
468 root = container_of(cache, struct btrfs_root, cache);
469 remove_cache_extent(&fs_info->fs_root_cache, cache);
470 btrfs_free_fs_root(fs_info, root);
472 return 0;
475 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
476 struct btrfs_key *location)
478 struct btrfs_root *root;
479 struct btrfs_root *tree_root = fs_info->tree_root;
480 struct btrfs_path *path;
481 struct extent_buffer *l;
482 u64 generation;
483 u32 blocksize;
484 int ret = 0;
486 root = malloc(sizeof(*root));
487 if (!root)
488 return ERR_PTR(-ENOMEM);
489 memset(root, 0, sizeof(*root));
490 if (location->offset == (u64)-1) {
491 ret = find_and_setup_root(tree_root, fs_info,
492 location->objectid, root);
493 if (ret) {
494 free(root);
495 return ERR_PTR(ret);
497 goto insert;
500 __setup_root(tree_root->nodesize, tree_root->leafsize,
501 tree_root->sectorsize, tree_root->stripesize,
502 root, fs_info, location->objectid);
504 path = btrfs_alloc_path();
505 BUG_ON(!path);
506 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
507 if (ret != 0) {
508 if (ret > 0)
509 ret = -ENOENT;
510 goto out;
512 l = path->nodes[0];
513 read_extent_buffer(l, &root->root_item,
514 btrfs_item_ptr_offset(l, path->slots[0]),
515 sizeof(root->root_item));
516 memcpy(&root->root_key, location, sizeof(*location));
517 ret = 0;
518 out:
519 btrfs_release_path(root, path);
520 btrfs_free_path(path);
521 if (ret) {
522 free(root);
523 return ERR_PTR(ret);
525 generation = btrfs_root_generation(&root->root_item);
526 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
527 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
528 blocksize, generation);
529 BUG_ON(!root->node);
530 insert:
531 root->ref_cows = 1;
532 return root;
535 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
536 struct btrfs_key *location)
538 struct btrfs_root *root;
539 struct cache_extent *cache;
540 int ret;
542 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
543 return fs_info->tree_root;
544 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
545 return fs_info->extent_root;
546 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
547 return fs_info->chunk_root;
548 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
549 return fs_info->dev_root;
550 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
551 return fs_info->csum_root;
553 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
554 location->offset != (u64)-1);
556 cache = find_cache_extent(&fs_info->fs_root_cache,
557 location->objectid, 1);
558 if (cache)
559 return container_of(cache, struct btrfs_root, cache);
561 root = btrfs_read_fs_root_no_cache(fs_info, location);
562 if (IS_ERR(root))
563 return root;
565 root->cache.start = location->objectid;
566 root->cache.size = 1;
567 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
568 &root->cache);
569 BUG_ON(ret);
570 return root;
573 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
575 int fp;
576 struct btrfs_root *root;
577 int flags = O_CREAT | O_RDWR;
579 if (!writes)
580 flags = O_RDONLY;
582 fp = open(filename, flags, 0600);
583 if (fp < 0) {
584 fprintf (stderr, "Could not open %s\n", filename);
585 return NULL;
587 root = open_ctree_fd(fp, filename, sb_bytenr, writes);
588 close(fp);
590 return root;
593 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
594 int writes)
596 u32 sectorsize;
597 u32 nodesize;
598 u32 leafsize;
599 u32 blocksize;
600 u32 stripesize;
601 u64 generation;
602 struct btrfs_key key;
603 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
604 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
605 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
606 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
607 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
608 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
609 int ret;
610 struct btrfs_super_block *disk_super;
611 struct btrfs_fs_devices *fs_devices = NULL;
612 u64 total_devs;
613 u64 features;
615 if (sb_bytenr == 0)
616 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
618 ret = btrfs_scan_one_device(fp, path, &fs_devices,
619 &total_devs, sb_bytenr);
621 if (ret) {
622 fprintf(stderr, "No valid Btrfs found on %s\n", path);
623 return NULL;
626 if (total_devs != 1) {
627 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
628 BUG_ON(ret);
631 memset(fs_info, 0, sizeof(*fs_info));
632 fs_info->tree_root = tree_root;
633 fs_info->extent_root = extent_root;
634 fs_info->chunk_root = chunk_root;
635 fs_info->dev_root = dev_root;
636 fs_info->csum_root = csum_root;
638 if (!writes)
639 fs_info->readonly = 1;
641 extent_io_tree_init(&fs_info->extent_cache);
642 extent_io_tree_init(&fs_info->free_space_cache);
643 extent_io_tree_init(&fs_info->block_group_cache);
644 extent_io_tree_init(&fs_info->pinned_extents);
645 extent_io_tree_init(&fs_info->pending_del);
646 extent_io_tree_init(&fs_info->extent_ins);
647 cache_tree_init(&fs_info->fs_root_cache);
649 cache_tree_init(&fs_info->mapping_tree.cache_tree);
651 mutex_init(&fs_info->fs_mutex);
652 fs_info->fs_devices = fs_devices;
653 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
654 INIT_LIST_HEAD(&fs_info->space_info);
656 __setup_root(4096, 4096, 4096, 4096, tree_root,
657 fs_info, BTRFS_ROOT_TREE_OBJECTID);
659 if (writes)
660 ret = btrfs_open_devices(fs_devices, O_RDWR);
661 else
662 ret = btrfs_open_devices(fs_devices, O_RDONLY);
663 BUG_ON(ret);
665 fs_info->super_bytenr = sb_bytenr;
666 disk_super = &fs_info->super_copy;
667 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
668 disk_super, sb_bytenr);
669 if (ret) {
670 printk("No valid btrfs found\n");
671 BUG_ON(1);
674 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
677 features = btrfs_super_incompat_flags(disk_super) &
678 ~BTRFS_FEATURE_INCOMPAT_SUPP;
679 if (features) {
680 printk("couldn't open because of unsupported "
681 "option features (%Lx).\n", features);
682 BUG_ON(1);
685 features = btrfs_super_incompat_flags(disk_super);
686 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
687 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
688 btrfs_set_super_incompat_flags(disk_super, features);
691 features = btrfs_super_compat_ro_flags(disk_super) &
692 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
693 if (writes && features) {
694 printk("couldn't open RDWR because of unsupported "
695 "option features (%Lx).\n", features);
696 BUG_ON(1);
699 nodesize = btrfs_super_nodesize(disk_super);
700 leafsize = btrfs_super_leafsize(disk_super);
701 sectorsize = btrfs_super_sectorsize(disk_super);
702 stripesize = btrfs_super_stripesize(disk_super);
703 tree_root->nodesize = nodesize;
704 tree_root->leafsize = leafsize;
705 tree_root->sectorsize = sectorsize;
706 tree_root->stripesize = stripesize;
708 ret = btrfs_read_sys_array(tree_root);
709 BUG_ON(ret);
710 blocksize = btrfs_level_size(tree_root,
711 btrfs_super_chunk_root_level(disk_super));
712 generation = btrfs_super_chunk_root_generation(disk_super);
714 __setup_root(nodesize, leafsize, sectorsize, stripesize,
715 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
717 chunk_root->node = read_tree_block(chunk_root,
718 btrfs_super_chunk_root(disk_super),
719 blocksize, generation);
721 BUG_ON(!chunk_root->node);
723 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
724 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
725 BTRFS_UUID_SIZE);
727 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
728 ret = btrfs_read_chunk_tree(chunk_root);
729 BUG_ON(ret);
732 blocksize = btrfs_level_size(tree_root,
733 btrfs_super_root_level(disk_super));
734 generation = btrfs_super_generation(disk_super);
736 tree_root->node = read_tree_block(tree_root,
737 btrfs_super_root(disk_super),
738 blocksize, generation);
739 BUG_ON(!tree_root->node);
740 ret = find_and_setup_root(tree_root, fs_info,
741 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
742 BUG_ON(ret);
743 extent_root->track_dirty = 1;
745 ret = find_and_setup_root(tree_root, fs_info,
746 BTRFS_DEV_TREE_OBJECTID, dev_root);
747 BUG_ON(ret);
748 dev_root->track_dirty = 1;
750 ret = find_and_setup_root(tree_root, fs_info,
751 BTRFS_CSUM_TREE_OBJECTID, csum_root);
752 BUG_ON(ret);
753 csum_root->track_dirty = 1;
755 BUG_ON(ret);
757 find_and_setup_log_root(tree_root, fs_info, disk_super);
759 fs_info->generation = generation + 1;
760 btrfs_read_block_groups(fs_info->tree_root);
762 key.objectid = BTRFS_FS_TREE_OBJECTID;
763 key.type = BTRFS_ROOT_ITEM_KEY;
764 key.offset = (u64)-1;
765 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
767 fs_info->data_alloc_profile = (u64)-1;
768 fs_info->metadata_alloc_profile = (u64)-1;
769 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
771 return fs_info->fs_root;
774 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
776 u8 fsid[BTRFS_FSID_SIZE];
777 struct btrfs_super_block buf;
778 int i;
779 int ret;
780 u64 transid = 0;
781 u64 bytenr;
783 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
784 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
785 if (ret < sizeof(buf))
786 return -1;
788 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
789 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
790 sizeof(buf.magic)))
791 return -1;
793 memcpy(sb, &buf, sizeof(*sb));
794 return 0;
797 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
798 bytenr = btrfs_sb_offset(i);
799 ret = pread64(fd, &buf, sizeof(buf), bytenr);
800 if (ret < sizeof(buf))
801 break;
803 if (btrfs_super_bytenr(&buf) != bytenr ||
804 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
805 sizeof(buf.magic)))
806 continue;
808 if (i == 0)
809 memcpy(fsid, buf.fsid, sizeof(fsid));
810 else if (memcmp(fsid, buf.fsid, sizeof(fsid)))
811 continue;
813 if (btrfs_super_generation(&buf) > transid) {
814 memcpy(sb, &buf, sizeof(*sb));
815 transid = btrfs_super_generation(&buf);
819 return transid > 0 ? 0 : -1;
822 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
823 struct btrfs_device *device)
825 u64 bytenr;
826 u32 crc;
827 int i, ret;
829 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
830 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
832 crc = ~(u32)0;
833 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
834 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
835 btrfs_csum_final(crc, (char *)&sb->csum[0]);
837 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
838 root->fs_info->super_bytenr);
839 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
840 return 0;
843 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
844 bytenr = btrfs_sb_offset(i);
845 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
846 break;
848 btrfs_set_super_bytenr(sb, bytenr);
850 crc = ~(u32)0;
851 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
852 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
853 btrfs_csum_final(crc, (char *)&sb->csum[0]);
855 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
856 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
858 return 0;
861 int write_all_supers(struct btrfs_root *root)
863 struct list_head *cur;
864 struct list_head *head = &root->fs_info->fs_devices->devices;
865 struct btrfs_device *dev;
866 struct btrfs_super_block *sb;
867 struct btrfs_dev_item *dev_item;
868 int ret;
869 u64 flags;
871 sb = &root->fs_info->super_copy;
872 dev_item = &sb->dev_item;
873 list_for_each(cur, head) {
874 dev = list_entry(cur, struct btrfs_device, dev_list);
875 if (!dev->writeable)
876 continue;
878 btrfs_set_stack_device_generation(dev_item, 0);
879 btrfs_set_stack_device_type(dev_item, dev->type);
880 btrfs_set_stack_device_id(dev_item, dev->devid);
881 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
882 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
883 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
884 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
885 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
886 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
887 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
889 flags = btrfs_super_flags(sb);
890 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
892 ret = write_dev_supers(root, sb, dev);
893 BUG_ON(ret);
895 return 0;
898 int write_ctree_super(struct btrfs_trans_handle *trans,
899 struct btrfs_root *root)
901 int ret;
902 struct btrfs_root *tree_root = root->fs_info->tree_root;
903 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
905 if (root->fs_info->readonly)
906 return 0;
908 btrfs_set_super_generation(&root->fs_info->super_copy,
909 trans->transid);
910 btrfs_set_super_root(&root->fs_info->super_copy,
911 tree_root->node->start);
912 btrfs_set_super_root_level(&root->fs_info->super_copy,
913 btrfs_header_level(tree_root->node));
914 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
915 chunk_root->node->start);
916 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
917 btrfs_header_level(chunk_root->node));
918 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
919 btrfs_header_generation(chunk_root->node));
921 ret = write_all_supers(root);
922 if (ret)
923 fprintf(stderr, "failed to write new super block err %d\n", ret);
924 return ret;
927 static int close_all_devices(struct btrfs_fs_info *fs_info)
929 struct list_head *list;
930 struct list_head *next;
931 struct btrfs_device *device;
933 return 0;
935 list = &fs_info->fs_devices->devices;
936 list_for_each(next, list) {
937 device = list_entry(next, struct btrfs_device, dev_list);
938 close(device->fd);
940 return 0;
943 int close_ctree(struct btrfs_root *root)
945 int ret;
946 struct btrfs_trans_handle *trans;
947 struct btrfs_fs_info *fs_info = root->fs_info;
949 trans = btrfs_start_transaction(root, 1);
950 btrfs_commit_transaction(trans, root);
951 trans = btrfs_start_transaction(root, 1);
952 ret = commit_tree_roots(trans, fs_info);
953 BUG_ON(ret);
954 ret = __commit_transaction(trans, root);
955 BUG_ON(ret);
956 write_ctree_super(trans, root);
957 btrfs_free_transaction(root, trans);
958 btrfs_free_block_groups(fs_info);
960 free_fs_roots(fs_info);
962 if (fs_info->extent_root->node)
963 free_extent_buffer(fs_info->extent_root->node);
964 if (fs_info->tree_root->node)
965 free_extent_buffer(fs_info->tree_root->node);
966 if (fs_info->chunk_root->node)
967 free_extent_buffer(fs_info->chunk_root->node);
968 if (fs_info->dev_root->node)
969 free_extent_buffer(fs_info->dev_root->node);
970 if (fs_info->csum_root->node)
971 free_extent_buffer(fs_info->csum_root->node);
973 if (fs_info->log_root_tree) {
974 if (fs_info->log_root_tree->node)
975 free_extent_buffer(fs_info->log_root_tree->node);
976 free(fs_info->log_root_tree);
979 close_all_devices(fs_info);
980 extent_io_tree_cleanup(&fs_info->extent_cache);
981 extent_io_tree_cleanup(&fs_info->free_space_cache);
982 extent_io_tree_cleanup(&fs_info->block_group_cache);
983 extent_io_tree_cleanup(&fs_info->pinned_extents);
984 extent_io_tree_cleanup(&fs_info->pending_del);
985 extent_io_tree_cleanup(&fs_info->extent_ins);
987 free(fs_info->tree_root);
988 free(fs_info->extent_root);
989 free(fs_info->chunk_root);
990 free(fs_info->dev_root);
991 free(fs_info->csum_root);
992 free(fs_info);
994 return 0;
997 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
998 struct extent_buffer *eb)
1000 return clear_extent_buffer_dirty(eb);
1003 int wait_on_tree_block_writeback(struct btrfs_root *root,
1004 struct extent_buffer *eb)
1006 return 0;
1009 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1011 set_extent_buffer_dirty(eb);
1014 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1016 int ret;
1018 ret = extent_buffer_uptodate(buf);
1019 if (!ret)
1020 return ret;
1022 ret = verify_parent_transid(buf->tree, buf, parent_transid);
1023 return !ret;
1026 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1028 return set_extent_buffer_uptodate(eb);