blob: 4da1b800387d101f452801f11d24160e3ce711a2 [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
Qu Wenruoa26a6be2020-06-24 18:03:09 +0200568int read_extent_data(struct btrfs_fs_info *fs_info, char *data, u64 logical,
569 u64 *len, int mirror)
570{
571 u64 offset = 0;
572 struct btrfs_multi_bio *multi = NULL;
573 struct btrfs_device *device;
574 int ret = 0;
575 u64 max_len = *len;
576
577 ret = btrfs_map_block(fs_info, READ, logical, len, &multi, mirror,
578 NULL);
579 if (ret) {
580 fprintf(stderr, "Couldn't map the block %llu\n",
581 logical + offset);
582 goto err;
583 }
584 device = multi->stripes[0].dev;
585
586 if (*len > max_len)
587 *len = max_len;
588 if (!device->desc || !device->part) {
589 ret = -EIO;
590 goto err;
591 }
592
593 ret = __btrfs_devread(device->desc, device->part, data, *len,
594 multi->stripes[0].physical);
595 if (ret != *len)
596 ret = -EIO;
597 else
598 ret = 0;
599err:
600 kfree(multi);
601 return ret;
602}
603
Qu Wenruof06bfcf2020-06-24 18:03:01 +0200604void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
605 u64 objectid)
606{
607 root->node = NULL;
608 root->track_dirty = 0;
609
610 root->fs_info = fs_info;
611 root->objectid = objectid;
612 root->last_trans = 0;
613 root->last_inode_alloc = 0;
614
615 memset(&root->root_key, 0, sizeof(root->root_key));
616 memset(&root->root_item, 0, sizeof(root->root_item));
617 root->root_key.objectid = objectid;
618}
619
620static int find_and_setup_root(struct btrfs_root *tree_root,
621 struct btrfs_fs_info *fs_info,
622 u64 objectid, struct btrfs_root *root)
623{
624 int ret;
625 u64 generation;
626
627 btrfs_setup_root(root, fs_info, objectid);
628 ret = btrfs_find_last_root(tree_root, objectid,
629 &root->root_item, &root->root_key);
630 if (ret)
631 return ret;
632
633 generation = btrfs_root_generation(&root->root_item);
634 root->node = read_tree_block(fs_info,
635 btrfs_root_bytenr(&root->root_item), generation);
636 if (!extent_buffer_uptodate(root->node))
637 return -EIO;
638
639 return 0;
640}
641
642int btrfs_free_fs_root(struct btrfs_root *root)
643{
644 if (root->node)
645 free_extent_buffer(root->node);
646 kfree(root);
647 return 0;
648}
649
650static void __free_fs_root(struct rb_node *node)
651{
652 struct btrfs_root *root;
653
654 root = container_of(node, struct btrfs_root, rb_node);
655 btrfs_free_fs_root(root);
656}
657
658FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
659
660struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
661 struct btrfs_key *location)
662{
663 struct btrfs_root *root;
664 struct btrfs_root *tree_root = fs_info->tree_root;
665 struct btrfs_path *path;
666 struct extent_buffer *l;
667 u64 generation;
668 int ret = 0;
669
670 root = calloc(1, sizeof(*root));
671 if (!root)
672 return ERR_PTR(-ENOMEM);
673 if (location->offset == (u64)-1) {
674 ret = find_and_setup_root(tree_root, fs_info,
675 location->objectid, root);
676 if (ret) {
677 free(root);
678 return ERR_PTR(ret);
679 }
680 goto insert;
681 }
682
683 btrfs_setup_root(root, fs_info,
684 location->objectid);
685
686 path = btrfs_alloc_path();
687 if (!path) {
688 free(root);
689 return ERR_PTR(-ENOMEM);
690 }
691
692 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
693 if (ret != 0) {
694 if (ret > 0)
695 ret = -ENOENT;
696 goto out;
697 }
698 l = path->nodes[0];
699 read_extent_buffer(l, &root->root_item,
700 btrfs_item_ptr_offset(l, path->slots[0]),
701 sizeof(root->root_item));
702 memcpy(&root->root_key, location, sizeof(*location));
703
704 /* If this root is already an orphan, no need to read */
705 if (btrfs_root_refs(&root->root_item) == 0) {
706 ret = -ENOENT;
707 goto out;
708 }
709 ret = 0;
710out:
711 btrfs_free_path(path);
712 if (ret) {
713 free(root);
714 return ERR_PTR(ret);
715 }
716 generation = btrfs_root_generation(&root->root_item);
717 root->node = read_tree_block(fs_info,
718 btrfs_root_bytenr(&root->root_item), generation);
719 if (!extent_buffer_uptodate(root->node)) {
720 free(root);
721 return ERR_PTR(-EIO);
722 }
723insert:
724 root->ref_cows = 1;
725 return root;
726}
727
728static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
729 void *data)
730{
731 u64 objectid = *((u64 *)data);
732 struct btrfs_root *root;
733
734 root = rb_entry(node, struct btrfs_root, rb_node);
735 if (objectid > root->objectid)
736 return 1;
737 else if (objectid < root->objectid)
738 return -1;
739 else
740 return 0;
741}
742
743int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
744{
745 struct btrfs_root *root;
746
747 root = rb_entry(node2, struct btrfs_root, rb_node);
748 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
749}
750
751struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
752 struct btrfs_key *location)
753{
754 struct btrfs_root *root;
755 struct rb_node *node;
756 int ret;
757 u64 objectid = location->objectid;
758
759 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
760 return fs_info->tree_root;
761 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
762 return fs_info->chunk_root;
763 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
764 return fs_info->csum_root;
765 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
766 location->offset != (u64)-1);
767
768 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
769 btrfs_fs_roots_compare_objectids, NULL);
770 if (node)
771 return container_of(node, struct btrfs_root, rb_node);
772
773 root = btrfs_read_fs_root_no_cache(fs_info, location);
774 if (IS_ERR(root))
775 return root;
776
777 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
778 btrfs_fs_roots_compare_roots);
779 BUG_ON(ret);
780 return root;
781}
782
783void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
784{
785 free(fs_info->tree_root);
786 free(fs_info->chunk_root);
787 free(fs_info->csum_root);
788 free(fs_info->super_copy);
789 free(fs_info);
790}
791
792struct btrfs_fs_info *btrfs_new_fs_info(void)
793{
794 struct btrfs_fs_info *fs_info;
795
796 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
797 if (!fs_info)
798 return NULL;
799
800 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
801 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
802 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
803 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
804
805 if (!fs_info->tree_root || !fs_info->chunk_root ||
806 !fs_info->csum_root || !fs_info->super_copy)
807 goto free_all;
808
809 extent_io_tree_init(&fs_info->extent_cache);
810
811 fs_info->fs_root_tree = RB_ROOT;
812 cache_tree_init(&fs_info->mapping_tree.cache_tree);
813
814 mutex_init(&fs_info->fs_mutex);
815
816 return fs_info;
817free_all:
818 btrfs_free_fs_info(fs_info);
819 return NULL;
820}
821
822static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
823 struct btrfs_root *info_root,
824 u64 objectid, char *str)
825{
826 struct btrfs_root *root = fs_info->tree_root;
827 int ret;
828
829 ret = find_and_setup_root(root, fs_info, objectid, info_root);
830 if (ret) {
831 error("could not setup %s tree", str);
832 return -EIO;
833 }
834
835 return 0;
836}
837
838int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info)
839{
840 struct btrfs_super_block *sb = fs_info->super_copy;
841 struct btrfs_root *root;
842 struct btrfs_key key;
843 u64 root_tree_bytenr;
844 u64 generation;
845 int ret;
846
847 root = fs_info->tree_root;
848 btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
849 generation = btrfs_super_generation(sb);
850
851 root_tree_bytenr = btrfs_super_root(sb);
852
853 root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
854 if (!extent_buffer_uptodate(root->node)) {
855 fprintf(stderr, "Couldn't read tree root\n");
856 return -EIO;
857 }
858
859 ret = setup_root_or_create_block(fs_info, fs_info->csum_root,
860 BTRFS_CSUM_TREE_OBJECTID, "csum");
861 if (ret)
862 return ret;
863 fs_info->csum_root->track_dirty = 1;
864
865 fs_info->last_trans_committed = generation;
866
867 key.objectid = BTRFS_FS_TREE_OBJECTID;
868 key.type = BTRFS_ROOT_ITEM_KEY;
869 key.offset = (u64)-1;
870 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
871
872 if (IS_ERR(fs_info->fs_root))
873 return -EIO;
874 return 0;
875}
876
877void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
878{
879 if (fs_info->csum_root)
880 free_extent_buffer(fs_info->csum_root->node);
881 if (fs_info->tree_root)
882 free_extent_buffer(fs_info->tree_root->node);
883 if (fs_info->chunk_root)
884 free_extent_buffer(fs_info->chunk_root->node);
885}
886
887static void free_map_lookup(struct cache_extent *ce)
888{
889 struct map_lookup *map;
890
891 map = container_of(ce, struct map_lookup, ce);
892 kfree(map);
893}
894
895FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
896
897void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
898{
899 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
900 extent_io_tree_cleanup(&fs_info->extent_cache);
901}
902
903static int btrfs_scan_fs_devices(struct blk_desc *desc,
904 struct disk_partition *part,
905 struct btrfs_fs_devices **fs_devices)
906{
907 u64 total_devs;
908 int ret;
909
910 if (round_up(BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
911 desc->blksz) > (part->size << desc->log2blksz)) {
912 error("superblock end %u is larger than device size " LBAFU,
913 BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
914 part->size << desc->log2blksz);
915 return -EINVAL;
916 }
917
918 ret = btrfs_scan_one_device(desc, part, fs_devices, &total_devs);
919 if (ret) {
920 fprintf(stderr, "No valid Btrfs found\n");
921 return ret;
922 }
923 return 0;
924}
925
926int btrfs_check_fs_compatibility(struct btrfs_super_block *sb)
927{
928 u64 features;
929
930 features = btrfs_super_incompat_flags(sb) &
931 ~BTRFS_FEATURE_INCOMPAT_SUPP;
932 if (features) {
933 printk("couldn't open because of unsupported "
934 "option features (%llx).\n",
935 (unsigned long long)features);
936 return -ENOTSUPP;
937 }
938
939 features = btrfs_super_incompat_flags(sb);
940 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
941 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
942 btrfs_set_super_incompat_flags(sb, features);
943 }
944
945 return 0;
946}
947
948static int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
949{
950 struct btrfs_super_block *sb = fs_info->super_copy;
951 u64 chunk_root_bytenr;
952 u64 generation;
953 int ret;
954
955 btrfs_setup_root(fs_info->chunk_root, fs_info,
956 BTRFS_CHUNK_TREE_OBJECTID);
957
958 ret = btrfs_read_sys_array(fs_info);
959 if (ret)
960 return ret;
961
962 generation = btrfs_super_chunk_root_generation(sb);
963 chunk_root_bytenr = btrfs_super_chunk_root(sb);
964
965 fs_info->chunk_root->node = read_tree_block(fs_info,
966 chunk_root_bytenr,
967 generation);
968 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
969 error("cannot read chunk root");
970 return -EIO;
971 }
972
973 ret = btrfs_read_chunk_tree(fs_info);
974 if (ret) {
975 fprintf(stderr, "Couldn't read chunk tree\n");
976 return ret;
977 }
978 return 0;
979}
980
981struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
982 struct disk_partition *part)
983{
984 struct btrfs_fs_info *fs_info;
985 struct btrfs_super_block *disk_super;
986 struct btrfs_fs_devices *fs_devices = NULL;
987 struct extent_buffer *eb;
988 int ret;
989
990 fs_info = btrfs_new_fs_info();
991 if (!fs_info) {
992 fprintf(stderr, "Failed to allocate memory for fs_info\n");
993 return NULL;
994 }
995
996 ret = btrfs_scan_fs_devices(desc, part, &fs_devices);
997 if (ret)
998 goto out;
999
1000 fs_info->fs_devices = fs_devices;
1001
1002 ret = btrfs_open_devices(fs_devices);
1003 if (ret)
1004 goto out;
1005
1006 disk_super = fs_info->super_copy;
1007 ret = btrfs_read_dev_super(desc, part, disk_super);
1008 if (ret) {
1009 printk("No valid btrfs found\n");
1010 goto out_devices;
1011 }
1012
1013 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID) {
1014 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1015 goto out_devices;
1016 }
1017
1018 ASSERT(!memcmp(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE));
1019 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
1020 ASSERT(!memcmp(disk_super->metadata_uuid,
1021 fs_devices->metadata_uuid, BTRFS_FSID_SIZE));
1022
1023 fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
1024 fs_info->nodesize = btrfs_super_nodesize(disk_super);
1025 fs_info->stripesize = btrfs_super_stripesize(disk_super);
1026
1027 ret = btrfs_check_fs_compatibility(fs_info->super_copy);
1028 if (ret)
1029 goto out_devices;
1030
1031 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1032 if (ret)
1033 goto out_chunk;
1034
1035 /* Chunk tree root is unable to read, return directly */
1036 if (!fs_info->chunk_root)
1037 return fs_info;
1038
1039 eb = fs_info->chunk_root->node;
1040 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1041 btrfs_header_chunk_tree_uuid(eb),
1042 BTRFS_UUID_SIZE);
1043
1044 ret = btrfs_setup_all_roots(fs_info);
1045 if (ret)
1046 goto out_chunk;
1047
1048 return fs_info;
1049
1050out_chunk:
1051 btrfs_release_all_roots(fs_info);
1052 btrfs_cleanup_all_caches(fs_info);
1053out_devices:
1054 btrfs_close_devices(fs_devices);
1055out:
1056 btrfs_free_fs_info(fs_info);
1057 return NULL;
1058}
1059
1060int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1061{
1062 int ret;
1063 int err = 0;
1064
1065 free_fs_roots_tree(&fs_info->fs_root_tree);
1066
1067 btrfs_release_all_roots(fs_info);
1068 ret = btrfs_close_devices(fs_info->fs_devices);
1069 btrfs_cleanup_all_caches(fs_info);
1070 btrfs_free_fs_info(fs_info);
1071 if (!err)
1072 err = ret;
1073 return err;
1074}
1075
1076int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1077{
1078 int ret;
1079
1080 ret = extent_buffer_uptodate(buf);
1081 if (!ret)
1082 return ret;
1083
1084 ret = verify_parent_transid(&buf->fs_info->extent_cache, buf,
1085 parent_transid, 1);
1086 return !ret;
1087}
1088
1089int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1090{
1091 return set_extent_buffer_uptodate(eb);
1092}