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