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 /* 23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <sys/zfs_context.h> 27 #include <sys/spa.h> 28 #include <sys/spa_impl.h> 29 #include <sys/zio.h> 30 #include <sys/ddt.h> 31 #include <sys/zap.h> 32 #include <sys/dmu_tx.h> 33 #include <sys/arc.h> 34 #include <sys/dsl_pool.h> 35 #include <sys/zio_checksum.h> 36 #include <sys/zio_compress.h> 37 #include <sys/dsl_scan.h> 38 39 /* 40 * Enable/disable prefetching of dedup-ed blocks which are going to be freed. 41 */ 42 int zfs_dedup_prefetch = 1; 43 44 static const ddt_ops_t *ddt_ops[DDT_TYPES] = { 45 &ddt_zap_ops, 46 }; 47 48 static const char *ddt_class_name[DDT_CLASSES] = { 49 "ditto", 50 "duplicate", 51 "unique", 52 }; 53 54 static void 55 ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 56 dmu_tx_t *tx) 57 { 58 spa_t *spa = ddt->ddt_spa; 59 objset_t *os = ddt->ddt_os; 60 uint64_t *objectp = &ddt->ddt_object[type][class]; 61 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_dedup; 62 char name[DDT_NAMELEN]; 63 64 ddt_object_name(ddt, type, class, name); 65 66 ASSERT(*objectp == 0); 67 VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0); 68 ASSERT(*objectp != 0); 69 70 VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name, 71 sizeof (uint64_t), 1, objectp, tx) == 0); 72 73 VERIFY(zap_add(os, spa->spa_ddt_stat_object, name, 74 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), 75 &ddt->ddt_histogram[type][class], tx) == 0); 76 } 77 78 static void 79 ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 80 dmu_tx_t *tx) 81 { 82 spa_t *spa = ddt->ddt_spa; 83 objset_t *os = ddt->ddt_os; 84 uint64_t *objectp = &ddt->ddt_object[type][class]; 85 char name[DDT_NAMELEN]; 86 87 ddt_object_name(ddt, type, class, name); 88 89 ASSERT(*objectp != 0); 90 ASSERT(ddt_object_count(ddt, type, class) == 0); 91 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class])); 92 VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0); 93 VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0); 94 VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0); 95 bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t)); 96 97 *objectp = 0; 98 } 99 100 static int 101 ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 102 { 103 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class]; 104 dmu_object_info_t doi; 105 char name[DDT_NAMELEN]; 106 int error; 107 108 ddt_object_name(ddt, type, class, name); 109 110 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name, 111 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]); 112 113 if (error) 114 return (error); 115 116 error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name, 117 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), 118 &ddt->ddt_histogram[type][class]); 119 120 /* 121 * Seed the cached statistics. 122 */ 123 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0); 124 125 ddo->ddo_count = ddt_object_count(ddt, type, class); 126 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9; 127 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size; 128 129 ASSERT(error == 0); 130 return (error); 131 } 132 133 static void 134 ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 135 dmu_tx_t *tx) 136 { 137 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class]; 138 dmu_object_info_t doi; 139 char name[DDT_NAMELEN]; 140 141 ddt_object_name(ddt, type, class, name); 142 143 VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name, 144 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), 145 &ddt->ddt_histogram[type][class], tx) == 0); 146 147 /* 148 * Cache DDT statistics; this is the only time they'll change. 149 */ 150 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0); 151 152 ddo->ddo_count = ddt_object_count(ddt, type, class); 153 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9; 154 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size; 155 } 156 157 static int 158 ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 159 ddt_entry_t *dde) 160 { 161 if (!ddt_object_exists(ddt, type, class)) 162 return (ENOENT); 163 164 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os, 165 ddt->ddt_object[type][class], dde)); 166 } 167 168 static void 169 ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 170 ddt_entry_t *dde) 171 { 172 if (!ddt_object_exists(ddt, type, class)) 173 return; 174 175 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os, 176 ddt->ddt_object[type][class], dde); 177 } 178 179 int 180 ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 181 ddt_entry_t *dde, dmu_tx_t *tx) 182 { 183 ASSERT(ddt_object_exists(ddt, type, class)); 184 185 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os, 186 ddt->ddt_object[type][class], dde, tx)); 187 } 188 189 static int 190 ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 191 ddt_entry_t *dde, dmu_tx_t *tx) 192 { 193 ASSERT(ddt_object_exists(ddt, type, class)); 194 195 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os, 196 ddt->ddt_object[type][class], dde, tx)); 197 } 198 199 int 200 ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 201 uint64_t *walk, ddt_entry_t *dde) 202 { 203 ASSERT(ddt_object_exists(ddt, type, class)); 204 205 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os, 206 ddt->ddt_object[type][class], dde, walk)); 207 } 208 209 uint64_t 210 ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 211 { 212 ASSERT(ddt_object_exists(ddt, type, class)); 213 214 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os, 215 ddt->ddt_object[type][class])); 216 } 217 218 int 219 ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 220 dmu_object_info_t *doi) 221 { 222 if (!ddt_object_exists(ddt, type, class)) 223 return (ENOENT); 224 225 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class], 226 doi)); 227 } 228 229 boolean_t 230 ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 231 { 232 return (!!ddt->ddt_object[type][class]); 233 } 234 235 void 236 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class, 237 char *name) 238 { 239 (void) sprintf(name, DMU_POOL_DDT, 240 zio_checksum_table[ddt->ddt_checksum].ci_name, 241 ddt_ops[type]->ddt_op_name, ddt_class_name[class]); 242 } 243 244 void 245 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg) 246 { 247 ASSERT(txg != 0); 248 249 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 250 bp->blk_dva[d] = ddp->ddp_dva[d]; 251 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth); 252 } 253 254 void 255 ddt_bp_create(enum zio_checksum checksum, 256 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp) 257 { 258 BP_ZERO(bp); 259 260 if (ddp != NULL) 261 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth); 262 263 bp->blk_cksum = ddk->ddk_cksum; 264 bp->blk_fill = 1; 265 266 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk)); 267 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk)); 268 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk)); 269 BP_SET_CHECKSUM(bp, checksum); 270 BP_SET_TYPE(bp, DMU_OT_DEDUP); 271 BP_SET_LEVEL(bp, 0); 272 BP_SET_DEDUP(bp, 0); 273 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 274 } 275 276 void 277 ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp) 278 { 279 ddk->ddk_cksum = bp->blk_cksum; 280 ddk->ddk_prop = 0; 281 282 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp)); 283 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp)); 284 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp)); 285 } 286 287 void 288 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp) 289 { 290 ASSERT(ddp->ddp_phys_birth == 0); 291 292 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 293 ddp->ddp_dva[d] = bp->blk_dva[d]; 294 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp); 295 } 296 297 void 298 ddt_phys_clear(ddt_phys_t *ddp) 299 { 300 bzero(ddp, sizeof (*ddp)); 301 } 302 303 void 304 ddt_phys_addref(ddt_phys_t *ddp) 305 { 306 ddp->ddp_refcnt++; 307 } 308 309 void 310 ddt_phys_decref(ddt_phys_t *ddp) 311 { 312 ASSERT((int64_t)ddp->ddp_refcnt > 0); 313 ddp->ddp_refcnt--; 314 } 315 316 void 317 ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg) 318 { 319 blkptr_t blk; 320 321 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 322 ddt_phys_clear(ddp); 323 zio_free(ddt->ddt_spa, txg, &blk); 324 } 325 326 ddt_phys_t * 327 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp) 328 { 329 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys; 330 331 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 332 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) && 333 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth) 334 return (ddp); 335 } 336 return (NULL); 337 } 338 339 uint64_t 340 ddt_phys_total_refcnt(const ddt_entry_t *dde) 341 { 342 uint64_t refcnt = 0; 343 344 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) 345 refcnt += dde->dde_phys[p].ddp_refcnt; 346 347 return (refcnt); 348 } 349 350 static void 351 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds) 352 { 353 spa_t *spa = ddt->ddt_spa; 354 ddt_phys_t *ddp = dde->dde_phys; 355 ddt_key_t *ddk = &dde->dde_key; 356 uint64_t lsize = DDK_GET_LSIZE(ddk); 357 uint64_t psize = DDK_GET_PSIZE(ddk); 358 359 bzero(dds, sizeof (*dds)); 360 361 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 362 uint64_t dsize = 0; 363 uint64_t refcnt = ddp->ddp_refcnt; 364 365 if (ddp->ddp_phys_birth == 0) 366 continue; 367 368 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 369 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]); 370 371 dds->dds_blocks += 1; 372 dds->dds_lsize += lsize; 373 dds->dds_psize += psize; 374 dds->dds_dsize += dsize; 375 376 dds->dds_ref_blocks += refcnt; 377 dds->dds_ref_lsize += lsize * refcnt; 378 dds->dds_ref_psize += psize * refcnt; 379 dds->dds_ref_dsize += dsize * refcnt; 380 } 381 } 382 383 void 384 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg) 385 { 386 const uint64_t *s = (const uint64_t *)src; 387 uint64_t *d = (uint64_t *)dst; 388 uint64_t *d_end = (uint64_t *)(dst + 1); 389 390 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */ 391 392 while (d < d_end) 393 *d++ += (*s++ ^ neg) - neg; 394 } 395 396 static void 397 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg) 398 { 399 ddt_stat_t dds; 400 ddt_histogram_t *ddh; 401 int bucket; 402 403 ddt_stat_generate(ddt, dde, &dds); 404 405 bucket = highbit(dds.dds_ref_blocks) - 1; 406 ASSERT(bucket >= 0); 407 408 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class]; 409 410 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg); 411 } 412 413 void 414 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src) 415 { 416 for (int h = 0; h < 64; h++) 417 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0); 418 } 419 420 void 421 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh) 422 { 423 bzero(dds, sizeof (*dds)); 424 425 for (int h = 0; h < 64; h++) 426 ddt_stat_add(dds, &ddh->ddh_stat[h], 0); 427 } 428 429 boolean_t 430 ddt_histogram_empty(const ddt_histogram_t *ddh) 431 { 432 const uint64_t *s = (const uint64_t *)ddh; 433 const uint64_t *s_end = (const uint64_t *)(ddh + 1); 434 435 while (s < s_end) 436 if (*s++ != 0) 437 return (B_FALSE); 438 439 return (B_TRUE); 440 } 441 442 void 443 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total) 444 { 445 /* Sum the statistics we cached in ddt_object_sync(). */ 446 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 447 ddt_t *ddt = spa->spa_ddt[c]; 448 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 449 for (enum ddt_class class = 0; class < DDT_CLASSES; 450 class++) { 451 ddt_object_t *ddo = 452 &ddt->ddt_object_stats[type][class]; 453 ddo_total->ddo_count += ddo->ddo_count; 454 ddo_total->ddo_dspace += ddo->ddo_dspace; 455 ddo_total->ddo_mspace += ddo->ddo_mspace; 456 } 457 } 458 } 459 460 /* ... and compute the averages. */ 461 if (ddo_total->ddo_count != 0) { 462 ddo_total->ddo_dspace /= ddo_total->ddo_count; 463 ddo_total->ddo_mspace /= ddo_total->ddo_count; 464 } else { 465 ASSERT(ddo_total->ddo_dspace == 0); 466 ASSERT(ddo_total->ddo_mspace == 0); 467 } 468 } 469 470 void 471 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh) 472 { 473 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 474 ddt_t *ddt = spa->spa_ddt[c]; 475 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 476 for (enum ddt_class class = 0; class < DDT_CLASSES; 477 class++) { 478 ddt_histogram_add(ddh, 479 &ddt->ddt_histogram_cache[type][class]); 480 } 481 } 482 } 483 } 484 485 void 486 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total) 487 { 488 ddt_histogram_t *ddh_total; 489 490 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP); 491 ddt_get_dedup_histogram(spa, ddh_total); 492 ddt_histogram_stat(dds_total, ddh_total); 493 kmem_free(ddh_total, sizeof (ddt_histogram_t)); 494 } 495 496 uint64_t 497 ddt_get_dedup_dspace(spa_t *spa) 498 { 499 ddt_stat_t dds_total = { 0 }; 500 501 ddt_get_dedup_stats(spa, &dds_total); 502 return (dds_total.dds_ref_dsize - dds_total.dds_dsize); 503 } 504 505 uint64_t 506 ddt_get_pool_dedup_ratio(spa_t *spa) 507 { 508 ddt_stat_t dds_total = { 0 }; 509 510 ddt_get_dedup_stats(spa, &dds_total); 511 if (dds_total.dds_dsize == 0) 512 return (100); 513 514 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize); 515 } 516 517 int 518 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref) 519 { 520 spa_t *spa = ddt->ddt_spa; 521 uint64_t total_refcnt = 0; 522 uint64_t ditto = spa->spa_dedup_ditto; 523 int total_copies = 0; 524 int desired_copies = 0; 525 526 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 527 ddt_phys_t *ddp = &dde->dde_phys[p]; 528 zio_t *zio = dde->dde_lead_zio[p]; 529 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */ 530 if (zio != NULL) 531 refcnt += zio->io_parent_count; /* pending refs */ 532 if (ddp == ddp_willref) 533 refcnt++; /* caller's ref */ 534 if (refcnt != 0) { 535 total_refcnt += refcnt; 536 total_copies += p; 537 } 538 } 539 540 if (ditto == 0 || ditto > UINT32_MAX) 541 ditto = UINT32_MAX; 542 543 if (total_refcnt >= 1) 544 desired_copies++; 545 if (total_refcnt >= ditto) 546 desired_copies++; 547 if (total_refcnt >= ditto * ditto) 548 desired_copies++; 549 550 return (MAX(desired_copies, total_copies) - total_copies); 551 } 552 553 int 554 ddt_ditto_copies_present(ddt_entry_t *dde) 555 { 556 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO]; 557 dva_t *dva = ddp->ddp_dva; 558 int copies = 0 - DVA_GET_GANG(dva); 559 560 for (int d = 0; d < SPA_DVAS_PER_BP; d++, dva++) 561 if (DVA_IS_VALID(dva)) 562 copies++; 563 564 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP); 565 566 return (copies); 567 } 568 569 size_t 570 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len) 571 { 572 uchar_t *version = dst++; 573 int cpfunc = ZIO_COMPRESS_ZLE; 574 zio_compress_info_t *ci = &zio_compress_table[cpfunc]; 575 size_t c_len; 576 577 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */ 578 579 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level); 580 581 if (c_len == s_len) { 582 cpfunc = ZIO_COMPRESS_OFF; 583 bcopy(src, dst, s_len); 584 } 585 586 *version = (ZFS_HOST_BYTEORDER & DDT_COMPRESS_BYTEORDER_MASK) | cpfunc; 587 588 return (c_len + 1); 589 } 590 591 void 592 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len) 593 { 594 uchar_t version = *src++; 595 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK; 596 zio_compress_info_t *ci = &zio_compress_table[cpfunc]; 597 598 if (ci->ci_decompress != NULL) 599 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level); 600 else 601 bcopy(src, dst, d_len); 602 603 if ((version ^ ZFS_HOST_BYTEORDER) & DDT_COMPRESS_BYTEORDER_MASK) 604 byteswap_uint64_array(dst, d_len); 605 } 606 607 ddt_t * 608 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c) 609 { 610 return (spa->spa_ddt[c]); 611 } 612 613 ddt_t * 614 ddt_select(spa_t *spa, const blkptr_t *bp) 615 { 616 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]); 617 } 618 619 void 620 ddt_enter(ddt_t *ddt) 621 { 622 mutex_enter(&ddt->ddt_lock); 623 } 624 625 void 626 ddt_exit(ddt_t *ddt) 627 { 628 mutex_exit(&ddt->ddt_lock); 629 } 630 631 static ddt_entry_t * 632 ddt_alloc(const ddt_key_t *ddk) 633 { 634 ddt_entry_t *dde; 635 636 dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP); 637 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL); 638 639 dde->dde_key = *ddk; 640 641 return (dde); 642 } 643 644 static void 645 ddt_free(ddt_entry_t *dde) 646 { 647 ASSERT(!dde->dde_loading); 648 649 for (int p = 0; p < DDT_PHYS_TYPES; p++) 650 ASSERT(dde->dde_lead_zio[p] == NULL); 651 652 if (dde->dde_repair_data != NULL) 653 zio_buf_free(dde->dde_repair_data, 654 DDK_GET_PSIZE(&dde->dde_key)); 655 656 cv_destroy(&dde->dde_cv); 657 kmem_free(dde, sizeof (*dde)); 658 } 659 660 void 661 ddt_remove(ddt_t *ddt, ddt_entry_t *dde) 662 { 663 ASSERT(MUTEX_HELD(&ddt->ddt_lock)); 664 665 avl_remove(&ddt->ddt_tree, dde); 666 ddt_free(dde); 667 } 668 669 ddt_entry_t * 670 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add) 671 { 672 ddt_entry_t *dde, dde_search; 673 enum ddt_type type; 674 enum ddt_class class; 675 avl_index_t where; 676 int error; 677 678 ASSERT(MUTEX_HELD(&ddt->ddt_lock)); 679 680 ddt_key_fill(&dde_search.dde_key, bp); 681 682 dde = avl_find(&ddt->ddt_tree, &dde_search, &where); 683 if (dde == NULL) { 684 if (!add) 685 return (NULL); 686 dde = ddt_alloc(&dde_search.dde_key); 687 avl_insert(&ddt->ddt_tree, dde, where); 688 } 689 690 while (dde->dde_loading) 691 cv_wait(&dde->dde_cv, &ddt->ddt_lock); 692 693 if (dde->dde_loaded) 694 return (dde); 695 696 dde->dde_loading = B_TRUE; 697 698 ddt_exit(ddt); 699 700 error = ENOENT; 701 702 for (type = 0; type < DDT_TYPES; type++) { 703 for (class = 0; class < DDT_CLASSES; class++) { 704 error = ddt_object_lookup(ddt, type, class, dde); 705 if (error != ENOENT) 706 break; 707 } 708 if (error != ENOENT) 709 break; 710 } 711 712 ASSERT(error == 0 || error == ENOENT); 713 714 ddt_enter(ddt); 715 716 ASSERT(dde->dde_loaded == B_FALSE); 717 ASSERT(dde->dde_loading == B_TRUE); 718 719 dde->dde_type = type; /* will be DDT_TYPES if no entry found */ 720 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */ 721 dde->dde_loaded = B_TRUE; 722 dde->dde_loading = B_FALSE; 723 724 if (error == 0) 725 ddt_stat_update(ddt, dde, -1ULL); 726 727 cv_broadcast(&dde->dde_cv); 728 729 return (dde); 730 } 731 732 void 733 ddt_prefetch(spa_t *spa, const blkptr_t *bp) 734 { 735 ddt_t *ddt; 736 ddt_entry_t dde; 737 738 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp)) 739 return; 740 741 /* 742 * We remove the DDT once it's empty and only prefetch dedup blocks 743 * when there are entries in the DDT. Thus no locking is required 744 * as the DDT can't disappear on us. 745 */ 746 ddt = ddt_select(spa, bp); 747 ddt_key_fill(&dde.dde_key, bp); 748 749 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 750 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { 751 ddt_object_prefetch(ddt, type, class, &dde); 752 } 753 } 754 } 755 756 int 757 ddt_entry_compare(const void *x1, const void *x2) 758 { 759 const ddt_entry_t *dde1 = x1; 760 const ddt_entry_t *dde2 = x2; 761 const uint64_t *u1 = (const uint64_t *)&dde1->dde_key; 762 const uint64_t *u2 = (const uint64_t *)&dde2->dde_key; 763 764 for (int i = 0; i < DDT_KEY_WORDS; i++) { 765 if (u1[i] < u2[i]) 766 return (-1); 767 if (u1[i] > u2[i]) 768 return (1); 769 } 770 771 return (0); 772 } 773 774 static ddt_t * 775 ddt_table_alloc(spa_t *spa, enum zio_checksum c) 776 { 777 ddt_t *ddt; 778 779 ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP); 780 781 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL); 782 avl_create(&ddt->ddt_tree, ddt_entry_compare, 783 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node)); 784 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare, 785 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node)); 786 ddt->ddt_checksum = c; 787 ddt->ddt_spa = spa; 788 ddt->ddt_os = spa->spa_meta_objset; 789 790 return (ddt); 791 } 792 793 static void 794 ddt_table_free(ddt_t *ddt) 795 { 796 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0); 797 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0); 798 avl_destroy(&ddt->ddt_tree); 799 avl_destroy(&ddt->ddt_repair_tree); 800 mutex_destroy(&ddt->ddt_lock); 801 kmem_free(ddt, sizeof (*ddt)); 802 } 803 804 void 805 ddt_create(spa_t *spa) 806 { 807 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM; 808 809 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) 810 spa->spa_ddt[c] = ddt_table_alloc(spa, c); 811 } 812 813 int 814 ddt_load(spa_t *spa) 815 { 816 int error; 817 818 ddt_create(spa); 819 820 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 821 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1, 822 &spa->spa_ddt_stat_object); 823 824 if (error) 825 return (error == ENOENT ? 0 : error); 826 827 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 828 ddt_t *ddt = spa->spa_ddt[c]; 829 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 830 for (enum ddt_class class = 0; class < DDT_CLASSES; 831 class++) { 832 error = ddt_object_load(ddt, type, class); 833 if (error != 0 && error != ENOENT) 834 return (error); 835 } 836 } 837 838 /* 839 * Seed the cached histograms. 840 */ 841 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache, 842 sizeof (ddt->ddt_histogram)); 843 } 844 845 return (0); 846 } 847 848 void 849 ddt_unload(spa_t *spa) 850 { 851 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 852 if (spa->spa_ddt[c]) { 853 ddt_table_free(spa->spa_ddt[c]); 854 spa->spa_ddt[c] = NULL; 855 } 856 } 857 } 858 859 boolean_t 860 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp) 861 { 862 ddt_t *ddt; 863 ddt_entry_t dde; 864 865 if (!BP_GET_DEDUP(bp)) 866 return (B_FALSE); 867 868 if (max_class == DDT_CLASS_UNIQUE) 869 return (B_TRUE); 870 871 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)]; 872 873 ddt_key_fill(&dde.dde_key, bp); 874 875 for (enum ddt_type type = 0; type < DDT_TYPES; type++) 876 for (enum ddt_class class = 0; class <= max_class; class++) 877 if (ddt_object_lookup(ddt, type, class, &dde) == 0) 878 return (B_TRUE); 879 880 return (B_FALSE); 881 } 882 883 ddt_entry_t * 884 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp) 885 { 886 ddt_key_t ddk; 887 ddt_entry_t *dde; 888 889 ddt_key_fill(&ddk, bp); 890 891 dde = ddt_alloc(&ddk); 892 893 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 894 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { 895 /* 896 * We can only do repair if there are multiple copies 897 * of the block. For anything in the UNIQUE class, 898 * there's definitely only one copy, so don't even try. 899 */ 900 if (class != DDT_CLASS_UNIQUE && 901 ddt_object_lookup(ddt, type, class, dde) == 0) 902 return (dde); 903 } 904 } 905 906 bzero(dde->dde_phys, sizeof (dde->dde_phys)); 907 908 return (dde); 909 } 910 911 void 912 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde) 913 { 914 avl_index_t where; 915 916 ddt_enter(ddt); 917 918 if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) && 919 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL) 920 avl_insert(&ddt->ddt_repair_tree, dde, where); 921 else 922 ddt_free(dde); 923 924 ddt_exit(ddt); 925 } 926 927 static void 928 ddt_repair_entry_done(zio_t *zio) 929 { 930 ddt_entry_t *rdde = zio->io_private; 931 932 ddt_free(rdde); 933 } 934 935 static void 936 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio) 937 { 938 ddt_phys_t *ddp = dde->dde_phys; 939 ddt_phys_t *rddp = rdde->dde_phys; 940 ddt_key_t *ddk = &dde->dde_key; 941 ddt_key_t *rddk = &rdde->dde_key; 942 zio_t *zio; 943 blkptr_t blk; 944 945 zio = zio_null(rio, rio->io_spa, NULL, 946 ddt_repair_entry_done, rdde, rio->io_flags); 947 948 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) { 949 if (ddp->ddp_phys_birth == 0 || 950 ddp->ddp_phys_birth != rddp->ddp_phys_birth || 951 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva))) 952 continue; 953 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 954 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk, 955 rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL, 956 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL)); 957 } 958 959 zio_nowait(zio); 960 } 961 962 static void 963 ddt_repair_table(ddt_t *ddt, zio_t *rio) 964 { 965 spa_t *spa = ddt->ddt_spa; 966 ddt_entry_t *dde, *rdde_next, *rdde; 967 avl_tree_t *t = &ddt->ddt_repair_tree; 968 blkptr_t blk; 969 970 if (spa_sync_pass(spa) > 1) 971 return; 972 973 ddt_enter(ddt); 974 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) { 975 rdde_next = AVL_NEXT(t, rdde); 976 avl_remove(&ddt->ddt_repair_tree, rdde); 977 ddt_exit(ddt); 978 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk); 979 dde = ddt_repair_start(ddt, &blk); 980 ddt_repair_entry(ddt, dde, rdde, rio); 981 ddt_repair_done(ddt, dde); 982 ddt_enter(ddt); 983 } 984 ddt_exit(ddt); 985 } 986 987 static void 988 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg) 989 { 990 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool; 991 ddt_phys_t *ddp = dde->dde_phys; 992 ddt_key_t *ddk = &dde->dde_key; 993 enum ddt_type otype = dde->dde_type; 994 enum ddt_type ntype = DDT_TYPE_CURRENT; 995 enum ddt_class oclass = dde->dde_class; 996 enum ddt_class nclass; 997 uint64_t total_refcnt = 0; 998 999 ASSERT(dde->dde_loaded); 1000 ASSERT(!dde->dde_loading); 1001 1002 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 1003 ASSERT(dde->dde_lead_zio[p] == NULL); 1004 ASSERT((int64_t)ddp->ddp_refcnt >= 0); 1005 if (ddp->ddp_phys_birth == 0) { 1006 ASSERT(ddp->ddp_refcnt == 0); 1007 continue; 1008 } 1009 if (p == DDT_PHYS_DITTO) { 1010 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0) 1011 ddt_phys_free(ddt, ddk, ddp, txg); 1012 continue; 1013 } 1014 if (ddp->ddp_refcnt == 0) 1015 ddt_phys_free(ddt, ddk, ddp, txg); 1016 total_refcnt += ddp->ddp_refcnt; 1017 } 1018 1019 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0) 1020 nclass = DDT_CLASS_DITTO; 1021 else if (total_refcnt > 1) 1022 nclass = DDT_CLASS_DUPLICATE; 1023 else 1024 nclass = DDT_CLASS_UNIQUE; 1025 1026 if (otype != DDT_TYPES && 1027 (otype != ntype || oclass != nclass || total_refcnt == 0)) { 1028 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0); 1029 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT); 1030 } 1031 1032 if (total_refcnt != 0) { 1033 dde->dde_type = ntype; 1034 dde->dde_class = nclass; 1035 ddt_stat_update(ddt, dde, 0); 1036 if (!ddt_object_exists(ddt, ntype, nclass)) 1037 ddt_object_create(ddt, ntype, nclass, tx); 1038 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0); 1039 1040 /* 1041 * If the class changes, the order that we scan this bp 1042 * changes. If it decreases, we could miss it, so 1043 * scan it right now. (This covers both class changing 1044 * while we are doing ddt_walk(), and when we are 1045 * traversing.) 1046 */ 1047 if (nclass < oclass) { 1048 dsl_scan_ddt_entry(dp->dp_scan, 1049 ddt->ddt_checksum, dde, tx); 1050 } 1051 } 1052 } 1053 1054 static void 1055 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg) 1056 { 1057 spa_t *spa = ddt->ddt_spa; 1058 ddt_entry_t *dde; 1059 void *cookie = NULL; 1060 1061 if (avl_numnodes(&ddt->ddt_tree) == 0) 1062 return; 1063 1064 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP); 1065 1066 if (spa->spa_ddt_stat_object == 0) { 1067 spa->spa_ddt_stat_object = zap_create(ddt->ddt_os, 1068 DMU_OT_DDT_STATS, DMU_OT_NONE, 0, tx); 1069 VERIFY(zap_add(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, 1070 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1, 1071 &spa->spa_ddt_stat_object, tx) == 0); 1072 } 1073 1074 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) { 1075 ddt_sync_entry(ddt, dde, tx, txg); 1076 ddt_free(dde); 1077 } 1078 1079 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 1080 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { 1081 if (!ddt_object_exists(ddt, type, class)) 1082 continue; 1083 ddt_object_sync(ddt, type, class, tx); 1084 if (ddt_object_count(ddt, type, class) == 0) 1085 ddt_object_destroy(ddt, type, class, tx); 1086 } 1087 } 1088 1089 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache, 1090 sizeof (ddt->ddt_histogram)); 1091 } 1092 1093 void 1094 ddt_sync(spa_t *spa, uint64_t txg) 1095 { 1096 dmu_tx_t *tx; 1097 zio_t *rio = zio_root(spa, NULL, NULL, 1098 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1099 1100 ASSERT(spa_syncing_txg(spa) == txg); 1101 1102 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1103 1104 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 1105 ddt_t *ddt = spa->spa_ddt[c]; 1106 if (ddt == NULL) 1107 continue; 1108 ddt_sync_table(ddt, tx, txg); 1109 ddt_repair_table(ddt, rio); 1110 } 1111 1112 (void) zio_wait(rio); 1113 1114 dmu_tx_commit(tx); 1115 } 1116 1117 int 1118 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde) 1119 { 1120 do { 1121 do { 1122 do { 1123 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum]; 1124 int error = ENOENT; 1125 if (ddt_object_exists(ddt, ddb->ddb_type, 1126 ddb->ddb_class)) { 1127 error = ddt_object_walk(ddt, 1128 ddb->ddb_type, ddb->ddb_class, 1129 &ddb->ddb_cursor, dde); 1130 } 1131 dde->dde_type = ddb->ddb_type; 1132 dde->dde_class = ddb->ddb_class; 1133 if (error == 0) 1134 return (0); 1135 if (error != ENOENT) 1136 return (error); 1137 ddb->ddb_cursor = 0; 1138 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS); 1139 ddb->ddb_checksum = 0; 1140 } while (++ddb->ddb_type < DDT_TYPES); 1141 ddb->ddb_type = 0; 1142 } while (++ddb->ddb_class < DDT_CLASSES); 1143 1144 return (ENOENT); 1145 } 1146