1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A/*
1N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A#ifndef _SYS_SPA_H
1N/A#define _SYS_SPA_H
1N/A
1N/A/*
1N/A * General-purpose 32-bit and 64-bit bitfield encodings.
1N/A */
1N/A#define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len))
1N/A#define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len))
1N/A#define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low))
1N/A#define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low))
1N/A
1N/A#define BF32_GET(x, low, len) BF32_DECODE(x, low, len)
1N/A#define BF64_GET(x, low, len) BF64_DECODE(x, low, len)
1N/A
1N/A#define BF32_SET(x, low, len, val) \
1N/A ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
1N/A#define BF64_SET(x, low, len, val) \
1N/A ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
1N/A
1N/A#define BF32_GET_SB(x, low, len, shift, bias) \
1N/A ((BF32_GET(x, low, len) + (bias)) << (shift))
1N/A#define BF64_GET_SB(x, low, len, shift, bias) \
1N/A ((BF64_GET(x, low, len) + (bias)) << (shift))
1N/A
1N/A#define BF32_SET_SB(x, low, len, shift, bias, val) \
1N/A BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
1N/A#define BF64_SET_SB(x, low, len, shift, bias, val) \
1N/A BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
1N/A
1N/A/*
1N/A * We currently support nine block sizes, from 512 bytes to 128K.
1N/A * We could go higher, but the benefits are near-zero and the cost
1N/A * of COWing a giant block to modify one byte would become excessive.
1N/A */
1N/A#define SPA_MINBLOCKSHIFT 9
1N/A#define SPA_MAXBLOCKSHIFT 20
1N/A#define SPA_128KBLOCKSHIFT 17
1N/A#define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT)
1N/A#define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT)
1N/A
1N/A#define SPA_BLOCKSIZES (SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
1N/A
1N/A/*
1N/A * Size of block to hold the configuration data (a packed nvlist)
1N/A */
1N/A#define SPA_CONFIG_BLOCKSIZE (1 << 14)
1N/A
1N/A/*
1N/A * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
1N/A * The ASIZE encoding should be at least 64 times larger (6 more bits)
1N/A * to support up to 4-way RAID-Z mirror mode with worst-case gang block
1N/A * overhead, three DVAs per bp, plus one more bit in case we do anything
1N/A * else that expands the ASIZE.
1N/A */
1N/A#define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */
1N/A#define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */
1N/A#define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */
1N/A
1N/A/*
1N/A * All SPA data is represented by 128-bit data virtual addresses (DVAs).
1N/A * The members of the dva_t should be considered opaque outside the SPA.
1N/A */
1N/Atypedef struct dva {
1N/A uint64_t dva_word[2];
1N/A} dva_t;
1N/A
1N/A/*
1N/A * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
1N/A */
1N/Atypedef struct zio_cksum {
1N/A uint64_t zc_word[4];
1N/A} zio_cksum_t;
1N/A
1N/A/*
1N/A * Each block is described by its DVAs, time of birth, checksum, etc.
1N/A * The word-by-word, bit-by-bit layout of the blkptr is as follows:
1N/A *
1N/A * 64 56 48 40 32 24 16 8 0
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 0 | vdev1 |ncopy|L| ASIZE |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 1 |G| offset1 |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 2 | vdev2 |ncopy|L| ASIZE |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 3 |G| offset2 |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 4 | vdev3 |ncopy|L| ASIZE |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 5 |G| offset3 |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 6 |BDE|lvl| type | cksum | comp | PSIZE | LSIZE |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 7 | padding |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 8 | padding |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * 9 | physical birth txg |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * a | logical birth txg |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * b | fill count |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * c | checksum[0] |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * d | checksum[1] |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * e | checksum[2] |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A * f | checksum[3] |
1N/A * +-------+-------+-------+-------+-------+-------+-------+-------+
1N/A *
1N/A * Legend:
1N/A *
1N/A * vdev virtual device ID
1N/A * offset offset into virtual device
1N/A * LSIZE logical size
1N/A * PSIZE physical size (after compression)
1N/A * ASIZE allocated size (including RAID-Z parity and gang block headers)
1N/A * L layout (e.g. standard vs. RAID-Z/mirror hybrid)
1N/A * ncopy number of copies if RAID-Z, otherwise 1
1N/A * cksum checksum function
1N/A * comp compression function
1N/A * G gang block indicator
1N/A * B byteorder (endianness)
1N/A * D dedup
1N/A * E encryption
1N/A * lvl level of indirection
1N/A * type DMU object type
1N/A * phys birth txg of block allocation; zero if same as logical birth txg
1N/A * log. birth transaction group in which the block was logically born
1N/A * fill count number of non-zero blocks under this bp
1N/A * checksum[4] 256-bit checksum of the data this bp describes
1N/A *
1N/A * Special notes for encryption:
1N/A *
1N/A * A single bit is used to indicate if the block is encrypted. This is
1N/A * sufficient since all blocks in a dataset always share the same encryption
1N/A * algorithm-keylen-mode.
1N/A *
1N/A * When encryption is enabled blk_dva[2] holds the IV.
1N/A * When encryption is enabled level 0 blocks checksum[2] and checksum[3] hold
1N/A * the MAC output from the encryption and the normal checksum is truncated
1N/A * and stored in checksum[0] and checksum[1].
1N/A *
1N/A */
1N/A#define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */
1N/A#define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */
1N/A
1N/Atypedef struct blkptr {
1N/A dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
1N/A uint64_t blk_prop; /* size, compression, type, etc */
1N/A uint64_t blk_pad[2]; /* Extra space for the future */
1N/A uint64_t blk_phys_birth; /* txg when block was allocated */
1N/A uint64_t blk_birth; /* transaction group at birth */
1N/A uint64_t blk_fill; /* fill count */
1N/A zio_cksum_t blk_cksum; /* 256-bit checksum */
1N/A} blkptr_t;
1N/A
1N/A/*
1N/A * DVA layouts. Normally mirror vdevs contain mirrored data, RAID-Z vdevs
1N/A * contain RAID-Z data, etc. However, for latency-sensitive metadata,
1N/A * we can use a mirrored layout across the children of a RAID-Z vdev.
1N/A * This ensures that such metadata can be read in a single I/O.
1N/A */
1N/Atypedef enum dva_layout {
1N/A DVA_LAYOUT_STANDARD = 0,
1N/A DVA_LAYOUT_RAIDZ_MIRROR
1N/A} dva_layout_t;
1N/A
1N/A/*
1N/A * Macros to get and set fields in a bp or DVA.
1N/A */
1N/A#define DVA_GET_ASIZE(dva) \
1N/A BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
1N/A#define DVA_SET_ASIZE(dva, x) \
1N/A BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
1N/A
1N/A#define DVA_GET_LAYOUT(dva) BF64_GET((dva)->dva_word[0], 24, 2)
1N/A#define DVA_SET_LAYOUT(dva, x) BF64_SET((dva)->dva_word[0], 24, 2, x)
1N/A
1N/A#define DVA_GET_COPIES(dva) BF64_GET_SB((dva)->dva_word[0], 26, 6, 0, 1)
1N/A#define DVA_SET_COPIES(dva, x) BF64_SET_SB((dva)->dva_word[0], 26, 6, 0, 1, x)
1N/A
1N/A#define DVA_MAX_COPIES (1ULL << 6)
1N/A#define DVA_MAX_INFLATION (4ULL)
1N/A
1N/A#define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32)
1N/A#define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x)
1N/A
1N/A#define DVA_GET_OFFSET(dva) \
1N/A BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
1N/A#define DVA_SET_OFFSET(dva, x) \
1N/A BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
1N/A
1N/A#define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1)
1N/A#define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x)
1N/A
1N/A#define DVA_EQUAL(dva1, dva2) \
1N/A ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
1N/A (dva1)->dva_word[0] == (dva2)->dva_word[0])
1N/A
1N/A#define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0)
1N/A
1N/A#define DVA_VALID_COPIES(dva) \
1N/A (DVA_IS_VALID(dva) ? DVA_GET_COPIES(dva) : 0)
1N/A
1N/A#define BP_GET_LSIZE(bp) \
1N/A BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
1N/A#define BP_SET_LSIZE(bp, x) \
1N/A BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
1N/A
1N/A#define BP_GET_PSIZE(bp) \
1N/A BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
1N/A#define BP_SET_PSIZE(bp, x) \
1N/A BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
1N/A
1N/A#define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 8)
1N/A#define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 8, x)
1N/A
1N/A#define BP_GET_CHECKSUM(bp) BF64_GET((bp)->blk_prop, 40, 8)
1N/A#define BP_SET_CHECKSUM(bp, x) BF64_SET((bp)->blk_prop, 40, 8, x)
1N/A
1N/A#define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8)
1N/A#define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x)
1N/A
1N/A#define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5)
1N/A#define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x)
1N/A
1N/A#define BP_GET_CRYPT(bp) BF64_GET((bp)->blk_prop, 61, 1)
1N/A#define BP_SET_CRYPT(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x)
1N/A#define BP_IS_ENCRYPTED(bp) (!!BP_GET_CRYPT(bp))
1N/A
1N/A#define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1)
1N/A#define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x)
1N/A
1N/A#define BP_GET_BYTEORDER(bp) (0 - BF64_GET((bp)->blk_prop, 63, 1))
1N/A#define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
1N/A
1N/A#define BP_PHYSICAL_BIRTH(bp) \
1N/A ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
1N/A
1N/A#define BP_SET_BIRTH(bp, logical, physical) \
1N/A{ \
1N/A (bp)->blk_birth = (logical); \
1N/A (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
1N/A}
1N/A
1N/A#define BP_GET_UCSIZE(bp) \
1N/A ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
1N/A BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
1N/A
1N/A#define BP_GET_ASIZE(bp) \
1N/A (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
1N/A DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
1N/A (BP_IS_ENCRYPTED(bp) ? 0 : DVA_GET_ASIZE(&(bp)->blk_dva[2])))
1N/A
1N/A#define BP_GET_NDVAS(bp) \
1N/A (DVA_IS_VALID(&(bp)->blk_dva[0]) + \
1N/A DVA_IS_VALID(&(bp)->blk_dva[1]) + \
1N/A (BP_IS_ENCRYPTED(bp) ? 0 : DVA_IS_VALID(&(bp)->blk_dva[2])))
1N/A
1N/A#define BP_GET_COPIES(bp) \
1N/A (DVA_VALID_COPIES(&(bp)->blk_dva[0]) + \
1N/A DVA_VALID_COPIES(&(bp)->blk_dva[1]) + \
1N/A (BP_IS_ENCRYPTED(bp) ? 0 : DVA_VALID_COPIES(&(bp)->blk_dva[2])))
1N/A
1N/A#define BP_COUNT_GANG(bp) \
1N/A (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
1N/A DVA_GET_GANG(&(bp)->blk_dva[1]) + \
1N/A (BP_IS_ENCRYPTED(bp) ? 0 : DVA_GET_GANG(&(bp)->blk_dva[2])))
1N/A
1N/A#define BP_EQUAL(bp1, bp2) \
1N/A (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \
1N/A DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \
1N/A DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \
1N/A DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
1N/A
1N/A#define ZIO_CHECKSUM_EQUAL(zc1, zc2) \
1N/A (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
1N/A ((zc1).zc_word[1] - (zc2).zc_word[1]) | \
1N/A ((zc1).zc_word[2] - (zc2).zc_word[2]) | \
1N/A ((zc1).zc_word[3] - (zc2).zc_word[3])))
1N/A
1N/A#define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \
1N/A{ \
1N/A (zcp)->zc_word[0] = w0; \
1N/A (zcp)->zc_word[1] = w1; \
1N/A (zcp)->zc_word[2] = w2; \
1N/A (zcp)->zc_word[3] = w3; \
1N/A}
1N/A
1N/A#define BP_IDENTITY(bp) (&(bp)->blk_dva[0])
1N/A#define BP_IS_GANG(bp) DVA_GET_GANG(BP_IDENTITY(bp))
1N/A#define BP_IS_HOLE(bp) ((bp)->blk_birth == 0)
1N/A
1N/A/* BP_IS_RAIDZ(bp) assumes no block compression */
1N/A#define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
1N/A BP_GET_PSIZE(bp))
1N/A
1N/A#define BP_ZERO(bp) \
1N/A{ \
1N/A (bp)->blk_dva[0].dva_word[0] = 0; \
1N/A (bp)->blk_dva[0].dva_word[1] = 0; \
1N/A (bp)->blk_dva[1].dva_word[0] = 0; \
1N/A (bp)->blk_dva[1].dva_word[1] = 0; \
1N/A (bp)->blk_dva[2].dva_word[0] = 0; \
1N/A (bp)->blk_dva[2].dva_word[1] = 0; \
1N/A (bp)->blk_prop = 0; \
1N/A (bp)->blk_pad[0] = 0; \
1N/A (bp)->blk_pad[1] = 0; \
1N/A (bp)->blk_phys_birth = 0; \
1N/A (bp)->blk_birth = 0; \
1N/A (bp)->blk_fill = 0; \
1N/A ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
1N/A}
1N/A/*
1N/A * Note: the byteorder is either 0 or -1, both of which are palindromes.
1N/A * This simplifies the endianness handling a bit.
1N/A */
1N/A#ifdef _BIG_ENDIAN
1N/A#define ZFS_HOST_BYTEORDER (0ULL)
1N/A#else
1N/A#define ZFS_HOST_BYTEORDER (-1ULL)
1N/A#endif
1N/A
1N/A#define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
1N/A
1N/A#define BP_SPRINTF_LEN 360
1N/A
1N/A#endif /* _SYS_SPA_H */