super.c 5.9 KB

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