spa.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
  9. */
  10. #ifndef ZFS_SPA_HEADER
  11. #define ZFS_SPA_HEADER 1
  12. /*
  13. * General-purpose 32-bit and 64-bit bitfield encodings.
  14. */
  15. #define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len))
  16. #define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len))
  17. #define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low))
  18. #define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low))
  19. #define BF32_GET(x, low, len) BF32_DECODE(x, low, len)
  20. #define BF64_GET(x, low, len) BF64_DECODE(x, low, len)
  21. #define BF32_SET(x, low, len, val) \
  22. ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
  23. #define BF64_SET(x, low, len, val) \
  24. ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
  25. #define BF32_GET_SB(x, low, len, shift, bias) \
  26. ((BF32_GET(x, low, len) + (bias)) << (shift))
  27. #define BF64_GET_SB(x, low, len, shift, bias) \
  28. ((BF64_GET(x, low, len) + (bias)) << (shift))
  29. #define BF32_SET_SB(x, low, len, shift, bias, val) \
  30. BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
  31. #define BF64_SET_SB(x, low, len, shift, bias, val) \
  32. BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
  33. /*
  34. * We currently support nine block sizes, from 512 bytes to 128K.
  35. * We could go higher, but the benefits are near-zero and the cost
  36. * of COWing a giant block to modify one byte would become excessive.
  37. */
  38. #define SPA_MINBLOCKSHIFT 9
  39. #define SPA_MAXBLOCKSHIFT 17
  40. #define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT)
  41. #define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT)
  42. #define SPA_BLOCKSIZES (SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
  43. /*
  44. * Size of block to hold the configuration data (a packed nvlist)
  45. */
  46. #define SPA_CONFIG_BLOCKSIZE (1 << 14)
  47. /*
  48. * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
  49. * The ASIZE encoding should be at least 64 times larger (6 more bits)
  50. * to support up to 4-way RAID-Z mirror mode with worst-case gang block
  51. * overhead, three DVAs per bp, plus one more bit in case we do anything
  52. * else that expands the ASIZE.
  53. */
  54. #define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */
  55. #define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */
  56. #define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */
  57. /*
  58. * All SPA data is represented by 128-bit data virtual addresses (DVAs).
  59. * The members of the dva_t should be considered opaque outside the SPA.
  60. */
  61. typedef struct dva {
  62. uint64_t dva_word[2];
  63. } dva_t;
  64. /*
  65. * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
  66. */
  67. typedef struct zio_cksum {
  68. uint64_t zc_word[4];
  69. } zio_cksum_t;
  70. /*
  71. * Each block is described by its DVAs, time of birth, checksum, etc.
  72. * The word-by-word, bit-by-bit layout of the blkptr is as follows:
  73. *
  74. * 64 56 48 40 32 24 16 8 0
  75. * +-------+-------+-------+-------+-------+-------+-------+-------+
  76. * 0 | vdev1 | GRID | ASIZE |
  77. * +-------+-------+-------+-------+-------+-------+-------+-------+
  78. * 1 |G| offset1 |
  79. * +-------+-------+-------+-------+-------+-------+-------+-------+
  80. * 2 | vdev2 | GRID | ASIZE |
  81. * +-------+-------+-------+-------+-------+-------+-------+-------+
  82. * 3 |G| offset2 |
  83. * +-------+-------+-------+-------+-------+-------+-------+-------+
  84. * 4 | vdev3 | GRID | ASIZE |
  85. * +-------+-------+-------+-------+-------+-------+-------+-------+
  86. * 5 |G| offset3 |
  87. * +-------+-------+-------+-------+-------+-------+-------+-------+
  88. * 6 |BDX|lvl| type | cksum | comp | PSIZE | LSIZE |
  89. * +-------+-------+-------+-------+-------+-------+-------+-------+
  90. * 7 | padding |
  91. * +-------+-------+-------+-------+-------+-------+-------+-------+
  92. * 8 | padding |
  93. * +-------+-------+-------+-------+-------+-------+-------+-------+
  94. * 9 | physical birth txg |
  95. * +-------+-------+-------+-------+-------+-------+-------+-------+
  96. * a | logical birth txg |
  97. * +-------+-------+-------+-------+-------+-------+-------+-------+
  98. * b | fill count |
  99. * +-------+-------+-------+-------+-------+-------+-------+-------+
  100. * c | checksum[0] |
  101. * +-------+-------+-------+-------+-------+-------+-------+-------+
  102. * d | checksum[1] |
  103. * +-------+-------+-------+-------+-------+-------+-------+-------+
  104. * e | checksum[2] |
  105. * +-------+-------+-------+-------+-------+-------+-------+-------+
  106. * f | checksum[3] |
  107. * +-------+-------+-------+-------+-------+-------+-------+-------+
  108. *
  109. * Legend:
  110. *
  111. * vdev virtual device ID
  112. * offset offset into virtual device
  113. * LSIZE logical size
  114. * PSIZE physical size (after compression)
  115. * ASIZE allocated size (including RAID-Z parity and gang block headers)
  116. * GRID RAID-Z layout information (reserved for future use)
  117. * cksum checksum function
  118. * comp compression function
  119. * G gang block indicator
  120. * B byteorder (endianness)
  121. * D dedup
  122. * X unused
  123. * lvl level of indirection
  124. * type DMU object type
  125. * phys birth txg of block allocation; zero if same as logical birth txg
  126. * log. birth transaction group in which the block was logically born
  127. * fill count number of non-zero blocks under this bp
  128. * checksum[4] 256-bit checksum of the data this bp describes
  129. */
  130. #define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */
  131. #define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */
  132. typedef struct blkptr {
  133. dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
  134. uint64_t blk_prop; /* size, compression, type, etc */
  135. uint64_t blk_pad[2]; /* Extra space for the future */
  136. uint64_t blk_phys_birth; /* txg when block was allocated */
  137. uint64_t blk_birth; /* transaction group at birth */
  138. uint64_t blk_fill; /* fill count */
  139. zio_cksum_t blk_cksum; /* 256-bit checksum */
  140. } blkptr_t;
  141. /*
  142. * Macros to get and set fields in a bp or DVA.
  143. */
  144. #define DVA_GET_ASIZE(dva) \
  145. BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
  146. #define DVA_SET_ASIZE(dva, x) \
  147. BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
  148. #define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8)
  149. #define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x)
  150. #define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32)
  151. #define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x)
  152. #define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1)
  153. #define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x)
  154. #define BP_GET_LSIZE(bp) \
  155. BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
  156. #define BP_SET_LSIZE(bp, x) \
  157. BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
  158. #define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 8)
  159. #define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 8, x)
  160. #define BP_GET_CHECKSUM(bp) BF64_GET((bp)->blk_prop, 40, 8)
  161. #define BP_SET_CHECKSUM(bp, x) BF64_SET((bp)->blk_prop, 40, 8, x)
  162. #define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8)
  163. #define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x)
  164. #define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5)
  165. #define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x)
  166. #define BP_GET_PROP_BIT_61(bp) BF64_GET((bp)->blk_prop, 61, 1)
  167. #define BP_SET_PROP_BIT_61(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x)
  168. #define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1)
  169. #define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x)
  170. #define BP_GET_BYTEORDER(bp) (0 - BF64_GET((bp)->blk_prop, 63, 1))
  171. #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
  172. #define BP_PHYSICAL_BIRTH(bp) \
  173. ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
  174. #define BP_SET_BIRTH(bp, logical, physical) \
  175. { \
  176. (bp)->blk_birth = (logical); \
  177. (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
  178. }
  179. #define BP_GET_ASIZE(bp) \
  180. (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
  181. DVA_GET_ASIZE(&(bp)->blk_dva[2]))
  182. #define BP_GET_UCSIZE(bp) \
  183. ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
  184. BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
  185. #define BP_GET_NDVAS(bp) \
  186. (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
  187. !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
  188. !!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
  189. #define BP_COUNT_GANG(bp) \
  190. (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
  191. DVA_GET_GANG(&(bp)->blk_dva[1]) + \
  192. DVA_GET_GANG(&(bp)->blk_dva[2]))
  193. #define DVA_EQUAL(dva1, dva2) \
  194. ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
  195. (dva1)->dva_word[0] == (dva2)->dva_word[0])
  196. #define BP_EQUAL(bp1, bp2) \
  197. (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \
  198. DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \
  199. DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \
  200. DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
  201. #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \
  202. (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
  203. ((zc1).zc_word[1] - (zc2).zc_word[1]) | \
  204. ((zc1).zc_word[2] - (zc2).zc_word[2]) | \
  205. ((zc1).zc_word[3] - (zc2).zc_word[3])))
  206. #define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0)
  207. #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \
  208. { \
  209. (zcp)->zc_word[0] = w0; \
  210. (zcp)->zc_word[1] = w1; \
  211. (zcp)->zc_word[2] = w2; \
  212. (zcp)->zc_word[3] = w3; \
  213. }
  214. #define BP_IDENTITY(bp) (&(bp)->blk_dva[0])
  215. #define BP_IS_GANG(bp) DVA_GET_GANG(BP_IDENTITY(bp))
  216. #define BP_IS_HOLE(bp) ((bp)->blk_birth == 0)
  217. /* BP_IS_RAIDZ(bp) assumes no block compression */
  218. #define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
  219. BP_GET_PSIZE(bp))
  220. #define BP_ZERO(bp) \
  221. { \
  222. (bp)->blk_dva[0].dva_word[0] = 0; \
  223. (bp)->blk_dva[0].dva_word[1] = 0; \
  224. (bp)->blk_dva[1].dva_word[0] = 0; \
  225. (bp)->blk_dva[1].dva_word[1] = 0; \
  226. (bp)->blk_dva[2].dva_word[0] = 0; \
  227. (bp)->blk_dva[2].dva_word[1] = 0; \
  228. (bp)->blk_prop = 0; \
  229. (bp)->blk_pad[0] = 0; \
  230. (bp)->blk_pad[1] = 0; \
  231. (bp)->blk_phys_birth = 0; \
  232. (bp)->blk_birth = 0; \
  233. (bp)->blk_fill = 0; \
  234. ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
  235. }
  236. #define BP_SPRINTF_LEN 320
  237. #endif /* ! ZFS_SPA_HEADER */