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