super.c 6.2 KB

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