super.c 6.2 KB

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