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