super.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include <common.h>
  8. #include <log.h>
  9. #include <memalign.h>
  10. #include <part.h>
  11. #include <linux/compat.h>
  12. #include "btrfs.h"
  13. #include "disk-io.h"
  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. #define BTRFS_SUPER_INFO_SIZE 4096
  20. /*
  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. */
  25. static int btrfs_check_super_roots(struct btrfs_super_block *sb)
  26. {
  27. struct btrfs_root_backup *root_backup;
  28. int i, newest = -1;
  29. int num_empty = 0;
  30. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; ++i) {
  31. root_backup = sb->super_roots + i;
  32. if (root_backup->tree_root == 0 && root_backup->tree_root_gen == 0)
  33. num_empty++;
  34. if (root_backup->tree_root_gen == sb->generation)
  35. newest = i;
  36. }
  37. if (num_empty == BTRFS_NUM_BACKUP_ROOTS) {
  38. return 0;
  39. } else if (newest >= 0) {
  40. return 0;
  41. }
  42. return -1;
  43. }
  44. static inline int is_power_of_2(u64 x)
  45. {
  46. return !(x & (x - 1));
  47. }
  48. static int btrfs_check_super_csum(char *raw_disk_sb)
  49. {
  50. struct btrfs_super_block *disk_sb =
  51. (struct btrfs_super_block *) raw_disk_sb;
  52. u16 csum_type = le16_to_cpu(disk_sb->csum_type);
  53. if (csum_type == BTRFS_CSUM_TYPE_CRC32 ||
  54. csum_type == BTRFS_CSUM_TYPE_SHA256 ||
  55. csum_type == BTRFS_CSUM_TYPE_XXHASH) {
  56. u8 result[BTRFS_CSUM_SIZE];
  57. btrfs_csum_data(csum_type, (u8 *)raw_disk_sb + BTRFS_CSUM_SIZE,
  58. result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  59. if (memcmp(raw_disk_sb, result, BTRFS_CSUM_SIZE))
  60. return -EIO;
  61. } else if (csum_type == BTRFS_CSUM_TYPE_BLAKE2) {
  62. printf("Blake2 csum type is not supported yet\n");
  63. return -ENOTSUPP;
  64. }
  65. return 0;
  66. }
  67. static int btrfs_check_super(struct btrfs_super_block *sb)
  68. {
  69. int ret = 0;
  70. if (sb->flags & ~BTRFS_SUPER_FLAG_SUPP) {
  71. printf("%s: Unsupported flags: %llu\n", __func__,
  72. sb->flags & ~BTRFS_SUPER_FLAG_SUPP);
  73. }
  74. if (sb->root_level > BTRFS_MAX_LEVEL) {
  75. printf("%s: tree_root level too big: %d >= %d\n", __func__,
  76. sb->root_level, BTRFS_MAX_LEVEL);
  77. ret = -1;
  78. }
  79. if (sb->chunk_root_level > BTRFS_MAX_LEVEL) {
  80. printf("%s: chunk_root level too big: %d >= %d\n", __func__,
  81. sb->chunk_root_level, BTRFS_MAX_LEVEL);
  82. ret = -1;
  83. }
  84. if (sb->log_root_level > BTRFS_MAX_LEVEL) {
  85. printf("%s: log_root level too big: %d >= %d\n", __func__,
  86. sb->log_root_level, BTRFS_MAX_LEVEL);
  87. ret = -1;
  88. }
  89. if (!is_power_of_2(sb->sectorsize) || sb->sectorsize < 4096 ||
  90. sb->sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  91. printf("%s: invalid sectorsize %u\n", __func__,
  92. sb->sectorsize);
  93. ret = -1;
  94. }
  95. if (!is_power_of_2(sb->nodesize) || sb->nodesize < sb->sectorsize ||
  96. sb->nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  97. printf("%s: invalid nodesize %u\n", __func__, sb->nodesize);
  98. ret = -1;
  99. }
  100. if (sb->nodesize != sb->__unused_leafsize) {
  101. printf("%s: invalid leafsize %u, should be %u\n", __func__,
  102. sb->__unused_leafsize, sb->nodesize);
  103. ret = -1;
  104. }
  105. if (!IS_ALIGNED(sb->root, sb->sectorsize)) {
  106. printf("%s: tree_root block unaligned: %llu\n", __func__,
  107. sb->root);
  108. ret = -1;
  109. }
  110. if (!IS_ALIGNED(sb->chunk_root, sb->sectorsize)) {
  111. printf("%s: chunk_root block unaligned: %llu\n", __func__,
  112. sb->chunk_root);
  113. ret = -1;
  114. }
  115. if (!IS_ALIGNED(sb->log_root, sb->sectorsize)) {
  116. printf("%s: log_root block unaligned: %llu\n", __func__,
  117. sb->log_root);
  118. ret = -1;
  119. }
  120. if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
  121. printf("%s: dev_item UUID does not match fsid\n", __func__);
  122. ret = -1;
  123. }
  124. if (sb->bytes_used < 6*sb->nodesize) {
  125. printf("%s: bytes_used is too small %llu\n", __func__,
  126. sb->bytes_used);
  127. ret = -1;
  128. }
  129. if (!is_power_of_2(sb->stripesize)) {
  130. printf("%s: invalid stripesize %u\n", __func__, sb->stripesize);
  131. ret = -1;
  132. }
  133. if (sb->sys_chunk_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  134. printf("%s: system chunk array too big %u > %u\n", __func__,
  135. sb->sys_chunk_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
  136. ret = -1;
  137. }
  138. if (sb->sys_chunk_array_size < sizeof(struct btrfs_key) +
  139. sizeof(struct btrfs_chunk)) {
  140. printf("%s: system chunk array too small %u < %zu\n", __func__,
  141. sb->sys_chunk_array_size, sizeof(struct btrfs_key)
  142. + sizeof(struct btrfs_chunk));
  143. ret = -1;
  144. }
  145. return ret;
  146. }
  147. int btrfs_read_superblock(void)
  148. {
  149. const u64 superblock_offsets[4] = {
  150. 0x10000ull,
  151. 0x4000000ull,
  152. 0x4000000000ull,
  153. 0x4000000000000ull
  154. };
  155. ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
  156. struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
  157. u64 dev_total_bytes;
  158. int i;
  159. dev_total_bytes = (u64) btrfs_part_info->size * btrfs_part_info->blksz;
  160. btrfs_info.sb.generation = 0;
  161. for (i = 0; i < 4; ++i) {
  162. if (superblock_offsets[i] + sizeof(sb) > dev_total_bytes)
  163. break;
  164. if (!btrfs_devread(superblock_offsets[i], BTRFS_SUPER_INFO_SIZE,
  165. raw_sb))
  166. break;
  167. if (btrfs_check_super_csum(raw_sb)) {
  168. debug("%s: invalid checksum at superblock mirror %i\n",
  169. __func__, i);
  170. continue;
  171. }
  172. btrfs_super_block_to_cpu(sb);
  173. if (sb->magic != BTRFS_MAGIC) {
  174. debug("%s: invalid BTRFS magic 0x%016llX at "
  175. "superblock mirror %i\n", __func__, sb->magic, i);
  176. } else if (sb->bytenr != superblock_offsets[i]) {
  177. printf("%s: invalid bytenr 0x%016llX (expected "
  178. "0x%016llX) at superblock mirror %i\n",
  179. __func__, sb->bytenr, superblock_offsets[i], i);
  180. } else if (btrfs_check_super(sb)) {
  181. printf("%s: Checking superblock mirror %i failed\n",
  182. __func__, i);
  183. } else if (sb->generation > btrfs_info.sb.generation) {
  184. memcpy(&btrfs_info.sb, sb, sizeof(*sb));
  185. } else {
  186. /* Nothing */
  187. }
  188. }
  189. if (!btrfs_info.sb.generation) {
  190. debug("%s: No valid BTRFS superblock found!\n", __func__);
  191. return -1;
  192. }
  193. if (btrfs_check_super_roots(&btrfs_info.sb)) {
  194. printf("%s: No valid root_backup found!\n", __func__);
  195. return -1;
  196. }
  197. if (sb->sectorsize != PAGE_SIZE) {
  198. printf(
  199. "%s: Unsupported sector size (%u), only supports %u as sector size\n",
  200. __func__, sb->sectorsize, PAGE_SIZE);
  201. return -1;
  202. }
  203. if (btrfs_info.sb.num_devices != 1) {
  204. printf("%s: Unsupported number of devices (%lli). This driver "
  205. "only supports filesystem on one device.\n", __func__,
  206. btrfs_info.sb.num_devices);
  207. return -1;
  208. }
  209. debug("Chosen superblock with generation = %llu\n",
  210. btrfs_info.sb.generation);
  211. return 0;
  212. }