btrfs-progs: Refactor the root used bytes are updated
[btrfs-progs-unstable/devel.git] / disk-io.c
blob26e4f6e93ed6832e74cc9dab1806e8441674ba56
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <uuid/uuid.h>
26 #include "kerncompat.h"
27 #include "radix-tree.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "volumes.h"
31 #include "transaction.h"
32 #include "crc32c.h"
33 #include "utils.h"
34 #include "print-tree.h"
35 #include "rbtree-utils.h"
37 /* specified errno for check_tree_block */
38 #define BTRFS_BAD_BYTENR (-1)
39 #define BTRFS_BAD_FSID (-2)
40 #define BTRFS_BAD_LEVEL (-3)
41 #define BTRFS_BAD_NRITEMS (-4)
43 /* Calculate max possible nritems for a leaf/node */
44 static u32 max_nritems(u8 level, u32 nodesize)
47 if (level == 0)
48 return ((nodesize - sizeof(struct btrfs_header)) /
49 sizeof(struct btrfs_item));
50 return ((nodesize - sizeof(struct btrfs_header)) /
51 sizeof(struct btrfs_key_ptr));
54 static int check_tree_block(struct btrfs_fs_info *fs_info,
55 struct extent_buffer *buf)
58 struct btrfs_fs_devices *fs_devices;
59 u32 nodesize = fs_info->nodesize;
60 int ret = BTRFS_BAD_FSID;
62 if (buf->start != btrfs_header_bytenr(buf))
63 return BTRFS_BAD_BYTENR;
64 if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
65 return BTRFS_BAD_LEVEL;
66 if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
67 nodesize))
68 return BTRFS_BAD_NRITEMS;
70 /* Only leaf can be empty */
71 if (btrfs_header_nritems(buf) == 0 &&
72 btrfs_header_level(buf) != 0)
73 return BTRFS_BAD_NRITEMS;
75 fs_devices = fs_info->fs_devices;
76 while (fs_devices) {
77 if (fs_info->ignore_fsid_mismatch ||
78 !memcmp_extent_buffer(buf, fs_devices->fsid,
79 btrfs_header_fsid(),
80 BTRFS_FSID_SIZE)) {
81 ret = 0;
82 break;
84 fs_devices = fs_devices->seed;
86 return ret;
89 static void print_tree_block_error(struct btrfs_fs_info *fs_info,
90 struct extent_buffer *eb,
91 int err)
93 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
94 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
95 u8 buf[BTRFS_UUID_SIZE];
97 if (!err)
98 return;
100 fprintf(stderr, "bad tree block %llu, ", eb->start);
101 switch (err) {
102 case BTRFS_BAD_FSID:
103 read_extent_buffer(eb, buf, btrfs_header_fsid(),
104 BTRFS_UUID_SIZE);
105 uuid_unparse(buf, found_uuid);
106 uuid_unparse(fs_info->fsid, fs_uuid);
107 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
108 fs_uuid, found_uuid);
109 break;
110 case BTRFS_BAD_BYTENR:
111 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
112 eb->start, btrfs_header_bytenr(eb));
113 break;
114 case BTRFS_BAD_LEVEL:
115 fprintf(stderr, "bad level, %u > %u\n",
116 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
117 break;
118 case BTRFS_BAD_NRITEMS:
119 fprintf(stderr, "invalid nr_items: %u\n",
120 btrfs_header_nritems(eb));
121 break;
125 u32 btrfs_csum_data(char *data, u32 seed, size_t len)
127 return crc32c(seed, data, len);
130 void btrfs_csum_final(u32 crc, u8 *result)
132 put_unaligned_le32(~crc, result);
135 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
136 int verify, int silent)
138 u8 result[BTRFS_CSUM_SIZE];
139 u32 len;
140 u32 crc = ~(u32)0;
142 len = buf->len - BTRFS_CSUM_SIZE;
143 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
144 btrfs_csum_final(crc, result);
146 if (verify) {
147 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
148 if (!silent)
149 printk("checksum verify failed on %llu found %08X wanted %08X\n",
150 (unsigned long long)buf->start,
151 *((u32 *)result),
152 *((u32*)(char *)buf->data));
153 return 1;
155 } else {
156 write_extent_buffer(buf, result, 0, csum_size);
158 return 0;
161 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
163 return __csum_tree_block_size(buf, csum_size, verify, 0);
166 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
168 return __csum_tree_block_size(buf, csum_size, 1, 1);
171 int csum_tree_block(struct btrfs_fs_info *fs_info,
172 struct extent_buffer *buf, int verify)
174 u16 csum_size =
175 btrfs_super_csum_size(fs_info->super_copy);
176 if (verify && fs_info->suppress_check_block_errors)
177 return verify_tree_block_csum_silent(buf, csum_size);
178 return csum_tree_block_size(buf, csum_size, verify);
181 struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
182 u64 bytenr, u32 blocksize)
184 return find_extent_buffer(&fs_info->extent_cache,
185 bytenr, blocksize);
188 struct extent_buffer* btrfs_find_create_tree_block(
189 struct btrfs_fs_info *fs_info, u64 bytenr)
191 return alloc_extent_buffer(fs_info, bytenr, fs_info->nodesize);
194 void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
195 u64 parent_transid)
197 struct extent_buffer *eb;
198 u64 length;
199 struct btrfs_multi_bio *multi = NULL;
200 struct btrfs_device *device;
202 eb = btrfs_find_tree_block(fs_info, bytenr, fs_info->nodesize);
203 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
204 !btrfs_map_block(fs_info, READ, bytenr, &length, &multi, 0,
205 NULL)) {
206 device = multi->stripes[0].dev;
207 device->total_ios++;
208 readahead(device->fd, multi->stripes[0].physical,
209 fs_info->nodesize);
212 free_extent_buffer(eb);
213 kfree(multi);
216 static int verify_parent_transid(struct extent_io_tree *io_tree,
217 struct extent_buffer *eb, u64 parent_transid,
218 int ignore)
220 int ret;
222 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
223 return 0;
225 if (extent_buffer_uptodate(eb) &&
226 btrfs_header_generation(eb) == parent_transid) {
227 ret = 0;
228 goto out;
230 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
231 (unsigned long long)eb->start,
232 (unsigned long long)parent_transid,
233 (unsigned long long)btrfs_header_generation(eb));
234 if (ignore) {
235 eb->flags |= EXTENT_BAD_TRANSID;
236 printk("Ignoring transid failure\n");
237 return 0;
240 ret = 1;
241 out:
242 clear_extent_buffer_uptodate(eb);
243 return ret;
248 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
250 unsigned long offset = 0;
251 struct btrfs_multi_bio *multi = NULL;
252 struct btrfs_device *device;
253 int ret = 0;
254 u64 read_len;
255 unsigned long bytes_left = eb->len;
257 while (bytes_left) {
258 read_len = bytes_left;
259 device = NULL;
261 if (!info->on_restoring &&
262 eb->start != BTRFS_SUPER_INFO_OFFSET) {
263 ret = btrfs_map_block(info, READ, eb->start + offset,
264 &read_len, &multi, mirror, NULL);
265 if (ret) {
266 printk("Couldn't map the block %Lu\n", eb->start + offset);
267 kfree(multi);
268 return -EIO;
270 device = multi->stripes[0].dev;
272 if (device->fd <= 0) {
273 kfree(multi);
274 return -EIO;
277 eb->fd = device->fd;
278 device->total_ios++;
279 eb->dev_bytenr = multi->stripes[0].physical;
280 kfree(multi);
281 multi = NULL;
282 } else {
283 /* special case for restore metadump */
284 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
285 if (device->devid == 1)
286 break;
289 eb->fd = device->fd;
290 eb->dev_bytenr = eb->start;
291 device->total_ios++;
294 if (read_len > bytes_left)
295 read_len = bytes_left;
297 ret = read_extent_from_disk(eb, offset, read_len);
298 if (ret)
299 return -EIO;
300 offset += read_len;
301 bytes_left -= read_len;
303 return 0;
306 struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
307 u64 parent_transid)
309 int ret;
310 struct extent_buffer *eb;
311 u64 best_transid = 0;
312 u32 sectorsize = fs_info->sectorsize;
313 int mirror_num = 0;
314 int good_mirror = 0;
315 int num_copies;
316 int ignore = 0;
319 * Don't even try to create tree block for unaligned tree block
320 * bytenr.
321 * Such unaligned tree block will free overlapping extent buffer,
322 * causing use-after-free bugs for fuzzed images.
324 if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
325 error("tree block bytenr %llu is not aligned to sectorsize %u",
326 bytenr, sectorsize);
327 return ERR_PTR(-EIO);
330 eb = btrfs_find_create_tree_block(fs_info, bytenr);
331 if (!eb)
332 return ERR_PTR(-ENOMEM);
334 if (btrfs_buffer_uptodate(eb, parent_transid))
335 return eb;
337 while (1) {
338 ret = read_whole_eb(fs_info, eb, mirror_num);
339 if (ret == 0 && csum_tree_block(fs_info, eb, 1) == 0 &&
340 check_tree_block(fs_info, eb) == 0 &&
341 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
342 == 0) {
343 if (eb->flags & EXTENT_BAD_TRANSID &&
344 list_empty(&eb->recow)) {
345 list_add_tail(&eb->recow,
346 &fs_info->recow_ebs);
347 eb->refs++;
349 btrfs_set_buffer_uptodate(eb);
350 return eb;
352 if (ignore) {
353 if (check_tree_block(fs_info, eb)) {
354 if (!fs_info->suppress_check_block_errors)
355 print_tree_block_error(fs_info, eb,
356 check_tree_block(fs_info, eb));
357 } else {
358 if (!fs_info->suppress_check_block_errors)
359 fprintf(stderr, "Csum didn't match\n");
361 ret = -EIO;
362 break;
364 num_copies = btrfs_num_copies(fs_info, eb->start, eb->len);
365 if (num_copies == 1) {
366 ignore = 1;
367 continue;
369 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
370 best_transid = btrfs_header_generation(eb);
371 good_mirror = mirror_num;
373 mirror_num++;
374 if (mirror_num > num_copies) {
375 mirror_num = good_mirror;
376 ignore = 1;
377 continue;
380 free_extent_buffer(eb);
381 return ERR_PTR(ret);
384 int read_extent_data(struct btrfs_fs_info *fs_info, char *data, u64 logical,
385 u64 *len, int mirror)
387 u64 offset = 0;
388 struct btrfs_multi_bio *multi = NULL;
389 struct btrfs_device *device;
390 int ret = 0;
391 u64 max_len = *len;
393 ret = btrfs_map_block(fs_info, READ, logical, len, &multi, mirror,
394 NULL);
395 if (ret) {
396 fprintf(stderr, "Couldn't map the block %llu\n",
397 logical + offset);
398 goto err;
400 device = multi->stripes[0].dev;
402 if (*len > max_len)
403 *len = max_len;
404 if (device->fd < 0) {
405 ret = -EIO;
406 goto err;
409 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
410 if (ret != *len)
411 ret = -EIO;
412 else
413 ret = 0;
414 err:
415 kfree(multi);
416 return ret;
419 int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
421 int ret;
422 int dev_nr;
423 u64 length;
424 u64 *raid_map = NULL;
425 struct btrfs_multi_bio *multi = NULL;
427 dev_nr = 0;
428 length = eb->len;
429 ret = btrfs_map_block(fs_info, WRITE, eb->start, &length,
430 &multi, 0, &raid_map);
432 if (raid_map) {
433 ret = write_raid56_with_parity(fs_info, eb, multi,
434 length, raid_map);
435 BUG_ON(ret);
436 } else while (dev_nr < multi->num_stripes) {
437 BUG_ON(ret);
438 eb->fd = multi->stripes[dev_nr].dev->fd;
439 eb->dev_bytenr = multi->stripes[dev_nr].physical;
440 multi->stripes[dev_nr].dev->total_ios++;
441 dev_nr++;
442 ret = write_extent_to_disk(eb);
443 BUG_ON(ret);
445 kfree(raid_map);
446 kfree(multi);
447 return 0;
450 int write_tree_block(struct btrfs_trans_handle *trans,
451 struct btrfs_fs_info *fs_info,
452 struct extent_buffer *eb)
454 if (check_tree_block(fs_info, eb)) {
455 print_tree_block_error(fs_info, eb,
456 check_tree_block(fs_info, eb));
457 BUG();
460 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
461 BUG();
463 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
464 csum_tree_block(fs_info, eb, 0);
466 return write_and_map_eb(fs_info, eb);
469 void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
470 u64 objectid)
472 root->node = NULL;
473 root->commit_root = NULL;
474 root->ref_cows = 0;
475 root->track_dirty = 0;
477 root->fs_info = fs_info;
478 root->objectid = objectid;
479 root->last_trans = 0;
480 root->last_inode_alloc = 0;
482 INIT_LIST_HEAD(&root->dirty_list);
483 INIT_LIST_HEAD(&root->orphan_data_extents);
484 memset(&root->root_key, 0, sizeof(root->root_key));
485 memset(&root->root_item, 0, sizeof(root->root_item));
486 root->root_key.objectid = objectid;
489 static int find_and_setup_root(struct btrfs_root *tree_root,
490 struct btrfs_fs_info *fs_info,
491 u64 objectid, struct btrfs_root *root)
493 int ret;
494 u64 generation;
496 btrfs_setup_root(root, fs_info, objectid);
497 ret = btrfs_find_last_root(tree_root, objectid,
498 &root->root_item, &root->root_key);
499 if (ret)
500 return ret;
502 generation = btrfs_root_generation(&root->root_item);
503 root->node = read_tree_block(fs_info,
504 btrfs_root_bytenr(&root->root_item), generation);
505 if (!extent_buffer_uptodate(root->node))
506 return -EIO;
508 return 0;
511 static int find_and_setup_log_root(struct btrfs_root *tree_root,
512 struct btrfs_fs_info *fs_info,
513 struct btrfs_super_block *disk_super)
515 u64 blocknr = btrfs_super_log_root(disk_super);
516 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
518 if (!log_root)
519 return -ENOMEM;
521 if (blocknr == 0) {
522 free(log_root);
523 return 0;
526 btrfs_setup_root(log_root, fs_info,
527 BTRFS_TREE_LOG_OBJECTID);
529 log_root->node = read_tree_block(fs_info, blocknr,
530 btrfs_super_generation(disk_super) + 1);
532 fs_info->log_root_tree = log_root;
534 if (!extent_buffer_uptodate(log_root->node)) {
535 free_extent_buffer(log_root->node);
536 free(log_root);
537 fs_info->log_root_tree = NULL;
538 return -EIO;
541 return 0;
544 int btrfs_free_fs_root(struct btrfs_root *root)
546 if (root->node)
547 free_extent_buffer(root->node);
548 if (root->commit_root)
549 free_extent_buffer(root->commit_root);
550 kfree(root);
551 return 0;
554 static void __free_fs_root(struct rb_node *node)
556 struct btrfs_root *root;
558 root = container_of(node, struct btrfs_root, rb_node);
559 btrfs_free_fs_root(root);
562 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
564 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
565 struct btrfs_key *location)
567 struct btrfs_root *root;
568 struct btrfs_root *tree_root = fs_info->tree_root;
569 struct btrfs_path *path;
570 struct extent_buffer *l;
571 u64 generation;
572 int ret = 0;
574 root = calloc(1, sizeof(*root));
575 if (!root)
576 return ERR_PTR(-ENOMEM);
577 if (location->offset == (u64)-1) {
578 ret = find_and_setup_root(tree_root, fs_info,
579 location->objectid, root);
580 if (ret) {
581 free(root);
582 return ERR_PTR(ret);
584 goto insert;
587 btrfs_setup_root(root, fs_info,
588 location->objectid);
590 path = btrfs_alloc_path();
591 if (!path) {
592 free(root);
593 return ERR_PTR(-ENOMEM);
596 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
597 if (ret != 0) {
598 if (ret > 0)
599 ret = -ENOENT;
600 goto out;
602 l = path->nodes[0];
603 read_extent_buffer(l, &root->root_item,
604 btrfs_item_ptr_offset(l, path->slots[0]),
605 sizeof(root->root_item));
606 memcpy(&root->root_key, location, sizeof(*location));
607 ret = 0;
608 out:
609 btrfs_free_path(path);
610 if (ret) {
611 free(root);
612 return ERR_PTR(ret);
614 generation = btrfs_root_generation(&root->root_item);
615 root->node = read_tree_block(fs_info,
616 btrfs_root_bytenr(&root->root_item), generation);
617 if (!extent_buffer_uptodate(root->node)) {
618 free(root);
619 return ERR_PTR(-EIO);
621 insert:
622 root->ref_cows = 1;
623 return root;
626 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
627 void *data)
629 u64 objectid = *((u64 *)data);
630 struct btrfs_root *root;
632 root = rb_entry(node, struct btrfs_root, rb_node);
633 if (objectid > root->objectid)
634 return 1;
635 else if (objectid < root->objectid)
636 return -1;
637 else
638 return 0;
641 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
642 struct rb_node *node2)
644 struct btrfs_root *root;
646 root = rb_entry(node2, struct btrfs_root, rb_node);
647 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
650 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
651 struct btrfs_key *location)
653 struct btrfs_root *root;
654 struct rb_node *node;
655 int ret;
656 u64 objectid = location->objectid;
658 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
659 return fs_info->tree_root;
660 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
661 return fs_info->extent_root;
662 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
663 return fs_info->chunk_root;
664 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
665 return fs_info->dev_root;
666 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
667 return fs_info->csum_root;
668 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
669 return fs_info->quota_enabled ? fs_info->quota_root :
670 ERR_PTR(-ENOENT);
672 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
673 location->offset != (u64)-1);
675 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
676 btrfs_fs_roots_compare_objectids, NULL);
677 if (node)
678 return container_of(node, struct btrfs_root, rb_node);
680 root = btrfs_read_fs_root_no_cache(fs_info, location);
681 if (IS_ERR(root))
682 return root;
684 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
685 btrfs_fs_roots_compare_roots);
686 BUG_ON(ret);
687 return root;
690 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
692 if (fs_info->quota_root)
693 free(fs_info->quota_root);
695 free(fs_info->tree_root);
696 free(fs_info->extent_root);
697 free(fs_info->chunk_root);
698 free(fs_info->dev_root);
699 free(fs_info->csum_root);
700 free(fs_info->free_space_root);
701 free(fs_info->super_copy);
702 free(fs_info->log_root_tree);
703 free(fs_info);
706 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
708 struct btrfs_fs_info *fs_info;
710 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
711 if (!fs_info)
712 return NULL;
714 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
715 fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
716 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
717 fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
718 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
719 fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
720 fs_info->free_space_root = calloc(1, sizeof(struct btrfs_root));
721 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
723 if (!fs_info->tree_root || !fs_info->extent_root ||
724 !fs_info->chunk_root || !fs_info->dev_root ||
725 !fs_info->csum_root || !fs_info->quota_root ||
726 !fs_info->free_space_root || !fs_info->super_copy)
727 goto free_all;
729 extent_io_tree_init(&fs_info->extent_cache);
730 extent_io_tree_init(&fs_info->free_space_cache);
731 extent_io_tree_init(&fs_info->block_group_cache);
732 extent_io_tree_init(&fs_info->pinned_extents);
733 extent_io_tree_init(&fs_info->pending_del);
734 extent_io_tree_init(&fs_info->extent_ins);
735 fs_info->excluded_extents = NULL;
737 fs_info->fs_root_tree = RB_ROOT;
738 cache_tree_init(&fs_info->mapping_tree.cache_tree);
740 mutex_init(&fs_info->fs_mutex);
741 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
742 INIT_LIST_HEAD(&fs_info->space_info);
743 INIT_LIST_HEAD(&fs_info->recow_ebs);
745 if (!writable)
746 fs_info->readonly = 1;
748 fs_info->super_bytenr = sb_bytenr;
749 fs_info->data_alloc_profile = (u64)-1;
750 fs_info->metadata_alloc_profile = (u64)-1;
751 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
752 return fs_info;
753 free_all:
754 btrfs_free_fs_info(fs_info);
755 return NULL;
758 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb,
759 unsigned int flags)
761 u64 features;
763 features = btrfs_super_incompat_flags(sb) &
764 ~BTRFS_FEATURE_INCOMPAT_SUPP;
765 if (features) {
766 printk("couldn't open because of unsupported "
767 "option features (%Lx).\n",
768 (unsigned long long)features);
769 return -ENOTSUP;
772 features = btrfs_super_incompat_flags(sb);
773 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
774 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
775 btrfs_set_super_incompat_flags(sb, features);
778 features = btrfs_super_compat_ro_flags(sb);
779 if (flags & OPEN_CTREE_WRITES) {
780 if (flags & OPEN_CTREE_INVALIDATE_FST) {
781 /* Clear the FREE_SPACE_TREE_VALID bit on disk... */
782 features &= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID;
783 btrfs_set_super_compat_ro_flags(sb, features);
784 /* ... and ignore the free space tree bit. */
785 features &= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE;
787 if (features & ~BTRFS_FEATURE_COMPAT_RO_SUPP) {
788 printk("couldn't open RDWR because of unsupported "
789 "option features (%Lx).\n",
790 (unsigned long long)features);
791 return -ENOTSUP;
795 return 0;
798 static int find_best_backup_root(struct btrfs_super_block *super)
800 struct btrfs_root_backup *backup;
801 u64 orig_gen = btrfs_super_generation(super);
802 u64 gen = 0;
803 int best_index = 0;
804 int i;
806 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
807 backup = super->super_roots + i;
808 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
809 btrfs_backup_tree_root_gen(backup) > gen) {
810 best_index = i;
811 gen = btrfs_backup_tree_root_gen(backup);
814 return best_index;
817 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
818 unsigned flags,
819 struct btrfs_root *info_root,
820 u64 objectid, char *str)
822 struct btrfs_root *root = fs_info->tree_root;
823 int ret;
825 ret = find_and_setup_root(root, fs_info, objectid, info_root);
826 if (ret) {
827 printk("Couldn't setup %s tree\n", str);
828 if (!(flags & OPEN_CTREE_PARTIAL))
829 return -EIO;
831 * Need a blank node here just so we don't screw up in the
832 * million of places that assume a root has a valid ->node
834 info_root->node =
835 btrfs_find_create_tree_block(fs_info, 0);
836 if (!info_root->node)
837 return -ENOMEM;
838 clear_extent_buffer_uptodate(info_root->node);
841 return 0;
844 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
845 unsigned flags)
847 struct btrfs_super_block *sb = fs_info->super_copy;
848 struct btrfs_root *root;
849 struct btrfs_key key;
850 u64 generation;
851 int ret;
853 root = fs_info->tree_root;
854 btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
855 generation = btrfs_super_generation(sb);
857 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
858 root_tree_bytenr = btrfs_super_root(sb);
859 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
860 struct btrfs_root_backup *backup;
861 int index = find_best_backup_root(sb);
862 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
863 fprintf(stderr, "Invalid backup root number\n");
864 return -EIO;
866 backup = fs_info->super_copy->super_roots + index;
867 root_tree_bytenr = btrfs_backup_tree_root(backup);
868 generation = btrfs_backup_tree_root_gen(backup);
871 root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
872 if (!extent_buffer_uptodate(root->node)) {
873 fprintf(stderr, "Couldn't read tree root\n");
874 return -EIO;
877 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
878 BTRFS_EXTENT_TREE_OBJECTID, "extent");
879 if (ret)
880 return ret;
881 fs_info->extent_root->track_dirty = 1;
883 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
884 fs_info->dev_root);
885 if (ret) {
886 printk("Couldn't setup device tree\n");
887 return -EIO;
889 fs_info->dev_root->track_dirty = 1;
891 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
892 BTRFS_CSUM_TREE_OBJECTID, "csum");
893 if (ret)
894 return ret;
895 fs_info->csum_root->track_dirty = 1;
897 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
898 fs_info->quota_root);
899 if (ret) {
900 free(fs_info->quota_root);
901 fs_info->quota_root = NULL;
902 } else {
903 fs_info->quota_enabled = 1;
906 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
907 ret = find_and_setup_root(root, fs_info, BTRFS_FREE_SPACE_TREE_OBJECTID,
908 fs_info->free_space_root);
909 if (ret) {
910 printk("Couldn't read free space tree\n");
911 return -EIO;
913 fs_info->free_space_root->track_dirty = 1;
916 ret = find_and_setup_log_root(root, fs_info, sb);
917 if (ret) {
918 printk("Couldn't setup log root tree\n");
919 if (!(flags & OPEN_CTREE_PARTIAL))
920 return -EIO;
923 fs_info->generation = generation;
924 fs_info->last_trans_committed = generation;
925 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
926 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS)) {
927 ret = btrfs_read_block_groups(fs_info->tree_root);
929 * If we don't find any blockgroups (ENOENT) we're either
930 * restoring or creating the filesystem, where it's expected,
931 * anything else is error
933 if (ret != -ENOENT)
934 return -EIO;
937 key.objectid = BTRFS_FS_TREE_OBJECTID;
938 key.type = BTRFS_ROOT_ITEM_KEY;
939 key.offset = (u64)-1;
940 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
942 if (IS_ERR(fs_info->fs_root))
943 return -EIO;
944 return 0;
947 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
949 if (fs_info->free_space_root)
950 free_extent_buffer(fs_info->free_space_root->node);
951 if (fs_info->quota_root)
952 free_extent_buffer(fs_info->quota_root->node);
953 if (fs_info->csum_root)
954 free_extent_buffer(fs_info->csum_root->node);
955 if (fs_info->dev_root)
956 free_extent_buffer(fs_info->dev_root->node);
957 if (fs_info->extent_root)
958 free_extent_buffer(fs_info->extent_root->node);
959 if (fs_info->tree_root)
960 free_extent_buffer(fs_info->tree_root->node);
961 if (fs_info->log_root_tree)
962 free_extent_buffer(fs_info->log_root_tree->node);
963 if (fs_info->chunk_root)
964 free_extent_buffer(fs_info->chunk_root->node);
967 static void free_map_lookup(struct cache_extent *ce)
969 struct map_lookup *map;
971 map = container_of(ce, struct map_lookup, ce);
972 kfree(map);
975 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
977 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
979 while (!list_empty(&fs_info->recow_ebs)) {
980 struct extent_buffer *eb;
981 eb = list_first_entry(&fs_info->recow_ebs,
982 struct extent_buffer, recow);
983 list_del_init(&eb->recow);
984 free_extent_buffer(eb);
986 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
987 extent_io_tree_cleanup(&fs_info->extent_cache);
988 extent_io_tree_cleanup(&fs_info->free_space_cache);
989 extent_io_tree_cleanup(&fs_info->block_group_cache);
990 extent_io_tree_cleanup(&fs_info->pinned_extents);
991 extent_io_tree_cleanup(&fs_info->pending_del);
992 extent_io_tree_cleanup(&fs_info->extent_ins);
995 int btrfs_scan_fs_devices(int fd, const char *path,
996 struct btrfs_fs_devices **fs_devices,
997 u64 sb_bytenr, unsigned sbflags,
998 int skip_devices)
1000 u64 total_devs;
1001 u64 dev_size;
1002 off_t seek_ret;
1003 int ret;
1004 if (!sb_bytenr)
1005 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1007 seek_ret = lseek(fd, 0, SEEK_END);
1008 if (seek_ret < 0)
1009 return -errno;
1011 dev_size = seek_ret;
1012 lseek(fd, 0, SEEK_SET);
1013 if (sb_bytenr > dev_size) {
1014 error("superblock bytenr %llu is larger than device size %llu",
1015 (unsigned long long)sb_bytenr,
1016 (unsigned long long)dev_size);
1017 return -EINVAL;
1020 ret = btrfs_scan_one_device(fd, path, fs_devices,
1021 &total_devs, sb_bytenr, sbflags);
1022 if (ret) {
1023 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1024 return ret;
1027 if (!skip_devices && total_devs != 1) {
1028 ret = btrfs_scan_devices();
1029 if (ret)
1030 return ret;
1032 return 0;
1035 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
1036 u64 chunk_root_bytenr)
1038 struct btrfs_super_block *sb = fs_info->super_copy;
1039 u64 generation;
1040 int ret;
1042 btrfs_setup_root(fs_info->chunk_root, fs_info,
1043 BTRFS_CHUNK_TREE_OBJECTID);
1045 ret = btrfs_read_sys_array(fs_info);
1046 if (ret)
1047 return ret;
1049 generation = btrfs_super_chunk_root_generation(sb);
1051 if (chunk_root_bytenr && !IS_ALIGNED(chunk_root_bytenr,
1052 fs_info->sectorsize)) {
1053 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1054 chunk_root_bytenr, fs_info->sectorsize);
1055 chunk_root_bytenr = 0;
1058 if (!chunk_root_bytenr)
1059 chunk_root_bytenr = btrfs_super_chunk_root(sb);
1060 else
1061 generation = 0;
1063 fs_info->chunk_root->node = read_tree_block(fs_info,
1064 chunk_root_bytenr,
1065 generation);
1066 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1067 if (fs_info->ignore_chunk_tree_error) {
1068 warning("cannot read chunk root, continue anyway");
1069 fs_info->chunk_root = NULL;
1070 return 0;
1071 } else {
1072 error("cannot read chunk root");
1073 return -EIO;
1077 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1078 ret = btrfs_read_chunk_tree(fs_info);
1079 if (ret) {
1080 fprintf(stderr, "Couldn't read chunk tree\n");
1081 return ret;
1084 return 0;
1087 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1088 u64 sb_bytenr,
1089 u64 root_tree_bytenr,
1090 u64 chunk_root_bytenr,
1091 unsigned flags)
1093 struct btrfs_fs_info *fs_info;
1094 struct btrfs_super_block *disk_super;
1095 struct btrfs_fs_devices *fs_devices = NULL;
1096 struct extent_buffer *eb;
1097 int ret;
1098 int oflags;
1099 unsigned sbflags = SBREAD_DEFAULT;
1101 if (sb_bytenr == 0)
1102 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1104 /* try to drop all the caches */
1105 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1106 fprintf(stderr, "Warning, could not drop caches\n");
1108 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1109 if (!fs_info) {
1110 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1111 return NULL;
1113 if (flags & OPEN_CTREE_RESTORE)
1114 fs_info->on_restoring = 1;
1115 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1116 fs_info->suppress_check_block_errors = 1;
1117 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1118 fs_info->ignore_fsid_mismatch = 1;
1119 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR)
1120 fs_info->ignore_chunk_tree_error = 1;
1122 if ((flags & OPEN_CTREE_RECOVER_SUPER)
1123 && (flags & OPEN_CTREE_TEMPORARY_SUPER)) {
1124 fprintf(stderr,
1125 "cannot open a filesystem with temporary super block for recovery");
1126 goto out;
1129 if (flags & OPEN_CTREE_TEMPORARY_SUPER)
1130 sbflags = SBREAD_TEMPORARY;
1132 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1133 sbflags |= SBREAD_IGNORE_FSID_MISMATCH;
1135 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
1136 (flags & OPEN_CTREE_NO_DEVICES));
1137 if (ret)
1138 goto out;
1140 fs_info->fs_devices = fs_devices;
1141 if (flags & OPEN_CTREE_WRITES)
1142 oflags = O_RDWR;
1143 else
1144 oflags = O_RDONLY;
1146 if (flags & OPEN_CTREE_EXCLUSIVE)
1147 oflags |= O_EXCL;
1149 ret = btrfs_open_devices(fs_devices, oflags);
1150 if (ret)
1151 goto out;
1153 disk_super = fs_info->super_copy;
1154 if (flags & OPEN_CTREE_RECOVER_SUPER)
1155 ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
1156 sb_bytenr, SBREAD_RECOVER);
1157 else
1158 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
1159 sbflags);
1160 if (ret) {
1161 printk("No valid btrfs found\n");
1162 goto out_devices;
1165 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1166 !fs_info->ignore_fsid_mismatch) {
1167 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1168 goto out_devices;
1171 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1172 fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
1173 fs_info->nodesize = btrfs_super_nodesize(disk_super);
1174 fs_info->stripesize = btrfs_super_stripesize(disk_super);
1176 ret = btrfs_check_fs_compatibility(fs_info->super_copy, flags);
1177 if (ret)
1178 goto out_devices;
1180 ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
1181 if (ret)
1182 goto out_chunk;
1184 /* Chunk tree root is unable to read, return directly */
1185 if (!fs_info->chunk_root)
1186 return fs_info;
1188 eb = fs_info->chunk_root->node;
1189 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1190 btrfs_header_chunk_tree_uuid(eb),
1191 BTRFS_UUID_SIZE);
1193 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1194 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
1195 !fs_info->ignore_chunk_tree_error)
1196 goto out_chunk;
1198 return fs_info;
1200 out_chunk:
1201 btrfs_release_all_roots(fs_info);
1202 btrfs_cleanup_all_caches(fs_info);
1203 out_devices:
1204 btrfs_close_devices(fs_devices);
1205 out:
1206 btrfs_free_fs_info(fs_info);
1207 return NULL;
1210 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1211 u64 sb_bytenr, u64 root_tree_bytenr,
1212 u64 chunk_root_bytenr,
1213 unsigned flags)
1215 int fp;
1216 int ret;
1217 struct btrfs_fs_info *info;
1218 int oflags = O_RDWR;
1219 struct stat st;
1221 ret = stat(filename, &st);
1222 if (ret < 0) {
1223 error("cannot stat '%s': %m", filename);
1224 return NULL;
1226 if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
1227 error("not a regular file or block device: %s", filename);
1228 return NULL;
1231 if (!(flags & OPEN_CTREE_WRITES))
1232 oflags = O_RDONLY;
1234 fp = open(filename, oflags);
1235 if (fp < 0) {
1236 error("cannot open '%s': %m", filename);
1237 return NULL;
1239 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1240 chunk_root_bytenr, flags);
1241 close(fp);
1242 return info;
1245 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1246 unsigned flags)
1248 struct btrfs_fs_info *info;
1250 /* This flags may not return fs_info with any valid root */
1251 BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
1252 info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
1253 if (!info)
1254 return NULL;
1255 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1256 return info->chunk_root;
1257 return info->fs_root;
1260 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1261 unsigned flags)
1263 struct btrfs_fs_info *info;
1265 /* This flags may not return fs_info with any valid root */
1266 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR) {
1267 error("invalid open_ctree flags: 0x%llx",
1268 (unsigned long long)flags);
1269 return NULL;
1271 info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
1272 if (!info)
1273 return NULL;
1274 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1275 return info->chunk_root;
1276 return info->fs_root;
1280 * Check if the super is valid:
1281 * - nodesize/sectorsize - minimum, maximum, alignment
1282 * - tree block starts - alignment
1283 * - number of devices - something sane
1284 * - sys array size - maximum
1286 static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
1288 u8 result[BTRFS_CSUM_SIZE];
1289 u32 crc;
1290 u16 csum_type;
1291 int csum_size;
1293 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1294 if (btrfs_super_magic(sb) == BTRFS_MAGIC_TEMPORARY) {
1295 if (!(sbflags & SBREAD_TEMPORARY)) {
1296 error("superblock magic doesn't match");
1297 return -EIO;
1302 csum_type = btrfs_super_csum_type(sb);
1303 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1304 error("unsupported checksum algorithm %u", csum_type);
1305 return -EIO;
1307 csum_size = btrfs_csum_sizes[csum_type];
1309 crc = ~(u32)0;
1310 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1311 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1312 btrfs_csum_final(crc, result);
1314 if (memcmp(result, sb->csum, csum_size)) {
1315 error("superblock checksum mismatch");
1316 return -EIO;
1318 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1319 error("tree_root level too big: %d >= %d",
1320 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1321 goto error_out;
1323 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1324 error("chunk_root level too big: %d >= %d",
1325 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1326 goto error_out;
1328 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1329 error("log_root level too big: %d >= %d",
1330 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1331 goto error_out;
1334 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1335 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
1336 goto error_out;
1338 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1339 error("chunk_root block unaligned: %llu",
1340 btrfs_super_chunk_root(sb));
1341 goto error_out;
1343 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1344 error("log_root block unaligned: %llu",
1345 btrfs_super_log_root(sb));
1346 goto error_out;
1348 if (btrfs_super_nodesize(sb) < 4096) {
1349 error("nodesize too small: %u < 4096",
1350 btrfs_super_nodesize(sb));
1351 goto error_out;
1353 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1354 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
1355 goto error_out;
1357 if (btrfs_super_sectorsize(sb) < 4096) {
1358 error("sectorsize too small: %u < 4096",
1359 btrfs_super_sectorsize(sb));
1360 goto error_out;
1362 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1363 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
1364 goto error_out;
1366 if (btrfs_super_total_bytes(sb) == 0) {
1367 error("invalid total_bytes 0");
1368 goto error_out;
1370 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
1371 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
1372 goto error_out;
1374 if ((btrfs_super_stripesize(sb) != 4096)
1375 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
1376 error("invalid stripesize %u", btrfs_super_stripesize(sb));
1377 goto error_out;
1380 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1381 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1382 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1384 uuid_unparse(sb->fsid, fsid);
1385 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1386 error("dev_item UUID does not match fsid: %s != %s",
1387 dev_fsid, fsid);
1388 goto error_out;
1392 * Hint to catch really bogus numbers, bitflips or so
1394 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1395 warning("suspicious number of devices: %llu",
1396 btrfs_super_num_devices(sb));
1399 if (btrfs_super_num_devices(sb) == 0) {
1400 error("number of devices is 0");
1401 goto error_out;
1405 * Obvious sys_chunk_array corruptions, it must hold at least one key
1406 * and one chunk
1408 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1409 error("system chunk array too big %u > %u",
1410 btrfs_super_sys_array_size(sb),
1411 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1412 goto error_out;
1414 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1415 + sizeof(struct btrfs_chunk)) {
1416 error("system chunk array too small %u < %zu",
1417 btrfs_super_sys_array_size(sb),
1418 sizeof(struct btrfs_disk_key) +
1419 sizeof(struct btrfs_chunk));
1420 goto error_out;
1423 return 0;
1425 error_out:
1426 error("superblock checksum matches but it has invalid members");
1427 return -EIO;
1431 * btrfs_read_dev_super - read a valid superblock from a block device
1432 * @fd: file descriptor of the device
1433 * @sb: buffer where the superblock is going to be read in
1434 * @sb_bytenr: offset of the particular superblock copy we want
1435 * @sbflags: flags controlling how the superblock is read
1437 * This function is used by various btrfs comands to obtain a valid superblock.
1439 * It's mode of operation is controlled by the @sb_bytenr and @sbdflags
1440 * parameters. If SBREAD_RECOVER flag is set and @sb_bytenr is
1441 * BTRFS_SUPER_INFO_OFFSET then the function reads all 3 superblock copies and
1442 * returns the newest one. If SBREAD_RECOVER is not set then only a single
1443 * copy is read, which one is decided by @sb_bytenr. If @sb_bytenr !=
1444 * BTRFS_SUPER_INFO_OFFSET then the @sbflags is effectively ignored and only a
1445 * single copy is read.
1447 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1448 unsigned sbflags)
1450 u8 fsid[BTRFS_FSID_SIZE];
1451 int fsid_is_initialized = 0;
1452 char tmp[BTRFS_SUPER_INFO_SIZE];
1453 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1454 int i;
1455 int ret;
1456 int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
1457 u64 transid = 0;
1458 u64 bytenr;
1460 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1461 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1462 /* real error */
1463 if (ret < 0)
1464 return -errno;
1466 /* Not large enough sb, return -ENOENT instead of normal -EIO */
1467 if (ret < BTRFS_SUPER_INFO_SIZE)
1468 return -ENOENT;
1470 if (btrfs_super_bytenr(buf) != sb_bytenr)
1471 return -EIO;
1473 ret = check_super(buf, sbflags);
1474 if (ret < 0)
1475 return ret;
1476 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1477 return 0;
1481 * we would like to check all the supers, but that would make
1482 * a btrfs mount succeed after a mkfs from a different FS.
1483 * So, we need to add a special mount option to scan for
1484 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1487 for (i = 0; i < max_super; i++) {
1488 bytenr = btrfs_sb_offset(i);
1489 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1490 if (ret < BTRFS_SUPER_INFO_SIZE)
1491 break;
1493 if (btrfs_super_bytenr(buf) != bytenr )
1494 continue;
1495 /* if magic is NULL, the device was removed */
1496 if (btrfs_super_magic(buf) == 0 && i == 0)
1497 break;
1498 if (check_super(buf, sbflags))
1499 continue;
1501 if (!fsid_is_initialized) {
1502 memcpy(fsid, buf->fsid, sizeof(fsid));
1503 fsid_is_initialized = 1;
1504 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1506 * the superblocks (the original one and
1507 * its backups) contain data of different
1508 * filesystems -> the super cannot be trusted
1510 continue;
1513 if (btrfs_super_generation(buf) > transid) {
1514 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1515 transid = btrfs_super_generation(buf);
1519 return transid > 0 ? 0 : -1;
1522 static int write_dev_supers(struct btrfs_fs_info *fs_info,
1523 struct btrfs_super_block *sb,
1524 struct btrfs_device *device)
1526 u64 bytenr;
1527 u32 crc;
1528 int i, ret;
1530 if (fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1531 btrfs_set_super_bytenr(sb, fs_info->super_bytenr);
1532 crc = ~(u32)0;
1533 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1534 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1535 btrfs_csum_final(crc, &sb->csum[0]);
1538 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1539 * zero filled, we can use it directly
1541 ret = pwrite64(device->fd, fs_info->super_copy,
1542 BTRFS_SUPER_INFO_SIZE,
1543 fs_info->super_bytenr);
1544 if (ret != BTRFS_SUPER_INFO_SIZE)
1545 goto write_err;
1546 return 0;
1549 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1550 bytenr = btrfs_sb_offset(i);
1551 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1552 break;
1554 btrfs_set_super_bytenr(sb, bytenr);
1556 crc = ~(u32)0;
1557 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1558 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1559 btrfs_csum_final(crc, &sb->csum[0]);
1562 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1563 * zero filled, we can use it directly
1565 ret = pwrite64(device->fd, fs_info->super_copy,
1566 BTRFS_SUPER_INFO_SIZE, bytenr);
1567 if (ret != BTRFS_SUPER_INFO_SIZE)
1568 goto write_err;
1571 return 0;
1573 write_err:
1574 if (ret > 0)
1575 fprintf(stderr, "WARNING: failed to write all sb data\n");
1576 else
1577 fprintf(stderr, "WARNING: failed to write sb: %m\n");
1578 return ret;
1581 int write_all_supers(struct btrfs_fs_info *fs_info)
1583 struct list_head *head = &fs_info->fs_devices->devices;
1584 struct btrfs_device *dev;
1585 struct btrfs_super_block *sb;
1586 struct btrfs_dev_item *dev_item;
1587 int ret;
1588 u64 flags;
1590 sb = fs_info->super_copy;
1591 dev_item = &sb->dev_item;
1592 list_for_each_entry(dev, head, dev_list) {
1593 if (!dev->writeable)
1594 continue;
1596 btrfs_set_stack_device_generation(dev_item, 0);
1597 btrfs_set_stack_device_type(dev_item, dev->type);
1598 btrfs_set_stack_device_id(dev_item, dev->devid);
1599 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1600 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1601 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1602 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1603 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1604 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1605 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1607 flags = btrfs_super_flags(sb);
1608 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1610 ret = write_dev_supers(fs_info, sb, dev);
1611 BUG_ON(ret);
1613 return 0;
1616 int write_ctree_super(struct btrfs_trans_handle *trans)
1618 int ret;
1619 struct btrfs_fs_info *fs_info = trans->fs_info;
1620 struct btrfs_root *tree_root = fs_info->tree_root;
1621 struct btrfs_root *chunk_root = fs_info->chunk_root;
1623 if (fs_info->readonly)
1624 return 0;
1626 btrfs_set_super_generation(fs_info->super_copy,
1627 trans->transid);
1628 btrfs_set_super_root(fs_info->super_copy,
1629 tree_root->node->start);
1630 btrfs_set_super_root_level(fs_info->super_copy,
1631 btrfs_header_level(tree_root->node));
1632 btrfs_set_super_chunk_root(fs_info->super_copy,
1633 chunk_root->node->start);
1634 btrfs_set_super_chunk_root_level(fs_info->super_copy,
1635 btrfs_header_level(chunk_root->node));
1636 btrfs_set_super_chunk_root_generation(fs_info->super_copy,
1637 btrfs_header_generation(chunk_root->node));
1639 ret = write_all_supers(fs_info);
1640 if (ret)
1641 fprintf(stderr, "failed to write new super block err %d\n", ret);
1642 return ret;
1645 int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1647 int ret;
1648 int err = 0;
1649 struct btrfs_trans_handle *trans;
1650 struct btrfs_root *root = fs_info->tree_root;
1652 if (fs_info->last_trans_committed !=
1653 fs_info->generation) {
1654 BUG_ON(!root);
1655 trans = btrfs_start_transaction(root, 1);
1656 if (IS_ERR(trans)) {
1657 err = PTR_ERR(trans);
1658 goto skip_commit;
1660 btrfs_commit_transaction(trans, root);
1661 trans = btrfs_start_transaction(root, 1);
1662 BUG_ON(IS_ERR(trans));
1663 ret = commit_tree_roots(trans, fs_info);
1664 BUG_ON(ret);
1665 ret = __commit_transaction(trans, root);
1666 BUG_ON(ret);
1667 write_ctree_super(trans);
1668 kfree(trans);
1671 if (fs_info->finalize_on_close) {
1672 btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
1673 root->fs_info->finalize_on_close = 0;
1674 ret = write_all_supers(fs_info);
1675 if (ret)
1676 fprintf(stderr,
1677 "failed to write new super block err %d\n", ret);
1680 skip_commit:
1681 btrfs_free_block_groups(fs_info);
1683 free_fs_roots_tree(&fs_info->fs_root_tree);
1685 btrfs_release_all_roots(fs_info);
1686 ret = btrfs_close_devices(fs_info->fs_devices);
1687 btrfs_cleanup_all_caches(fs_info);
1688 btrfs_free_fs_info(fs_info);
1689 if (!err)
1690 err = ret;
1691 return err;
1694 int clean_tree_block(struct extent_buffer *eb)
1696 return clear_extent_buffer_dirty(eb);
1699 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1701 set_extent_buffer_dirty(eb);
1704 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1706 int ret;
1708 ret = extent_buffer_uptodate(buf);
1709 if (!ret)
1710 return ret;
1712 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1713 return !ret;
1716 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1718 return set_extent_buffer_uptodate(eb);