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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2013 by Delphix. All rights reserved.
  24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25  */
  26 
  27 #include <sys/dmu_objset.h>
  28 #include <sys/dsl_dataset.h>
  29 #include <sys/dsl_dir.h>
  30 #include <sys/dsl_prop.h>
  31 #include <sys/dsl_synctask.h>
  32 #include <sys/dmu_traverse.h>
  33 #include <sys/dmu_impl.h>
  34 #include <sys/dmu_tx.h>
  35 #include <sys/arc.h>
  36 #include <sys/zio.h>
  37 #include <sys/zap.h>
  38 #include <sys/zfeature.h>
  39 #include <sys/unique.h>
  40 #include <sys/zfs_context.h>
  41 #include <sys/zfs_ioctl.h>
  42 #include <sys/spa.h>
  43 #include <sys/zfs_znode.h>
  44 #include <sys/zfs_onexit.h>
  45 #include <sys/zvol.h>
  46 #include <sys/dsl_scan.h>
  47 #include <sys/dsl_deadlist.h>
  48 #include <sys/dsl_destroy.h>
  49 #include <sys/dsl_userhold.h>
  50 
  51 #define SWITCH64(x, y) \
  52         { \
  53                 uint64_t __tmp = (x); \
  54                 (x) = (y); \
  55                 (y) = __tmp; \
  56         }
  57 
  58 #define DS_REF_MAX      (1ULL << 62)
  59 
  60 #define DSL_DEADLIST_BLOCKSIZE  SPA_MAXBLOCKSIZE
  61 
  62 /*
  63  * Figure out how much of this delta should be propogated to the dsl_dir
  64  * layer.  If there's a refreservation, that space has already been
  65  * partially accounted for in our ancestors.
  66  */
  67 static int64_t
  68 parent_delta(dsl_dataset_t *ds, int64_t delta)
  69 {
  70         uint64_t old_bytes, new_bytes;
  71 
  72         if (ds->ds_reserved == 0)
  73                 return (delta);
  74 
  75         old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
  76         new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
  77 
  78         ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
  79         return (new_bytes - old_bytes);
  80 }
  81 
  82 void
  83 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
  84 {
  85         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
  86         int compressed = BP_GET_PSIZE(bp);
  87         int uncompressed = BP_GET_UCSIZE(bp);
  88         int64_t delta;
  89 
  90         dprintf_bp(bp, "ds=%p", ds);
  91 
  92         ASSERT(dmu_tx_is_syncing(tx));
  93         /* It could have been compressed away to nothing */
  94         if (BP_IS_HOLE(bp))
  95                 return;
  96         ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
  97         ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
  98         if (ds == NULL) {
  99                 dsl_pool_mos_diduse_space(tx->tx_pool,
 100                     used, compressed, uncompressed);
 101                 return;
 102         }
 103         dmu_buf_will_dirty(ds->ds_dbuf, tx);
 104 
 105         mutex_enter(&ds->ds_dir->dd_lock);
 106         mutex_enter(&ds->ds_lock);
 107         delta = parent_delta(ds, used);
 108         ds->ds_phys->ds_referenced_bytes += used;
 109         ds->ds_phys->ds_compressed_bytes += compressed;
 110         ds->ds_phys->ds_uncompressed_bytes += uncompressed;
 111         ds->ds_phys->ds_unique_bytes += used;
 112         mutex_exit(&ds->ds_lock);
 113         dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
 114             compressed, uncompressed, tx);
 115         dsl_dir_transfer_space(ds->ds_dir, used - delta,
 116             DD_USED_REFRSRV, DD_USED_HEAD, tx);
 117         mutex_exit(&ds->ds_dir->dd_lock);
 118 }
 119 
 120 int
 121 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
 122     boolean_t async)
 123 {
 124         if (BP_IS_HOLE(bp))
 125                 return (0);
 126 
 127         ASSERT(dmu_tx_is_syncing(tx));
 128         ASSERT(bp->blk_birth <= tx->tx_txg);
 129 
 130         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
 131         int compressed = BP_GET_PSIZE(bp);
 132         int uncompressed = BP_GET_UCSIZE(bp);
 133 
 134         ASSERT(used > 0);
 135         if (ds == NULL) {
 136                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
 137                 dsl_pool_mos_diduse_space(tx->tx_pool,
 138                     -used, -compressed, -uncompressed);
 139                 return (used);
 140         }
 141         ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
 142 
 143         ASSERT(!dsl_dataset_is_snapshot(ds));
 144         dmu_buf_will_dirty(ds->ds_dbuf, tx);
 145 
 146         if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
 147                 int64_t delta;
 148 
 149                 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
 150                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
 151 
 152                 mutex_enter(&ds->ds_dir->dd_lock);
 153                 mutex_enter(&ds->ds_lock);
 154                 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
 155                     !DS_UNIQUE_IS_ACCURATE(ds));
 156                 delta = parent_delta(ds, -used);
 157                 ds->ds_phys->ds_unique_bytes -= used;
 158                 mutex_exit(&ds->ds_lock);
 159                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
 160                     delta, -compressed, -uncompressed, tx);
 161                 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
 162                     DD_USED_REFRSRV, DD_USED_HEAD, tx);
 163                 mutex_exit(&ds->ds_dir->dd_lock);
 164         } else {
 165                 dprintf_bp(bp, "putting on dead list: %s", "");
 166                 if (async) {
 167                         /*
 168                          * We are here as part of zio's write done callback,
 169                          * which means we're a zio interrupt thread.  We can't
 170                          * call dsl_deadlist_insert() now because it may block
 171                          * waiting for I/O.  Instead, put bp on the deferred
 172                          * queue and let dsl_pool_sync() finish the job.
 173                          */
 174                         bplist_append(&ds->ds_pending_deadlist, bp);
 175                 } else {
 176                         dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
 177                 }
 178                 ASSERT3U(ds->ds_prev->ds_object, ==,
 179                     ds->ds_phys->ds_prev_snap_obj);
 180                 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
 181                 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
 182                 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
 183                     ds->ds_object && bp->blk_birth >
 184                     ds->ds_prev->ds_phys->ds_prev_snap_txg) {
 185                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
 186                         mutex_enter(&ds->ds_prev->ds_lock);
 187                         ds->ds_prev->ds_phys->ds_unique_bytes += used;
 188                         mutex_exit(&ds->ds_prev->ds_lock);
 189                 }
 190                 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
 191                         dsl_dir_transfer_space(ds->ds_dir, used,
 192                             DD_USED_HEAD, DD_USED_SNAP, tx);
 193                 }
 194         }
 195         mutex_enter(&ds->ds_lock);
 196         ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
 197         ds->ds_phys->ds_referenced_bytes -= used;
 198         ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
 199         ds->ds_phys->ds_compressed_bytes -= compressed;
 200         ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
 201         ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
 202         mutex_exit(&ds->ds_lock);
 203 
 204         return (used);
 205 }
 206 
 207 uint64_t
 208 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
 209 {
 210         uint64_t trysnap = 0;
 211 
 212         if (ds == NULL)
 213                 return (0);
 214         /*
 215          * The snapshot creation could fail, but that would cause an
 216          * incorrect FALSE return, which would only result in an
 217          * overestimation of the amount of space that an operation would
 218          * consume, which is OK.
 219          *
 220          * There's also a small window where we could miss a pending
 221          * snapshot, because we could set the sync task in the quiescing
 222          * phase.  So this should only be used as a guess.
 223          */
 224         if (ds->ds_trysnap_txg >
 225             spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
 226                 trysnap = ds->ds_trysnap_txg;
 227         return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
 228 }
 229 
 230 boolean_t
 231 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
 232     uint64_t blk_birth)
 233 {
 234         if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
 235                 return (B_FALSE);
 236 
 237         ddt_prefetch(dsl_dataset_get_spa(ds), bp);
 238 
 239         return (B_TRUE);
 240 }
 241 
 242 /* ARGSUSED */
 243 static void
 244 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
 245 {
 246         dsl_dataset_t *ds = dsv;
 247 
 248         ASSERT(ds->ds_owner == NULL);
 249 
 250         unique_remove(ds->ds_fsid_guid);
 251 
 252         if (ds->ds_objset != NULL)
 253                 dmu_objset_evict(ds->ds_objset);
 254 
 255         if (ds->ds_prev) {
 256                 dsl_dataset_rele(ds->ds_prev, ds);
 257                 ds->ds_prev = NULL;
 258         }
 259 
 260         bplist_destroy(&ds->ds_pending_deadlist);
 261         if (ds->ds_phys->ds_deadlist_obj != 0)
 262                 dsl_deadlist_close(&ds->ds_deadlist);
 263         if (ds->ds_dir)
 264                 dsl_dir_rele(ds->ds_dir, ds);
 265 
 266         ASSERT(!list_link_active(&ds->ds_synced_link));
 267 
 268         mutex_destroy(&ds->ds_lock);
 269         mutex_destroy(&ds->ds_opening_lock);
 270         refcount_destroy(&ds->ds_longholds);
 271 
 272         kmem_free(ds, sizeof (dsl_dataset_t));
 273 }
 274 
 275 int
 276 dsl_dataset_get_snapname(dsl_dataset_t *ds)
 277 {
 278         dsl_dataset_phys_t *headphys;
 279         int err;
 280         dmu_buf_t *headdbuf;
 281         dsl_pool_t *dp = ds->ds_dir->dd_pool;
 282         objset_t *mos = dp->dp_meta_objset;
 283 
 284         if (ds->ds_snapname[0])
 285                 return (0);
 286         if (ds->ds_phys->ds_next_snap_obj == 0)
 287                 return (0);
 288 
 289         err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
 290             FTAG, &headdbuf);
 291         if (err != 0)
 292                 return (err);
 293         headphys = headdbuf->db_data;
 294         err = zap_value_search(dp->dp_meta_objset,
 295             headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
 296         dmu_buf_rele(headdbuf, FTAG);
 297         return (err);
 298 }
 299 
 300 int
 301 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
 302 {
 303         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 304         uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
 305         matchtype_t mt;
 306         int err;
 307 
 308         if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
 309                 mt = MT_FIRST;
 310         else
 311                 mt = MT_EXACT;
 312 
 313         err = zap_lookup_norm(mos, snapobj, name, 8, 1,
 314             value, mt, NULL, 0, NULL);
 315         if (err == ENOTSUP && mt == MT_FIRST)
 316                 err = zap_lookup(mos, snapobj, name, 8, 1, value);
 317         return (err);
 318 }
 319 
 320 int
 321 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
 322 {
 323         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 324         uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
 325         matchtype_t mt;
 326         int err;
 327 
 328         dsl_dir_snap_cmtime_update(ds->ds_dir);
 329 
 330         if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
 331                 mt = MT_FIRST;
 332         else
 333                 mt = MT_EXACT;
 334 
 335         err = zap_remove_norm(mos, snapobj, name, mt, tx);
 336         if (err == ENOTSUP && mt == MT_FIRST)
 337                 err = zap_remove(mos, snapobj, name, tx);
 338         return (err);
 339 }
 340 
 341 int
 342 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
 343     dsl_dataset_t **dsp)
 344 {
 345         objset_t *mos = dp->dp_meta_objset;
 346         dmu_buf_t *dbuf;
 347         dsl_dataset_t *ds;
 348         int err;
 349         dmu_object_info_t doi;
 350 
 351         ASSERT(dsl_pool_config_held(dp));
 352 
 353         err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
 354         if (err != 0)
 355                 return (err);
 356 
 357         /* Make sure dsobj has the correct object type. */
 358         dmu_object_info_from_db(dbuf, &doi);
 359         if (doi.doi_type != DMU_OT_DSL_DATASET) {
 360                 dmu_buf_rele(dbuf, tag);
 361                 return (SET_ERROR(EINVAL));
 362         }
 363 
 364         ds = dmu_buf_get_user(dbuf);
 365         if (ds == NULL) {
 366                 dsl_dataset_t *winner = NULL;
 367 
 368                 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
 369                 ds->ds_dbuf = dbuf;
 370                 ds->ds_object = dsobj;
 371                 ds->ds_phys = dbuf->db_data;
 372 
 373                 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
 374                 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
 375                 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
 376                 refcount_create(&ds->ds_longholds);
 377 
 378                 bplist_create(&ds->ds_pending_deadlist);
 379                 dsl_deadlist_open(&ds->ds_deadlist,
 380                     mos, ds->ds_phys->ds_deadlist_obj);
 381 
 382                 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
 383                     offsetof(dmu_sendarg_t, dsa_link));
 384 
 385                 if (err == 0) {
 386                         err = dsl_dir_hold_obj(dp,
 387                             ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
 388                 }
 389                 if (err != 0) {
 390                         mutex_destroy(&ds->ds_lock);
 391                         mutex_destroy(&ds->ds_opening_lock);
 392                         refcount_destroy(&ds->ds_longholds);
 393                         bplist_destroy(&ds->ds_pending_deadlist);
 394                         dsl_deadlist_close(&ds->ds_deadlist);
 395                         kmem_free(ds, sizeof (dsl_dataset_t));
 396                         dmu_buf_rele(dbuf, tag);
 397                         return (err);
 398                 }
 399 
 400                 if (!dsl_dataset_is_snapshot(ds)) {
 401                         ds->ds_snapname[0] = '\0';
 402                         if (ds->ds_phys->ds_prev_snap_obj != 0) {
 403                                 err = dsl_dataset_hold_obj(dp,
 404                                     ds->ds_phys->ds_prev_snap_obj,
 405                                     ds, &ds->ds_prev);
 406                         }
 407                 } else {
 408                         if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
 409                                 err = dsl_dataset_get_snapname(ds);
 410                         if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
 411                                 err = zap_count(
 412                                     ds->ds_dir->dd_pool->dp_meta_objset,
 413                                     ds->ds_phys->ds_userrefs_obj,
 414                                     &ds->ds_userrefs);
 415                         }
 416                 }
 417 
 418                 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
 419                         err = dsl_prop_get_int_ds(ds,
 420                             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
 421                             &ds->ds_reserved);
 422                         if (err == 0) {
 423                                 err = dsl_prop_get_int_ds(ds,
 424                                     zfs_prop_to_name(ZFS_PROP_REFQUOTA),
 425                                     &ds->ds_quota);
 426                         }
 427                 } else {
 428                         ds->ds_reserved = ds->ds_quota = 0;
 429                 }
 430 
 431                 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
 432                     &ds->ds_phys, dsl_dataset_evict)) != NULL) {
 433                         bplist_destroy(&ds->ds_pending_deadlist);
 434                         dsl_deadlist_close(&ds->ds_deadlist);
 435                         if (ds->ds_prev)
 436                                 dsl_dataset_rele(ds->ds_prev, ds);
 437                         dsl_dir_rele(ds->ds_dir, ds);
 438                         mutex_destroy(&ds->ds_lock);
 439                         mutex_destroy(&ds->ds_opening_lock);
 440                         refcount_destroy(&ds->ds_longholds);
 441                         kmem_free(ds, sizeof (dsl_dataset_t));
 442                         if (err != 0) {
 443                                 dmu_buf_rele(dbuf, tag);
 444                                 return (err);
 445                         }
 446                         ds = winner;
 447                 } else {
 448                         ds->ds_fsid_guid =
 449                             unique_insert(ds->ds_phys->ds_fsid_guid);
 450                 }
 451         }
 452         ASSERT3P(ds->ds_dbuf, ==, dbuf);
 453         ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
 454         ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
 455             spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
 456             dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
 457         *dsp = ds;
 458         return (0);
 459 }
 460 
 461 int
 462 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
 463     void *tag, dsl_dataset_t **dsp)
 464 {
 465         dsl_dir_t *dd;
 466         const char *snapname;
 467         uint64_t obj;
 468         int err = 0;
 469 
 470         err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
 471         if (err != 0)
 472                 return (err);
 473 
 474         ASSERT(dsl_pool_config_held(dp));
 475         obj = dd->dd_phys->dd_head_dataset_obj;
 476         if (obj != 0)
 477                 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
 478         else
 479                 err = SET_ERROR(ENOENT);
 480 
 481         /* we may be looking for a snapshot */
 482         if (err == 0 && snapname != NULL) {
 483                 dsl_dataset_t *ds;
 484 
 485                 if (*snapname++ != '@') {
 486                         dsl_dataset_rele(*dsp, tag);
 487                         dsl_dir_rele(dd, FTAG);
 488                         return (SET_ERROR(ENOENT));
 489                 }
 490 
 491                 dprintf("looking for snapshot '%s'\n", snapname);
 492                 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
 493                 if (err == 0)
 494                         err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
 495                 dsl_dataset_rele(*dsp, tag);
 496 
 497                 if (err == 0) {
 498                         mutex_enter(&ds->ds_lock);
 499                         if (ds->ds_snapname[0] == 0)
 500                                 (void) strlcpy(ds->ds_snapname, snapname,
 501                                     sizeof (ds->ds_snapname));
 502                         mutex_exit(&ds->ds_lock);
 503                         *dsp = ds;
 504                 }
 505         }
 506 
 507         dsl_dir_rele(dd, FTAG);
 508         return (err);
 509 }
 510 
 511 int
 512 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
 513     void *tag, dsl_dataset_t **dsp)
 514 {
 515         int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
 516         if (err != 0)
 517                 return (err);
 518         if (!dsl_dataset_tryown(*dsp, tag)) {
 519                 dsl_dataset_rele(*dsp, tag);
 520                 *dsp = NULL;
 521                 return (SET_ERROR(EBUSY));
 522         }
 523         return (0);
 524 }
 525 
 526 int
 527 dsl_dataset_own(dsl_pool_t *dp, const char *name,
 528     void *tag, dsl_dataset_t **dsp)
 529 {
 530         int err = dsl_dataset_hold(dp, name, tag, dsp);
 531         if (err != 0)
 532                 return (err);
 533         if (!dsl_dataset_tryown(*dsp, tag)) {
 534                 dsl_dataset_rele(*dsp, tag);
 535                 return (SET_ERROR(EBUSY));
 536         }
 537         return (0);
 538 }
 539 
 540 /*
 541  * See the comment above dsl_pool_hold() for details.  In summary, a long
 542  * hold is used to prevent destruction of a dataset while the pool hold
 543  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
 544  *
 545  * The dataset and pool must be held when this function is called.  After it
 546  * is called, the pool hold may be released while the dataset is still held
 547  * and accessed.
 548  */
 549 void
 550 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
 551 {
 552         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
 553         (void) refcount_add(&ds->ds_longholds, tag);
 554 }
 555 
 556 void
 557 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
 558 {
 559         (void) refcount_remove(&ds->ds_longholds, tag);
 560 }
 561 
 562 /* Return B_TRUE if there are any long holds on this dataset. */
 563 boolean_t
 564 dsl_dataset_long_held(dsl_dataset_t *ds)
 565 {
 566         return (!refcount_is_zero(&ds->ds_longholds));
 567 }
 568 
 569 void
 570 dsl_dataset_name(dsl_dataset_t *ds, char *name)
 571 {
 572         if (ds == NULL) {
 573                 (void) strcpy(name, "mos");
 574         } else {
 575                 dsl_dir_name(ds->ds_dir, name);
 576                 VERIFY0(dsl_dataset_get_snapname(ds));
 577                 if (ds->ds_snapname[0]) {
 578                         (void) strcat(name, "@");
 579                         /*
 580                          * We use a "recursive" mutex so that we
 581                          * can call dprintf_ds() with ds_lock held.
 582                          */
 583                         if (!MUTEX_HELD(&ds->ds_lock)) {
 584                                 mutex_enter(&ds->ds_lock);
 585                                 (void) strcat(name, ds->ds_snapname);
 586                                 mutex_exit(&ds->ds_lock);
 587                         } else {
 588                                 (void) strcat(name, ds->ds_snapname);
 589                         }
 590                 }
 591         }
 592 }
 593 
 594 static int
 595 dsl_dataset_namelen(dsl_dataset_t *ds)
 596 {
 597         int result;
 598 
 599         if (ds == NULL) {
 600                 result = 3;     /* "mos" */
 601         } else {
 602                 result = dsl_dir_namelen(ds->ds_dir);
 603                 VERIFY0(dsl_dataset_get_snapname(ds));
 604                 if (ds->ds_snapname[0]) {
 605                         ++result;       /* adding one for the @-sign */
 606                         if (!MUTEX_HELD(&ds->ds_lock)) {
 607                                 mutex_enter(&ds->ds_lock);
 608                                 result += strlen(ds->ds_snapname);
 609                                 mutex_exit(&ds->ds_lock);
 610                         } else {
 611                                 result += strlen(ds->ds_snapname);
 612                         }
 613                 }
 614         }
 615 
 616         return (result);
 617 }
 618 
 619 void
 620 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
 621 {
 622         dmu_buf_rele(ds->ds_dbuf, tag);
 623 }
 624 
 625 void
 626 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
 627 {
 628         ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
 629 
 630         mutex_enter(&ds->ds_lock);
 631         ds->ds_owner = NULL;
 632         mutex_exit(&ds->ds_lock);
 633         dsl_dataset_long_rele(ds, tag);
 634         if (ds->ds_dbuf != NULL)
 635                 dsl_dataset_rele(ds, tag);
 636         else
 637                 dsl_dataset_evict(NULL, ds);
 638 }
 639 
 640 boolean_t
 641 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
 642 {
 643         boolean_t gotit = FALSE;
 644 
 645         mutex_enter(&ds->ds_lock);
 646         if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
 647                 ds->ds_owner = tag;
 648                 dsl_dataset_long_hold(ds, tag);
 649                 gotit = TRUE;
 650         }
 651         mutex_exit(&ds->ds_lock);
 652         return (gotit);
 653 }
 654 
 655 uint64_t
 656 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
 657     uint64_t flags, dmu_tx_t *tx)
 658 {
 659         dsl_pool_t *dp = dd->dd_pool;
 660         dmu_buf_t *dbuf;
 661         dsl_dataset_phys_t *dsphys;
 662         uint64_t dsobj;
 663         objset_t *mos = dp->dp_meta_objset;
 664 
 665         if (origin == NULL)
 666                 origin = dp->dp_origin_snap;
 667 
 668         ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
 669         ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
 670         ASSERT(dmu_tx_is_syncing(tx));
 671         ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
 672 
 673         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
 674             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
 675         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
 676         dmu_buf_will_dirty(dbuf, tx);
 677         dsphys = dbuf->db_data;
 678         bzero(dsphys, sizeof (dsl_dataset_phys_t));
 679         dsphys->ds_dir_obj = dd->dd_object;
 680         dsphys->ds_flags = flags;
 681         dsphys->ds_fsid_guid = unique_create();
 682         (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
 683             sizeof (dsphys->ds_guid));
 684         dsphys->ds_snapnames_zapobj =
 685             zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
 686             DMU_OT_NONE, 0, tx);
 687         dsphys->ds_creation_time = gethrestime_sec();
 688         dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
 689 
 690         if (origin == NULL) {
 691                 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
 692         } else {
 693                 dsl_dataset_t *ohds; /* head of the origin snapshot */
 694 
 695                 dsphys->ds_prev_snap_obj = origin->ds_object;
 696                 dsphys->ds_prev_snap_txg =
 697                     origin->ds_phys->ds_creation_txg;
 698                 dsphys->ds_referenced_bytes =
 699                     origin->ds_phys->ds_referenced_bytes;
 700                 dsphys->ds_compressed_bytes =
 701                     origin->ds_phys->ds_compressed_bytes;
 702                 dsphys->ds_uncompressed_bytes =
 703                     origin->ds_phys->ds_uncompressed_bytes;
 704                 dsphys->ds_bp = origin->ds_phys->ds_bp;
 705                 dsphys->ds_flags |= origin->ds_phys->ds_flags;
 706 
 707                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
 708                 origin->ds_phys->ds_num_children++;
 709 
 710                 VERIFY0(dsl_dataset_hold_obj(dp,
 711                     origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
 712                 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
 713                     dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
 714                 dsl_dataset_rele(ohds, FTAG);
 715 
 716                 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
 717                         if (origin->ds_phys->ds_next_clones_obj == 0) {
 718                                 origin->ds_phys->ds_next_clones_obj =
 719                                     zap_create(mos,
 720                                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
 721                         }
 722                         VERIFY0(zap_add_int(mos,
 723                             origin->ds_phys->ds_next_clones_obj, dsobj, tx));
 724                 }
 725 
 726                 dmu_buf_will_dirty(dd->dd_dbuf, tx);
 727                 dd->dd_phys->dd_origin_obj = origin->ds_object;
 728                 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
 729                         if (origin->ds_dir->dd_phys->dd_clones == 0) {
 730                                 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
 731                                 origin->ds_dir->dd_phys->dd_clones =
 732                                     zap_create(mos,
 733                                     DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
 734                         }
 735                         VERIFY0(zap_add_int(mos,
 736                             origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
 737                 }
 738         }
 739 
 740         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
 741                 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
 742 
 743         dmu_buf_rele(dbuf, FTAG);
 744 
 745         dmu_buf_will_dirty(dd->dd_dbuf, tx);
 746         dd->dd_phys->dd_head_dataset_obj = dsobj;
 747 
 748         return (dsobj);
 749 }
 750 
 751 static void
 752 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
 753 {
 754         objset_t *os;
 755 
 756         VERIFY0(dmu_objset_from_ds(ds, &os));
 757         bzero(&os->os_zil_header, sizeof (os->os_zil_header));
 758         dsl_dataset_dirty(ds, tx);
 759 }
 760 
 761 uint64_t
 762 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
 763     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
 764 {
 765         dsl_pool_t *dp = pdd->dd_pool;
 766         uint64_t dsobj, ddobj;
 767         dsl_dir_t *dd;
 768 
 769         ASSERT(dmu_tx_is_syncing(tx));
 770         ASSERT(lastname[0] != '@');
 771 
 772         ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
 773         VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
 774 
 775         dsobj = dsl_dataset_create_sync_dd(dd, origin,
 776             flags & ~DS_CREATE_FLAG_NODIRTY, tx);
 777 
 778         dsl_deleg_set_create_perms(dd, tx, cr);
 779 
 780         dsl_dir_rele(dd, FTAG);
 781 
 782         /*
 783          * If we are creating a clone, make sure we zero out any stale
 784          * data from the origin snapshots zil header.
 785          */
 786         if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
 787                 dsl_dataset_t *ds;
 788 
 789                 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 790                 dsl_dataset_zero_zil(ds, tx);
 791                 dsl_dataset_rele(ds, FTAG);
 792         }
 793 
 794         return (dsobj);
 795 }
 796 
 797 /*
 798  * The unique space in the head dataset can be calculated by subtracting
 799  * the space used in the most recent snapshot, that is still being used
 800  * in this file system, from the space currently in use.  To figure out
 801  * the space in the most recent snapshot still in use, we need to take
 802  * the total space used in the snapshot and subtract out the space that
 803  * has been freed up since the snapshot was taken.
 804  */
 805 void
 806 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
 807 {
 808         uint64_t mrs_used;
 809         uint64_t dlused, dlcomp, dluncomp;
 810 
 811         ASSERT(!dsl_dataset_is_snapshot(ds));
 812 
 813         if (ds->ds_phys->ds_prev_snap_obj != 0)
 814                 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
 815         else
 816                 mrs_used = 0;
 817 
 818         dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
 819 
 820         ASSERT3U(dlused, <=, mrs_used);
 821         ds->ds_phys->ds_unique_bytes =
 822             ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
 823 
 824         if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
 825             SPA_VERSION_UNIQUE_ACCURATE)
 826                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
 827 }
 828 
 829 void
 830 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
 831     dmu_tx_t *tx)
 832 {
 833         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 834         uint64_t count;
 835         int err;
 836 
 837         ASSERT(ds->ds_phys->ds_num_children >= 2);
 838         err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
 839         /*
 840          * The err should not be ENOENT, but a bug in a previous version
 841          * of the code could cause upgrade_clones_cb() to not set
 842          * ds_next_snap_obj when it should, leading to a missing entry.
 843          * If we knew that the pool was created after
 844          * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
 845          * ENOENT.  However, at least we can check that we don't have
 846          * too many entries in the next_clones_obj even after failing to
 847          * remove this one.
 848          */
 849         if (err != ENOENT)
 850                 VERIFY0(err);
 851         ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
 852             &count));
 853         ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
 854 }
 855 
 856 
 857 blkptr_t *
 858 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
 859 {
 860         return (&ds->ds_phys->ds_bp);
 861 }
 862 
 863 void
 864 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
 865 {
 866         ASSERT(dmu_tx_is_syncing(tx));
 867         /* If it's the meta-objset, set dp_meta_rootbp */
 868         if (ds == NULL) {
 869                 tx->tx_pool->dp_meta_rootbp = *bp;
 870         } else {
 871                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
 872                 ds->ds_phys->ds_bp = *bp;
 873         }
 874 }
 875 
 876 spa_t *
 877 dsl_dataset_get_spa(dsl_dataset_t *ds)
 878 {
 879         return (ds->ds_dir->dd_pool->dp_spa);
 880 }
 881 
 882 void
 883 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
 884 {
 885         dsl_pool_t *dp;
 886 
 887         if (ds == NULL) /* this is the meta-objset */
 888                 return;
 889 
 890         ASSERT(ds->ds_objset != NULL);
 891 
 892         if (ds->ds_phys->ds_next_snap_obj != 0)
 893                 panic("dirtying snapshot!");
 894 
 895         dp = ds->ds_dir->dd_pool;
 896 
 897         if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
 898                 /* up the hold count until we can be written out */
 899                 dmu_buf_add_ref(ds->ds_dbuf, ds);
 900         }
 901 }
 902 
 903 boolean_t
 904 dsl_dataset_is_dirty(dsl_dataset_t *ds)
 905 {
 906         for (int t = 0; t < TXG_SIZE; t++) {
 907                 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
 908                     ds, t))
 909                         return (B_TRUE);
 910         }
 911         return (B_FALSE);
 912 }
 913 
 914 static int
 915 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
 916 {
 917         uint64_t asize;
 918 
 919         if (!dmu_tx_is_syncing(tx))
 920                 return (0);
 921 
 922         /*
 923          * If there's an fs-only reservation, any blocks that might become
 924          * owned by the snapshot dataset must be accommodated by space
 925          * outside of the reservation.
 926          */
 927         ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
 928         asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
 929         if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
 930                 return (SET_ERROR(ENOSPC));
 931 
 932         /*
 933          * Propagate any reserved space for this snapshot to other
 934          * snapshot checks in this sync group.
 935          */
 936         if (asize > 0)
 937                 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
 938 
 939         return (0);
 940 }
 941 
 942 typedef struct dsl_dataset_snapshot_arg {
 943         nvlist_t *ddsa_snaps;
 944         nvlist_t *ddsa_props;
 945         nvlist_t *ddsa_errors;
 946 } dsl_dataset_snapshot_arg_t;
 947 
 948 int
 949 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
 950     dmu_tx_t *tx)
 951 {
 952         int error;
 953         uint64_t value;
 954 
 955         ds->ds_trysnap_txg = tx->tx_txg;
 956 
 957         if (!dmu_tx_is_syncing(tx))
 958                 return (0);
 959 
 960         /*
 961          * We don't allow multiple snapshots of the same txg.  If there
 962          * is already one, try again.
 963          */
 964         if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
 965                 return (SET_ERROR(EAGAIN));
 966 
 967         /*
 968          * Check for conflicting snapshot name.
 969          */
 970         error = dsl_dataset_snap_lookup(ds, snapname, &value);
 971         if (error == 0)
 972                 return (SET_ERROR(EEXIST));
 973         if (error != ENOENT)
 974                 return (error);
 975 
 976         error = dsl_dataset_snapshot_reserve_space(ds, tx);
 977         if (error != 0)
 978                 return (error);
 979 
 980         return (0);
 981 }
 982 
 983 static int
 984 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
 985 {
 986         dsl_dataset_snapshot_arg_t *ddsa = arg;
 987         dsl_pool_t *dp = dmu_tx_pool(tx);
 988         nvpair_t *pair;
 989         int rv = 0;
 990 
 991         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
 992             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
 993                 int error = 0;
 994                 dsl_dataset_t *ds;
 995                 char *name, *atp;
 996                 char dsname[MAXNAMELEN];
 997 
 998                 name = nvpair_name(pair);
 999                 if (strlen(name) >= MAXNAMELEN)
1000                         error = SET_ERROR(ENAMETOOLONG);
1001                 if (error == 0) {
1002                         atp = strchr(name, '@');
1003                         if (atp == NULL)
1004                                 error = SET_ERROR(EINVAL);
1005                         if (error == 0)
1006                                 (void) strlcpy(dsname, name, atp - name + 1);
1007                 }
1008                 if (error == 0)
1009                         error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1010                 if (error == 0) {
1011                         error = dsl_dataset_snapshot_check_impl(ds,
1012                             atp + 1, tx);
1013                         dsl_dataset_rele(ds, FTAG);
1014                 }
1015 
1016                 if (error != 0) {
1017                         if (ddsa->ddsa_errors != NULL) {
1018                                 fnvlist_add_int32(ddsa->ddsa_errors,
1019                                     name, error);
1020                         }
1021                         rv = error;
1022                 }
1023         }
1024         return (rv);
1025 }
1026 
1027 void
1028 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1029     dmu_tx_t *tx)
1030 {
1031         static zil_header_t zero_zil;
1032 
1033         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1034         dmu_buf_t *dbuf;
1035         dsl_dataset_phys_t *dsphys;
1036         uint64_t dsobj, crtxg;
1037         objset_t *mos = dp->dp_meta_objset;
1038         objset_t *os;
1039 
1040         ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1041 
1042         /*
1043          * If we are on an old pool, the zil must not be active, in which
1044          * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1045          */
1046         ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1047             dmu_objset_from_ds(ds, &os) != 0 ||
1048             bcmp(&os->os_phys->os_zil_header, &zero_zil,
1049             sizeof (zero_zil)) == 0);
1050 
1051 
1052         /*
1053          * The origin's ds_creation_txg has to be < TXG_INITIAL
1054          */
1055         if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1056                 crtxg = 1;
1057         else
1058                 crtxg = tx->tx_txg;
1059 
1060         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1061             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1062         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1063         dmu_buf_will_dirty(dbuf, tx);
1064         dsphys = dbuf->db_data;
1065         bzero(dsphys, sizeof (dsl_dataset_phys_t));
1066         dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1067         dsphys->ds_fsid_guid = unique_create();
1068         (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1069             sizeof (dsphys->ds_guid));
1070         dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1071         dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1072         dsphys->ds_next_snap_obj = ds->ds_object;
1073         dsphys->ds_num_children = 1;
1074         dsphys->ds_creation_time = gethrestime_sec();
1075         dsphys->ds_creation_txg = crtxg;
1076         dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1077         dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1078         dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1079         dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1080         dsphys->ds_flags = ds->ds_phys->ds_flags;
1081         dsphys->ds_bp = ds->ds_phys->ds_bp;
1082         dmu_buf_rele(dbuf, FTAG);
1083 
1084         ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1085         if (ds->ds_prev) {
1086                 uint64_t next_clones_obj =
1087                     ds->ds_prev->ds_phys->ds_next_clones_obj;
1088                 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1089                     ds->ds_object ||
1090                     ds->ds_prev->ds_phys->ds_num_children > 1);
1091                 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1092                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1093                         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1094                             ds->ds_prev->ds_phys->ds_creation_txg);
1095                         ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1096                 } else if (next_clones_obj != 0) {
1097                         dsl_dataset_remove_from_next_clones(ds->ds_prev,
1098                             dsphys->ds_next_snap_obj, tx);
1099                         VERIFY0(zap_add_int(mos,
1100                             next_clones_obj, dsobj, tx));
1101                 }
1102         }
1103 
1104         /*
1105          * If we have a reference-reservation on this dataset, we will
1106          * need to increase the amount of refreservation being charged
1107          * since our unique space is going to zero.
1108          */
1109         if (ds->ds_reserved) {
1110                 int64_t delta;
1111                 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1112                 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1113                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1114                     delta, 0, 0, tx);
1115         }
1116 
1117         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1118         ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1119             UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1120         dsl_deadlist_close(&ds->ds_deadlist);
1121         dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1122         dsl_deadlist_add_key(&ds->ds_deadlist,
1123             ds->ds_phys->ds_prev_snap_txg, tx);
1124 
1125         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1126         ds->ds_phys->ds_prev_snap_obj = dsobj;
1127         ds->ds_phys->ds_prev_snap_txg = crtxg;
1128         ds->ds_phys->ds_unique_bytes = 0;
1129         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1130                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1131 
1132         VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1133             snapname, 8, 1, &dsobj, tx));
1134 
1135         if (ds->ds_prev)
1136                 dsl_dataset_rele(ds->ds_prev, ds);
1137         VERIFY0(dsl_dataset_hold_obj(dp,
1138             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1139 
1140         dsl_scan_ds_snapshotted(ds, tx);
1141 
1142         dsl_dir_snap_cmtime_update(ds->ds_dir);
1143 
1144         spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1145 }
1146 
1147 static void
1148 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1149 {
1150         dsl_dataset_snapshot_arg_t *ddsa = arg;
1151         dsl_pool_t *dp = dmu_tx_pool(tx);
1152         nvpair_t *pair;
1153 
1154         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1155             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1156                 dsl_dataset_t *ds;
1157                 char *name, *atp;
1158                 char dsname[MAXNAMELEN];
1159 
1160                 name = nvpair_name(pair);
1161                 atp = strchr(name, '@');
1162                 (void) strlcpy(dsname, name, atp - name + 1);
1163                 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1164 
1165                 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1166                 if (ddsa->ddsa_props != NULL) {
1167                         dsl_props_set_sync_impl(ds->ds_prev,
1168                             ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1169                 }
1170                 dsl_dataset_rele(ds, FTAG);
1171         }
1172 }
1173 
1174 /*
1175  * The snapshots must all be in the same pool.
1176  * All-or-nothing: if there are any failures, nothing will be modified.
1177  */
1178 int
1179 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1180 {
1181         dsl_dataset_snapshot_arg_t ddsa;
1182         nvpair_t *pair;
1183         boolean_t needsuspend;
1184         int error;
1185         spa_t *spa;
1186         char *firstname;
1187         nvlist_t *suspended = NULL;
1188 
1189         pair = nvlist_next_nvpair(snaps, NULL);
1190         if (pair == NULL)
1191                 return (0);
1192         firstname = nvpair_name(pair);
1193 
1194         error = spa_open(firstname, &spa, FTAG);
1195         if (error != 0)
1196                 return (error);
1197         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1198         spa_close(spa, FTAG);
1199 
1200         if (needsuspend) {
1201                 suspended = fnvlist_alloc();
1202                 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1203                     pair = nvlist_next_nvpair(snaps, pair)) {
1204                         char fsname[MAXNAMELEN];
1205                         char *snapname = nvpair_name(pair);
1206                         char *atp;
1207                         void *cookie;
1208 
1209                         atp = strchr(snapname, '@');
1210                         if (atp == NULL) {
1211                                 error = SET_ERROR(EINVAL);
1212                                 break;
1213                         }
1214                         (void) strlcpy(fsname, snapname, atp - snapname + 1);
1215 
1216                         error = zil_suspend(fsname, &cookie);
1217                         if (error != 0)
1218                                 break;
1219                         fnvlist_add_uint64(suspended, fsname,
1220                             (uintptr_t)cookie);
1221                 }
1222         }
1223 
1224         ddsa.ddsa_snaps = snaps;
1225         ddsa.ddsa_props = props;
1226         ddsa.ddsa_errors = errors;
1227 
1228         if (error == 0) {
1229                 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1230                     dsl_dataset_snapshot_sync, &ddsa,
1231                     fnvlist_num_pairs(snaps) * 3);
1232         }
1233 
1234         if (suspended != NULL) {
1235                 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1236                     pair = nvlist_next_nvpair(suspended, pair)) {
1237                         zil_resume((void *)(uintptr_t)
1238                             fnvpair_value_uint64(pair));
1239                 }
1240                 fnvlist_free(suspended);
1241         }
1242 
1243         return (error);
1244 }
1245 
1246 typedef struct dsl_dataset_snapshot_tmp_arg {
1247         const char *ddsta_fsname;
1248         const char *ddsta_snapname;
1249         minor_t ddsta_cleanup_minor;
1250         const char *ddsta_htag;
1251 } dsl_dataset_snapshot_tmp_arg_t;
1252 
1253 static int
1254 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1255 {
1256         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1257         dsl_pool_t *dp = dmu_tx_pool(tx);
1258         dsl_dataset_t *ds;
1259         int error;
1260 
1261         error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1262         if (error != 0)
1263                 return (error);
1264 
1265         error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, tx);
1266         if (error != 0) {
1267                 dsl_dataset_rele(ds, FTAG);
1268                 return (error);
1269         }
1270 
1271         if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1272                 dsl_dataset_rele(ds, FTAG);
1273                 return (SET_ERROR(ENOTSUP));
1274         }
1275         error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1276             B_TRUE, tx);
1277         if (error != 0) {
1278                 dsl_dataset_rele(ds, FTAG);
1279                 return (error);
1280         }
1281 
1282         dsl_dataset_rele(ds, FTAG);
1283         return (0);
1284 }
1285 
1286 static void
1287 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1288 {
1289         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1290         dsl_pool_t *dp = dmu_tx_pool(tx);
1291         dsl_dataset_t *ds;
1292 
1293         VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1294 
1295         dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1296         dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1297             ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1298         dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1299 
1300         dsl_dataset_rele(ds, FTAG);
1301 }
1302 
1303 int
1304 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1305     minor_t cleanup_minor, const char *htag)
1306 {
1307         dsl_dataset_snapshot_tmp_arg_t ddsta;
1308         int error;
1309         spa_t *spa;
1310         boolean_t needsuspend;
1311         void *cookie;
1312 
1313         ddsta.ddsta_fsname = fsname;
1314         ddsta.ddsta_snapname = snapname;
1315         ddsta.ddsta_cleanup_minor = cleanup_minor;
1316         ddsta.ddsta_htag = htag;
1317 
1318         error = spa_open(fsname, &spa, FTAG);
1319         if (error != 0)
1320                 return (error);
1321         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1322         spa_close(spa, FTAG);
1323 
1324         if (needsuspend) {
1325                 error = zil_suspend(fsname, &cookie);
1326                 if (error != 0)
1327                         return (error);
1328         }
1329 
1330         error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1331             dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1332 
1333         if (needsuspend)
1334                 zil_resume(cookie);
1335         return (error);
1336 }
1337 
1338 
1339 void
1340 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1341 {
1342         ASSERT(dmu_tx_is_syncing(tx));
1343         ASSERT(ds->ds_objset != NULL);
1344         ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1345 
1346         /*
1347          * in case we had to change ds_fsid_guid when we opened it,
1348          * sync it out now.
1349          */
1350         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1351         ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1352 
1353         dmu_objset_sync(ds->ds_objset, zio, tx);
1354 }
1355 
1356 static void
1357 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1358 {
1359         uint64_t count = 0;
1360         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1361         zap_cursor_t zc;
1362         zap_attribute_t za;
1363         nvlist_t *propval = fnvlist_alloc();
1364         nvlist_t *val = fnvlist_alloc();
1365 
1366         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1367 
1368         /*
1369          * There may be missing entries in ds_next_clones_obj
1370          * due to a bug in a previous version of the code.
1371          * Only trust it if it has the right number of entries.
1372          */
1373         if (ds->ds_phys->ds_next_clones_obj != 0) {
1374                 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1375                     &count));
1376         }
1377         if (count != ds->ds_phys->ds_num_children - 1)
1378                 goto fail;
1379         for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1380             zap_cursor_retrieve(&zc, &za) == 0;
1381             zap_cursor_advance(&zc)) {
1382                 dsl_dataset_t *clone;
1383                 char buf[ZFS_MAXNAMELEN];
1384                 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1385                     za.za_first_integer, FTAG, &clone));
1386                 dsl_dir_name(clone->ds_dir, buf);
1387                 fnvlist_add_boolean(val, buf);
1388                 dsl_dataset_rele(clone, FTAG);
1389         }
1390         zap_cursor_fini(&zc);
1391         fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1392         fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1393 fail:
1394         nvlist_free(val);
1395         nvlist_free(propval);
1396 }
1397 
1398 void
1399 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1400 {
1401         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1402         uint64_t refd, avail, uobjs, aobjs, ratio;
1403 
1404         ASSERT(dsl_pool_config_held(dp));
1405 
1406         ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1407             (ds->ds_phys->ds_uncompressed_bytes * 100 /
1408             ds->ds_phys->ds_compressed_bytes);
1409 
1410         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1411         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1412             ds->ds_phys->ds_uncompressed_bytes);
1413 
1414         if (dsl_dataset_is_snapshot(ds)) {
1415                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1416                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1417                     ds->ds_phys->ds_unique_bytes);
1418                 get_clones_stat(ds, nv);
1419         } else {
1420                 dsl_dir_stats(ds->ds_dir, nv);
1421         }
1422 
1423         dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1424         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1425         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1426 
1427         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1428             ds->ds_phys->ds_creation_time);
1429         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1430             ds->ds_phys->ds_creation_txg);
1431         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1432             ds->ds_quota);
1433         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1434             ds->ds_reserved);
1435         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1436             ds->ds_phys->ds_guid);
1437         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1438             ds->ds_phys->ds_unique_bytes);
1439         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1440             ds->ds_object);
1441         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1442             ds->ds_userrefs);
1443         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1444             DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1445 
1446         if (ds->ds_phys->ds_prev_snap_obj != 0) {
1447                 uint64_t written, comp, uncomp;
1448                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1449                 dsl_dataset_t *prev;
1450 
1451                 int err = dsl_dataset_hold_obj(dp,
1452                     ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1453                 if (err == 0) {
1454                         err = dsl_dataset_space_written(prev, ds, &written,
1455                             &comp, &uncomp);
1456                         dsl_dataset_rele(prev, FTAG);
1457                         if (err == 0) {
1458                                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1459                                     written);
1460                         }
1461                 }
1462         }
1463 }
1464 
1465 void
1466 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1467 {
1468         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1469         ASSERT(dsl_pool_config_held(dp));
1470 
1471         stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1472         stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1473         stat->dds_guid = ds->ds_phys->ds_guid;
1474         stat->dds_origin[0] = '\0';
1475         if (dsl_dataset_is_snapshot(ds)) {
1476                 stat->dds_is_snapshot = B_TRUE;
1477                 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1478         } else {
1479                 stat->dds_is_snapshot = B_FALSE;
1480                 stat->dds_num_clones = 0;
1481 
1482                 if (dsl_dir_is_clone(ds->ds_dir)) {
1483                         dsl_dataset_t *ods;
1484 
1485                         VERIFY0(dsl_dataset_hold_obj(dp,
1486                             ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1487                         dsl_dataset_name(ods, stat->dds_origin);
1488                         dsl_dataset_rele(ods, FTAG);
1489                 }
1490         }
1491 }
1492 
1493 uint64_t
1494 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1495 {
1496         return (ds->ds_fsid_guid);
1497 }
1498 
1499 void
1500 dsl_dataset_space(dsl_dataset_t *ds,
1501     uint64_t *refdbytesp, uint64_t *availbytesp,
1502     uint64_t *usedobjsp, uint64_t *availobjsp)
1503 {
1504         *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1505         *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1506         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1507                 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1508         if (ds->ds_quota != 0) {
1509                 /*
1510                  * Adjust available bytes according to refquota
1511                  */
1512                 if (*refdbytesp < ds->ds_quota)
1513                         *availbytesp = MIN(*availbytesp,
1514                             ds->ds_quota - *refdbytesp);
1515                 else
1516                         *availbytesp = 0;
1517         }
1518         *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1519         *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1520 }
1521 
1522 boolean_t
1523 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1524 {
1525         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1526 
1527         ASSERT(dsl_pool_config_held(dp));
1528         if (ds->ds_prev == NULL)
1529                 return (B_FALSE);
1530         if (ds->ds_phys->ds_bp.blk_birth >
1531             ds->ds_prev->ds_phys->ds_creation_txg) {
1532                 objset_t *os, *os_prev;
1533                 /*
1534                  * It may be that only the ZIL differs, because it was
1535                  * reset in the head.  Don't count that as being
1536                  * modified.
1537                  */
1538                 if (dmu_objset_from_ds(ds, &os) != 0)
1539                         return (B_TRUE);
1540                 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
1541                         return (B_TRUE);
1542                 return (bcmp(&os->os_phys->os_meta_dnode,
1543                     &os_prev->os_phys->os_meta_dnode,
1544                     sizeof (os->os_phys->os_meta_dnode)) != 0);
1545         }
1546         return (B_FALSE);
1547 }
1548 
1549 typedef struct dsl_dataset_rename_snapshot_arg {
1550         const char *ddrsa_fsname;
1551         const char *ddrsa_oldsnapname;
1552         const char *ddrsa_newsnapname;
1553         boolean_t ddrsa_recursive;
1554         dmu_tx_t *ddrsa_tx;
1555 } dsl_dataset_rename_snapshot_arg_t;
1556 
1557 /* ARGSUSED */
1558 static int
1559 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1560     dsl_dataset_t *hds, void *arg)
1561 {
1562         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1563         int error;
1564         uint64_t val;
1565 
1566         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1567         if (error != 0) {
1568                 /* ignore nonexistent snapshots */
1569                 return (error == ENOENT ? 0 : error);
1570         }
1571 
1572         /* new name should not exist */
1573         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1574         if (error == 0)
1575                 error = SET_ERROR(EEXIST);
1576         else if (error == ENOENT)
1577                 error = 0;
1578 
1579         /* dataset name + 1 for the "@" + the new snapshot name must fit */
1580         if (dsl_dir_namelen(hds->ds_dir) + 1 +
1581             strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1582                 error = SET_ERROR(ENAMETOOLONG);
1583 
1584         return (error);
1585 }
1586 
1587 static int
1588 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1589 {
1590         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1591         dsl_pool_t *dp = dmu_tx_pool(tx);
1592         dsl_dataset_t *hds;
1593         int error;
1594 
1595         error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1596         if (error != 0)
1597                 return (error);
1598 
1599         if (ddrsa->ddrsa_recursive) {
1600                 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1601                     dsl_dataset_rename_snapshot_check_impl, ddrsa,
1602                     DS_FIND_CHILDREN);
1603         } else {
1604                 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1605         }
1606         dsl_dataset_rele(hds, FTAG);
1607         return (error);
1608 }
1609 
1610 static int
1611 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1612     dsl_dataset_t *hds, void *arg)
1613 {
1614         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1615         dsl_dataset_t *ds;
1616         uint64_t val;
1617         dmu_tx_t *tx = ddrsa->ddrsa_tx;
1618         int error;
1619 
1620         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1621         ASSERT(error == 0 || error == ENOENT);
1622         if (error == ENOENT) {
1623                 /* ignore nonexistent snapshots */
1624                 return (0);
1625         }
1626 
1627         VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1628 
1629         /* log before we change the name */
1630         spa_history_log_internal_ds(ds, "rename", tx,
1631             "-> @%s", ddrsa->ddrsa_newsnapname);
1632 
1633         VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1634         mutex_enter(&ds->ds_lock);
1635         (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1636         mutex_exit(&ds->ds_lock);
1637         VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1638             ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1639 
1640         dsl_dataset_rele(ds, FTAG);
1641         return (0);
1642 }
1643 
1644 static void
1645 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1646 {
1647         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1648         dsl_pool_t *dp = dmu_tx_pool(tx);
1649         dsl_dataset_t *hds;
1650 
1651         VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1652         ddrsa->ddrsa_tx = tx;
1653         if (ddrsa->ddrsa_recursive) {
1654                 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1655                     dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1656                     DS_FIND_CHILDREN));
1657         } else {
1658                 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1659         }
1660         dsl_dataset_rele(hds, FTAG);
1661 }
1662 
1663 int
1664 dsl_dataset_rename_snapshot(const char *fsname,
1665     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1666 {
1667         dsl_dataset_rename_snapshot_arg_t ddrsa;
1668 
1669         ddrsa.ddrsa_fsname = fsname;
1670         ddrsa.ddrsa_oldsnapname = oldsnapname;
1671         ddrsa.ddrsa_newsnapname = newsnapname;
1672         ddrsa.ddrsa_recursive = recursive;
1673 
1674         return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1675             dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1676 }
1677 
1678 static int
1679 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1680 {
1681         const char *fsname = arg;
1682         dsl_pool_t *dp = dmu_tx_pool(tx);
1683         dsl_dataset_t *ds;
1684         int64_t unused_refres_delta;
1685         int error;
1686 
1687         error = dsl_dataset_hold(dp, fsname, FTAG, &ds);
1688         if (error != 0)
1689                 return (error);
1690 
1691         /* must not be a snapshot */
1692         if (dsl_dataset_is_snapshot(ds)) {
1693                 dsl_dataset_rele(ds, FTAG);
1694                 return (SET_ERROR(EINVAL));
1695         }
1696 
1697         /* must have a most recent snapshot */
1698         if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1699                 dsl_dataset_rele(ds, FTAG);
1700                 return (SET_ERROR(EINVAL));
1701         }
1702 
1703         if (dsl_dataset_long_held(ds)) {
1704                 dsl_dataset_rele(ds, FTAG);
1705                 return (SET_ERROR(EBUSY));
1706         }
1707 
1708         /*
1709          * Check if the snap we are rolling back to uses more than
1710          * the refquota.
1711          */
1712         if (ds->ds_quota != 0 &&
1713             ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1714                 dsl_dataset_rele(ds, FTAG);
1715                 return (SET_ERROR(EDQUOT));
1716         }
1717 
1718         /*
1719          * When we do the clone swap, we will temporarily use more space
1720          * due to the refreservation (the head will no longer have any
1721          * unique space, so the entire amount of the refreservation will need
1722          * to be free).  We will immediately destroy the clone, freeing
1723          * this space, but the freeing happens over many txg's.
1724          */
1725         unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1726             ds->ds_phys->ds_unique_bytes);
1727 
1728         if (unused_refres_delta > 0 &&
1729             unused_refres_delta >
1730             dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1731                 dsl_dataset_rele(ds, FTAG);
1732                 return (SET_ERROR(ENOSPC));
1733         }
1734 
1735         dsl_dataset_rele(ds, FTAG);
1736         return (0);
1737 }
1738 
1739 static void
1740 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1741 {
1742         const char *fsname = arg;
1743         dsl_pool_t *dp = dmu_tx_pool(tx);
1744         dsl_dataset_t *ds, *clone;
1745         uint64_t cloneobj;
1746 
1747         VERIFY0(dsl_dataset_hold(dp, fsname, FTAG, &ds));
1748 
1749         cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1750             ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1751 
1752         VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1753 
1754         dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1755         dsl_dataset_zero_zil(ds, tx);
1756 
1757         dsl_destroy_head_sync_impl(clone, tx);
1758 
1759         dsl_dataset_rele(clone, FTAG);
1760         dsl_dataset_rele(ds, FTAG);
1761 }
1762 
1763 int
1764 dsl_dataset_rollback(const char *fsname)
1765 {
1766         return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1767             dsl_dataset_rollback_sync, (void *)fsname, 1));
1768 }
1769 
1770 struct promotenode {
1771         list_node_t link;
1772         dsl_dataset_t *ds;
1773 };
1774 
1775 typedef struct dsl_dataset_promote_arg {
1776         const char *ddpa_clonename;
1777         dsl_dataset_t *ddpa_clone;
1778         list_t shared_snaps, origin_snaps, clone_snaps;
1779         dsl_dataset_t *origin_origin; /* origin of the origin */
1780         uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1781         char *err_ds;
1782 } dsl_dataset_promote_arg_t;
1783 
1784 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1785 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1786     void *tag);
1787 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1788 
1789 static int
1790 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1791 {
1792         dsl_dataset_promote_arg_t *ddpa = arg;
1793         dsl_pool_t *dp = dmu_tx_pool(tx);
1794         dsl_dataset_t *hds;
1795         struct promotenode *snap;
1796         dsl_dataset_t *origin_ds;
1797         int err;
1798         uint64_t unused;
1799 
1800         err = promote_hold(ddpa, dp, FTAG);
1801         if (err != 0)
1802                 return (err);
1803 
1804         hds = ddpa->ddpa_clone;
1805 
1806         if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1807                 promote_rele(ddpa, FTAG);
1808                 return (SET_ERROR(EXDEV));
1809         }
1810 
1811         /*
1812          * Compute and check the amount of space to transfer.  Since this is
1813          * so expensive, don't do the preliminary check.
1814          */
1815         if (!dmu_tx_is_syncing(tx)) {
1816                 promote_rele(ddpa, FTAG);
1817                 return (0);
1818         }
1819 
1820         snap = list_head(&ddpa->shared_snaps);
1821         origin_ds = snap->ds;
1822 
1823         /* compute origin's new unique space */
1824         snap = list_tail(&ddpa->clone_snaps);
1825         ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1826         dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1827             origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1828             &ddpa->unique, &unused, &unused);
1829 
1830         /*
1831          * Walk the snapshots that we are moving
1832          *
1833          * Compute space to transfer.  Consider the incremental changes
1834          * to used by each snapshot:
1835          * (my used) = (prev's used) + (blocks born) - (blocks killed)
1836          * So each snapshot gave birth to:
1837          * (blocks born) = (my used) - (prev's used) + (blocks killed)
1838          * So a sequence would look like:
1839          * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1840          * Which simplifies to:
1841          * uN + kN + kN-1 + ... + k1 + k0
1842          * Note however, if we stop before we reach the ORIGIN we get:
1843          * uN + kN + kN-1 + ... + kM - uM-1
1844          */
1845         ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1846         ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1847         ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1848         for (snap = list_head(&ddpa->shared_snaps); snap;
1849             snap = list_next(&ddpa->shared_snaps, snap)) {
1850                 uint64_t val, dlused, dlcomp, dluncomp;
1851                 dsl_dataset_t *ds = snap->ds;
1852 
1853                 /*
1854                  * If there are long holds, we won't be able to evict
1855                  * the objset.
1856                  */
1857                 if (dsl_dataset_long_held(ds)) {
1858                         err = SET_ERROR(EBUSY);
1859                         goto out;
1860                 }
1861 
1862                 /* Check that the snapshot name does not conflict */
1863                 VERIFY0(dsl_dataset_get_snapname(ds));
1864                 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1865                 if (err == 0) {
1866                         (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1867                         err = SET_ERROR(EEXIST);
1868                         goto out;
1869                 }
1870                 if (err != ENOENT)
1871                         goto out;
1872 
1873                 /* The very first snapshot does not have a deadlist */
1874                 if (ds->ds_phys->ds_prev_snap_obj == 0)
1875                         continue;
1876 
1877                 dsl_deadlist_space(&ds->ds_deadlist,
1878                     &dlused, &dlcomp, &dluncomp);
1879                 ddpa->used += dlused;
1880                 ddpa->comp += dlcomp;
1881                 ddpa->uncomp += dluncomp;
1882         }
1883 
1884         /*
1885          * If we are a clone of a clone then we never reached ORIGIN,
1886          * so we need to subtract out the clone origin's used space.
1887          */
1888         if (ddpa->origin_origin) {
1889                 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
1890                 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
1891                 ddpa->uncomp -=
1892                     ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
1893         }
1894 
1895         /* Check that there is enough space here */
1896         err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
1897             ddpa->used);
1898         if (err != 0)
1899                 goto out;
1900 
1901         /*
1902          * Compute the amounts of space that will be used by snapshots
1903          * after the promotion (for both origin and clone).  For each,
1904          * it is the amount of space that will be on all of their
1905          * deadlists (that was not born before their new origin).
1906          */
1907         if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1908                 uint64_t space;
1909 
1910                 /*
1911                  * Note, typically this will not be a clone of a clone,
1912                  * so dd_origin_txg will be < TXG_INITIAL, so
1913                  * these snaplist_space() -> dsl_deadlist_space_range()
1914                  * calls will be fast because they do not have to
1915                  * iterate over all bps.
1916                  */
1917                 snap = list_head(&ddpa->origin_snaps);
1918                 err = snaplist_space(&ddpa->shared_snaps,
1919                     snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
1920                 if (err != 0)
1921                         goto out;
1922 
1923                 err = snaplist_space(&ddpa->clone_snaps,
1924                     snap->ds->ds_dir->dd_origin_txg, &space);
1925                 if (err != 0)
1926                         goto out;
1927                 ddpa->cloneusedsnap += space;
1928         }
1929         if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1930                 err = snaplist_space(&ddpa->origin_snaps,
1931                     origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
1932                 if (err != 0)
1933                         goto out;
1934         }
1935 
1936 out:
1937         promote_rele(ddpa, FTAG);
1938         return (err);
1939 }
1940 
1941 static void
1942 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
1943 {
1944         dsl_dataset_promote_arg_t *ddpa = arg;
1945         dsl_pool_t *dp = dmu_tx_pool(tx);
1946         dsl_dataset_t *hds;
1947         struct promotenode *snap;
1948         dsl_dataset_t *origin_ds;
1949         dsl_dataset_t *origin_head;
1950         dsl_dir_t *dd;
1951         dsl_dir_t *odd = NULL;
1952         uint64_t oldnext_obj;
1953         int64_t delta;
1954 
1955         VERIFY0(promote_hold(ddpa, dp, FTAG));
1956         hds = ddpa->ddpa_clone;
1957 
1958         ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
1959 
1960         snap = list_head(&ddpa->shared_snaps);
1961         origin_ds = snap->ds;
1962         dd = hds->ds_dir;
1963 
1964         snap = list_head(&ddpa->origin_snaps);
1965         origin_head = snap->ds;
1966 
1967         /*
1968          * We need to explicitly open odd, since origin_ds's dd will be
1969          * changing.
1970          */
1971         VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
1972             NULL, FTAG, &odd));
1973 
1974         /* change origin's next snap */
1975         dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
1976         oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
1977         snap = list_tail(&ddpa->clone_snaps);
1978         ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1979         origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
1980 
1981         /* change the origin's next clone */
1982         if (origin_ds->ds_phys->ds_next_clones_obj) {
1983                 dsl_dataset_remove_from_next_clones(origin_ds,
1984                     snap->ds->ds_object, tx);
1985                 VERIFY0(zap_add_int(dp->dp_meta_objset,
1986                     origin_ds->ds_phys->ds_next_clones_obj,
1987                     oldnext_obj, tx));
1988         }
1989 
1990         /* change origin */
1991         dmu_buf_will_dirty(dd->dd_dbuf, tx);
1992         ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
1993         dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
1994         dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
1995         dmu_buf_will_dirty(odd->dd_dbuf, tx);
1996         odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
1997         origin_head->ds_dir->dd_origin_txg =
1998             origin_ds->ds_phys->ds_creation_txg;
1999 
2000         /* change dd_clone entries */
2001         if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2002                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2003                     odd->dd_phys->dd_clones, hds->ds_object, tx));
2004                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2005                     ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2006                     hds->ds_object, tx));
2007 
2008                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2009                     ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2010                     origin_head->ds_object, tx));
2011                 if (dd->dd_phys->dd_clones == 0) {
2012                         dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2013                             DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2014                 }
2015                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2016                     dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2017         }
2018 
2019         /* move snapshots to this dir */
2020         for (snap = list_head(&ddpa->shared_snaps); snap;
2021             snap = list_next(&ddpa->shared_snaps, snap)) {
2022                 dsl_dataset_t *ds = snap->ds;
2023 
2024                 /*
2025                  * Property callbacks are registered to a particular
2026                  * dsl_dir.  Since ours is changing, evict the objset
2027                  * so that they will be unregistered from the old dsl_dir.
2028                  */
2029                 if (ds->ds_objset) {
2030                         dmu_objset_evict(ds->ds_objset);
2031                         ds->ds_objset = NULL;
2032                 }
2033 
2034                 /* move snap name entry */
2035                 VERIFY0(dsl_dataset_get_snapname(ds));
2036                 VERIFY0(dsl_dataset_snap_remove(origin_head,
2037                     ds->ds_snapname, tx));
2038                 VERIFY0(zap_add(dp->dp_meta_objset,
2039                     hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2040                     8, 1, &ds->ds_object, tx));
2041 
2042                 /* change containing dsl_dir */
2043                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2044                 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2045                 ds->ds_phys->ds_dir_obj = dd->dd_object;
2046                 ASSERT3P(ds->ds_dir, ==, odd);
2047                 dsl_dir_rele(ds->ds_dir, ds);
2048                 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2049                     NULL, ds, &ds->ds_dir));
2050 
2051                 /* move any clone references */
2052                 if (ds->ds_phys->ds_next_clones_obj &&
2053                     spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2054                         zap_cursor_t zc;
2055                         zap_attribute_t za;
2056 
2057                         for (zap_cursor_init(&zc, dp->dp_meta_objset,
2058                             ds->ds_phys->ds_next_clones_obj);
2059                             zap_cursor_retrieve(&zc, &za) == 0;
2060                             zap_cursor_advance(&zc)) {
2061                                 dsl_dataset_t *cnds;
2062                                 uint64_t o;
2063 
2064                                 if (za.za_first_integer == oldnext_obj) {
2065                                         /*
2066                                          * We've already moved the
2067                                          * origin's reference.
2068                                          */
2069                                         continue;
2070                                 }
2071 
2072                                 VERIFY0(dsl_dataset_hold_obj(dp,
2073                                     za.za_first_integer, FTAG, &cnds));
2074                                 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2075 
2076                                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2077                                     odd->dd_phys->dd_clones, o, tx));
2078                                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2079                                     dd->dd_phys->dd_clones, o, tx));
2080                                 dsl_dataset_rele(cnds, FTAG);
2081                         }
2082                         zap_cursor_fini(&zc);
2083                 }
2084 
2085                 ASSERT(!dsl_prop_hascb(ds));
2086         }
2087 
2088         /*
2089          * Change space accounting.
2090          * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2091          * both be valid, or both be 0 (resulting in delta == 0).  This
2092          * is true for each of {clone,origin} independently.
2093          */
2094 
2095         delta = ddpa->cloneusedsnap -
2096             dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2097         ASSERT3S(delta, >=, 0);
2098         ASSERT3U(ddpa->used, >=, delta);
2099         dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2100         dsl_dir_diduse_space(dd, DD_USED_HEAD,
2101             ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2102 
2103         delta = ddpa->originusedsnap -
2104             odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2105         ASSERT3S(delta, <=, 0);
2106         ASSERT3U(ddpa->used, >=, -delta);
2107         dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2108         dsl_dir_diduse_space(odd, DD_USED_HEAD,
2109             -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2110 
2111         origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2112 
2113         /* log history record */
2114         spa_history_log_internal_ds(hds, "promote", tx, "");
2115 
2116         dsl_dir_rele(odd, FTAG);
2117         promote_rele(ddpa, FTAG);
2118 }
2119 
2120 /*
2121  * Make a list of dsl_dataset_t's for the snapshots between first_obj
2122  * (exclusive) and last_obj (inclusive).  The list will be in reverse
2123  * order (last_obj will be the list_head()).  If first_obj == 0, do all
2124  * snapshots back to this dataset's origin.
2125  */
2126 static int
2127 snaplist_make(dsl_pool_t *dp,
2128     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2129 {
2130         uint64_t obj = last_obj;
2131 
2132         list_create(l, sizeof (struct promotenode),
2133             offsetof(struct promotenode, link));
2134 
2135         while (obj != first_obj) {
2136                 dsl_dataset_t *ds;
2137                 struct promotenode *snap;
2138                 int err;
2139 
2140                 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2141                 ASSERT(err != ENOENT);
2142                 if (err != 0)
2143                         return (err);
2144 
2145                 if (first_obj == 0)
2146                         first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2147 
2148                 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2149                 snap->ds = ds;
2150                 list_insert_tail(l, snap);
2151                 obj = ds->ds_phys->ds_prev_snap_obj;
2152         }
2153 
2154         return (0);
2155 }
2156 
2157 static int
2158 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2159 {
2160         struct promotenode *snap;
2161 
2162         *spacep = 0;
2163         for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2164                 uint64_t used, comp, uncomp;
2165                 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2166                     mintxg, UINT64_MAX, &used, &comp, &uncomp);
2167                 *spacep += used;
2168         }
2169         return (0);
2170 }
2171 
2172 static void
2173 snaplist_destroy(list_t *l, void *tag)
2174 {
2175         struct promotenode *snap;
2176 
2177         if (l == NULL || !list_link_active(&l->list_head))
2178                 return;
2179 
2180         while ((snap = list_tail(l)) != NULL) {
2181                 list_remove(l, snap);
2182                 dsl_dataset_rele(snap->ds, tag);
2183                 kmem_free(snap, sizeof (*snap));
2184         }
2185         list_destroy(l);
2186 }
2187 
2188 static int
2189 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2190 {
2191         int error;
2192         dsl_dir_t *dd;
2193         struct promotenode *snap;
2194 
2195         error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2196             &ddpa->ddpa_clone);
2197         if (error != 0)
2198                 return (error);
2199         dd = ddpa->ddpa_clone->ds_dir;
2200 
2201         if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2202             !dsl_dir_is_clone(dd)) {
2203                 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2204                 return (SET_ERROR(EINVAL));
2205         }
2206 
2207         error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2208             &ddpa->shared_snaps, tag);
2209         if (error != 0)
2210                 goto out;
2211 
2212         error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2213             &ddpa->clone_snaps, tag);
2214         if (error != 0)
2215                 goto out;
2216 
2217         snap = list_head(&ddpa->shared_snaps);
2218         ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2219         error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2220             snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2221             &ddpa->origin_snaps, tag);
2222         if (error != 0)
2223                 goto out;
2224 
2225         if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2226                 error = dsl_dataset_hold_obj(dp,
2227                     snap->ds->ds_dir->dd_phys->dd_origin_obj,
2228                     tag, &ddpa->origin_origin);
2229                 if (error != 0)
2230                         goto out;
2231         }
2232 out:
2233         if (error != 0)
2234                 promote_rele(ddpa, tag);
2235         return (error);
2236 }
2237 
2238 static void
2239 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2240 {
2241         snaplist_destroy(&ddpa->shared_snaps, tag);
2242         snaplist_destroy(&ddpa->clone_snaps, tag);
2243         snaplist_destroy(&ddpa->origin_snaps, tag);
2244         if (ddpa->origin_origin != NULL)
2245                 dsl_dataset_rele(ddpa->origin_origin, tag);
2246         dsl_dataset_rele(ddpa->ddpa_clone, tag);
2247 }
2248 
2249 /*
2250  * Promote a clone.
2251  *
2252  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2253  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
2254  */
2255 int
2256 dsl_dataset_promote(const char *name, char *conflsnap)
2257 {
2258         dsl_dataset_promote_arg_t ddpa = { 0 };
2259         uint64_t numsnaps;
2260         int error;
2261         objset_t *os;
2262 
2263         /*
2264          * We will modify space proportional to the number of
2265          * snapshots.  Compute numsnaps.
2266          */
2267         error = dmu_objset_hold(name, FTAG, &os);
2268         if (error != 0)
2269                 return (error);
2270         error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2271             dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2272         dmu_objset_rele(os, FTAG);
2273         if (error != 0)
2274                 return (error);
2275 
2276         ddpa.ddpa_clonename = name;
2277         ddpa.err_ds = conflsnap;
2278 
2279         return (dsl_sync_task(name, dsl_dataset_promote_check,
2280             dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2281 }
2282 
2283 int
2284 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2285     dsl_dataset_t *origin_head, boolean_t force)
2286 {
2287         int64_t unused_refres_delta;
2288 
2289         /* they should both be heads */
2290         if (dsl_dataset_is_snapshot(clone) ||
2291             dsl_dataset_is_snapshot(origin_head))
2292                 return (SET_ERROR(EINVAL));
2293 
2294         /* the branch point should be just before them */
2295         if (clone->ds_prev != origin_head->ds_prev)
2296                 return (SET_ERROR(EINVAL));
2297 
2298         /* clone should be the clone (unless they are unrelated) */
2299         if (clone->ds_prev != NULL &&
2300             clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2301             origin_head->ds_object !=
2302             clone->ds_prev->ds_phys->ds_next_snap_obj)
2303                 return (SET_ERROR(EINVAL));
2304 
2305         /* the clone should be a child of the origin */
2306         if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2307                 return (SET_ERROR(EINVAL));
2308 
2309         /* origin_head shouldn't be modified unless 'force' */
2310         if (!force && dsl_dataset_modified_since_lastsnap(origin_head))
2311                 return (SET_ERROR(ETXTBSY));
2312 
2313         /* origin_head should have no long holds (e.g. is not mounted) */
2314         if (dsl_dataset_long_held(origin_head))
2315                 return (SET_ERROR(EBUSY));
2316 
2317         /* check amount of any unconsumed refreservation */
2318         unused_refres_delta =
2319             (int64_t)MIN(origin_head->ds_reserved,
2320             origin_head->ds_phys->ds_unique_bytes) -
2321             (int64_t)MIN(origin_head->ds_reserved,
2322             clone->ds_phys->ds_unique_bytes);
2323 
2324         if (unused_refres_delta > 0 &&
2325             unused_refres_delta >
2326             dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2327                 return (SET_ERROR(ENOSPC));
2328 
2329         /* clone can't be over the head's refquota */
2330         if (origin_head->ds_quota != 0 &&
2331             clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2332                 return (SET_ERROR(EDQUOT));
2333 
2334         return (0);
2335 }
2336 
2337 void
2338 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2339     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2340 {
2341         dsl_pool_t *dp = dmu_tx_pool(tx);
2342         int64_t unused_refres_delta;
2343 
2344         ASSERT(clone->ds_reserved == 0);
2345         ASSERT(origin_head->ds_quota == 0 ||
2346             clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2347 
2348         dmu_buf_will_dirty(clone->ds_dbuf, tx);
2349         dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2350 
2351         if (clone->ds_objset != NULL) {
2352                 dmu_objset_evict(clone->ds_objset);
2353                 clone->ds_objset = NULL;
2354         }
2355 
2356         if (origin_head->ds_objset != NULL) {
2357                 dmu_objset_evict(origin_head->ds_objset);
2358                 origin_head->ds_objset = NULL;
2359         }
2360 
2361         unused_refres_delta =
2362             (int64_t)MIN(origin_head->ds_reserved,
2363             origin_head->ds_phys->ds_unique_bytes) -
2364             (int64_t)MIN(origin_head->ds_reserved,
2365             clone->ds_phys->ds_unique_bytes);
2366 
2367         /*
2368          * Reset origin's unique bytes, if it exists.
2369          */
2370         if (clone->ds_prev) {
2371                 dsl_dataset_t *origin = clone->ds_prev;
2372                 uint64_t comp, uncomp;
2373 
2374                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2375                 dsl_deadlist_space_range(&clone->ds_deadlist,
2376                     origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2377                     &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2378         }
2379 
2380         /* swap blkptrs */
2381         {
2382                 blkptr_t tmp;
2383                 tmp = origin_head->ds_phys->ds_bp;
2384                 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2385                 clone->ds_phys->ds_bp = tmp;
2386         }
2387 
2388         /* set dd_*_bytes */
2389         {
2390                 int64_t dused, dcomp, duncomp;
2391                 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2392                 uint64_t odl_used, odl_comp, odl_uncomp;
2393 
2394                 ASSERT3U(clone->ds_dir->dd_phys->
2395                     dd_used_breakdown[DD_USED_SNAP], ==, 0);
2396 
2397                 dsl_deadlist_space(&clone->ds_deadlist,
2398                     &cdl_used, &cdl_comp, &cdl_uncomp);
2399                 dsl_deadlist_space(&origin_head->ds_deadlist,
2400                     &odl_used, &odl_comp, &odl_uncomp);
2401 
2402                 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2403                     (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2404                 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2405                     (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2406                 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2407                     cdl_uncomp -
2408                     (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2409 
2410                 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2411                     dused, dcomp, duncomp, tx);
2412                 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2413                     -dused, -dcomp, -duncomp, tx);
2414 
2415                 /*
2416                  * The difference in the space used by snapshots is the
2417                  * difference in snapshot space due to the head's
2418                  * deadlist (since that's the only thing that's
2419                  * changing that affects the snapused).
2420                  */
2421                 dsl_deadlist_space_range(&clone->ds_deadlist,
2422                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2423                     &cdl_used, &cdl_comp, &cdl_uncomp);
2424                 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2425                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2426                     &odl_used, &odl_comp, &odl_uncomp);
2427                 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2428                     DD_USED_HEAD, DD_USED_SNAP, tx);
2429         }
2430 
2431         /* swap ds_*_bytes */
2432         SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2433             clone->ds_phys->ds_referenced_bytes);
2434         SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2435             clone->ds_phys->ds_compressed_bytes);
2436         SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2437             clone->ds_phys->ds_uncompressed_bytes);
2438         SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2439             clone->ds_phys->ds_unique_bytes);
2440 
2441         /* apply any parent delta for change in unconsumed refreservation */
2442         dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2443             unused_refres_delta, 0, 0, tx);
2444 
2445         /*
2446          * Swap deadlists.
2447          */
2448         dsl_deadlist_close(&clone->ds_deadlist);
2449         dsl_deadlist_close(&origin_head->ds_deadlist);
2450         SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2451             clone->ds_phys->ds_deadlist_obj);
2452         dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2453             clone->ds_phys->ds_deadlist_obj);
2454         dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2455             origin_head->ds_phys->ds_deadlist_obj);
2456 
2457         dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2458 
2459         spa_history_log_internal_ds(clone, "clone swap", tx,
2460             "parent=%s", origin_head->ds_dir->dd_myname);
2461 }
2462 
2463 /*
2464  * Given a pool name and a dataset object number in that pool,
2465  * return the name of that dataset.
2466  */
2467 int
2468 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2469 {
2470         dsl_pool_t *dp;
2471         dsl_dataset_t *ds;
2472         int error;
2473 
2474         error = dsl_pool_hold(pname, FTAG, &dp);
2475         if (error != 0)
2476                 return (error);
2477 
2478         error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2479         if (error == 0) {
2480                 dsl_dataset_name(ds, buf);
2481                 dsl_dataset_rele(ds, FTAG);
2482         }
2483         dsl_pool_rele(dp, FTAG);
2484 
2485         return (error);
2486 }
2487 
2488 int
2489 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2490     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2491 {
2492         int error = 0;
2493 
2494         ASSERT3S(asize, >, 0);
2495 
2496         /*
2497          * *ref_rsrv is the portion of asize that will come from any
2498          * unconsumed refreservation space.
2499          */
2500         *ref_rsrv = 0;
2501 
2502         mutex_enter(&ds->ds_lock);
2503         /*
2504          * Make a space adjustment for reserved bytes.
2505          */
2506         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2507                 ASSERT3U(*used, >=,
2508                     ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2509                 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2510                 *ref_rsrv =
2511                     asize - MIN(asize, parent_delta(ds, asize + inflight));
2512         }
2513 
2514         if (!check_quota || ds->ds_quota == 0) {
2515                 mutex_exit(&ds->ds_lock);
2516                 return (0);
2517         }
2518         /*
2519          * If they are requesting more space, and our current estimate
2520          * is over quota, they get to try again unless the actual
2521          * on-disk is over quota and there are no pending changes (which
2522          * may free up space for us).
2523          */
2524         if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2525                 if (inflight > 0 ||
2526                     ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2527                         error = SET_ERROR(ERESTART);
2528                 else
2529                         error = SET_ERROR(EDQUOT);
2530         }
2531         mutex_exit(&ds->ds_lock);
2532 
2533         return (error);
2534 }
2535 
2536 typedef struct dsl_dataset_set_qr_arg {
2537         const char *ddsqra_name;
2538         zprop_source_t ddsqra_source;
2539         uint64_t ddsqra_value;
2540 } dsl_dataset_set_qr_arg_t;
2541 
2542 
2543 /* ARGSUSED */
2544 static int
2545 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2546 {
2547         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2548         dsl_pool_t *dp = dmu_tx_pool(tx);
2549         dsl_dataset_t *ds;
2550         int error;
2551         uint64_t newval;
2552 
2553         if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2554                 return (SET_ERROR(ENOTSUP));
2555 
2556         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2557         if (error != 0)
2558                 return (error);
2559 
2560         if (dsl_dataset_is_snapshot(ds)) {
2561                 dsl_dataset_rele(ds, FTAG);
2562                 return (SET_ERROR(EINVAL));
2563         }
2564 
2565         error = dsl_prop_predict(ds->ds_dir,
2566             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2567             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2568         if (error != 0) {
2569                 dsl_dataset_rele(ds, FTAG);
2570                 return (error);
2571         }
2572 
2573         if (newval == 0) {
2574                 dsl_dataset_rele(ds, FTAG);
2575                 return (0);
2576         }
2577 
2578         if (newval < ds->ds_phys->ds_referenced_bytes ||
2579             newval < ds->ds_reserved) {
2580                 dsl_dataset_rele(ds, FTAG);
2581                 return (SET_ERROR(ENOSPC));
2582         }
2583 
2584         dsl_dataset_rele(ds, FTAG);
2585         return (0);
2586 }
2587 
2588 static void
2589 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2590 {
2591         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2592         dsl_pool_t *dp = dmu_tx_pool(tx);
2593         dsl_dataset_t *ds;
2594         uint64_t newval;
2595 
2596         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2597 
2598         dsl_prop_set_sync_impl(ds,
2599             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2600             ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2601             &ddsqra->ddsqra_value, tx);
2602 
2603         VERIFY0(dsl_prop_get_int_ds(ds,
2604             zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2605 
2606         if (ds->ds_quota != newval) {
2607                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2608                 ds->ds_quota = newval;
2609         }
2610         dsl_dataset_rele(ds, FTAG);
2611 }
2612 
2613 int
2614 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2615     uint64_t refquota)
2616 {
2617         dsl_dataset_set_qr_arg_t ddsqra;
2618 
2619         ddsqra.ddsqra_name = dsname;
2620         ddsqra.ddsqra_source = source;
2621         ddsqra.ddsqra_value = refquota;
2622 
2623         return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2624             dsl_dataset_set_refquota_sync, &ddsqra, 0));
2625 }
2626 
2627 static int
2628 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2629 {
2630         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2631         dsl_pool_t *dp = dmu_tx_pool(tx);
2632         dsl_dataset_t *ds;
2633         int error;
2634         uint64_t newval, unique;
2635 
2636         if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2637                 return (SET_ERROR(ENOTSUP));
2638 
2639         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2640         if (error != 0)
2641                 return (error);
2642 
2643         if (dsl_dataset_is_snapshot(ds)) {
2644                 dsl_dataset_rele(ds, FTAG);
2645                 return (SET_ERROR(EINVAL));
2646         }
2647 
2648         error = dsl_prop_predict(ds->ds_dir,
2649             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2650             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2651         if (error != 0) {
2652                 dsl_dataset_rele(ds, FTAG);
2653                 return (error);
2654         }
2655 
2656         /*
2657          * If we are doing the preliminary check in open context, the
2658          * space estimates may be inaccurate.
2659          */
2660         if (!dmu_tx_is_syncing(tx)) {
2661                 dsl_dataset_rele(ds, FTAG);
2662                 return (0);
2663         }
2664 
2665         mutex_enter(&ds->ds_lock);
2666         if (!DS_UNIQUE_IS_ACCURATE(ds))
2667                 dsl_dataset_recalc_head_uniq(ds);
2668         unique = ds->ds_phys->ds_unique_bytes;
2669         mutex_exit(&ds->ds_lock);
2670 
2671         if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2672                 uint64_t delta = MAX(unique, newval) -
2673                     MAX(unique, ds->ds_reserved);
2674 
2675                 if (delta >
2676                     dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2677                     (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2678                         dsl_dataset_rele(ds, FTAG);
2679                         return (SET_ERROR(ENOSPC));
2680                 }
2681         }
2682 
2683         dsl_dataset_rele(ds, FTAG);
2684         return (0);
2685 }
2686 
2687 void
2688 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2689     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2690 {
2691         uint64_t newval;
2692         uint64_t unique;
2693         int64_t delta;
2694 
2695         dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2696             source, sizeof (value), 1, &value, tx);
2697 
2698         VERIFY0(dsl_prop_get_int_ds(ds,
2699             zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2700 
2701         dmu_buf_will_dirty(ds->ds_dbuf, tx);
2702         mutex_enter(&ds->ds_dir->dd_lock);
2703         mutex_enter(&ds->ds_lock);
2704         ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2705         unique = ds->ds_phys->ds_unique_bytes;
2706         delta = MAX(0, (int64_t)(newval - unique)) -
2707             MAX(0, (int64_t)(ds->ds_reserved - unique));
2708         ds->ds_reserved = newval;
2709         mutex_exit(&ds->ds_lock);
2710 
2711         dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2712         mutex_exit(&ds->ds_dir->dd_lock);
2713 }
2714 
2715 static void
2716 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2717 {
2718         dsl_dataset_set_qr_arg_t *ddsqra = arg;
2719         dsl_pool_t *dp = dmu_tx_pool(tx);
2720         dsl_dataset_t *ds;
2721 
2722         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2723         dsl_dataset_set_refreservation_sync_impl(ds,
2724             ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2725         dsl_dataset_rele(ds, FTAG);
2726 }
2727 
2728 int
2729 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2730     uint64_t refreservation)
2731 {
2732         dsl_dataset_set_qr_arg_t ddsqra;
2733 
2734         ddsqra.ddsqra_name = dsname;
2735         ddsqra.ddsqra_source = source;
2736         ddsqra.ddsqra_value = refreservation;
2737 
2738         return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2739             dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2740 }
2741 
2742 /*
2743  * Return (in *usedp) the amount of space written in new that is not
2744  * present in oldsnap.  New may be a snapshot or the head.  Old must be
2745  * a snapshot before new, in new's filesystem (or its origin).  If not then
2746  * fail and return EINVAL.
2747  *
2748  * The written space is calculated by considering two components:  First, we
2749  * ignore any freed space, and calculate the written as new's used space
2750  * minus old's used space.  Next, we add in the amount of space that was freed
2751  * between the two snapshots, thus reducing new's used space relative to old's.
2752  * Specifically, this is the space that was born before old->ds_creation_txg,
2753  * and freed before new (ie. on new's deadlist or a previous deadlist).
2754  *
2755  * space freed                         [---------------------]
2756  * snapshots                       ---O-------O--------O-------O------
2757  *                                         oldsnap            new
2758  */
2759 int
2760 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2761     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2762 {
2763         int err = 0;
2764         uint64_t snapobj;
2765         dsl_pool_t *dp = new->ds_dir->dd_pool;
2766 
2767         ASSERT(dsl_pool_config_held(dp));
2768 
2769         *usedp = 0;
2770         *usedp += new->ds_phys->ds_referenced_bytes;
2771         *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2772 
2773         *compp = 0;
2774         *compp += new->ds_phys->ds_compressed_bytes;
2775         *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2776 
2777         *uncompp = 0;
2778         *uncompp += new->ds_phys->ds_uncompressed_bytes;
2779         *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2780 
2781         snapobj = new->ds_object;
2782         while (snapobj != oldsnap->ds_object) {
2783                 dsl_dataset_t *snap;
2784                 uint64_t used, comp, uncomp;
2785 
2786                 if (snapobj == new->ds_object) {
2787                         snap = new;
2788                 } else {
2789                         err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2790                         if (err != 0)
2791                                 break;
2792                 }
2793 
2794                 if (snap->ds_phys->ds_prev_snap_txg ==
2795                     oldsnap->ds_phys->ds_creation_txg) {
2796                         /*
2797                          * The blocks in the deadlist can not be born after
2798                          * ds_prev_snap_txg, so get the whole deadlist space,
2799                          * which is more efficient (especially for old-format
2800                          * deadlists).  Unfortunately the deadlist code
2801                          * doesn't have enough information to make this
2802                          * optimization itself.
2803                          */
2804                         dsl_deadlist_space(&snap->ds_deadlist,
2805                             &used, &comp, &uncomp);
2806                 } else {
2807                         dsl_deadlist_space_range(&snap->ds_deadlist,
2808                             0, oldsnap->ds_phys->ds_creation_txg,
2809                             &used, &comp, &uncomp);
2810                 }
2811                 *usedp += used;
2812                 *compp += comp;
2813                 *uncompp += uncomp;
2814 
2815                 /*
2816                  * If we get to the beginning of the chain of snapshots
2817                  * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2818                  * was not a snapshot of/before new.
2819                  */
2820                 snapobj = snap->ds_phys->ds_prev_snap_obj;
2821                 if (snap != new)
2822                         dsl_dataset_rele(snap, FTAG);
2823                 if (snapobj == 0) {
2824                         err = SET_ERROR(EINVAL);
2825                         break;
2826                 }
2827 
2828         }
2829         return (err);
2830 }
2831 
2832 /*
2833  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2834  * lastsnap, and all snapshots in between are deleted.
2835  *
2836  * blocks that would be freed            [---------------------------]
2837  * snapshots                       ---O-------O--------O-------O--------O
2838  *                                        firstsnap        lastsnap
2839  *
2840  * This is the set of blocks that were born after the snap before firstsnap,
2841  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2842  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2843  * We calculate this by iterating over the relevant deadlists (from the snap
2844  * after lastsnap, backward to the snap after firstsnap), summing up the
2845  * space on the deadlist that was born after the snap before firstsnap.
2846  */
2847 int
2848 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2849     dsl_dataset_t *lastsnap,
2850     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2851 {
2852         int err = 0;
2853         uint64_t snapobj;
2854         dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2855 
2856         ASSERT(dsl_dataset_is_snapshot(firstsnap));
2857         ASSERT(dsl_dataset_is_snapshot(lastsnap));
2858 
2859         /*
2860          * Check that the snapshots are in the same dsl_dir, and firstsnap
2861          * is before lastsnap.
2862          */
2863         if (firstsnap->ds_dir != lastsnap->ds_dir ||
2864             firstsnap->ds_phys->ds_creation_txg >
2865             lastsnap->ds_phys->ds_creation_txg)
2866                 return (SET_ERROR(EINVAL));
2867 
2868         *usedp = *compp = *uncompp = 0;
2869 
2870         snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2871         while (snapobj != firstsnap->ds_object) {
2872                 dsl_dataset_t *ds;
2873                 uint64_t used, comp, uncomp;
2874 
2875                 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
2876                 if (err != 0)
2877                         break;
2878 
2879                 dsl_deadlist_space_range(&ds->ds_deadlist,
2880                     firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2881                     &used, &comp, &uncomp);
2882                 *usedp += used;
2883                 *compp += comp;
2884                 *uncompp += uncomp;
2885 
2886                 snapobj = ds->ds_phys->ds_prev_snap_obj;
2887                 ASSERT3U(snapobj, !=, 0);
2888                 dsl_dataset_rele(ds, FTAG);
2889         }
2890         return (err);
2891 }
2892 
2893 /*
2894  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2895  * For example, they could both be snapshots of the same filesystem, and
2896  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
2897  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
2898  * filesystem.  Or 'earlier' could be the origin's origin.
2899  */
2900 boolean_t
2901 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
2902 {
2903         dsl_pool_t *dp = later->ds_dir->dd_pool;
2904         int error;
2905         boolean_t ret;
2906 
2907         ASSERT(dsl_pool_config_held(dp));
2908 
2909         if (earlier->ds_phys->ds_creation_txg >=
2910             later->ds_phys->ds_creation_txg)
2911                 return (B_FALSE);
2912 
2913         if (later->ds_dir == earlier->ds_dir)
2914                 return (B_TRUE);
2915         if (!dsl_dir_is_clone(later->ds_dir))
2916                 return (B_FALSE);
2917 
2918         if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
2919                 return (B_TRUE);
2920         dsl_dataset_t *origin;
2921         error = dsl_dataset_hold_obj(dp,
2922             later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
2923         if (error != 0)
2924                 return (B_FALSE);
2925         ret = dsl_dataset_is_before(origin, earlier);
2926         dsl_dataset_rele(origin, FTAG);
2927         return (ret);
2928 }