1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * This file and its contents are supplied under the terms of the 4 * Common Development and Distribution License ("CDDL"), version 1.0. 5 * You may only use this file in accordance with the terms of version 6 * 1.0 of the CDDL. 7 * 8 * A full copy of the text of the CDDL should have accompanied this 9 * source. A copy of the CDDL is also available via the Internet at 10 * https://opensource.org/license/CDDL-1.0. 11 */ 12 /* 13 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 14 * Copyright (c) 2016 by Delphix. All rights reserved. 15 * Copyright (c) 2023, Klara Inc. 16 */ 17 18 #ifndef _SYS_DDT_H 19 #define _SYS_DDT_H 20 21 #include <sys/sysmacros.h> 22 #include <sys/types.h> 23 #include <sys/fs/zfs.h> 24 #include <sys/zio.h> 25 #include <sys/dmu.h> 26 #include <sys/wmsum.h> 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 struct abd; 33 34 /* 35 * DDT-wide feature flags. These are set in ddt_flags by ddt_configure(). 36 */ 37 #define DDT_FLAG_FLAT (1 << 0) /* single extensible phys */ 38 #define DDT_FLAG_LOG (1 << 1) /* dedup log (journal) */ 39 #define DDT_FLAG_MASK (DDT_FLAG_FLAT|DDT_FLAG_LOG) 40 41 /* 42 * DDT on-disk storage object types. Each one corresponds to specific 43 * implementation, see ddt_ops_t. The value itself is not stored on disk. 44 * 45 * When searching for an entry, objects types will be searched in this order. 46 * 47 * Note that DDT_TYPES is used as the "no type" for new entries that have not 48 * yet been written to a storage object. 49 */ 50 typedef enum { 51 DDT_TYPE_ZAP = 0, /* ZAP storage object, ddt_zap */ 52 DDT_TYPES 53 } ddt_type_t; 54 55 _Static_assert(DDT_TYPES <= UINT8_MAX, 56 "ddt_type_t must fit in a uint8_t"); 57 58 /* New and updated entries recieve this type, see ddt_sync_entry() */ 59 #define DDT_TYPE_DEFAULT (DDT_TYPE_ZAP) 60 61 /* 62 * DDT storage classes. Each class has a separate storage object for each type. 63 * The value itself is not stored on disk. 64 * 65 * When search for an entry, object classes will be searched in this order. 66 * 67 * Note that DDT_CLASSES is used as the "no class" for new entries that have not 68 * yet been written to a storage object. 69 */ 70 typedef enum { 71 DDT_CLASS_DITTO = 0, /* entry has ditto blocks (obsolete) */ 72 DDT_CLASS_DUPLICATE, /* entry has multiple references */ 73 DDT_CLASS_UNIQUE, /* entry has a single reference */ 74 DDT_CLASSES 75 } ddt_class_t; 76 77 _Static_assert(DDT_CLASSES < UINT8_MAX, 78 "ddt_class_t must fit in a uint8_t"); 79 80 /* 81 * The "key" part of an on-disk entry. This is the unique "name" for a block, 82 * that is, that parts of the block pointer that will always be the same for 83 * the same data. 84 */ 85 typedef struct { 86 zio_cksum_t ddk_cksum; /* 256-bit block checksum */ 87 /* 88 * Encoded with logical & physical size, encryption, and compression, 89 * as follows: 90 * +-------+-------+-------+-------+-------+-------+-------+-------+ 91 * | 0 | 0 | 0 |X| comp| PSIZE | LSIZE | 92 * +-------+-------+-------+-------+-------+-------+-------+-------+ 93 */ 94 uint64_t ddk_prop; 95 } ddt_key_t; 96 97 /* 98 * Macros for accessing parts of a ddt_key_t. These are similar to their BP_* 99 * counterparts. 100 */ 101 #define DDK_GET_LSIZE(ddk) \ 102 BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1) 103 #define DDK_SET_LSIZE(ddk, x) \ 104 BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x) 105 106 #define DDK_GET_PSIZE(ddk) \ 107 BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1) 108 #define DDK_SET_PSIZE(ddk, x) \ 109 BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x) 110 111 #define DDK_GET_COMPRESS(ddk) BF64_GET((ddk)->ddk_prop, 32, 7) 112 #define DDK_SET_COMPRESS(ddk, x) BF64_SET((ddk)->ddk_prop, 32, 7, x) 113 114 #define DDK_GET_CRYPT(ddk) BF64_GET((ddk)->ddk_prop, 39, 1) 115 #define DDK_SET_CRYPT(ddk, x) BF64_SET((ddk)->ddk_prop, 39, 1, x) 116 117 /* 118 * The "value" part for an on-disk entry. These are the "physical" 119 * characteristics of the stored block, such as its location on disk (DVAs), 120 * birth txg and ref count. 121 * 122 * The "traditional" entry has an array of four, one for each value of the 123 * copies= property the block was written with and another for additional 124 * "ditto" copies. (The stored block's BP may carry more DVAs than copies=, 125 * eg a gang header is stored in more copies than the data it gangs, so the 126 * slot is not the BP's DVA count.) Users of the traditional struct will 127 * specify the variant (index) of the one they want. 128 * 129 * The newer "flat" entry has only a single form that is specified using the 130 * DDT_PHYS_FLAT variant. 131 * 132 * Since the value size varies, use one of the size macros when interfacing 133 * with the ddt zap. 134 */ 135 136 #define DDT_PHYS_MAX (4) 137 138 /* 139 * Note - this can be used in a flexible array and allocated for 140 * a specific size (ddp_trad or ddp_flat). So be careful not to 141 * copy using "=" assignment but instead use ddt_phys_copy(). 142 */ 143 typedef union { 144 /* 145 * Traditional physical payload value for DDT zap (256 bytes) 146 */ 147 struct { 148 dva_t ddp_dva[SPA_DVAS_PER_BP]; 149 uint64_t ddp_refcnt; 150 uint64_t ddp_phys_birth; 151 } ddp_trad[DDT_PHYS_MAX]; 152 153 /* 154 * Flat physical payload value for DDT zap (72 bytes) 155 */ 156 struct { 157 dva_t ddp_dva[SPA_DVAS_PER_BP]; 158 uint64_t ddp_refcnt; 159 uint64_t ddp_phys_birth; /* txg based from BP */ 160 uint64_t ddp_class_start; /* in realtime seconds */ 161 } ddp_flat; 162 } ddt_univ_phys_t; 163 164 /* 165 * This enum denotes which variant of a ddt_univ_phys_t to target. For 166 * a traditional DDT entry, it represents the indexes into the ddp_trad 167 * array. Any consumer of a ddt_univ_phys_t needs to know which variant 168 * is being targeted. 169 * 170 * Note, we no longer generate new DDT_PHYS_DITTO-type blocks. However, 171 * we maintain the ability to free existing dedup-ditto blocks. 172 */ 173 174 typedef enum { 175 DDT_PHYS_DITTO = 0, 176 DDT_PHYS_SINGLE = 1, 177 DDT_PHYS_DOUBLE = 2, 178 DDT_PHYS_TRIPLE = 3, 179 DDT_PHYS_FLAT = 4, 180 DDT_PHYS_NONE = 5 181 } ddt_phys_variant_t; 182 183 #define DDT_PHYS_VARIANT(ddt, p) \ 184 (ASSERT((p) < DDT_PHYS_NONE), \ 185 ((ddt)->ddt_flags & DDT_FLAG_FLAT ? DDT_PHYS_FLAT : (p))) 186 187 #define DDT_TRAD_PHYS_SIZE sizeof (((ddt_univ_phys_t *)0)->ddp_trad) 188 #define DDT_FLAT_PHYS_SIZE sizeof (((ddt_univ_phys_t *)0)->ddp_flat) 189 190 #define _DDT_PHYS_SWITCH(ddt, flat, trad) \ 191 (((ddt)->ddt_flags & DDT_FLAG_FLAT) ? (flat) : (trad)) 192 193 #define DDT_PHYS_SIZE(ddt) _DDT_PHYS_SWITCH(ddt, \ 194 DDT_FLAT_PHYS_SIZE, DDT_TRAD_PHYS_SIZE) 195 196 #define DDT_NPHYS(ddt) _DDT_PHYS_SWITCH(ddt, 1, DDT_PHYS_MAX) 197 #define DDT_PHYS_FOR_COPIES(ddt, p) _DDT_PHYS_SWITCH(ddt, 0, p) 198 #define DDT_PHYS_IS_DITTO(ddt, p) _DDT_PHYS_SWITCH(ddt, 0, (p == 0)) 199 200 /* 201 * A "live" entry, holding changes to an entry made this txg, and other data to 202 * support loading, updating and repairing the entry. 203 */ 204 205 /* State flags for dde_flags */ 206 #define DDE_FLAG_LOADED (1 << 0) /* entry ready for use */ 207 #define DDE_FLAG_OVERQUOTA (1 << 1) /* entry unusable, no space */ 208 #define DDE_FLAG_LOGGED (1 << 2) /* loaded from log */ 209 #define DDE_FLAG_FROM_FLUSHING (1 << 3) /* loaded from flushing log */ 210 211 /* 212 * Additional data to support entry update or repair. This is fixed size 213 * because its relatively rarely used. 214 */ 215 typedef struct { 216 /* protects dde_phys, dde_orig_phys and dde_lead_zio during I/O */ 217 kmutex_t dde_io_lock; 218 219 /* copy of data after a repair read, to be rewritten */ 220 abd_t *dde_repair_abd; 221 222 /* original phys contents before update, for error handling */ 223 ddt_univ_phys_t dde_orig_phys; 224 225 /* in-flight update IOs */ 226 zio_t *dde_lead_zio[DDT_PHYS_MAX]; 227 } ddt_entry_io_t; 228 229 typedef struct { 230 /* key must be first for ddt_key_compare */ 231 ddt_key_t dde_key; /* ddt_tree key */ 232 avl_node_t dde_node; /* ddt_tree_node */ 233 234 /* storage type and class the entry was loaded from */ 235 ddt_type_t dde_type; 236 ddt_class_t dde_class; 237 238 uint8_t dde_flags; /* load state flags */ 239 kcondvar_t dde_cv; /* signaled when load completes */ 240 uint64_t dde_waiters; /* count of waiters on dde_cv */ 241 242 ddt_entry_io_t *dde_io; /* IO support, when required */ 243 244 ddt_univ_phys_t dde_phys[]; /* flexible -- allocated size varies */ 245 } ddt_entry_t; 246 247 /* 248 * A lightweight entry is for short-lived or transient uses, like iterating or 249 * inspecting, when you don't care where it came from. 250 */ 251 typedef struct { 252 ddt_key_t ddlwe_key; 253 ddt_type_t ddlwe_type; 254 ddt_class_t ddlwe_class; 255 ddt_univ_phys_t ddlwe_phys; 256 } ddt_lightweight_entry_t; 257 258 /* 259 * In-core DDT log. A separate struct to make it easier to switch between the 260 * appending and flushing logs. 261 */ 262 typedef struct { 263 avl_tree_t ddl_tree; /* logged entries */ 264 uint32_t ddl_flags; /* flags for this log */ 265 uint64_t ddl_object; /* log object id */ 266 uint64_t ddl_length; /* on-disk log size */ 267 uint64_t ddl_first_txg; /* txg log became active */ 268 ddt_key_t ddl_checkpoint; /* last checkpoint */ 269 } ddt_log_t; 270 271 /* 272 * In-core DDT object. This covers all entries and stats for a the whole pool 273 * for a given checksum type. 274 */ 275 typedef struct { 276 kmutex_t ddt_lock; /* protects changes to all fields */ 277 avl_tree_t ddt_tree; /* "live" (changed) entries this txg */ 278 avl_tree_t ddt_repair_tree; /* entries being repaired */ 279 280 /* Protects ddt_object[] and ddt_object_dnode[]. */ 281 krwlock_t ddt_objects_lock ____cacheline_aligned; 282 283 /* 284 * Log trees are stable during I/O, and only modified during sync 285 * with exclusive access. 286 */ 287 ddt_log_t ddt_log[2] ____cacheline_aligned; /* logged entries */ 288 ddt_log_t *ddt_log_active; /* pointers into ddt_log */ 289 ddt_log_t *ddt_log_flushing; /* swapped when flush starts */ 290 291 int32_t ddt_log_ingest_rate; /* rolling log ingest rate */ 292 int32_t ddt_log_flush_rate; /* rolling log flush rate */ 293 int32_t ddt_log_flush_time_rate; /* avg time spent flushing */ 294 uint32_t ddt_log_flush_pressure; /* pressure to apply for cap */ 295 uint32_t ddt_log_flush_prev_backlog; /* prev backlog size */ 296 297 uint64_t ddt_flush_force_txg; /* flush hard before this txg */ 298 299 kstat_t *ddt_ksp; /* kstats context */ 300 301 /* wmsums for hot-path lookup counters */ 302 wmsum_t ddt_kstat_dds_lookup; 303 wmsum_t ddt_kstat_dds_lookup_live_hit; 304 wmsum_t ddt_kstat_dds_lookup_live_wait; 305 wmsum_t ddt_kstat_dds_lookup_live_miss; 306 wmsum_t ddt_kstat_dds_lookup_existing; 307 wmsum_t ddt_kstat_dds_lookup_new; 308 wmsum_t ddt_kstat_dds_lookup_log_hit; 309 wmsum_t ddt_kstat_dds_lookup_log_active_hit; 310 wmsum_t ddt_kstat_dds_lookup_log_flushing_hit; 311 wmsum_t ddt_kstat_dds_lookup_log_miss; 312 wmsum_t ddt_kstat_dds_lookup_stored_hit; 313 wmsum_t ddt_kstat_dds_lookup_stored_miss; 314 315 enum zio_checksum ddt_checksum; /* checksum algorithm in use */ 316 spa_t *ddt_spa; /* pool this ddt is on */ 317 objset_t *ddt_os; /* ddt objset (always MOS) */ 318 319 uint64_t ddt_dir_object; /* MOS dir holding ddt objects */ 320 uint64_t ddt_version; /* DDT version */ 321 uint64_t ddt_flags; /* FDT option flags */ 322 323 /* per-type/per-class entry store objects */ 324 uint64_t ddt_object[DDT_TYPES][DDT_CLASSES]; 325 dnode_t *ddt_object_dnode[DDT_TYPES][DDT_CLASSES]; 326 327 /* object ids for stored, logged and per-type/per-class stats */ 328 uint64_t ddt_stat_object; 329 ddt_object_t ddt_log_stats; 330 ddt_object_t ddt_object_stats[DDT_TYPES][DDT_CLASSES]; 331 332 /* type/class stats by power-2-sized referenced blocks */ 333 ddt_histogram_t ddt_histogram[DDT_TYPES][DDT_CLASSES]; 334 ddt_histogram_t ddt_histogram_cache[DDT_TYPES][DDT_CLASSES]; 335 336 /* log stats power-2-sized referenced blocks */ 337 ddt_histogram_t ddt_log_histogram; 338 } ddt_t; 339 340 /* 341 * In-core and on-disk bookmark for DDT walks. This is a cursor for ddt_walk(), 342 * and is stable across calls, even if the DDT is updated, the pool is 343 * restarted or loaded on another system, or OpenZFS is upgraded. 344 */ 345 typedef struct { 346 uint64_t ddb_class; 347 uint64_t ddb_type; 348 uint64_t ddb_checksum; 349 uint64_t ddb_cursor; 350 } ddt_bookmark_t; 351 352 extern void ddt_bp_fill(const ddt_univ_phys_t *ddp, ddt_phys_variant_t v, 353 blkptr_t *bp, uint64_t txg); 354 extern void ddt_bp_create(enum zio_checksum checksum, const ddt_key_t *ddk, 355 const ddt_univ_phys_t *ddp, ddt_phys_variant_t v, blkptr_t *bp); 356 357 extern void ddt_phys_extend(ddt_univ_phys_t *ddp, ddt_phys_variant_t v, 358 const blkptr_t *bp); 359 extern void ddt_phys_unextend(ddt_univ_phys_t *cur, ddt_univ_phys_t *orig, 360 ddt_phys_variant_t v); 361 extern void ddt_phys_copy(ddt_univ_phys_t *dst, const ddt_univ_phys_t *src, 362 ddt_phys_variant_t v); 363 extern void ddt_phys_clear(ddt_univ_phys_t *ddp, ddt_phys_variant_t v); 364 extern void ddt_phys_addref(ddt_univ_phys_t *ddp, ddt_phys_variant_t v); 365 extern uint64_t ddt_phys_decref(ddt_univ_phys_t *ddp, ddt_phys_variant_t v); 366 extern uint64_t ddt_phys_refcnt(const ddt_univ_phys_t *ddp, 367 ddt_phys_variant_t v); 368 extern ddt_phys_variant_t ddt_phys_select(const ddt_t *ddt, 369 const ddt_entry_t *dde, const blkptr_t *bp); 370 extern uint64_t ddt_phys_birth(const ddt_univ_phys_t *ddp, 371 ddt_phys_variant_t v); 372 extern int ddt_phys_is_gang(const ddt_univ_phys_t *ddp, 373 ddt_phys_variant_t v); 374 extern int ddt_phys_dva_count(const ddt_univ_phys_t *ddp, ddt_phys_variant_t v, 375 boolean_t encrypted); 376 377 extern void ddt_histogram_add_entry(ddt_t *ddt, ddt_histogram_t *ddh, 378 const ddt_lightweight_entry_t *ddlwe); 379 extern void ddt_histogram_sub_entry(ddt_t *ddt, ddt_histogram_t *ddh, 380 const ddt_lightweight_entry_t *ddlwe); 381 382 extern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src); 383 extern void ddt_histogram_total(ddt_stat_t *dds, const ddt_histogram_t *ddh); 384 extern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh); 385 386 extern void ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo); 387 extern uint64_t ddt_get_ddt_dsize(spa_t *spa); 388 extern void ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh); 389 extern void ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total); 390 391 extern uint64_t ddt_get_dedup_dspace(spa_t *spa); 392 extern uint64_t ddt_get_dedup_used(spa_t *spa); 393 extern uint64_t ddt_get_dedup_saved(spa_t *spa); 394 extern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa); 395 extern int ddt_get_pool_dedup_cached(spa_t *spa, uint64_t *psize); 396 extern uint64_t ddt_sync_dirty_est(spa_t *spa); 397 398 extern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp); 399 extern void ddt_enter(ddt_t *ddt); 400 extern void ddt_exit(ddt_t *ddt); 401 extern void ddt_init(void); 402 extern void ddt_fini(void); 403 extern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp, 404 boolean_t verify); 405 extern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde); 406 extern void ddt_prefetch(spa_t *spa, const blkptr_t *bp); 407 extern void ddt_prefetch_all(spa_t *spa); 408 409 extern boolean_t ddt_class_contains(spa_t *spa, ddt_class_t max_class, 410 const blkptr_t *bp); 411 412 extern void ddt_alloc_entry_io(ddt_entry_t *dde); 413 414 extern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp); 415 extern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde); 416 417 extern int ddt_key_compare(const void *x1, const void *x2); 418 419 extern void ddt_create(spa_t *spa); 420 extern int ddt_load(spa_t *spa); 421 extern void ddt_unload(spa_t *spa); 422 extern void ddt_sync(spa_t *spa, uint64_t txg); 423 424 extern void ddt_walk_init(spa_t *spa, uint64_t txg); 425 extern boolean_t ddt_walk_ready(spa_t *spa); 426 extern int ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, 427 ddt_lightweight_entry_t *ddlwe); 428 429 extern boolean_t ddt_addref(spa_t *spa, const blkptr_t *bp); 430 431 extern int ddt_prune_unique_entries(spa_t *spa, zpool_ddt_prune_unit_t unit, 432 uint64_t amount); 433 434 #ifdef __cplusplus 435 } 436 #endif 437 438 #endif /* _SYS_DDT_H */ 439