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