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