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