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