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