1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2016 by Delphix. All rights reserved. 24 */ 25 26 #ifndef _SYS_DDT_H 27 #define _SYS_DDT_H 28 29 #include <sys/sysmacros.h> 30 #include <sys/types.h> 31 #include <sys/fs/zfs.h> 32 #include <sys/zio.h> 33 #include <sys/dmu.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 struct abd; 40 41 /* 42 * On-disk DDT formats, in the desired search order (newest version first). 43 */ 44 enum ddt_type { 45 DDT_TYPE_ZAP = 0, 46 DDT_TYPES 47 }; 48 49 /* 50 * DDT classes, in the desired search order (highest replication level first). 51 */ 52 enum ddt_class { 53 DDT_CLASS_DITTO = 0, 54 DDT_CLASS_DUPLICATE, 55 DDT_CLASS_UNIQUE, 56 DDT_CLASSES 57 }; 58 59 #define DDT_TYPE_CURRENT 0 60 61 #define DDT_COMPRESS_BYTEORDER_MASK 0x80 62 #define DDT_COMPRESS_FUNCTION_MASK 0x7f 63 64 /* 65 * On-disk ddt entry: key (name) and physical storage (value). 66 */ 67 typedef struct ddt_key { 68 zio_cksum_t ddk_cksum; /* 256-bit block checksum */ 69 /* 70 * Encoded with logical & physical size, encryption, and compression, 71 * as follows: 72 * +-------+-------+-------+-------+-------+-------+-------+-------+ 73 * | 0 | 0 | 0 |X| comp| PSIZE | LSIZE | 74 * +-------+-------+-------+-------+-------+-------+-------+-------+ 75 */ 76 uint64_t ddk_prop; 77 } ddt_key_t; 78 79 #define DDK_GET_LSIZE(ddk) \ 80 BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1) 81 #define DDK_SET_LSIZE(ddk, x) \ 82 BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x) 83 84 #define DDK_GET_PSIZE(ddk) \ 85 BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1) 86 #define DDK_SET_PSIZE(ddk, x) \ 87 BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x) 88 89 #define DDK_GET_COMPRESS(ddk) BF64_GET((ddk)->ddk_prop, 32, 7) 90 #define DDK_SET_COMPRESS(ddk, x) BF64_SET((ddk)->ddk_prop, 32, 7, x) 91 92 #define DDK_GET_CRYPT(ddk) BF64_GET((ddk)->ddk_prop, 39, 1) 93 #define DDK_SET_CRYPT(ddk, x) BF64_SET((ddk)->ddk_prop, 39, 1, x) 94 95 #define DDT_KEY_WORDS (sizeof (ddt_key_t) / sizeof (uint64_t)) 96 97 #define DDE_GET_NDVAS(dde) (DDK_GET_CRYPT(&dde->dde_key) \ 98 ? SPA_DVAS_PER_BP - 1 : SPA_DVAS_PER_BP) 99 100 typedef struct ddt_phys { 101 dva_t ddp_dva[SPA_DVAS_PER_BP]; 102 uint64_t ddp_refcnt; 103 uint64_t ddp_phys_birth; 104 } ddt_phys_t; 105 106 enum ddt_phys_type { 107 DDT_PHYS_DITTO = 0, 108 DDT_PHYS_SINGLE = 1, 109 DDT_PHYS_DOUBLE = 2, 110 DDT_PHYS_TRIPLE = 3, 111 DDT_PHYS_TYPES 112 }; 113 114 /* 115 * In-core ddt entry 116 */ 117 struct ddt_entry { 118 ddt_key_t dde_key; 119 ddt_phys_t dde_phys[DDT_PHYS_TYPES]; 120 zio_t *dde_lead_zio[DDT_PHYS_TYPES]; 121 struct abd *dde_repair_abd; 122 enum ddt_type dde_type; 123 enum ddt_class dde_class; 124 uint8_t dde_loading; 125 uint8_t dde_loaded; 126 kcondvar_t dde_cv; 127 avl_node_t dde_node; 128 }; 129 130 /* 131 * In-core ddt 132 */ 133 struct ddt { 134 kmutex_t ddt_lock; 135 avl_tree_t ddt_tree; 136 avl_tree_t ddt_repair_tree; 137 enum zio_checksum ddt_checksum; 138 spa_t *ddt_spa; 139 objset_t *ddt_os; 140 uint64_t ddt_stat_object; 141 uint64_t ddt_object[DDT_TYPES][DDT_CLASSES]; 142 ddt_histogram_t ddt_histogram[DDT_TYPES][DDT_CLASSES]; 143 ddt_histogram_t ddt_histogram_cache[DDT_TYPES][DDT_CLASSES]; 144 ddt_object_t ddt_object_stats[DDT_TYPES][DDT_CLASSES]; 145 avl_node_t ddt_node; 146 }; 147 148 /* 149 * In-core and on-disk bookmark for DDT walks 150 */ 151 typedef struct ddt_bookmark { 152 uint64_t ddb_class; 153 uint64_t ddb_type; 154 uint64_t ddb_checksum; 155 uint64_t ddb_cursor; 156 } ddt_bookmark_t; 157 158 /* 159 * Ops vector to access a specific DDT object type. 160 */ 161 typedef struct ddt_ops { 162 char ddt_op_name[32]; 163 int (*ddt_op_create)(objset_t *os, uint64_t *object, dmu_tx_t *tx, 164 boolean_t prehash); 165 int (*ddt_op_destroy)(objset_t *os, uint64_t object, dmu_tx_t *tx); 166 int (*ddt_op_lookup)(objset_t *os, uint64_t object, ddt_entry_t *dde); 167 void (*ddt_op_prefetch)(objset_t *os, uint64_t object, 168 ddt_entry_t *dde); 169 int (*ddt_op_update)(objset_t *os, uint64_t object, ddt_entry_t *dde, 170 dmu_tx_t *tx); 171 int (*ddt_op_remove)(objset_t *os, uint64_t object, ddt_entry_t *dde, 172 dmu_tx_t *tx); 173 int (*ddt_op_walk)(objset_t *os, uint64_t object, ddt_entry_t *dde, 174 uint64_t *walk); 175 uint64_t (*ddt_op_count)(objset_t *os, uint64_t object); 176 } ddt_ops_t; 177 178 #define DDT_NAMELEN 80 179 180 extern void ddt_object_name(ddt_t *ddt, enum ddt_type type, 181 enum ddt_class class, char *name); 182 extern int ddt_object_walk(ddt_t *ddt, enum ddt_type type, 183 enum ddt_class class, uint64_t *walk, ddt_entry_t *dde); 184 extern uint64_t ddt_object_count(ddt_t *ddt, enum ddt_type type, 185 enum ddt_class class); 186 extern int ddt_object_info(ddt_t *ddt, enum ddt_type type, 187 enum ddt_class class, dmu_object_info_t *); 188 extern boolean_t ddt_object_exists(ddt_t *ddt, enum ddt_type type, 189 enum ddt_class class); 190 191 extern void ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, 192 uint64_t txg); 193 extern void ddt_bp_create(enum zio_checksum checksum, const ddt_key_t *ddk, 194 const ddt_phys_t *ddp, blkptr_t *bp); 195 196 extern void ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp); 197 198 extern void ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp); 199 extern void ddt_phys_clear(ddt_phys_t *ddp); 200 extern void ddt_phys_addref(ddt_phys_t *ddp); 201 extern void ddt_phys_decref(ddt_phys_t *ddp); 202 extern void ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, 203 uint64_t txg); 204 extern ddt_phys_t *ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp); 205 extern uint64_t ddt_phys_total_refcnt(const ddt_entry_t *dde); 206 207 extern void ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg); 208 209 extern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src); 210 extern void ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh); 211 extern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh); 212 extern void ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo); 213 extern void ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh); 214 extern void ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total); 215 216 extern uint64_t ddt_get_dedup_dspace(spa_t *spa); 217 extern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa); 218 219 extern int ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, 220 ddt_phys_t *ddp_willref); 221 extern int ddt_ditto_copies_present(ddt_entry_t *dde); 222 223 extern size_t ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len); 224 extern void ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len); 225 226 extern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp); 227 extern void ddt_enter(ddt_t *ddt); 228 extern void ddt_exit(ddt_t *ddt); 229 extern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add); 230 extern void ddt_prefetch(spa_t *spa, const blkptr_t *bp); 231 extern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde); 232 233 extern boolean_t ddt_class_contains(spa_t *spa, enum ddt_class max_class, 234 const blkptr_t *bp); 235 236 extern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp); 237 extern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde); 238 239 extern int ddt_entry_compare(const void *x1, const void *x2); 240 241 extern void ddt_create(spa_t *spa); 242 extern int ddt_load(spa_t *spa); 243 extern void ddt_unload(spa_t *spa); 244 extern void ddt_sync(spa_t *spa, uint64_t txg); 245 extern int ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde); 246 extern int ddt_object_update(ddt_t *ddt, enum ddt_type type, 247 enum ddt_class class, ddt_entry_t *dde, dmu_tx_t *tx); 248 249 extern const ddt_ops_t ddt_zap_ops; 250 251 #ifdef __cplusplus 252 } 253 #endif 254 255 #endif /* _SYS_DDT_H */ 256