spa.h 11 KB

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