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