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