1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  25  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
  26  * Copyright (c) 2014 RackTop Systems.
  27  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  28  * Copyright (c) 2014 Integros [integros.com]
  29  * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
  30  * Copyright 2017 Nexenta Systems, Inc.
  31  */
  32 
  33 #include <sys/dmu_objset.h>
  34 #include <sys/dsl_dataset.h>
  35 #include <sys/dsl_dir.h>
  36 #include <sys/dsl_prop.h>
  37 #include <sys/dsl_synctask.h>
  38 #include <sys/dmu_traverse.h>
  39 #include <sys/dmu_impl.h>
  40 #include <sys/dmu_tx.h>
  41 #include <sys/arc.h>
  42 #include <sys/zio.h>
  43 #include <sys/zap.h>
  44 #include <sys/zfeature.h>
  45 #include <sys/unique.h>
  46 #include <sys/zfs_context.h>
  47 #include <sys/zfs_ioctl.h>
  48 #include <sys/spa.h>
  49 #include <sys/spa_impl.h>
  50 #include <sys/zfs_znode.h>
  51 #include <sys/zfs_onexit.h>
  52 #include <sys/zvol.h>
  53 #include <sys/dsl_scan.h>
  54 #include <sys/dsl_deadlist.h>
  55 #include <sys/dsl_destroy.h>
  56 #include <sys/dsl_userhold.h>
  57 #include <sys/dsl_bookmark.h>
  58 #include <sys/autosnap.h>
  59 #include <sys/dmu_send.h>
  60 #include <sys/zio_checksum.h>
  61 #include <sys/zio_compress.h>
  62 #include <zfs_fletcher.h>
  63 
  64 /*
  65  * The SPA supports block sizes up to 16MB.  However, very large blocks
  66  * can have an impact on i/o latency (e.g. tying up a spinning disk for
  67  * ~300ms), and also potentially on the memory allocator.  Therefore,
  68  * we do not allow the recordsize to be set larger than zfs_max_recordsize
  69  * (default 1MB).  Larger blocks can be created by changing this tunable,
  70  * and pools with larger blocks can always be imported and used, regardless
  71  * of this setting.
  72  */
  73 int zfs_max_recordsize = 1 * 1024 * 1024;
  74 
  75 #define SWITCH64(x, y) \
  76         { \
  77                 uint64_t __tmp = (x); \
  78                 (x) = (y); \
  79                 (y) = __tmp; \
  80         }
  81 
  82 #define DS_REF_MAX      (1ULL << 62)
  83 
  84 extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
  85 
  86 extern int spa_asize_inflation;
  87 
  88 static zil_header_t zero_zil;
  89 
  90 kmem_cache_t *zfs_ds_collector_cache = NULL;
  91 
  92 zfs_ds_collector_entry_t *
  93 dsl_dataset_collector_cache_alloc()
  94 {
  95         return (kmem_cache_alloc(zfs_ds_collector_cache, KM_SLEEP));
  96 }
  97 
  98 void
  99 dsl_dataset_collector_cache_free(zfs_ds_collector_entry_t *entry)
 100 {
 101         kmem_cache_free(zfs_ds_collector_cache, entry);
 102 }
 103 
 104 /*
 105  * Figure out how much of this delta should be propogated to the dsl_dir
 106  * layer.  If there's a refreservation, that space has already been
 107  * partially accounted for in our ancestors.
 108  */
 109 static int64_t
 110 parent_delta(dsl_dataset_t *ds, int64_t delta)
 111 {
 112         dsl_dataset_phys_t *ds_phys;
 113         uint64_t old_bytes, new_bytes;
 114 
 115         if (ds->ds_reserved == 0)
 116                 return (delta);
 117 
 118         ds_phys = dsl_dataset_phys(ds);
 119         old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
 120         new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
 121 
 122         ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
 123         return (new_bytes - old_bytes);
 124 }
 125 
 126 void
 127 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
 128 {
 129         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
 130         int compressed = BP_GET_PSIZE(bp);
 131         int uncompressed = BP_GET_UCSIZE(bp);
 132         int64_t delta;
 133 
 134         dprintf_bp(bp, "ds=%p", ds);
 135 
 136         ASSERT(dmu_tx_is_syncing(tx));
 137         /* It could have been compressed away to nothing */
 138         if (BP_IS_HOLE(bp))
 139                 return;
 140         ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
 141         ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
 142         if (ds == NULL) {
 143                 dsl_pool_mos_diduse_space(tx->tx_pool,
 144                     used, compressed, uncompressed);
 145                 return;
 146         }
 147 
 148         ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
 149         dmu_buf_will_dirty(ds->ds_dbuf, tx);
 150         mutex_enter(&ds->ds_lock);
 151         delta = parent_delta(ds, used);
 152         dsl_dataset_phys(ds)->ds_referenced_bytes += used;
 153         dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
 154         dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
 155         dsl_dataset_phys(ds)->ds_unique_bytes += used;
 156 
 157         if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
 158                 ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
 159                     B_TRUE;
 160         }
 161 
 162         spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
 163         if (f != SPA_FEATURE_NONE)
 164                 ds->ds_feature_activation_needed[f] = B_TRUE;
 165 
 166         mutex_exit(&ds->ds_lock);
 167         dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
 168             compressed, uncompressed, tx);
 169         dsl_dir_transfer_space(ds->ds_dir, used - delta,
 170             DD_USED_REFRSRV, DD_USED_HEAD, tx);
 171 }
 172 
 173 int
 174 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
 175     boolean_t async)
 176 {
 177         int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
 178         int compressed = BP_GET_PSIZE(bp);
 179         int uncompressed = BP_GET_UCSIZE(bp);
 180         wbc_data_t *wbc_data = spa_get_wbc_data(tx->tx_pool->dp_spa);
 181 
 182         if (BP_IS_HOLE(bp))
 183                 return (0);
 184 
 185         ASSERT(dmu_tx_is_syncing(tx));
 186         ASSERT(bp->blk_birth <= tx->tx_txg);
 187 
 188         if (ds == NULL) {
 189                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
 190                 dsl_pool_mos_diduse_space(tx->tx_pool,
 191                     -used, -compressed, -uncompressed);
 192                 return (used);
 193         }
 194         ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
 195 
 196         ASSERT(!ds->ds_is_snapshot);
 197         dmu_buf_will_dirty(ds->ds_dbuf, tx);
 198 
 199         if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
 200                 int64_t delta;
 201 
 202                 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
 203                 dsl_free(tx->tx_pool, tx->tx_txg, bp);
 204 
 205                 /* update amount of data which is changed in the window */
 206                 mutex_enter(&wbc_data->wbc_lock);
 207                 if (wbc_data->wbc_isvalid &&
 208                     bp->blk_birth && wbc_data->wbc_finish_txg &&
 209                     bp->blk_birth <= wbc_data->wbc_finish_txg &&
 210                     bp->blk_birth >= wbc_data->wbc_start_txg &&
 211                     !wbc_data->wbc_purge) {
 212 
 213                         wbc_data->wbc_altered_bytes += used;
 214                         if (wbc_data->wbc_altered_limit &&
 215                             wbc_data->wbc_altered_bytes >
 216                             wbc_data->wbc_altered_limit) {
 217                                 wbc_purge_window(tx->tx_pool->dp_spa, tx);
 218                         }
 219                 }
 220                 mutex_exit(&wbc_data->wbc_lock);
 221 
 222                 mutex_enter(&ds->ds_lock);
 223                 ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
 224                     !DS_UNIQUE_IS_ACCURATE(ds));
 225                 delta = parent_delta(ds, -used);
 226                 dsl_dataset_phys(ds)->ds_unique_bytes -= used;
 227                 mutex_exit(&ds->ds_lock);
 228                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
 229                     delta, -compressed, -uncompressed, tx);
 230                 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
 231                     DD_USED_REFRSRV, DD_USED_HEAD, tx);
 232         } else {
 233                 dprintf_bp(bp, "putting on dead list: %s", "");
 234                 if (async) {
 235                         /*
 236                          * We are here as part of zio's write done callback,
 237                          * which means we're a zio interrupt thread.  We can't
 238                          * call dsl_deadlist_insert() now because it may block
 239                          * waiting for I/O.  Instead, put bp on the deferred
 240                          * queue and let dsl_pool_sync() finish the job.
 241                          */
 242                         bplist_append(&ds->ds_pending_deadlist, bp);
 243                 } else {
 244                         dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
 245                 }
 246                 ASSERT3U(ds->ds_prev->ds_object, ==,
 247                     dsl_dataset_phys(ds)->ds_prev_snap_obj);
 248                 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
 249                 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
 250                 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
 251                     ds->ds_object && bp->blk_birth >
 252                     dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
 253                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
 254                         mutex_enter(&ds->ds_prev->ds_lock);
 255                         dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
 256                         mutex_exit(&ds->ds_prev->ds_lock);
 257                 }
 258                 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
 259                         dsl_dir_transfer_space(ds->ds_dir, used,
 260                             DD_USED_HEAD, DD_USED_SNAP, tx);
 261                 }
 262         }
 263         mutex_enter(&ds->ds_lock);
 264         ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
 265         dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
 266         ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
 267         dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
 268         ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
 269         dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
 270         mutex_exit(&ds->ds_lock);
 271 
 272         return (used);
 273 }
 274 
 275 /*
 276  * We have to release the fsid syncronously or we risk that a subsequent
 277  * mount of the same dataset will fail to unique_insert the fsid.  This
 278  * failure would manifest itself as the fsid of this dataset changing
 279  * between mounts which makes NFS clients quite unhappy.
 280  */
 281 static void
 282 dsl_dataset_evict_sync(void *dbu)
 283 {
 284         dsl_dataset_t *ds = dbu;
 285 
 286         ASSERT(ds->ds_owner == NULL);
 287 
 288         unique_remove(ds->ds_fsid_guid);
 289 }
 290 
 291 static void
 292 dsl_dataset_evict_async(void *dbu)
 293 {
 294         dsl_dataset_t *ds = dbu;
 295 
 296         ASSERT(ds->ds_owner == NULL);
 297 
 298         ds->ds_dbuf = NULL;
 299 
 300         if (ds->ds_objset != NULL)
 301                 dmu_objset_evict(ds->ds_objset);
 302 
 303         if (ds->ds_prev) {
 304                 dsl_dataset_rele(ds->ds_prev, ds);
 305                 ds->ds_prev = NULL;
 306         }
 307 
 308         bplist_destroy(&ds->ds_pending_deadlist);
 309         if (ds->ds_deadlist.dl_os != NULL)
 310                 dsl_deadlist_close(&ds->ds_deadlist);
 311         if (ds->ds_dir)
 312                 dsl_dir_async_rele(ds->ds_dir, ds);
 313 
 314         ASSERT(!list_link_active(&ds->ds_synced_link));
 315 
 316         list_destroy(&ds->ds_prop_cbs);
 317         mutex_destroy(&ds->ds_lock);
 318         mutex_destroy(&ds->ds_opening_lock);
 319         mutex_destroy(&ds->ds_sendstream_lock);
 320         refcount_destroy(&ds->ds_longholds);
 321         rrw_destroy(&ds->ds_bp_rwlock);
 322 
 323         kmem_free(ds, sizeof (dsl_dataset_t));
 324 }
 325 
 326 int
 327 dsl_dataset_get_snapname(dsl_dataset_t *ds)
 328 {
 329         dsl_dataset_phys_t *headphys;
 330         int err;
 331         dmu_buf_t *headdbuf;
 332         dsl_pool_t *dp = ds->ds_dir->dd_pool;
 333         objset_t *mos = dp->dp_meta_objset;
 334 
 335         if (ds->ds_snapname[0])
 336                 return (0);
 337         if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
 338                 return (0);
 339 
 340         err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
 341             FTAG, &headdbuf);
 342         if (err != 0)
 343                 return (err);
 344         headphys = headdbuf->db_data;
 345         err = zap_value_search(dp->dp_meta_objset,
 346             headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
 347         dmu_buf_rele(headdbuf, FTAG);
 348         return (err);
 349 }
 350 
 351 int
 352 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
 353 {
 354         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 355         uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
 356         matchtype_t mt = 0;
 357         int err;
 358 
 359         if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
 360                 mt = MT_NORMALIZE;
 361 
 362         err = zap_lookup_norm(mos, snapobj, name, 8, 1,
 363             value, mt, NULL, 0, NULL);
 364         if (err == ENOTSUP && (mt & MT_NORMALIZE))
 365                 err = zap_lookup(mos, snapobj, name, 8, 1, value);
 366         return (err);
 367 }
 368 
 369 int
 370 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
 371     boolean_t adj_cnt)
 372 {
 373         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 374         uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
 375         matchtype_t mt = 0;
 376         int err;
 377 
 378         dsl_dir_snap_cmtime_update(ds->ds_dir);
 379 
 380         if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
 381                 mt = MT_NORMALIZE;
 382 
 383         err = zap_remove_norm(mos, snapobj, name, mt, tx);
 384         if (err == ENOTSUP && (mt & MT_NORMALIZE))
 385                 err = zap_remove(mos, snapobj, name, tx);
 386 
 387         if (err == 0 && adj_cnt)
 388                 dsl_fs_ss_count_adjust(ds->ds_dir, -1,
 389                     DD_FIELD_SNAPSHOT_COUNT, tx);
 390 
 391         return (err);
 392 }
 393 
 394 boolean_t
 395 dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
 396 {
 397         dmu_buf_t *dbuf = ds->ds_dbuf;
 398         boolean_t result = B_FALSE;
 399 
 400         if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
 401             ds->ds_object, DMU_BONUS_BLKID, tag)) {
 402 
 403                 if (ds == dmu_buf_get_user(dbuf))
 404                         result = B_TRUE;
 405                 else
 406                         dmu_buf_rele(dbuf, tag);
 407         }
 408 
 409         return (result);
 410 }
 411 
 412 int
 413 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
 414     dsl_dataset_t **dsp)
 415 {
 416         objset_t *mos = dp->dp_meta_objset;
 417         dmu_buf_t *dbuf;
 418         dsl_dataset_t *ds;
 419         int err;
 420         dmu_object_info_t doi;
 421 
 422         ASSERT(dsl_pool_config_held(dp));
 423 
 424         err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
 425         if (err != 0)
 426                 return (err);
 427 
 428         /* Make sure dsobj has the correct object type. */
 429         dmu_object_info_from_db(dbuf, &doi);
 430         if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
 431                 dmu_buf_rele(dbuf, tag);
 432                 return (SET_ERROR(EINVAL));
 433         }
 434 
 435         ds = dmu_buf_get_user(dbuf);
 436         if (ds == NULL) {
 437                 dsl_dataset_t *winner = NULL;
 438 
 439                 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
 440                 ds->ds_dbuf = dbuf;
 441                 ds->ds_object = dsobj;
 442                 ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
 443 
 444                 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
 445                 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
 446                 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
 447                 rrw_init(&ds->ds_bp_rwlock, B_FALSE);
 448                 refcount_create(&ds->ds_longholds);
 449 
 450                 bplist_create(&ds->ds_pending_deadlist);
 451                 dsl_deadlist_open(&ds->ds_deadlist,
 452                     mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
 453 
 454                 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
 455                     offsetof(dmu_sendarg_t, dsa_link));
 456 
 457                 list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
 458                     offsetof(dsl_prop_cb_record_t, cbr_ds_node));
 459 
 460                 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
 461                         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
 462                                 if (!(spa_feature_table[f].fi_flags &
 463                                     ZFEATURE_FLAG_PER_DATASET))
 464                                         continue;
 465                                 err = zap_contains(mos, dsobj,
 466                                     spa_feature_table[f].fi_guid);
 467                                 if (err == 0) {
 468                                         ds->ds_feature_inuse[f] = B_TRUE;
 469                                 } else {
 470                                         ASSERT3U(err, ==, ENOENT);
 471                                         err = 0;
 472                                 }
 473                         }
 474                 }
 475 
 476                 err = dsl_dir_hold_obj(dp,
 477                     dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
 478                 if (err != 0) {
 479                         mutex_destroy(&ds->ds_lock);
 480                         mutex_destroy(&ds->ds_opening_lock);
 481                         mutex_destroy(&ds->ds_sendstream_lock);
 482                         refcount_destroy(&ds->ds_longholds);
 483                         bplist_destroy(&ds->ds_pending_deadlist);
 484                         dsl_deadlist_close(&ds->ds_deadlist);
 485                         kmem_free(ds, sizeof (dsl_dataset_t));
 486                         dmu_buf_rele(dbuf, tag);
 487                         return (err);
 488                 }
 489 
 490                 if (!ds->ds_is_snapshot) {
 491                         ds->ds_snapname[0] = '\0';
 492                         if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
 493                                 err = dsl_dataset_hold_obj(dp,
 494                                     dsl_dataset_phys(ds)->ds_prev_snap_obj,
 495                                     ds, &ds->ds_prev);
 496                         }
 497                         if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
 498                                 int zaperr = zap_lookup(mos, ds->ds_object,
 499                                     DS_FIELD_BOOKMARK_NAMES,
 500                                     sizeof (ds->ds_bookmarks), 1,
 501                                     &ds->ds_bookmarks);
 502                                 if (zaperr != ENOENT)
 503                                         VERIFY0(zaperr);
 504                         }
 505                 } else {
 506                         if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
 507                                 err = dsl_dataset_get_snapname(ds);
 508                         if (err == 0 &&
 509                             dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
 510                                 err = zap_count(
 511                                     ds->ds_dir->dd_pool->dp_meta_objset,
 512                                     dsl_dataset_phys(ds)->ds_userrefs_obj,
 513                                     &ds->ds_userrefs);
 514                         }
 515                 }
 516 
 517                 if (err == 0 && !ds->ds_is_snapshot) {
 518                         err = dsl_prop_get_int_ds(ds,
 519                             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
 520                             &ds->ds_reserved);
 521                         if (err == 0) {
 522                                 err = dsl_prop_get_int_ds(ds,
 523                                     zfs_prop_to_name(ZFS_PROP_REFQUOTA),
 524                                     &ds->ds_quota);
 525                         }
 526                 } else {
 527                         ds->ds_reserved = ds->ds_quota = 0;
 528                 }
 529 
 530                 dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
 531                     dsl_dataset_evict_async, &ds->ds_dbuf);
 532                 if (err == 0)
 533                         winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
 534 
 535                 if (err != 0 || winner != NULL) {
 536                         bplist_destroy(&ds->ds_pending_deadlist);
 537                         dsl_deadlist_close(&ds->ds_deadlist);
 538                         if (ds->ds_prev)
 539                                 dsl_dataset_rele(ds->ds_prev, ds);
 540                         dsl_dir_rele(ds->ds_dir, ds);
 541                         mutex_destroy(&ds->ds_lock);
 542                         mutex_destroy(&ds->ds_opening_lock);
 543                         mutex_destroy(&ds->ds_sendstream_lock);
 544                         refcount_destroy(&ds->ds_longholds);
 545                         kmem_free(ds, sizeof (dsl_dataset_t));
 546                         if (err != 0) {
 547                                 dmu_buf_rele(dbuf, tag);
 548                                 return (err);
 549                         }
 550                         ds = winner;
 551                 } else {
 552                         ds->ds_fsid_guid =
 553                             unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
 554                         if (ds->ds_fsid_guid !=
 555                             dsl_dataset_phys(ds)->ds_fsid_guid) {
 556                                 zfs_dbgmsg("ds_fsid_guid changed from "
 557                                     "%llx to %llx for pool %s dataset id %llu",
 558                                     (long long)
 559                                     dsl_dataset_phys(ds)->ds_fsid_guid,
 560                                     (long long)ds->ds_fsid_guid,
 561                                     spa_name(dp->dp_spa),
 562                                     dsobj);
 563                         }
 564                 }
 565         }
 566         ASSERT3P(ds->ds_dbuf, ==, dbuf);
 567         ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
 568         ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
 569             spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
 570             dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
 571         *dsp = ds;
 572         return (0);
 573 }
 574 
 575 int
 576 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
 577     void *tag, dsl_dataset_t **dsp)
 578 {
 579         dsl_dir_t *dd;
 580         const char *snapname;
 581         uint64_t obj;
 582         int err = 0;
 583         dsl_dataset_t *ds;
 584 
 585         err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
 586         if (err != 0)
 587                 return (err);
 588 
 589         ASSERT(dsl_pool_config_held(dp));
 590         obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
 591         if (obj != 0)
 592                 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
 593         else
 594                 err = SET_ERROR(ENOENT);
 595 
 596         /* we may be looking for a snapshot */
 597         if (err == 0 && snapname != NULL) {
 598                 dsl_dataset_t *snap_ds;
 599 
 600                 if (*snapname++ != '@') {
 601                         dsl_dataset_rele(ds, tag);
 602                         dsl_dir_rele(dd, FTAG);
 603                         return (SET_ERROR(ENOENT));
 604                 }
 605 
 606                 dprintf("looking for snapshot '%s'\n", snapname);
 607                 err = dsl_dataset_snap_lookup(ds, snapname, &obj);
 608                 if (err == 0)
 609                         err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
 610                 dsl_dataset_rele(ds, tag);
 611 
 612                 if (err == 0) {
 613                         mutex_enter(&snap_ds->ds_lock);
 614                         if (snap_ds->ds_snapname[0] == 0)
 615                                 (void) strlcpy(snap_ds->ds_snapname, snapname,
 616                                     sizeof (snap_ds->ds_snapname));
 617                         mutex_exit(&snap_ds->ds_lock);
 618                         ds = snap_ds;
 619                 }
 620         }
 621         if (err == 0)
 622                 *dsp = ds;
 623         dsl_dir_rele(dd, FTAG);
 624         return (err);
 625 }
 626 
 627 int
 628 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
 629     void *tag, dsl_dataset_t **dsp)
 630 {
 631         int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
 632         if (err != 0)
 633                 return (err);
 634         if (!dsl_dataset_tryown(*dsp, tag)) {
 635                 dsl_dataset_rele(*dsp, tag);
 636                 *dsp = NULL;
 637                 return (SET_ERROR(EBUSY));
 638         }
 639         return (0);
 640 }
 641 
 642 int
 643 dsl_dataset_own(dsl_pool_t *dp, const char *name,
 644     void *tag, dsl_dataset_t **dsp)
 645 {
 646         int err = dsl_dataset_hold(dp, name, tag, dsp);
 647         if (err != 0)
 648                 return (err);
 649         if (!dsl_dataset_tryown(*dsp, tag)) {
 650                 dsl_dataset_rele(*dsp, tag);
 651                 return (SET_ERROR(EBUSY));
 652         }
 653         return (0);
 654 }
 655 
 656 /*
 657  * See the comment above dsl_pool_hold() for details.  In summary, a long
 658  * hold is used to prevent destruction of a dataset while the pool hold
 659  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
 660  *
 661  * The dataset and pool must be held when this function is called.  After it
 662  * is called, the pool hold may be released while the dataset is still held
 663  * and accessed.
 664  */
 665 void
 666 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
 667 {
 668         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
 669         (void) refcount_add(&ds->ds_longholds, tag);
 670 }
 671 
 672 void
 673 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
 674 {
 675         (void) refcount_remove(&ds->ds_longholds, tag);
 676 }
 677 
 678 /* Return B_TRUE if there are any long holds on this dataset. */
 679 boolean_t
 680 dsl_dataset_long_held(dsl_dataset_t *ds)
 681 {
 682         return (!refcount_is_zero(&ds->ds_longholds));
 683 }
 684 
 685 void
 686 dsl_dataset_name(dsl_dataset_t *ds, char *name)
 687 {
 688         if (ds == NULL) {
 689                 (void) strcpy(name, "mos");
 690         } else {
 691                 dsl_dir_name(ds->ds_dir, name);
 692                 VERIFY0(dsl_dataset_get_snapname(ds));
 693                 if (ds->ds_snapname[0]) {
 694                         VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
 695                             <, ZFS_MAX_DATASET_NAME_LEN);
 696                         /*
 697                          * We use a "recursive" mutex so that we
 698                          * can call dprintf_ds() with ds_lock held.
 699                          */
 700                         if (!MUTEX_HELD(&ds->ds_lock)) {
 701                                 mutex_enter(&ds->ds_lock);
 702                                 VERIFY3U(strlcat(name, ds->ds_snapname,
 703                                     ZFS_MAX_DATASET_NAME_LEN), <,
 704                                     ZFS_MAX_DATASET_NAME_LEN);
 705                                 mutex_exit(&ds->ds_lock);
 706                         } else {
 707                                 VERIFY3U(strlcat(name, ds->ds_snapname,
 708                                     ZFS_MAX_DATASET_NAME_LEN), <,
 709                                     ZFS_MAX_DATASET_NAME_LEN);
 710                         }
 711                 }
 712         }
 713 }
 714 
 715 int
 716 dsl_dataset_namelen(dsl_dataset_t *ds)
 717 {
 718         VERIFY0(dsl_dataset_get_snapname(ds));
 719         mutex_enter(&ds->ds_lock);
 720         int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
 721         mutex_exit(&ds->ds_lock);
 722         return (len);
 723 }
 724 
 725 void
 726 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
 727 {
 728         dmu_buf_rele(ds->ds_dbuf, tag);
 729 }
 730 
 731 void
 732 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
 733 {
 734         ASSERT3P(ds->ds_owner, ==, tag);
 735         ASSERT(ds->ds_dbuf != NULL);
 736 
 737         mutex_enter(&ds->ds_lock);
 738         ds->ds_owner = NULL;
 739         mutex_exit(&ds->ds_lock);
 740         dsl_dataset_long_rele(ds, tag);
 741         dsl_dataset_rele(ds, tag);
 742 }
 743 
 744 boolean_t
 745 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
 746 {
 747         boolean_t gotit = FALSE;
 748 
 749         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
 750         mutex_enter(&ds->ds_lock);
 751         if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
 752                 ds->ds_owner = tag;
 753                 dsl_dataset_long_hold(ds, tag);
 754                 gotit = TRUE;
 755         }
 756         mutex_exit(&ds->ds_lock);
 757         return (gotit);
 758 }
 759 
 760 boolean_t
 761 dsl_dataset_has_owner(dsl_dataset_t *ds)
 762 {
 763         boolean_t rv;
 764         mutex_enter(&ds->ds_lock);
 765         rv = (ds->ds_owner != NULL);
 766         mutex_exit(&ds->ds_lock);
 767         return (rv);
 768 }
 769 
 770 static void
 771 dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
 772 {
 773         spa_t *spa = dmu_tx_pool(tx)->dp_spa;
 774         objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
 775         uint64_t zero = 0;
 776 
 777         VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
 778 
 779         spa_feature_incr(spa, f, tx);
 780         dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
 781 
 782         VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
 783             sizeof (zero), 1, &zero, tx));
 784 }
 785 
 786 void
 787 dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
 788 {
 789         spa_t *spa = dmu_tx_pool(tx)->dp_spa;
 790         objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
 791 
 792         VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
 793 
 794         VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
 795         spa_feature_decr(spa, f, tx);
 796 }
 797 
 798 uint64_t
 799 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
 800     uint64_t flags, dmu_tx_t *tx)
 801 {
 802         dsl_pool_t *dp = dd->dd_pool;
 803         dmu_buf_t *dbuf;
 804         dsl_dataset_phys_t *dsphys;
 805         uint64_t dsobj;
 806         objset_t *mos = dp->dp_meta_objset;
 807 
 808         if (origin == NULL)
 809                 origin = dp->dp_origin_snap;
 810 
 811         ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
 812         ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
 813         ASSERT(dmu_tx_is_syncing(tx));
 814         ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
 815 
 816         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
 817             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
 818         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
 819         dmu_buf_will_dirty(dbuf, tx);
 820         dsphys = dbuf->db_data;
 821         bzero(dsphys, sizeof (dsl_dataset_phys_t));
 822         dsphys->ds_dir_obj = dd->dd_object;
 823         dsphys->ds_flags = flags;
 824         dsphys->ds_fsid_guid = unique_create();
 825         (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
 826             sizeof (dsphys->ds_guid));
 827         dsphys->ds_snapnames_zapobj =
 828             zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
 829             DMU_OT_NONE, 0, tx);
 830         dsphys->ds_creation_time = gethrestime_sec();
 831         dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
 832 
 833         if (origin == NULL) {
 834                 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
 835         } else {
 836                 dsl_dataset_t *ohds; /* head of the origin snapshot */
 837 
 838                 dsphys->ds_prev_snap_obj = origin->ds_object;
 839                 dsphys->ds_prev_snap_txg =
 840                     dsl_dataset_phys(origin)->ds_creation_txg;
 841                 dsphys->ds_referenced_bytes =
 842                     dsl_dataset_phys(origin)->ds_referenced_bytes;
 843                 dsphys->ds_compressed_bytes =
 844                     dsl_dataset_phys(origin)->ds_compressed_bytes;
 845                 dsphys->ds_uncompressed_bytes =
 846                     dsl_dataset_phys(origin)->ds_uncompressed_bytes;
 847                 rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
 848                 dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
 849                 rrw_exit(&origin->ds_bp_rwlock, FTAG);
 850 
 851                 /*
 852                  * Inherit flags that describe the dataset's contents
 853                  * (INCONSISTENT) or properties (Case Insensitive).
 854                  */
 855                 dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
 856                     (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
 857 
 858                 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
 859                         if (origin->ds_feature_inuse[f])
 860                                 dsl_dataset_activate_feature(dsobj, f, tx);
 861                 }
 862 
 863                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
 864                 dsl_dataset_phys(origin)->ds_num_children++;
 865 
 866                 VERIFY0(dsl_dataset_hold_obj(dp,
 867                     dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
 868                     FTAG, &ohds));
 869                 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
 870                     dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
 871                 dsl_dataset_rele(ohds, FTAG);
 872 
 873                 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
 874                         if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
 875                                 dsl_dataset_phys(origin)->ds_next_clones_obj =
 876                                     zap_create(mos,
 877                                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
 878                         }
 879                         VERIFY0(zap_add_int(mos,
 880                             dsl_dataset_phys(origin)->ds_next_clones_obj,
 881                             dsobj, tx));
 882                 }
 883 
 884                 dmu_buf_will_dirty(dd->dd_dbuf, tx);
 885                 dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
 886                 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
 887                         if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
 888                                 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
 889                                 dsl_dir_phys(origin->ds_dir)->dd_clones =
 890                                     zap_create(mos,
 891                                     DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
 892                         }
 893                         VERIFY0(zap_add_int(mos,
 894                             dsl_dir_phys(origin->ds_dir)->dd_clones,
 895                             dsobj, tx));
 896                 }
 897         }
 898 
 899         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
 900                 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
 901 
 902         dmu_buf_rele(dbuf, FTAG);
 903 
 904         dmu_buf_will_dirty(dd->dd_dbuf, tx);
 905         dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
 906 
 907         return (dsobj);
 908 }
 909 
 910 static void
 911 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
 912 {
 913         objset_t *os;
 914 
 915         VERIFY0(dmu_objset_from_ds(ds, &os));
 916         if (bcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
 917                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
 918                 zio_t *zio;
 919 
 920                 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
 921 
 922                 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
 923                 dsl_dataset_sync(ds, zio, tx);
 924                 VERIFY0(zio_wait(zio));
 925 
 926                 /* dsl_dataset_sync_done will drop this reference. */
 927                 dmu_buf_add_ref(ds->ds_dbuf, ds);
 928                 dsl_dataset_sync_done(ds, tx);
 929         }
 930 }
 931 
 932 uint64_t
 933 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
 934     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
 935 {
 936         dsl_pool_t *dp = pdd->dd_pool;
 937         uint64_t dsobj, ddobj;
 938         dsl_dir_t *dd;
 939 
 940         ASSERT(dmu_tx_is_syncing(tx));
 941         ASSERT(lastname[0] != '@');
 942 
 943         ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
 944         VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
 945 
 946         dsobj = dsl_dataset_create_sync_dd(dd, origin,
 947             flags & ~DS_CREATE_FLAG_NODIRTY, tx);
 948 
 949         dsl_deleg_set_create_perms(dd, tx, cr);
 950 
 951         /*
 952          * Since we're creating a new node we know it's a leaf, so we can
 953          * initialize the counts if the limit feature is active.
 954          */
 955         if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
 956                 uint64_t cnt = 0;
 957                 objset_t *os = dd->dd_pool->dp_meta_objset;
 958 
 959                 dsl_dir_zapify(dd, tx);
 960                 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
 961                     sizeof (cnt), 1, &cnt, tx));
 962                 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
 963                     sizeof (cnt), 1, &cnt, tx));
 964         }
 965 
 966         dsl_dir_rele(dd, FTAG);
 967 
 968         /*
 969          * If we are creating a clone, make sure we zero out any stale
 970          * data from the origin snapshots zil header.
 971          */
 972         if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
 973                 dsl_dataset_t *ds;
 974 
 975                 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 976                 dsl_dataset_zero_zil(ds, tx);
 977                 dsl_dataset_rele(ds, FTAG);
 978         }
 979 
 980         return (dsobj);
 981 }
 982 
 983 /*
 984  * The unique space in the head dataset can be calculated by subtracting
 985  * the space used in the most recent snapshot, that is still being used
 986  * in this file system, from the space currently in use.  To figure out
 987  * the space in the most recent snapshot still in use, we need to take
 988  * the total space used in the snapshot and subtract out the space that
 989  * has been freed up since the snapshot was taken.
 990  */
 991 void
 992 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
 993 {
 994         uint64_t mrs_used;
 995         uint64_t dlused, dlcomp, dluncomp;
 996 
 997         ASSERT(!ds->ds_is_snapshot);
 998 
 999         if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1000                 mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
1001         else
1002                 mrs_used = 0;
1003 
1004         dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1005 
1006         ASSERT3U(dlused, <=, mrs_used);
1007         dsl_dataset_phys(ds)->ds_unique_bytes =
1008             dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
1009 
1010         if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1011             SPA_VERSION_UNIQUE_ACCURATE)
1012                 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1013 }
1014 
1015 void
1016 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
1017     dmu_tx_t *tx)
1018 {
1019         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1020         uint64_t count;
1021         int err;
1022 
1023         ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1024         err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1025             obj, tx);
1026         /*
1027          * The err should not be ENOENT, but a bug in a previous version
1028          * of the code could cause upgrade_clones_cb() to not set
1029          * ds_next_snap_obj when it should, leading to a missing entry.
1030          * If we knew that the pool was created after
1031          * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1032          * ENOENT.  However, at least we can check that we don't have
1033          * too many entries in the next_clones_obj even after failing to
1034          * remove this one.
1035          */
1036         if (err != ENOENT)
1037                 VERIFY0(err);
1038         ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1039             &count));
1040         ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1041 }
1042 
1043 
1044 blkptr_t *
1045 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1046 {
1047         return (&dsl_dataset_phys(ds)->ds_bp);
1048 }
1049 
1050 spa_t *
1051 dsl_dataset_get_spa(dsl_dataset_t *ds)
1052 {
1053         return (ds->ds_dir->dd_pool->dp_spa);
1054 }
1055 
1056 void
1057 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1058 {
1059         dsl_pool_t *dp;
1060 
1061         if (ds == NULL) /* this is the meta-objset */
1062                 return;
1063 
1064         ASSERT(ds->ds_objset != NULL);
1065 
1066         if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1067                 panic("dirtying snapshot!");
1068 
1069         /* Must not dirty a dataset in the same txg where it got snapshotted. */
1070         ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1071 
1072         dp = ds->ds_dir->dd_pool;
1073         if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1074                 /* up the hold count until we can be written out */
1075                 dmu_buf_add_ref(ds->ds_dbuf, ds);
1076         }
1077 }
1078 
1079 boolean_t
1080 dsl_dataset_is_dirty(dsl_dataset_t *ds)
1081 {
1082         for (int t = 0; t < TXG_SIZE; t++) {
1083                 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1084                     ds, t))
1085                         return (B_TRUE);
1086         }
1087         return (B_FALSE);
1088 }
1089 
1090 static int
1091 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1092 {
1093         uint64_t asize;
1094 
1095         if (!dmu_tx_is_syncing(tx))
1096                 return (0);
1097 
1098         /*
1099          * If there's an fs-only reservation, any blocks that might become
1100          * owned by the snapshot dataset must be accommodated by space
1101          * outside of the reservation.
1102          */
1103         ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1104         asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
1105         if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1106                 return (SET_ERROR(ENOSPC));
1107 
1108         /*
1109          * Propagate any reserved space for this snapshot to other
1110          * snapshot checks in this sync group.
1111          */
1112         if (asize > 0)
1113                 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1114 
1115         return (0);
1116 }
1117 
1118 int
1119 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1120     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
1121 {
1122         int error;
1123         uint64_t value;
1124 
1125         ds->ds_trysnap_txg = tx->tx_txg;
1126 
1127         if (!dmu_tx_is_syncing(tx))
1128                 return (0);
1129 
1130         /*
1131          * We don't allow multiple snapshots of the same txg.  If there
1132          * is already one, try again.
1133          */
1134         if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1135                 return (SET_ERROR(EAGAIN));
1136 
1137         /*
1138          * Check for conflicting snapshot name.
1139          */
1140         error = dsl_dataset_snap_lookup(ds, snapname, &value);
1141         if (error == 0)
1142                 return (SET_ERROR(EEXIST));
1143         if (error != ENOENT)
1144                 return (error);
1145 
1146         /*
1147          * We don't allow taking snapshots of inconsistent datasets, such as
1148          * those into which we are currently receiving.  However, if we are
1149          * creating this snapshot as part of a receive, this check will be
1150          * executed atomically with respect to the completion of the receive
1151          * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1152          * case we ignore this, knowing it will be fixed up for us shortly in
1153          * dmu_recv_end_sync().
1154          */
1155         if (!recv && DS_IS_INCONSISTENT(ds))
1156                 return (SET_ERROR(EBUSY));
1157 
1158         /*
1159          * Skip the check for temporary snapshots or if we have already checked
1160          * the counts in dsl_dataset_snapshot_check. This means we really only
1161          * check the count here when we're receiving a stream.
1162          */
1163         if (cnt != 0 && cr != NULL) {
1164                 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1165                     ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1166                 if (error != 0)
1167                         return (error);
1168         }
1169 
1170         error = dsl_dataset_snapshot_reserve_space(ds, tx);
1171         if (error != 0)
1172                 return (error);
1173 
1174         return (0);
1175 }
1176 
1177 int
1178 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1179 {
1180         dsl_dataset_snapshot_arg_t *ddsa = arg;
1181         dsl_pool_t *dp = dmu_tx_pool(tx);
1182         nvpair_t *pair;
1183         int rv = 0;
1184 
1185         if (ddsa->ddsa_autosnap && dmu_tx_is_syncing(tx))
1186                 autosnap_invalidate_list(dp, ddsa->ddsa_snaps);
1187 
1188         /*
1189          * Pre-compute how many total new snapshots will be created for each
1190          * level in the tree and below. This is needed for validating the
1191          * snapshot limit when either taking a recursive snapshot or when
1192          * taking multiple snapshots.
1193          *
1194          * The problem is that the counts are not actually adjusted when
1195          * we are checking, only when we finally sync. For a single snapshot,
1196          * this is easy, the count will increase by 1 at each node up the tree,
1197          * but its more complicated for the recursive/multiple snapshot case.
1198          *
1199          * The dsl_fs_ss_limit_check function does recursively check the count
1200          * at each level up the tree but since it is validating each snapshot
1201          * independently we need to be sure that we are validating the complete
1202          * count for the entire set of snapshots. We do this by rolling up the
1203          * counts for each component of the name into an nvlist and then
1204          * checking each of those cases with the aggregated count.
1205          *
1206          * This approach properly handles not only the recursive snapshot
1207          * case (where we get all of those on the ddsa_snaps list) but also
1208          * the sibling case (e.g. snapshot a/b and a/c so that we will also
1209          * validate the limit on 'a' using a count of 2).
1210          *
1211          * We validate the snapshot names in the third loop and only report
1212          * name errors once.
1213          */
1214         if (dmu_tx_is_syncing(tx)) {
1215                 nvlist_t *cnt_track = NULL;
1216                 cnt_track = fnvlist_alloc();
1217 
1218                 /* Rollup aggregated counts into the cnt_track list */
1219                 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1220                     pair != NULL;
1221                     pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1222                         char *pdelim;
1223                         uint64_t val;
1224                         char nm[MAXPATHLEN];
1225 
1226                         (void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1227                         pdelim = strchr(nm, '@');
1228                         if (pdelim == NULL)
1229                                 continue;
1230                         *pdelim = '\0';
1231 
1232                         do {
1233                                 if (nvlist_lookup_uint64(cnt_track, nm,
1234                                     &val) == 0) {
1235                                         /* update existing entry */
1236                                         fnvlist_add_uint64(cnt_track, nm,
1237                                             val + 1);
1238                                 } else {
1239                                         /* add to list */
1240                                         fnvlist_add_uint64(cnt_track, nm, 1);
1241                                 }
1242 
1243                                 pdelim = strrchr(nm, '/');
1244                                 if (pdelim != NULL)
1245                                         *pdelim = '\0';
1246                         } while (pdelim != NULL);
1247                 }
1248 
1249                 /* Check aggregated counts at each level */
1250                 for (pair = nvlist_next_nvpair(cnt_track, NULL);
1251                     pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1252                         int error = 0;
1253                         char *name;
1254                         uint64_t cnt = 0;
1255                         dsl_dataset_t *ds;
1256 
1257                         name = nvpair_name(pair);
1258                         cnt = fnvpair_value_uint64(pair);
1259                         ASSERT(cnt > 0);
1260 
1261                         error = dsl_dataset_hold(dp, name, FTAG, &ds);
1262                         if (error == 0) {
1263                                 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1264                                     ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1265                                     ddsa->ddsa_cr);
1266                                 dsl_dataset_rele(ds, FTAG);
1267                         }
1268 
1269                         if (error != 0) {
1270                                 if (ddsa->ddsa_errors != NULL)
1271                                         fnvlist_add_int32(ddsa->ddsa_errors,
1272                                             name, error);
1273                                 rv = error;
1274                                 /* only report one error for this check */
1275                                 break;
1276                         }
1277                 }
1278                 nvlist_free(cnt_track);
1279         }
1280 
1281         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1282             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1283                 int error = 0;
1284                 dsl_dataset_t *ds;
1285                 char *name, *atp;
1286                 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1287 
1288                 name = nvpair_name(pair);
1289                 if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1290                         error = SET_ERROR(ENAMETOOLONG);
1291                 if (error == 0) {
1292                         atp = strchr(name, '@');
1293                         if (atp == NULL)
1294                                 error = SET_ERROR(EINVAL);
1295                         if (error == 0)
1296                                 (void) strlcpy(dsname, name, atp - name + 1);
1297                 }
1298                 if (error == 0)
1299                         error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1300                 if (error == 0) {
1301                         /* passing 0/NULL skips dsl_fs_ss_limit_check */
1302                         error = dsl_dataset_snapshot_check_impl(ds,
1303                             atp + 1, tx, B_FALSE, 0, NULL);
1304                         dsl_dataset_rele(ds, FTAG);
1305                 }
1306 
1307                 if (error != 0) {
1308                         if (ddsa->ddsa_errors != NULL) {
1309                                 fnvlist_add_int32(ddsa->ddsa_errors,
1310                                     name, error);
1311                         }
1312                         rv = error;
1313                 }
1314         }
1315 
1316         return (rv);
1317 }
1318 
1319 void
1320 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1321     dmu_tx_t *tx)
1322 {
1323         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1324         dmu_buf_t *dbuf;
1325         dsl_dataset_phys_t *dsphys;
1326         uint64_t dsobj, crtxg;
1327         objset_t *mos = dp->dp_meta_objset;
1328         objset_t *os = NULL;
1329         uint64_t unique_bytes = 0;
1330 
1331         ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1332 
1333         /*
1334          * If we are on an old pool, the zil must not be active, in which
1335          * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1336          */
1337         ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1338             dmu_objset_from_ds(ds, &os) != 0 ||
1339             bcmp(&os->os_phys->os_zil_header, &zero_zil,
1340             sizeof (zero_zil)) == 0);
1341 
1342         /* Should not snapshot a dirty dataset. */
1343         ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1344             ds, tx->tx_txg));
1345 
1346         dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1347 
1348         /*
1349          * The origin's ds_creation_txg has to be < TXG_INITIAL
1350          */
1351         if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1352                 crtxg = 1;
1353         else
1354                 crtxg = tx->tx_txg;
1355 
1356         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1357             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1358         VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1359         dmu_buf_will_dirty(dbuf, tx);
1360         dsphys = dbuf->db_data;
1361         bzero(dsphys, sizeof (dsl_dataset_phys_t));
1362         dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1363         dsphys->ds_fsid_guid = unique_create();
1364         (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1365             sizeof (dsphys->ds_guid));
1366         dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1367         dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1368         dsphys->ds_next_snap_obj = ds->ds_object;
1369         dsphys->ds_num_children = 1;
1370         dsphys->ds_creation_time = gethrestime_sec();
1371         dsphys->ds_creation_txg = crtxg;
1372         dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1373         dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1374         dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1375         dsphys->ds_uncompressed_bytes =
1376             dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1377         dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1378         rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1379         dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1380         rrw_exit(&ds->ds_bp_rwlock, FTAG);
1381         dmu_buf_rele(dbuf, FTAG);
1382 
1383         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1384                 if (ds->ds_feature_inuse[f])
1385                         dsl_dataset_activate_feature(dsobj, f, tx);
1386         }
1387 
1388         ASSERT3U(ds->ds_prev != 0, ==,
1389             dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1390         if (ds->ds_prev) {
1391                 uint64_t next_clones_obj =
1392                     dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1393                 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1394                     ds->ds_object ||
1395                     dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1396                 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1397                     ds->ds_object) {
1398                         dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1399                         ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1400                             dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1401                         dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1402                 } else if (next_clones_obj != 0) {
1403                         dsl_dataset_remove_from_next_clones(ds->ds_prev,
1404                             dsphys->ds_next_snap_obj, tx);
1405                         VERIFY0(zap_add_int(mos,
1406                             next_clones_obj, dsobj, tx));
1407                 }
1408         }
1409 
1410         /*
1411          * If we have a reference-reservation on this dataset, we will
1412          * need to increase the amount of refreservation being charged
1413          * since our unique space is going to zero.
1414          */
1415         if (ds->ds_reserved) {
1416                 int64_t delta;
1417                 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1418                 delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1419                     ds->ds_reserved);
1420                 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1421                     delta, 0, 0, tx);
1422         }
1423 
1424         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1425         dsl_dataset_phys(ds)->ds_deadlist_obj =
1426             dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1427             dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1428         dsl_deadlist_close(&ds->ds_deadlist);
1429         dsl_deadlist_open(&ds->ds_deadlist, mos,
1430             dsl_dataset_phys(ds)->ds_deadlist_obj);
1431         dsl_deadlist_add_key(&ds->ds_deadlist,
1432             dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1433 
1434         ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1435         dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1436         dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1437         unique_bytes = dsl_dataset_phys(ds)->ds_unique_bytes;
1438         dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1439         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1440                 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1441 
1442         VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1443             snapname, 8, 1, &dsobj, tx));
1444 
1445         if (ds->ds_prev)
1446                 dsl_dataset_rele(ds->ds_prev, ds);
1447         VERIFY0(dsl_dataset_hold_obj(dp,
1448             dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1449 
1450         dsl_scan_ds_snapshotted(ds, tx);
1451 
1452         dsl_dir_snap_cmtime_update(ds->ds_dir);
1453 
1454         spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1455 
1456         if (autosnap_check_name(snapname)) {
1457                 autosnap_create_cb(spa_get_autosnap(dp->dp_spa),
1458                     ds, snapname, tx->tx_txg);
1459         }
1460 
1461         if (os == NULL)
1462                 VERIFY0(dmu_objset_from_ds(ds, &os));
1463 
1464         if (os->os_wbc_mode != ZFS_WBC_MODE_OFF)
1465                 wbc_add_bytes(dp->dp_spa, tx->tx_txg, unique_bytes);
1466 }
1467 
1468 void
1469 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1470 {
1471         dsl_dataset_snapshot_arg_t *ddsa = arg;
1472         dsl_pool_t *dp = dmu_tx_pool(tx);
1473         nvpair_t *pair;
1474 
1475         for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1476             pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1477                 dsl_dataset_t *ds;
1478                 char *name, *atp;
1479                 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1480 
1481                 name = nvpair_name(pair);
1482 
1483                 atp = strchr(name, '@');
1484                 (void) strlcpy(dsname, name, atp - name + 1);
1485                 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1486 
1487                 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1488                 if (ddsa->ddsa_props != NULL) {
1489                         dsl_props_set_sync_impl(ds->ds_prev,
1490                             ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1491                 }
1492                 dsl_dataset_rele(ds, FTAG);
1493         }
1494 }
1495 
1496 /*
1497  * The snapshots must all be in the same pool.
1498  * All-or-nothing: if there are any failures, nothing will be modified.
1499  */
1500 int
1501 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1502 {
1503         dsl_dataset_snapshot_arg_t ddsa;
1504         nvpair_t *pair;
1505         boolean_t needsuspend;
1506         int error;
1507         spa_t *spa;
1508         char *firstname;
1509         nvlist_t *suspended = NULL;
1510 
1511         pair = nvlist_next_nvpair(snaps, NULL);
1512         if (pair == NULL)
1513                 return (0);
1514         firstname = nvpair_name(pair);
1515 
1516         error = spa_open(firstname, &spa, FTAG);
1517         if (error != 0)
1518                 return (error);
1519         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1520         spa_close(spa, FTAG);
1521 
1522         if (needsuspend) {
1523                 suspended = fnvlist_alloc();
1524                 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1525                     pair = nvlist_next_nvpair(snaps, pair)) {
1526                         char fsname[ZFS_MAX_DATASET_NAME_LEN];
1527                         char *snapname = nvpair_name(pair);
1528                         char *atp;
1529                         void *cookie;
1530 
1531                         atp = strchr(snapname, '@');
1532                         if (atp == NULL) {
1533                                 error = SET_ERROR(EINVAL);
1534                                 break;
1535                         }
1536                         (void) strlcpy(fsname, snapname, atp - snapname + 1);
1537 
1538                         error = zil_suspend(fsname, &cookie);
1539                         if (error != 0)
1540                                 break;
1541                         fnvlist_add_uint64(suspended, fsname,
1542                             (uintptr_t)cookie);
1543                 }
1544         }
1545 
1546         ddsa.ddsa_snaps = snaps;
1547         ddsa.ddsa_props = props;
1548         ddsa.ddsa_errors = errors;
1549         ddsa.ddsa_cr = CRED();
1550         ddsa.ddsa_autosnap = B_FALSE;
1551 
1552         if (error == 0) {
1553                 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1554                     dsl_dataset_snapshot_sync, &ddsa,
1555                     fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1556         }
1557 
1558         if (suspended != NULL) {
1559                 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1560                     pair = nvlist_next_nvpair(suspended, pair)) {
1561                         zil_resume((void *)(uintptr_t)
1562                             fnvpair_value_uint64(pair));
1563                 }
1564                 fnvlist_free(suspended);
1565         }
1566 
1567         return (error);
1568 }
1569 
1570 typedef struct dsl_dataset_snapshot_tmp_arg {
1571         const char *ddsta_fsname;
1572         const char *ddsta_snapname;
1573         minor_t ddsta_cleanup_minor;
1574         const char *ddsta_htag;
1575 } dsl_dataset_snapshot_tmp_arg_t;
1576 
1577 static int
1578 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1579 {
1580         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1581         dsl_pool_t *dp = dmu_tx_pool(tx);
1582         dsl_dataset_t *ds;
1583         int error;
1584 
1585         error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1586         if (error != 0)
1587                 return (error);
1588 
1589         /* NULL cred means no limit check for tmp snapshot */
1590         error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1591             tx, B_FALSE, 0, NULL);
1592         if (error != 0) {
1593                 dsl_dataset_rele(ds, FTAG);
1594                 return (error);
1595         }
1596 
1597         if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1598                 dsl_dataset_rele(ds, FTAG);
1599                 return (SET_ERROR(ENOTSUP));
1600         }
1601         error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1602             B_TRUE, tx);
1603         if (error != 0) {
1604                 dsl_dataset_rele(ds, FTAG);
1605                 return (error);
1606         }
1607 
1608         dsl_dataset_rele(ds, FTAG);
1609         return (0);
1610 }
1611 
1612 static void
1613 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1614 {
1615         dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1616         dsl_pool_t *dp = dmu_tx_pool(tx);
1617         dsl_dataset_t *ds;
1618 
1619         VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1620 
1621         dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1622         dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1623             ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1624         dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1625 
1626         dsl_dataset_rele(ds, FTAG);
1627 }
1628 
1629 int
1630 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1631     minor_t cleanup_minor, const char *htag)
1632 {
1633         dsl_dataset_snapshot_tmp_arg_t ddsta;
1634         int error;
1635         spa_t *spa;
1636         boolean_t needsuspend;
1637         void *cookie;
1638 
1639         ddsta.ddsta_fsname = fsname;
1640         ddsta.ddsta_snapname = snapname;
1641         ddsta.ddsta_cleanup_minor = cleanup_minor;
1642         ddsta.ddsta_htag = htag;
1643 
1644         error = spa_open(fsname, &spa, FTAG);
1645         if (error != 0)
1646                 return (error);
1647         needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1648         spa_close(spa, FTAG);
1649 
1650         if (needsuspend) {
1651                 error = zil_suspend(fsname, &cookie);
1652                 if (error != 0)
1653                         return (error);
1654         }
1655 
1656         error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1657             dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
1658 
1659         if (needsuspend)
1660                 zil_resume(cookie);
1661         return (error);
1662 }
1663 
1664 void
1665 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1666 {
1667         ASSERT(dmu_tx_is_syncing(tx));
1668         ASSERT(ds->ds_objset != NULL);
1669         ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
1670 
1671         /*
1672          * in case we had to change ds_fsid_guid when we opened it,
1673          * sync it out now.
1674          */
1675         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1676         dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
1677 
1678         if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
1679                 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1680                     ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
1681                     &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
1682                 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1683                     ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
1684                     &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
1685                 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1686                     ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
1687                     &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
1688                 ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
1689                 ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
1690                 ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
1691         }
1692 
1693         dmu_objset_sync(ds->ds_objset, zio, tx);
1694 
1695         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1696                 if (ds->ds_feature_activation_needed[f]) {
1697                         if (ds->ds_feature_inuse[f])
1698                                 continue;
1699                         dsl_dataset_activate_feature(ds->ds_object, f, tx);
1700                         ds->ds_feature_inuse[f] = B_TRUE;
1701                 }
1702         }
1703 }
1704 
1705 static int
1706 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1707 {
1708         dsl_deadlist_t *dl = arg;
1709         dsl_deadlist_insert(dl, bp, tx);
1710         return (0);
1711 }
1712 
1713 void
1714 dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
1715 {
1716         objset_t *os = ds->ds_objset;
1717 
1718         bplist_iterate(&ds->ds_pending_deadlist,
1719             deadlist_enqueue_cb, &ds->ds_deadlist, tx);
1720 
1721         if (os->os_synced_dnodes != NULL) {
1722                 multilist_destroy(os->os_synced_dnodes);
1723                 os->os_synced_dnodes = NULL;
1724         }
1725 
1726         ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
1727 
1728         dmu_buf_rele(ds->ds_dbuf, ds);
1729 }
1730 
1731 int
1732 get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val)
1733 {
1734         uint64_t count = 0;
1735         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1736         zap_cursor_t zc;
1737         zap_attribute_t za;
1738 
1739         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1740 
1741         /*
1742          * There may be missing entries in ds_next_clones_obj
1743          * due to a bug in a previous version of the code.
1744          * Only trust it if it has the right number of entries.
1745          */
1746         if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1747                 VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1748                     &count));
1749         }
1750         if (count != dsl_dataset_phys(ds)->ds_num_children - 1) {
1751                 return (ENOENT);
1752         }
1753         for (zap_cursor_init(&zc, mos,
1754             dsl_dataset_phys(ds)->ds_next_clones_obj);
1755             zap_cursor_retrieve(&zc, &za) == 0;
1756             zap_cursor_advance(&zc)) {
1757                 dsl_dataset_t *clone;
1758                 char buf[ZFS_MAX_DATASET_NAME_LEN];
1759                 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1760                     za.za_first_integer, FTAG, &clone));
1761                 dsl_dir_name(clone->ds_dir, buf);
1762                 fnvlist_add_boolean(val, buf);
1763                 dsl_dataset_rele(clone, FTAG);
1764         }
1765         zap_cursor_fini(&zc);
1766         return (0);
1767 }
1768 
1769 void
1770 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1771 {
1772         nvlist_t *propval = fnvlist_alloc();
1773         nvlist_t *val;
1774 
1775         /*
1776          * We use nvlist_alloc() instead of fnvlist_alloc() because the
1777          * latter would allocate the list with NV_UNIQUE_NAME flag.
1778          * As a result, every time a clone name is appended to the list
1779          * it would be (linearly) searched for for a duplicate name.
1780          * We already know that all clone names must be unique and we
1781          * want avoid the quadratic complexity of double-checking that
1782          * because we can have a large number of clones.
1783          */
1784         VERIFY0(nvlist_alloc(&val, 0, KM_SLEEP));
1785 
1786         if (get_clones_stat_impl(ds, val) == 0) {
1787                 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1788                 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
1789                     propval);
1790         }
1791 
1792         nvlist_free(val);
1793         nvlist_free(propval);
1794 }
1795 
1796 /*
1797  * Returns a string that represents the receive resume stats token. It should
1798  * be freed with strfree().
1799  */
1800 char *
1801 get_receive_resume_stats_impl(dsl_dataset_t *ds)
1802 {
1803         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1804 
1805         if (dsl_dataset_has_resume_receive_state(ds)) {
1806                 char *str;
1807                 void *packed;
1808                 uint8_t *compressed;
1809                 uint64_t val;
1810                 nvlist_t *token_nv = fnvlist_alloc();
1811                 size_t packed_size, compressed_size;
1812 
1813                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1814                     DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
1815                         fnvlist_add_uint64(token_nv, "fromguid", val);
1816                 }
1817                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1818                     DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
1819                         fnvlist_add_uint64(token_nv, "object", val);
1820                 }
1821                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1822                     DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
1823                         fnvlist_add_uint64(token_nv, "offset", val);
1824                 }
1825                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1826                     DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
1827                         fnvlist_add_uint64(token_nv, "bytes", val);
1828                 }
1829                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1830                     DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
1831                         fnvlist_add_uint64(token_nv, "toguid", val);
1832                 }
1833                 char buf[256];
1834                 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1835                     DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
1836                         fnvlist_add_string(token_nv, "toname", buf);
1837                 }
1838                 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1839                     DS_FIELD_RESUME_LARGEBLOCK) == 0) {
1840                         fnvlist_add_boolean(token_nv, "largeblockok");
1841                 }
1842                 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1843                     DS_FIELD_RESUME_EMBEDOK) == 0) {
1844                         fnvlist_add_boolean(token_nv, "embedok");
1845                 }
1846                 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1847                     DS_FIELD_RESUME_COMPRESSOK) == 0) {
1848                         fnvlist_add_boolean(token_nv, "compressok");
1849                 }
1850                 packed = fnvlist_pack(token_nv, &packed_size);
1851                 fnvlist_free(token_nv);
1852                 compressed = kmem_alloc(packed_size, KM_SLEEP);
1853 
1854                 compressed_size = gzip_compress(packed, compressed,
1855                     packed_size, packed_size, 6);
1856 
1857                 zio_cksum_t cksum;
1858                 fletcher_4_native(compressed, compressed_size, NULL, &cksum);
1859 
1860                 str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
1861                 for (int i = 0; i < compressed_size; i++) {
1862                         (void) sprintf(str + i * 2, "%02x", compressed[i]);
1863                 }
1864                 str[compressed_size * 2] = '\0';
1865                 char *propval = kmem_asprintf("%u-%llx-%llx-%s",
1866                     ZFS_SEND_RESUME_TOKEN_VERSION,
1867                     (longlong_t)cksum.zc_word[0],
1868                     (longlong_t)packed_size, str);
1869                 kmem_free(packed, packed_size);
1870                 kmem_free(str, compressed_size * 2 + 1);
1871                 kmem_free(compressed, packed_size);
1872                 return (propval);
1873         }
1874         return (strdup(""));
1875 }
1876 
1877 /*
1878  * Returns a string that represents the receive resume stats token of the
1879  * dataset's child. It should be freed with strfree().
1880  */
1881 char *
1882 get_child_receive_stats(dsl_dataset_t *ds)
1883 {
1884         char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1885         dsl_dataset_t *recv_ds;
1886         dsl_dataset_name(ds, recvname);
1887         if (strlcat(recvname, "/", sizeof (recvname)) <
1888             sizeof (recvname) &&
1889             strlcat(recvname, recv_clone_name, sizeof (recvname)) <
1890             sizeof (recvname) &&
1891             dsl_dataset_hold(ds->ds_dir->dd_pool, recvname, FTAG,
1892             &recv_ds)  == 0) {
1893                 char *propval = get_receive_resume_stats_impl(recv_ds);
1894                 dsl_dataset_rele(recv_ds, FTAG);
1895                 return (propval);
1896         }
1897         return (strdup(""));
1898 }
1899 
1900 static void
1901 get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
1902 {
1903         char *propval = get_receive_resume_stats_impl(ds);
1904         if (strcmp(propval, "") != 0) {
1905                 dsl_prop_nvlist_add_string(nv,
1906                     ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
1907         } else {
1908                 char *childval = get_child_receive_stats(ds);
1909                 if (strcmp(childval, "") != 0) {
1910                         dsl_prop_nvlist_add_string(nv,
1911                             ZFS_PROP_RECEIVE_RESUME_TOKEN, childval);
1912                 }
1913                 strfree(childval);
1914         }
1915         strfree(propval);
1916 }
1917 
1918 uint64_t
1919 dsl_get_refratio(dsl_dataset_t *ds)
1920 {
1921         uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1922             (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1923             dsl_dataset_phys(ds)->ds_compressed_bytes);
1924         return (ratio);
1925 }
1926 
1927 uint64_t
1928 dsl_get_logicalreferenced(dsl_dataset_t *ds)
1929 {
1930         return (dsl_dataset_phys(ds)->ds_uncompressed_bytes);
1931 }
1932 
1933 uint64_t
1934 dsl_get_compressratio(dsl_dataset_t *ds)
1935 {
1936         if (ds->ds_is_snapshot) {
1937                 return (dsl_get_refratio(ds));
1938         } else {
1939                 dsl_dir_t *dd = ds->ds_dir;
1940                 mutex_enter(&dd->dd_lock);
1941                 uint64_t val = dsl_dir_get_compressratio(dd);
1942                 mutex_exit(&dd->dd_lock);
1943                 return (val);
1944         }
1945 }
1946 
1947 uint64_t
1948 dsl_get_used(dsl_dataset_t *ds)
1949 {
1950         if (ds->ds_is_snapshot) {
1951                 return (dsl_dataset_phys(ds)->ds_unique_bytes);
1952         } else {
1953                 dsl_dir_t *dd = ds->ds_dir;
1954                 mutex_enter(&dd->dd_lock);
1955                 uint64_t val = dsl_dir_get_used(dd);
1956                 mutex_exit(&dd->dd_lock);
1957                 return (val);
1958         }
1959 }
1960 
1961 uint64_t
1962 dsl_get_creation(dsl_dataset_t *ds)
1963 {
1964         return (dsl_dataset_phys(ds)->ds_creation_time);
1965 }
1966 
1967 uint64_t
1968 dsl_get_creationtxg(dsl_dataset_t *ds)
1969 {
1970         return (dsl_dataset_phys(ds)->ds_creation_txg);
1971 }
1972 
1973 uint64_t
1974 dsl_get_refquota(dsl_dataset_t *ds)
1975 {
1976         return (ds->ds_quota);
1977 }
1978 
1979 uint64_t
1980 dsl_get_refreservation(dsl_dataset_t *ds)
1981 {
1982         return (ds->ds_reserved);
1983 }
1984 
1985 uint64_t
1986 dsl_get_guid(dsl_dataset_t *ds)
1987 {
1988         return (dsl_dataset_phys(ds)->ds_guid);
1989 }
1990 
1991 uint64_t
1992 dsl_get_unique(dsl_dataset_t *ds)
1993 {
1994         return (dsl_dataset_phys(ds)->ds_unique_bytes);
1995 }
1996 
1997 uint64_t
1998 dsl_get_objsetid(dsl_dataset_t *ds)
1999 {
2000         return (ds->ds_object);
2001 }
2002 
2003 uint64_t
2004 dsl_get_userrefs(dsl_dataset_t *ds)
2005 {
2006         return (ds->ds_userrefs);
2007 }
2008 
2009 uint64_t
2010 dsl_get_defer_destroy(dsl_dataset_t *ds)
2011 {
2012         return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2013 }
2014 
2015 uint64_t
2016 dsl_get_referenced(dsl_dataset_t *ds)
2017 {
2018         return (dsl_dataset_phys(ds)->ds_referenced_bytes);
2019 }
2020 
2021 uint64_t
2022 dsl_get_numclones(dsl_dataset_t *ds)
2023 {
2024         ASSERT(ds->ds_is_snapshot);
2025         return (dsl_dataset_phys(ds)->ds_num_children - 1);
2026 }
2027 
2028 uint64_t
2029 dsl_get_inconsistent(dsl_dataset_t *ds)
2030 {
2031         return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ?
2032             1 : 0);
2033 }
2034 
2035 uint64_t
2036 dsl_get_available(dsl_dataset_t *ds)
2037 {
2038         uint64_t refdbytes = dsl_get_referenced(ds);
2039         uint64_t availbytes = dsl_dir_space_available(ds->ds_dir,
2040             NULL, 0, TRUE);
2041         if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
2042                 availbytes +=
2043                     ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2044         }
2045         if (ds->ds_quota != 0) {
2046                 /*
2047                  * Adjust available bytes according to refquota
2048                  */
2049                 if (refdbytes < ds->ds_quota) {
2050                         availbytes = MIN(availbytes,
2051                             ds->ds_quota - refdbytes);
2052                 } else {
2053                         availbytes = 0;
2054                 }
2055         }
2056         return (availbytes);
2057 }
2058 
2059 int
2060 dsl_get_written(dsl_dataset_t *ds, uint64_t *written)
2061 {
2062         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2063         dsl_dataset_t *prev;
2064         int err = dsl_dataset_hold_obj(dp,
2065             dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2066         if (err == 0) {
2067                 uint64_t comp, uncomp;
2068                 err = dsl_dataset_space_written(prev, ds, written,
2069                     &comp, &uncomp);
2070                 dsl_dataset_rele(prev, FTAG);
2071         }
2072         return (err);
2073 }
2074 
2075 /*
2076  * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2077  */
2078 int
2079 dsl_get_prev_snap(dsl_dataset_t *ds, char *snap)
2080 {
2081         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2082         if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
2083                 dsl_dataset_name(ds->ds_prev, snap);
2084                 return (0);
2085         } else {
2086                 return (ENOENT);
2087         }
2088 }
2089 
2090 /*
2091  * Returns the mountpoint property and source for the given dataset in the value
2092  * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2093  * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2094  * Returns 0 on success and an error on failure.
2095  */
2096 int
2097 dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value,
2098     char *source)
2099 {
2100         int error;
2101         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2102 
2103         /* Retrieve the mountpoint value stored in the zap opbject */
2104         error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1,
2105             ZAP_MAXVALUELEN, value, source);
2106         if (error != 0) {
2107                 return (error);
2108         }
2109 
2110         /* Process the dsname and source to find the full mountpoint string */
2111         if (value[0] == '/') {
2112                 char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
2113                 char *root = buf;
2114                 const char *relpath;
2115 
2116                 /*
2117                  * If we inherit the mountpoint, even from a dataset
2118                  * with a received value, the source will be the path of
2119                  * the dataset we inherit from. If source is
2120                  * ZPROP_SOURCE_VAL_RECVD, the received value is not
2121                  * inherited.
2122                  */
2123                 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2124                         relpath = "";
2125                 } else {
2126                         ASSERT0(strncmp(dsname, source, strlen(source)));
2127                         relpath = dsname + strlen(source);
2128                         if (relpath[0] == '/')
2129                                 relpath++;
2130                 }
2131 
2132                 spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN);
2133 
2134                 /*
2135                  * Special case an alternate root of '/'. This will
2136                  * avoid having multiple leading slashes in the
2137                  * mountpoint path.
2138                  */
2139                 if (strcmp(root, "/") == 0)
2140                         root++;
2141 
2142                 /*
2143                  * If the mountpoint is '/' then skip over this
2144                  * if we are obtaining either an alternate root or
2145                  * an inherited mountpoint.
2146                  */
2147                 char *mnt = value;
2148                 if (value[1] == '\0' && (root[0] != '\0' ||
2149                     relpath[0] != '\0'))
2150                         mnt = value + 1;
2151 
2152                 if (relpath[0] == '\0') {
2153                         (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s",
2154                             root, mnt);
2155                 } else {
2156                         (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s",
2157                             root, mnt, relpath[0] == '@' ? "" : "/",
2158                             relpath);
2159                 }
2160                 kmem_free(buf, ZAP_MAXVALUELEN);
2161         } else {
2162                 /* 'legacy' or 'none' */
2163                 (void) snprintf(value, ZAP_MAXVALUELEN, "%s", value);
2164         }
2165         return (0);
2166 }
2167 
2168 void
2169 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2170 {
2171         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2172 
2173         ASSERT(dsl_pool_config_held(dp));
2174 
2175         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO,
2176             dsl_get_refratio(ds));
2177         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
2178             dsl_get_logicalreferenced(ds));
2179         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
2180             dsl_get_compressratio(ds));
2181         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2182             dsl_get_used(ds));
2183 
2184         if (ds->ds_is_snapshot) {
2185                 dsl_dataset_t *hds = NULL;
2186                 boolean_t modified = B_FALSE;
2187 
2188                 if (dsl_dataset_hold_obj(dp,
2189                     dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
2190                     FTAG, &hds) == 0) {
2191                         modified = dsl_dataset_modified_since_snap(hds, ds);
2192                         dsl_dataset_rele(hds, FTAG);
2193                 }
2194 
2195                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_MODIFIED,
2196                     modified ? 1 : 0);
2197 
2198                 get_clones_stat(ds, nv);
2199         } else {
2200                 char buf[ZFS_MAX_DATASET_NAME_LEN];
2201                 if (dsl_get_prev_snap(ds, buf) == 0)
2202                         dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP,
2203                             buf);
2204                 dsl_dir_stats(ds->ds_dir, nv);
2205         }
2206 
2207         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
2208             dsl_get_available(ds));
2209         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
2210             dsl_get_referenced(ds));
2211         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2212             dsl_get_creation(ds));
2213         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2214             dsl_get_creationtxg(ds));
2215         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2216             dsl_get_refquota(ds));
2217         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2218             dsl_get_refreservation(ds));
2219         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2220             dsl_get_guid(ds));
2221         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2222             dsl_get_unique(ds));
2223         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2224             dsl_get_objsetid(ds));
2225         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2226             dsl_get_userrefs(ds));
2227         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2228             dsl_get_defer_destroy(ds));
2229 
2230         if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2231                 uint64_t written;
2232                 if (dsl_get_written(ds, &written) == 0) {
2233                         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2234                             written);
2235                 }
2236         }
2237 
2238         if (!dsl_dataset_is_snapshot(ds)) {
2239                 /*
2240                  * A failed "newfs" (e.g. full) resumable receive leaves
2241                  * the stats set on this dataset.  Check here for the prop.
2242                  */
2243                 get_receive_resume_stats(ds, nv);
2244 
2245                 /*
2246                  * A failed incremental resumable receive leaves the
2247                  * stats set on our child named "%recv".  Check the child
2248                  * for the prop.
2249                  */
2250                 /* 6 extra bytes for /%recv */
2251                 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
2252                 dsl_dataset_t *recv_ds;
2253                 dsl_dataset_name(ds, recvname);
2254                 if (strlcat(recvname, "/", sizeof (recvname)) <
2255                     sizeof (recvname) &&
2256                     strlcat(recvname, recv_clone_name, sizeof (recvname)) <
2257                     sizeof (recvname) &&
2258                     dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
2259                         get_receive_resume_stats(recv_ds, nv);
2260                         dsl_dataset_rele(recv_ds, FTAG);
2261                 }
2262         }
2263 }
2264 
2265 void
2266 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2267 {
2268         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2269         ASSERT(dsl_pool_config_held(dp));
2270 
2271         stat->dds_creation_txg = dsl_get_creationtxg(ds);
2272         stat->dds_inconsistent = dsl_get_inconsistent(ds);
2273         stat->dds_guid = dsl_get_guid(ds);
2274         stat->dds_origin[0] = '\0';
2275         stat->dds_is_snapshot = B_FALSE;
2276         stat->dds_is_autosnapshot = B_FALSE;
2277         if (ds->ds_is_snapshot) {
2278                 if (autosnap_is_autosnap(ds))
2279                         stat->dds_is_autosnapshot = B_TRUE;
2280                 else
2281                         stat->dds_is_snapshot = B_TRUE;
2282 
2283                 stat->dds_num_clones = dsl_get_numclones(ds);
2284         } else {
2285                 stat->dds_num_clones = 0;
2286 
2287                 if (dsl_dir_is_clone(ds->ds_dir)) {
2288                         dsl_dir_get_origin(ds->ds_dir, stat->dds_origin);
2289                 }
2290         }
2291 }
2292 
2293 uint64_t
2294 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2295 {
2296         return (ds->ds_fsid_guid);
2297 }
2298 
2299 void
2300 dsl_dataset_space(dsl_dataset_t *ds,
2301     uint64_t *refdbytesp, uint64_t *availbytesp,
2302     uint64_t *usedobjsp, uint64_t *availobjsp)
2303 {
2304         *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2305         *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2306         if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2307                 *availbytesp +=
2308                     ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2309         if (ds->ds_quota != 0) {
2310                 /*
2311                  * Adjust available bytes according to refquota
2312                  */
2313                 if (*refdbytesp < ds->ds_quota)
2314                         *availbytesp = MIN(*availbytesp,
2315                             ds->ds_quota - *refdbytesp);
2316                 else
2317                         *availbytesp = 0;
2318         }
2319         rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2320         *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2321         rrw_exit(&ds->ds_bp_rwlock, FTAG);
2322         *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2323 }
2324 
2325 boolean_t
2326 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2327 {
2328         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2329         uint64_t birth;
2330 
2331         ASSERT(dsl_pool_config_held(dp));
2332         if (snap == NULL)
2333                 return (B_FALSE);
2334         rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2335         birth = dsl_dataset_get_blkptr(ds)->blk_birth;
2336         rrw_exit(&ds->ds_bp_rwlock, FTAG);
2337         if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
2338                 objset_t *os, *os_snap;
2339                 /*
2340                  * It may be that only the ZIL differs, because it was
2341                  * reset in the head.  Don't count that as being
2342                  * modified.
2343                  */
2344                 if (dmu_objset_from_ds(ds, &os) != 0)
2345                         return (B_TRUE);
2346                 if (dmu_objset_from_ds(snap, &os_snap) != 0)
2347                         return (B_TRUE);
2348                 return (bcmp(&os->os_phys->os_meta_dnode,
2349                     &os_snap->os_phys->os_meta_dnode,
2350                     sizeof (os->os_phys->os_meta_dnode)) != 0);
2351         }
2352         return (B_FALSE);
2353 }
2354 
2355 typedef struct dsl_dataset_rename_snapshot_arg {
2356         const char *ddrsa_fsname;
2357         const char *ddrsa_oldsnapname;
2358         const char *ddrsa_newsnapname;
2359         boolean_t ddrsa_recursive;
2360         dmu_tx_t *ddrsa_tx;
2361 } dsl_dataset_rename_snapshot_arg_t;
2362 
2363 /* ARGSUSED */
2364 static int
2365 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
2366     dsl_dataset_t *hds, void *arg)
2367 {
2368         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2369         int error;
2370         uint64_t val;
2371 
2372         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2373         if (error != 0) {
2374                 /* ignore nonexistent snapshots */
2375                 return (error == ENOENT ? 0 : error);
2376         }
2377 
2378         /* new name should not exist */
2379         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
2380         if (error == 0)
2381                 error = SET_ERROR(EEXIST);
2382         else if (error == ENOENT)
2383                 error = 0;
2384 
2385         /* dataset name + 1 for the "@" + the new snapshot name must fit */
2386         if (dsl_dir_namelen(hds->ds_dir) + 1 +
2387             strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2388                 error = SET_ERROR(ENAMETOOLONG);
2389 
2390         return (error);
2391 }
2392 
2393 static int
2394 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
2395 {
2396         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2397         dsl_pool_t *dp = dmu_tx_pool(tx);
2398         dsl_dataset_t *hds;
2399         int error;
2400 
2401         /* You cannot rename an autosnapshot */
2402         if (autosnap_check_name(ddrsa->ddrsa_oldsnapname))
2403                 return (SET_ERROR(EPERM));
2404 
2405         /* New name cannot match the AUTOSNAP prefix */
2406         if (autosnap_check_name(ddrsa->ddrsa_newsnapname))
2407                 return (SET_ERROR(EPERM));
2408 
2409         error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
2410         if (error != 0)
2411                 return (error);
2412 
2413         if (ddrsa->ddrsa_recursive) {
2414                 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2415                     dsl_dataset_rename_snapshot_check_impl, ddrsa,
2416                     DS_FIND_CHILDREN);
2417         } else {
2418                 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
2419         }
2420         dsl_dataset_rele(hds, FTAG);
2421         return (error);
2422 }
2423 
2424 static int
2425 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
2426     dsl_dataset_t *hds, void *arg)
2427 {
2428         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2429         dsl_dataset_t *ds;
2430         uint64_t val;
2431         dmu_tx_t *tx = ddrsa->ddrsa_tx;
2432         int error;
2433 
2434         error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2435         ASSERT(error == 0 || error == ENOENT);
2436         if (error == ENOENT) {
2437                 /* ignore nonexistent snapshots */
2438                 return (0);
2439         }
2440 
2441         VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
2442 
2443         /* log before we change the name */
2444         spa_history_log_internal_ds(ds, "rename", tx,
2445             "-> @%s", ddrsa->ddrsa_newsnapname);
2446 
2447         VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2448             B_FALSE));
2449         mutex_enter(&ds->ds_lock);
2450         (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
2451         mutex_exit(&ds->ds_lock);
2452         VERIFY0(zap_add(dp->dp_meta_objset,
2453             dsl_dataset_phys(hds)->ds_snapnames_zapobj,
2454             ds->ds_snapname, 8, 1, &ds->ds_object, tx));
2455 
2456         dsl_dataset_rele(ds, FTAG);
2457         return (0);
2458 }
2459 
2460 static void
2461 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
2462 {
2463         dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2464         dsl_pool_t *dp = dmu_tx_pool(tx);
2465         dsl_dataset_t *hds;
2466 
2467         VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
2468         ddrsa->ddrsa_tx = tx;
2469         if (ddrsa->ddrsa_recursive) {
2470                 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2471                     dsl_dataset_rename_snapshot_sync_impl, ddrsa,
2472                     DS_FIND_CHILDREN));
2473         } else {
2474                 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
2475         }
2476         dsl_dataset_rele(hds, FTAG);
2477 }
2478 
2479 int
2480 dsl_dataset_rename_snapshot(const char *fsname,
2481     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
2482 {
2483         dsl_dataset_rename_snapshot_arg_t ddrsa;
2484 
2485         ddrsa.ddrsa_fsname = fsname;
2486         ddrsa.ddrsa_oldsnapname = oldsnapname;
2487         ddrsa.ddrsa_newsnapname = newsnapname;
2488         ddrsa.ddrsa_recursive = recursive;
2489 
2490         return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
2491             dsl_dataset_rename_snapshot_sync, &ddrsa,
2492             1, ZFS_SPACE_CHECK_RESERVED));
2493 }
2494 
2495 /*
2496  * If we're doing an ownership handoff, we need to make sure that there is
2497  * only one long hold on the dataset.  We're not allowed to change anything here
2498  * so we don't permanently release the long hold or regular hold here.  We want
2499  * to do this only when syncing to avoid the dataset unexpectedly going away
2500  * when we release the long hold.
2501  */
2502 static int
2503 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
2504 {
2505         boolean_t held;
2506 
2507         if (!dmu_tx_is_syncing(tx))
2508                 return (0);
2509 
2510         if (owner != NULL) {
2511                 VERIFY3P(ds->ds_owner, ==, owner);
2512                 dsl_dataset_long_rele(ds, owner);
2513         }
2514 
2515         held = dsl_dataset_long_held(ds);
2516 
2517         if (owner != NULL)
2518                 dsl_dataset_long_hold(ds, owner);
2519 
2520         if (held)
2521                 return (SET_ERROR(EBUSY));
2522 
2523         return (0);
2524 }
2525 
2526 int
2527 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
2528 {
2529         dsl_dataset_rollback_arg_t *ddra = arg;
2530         dsl_pool_t *dp = dmu_tx_pool(tx);
2531         dsl_dataset_t *ds;
2532         int64_t unused_refres_delta;
2533         int error;
2534 
2535         error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
2536         if (error != 0)
2537                 return (error);
2538 
2539         /* must not be a snapshot */
2540         if (ds->ds_is_snapshot) {
2541                 dsl_dataset_rele(ds, FTAG);
2542                 return (SET_ERROR(EINVAL));
2543         }
2544 
2545         /* must have a most recent snapshot */
2546         if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
2547                 dsl_dataset_rele(ds, FTAG);
2548                 return (SET_ERROR(ESRCH));
2549         }
2550 
2551         /*
2552          * No rollback to a snapshot created in the current txg, because
2553          * the rollback may dirty the dataset and create blocks that are
2554          * not reachable from the rootbp while having a birth txg that
2555          * falls into the snapshot's range.
2556          */
2557         if (dmu_tx_is_syncing(tx) &&
2558             dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
2559                 dsl_dataset_rele(ds, FTAG);
2560                 return (SET_ERROR(EAGAIN));
2561         }
2562 
2563         /*
2564          * If the expected target snapshot is specified, then check that
2565          * the latest snapshot is it.
2566          */
2567         if (ddra->ddra_tosnap != NULL) {
2568                 dsl_dataset_t *snapds;
2569 
2570                 /* Check if the target snapshot exists at all. */
2571                 error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds);
2572                 if (error != 0) {
2573                         /*
2574                          * ESRCH is used to signal that the target snapshot does
2575                          * not exist, while ENOENT is used to report that
2576                          * the rolled back dataset does not exist.
2577                          * ESRCH is also used to cover other cases where the
2578                          * target snapshot is not related to the dataset being
2579                          * rolled back such as being in a different pool.
2580                          */
2581                         if (error == ENOENT || error == EXDEV)
2582                                 error = SET_ERROR(ESRCH);
2583                         dsl_dataset_rele(ds, FTAG);
2584                         return (error);
2585                 }
2586                 ASSERT(snapds->ds_is_snapshot);
2587 
2588                 /* Check if the snapshot is the latest snapshot indeed. */
2589                 if (snapds != ds->ds_prev) {
2590                         /*
2591                          * Distinguish between the case where the only problem
2592                          * is intervening snapshots (EEXIST) vs the snapshot
2593                          * not being a valid target for rollback (ESRCH).
2594                          */
2595                         if (snapds->ds_dir == ds->ds_dir ||
2596                             (dsl_dir_is_clone(ds->ds_dir) &&
2597                             dsl_dir_phys(ds->ds_dir)->dd_origin_obj ==
2598                             snapds->ds_object)) {
2599                                 error = SET_ERROR(EEXIST);
2600                         } else {
2601                                 error = SET_ERROR(ESRCH);
2602                         }
2603                         dsl_dataset_rele(snapds, FTAG);
2604                         dsl_dataset_rele(ds, FTAG);
2605                         return (error);
2606                 }
2607                 dsl_dataset_rele(snapds, FTAG);
2608         }
2609 
2610         /* must not have any bookmarks after the most recent snapshot */
2611         nvlist_t *proprequest = fnvlist_alloc();
2612         fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
2613         nvlist_t *bookmarks = fnvlist_alloc();
2614         error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
2615         fnvlist_free(proprequest);
2616         if (error != 0) {
2617                 dsl_dataset_rele(ds, FTAG);
2618                 return (error);
2619         }
2620         for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
2621             pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
2622                 nvlist_t *valuenv =
2623                     fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
2624                     zfs_prop_to_name(ZFS_PROP_CREATETXG));
2625                 uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2626                 if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
2627                         fnvlist_free(bookmarks);
2628                         dsl_dataset_rele(ds, FTAG);
2629                         return (SET_ERROR(EEXIST));
2630                 }
2631         }
2632         fnvlist_free(bookmarks);
2633 
2634         error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
2635         if (error != 0) {
2636                 dsl_dataset_rele(ds, FTAG);
2637                 return (error);
2638         }
2639 
2640         /*
2641          * Check if the snap we are rolling back to uses more than
2642          * the refquota.
2643          */
2644         if (ds->ds_quota != 0 &&
2645             dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
2646                 dsl_dataset_rele(ds, FTAG);
2647                 return (SET_ERROR(EDQUOT));
2648         }
2649 
2650         /*
2651          * When we do the clone swap, we will temporarily use more space
2652          * due to the refreservation (the head will no longer have any
2653          * unique space, so the entire amount of the refreservation will need
2654          * to be free).  We will immediately destroy the clone, freeing
2655          * this space, but the freeing happens over many txg's.
2656          */
2657         unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2658             dsl_dataset_phys(ds)->ds_unique_bytes);
2659 
2660         if (unused_refres_delta > 0 &&
2661             unused_refres_delta >
2662             dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
2663                 dsl_dataset_rele(ds, FTAG);
2664                 return (SET_ERROR(ENOSPC));
2665         }
2666 
2667         dsl_dataset_rele(ds, FTAG);
2668         return (0);
2669 }
2670 
2671 void
2672 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
2673 {
2674         dsl_dataset_rollback_arg_t *ddra = arg;
2675         dsl_pool_t *dp = dmu_tx_pool(tx);
2676         dsl_dataset_t *ds, *clone;
2677         uint64_t cloneobj;
2678         char namebuf[ZFS_MAX_DATASET_NAME_LEN];
2679 
2680         VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
2681 
2682         dsl_dataset_name(ds->ds_prev, namebuf);
2683         fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2684 
2685         cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
2686             ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
2687 
2688         VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
2689 
2690         dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
2691         dsl_dataset_zero_zil(ds, tx);
2692 
2693         dsl_destroy_head_sync_impl(clone, tx);
2694 
2695         dsl_dataset_rele(clone, FTAG);
2696         dsl_dataset_rele(ds, FTAG);
2697 }
2698 
2699 /*
2700  * Rolls back the given filesystem or volume to the most recent snapshot.
2701  * The name of the most recent snapshot will be returned under key "target"
2702  * in the result nvlist.
2703  *
2704  * If owner != NULL:
2705  * - The existing dataset MUST be owned by the specified owner at entry
2706  * - Upon return, dataset will still be held by the same owner, whether we
2707  *   succeed or not.
2708  *
2709  * This mode is required any time the existing filesystem is mounted.  See
2710  * notes above zfs_suspend_fs() for further details.
2711  */
2712 int
2713 dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner,
2714     nvlist_t *result)
2715 {
2716         dsl_dataset_rollback_arg_t ddra;
2717 
2718         ddra.ddra_fsname = fsname;
2719         ddra.ddra_tosnap = tosnap;
2720         ddra.ddra_owner = owner;
2721         ddra.ddra_result = result;
2722 
2723         return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
2724             dsl_dataset_rollback_sync, &ddra,
2725             1, ZFS_SPACE_CHECK_RESERVED));
2726 }
2727 
2728 struct promotenode {
2729         list_node_t link;
2730         dsl_dataset_t *ds;
2731 };
2732 
2733 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2734 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
2735     void *tag);
2736 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
2737 
2738 int
2739 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
2740 {
2741         dsl_dataset_promote_arg_t *ddpa = arg;
2742         dsl_pool_t *dp = dmu_tx_pool(tx);
2743         dsl_dataset_t *hds;
2744         struct promotenode *snap;
2745         dsl_dataset_t *origin_ds;
2746         int err;
2747         uint64_t unused;
2748         uint64_t ss_mv_cnt;
2749         size_t max_snap_len;
2750         boolean_t conflicting_snaps;
2751 
2752         err = promote_hold(ddpa, dp, FTAG);
2753         if (err != 0)
2754                 return (err);
2755 
2756         hds = ddpa->ddpa_clone;
2757         snap = list_head(&ddpa->shared_snaps);
2758         origin_ds = snap->ds;
2759         max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
2760 
2761         snap = list_head(&ddpa->origin_snaps);
2762 
2763         if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
2764                 promote_rele(ddpa, FTAG);
2765                 return (SET_ERROR(EXDEV));
2766         }
2767 
2768         /*
2769          * Compute and check the amount of space to transfer.  Since this is
2770          * so expensive, don't do the preliminary check.
2771          */
2772         if (!dmu_tx_is_syncing(tx)) {
2773                 promote_rele(ddpa, FTAG);
2774                 return (0);
2775         }
2776 
2777         /* compute origin's new unique space */
2778         snap = list_tail(&ddpa->clone_snaps);
2779         ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2780             origin_ds->ds_object);
2781         dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2782             dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
2783             &ddpa->unique, &unused, &unused);
2784 
2785         /*
2786          * Walk the snapshots that we are moving
2787          *
2788          * Compute space to transfer.  Consider the incremental changes
2789          * to used by each snapshot:
2790          * (my used) = (prev's used) + (blocks born) - (blocks killed)
2791          * So each snapshot gave birth to:
2792          * (blocks born) = (my used) - (prev's used) + (blocks killed)
2793          * So a sequence would look like:
2794          * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2795          * Which simplifies to:
2796          * uN + kN + kN-1 + ... + k1 + k0
2797          * Note however, if we stop before we reach the ORIGIN we get:
2798          * uN + kN + kN-1 + ... + kM - uM-1
2799          */
2800         conflicting_snaps = B_FALSE;
2801         ss_mv_cnt = 0;
2802         ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2803         ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2804         ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
2805         for (snap = list_head(&ddpa->shared_snaps); snap;
2806             snap = list_next(&ddpa->shared_snaps, snap)) {
2807                 uint64_t val, dlused, dlcomp, dluncomp;
2808                 dsl_dataset_t *ds = snap->ds;
2809 
2810                 ss_mv_cnt++;
2811 
2812                 /*
2813                  * If there are long holds, we won't be able to evict
2814                  * the objset.
2815                  */
2816                 if (dsl_dataset_long_held(ds)) {
2817                         err = SET_ERROR(EBUSY);
2818                         goto out;
2819                 }
2820 
2821                 /* Check that the snapshot name does not conflict */
2822                 VERIFY0(dsl_dataset_get_snapname(ds));
2823                 if (strlen(ds->ds_snapname) >= max_snap_len) {
2824                         err = SET_ERROR(ENAMETOOLONG);
2825                         goto out;
2826                 }
2827                 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2828                 if (err == 0) {
2829                         fnvlist_add_boolean(ddpa->err_ds,
2830                             snap->ds->ds_snapname);
2831                         conflicting_snaps = B_TRUE;
2832                 } else if (err != ENOENT) {
2833                         goto out;
2834                 }
2835 
2836                 /* The very first snapshot does not have a deadlist */
2837                 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
2838                         continue;
2839 
2840                 dsl_deadlist_space(&ds->ds_deadlist,
2841                     &dlused, &dlcomp, &dluncomp);
2842                 ddpa->used += dlused;
2843                 ddpa->comp += dlcomp;
2844                 ddpa->uncomp += dluncomp;
2845         }
2846 
2847         /*
2848          * In order to return the full list of conflicting snapshots, we check
2849          * whether there was a conflict after traversing all of them.
2850          */
2851         if (conflicting_snaps) {
2852                 err = SET_ERROR(EEXIST);
2853                 goto out;
2854         }
2855 
2856         /*
2857          * If we are a clone of a clone then we never reached ORIGIN,
2858          * so we need to subtract out the clone origin's used space.
2859          */
2860         if (ddpa->origin_origin) {
2861                 ddpa->used -=
2862                     dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2863                 ddpa->comp -=
2864                     dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
2865                 ddpa->uncomp -=
2866                     dsl_dataset_phys(ddpa->origin_origin)->
2867                     ds_uncompressed_bytes;
2868         }
2869 
2870         /* Check that there is enough space and limit headroom here */
2871         err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2872             0, ss_mv_cnt, ddpa->used, ddpa->cr);
2873         if (err != 0)
2874                 goto out;
2875 
2876         /*
2877          * Compute the amounts of space that will be used by snapshots
2878          * after the promotion (for both origin and clone).  For each,
2879          * it is the amount of space that will be on all of their
2880          * deadlists (that was not born before their new origin).
2881          */
2882         if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2883                 uint64_t space;
2884 
2885                 /*
2886                  * Note, typically this will not be a clone of a clone,
2887                  * so dd_origin_txg will be < TXG_INITIAL, so
2888                  * these snaplist_space() -> dsl_deadlist_space_range()
2889                  * calls will be fast because they do not have to
2890                  * iterate over all bps.
2891                  */
2892                 snap = list_head(&ddpa->origin_snaps);
2893                 err = snaplist_space(&ddpa->shared_snaps,
2894                     snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2895                 if (err != 0)
2896                         goto out;
2897 
2898                 err = snaplist_space(&ddpa->clone_snaps,
2899                     snap->ds->ds_dir->dd_origin_txg, &space);
2900                 if (err != 0)
2901                         goto out;
2902                 ddpa->cloneusedsnap += space;
2903         }
2904         if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2905             DD_FLAG_USED_BREAKDOWN) {
2906                 err = snaplist_space(&ddpa->origin_snaps,
2907                     dsl_dataset_phys(origin_ds)->ds_creation_txg,
2908                     &ddpa->originusedsnap);
2909                 if (err != 0)
2910                         goto out;
2911         }
2912 
2913 out:
2914         promote_rele(ddpa, FTAG);
2915         return (err);
2916 }
2917 
2918 void
2919 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2920 {
2921         dsl_dataset_promote_arg_t *ddpa = arg;
2922         dsl_pool_t *dp = dmu_tx_pool(tx);
2923         dsl_dataset_t *hds;
2924         struct promotenode *snap;
2925         dsl_dataset_t *origin_ds;
2926         dsl_dataset_t *origin_head;
2927         dsl_dir_t *dd;
2928         dsl_dir_t *odd = NULL;
2929         uint64_t oldnext_obj;
2930         int64_t delta;
2931 
2932         VERIFY0(promote_hold(ddpa, dp, FTAG));
2933         hds = ddpa->ddpa_clone;
2934 
2935         ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
2936 
2937         snap = list_head(&ddpa->shared_snaps);
2938         origin_ds = snap->ds;
2939         dd = hds->ds_dir;
2940 
2941         snap = list_head(&ddpa->origin_snaps);
2942         origin_head = snap->ds;
2943 
2944         /*
2945          * We need to explicitly open odd, since origin_ds's dd will be
2946          * changing.
2947          */
2948         VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2949             NULL, FTAG, &odd));
2950 
2951         /* change origin's next snap */
2952         dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2953         oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
2954         snap = list_tail(&ddpa->clone_snaps);
2955         ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2956             origin_ds->ds_object);
2957         dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2958 
2959         /* change the origin's next clone */
2960         if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
2961                 dsl_dataset_remove_from_next_clones(origin_ds,
2962                     snap->ds->ds_object, tx);
2963                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2964                     dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2965                     oldnext_obj, tx));
2966         }
2967 
2968         /* change origin */
2969         dmu_buf_will_dirty(dd->dd_dbuf, tx);
2970         ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2971         dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
2972         dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2973         dmu_buf_will_dirty(odd->dd_dbuf, tx);
2974         dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
2975         origin_head->ds_dir->dd_origin_txg =
2976             dsl_dataset_phys(origin_ds)->ds_creation_txg;
2977 
2978         /* change dd_clone entries */
2979         if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2980                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2981                     dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
2982                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2983                     dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2984                     hds->ds_object, tx));
2985 
2986                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2987                     dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2988                     origin_head->ds_object, tx));
2989                 if (dsl_dir_phys(dd)->dd_clones == 0) {
2990                         dsl_dir_phys(dd)->dd_clones =
2991                             zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2992                             DMU_OT_NONE, 0, tx);
2993                 }
2994                 VERIFY0(zap_add_int(dp->dp_meta_objset,
2995                     dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2996         }
2997 
2998         /* move snapshots to this dir */
2999         for (snap = list_head(&ddpa->shared_snaps); snap;
3000             snap = list_next(&ddpa->shared_snaps, snap)) {
3001                 dsl_dataset_t *ds = snap->ds;
3002 
3003                 /*
3004                  * Property callbacks are registered to a particular
3005                  * dsl_dir.  Since ours is changing, evict the objset
3006                  * so that they will be unregistered from the old dsl_dir.
3007                  */
3008                 if (ds->ds_objset) {
3009                         dmu_objset_evict(ds->ds_objset);
3010                         ds->ds_objset = NULL;
3011                 }
3012 
3013                 /* move snap name entry */
3014                 VERIFY0(dsl_dataset_get_snapname(ds));
3015                 VERIFY0(dsl_dataset_snap_remove(origin_head,
3016                     ds->ds_snapname, tx, B_TRUE));
3017                 VERIFY0(zap_add(dp->dp_meta_objset,
3018                     dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
3019                     8, 1, &ds->ds_object, tx));
3020                 dsl_fs_ss_count_adjust(hds->ds_dir, 1,
3021                     DD_FIELD_SNAPSHOT_COUNT, tx);
3022 
3023                 /* change containing dsl_dir */
3024                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3025                 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
3026                 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
3027                 ASSERT3P(ds->ds_dir, ==, odd);
3028                 dsl_dir_rele(ds->ds_dir, ds);
3029                 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
3030                     NULL, ds, &ds->ds_dir));
3031 
3032                 /* move any clone references */
3033                 if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
3034                     spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3035                         zap_cursor_t zc;
3036                         zap_attribute_t za;
3037 
3038                         for (zap_cursor_init(&zc, dp->dp_meta_objset,
3039                             dsl_dataset_phys(ds)->ds_next_clones_obj);
3040                             zap_cursor_retrieve(&zc, &za) == 0;
3041                             zap_cursor_advance(&zc)) {
3042                                 dsl_dataset_t *cnds;
3043                                 uint64_t o;
3044 
3045                                 if (za.za_first_integer == oldnext_obj) {
3046                                         /*
3047                                          * We've already moved the
3048                                          * origin's reference.
3049                                          */
3050                                         continue;
3051                                 }
3052 
3053                                 VERIFY0(dsl_dataset_hold_obj(dp,
3054                                     za.za_first_integer, FTAG, &cnds));
3055                                 o = dsl_dir_phys(cnds->ds_dir)->
3056                                     dd_head_dataset_obj;
3057 
3058                                 VERIFY0(zap_remove_int(dp->dp_meta_objset,
3059                                     dsl_dir_phys(odd)->dd_clones, o, tx));
3060                                 VERIFY0(zap_add_int(dp->dp_meta_objset,
3061                                     dsl_dir_phys(dd)->dd_clones, o, tx));
3062                                 dsl_dataset_rele(cnds, FTAG);
3063                         }
3064                         zap_cursor_fini(&zc);
3065                 }
3066 
3067                 ASSERT(!dsl_prop_hascb(ds));
3068         }
3069 
3070         /*
3071          * Change space accounting.
3072          * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3073          * both be valid, or both be 0 (resulting in delta == 0).  This
3074          * is true for each of {clone,origin} independently.
3075          */
3076 
3077         delta = ddpa->cloneusedsnap -
3078             dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
3079         ASSERT3S(delta, >=, 0);
3080         ASSERT3U(ddpa->used, >=, delta);
3081         dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
3082         dsl_dir_diduse_space(dd, DD_USED_HEAD,
3083             ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
3084 
3085         delta = ddpa->originusedsnap -
3086             dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
3087         ASSERT3S(delta, <=, 0);
3088         ASSERT3U(ddpa->used, >=, -delta);
3089         dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
3090         dsl_dir_diduse_space(odd, DD_USED_HEAD,
3091             -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
3092 
3093         dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
3094 
3095         /* log history record */
3096         spa_history_log_internal_ds(hds, "promote", tx, "");
3097 
3098         dsl_dir_rele(odd, FTAG);
3099         promote_rele(ddpa, FTAG);
3100 }
3101 
3102 /*
3103  * Make a list of dsl_dataset_t's for the snapshots between first_obj
3104  * (exclusive) and last_obj (inclusive).  The list will be in reverse
3105  * order (last_obj will be the list_head()).  If first_obj == 0, do all
3106  * snapshots back to this dataset's origin.
3107  */
3108 static int
3109 snaplist_make(dsl_pool_t *dp,
3110     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
3111 {
3112         uint64_t obj = last_obj;
3113 
3114         list_create(l, sizeof (struct promotenode),
3115             offsetof(struct promotenode, link));
3116 
3117         while (obj != first_obj) {
3118                 dsl_dataset_t *ds;
3119                 struct promotenode *snap;
3120                 int err;
3121 
3122                 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
3123                 ASSERT(err != ENOENT);
3124                 if (err != 0)
3125                         return (err);
3126 
3127                 if (first_obj == 0)
3128                         first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
3129 
3130                 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
3131                 snap->ds = ds;
3132                 list_insert_tail(l, snap);
3133                 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3134         }
3135 
3136         return (0);
3137 }
3138 
3139 static int
3140 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
3141 {
3142         struct promotenode *snap;
3143 
3144         *spacep = 0;
3145         for (snap = list_head(l); snap; snap = list_next(l, snap)) {
3146                 uint64_t used, comp, uncomp;
3147                 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3148                     mintxg, UINT64_MAX, &used, &comp, &uncomp);
3149                 *spacep += used;
3150         }
3151         return (0);
3152 }
3153 
3154 static void
3155 snaplist_destroy(list_t *l, void *tag)
3156 {
3157         struct promotenode *snap;
3158 
3159         if (l == NULL || !list_link_active(&l->list_head))
3160                 return;
3161 
3162         while ((snap = list_tail(l)) != NULL) {
3163                 list_remove(l, snap);
3164                 dsl_dataset_rele(snap->ds, tag);
3165                 kmem_free(snap, sizeof (*snap));
3166         }
3167         list_destroy(l);
3168 }
3169 
3170 static int
3171 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
3172 {
3173         int error;
3174         dsl_dir_t *dd;
3175         struct promotenode *snap;
3176 
3177         error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
3178             &ddpa->ddpa_clone);
3179         if (error != 0)
3180                 return (error);
3181         dd = ddpa->ddpa_clone->ds_dir;
3182 
3183         if (ddpa->ddpa_clone->ds_is_snapshot ||
3184             !dsl_dir_is_clone(dd)) {
3185                 dsl_dataset_rele(ddpa->ddpa_clone, tag);
3186                 return (SET_ERROR(EINVAL));
3187         }
3188 
3189         error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
3190             &ddpa->shared_snaps, tag);
3191         if (error != 0)
3192                 goto out;
3193 
3194         error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
3195             &ddpa->clone_snaps, tag);
3196         if (error != 0)
3197                 goto out;
3198 
3199         snap = list_head(&ddpa->shared_snaps);
3200         ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
3201         error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
3202             dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
3203             &ddpa->origin_snaps, tag);
3204         if (error != 0)
3205                 goto out;
3206 
3207         if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
3208                 error = dsl_dataset_hold_obj(dp,
3209                     dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
3210                     tag, &ddpa->origin_origin);
3211                 if (error != 0)
3212                         goto out;
3213         }
3214 out:
3215         if (error != 0)
3216                 promote_rele(ddpa, tag);
3217         return (error);
3218 }
3219 
3220 static void
3221 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
3222 {
3223         snaplist_destroy(&ddpa->shared_snaps, tag);
3224         snaplist_destroy(&ddpa->clone_snaps, tag);
3225         snaplist_destroy(&ddpa->origin_snaps, tag);
3226         if (ddpa->origin_origin != NULL)
3227                 dsl_dataset_rele(ddpa->origin_origin, tag);
3228         dsl_dataset_rele(ddpa->ddpa_clone, tag);
3229 }
3230 
3231 /*
3232  * Promote a clone.
3233  *
3234  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
3235  * in with the name.  (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3236  */
3237 int
3238 dsl_dataset_promote(const char *name, char *conflsnap)
3239 {
3240         dsl_dataset_promote_arg_t ddpa = { 0 };
3241         uint64_t numsnaps;
3242         int error;
3243         nvpair_t *snap_pair;
3244         objset_t *os;
3245 
3246         /*
3247          * We will modify space proportional to the number of
3248          * snapshots.  Compute numsnaps.
3249          */
3250         error = dmu_objset_hold(name, FTAG, &os);
3251         if (error != 0)
3252                 return (error);
3253         error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
3254             dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
3255             &numsnaps);
3256         dmu_objset_rele(os, FTAG);
3257         if (error != 0)
3258                 return (error);
3259 
3260         ddpa.ddpa_clonename = name;
3261         ddpa.err_ds = fnvlist_alloc();
3262         ddpa.cr = CRED();
3263 
3264         error = dsl_sync_task(name, dsl_dataset_promote_check,
3265             dsl_dataset_promote_sync, &ddpa,
3266             2 + numsnaps, ZFS_SPACE_CHECK_RESERVED);
3267 
3268         /*
3269          * Return the first conflicting snapshot found.
3270          */
3271         snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL);
3272         if (snap_pair != NULL && conflsnap != NULL)
3273                 (void) strcpy(conflsnap, nvpair_name(snap_pair));
3274 
3275         fnvlist_free(ddpa.err_ds);
3276         return (error);
3277 }
3278 
3279 int
3280 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
3281     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
3282 {
3283         /*
3284          * "slack" factor for received datasets with refquota set on them.
3285          * See the bottom of this function for details on its use.
3286          */
3287         uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
3288         int64_t unused_refres_delta;
3289 
3290         /* they should both be heads */
3291         if (clone->ds_is_snapshot ||
3292             origin_head->ds_is_snapshot)
3293                 return (SET_ERROR(EINVAL));
3294 
3295         /* if we are not forcing, the branch point should be just before them */
3296         if (!force && clone->ds_prev != origin_head->ds_prev)
3297                 return (SET_ERROR(EINVAL));
3298 
3299         /* clone should be the clone (unless they are unrelated) */
3300         if (clone->ds_prev != NULL &&
3301             clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
3302             origin_head->ds_dir != clone->ds_prev->ds_dir)
3303                 return (SET_ERROR(EINVAL));
3304 
3305         /* the clone should be a child of the origin */
3306         if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3307                 return (SET_ERROR(EINVAL));
3308 
3309         /* origin_head shouldn't be modified unless 'force' */
3310         if (!force &&
3311             dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3312                 return (SET_ERROR(ETXTBSY));
3313 
3314         /* origin_head should have no long holds (e.g. is not mounted) */
3315         if (dsl_dataset_handoff_check(origin_head, owner, tx))
3316                 return (SET_ERROR(EBUSY));
3317 
3318         /* check amount of any unconsumed refreservation */
3319         unused_refres_delta =
3320             (int64_t)MIN(origin_head->ds_reserved,
3321             dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3322             (int64_t)MIN(origin_head->ds_reserved,
3323             dsl_dataset_phys(clone)->ds_unique_bytes);
3324 
3325         if (unused_refres_delta > 0 &&
3326             unused_refres_delta >
3327             dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3328                 return (SET_ERROR(ENOSPC));
3329 
3330         /*
3331          * The clone can't be too much over the head's refquota.
3332          *
3333          * To ensure that the entire refquota can be used, we allow one
3334          * transaction to exceed the the refquota.  Therefore, this check
3335          * needs to also allow for the space referenced to be more than the
3336          * refquota.  The maximum amount of space that one transaction can use
3337          * on disk is DMU_MAX_ACCESS * spa_asize_inflation.  Allowing this
3338          * overage ensures that we are able to receive a filesystem that
3339          * exceeds the refquota on the source system.
3340          *
3341          * So that overage is the refquota_slack we use below.
3342          */
3343         if (origin_head->ds_quota != 0 &&
3344             dsl_dataset_phys(clone)->ds_referenced_bytes >
3345             origin_head->ds_quota + refquota_slack)
3346                 return (SET_ERROR(EDQUOT));
3347 
3348         return (0);
3349 }
3350 
3351 void
3352 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
3353     dsl_dataset_t *origin_head, dmu_tx_t *tx)
3354 {
3355         dsl_pool_t *dp = dmu_tx_pool(tx);
3356         int64_t unused_refres_delta;
3357 
3358         ASSERT(clone->ds_reserved == 0);
3359         /*
3360          * NOTE: On DEBUG kernels there could be a race between this and
3361          * the check function if spa_asize_inflation is adjusted...
3362          */
3363         ASSERT(origin_head->ds_quota == 0 ||
3364             dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
3365             DMU_MAX_ACCESS * spa_asize_inflation);
3366         ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
3367 
3368         /*
3369          * Swap per-dataset feature flags.
3370          */
3371         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3372                 if (!(spa_feature_table[f].fi_flags &
3373                     ZFEATURE_FLAG_PER_DATASET)) {
3374                         ASSERT(!clone->ds_feature_inuse[f]);
3375                         ASSERT(!origin_head->ds_feature_inuse[f]);
3376                         continue;
3377                 }
3378 
3379                 boolean_t clone_inuse = clone->ds_feature_inuse[f];
3380                 boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
3381 
3382                 if (clone_inuse) {
3383                         dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
3384                         clone->ds_feature_inuse[f] = B_FALSE;
3385                 }
3386                 if (origin_head_inuse) {
3387                         dsl_dataset_deactivate_feature(origin_head->ds_object,
3388                             f, tx);
3389                         origin_head->ds_feature_inuse[f] = B_FALSE;
3390                 }
3391                 if (clone_inuse) {
3392                         dsl_dataset_activate_feature(origin_head->ds_object,
3393                             f, tx);
3394                         origin_head->ds_feature_inuse[f] = B_TRUE;
3395                 }
3396                 if (origin_head_inuse) {
3397                         dsl_dataset_activate_feature(clone->ds_object, f, tx);
3398                         clone->ds_feature_inuse[f] = B_TRUE;
3399                 }
3400         }
3401 
3402         dmu_buf_will_dirty(clone->ds_dbuf, tx);
3403         dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3404 
3405         if (clone->ds_objset != NULL) {
3406                 dmu_objset_evict(clone->ds_objset);
3407                 clone->ds_objset = NULL;
3408         }
3409 
3410         if (origin_head->ds_objset != NULL) {
3411                 dmu_objset_evict(origin_head->ds_objset);
3412                 origin_head->ds_objset = NULL;
3413         }
3414 
3415         unused_refres_delta =
3416             (int64_t)MIN(origin_head->ds_reserved,
3417             dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3418             (int64_t)MIN(origin_head->ds_reserved,
3419             dsl_dataset_phys(clone)->ds_unique_bytes);
3420 
3421         /*
3422          * Reset origin's unique bytes, if it exists.
3423          */
3424         if (clone->ds_prev) {
3425                 dsl_dataset_t *origin = clone->ds_prev;
3426                 uint64_t comp, uncomp;
3427 
3428                 dmu_buf_will_dirty(origin->ds_dbuf, tx);
3429                 dsl_deadlist_space_range(&clone->ds_deadlist,
3430                     dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
3431                     &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
3432         }
3433 
3434         /* swap blkptrs */
3435         {
3436                 rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
3437                 rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
3438                 blkptr_t tmp;
3439                 tmp = dsl_dataset_phys(origin_head)->ds_bp;
3440                 dsl_dataset_phys(origin_head)->ds_bp =
3441                     dsl_dataset_phys(clone)->ds_bp;
3442                 dsl_dataset_phys(clone)->ds_bp = tmp;
3443                 rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
3444                 rrw_exit(&clone->ds_bp_rwlock, FTAG);
3445         }
3446 
3447         /* set dd_*_bytes */
3448         {
3449                 int64_t dused, dcomp, duncomp;
3450                 uint64_t cdl_used, cdl_comp, cdl_uncomp;
3451                 uint64_t odl_used, odl_comp, odl_uncomp;
3452 
3453                 ASSERT3U(dsl_dir_phys(clone->ds_dir)->
3454                     dd_used_breakdown[DD_USED_SNAP], ==, 0);
3455 
3456                 dsl_deadlist_space(&clone->ds_deadlist,
3457                     &cdl_used, &cdl_comp, &cdl_uncomp);
3458                 dsl_deadlist_space(&origin_head->ds_deadlist,
3459                     &odl_used, &odl_comp, &odl_uncomp);
3460 
3461                 dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
3462                     cdl_used -
3463                     (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
3464                     odl_used);
3465                 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
3466                     cdl_comp -
3467                     (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
3468                     odl_comp);
3469                 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
3470                     cdl_uncomp -
3471                     (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
3472                     odl_uncomp);
3473 
3474                 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
3475                     dused, dcomp, duncomp, tx);
3476                 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
3477                     -dused, -dcomp, -duncomp, tx);
3478 
3479                 /*
3480                  * The difference in the space used by snapshots is the
3481                  * difference in snapshot space due to the head's
3482                  * deadlist (since that's the only thing that's
3483                  * changing that affects the snapused).
3484                  */
3485                 dsl_deadlist_space_range(&clone->ds_deadlist,
3486                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3487                     &cdl_used, &cdl_comp, &cdl_uncomp);
3488                 dsl_deadlist_space_range(&origin_head->ds_deadlist,
3489                     origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3490                     &odl_used, &odl_comp, &odl_uncomp);
3491                 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
3492                     DD_USED_HEAD, DD_USED_SNAP, tx);
3493         }
3494 
3495         /* swap ds_*_bytes */
3496         SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
3497             dsl_dataset_phys(clone)->ds_referenced_bytes);
3498         SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
3499             dsl_dataset_phys(clone)->ds_compressed_bytes);
3500         SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
3501             dsl_dataset_phys(clone)->ds_uncompressed_bytes);
3502         SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
3503             dsl_dataset_phys(clone)->ds_unique_bytes);
3504 
3505         /* apply any parent delta for change in unconsumed refreservation */
3506         dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
3507             unused_refres_delta, 0, 0, tx);
3508 
3509         /*
3510          * Swap deadlists.
3511          */
3512         dsl_deadlist_close(&clone->ds_deadlist);
3513         dsl_deadlist_close(&origin_head->ds_deadlist);
3514         SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
3515             dsl_dataset_phys(clone)->ds_deadlist_obj);
3516         dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
3517             dsl_dataset_phys(clone)->ds_deadlist_obj);
3518         dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3519             dsl_dataset_phys(origin_head)->ds_deadlist_obj);
3520 
3521         dsl_scan_ds_clone_swapped(origin_head, clone, tx);
3522 
3523         spa_history_log_internal_ds(clone, "clone swap", tx,
3524             "parent=%s", origin_head->ds_dir->dd_myname);
3525 }
3526 
3527 /*
3528  * Given a pool name and a dataset object number in that pool,
3529  * return the name of that dataset.
3530  */
3531 int
3532 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3533 {
3534         dsl_pool_t *dp;
3535         dsl_dataset_t *ds;
3536         int error;
3537 
3538         error = dsl_pool_hold(pname, FTAG, &dp);
3539         if (error != 0)
3540                 return (error);
3541 
3542         error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
3543         if (error == 0) {
3544                 dsl_dataset_name(ds, buf);
3545                 dsl_dataset_rele(ds, FTAG);
3546         }
3547         dsl_pool_rele(dp, FTAG);
3548 
3549         return (error);
3550 }
3551 
3552 int
3553 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3554     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3555 {
3556         int error = 0;
3557 
3558         ASSERT3S(asize, >, 0);
3559 
3560         /*
3561          * *ref_rsrv is the portion of asize that will come from any
3562          * unconsumed refreservation space.
3563          */
3564         *ref_rsrv = 0;
3565 
3566         mutex_enter(&ds->ds_lock);
3567         /*
3568          * Make a space adjustment for reserved bytes.
3569          */
3570         if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3571                 ASSERT3U(*used, >=,
3572                     ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3573                 *used -=
3574                     (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3575                 *ref_rsrv =
3576                     asize - MIN(asize, parent_delta(ds, asize + inflight));
3577         }
3578 
3579         if (!check_quota || ds->ds_quota == 0) {
3580                 mutex_exit(&ds->ds_lock);
3581                 return (0);
3582         }
3583         /*
3584          * If they are requesting more space, and our current estimate
3585          * is over quota, they get to try again unless the actual
3586          * on-disk is over quota and there are no pending changes (which
3587          * may free up space for us).
3588          */
3589         if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3590             ds->ds_quota) {
3591                 if (inflight > 0 ||
3592                     dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3593                         error = SET_ERROR(ERESTART);
3594                 else
3595                         error = SET_ERROR(EDQUOT);
3596         }
3597         mutex_exit(&ds->ds_lock);
3598 
3599         return (error);
3600 }
3601 
3602 typedef struct dsl_dataset_set_qr_arg {
3603         const char *ddsqra_name;
3604         zprop_source_t ddsqra_source;
3605         uint64_t ddsqra_value;
3606 } dsl_dataset_set_qr_arg_t;
3607 
3608 
3609 /* ARGSUSED */
3610 static int
3611 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3612 {
3613         dsl_dataset_set_qr_arg_t *ddsqra = arg;
3614         dsl_pool_t *dp = dmu_tx_pool(tx);
3615         dsl_dataset_t *ds;
3616         int error;
3617         uint64_t newval;
3618 
3619         if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3620                 return (SET_ERROR(ENOTSUP));
3621 
3622         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3623         if (error != 0)
3624                 return (error);
3625 
3626         if (ds->ds_is_snapshot) {
3627                 dsl_dataset_rele(ds, FTAG);
3628                 return (SET_ERROR(EINVAL));
3629         }
3630 
3631         error = dsl_prop_predict(ds->ds_dir,
3632             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3633             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3634         if (error != 0) {
3635                 dsl_dataset_rele(ds, FTAG);
3636                 return (error);
3637         }
3638 
3639         if (newval == 0) {
3640                 dsl_dataset_rele(ds, FTAG);
3641                 return (0);
3642         }
3643 
3644         if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
3645             newval < ds->ds_reserved) {
3646                 dsl_dataset_rele(ds, FTAG);
3647                 return (SET_ERROR(ENOSPC));
3648         }
3649 
3650         dsl_dataset_rele(ds, FTAG);
3651         return (0);
3652 }
3653 
3654 static void
3655 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3656 {
3657         dsl_dataset_set_qr_arg_t *ddsqra = arg;
3658         dsl_pool_t *dp = dmu_tx_pool(tx);
3659         dsl_dataset_t *ds;
3660         uint64_t newval;
3661 
3662         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3663 
3664         dsl_prop_set_sync_impl(ds,
3665             zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3666             ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
3667             &ddsqra->ddsqra_value, tx);
3668 
3669         VERIFY0(dsl_prop_get_int_ds(ds,
3670             zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3671 
3672         if (ds->ds_quota != newval) {
3673                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3674                 ds->ds_quota = newval;
3675         }
3676         dsl_dataset_rele(ds, FTAG);
3677 }
3678 
3679 int
3680 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
3681     uint64_t refquota)
3682 {
3683         dsl_dataset_set_qr_arg_t ddsqra;
3684 
3685         ddsqra.ddsqra_name = dsname;
3686         ddsqra.ddsqra_source = source;
3687         ddsqra.ddsqra_value = refquota;
3688 
3689         return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
3690             dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3691 }
3692 
3693 static int
3694 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3695 {
3696         dsl_dataset_set_qr_arg_t *ddsqra = arg;
3697         dsl_pool_t *dp = dmu_tx_pool(tx);
3698         dsl_dataset_t *ds;
3699         int error;
3700         uint64_t newval, unique;
3701 
3702         if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3703                 return (SET_ERROR(ENOTSUP));
3704 
3705         error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3706         if (error != 0)
3707                 return (error);
3708 
3709         if (ds->ds_is_snapshot) {
3710                 dsl_dataset_rele(ds, FTAG);
3711                 return (SET_ERROR(EINVAL));
3712         }
3713 
3714         error = dsl_prop_predict(ds->ds_dir,
3715             zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3716             ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3717         if (error != 0) {
3718                 dsl_dataset_rele(ds, FTAG);
3719                 return (error);
3720         }
3721 
3722         /*
3723          * If we are doing the preliminary check in open context, the
3724          * space estimates may be inaccurate.
3725          */
3726         if (!dmu_tx_is_syncing(tx)) {
3727                 dsl_dataset_rele(ds, FTAG);
3728                 return (0);
3729         }
3730 
3731         mutex_enter(&ds->ds_lock);
3732         if (!DS_UNIQUE_IS_ACCURATE(ds))
3733                 dsl_dataset_recalc_head_uniq(ds);
3734         unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3735         mutex_exit(&ds->ds_lock);
3736 
3737         if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
3738                 uint64_t delta = MAX(unique, newval) -
3739                     MAX(unique, ds->ds_reserved);
3740 
3741                 if (delta >
3742                     dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
3743                     (ds->ds_quota > 0 && newval > ds->ds_quota)) {
3744                         dsl_dataset_rele(ds, FTAG);
3745                         return (SET_ERROR(ENOSPC));
3746                 }
3747         }
3748 
3749         dsl_dataset_rele(ds, FTAG);
3750         return (0);
3751 }
3752 
3753 void
3754 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
3755     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3756 {
3757         uint64_t newval;
3758         uint64_t unique;
3759         int64_t delta;
3760 
3761         dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3762             source, sizeof (value), 1, &value, tx);
3763 
3764         VERIFY0(dsl_prop_get_int_ds(ds,
3765             zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3766 
3767         dmu_buf_will_dirty(ds->ds_dbuf, tx);
3768         mutex_enter(&ds->ds_dir->dd_lock);
3769         mutex_enter(&ds->ds_lock);
3770         ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3771         unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3772         delta = MAX(0, (int64_t)(newval - unique)) -
3773             MAX(0, (int64_t)(ds->ds_reserved - unique));
3774         ds->ds_reserved = newval;
3775         mutex_exit(&ds->ds_lock);
3776 
3777         dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3778         mutex_exit(&ds->ds_dir->dd_lock);
3779 }
3780 
3781 static void
3782 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3783 {
3784         dsl_dataset_set_qr_arg_t *ddsqra = arg;
3785         dsl_pool_t *dp = dmu_tx_pool(tx);
3786         dsl_dataset_t *ds;
3787 
3788         VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3789         dsl_dataset_set_refreservation_sync_impl(ds,
3790             ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3791         dsl_dataset_rele(ds, FTAG);
3792 }
3793 
3794 int
3795 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
3796     uint64_t refreservation)
3797 {
3798         dsl_dataset_set_qr_arg_t ddsqra;
3799 
3800         ddsqra.ddsqra_name = dsname;
3801         ddsqra.ddsqra_source = source;
3802         ddsqra.ddsqra_value = refreservation;
3803 
3804         return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
3805             dsl_dataset_set_refreservation_sync, &ddsqra,
3806             0, ZFS_SPACE_CHECK_NONE));
3807 }
3808 
3809 /*
3810  * Return (in *usedp) the amount of space written in new that is not
3811  * present in oldsnap.  New may be a snapshot or the head.  Old must be
3812  * a snapshot before new, in new's filesystem (or its origin).  If not then
3813  * fail and return EINVAL.
3814  *
3815  * The written space is calculated by considering two components:  First, we
3816  * ignore any freed space, and calculate the written as new's used space
3817  * minus old's used space.  Next, we add in the amount of space that was freed
3818  * between the two snapshots, thus reducing new's used space relative to old's.
3819  * Specifically, this is the space that was born before old->ds_creation_txg,
3820  * and freed before new (ie. on new's deadlist or a previous deadlist).
3821  *
3822  * space freed                         [---------------------]
3823  * snapshots                       ---O-------O--------O-------O------
3824  *                                         oldsnap            new
3825  */
3826 int
3827 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
3828     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3829 {
3830         int err = 0;
3831         uint64_t snapobj;
3832         dsl_pool_t *dp = new->ds_dir->dd_pool;
3833 
3834         ASSERT(dsl_pool_config_held(dp));
3835 
3836         *usedp = 0;
3837         *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3838         *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
3839 
3840         *compp = 0;
3841         *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3842         *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
3843 
3844         *uncompp = 0;
3845         *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3846         *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
3847 
3848         snapobj = new->ds_object;
3849         while (snapobj != oldsnap->ds_object) {
3850                 dsl_dataset_t *snap;
3851                 uint64_t used, comp, uncomp;
3852 
3853                 if (snapobj == new->ds_object) {
3854                         snap = new;
3855                 } else {
3856                         err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3857                         if (err != 0)
3858                                 break;
3859                 }
3860 
3861                 if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3862                     dsl_dataset_phys(oldsnap)->ds_creation_txg) {
3863                         /*
3864                          * The blocks in the deadlist can not be born after
3865                          * ds_prev_snap_txg, so get the whole deadlist space,
3866                          * which is more efficient (especially for old-format
3867                          * deadlists).  Unfortunately the deadlist code
3868                          * doesn't have enough information to make this
3869                          * optimization itself.
3870                          */
3871                         dsl_deadlist_space(&snap->ds_deadlist,
3872                             &used, &comp, &uncomp);
3873                 } else {
3874                         dsl_deadlist_space_range(&snap->ds_deadlist,
3875                             0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
3876                             &used, &comp, &uncomp);
3877                 }
3878                 *usedp += used;
3879                 *compp += comp;
3880                 *uncompp += uncomp;
3881 
3882                 /*
3883                  * If we get to the beginning of the chain of snapshots
3884                  * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3885                  * was not a snapshot of/before new.
3886                  */
3887                 snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3888                 if (snap != new)
3889                         dsl_dataset_rele(snap, FTAG);
3890                 if (snapobj == 0) {
3891                         err = SET_ERROR(EINVAL);
3892                         break;
3893                 }
3894 
3895         }
3896         return (err);
3897 }
3898 
3899 /*
3900  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3901  * lastsnap, and all snapshots in between are deleted.
3902  *
3903  * blocks that would be freed            [---------------------------]
3904  * snapshots                       ---O-------O--------O-------O--------O
3905  *                                        firstsnap        lastsnap
3906  *
3907  * This is the set of blocks that were born after the snap before firstsnap,
3908  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3909  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3910  * We calculate this by iterating over the relevant deadlists (from the snap
3911  * after lastsnap, backward to the snap after firstsnap), summing up the
3912  * space on the deadlist that was born after the snap before firstsnap.
3913  */
3914 int
3915 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
3916     dsl_dataset_t *lastsnap,
3917     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3918 {
3919         int err = 0;
3920         uint64_t snapobj;
3921         dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3922 
3923         ASSERT(firstsnap->ds_is_snapshot);
3924         ASSERT(lastsnap->ds_is_snapshot);
3925 
3926         /*
3927          * Check that the snapshots are in the same dsl_dir, and firstsnap
3928          * is before lastsnap.
3929          */
3930         if (firstsnap->ds_dir != lastsnap->ds_dir ||
3931             dsl_dataset_phys(firstsnap)->ds_creation_txg >
3932             dsl_dataset_phys(lastsnap)->ds_creation_txg)
3933                 return (SET_ERROR(EINVAL));
3934 
3935         *usedp = *compp = *uncompp = 0;
3936 
3937         snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
3938         while (snapobj != firstsnap->ds_object) {
3939                 dsl_dataset_t *ds;
3940                 uint64_t used, comp, uncomp;
3941 
3942                 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3943                 if (err != 0)
3944                         break;
3945 
3946                 dsl_deadlist_space_range(&ds->ds_deadlist,
3947                     dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
3948                     &used, &comp, &uncomp);
3949                 *usedp += used;
3950                 *compp += comp;
3951                 *uncompp += uncomp;
3952 
3953                 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3954                 ASSERT3U(snapobj, !=, 0);
3955                 dsl_dataset_rele(ds, FTAG);
3956         }
3957         return (err);
3958 }
3959 
3960 /*
3961  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3962  * For example, they could both be snapshots of the same filesystem, and
3963  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
3964  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
3965  * filesystem.  Or 'earlier' could be the origin's origin.
3966  *
3967  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3968  */
3969 boolean_t
3970 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3971     uint64_t earlier_txg)
3972 {
3973         dsl_pool_t *dp = later->ds_dir->dd_pool;
3974         int error;
3975         boolean_t ret;
3976 
3977         ASSERT(dsl_pool_config_held(dp));
3978         ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
3979 
3980         if (earlier_txg == 0)
3981                 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
3982 
3983         if (later->ds_is_snapshot &&
3984             earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
3985                 return (B_FALSE);
3986 
3987         if (later->ds_dir == earlier->ds_dir)
3988                 return (B_TRUE);
3989         if (!dsl_dir_is_clone(later->ds_dir))
3990                 return (B_FALSE);
3991 
3992         if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
3993                 return (B_TRUE);
3994         dsl_dataset_t *origin;
3995         error = dsl_dataset_hold_obj(dp,
3996             dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
3997         if (error != 0)
3998                 return (B_FALSE);
3999         ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
4000         dsl_dataset_rele(origin, FTAG);
4001         return (ret);
4002 }
4003 
4004 void
4005 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
4006 {
4007         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4008         dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
4009 }
4010 
4011 boolean_t
4012 dataset_name_hidden(const char *name)
4013 {
4014         if (strchr(name, '$') != NULL)
4015                 return (B_TRUE);
4016         if (strchr(name, '%') != NULL)
4017                 return (B_TRUE);
4018         if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
4019                 return (B_TRUE);
4020         return (B_FALSE);
4021 }
4022 
4023 uint64_t
4024 dsl_dataset_creation_txg(const char *name)
4025 {
4026         dsl_pool_t *dp;
4027         dsl_dataset_t *ds;
4028         int err;
4029         uint64_t ret = UINT64_MAX;
4030 
4031         err = dsl_pool_hold(name, FTAG, &dp);
4032 
4033         if (err)
4034                 return (ret);
4035 
4036         err = dsl_dataset_hold(dp, name, FTAG, &ds);
4037 
4038         if (!err) {
4039                 ret = dsl_dataset_phys(ds)->ds_creation_txg;
4040                 dsl_dataset_rele(ds, FTAG);
4041         }
4042 
4043         dsl_pool_rele(dp, FTAG);
4044 
4045         return (ret);
4046 }
4047 
4048 boolean_t
4049 dsl_dataset_is_zapified(dsl_dataset_t *ds)
4050 {
4051         dmu_object_info_t doi;
4052 
4053         dmu_object_info_from_db(ds->ds_dbuf, &doi);
4054         return (doi.doi_type == DMU_OTN_ZAP_METADATA);
4055 }
4056 
4057 boolean_t
4058 dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
4059 {
4060         return (dsl_dataset_is_zapified(ds) &&
4061             zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
4062             ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
4063 }