btrfs-progs: mkfs: rename nodiscard option to K
[btrfs-progs-unstable/devel.git] / disk-io.c
blobb21a87f827a6250da45f2fb6a1c3a6b651062243
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 close_all_devices(struct btrfs_fs_info *fs_info);
40 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
43 struct btrfs_fs_devices *fs_devices;
44 int ret = 1;
46 if (buf->start != btrfs_header_bytenr(buf)) {
47 printk("Check tree block failed, want=%Lu, have=%Lu\n",
48 buf->start, btrfs_header_bytenr(buf));
49 return ret;
52 fs_devices = root->fs_info->fs_devices;
53 while (fs_devices) {
54 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
55 (unsigned long)btrfs_header_fsid(buf),
56 BTRFS_FSID_SIZE)) {
57 ret = 0;
58 break;
60 fs_devices = fs_devices->seed;
62 return ret;
65 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
67 return crc32c(seed, data, len);
70 void btrfs_csum_final(u32 crc, char *result)
72 *(__le32 *)result = ~cpu_to_le32(crc);
75 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
76 int verify)
78 char *result;
79 u32 len;
80 u32 crc = ~(u32)0;
82 result = malloc(csum_size * sizeof(char));
83 if (!result)
84 return 1;
86 len = buf->len - BTRFS_CSUM_SIZE;
87 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
88 btrfs_csum_final(crc, result);
90 if (verify) {
91 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
92 printk("checksum verify failed on %llu wanted %X "
93 "found %X\n", (unsigned long long)buf->start,
94 *((int *)result), *((char *)buf->data));
95 free(result);
96 return 1;
98 } else {
99 write_extent_buffer(buf, result, 0, csum_size);
101 free(result);
102 return 0;
105 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
106 int verify)
108 u16 csum_size =
109 btrfs_super_csum_size(&root->fs_info->super_copy);
110 return csum_tree_block_size(buf, csum_size, verify);
113 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
114 u64 bytenr, u32 blocksize)
116 return find_extent_buffer(&root->fs_info->extent_cache,
117 bytenr, blocksize);
120 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
121 u64 bytenr, u32 blocksize)
123 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
124 blocksize);
127 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
128 u64 parent_transid)
130 int ret;
131 struct extent_buffer *eb;
132 u64 length;
133 struct btrfs_multi_bio *multi = NULL;
134 struct btrfs_device *device;
136 eb = btrfs_find_tree_block(root, bytenr, blocksize);
137 if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
138 free_extent_buffer(eb);
139 return 0;
142 length = blocksize;
143 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
144 bytenr, &length, &multi, 0);
145 BUG_ON(ret);
146 device = multi->stripes[0].dev;
147 device->total_ios++;
148 blocksize = min(blocksize, (u32)(64 * 1024));
149 readahead(device->fd, multi->stripes[0].physical, blocksize);
150 kfree(multi);
151 return 0;
154 static int verify_parent_transid(struct extent_io_tree *io_tree,
155 struct extent_buffer *eb, u64 parent_transid,
156 int ignore)
158 int ret;
160 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
161 return 0;
163 if (extent_buffer_uptodate(eb) &&
164 btrfs_header_generation(eb) == parent_transid) {
165 ret = 0;
166 goto out;
168 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
169 (unsigned long long)eb->start,
170 (unsigned long long)parent_transid,
171 (unsigned long long)btrfs_header_generation(eb));
172 if (ignore) {
173 printk("Ignoring transid failure\n");
174 return 0;
177 ret = 1;
178 out:
179 clear_extent_buffer_uptodate(io_tree, eb);
180 return ret;
185 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
186 u32 blocksize, u64 parent_transid)
188 int ret;
189 struct extent_buffer *eb;
190 u64 length;
191 u64 best_transid = 0;
192 struct btrfs_multi_bio *multi = NULL;
193 struct btrfs_device *device;
194 int mirror_num = 0;
195 int good_mirror = 0;
196 int num_copies;
197 int ignore = 0;
199 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
200 if (!eb)
201 return NULL;
203 if (btrfs_buffer_uptodate(eb, parent_transid))
204 return eb;
206 length = blocksize;
207 while (1) {
208 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
209 eb->start, &length, &multi, mirror_num);
210 if (ret) {
211 printk("Couldn't map the block %Lu\n", bytenr);
212 break;
214 device = multi->stripes[0].dev;
215 eb->fd = device->fd;
216 device->total_ios++;
217 eb->dev_bytenr = multi->stripes[0].physical;
218 kfree(multi);
219 ret = read_extent_from_disk(eb);
221 if (ret == 0 && check_tree_block(root, eb) == 0 &&
222 csum_tree_block(root, eb, 1) == 0 &&
223 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
224 == 0) {
225 btrfs_set_buffer_uptodate(eb);
226 return eb;
228 if (ignore) {
229 if (check_tree_block(root, eb))
230 printk("read block failed check_tree_block\n");
231 else
232 printk("Csum didn't match\n");
233 break;
235 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
236 eb->start, eb->len);
237 if (num_copies == 1) {
238 ignore = 1;
239 continue;
241 if (btrfs_header_generation(eb) > best_transid) {
242 best_transid = btrfs_header_generation(eb);
243 good_mirror = mirror_num;
245 mirror_num++;
246 if (mirror_num > num_copies) {
247 mirror_num = good_mirror;
248 ignore = 1;
249 continue;
252 free_extent_buffer(eb);
253 return NULL;
256 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
257 struct extent_buffer *eb)
259 int ret;
260 int dev_nr;
261 u64 length;
262 struct btrfs_multi_bio *multi = NULL;
264 if (check_tree_block(root, eb))
265 BUG();
266 if (!btrfs_buffer_uptodate(eb, trans->transid))
267 BUG();
269 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
270 csum_tree_block(root, eb, 0);
272 dev_nr = 0;
273 length = eb->len;
274 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
275 eb->start, &length, &multi, 0);
277 while(dev_nr < multi->num_stripes) {
278 BUG_ON(ret);
279 eb->fd = multi->stripes[dev_nr].dev->fd;
280 eb->dev_bytenr = multi->stripes[dev_nr].physical;
281 multi->stripes[dev_nr].dev->total_ios++;
282 dev_nr++;
283 ret = write_extent_to_disk(eb);
284 BUG_ON(ret);
286 kfree(multi);
287 return 0;
290 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
291 u32 stripesize, struct btrfs_root *root,
292 struct btrfs_fs_info *fs_info, u64 objectid)
294 root->node = NULL;
295 root->commit_root = NULL;
296 root->sectorsize = sectorsize;
297 root->nodesize = nodesize;
298 root->leafsize = leafsize;
299 root->stripesize = stripesize;
300 root->ref_cows = 0;
301 root->track_dirty = 0;
303 root->fs_info = fs_info;
304 root->objectid = objectid;
305 root->last_trans = 0;
306 root->highest_inode = 0;
307 root->last_inode_alloc = 0;
309 INIT_LIST_HEAD(&root->dirty_list);
310 memset(&root->root_key, 0, sizeof(root->root_key));
311 memset(&root->root_item, 0, sizeof(root->root_item));
312 root->root_key.objectid = objectid;
313 return 0;
316 static int update_cowonly_root(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root)
319 int ret;
320 u64 old_root_bytenr;
321 struct btrfs_root *tree_root = root->fs_info->tree_root;
323 btrfs_write_dirty_block_groups(trans, root);
324 while(1) {
325 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
326 if (old_root_bytenr == root->node->start)
327 break;
328 btrfs_set_root_bytenr(&root->root_item,
329 root->node->start);
330 btrfs_set_root_generation(&root->root_item,
331 trans->transid);
332 root->root_item.level = btrfs_header_level(root->node);
333 ret = btrfs_update_root(trans, tree_root,
334 &root->root_key,
335 &root->root_item);
336 BUG_ON(ret);
337 btrfs_write_dirty_block_groups(trans, root);
339 return 0;
342 static int commit_tree_roots(struct btrfs_trans_handle *trans,
343 struct btrfs_fs_info *fs_info)
345 struct btrfs_root *root;
346 struct list_head *next;
347 struct extent_buffer *eb;
349 if (fs_info->readonly)
350 return 0;
352 eb = fs_info->tree_root->node;
353 extent_buffer_get(eb);
354 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
355 free_extent_buffer(eb);
357 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
358 next = fs_info->dirty_cowonly_roots.next;
359 list_del_init(next);
360 root = list_entry(next, struct btrfs_root, dirty_list);
361 update_cowonly_root(trans, root);
363 return 0;
366 static int __commit_transaction(struct btrfs_trans_handle *trans,
367 struct btrfs_root *root)
369 u64 start;
370 u64 end;
371 struct extent_buffer *eb;
372 struct extent_io_tree *tree = &root->fs_info->extent_cache;
373 int ret;
375 while(1) {
376 ret = find_first_extent_bit(tree, 0, &start, &end,
377 EXTENT_DIRTY);
378 if (ret)
379 break;
380 while(start <= end) {
381 eb = find_first_extent_buffer(tree, start);
382 BUG_ON(!eb || eb->start != start);
383 ret = write_tree_block(trans, root, eb);
384 BUG_ON(ret);
385 start += eb->len;
386 clear_extent_buffer_dirty(eb);
387 free_extent_buffer(eb);
390 return 0;
393 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root)
396 u64 transid = trans->transid;
397 int ret = 0;
398 struct btrfs_fs_info *fs_info = root->fs_info;
400 if (root->commit_root == root->node)
401 goto commit_tree;
403 free_extent_buffer(root->commit_root);
404 root->commit_root = NULL;
406 btrfs_set_root_bytenr(&root->root_item, root->node->start);
407 btrfs_set_root_generation(&root->root_item, trans->transid);
408 root->root_item.level = btrfs_header_level(root->node);
409 ret = btrfs_update_root(trans, root->fs_info->tree_root,
410 &root->root_key, &root->root_item);
411 BUG_ON(ret);
412 commit_tree:
413 ret = commit_tree_roots(trans, fs_info);
414 BUG_ON(ret);
415 ret = __commit_transaction(trans, root);
416 BUG_ON(ret);
417 write_ctree_super(trans, root);
418 btrfs_finish_extent_commit(trans, fs_info->extent_root,
419 &fs_info->pinned_extents);
420 btrfs_free_transaction(root, trans);
421 free_extent_buffer(root->commit_root);
422 root->commit_root = NULL;
423 fs_info->running_transaction = NULL;
424 fs_info->last_trans_committed = transid;
425 return 0;
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)
432 int ret;
433 u32 blocksize;
434 u64 generation;
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);
441 BUG_ON(ret);
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);
447 if (!extent_buffer_uptodate(root->node))
448 return -EIO;
450 return 0;
453 static int find_and_setup_log_root(struct btrfs_root *tree_root,
454 struct btrfs_fs_info *fs_info,
455 struct btrfs_super_block *disk_super)
457 u32 blocksize;
458 u64 blocknr = btrfs_super_log_root(disk_super);
459 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
461 if (blocknr == 0)
462 return 0;
464 blocksize = btrfs_level_size(tree_root,
465 btrfs_super_log_root_level(disk_super));
467 __setup_root(tree_root->nodesize, tree_root->leafsize,
468 tree_root->sectorsize, tree_root->stripesize,
469 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
471 log_root->node = read_tree_block(tree_root, blocknr,
472 blocksize,
473 btrfs_super_generation(disk_super) + 1);
475 fs_info->log_root_tree = log_root;
477 if (!extent_buffer_uptodate(log_root->node))
478 return -EIO;
479 return 0;
483 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
484 struct btrfs_root *root)
486 if (root->node)
487 free_extent_buffer(root->node);
488 if (root->commit_root)
489 free_extent_buffer(root->commit_root);
490 kfree(root);
491 return 0;
494 static int free_fs_roots(struct btrfs_fs_info *fs_info)
496 struct cache_extent *cache;
497 struct btrfs_root *root;
499 while (1) {
500 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
501 if (!cache)
502 break;
503 root = container_of(cache, struct btrfs_root, cache);
504 remove_cache_extent(&fs_info->fs_root_cache, cache);
505 btrfs_free_fs_root(fs_info, root);
507 return 0;
510 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
511 struct btrfs_key *location)
513 struct btrfs_root *root;
514 struct btrfs_root *tree_root = fs_info->tree_root;
515 struct btrfs_path *path;
516 struct extent_buffer *l;
517 u64 generation;
518 u32 blocksize;
519 int ret = 0;
521 root = malloc(sizeof(*root));
522 if (!root)
523 return ERR_PTR(-ENOMEM);
524 memset(root, 0, sizeof(*root));
525 if (location->offset == (u64)-1) {
526 ret = find_and_setup_root(tree_root, fs_info,
527 location->objectid, root);
528 if (ret) {
529 free(root);
530 return ERR_PTR(ret);
532 goto insert;
535 __setup_root(tree_root->nodesize, tree_root->leafsize,
536 tree_root->sectorsize, tree_root->stripesize,
537 root, fs_info, location->objectid);
539 path = btrfs_alloc_path();
540 BUG_ON(!path);
541 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
542 if (ret != 0) {
543 if (ret > 0)
544 ret = -ENOENT;
545 goto out;
547 l = path->nodes[0];
548 read_extent_buffer(l, &root->root_item,
549 btrfs_item_ptr_offset(l, path->slots[0]),
550 sizeof(root->root_item));
551 memcpy(&root->root_key, location, sizeof(*location));
552 ret = 0;
553 out:
554 btrfs_release_path(root, path);
555 btrfs_free_path(path);
556 if (ret) {
557 free(root);
558 return ERR_PTR(ret);
560 generation = btrfs_root_generation(&root->root_item);
561 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
562 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
563 blocksize, generation);
564 BUG_ON(!root->node);
565 insert:
566 root->ref_cows = 1;
567 return root;
570 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
571 struct btrfs_key *location)
573 struct btrfs_root *root;
574 struct cache_extent *cache;
575 int ret;
577 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
578 return fs_info->tree_root;
579 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
580 return fs_info->extent_root;
581 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
582 return fs_info->chunk_root;
583 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
584 return fs_info->dev_root;
585 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
586 return fs_info->csum_root;
588 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
589 location->offset != (u64)-1);
591 cache = find_cache_extent(&fs_info->fs_root_cache,
592 location->objectid, 1);
593 if (cache)
594 return container_of(cache, struct btrfs_root, cache);
596 root = btrfs_read_fs_root_no_cache(fs_info, location);
597 if (IS_ERR(root))
598 return root;
600 root->cache.start = location->objectid;
601 root->cache.size = 1;
602 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
603 &root->cache);
604 BUG_ON(ret);
605 return root;
608 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
609 u64 sb_bytenr,
610 u64 root_tree_bytenr, int writes,
611 int partial)
613 u32 sectorsize;
614 u32 nodesize;
615 u32 leafsize;
616 u32 blocksize;
617 u32 stripesize;
618 u64 generation;
619 struct btrfs_key key;
620 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
621 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
622 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
623 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
624 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
625 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
626 int ret;
627 struct btrfs_super_block *disk_super;
628 struct btrfs_fs_devices *fs_devices = NULL;
629 u64 total_devs;
630 u64 features;
632 if (sb_bytenr == 0)
633 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
635 ret = btrfs_scan_one_device(fp, path, &fs_devices,
636 &total_devs, sb_bytenr);
638 if (ret) {
639 fprintf(stderr, "No valid Btrfs found on %s\n", path);
640 goto out;
643 if (total_devs != 1) {
644 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
645 if (ret)
646 goto out;
649 memset(fs_info, 0, sizeof(*fs_info));
650 fs_info->tree_root = tree_root;
651 fs_info->extent_root = extent_root;
652 fs_info->chunk_root = chunk_root;
653 fs_info->dev_root = dev_root;
654 fs_info->csum_root = csum_root;
656 if (!writes)
657 fs_info->readonly = 1;
659 extent_io_tree_init(&fs_info->extent_cache);
660 extent_io_tree_init(&fs_info->free_space_cache);
661 extent_io_tree_init(&fs_info->block_group_cache);
662 extent_io_tree_init(&fs_info->pinned_extents);
663 extent_io_tree_init(&fs_info->pending_del);
664 extent_io_tree_init(&fs_info->extent_ins);
665 cache_tree_init(&fs_info->fs_root_cache);
667 cache_tree_init(&fs_info->mapping_tree.cache_tree);
669 mutex_init(&fs_info->fs_mutex);
670 fs_info->fs_devices = fs_devices;
671 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
672 INIT_LIST_HEAD(&fs_info->space_info);
674 __setup_root(4096, 4096, 4096, 4096, tree_root,
675 fs_info, BTRFS_ROOT_TREE_OBJECTID);
677 if (writes)
678 ret = btrfs_open_devices(fs_devices, O_RDWR);
679 else
680 ret = btrfs_open_devices(fs_devices, O_RDONLY);
681 if (ret)
682 goto out_cleanup;
684 fs_info->super_bytenr = sb_bytenr;
685 disk_super = &fs_info->super_copy;
686 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
687 disk_super, sb_bytenr);
688 if (ret) {
689 printk("No valid btrfs found\n");
690 goto out_devices;
693 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
696 features = btrfs_super_incompat_flags(disk_super) &
697 ~BTRFS_FEATURE_INCOMPAT_SUPP;
698 if (features) {
699 printk("couldn't open because of unsupported "
700 "option features (%Lx).\n",
701 (unsigned long long)features);
702 goto out_devices;
705 features = btrfs_super_incompat_flags(disk_super);
706 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
707 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
708 btrfs_set_super_incompat_flags(disk_super, features);
711 features = btrfs_super_compat_ro_flags(disk_super) &
712 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
713 if (writes && features) {
714 printk("couldn't open RDWR because of unsupported "
715 "option features (%Lx).\n",
716 (unsigned long long)features);
717 goto out_devices;
720 nodesize = btrfs_super_nodesize(disk_super);
721 leafsize = btrfs_super_leafsize(disk_super);
722 sectorsize = btrfs_super_sectorsize(disk_super);
723 stripesize = btrfs_super_stripesize(disk_super);
724 tree_root->nodesize = nodesize;
725 tree_root->leafsize = leafsize;
726 tree_root->sectorsize = sectorsize;
727 tree_root->stripesize = stripesize;
729 ret = btrfs_read_sys_array(tree_root);
730 if (ret)
731 goto out_devices;
732 blocksize = btrfs_level_size(tree_root,
733 btrfs_super_chunk_root_level(disk_super));
734 generation = btrfs_super_chunk_root_generation(disk_super);
736 __setup_root(nodesize, leafsize, sectorsize, stripesize,
737 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
739 chunk_root->node = read_tree_block(chunk_root,
740 btrfs_super_chunk_root(disk_super),
741 blocksize, generation);
742 if (!extent_buffer_uptodate(chunk_root->node)) {
743 printk("Couldn't read chunk root\n");
744 goto out_devices;
747 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
748 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
749 BTRFS_UUID_SIZE);
751 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
752 ret = btrfs_read_chunk_tree(chunk_root);
753 if (ret)
754 goto out_failed;
757 blocksize = btrfs_level_size(tree_root,
758 btrfs_super_root_level(disk_super));
759 generation = btrfs_super_generation(disk_super);
761 if (!root_tree_bytenr)
762 root_tree_bytenr = btrfs_super_root(disk_super);
763 tree_root->node = read_tree_block(tree_root,
764 root_tree_bytenr,
765 blocksize, generation);
766 if (!extent_buffer_uptodate(tree_root->node)) {
767 printk("Couldn't read tree root\n");
768 goto out_failed;
770 ret = find_and_setup_root(tree_root, fs_info,
771 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
772 if (ret) {
773 printk("Couldn't setup extent tree\n");
774 goto out_failed;
776 extent_root->track_dirty = 1;
778 ret = find_and_setup_root(tree_root, fs_info,
779 BTRFS_DEV_TREE_OBJECTID, dev_root);
780 if (ret) {
781 printk("Couldn't setup device tree\n");
782 goto out_failed;
784 dev_root->track_dirty = 1;
786 ret = find_and_setup_root(tree_root, fs_info,
787 BTRFS_CSUM_TREE_OBJECTID, csum_root);
788 if (ret) {
789 printk("Couldn't setup csum tree\n");
790 if (!partial)
791 goto out_failed;
793 csum_root->track_dirty = 1;
795 find_and_setup_log_root(tree_root, fs_info, disk_super);
797 fs_info->generation = generation;
798 fs_info->last_trans_committed = generation;
799 btrfs_read_block_groups(fs_info->tree_root);
801 key.objectid = BTRFS_FS_TREE_OBJECTID;
802 key.type = BTRFS_ROOT_ITEM_KEY;
803 key.offset = (u64)-1;
804 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
806 if (!fs_info->fs_root)
807 goto out_failed;
809 fs_info->data_alloc_profile = (u64)-1;
810 fs_info->metadata_alloc_profile = (u64)-1;
811 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
813 return fs_info;
815 out_failed:
816 if (partial)
817 return fs_info;
819 if (fs_info->csum_root)
820 free_extent_buffer(fs_info->csum_root->node);
821 if (fs_info->dev_root)
822 free_extent_buffer(fs_info->dev_root->node);
823 if (fs_info->extent_root)
824 free_extent_buffer(fs_info->extent_root->node);
825 if (fs_info->tree_root)
826 free_extent_buffer(fs_info->tree_root->node);
827 if (fs_info->chunk_root)
828 free_extent_buffer(fs_info->chunk_root->node);
829 out_devices:
830 close_all_devices(fs_info);
831 out_cleanup:
832 extent_io_tree_cleanup(&fs_info->extent_cache);
833 extent_io_tree_cleanup(&fs_info->free_space_cache);
834 extent_io_tree_cleanup(&fs_info->block_group_cache);
835 extent_io_tree_cleanup(&fs_info->pinned_extents);
836 extent_io_tree_cleanup(&fs_info->pending_del);
837 extent_io_tree_cleanup(&fs_info->extent_ins);
838 out:
839 free(tree_root);
840 free(extent_root);
841 free(chunk_root);
842 free(dev_root);
843 free(csum_root);
844 free(fs_info);
845 return NULL;
848 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
849 u64 sb_bytenr, int writes,
850 int partial)
852 int fp;
853 struct btrfs_fs_info *info;
854 int flags = O_CREAT | O_RDWR;
856 if (!writes)
857 flags = O_RDONLY;
859 fp = open(filename, flags, 0600);
860 if (fp < 0) {
861 fprintf (stderr, "Could not open %s\n", filename);
862 return NULL;
864 info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
865 close(fp);
866 return info;
869 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
871 struct btrfs_fs_info *info;
873 info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
874 if (!info)
875 return NULL;
876 return info->fs_root;
879 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
880 u64 root_tree_bytenr)
882 int fp;
883 struct btrfs_fs_info *info;
886 fp = open(filename, O_RDONLY);
887 if (fp < 0) {
888 fprintf (stderr, "Could not open %s\n", filename);
889 return NULL;
891 info = __open_ctree_fd(fp, filename, sb_bytenr,
892 root_tree_bytenr, 0, 0);
893 close(fp);
895 if (!info)
896 return NULL;
897 return info->fs_root;
900 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
901 int writes)
903 struct btrfs_fs_info *info;
904 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
905 if (!info)
906 return NULL;
907 return info->fs_root;
910 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
912 u8 fsid[BTRFS_FSID_SIZE];
913 struct btrfs_super_block buf;
914 int i;
915 int ret;
916 u64 transid = 0;
917 u64 bytenr;
919 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
920 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
921 if (ret < sizeof(buf))
922 return -1;
924 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
925 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
926 sizeof(buf.magic)))
927 return -1;
929 memcpy(sb, &buf, sizeof(*sb));
930 return 0;
933 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
934 bytenr = btrfs_sb_offset(i);
935 ret = pread64(fd, &buf, sizeof(buf), bytenr);
936 if (ret < sizeof(buf))
937 break;
939 if (btrfs_super_bytenr(&buf) != bytenr ||
940 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
941 sizeof(buf.magic)))
942 continue;
944 if (i == 0)
945 memcpy(fsid, buf.fsid, sizeof(fsid));
946 else if (memcmp(fsid, buf.fsid, sizeof(fsid)))
947 continue;
949 if (btrfs_super_generation(&buf) > transid) {
950 memcpy(sb, &buf, sizeof(*sb));
951 transid = btrfs_super_generation(&buf);
955 return transid > 0 ? 0 : -1;
958 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
959 struct btrfs_device *device)
961 u64 bytenr;
962 u32 crc;
963 int i, ret;
965 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
966 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
967 crc = ~(u32)0;
968 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
969 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
970 btrfs_csum_final(crc, (char *)&sb->csum[0]);
972 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
973 root->fs_info->super_bytenr);
974 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
975 return 0;
978 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
979 bytenr = btrfs_sb_offset(i);
980 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
981 break;
983 btrfs_set_super_bytenr(sb, bytenr);
985 crc = ~(u32)0;
986 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
987 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
988 btrfs_csum_final(crc, (char *)&sb->csum[0]);
990 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
991 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
993 return 0;
996 int write_all_supers(struct btrfs_root *root)
998 struct list_head *cur;
999 struct list_head *head = &root->fs_info->fs_devices->devices;
1000 struct btrfs_device *dev;
1001 struct btrfs_super_block *sb;
1002 struct btrfs_dev_item *dev_item;
1003 int ret;
1004 u64 flags;
1006 sb = &root->fs_info->super_copy;
1007 dev_item = &sb->dev_item;
1008 list_for_each(cur, head) {
1009 dev = list_entry(cur, struct btrfs_device, dev_list);
1010 if (!dev->writeable)
1011 continue;
1013 btrfs_set_stack_device_generation(dev_item, 0);
1014 btrfs_set_stack_device_type(dev_item, dev->type);
1015 btrfs_set_stack_device_id(dev_item, dev->devid);
1016 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1017 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1018 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1019 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1020 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1021 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1022 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1024 flags = btrfs_super_flags(sb);
1025 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1027 ret = write_dev_supers(root, sb, dev);
1028 BUG_ON(ret);
1030 return 0;
1033 int write_ctree_super(struct btrfs_trans_handle *trans,
1034 struct btrfs_root *root)
1036 int ret;
1037 struct btrfs_root *tree_root = root->fs_info->tree_root;
1038 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1040 if (root->fs_info->readonly)
1041 return 0;
1043 btrfs_set_super_generation(&root->fs_info->super_copy,
1044 trans->transid);
1045 btrfs_set_super_root(&root->fs_info->super_copy,
1046 tree_root->node->start);
1047 btrfs_set_super_root_level(&root->fs_info->super_copy,
1048 btrfs_header_level(tree_root->node));
1049 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1050 chunk_root->node->start);
1051 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1052 btrfs_header_level(chunk_root->node));
1053 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1054 btrfs_header_generation(chunk_root->node));
1056 ret = write_all_supers(root);
1057 if (ret)
1058 fprintf(stderr, "failed to write new super block err %d\n", ret);
1059 return ret;
1062 static int close_all_devices(struct btrfs_fs_info *fs_info)
1064 struct list_head *list;
1065 struct list_head *next;
1066 struct btrfs_device *device;
1068 return 0;
1070 list = &fs_info->fs_devices->devices;
1071 list_for_each(next, list) {
1072 device = list_entry(next, struct btrfs_device, dev_list);
1073 close(device->fd);
1075 return 0;
1078 int close_ctree(struct btrfs_root *root)
1080 int ret;
1081 struct btrfs_trans_handle *trans;
1082 struct btrfs_fs_info *fs_info = root->fs_info;
1084 if (fs_info->last_trans_committed !=
1085 fs_info->generation) {
1086 trans = btrfs_start_transaction(root, 1);
1087 btrfs_commit_transaction(trans, root);
1088 trans = btrfs_start_transaction(root, 1);
1089 ret = commit_tree_roots(trans, fs_info);
1090 BUG_ON(ret);
1091 ret = __commit_transaction(trans, root);
1092 BUG_ON(ret);
1093 write_ctree_super(trans, root);
1094 btrfs_free_transaction(root, trans);
1096 btrfs_free_block_groups(fs_info);
1098 free_fs_roots(fs_info);
1100 if (fs_info->extent_root->node)
1101 free_extent_buffer(fs_info->extent_root->node);
1102 if (fs_info->tree_root->node)
1103 free_extent_buffer(fs_info->tree_root->node);
1104 if (fs_info->chunk_root->node)
1105 free_extent_buffer(fs_info->chunk_root->node);
1106 if (fs_info->dev_root->node)
1107 free_extent_buffer(fs_info->dev_root->node);
1108 if (fs_info->csum_root->node)
1109 free_extent_buffer(fs_info->csum_root->node);
1111 if (fs_info->log_root_tree) {
1112 if (fs_info->log_root_tree->node)
1113 free_extent_buffer(fs_info->log_root_tree->node);
1114 free(fs_info->log_root_tree);
1117 close_all_devices(fs_info);
1118 extent_io_tree_cleanup(&fs_info->extent_cache);
1119 extent_io_tree_cleanup(&fs_info->free_space_cache);
1120 extent_io_tree_cleanup(&fs_info->block_group_cache);
1121 extent_io_tree_cleanup(&fs_info->pinned_extents);
1122 extent_io_tree_cleanup(&fs_info->pending_del);
1123 extent_io_tree_cleanup(&fs_info->extent_ins);
1125 free(fs_info->tree_root);
1126 free(fs_info->extent_root);
1127 free(fs_info->chunk_root);
1128 free(fs_info->dev_root);
1129 free(fs_info->csum_root);
1130 free(fs_info);
1132 return 0;
1135 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1136 struct extent_buffer *eb)
1138 return clear_extent_buffer_dirty(eb);
1141 int wait_on_tree_block_writeback(struct btrfs_root *root,
1142 struct extent_buffer *eb)
1144 return 0;
1147 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1149 set_extent_buffer_dirty(eb);
1152 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1154 int ret;
1156 ret = extent_buffer_uptodate(buf);
1157 if (!ret)
1158 return ret;
1160 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1161 return !ret;
1164 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1166 return set_extent_buffer_uptodate(eb);