183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
221a14facSMarek Behún /*
321a14facSMarek Behún * BTRFS filesystem implementation for U-Boot
421a14facSMarek Behún *
521a14facSMarek Behún * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
621a14facSMarek Behún */
721a14facSMarek Behún
821a14facSMarek Behún #include "btrfs.h"
9*c9795396SMarek Vasut #include <memalign.h>
1021a14facSMarek Behún
1121a14facSMarek Behún #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN \
1221a14facSMarek Behún | BTRFS_HEADER_FLAG_RELOC \
1321a14facSMarek Behún | BTRFS_SUPER_FLAG_ERROR \
1421a14facSMarek Behún | BTRFS_SUPER_FLAG_SEEDING \
1521a14facSMarek Behún | BTRFS_SUPER_FLAG_METADUMP)
1621a14facSMarek Behún
1721a14facSMarek Behún #define BTRFS_SUPER_INFO_SIZE 4096
1821a14facSMarek Behún
19d146a7b9SYevgeny Popovych /*
20d146a7b9SYevgeny Popovych * checks if a valid root backup is present.
21d146a7b9SYevgeny Popovych * considers the case when all root backups empty valid.
22d146a7b9SYevgeny Popovych * returns -1 in case of invalid root backup and 0 for valid.
23d146a7b9SYevgeny Popovych */
btrfs_check_super_roots(struct btrfs_super_block * sb)24d146a7b9SYevgeny Popovych static int btrfs_check_super_roots(struct btrfs_super_block *sb)
2521a14facSMarek Behún {
2621a14facSMarek Behún struct btrfs_root_backup *root_backup;
2721a14facSMarek Behún int i, newest = -1;
28d146a7b9SYevgeny Popovych int num_empty = 0;
2921a14facSMarek Behún
3021a14facSMarek Behún for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; ++i) {
3121a14facSMarek Behún root_backup = sb->super_roots + i;
32d146a7b9SYevgeny Popovych
33d146a7b9SYevgeny Popovych if (root_backup->tree_root == 0 && root_backup->tree_root_gen == 0)
34d146a7b9SYevgeny Popovych num_empty++;
35d146a7b9SYevgeny Popovych
3621a14facSMarek Behún if (root_backup->tree_root_gen == sb->generation)
3721a14facSMarek Behún newest = i;
3821a14facSMarek Behún }
3921a14facSMarek Behún
40d146a7b9SYevgeny Popovych if (num_empty == BTRFS_NUM_BACKUP_ROOTS) {
41d146a7b9SYevgeny Popovych return 0;
42d146a7b9SYevgeny Popovych } else if (newest >= 0) {
43d146a7b9SYevgeny Popovych return 0;
44d146a7b9SYevgeny Popovych }
45d146a7b9SYevgeny Popovych
46d146a7b9SYevgeny Popovych return -1;
4721a14facSMarek Behún }
4821a14facSMarek Behún
is_power_of_2(u64 x)4921a14facSMarek Behún static inline int is_power_of_2(u64 x)
5021a14facSMarek Behún {
5121a14facSMarek Behún return !(x & (x - 1));
5221a14facSMarek Behún }
5321a14facSMarek Behún
btrfs_check_super_csum(char * raw_disk_sb)5421a14facSMarek Behún static int btrfs_check_super_csum(char *raw_disk_sb)
5521a14facSMarek Behún {
5621a14facSMarek Behún struct btrfs_super_block *disk_sb =
5721a14facSMarek Behún (struct btrfs_super_block *) raw_disk_sb;
5821a14facSMarek Behún u16 csum_type = le16_to_cpu(disk_sb->csum_type);
5921a14facSMarek Behún
6021a14facSMarek Behún if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
6121a14facSMarek Behún u32 crc = ~(u32) 0;
6221a14facSMarek Behún const int csum_size = sizeof(crc);
6321a14facSMarek Behún char result[csum_size];
6421a14facSMarek Behún
6521a14facSMarek Behún crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, crc,
6621a14facSMarek Behún BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
6721a14facSMarek Behún btrfs_csum_final(crc, result);
6821a14facSMarek Behún
6921a14facSMarek Behún if (memcmp(raw_disk_sb, result, csum_size))
7021a14facSMarek Behún return -1;
7121a14facSMarek Behún } else {
7221a14facSMarek Behún return -1;
7321a14facSMarek Behún }
7421a14facSMarek Behún
7521a14facSMarek Behún return 0;
7621a14facSMarek Behún }
7721a14facSMarek Behún
btrfs_check_super(struct btrfs_super_block * sb)7821a14facSMarek Behún static int btrfs_check_super(struct btrfs_super_block *sb)
7921a14facSMarek Behún {
8021a14facSMarek Behún int ret = 0;
8121a14facSMarek Behún
8221a14facSMarek Behún if (sb->flags & ~BTRFS_SUPER_FLAG_SUPP) {
8321a14facSMarek Behún printf("%s: Unsupported flags: %llu\n", __func__,
8421a14facSMarek Behún sb->flags & ~BTRFS_SUPER_FLAG_SUPP);
8521a14facSMarek Behún }
8621a14facSMarek Behún
8721a14facSMarek Behún if (sb->root_level > BTRFS_MAX_LEVEL) {
8821a14facSMarek Behún printf("%s: tree_root level too big: %d >= %d\n", __func__,
8921a14facSMarek Behún sb->root_level, BTRFS_MAX_LEVEL);
9021a14facSMarek Behún ret = -1;
9121a14facSMarek Behún }
9221a14facSMarek Behún
9321a14facSMarek Behún if (sb->chunk_root_level > BTRFS_MAX_LEVEL) {
9421a14facSMarek Behún printf("%s: chunk_root level too big: %d >= %d\n", __func__,
9521a14facSMarek Behún sb->chunk_root_level, BTRFS_MAX_LEVEL);
9621a14facSMarek Behún ret = -1;
9721a14facSMarek Behún }
9821a14facSMarek Behún
9921a14facSMarek Behún if (sb->log_root_level > BTRFS_MAX_LEVEL) {
10021a14facSMarek Behún printf("%s: log_root level too big: %d >= %d\n", __func__,
10121a14facSMarek Behún sb->log_root_level, BTRFS_MAX_LEVEL);
10221a14facSMarek Behún ret = -1;
10321a14facSMarek Behún }
10421a14facSMarek Behún
10521a14facSMarek Behún if (!is_power_of_2(sb->sectorsize) || sb->sectorsize < 4096 ||
10621a14facSMarek Behún sb->sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
10721a14facSMarek Behún printf("%s: invalid sectorsize %u\n", __func__,
10821a14facSMarek Behún sb->sectorsize);
10921a14facSMarek Behún ret = -1;
11021a14facSMarek Behún }
11121a14facSMarek Behún
11221a14facSMarek Behún if (!is_power_of_2(sb->nodesize) || sb->nodesize < sb->sectorsize ||
11321a14facSMarek Behún sb->nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
11421a14facSMarek Behún printf("%s: invalid nodesize %u\n", __func__, sb->nodesize);
11521a14facSMarek Behún ret = -1;
11621a14facSMarek Behún }
11721a14facSMarek Behún
11821a14facSMarek Behún if (sb->nodesize != sb->__unused_leafsize) {
11921a14facSMarek Behún printf("%s: invalid leafsize %u, should be %u\n", __func__,
12021a14facSMarek Behún sb->__unused_leafsize, sb->nodesize);
12121a14facSMarek Behún ret = -1;
12221a14facSMarek Behún }
12321a14facSMarek Behún
12421a14facSMarek Behún if (!IS_ALIGNED(sb->root, sb->sectorsize)) {
12521a14facSMarek Behún printf("%s: tree_root block unaligned: %llu\n", __func__,
12621a14facSMarek Behún sb->root);
12721a14facSMarek Behún ret = -1;
12821a14facSMarek Behún }
12921a14facSMarek Behún
13021a14facSMarek Behún if (!IS_ALIGNED(sb->chunk_root, sb->sectorsize)) {
13121a14facSMarek Behún printf("%s: chunk_root block unaligned: %llu\n", __func__,
13221a14facSMarek Behún sb->chunk_root);
13321a14facSMarek Behún ret = -1;
13421a14facSMarek Behún }
13521a14facSMarek Behún
13621a14facSMarek Behún if (!IS_ALIGNED(sb->log_root, sb->sectorsize)) {
13721a14facSMarek Behún printf("%s: log_root block unaligned: %llu\n", __func__,
13821a14facSMarek Behún sb->log_root);
13921a14facSMarek Behún ret = -1;
14021a14facSMarek Behún }
14121a14facSMarek Behún
14221a14facSMarek Behún if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
14321a14facSMarek Behún printf("%s: dev_item UUID does not match fsid\n", __func__);
14421a14facSMarek Behún ret = -1;
14521a14facSMarek Behún }
14621a14facSMarek Behún
14721a14facSMarek Behún if (sb->bytes_used < 6*sb->nodesize) {
14821a14facSMarek Behún printf("%s: bytes_used is too small %llu\n", __func__,
14921a14facSMarek Behún sb->bytes_used);
15021a14facSMarek Behún ret = -1;
15121a14facSMarek Behún }
15221a14facSMarek Behún
15321a14facSMarek Behún if (!is_power_of_2(sb->stripesize)) {
15421a14facSMarek Behún printf("%s: invalid stripesize %u\n", __func__, sb->stripesize);
15521a14facSMarek Behún ret = -1;
15621a14facSMarek Behún }
15721a14facSMarek Behún
15821a14facSMarek Behún if (sb->sys_chunk_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
15921a14facSMarek Behún printf("%s: system chunk array too big %u > %u\n", __func__,
16021a14facSMarek Behún sb->sys_chunk_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
16121a14facSMarek Behún ret = -1;
16221a14facSMarek Behún }
16321a14facSMarek Behún
16421a14facSMarek Behún if (sb->sys_chunk_array_size < sizeof(struct btrfs_key) +
16521a14facSMarek Behún sizeof(struct btrfs_chunk)) {
166f39bfec2STom Rini printf("%s: system chunk array too small %u < %zu\n", __func__,
167f39bfec2STom Rini sb->sys_chunk_array_size, sizeof(struct btrfs_key)
16821a14facSMarek Behún + sizeof(struct btrfs_chunk));
16921a14facSMarek Behún ret = -1;
17021a14facSMarek Behún }
17121a14facSMarek Behún
17221a14facSMarek Behún return ret;
17321a14facSMarek Behún }
17421a14facSMarek Behún
btrfs_read_superblock(void)17521a14facSMarek Behún int btrfs_read_superblock(void)
17621a14facSMarek Behún {
17721a14facSMarek Behún const u64 superblock_offsets[4] = {
17821a14facSMarek Behún 0x10000ull,
17921a14facSMarek Behún 0x4000000ull,
18021a14facSMarek Behún 0x4000000000ull,
18121a14facSMarek Behún 0x4000000000000ull
18221a14facSMarek Behún };
183*c9795396SMarek Vasut ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
18421a14facSMarek Behún struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
18521a14facSMarek Behún u64 dev_total_bytes;
186d146a7b9SYevgeny Popovych int i;
18721a14facSMarek Behún
18821a14facSMarek Behún dev_total_bytes = (u64) btrfs_part_info->size * btrfs_part_info->blksz;
18921a14facSMarek Behún
19021a14facSMarek Behún btrfs_info.sb.generation = 0;
19121a14facSMarek Behún
19221a14facSMarek Behún for (i = 0; i < 4; ++i) {
19321a14facSMarek Behún if (superblock_offsets[i] + sizeof(sb) > dev_total_bytes)
19421a14facSMarek Behún break;
19521a14facSMarek Behún
19621a14facSMarek Behún if (!btrfs_devread(superblock_offsets[i], BTRFS_SUPER_INFO_SIZE,
19721a14facSMarek Behún raw_sb))
19821a14facSMarek Behún break;
19921a14facSMarek Behún
20021a14facSMarek Behún if (btrfs_check_super_csum(raw_sb)) {
20121a14facSMarek Behún printf("%s: invalid checksum at superblock mirror %i\n",
20221a14facSMarek Behún __func__, i);
20321a14facSMarek Behún continue;
20421a14facSMarek Behún }
20521a14facSMarek Behún
20621a14facSMarek Behún btrfs_super_block_to_cpu(sb);
20721a14facSMarek Behún
20821a14facSMarek Behún if (sb->magic != BTRFS_MAGIC) {
20921a14facSMarek Behún printf("%s: invalid BTRFS magic 0x%016llX at "
21021a14facSMarek Behún "superblock mirror %i\n", __func__, sb->magic,
21121a14facSMarek Behún i);
21221a14facSMarek Behún } else if (sb->bytenr != superblock_offsets[i]) {
21321a14facSMarek Behún printf("%s: invalid bytenr 0x%016llX (expected "
21421a14facSMarek Behún "0x%016llX) at superblock mirror %i\n",
21521a14facSMarek Behún __func__, sb->bytenr, superblock_offsets[i], i);
21621a14facSMarek Behún } else if (btrfs_check_super(sb)) {
21721a14facSMarek Behún printf("%s: Checking superblock mirror %i failed\n",
21821a14facSMarek Behún __func__, i);
21921a14facSMarek Behún } else if (sb->generation > btrfs_info.sb.generation) {
22021a14facSMarek Behún memcpy(&btrfs_info.sb, sb, sizeof(*sb));
22121a14facSMarek Behún } else {
22221a14facSMarek Behún /* Nothing */
22321a14facSMarek Behún }
22421a14facSMarek Behún }
22521a14facSMarek Behún
22621a14facSMarek Behún if (!btrfs_info.sb.generation) {
22721a14facSMarek Behún printf("%s: No valid BTRFS superblock found!\n", __func__);
22821a14facSMarek Behún return -1;
22921a14facSMarek Behún }
23021a14facSMarek Behún
231d146a7b9SYevgeny Popovych if (btrfs_check_super_roots(&btrfs_info.sb)) {
23221a14facSMarek Behún printf("%s: No valid root_backup found!\n", __func__);
23321a14facSMarek Behún return -1;
23421a14facSMarek Behún }
23521a14facSMarek Behún
236d146a7b9SYevgeny Popovych if (btrfs_info.sb.num_devices != 1) {
23721a14facSMarek Behún printf("%s: Unsupported number of devices (%lli). This driver "
23821a14facSMarek Behún "only supports filesystem on one device.\n", __func__,
239d146a7b9SYevgeny Popovych btrfs_info.sb.num_devices);
24021a14facSMarek Behún return -1;
24121a14facSMarek Behún }
24221a14facSMarek Behún
24321a14facSMarek Behún debug("Chosen superblock with generation = %llu\n",
24421a14facSMarek Behún btrfs_info.sb.generation);
24521a14facSMarek Behún
24621a14facSMarek Behún return 0;
24721a14facSMarek Behún }
248