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