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_UNIQUE, 2021 dsl_dataset_unique(ds)); 2022 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 2023 ds->ds_object); 2024 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, ds->ds_userrefs); 2025 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 2026 DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2027 2028 if (ds->ds_phys->ds_next_snap_obj) { 2029 /* 2030 * This is a snapshot; override the dd's space used with 2031 * our unique space and compression ratio. 2032 */ 2033 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 2034 ds->ds_phys->ds_unique_bytes); 2035 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 2036 ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 2037 (ds->ds_phys->ds_uncompressed_bytes * 100 / 2038 ds->ds_phys->ds_compressed_bytes)); 2039 } 2040 } 2041 2042 void 2043 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2044 { 2045 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 2046 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 2047 stat->dds_guid = ds->ds_phys->ds_guid; 2048 if (ds->ds_phys->ds_next_snap_obj) { 2049 stat->dds_is_snapshot = B_TRUE; 2050 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 2051 } else { 2052 stat->dds_is_snapshot = B_FALSE; 2053 stat->dds_num_clones = 0; 2054 } 2055 2056 /* clone origin is really a dsl_dir thing... */ 2057 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 2058 if (dsl_dir_is_clone(ds->ds_dir)) { 2059 dsl_dataset_t *ods; 2060 2061 VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 2062 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 2063 dsl_dataset_name(ods, stat->dds_origin); 2064 dsl_dataset_drop_ref(ods, FTAG); 2065 } else { 2066 stat->dds_origin[0] = '\0'; 2067 } 2068 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 2069 } 2070 2071 uint64_t 2072 dsl_dataset_fsid_guid(dsl_dataset_t *ds) 2073 { 2074 return (ds->ds_fsid_guid); 2075 } 2076 2077 void 2078 dsl_dataset_space(dsl_dataset_t *ds, 2079 uint64_t *refdbytesp, uint64_t *availbytesp, 2080 uint64_t *usedobjsp, uint64_t *availobjsp) 2081 { 2082 *refdbytesp = ds->ds_phys->ds_used_bytes; 2083 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 2084 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 2085 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 2086 if (ds->ds_quota != 0) { 2087 /* 2088 * Adjust available bytes according to refquota 2089 */ 2090 if (*refdbytesp < ds->ds_quota) 2091 *availbytesp = MIN(*availbytesp, 2092 ds->ds_quota - *refdbytesp); 2093 else 2094 *availbytesp = 0; 2095 } 2096 *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 2097 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2098 } 2099 2100 boolean_t 2101 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 2102 { 2103 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2104 2105 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 2106 dsl_pool_sync_context(dp)); 2107 if (ds->ds_prev == NULL) 2108 return (B_FALSE); 2109 if (ds->ds_phys->ds_bp.blk_birth > 2110 ds->ds_prev->ds_phys->ds_creation_txg) 2111 return (B_TRUE); 2112 return (B_FALSE); 2113 } 2114 2115 /* ARGSUSED */ 2116 static int 2117 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2118 { 2119 dsl_dataset_t *ds = arg1; 2120 char *newsnapname = arg2; 2121 dsl_dir_t *dd = ds->ds_dir; 2122 dsl_dataset_t *hds; 2123 uint64_t val; 2124 int err; 2125 2126 err = dsl_dataset_hold_obj(dd->dd_pool, 2127 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2128 if (err) 2129 return (err); 2130 2131 /* new name better not be in use */ 2132 err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 2133 dsl_dataset_rele(hds, FTAG); 2134 2135 if (err == 0) 2136 err = EEXIST; 2137 else if (err == ENOENT) 2138 err = 0; 2139 2140 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 2141 if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 2142 err = ENAMETOOLONG; 2143 2144 return (err); 2145 } 2146 2147 static void 2148 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 2149 cred_t *cr, dmu_tx_t *tx) 2150 { 2151 dsl_dataset_t *ds = arg1; 2152 const char *newsnapname = arg2; 2153 dsl_dir_t *dd = ds->ds_dir; 2154 objset_t *mos = dd->dd_pool->dp_meta_objset; 2155 dsl_dataset_t *hds; 2156 int err; 2157 2158 ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2159 2160 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 2161 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2162 2163 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2164 err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2165 ASSERT3U(err, ==, 0); 2166 mutex_enter(&ds->ds_lock); 2167 (void) strcpy(ds->ds_snapname, newsnapname); 2168 mutex_exit(&ds->ds_lock); 2169 err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 2170 ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2171 ASSERT3U(err, ==, 0); 2172 2173 spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 2174 cr, "dataset = %llu", ds->ds_object); 2175 dsl_dataset_rele(hds, FTAG); 2176 } 2177 2178 struct renamesnaparg { 2179 dsl_sync_task_group_t *dstg; 2180 char failed[MAXPATHLEN]; 2181 char *oldsnap; 2182 char *newsnap; 2183 }; 2184 2185 static int 2186 dsl_snapshot_rename_one(char *name, void *arg) 2187 { 2188 struct renamesnaparg *ra = arg; 2189 dsl_dataset_t *ds = NULL; 2190 char *cp; 2191 int err; 2192 2193 cp = name + strlen(name); 2194 *cp = '@'; 2195 (void) strcpy(cp + 1, ra->oldsnap); 2196 2197 /* 2198 * For recursive snapshot renames the parent won't be changing 2199 * so we just pass name for both the to/from argument. 2200 */ 2201 err = zfs_secpolicy_rename_perms(name, name, CRED()); 2202 if (err == ENOENT) { 2203 return (0); 2204 } else if (err) { 2205 (void) strcpy(ra->failed, name); 2206 return (err); 2207 } 2208 2209 #ifdef _KERNEL 2210 /* 2211 * For all filesystems undergoing rename, we'll need to unmount it. 2212 */ 2213 (void) zfs_unmount_snap(name, NULL); 2214 #endif 2215 err = dsl_dataset_hold(name, ra->dstg, &ds); 2216 *cp = '\0'; 2217 if (err == ENOENT) { 2218 return (0); 2219 } else if (err) { 2220 (void) strcpy(ra->failed, name); 2221 return (err); 2222 } 2223 2224 dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 2225 dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 2226 2227 return (0); 2228 } 2229 2230 static int 2231 dsl_recursive_rename(char *oldname, const char *newname) 2232 { 2233 int err; 2234 struct renamesnaparg *ra; 2235 dsl_sync_task_t *dst; 2236 spa_t *spa; 2237 char *cp, *fsname = spa_strdup(oldname); 2238 int len = strlen(oldname); 2239 2240 /* truncate the snapshot name to get the fsname */ 2241 cp = strchr(fsname, '@'); 2242 *cp = '\0'; 2243 2244 err = spa_open(fsname, &spa, FTAG); 2245 if (err) { 2246 kmem_free(fsname, len + 1); 2247 return (err); 2248 } 2249 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 2250 ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 2251 2252 ra->oldsnap = strchr(oldname, '@') + 1; 2253 ra->newsnap = strchr(newname, '@') + 1; 2254 *ra->failed = '\0'; 2255 2256 err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 2257 DS_FIND_CHILDREN); 2258 kmem_free(fsname, len + 1); 2259 2260 if (err == 0) { 2261 err = dsl_sync_task_group_wait(ra->dstg); 2262 } 2263 2264 for (dst = list_head(&ra->dstg->dstg_tasks); dst; 2265 dst = list_next(&ra->dstg->dstg_tasks, dst)) { 2266 dsl_dataset_t *ds = dst->dst_arg1; 2267 if (dst->dst_err) { 2268 dsl_dir_name(ds->ds_dir, ra->failed); 2269 (void) strcat(ra->failed, "@"); 2270 (void) strcat(ra->failed, ra->newsnap); 2271 } 2272 dsl_dataset_rele(ds, ra->dstg); 2273 } 2274 2275 if (err) 2276 (void) strcpy(oldname, ra->failed); 2277 2278 dsl_sync_task_group_destroy(ra->dstg); 2279 kmem_free(ra, sizeof (struct renamesnaparg)); 2280 spa_close(spa, FTAG); 2281 return (err); 2282 } 2283 2284 static int 2285 dsl_valid_rename(char *oldname, void *arg) 2286 { 2287 int delta = *(int *)arg; 2288 2289 if (strlen(oldname) + delta >= MAXNAMELEN) 2290 return (ENAMETOOLONG); 2291 2292 return (0); 2293 } 2294 2295 #pragma weak dmu_objset_rename = dsl_dataset_rename 2296 int 2297 dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2298 { 2299 dsl_dir_t *dd; 2300 dsl_dataset_t *ds; 2301 const char *tail; 2302 int err; 2303 2304 err = dsl_dir_open(oldname, FTAG, &dd, &tail); 2305 if (err) 2306 return (err); 2307 /* 2308 * If there are more than 2 references there may be holds 2309 * hanging around that haven't been cleared out yet. 2310 */ 2311 if (dmu_buf_refcount(dd->dd_dbuf) > 2) 2312 txg_wait_synced(dd->dd_pool, 0); 2313 if (tail == NULL) { 2314 int delta = strlen(newname) - strlen(oldname); 2315 2316 /* if we're growing, validate child name lengths */ 2317 if (delta > 0) 2318 err = dmu_objset_find(oldname, dsl_valid_rename, 2319 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 2320 2321 if (!err) 2322 err = dsl_dir_rename(dd, newname); 2323 dsl_dir_close(dd, FTAG); 2324 return (err); 2325 } 2326 if (tail[0] != '@') { 2327 /* the name ended in a nonexistant component */ 2328 dsl_dir_close(dd, FTAG); 2329 return (ENOENT); 2330 } 2331 2332 dsl_dir_close(dd, FTAG); 2333 2334 /* new name must be snapshot in same filesystem */ 2335 tail = strchr(newname, '@'); 2336 if (tail == NULL) 2337 return (EINVAL); 2338 tail++; 2339 if (strncmp(oldname, newname, tail - newname) != 0) 2340 return (EXDEV); 2341 2342 if (recursive) { 2343 err = dsl_recursive_rename(oldname, newname); 2344 } else { 2345 err = dsl_dataset_hold(oldname, FTAG, &ds); 2346 if (err) 2347 return (err); 2348 2349 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2350 dsl_dataset_snapshot_rename_check, 2351 dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 2352 2353 dsl_dataset_rele(ds, FTAG); 2354 } 2355 2356 return (err); 2357 } 2358 2359 struct promotenode { 2360 list_node_t link; 2361 dsl_dataset_t *ds; 2362 }; 2363 2364 struct promotearg { 2365 list_t shared_snaps, origin_snaps, clone_snaps; 2366 dsl_dataset_t *origin_origin, *origin_head; 2367 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2368 }; 2369 2370 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 2371 2372 /* ARGSUSED */ 2373 static int 2374 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 2375 { 2376 dsl_dataset_t *hds = arg1; 2377 struct promotearg *pa = arg2; 2378 struct promotenode *snap = list_head(&pa->shared_snaps); 2379 dsl_dataset_t *origin_ds = snap->ds; 2380 int err; 2381 2382 /* Check that it is a real clone */ 2383 if (!dsl_dir_is_clone(hds->ds_dir)) 2384 return (EINVAL); 2385 2386 /* Since this is so expensive, don't do the preliminary check */ 2387 if (!dmu_tx_is_syncing(tx)) 2388 return (0); 2389 2390 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 2391 return (EXDEV); 2392 2393 /* compute origin's new unique space */ 2394 snap = list_tail(&pa->clone_snaps); 2395 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2396 err = bplist_space_birthrange(&snap->ds->ds_deadlist, 2397 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 2398 if (err) 2399 return (err); 2400 2401 /* 2402 * Walk the snapshots that we are moving 2403 * 2404 * Compute space to transfer. Consider the incremental changes 2405 * to used for each snapshot: 2406 * (my used) = (prev's used) + (blocks born) - (blocks killed) 2407 * So each snapshot gave birth to: 2408 * (blocks born) = (my used) - (prev's used) + (blocks killed) 2409 * So a sequence would look like: 2410 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2411 * Which simplifies to: 2412 * uN + kN + kN-1 + ... + k1 + k0 2413 * Note however, if we stop before we reach the ORIGIN we get: 2414 * uN + kN + kN-1 + ... + kM - uM-1 2415 */ 2416 pa->used = origin_ds->ds_phys->ds_used_bytes; 2417 pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 2418 pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 2419 for (snap = list_head(&pa->shared_snaps); snap; 2420 snap = list_next(&pa->shared_snaps, snap)) { 2421 uint64_t val, dlused, dlcomp, dluncomp; 2422 dsl_dataset_t *ds = snap->ds; 2423 2424 /* Check that the snapshot name does not conflict */ 2425 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2426 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2427 if (err == 0) 2428 return (EEXIST); 2429 if (err != ENOENT) 2430 return (err); 2431 2432 /* The very first snapshot does not have a deadlist */ 2433 if (ds->ds_phys->ds_prev_snap_obj == 0) 2434 continue; 2435 2436 if (err = bplist_space(&ds->ds_deadlist, 2437 &dlused, &dlcomp, &dluncomp)) 2438 return (err); 2439 pa->used += dlused; 2440 pa->comp += dlcomp; 2441 pa->uncomp += dluncomp; 2442 } 2443 2444 /* 2445 * If we are a clone of a clone then we never reached ORIGIN, 2446 * so we need to subtract out the clone origin's used space. 2447 */ 2448 if (pa->origin_origin) { 2449 pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 2450 pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 2451 pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 2452 } 2453 2454 /* Check that there is enough space here */ 2455 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2456 pa->used); 2457 if (err) 2458 return (err); 2459 2460 /* 2461 * Compute the amounts of space that will be used by snapshots 2462 * after the promotion (for both origin and clone). For each, 2463 * it is the amount of space that will be on all of their 2464 * deadlists (that was not born before their new origin). 2465 */ 2466 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2467 uint64_t space; 2468 2469 /* 2470 * Note, typically this will not be a clone of a clone, 2471 * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 2472 * these snaplist_space() -> bplist_space_birthrange() 2473 * calls will be fast because they do not have to 2474 * iterate over all bps. 2475 */ 2476 snap = list_head(&pa->origin_snaps); 2477 err = snaplist_space(&pa->shared_snaps, 2478 snap->ds->ds_origin_txg, &pa->cloneusedsnap); 2479 if (err) 2480 return (err); 2481 2482 err = snaplist_space(&pa->clone_snaps, 2483 snap->ds->ds_origin_txg, &space); 2484 if (err) 2485 return (err); 2486 pa->cloneusedsnap += space; 2487 } 2488 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2489 err = snaplist_space(&pa->origin_snaps, 2490 origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 2491 if (err) 2492 return (err); 2493 } 2494 2495 return (0); 2496 } 2497 2498 static void 2499 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 2500 { 2501 dsl_dataset_t *hds = arg1; 2502 struct promotearg *pa = arg2; 2503 struct promotenode *snap = list_head(&pa->shared_snaps); 2504 dsl_dataset_t *origin_ds = snap->ds; 2505 dsl_dataset_t *origin_head; 2506 dsl_dir_t *dd = hds->ds_dir; 2507 dsl_pool_t *dp = hds->ds_dir->dd_pool; 2508 dsl_dir_t *odd = NULL; 2509 uint64_t oldnext_obj; 2510 int64_t delta; 2511 2512 ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 2513 2514 snap = list_head(&pa->origin_snaps); 2515 origin_head = snap->ds; 2516 2517 /* 2518 * We need to explicitly open odd, since origin_ds's dd will be 2519 * changing. 2520 */ 2521 VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 2522 NULL, FTAG, &odd)); 2523 2524 /* change origin's next snap */ 2525 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2526 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 2527 snap = list_tail(&pa->clone_snaps); 2528 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2529 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 2530 2531 /* change the origin's next clone */ 2532 if (origin_ds->ds_phys->ds_next_clones_obj) { 2533 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 2534 origin_ds->ds_phys->ds_next_clones_obj, 2535 origin_ds->ds_phys->ds_next_snap_obj, tx)); 2536 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 2537 origin_ds->ds_phys->ds_next_clones_obj, 2538 oldnext_obj, tx)); 2539 } 2540 2541 /* change origin */ 2542 dmu_buf_will_dirty(dd->dd_dbuf, tx); 2543 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2544 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 2545 hds->ds_origin_txg = origin_head->ds_origin_txg; 2546 dmu_buf_will_dirty(odd->dd_dbuf, tx); 2547 odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 2548 origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 2549 2550 /* move snapshots to this dir */ 2551 for (snap = list_head(&pa->shared_snaps); snap; 2552 snap = list_next(&pa->shared_snaps, snap)) { 2553 dsl_dataset_t *ds = snap->ds; 2554 2555 /* unregister props as dsl_dir is changing */ 2556 if (ds->ds_objset) { 2557 dmu_objset_evict(ds->ds_objset); 2558 ds->ds_objset = NULL; 2559 } 2560 /* move snap name entry */ 2561 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2562 VERIFY(0 == dsl_dataset_snap_remove(origin_head, 2563 ds->ds_snapname, tx)); 2564 VERIFY(0 == zap_add(dp->dp_meta_objset, 2565 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 2566 8, 1, &ds->ds_object, tx)); 2567 /* change containing dsl_dir */ 2568 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2569 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 2570 ds->ds_phys->ds_dir_obj = dd->dd_object; 2571 ASSERT3P(ds->ds_dir, ==, odd); 2572 dsl_dir_close(ds->ds_dir, ds); 2573 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 2574 NULL, ds, &ds->ds_dir)); 2575 2576 ASSERT3U(dsl_prop_numcb(ds), ==, 0); 2577 } 2578 2579 /* 2580 * Change space accounting. 2581 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 2582 * both be valid, or both be 0 (resulting in delta == 0). This 2583 * is true for each of {clone,origin} independently. 2584 */ 2585 2586 delta = pa->cloneusedsnap - 2587 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2588 ASSERT3S(delta, >=, 0); 2589 ASSERT3U(pa->used, >=, delta); 2590 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 2591 dsl_dir_diduse_space(dd, DD_USED_HEAD, 2592 pa->used - delta, pa->comp, pa->uncomp, tx); 2593 2594 delta = pa->originusedsnap - 2595 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2596 ASSERT3S(delta, <=, 0); 2597 ASSERT3U(pa->used, >=, -delta); 2598 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 2599 dsl_dir_diduse_space(odd, DD_USED_HEAD, 2600 -pa->used - delta, -pa->comp, -pa->uncomp, tx); 2601 2602 origin_ds->ds_phys->ds_unique_bytes = pa->unique; 2603 2604 /* log history record */ 2605 spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 2606 cr, "dataset = %llu", hds->ds_object); 2607 2608 dsl_dir_close(odd, FTAG); 2609 } 2610 2611 static char *snaplist_tag = "snaplist"; 2612 /* 2613 * Make a list of dsl_dataset_t's for the snapshots between first_obj 2614 * (exclusive) and last_obj (inclusive). The list will be in reverse 2615 * order (last_obj will be the list_head()). If first_obj == 0, do all 2616 * snapshots back to this dataset's origin. 2617 */ 2618 static int 2619 snaplist_make(dsl_pool_t *dp, boolean_t own, 2620 uint64_t first_obj, uint64_t last_obj, list_t *l) 2621 { 2622 uint64_t obj = last_obj; 2623 2624 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 2625 2626 list_create(l, sizeof (struct promotenode), 2627 offsetof(struct promotenode, link)); 2628 2629 while (obj != first_obj) { 2630 dsl_dataset_t *ds; 2631 struct promotenode *snap; 2632 int err; 2633 2634 if (own) { 2635 err = dsl_dataset_own_obj(dp, obj, 2636 0, snaplist_tag, &ds); 2637 if (err == 0) 2638 dsl_dataset_make_exclusive(ds, snaplist_tag); 2639 } else { 2640 err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 2641 } 2642 if (err == ENOENT) { 2643 /* lost race with snapshot destroy */ 2644 struct promotenode *last = list_tail(l); 2645 ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 2646 obj = last->ds->ds_phys->ds_prev_snap_obj; 2647 continue; 2648 } else if (err) { 2649 return (err); 2650 } 2651 2652 if (first_obj == 0) 2653 first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 2654 2655 snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 2656 snap->ds = ds; 2657 list_insert_tail(l, snap); 2658 obj = ds->ds_phys->ds_prev_snap_obj; 2659 } 2660 2661 return (0); 2662 } 2663 2664 static int 2665 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2666 { 2667 struct promotenode *snap; 2668 2669 *spacep = 0; 2670 for (snap = list_head(l); snap; snap = list_next(l, snap)) { 2671 uint64_t used; 2672 int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 2673 mintxg, UINT64_MAX, &used); 2674 if (err) 2675 return (err); 2676 *spacep += used; 2677 } 2678 return (0); 2679 } 2680 2681 static void 2682 snaplist_destroy(list_t *l, boolean_t own) 2683 { 2684 struct promotenode *snap; 2685 2686 if (!l || !list_link_active(&l->list_head)) 2687 return; 2688 2689 while ((snap = list_tail(l)) != NULL) { 2690 list_remove(l, snap); 2691 if (own) 2692 dsl_dataset_disown(snap->ds, snaplist_tag); 2693 else 2694 dsl_dataset_rele(snap->ds, snaplist_tag); 2695 kmem_free(snap, sizeof (struct promotenode)); 2696 } 2697 list_destroy(l); 2698 } 2699 2700 /* 2701 * Promote a clone. Nomenclature note: 2702 * "clone" or "cds": the original clone which is being promoted 2703 * "origin" or "ods": the snapshot which is originally clone's origin 2704 * "origin head" or "ohds": the dataset which is the head 2705 * (filesystem/volume) for the origin 2706 * "origin origin": the origin of the origin's filesystem (typically 2707 * NULL, indicating that the clone is not a clone of a clone). 2708 */ 2709 int 2710 dsl_dataset_promote(const char *name) 2711 { 2712 dsl_dataset_t *ds; 2713 dsl_dir_t *dd; 2714 dsl_pool_t *dp; 2715 dmu_object_info_t doi; 2716 struct promotearg pa = { 0 }; 2717 struct promotenode *snap; 2718 int err; 2719 2720 err = dsl_dataset_hold(name, FTAG, &ds); 2721 if (err) 2722 return (err); 2723 dd = ds->ds_dir; 2724 dp = dd->dd_pool; 2725 2726 err = dmu_object_info(dp->dp_meta_objset, 2727 ds->ds_phys->ds_snapnames_zapobj, &doi); 2728 if (err) { 2729 dsl_dataset_rele(ds, FTAG); 2730 return (err); 2731 } 2732 2733 if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 2734 dsl_dataset_rele(ds, FTAG); 2735 return (EINVAL); 2736 } 2737 2738 /* 2739 * We are going to inherit all the snapshots taken before our 2740 * origin (i.e., our new origin will be our parent's origin). 2741 * Take ownership of them so that we can rename them into our 2742 * namespace. 2743 */ 2744 rw_enter(&dp->dp_config_rwlock, RW_READER); 2745 2746 err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 2747 &pa.shared_snaps); 2748 if (err != 0) 2749 goto out; 2750 2751 err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 2752 if (err != 0) 2753 goto out; 2754 2755 snap = list_head(&pa.shared_snaps); 2756 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 2757 err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 2758 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 2759 if (err != 0) 2760 goto out; 2761 2762 if (dsl_dir_is_clone(snap->ds->ds_dir)) { 2763 err = dsl_dataset_own_obj(dp, 2764 snap->ds->ds_dir->dd_phys->dd_origin_obj, 2765 0, FTAG, &pa.origin_origin); 2766 if (err != 0) 2767 goto out; 2768 } 2769 2770 out: 2771 rw_exit(&dp->dp_config_rwlock); 2772 2773 /* 2774 * Add in 128x the snapnames zapobj size, since we will be moving 2775 * a bunch of snapnames to the promoted ds, and dirtying their 2776 * bonus buffers. 2777 */ 2778 if (err == 0) { 2779 err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 2780 dsl_dataset_promote_sync, ds, &pa, 2781 2 + 2 * doi.doi_physical_blks); 2782 } 2783 2784 snaplist_destroy(&pa.shared_snaps, B_TRUE); 2785 snaplist_destroy(&pa.clone_snaps, B_FALSE); 2786 snaplist_destroy(&pa.origin_snaps, B_FALSE); 2787 if (pa.origin_origin) 2788 dsl_dataset_disown(pa.origin_origin, FTAG); 2789 dsl_dataset_rele(ds, FTAG); 2790 return (err); 2791 } 2792 2793 struct cloneswaparg { 2794 dsl_dataset_t *cds; /* clone dataset */ 2795 dsl_dataset_t *ohds; /* origin's head dataset */ 2796 boolean_t force; 2797 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 2798 }; 2799 2800 /* ARGSUSED */ 2801 static int 2802 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 2803 { 2804 struct cloneswaparg *csa = arg1; 2805 2806 /* they should both be heads */ 2807 if (dsl_dataset_is_snapshot(csa->cds) || 2808 dsl_dataset_is_snapshot(csa->ohds)) 2809 return (EINVAL); 2810 2811 /* the branch point should be just before them */ 2812 if (csa->cds->ds_prev != csa->ohds->ds_prev) 2813 return (EINVAL); 2814 2815 /* cds should be the clone (unless they are unrelated) */ 2816 if (csa->cds->ds_prev != NULL && 2817 csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 2818 csa->ohds->ds_object != 2819 csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 2820 return (EINVAL); 2821 2822 /* the clone should be a child of the origin */ 2823 if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 2824 return (EINVAL); 2825 2826 /* ohds shouldn't be modified unless 'force' */ 2827 if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 2828 return (ETXTBSY); 2829 2830 /* adjust amount of any unconsumed refreservation */ 2831 csa->unused_refres_delta = 2832 (int64_t)MIN(csa->ohds->ds_reserved, 2833 csa->ohds->ds_phys->ds_unique_bytes) - 2834 (int64_t)MIN(csa->ohds->ds_reserved, 2835 csa->cds->ds_phys->ds_unique_bytes); 2836 2837 if (csa->unused_refres_delta > 0 && 2838 csa->unused_refres_delta > 2839 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 2840 return (ENOSPC); 2841 2842 return (0); 2843 } 2844 2845 /* ARGSUSED */ 2846 static void 2847 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 2848 { 2849 struct cloneswaparg *csa = arg1; 2850 dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 2851 2852 ASSERT(csa->cds->ds_reserved == 0); 2853 ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota); 2854 2855 dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 2856 dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 2857 2858 if (csa->cds->ds_objset != NULL) { 2859 dmu_objset_evict(csa->cds->ds_objset); 2860 csa->cds->ds_objset = NULL; 2861 } 2862 2863 if (csa->ohds->ds_objset != NULL) { 2864 dmu_objset_evict(csa->ohds->ds_objset); 2865 csa->ohds->ds_objset = NULL; 2866 } 2867 2868 /* 2869 * Reset origin's unique bytes, if it exists. 2870 */ 2871 if (csa->cds->ds_prev) { 2872 dsl_dataset_t *origin = csa->cds->ds_prev; 2873 dmu_buf_will_dirty(origin->ds_dbuf, tx); 2874 VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 2875 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 2876 &origin->ds_phys->ds_unique_bytes)); 2877 } 2878 2879 /* swap blkptrs */ 2880 { 2881 blkptr_t tmp; 2882 tmp = csa->ohds->ds_phys->ds_bp; 2883 csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 2884 csa->cds->ds_phys->ds_bp = tmp; 2885 } 2886 2887 /* set dd_*_bytes */ 2888 { 2889 int64_t dused, dcomp, duncomp; 2890 uint64_t cdl_used, cdl_comp, cdl_uncomp; 2891 uint64_t odl_used, odl_comp, odl_uncomp; 2892 2893 ASSERT3U(csa->cds->ds_dir->dd_phys-> 2894 dd_used_breakdown[DD_USED_SNAP], ==, 0); 2895 2896 VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 2897 &cdl_comp, &cdl_uncomp)); 2898 VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 2899 &odl_comp, &odl_uncomp)); 2900 2901 dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 2902 (csa->ohds->ds_phys->ds_used_bytes + odl_used); 2903 dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 2904 (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 2905 duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 2906 cdl_uncomp - 2907 (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2908 2909 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 2910 dused, dcomp, duncomp, tx); 2911 dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 2912 -dused, -dcomp, -duncomp, tx); 2913 2914 /* 2915 * The difference in the space used by snapshots is the 2916 * difference in snapshot space due to the head's 2917 * deadlist (since that's the only thing that's 2918 * changing that affects the snapused). 2919 */ 2920 VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 2921 csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 2922 VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 2923 csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 2924 dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 2925 DD_USED_HEAD, DD_USED_SNAP, tx); 2926 } 2927 2928 #define SWITCH64(x, y) \ 2929 { \ 2930 uint64_t __tmp = (x); \ 2931 (x) = (y); \ 2932 (y) = __tmp; \ 2933 } 2934 2935 /* swap ds_*_bytes */ 2936 SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 2937 csa->cds->ds_phys->ds_used_bytes); 2938 SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 2939 csa->cds->ds_phys->ds_compressed_bytes); 2940 SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 2941 csa->cds->ds_phys->ds_uncompressed_bytes); 2942 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 2943 csa->cds->ds_phys->ds_unique_bytes); 2944 2945 /* apply any parent delta for change in unconsumed refreservation */ 2946 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 2947 csa->unused_refres_delta, 0, 0, tx); 2948 2949 /* swap deadlists */ 2950 bplist_close(&csa->cds->ds_deadlist); 2951 bplist_close(&csa->ohds->ds_deadlist); 2952 SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 2953 csa->cds->ds_phys->ds_deadlist_obj); 2954 VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 2955 csa->cds->ds_phys->ds_deadlist_obj)); 2956 VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 2957 csa->ohds->ds_phys->ds_deadlist_obj)); 2958 2959 dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 2960 } 2961 2962 /* 2963 * Swap 'clone' with its origin head datasets. Used at the end of "zfs 2964 * recv" into an existing fs to swizzle the file system to the new 2965 * version, and by "zfs rollback". Can also be used to swap two 2966 * independent head datasets if neither has any snapshots. 2967 */ 2968 int 2969 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 2970 boolean_t force) 2971 { 2972 struct cloneswaparg csa; 2973 int error; 2974 2975 ASSERT(clone->ds_owner); 2976 ASSERT(origin_head->ds_owner); 2977 retry: 2978 /* Need exclusive access for the swap */ 2979 rw_enter(&clone->ds_rwlock, RW_WRITER); 2980 if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 2981 rw_exit(&clone->ds_rwlock); 2982 rw_enter(&origin_head->ds_rwlock, RW_WRITER); 2983 if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 2984 rw_exit(&origin_head->ds_rwlock); 2985 goto retry; 2986 } 2987 } 2988 csa.cds = clone; 2989 csa.ohds = origin_head; 2990 csa.force = force; 2991 error = dsl_sync_task_do(clone->ds_dir->dd_pool, 2992 dsl_dataset_clone_swap_check, 2993 dsl_dataset_clone_swap_sync, &csa, NULL, 9); 2994 return (error); 2995 } 2996 2997 /* 2998 * Given a pool name and a dataset object number in that pool, 2999 * return the name of that dataset. 3000 */ 3001 int 3002 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 3003 { 3004 spa_t *spa; 3005 dsl_pool_t *dp; 3006 dsl_dataset_t *ds; 3007 int error; 3008 3009 if ((error = spa_open(pname, &spa, FTAG)) != 0) 3010 return (error); 3011 dp = spa_get_dsl(spa); 3012 rw_enter(&dp->dp_config_rwlock, RW_READER); 3013 if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 3014 dsl_dataset_name(ds, buf); 3015 dsl_dataset_rele(ds, FTAG); 3016 } 3017 rw_exit(&dp->dp_config_rwlock); 3018 spa_close(spa, FTAG); 3019 3020 return (error); 3021 } 3022 3023 int 3024 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 3025 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 3026 { 3027 int error = 0; 3028 3029 ASSERT3S(asize, >, 0); 3030 3031 /* 3032 * *ref_rsrv is the portion of asize that will come from any 3033 * unconsumed refreservation space. 3034 */ 3035 *ref_rsrv = 0; 3036 3037 mutex_enter(&ds->ds_lock); 3038 /* 3039 * Make a space adjustment for reserved bytes. 3040 */ 3041 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 3042 ASSERT3U(*used, >=, 3043 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 3044 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 3045 *ref_rsrv = 3046 asize - MIN(asize, parent_delta(ds, asize + inflight)); 3047 } 3048 3049 if (!check_quota || ds->ds_quota == 0) { 3050 mutex_exit(&ds->ds_lock); 3051 return (0); 3052 } 3053 /* 3054 * If they are requesting more space, and our current estimate 3055 * is over quota, they get to try again unless the actual 3056 * on-disk is over quota and there are no pending changes (which 3057 * may free up space for us). 3058 */ 3059 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 3060 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 3061 error = ERESTART; 3062 else 3063 error = EDQUOT; 3064 } 3065 mutex_exit(&ds->ds_lock); 3066 3067 return (error); 3068 } 3069 3070 /* ARGSUSED */ 3071 static int 3072 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 3073 { 3074 dsl_dataset_t *ds = arg1; 3075 uint64_t *quotap = arg2; 3076 uint64_t new_quota = *quotap; 3077 3078 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 3079 return (ENOTSUP); 3080 3081 if (new_quota == 0) 3082 return (0); 3083 3084 if (new_quota < ds->ds_phys->ds_used_bytes || 3085 new_quota < ds->ds_reserved) 3086 return (ENOSPC); 3087 3088 return (0); 3089 } 3090 3091 /* ARGSUSED */ 3092 void 3093 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 3094 { 3095 dsl_dataset_t *ds = arg1; 3096 uint64_t *quotap = arg2; 3097 uint64_t new_quota = *quotap; 3098 3099 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3100 3101 ds->ds_quota = new_quota; 3102 3103 dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 3104 3105 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 3106 tx, cr, "%lld dataset = %llu ", 3107 (longlong_t)new_quota, ds->ds_object); 3108 } 3109 3110 int 3111 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 3112 { 3113 dsl_dataset_t *ds; 3114 int err; 3115 3116 err = dsl_dataset_hold(dsname, FTAG, &ds); 3117 if (err) 3118 return (err); 3119 3120 if (quota != ds->ds_quota) { 3121 /* 3122 * If someone removes a file, then tries to set the quota, we 3123 * want to make sure the file freeing takes effect. 3124 */ 3125 txg_wait_open(ds->ds_dir->dd_pool, 0); 3126 3127 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 3128 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 3129 ds, "a, 0); 3130 } 3131 dsl_dataset_rele(ds, FTAG); 3132 return (err); 3133 } 3134 3135 static int 3136 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 3137 { 3138 dsl_dataset_t *ds = arg1; 3139 uint64_t *reservationp = arg2; 3140 uint64_t new_reservation = *reservationp; 3141 uint64_t unique; 3142 3143 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 3144 SPA_VERSION_REFRESERVATION) 3145 return (ENOTSUP); 3146 3147 if (dsl_dataset_is_snapshot(ds)) 3148 return (EINVAL); 3149 3150 /* 3151 * If we are doing the preliminary check in open context, the 3152 * space estimates may be inaccurate. 3153 */ 3154 if (!dmu_tx_is_syncing(tx)) 3155 return (0); 3156 3157 mutex_enter(&ds->ds_lock); 3158 unique = dsl_dataset_unique(ds); 3159 mutex_exit(&ds->ds_lock); 3160 3161 if (MAX(unique, new_reservation) > MAX(unique, ds->ds_reserved)) { 3162 uint64_t delta = MAX(unique, new_reservation) - 3163 MAX(unique, ds->ds_reserved); 3164 3165 if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 3166 return (ENOSPC); 3167 if (ds->ds_quota > 0 && 3168 new_reservation > ds->ds_quota) 3169 return (ENOSPC); 3170 } 3171 3172 return (0); 3173 } 3174 3175 /* ARGSUSED */ 3176 static void 3177 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 3178 dmu_tx_t *tx) 3179 { 3180 dsl_dataset_t *ds = arg1; 3181 uint64_t *reservationp = arg2; 3182 uint64_t new_reservation = *reservationp; 3183 uint64_t unique; 3184 int64_t delta; 3185 3186 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3187 3188 mutex_enter(&ds->ds_dir->dd_lock); 3189 mutex_enter(&ds->ds_lock); 3190 unique = dsl_dataset_unique(ds); 3191 delta = MAX(0, (int64_t)(new_reservation - unique)) - 3192 MAX(0, (int64_t)(ds->ds_reserved - unique)); 3193 ds->ds_reserved = new_reservation; 3194 mutex_exit(&ds->ds_lock); 3195 3196 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 3197 mutex_exit(&ds->ds_dir->dd_lock); 3198 dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refreservation", 3199 new_reservation, cr, tx); 3200 3201 spa_history_internal_log(LOG_DS_REFRESERV, 3202 ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 3203 (longlong_t)new_reservation, ds->ds_object); 3204 } 3205 3206 int 3207 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 3208 { 3209 dsl_dataset_t *ds; 3210 int err; 3211 3212 err = dsl_dataset_hold(dsname, FTAG, &ds); 3213 if (err) 3214 return (err); 3215 3216 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 3217 dsl_dataset_set_reservation_check, 3218 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 3219 dsl_dataset_rele(ds, FTAG); 3220 return (err); 3221 } 3222 3223 struct dsl_ds_holdarg { 3224 dsl_sync_task_group_t *dstg; 3225 char *htag; 3226 char *snapname; 3227 boolean_t recursive; 3228 boolean_t gotone; 3229 boolean_t temphold; 3230 char failed[MAXPATHLEN]; 3231 }; 3232 3233 static int 3234 dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 3235 { 3236 dsl_dataset_t *ds = arg1; 3237 struct dsl_ds_holdarg *ha = arg2; 3238 char *htag = ha->htag; 3239 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3240 int error = 0; 3241 3242 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 3243 return (ENOTSUP); 3244 3245 if (!dsl_dataset_is_snapshot(ds)) 3246 return (EINVAL); 3247 3248 /* tags must be unique */ 3249 mutex_enter(&ds->ds_lock); 3250 if (ds->ds_phys->ds_userrefs_obj) { 3251 error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 3252 8, 1, tx); 3253 if (error == 0) 3254 error = EEXIST; 3255 else if (error == ENOENT) 3256 error = 0; 3257 } 3258 mutex_exit(&ds->ds_lock); 3259 3260 return (error); 3261 } 3262 3263 static void 3264 dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 3265 { 3266 dsl_dataset_t *ds = arg1; 3267 struct dsl_ds_holdarg *ha = arg2; 3268 char *htag = ha->htag; 3269 dsl_pool_t *dp = ds->ds_dir->dd_pool; 3270 objset_t *mos = dp->dp_meta_objset; 3271 time_t now = gethrestime_sec(); 3272 uint64_t zapobj; 3273 3274 mutex_enter(&ds->ds_lock); 3275 if (ds->ds_phys->ds_userrefs_obj == 0) { 3276 /* 3277 * This is the first user hold for this dataset. Create 3278 * the userrefs zap object. 3279 */ 3280 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3281 zapobj = ds->ds_phys->ds_userrefs_obj = 3282 zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 3283 } else { 3284 zapobj = ds->ds_phys->ds_userrefs_obj; 3285 } 3286 ds->ds_userrefs++; 3287 mutex_exit(&ds->ds_lock); 3288 3289 VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 3290 3291 if (ha->temphold) { 3292 VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 3293 htag, &now, tx)); 3294 } 3295 3296 spa_history_internal_log(LOG_DS_USER_HOLD, 3297 dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag, 3298 (int)ha->temphold, ds->ds_object); 3299 } 3300 3301 static int 3302 dsl_dataset_user_hold_one(char *dsname, void *arg) 3303 { 3304 struct dsl_ds_holdarg *ha = arg; 3305 dsl_dataset_t *ds; 3306 int error; 3307 char *name; 3308 3309 /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 3310 name = kmem_asprintf("%s@%s", dsname, ha->snapname); 3311 error = dsl_dataset_hold(name, ha->dstg, &ds); 3312 strfree(name); 3313 if (error == 0) { 3314 ha->gotone = B_TRUE; 3315 dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 3316 dsl_dataset_user_hold_sync, ds, ha, 0); 3317 } else if (error == ENOENT && ha->recursive) { 3318 error = 0; 3319 } else { 3320 (void) strcpy(ha->failed, dsname); 3321 } 3322 return (error); 3323 } 3324 3325 int 3326 dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 3327 boolean_t recursive, boolean_t temphold) 3328 { 3329 struct dsl_ds_holdarg *ha; 3330 dsl_sync_task_t *dst; 3331 spa_t *spa; 3332 int error; 3333 3334 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 3335 3336 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3337 3338 error = spa_open(dsname, &spa, FTAG); 3339 if (error) { 3340 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3341 return (error); 3342 } 3343 3344 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 3345 ha->htag = htag; 3346 ha->snapname = snapname; 3347 ha->recursive = recursive; 3348 ha->temphold = temphold; 3349 if (recursive) { 3350 error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 3351 ha, DS_FIND_CHILDREN); 3352 } else { 3353 error = dsl_dataset_user_hold_one(dsname, ha); 3354 } 3355 if (error == 0) 3356 error = dsl_sync_task_group_wait(ha->dstg); 3357 3358 for (dst = list_head(&ha->dstg->dstg_tasks); dst; 3359 dst = list_next(&ha->dstg->dstg_tasks, dst)) { 3360 dsl_dataset_t *ds = dst->dst_arg1; 3361 3362 if (dst->dst_err) { 3363 dsl_dataset_name(ds, ha->failed); 3364 *strchr(ha->failed, '@') = '\0'; 3365 } 3366 dsl_dataset_rele(ds, ha->dstg); 3367 } 3368 3369 if (error == 0 && recursive && !ha->gotone) 3370 error = ENOENT; 3371 3372 if (error) 3373 (void) strcpy(dsname, ha->failed); 3374 3375 dsl_sync_task_group_destroy(ha->dstg); 3376 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3377 spa_close(spa, FTAG); 3378 return (error); 3379 } 3380 3381 struct dsl_ds_releasearg { 3382 dsl_dataset_t *ds; 3383 const char *htag; 3384 boolean_t own; /* do we own or just hold ds? */ 3385 }; 3386 3387 static int 3388 dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 3389 boolean_t *might_destroy) 3390 { 3391 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3392 uint64_t zapobj; 3393 uint64_t tmp; 3394 int error; 3395 3396 *might_destroy = B_FALSE; 3397 3398 mutex_enter(&ds->ds_lock); 3399 zapobj = ds->ds_phys->ds_userrefs_obj; 3400 if (zapobj == 0) { 3401 /* The tag can't possibly exist */ 3402 mutex_exit(&ds->ds_lock); 3403 return (ESRCH); 3404 } 3405 3406 /* Make sure the tag exists */ 3407 error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 3408 if (error) { 3409 mutex_exit(&ds->ds_lock); 3410 if (error == ENOENT) 3411 error = ESRCH; 3412 return (error); 3413 } 3414 3415 if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 3416 DS_IS_DEFER_DESTROY(ds)) 3417 *might_destroy = B_TRUE; 3418 3419 mutex_exit(&ds->ds_lock); 3420 return (0); 3421 } 3422 3423 static int 3424 dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 3425 { 3426 struct dsl_ds_releasearg *ra = arg1; 3427 dsl_dataset_t *ds = ra->ds; 3428 boolean_t might_destroy; 3429 int error; 3430 3431 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 3432 return (ENOTSUP); 3433 3434 error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 3435 if (error) 3436 return (error); 3437 3438 if (might_destroy) { 3439 struct dsl_ds_destroyarg dsda = {0}; 3440 3441 if (dmu_tx_is_syncing(tx)) { 3442 /* 3443 * If we're not prepared to remove the snapshot, 3444 * we can't allow the release to happen right now. 3445 */ 3446 if (!ra->own) 3447 return (EBUSY); 3448 if (ds->ds_objset) { 3449 dmu_objset_evict(ds->ds_objset); 3450 ds->ds_objset = NULL; 3451 } 3452 } 3453 dsda.ds = ds; 3454 dsda.releasing = B_TRUE; 3455 return (dsl_dataset_destroy_check(&dsda, tag, tx)); 3456 } 3457 3458 return (0); 3459 } 3460 3461 static void 3462 dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 3463 { 3464 struct dsl_ds_releasearg *ra = arg1; 3465 dsl_dataset_t *ds = ra->ds; 3466 dsl_pool_t *dp = ds->ds_dir->dd_pool; 3467 objset_t *mos = dp->dp_meta_objset; 3468 uint64_t zapobj; 3469 uint64_t dsobj = ds->ds_object; 3470 uint64_t refs; 3471 int error; 3472 3473 mutex_enter(&ds->ds_lock); 3474 ds->ds_userrefs--; 3475 refs = ds->ds_userrefs; 3476 mutex_exit(&ds->ds_lock); 3477 error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 3478 VERIFY(error == 0 || error == ENOENT); 3479 zapobj = ds->ds_phys->ds_userrefs_obj; 3480 VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 3481 if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 3482 DS_IS_DEFER_DESTROY(ds)) { 3483 struct dsl_ds_destroyarg dsda = {0}; 3484 3485 ASSERT(ra->own); 3486 dsda.ds = ds; 3487 dsda.releasing = B_TRUE; 3488 /* We already did the destroy_check */ 3489 dsl_dataset_destroy_sync(&dsda, tag, cr, tx); 3490 } 3491 3492 spa_history_internal_log(LOG_DS_USER_RELEASE, 3493 dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu", 3494 ra->htag, (longlong_t)refs, dsobj); 3495 } 3496 3497 static int 3498 dsl_dataset_user_release_one(char *dsname, void *arg) 3499 { 3500 struct dsl_ds_holdarg *ha = arg; 3501 struct dsl_ds_releasearg *ra; 3502 dsl_dataset_t *ds; 3503 int error; 3504 void *dtag = ha->dstg; 3505 char *name; 3506 boolean_t own = B_FALSE; 3507 boolean_t might_destroy; 3508 3509 /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 3510 name = kmem_asprintf("%s@%s", dsname, ha->snapname); 3511 error = dsl_dataset_hold(name, dtag, &ds); 3512 strfree(name); 3513 if (error == ENOENT && ha->recursive) 3514 return (0); 3515 (void) strcpy(ha->failed, dsname); 3516 if (error) 3517 return (error); 3518 3519 ha->gotone = B_TRUE; 3520 3521 ASSERT(dsl_dataset_is_snapshot(ds)); 3522 3523 error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 3524 if (error) { 3525 dsl_dataset_rele(ds, dtag); 3526 return (error); 3527 } 3528 3529 if (might_destroy) { 3530 #ifdef _KERNEL 3531 error = zfs_unmount_snap(name, NULL); 3532 if (error) { 3533 dsl_dataset_rele(ds, dtag); 3534 return (error); 3535 } 3536 error = dsl_dataset_zvol_cleanup(ds, name); 3537 if (error) { 3538 dsl_dataset_rele(ds, dtag); 3539 return (error); 3540 } 3541 #endif 3542 if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 3543 dsl_dataset_rele(ds, dtag); 3544 return (EBUSY); 3545 } else { 3546 own = B_TRUE; 3547 dsl_dataset_make_exclusive(ds, dtag); 3548 } 3549 } 3550 3551 ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 3552 ra->ds = ds; 3553 ra->htag = ha->htag; 3554 ra->own = own; 3555 dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 3556 dsl_dataset_user_release_sync, ra, dtag, 0); 3557 3558 return (0); 3559 } 3560 3561 int 3562 dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 3563 boolean_t recursive) 3564 { 3565 struct dsl_ds_holdarg *ha; 3566 dsl_sync_task_t *dst; 3567 spa_t *spa; 3568 int error; 3569 3570 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 3571 3572 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 3573 3574 error = spa_open(dsname, &spa, FTAG); 3575 if (error) { 3576 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3577 return (error); 3578 } 3579 3580 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 3581 ha->htag = htag; 3582 ha->snapname = snapname; 3583 ha->recursive = recursive; 3584 if (recursive) { 3585 error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 3586 ha, DS_FIND_CHILDREN); 3587 } else { 3588 error = dsl_dataset_user_release_one(dsname, ha); 3589 } 3590 if (error == 0) 3591 error = dsl_sync_task_group_wait(ha->dstg); 3592 3593 for (dst = list_head(&ha->dstg->dstg_tasks); dst; 3594 dst = list_next(&ha->dstg->dstg_tasks, dst)) { 3595 struct dsl_ds_releasearg *ra = dst->dst_arg1; 3596 dsl_dataset_t *ds = ra->ds; 3597 3598 if (dst->dst_err) 3599 dsl_dataset_name(ds, ha->failed); 3600 3601 if (ra->own) 3602 dsl_dataset_disown(ds, ha->dstg); 3603 else 3604 dsl_dataset_rele(ds, ha->dstg); 3605 3606 kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 3607 } 3608 3609 if (error == 0 && recursive && !ha->gotone) 3610 error = ENOENT; 3611 3612 if (error) 3613 (void) strcpy(dsname, ha->failed); 3614 3615 dsl_sync_task_group_destroy(ha->dstg); 3616 kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 3617 spa_close(spa, FTAG); 3618 return (error); 3619 } 3620 3621 /* 3622 * Called at spa_load time to release a stale temporary user hold. 3623 */ 3624 int 3625 dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 3626 { 3627 dsl_dataset_t *ds; 3628 char *snap; 3629 char *name; 3630 int namelen; 3631 int error; 3632 3633 rw_enter(&dp->dp_config_rwlock, RW_READER); 3634 error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 3635 rw_exit(&dp->dp_config_rwlock); 3636 if (error) 3637 return (error); 3638 namelen = dsl_dataset_namelen(ds)+1; 3639 name = kmem_alloc(namelen, KM_SLEEP); 3640 dsl_dataset_name(ds, name); 3641 dsl_dataset_rele(ds, FTAG); 3642 3643 snap = strchr(name, '@'); 3644 *snap = '\0'; 3645 ++snap; 3646 return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 3647 } 3648 3649 int 3650 dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 3651 { 3652 dsl_dataset_t *ds; 3653 int err; 3654 3655 err = dsl_dataset_hold(dsname, FTAG, &ds); 3656 if (err) 3657 return (err); 3658 3659 VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 3660 if (ds->ds_phys->ds_userrefs_obj != 0) { 3661 zap_attribute_t *za; 3662 zap_cursor_t zc; 3663 3664 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 3665 for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 3666 ds->ds_phys->ds_userrefs_obj); 3667 zap_cursor_retrieve(&zc, za) == 0; 3668 zap_cursor_advance(&zc)) { 3669 VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 3670 za->za_first_integer)); 3671 } 3672 zap_cursor_fini(&zc); 3673 kmem_free(za, sizeof (zap_attribute_t)); 3674 } 3675 dsl_dataset_rele(ds, FTAG); 3676 return (0); 3677 } 3678 3679 /* 3680 * Note, this fuction is used as the callback for dmu_objset_find(). We 3681 * always return 0 so that we will continue to find and process 3682 * inconsistent datasets, even if we encounter an error trying to 3683 * process one of them. 3684 */ 3685 /* ARGSUSED */ 3686 int 3687 dsl_destroy_inconsistent(char *dsname, void *arg) 3688 { 3689 dsl_dataset_t *ds; 3690 3691 if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 3692 if (DS_IS_INCONSISTENT(ds)) 3693 (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 3694 else 3695 dsl_dataset_disown(ds, FTAG); 3696 } 3697 return (0); 3698 } 3699