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