blob: f80c14862768b97f59b0695b778e3a364332ba16 [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
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <common.h>
Marek Vasutc9795392018-09-22 04:13:35 +02009#include <memalign.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060010#include <part.h>
Qu Wenruoa62db242020-03-26 13:35:55 +080011#include <linux/compat.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include "btrfs.h"
Marek Behún21a14fa2017-09-03 17:00:28 +020013
14#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN \
15 | BTRFS_HEADER_FLAG_RELOC \
16 | BTRFS_SUPER_FLAG_ERROR \
17 | BTRFS_SUPER_FLAG_SEEDING \
18 | BTRFS_SUPER_FLAG_METADUMP)
19
20#define BTRFS_SUPER_INFO_SIZE 4096
21
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030022/*
23 * checks if a valid root backup is present.
24 * considers the case when all root backups empty valid.
25 * returns -1 in case of invalid root backup and 0 for valid.
26 */
27static int btrfs_check_super_roots(struct btrfs_super_block *sb)
Marek Behún21a14fa2017-09-03 17:00:28 +020028{
29 struct btrfs_root_backup *root_backup;
30 int i, newest = -1;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030031 int num_empty = 0;
Marek Behún21a14fa2017-09-03 17:00:28 +020032
33 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; ++i) {
34 root_backup = sb->super_roots + i;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030035
36 if (root_backup->tree_root == 0 && root_backup->tree_root_gen == 0)
37 num_empty++;
38
Marek Behún21a14fa2017-09-03 17:00:28 +020039 if (root_backup->tree_root_gen == sb->generation)
40 newest = i;
41 }
42
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +030043 if (num_empty == BTRFS_NUM_BACKUP_ROOTS) {
44 return 0;
45 } else if (newest >= 0) {
46 return 0;
47 }
48
49 return -1;
Marek Behún21a14fa2017-09-03 17:00:28 +020050}
51
52static inline int is_power_of_2(u64 x)
53{
54 return !(x & (x - 1));
55}
56
57static int btrfs_check_super_csum(char *raw_disk_sb)
58{
59 struct btrfs_super_block *disk_sb =
60 (struct btrfs_super_block *) raw_disk_sb;
61 u16 csum_type = le16_to_cpu(disk_sb->csum_type);
62
63 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
64 u32 crc = ~(u32) 0;
65 const int csum_size = sizeof(crc);
66 char result[csum_size];
67
68 crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, crc,
69 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
70 btrfs_csum_final(crc, result);
71
72 if (memcmp(raw_disk_sb, result, csum_size))
73 return -1;
74 } else {
75 return -1;
76 }
77
78 return 0;
79}
80
81static int btrfs_check_super(struct btrfs_super_block *sb)
82{
83 int ret = 0;
84
85 if (sb->flags & ~BTRFS_SUPER_FLAG_SUPP) {
86 printf("%s: Unsupported flags: %llu\n", __func__,
87 sb->flags & ~BTRFS_SUPER_FLAG_SUPP);
88 }
89
90 if (sb->root_level > BTRFS_MAX_LEVEL) {
91 printf("%s: tree_root level too big: %d >= %d\n", __func__,
92 sb->root_level, BTRFS_MAX_LEVEL);
93 ret = -1;
94 }
95
96 if (sb->chunk_root_level > BTRFS_MAX_LEVEL) {
97 printf("%s: chunk_root level too big: %d >= %d\n", __func__,
98 sb->chunk_root_level, BTRFS_MAX_LEVEL);
99 ret = -1;
100 }
101
102 if (sb->log_root_level > BTRFS_MAX_LEVEL) {
103 printf("%s: log_root level too big: %d >= %d\n", __func__,
104 sb->log_root_level, BTRFS_MAX_LEVEL);
105 ret = -1;
106 }
107
108 if (!is_power_of_2(sb->sectorsize) || sb->sectorsize < 4096 ||
109 sb->sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
110 printf("%s: invalid sectorsize %u\n", __func__,
111 sb->sectorsize);
112 ret = -1;
113 }
114
115 if (!is_power_of_2(sb->nodesize) || sb->nodesize < sb->sectorsize ||
116 sb->nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
117 printf("%s: invalid nodesize %u\n", __func__, sb->nodesize);
118 ret = -1;
119 }
120
121 if (sb->nodesize != sb->__unused_leafsize) {
122 printf("%s: invalid leafsize %u, should be %u\n", __func__,
123 sb->__unused_leafsize, sb->nodesize);
124 ret = -1;
125 }
126
127 if (!IS_ALIGNED(sb->root, sb->sectorsize)) {
128 printf("%s: tree_root block unaligned: %llu\n", __func__,
129 sb->root);
130 ret = -1;
131 }
132
133 if (!IS_ALIGNED(sb->chunk_root, sb->sectorsize)) {
134 printf("%s: chunk_root block unaligned: %llu\n", __func__,
135 sb->chunk_root);
136 ret = -1;
137 }
138
139 if (!IS_ALIGNED(sb->log_root, sb->sectorsize)) {
140 printf("%s: log_root block unaligned: %llu\n", __func__,
141 sb->log_root);
142 ret = -1;
143 }
144
145 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
146 printf("%s: dev_item UUID does not match fsid\n", __func__);
147 ret = -1;
148 }
149
150 if (sb->bytes_used < 6*sb->nodesize) {
151 printf("%s: bytes_used is too small %llu\n", __func__,
152 sb->bytes_used);
153 ret = -1;
154 }
155
156 if (!is_power_of_2(sb->stripesize)) {
157 printf("%s: invalid stripesize %u\n", __func__, sb->stripesize);
158 ret = -1;
159 }
160
161 if (sb->sys_chunk_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
162 printf("%s: system chunk array too big %u > %u\n", __func__,
163 sb->sys_chunk_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
164 ret = -1;
165 }
166
167 if (sb->sys_chunk_array_size < sizeof(struct btrfs_key) +
168 sizeof(struct btrfs_chunk)) {
Tom Rinif39bfec2018-01-27 17:23:21 -0500169 printf("%s: system chunk array too small %u < %zu\n", __func__,
170 sb->sys_chunk_array_size, sizeof(struct btrfs_key)
Marek Behún21a14fa2017-09-03 17:00:28 +0200171 + sizeof(struct btrfs_chunk));
172 ret = -1;
173 }
174
175 return ret;
176}
177
178int btrfs_read_superblock(void)
179{
180 const u64 superblock_offsets[4] = {
181 0x10000ull,
182 0x4000000ull,
183 0x4000000000ull,
184 0x4000000000000ull
185 };
Marek Vasutc9795392018-09-22 04:13:35 +0200186 ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
Marek Behún21a14fa2017-09-03 17:00:28 +0200187 struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
188 u64 dev_total_bytes;
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300189 int i;
Marek Behún21a14fa2017-09-03 17:00:28 +0200190
191 dev_total_bytes = (u64) btrfs_part_info->size * btrfs_part_info->blksz;
192
193 btrfs_info.sb.generation = 0;
194
195 for (i = 0; i < 4; ++i) {
196 if (superblock_offsets[i] + sizeof(sb) > dev_total_bytes)
197 break;
198
199 if (!btrfs_devread(superblock_offsets[i], BTRFS_SUPER_INFO_SIZE,
200 raw_sb))
201 break;
202
203 if (btrfs_check_super_csum(raw_sb)) {
Marek Behún881e0202019-04-26 15:11:09 +0200204 debug("%s: invalid checksum at superblock mirror %i\n",
205 __func__, i);
Marek Behún21a14fa2017-09-03 17:00:28 +0200206 continue;
207 }
208
209 btrfs_super_block_to_cpu(sb);
210
211 if (sb->magic != BTRFS_MAGIC) {
Marek Behún881e0202019-04-26 15:11:09 +0200212 debug("%s: invalid BTRFS magic 0x%016llX at "
213 "superblock mirror %i\n", __func__, sb->magic, i);
Marek Behún21a14fa2017-09-03 17:00:28 +0200214 } else if (sb->bytenr != superblock_offsets[i]) {
215 printf("%s: invalid bytenr 0x%016llX (expected "
216 "0x%016llX) at superblock mirror %i\n",
217 __func__, sb->bytenr, superblock_offsets[i], i);
218 } else if (btrfs_check_super(sb)) {
219 printf("%s: Checking superblock mirror %i failed\n",
220 __func__, i);
221 } else if (sb->generation > btrfs_info.sb.generation) {
222 memcpy(&btrfs_info.sb, sb, sizeof(*sb));
223 } else {
224 /* Nothing */
225 }
226 }
227
228 if (!btrfs_info.sb.generation) {
Marek Behún881e0202019-04-26 15:11:09 +0200229 debug("%s: No valid BTRFS superblock found!\n", __func__);
Marek Behún21a14fa2017-09-03 17:00:28 +0200230 return -1;
231 }
232
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300233 if (btrfs_check_super_roots(&btrfs_info.sb)) {
Marek Behún21a14fa2017-09-03 17:00:28 +0200234 printf("%s: No valid root_backup found!\n", __func__);
235 return -1;
236 }
Marek Behún21a14fa2017-09-03 17:00:28 +0200237
Qu Wenruoa62db242020-03-26 13:35:55 +0800238 if (sb->sectorsize != PAGE_SIZE) {
239 printf(
240 "%s: Unsupported sector size (%u), only supports %u as sector size\n",
241 __func__, sb->sectorsize, PAGE_SIZE);
242 return -1;
243 }
244
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300245 if (btrfs_info.sb.num_devices != 1) {
Marek Behún21a14fa2017-09-03 17:00:28 +0200246 printf("%s: Unsupported number of devices (%lli). This driver "
247 "only supports filesystem on one device.\n", __func__,
Yevgeny Popovychd146a7b2018-06-11 14:14:33 +0300248 btrfs_info.sb.num_devices);
Marek Behún21a14fa2017-09-03 17:00:28 +0200249 return -1;
250 }
251
252 debug("Chosen superblock with generation = %llu\n",
253 btrfs_info.sb.generation);
254
255 return 0;
256}