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