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         }
 465 }
 466 
 467 void
 468 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
 469 {
 470         for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
 471                 ddt_t *ddt = spa->spa_ddt[c];
 472                 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
 473                         for (enum ddt_class class = 0; class < DDT_CLASSES;
 474                             class++) {
 475                                 ddt_histogram_add(ddh,
 476                                     &ddt->ddt_histogram_cache[type][class]);
 477                         }
 478                 }
 479         }
 480 }
 481 
 482 void
 483 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
 484 {
 485         ddt_histogram_t *ddh_total;
 486 
 487         ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
 488         ddt_get_dedup_histogram(spa, ddh_total);
 489         ddt_histogram_stat(dds_total, ddh_total);
 490         kmem_free(ddh_total, sizeof (ddt_histogram_t));
 491 }
 492 
 493 uint64_t
 494 ddt_get_dedup_dspace(spa_t *spa)
 495 {
 496         ddt_stat_t dds_total = { 0 };
 497 
 498         ddt_get_dedup_stats(spa, &dds_total);
 499         return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
 500 }
 501 
 502 uint64_t
 503 ddt_get_pool_dedup_ratio(spa_t *spa)
 504 {
 505         ddt_stat_t dds_total = { 0 };
 506 
 507         ddt_get_dedup_stats(spa, &dds_total);
 508         if (dds_total.dds_dsize == 0)
 509                 return (100);
 510 
 511         return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
 512 }
 513 
 514 int
 515 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
 516 {
 517         spa_t *spa = ddt->ddt_spa;
 518         uint64_t total_refcnt = 0;
 519         uint64_t ditto = spa->spa_dedup_ditto;
 520         int total_copies = 0;
 521         int desired_copies = 0;
 522 
 523         for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
 524                 ddt_phys_t *ddp = &dde->dde_phys[p];
 525                 zio_t *zio = dde->dde_lead_zio[p];
 526                 uint64_t refcnt = ddp->ddp_refcnt;   /* committed refs */
 527                 if (zio != NULL)
 528                         refcnt += zio->io_parent_count;      /* pending refs */
 529                 if (ddp == ddp_willref)
 530                         refcnt++;                       /* caller's ref */
 531                 if (refcnt != 0) {
 532                         total_refcnt += refcnt;
 533                         total_copies += p;
 534                 }
 535         }
 536 
 537         if (ditto == 0 || ditto > UINT32_MAX)
 538                 ditto = UINT32_MAX;
 539 
 540         if (total_refcnt >= 1)
 541                 desired_copies++;
 542         if (total_refcnt >= ditto)
 543                 desired_copies++;
 544         if (total_refcnt >= ditto * ditto)
 545                 desired_copies++;
 546 
 547         return (MAX(desired_copies, total_copies) - total_copies);
 548 }
 549 
 550 int
 551 ddt_ditto_copies_present(ddt_entry_t *dde)
 552 {
 553         ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
 554         dva_t *dva = ddp->ddp_dva;
 555         int copies = 0 - DVA_GET_GANG(dva);
 556 
 557         for (int d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
 558                 if (DVA_IS_VALID(dva))
 559                         copies++;
 560 
 561         ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
 562 
 563         return (copies);
 564 }
 565 
 566 size_t
 567 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
 568 {
 569         uchar_t *version = dst++;
 570         int cpfunc = ZIO_COMPRESS_ZLE;
 571         zio_compress_info_t *ci = &zio_compress_table[cpfunc];
 572         size_t c_len;
 573 
 574         ASSERT(d_len >= s_len + 1);  /* no compression plus version byte */
 575 
 576         c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
 577 
 578         if (c_len == s_len) {
 579                 cpfunc = ZIO_COMPRESS_OFF;
 580                 bcopy(src, dst, s_len);
 581         }
 582 
 583         *version = (ZFS_HOST_BYTEORDER & DDT_COMPRESS_BYTEORDER_MASK) | cpfunc;
 584 
 585         return (c_len + 1);
 586 }
 587 
 588 void
 589 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
 590 {
 591         uchar_t version = *src++;
 592         int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
 593         zio_compress_info_t *ci = &zio_compress_table[cpfunc];
 594 
 595         if (ci->ci_decompress != NULL)
 596                 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
 597         else
 598                 bcopy(src, dst, d_len);
 599 
 600         if ((version ^ ZFS_HOST_BYTEORDER) & DDT_COMPRESS_BYTEORDER_MASK)
 601                 byteswap_uint64_array(dst, d_len);
 602 }
 603 
 604 ddt_t *
 605 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
 606 {
 607         return (spa->spa_ddt[c]);
 608 }
 609 
 610 ddt_t *
 611 ddt_select(spa_t *spa, const blkptr_t *bp)
 612 {
 613         return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
 614 }
 615 
 616 void
 617 ddt_enter(ddt_t *ddt)
 618 {
 619         mutex_enter(&ddt->ddt_lock);
 620 }
 621 
 622 void
 623 ddt_exit(ddt_t *ddt)
 624 {
 625         mutex_exit(&ddt->ddt_lock);
 626 }
 627 
 628 static ddt_entry_t *
 629 ddt_alloc(const ddt_key_t *ddk)
 630 {
 631         ddt_entry_t *dde;
 632 
 633         dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP);
 634         cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
 635 
 636         dde->dde_key = *ddk;
 637 
 638         return (dde);
 639 }
 640 
 641 static void
 642 ddt_free(ddt_entry_t *dde)
 643 {
 644         ASSERT(!dde->dde_loading);
 645 
 646         for (int p = 0; p < DDT_PHYS_TYPES; p++)
 647                 ASSERT(dde->dde_lead_zio[p] == NULL);
 648 
 649         if (dde->dde_repair_data != NULL)
 650                 zio_buf_free(dde->dde_repair_data,
 651                     DDK_GET_PSIZE(&dde->dde_key));
 652 
 653         cv_destroy(&dde->dde_cv);
 654         kmem_free(dde, sizeof (*dde));
 655 }
 656 
 657 void
 658 ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
 659 {
 660         ASSERT(MUTEX_HELD(&ddt->ddt_lock));
 661 
 662         avl_remove(&ddt->ddt_tree, dde);
 663         ddt_free(dde);
 664 }
 665 
 666 ddt_entry_t *
 667 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
 668 {
 669         ddt_entry_t *dde, dde_search;
 670         enum ddt_type type;
 671         enum ddt_class class;
 672         avl_index_t where;
 673         int error;
 674 
 675         ASSERT(MUTEX_HELD(&ddt->ddt_lock));
 676 
 677         ddt_key_fill(&dde_search.dde_key, bp);
 678 
 679         dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
 680         if (dde == NULL) {
 681                 if (!add)
 682                         return (NULL);
 683                 dde = ddt_alloc(&dde_search.dde_key);
 684                 avl_insert(&ddt->ddt_tree, dde, where);
 685         }
 686 
 687         while (dde->dde_loading)
 688                 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
 689 
 690         if (dde->dde_loaded)
 691                 return (dde);
 692 
 693         dde->dde_loading = B_TRUE;
 694 
 695         ddt_exit(ddt);
 696 
 697         error = ENOENT;
 698 
 699         for (type = 0; type < DDT_TYPES; type++) {
 700                 for (class = 0; class < DDT_CLASSES; class++) {
 701                         error = ddt_object_lookup(ddt, type, class, dde);
 702                         if (error != ENOENT)
 703                                 break;
 704                 }
 705                 if (error != ENOENT)
 706                         break;
 707         }
 708 
 709         ASSERT(error == 0 || error == ENOENT);
 710 
 711         ddt_enter(ddt);
 712 
 713         ASSERT(dde->dde_loaded == B_FALSE);
 714         ASSERT(dde->dde_loading == B_TRUE);
 715 
 716         dde->dde_type = type;        /* will be DDT_TYPES if no entry found */
 717         dde->dde_class = class;      /* will be DDT_CLASSES if no entry found */
 718         dde->dde_loaded = B_TRUE;
 719         dde->dde_loading = B_FALSE;
 720 
 721         if (error == 0)
 722                 ddt_stat_update(ddt, dde, -1ULL);
 723 
 724         cv_broadcast(&dde->dde_cv);
 725 
 726         return (dde);
 727 }
 728 
 729 void
 730 ddt_prefetch(spa_t *spa, const blkptr_t *bp)
 731 {
 732         ddt_t *ddt;
 733         ddt_entry_t dde;
 734 
 735         if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
 736                 return;
 737 
 738         /*
 739          * We only remove the DDT once all tables are empty and only
 740          * prefetch dedup blocks when there are entries in the DDT.
 741          * Thus no locking is required as the DDT can't disappear on us.
 742          */
 743         ddt = ddt_select(spa, bp);
 744         ddt_key_fill(&dde.dde_key, bp);
 745 
 746         for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
 747                 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
 748                         ddt_object_prefetch(ddt, type, class, &dde);
 749                 }
 750         }
 751 }
 752 
 753 int
 754 ddt_entry_compare(const void *x1, const void *x2)
 755 {
 756         const ddt_entry_t *dde1 = x1;
 757         const ddt_entry_t *dde2 = x2;
 758         const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
 759         const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
 760 
 761         for (int i = 0; i < DDT_KEY_WORDS; i++) {
 762                 if (u1[i] < u2[i])
 763                         return (-1);
 764                 if (u1[i] > u2[i])
 765                         return (1);
 766         }
 767 
 768         return (0);
 769 }
 770 
 771 static ddt_t *
 772 ddt_table_alloc(spa_t *spa, enum zio_checksum c)
 773 {
 774         ddt_t *ddt;
 775 
 776         ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP);
 777 
 778         mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
 779         avl_create(&ddt->ddt_tree, ddt_entry_compare,
 780             sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
 781         avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
 782             sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
 783         ddt->ddt_checksum = c;
 784         ddt->ddt_spa = spa;
 785         ddt->ddt_os = spa->spa_meta_objset;
 786 
 787         return (ddt);
 788 }
 789 
 790 static void
 791 ddt_table_free(ddt_t *ddt)
 792 {
 793         ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
 794         ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
 795         avl_destroy(&ddt->ddt_tree);
 796         avl_destroy(&ddt->ddt_repair_tree);
 797         mutex_destroy(&ddt->ddt_lock);
 798         kmem_free(ddt, sizeof (*ddt));
 799 }
 800 
 801 void
 802 ddt_create(spa_t *spa)
 803 {
 804         spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
 805 
 806         for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
 807                 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
 808 }
 809 
 810 int
 811 ddt_load(spa_t *spa)
 812 {
 813         int error;
 814 
 815         ddt_create(spa);
 816 
 817         error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
 818             DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
 819             &spa->spa_ddt_stat_object);
 820 
 821         if (error)
 822                 return (error == ENOENT ? 0 : error);
 823 
 824         for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
 825                 ddt_t *ddt = spa->spa_ddt[c];
 826                 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
 827                         for (enum ddt_class class = 0; class < DDT_CLASSES;
 828                             class++) {
 829                                 error = ddt_object_load(ddt, type, class);
 830                                 if (error != 0 && error != ENOENT)
 831                                         return (error);
 832                         }
 833                 }
 834 
 835                 /*
 836                  * Seed the cached histograms.
 837                  */
 838                 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
 839                     sizeof (ddt->ddt_histogram));
 840         }
 841 
 842         return (0);
 843 }
 844 
 845 void
 846 ddt_unload(spa_t *spa)
 847 {
 848         for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
 849                 if (spa->spa_ddt[c]) {
 850                         ddt_table_free(spa->spa_ddt[c]);
 851                         spa->spa_ddt[c] = NULL;
 852                 }
 853         }
 854 }
 855 
 856 boolean_t
 857 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
 858 {
 859         ddt_t *ddt;
 860         ddt_entry_t dde;
 861 
 862         if (!BP_GET_DEDUP(bp))
 863                 return (B_FALSE);
 864 
 865         if (max_class == DDT_CLASS_UNIQUE)
 866                 return (B_TRUE);
 867 
 868         ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
 869 
 870         ddt_key_fill(&dde.dde_key, bp);
 871 
 872         for (enum ddt_type type = 0; type < DDT_TYPES; type++)
 873                 for (enum ddt_class class = 0; class <= max_class; class++)
 874                         if (ddt_object_lookup(ddt, type, class, &dde) == 0)
 875                                 return (B_TRUE);
 876 
 877         return (B_FALSE);
 878 }
 879 
 880 ddt_entry_t *
 881 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
 882 {
 883         ddt_key_t ddk;
 884         ddt_entry_t *dde;
 885 
 886         ddt_key_fill(&ddk, bp);
 887 
 888         dde = ddt_alloc(&ddk);
 889 
 890         for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
 891                 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
 892                         /*
 893                          * We can only do repair if there are multiple copies
 894                          * of the block.  For anything in the UNIQUE class,
 895                          * there's definitely only one copy, so don't even try.
 896                          */
 897                         if (class != DDT_CLASS_UNIQUE &&
 898                             ddt_object_lookup(ddt, type, class, dde) == 0)
 899                                 return (dde);
 900                 }
 901         }
 902 
 903         bzero(dde->dde_phys, sizeof (dde->dde_phys));
 904 
 905         return (dde);
 906 }
 907 
 908 void
 909 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
 910 {
 911         avl_index_t where;
 912 
 913         ddt_enter(ddt);
 914 
 915         if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) &&
 916             avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
 917                 avl_insert(&ddt->ddt_repair_tree, dde, where);
 918         else
 919                 ddt_free(dde);
 920 
 921         ddt_exit(ddt);
 922 }
 923 
 924 static void
 925 ddt_repair_entry_done(zio_t *zio)
 926 {
 927         ddt_entry_t *rdde = zio->io_private;
 928 
 929         ddt_free(rdde);
 930 }
 931 
 932 static void
 933 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
 934 {
 935         ddt_phys_t *ddp = dde->dde_phys;
 936         ddt_phys_t *rddp = rdde->dde_phys;
 937         ddt_key_t *ddk = &dde->dde_key;
 938         ddt_key_t *rddk = &rdde->dde_key;
 939         zio_t *zio;
 940         blkptr_t blk;
 941 
 942         zio = zio_null(rio, rio->io_spa, NULL,
 943             ddt_repair_entry_done, rdde, rio->io_flags);
 944 
 945         for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
 946                 if (ddp->ddp_phys_birth == 0 ||
 947                     ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
 948                     bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
 949                         continue;
 950                 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
 951                 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
 952                     rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL,
 953                     ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
 954         }
 955 
 956         zio_nowait(zio);
 957 }
 958 
 959 static void
 960 ddt_repair_table(ddt_t *ddt, zio_t *rio)
 961 {
 962         spa_t *spa = ddt->ddt_spa;
 963         ddt_entry_t *dde, *rdde_next, *rdde;
 964         avl_tree_t *t = &ddt->ddt_repair_tree;
 965         blkptr_t blk;
 966 
 967         if (spa_sync_pass(spa) > 1)
 968                 return;
 969 
 970         ddt_enter(ddt);
 971         for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
 972                 rdde_next = AVL_NEXT(t, rdde);
 973                 avl_remove(&ddt->ddt_repair_tree, rdde);
 974                 ddt_exit(ddt);
 975                 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
 976                 dde = ddt_repair_start(ddt, &blk);
 977                 ddt_repair_entry(ddt, dde, rdde, rio);
 978                 ddt_repair_done(ddt, dde);
 979                 ddt_enter(ddt);
 980         }
 981         ddt_exit(ddt);
 982 }
 983 
 984 static void
 985 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
 986 {
 987         dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
 988         ddt_phys_t *ddp = dde->dde_phys;
 989         ddt_key_t *ddk = &dde->dde_key;
 990         enum ddt_type otype = dde->dde_type;
 991         enum ddt_type ntype = DDT_TYPE_CURRENT;
 992         enum ddt_class oclass = dde->dde_class;
 993         enum ddt_class nclass;
 994         uint64_t total_refcnt = 0;
 995 
 996         ASSERT(dde->dde_loaded);
 997         ASSERT(!dde->dde_loading);
 998 
 999         for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1000                 ASSERT(dde->dde_lead_zio[p] == NULL);
1001                 ASSERT((int64_t)ddp->ddp_refcnt >= 0);
1002                 if (ddp->ddp_phys_birth == 0) {
1003                         ASSERT(ddp->ddp_refcnt == 0);
1004                         continue;
1005                 }
1006                 if (p == DDT_PHYS_DITTO) {
1007                         if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1008                                 ddt_phys_free(ddt, ddk, ddp, txg);
1009                         continue;
1010                 }
1011                 if (ddp->ddp_refcnt == 0)
1012                         ddt_phys_free(ddt, ddk, ddp, txg);
1013                 total_refcnt += ddp->ddp_refcnt;
1014         }
1015 
1016         if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1017                 nclass = DDT_CLASS_DITTO;
1018         else if (total_refcnt > 1)
1019                 nclass = DDT_CLASS_DUPLICATE;
1020         else
1021                 nclass = DDT_CLASS_UNIQUE;
1022 
1023         if (otype != DDT_TYPES &&
1024             (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1025                 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1026                 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1027         }
1028 
1029         if (total_refcnt != 0) {
1030                 dde->dde_type = ntype;
1031                 dde->dde_class = nclass;
1032                 ddt_stat_update(ddt, dde, 0);
1033                 if (!ddt_object_exists(ddt, ntype, nclass))
1034                         ddt_object_create(ddt, ntype, nclass, tx);
1035                 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1036 
1037                 /*
1038                  * If the class changes, the order that we scan this bp
1039                  * changes.  If it decreases, we could miss it, so
1040                  * scan it right now.  (This covers both class changing
1041                  * while we are doing ddt_walk(), and when we are
1042                  * traversing.)
1043                  */
1044                 if (nclass < oclass) {
1045                         dsl_scan_ddt_entry(dp->dp_scan,
1046                             ddt->ddt_checksum, dde, tx);
1047                 }
1048         }
1049 }
1050 
1051 static void
1052 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1053 {
1054         spa_t *spa = ddt->ddt_spa;
1055         ddt_entry_t *dde;
1056         void *cookie = NULL;
1057 
1058         if (avl_numnodes(&ddt->ddt_tree) == 0)
1059                 return;
1060 
1061         ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1062 
1063         if (spa->spa_ddt_stat_object == 0) {
1064                 spa->spa_ddt_stat_object = zap_create(ddt->ddt_os,
1065                     DMU_OT_DDT_STATS, DMU_OT_NONE, 0, tx);
1066                 VERIFY(zap_add(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT,
1067                     DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
1068                     &spa->spa_ddt_stat_object, tx) == 0);
1069         }
1070 
1071         while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1072                 ddt_sync_entry(ddt, dde, tx, txg);
1073                 ddt_free(dde);
1074         }
1075 
1076         for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1077                 uint64_t count = 0;
1078                 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1079                         if (ddt_object_exists(ddt, type, class)) {
1080                                 ddt_object_sync(ddt, type, class, tx);
1081                                 count += ddt_object_count(ddt, type, class);
1082                         }
1083                 }
1084                 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1085                         if (count == 0 && ddt_object_exists(ddt, type, class))
1086                                 ddt_object_destroy(ddt, type, class, tx);
1087                 }
1088         }
1089 
1090         bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1091             sizeof (ddt->ddt_histogram));
1092 }
1093 
1094 void
1095 ddt_sync(spa_t *spa, uint64_t txg)
1096 {
1097         dmu_tx_t *tx;
1098         zio_t *rio = zio_root(spa, NULL, NULL,
1099             ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1100 
1101         ASSERT(spa_syncing_txg(spa) == txg);
1102 
1103         tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1104 
1105         for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1106                 ddt_t *ddt = spa->spa_ddt[c];
1107                 if (ddt == NULL)
1108                         continue;
1109                 ddt_sync_table(ddt, tx, txg);
1110                 ddt_repair_table(ddt, rio);
1111         }
1112 
1113         (void) zio_wait(rio);
1114 
1115         dmu_tx_commit(tx);
1116 }
1117 
1118 int
1119 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1120 {
1121         do {
1122                 do {
1123                         do {
1124                                 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1125                                 int error = ENOENT;
1126                                 if (ddt_object_exists(ddt, ddb->ddb_type,
1127                                     ddb->ddb_class)) {
1128                                         error = ddt_object_walk(ddt,
1129                                             ddb->ddb_type, ddb->ddb_class,
1130                                             &ddb->ddb_cursor, dde);
1131                                 }
1132                                 dde->dde_type = ddb->ddb_type;
1133                                 dde->dde_class = ddb->ddb_class;
1134                                 if (error == 0)
1135                                         return (0);
1136                                 if (error != ENOENT)
1137                                         return (error);
1138                                 ddb->ddb_cursor = 0;
1139                         } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1140                         ddb->ddb_checksum = 0;
1141                 } while (++ddb->ddb_type < DDT_TYPES);
1142                 ddb->ddb_type = 0;
1143         } while (++ddb->ddb_class < DDT_CLASSES);
1144 
1145         return (ENOENT);
1146 }