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 */ 24 25 #include <sys/dmu_objset.h> 26 #include <sys/dsl_dataset.h> 27 #include <sys/dsl_dir.h> 28 #include <sys/dsl_prop.h> 29 #include <sys/dsl_synctask.h> 30 #include <sys/dmu_traverse.h> 31 #include <sys/dmu_tx.h> 32 #include <sys/arc.h> 33 #include <sys/zio.h> 34 #include <sys/zap.h> 35 #include <sys/unique.h> 36 #include <sys/zfs_context.h> 37 #include <sys/zfs_ioctl.h> 38 #include <sys/spa.h> 39 #include <sys/zfs_znode.h> 40 #include <sys/zfs_onexit.h> 41 #include <sys/zvol.h> 42 #include <sys/dsl_scan.h> 43 #include <sys/dsl_deadlist.h> 44 45 /* 46 * Enable/disable prefetching of dedup-ed blocks which are going to be freed. 47 */ 48 int zfs_dedup_prefetch = 1; 49 50 static char *dsl_reaper = "the grim reaper"; 51 52 static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 53 static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 54 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 55 56 #define SWITCH64(x, y) \ 57 { \ 58 uint64_t __tmp = (x); \ 59 (x) = (y); \ 60 (y) = __tmp; \ 61 } 62 63 #define DS_REF_MAX (1ULL << 62) 64 65 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 66 67 #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 68 69 70 /* 71 * Figure out how much of this delta should be propogated to the dsl_dir 72 * layer. If there's a refreservation, that space has already been 73 * partially accounted for in our ancestors. 74 */ 75 static int64_t 76 parent_delta(dsl_dataset_t *ds, int64_t delta) 77 { 78 uint64_t old_bytes, new_bytes; 79 80 if (ds->ds_reserved == 0) 81 return (delta); 82 83 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 84 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 85 86 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 87 return (new_bytes - old_bytes); 88 } 89 90 void 91 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 92 { 93 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 94 int compressed = BP_GET_PSIZE(bp); 95 int uncompressed = BP_GET_UCSIZE(bp); 96 int64_t delta; 97 98 dprintf_bp(bp, "ds=%p", ds); 99 100 ASSERT(dmu_tx_is_syncing(tx)); 101 /* It could have been compressed away to nothing */ 102 if (BP_IS_HOLE(bp)) 103 return; 104 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 105 ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 106 if (ds == NULL) { 107 /* 108 * Account for the meta-objset space in its placeholder 109 * dsl_dir. 110 */ 111 ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 112 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 113 used, compressed, uncompressed, tx); 114 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 115 return; 116 } 117 dmu_buf_will_dirty(ds->ds_dbuf, tx); 118 119 mutex_enter(&ds->ds_dir->dd_lock); 120 mutex_enter(&ds->ds_lock); 121 delta = parent_delta(ds, used); 122 ds->ds_phys->ds_used_bytes += used; 123 ds->ds_phys->ds_compressed_bytes += compressed; 124 ds->ds_phys->ds_uncompressed_bytes += uncompressed; 125 ds->ds_phys->ds_unique_bytes += used; 126 mutex_exit(&ds->ds_lock); 127 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 128 compressed, uncompressed, tx); 129 dsl_dir_transfer_space(ds->ds_dir, used - delta, 130 DD_USED_REFRSRV, DD_USED_HEAD, tx); 131 mutex_exit(&ds->ds_dir->dd_lock); 132 } 133 134 int 135 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 136 boolean_t async) 137 { 138 if (BP_IS_HOLE(bp)) 139 return (0); 140 141 ASSERT(dmu_tx_is_syncing(tx)); 142 ASSERT(bp->blk_birth <= tx->tx_txg); 143 144 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 145 int compressed = BP_GET_PSIZE(bp); 146 int uncompressed = BP_GET_UCSIZE(bp); 147 148 ASSERT(used > 0); 149 if (ds == NULL) { 150 /* 151 * Account for the meta-objset space in its placeholder 152 * dataset. 153 */ 154 dsl_free(tx->tx_pool, tx->tx_txg, bp); 155 156 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 157 -used, -compressed, -uncompressed, tx); 158 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 159 return (used); 160 } 161 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 162 163 ASSERT(!dsl_dataset_is_snapshot(ds)); 164 dmu_buf_will_dirty(ds->ds_dbuf, tx); 165 166 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 167 int64_t delta; 168 169 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 170 dsl_free(tx->tx_pool, tx->tx_txg, bp); 171 172 mutex_enter(&ds->ds_dir->dd_lock); 173 mutex_enter(&ds->ds_lock); 174 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 175 !DS_UNIQUE_IS_ACCURATE(ds)); 176 delta = parent_delta(ds, -used); 177 ds->ds_phys->ds_unique_bytes -= used; 178 mutex_exit(&ds->ds_lock); 179 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 180 delta, -compressed, -uncompressed, tx); 181 dsl_dir_transfer_space(ds->ds_dir, -used - delta, 182 DD_USED_REFRSRV, DD_USED_HEAD, tx); 183 mutex_exit(&ds->ds_dir->dd_lock); 184 } else { 185 dprintf_bp(bp, "putting on dead list: %s", ""); 186 if (async) { 187 /* 188 * We are here as part of zio's write done callback, 189 * which means we're a zio interrupt thread. We can't 190 * call dsl_deadlist_insert() now because it may block 191 * waiting for I/O. Instead, put bp on the deferred 192 * queue and let dsl_pool_sync() finish the job. 193 */ 194 bplist_append(&ds->ds_pending_deadlist, bp); 195 } else { 196 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 197 } 198 ASSERT3U(ds->ds_prev->ds_object, ==, 199 ds->ds_phys->ds_prev_snap_obj); 200 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 201 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 202 if (ds->ds_prev->ds_phys->ds_next_snap_obj == 203 ds->ds_object && bp->blk_birth > 204 ds->ds_prev->ds_phys->ds_prev_snap_txg) { 205 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 206 mutex_enter(&ds->ds_prev->ds_lock); 207 ds->ds_prev->ds_phys->ds_unique_bytes += used; 208 mutex_exit(&ds->ds_prev->ds_lock); 209 } 210 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 211 dsl_dir_transfer_space(ds->ds_dir, used, 212 DD_USED_HEAD, DD_USED_SNAP, tx); 213 } 214 } 215 mutex_enter(&ds->ds_lock); 216 ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 217 ds->ds_phys->ds_used_bytes -= used; 218 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 219 ds->ds_phys->ds_compressed_bytes -= compressed; 220 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 221 ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 222 mutex_exit(&ds->ds_lock); 223 224 return (used); 225 } 226 227 uint64_t 228 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 229 { 230 uint64_t trysnap = 0; 231 232 if (ds == NULL) 233 return (0); 234 /* 235 * The snapshot creation could fail, but that would cause an 236 * incorrect FALSE return, which would only result in an 237 * overestimation of the amount of space that an operation would 238 * consume, which is OK. 239 * 240 * There's also a small window where we could miss a pending 241 * snapshot, because we could set the sync task in the quiescing 242 * phase. So this should only be used as a guess. 243 */ 244 if (ds->ds_trysnap_txg > 245 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 246 trysnap = ds->ds_trysnap_txg; 247 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 248 } 249 250 boolean_t 251 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 252 uint64_t blk_birth) 253 { 254 if (blk_birth <= dsl_dataset_prev_snap_txg(ds)) 255 return (B_FALSE); 256 257 if (zfs_dedup_prefetch && bp && BP_GET_DEDUP(bp)) 258 ddt_prefetch(dsl_dataset_get_spa(ds), bp); 259 260 return (B_TRUE); 261 } 262 263 /* ARGSUSED */ 264 static void 265 dsl_dataset_evict(dmu_buf_t *db, void *dsv) 266 { 267 dsl_dataset_t *ds = dsv; 268 269 ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 270 271 unique_remove(ds->ds_fsid_guid); 272 273 if (ds->ds_objset != NULL) 274 dmu_objset_evict(ds->ds_objset); 275 276 if (ds->ds_prev) { 277 dsl_dataset_drop_ref(ds->ds_prev, ds); 278 ds->ds_prev = NULL; 279 } 280 281 bplist_destroy(&ds->ds_pending_deadlist); 282 if (db != NULL) { 283 dsl_deadlist_close(&ds->ds_deadlist); 284 } else { 285 ASSERT(ds->ds_deadlist.dl_dbuf == NULL); 286 ASSERT(!ds->ds_deadlist.dl_oldfmt); 287 } 288 if (ds->ds_dir) 289 dsl_dir_close(ds->ds_dir, ds); 290 291 ASSERT(!list_link_active(&ds->ds_synced_link)); 292 293 mutex_destroy(&ds->ds_lock); 294 mutex_destroy(&ds->ds_recvlock); 295 mutex_destroy(&ds->ds_opening_lock); 296 rw_destroy(&ds->ds_rwlock); 297 cv_destroy(&ds->ds_exclusive_cv); 298 299 kmem_free(ds, sizeof (dsl_dataset_t)); 300 } 301 302 static int 303 dsl_dataset_get_snapname(dsl_dataset_t *ds) 304 { 305 dsl_dataset_phys_t *headphys; 306 int err; 307 dmu_buf_t *headdbuf; 308 dsl_pool_t *dp = ds->ds_dir->dd_pool; 309 objset_t *mos = dp->dp_meta_objset; 310 311 if (ds->ds_snapname[0]) 312 return (0); 313 if (ds->ds_phys->ds_next_snap_obj == 0) 314 return (0); 315 316 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 317 FTAG, &headdbuf); 318 if (err) 319 return (err); 320 headphys = headdbuf->db_data; 321 err = zap_value_search(dp->dp_meta_objset, 322 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 323 dmu_buf_rele(headdbuf, FTAG); 324 return (err); 325 } 326 327 static int 328 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 329 { 330 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 331 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 332 matchtype_t mt; 333 int err; 334 335 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 336 mt = MT_FIRST; 337 else 338 mt = MT_EXACT; 339 340 err = zap_lookup_norm(mos, snapobj, name, 8, 1, 341 value, mt, NULL, 0, NULL); 342 if (err == ENOTSUP && mt == MT_FIRST) 343 err = zap_lookup(mos, snapobj, name, 8, 1, value); 344 return (err); 345 } 346 347 static int 348 dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 349 { 350 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 351 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 352 matchtype_t mt; 353 int err; 354 355 dsl_dir_snap_cmtime_update(ds->ds_dir); 356 357 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 358 mt = MT_FIRST; 359 else 360 mt = MT_EXACT; 361 362 err = zap_remove_norm(mos, snapobj, name, mt, tx); 363 if (err == ENOTSUP && mt == MT_FIRST) 364 err = zap_remove(mos, snapobj, name, tx); 365 return (err); 366 } 367 368 static int 369 dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 370 dsl_dataset_t **dsp) 371 { 372 objset_t *mos = dp->dp_meta_objset; 373 dmu_buf_t *dbuf; 374 dsl_dataset_t *ds; 375 int err; 376 377 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 378 dsl_pool_sync_context(dp)); 379 380 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 381 if (err) 382 return (err); 383 ds = dmu_buf_get_user(dbuf); 384 if (ds == NULL) { 385 dsl_dataset_t *winner; 386 387 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 388 ds->ds_dbuf = dbuf; 389 ds->ds_object = dsobj; 390 ds->ds_phys = dbuf->db_data; 391 392 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 393 mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL); 394 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 395 rw_init(&ds->ds_rwlock, 0, 0, 0); 396 cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 397 398 bplist_create(&ds->ds_pending_deadlist); 399 dsl_deadlist_open(&ds->ds_deadlist, 400 mos, ds->ds_phys->ds_deadlist_obj); 401 402 if (err == 0) { 403 err = dsl_dir_open_obj(dp, 404 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 405 } 406 if (err) { 407 mutex_destroy(&ds->ds_lock); 408 mutex_destroy(&ds->ds_recvlock); 409 mutex_destroy(&ds->ds_opening_lock); 410 rw_destroy(&ds->ds_rwlock); 411 cv_destroy(&ds->ds_exclusive_cv); 412 bplist_destroy(&ds->ds_pending_deadlist); 413 dsl_deadlist_close(&ds->ds_deadlist); 414 kmem_free(ds, sizeof (dsl_dataset_t)); 415 dmu_buf_rele(dbuf, tag); 416 return (err); 417 } 418 419 if (!dsl_dataset_is_snapshot(ds)) { 420 ds->ds_snapname[0] = '\0'; 421 if (ds->ds_phys->ds_prev_snap_obj) { 422 err = dsl_dataset_get_ref(dp, 423 ds->ds_phys->ds_prev_snap_obj, 424 ds, &ds->ds_prev); 425 } 426 } else { 427 if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 428 err = dsl_dataset_get_snapname(ds); 429 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 430 err = zap_count( 431 ds->ds_dir->dd_pool->dp_meta_objset, 432 ds->ds_phys->ds_userrefs_obj, 433 &ds->ds_userrefs); 434 } 435 } 436 437 if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 438 /* 439 * In sync context, we're called with either no lock 440 * or with the write lock. If we're not syncing, 441 * we're always called with the read lock held. 442 */ 443 boolean_t need_lock = 444 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 445 dsl_pool_sync_context(dp); 446 447 if (need_lock) 448 rw_enter(&dp->dp_config_rwlock, RW_READER); 449 450 err = dsl_prop_get_ds(ds, 451 "refreservation", sizeof (uint64_t), 1, 452 &ds->ds_reserved, NULL); 453 if (err == 0) { 454 err = dsl_prop_get_ds(ds, 455 "refquota", sizeof (uint64_t), 1, 456 &ds->ds_quota, NULL); 457 } 458 459 if (need_lock) 460 rw_exit(&dp->dp_config_rwlock); 461 } else { 462 ds->ds_reserved = ds->ds_quota = 0; 463 } 464 465 if (err == 0) { 466 winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 467 dsl_dataset_evict); 468 } 469 if (err || winner) { 470 bplist_destroy(&ds->ds_pending_deadlist); 471 dsl_deadlist_close(&ds->ds_deadlist); 472 if (ds->ds_prev) 473 dsl_dataset_drop_ref(ds->ds_prev, ds); 474 dsl_dir_close(ds->ds_dir, ds); 475 mutex_destroy(&ds->ds_lock); 476 mutex_destroy(&ds->ds_recvlock); 477 mutex_destroy(&ds->ds_opening_lock); 478 rw_destroy(&ds->ds_rwlock); 479 cv_destroy(&ds->ds_exclusive_cv); 480 kmem_free(ds, sizeof (dsl_dataset_t)); 481 if (err) { 482 dmu_buf_rele(dbuf, tag); 483 return (err); 484 } 485 ds = winner; 486 } else { 487 ds->ds_fsid_guid = 488 unique_insert(ds->ds_phys->ds_fsid_guid); 489 } 490 } 491 ASSERT3P(ds->ds_dbuf, ==, dbuf); 492 ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 493 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 494 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 495 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 496 mutex_enter(&ds->ds_lock); 497 if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 498 mutex_exit(&ds->ds_lock); 499 dmu_buf_rele(ds->ds_dbuf, tag); 500 return (ENOENT); 501 } 502 mutex_exit(&ds->ds_lock); 503 *dsp = ds; 504 return (0); 505 } 506 507 static int 508 dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 509 { 510 dsl_pool_t *dp = ds->ds_dir->dd_pool; 511 512 /* 513 * In syncing context we don't want the rwlock lock: there 514 * may be an existing writer waiting for sync phase to 515 * finish. We don't need to worry about such writers, since 516 * sync phase is single-threaded, so the writer can't be 517 * doing anything while we are active. 518 */ 519 if (dsl_pool_sync_context(dp)) { 520 ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 521 return (0); 522 } 523 524 /* 525 * Normal users will hold the ds_rwlock as a READER until they 526 * are finished (i.e., call dsl_dataset_rele()). "Owners" will 527 * drop their READER lock after they set the ds_owner field. 528 * 529 * If the dataset is being destroyed, the destroy thread will 530 * obtain a WRITER lock for exclusive access after it's done its 531 * open-context work and then change the ds_owner to 532 * dsl_reaper once destruction is assured. So threads 533 * may block here temporarily, until the "destructability" of 534 * the dataset is determined. 535 */ 536 ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 537 mutex_enter(&ds->ds_lock); 538 while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 539 rw_exit(&dp->dp_config_rwlock); 540 cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 541 if (DSL_DATASET_IS_DESTROYED(ds)) { 542 mutex_exit(&ds->ds_lock); 543 dsl_dataset_drop_ref(ds, tag); 544 rw_enter(&dp->dp_config_rwlock, RW_READER); 545 return (ENOENT); 546 } 547 /* 548 * The dp_config_rwlock lives above the ds_lock. And 549 * we need to check DSL_DATASET_IS_DESTROYED() while 550 * holding the ds_lock, so we have to drop and reacquire 551 * the ds_lock here. 552 */ 553 mutex_exit(&ds->ds_lock); 554 rw_enter(&dp->dp_config_rwlock, RW_READER); 555 mutex_enter(&ds->ds_lock); 556 } 557 mutex_exit(&ds->ds_lock); 558 return (0); 559 } 560 561 int 562 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 563 dsl_dataset_t **dsp) 564 { 565 int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 566 567 if (err) 568 return (err); 569 return (dsl_dataset_hold_ref(*dsp, tag)); 570 } 571 572 int 573 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok, 574 void *tag, dsl_dataset_t **dsp) 575 { 576 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 577 if (err) 578 return (err); 579 if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 580 dsl_dataset_rele(*dsp, tag); 581 *dsp = NULL; 582 return (EBUSY); 583 } 584 return (0); 585 } 586 587 int 588 dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 589 { 590 dsl_dir_t *dd; 591 dsl_pool_t *dp; 592 const char *snapname; 593 uint64_t obj; 594 int err = 0; 595 596 err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 597 if (err) 598 return (err); 599 600 dp = dd->dd_pool; 601 obj = dd->dd_phys->dd_head_dataset_obj; 602 rw_enter(&dp->dp_config_rwlock, RW_READER); 603 if (obj) 604 err = dsl_dataset_get_ref(dp, obj, tag, dsp); 605 else 606 err = ENOENT; 607 if (err) 608 goto out; 609 610 err = dsl_dataset_hold_ref(*dsp, tag); 611 612 /* we may be looking for a snapshot */ 613 if (err == 0 && snapname != NULL) { 614 dsl_dataset_t *ds = NULL; 615 616 if (*snapname++ != '@') { 617 dsl_dataset_rele(*dsp, tag); 618 err = ENOENT; 619 goto out; 620 } 621 622 dprintf("looking for snapshot '%s'\n", snapname); 623 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 624 if (err == 0) 625 err = dsl_dataset_get_ref(dp, obj, tag, &ds); 626 dsl_dataset_rele(*dsp, tag); 627 628 ASSERT3U((err == 0), ==, (ds != NULL)); 629 630 if (ds) { 631 mutex_enter(&ds->ds_lock); 632 if (ds->ds_snapname[0] == 0) 633 (void) strlcpy(ds->ds_snapname, snapname, 634 sizeof (ds->ds_snapname)); 635 mutex_exit(&ds->ds_lock); 636 err = dsl_dataset_hold_ref(ds, tag); 637 *dsp = err ? NULL : ds; 638 } 639 } 640 out: 641 rw_exit(&dp->dp_config_rwlock); 642 dsl_dir_close(dd, FTAG); 643 return (err); 644 } 645 646 int 647 dsl_dataset_own(const char *name, boolean_t inconsistentok, 648 void *tag, dsl_dataset_t **dsp) 649 { 650 int err = dsl_dataset_hold(name, tag, dsp); 651 if (err) 652 return (err); 653 if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 654 dsl_dataset_rele(*dsp, tag); 655 return (EBUSY); 656 } 657 return (0); 658 } 659 660 void 661 dsl_dataset_name(dsl_dataset_t *ds, char *name) 662 { 663 if (ds == NULL) { 664 (void) strcpy(name, "mos"); 665 } else { 666 dsl_dir_name(ds->ds_dir, name); 667 VERIFY(0 == dsl_dataset_get_snapname(ds)); 668 if (ds->ds_snapname[0]) { 669 (void) strcat(name, "@"); 670 /* 671 * We use a "recursive" mutex so that we 672 * can call dprintf_ds() with ds_lock held. 673 */ 674 if (!MUTEX_HELD(&ds->ds_lock)) { 675 mutex_enter(&ds->ds_lock); 676 (void) strcat(name, ds->ds_snapname); 677 mutex_exit(&ds->ds_lock); 678 } else { 679 (void) strcat(name, ds->ds_snapname); 680 } 681 } 682 } 683 } 684 685 static int 686 dsl_dataset_namelen(dsl_dataset_t *ds) 687 { 688 int result; 689 690 if (ds == NULL) { 691 result = 3; /* "mos" */ 692 } else { 693 result = dsl_dir_namelen(ds->ds_dir); 694 VERIFY(0 == dsl_dataset_get_snapname(ds)); 695 if (ds->ds_snapname[0]) { 696 ++result; /* adding one for the @-sign */ 697 if (!MUTEX_HELD(&ds->ds_lock)) { 698 mutex_enter(&ds->ds_lock); 699 result += strlen(ds->ds_snapname); 700 mutex_exit(&ds->ds_lock); 701 } else { 702 result += strlen(ds->ds_snapname); 703 } 704 } 705 } 706 707 return (result); 708 } 709 710 void 711 dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 712 { 713 dmu_buf_rele(ds->ds_dbuf, tag); 714 } 715 716 void 717 dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 718 { 719 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 720 rw_exit(&ds->ds_rwlock); 721 } 722 dsl_dataset_drop_ref(ds, tag); 723 } 724 725 void 726 dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 727 { 728 ASSERT((ds->ds_owner == tag && ds->ds_dbuf) || 729 (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 730 731 mutex_enter(&ds->ds_lock); 732 ds->ds_owner = NULL; 733 if (RW_WRITE_HELD(&ds->ds_rwlock)) { 734 rw_exit(&ds->ds_rwlock); 735 cv_broadcast(&ds->ds_exclusive_cv); 736 } 737 mutex_exit(&ds->ds_lock); 738 if (ds->ds_dbuf) 739 dsl_dataset_drop_ref(ds, tag); 740 else 741 dsl_dataset_evict(NULL, ds); 742 } 743 744 boolean_t 745 dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag) 746 { 747 boolean_t gotit = FALSE; 748 749 mutex_enter(&ds->ds_lock); 750 if (ds->ds_owner == NULL && 751 (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 752 ds->ds_owner = tag; 753 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 754 rw_exit(&ds->ds_rwlock); 755 gotit = TRUE; 756 } 757 mutex_exit(&ds->ds_lock); 758 return (gotit); 759 } 760 761 void 762 dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 763 { 764 ASSERT3P(owner, ==, ds->ds_owner); 765 if (!RW_WRITE_HELD(&ds->ds_rwlock)) 766 rw_enter(&ds->ds_rwlock, RW_WRITER); 767 } 768 769 uint64_t 770 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 771 uint64_t flags, dmu_tx_t *tx) 772 { 773 dsl_pool_t *dp = dd->dd_pool; 774 dmu_buf_t *dbuf; 775 dsl_dataset_phys_t *dsphys; 776 uint64_t dsobj; 777 objset_t *mos = dp->dp_meta_objset; 778 779 if (origin == NULL) 780 origin = dp->dp_origin_snap; 781 782 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 783 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 784 ASSERT(dmu_tx_is_syncing(tx)); 785 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 786 787 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 788 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 789 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 790 dmu_buf_will_dirty(dbuf, tx); 791 dsphys = dbuf->db_data; 792 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 793 dsphys->ds_dir_obj = dd->dd_object; 794 dsphys->ds_flags = flags; 795 dsphys->ds_fsid_guid = unique_create(); 796 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 797 sizeof (dsphys->ds_guid)); 798 dsphys->ds_snapnames_zapobj = 799 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 800 DMU_OT_NONE, 0, tx); 801 dsphys->ds_creation_time = gethrestime_sec(); 802 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 803 804 if (origin == NULL) { 805 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 806 } else { 807 dsl_dataset_t *ohds; 808 809 dsphys->ds_prev_snap_obj = origin->ds_object; 810 dsphys->ds_prev_snap_txg = 811 origin->ds_phys->ds_creation_txg; 812 dsphys->ds_used_bytes = 813 origin->ds_phys->ds_used_bytes; 814 dsphys->ds_compressed_bytes = 815 origin->ds_phys->ds_compressed_bytes; 816 dsphys->ds_uncompressed_bytes = 817 origin->ds_phys->ds_uncompressed_bytes; 818 dsphys->ds_bp = origin->ds_phys->ds_bp; 819 dsphys->ds_flags |= origin->ds_phys->ds_flags; 820 821 dmu_buf_will_dirty(origin->ds_dbuf, tx); 822 origin->ds_phys->ds_num_children++; 823 824 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, 825 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds)); 826 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 827 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 828 dsl_dataset_rele(ohds, FTAG); 829 830 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 831 if (origin->ds_phys->ds_next_clones_obj == 0) { 832 origin->ds_phys->ds_next_clones_obj = 833 zap_create(mos, 834 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 835 } 836 VERIFY(0 == zap_add_int(mos, 837 origin->ds_phys->ds_next_clones_obj, 838 dsobj, tx)); 839 } 840 841 dmu_buf_will_dirty(dd->dd_dbuf, tx); 842 dd->dd_phys->dd_origin_obj = origin->ds_object; 843 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 844 if (origin->ds_dir->dd_phys->dd_clones == 0) { 845 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 846 origin->ds_dir->dd_phys->dd_clones = 847 zap_create(mos, 848 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 849 } 850 VERIFY3U(0, ==, zap_add_int(mos, 851 origin->ds_dir->dd_phys->dd_clones, dsobj, tx)); 852 } 853 } 854 855 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 856 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 857 858 dmu_buf_rele(dbuf, FTAG); 859 860 dmu_buf_will_dirty(dd->dd_dbuf, tx); 861 dd->dd_phys->dd_head_dataset_obj = dsobj; 862 863 return (dsobj); 864 } 865 866 uint64_t 867 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 868 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 869 { 870 dsl_pool_t *dp = pdd->dd_pool; 871 uint64_t dsobj, ddobj; 872 dsl_dir_t *dd; 873 874 ASSERT(lastname[0] != '@'); 875 876 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 877 VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 878 879 dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 880 881 dsl_deleg_set_create_perms(dd, tx, cr); 882 883 dsl_dir_close(dd, FTAG); 884 885 return (dsobj); 886 } 887 888 struct destroyarg { 889 dsl_sync_task_group_t *dstg; 890 char *snapname; 891 char *failed; 892 boolean_t defer; 893 }; 894 895 static int 896 dsl_snapshot_destroy_one(const char *name, void *arg) 897 { 898 struct destroyarg *da = arg; 899 dsl_dataset_t *ds; 900 int err; 901 char *dsname; 902 903 dsname = kmem_asprintf("%s@%s", name, da->snapname); 904 err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds); 905 strfree(dsname); 906 if (err == 0) { 907 struct dsl_ds_destroyarg *dsda; 908 909 dsl_dataset_make_exclusive(ds, da->dstg); 910 dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP); 911 dsda->ds = ds; 912 dsda->defer = da->defer; 913 dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 914 dsl_dataset_destroy_sync, dsda, da->dstg, 0); 915 } else if (err == ENOENT) { 916 err = 0; 917 } else { 918 (void) strcpy(da->failed, name); 919 } 920 return (err); 921 } 922 923 /* 924 * Destroy 'snapname' in all descendants of 'fsname'. 925 */ 926 #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 927 int 928 dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer) 929 { 930 int err; 931 struct destroyarg da; 932 dsl_sync_task_t *dst; 933 spa_t *spa; 934 935 err = spa_open(fsname, &spa, FTAG); 936 if (err) 937 return (err); 938 da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 939 da.snapname = snapname; 940 da.failed = fsname; 941 da.defer = defer; 942 943 err = dmu_objset_find(fsname, 944 dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 945 946 if (err == 0) 947 err = dsl_sync_task_group_wait(da.dstg); 948 949 for (dst = list_head(&da.dstg->dstg_tasks); dst; 950 dst = list_next(&da.dstg->dstg_tasks, dst)) { 951 struct dsl_ds_destroyarg *dsda = dst->dst_arg1; 952 dsl_dataset_t *ds = dsda->ds; 953 954 /* 955 * Return the file system name that triggered the error 956 */ 957 if (dst->dst_err) { 958 dsl_dataset_name(ds, fsname); 959 *strchr(fsname, '@') = '\0'; 960 } 961 ASSERT3P(dsda->rm_origin, ==, NULL); 962 dsl_dataset_disown(ds, da.dstg); 963 kmem_free(dsda, sizeof (struct dsl_ds_destroyarg)); 964 } 965 966 dsl_sync_task_group_destroy(da.dstg); 967 spa_close(spa, FTAG); 968 return (err); 969 } 970 971 static boolean_t 972 dsl_dataset_might_destroy_origin(dsl_dataset_t *ds) 973 { 974 boolean_t might_destroy = B_FALSE; 975 976 mutex_enter(&ds->ds_lock); 977 if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 && 978 DS_IS_DEFER_DESTROY(ds)) 979 might_destroy = B_TRUE; 980 mutex_exit(&ds->ds_lock); 981 982 return (might_destroy); 983 } 984 985 /* 986 * If we're removing a clone, and these three conditions are true: 987 * 1) the clone's origin has no other children 988 * 2) the clone's origin has no user references 989 * 3) the clone's origin has been marked for deferred destruction 990 * Then, prepare to remove the origin as part of this sync task group. 991 */ 992 static int 993 dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag) 994 { 995 dsl_dataset_t *ds = dsda->ds; 996 dsl_dataset_t *origin = ds->ds_prev; 997 998 if (dsl_dataset_might_destroy_origin(origin)) { 999 char *name; 1000 int namelen; 1001 int error; 1002 1003 namelen = dsl_dataset_namelen(origin) + 1; 1004 name = kmem_alloc(namelen, KM_SLEEP); 1005 dsl_dataset_name(origin, name); 1006 #ifdef _KERNEL 1007 error = zfs_unmount_snap(name, NULL); 1008 if (error) { 1009 kmem_free(name, namelen); 1010 return (error); 1011 } 1012 #endif 1013 error = dsl_dataset_own(name, B_TRUE, tag, &origin); 1014 kmem_free(name, namelen); 1015 if (error) 1016 return (error); 1017 dsda->rm_origin = origin; 1018 dsl_dataset_make_exclusive(origin, tag); 1019 } 1020 1021 return (0); 1022 } 1023 1024 /* 1025 * ds must be opened as OWNER. On return (whether successful or not), 1026 * ds will be closed and caller can no longer dereference it. 1027 */ 1028 int 1029 dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer) 1030 { 1031 int err; 1032 dsl_sync_task_group_t *dstg; 1033 objset_t *os; 1034 dsl_dir_t *dd; 1035 uint64_t obj; 1036 struct dsl_ds_destroyarg dsda = { 0 }; 1037 dsl_dataset_t dummy_ds = { 0 }; 1038 1039 dsda.ds = ds; 1040 1041 if (dsl_dataset_is_snapshot(ds)) { 1042 /* Destroying a snapshot is simpler */ 1043 dsl_dataset_make_exclusive(ds, tag); 1044 1045 dsda.defer = defer; 1046 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1047 dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 1048 &dsda, tag, 0); 1049 ASSERT3P(dsda.rm_origin, ==, NULL); 1050 goto out; 1051 } else if (defer) { 1052 err = EINVAL; 1053 goto out; 1054 } 1055 1056 dd = ds->ds_dir; 1057 dummy_ds.ds_dir = dd; 1058 dummy_ds.ds_object = ds->ds_object; 1059 1060 /* 1061 * Check for errors and mark this ds as inconsistent, in 1062 * case we crash while freeing the objects. 1063 */ 1064 err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 1065 dsl_dataset_destroy_begin_sync, ds, NULL, 0); 1066 if (err) 1067 goto out; 1068 1069 err = dmu_objset_from_ds(ds, &os); 1070 if (err) 1071 goto out; 1072 1073 /* 1074 * remove the objects in open context, so that we won't 1075 * have too much to do in syncing context. 1076 */ 1077 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 1078 ds->ds_phys->ds_prev_snap_txg)) { 1079 /* 1080 * Ignore errors, if there is not enough disk space 1081 * we will deal with it in dsl_dataset_destroy_sync(). 1082 */ 1083 (void) dmu_free_object(os, obj); 1084 } 1085 1086 /* 1087 * We need to sync out all in-flight IO before we try to evict 1088 * (the dataset evict func is trying to clear the cached entries 1089 * for this dataset in the ARC). 1090 */ 1091 txg_wait_synced(dd->dd_pool, 0); 1092 1093 /* 1094 * If we managed to free all the objects in open 1095 * context, the user space accounting should be zero. 1096 */ 1097 if (ds->ds_phys->ds_bp.blk_fill == 0 && 1098 dmu_objset_userused_enabled(os)) { 1099 uint64_t count; 1100 1101 ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 || 1102 count == 0); 1103 ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 || 1104 count == 0); 1105 } 1106 1107 if (err != ESRCH) 1108 goto out; 1109 1110 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 1111 err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 1112 rw_exit(&dd->dd_pool->dp_config_rwlock); 1113 1114 if (err) 1115 goto out; 1116 1117 /* 1118 * Blow away the dsl_dir + head dataset. 1119 */ 1120 dsl_dataset_make_exclusive(ds, tag); 1121 /* 1122 * If we're removing a clone, we might also need to remove its 1123 * origin. 1124 */ 1125 do { 1126 dsda.need_prep = B_FALSE; 1127 if (dsl_dir_is_clone(dd)) { 1128 err = dsl_dataset_origin_rm_prep(&dsda, tag); 1129 if (err) { 1130 dsl_dir_close(dd, FTAG); 1131 goto out; 1132 } 1133 } 1134 1135 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 1136 dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 1137 dsl_dataset_destroy_sync, &dsda, tag, 0); 1138 dsl_sync_task_create(dstg, dsl_dir_destroy_check, 1139 dsl_dir_destroy_sync, &dummy_ds, FTAG, 0); 1140 err = dsl_sync_task_group_wait(dstg); 1141 dsl_sync_task_group_destroy(dstg); 1142 1143 /* 1144 * We could be racing against 'zfs release' or 'zfs destroy -d' 1145 * on the origin snap, in which case we can get EBUSY if we 1146 * needed to destroy the origin snap but were not ready to 1147 * do so. 1148 */ 1149 if (dsda.need_prep) { 1150 ASSERT(err == EBUSY); 1151 ASSERT(dsl_dir_is_clone(dd)); 1152 ASSERT(dsda.rm_origin == NULL); 1153 } 1154 } while (dsda.need_prep); 1155 1156 if (dsda.rm_origin != NULL) 1157 dsl_dataset_disown(dsda.rm_origin, tag); 1158 1159 /* if it is successful, dsl_dir_destroy_sync will close the dd */ 1160 if (err) 1161 dsl_dir_close(dd, FTAG); 1162 out: 1163 dsl_dataset_disown(ds, tag); 1164 return (err); 1165 } 1166 1167 blkptr_t * 1168 dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1169 { 1170 return (&ds->ds_phys->ds_bp); 1171 } 1172 1173 void 1174 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1175 { 1176 ASSERT(dmu_tx_is_syncing(tx)); 1177 /* If it's the meta-objset, set dp_meta_rootbp */ 1178 if (ds == NULL) { 1179 tx->tx_pool->dp_meta_rootbp = *bp; 1180 } else { 1181 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1182 ds->ds_phys->ds_bp = *bp; 1183 } 1184 } 1185 1186 spa_t * 1187 dsl_dataset_get_spa(dsl_dataset_t *ds) 1188 { 1189 return (ds->ds_dir->dd_pool->dp_spa); 1190 } 1191 1192 void 1193 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1194 { 1195 dsl_pool_t *dp; 1196 1197 if (ds == NULL) /* this is the meta-objset */ 1198 return; 1199 1200 ASSERT(ds->ds_objset != NULL); 1201 1202 if (ds->ds_phys->ds_next_snap_obj != 0) 1203 panic("dirtying snapshot!"); 1204 1205 dp = ds->ds_dir->dd_pool; 1206 1207 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1208 /* up the hold count until we can be written out */ 1209 dmu_buf_add_ref(ds->ds_dbuf, ds); 1210 } 1211 } 1212 1213 /* 1214 * The unique space in the head dataset can be calculated by subtracting 1215 * the space used in the most recent snapshot, that is still being used 1216 * in this file system, from the space currently in use. To figure out 1217 * the space in the most recent snapshot still in use, we need to take 1218 * the total space used in the snapshot and subtract out the space that 1219 * has been freed up since the snapshot was taken. 1220 */ 1221 static void 1222 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 1223 { 1224 uint64_t mrs_used; 1225 uint64_t dlused, dlcomp, dluncomp; 1226 1227 ASSERT(!dsl_dataset_is_snapshot(ds)); 1228 1229 if (ds->ds_phys->ds_prev_snap_obj != 0) 1230 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 1231 else 1232 mrs_used = 0; 1233 1234 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 1235 1236 ASSERT3U(dlused, <=, mrs_used); 1237 ds->ds_phys->ds_unique_bytes = 1238 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 1239 1240 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 1241 SPA_VERSION_UNIQUE_ACCURATE) 1242 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1243 } 1244 1245 struct killarg { 1246 dsl_dataset_t *ds; 1247 dmu_tx_t *tx; 1248 }; 1249 1250 /* ARGSUSED */ 1251 static int 1252 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf, 1253 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1254 { 1255 struct killarg *ka = arg; 1256 dmu_tx_t *tx = ka->tx; 1257 1258 if (bp == NULL) 1259 return (0); 1260 1261 if (zb->zb_level == ZB_ZIL_LEVEL) { 1262 ASSERT(zilog != NULL); 1263 /* 1264 * It's a block in the intent log. It has no 1265 * accounting, so just free it. 1266 */ 1267 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 1268 } else { 1269 ASSERT(zilog == NULL); 1270 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 1271 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 1272 } 1273 1274 return (0); 1275 } 1276 1277 /* ARGSUSED */ 1278 static int 1279 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 1280 { 1281 dsl_dataset_t *ds = arg1; 1282 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1283 uint64_t count; 1284 int err; 1285 1286 /* 1287 * Can't delete a head dataset if there are snapshots of it. 1288 * (Except if the only snapshots are from the branch we cloned 1289 * from.) 1290 */ 1291 if (ds->ds_prev != NULL && 1292 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1293 return (EBUSY); 1294 1295 /* 1296 * This is really a dsl_dir thing, but check it here so that 1297 * we'll be less likely to leave this dataset inconsistent & 1298 * nearly destroyed. 1299 */ 1300 err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 1301 if (err) 1302 return (err); 1303 if (count != 0) 1304 return (EEXIST); 1305 1306 return (0); 1307 } 1308 1309 /* ARGSUSED */ 1310 static void 1311 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx) 1312 { 1313 dsl_dataset_t *ds = arg1; 1314 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1315 1316 /* Mark it as inconsistent on-disk, in case we crash */ 1317 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1318 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 1319 1320 spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 1321 "dataset = %llu", ds->ds_object); 1322 } 1323 1324 static int 1325 dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag, 1326 dmu_tx_t *tx) 1327 { 1328 dsl_dataset_t *ds = dsda->ds; 1329 dsl_dataset_t *ds_prev = ds->ds_prev; 1330 1331 if (dsl_dataset_might_destroy_origin(ds_prev)) { 1332 struct dsl_ds_destroyarg ndsda = {0}; 1333 1334 /* 1335 * If we're not prepared to remove the origin, don't remove 1336 * the clone either. 1337 */ 1338 if (dsda->rm_origin == NULL) { 1339 dsda->need_prep = B_TRUE; 1340 return (EBUSY); 1341 } 1342 1343 ndsda.ds = ds_prev; 1344 ndsda.is_origin_rm = B_TRUE; 1345 return (dsl_dataset_destroy_check(&ndsda, tag, tx)); 1346 } 1347 1348 /* 1349 * If we're not going to remove the origin after all, 1350 * undo the open context setup. 1351 */ 1352 if (dsda->rm_origin != NULL) { 1353 dsl_dataset_disown(dsda->rm_origin, tag); 1354 dsda->rm_origin = NULL; 1355 } 1356 1357 return (0); 1358 } 1359 1360 /* ARGSUSED */ 1361 int 1362 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 1363 { 1364 struct dsl_ds_destroyarg *dsda = arg1; 1365 dsl_dataset_t *ds = dsda->ds; 1366 1367 /* we have an owner hold, so noone else can destroy us */ 1368 ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 1369 1370 /* 1371 * Only allow deferred destroy on pools that support it. 1372 * NOTE: deferred destroy is only supported on snapshots. 1373 */ 1374 if (dsda->defer) { 1375 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 1376 SPA_VERSION_USERREFS) 1377 return (ENOTSUP); 1378 ASSERT(dsl_dataset_is_snapshot(ds)); 1379 return (0); 1380 } 1381 1382 /* 1383 * Can't delete a head dataset if there are snapshots of it. 1384 * (Except if the only snapshots are from the branch we cloned 1385 * from.) 1386 */ 1387 if (ds->ds_prev != NULL && 1388 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1389 return (EBUSY); 1390 1391 /* 1392 * If we made changes this txg, traverse_dsl_dataset won't find 1393 * them. Try again. 1394 */ 1395 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1396 return (EAGAIN); 1397 1398 if (dsl_dataset_is_snapshot(ds)) { 1399 /* 1400 * If this snapshot has an elevated user reference count, 1401 * we can't destroy it yet. 1402 */ 1403 if (ds->ds_userrefs > 0 && !dsda->releasing) 1404 return (EBUSY); 1405 1406 mutex_enter(&ds->ds_lock); 1407 /* 1408 * Can't delete a branch point. However, if we're destroying 1409 * a clone and removing its origin due to it having a user 1410 * hold count of 0 and having been marked for deferred destroy, 1411 * it's OK for the origin to have a single clone. 1412 */ 1413 if (ds->ds_phys->ds_num_children > 1414 (dsda->is_origin_rm ? 2 : 1)) { 1415 mutex_exit(&ds->ds_lock); 1416 return (EEXIST); 1417 } 1418 mutex_exit(&ds->ds_lock); 1419 } else if (dsl_dir_is_clone(ds->ds_dir)) { 1420 return (dsl_dataset_origin_check(dsda, arg2, tx)); 1421 } 1422 1423 /* XXX we should do some i/o error checking... */ 1424 return (0); 1425 } 1426 1427 struct refsarg { 1428 kmutex_t lock; 1429 boolean_t gone; 1430 kcondvar_t cv; 1431 }; 1432 1433 /* ARGSUSED */ 1434 static void 1435 dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 1436 { 1437 struct refsarg *arg = argv; 1438 1439 mutex_enter(&arg->lock); 1440 arg->gone = TRUE; 1441 cv_signal(&arg->cv); 1442 mutex_exit(&arg->lock); 1443 } 1444 1445 static void 1446 dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 1447 { 1448 struct refsarg arg; 1449 1450 mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 1451 cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 1452 arg.gone = FALSE; 1453 (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 1454 dsl_dataset_refs_gone); 1455 dmu_buf_rele(ds->ds_dbuf, tag); 1456 mutex_enter(&arg.lock); 1457 while (!arg.gone) 1458 cv_wait(&arg.cv, &arg.lock); 1459 ASSERT(arg.gone); 1460 mutex_exit(&arg.lock); 1461 ds->ds_dbuf = NULL; 1462 ds->ds_phys = NULL; 1463 mutex_destroy(&arg.lock); 1464 cv_destroy(&arg.cv); 1465 } 1466 1467 static void 1468 remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx) 1469 { 1470 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1471 uint64_t count; 1472 int err; 1473 1474 ASSERT(ds->ds_phys->ds_num_children >= 2); 1475 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 1476 /* 1477 * The err should not be ENOENT, but a bug in a previous version 1478 * of the code could cause upgrade_clones_cb() to not set 1479 * ds_next_snap_obj when it should, leading to a missing entry. 1480 * If we knew that the pool was created after 1481 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 1482 * ENOENT. However, at least we can check that we don't have 1483 * too many entries in the next_clones_obj even after failing to 1484 * remove this one. 1485 */ 1486 if (err != ENOENT) { 1487 VERIFY3U(err, ==, 0); 1488 } 1489 ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj, 1490 &count)); 1491 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 1492 } 1493 1494 static void 1495 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx) 1496 { 1497 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1498 zap_cursor_t zc; 1499 zap_attribute_t za; 1500 1501 /* 1502 * If it is the old version, dd_clones doesn't exist so we can't 1503 * find the clones, but deadlist_remove_key() is a no-op so it 1504 * doesn't matter. 1505 */ 1506 if (ds->ds_dir->dd_phys->dd_clones == 0) 1507 return; 1508 1509 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones); 1510 zap_cursor_retrieve(&zc, &za) == 0; 1511 zap_cursor_advance(&zc)) { 1512 dsl_dataset_t *clone; 1513 1514 VERIFY3U(0, ==, dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 1515 za.za_first_integer, FTAG, &clone)); 1516 if (clone->ds_dir->dd_origin_txg > mintxg) { 1517 dsl_deadlist_remove_key(&clone->ds_deadlist, 1518 mintxg, tx); 1519 dsl_dataset_remove_clones_key(clone, mintxg, tx); 1520 } 1521 dsl_dataset_rele(clone, FTAG); 1522 } 1523 zap_cursor_fini(&zc); 1524 } 1525 1526 struct process_old_arg { 1527 dsl_dataset_t *ds; 1528 dsl_dataset_t *ds_prev; 1529 boolean_t after_branch_point; 1530 zio_t *pio; 1531 uint64_t used, comp, uncomp; 1532 }; 1533 1534 static int 1535 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1536 { 1537 struct process_old_arg *poa = arg; 1538 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool; 1539 1540 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) { 1541 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx); 1542 if (poa->ds_prev && !poa->after_branch_point && 1543 bp->blk_birth > 1544 poa->ds_prev->ds_phys->ds_prev_snap_txg) { 1545 poa->ds_prev->ds_phys->ds_unique_bytes += 1546 bp_get_dsize_sync(dp->dp_spa, bp); 1547 } 1548 } else { 1549 poa->used += bp_get_dsize_sync(dp->dp_spa, bp); 1550 poa->comp += BP_GET_PSIZE(bp); 1551 poa->uncomp += BP_GET_UCSIZE(bp); 1552 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp); 1553 } 1554 return (0); 1555 } 1556 1557 static void 1558 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev, 1559 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx) 1560 { 1561 struct process_old_arg poa = { 0 }; 1562 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1563 objset_t *mos = dp->dp_meta_objset; 1564 1565 ASSERT(ds->ds_deadlist.dl_oldfmt); 1566 ASSERT(ds_next->ds_deadlist.dl_oldfmt); 1567 1568 poa.ds = ds; 1569 poa.ds_prev = ds_prev; 1570 poa.after_branch_point = after_branch_point; 1571 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1572 VERIFY3U(0, ==, bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj, 1573 process_old_cb, &poa, tx)); 1574 VERIFY3U(zio_wait(poa.pio), ==, 0); 1575 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes); 1576 1577 /* change snapused */ 1578 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 1579 -poa.used, -poa.comp, -poa.uncomp, tx); 1580 1581 /* swap next's deadlist to our deadlist */ 1582 dsl_deadlist_close(&ds->ds_deadlist); 1583 dsl_deadlist_close(&ds_next->ds_deadlist); 1584 SWITCH64(ds_next->ds_phys->ds_deadlist_obj, 1585 ds->ds_phys->ds_deadlist_obj); 1586 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 1587 dsl_deadlist_open(&ds_next->ds_deadlist, mos, 1588 ds_next->ds_phys->ds_deadlist_obj); 1589 } 1590 1591 void 1592 dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx) 1593 { 1594 struct dsl_ds_destroyarg *dsda = arg1; 1595 dsl_dataset_t *ds = dsda->ds; 1596 int err; 1597 int after_branch_point = FALSE; 1598 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1599 objset_t *mos = dp->dp_meta_objset; 1600 dsl_dataset_t *ds_prev = NULL; 1601 uint64_t obj; 1602 1603 ASSERT(ds->ds_owner); 1604 ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1); 1605 ASSERT(ds->ds_prev == NULL || 1606 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 1607 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 1608 1609 if (dsda->defer) { 1610 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 1611 if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) { 1612 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1613 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY; 1614 return; 1615 } 1616 } 1617 1618 /* signal any waiters that this dataset is going away */ 1619 mutex_enter(&ds->ds_lock); 1620 ds->ds_owner = dsl_reaper; 1621 cv_broadcast(&ds->ds_exclusive_cv); 1622 mutex_exit(&ds->ds_lock); 1623 1624 if (ds->ds_objset) { 1625 dmu_objset_evict(ds->ds_objset); 1626 ds->ds_objset = NULL; 1627 } 1628 1629 /* Remove our reservation */ 1630 if (ds->ds_reserved != 0) { 1631 dsl_prop_setarg_t psa; 1632 uint64_t value = 0; 1633 1634 dsl_prop_setarg_init_uint64(&psa, "refreservation", 1635 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), 1636 &value); 1637 psa.psa_effective_value = 0; /* predict default value */ 1638 1639 dsl_dataset_set_reservation_sync(ds, &psa, tx); 1640 ASSERT3U(ds->ds_reserved, ==, 0); 1641 } 1642 1643 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1644 1645 dsl_scan_ds_destroyed(ds, tx); 1646 1647 obj = ds->ds_object; 1648 1649 if (ds->ds_phys->ds_prev_snap_obj != 0) { 1650 if (ds->ds_prev) { 1651 ds_prev = ds->ds_prev; 1652 } else { 1653 VERIFY(0 == dsl_dataset_hold_obj(dp, 1654 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1655 } 1656 after_branch_point = 1657 (ds_prev->ds_phys->ds_next_snap_obj != obj); 1658 1659 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1660 if (after_branch_point && 1661 ds_prev->ds_phys->ds_next_clones_obj != 0) { 1662 remove_from_next_clones(ds_prev, obj, tx); 1663 if (ds->ds_phys->ds_next_snap_obj != 0) { 1664 VERIFY(0 == zap_add_int(mos, 1665 ds_prev->ds_phys->ds_next_clones_obj, 1666 ds->ds_phys->ds_next_snap_obj, tx)); 1667 } 1668 } 1669 if (after_branch_point && 1670 ds->ds_phys->ds_next_snap_obj == 0) { 1671 /* This clone is toast. */ 1672 ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1673 ds_prev->ds_phys->ds_num_children--; 1674 1675 /* 1676 * If the clone's origin has no other clones, no 1677 * user holds, and has been marked for deferred 1678 * deletion, then we should have done the necessary 1679 * destroy setup for it. 1680 */ 1681 if (ds_prev->ds_phys->ds_num_children == 1 && 1682 ds_prev->ds_userrefs == 0 && 1683 DS_IS_DEFER_DESTROY(ds_prev)) { 1684 ASSERT3P(dsda->rm_origin, !=, NULL); 1685 } else { 1686 ASSERT3P(dsda->rm_origin, ==, NULL); 1687 } 1688 } else if (!after_branch_point) { 1689 ds_prev->ds_phys->ds_next_snap_obj = 1690 ds->ds_phys->ds_next_snap_obj; 1691 } 1692 } 1693 1694 if (dsl_dataset_is_snapshot(ds)) { 1695 dsl_dataset_t *ds_next; 1696 uint64_t old_unique; 1697 uint64_t used = 0, comp = 0, uncomp = 0; 1698 1699 VERIFY(0 == dsl_dataset_hold_obj(dp, 1700 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1701 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1702 1703 old_unique = ds_next->ds_phys->ds_unique_bytes; 1704 1705 dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1706 ds_next->ds_phys->ds_prev_snap_obj = 1707 ds->ds_phys->ds_prev_snap_obj; 1708 ds_next->ds_phys->ds_prev_snap_txg = 1709 ds->ds_phys->ds_prev_snap_txg; 1710 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1711 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1712 1713 1714 if (ds_next->ds_deadlist.dl_oldfmt) { 1715 process_old_deadlist(ds, ds_prev, ds_next, 1716 after_branch_point, tx); 1717 } else { 1718 /* Adjust prev's unique space. */ 1719 if (ds_prev && !after_branch_point) { 1720 dsl_deadlist_space_range(&ds_next->ds_deadlist, 1721 ds_prev->ds_phys->ds_prev_snap_txg, 1722 ds->ds_phys->ds_prev_snap_txg, 1723 &used, &comp, &uncomp); 1724 ds_prev->ds_phys->ds_unique_bytes += used; 1725 } 1726 1727 /* Adjust snapused. */ 1728 dsl_deadlist_space_range(&ds_next->ds_deadlist, 1729 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 1730 &used, &comp, &uncomp); 1731 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 1732 -used, -comp, -uncomp, tx); 1733 1734 /* Move blocks to be freed to pool's free list. */ 1735 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist, 1736 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg, 1737 tx); 1738 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, 1739 DD_USED_HEAD, used, comp, uncomp, tx); 1740 dsl_dir_dirty(tx->tx_pool->dp_free_dir, tx); 1741 1742 /* Merge our deadlist into next's and free it. */ 1743 dsl_deadlist_merge(&ds_next->ds_deadlist, 1744 ds->ds_phys->ds_deadlist_obj, tx); 1745 } 1746 dsl_deadlist_close(&ds->ds_deadlist); 1747 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx); 1748 1749 /* Collapse range in clone heads */ 1750 dsl_dataset_remove_clones_key(ds, 1751 ds->ds_phys->ds_creation_txg, tx); 1752 1753 if (dsl_dataset_is_snapshot(ds_next)) { 1754 dsl_dataset_t *ds_nextnext; 1755 1756 /* 1757 * Update next's unique to include blocks which 1758 * were previously shared by only this snapshot 1759 * and it. Those blocks will be born after the 1760 * prev snap and before this snap, and will have 1761 * died after the next snap and before the one 1762 * after that (ie. be on the snap after next's 1763 * deadlist). 1764 */ 1765 VERIFY(0 == dsl_dataset_hold_obj(dp, 1766 ds_next->ds_phys->ds_next_snap_obj, 1767 FTAG, &ds_nextnext)); 1768 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist, 1769 ds->ds_phys->ds_prev_snap_txg, 1770 ds->ds_phys->ds_creation_txg, 1771 &used, &comp, &uncomp); 1772 ds_next->ds_phys->ds_unique_bytes += used; 1773 dsl_dataset_rele(ds_nextnext, FTAG); 1774 ASSERT3P(ds_next->ds_prev, ==, NULL); 1775 1776 /* Collapse range in this head. */ 1777 dsl_dataset_t *hds; 1778 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, 1779 ds->ds_dir->dd_phys->dd_head_dataset_obj, 1780 FTAG, &hds)); 1781 dsl_deadlist_remove_key(&hds->ds_deadlist, 1782 ds->ds_phys->ds_creation_txg, tx); 1783 dsl_dataset_rele(hds, FTAG); 1784 1785 } else { 1786 ASSERT3P(ds_next->ds_prev, ==, ds); 1787 dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 1788 ds_next->ds_prev = NULL; 1789 if (ds_prev) { 1790 VERIFY(0 == dsl_dataset_get_ref(dp, 1791 ds->ds_phys->ds_prev_snap_obj, 1792 ds_next, &ds_next->ds_prev)); 1793 } 1794 1795 dsl_dataset_recalc_head_uniq(ds_next); 1796 1797 /* 1798 * Reduce the amount of our unconsmed refreservation 1799 * being charged to our parent by the amount of 1800 * new unique data we have gained. 1801 */ 1802 if (old_unique < ds_next->ds_reserved) { 1803 int64_t mrsdelta; 1804 uint64_t new_unique = 1805 ds_next->ds_phys->ds_unique_bytes; 1806 1807 ASSERT(old_unique <= new_unique); 1808 mrsdelta = MIN(new_unique - old_unique, 1809 ds_next->ds_reserved - old_unique); 1810 dsl_dir_diduse_space(ds->ds_dir, 1811 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 1812 } 1813 } 1814 dsl_dataset_rele(ds_next, FTAG); 1815 } else { 1816 /* 1817 * There's no next snapshot, so this is a head dataset. 1818 * Destroy the deadlist. Unless it's a clone, the 1819 * deadlist should be empty. (If it's a clone, it's 1820 * safe to ignore the deadlist contents.) 1821 */ 1822 struct killarg ka; 1823 1824 dsl_deadlist_close(&ds->ds_deadlist); 1825 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx); 1826 ds->ds_phys->ds_deadlist_obj = 0; 1827 1828 /* 1829 * Free everything that we point to (that's born after 1830 * the previous snapshot, if we are a clone) 1831 * 1832 * NB: this should be very quick, because we already 1833 * freed all the objects in open context. 1834 */ 1835 ka.ds = ds; 1836 ka.tx = tx; 1837 err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1838 TRAVERSE_POST, kill_blkptr, &ka); 1839 ASSERT3U(err, ==, 0); 1840 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 1841 ds->ds_phys->ds_unique_bytes == 0); 1842 1843 if (ds->ds_prev != NULL) { 1844 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 1845 VERIFY3U(0, ==, zap_remove_int(mos, 1846 ds->ds_prev->ds_dir->dd_phys->dd_clones, 1847 ds->ds_object, tx)); 1848 } 1849 dsl_dataset_rele(ds->ds_prev, ds); 1850 ds->ds_prev = ds_prev = NULL; 1851 } 1852 } 1853 1854 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 1855 /* Erase the link in the dir */ 1856 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1857 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 1858 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1859 err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1860 ASSERT(err == 0); 1861 } else { 1862 /* remove from snapshot namespace */ 1863 dsl_dataset_t *ds_head; 1864 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 1865 VERIFY(0 == dsl_dataset_hold_obj(dp, 1866 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 1867 VERIFY(0 == dsl_dataset_get_snapname(ds)); 1868 #ifdef ZFS_DEBUG 1869 { 1870 uint64_t val; 1871 1872 err = dsl_dataset_snap_lookup(ds_head, 1873 ds->ds_snapname, &val); 1874 ASSERT3U(err, ==, 0); 1875 ASSERT3U(val, ==, obj); 1876 } 1877 #endif 1878 err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1879 ASSERT(err == 0); 1880 dsl_dataset_rele(ds_head, FTAG); 1881 } 1882 1883 if (ds_prev && ds->ds_prev != ds_prev) 1884 dsl_dataset_rele(ds_prev, FTAG); 1885 1886 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 1887 spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx, 1888 "dataset = %llu", ds->ds_object); 1889 1890 if (ds->ds_phys->ds_next_clones_obj != 0) { 1891 uint64_t count; 1892 ASSERT(0 == zap_count(mos, 1893 ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 1894 VERIFY(0 == dmu_object_free(mos, 1895 ds->ds_phys->ds_next_clones_obj, tx)); 1896 } 1897 if (ds->ds_phys->ds_props_obj != 0) 1898 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 1899 if (ds->ds_phys->ds_userrefs_obj != 0) 1900 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx)); 1901 dsl_dir_close(ds->ds_dir, ds); 1902 ds->ds_dir = NULL; 1903 dsl_dataset_drain_refs(ds, tag); 1904 VERIFY(0 == dmu_object_free(mos, obj, tx)); 1905 1906 if (dsda->rm_origin) { 1907 /* 1908 * Remove the origin of the clone we just destroyed. 1909 */ 1910 struct dsl_ds_destroyarg ndsda = {0}; 1911 1912 ndsda.ds = dsda->rm_origin; 1913 dsl_dataset_destroy_sync(&ndsda, tag, tx); 1914 } 1915 } 1916 1917 static int 1918 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1919 { 1920 uint64_t asize; 1921 1922 if (!dmu_tx_is_syncing(tx)) 1923 return (0); 1924 1925 /* 1926 * If there's an fs-only reservation, any blocks that might become 1927 * owned by the snapshot dataset must be accommodated by space 1928 * outside of the reservation. 1929 */ 1930 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1931 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 1932 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 1933 return (ENOSPC); 1934 1935 /* 1936 * Propogate any reserved space for this snapshot to other 1937 * snapshot checks in this sync group. 1938 */ 1939 if (asize > 0) 1940 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 1941 1942 return (0); 1943 } 1944 1945 int 1946 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 1947 { 1948 dsl_dataset_t *ds = arg1; 1949 const char *snapname = arg2; 1950 int err; 1951 uint64_t value; 1952 1953 /* 1954 * We don't allow multiple snapshots of the same txg. If there 1955 * is already one, try again. 1956 */ 1957 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 1958 return (EAGAIN); 1959 1960 /* 1961 * Check for conflicting name snapshot name. 1962 */ 1963 err = dsl_dataset_snap_lookup(ds, snapname, &value); 1964 if (err == 0) 1965 return (EEXIST); 1966 if (err != ENOENT) 1967 return (err); 1968 1969 /* 1970 * Check that the dataset's name is not too long. Name consists 1971 * of the dataset's length + 1 for the @-sign + snapshot name's length 1972 */ 1973 if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 1974 return (ENAMETOOLONG); 1975 1976 err = dsl_dataset_snapshot_reserve_space(ds, tx); 1977 if (err) 1978 return (err); 1979 1980 ds->ds_trysnap_txg = tx->tx_txg; 1981 return (0); 1982 } 1983 1984 void 1985 dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx) 1986 { 1987 dsl_dataset_t *ds = arg1; 1988 const char *snapname = arg2; 1989 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1990 dmu_buf_t *dbuf; 1991 dsl_dataset_phys_t *dsphys; 1992 uint64_t dsobj, crtxg; 1993 objset_t *mos = dp->dp_meta_objset; 1994 int err; 1995 1996 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1997 1998 /* 1999 * The origin's ds_creation_txg has to be < TXG_INITIAL 2000 */ 2001 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 2002 crtxg = 1; 2003 else 2004 crtxg = tx->tx_txg; 2005 2006 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 2007 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 2008 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 2009 dmu_buf_will_dirty(dbuf, tx); 2010 dsphys = dbuf->db_data; 2011 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 2012 dsphys->ds_dir_obj = ds->ds_dir->dd_object; 2013 dsphys->ds_fsid_guid = unique_create(); 2014 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 2015 sizeof (dsphys->ds_guid)); 2016 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 2017 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 2018 dsphys->ds_next_snap_obj = ds->ds_object; 2019 dsphys->ds_num_children = 1; 2020 dsphys->ds_creation_time = gethrestime_sec(); 2021 dsphys->ds_creation_txg = crtxg; 2022 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 2023 dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 2024 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 2025 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 2026 dsphys->ds_flags = ds->ds_phys->ds_flags; 2027 dsphys->ds_bp = ds->ds_phys->ds_bp; 2028 dmu_buf_rele(dbuf, FTAG); 2029 2030 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 2031 if (ds->ds_prev) { 2032 uint64_t next_clones_obj = 2033 ds->ds_prev->ds_phys->ds_next_clones_obj; 2034 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 2035 ds->ds_object || 2036 ds->ds_prev->ds_phys->ds_num_children > 1); 2037 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 2038 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 2039 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 2040 ds->ds_prev->ds_phys->ds_creation_txg); 2041 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 2042 } else if (next_clones_obj != 0) { 2043 remove_from_next_clones(ds->ds_prev, 2044 dsphys->ds_next_snap_obj, tx); 2045 VERIFY3U(0, ==, zap_add_int(mos, 2046 next_clones_obj, dsobj, tx)); 2047 } 2048 } 2049 2050 /* 2051 * If we have a reference-reservation on this dataset, we will 2052 * need to increase the amount of refreservation being charged 2053 * since our unique space is going to zero. 2054 */ 2055 if (ds->ds_reserved) { 2056 int64_t delta; 2057 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 2058 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 2059 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 2060 delta, 0, 0, tx); 2061 } 2062 2063 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2064 zfs_dbgmsg("taking snapshot %s@%s/%llu; newkey=%llu", 2065 ds->ds_dir->dd_myname, snapname, dsobj, 2066 ds->ds_phys->ds_prev_snap_txg); 2067 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist, 2068 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx); 2069 dsl_deadlist_close(&ds->ds_deadlist); 2070 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 2071 dsl_deadlist_add_key(&ds->ds_deadlist, 2072 ds->ds_phys->ds_prev_snap_txg, tx); 2073 2074 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 2075 ds->ds_phys->ds_prev_snap_obj = dsobj; 2076 ds->ds_phys->ds_prev_snap_txg = crtxg; 2077 ds->ds_phys->ds_unique_bytes = 0; 2078 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 2079 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 2080 2081 err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 2082 snapname, 8, 1, &dsobj, tx); 2083 ASSERT(err == 0); 2084 2085 if (ds->ds_prev) 2086 dsl_dataset_drop_ref(ds->ds_prev, ds); 2087 VERIFY(0 == dsl_dataset_get_ref(dp, 2088 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 2089 2090 dsl_scan_ds_snapshotted(ds, tx); 2091 2092 dsl_dir_snap_cmtime_update(ds->ds_dir); 2093 2094 spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx, 2095 "dataset = %llu", dsobj); 2096 } 2097 2098 void 2099 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 2100 { 2101 ASSERT(dmu_tx_is_syncing(tx)); 2102 ASSERT(ds->ds_objset != NULL); 2103 ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 2104 2105 /* 2106 * in case we had to change ds_fsid_guid when we opened it, 2107 * sync it out now. 2108 */ 2109 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2110 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 2111 2112 dsl_dir_dirty(ds->ds_dir, tx); 2113 dmu_objset_sync(ds->ds_objset, zio, tx); 2114 } 2115 2116 void 2117 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2118 { 2119 uint64_t refd, avail, uobjs, aobjs; 2120 2121 dsl_dir_stats(ds->ds_dir, nv); 2122 2123 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 2124 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 2125 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 2126 2127 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 2128 ds->ds_phys->ds_creation_time); 2129 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 2130 ds->ds_phys->ds_creation_txg); 2131 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 2132 ds->ds_quota); 2133 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 2134 ds->ds_reserved); 2135 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 2136 ds->ds_phys->ds_guid); 2137 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 2138 ds->ds_phys->ds_unique_bytes); 2139 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 2140 ds->ds_object); 2141 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 2142 ds->ds_userrefs); 2143 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 2144 DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2145 2146 if (ds->ds_phys->ds_next_snap_obj) { 2147 /* 2148 * This is a snapshot; override the dd's space used with 2149 * our unique space and compression ratio. 2150 */ 2151 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 2152 ds->ds_phys->ds_unique_bytes); 2153 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 2154 ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 2155 (ds->ds_phys->ds_uncompressed_bytes * 100 / 2156 ds->ds_phys->ds_compressed_bytes)); 2157 } 2158 } 2159 2160 void 2161 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2162 { 2163 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 2164 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 2165 stat->dds_guid = ds->ds_phys->ds_guid; 2166 if (ds->ds_phys->ds_next_snap_obj) { 2167 stat->dds_is_snapshot = B_TRUE; 2168 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 2169 } else { 2170 stat->dds_is_snapshot = B_FALSE; 2171 stat->dds_num_clones = 0; 2172 } 2173 2174 /* clone origin is really a dsl_dir thing... */ 2175 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 2176 if (dsl_dir_is_clone(ds->ds_dir)) { 2177 dsl_dataset_t *ods; 2178 2179 VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 2180 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 2181 dsl_dataset_name(ods, stat->dds_origin); 2182 dsl_dataset_drop_ref(ods, FTAG); 2183 } else { 2184 stat->dds_origin[0] = '\0'; 2185 } 2186 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 2187 } 2188 2189 uint64_t 2190 dsl_dataset_fsid_guid(dsl_dataset_t *ds) 2191 { 2192 return (ds->ds_fsid_guid); 2193 } 2194 2195 void 2196 dsl_dataset_space(dsl_dataset_t *ds, 2197 uint64_t *refdbytesp, uint64_t *availbytesp, 2198 uint64_t *usedobjsp, uint64_t *availobjsp) 2199 { 2200 *refdbytesp = ds->ds_phys->ds_used_bytes; 2201 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 2202 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 2203 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 2204 if (ds->ds_quota != 0) { 2205 /* 2206 * Adjust available bytes according to refquota 2207 */ 2208 if (*refdbytesp < ds->ds_quota) 2209 *availbytesp = MIN(*availbytesp, 2210 ds->ds_quota - *refdbytesp); 2211 else 2212 *availbytesp = 0; 2213 } 2214 *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 2215 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2216 } 2217 2218 boolean_t 2219 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 2220 { 2221 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2222 2223 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 2224 dsl_pool_sync_context(dp)); 2225 if (ds->ds_prev == NULL) 2226 return (B_FALSE); 2227 if (ds->ds_phys->ds_bp.blk_birth > 2228 ds->ds_prev->ds_phys->ds_creation_txg) 2229 return (B_TRUE); 2230 return (B_FALSE); 2231 } 2232 2233 /* ARGSUSED */ 2234 static int 2235 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2236 { 2237 dsl_dataset_t *ds = arg1; 2238 char *newsnapname = arg2; 2239 dsl_dir_t *dd = ds->ds_dir; 2240 dsl_dataset_t *hds; 2241 uint64_t val; 2242 int err; 2243 2244 err = dsl_dataset_hold_obj(dd->dd_pool, 2245 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2246 if (err) 2247 return (err); 2248 2249 /* new name better not be in use */ 2250 err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 2251 dsl_dataset_rele(hds, FTAG); 2252 2253 if (err == 0) 2254 err = EEXIST; 2255 else if (err == ENOENT) 2256 err = 0; 2257 2258 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 2259 if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 2260 err = ENAMETOOLONG; 2261 2262 return (err); 2263 } 2264 2265 static void 2266 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx) 2267 { 2268 dsl_dataset_t *ds = arg1; 2269 const char *newsnapname = arg2; 2270 dsl_dir_t *dd = ds->ds_dir; 2271 objset_t *mos = dd->dd_pool->dp_meta_objset; 2272 dsl_dataset_t *hds; 2273 int err; 2274 2275 ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2276 2277 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 2278 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2279 2280 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2281 err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2282 ASSERT3U(err, ==, 0); 2283 mutex_enter(&ds->ds_lock); 2284 (void) strcpy(ds->ds_snapname, newsnapname); 2285 mutex_exit(&ds->ds_lock); 2286 err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 2287 ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2288 ASSERT3U(err, ==, 0); 2289 2290 spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 2291 "dataset = %llu", ds->ds_object); 2292 dsl_dataset_rele(hds, FTAG); 2293 } 2294 2295 struct renamesnaparg { 2296 dsl_sync_task_group_t *dstg; 2297 char failed[MAXPATHLEN]; 2298 char *oldsnap; 2299 char *newsnap; 2300 }; 2301 2302 static int 2303 dsl_snapshot_rename_one(const char *name, void *arg) 2304 { 2305 struct renamesnaparg *ra = arg; 2306 dsl_dataset_t *ds = NULL; 2307 char *snapname; 2308 int err; 2309 2310 snapname = kmem_asprintf("%s@%s", name, ra->oldsnap); 2311 (void) strlcpy(ra->failed, snapname, sizeof (ra->failed)); 2312 2313 /* 2314 * For recursive snapshot renames the parent won't be changing 2315 * so we just pass name for both the to/from argument. 2316 */ 2317 err = zfs_secpolicy_rename_perms(snapname, snapname, CRED()); 2318 if (err != 0) { 2319 strfree(snapname); 2320 return (err == ENOENT ? 0 : err); 2321 } 2322 2323 #ifdef _KERNEL 2324 /* 2325 * For all filesystems undergoing rename, we'll need to unmount it. 2326 */ 2327 (void) zfs_unmount_snap(snapname, NULL); 2328 #endif 2329 err = dsl_dataset_hold(snapname, ra->dstg, &ds); 2330 strfree(snapname); 2331 if (err != 0) 2332 return (err == ENOENT ? 0 : err); 2333 2334 dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 2335 dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 2336 2337 return (0); 2338 } 2339 2340 static int 2341 dsl_recursive_rename(char *oldname, const char *newname) 2342 { 2343 int err; 2344 struct renamesnaparg *ra; 2345 dsl_sync_task_t *dst; 2346 spa_t *spa; 2347 char *cp, *fsname = spa_strdup(oldname); 2348 int len = strlen(oldname) + 1; 2349 2350 /* truncate the snapshot name to get the fsname */ 2351 cp = strchr(fsname, '@'); 2352 *cp = '\0'; 2353 2354 err = spa_open(fsname, &spa, FTAG); 2355 if (err) { 2356 kmem_free(fsname, len); 2357 return (err); 2358 } 2359 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 2360 ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 2361 2362 ra->oldsnap = strchr(oldname, '@') + 1; 2363 ra->newsnap = strchr(newname, '@') + 1; 2364 *ra->failed = '\0'; 2365 2366 err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 2367 DS_FIND_CHILDREN); 2368 kmem_free(fsname, len); 2369 2370 if (err == 0) { 2371 err = dsl_sync_task_group_wait(ra->dstg); 2372 } 2373 2374 for (dst = list_head(&ra->dstg->dstg_tasks); dst; 2375 dst = list_next(&ra->dstg->dstg_tasks, dst)) { 2376 dsl_dataset_t *ds = dst->dst_arg1; 2377 if (dst->dst_err) { 2378 dsl_dir_name(ds->ds_dir, ra->failed); 2379 (void) strlcat(ra->failed, "@", sizeof (ra->failed)); 2380 (void) strlcat(ra->failed, ra->newsnap, 2381 sizeof (ra->failed)); 2382 } 2383 dsl_dataset_rele(ds, ra->dstg); 2384 } 2385 2386 if (err) 2387 (void) strlcpy(oldname, ra->failed, sizeof (ra->failed)); 2388 2389 dsl_sync_task_group_destroy(ra->dstg); 2390 kmem_free(ra, sizeof (struct renamesnaparg)); 2391 spa_close(spa, FTAG); 2392 return (err); 2393 } 2394 2395 static int 2396 dsl_valid_rename(const char *oldname, void *arg) 2397 { 2398 int delta = *(int *)arg; 2399 2400 if (strlen(oldname) + delta >= MAXNAMELEN) 2401 return (ENAMETOOLONG); 2402 2403 return (0); 2404 } 2405 2406 #pragma weak dmu_objset_rename = dsl_dataset_rename 2407 int 2408 dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2409 { 2410 dsl_dir_t *dd; 2411 dsl_dataset_t *ds; 2412 const char *tail; 2413 int err; 2414 2415 err = dsl_dir_open(oldname, FTAG, &dd, &tail); 2416 if (err) 2417 return (err); 2418 2419 if (tail == NULL) { 2420 int delta = strlen(newname) - strlen(oldname); 2421 2422 /* if we're growing, validate child name lengths */ 2423 if (delta > 0) 2424 err = dmu_objset_find(oldname, dsl_valid_rename, 2425 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 2426 2427 if (err == 0) 2428 err = dsl_dir_rename(dd, newname); 2429 dsl_dir_close(dd, FTAG); 2430 return (err); 2431 } 2432 2433 if (tail[0] != '@') { 2434 /* the name ended in a nonexistent component */ 2435 dsl_dir_close(dd, FTAG); 2436 return (ENOENT); 2437 } 2438 2439 dsl_dir_close(dd, FTAG); 2440 2441 /* new name must be snapshot in same filesystem */ 2442 tail = strchr(newname, '@'); 2443 if (tail == NULL) 2444 return (EINVAL); 2445 tail++; 2446 if (strncmp(oldname, newname, tail - newname) != 0) 2447 return (EXDEV); 2448 2449 if (recursive) { 2450 err = dsl_recursive_rename(oldname, newname); 2451 } else { 2452 err = dsl_dataset_hold(oldname, FTAG, &ds); 2453 if (err) 2454 return (err); 2455 2456 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2457 dsl_dataset_snapshot_rename_check, 2458 dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 2459 2460 dsl_dataset_rele(ds, FTAG); 2461 } 2462 2463 return (err); 2464 } 2465 2466 struct promotenode { 2467 list_node_t link; 2468 dsl_dataset_t *ds; 2469 }; 2470 2471 struct promotearg { 2472 list_t shared_snaps, origin_snaps, clone_snaps; 2473 dsl_dataset_t *origin_origin; 2474 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2475 char *err_ds; 2476 }; 2477 2478 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 2479 static boolean_t snaplist_unstable(list_t *l); 2480 2481 static int 2482 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 2483 { 2484 dsl_dataset_t *hds = arg1; 2485 struct promotearg *pa = arg2; 2486 struct promotenode *snap = list_head(&pa->shared_snaps); 2487 dsl_dataset_t *origin_ds = snap->ds; 2488 int err; 2489 uint64_t unused; 2490 2491 /* Check that it is a real clone */ 2492 if (!dsl_dir_is_clone(hds->ds_dir)) 2493 return (EINVAL); 2494 2495 /* Since this is so expensive, don't do the preliminary check */ 2496 if (!dmu_tx_is_syncing(tx)) 2497 return (0); 2498 2499 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 2500 return (EXDEV); 2501 2502 /* compute origin's new unique space */ 2503 snap = list_tail(&pa->clone_snaps); 2504 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2505 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2506 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 2507 &pa->unique, &unused, &unused); 2508 2509 /* 2510 * Walk the snapshots that we are moving 2511 * 2512 * Compute space to transfer. Consider the incremental changes 2513 * to used for each snapshot: 2514 * (my used) = (prev's used) + (blocks born) - (blocks killed) 2515 * So each snapshot gave birth to: 2516 * (blocks born) = (my used) - (prev's used) + (blocks killed) 2517 * So a sequence would look like: 2518 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2519 * Which simplifies to: 2520 * uN + kN + kN-1 + ... + k1 + k0 2521 * Note however, if we stop before we reach the ORIGIN we get: 2522 * uN + kN + kN-1 + ... + kM - uM-1 2523 */ 2524 pa->used = origin_ds->ds_phys->ds_used_bytes; 2525 pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 2526 pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 2527 for (snap = list_head(&pa->shared_snaps); snap; 2528 snap = list_next(&pa->shared_snaps, snap)) { 2529 uint64_t val, dlused, dlcomp, dluncomp; 2530 dsl_dataset_t *ds = snap->ds; 2531 2532 /* Check that the snapshot name does not conflict */ 2533 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2534 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2535 if (err == 0) { 2536 err = EEXIST; 2537 goto out; 2538 } 2539 if (err != ENOENT) 2540 goto out; 2541 2542 /* The very first snapshot does not have a deadlist */ 2543 if (ds->ds_phys->ds_prev_snap_obj == 0) 2544 continue; 2545 2546 dsl_deadlist_space(&ds->ds_deadlist, 2547 &dlused, &dlcomp, &dluncomp); 2548 pa->used += dlused; 2549 pa->comp += dlcomp; 2550 pa->uncomp += dluncomp; 2551 } 2552 2553 /* 2554 * If we are a clone of a clone then we never reached ORIGIN, 2555 * so we need to subtract out the clone origin's used space. 2556 */ 2557 if (pa->origin_origin) { 2558 pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 2559 pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 2560 pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 2561 } 2562 2563 /* Check that there is enough space here */ 2564 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2565 pa->used); 2566 if (err) 2567 return (err); 2568 2569 /* 2570 * Compute the amounts of space that will be used by snapshots 2571 * after the promotion (for both origin and clone). For each, 2572 * it is the amount of space that will be on all of their 2573 * deadlists (that was not born before their new origin). 2574 */ 2575 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2576 uint64_t space; 2577 2578 /* 2579 * Note, typically this will not be a clone of a clone, 2580 * so dd_origin_txg will be < TXG_INITIAL, so 2581 * these snaplist_space() -> dsl_deadlist_space_range() 2582 * calls will be fast because they do not have to 2583 * iterate over all bps. 2584 */ 2585 snap = list_head(&pa->origin_snaps); 2586 err = snaplist_space(&pa->shared_snaps, 2587 snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap); 2588 if (err) 2589 return (err); 2590 2591 err = snaplist_space(&pa->clone_snaps, 2592 snap->ds->ds_dir->dd_origin_txg, &space); 2593 if (err) 2594 return (err); 2595 pa->cloneusedsnap += space; 2596 } 2597 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2598 err = snaplist_space(&pa->origin_snaps, 2599 origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 2600 if (err) 2601 return (err); 2602 } 2603 2604 return (0); 2605 out: 2606 pa->err_ds = snap->ds->ds_snapname; 2607 return (err); 2608 } 2609 2610 static void 2611 dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx) 2612 { 2613 dsl_dataset_t *hds = arg1; 2614 struct promotearg *pa = arg2; 2615 struct promotenode *snap = list_head(&pa->shared_snaps); 2616 dsl_dataset_t *origin_ds = snap->ds; 2617 dsl_dataset_t *origin_head; 2618 dsl_dir_t *dd = hds->ds_dir; 2619 dsl_pool_t *dp = hds->ds_dir->dd_pool; 2620 dsl_dir_t *odd = NULL; 2621 uint64_t oldnext_obj; 2622 int64_t delta; 2623 2624 ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 2625 2626 snap = list_head(&pa->origin_snaps); 2627 origin_head = snap->ds; 2628 2629 /* 2630 * We need to explicitly open odd, since origin_ds's dd will be 2631 * changing. 2632 */ 2633 VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 2634 NULL, FTAG, &odd)); 2635 2636 /* change origin's next snap */ 2637 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2638 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 2639 snap = list_tail(&pa->clone_snaps); 2640 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2641 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 2642 2643 /* change the origin's next clone */ 2644 if (origin_ds->ds_phys->ds_next_clones_obj) { 2645 remove_from_next_clones(origin_ds, snap->ds->ds_object, tx); 2646 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 2647 origin_ds->ds_phys->ds_next_clones_obj, 2648 oldnext_obj, tx)); 2649 } 2650 2651 /* change origin */ 2652 dmu_buf_will_dirty(dd->dd_dbuf, tx); 2653 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2654 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 2655 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2656 dmu_buf_will_dirty(odd->dd_dbuf, tx); 2657 odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 2658 origin_head->ds_dir->dd_origin_txg = 2659 origin_ds->ds_phys->ds_creation_txg; 2660 2661 /* change dd_clone entries */ 2662 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2663 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 2664 odd->dd_phys->dd_clones, hds->ds_object, tx)); 2665 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 2666 pa->origin_origin->ds_dir->dd_phys->dd_clones, 2667 hds->ds_object, tx)); 2668 2669 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 2670 pa->origin_origin->ds_dir->dd_phys->dd_clones, 2671 origin_head->ds_object, tx)); 2672 if (dd->dd_phys->dd_clones == 0) { 2673 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset, 2674 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 2675 } 2676 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 2677 dd->dd_phys->dd_clones, origin_head->ds_object, tx)); 2678 2679 } 2680 2681 /* move snapshots to this dir */ 2682 for (snap = list_head(&pa->shared_snaps); snap; 2683 snap = list_next(&pa->shared_snaps, snap)) { 2684 dsl_dataset_t *ds = snap->ds; 2685 2686 /* unregister props as dsl_dir is changing */ 2687 if (ds->ds_objset) { 2688 dmu_objset_evict(ds->ds_objset); 2689 ds->ds_objset = NULL; 2690 } 2691 /* move snap name entry */ 2692 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2693 VERIFY(0 == dsl_dataset_snap_remove(origin_head, 2694 ds->ds_snapname, tx)); 2695 VERIFY(0 == zap_add(dp->dp_meta_objset, 2696 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 2697 8, 1, &ds->ds_object, tx)); 2698 2699 /* change containing dsl_dir */ 2700 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2701 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 2702 ds->ds_phys->ds_dir_obj = dd->dd_object; 2703 ASSERT3P(ds->ds_dir, ==, odd); 2704 dsl_dir_close(ds->ds_dir, ds); 2705 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 2706 NULL, ds, &ds->ds_dir)); 2707 2708 /* move any clone references */ 2709 if (ds->ds_phys->ds_next_clones_obj && 2710 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2711 zap_cursor_t zc; 2712 zap_attribute_t za; 2713 2714 for (zap_cursor_init(&zc, dp->dp_meta_objset, 2715 ds->ds_phys->ds_next_clones_obj); 2716 zap_cursor_retrieve(&zc, &za) == 0; 2717 zap_cursor_advance(&zc)) { 2718 dsl_dataset_t *cnds; 2719 uint64_t o; 2720 2721 if (za.za_first_integer == oldnext_obj) { 2722 /* 2723 * We've already moved the 2724 * origin's reference. 2725 */ 2726 continue; 2727 } 2728 2729 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, 2730 za.za_first_integer, FTAG, &cnds)); 2731 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj; 2732 2733 VERIFY3U(zap_remove_int(dp->dp_meta_objset, 2734 odd->dd_phys->dd_clones, o, tx), ==, 0); 2735 VERIFY3U(zap_add_int(dp->dp_meta_objset, 2736 dd->dd_phys->dd_clones, o, tx), ==, 0); 2737 dsl_dataset_rele(cnds, FTAG); 2738 } 2739 zap_cursor_fini(&zc); 2740 } 2741 2742 ASSERT3U(dsl_prop_numcb(ds), ==, 0); 2743 } 2744 2745 /* 2746 * Change space accounting. 2747 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 2748 * both be valid, or both be 0 (resulting in delta == 0). This 2749 * is true for each of {clone,origin} independently. 2750 */ 2751 2752 delta = pa->cloneusedsnap - 2753 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2754 ASSERT3S(delta, >=, 0); 2755 ASSERT3U(pa->used, >=, delta); 2756 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 2757 dsl_dir_diduse_space(dd, DD_USED_HEAD, 2758 pa->used - delta, pa->comp, pa->uncomp, tx); 2759 2760 delta = pa->originusedsnap - 2761 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2762 ASSERT3S(delta, <=, 0); 2763 ASSERT3U(pa->used, >=, -delta); 2764 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 2765 dsl_dir_diduse_space(odd, DD_USED_HEAD, 2766 -pa->used - delta, -pa->comp, -pa->uncomp, tx); 2767 2768 origin_ds->ds_phys->ds_unique_bytes = pa->unique; 2769 2770 /* log history record */ 2771 spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 2772 "dataset = %llu", hds->ds_object); 2773 2774 dsl_dir_close(odd, FTAG); 2775 } 2776 2777 static char *snaplist_tag = "snaplist"; 2778 /* 2779 * Make a list of dsl_dataset_t's for the snapshots between first_obj 2780 * (exclusive) and last_obj (inclusive). The list will be in reverse 2781 * order (last_obj will be the list_head()). If first_obj == 0, do all 2782 * snapshots back to this dataset's origin. 2783 */ 2784 static int 2785 snaplist_make(dsl_pool_t *dp, boolean_t own, 2786 uint64_t first_obj, uint64_t last_obj, list_t *l) 2787 { 2788 uint64_t obj = last_obj; 2789 2790 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 2791 2792 list_create(l, sizeof (struct promotenode), 2793 offsetof(struct promotenode, link)); 2794 2795 while (obj != first_obj) { 2796 dsl_dataset_t *ds; 2797 struct promotenode *snap; 2798 int err; 2799 2800 if (own) { 2801 err = dsl_dataset_own_obj(dp, obj, 2802 0, snaplist_tag, &ds); 2803 if (err == 0) 2804 dsl_dataset_make_exclusive(ds, snaplist_tag); 2805 } else { 2806 err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 2807 } 2808 if (err == ENOENT) { 2809 /* lost race with snapshot destroy */ 2810 struct promotenode *last = list_tail(l); 2811 ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 2812 obj = last->ds->ds_phys->ds_prev_snap_obj; 2813 continue; 2814 } else if (err) { 2815 return (err); 2816 } 2817 2818 if (first_obj == 0) 2819 first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 2820 2821 snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 2822 snap->ds = ds; 2823 list_insert_tail(l, snap); 2824 obj = ds->ds_phys->ds_prev_snap_obj; 2825 } 2826 2827 return (0); 2828 } 2829 2830 static int 2831 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2832 { 2833 struct promotenode *snap; 2834 2835 *spacep = 0; 2836 for (snap = list_head(l); snap; snap = list_next(l, snap)) { 2837 uint64_t used, comp, uncomp; 2838 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2839 mintxg, UINT64_MAX, &used, &comp, &uncomp); 2840 *spacep += used; 2841 } 2842 return (0); 2843 } 2844 2845 static void 2846 snaplist_destroy(list_t *l, boolean_t own) 2847 { 2848 struct promotenode *snap; 2849 2850 if (!l || !list_link_active(&l->list_head)) 2851 return; 2852 2853 while ((snap = list_tail(l)) != NULL) { 2854 list_remove(l, snap); 2855 if (own) 2856 dsl_dataset_disown(snap->ds, snaplist_tag); 2857 else 2858 dsl_dataset_rele(snap->ds, snaplist_tag); 2859 kmem_free(snap, sizeof (struct promotenode)); 2860 } 2861 list_destroy(l); 2862 } 2863 2864 /* 2865 * Promote a clone. Nomenclature note: 2866 * "clone" or "cds": the original clone which is being promoted 2867 * "origin" or "ods": the snapshot which is originally clone's origin 2868 * "origin head" or "ohds": the dataset which is the head 2869 * (filesystem/volume) for the origin 2870 * "origin origin": the origin of the origin's filesystem (typically 2871 * NULL, indicating that the clone is not a clone of a clone). 2872 */ 2873 int 2874 dsl_dataset_promote(const char *name, char *conflsnap) 2875 { 2876 dsl_dataset_t *ds; 2877 dsl_dir_t *dd; 2878 dsl_pool_t *dp; 2879 dmu_object_info_t doi; 2880 struct promotearg pa = { 0 }; 2881 struct promotenode *snap; 2882 int err; 2883 2884 err = dsl_dataset_hold(name, FTAG, &ds); 2885 if (err) 2886 return (err); 2887 dd = ds->ds_dir; 2888 dp = dd->dd_pool; 2889 2890 err = dmu_object_info(dp->dp_meta_objset, 2891 ds->ds_phys->ds_snapnames_zapobj, &doi); 2892 if (err) { 2893 dsl_dataset_rele(ds, FTAG); 2894 return (err); 2895 } 2896 2897 if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 2898 dsl_dataset_rele(ds, FTAG); 2899 return (EINVAL); 2900 } 2901 2902 /* 2903 * We are going to inherit all the snapshots taken before our 2904 * origin (i.e., our new origin will be our parent's origin). 2905 * Take ownership of them so that we can rename them into our 2906 * namespace. 2907 */ 2908 rw_enter(&dp->dp_config_rwlock, RW_READER); 2909 2910 err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 2911 &pa.shared_snaps); 2912 if (err != 0) 2913 goto out; 2914 2915 err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 2916 if (err != 0) 2917 goto out; 2918 2919 snap = list_head(&pa.shared_snaps); 2920 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 2921 err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 2922 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 2923 if (err != 0) 2924 goto out; 2925 2926 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) { 2927 err = dsl_dataset_hold_obj(dp, 2928 snap->ds->ds_dir->dd_phys->dd_origin_obj, 2929 FTAG, &pa.origin_origin); 2930 if (err != 0) 2931 goto out; 2932 } 2933 2934 out: 2935 rw_exit(&dp->dp_config_rwlock); 2936 2937 /* 2938 * Add in 128x the snapnames zapobj size, since we will be moving 2939 * a bunch of snapnames to the promoted ds, and dirtying their 2940 * bonus buffers. 2941 */ 2942 if (err == 0) { 2943 err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 2944 dsl_dataset_promote_sync, ds, &pa, 2945 2 + 2 * doi.doi_physical_blocks_512); 2946 if (err && pa.err_ds && conflsnap) 2947 (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN); 2948 } 2949 2950 snaplist_destroy(&pa.shared_snaps, B_TRUE); 2951 snaplist_destroy(&pa.clone_snaps, B_FALSE); 2952 snaplist_destroy(&pa.origin_snaps, B_FALSE); 2953 if (pa.origin_origin) 2954 dsl_dataset_rele(pa.origin_origin, FTAG); 2955 dsl_dataset_rele(ds, FTAG); 2956 return (err); 2957 } 2958 2959 struct cloneswaparg { 2960 dsl_dataset_t *cds; /* clone dataset */ 2961 dsl_dataset_t *ohds; /* origin's head dataset */ 2962 boolean_t force; 2963 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 2964 }; 2965 2966 /* ARGSUSED */ 2967 static int 2968 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 2969 { 2970 struct cloneswaparg *csa = arg1; 2971 2972 /* they should both be heads */ 2973 if (dsl_dataset_is_snapshot(csa->cds) || 2974 dsl_dataset_is_snapshot(csa->ohds)) 2975 return (EINVAL); 2976 2977 /* the branch point should be just before them */ 2978 if (csa->cds->ds_prev != csa->ohds->ds_prev) 2979 return (EINVAL); 2980 2981 /* cds should be the clone (unless they are unrelated) */ 2982 if (csa->cds->ds_prev != NULL && 2983 csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 2984 csa->ohds->ds_object != 2985 csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 2986 return (EINVAL); 2987 2988 /* the clone should be a child of the origin */ 2989 if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 2990 return (EINVAL); 2991 2992 /* ohds shouldn't be modified unless 'force' */ 2993 if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 2994 return (ETXTBSY); 2995 2996 /* adjust amount of any unconsumed refreservation */ 2997 csa->unused_refres_delta = 2998 (int64_t)MIN(csa->ohds->ds_reserved, 2999 csa->ohds->ds_phys->ds_unique_bytes) - 3000 (int64_t)MIN(csa->ohds->ds_reserved, 3001 csa->cds->ds_phys->ds_unique_bytes); 3002 3003 if (csa->unused_refres_delta > 0 && 3004 csa->unused_refres_delta > 3005 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 3006 return (ENOSPC); 3007 3008 if (csa->ohds->ds_quota != 0 && 3009 csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota) 3010 return (EDQUOT); 3011 3012 return (0); 3013 } 3014 3015 /* ARGSUSED */ 3016 static void 3017 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx) 3018 { 3019 struct cloneswaparg *csa = arg1; 3020 dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 3021 3022 ASSERT(csa->cds->ds_reserved == 0); 3023 ASSERT(csa->ohds->ds_quota == 0 || 3024 csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota); 3025 3026 dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 3027 dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 3028 3029 if (csa->cds->ds_objset != NULL) { 3030 dmu_objset_evict(csa->cds->ds_objset); 3031 csa->cds->ds_objset = NULL; 3032 } 3033 3034 if (csa->ohds->ds_objset != NULL) { 3035 dmu_objset_evict(csa->ohds->ds_objset); 3036 csa->ohds->ds_objset = NULL; 3037 } 3038 3039 /* 3040 * Reset origin's unique bytes, if it exists. 3041 */ 3042 if (csa->cds->ds_prev) { 3043 dsl_dataset_t *origin = csa->cds->ds_prev; 3044 uint64_t comp, uncomp; 3045 3046 dmu_buf_will_dirty(origin->ds_dbuf, tx); 3047 dsl_deadlist_space_range(&csa->cds->ds_deadlist, 3048 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 3049 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp); 3050 } 3051 3052 /* swap blkptrs */ 3053 { 3054 blkptr_t tmp; 3055 tmp = csa->ohds->ds_phys->ds_bp; 3056 csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 3057 csa->cds->ds_phys->ds_bp = tmp; 3058 } 3059 3060 /* set dd_*_bytes */ 3061 { 3062 int64_t dused, dcomp, duncomp; 3063 uint64_t cdl_used, cdl_comp, cdl_uncomp; 3064 uint64_t odl_used, odl_comp, odl_uncomp; 3065 3066 ASSERT3U(csa->cds->ds_dir->dd_phys-> 3067 dd_used_breakdown[DD_USED_SNAP], ==, 0); 3068 3069 dsl_deadlist_space(&csa->cds->ds_deadlist, 3070 &cdl_used, &cdl_comp, &cdl_uncomp); 3071 dsl_deadlist_space(&csa->ohds->ds_deadlist, 3072 &odl_used, &odl_comp, &odl_uncomp); 3073 3074 dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 3075 (csa->ohds->ds_phys->ds_used_bytes + odl_used); 3076 dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 3077 (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 3078 duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 3079 cdl_uncomp - 3080 (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 3081 3082 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 3083 dused, dcomp, duncomp, tx); 3084 dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 3085 -dused, -dcomp, -duncomp, tx); 3086 3087 /* 3088 * The difference in the space used by snapshots is the 3089 * difference in snapshot space due to the head's 3090 * deadlist (since that's the only thing that's 3091 * changing that affects the snapused). 3092 */ 3093 dsl_deadlist_space_range(&csa->cds->ds_deadlist, 3094 csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, 3095 &cdl_used, &cdl_comp, &cdl_uncomp); 3096 dsl_deadlist_space_range(&csa->ohds->ds_deadlist, 3097 csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, 3098 &odl_used, &odl_comp, &odl_uncomp); 3099 dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 3100 DD_USED_HEAD, DD_USED_SNAP, tx); 3101 } 3102 3103 /* swap ds_*_bytes */ 3104 SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 3105 csa->cds->ds_phys->ds_used_bytes); 3106 SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 3107 csa->cds->ds_phys->ds_compressed_bytes); 3108 SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 3109 csa->cds->ds_phys->ds_uncompressed_bytes); 3110 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 3111 csa->cds->ds_phys->ds_unique_bytes); 3112 3113 /* apply any parent delta for change in unconsumed refreservation */ 3114 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 3115 csa->unused_refres_delta, 0, 0, tx); 3116 3117 /* 3118 * Swap deadlists. 3119 */ 3120 dsl_deadlist_close(&csa->cds->ds_deadlist); 3121 dsl_deadlist_close(&csa->ohds->ds_deadlist); 3122 SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 3123 csa->cds->ds_phys->ds_deadlist_obj); 3124 dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 3125 csa->cds->ds_phys->ds_deadlist_obj); 3126 dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 3127 csa->ohds->ds_phys->ds_deadlist_obj); 3128 3129 dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx); 3130 } 3131 3132 /* 3133 * Swap 'clone' with its origin head datasets. Used at the end of "zfs 3134 * recv" into an existing fs to swizzle the file system to the new 3135 * version, and by "zfs rollback". Can also be used to swap two 3136 * independent head datasets if neither has any snapshots. 3137 */ 3138 int 3139 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 3140 boolean_t force) 3141 { 3142 struct cloneswaparg csa; 3143 int error; 3144 3145 ASSERT(clone->ds_owner); 3146 ASSERT(origin_head->ds_owner); 3147 retry: 3148 /* Need exclusive access for the swap */ 3149 rw_enter(&clone->ds_rwlock, RW_WRITER); 3150 if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 3151 rw_exit(&clone->ds_rwlock); 3152 rw_enter(&origin_head->ds_rwlock, RW_WRITER); 3153 if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 3154 rw_exit(&origin_head->ds_rwlock); 3155 goto retry; 3156 } 3157 } 3158 csa.cds = clone; 3159 csa.ohds = origin_head; 3160 csa.force = force; 3161 error = dsl_sync_task_do(clone->ds_dir->dd_pool, 3162 dsl_dataset_clone_swap_check, 3163 dsl_dataset_clone_swap_sync, &csa, NULL, 9); 3164 return (error); 3165 } 3166 3167 /* 3168 * Given a pool name and a dataset object number in that pool, 3169 * return the name of that dataset. 3170 */ 3171 int 3172 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 3173 { 3174 spa_t *spa; 3175 dsl_pool_t *dp; 3176 dsl_dataset_t *ds; 3177 int error; 3178 3179 if ((error = spa_open(pname, &spa, FTAG)) != 0) 3180 return (error); 3181 dp = spa_get_dsl(spa); 3182 rw_enter(&dp->dp_config_rwlock, RW_READER); 3183 if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 3184 dsl_dataset_name(ds, buf); 3185 dsl_dataset_rele(ds, FTAG); 3186 } 3187 rw_exit(&dp->dp_config_rwlock); 3188 spa_close(spa, FTAG); 3189 3190 return (error); 3191 } 3192 3193 int 3194 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 3195 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 3196 { 3197 int error = 0; 3198 3199 ASSERT3S(asize, >, 0); 3200 3201 /* 3202 * *ref_rsrv is the portion of asize that will come from any 3203 * unconsumed refreservation space. 3204 */ 3205 *ref_rsrv = 0; 3206 3207 mutex_enter(&ds->ds_lock); 3208 /* 3209 * Make a space adjustment for reserved bytes. 3210 */ 3211 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 3212 ASSERT3U(*used, >=, 3213 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 3214 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 3215 *ref_rsrv = 3216 asize - MIN(asize, parent_delta(ds, asize + inflight)); 3217 } 3218 3219 if (!check_quota || ds->ds_quota == 0) { 3220 mutex_exit(&ds->ds_lock); 3221 return (0); 3222 } 3223 /* 3224 * If they are requesting more space, and our current estimate 3225 * is over quota, they get to try again unless the actual 3226 * on-disk is over quota and there are no pending changes (which 3227 * may free up space for us). 3228 */ 3229 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 3230 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 3231 error = ERESTART; 3232 else 3233 error = EDQUOT; 3234 } 3235 mutex_exit(&ds->ds_lock); 3236 3237 return (error); 3238 } 3239 3240 /* ARGSUSED */ 3241 static int 3242 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 3243 { 3244 dsl_dataset_t *ds = arg1; 3245 dsl_prop_setarg_t *psa = arg2; 3246 int err; 3247 3248 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 3249 return (ENOTSUP); 3250 3251 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 3252 return (err); 3253 3254 if (psa->psa_effective_value == 0) 3255 return (0); 3256 3257 if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes || 3258 psa->psa_effective_value < ds->ds_reserved) 3259 return (ENOSPC); 3260 3261 return (0); 3262 } 3263 3264 extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *); 3265 3266 void 3267 dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx) 3268 { 3269 dsl_dataset_t *ds = arg1; 3270 dsl_prop_setarg_t *psa = arg2; 3271 uint64_t effective_value = psa->psa_effective_value; 3272 3273 dsl_prop_set_sync(ds, psa, tx); 3274 DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 3275 3276 if (ds->ds_quota != effective_value) { 3277 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3278 ds->ds_quota = effective_value; 3279 3280 spa_history_log_internal(LOG_DS_REFQUOTA, 3281 ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ", 3282 (longlong_t)ds->ds_quota, ds->ds_object); 3283 } 3284 } 3285 3286 int 3287 dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota) 3288 { 3289 dsl_dataset_t *ds; 3290 dsl_prop_setarg_t psa; 3291 int err; 3292 3293 dsl_prop_setarg_init_uint64(&psa, "refquota", source, "a); 3294 3295 err = dsl_dataset_hold(dsname, FTAG, &ds); 3296 if (err) 3297 return (err); 3298 3299 /* 3300 * If someone removes a file, then tries to set the quota, we 3301 * want to make sure the file freeing takes effect. 3302 */ 3303 txg_wait_open(ds->ds_dir->dd_pool, 0); 3304 3305 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 3306 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 3307 ds, &psa, 0); 3308 3309 dsl_dataset_rele(ds, FTAG); 3310 return (err); 3311 } 3312 3313 static int 3314 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 3315 { 3316 dsl_dataset_t *ds = arg1; 3317 dsl_prop_setarg_t *psa = arg2; 3318 uint64_t effective_value; 3319 uint64_t unique; 3320 int err; 3321 3322 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 3323 SPA_VERSION_REFRESERVATION) 3324 return (ENOTSUP); 3325 3326 if (dsl_dataset_is_snapshot(ds)) 3327 return (EINVAL); 3328 3329 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 3330 return (err); 3331 3332 effective_value = psa->psa_effective_value; 3333 3334 /* 3335 * If we are doing the preliminary check in open context, the 3336 * space estimates may be inaccurate. 3337 */ 3338 if (!dmu_tx_is_syncing(tx)) 3339 return (0); 3340 3341 mutex_enter(&ds->ds_lock); 3342 if (!DS_UNIQUE_IS_ACCURATE(ds)) 3343 dsl_dataset_recalc_head_uniq(ds); 3344 unique = ds->ds_phys->ds_unique_bytes; 3345 mutex_exit(&ds->ds_lock); 3346 3347 if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) { 3348 uint64_t delta = MAX(unique, effective_value) - 3349 MAX(unique, ds->ds_reserved); 3350 3351 if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 3352 return (ENOSPC); 3353 if (ds->ds_quota > 0 && 3354 effective_value > ds->ds_quota) 3355 return (ENOSPC); 3356 } 3357 3358 return (0); 3359 } 3360 3361 static void 3362 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx) 3363 { 3364 dsl_dataset_t *ds = arg1; 3365 dsl_prop_setarg_t *psa = arg2; 3366 uint64_t effective_value = psa->psa_effective_value; 3367 uint64_t unique; 3368 int64_t delta; 3369 3370 dsl_prop_set_sync(ds, psa, tx); 3371 DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 3372 3373 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3374 3375 mutex_enter(&ds->ds_dir->dd_lock); 3376 mutex_enter(&ds->ds_lock); 3377 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3378 unique = ds->ds_phys->ds_unique_bytes; 3379 delta = MAX(0, (int64_t)(effective_value - unique)) - 3380 MAX(0, (int64_t)(ds->ds_reserved - unique)); 3381 ds->ds_reserved = effective_value; 3382 mutex_exit(&ds->ds_lock); 3383 3384 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 3385 mutex_exit(&ds->ds_dir->dd_lock); 3386 3387 spa_history_log_internal(LOG_DS_REFRESERV, 3388 ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu", 3389 (longlong_t)effective_value, ds->ds_object); 3390 } 3391 3392 int 3393 dsl_dataset_set_reservation(const char *dsname, zprop_source_t source, 3394 uint64_t reservation) 3395 { 3396 dsl_dataset_t *ds; 3397 dsl_prop_setarg_t psa; 3398 int err; 3399 3400 dsl_prop_setarg_init_uint64(&psa, "refreservation", source, 3401 &reservation); 3402 3403 err = dsl_dataset_hold(dsname, FTAG, &ds); 3404 if (err) 3405 return (err); 3406 3407 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 3408 dsl_dataset_set_reservation_check, 3409 dsl_dataset_set_reservation_sync, ds, &psa, 0); 3410 3411 dsl_dataset_rele(ds, FTAG); 3412 return (err); 3413 } 3414 3415 struct dsl_ds_holdarg { 3416 dsl_sync_task_group_t *dstg; 3417 char *htag; 3418 char *snapname; 3419 boolean_t recursive; 3420 boolean_t gotone; 3421 boolean_t temphold; 3422 char failed[MAXPATHLEN]; 3423 }; 3424 3425 typedef struct zfs_hold_cleanup_arg { 3426 char dsname[MAXNAMELEN]; 3427 char snapname[MAXNAMELEN]; 3428 char htag[MAXNAMELEN]; 3429 boolean_t recursive; 3430 } zfs_hold_cleanup_arg_t; 3431 3432 static void 3433 dsl_dataset_user_release_onexit(void *arg) 3434 { 3435 zfs_hold_cleanup_arg_t *ca = arg; 3436 3437 (void) dsl_dataset_user_release(ca->dsname, ca->snapname, 3438 ca->htag, ca->recursive); 3439 kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t)); 3440 } 3441 3442 /* 3443 * The max length of a temporary tag prefix is the number of hex digits 3444 * required to express UINT64_MAX plus one for the hyphen. 3445 */ 3446 #define MAX_TAG_PREFIX_LEN 17 3447 3448 static int 3449 dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 3450 { 3451 dsl_dataset_t *ds = arg1; 3452 struct dsl_ds_holdarg *ha = arg2; 3453 char *htag = ha->htag; 3454 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3455 int error = 0; 3456 3457 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 3458 return (ENOTSUP); 3459 3460 if (!dsl_dataset_is_snapshot(ds)) 3461 return (EINVAL); 3462 3463 /* tags must be unique */ 3464 mutex_enter(&ds->ds_lock); 3465 if (ds->ds_phys->ds_userrefs_obj) { 3466 error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 3467 8, 1, tx); 3468 if (error == 0) 3469 error = EEXIST; 3470 else if (error == ENOENT) 3471 error = 0; 3472 } 3473 mutex_exit(&ds->ds_lock); 3474 3475 if (error == 0 && ha->temphold && 3476 strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN) 3477 error = E2BIG; 3478 3479 return (error); 3480 } 3481 3482 static void 3483 dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx) 3484 { 3485 dsl_dataset_t *ds = arg1; 3486 struct dsl_ds_holdarg *ha = arg2; 3487 char *htag = ha->htag; 3488 dsl_pool_t *dp = ds->ds_dir->dd_pool; 3489 objset_t *mos = dp->dp_meta_objset; 3490 uint64_t now = gethrestime_sec(); 3491 uint64_t zapobj; 3492 3493 mutex_enter(&ds->ds_lock); 3494 if (ds->ds_phys->ds_userrefs_obj == 0) { 3495 /* 3496 * This is the first user hold for this dataset. Create 3497 * the userrefs zap object. 3498 */ 3499 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3500 zapobj = ds->ds_phys->ds_userrefs_obj = 3501 zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 3502 } else { 3503 zapobj = ds->ds_phys->ds_userrefs_obj; 3504 } 3505 ds->ds_userrefs++; 3506 mutex_exit(&ds->ds_lock); 3507 3508 VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 3509 3510 if (ha->temphold) { 3511 VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 3512 htag, &now, tx)); 3513 } 3514 3515 spa_history_log_internal(LOG_DS_USER_HOLD, 3516 dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag, 3517 (int)ha->temphold, ds->ds_object); 3518 } 3519 3520 static int 3521 dsl_dataset_user_hold_one(const char *dsname, void *arg) 3522 { 3523 struct dsl_ds_holdarg *ha = arg; 3524 dsl_dataset_t *ds; 3525 int error; 3526 char *name; 3527 3528 /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 3529 name = kmem_asprintf("%s@%s", dsname, ha->snapname); 3530 error = dsl_dataset_hold(name, ha->dstg, &ds); 3531 strfree(name); 3532 if (error == 0) { 3533 ha->gotone = B_TRUE; 3534 dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 3535 dsl_dataset_user_hold_sync, ds, ha, 0); 3536 } else if (error == ENOENT && ha->recursive) { 3537 error = 0; 3538 } else { 3539 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3540 } 3541 return (error); 3542 } 3543 3544 int 3545 dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 3546 boolean_t recursive, boolean_t temphold, int cleanup_fd) 3547 { 3548 struct dsl_ds_holdarg *ha; 3549 dsl_sync_task_t *dst; 3550 spa_t *spa; 3551 int error; 3552 3553 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 3554 3555 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3556 3557 error = spa_open(dsname, &spa, FTAG); 3558 if (error) { 3559 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3560 return (error); 3561 } 3562 3563 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 3564 ha->htag = htag; 3565 ha->snapname = snapname; 3566 ha->recursive = recursive; 3567 ha->temphold = temphold; 3568 3569 if (recursive) { 3570 error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 3571 ha, DS_FIND_CHILDREN); 3572 } else { 3573 error = dsl_dataset_user_hold_one(dsname, ha); 3574 } 3575 if (error == 0) 3576 error = dsl_sync_task_group_wait(ha->dstg); 3577 3578 for (dst = list_head(&ha->dstg->dstg_tasks); dst; 3579 dst = list_next(&ha->dstg->dstg_tasks, dst)) { 3580 dsl_dataset_t *ds = dst->dst_arg1; 3581 3582 if (dst->dst_err) { 3583 dsl_dataset_name(ds, ha->failed); 3584 *strchr(ha->failed, '@') = '\0'; 3585 } 3586 dsl_dataset_rele(ds, ha->dstg); 3587 } 3588 3589 if (error == 0 && recursive && !ha->gotone) 3590 error = ENOENT; 3591 3592 if (error) 3593 (void) strlcpy(dsname, ha->failed, sizeof (ha->failed)); 3594 3595 dsl_sync_task_group_destroy(ha->dstg); 3596 3597 /* 3598 * If this set of temporary holds is to be removed upon process exit, 3599 * register that action now. 3600 */ 3601 if (error == 0 && cleanup_fd != -1 && temphold) { 3602 zfs_hold_cleanup_arg_t *ca; 3603 uint64_t action_handle; 3604 3605 ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP); 3606 (void) strlcpy(ca->dsname, dsname, sizeof (ca->dsname)); 3607 (void) strlcpy(ca->snapname, snapname, sizeof (ca->snapname)); 3608 (void) strlcpy(ca->htag, htag, sizeof (ca->htag)); 3609 ca->recursive = recursive; 3610 ASSERT3U(0, ==, zfs_onexit_add_cb(cleanup_fd, 3611 dsl_dataset_user_release_onexit, ca, &action_handle)); 3612 } 3613 3614 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3615 spa_close(spa, FTAG); 3616 return (error); 3617 } 3618 3619 struct dsl_ds_releasearg { 3620 dsl_dataset_t *ds; 3621 const char *htag; 3622 boolean_t own; /* do we own or just hold ds? */ 3623 }; 3624 3625 static int 3626 dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 3627 boolean_t *might_destroy) 3628 { 3629 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3630 uint64_t zapobj; 3631 uint64_t tmp; 3632 int error; 3633 3634 *might_destroy = B_FALSE; 3635 3636 mutex_enter(&ds->ds_lock); 3637 zapobj = ds->ds_phys->ds_userrefs_obj; 3638 if (zapobj == 0) { 3639 /* The tag can't possibly exist */ 3640 mutex_exit(&ds->ds_lock); 3641 return (ESRCH); 3642 } 3643 3644 /* Make sure the tag exists */ 3645 error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 3646 if (error) { 3647 mutex_exit(&ds->ds_lock); 3648 if (error == ENOENT) 3649 error = ESRCH; 3650 return (error); 3651 } 3652 3653 if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 3654 DS_IS_DEFER_DESTROY(ds)) 3655 *might_destroy = B_TRUE; 3656 3657 mutex_exit(&ds->ds_lock); 3658 return (0); 3659 } 3660 3661 static int 3662 dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 3663 { 3664 struct dsl_ds_releasearg *ra = arg1; 3665 dsl_dataset_t *ds = ra->ds; 3666 boolean_t might_destroy; 3667 int error; 3668 3669 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 3670 return (ENOTSUP); 3671 3672 error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 3673 if (error) 3674 return (error); 3675 3676 if (might_destroy) { 3677 struct dsl_ds_destroyarg dsda = {0}; 3678 3679 if (dmu_tx_is_syncing(tx)) { 3680 /* 3681 * If we're not prepared to remove the snapshot, 3682 * we can't allow the release to happen right now. 3683 */ 3684 if (!ra->own) 3685 return (EBUSY); 3686 } 3687 dsda.ds = ds; 3688 dsda.releasing = B_TRUE; 3689 return (dsl_dataset_destroy_check(&dsda, tag, tx)); 3690 } 3691 3692 return (0); 3693 } 3694 3695 static void 3696 dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx) 3697 { 3698 struct dsl_ds_releasearg *ra = arg1; 3699 dsl_dataset_t *ds = ra->ds; 3700 dsl_pool_t *dp = ds->ds_dir->dd_pool; 3701 objset_t *mos = dp->dp_meta_objset; 3702 uint64_t zapobj; 3703 uint64_t dsobj = ds->ds_object; 3704 uint64_t refs; 3705 int error; 3706 3707 if (ds->ds_objset) { 3708 dmu_objset_evict(ds->ds_objset); 3709 ds->ds_objset = NULL; 3710 } 3711 3712 mutex_enter(&ds->ds_lock); 3713 ds->ds_userrefs--; 3714 refs = ds->ds_userrefs; 3715 mutex_exit(&ds->ds_lock); 3716 error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 3717 VERIFY(error == 0 || error == ENOENT); 3718 zapobj = ds->ds_phys->ds_userrefs_obj; 3719 VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 3720 if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 3721 DS_IS_DEFER_DESTROY(ds)) { 3722 struct dsl_ds_destroyarg dsda = {0}; 3723 3724 ASSERT(ra->own); 3725 dsda.ds = ds; 3726 dsda.releasing = B_TRUE; 3727 /* We already did the destroy_check */ 3728 dsl_dataset_destroy_sync(&dsda, tag, tx); 3729 } 3730 3731 spa_history_log_internal(LOG_DS_USER_RELEASE, 3732 dp->dp_spa, tx, "<%s> %lld dataset = %llu", 3733 ra->htag, (longlong_t)refs, dsobj); 3734 } 3735 3736 static int 3737 dsl_dataset_user_release_one(const char *dsname, void *arg) 3738 { 3739 struct dsl_ds_holdarg *ha = arg; 3740 struct dsl_ds_releasearg *ra; 3741 dsl_dataset_t *ds; 3742 int error; 3743 void *dtag = ha->dstg; 3744 char *name; 3745 boolean_t own = B_FALSE; 3746 boolean_t might_destroy; 3747 3748 /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 3749 name = kmem_asprintf("%s@%s", dsname, ha->snapname); 3750 error = dsl_dataset_hold(name, dtag, &ds); 3751 strfree(name); 3752 if (error == ENOENT && ha->recursive) 3753 return (0); 3754 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3755 if (error) 3756 return (error); 3757 3758 ha->gotone = B_TRUE; 3759 3760 ASSERT(dsl_dataset_is_snapshot(ds)); 3761 3762 error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 3763 if (error) { 3764 dsl_dataset_rele(ds, dtag); 3765 return (error); 3766 } 3767 3768 if (might_destroy) { 3769 #ifdef _KERNEL 3770 name = kmem_asprintf("%s@%s", dsname, ha->snapname); 3771 error = zfs_unmount_snap(name, NULL); 3772 strfree(name); 3773 if (error) { 3774 dsl_dataset_rele(ds, dtag); 3775 return (error); 3776 } 3777 #endif 3778 if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 3779 dsl_dataset_rele(ds, dtag); 3780 return (EBUSY); 3781 } else { 3782 own = B_TRUE; 3783 dsl_dataset_make_exclusive(ds, dtag); 3784 } 3785 } 3786 3787 ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 3788 ra->ds = ds; 3789 ra->htag = ha->htag; 3790 ra->own = own; 3791 dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 3792 dsl_dataset_user_release_sync, ra, dtag, 0); 3793 3794 return (0); 3795 } 3796 3797 int 3798 dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 3799 boolean_t recursive) 3800 { 3801 struct dsl_ds_holdarg *ha; 3802 dsl_sync_task_t *dst; 3803 spa_t *spa; 3804 int error; 3805 3806 top: 3807 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 3808 3809 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3810 3811 error = spa_open(dsname, &spa, FTAG); 3812 if (error) { 3813 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3814 return (error); 3815 } 3816 3817 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 3818 ha->htag = htag; 3819 ha->snapname = snapname; 3820 ha->recursive = recursive; 3821 if (recursive) { 3822 error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 3823 ha, DS_FIND_CHILDREN); 3824 } else { 3825 error = dsl_dataset_user_release_one(dsname, ha); 3826 } 3827 if (error == 0) 3828 error = dsl_sync_task_group_wait(ha->dstg); 3829 3830 for (dst = list_head(&ha->dstg->dstg_tasks); dst; 3831 dst = list_next(&ha->dstg->dstg_tasks, dst)) { 3832 struct dsl_ds_releasearg *ra = dst->dst_arg1; 3833 dsl_dataset_t *ds = ra->ds; 3834 3835 if (dst->dst_err) 3836 dsl_dataset_name(ds, ha->failed); 3837 3838 if (ra->own) 3839 dsl_dataset_disown(ds, ha->dstg); 3840 else 3841 dsl_dataset_rele(ds, ha->dstg); 3842 3843 kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 3844 } 3845 3846 if (error == 0 && recursive && !ha->gotone) 3847 error = ENOENT; 3848 3849 if (error && error != EBUSY) 3850 (void) strlcpy(dsname, ha->failed, sizeof (ha->failed)); 3851 3852 dsl_sync_task_group_destroy(ha->dstg); 3853 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3854 spa_close(spa, FTAG); 3855 3856 /* 3857 * We can get EBUSY if we were racing with deferred destroy and 3858 * dsl_dataset_user_release_check() hadn't done the necessary 3859 * open context setup. We can also get EBUSY if we're racing 3860 * with destroy and that thread is the ds_owner. Either way 3861 * the busy condition should be transient, and we should retry 3862 * the release operation. 3863 */ 3864 if (error == EBUSY) 3865 goto top; 3866 3867 return (error); 3868 } 3869 3870 /* 3871 * Called at spa_load time to release a stale temporary user hold. 3872 */ 3873 int 3874 dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 3875 { 3876 dsl_dataset_t *ds; 3877 char *snap; 3878 char *name; 3879 int namelen; 3880 int error; 3881 3882 rw_enter(&dp->dp_config_rwlock, RW_READER); 3883 error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 3884 rw_exit(&dp->dp_config_rwlock); 3885 if (error) 3886 return (error); 3887 namelen = dsl_dataset_namelen(ds)+1; 3888 name = kmem_alloc(namelen, KM_SLEEP); 3889 dsl_dataset_name(ds, name); 3890 dsl_dataset_rele(ds, FTAG); 3891 3892 snap = strchr(name, '@'); 3893 *snap = '\0'; 3894 ++snap; 3895 return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 3896 } 3897 3898 int 3899 dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 3900 { 3901 dsl_dataset_t *ds; 3902 int err; 3903 3904 err = dsl_dataset_hold(dsname, FTAG, &ds); 3905 if (err) 3906 return (err); 3907 3908 VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 3909 if (ds->ds_phys->ds_userrefs_obj != 0) { 3910 zap_attribute_t *za; 3911 zap_cursor_t zc; 3912 3913 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 3914 for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 3915 ds->ds_phys->ds_userrefs_obj); 3916 zap_cursor_retrieve(&zc, za) == 0; 3917 zap_cursor_advance(&zc)) { 3918 VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 3919 za->za_first_integer)); 3920 } 3921 zap_cursor_fini(&zc); 3922 kmem_free(za, sizeof (zap_attribute_t)); 3923 } 3924 dsl_dataset_rele(ds, FTAG); 3925 return (0); 3926 } 3927 3928 /* 3929 * Note, this fuction is used as the callback for dmu_objset_find(). We 3930 * always return 0 so that we will continue to find and process 3931 * inconsistent datasets, even if we encounter an error trying to 3932 * process one of them. 3933 */ 3934 /* ARGSUSED */ 3935 int 3936 dsl_destroy_inconsistent(const char *dsname, void *arg) 3937 { 3938 dsl_dataset_t *ds; 3939 3940 if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 3941 if (DS_IS_INCONSISTENT(ds)) 3942 (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 3943 else 3944 dsl_dataset_disown(ds, FTAG); 3945 } 3946 return (0); 3947 } 3948