blob: e5bfaf461c2bcb6075f7dd9b43a3ba2f0e045462 [file] [log] [blame]
Qu Wenruo0e7b8542020-06-24 18:02:48 +02001// SPDX-License-Identifier: GPL-2.0+
Qu Wenruo0e7b8542020-06-24 18:02:48 +02002#include <fs_internal.h>
Simon Glass3862fab2022-10-11 09:47:11 -06003#include <log.h>
Qu Wenruo1a618082020-06-24 18:02:49 +02004#include <uuid.h>
5#include <memalign.h>
6#include "kernel-shared/btrfs_tree.h"
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02007#include "common/rbtree-utils.h"
Qu Wenruo0e7b8542020-06-24 18:02:48 +02008#include "disk-io.h"
Qu Wenruo1a618082020-06-24 18:02:49 +02009#include "ctree.h"
10#include "btrfs.h"
Qu Wenruod85f9592020-06-24 18:02:55 +020011#include "volumes.h"
12#include "extent-io.h"
Qu Wenruo0e7b8542020-06-24 18:02:48 +020013#include "crypto/hash.h"
14
Qu Wenruod85f9592020-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 Wenruo0e7b8542020-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 Wenruoc5276872021-12-27 14:12:08 +0800129 case BTRFS_CSUM_TYPE_BLAKE2:
130 return hash_blake2(data, len, out);
Qu Wenruo0e7b8542020-06-24 18:02:48 +0200131 default:
132 printf("Unknown csum type %d\n", csum_type);
133 return -EINVAL;
134 }
135}
Qu Wenruo1a618082020-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 Vasut91933982021-05-18 00:39:39 +0200296 ALLOC_CACHE_ALIGN_BUFFER(char, tmp, BTRFS_SUPER_INFO_SIZE);
Qu Wenruo1a618082020-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 Wenruod85f9592020-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 }
Qu Wenruo1a618082020-06-24 18:02:49 +0200338 return 0;
339}
Qu Wenruod85f9592020-06-24 18:02:55 +0200340
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 Wenruod85f9592020-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 Wenruo1d5a7b72020-06-24 18:03:01 +0200539
Qu Wenruo83b04df2020-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{
Qu Wenruo5c5df2d2022-12-30 09:07:05 +0800543 u64 orig_len = *len;
544 u64 cur = logical;
Qu Wenruo83b04df2020-06-24 18:03:09 +0200545 struct btrfs_multi_bio *multi = NULL;
546 struct btrfs_device *device;
547 int ret = 0;
Qu Wenruo83b04df2020-06-24 18:03:09 +0200548
Qu Wenruo5c5df2d2022-12-30 09:07:05 +0800549 while (cur < logical + orig_len) {
550 u64 cur_len = logical + orig_len - cur;
Qu Wenruo83b04df2020-06-24 18:03:09 +0200551
Qu Wenruo5c5df2d2022-12-30 09:07:05 +0800552 ret = btrfs_map_block(fs_info, READ, cur, &cur_len, &multi,
553 mirror, NULL);
554 if (ret) {
555 error("Couldn't map the block %llu", cur);
556 goto err;
557 }
558 device = multi->stripes[0].dev;
559 if (!device->desc || !device->part) {
560 error("devid %llu is missing", device->devid);
561 ret = -EIO;
562 goto err;
563 }
564 ret = __btrfs_devread(device->desc, device->part,
565 data + (cur - logical), cur_len,
566 multi->stripes[0].physical);
567 if (ret != cur_len) {
568 error("read failed on devid %llu physical %llu",
569 device->devid, multi->stripes[0].physical);
570 ret = -EIO;
571 goto err;
572 }
573 cur += cur_len;
Qu Wenruo83b04df2020-06-24 18:03:09 +0200574 ret = 0;
Qu Wenruo5c5df2d2022-12-30 09:07:05 +0800575 }
Qu Wenruo83b04df2020-06-24 18:03:09 +0200576err:
577 kfree(multi);
578 return ret;
579}
580
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200581void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
582 u64 objectid)
583{
584 root->node = NULL;
585 root->track_dirty = 0;
586
587 root->fs_info = fs_info;
588 root->objectid = objectid;
589 root->last_trans = 0;
590 root->last_inode_alloc = 0;
591
592 memset(&root->root_key, 0, sizeof(root->root_key));
593 memset(&root->root_item, 0, sizeof(root->root_item));
594 root->root_key.objectid = objectid;
595}
596
597static int find_and_setup_root(struct btrfs_root *tree_root,
598 struct btrfs_fs_info *fs_info,
599 u64 objectid, struct btrfs_root *root)
600{
601 int ret;
602 u64 generation;
603
604 btrfs_setup_root(root, fs_info, objectid);
605 ret = btrfs_find_last_root(tree_root, objectid,
606 &root->root_item, &root->root_key);
607 if (ret)
608 return ret;
609
610 generation = btrfs_root_generation(&root->root_item);
611 root->node = read_tree_block(fs_info,
612 btrfs_root_bytenr(&root->root_item), generation);
613 if (!extent_buffer_uptodate(root->node))
614 return -EIO;
615
616 return 0;
617}
618
619int btrfs_free_fs_root(struct btrfs_root *root)
620{
621 if (root->node)
622 free_extent_buffer(root->node);
623 kfree(root);
624 return 0;
625}
626
627static void __free_fs_root(struct rb_node *node)
628{
629 struct btrfs_root *root;
630
631 root = container_of(node, struct btrfs_root, rb_node);
632 btrfs_free_fs_root(root);
633}
634
635FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
636
637struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
638 struct btrfs_key *location)
639{
640 struct btrfs_root *root;
641 struct btrfs_root *tree_root = fs_info->tree_root;
642 struct btrfs_path *path;
643 struct extent_buffer *l;
644 u64 generation;
645 int ret = 0;
646
647 root = calloc(1, sizeof(*root));
648 if (!root)
649 return ERR_PTR(-ENOMEM);
650 if (location->offset == (u64)-1) {
651 ret = find_and_setup_root(tree_root, fs_info,
652 location->objectid, root);
653 if (ret) {
654 free(root);
655 return ERR_PTR(ret);
656 }
657 goto insert;
658 }
659
660 btrfs_setup_root(root, fs_info,
661 location->objectid);
662
663 path = btrfs_alloc_path();
664 if (!path) {
665 free(root);
666 return ERR_PTR(-ENOMEM);
667 }
668
669 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
670 if (ret != 0) {
671 if (ret > 0)
672 ret = -ENOENT;
673 goto out;
674 }
675 l = path->nodes[0];
676 read_extent_buffer(l, &root->root_item,
677 btrfs_item_ptr_offset(l, path->slots[0]),
678 sizeof(root->root_item));
679 memcpy(&root->root_key, location, sizeof(*location));
680
681 /* If this root is already an orphan, no need to read */
682 if (btrfs_root_refs(&root->root_item) == 0) {
683 ret = -ENOENT;
684 goto out;
685 }
686 ret = 0;
687out:
688 btrfs_free_path(path);
689 if (ret) {
690 free(root);
691 return ERR_PTR(ret);
692 }
693 generation = btrfs_root_generation(&root->root_item);
694 root->node = read_tree_block(fs_info,
695 btrfs_root_bytenr(&root->root_item), generation);
696 if (!extent_buffer_uptodate(root->node)) {
697 free(root);
698 return ERR_PTR(-EIO);
699 }
700insert:
701 root->ref_cows = 1;
702 return root;
703}
704
705static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
706 void *data)
707{
708 u64 objectid = *((u64 *)data);
709 struct btrfs_root *root;
710
711 root = rb_entry(node, struct btrfs_root, rb_node);
712 if (objectid > root->objectid)
713 return 1;
714 else if (objectid < root->objectid)
715 return -1;
716 else
717 return 0;
718}
719
720int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
721{
722 struct btrfs_root *root;
723
724 root = rb_entry(node2, struct btrfs_root, rb_node);
725 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
726}
727
728struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
729 struct btrfs_key *location)
730{
731 struct btrfs_root *root;
732 struct rb_node *node;
733 int ret;
734 u64 objectid = location->objectid;
735
736 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
737 return fs_info->tree_root;
738 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
739 return fs_info->chunk_root;
740 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
741 return fs_info->csum_root;
Marek Behún33d1eab2021-02-09 18:33:37 +0100742 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID);
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200743
744 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
745 btrfs_fs_roots_compare_objectids, NULL);
746 if (node)
747 return container_of(node, struct btrfs_root, rb_node);
748
749 root = btrfs_read_fs_root_no_cache(fs_info, location);
750 if (IS_ERR(root))
751 return root;
752
753 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
754 btrfs_fs_roots_compare_roots);
755 BUG_ON(ret);
756 return root;
757}
758
759void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
760{
761 free(fs_info->tree_root);
762 free(fs_info->chunk_root);
763 free(fs_info->csum_root);
764 free(fs_info->super_copy);
765 free(fs_info);
766}
767
768struct btrfs_fs_info *btrfs_new_fs_info(void)
769{
770 struct btrfs_fs_info *fs_info;
771
772 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
773 if (!fs_info)
774 return NULL;
775
776 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
777 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
778 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
779 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
780
781 if (!fs_info->tree_root || !fs_info->chunk_root ||
782 !fs_info->csum_root || !fs_info->super_copy)
783 goto free_all;
784
785 extent_io_tree_init(&fs_info->extent_cache);
786
787 fs_info->fs_root_tree = RB_ROOT;
788 cache_tree_init(&fs_info->mapping_tree.cache_tree);
789
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200790 return fs_info;
791free_all:
792 btrfs_free_fs_info(fs_info);
793 return NULL;
794}
795
796static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
797 struct btrfs_root *info_root,
798 u64 objectid, char *str)
799{
800 struct btrfs_root *root = fs_info->tree_root;
801 int ret;
802
803 ret = find_and_setup_root(root, fs_info, objectid, info_root);
804 if (ret) {
805 error("could not setup %s tree", str);
806 return -EIO;
807 }
808
809 return 0;
810}
811
Matwey V. Kornilovb8a77ab2021-08-01 23:52:16 +0300812static int get_default_subvolume(struct btrfs_fs_info *fs_info,
813 struct btrfs_key *key_ret)
814{
815 struct btrfs_root *root = fs_info->tree_root;
816 struct btrfs_dir_item *dir_item;
817 struct btrfs_path path;
818 int ret = 0;
819
820 btrfs_init_path(&path);
821
822 dir_item = btrfs_lookup_dir_item(NULL, root, &path,
823 BTRFS_ROOT_TREE_DIR_OBJECTID,
824 "default", 7, 0);
825 if (IS_ERR(dir_item)) {
826 ret = PTR_ERR(dir_item);
827 goto out;
828 }
829
830 btrfs_dir_item_key_to_cpu(path.nodes[0], dir_item, key_ret);
831out:
832 btrfs_release_path(&path);
833 return ret;
834}
835
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200836int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info)
837{
838 struct btrfs_super_block *sb = fs_info->super_copy;
839 struct btrfs_root *root;
840 struct btrfs_key key;
841 u64 root_tree_bytenr;
842 u64 generation;
843 int ret;
844
845 root = fs_info->tree_root;
846 btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
847 generation = btrfs_super_generation(sb);
848
849 root_tree_bytenr = btrfs_super_root(sb);
850
851 root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
852 if (!extent_buffer_uptodate(root->node)) {
853 fprintf(stderr, "Couldn't read tree root\n");
854 return -EIO;
855 }
856
857 ret = setup_root_or_create_block(fs_info, fs_info->csum_root,
858 BTRFS_CSUM_TREE_OBJECTID, "csum");
859 if (ret)
860 return ret;
861 fs_info->csum_root->track_dirty = 1;
862
863 fs_info->last_trans_committed = generation;
864
Matwey V. Kornilovb8a77ab2021-08-01 23:52:16 +0300865 ret = get_default_subvolume(fs_info, &key);
866 if (ret) {
867 /*
868 * The default dir item isn't there. Linux kernel behaviour is
869 * to silently use the top-level subvolume in this case.
870 */
871 key.objectid = BTRFS_FS_TREE_OBJECTID;
872 key.type = BTRFS_ROOT_ITEM_KEY;
873 key.offset = (u64)-1;
874 }
875
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200876 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
877
878 if (IS_ERR(fs_info->fs_root))
879 return -EIO;
880 return 0;
881}
882
883void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
884{
885 if (fs_info->csum_root)
886 free_extent_buffer(fs_info->csum_root->node);
887 if (fs_info->tree_root)
888 free_extent_buffer(fs_info->tree_root->node);
889 if (fs_info->chunk_root)
890 free_extent_buffer(fs_info->chunk_root->node);
891}
892
893static void free_map_lookup(struct cache_extent *ce)
894{
895 struct map_lookup *map;
896
897 map = container_of(ce, struct map_lookup, ce);
898 kfree(map);
899}
900
901FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
902
903void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
904{
905 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
906 extent_io_tree_cleanup(&fs_info->extent_cache);
907}
908
909static int btrfs_scan_fs_devices(struct blk_desc *desc,
910 struct disk_partition *part,
911 struct btrfs_fs_devices **fs_devices)
912{
913 u64 total_devs;
914 int ret;
915
916 if (round_up(BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
917 desc->blksz) > (part->size << desc->log2blksz)) {
Simon Glass3862fab2022-10-11 09:47:11 -0600918 log_debug("superblock end %u is larger than device size " LBAFU,
919 BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
920 part->size << desc->log2blksz);
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200921 return -EINVAL;
922 }
923
924 ret = btrfs_scan_one_device(desc, part, fs_devices, &total_devs);
925 if (ret) {
Simon Glass4acb84a2021-08-18 21:40:26 -0600926 /*
927 * Avoid showing this when probing for a possible Btrfs
928 *
929 * fprintf(stderr, "No valid Btrfs found\n");
930 */
Qu Wenruo1d5a7b72020-06-24 18:03:01 +0200931 return ret;
932 }
933 return 0;
934}
935
936int btrfs_check_fs_compatibility(struct btrfs_super_block *sb)
937{
938 u64 features;
939
940 features = btrfs_super_incompat_flags(sb) &
941 ~BTRFS_FEATURE_INCOMPAT_SUPP;
942 if (features) {
943 printk("couldn't open because of unsupported "
944 "option features (%llx).\n",
945 (unsigned long long)features);
946 return -ENOTSUPP;
947 }
948
949 features = btrfs_super_incompat_flags(sb);
950 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
951 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
952 btrfs_set_super_incompat_flags(sb, features);
953 }
954
955 return 0;
956}
957
958static int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
959{
960 struct btrfs_super_block *sb = fs_info->super_copy;
961 u64 chunk_root_bytenr;
962 u64 generation;
963 int ret;
964
965 btrfs_setup_root(fs_info->chunk_root, fs_info,
966 BTRFS_CHUNK_TREE_OBJECTID);
967
968 ret = btrfs_read_sys_array(fs_info);
969 if (ret)
970 return ret;
971
972 generation = btrfs_super_chunk_root_generation(sb);
973 chunk_root_bytenr = btrfs_super_chunk_root(sb);
974
975 fs_info->chunk_root->node = read_tree_block(fs_info,
976 chunk_root_bytenr,
977 generation);
978 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
979 error("cannot read chunk root");
980 return -EIO;
981 }
982
983 ret = btrfs_read_chunk_tree(fs_info);
984 if (ret) {
985 fprintf(stderr, "Couldn't read chunk tree\n");
986 return ret;
987 }
988 return 0;
989}
990
991struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
992 struct disk_partition *part)
993{
994 struct btrfs_fs_info *fs_info;
995 struct btrfs_super_block *disk_super;
996 struct btrfs_fs_devices *fs_devices = NULL;
997 struct extent_buffer *eb;
998 int ret;
999
1000 fs_info = btrfs_new_fs_info();
1001 if (!fs_info) {
1002 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1003 return NULL;
1004 }
1005
1006 ret = btrfs_scan_fs_devices(desc, part, &fs_devices);
1007 if (ret)
1008 goto out;
1009
1010 fs_info->fs_devices = fs_devices;
1011
1012 ret = btrfs_open_devices(fs_devices);
1013 if (ret)
1014 goto out;
1015
1016 disk_super = fs_info->super_copy;
1017 ret = btrfs_read_dev_super(desc, part, disk_super);
1018 if (ret) {
Simon Glass4acb84a2021-08-18 21:40:26 -06001019 debug("No valid btrfs found\n");
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02001020 goto out_devices;
1021 }
1022
1023 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID) {
1024 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1025 goto out_devices;
1026 }
1027
1028 ASSERT(!memcmp(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE));
1029 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
1030 ASSERT(!memcmp(disk_super->metadata_uuid,
1031 fs_devices->metadata_uuid, BTRFS_FSID_SIZE));
1032
1033 fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
1034 fs_info->nodesize = btrfs_super_nodesize(disk_super);
1035 fs_info->stripesize = btrfs_super_stripesize(disk_super);
1036
1037 ret = btrfs_check_fs_compatibility(fs_info->super_copy);
1038 if (ret)
1039 goto out_devices;
1040
1041 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1042 if (ret)
1043 goto out_chunk;
1044
1045 /* Chunk tree root is unable to read, return directly */
1046 if (!fs_info->chunk_root)
1047 return fs_info;
1048
1049 eb = fs_info->chunk_root->node;
1050 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1051 btrfs_header_chunk_tree_uuid(eb),
1052 BTRFS_UUID_SIZE);
1053
1054 ret = btrfs_setup_all_roots(fs_info);
1055 if (ret)
1056 goto out_chunk;
1057
1058 return fs_info;
1059
1060out_chunk:
1061 btrfs_release_all_roots(fs_info);
1062 btrfs_cleanup_all_caches(fs_info);
1063out_devices:
1064 btrfs_close_devices(fs_devices);
1065out:
1066 btrfs_free_fs_info(fs_info);
1067 return NULL;
1068}
1069
1070int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1071{
1072 int ret;
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02001073
1074 free_fs_roots_tree(&fs_info->fs_root_tree);
1075
1076 btrfs_release_all_roots(fs_info);
1077 ret = btrfs_close_devices(fs_info->fs_devices);
1078 btrfs_cleanup_all_caches(fs_info);
1079 btrfs_free_fs_info(fs_info);
Heinrich Schuchardtb06c36f2020-12-25 13:45:25 +01001080 return ret;
Qu Wenruo1d5a7b72020-06-24 18:03:01 +02001081}
1082
1083int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1084{
1085 int ret;
1086
1087 ret = extent_buffer_uptodate(buf);
1088 if (!ret)
1089 return ret;
1090
1091 ret = verify_parent_transid(&buf->fs_info->extent_cache, buf,
1092 parent_transid, 1);
1093 return !ret;
1094}
1095
1096int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1097{
1098 return set_extent_buffer_uptodate(eb);
1099}