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