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