blob: e680caa56a4d0a8063072ca60acf9780a325a558 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún21a14fa2017-09-03 17:00:28 +02002/*
3 * BTRFS filesystem implementation for U-Boot
4 *
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
Marek Behún21a14fa2017-09-03 17:00:28 +02006 */
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
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030018/*
19 * checks if a valid root backup is present.
20 * considers the case when all root backups empty valid.
21 * returns -1 in case of invalid root backup and 0 for valid.
22 */
23static int btrfs_check_super_roots(struct btrfs_super_block *sb)
Marek Behún21a14fa2017-09-03 17:00:28 +020024{
25 struct btrfs_root_backup *root_backup;
26 int i, newest = -1;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030027 int num_empty = 0;
Marek Behún21a14fa2017-09-03 17:00:28 +020028
29 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; ++i) {
30 root_backup = sb->super_roots + i;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030031
32 if (root_backup->tree_root == 0 && root_backup->tree_root_gen == 0)
33 num_empty++;
34
Marek Behún21a14fa2017-09-03 17:00:28 +020035 if (root_backup->tree_root_gen == sb->generation)
36 newest = i;
37 }
38
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030039 if (num_empty == BTRFS_NUM_BACKUP_ROOTS) {
40 return 0;
41 } else if (newest >= 0) {
42 return 0;
43 }
44
45 return -1;
Marek Behún21a14fa2017-09-03 17:00:28 +020046}
47
48static inline int is_power_of_2(u64 x)
49{
50 return !(x & (x - 1));
51}
52
53static int btrfs_check_super_csum(char *raw_disk_sb)
54{
55 struct btrfs_super_block *disk_sb =
56 (struct btrfs_super_block *) raw_disk_sb;
57 u16 csum_type = le16_to_cpu(disk_sb->csum_type);
58
59 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
60 u32 crc = ~(u32) 0;
61 const int csum_size = sizeof(crc);
62 char result[csum_size];
63
64 crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, crc,
65 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
66 btrfs_csum_final(crc, result);
67
68 if (memcmp(raw_disk_sb, result, csum_size))
69 return -1;
70 } else {
71 return -1;
72 }
73
74 return 0;
75}
76
77static int btrfs_check_super(struct btrfs_super_block *sb)
78{
79 int ret = 0;
80
81 if (sb->flags & ~BTRFS_SUPER_FLAG_SUPP) {
82 printf("%s: Unsupported flags: %llu\n", __func__,
83 sb->flags & ~BTRFS_SUPER_FLAG_SUPP);
84 }
85
86 if (sb->root_level > BTRFS_MAX_LEVEL) {
87 printf("%s: tree_root level too big: %d >= %d\n", __func__,
88 sb->root_level, BTRFS_MAX_LEVEL);
89 ret = -1;
90 }
91
92 if (sb->chunk_root_level > BTRFS_MAX_LEVEL) {
93 printf("%s: chunk_root level too big: %d >= %d\n", __func__,
94 sb->chunk_root_level, BTRFS_MAX_LEVEL);
95 ret = -1;
96 }
97
98 if (sb->log_root_level > BTRFS_MAX_LEVEL) {
99 printf("%s: log_root level too big: %d >= %d\n", __func__,
100 sb->log_root_level, BTRFS_MAX_LEVEL);
101 ret = -1;
102 }
103
104 if (!is_power_of_2(sb->sectorsize) || sb->sectorsize < 4096 ||
105 sb->sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
106 printf("%s: invalid sectorsize %u\n", __func__,
107 sb->sectorsize);
108 ret = -1;
109 }
110
111 if (!is_power_of_2(sb->nodesize) || sb->nodesize < sb->sectorsize ||
112 sb->nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
113 printf("%s: invalid nodesize %u\n", __func__, sb->nodesize);
114 ret = -1;
115 }
116
117 if (sb->nodesize != sb->__unused_leafsize) {
118 printf("%s: invalid leafsize %u, should be %u\n", __func__,
119 sb->__unused_leafsize, sb->nodesize);
120 ret = -1;
121 }
122
123 if (!IS_ALIGNED(sb->root, sb->sectorsize)) {
124 printf("%s: tree_root block unaligned: %llu\n", __func__,
125 sb->root);
126 ret = -1;
127 }
128
129 if (!IS_ALIGNED(sb->chunk_root, sb->sectorsize)) {
130 printf("%s: chunk_root block unaligned: %llu\n", __func__,
131 sb->chunk_root);
132 ret = -1;
133 }
134
135 if (!IS_ALIGNED(sb->log_root, sb->sectorsize)) {
136 printf("%s: log_root block unaligned: %llu\n", __func__,
137 sb->log_root);
138 ret = -1;
139 }
140
141 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
142 printf("%s: dev_item UUID does not match fsid\n", __func__);
143 ret = -1;
144 }
145
146 if (sb->bytes_used < 6*sb->nodesize) {
147 printf("%s: bytes_used is too small %llu\n", __func__,
148 sb->bytes_used);
149 ret = -1;
150 }
151
152 if (!is_power_of_2(sb->stripesize)) {
153 printf("%s: invalid stripesize %u\n", __func__, sb->stripesize);
154 ret = -1;
155 }
156
157 if (sb->sys_chunk_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
158 printf("%s: system chunk array too big %u > %u\n", __func__,
159 sb->sys_chunk_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
160 ret = -1;
161 }
162
163 if (sb->sys_chunk_array_size < sizeof(struct btrfs_key) +
164 sizeof(struct btrfs_chunk)) {
Tom Rinif39bfec2018-01-27 17:23:21 -0500165 printf("%s: system chunk array too small %u < %zu\n", __func__,
166 sb->sys_chunk_array_size, sizeof(struct btrfs_key)
Marek Behún21a14fa2017-09-03 17:00:28 +0200167 + sizeof(struct btrfs_chunk));
168 ret = -1;
169 }
170
171 return ret;
172}
173
174int btrfs_read_superblock(void)
175{
176 const u64 superblock_offsets[4] = {
177 0x10000ull,
178 0x4000000ull,
179 0x4000000000ull,
180 0x4000000000000ull
181 };
182 char raw_sb[BTRFS_SUPER_INFO_SIZE];
183 struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
184 u64 dev_total_bytes;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300185 int i;
Marek Behún21a14fa2017-09-03 17:00:28 +0200186
187 dev_total_bytes = (u64) btrfs_part_info->size * btrfs_part_info->blksz;
188
189 btrfs_info.sb.generation = 0;
190
191 for (i = 0; i < 4; ++i) {
192 if (superblock_offsets[i] + sizeof(sb) > dev_total_bytes)
193 break;
194
195 if (!btrfs_devread(superblock_offsets[i], BTRFS_SUPER_INFO_SIZE,
196 raw_sb))
197 break;
198
199 if (btrfs_check_super_csum(raw_sb)) {
200 printf("%s: invalid checksum at superblock mirror %i\n",
201 __func__, i);
202 continue;
203 }
204
205 btrfs_super_block_to_cpu(sb);
206
207 if (sb->magic != BTRFS_MAGIC) {
208 printf("%s: invalid BTRFS magic 0x%016llX at "
209 "superblock mirror %i\n", __func__, sb->magic,
210 i);
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) {
226 printf("%s: No valid BTRFS superblock found!\n", __func__);
227 return -1;
228 }
229
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300230 if (btrfs_check_super_roots(&btrfs_info.sb)) {
Marek Behún21a14fa2017-09-03 17:00:28 +0200231 printf("%s: No valid root_backup found!\n", __func__);
232 return -1;
233 }
Marek Behún21a14fa2017-09-03 17:00:28 +0200234
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300235 if (btrfs_info.sb.num_devices != 1) {
Marek Behún21a14fa2017-09-03 17:00:28 +0200236 printf("%s: Unsupported number of devices (%lli). This driver "
237 "only supports filesystem on one device.\n", __func__,
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300238 btrfs_info.sb.num_devices);
Marek Behún21a14fa2017-09-03 17:00:28 +0200239 return -1;
240 }
241
242 debug("Chosen superblock with generation = %llu\n",
243 btrfs_info.sb.generation);
244
245 return 0;
246}