blob: 4d08b0204ac29025b5435e86e86edca1209f854e [file] [log] [blame]
Qu Wenruo565a4142020-06-24 18:02:48 +02001// SPDX-License-Identifier: GPL-2.0+
2#include <common.h>
3#include <fs_internal.h>
Qu Wenruo4aebb992020-06-24 18:02:49 +02004#include <uuid.h>
5#include <memalign.h>
6#include "kernel-shared/btrfs_tree.h"
Qu Wenruof06bfcf2020-06-24 18:03:01 +02007#include "common/rbtree-utils.h"
Qu Wenruo565a4142020-06-24 18:02:48 +02008#include "disk-io.h"
Qu Wenruo4aebb992020-06-24 18:02:49 +02009#include "ctree.h"
10#include "btrfs.h"
Qu Wenruo75b08172020-06-24 18:02:55 +020011#include "volumes.h"
12#include "extent-io.h"
Qu Wenruo565a4142020-06-24 18:02:48 +020013#include "crypto/hash.h"
14
Qu Wenruo75b08172020-06-24 18:02:55 +020015/* specified errno for check_tree_block */
16#define BTRFS_BAD_BYTENR (-1)
17#define BTRFS_BAD_FSID (-2)
18#define BTRFS_BAD_LEVEL (-3)
19#define BTRFS_BAD_NRITEMS (-4)
20
21/* Calculate max possible nritems for a leaf/node */
22static u32 max_nritems(u8 level, u32 nodesize)
23{
24
25 if (level == 0)
26 return ((nodesize - sizeof(struct btrfs_header)) /
27 sizeof(struct btrfs_item));
28 return ((nodesize - sizeof(struct btrfs_header)) /
29 sizeof(struct btrfs_key_ptr));
30}
31
32static int check_tree_block(struct btrfs_fs_info *fs_info,
33 struct extent_buffer *buf)
34{
35
36 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
37 u32 nodesize = fs_info->nodesize;
38 bool fsid_match = false;
39 int ret = BTRFS_BAD_FSID;
40
41 if (buf->start != btrfs_header_bytenr(buf))
42 return BTRFS_BAD_BYTENR;
43 if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
44 return BTRFS_BAD_LEVEL;
45 if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
46 nodesize))
47 return BTRFS_BAD_NRITEMS;
48
49 /* Only leaf can be empty */
50 if (btrfs_header_nritems(buf) == 0 &&
51 btrfs_header_level(buf) != 0)
52 return BTRFS_BAD_NRITEMS;
53
54 while (fs_devices) {
55 /*
56 * Checking the incompat flag is only valid for the current
57 * fs. For seed devices it's forbidden to have their uuid
58 * changed so reading ->fsid in this case is fine
59 */
60 if (fs_devices == fs_info->fs_devices &&
61 btrfs_fs_incompat(fs_info, METADATA_UUID))
62 fsid_match = !memcmp_extent_buffer(buf,
63 fs_devices->metadata_uuid,
64 btrfs_header_fsid(),
65 BTRFS_FSID_SIZE);
66 else
67 fsid_match = !memcmp_extent_buffer(buf,
68 fs_devices->fsid,
69 btrfs_header_fsid(),
70 BTRFS_FSID_SIZE);
71
72
73 if (fsid_match) {
74 ret = 0;
75 break;
76 }
77 fs_devices = fs_devices->seed;
78 }
79 return ret;
80}
81
82static void print_tree_block_error(struct btrfs_fs_info *fs_info,
83 struct extent_buffer *eb,
84 int err)
85{
86 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
87 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
88 u8 buf[BTRFS_UUID_SIZE];
89
90 if (!err)
91 return;
92
93 fprintf(stderr, "bad tree block %llu, ", eb->start);
94 switch (err) {
95 case BTRFS_BAD_FSID:
96 read_extent_buffer(eb, buf, btrfs_header_fsid(),
97 BTRFS_UUID_SIZE);
98 uuid_unparse(buf, found_uuid);
99 uuid_unparse(fs_info->fs_devices->metadata_uuid, fs_uuid);
100 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
101 fs_uuid, found_uuid);
102 break;
103 case BTRFS_BAD_BYTENR:
104 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
105 eb->start, btrfs_header_bytenr(eb));
106 break;
107 case BTRFS_BAD_LEVEL:
108 fprintf(stderr, "bad level, %u > %d\n",
109 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
110 break;
111 case BTRFS_BAD_NRITEMS:
112 fprintf(stderr, "invalid nr_items: %u\n",
113 btrfs_header_nritems(eb));
114 break;
115 }
116}
117
Qu Wenruo565a4142020-06-24 18:02:48 +0200118int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
119{
120 memset(out, 0, BTRFS_CSUM_SIZE);
121
122 switch (csum_type) {
123 case BTRFS_CSUM_TYPE_CRC32:
124 return hash_crc32c(data, len, out);
125 case BTRFS_CSUM_TYPE_XXHASH:
126 return hash_xxhash(data, len, out);
127 case BTRFS_CSUM_TYPE_SHA256:
128 return hash_sha256(data, len, out);
129 default:
130 printf("Unknown csum type %d\n", csum_type);
131 return -EINVAL;
132 }
133}
Qu Wenruo4aebb992020-06-24 18:02:49 +0200134
135/*
136 * Check if the super is valid:
137 * - nodesize/sectorsize - minimum, maximum, alignment
138 * - tree block starts - alignment
139 * - number of devices - something sane
140 * - sys array size - maximum
141 */
142static int btrfs_check_super(struct btrfs_super_block *sb)
143{
144 u8 result[BTRFS_CSUM_SIZE];
145 u16 csum_type;
146 int csum_size;
147 u8 *metadata_uuid;
148
149 if (btrfs_super_magic(sb) != BTRFS_MAGIC)
150 return -EIO;
151
152 csum_type = btrfs_super_csum_type(sb);
153 if (csum_type >= btrfs_super_num_csums()) {
154 error("unsupported checksum algorithm %u", csum_type);
155 return -EIO;
156 }
157 csum_size = btrfs_super_csum_size(sb);
158
159 btrfs_csum_data(csum_type, (u8 *)sb + BTRFS_CSUM_SIZE,
160 result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
161
162 if (memcmp(result, sb->csum, csum_size)) {
163 error("superblock checksum mismatch");
164 return -EIO;
165 }
166 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
167 error("tree_root level too big: %d >= %d",
168 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
169 goto error_out;
170 }
171 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
172 error("chunk_root level too big: %d >= %d",
173 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
174 goto error_out;
175 }
176 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
177 error("log_root level too big: %d >= %d",
178 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
179 goto error_out;
180 }
181
182 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
183 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
184 goto error_out;
185 }
186 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
187 error("chunk_root block unaligned: %llu",
188 btrfs_super_chunk_root(sb));
189 goto error_out;
190 }
191 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
192 error("log_root block unaligned: %llu",
193 btrfs_super_log_root(sb));
194 goto error_out;
195 }
196 if (btrfs_super_nodesize(sb) < 4096) {
197 error("nodesize too small: %u < 4096",
198 btrfs_super_nodesize(sb));
199 goto error_out;
200 }
201 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
202 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
203 goto error_out;
204 }
205 if (btrfs_super_sectorsize(sb) < 4096) {
206 error("sectorsize too small: %u < 4096",
207 btrfs_super_sectorsize(sb));
208 goto error_out;
209 }
210 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
211 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
212 goto error_out;
213 }
214 if (btrfs_super_total_bytes(sb) == 0) {
215 error("invalid total_bytes 0");
216 goto error_out;
217 }
218 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
219 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
220 goto error_out;
221 }
222 if ((btrfs_super_stripesize(sb) != 4096)
223 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
224 error("invalid stripesize %u", btrfs_super_stripesize(sb));
225 goto error_out;
226 }
227
228 if (btrfs_super_incompat_flags(sb) & BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
229 metadata_uuid = sb->metadata_uuid;
230 else
231 metadata_uuid = sb->fsid;
232
233 if (memcmp(metadata_uuid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
234 char fsid[BTRFS_UUID_UNPARSED_SIZE];
235 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
236
237 uuid_unparse(sb->metadata_uuid, fsid);
238 uuid_unparse(sb->dev_item.fsid, dev_fsid);
239 error("dev_item UUID does not match fsid: %s != %s",
240 dev_fsid, fsid);
241 goto error_out;
242 }
243
244 /*
245 * Hint to catch really bogus numbers, bitflips or so
246 */
247 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
248 error("suspicious number of devices: %llu",
249 btrfs_super_num_devices(sb));
250 }
251
252 if (btrfs_super_num_devices(sb) == 0) {
253 error("number of devices is 0");
254 goto error_out;
255 }
256
257 /*
258 * Obvious sys_chunk_array corruptions, it must hold at least one key
259 * and one chunk
260 */
261 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
262 error("system chunk array too big %u > %u",
263 btrfs_super_sys_array_size(sb),
264 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
265 goto error_out;
266 }
267 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
268 + sizeof(struct btrfs_chunk)) {
269 error("system chunk array too small %u < %zu",
270 btrfs_super_sys_array_size(sb),
271 sizeof(struct btrfs_disk_key) +
272 sizeof(struct btrfs_chunk));
273 goto error_out;
274 }
275
276 return 0;
277
278error_out:
279 error("superblock checksum matches but it has invalid members");
280 return -EIO;
281}
282
283/*
284 * btrfs_read_dev_super - read a valid primary superblock from a block device
285 * @desc,@part: file descriptor of the device
286 * @sb: buffer where the superblock is going to be read in
287 *
288 * Unlike the btrfs-progs/kernel version, here we ony care about the first
289 * super block, thus it's much simpler.
290 */
291int btrfs_read_dev_super(struct blk_desc *desc, struct disk_partition *part,
292 struct btrfs_super_block *sb)
293{
294 char tmp[BTRFS_SUPER_INFO_SIZE];
295 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
296 int ret;
297
298 ret = __btrfs_devread(desc, part, tmp, BTRFS_SUPER_INFO_SIZE,
299 BTRFS_SUPER_INFO_OFFSET);
300 if (ret < BTRFS_SUPER_INFO_SIZE)
301 return -EIO;
302
303 if (btrfs_super_bytenr(buf) != BTRFS_SUPER_INFO_OFFSET)
304 return -EIO;
305
306 if (btrfs_check_super(buf))
307 return -EIO;
308
309 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
310 return 0;
311}
312
313int btrfs_read_superblock(void)
314{
315 ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
316 struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
317 int ret;
318
319
320 btrfs_info.sb.generation = 0;
321
322 ret = btrfs_read_dev_super(btrfs_blk_desc, btrfs_part_info, sb);
323 if (ret < 0) {
324 pr_debug("%s: No valid BTRFS superblock found!\n", __func__);
325 return ret;
326 }
327 btrfs_super_block_to_cpu(sb);
328 memcpy(&btrfs_info.sb, sb, sizeof(*sb));
329
330 if (btrfs_info.sb.num_devices != 1) {
331 printf("%s: Unsupported number of devices (%lli). This driver "
332 "only supports filesystem on one device.\n", __func__,
333 btrfs_info.sb.num_devices);
334 return -1;
335 }
336
337 pr_debug("Chosen superblock with generation = %llu\n",
338 btrfs_info.sb.generation);
339
340 return 0;
341}
Qu Wenruo75b08172020-06-24 18:02:55 +0200342
343static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
344 int verify, int silent, u16 csum_type)
345{
346 u8 result[BTRFS_CSUM_SIZE];
347 u32 len;
348
349 len = buf->len - BTRFS_CSUM_SIZE;
350 btrfs_csum_data(csum_type, (u8 *)buf->data + BTRFS_CSUM_SIZE,
351 result, len);
352
353 if (verify) {
354 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
355 /* FIXME: format */
356 if (!silent)
357 printk("checksum verify failed on %llu found %08X wanted %08X\n",
358 (unsigned long long)buf->start,
359 result[0],
360 buf->data[0]);
361 return 1;
362 }
363 } else {
364 write_extent_buffer(buf, result, 0, csum_size);
365 }
366 return 0;
367}
368
369int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify,
370 u16 csum_type)
371{
372 return __csum_tree_block_size(buf, csum_size, verify, 0, csum_type);
373}
374
375static int csum_tree_block(struct btrfs_fs_info *fs_info,
376 struct extent_buffer *buf, int verify)
377{
378 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
379 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
380
381 return csum_tree_block_size(buf, csum_size, verify, csum_type);
382}
383
384struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
385 u64 bytenr, u32 blocksize)
386{
387 return find_extent_buffer(&fs_info->extent_cache,
388 bytenr, blocksize);
389}
390
391struct extent_buffer* btrfs_find_create_tree_block(
392 struct btrfs_fs_info *fs_info, u64 bytenr)
393{
394 return alloc_extent_buffer(fs_info, bytenr, fs_info->nodesize);
395}
396
397static int verify_parent_transid(struct extent_io_tree *io_tree,
398 struct extent_buffer *eb, u64 parent_transid,
399 int ignore)
400{
401 int ret;
402
403 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
404 return 0;
405
406 if (extent_buffer_uptodate(eb) &&
407 btrfs_header_generation(eb) == parent_transid) {
408 ret = 0;
409 goto out;
410 }
411 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
412 (unsigned long long)eb->start,
413 (unsigned long long)parent_transid,
414 (unsigned long long)btrfs_header_generation(eb));
415 if (ignore) {
416 eb->flags |= EXTENT_BAD_TRANSID;
417 printk("Ignoring transid failure\n");
418 return 0;
419 }
420
421 ret = 1;
422out:
423 clear_extent_buffer_uptodate(eb);
424 return ret;
425
426}
427
Qu Wenruo75b08172020-06-24 18:02:55 +0200428int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
429{
430 unsigned long offset = 0;
431 struct btrfs_multi_bio *multi = NULL;
432 struct btrfs_device *device;
433 int ret = 0;
434 u64 read_len;
435 unsigned long bytes_left = eb->len;
436
437 while (bytes_left) {
438 read_len = bytes_left;
439 device = NULL;
440
441 ret = btrfs_map_block(info, READ, eb->start + offset,
442 &read_len, &multi, mirror, NULL);
443 if (ret) {
444 printk("Couldn't map the block %Lu\n", eb->start + offset);
445 kfree(multi);
446 return -EIO;
447 }
448 device = multi->stripes[0].dev;
449
450 if (!device->desc || !device->part) {
451 kfree(multi);
452 return -EIO;
453 }
454
455 if (read_len > bytes_left)
456 read_len = bytes_left;
457
458 ret = read_extent_from_disk(device->desc, device->part,
459 multi->stripes[0].physical, eb,
460 offset, read_len);
461 kfree(multi);
462 multi = NULL;
463
464 if (ret)
465 return -EIO;
466 offset += read_len;
467 bytes_left -= read_len;
468 }
469 return 0;
470}
471
472struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
473 u64 parent_transid)
474{
475 int ret;
476 struct extent_buffer *eb;
477 u64 best_transid = 0;
478 u32 sectorsize = fs_info->sectorsize;
479 int mirror_num = 1;
480 int good_mirror = 0;
481 int candidate_mirror = 0;
482 int num_copies;
483 int ignore = 0;
484
485 /*
486 * Don't even try to create tree block for unaligned tree block
487 * bytenr.
488 * Such unaligned tree block will free overlapping extent buffer,
489 * causing use-after-free bugs for fuzzed images.
490 */
491 if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
492 error("tree block bytenr %llu is not aligned to sectorsize %u",
493 bytenr, sectorsize);
494 return ERR_PTR(-EIO);
495 }
496
497 eb = btrfs_find_create_tree_block(fs_info, bytenr);
498 if (!eb)
499 return ERR_PTR(-ENOMEM);
500
501 if (btrfs_buffer_uptodate(eb, parent_transid))
502 return eb;
503
504 num_copies = btrfs_num_copies(fs_info, eb->start, eb->len);
505 while (1) {
506 ret = read_whole_eb(fs_info, eb, mirror_num);
507 if (ret == 0 && csum_tree_block(fs_info, eb, 1) == 0 &&
508 check_tree_block(fs_info, eb) == 0 &&
509 verify_parent_transid(&fs_info->extent_cache, eb,
510 parent_transid, ignore) == 0) {
511 /*
512 * check_tree_block() is less strict to allow btrfs
513 * check to get raw eb with bad key order and fix it.
514 * But we still need to try to get a good copy if
515 * possible, or bad key order can go into tools like
516 * btrfs ins dump-tree.
517 */
518 if (btrfs_header_level(eb))
519 ret = btrfs_check_node(fs_info, NULL, eb);
520 else
521 ret = btrfs_check_leaf(fs_info, NULL, eb);
522 if (!ret || candidate_mirror == mirror_num) {
523 btrfs_set_buffer_uptodate(eb);
524 return eb;
525 }
526 if (candidate_mirror <= 0)
527 candidate_mirror = mirror_num;
528 }
529 if (ignore) {
530 if (candidate_mirror > 0) {
531 mirror_num = candidate_mirror;
532 continue;
533 }
534 if (check_tree_block(fs_info, eb))
535 print_tree_block_error(fs_info, eb,
536 check_tree_block(fs_info, eb));
537 else
538 fprintf(stderr, "Csum didn't match\n");
539 ret = -EIO;
540 break;
541 }
542 if (num_copies == 1) {
543 ignore = 1;
544 continue;
545 }
546 if (btrfs_header_generation(eb) > best_transid) {
547 best_transid = btrfs_header_generation(eb);
548 good_mirror = mirror_num;
549 }
550 mirror_num++;
551 if (mirror_num > num_copies) {
552 if (candidate_mirror > 0)
553 mirror_num = candidate_mirror;
554 else
555 mirror_num = good_mirror;
556 ignore = 1;
557 continue;
558 }
559 }
560 /*
561 * We failed to read this tree block, it be should deleted right now
562 * to avoid stale cache populate the cache.
563 */
564 free_extent_buffer(eb);
565 return ERR_PTR(ret);
566}
Qu Wenruof06bfcf2020-06-24 18:03:01 +0200567
568void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
569 u64 objectid)
570{
571 root->node = NULL;
572 root->track_dirty = 0;
573
574 root->fs_info = fs_info;
575 root->objectid = objectid;
576 root->last_trans = 0;
577 root->last_inode_alloc = 0;
578
579 memset(&root->root_key, 0, sizeof(root->root_key));
580 memset(&root->root_item, 0, sizeof(root->root_item));
581 root->root_key.objectid = objectid;
582}
583
584static int find_and_setup_root(struct btrfs_root *tree_root,
585 struct btrfs_fs_info *fs_info,
586 u64 objectid, struct btrfs_root *root)
587{
588 int ret;
589 u64 generation;
590
591 btrfs_setup_root(root, fs_info, objectid);
592 ret = btrfs_find_last_root(tree_root, objectid,
593 &root->root_item, &root->root_key);
594 if (ret)
595 return ret;
596
597 generation = btrfs_root_generation(&root->root_item);
598 root->node = read_tree_block(fs_info,
599 btrfs_root_bytenr(&root->root_item), generation);
600 if (!extent_buffer_uptodate(root->node))
601 return -EIO;
602
603 return 0;
604}
605
606int btrfs_free_fs_root(struct btrfs_root *root)
607{
608 if (root->node)
609 free_extent_buffer(root->node);
610 kfree(root);
611 return 0;
612}
613
614static void __free_fs_root(struct rb_node *node)
615{
616 struct btrfs_root *root;
617
618 root = container_of(node, struct btrfs_root, rb_node);
619 btrfs_free_fs_root(root);
620}
621
622FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
623
624struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
625 struct btrfs_key *location)
626{
627 struct btrfs_root *root;
628 struct btrfs_root *tree_root = fs_info->tree_root;
629 struct btrfs_path *path;
630 struct extent_buffer *l;
631 u64 generation;
632 int ret = 0;
633
634 root = calloc(1, sizeof(*root));
635 if (!root)
636 return ERR_PTR(-ENOMEM);
637 if (location->offset == (u64)-1) {
638 ret = find_and_setup_root(tree_root, fs_info,
639 location->objectid, root);
640 if (ret) {
641 free(root);
642 return ERR_PTR(ret);
643 }
644 goto insert;
645 }
646
647 btrfs_setup_root(root, fs_info,
648 location->objectid);
649
650 path = btrfs_alloc_path();
651 if (!path) {
652 free(root);
653 return ERR_PTR(-ENOMEM);
654 }
655
656 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
657 if (ret != 0) {
658 if (ret > 0)
659 ret = -ENOENT;
660 goto out;
661 }
662 l = path->nodes[0];
663 read_extent_buffer(l, &root->root_item,
664 btrfs_item_ptr_offset(l, path->slots[0]),
665 sizeof(root->root_item));
666 memcpy(&root->root_key, location, sizeof(*location));
667
668 /* If this root is already an orphan, no need to read */
669 if (btrfs_root_refs(&root->root_item) == 0) {
670 ret = -ENOENT;
671 goto out;
672 }
673 ret = 0;
674out:
675 btrfs_free_path(path);
676 if (ret) {
677 free(root);
678 return ERR_PTR(ret);
679 }
680 generation = btrfs_root_generation(&root->root_item);
681 root->node = read_tree_block(fs_info,
682 btrfs_root_bytenr(&root->root_item), generation);
683 if (!extent_buffer_uptodate(root->node)) {
684 free(root);
685 return ERR_PTR(-EIO);
686 }
687insert:
688 root->ref_cows = 1;
689 return root;
690}
691
692static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
693 void *data)
694{
695 u64 objectid = *((u64 *)data);
696 struct btrfs_root *root;
697
698 root = rb_entry(node, struct btrfs_root, rb_node);
699 if (objectid > root->objectid)
700 return 1;
701 else if (objectid < root->objectid)
702 return -1;
703 else
704 return 0;
705}
706
707int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
708{
709 struct btrfs_root *root;
710
711 root = rb_entry(node2, struct btrfs_root, rb_node);
712 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
713}
714
715struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
716 struct btrfs_key *location)
717{
718 struct btrfs_root *root;
719 struct rb_node *node;
720 int ret;
721 u64 objectid = location->objectid;
722
723 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
724 return fs_info->tree_root;
725 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
726 return fs_info->chunk_root;
727 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
728 return fs_info->csum_root;
729 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
730 location->offset != (u64)-1);
731
732 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
733 btrfs_fs_roots_compare_objectids, NULL);
734 if (node)
735 return container_of(node, struct btrfs_root, rb_node);
736
737 root = btrfs_read_fs_root_no_cache(fs_info, location);
738 if (IS_ERR(root))
739 return root;
740
741 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
742 btrfs_fs_roots_compare_roots);
743 BUG_ON(ret);
744 return root;
745}
746
747void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
748{
749 free(fs_info->tree_root);
750 free(fs_info->chunk_root);
751 free(fs_info->csum_root);
752 free(fs_info->super_copy);
753 free(fs_info);
754}
755
756struct btrfs_fs_info *btrfs_new_fs_info(void)
757{
758 struct btrfs_fs_info *fs_info;
759
760 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
761 if (!fs_info)
762 return NULL;
763
764 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
765 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
766 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
767 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
768
769 if (!fs_info->tree_root || !fs_info->chunk_root ||
770 !fs_info->csum_root || !fs_info->super_copy)
771 goto free_all;
772
773 extent_io_tree_init(&fs_info->extent_cache);
774
775 fs_info->fs_root_tree = RB_ROOT;
776 cache_tree_init(&fs_info->mapping_tree.cache_tree);
777
778 mutex_init(&fs_info->fs_mutex);
779
780 return fs_info;
781free_all:
782 btrfs_free_fs_info(fs_info);
783 return NULL;
784}
785
786static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
787 struct btrfs_root *info_root,
788 u64 objectid, char *str)
789{
790 struct btrfs_root *root = fs_info->tree_root;
791 int ret;
792
793 ret = find_and_setup_root(root, fs_info, objectid, info_root);
794 if (ret) {
795 error("could not setup %s tree", str);
796 return -EIO;
797 }
798
799 return 0;
800}
801
802int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info)
803{
804 struct btrfs_super_block *sb = fs_info->super_copy;
805 struct btrfs_root *root;
806 struct btrfs_key key;
807 u64 root_tree_bytenr;
808 u64 generation;
809 int ret;
810
811 root = fs_info->tree_root;
812 btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
813 generation = btrfs_super_generation(sb);
814
815 root_tree_bytenr = btrfs_super_root(sb);
816
817 root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
818 if (!extent_buffer_uptodate(root->node)) {
819 fprintf(stderr, "Couldn't read tree root\n");
820 return -EIO;
821 }
822
823 ret = setup_root_or_create_block(fs_info, fs_info->csum_root,
824 BTRFS_CSUM_TREE_OBJECTID, "csum");
825 if (ret)
826 return ret;
827 fs_info->csum_root->track_dirty = 1;
828
829 fs_info->last_trans_committed = generation;
830
831 key.objectid = BTRFS_FS_TREE_OBJECTID;
832 key.type = BTRFS_ROOT_ITEM_KEY;
833 key.offset = (u64)-1;
834 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
835
836 if (IS_ERR(fs_info->fs_root))
837 return -EIO;
838 return 0;
839}
840
841void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
842{
843 if (fs_info->csum_root)
844 free_extent_buffer(fs_info->csum_root->node);
845 if (fs_info->tree_root)
846 free_extent_buffer(fs_info->tree_root->node);
847 if (fs_info->chunk_root)
848 free_extent_buffer(fs_info->chunk_root->node);
849}
850
851static void free_map_lookup(struct cache_extent *ce)
852{
853 struct map_lookup *map;
854
855 map = container_of(ce, struct map_lookup, ce);
856 kfree(map);
857}
858
859FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
860
861void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
862{
863 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
864 extent_io_tree_cleanup(&fs_info->extent_cache);
865}
866
867static int btrfs_scan_fs_devices(struct blk_desc *desc,
868 struct disk_partition *part,
869 struct btrfs_fs_devices **fs_devices)
870{
871 u64 total_devs;
872 int ret;
873
874 if (round_up(BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
875 desc->blksz) > (part->size << desc->log2blksz)) {
876 error("superblock end %u is larger than device size " LBAFU,
877 BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
878 part->size << desc->log2blksz);
879 return -EINVAL;
880 }
881
882 ret = btrfs_scan_one_device(desc, part, fs_devices, &total_devs);
883 if (ret) {
884 fprintf(stderr, "No valid Btrfs found\n");
885 return ret;
886 }
887 return 0;
888}
889
890int btrfs_check_fs_compatibility(struct btrfs_super_block *sb)
891{
892 u64 features;
893
894 features = btrfs_super_incompat_flags(sb) &
895 ~BTRFS_FEATURE_INCOMPAT_SUPP;
896 if (features) {
897 printk("couldn't open because of unsupported "
898 "option features (%llx).\n",
899 (unsigned long long)features);
900 return -ENOTSUPP;
901 }
902
903 features = btrfs_super_incompat_flags(sb);
904 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
905 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
906 btrfs_set_super_incompat_flags(sb, features);
907 }
908
909 return 0;
910}
911
912static int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
913{
914 struct btrfs_super_block *sb = fs_info->super_copy;
915 u64 chunk_root_bytenr;
916 u64 generation;
917 int ret;
918
919 btrfs_setup_root(fs_info->chunk_root, fs_info,
920 BTRFS_CHUNK_TREE_OBJECTID);
921
922 ret = btrfs_read_sys_array(fs_info);
923 if (ret)
924 return ret;
925
926 generation = btrfs_super_chunk_root_generation(sb);
927 chunk_root_bytenr = btrfs_super_chunk_root(sb);
928
929 fs_info->chunk_root->node = read_tree_block(fs_info,
930 chunk_root_bytenr,
931 generation);
932 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
933 error("cannot read chunk root");
934 return -EIO;
935 }
936
937 ret = btrfs_read_chunk_tree(fs_info);
938 if (ret) {
939 fprintf(stderr, "Couldn't read chunk tree\n");
940 return ret;
941 }
942 return 0;
943}
944
945struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
946 struct disk_partition *part)
947{
948 struct btrfs_fs_info *fs_info;
949 struct btrfs_super_block *disk_super;
950 struct btrfs_fs_devices *fs_devices = NULL;
951 struct extent_buffer *eb;
952 int ret;
953
954 fs_info = btrfs_new_fs_info();
955 if (!fs_info) {
956 fprintf(stderr, "Failed to allocate memory for fs_info\n");
957 return NULL;
958 }
959
960 ret = btrfs_scan_fs_devices(desc, part, &fs_devices);
961 if (ret)
962 goto out;
963
964 fs_info->fs_devices = fs_devices;
965
966 ret = btrfs_open_devices(fs_devices);
967 if (ret)
968 goto out;
969
970 disk_super = fs_info->super_copy;
971 ret = btrfs_read_dev_super(desc, part, disk_super);
972 if (ret) {
973 printk("No valid btrfs found\n");
974 goto out_devices;
975 }
976
977 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID) {
978 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
979 goto out_devices;
980 }
981
982 ASSERT(!memcmp(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE));
983 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
984 ASSERT(!memcmp(disk_super->metadata_uuid,
985 fs_devices->metadata_uuid, BTRFS_FSID_SIZE));
986
987 fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
988 fs_info->nodesize = btrfs_super_nodesize(disk_super);
989 fs_info->stripesize = btrfs_super_stripesize(disk_super);
990
991 ret = btrfs_check_fs_compatibility(fs_info->super_copy);
992 if (ret)
993 goto out_devices;
994
995 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
996 if (ret)
997 goto out_chunk;
998
999 /* Chunk tree root is unable to read, return directly */
1000 if (!fs_info->chunk_root)
1001 return fs_info;
1002
1003 eb = fs_info->chunk_root->node;
1004 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1005 btrfs_header_chunk_tree_uuid(eb),
1006 BTRFS_UUID_SIZE);
1007
1008 ret = btrfs_setup_all_roots(fs_info);
1009 if (ret)
1010 goto out_chunk;
1011
1012 return fs_info;
1013
1014out_chunk:
1015 btrfs_release_all_roots(fs_info);
1016 btrfs_cleanup_all_caches(fs_info);
1017out_devices:
1018 btrfs_close_devices(fs_devices);
1019out:
1020 btrfs_free_fs_info(fs_info);
1021 return NULL;
1022}
1023
1024int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1025{
1026 int ret;
1027 int err = 0;
1028
1029 free_fs_roots_tree(&fs_info->fs_root_tree);
1030
1031 btrfs_release_all_roots(fs_info);
1032 ret = btrfs_close_devices(fs_info->fs_devices);
1033 btrfs_cleanup_all_caches(fs_info);
1034 btrfs_free_fs_info(fs_info);
1035 if (!err)
1036 err = ret;
1037 return err;
1038}
1039
1040int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1041{
1042 int ret;
1043
1044 ret = extent_buffer_uptodate(buf);
1045 if (!ret)
1046 return ret;
1047
1048 ret = verify_parent_transid(&buf->fs_info->extent_cache, buf,
1049 parent_transid, 1);
1050 return !ret;
1051}
1052
1053int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1054{
1055 return set_extent_buffer_uptodate(eb);
1056}