Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * BTRFS filesystem implementation for U-Boot |
| 4 | * |
| 5 | * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 9 | #include <log.h> |
Marek Vasut | a2f9cbf | 2018-09-22 04:13:35 +0200 | [diff] [blame] | 10 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <part.h> |
Qu Wenruo | 37539f5 | 2020-03-26 13:35:55 +0800 | [diff] [blame] | 12 | #include <linux/compat.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include "btrfs.h" |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 14 | |
| 15 | #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN \ |
| 16 | | BTRFS_HEADER_FLAG_RELOC \ |
| 17 | | BTRFS_SUPER_FLAG_ERROR \ |
| 18 | | BTRFS_SUPER_FLAG_SEEDING \ |
| 19 | | BTRFS_SUPER_FLAG_METADUMP) |
| 20 | |
| 21 | #define BTRFS_SUPER_INFO_SIZE 4096 |
| 22 | |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 23 | /* |
| 24 | * checks if a valid root backup is present. |
| 25 | * considers the case when all root backups empty valid. |
| 26 | * returns -1 in case of invalid root backup and 0 for valid. |
| 27 | */ |
| 28 | static int btrfs_check_super_roots(struct btrfs_super_block *sb) |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 29 | { |
| 30 | struct btrfs_root_backup *root_backup; |
| 31 | int i, newest = -1; |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 32 | int num_empty = 0; |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 33 | |
| 34 | for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; ++i) { |
| 35 | root_backup = sb->super_roots + i; |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 36 | |
| 37 | if (root_backup->tree_root == 0 && root_backup->tree_root_gen == 0) |
| 38 | num_empty++; |
| 39 | |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 40 | if (root_backup->tree_root_gen == sb->generation) |
| 41 | newest = i; |
| 42 | } |
| 43 | |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 44 | if (num_empty == BTRFS_NUM_BACKUP_ROOTS) { |
| 45 | return 0; |
| 46 | } else if (newest >= 0) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | return -1; |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static inline int is_power_of_2(u64 x) |
| 54 | { |
| 55 | return !(x & (x - 1)); |
| 56 | } |
| 57 | |
| 58 | static int btrfs_check_super_csum(char *raw_disk_sb) |
| 59 | { |
| 60 | struct btrfs_super_block *disk_sb = |
| 61 | (struct btrfs_super_block *) raw_disk_sb; |
| 62 | u16 csum_type = le16_to_cpu(disk_sb->csum_type); |
| 63 | |
| 64 | if (csum_type == BTRFS_CSUM_TYPE_CRC32) { |
| 65 | u32 crc = ~(u32) 0; |
| 66 | const int csum_size = sizeof(crc); |
| 67 | char result[csum_size]; |
| 68 | |
| 69 | crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, crc, |
| 70 | BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); |
| 71 | btrfs_csum_final(crc, result); |
| 72 | |
| 73 | if (memcmp(raw_disk_sb, result, csum_size)) |
| 74 | return -1; |
| 75 | } else { |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int btrfs_check_super(struct btrfs_super_block *sb) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | |
| 86 | if (sb->flags & ~BTRFS_SUPER_FLAG_SUPP) { |
| 87 | printf("%s: Unsupported flags: %llu\n", __func__, |
| 88 | sb->flags & ~BTRFS_SUPER_FLAG_SUPP); |
| 89 | } |
| 90 | |
| 91 | if (sb->root_level > BTRFS_MAX_LEVEL) { |
| 92 | printf("%s: tree_root level too big: %d >= %d\n", __func__, |
| 93 | sb->root_level, BTRFS_MAX_LEVEL); |
| 94 | ret = -1; |
| 95 | } |
| 96 | |
| 97 | if (sb->chunk_root_level > BTRFS_MAX_LEVEL) { |
| 98 | printf("%s: chunk_root level too big: %d >= %d\n", __func__, |
| 99 | sb->chunk_root_level, BTRFS_MAX_LEVEL); |
| 100 | ret = -1; |
| 101 | } |
| 102 | |
| 103 | if (sb->log_root_level > BTRFS_MAX_LEVEL) { |
| 104 | printf("%s: log_root level too big: %d >= %d\n", __func__, |
| 105 | sb->log_root_level, BTRFS_MAX_LEVEL); |
| 106 | ret = -1; |
| 107 | } |
| 108 | |
| 109 | if (!is_power_of_2(sb->sectorsize) || sb->sectorsize < 4096 || |
| 110 | sb->sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) { |
| 111 | printf("%s: invalid sectorsize %u\n", __func__, |
| 112 | sb->sectorsize); |
| 113 | ret = -1; |
| 114 | } |
| 115 | |
| 116 | if (!is_power_of_2(sb->nodesize) || sb->nodesize < sb->sectorsize || |
| 117 | sb->nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) { |
| 118 | printf("%s: invalid nodesize %u\n", __func__, sb->nodesize); |
| 119 | ret = -1; |
| 120 | } |
| 121 | |
| 122 | if (sb->nodesize != sb->__unused_leafsize) { |
| 123 | printf("%s: invalid leafsize %u, should be %u\n", __func__, |
| 124 | sb->__unused_leafsize, sb->nodesize); |
| 125 | ret = -1; |
| 126 | } |
| 127 | |
| 128 | if (!IS_ALIGNED(sb->root, sb->sectorsize)) { |
| 129 | printf("%s: tree_root block unaligned: %llu\n", __func__, |
| 130 | sb->root); |
| 131 | ret = -1; |
| 132 | } |
| 133 | |
| 134 | if (!IS_ALIGNED(sb->chunk_root, sb->sectorsize)) { |
| 135 | printf("%s: chunk_root block unaligned: %llu\n", __func__, |
| 136 | sb->chunk_root); |
| 137 | ret = -1; |
| 138 | } |
| 139 | |
| 140 | if (!IS_ALIGNED(sb->log_root, sb->sectorsize)) { |
| 141 | printf("%s: log_root block unaligned: %llu\n", __func__, |
| 142 | sb->log_root); |
| 143 | ret = -1; |
| 144 | } |
| 145 | |
| 146 | if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) { |
| 147 | printf("%s: dev_item UUID does not match fsid\n", __func__); |
| 148 | ret = -1; |
| 149 | } |
| 150 | |
| 151 | if (sb->bytes_used < 6*sb->nodesize) { |
| 152 | printf("%s: bytes_used is too small %llu\n", __func__, |
| 153 | sb->bytes_used); |
| 154 | ret = -1; |
| 155 | } |
| 156 | |
| 157 | if (!is_power_of_2(sb->stripesize)) { |
| 158 | printf("%s: invalid stripesize %u\n", __func__, sb->stripesize); |
| 159 | ret = -1; |
| 160 | } |
| 161 | |
| 162 | if (sb->sys_chunk_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 163 | printf("%s: system chunk array too big %u > %u\n", __func__, |
| 164 | sb->sys_chunk_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); |
| 165 | ret = -1; |
| 166 | } |
| 167 | |
| 168 | if (sb->sys_chunk_array_size < sizeof(struct btrfs_key) + |
| 169 | sizeof(struct btrfs_chunk)) { |
Tom Rini | 23884dc | 2018-01-27 17:23:21 -0500 | [diff] [blame] | 170 | printf("%s: system chunk array too small %u < %zu\n", __func__, |
| 171 | sb->sys_chunk_array_size, sizeof(struct btrfs_key) |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 172 | + sizeof(struct btrfs_chunk)); |
| 173 | ret = -1; |
| 174 | } |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | int btrfs_read_superblock(void) |
| 180 | { |
| 181 | const u64 superblock_offsets[4] = { |
| 182 | 0x10000ull, |
| 183 | 0x4000000ull, |
| 184 | 0x4000000000ull, |
| 185 | 0x4000000000000ull |
| 186 | }; |
Marek Vasut | a2f9cbf | 2018-09-22 04:13:35 +0200 | [diff] [blame] | 187 | ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE); |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 188 | struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb; |
| 189 | u64 dev_total_bytes; |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 190 | int i; |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 191 | |
| 192 | dev_total_bytes = (u64) btrfs_part_info->size * btrfs_part_info->blksz; |
| 193 | |
| 194 | btrfs_info.sb.generation = 0; |
| 195 | |
| 196 | for (i = 0; i < 4; ++i) { |
| 197 | if (superblock_offsets[i] + sizeof(sb) > dev_total_bytes) |
| 198 | break; |
| 199 | |
| 200 | if (!btrfs_devread(superblock_offsets[i], BTRFS_SUPER_INFO_SIZE, |
| 201 | raw_sb)) |
| 202 | break; |
| 203 | |
| 204 | if (btrfs_check_super_csum(raw_sb)) { |
Marek Behún | 7d3db16 | 2019-04-26 15:11:09 +0200 | [diff] [blame] | 205 | debug("%s: invalid checksum at superblock mirror %i\n", |
| 206 | __func__, i); |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | |
| 210 | btrfs_super_block_to_cpu(sb); |
| 211 | |
| 212 | if (sb->magic != BTRFS_MAGIC) { |
Marek Behún | 7d3db16 | 2019-04-26 15:11:09 +0200 | [diff] [blame] | 213 | debug("%s: invalid BTRFS magic 0x%016llX at " |
| 214 | "superblock mirror %i\n", __func__, sb->magic, i); |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 215 | } else if (sb->bytenr != superblock_offsets[i]) { |
| 216 | printf("%s: invalid bytenr 0x%016llX (expected " |
| 217 | "0x%016llX) at superblock mirror %i\n", |
| 218 | __func__, sb->bytenr, superblock_offsets[i], i); |
| 219 | } else if (btrfs_check_super(sb)) { |
| 220 | printf("%s: Checking superblock mirror %i failed\n", |
| 221 | __func__, i); |
| 222 | } else if (sb->generation > btrfs_info.sb.generation) { |
| 223 | memcpy(&btrfs_info.sb, sb, sizeof(*sb)); |
| 224 | } else { |
| 225 | /* Nothing */ |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (!btrfs_info.sb.generation) { |
Marek Behún | 7d3db16 | 2019-04-26 15:11:09 +0200 | [diff] [blame] | 230 | debug("%s: No valid BTRFS superblock found!\n", __func__); |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 231 | return -1; |
| 232 | } |
| 233 | |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 234 | if (btrfs_check_super_roots(&btrfs_info.sb)) { |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 235 | printf("%s: No valid root_backup found!\n", __func__); |
| 236 | return -1; |
| 237 | } |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 238 | |
Qu Wenruo | 37539f5 | 2020-03-26 13:35:55 +0800 | [diff] [blame] | 239 | if (sb->sectorsize != PAGE_SIZE) { |
| 240 | printf( |
| 241 | "%s: Unsupported sector size (%u), only supports %u as sector size\n", |
| 242 | __func__, sb->sectorsize, PAGE_SIZE); |
| 243 | return -1; |
| 244 | } |
| 245 | |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 246 | if (btrfs_info.sb.num_devices != 1) { |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 247 | printf("%s: Unsupported number of devices (%lli). This driver " |
| 248 | "only supports filesystem on one device.\n", __func__, |
Yevgeny Popovych | 0a4b93f | 2018-06-11 14:14:33 +0300 | [diff] [blame] | 249 | btrfs_info.sb.num_devices); |
Marek Behún | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | debug("Chosen superblock with generation = %llu\n", |
| 254 | btrfs_info.sb.generation); |
| 255 | |
| 256 | return 0; |
| 257 | } |