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