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