super.c 6.4 KB

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