super.c 6.4 KB

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