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