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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/dmu_objset.h> 29 #include <sys/dsl_dataset.h> 30 #include <sys/dsl_dir.h> 31 #include <sys/dsl_prop.h> 32 #include <sys/dsl_synctask.h> 33 #include <sys/dmu_traverse.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/arc.h> 36 #include <sys/zio.h> 37 #include <sys/zap.h> 38 #include <sys/unique.h> 39 #include <sys/zfs_context.h> 40 #include <sys/zfs_ioctl.h> 41 #include <sys/spa.h> 42 #include <sys/sunddi.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_checkfunc_t dsl_dataset_rollback_check; 49 static dsl_syncfunc_t dsl_dataset_rollback_sync; 50 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 51 52 #define DS_REF_MAX (1ULL << 62) 53 54 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 55 56 #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 57 58 static void dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag); 59 60 /* 61 * Figure out how much of this delta should be propogated to the dsl_dir 62 * layer. If there's a refreservation, that space has already been 63 * partially accounted for in our ancestors. 64 */ 65 static int64_t 66 parent_delta(dsl_dataset_t *ds, int64_t delta) 67 { 68 uint64_t old_bytes, new_bytes; 69 70 if (ds->ds_reserved == 0) 71 return (delta); 72 73 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 74 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 75 76 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 77 return (new_bytes - old_bytes); 78 } 79 80 void 81 dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 82 { 83 int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 84 int compressed = BP_GET_PSIZE(bp); 85 int uncompressed = BP_GET_UCSIZE(bp); 86 int64_t delta; 87 88 dprintf_bp(bp, "born, ds=%p\n", ds); 89 90 ASSERT(dmu_tx_is_syncing(tx)); 91 /* It could have been compressed away to nothing */ 92 if (BP_IS_HOLE(bp)) 93 return; 94 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 95 ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 96 if (ds == NULL) { 97 /* 98 * Account for the meta-objset space in its placeholder 99 * dsl_dir. 100 */ 101 ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 102 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 103 used, compressed, uncompressed, tx); 104 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 105 return; 106 } 107 dmu_buf_will_dirty(ds->ds_dbuf, tx); 108 mutex_enter(&ds->ds_lock); 109 delta = parent_delta(ds, used); 110 ds->ds_phys->ds_used_bytes += used; 111 ds->ds_phys->ds_compressed_bytes += compressed; 112 ds->ds_phys->ds_uncompressed_bytes += uncompressed; 113 ds->ds_phys->ds_unique_bytes += used; 114 mutex_exit(&ds->ds_lock); 115 dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx); 116 } 117 118 int 119 dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 120 dmu_tx_t *tx) 121 { 122 int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 123 int compressed = BP_GET_PSIZE(bp); 124 int uncompressed = BP_GET_UCSIZE(bp); 125 126 ASSERT(dmu_tx_is_syncing(tx)); 127 /* No block pointer => nothing to free */ 128 if (BP_IS_HOLE(bp)) 129 return (0); 130 131 ASSERT(used > 0); 132 if (ds == NULL) { 133 int err; 134 /* 135 * Account for the meta-objset space in its placeholder 136 * dataset. 137 */ 138 err = arc_free(pio, tx->tx_pool->dp_spa, 139 tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 140 ASSERT(err == 0); 141 142 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 143 -used, -compressed, -uncompressed, tx); 144 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 145 return (used); 146 } 147 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 148 149 dmu_buf_will_dirty(ds->ds_dbuf, tx); 150 151 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 152 int err; 153 int64_t delta; 154 155 dprintf_bp(bp, "freeing: %s", ""); 156 err = arc_free(pio, tx->tx_pool->dp_spa, 157 tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 158 ASSERT(err == 0); 159 160 mutex_enter(&ds->ds_lock); 161 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 162 !DS_UNIQUE_IS_ACCURATE(ds)); 163 delta = parent_delta(ds, -used); 164 ds->ds_phys->ds_unique_bytes -= used; 165 mutex_exit(&ds->ds_lock); 166 dsl_dir_diduse_space(ds->ds_dir, 167 delta, -compressed, -uncompressed, tx); 168 } else { 169 dprintf_bp(bp, "putting on dead list: %s", ""); 170 VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 171 ASSERT3U(ds->ds_prev->ds_object, ==, 172 ds->ds_phys->ds_prev_snap_obj); 173 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 174 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 175 if (ds->ds_prev->ds_phys->ds_next_snap_obj == 176 ds->ds_object && bp->blk_birth > 177 ds->ds_prev->ds_phys->ds_prev_snap_txg) { 178 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 179 mutex_enter(&ds->ds_prev->ds_lock); 180 ds->ds_prev->ds_phys->ds_unique_bytes += used; 181 mutex_exit(&ds->ds_prev->ds_lock); 182 } 183 } 184 mutex_enter(&ds->ds_lock); 185 ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 186 ds->ds_phys->ds_used_bytes -= used; 187 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 188 ds->ds_phys->ds_compressed_bytes -= compressed; 189 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 190 ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 191 mutex_exit(&ds->ds_lock); 192 193 return (used); 194 } 195 196 uint64_t 197 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 198 { 199 uint64_t trysnap = 0; 200 201 if (ds == NULL) 202 return (0); 203 /* 204 * The snapshot creation could fail, but that would cause an 205 * incorrect FALSE return, which would only result in an 206 * overestimation of the amount of space that an operation would 207 * consume, which is OK. 208 * 209 * There's also a small window where we could miss a pending 210 * snapshot, because we could set the sync task in the quiescing 211 * phase. So this should only be used as a guess. 212 */ 213 if (ds->ds_trysnap_txg > 214 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 215 trysnap = ds->ds_trysnap_txg; 216 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 217 } 218 219 int 220 dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 221 { 222 return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 223 } 224 225 /* ARGSUSED */ 226 static void 227 dsl_dataset_evict(dmu_buf_t *db, void *dsv) 228 { 229 dsl_dataset_t *ds = dsv; 230 231 ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 232 233 dprintf_ds(ds, "evicting %s\n", ""); 234 235 unique_remove(ds->ds_fsid_guid); 236 237 if (ds->ds_user_ptr != NULL) 238 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 239 240 if (ds->ds_prev) { 241 dsl_dataset_drop_ref(ds->ds_prev, ds); 242 ds->ds_prev = NULL; 243 } 244 245 bplist_close(&ds->ds_deadlist); 246 if (ds->ds_dir) 247 dsl_dir_close(ds->ds_dir, ds); 248 249 ASSERT(!list_link_active(&ds->ds_synced_link)); 250 251 mutex_destroy(&ds->ds_lock); 252 mutex_destroy(&ds->ds_opening_lock); 253 mutex_destroy(&ds->ds_deadlist.bpl_lock); 254 rw_destroy(&ds->ds_rwlock); 255 cv_destroy(&ds->ds_exclusive_cv); 256 257 kmem_free(ds, sizeof (dsl_dataset_t)); 258 } 259 260 static int 261 dsl_dataset_get_snapname(dsl_dataset_t *ds) 262 { 263 dsl_dataset_phys_t *headphys; 264 int err; 265 dmu_buf_t *headdbuf; 266 dsl_pool_t *dp = ds->ds_dir->dd_pool; 267 objset_t *mos = dp->dp_meta_objset; 268 269 if (ds->ds_snapname[0]) 270 return (0); 271 if (ds->ds_phys->ds_next_snap_obj == 0) 272 return (0); 273 274 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 275 FTAG, &headdbuf); 276 if (err) 277 return (err); 278 headphys = headdbuf->db_data; 279 err = zap_value_search(dp->dp_meta_objset, 280 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 281 dmu_buf_rele(headdbuf, FTAG); 282 return (err); 283 } 284 285 static int 286 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 287 { 288 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 289 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 290 matchtype_t mt; 291 int err; 292 293 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 294 mt = MT_FIRST; 295 else 296 mt = MT_EXACT; 297 298 err = zap_lookup_norm(mos, snapobj, name, 8, 1, 299 value, mt, NULL, 0, NULL); 300 if (err == ENOTSUP && mt == MT_FIRST) 301 err = zap_lookup(mos, snapobj, name, 8, 1, value); 302 return (err); 303 } 304 305 static int 306 dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 307 { 308 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 309 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 310 matchtype_t mt; 311 int err; 312 313 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 314 mt = MT_FIRST; 315 else 316 mt = MT_EXACT; 317 318 err = zap_remove_norm(mos, snapobj, name, mt, tx); 319 if (err == ENOTSUP && mt == MT_FIRST) 320 err = zap_remove(mos, snapobj, name, tx); 321 return (err); 322 } 323 324 static int 325 dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 326 dsl_dataset_t **dsp) 327 { 328 objset_t *mos = dp->dp_meta_objset; 329 dmu_buf_t *dbuf; 330 dsl_dataset_t *ds; 331 int err; 332 333 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 334 dsl_pool_sync_context(dp)); 335 336 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 337 if (err) 338 return (err); 339 ds = dmu_buf_get_user(dbuf); 340 if (ds == NULL) { 341 dsl_dataset_t *winner; 342 343 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 344 ds->ds_dbuf = dbuf; 345 ds->ds_object = dsobj; 346 ds->ds_phys = dbuf->db_data; 347 348 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 349 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 350 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 351 NULL); 352 rw_init(&ds->ds_rwlock, 0, 0, 0); 353 cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 354 355 err = bplist_open(&ds->ds_deadlist, 356 mos, ds->ds_phys->ds_deadlist_obj); 357 if (err == 0) { 358 err = dsl_dir_open_obj(dp, 359 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 360 } 361 if (err) { 362 /* 363 * we don't really need to close the blist if we 364 * just opened it. 365 */ 366 mutex_destroy(&ds->ds_lock); 367 mutex_destroy(&ds->ds_opening_lock); 368 mutex_destroy(&ds->ds_deadlist.bpl_lock); 369 rw_destroy(&ds->ds_rwlock); 370 cv_destroy(&ds->ds_exclusive_cv); 371 kmem_free(ds, sizeof (dsl_dataset_t)); 372 dmu_buf_rele(dbuf, tag); 373 return (err); 374 } 375 376 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) { 377 ds->ds_snapname[0] = '\0'; 378 if (ds->ds_phys->ds_prev_snap_obj) { 379 err = dsl_dataset_get_ref(dp, 380 ds->ds_phys->ds_prev_snap_obj, 381 ds, &ds->ds_prev); 382 } 383 } else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) { 384 err = dsl_dataset_get_snapname(ds); 385 } 386 387 if (!dsl_dataset_is_snapshot(ds)) { 388 /* 389 * In sync context, we're called with either no lock 390 * or with the write lock. If we're not syncing, 391 * we're always called with the read lock held. 392 */ 393 boolean_t need_lock = 394 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 395 dsl_pool_sync_context(dp); 396 397 if (need_lock) 398 rw_enter(&dp->dp_config_rwlock, RW_READER); 399 400 err = dsl_prop_get_ds_locked(ds->ds_dir, 401 "refreservation", sizeof (uint64_t), 1, 402 &ds->ds_reserved, NULL); 403 if (err == 0) { 404 err = dsl_prop_get_ds_locked(ds->ds_dir, 405 "refquota", sizeof (uint64_t), 1, 406 &ds->ds_quota, NULL); 407 } 408 409 if (need_lock) 410 rw_exit(&dp->dp_config_rwlock); 411 } else { 412 ds->ds_reserved = ds->ds_quota = 0; 413 } 414 415 if (err == 0) { 416 winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 417 dsl_dataset_evict); 418 } 419 if (err || winner) { 420 bplist_close(&ds->ds_deadlist); 421 if (ds->ds_prev) 422 dsl_dataset_drop_ref(ds->ds_prev, ds); 423 dsl_dir_close(ds->ds_dir, ds); 424 mutex_destroy(&ds->ds_lock); 425 mutex_destroy(&ds->ds_opening_lock); 426 mutex_destroy(&ds->ds_deadlist.bpl_lock); 427 rw_destroy(&ds->ds_rwlock); 428 cv_destroy(&ds->ds_exclusive_cv); 429 kmem_free(ds, sizeof (dsl_dataset_t)); 430 if (err) { 431 dmu_buf_rele(dbuf, tag); 432 return (err); 433 } 434 ds = winner; 435 } else { 436 ds->ds_fsid_guid = 437 unique_insert(ds->ds_phys->ds_fsid_guid); 438 } 439 } 440 ASSERT3P(ds->ds_dbuf, ==, dbuf); 441 ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 442 mutex_enter(&ds->ds_lock); 443 if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 444 mutex_exit(&ds->ds_lock); 445 dmu_buf_rele(ds->ds_dbuf, tag); 446 return (ENOENT); 447 } 448 mutex_exit(&ds->ds_lock); 449 *dsp = ds; 450 return (0); 451 } 452 453 static int 454 dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 455 { 456 dsl_pool_t *dp = ds->ds_dir->dd_pool; 457 458 /* 459 * In syncing context we don't want the rwlock lock: there 460 * may be an existing writer waiting for sync phase to 461 * finish. We don't need to worry about such writers, since 462 * sync phase is single-threaded, so the writer can't be 463 * doing anything while we are active. 464 */ 465 if (dsl_pool_sync_context(dp)) { 466 ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 467 return (0); 468 } 469 470 /* 471 * Normal users will hold the ds_rwlock as a READER until they 472 * are finished (i.e., call dsl_dataset_rele()). "Owners" will 473 * drop their READER lock after they set the ds_owner field. 474 * 475 * If the dataset is being destroyed, the destroy thread will 476 * obtain a WRITER lock for exclusive access after it's done its 477 * open-context work and then change the ds_owner to 478 * dsl_reaper once destruction is assured. So threads 479 * may block here temporarily, until the "destructability" of 480 * the dataset is determined. 481 */ 482 ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 483 mutex_enter(&ds->ds_lock); 484 while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 485 rw_exit(&dp->dp_config_rwlock); 486 cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 487 if (DSL_DATASET_IS_DESTROYED(ds)) { 488 mutex_exit(&ds->ds_lock); 489 dsl_dataset_drop_ref(ds, tag); 490 rw_enter(&dp->dp_config_rwlock, RW_READER); 491 return (ENOENT); 492 } 493 rw_enter(&dp->dp_config_rwlock, RW_READER); 494 } 495 mutex_exit(&ds->ds_lock); 496 return (0); 497 } 498 499 int 500 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 501 dsl_dataset_t **dsp) 502 { 503 int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 504 505 if (err) 506 return (err); 507 return (dsl_dataset_hold_ref(*dsp, tag)); 508 } 509 510 int 511 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner, 512 dsl_dataset_t **dsp) 513 { 514 int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp); 515 516 ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER); 517 518 if (err) 519 return (err); 520 if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 521 dsl_dataset_rele(*dsp, owner); 522 return (EBUSY); 523 } 524 return (0); 525 } 526 527 int 528 dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 529 { 530 dsl_dir_t *dd; 531 dsl_pool_t *dp; 532 const char *snapname; 533 uint64_t obj; 534 int err = 0; 535 536 err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 537 if (err) 538 return (err); 539 540 dp = dd->dd_pool; 541 obj = dd->dd_phys->dd_head_dataset_obj; 542 rw_enter(&dp->dp_config_rwlock, RW_READER); 543 if (obj) 544 err = dsl_dataset_get_ref(dp, obj, tag, dsp); 545 else 546 err = ENOENT; 547 if (err) 548 goto out; 549 550 err = dsl_dataset_hold_ref(*dsp, tag); 551 552 /* we may be looking for a snapshot */ 553 if (err == 0 && snapname != NULL) { 554 dsl_dataset_t *ds = NULL; 555 556 if (*snapname++ != '@') { 557 dsl_dataset_rele(*dsp, tag); 558 err = ENOENT; 559 goto out; 560 } 561 562 dprintf("looking for snapshot '%s'\n", snapname); 563 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 564 if (err == 0) 565 err = dsl_dataset_get_ref(dp, obj, tag, &ds); 566 dsl_dataset_rele(*dsp, tag); 567 568 ASSERT3U((err == 0), ==, (ds != NULL)); 569 570 if (ds) { 571 mutex_enter(&ds->ds_lock); 572 if (ds->ds_snapname[0] == 0) 573 (void) strlcpy(ds->ds_snapname, snapname, 574 sizeof (ds->ds_snapname)); 575 mutex_exit(&ds->ds_lock); 576 err = dsl_dataset_hold_ref(ds, tag); 577 *dsp = err ? NULL : ds; 578 } 579 } 580 out: 581 rw_exit(&dp->dp_config_rwlock); 582 dsl_dir_close(dd, FTAG); 583 return (err); 584 } 585 586 int 587 dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp) 588 { 589 int err = dsl_dataset_hold(name, owner, dsp); 590 if (err) 591 return (err); 592 if ((*dsp)->ds_phys->ds_num_children > 0 && 593 !DS_MODE_IS_READONLY(flags)) { 594 dsl_dataset_rele(*dsp, owner); 595 return (EROFS); 596 } 597 if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 598 dsl_dataset_rele(*dsp, owner); 599 return (EBUSY); 600 } 601 return (0); 602 } 603 604 void 605 dsl_dataset_name(dsl_dataset_t *ds, char *name) 606 { 607 if (ds == NULL) { 608 (void) strcpy(name, "mos"); 609 } else { 610 dsl_dir_name(ds->ds_dir, name); 611 VERIFY(0 == dsl_dataset_get_snapname(ds)); 612 if (ds->ds_snapname[0]) { 613 (void) strcat(name, "@"); 614 /* 615 * We use a "recursive" mutex so that we 616 * can call dprintf_ds() with ds_lock held. 617 */ 618 if (!MUTEX_HELD(&ds->ds_lock)) { 619 mutex_enter(&ds->ds_lock); 620 (void) strcat(name, ds->ds_snapname); 621 mutex_exit(&ds->ds_lock); 622 } else { 623 (void) strcat(name, ds->ds_snapname); 624 } 625 } 626 } 627 } 628 629 static int 630 dsl_dataset_namelen(dsl_dataset_t *ds) 631 { 632 int result; 633 634 if (ds == NULL) { 635 result = 3; /* "mos" */ 636 } else { 637 result = dsl_dir_namelen(ds->ds_dir); 638 VERIFY(0 == dsl_dataset_get_snapname(ds)); 639 if (ds->ds_snapname[0]) { 640 ++result; /* adding one for the @-sign */ 641 if (!MUTEX_HELD(&ds->ds_lock)) { 642 mutex_enter(&ds->ds_lock); 643 result += strlen(ds->ds_snapname); 644 mutex_exit(&ds->ds_lock); 645 } else { 646 result += strlen(ds->ds_snapname); 647 } 648 } 649 } 650 651 return (result); 652 } 653 654 static void 655 dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 656 { 657 dmu_buf_rele(ds->ds_dbuf, tag); 658 } 659 660 void 661 dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 662 { 663 ASSERT(ds->ds_owner != tag); 664 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 665 rw_exit(&ds->ds_rwlock); 666 } 667 dsl_dataset_drop_ref(ds, tag); 668 } 669 670 void 671 dsl_dataset_disown(dsl_dataset_t *ds, void *owner) 672 { 673 ASSERT((ds->ds_owner == owner && ds->ds_dbuf) || 674 (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 675 676 mutex_enter(&ds->ds_lock); 677 ds->ds_owner = NULL; 678 if (RW_WRITE_HELD(&ds->ds_rwlock)) { 679 rw_exit(&ds->ds_rwlock); 680 cv_broadcast(&ds->ds_exclusive_cv); 681 } 682 mutex_exit(&ds->ds_lock); 683 if (ds->ds_dbuf) 684 dsl_dataset_drop_ref(ds, owner); 685 else 686 dsl_dataset_evict(ds->ds_dbuf, ds); 687 } 688 689 boolean_t 690 dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner) 691 { 692 boolean_t gotit = FALSE; 693 694 mutex_enter(&ds->ds_lock); 695 if (ds->ds_owner == NULL && 696 (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 697 ds->ds_owner = owner; 698 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 699 rw_exit(&ds->ds_rwlock); 700 gotit = TRUE; 701 } 702 mutex_exit(&ds->ds_lock); 703 return (gotit); 704 } 705 706 void 707 dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 708 { 709 ASSERT3P(owner, ==, ds->ds_owner); 710 if (!RW_WRITE_HELD(&ds->ds_rwlock)) 711 rw_enter(&ds->ds_rwlock, RW_WRITER); 712 } 713 714 void 715 dsl_dataset_create_root(dsl_pool_t *dp, uint64_t *ddobjp, dmu_tx_t *tx) 716 { 717 objset_t *mos = dp->dp_meta_objset; 718 dmu_buf_t *dbuf; 719 dsl_dataset_phys_t *dsphys; 720 dsl_dataset_t *ds; 721 uint64_t dsobj; 722 dsl_dir_t *dd; 723 724 dsl_dir_create_root(mos, ddobjp, tx); 725 VERIFY(0 == dsl_dir_open_obj(dp, *ddobjp, NULL, FTAG, &dd)); 726 727 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 728 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 729 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 730 dmu_buf_will_dirty(dbuf, tx); 731 dsphys = dbuf->db_data; 732 dsphys->ds_dir_obj = dd->dd_object; 733 dsphys->ds_fsid_guid = unique_create(); 734 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 735 sizeof (dsphys->ds_guid)); 736 dsphys->ds_snapnames_zapobj = 737 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 738 DMU_OT_NONE, 0, tx); 739 dsphys->ds_creation_time = gethrestime_sec(); 740 dsphys->ds_creation_txg = tx->tx_txg; 741 dsphys->ds_deadlist_obj = 742 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 743 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 744 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 745 dmu_buf_rele(dbuf, FTAG); 746 747 dmu_buf_will_dirty(dd->dd_dbuf, tx); 748 dd->dd_phys->dd_head_dataset_obj = dsobj; 749 dsl_dir_close(dd, FTAG); 750 751 VERIFY(0 == dsl_dataset_get_ref(dp, dsobj, FTAG, &ds)); 752 (void) dmu_objset_create_impl(dp->dp_spa, ds, 753 &ds->ds_phys->ds_bp, DMU_OST_ZFS, tx); 754 dsl_dataset_drop_ref(ds, FTAG); 755 } 756 757 uint64_t 758 dsl_dataset_create_sync_impl(dsl_dir_t *dd, dsl_dataset_t *origin, 759 uint64_t flags, dmu_tx_t *tx) 760 { 761 dsl_pool_t *dp = dd->dd_pool; 762 dmu_buf_t *dbuf; 763 dsl_dataset_phys_t *dsphys; 764 uint64_t dsobj; 765 objset_t *mos = dp->dp_meta_objset; 766 767 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 768 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 769 ASSERT(dmu_tx_is_syncing(tx)); 770 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 771 772 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 773 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 774 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 775 dmu_buf_will_dirty(dbuf, tx); 776 dsphys = dbuf->db_data; 777 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 778 dsphys->ds_dir_obj = dd->dd_object; 779 dsphys->ds_flags = flags; 780 dsphys->ds_fsid_guid = unique_create(); 781 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 782 sizeof (dsphys->ds_guid)); 783 dsphys->ds_snapnames_zapobj = 784 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 785 DMU_OT_NONE, 0, tx); 786 dsphys->ds_creation_time = gethrestime_sec(); 787 dsphys->ds_creation_txg = tx->tx_txg; 788 dsphys->ds_deadlist_obj = 789 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 790 791 if (origin) { 792 dsphys->ds_prev_snap_obj = origin->ds_object; 793 dsphys->ds_prev_snap_txg = 794 origin->ds_phys->ds_creation_txg; 795 dsphys->ds_used_bytes = 796 origin->ds_phys->ds_used_bytes; 797 dsphys->ds_compressed_bytes = 798 origin->ds_phys->ds_compressed_bytes; 799 dsphys->ds_uncompressed_bytes = 800 origin->ds_phys->ds_uncompressed_bytes; 801 dsphys->ds_bp = origin->ds_phys->ds_bp; 802 dsphys->ds_flags |= origin->ds_phys->ds_flags; 803 804 dmu_buf_will_dirty(origin->ds_dbuf, tx); 805 origin->ds_phys->ds_num_children++; 806 807 dmu_buf_will_dirty(dd->dd_dbuf, tx); 808 dd->dd_phys->dd_origin_obj = origin->ds_object; 809 } 810 811 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 812 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 813 814 dmu_buf_rele(dbuf, FTAG); 815 816 dmu_buf_will_dirty(dd->dd_dbuf, tx); 817 dd->dd_phys->dd_head_dataset_obj = dsobj; 818 819 return (dsobj); 820 } 821 822 uint64_t 823 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 824 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 825 { 826 dsl_pool_t *dp = pdd->dd_pool; 827 uint64_t dsobj, ddobj; 828 dsl_dir_t *dd; 829 830 ASSERT(lastname[0] != '@'); 831 832 ddobj = dsl_dir_create_sync(pdd, lastname, tx); 833 VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 834 835 dsobj = dsl_dataset_create_sync_impl(dd, origin, flags, tx); 836 837 dsl_deleg_set_create_perms(dd, tx, cr); 838 839 dsl_dir_close(dd, FTAG); 840 841 return (dsobj); 842 } 843 844 struct destroyarg { 845 dsl_sync_task_group_t *dstg; 846 char *snapname; 847 char *failed; 848 }; 849 850 static int 851 dsl_snapshot_destroy_one(char *name, void *arg) 852 { 853 struct destroyarg *da = arg; 854 dsl_dataset_t *ds; 855 char *cp; 856 int err; 857 858 (void) strcat(name, "@"); 859 (void) strcat(name, da->snapname); 860 err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT, 861 da->dstg, &ds); 862 cp = strchr(name, '@'); 863 *cp = '\0'; 864 if (err == 0) { 865 dsl_dataset_make_exclusive(ds, da->dstg); 866 dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 867 dsl_dataset_destroy_sync, ds, da->dstg, 0); 868 } else if (err == ENOENT) { 869 err = 0; 870 } else { 871 (void) strcpy(da->failed, name); 872 } 873 return (err); 874 } 875 876 /* 877 * Destroy 'snapname' in all descendants of 'fsname'. 878 */ 879 #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 880 int 881 dsl_snapshots_destroy(char *fsname, char *snapname) 882 { 883 int err; 884 struct destroyarg da; 885 dsl_sync_task_t *dst; 886 spa_t *spa; 887 888 err = spa_open(fsname, &spa, FTAG); 889 if (err) 890 return (err); 891 da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 892 da.snapname = snapname; 893 da.failed = fsname; 894 895 err = dmu_objset_find(fsname, 896 dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 897 898 if (err == 0) 899 err = dsl_sync_task_group_wait(da.dstg); 900 901 for (dst = list_head(&da.dstg->dstg_tasks); dst; 902 dst = list_next(&da.dstg->dstg_tasks, dst)) { 903 dsl_dataset_t *ds = dst->dst_arg1; 904 /* 905 * Return the file system name that triggered the error 906 */ 907 if (dst->dst_err) { 908 dsl_dataset_name(ds, fsname); 909 *strchr(fsname, '@') = '\0'; 910 } 911 dsl_dataset_disown(ds, da.dstg); 912 } 913 914 dsl_sync_task_group_destroy(da.dstg); 915 spa_close(spa, FTAG); 916 return (err); 917 } 918 919 /* 920 * ds must be opened as OWNER. On return (whether successful or not), 921 * ds will be closed and caller can no longer dereference it. 922 */ 923 int 924 dsl_dataset_destroy(dsl_dataset_t *ds, void *tag) 925 { 926 int err; 927 dsl_sync_task_group_t *dstg; 928 objset_t *os; 929 dsl_dir_t *dd; 930 uint64_t obj; 931 932 if (dsl_dataset_is_snapshot(ds)) { 933 /* Destroying a snapshot is simpler */ 934 dsl_dataset_make_exclusive(ds, tag); 935 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 936 dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 937 ds, tag, 0); 938 goto out; 939 } 940 941 dd = ds->ds_dir; 942 943 /* 944 * Check for errors and mark this ds as inconsistent, in 945 * case we crash while freeing the objects. 946 */ 947 err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 948 dsl_dataset_destroy_begin_sync, ds, NULL, 0); 949 if (err) 950 goto out; 951 952 err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os); 953 if (err) 954 goto out; 955 956 /* 957 * remove the objects in open context, so that we won't 958 * have too much to do in syncing context. 959 */ 960 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 961 ds->ds_phys->ds_prev_snap_txg)) { 962 /* 963 * Ignore errors, if there is not enough disk space 964 * we will deal with it in dsl_dataset_destroy_sync(). 965 */ 966 (void) dmu_free_object(os, obj); 967 } 968 969 dmu_objset_close(os); 970 if (err != ESRCH) 971 goto out; 972 973 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 974 err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 975 rw_exit(&dd->dd_pool->dp_config_rwlock); 976 977 if (err) 978 goto out; 979 980 if (ds->ds_user_ptr) { 981 /* 982 * We need to sync out all in-flight IO before we try 983 * to evict (the dataset evict func is trying to clear 984 * the cached entries for this dataset in the ARC). 985 */ 986 txg_wait_synced(dd->dd_pool, 0); 987 } 988 989 /* 990 * Blow away the dsl_dir + head dataset. 991 */ 992 dsl_dataset_make_exclusive(ds, tag); 993 if (ds->ds_user_ptr) { 994 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 995 ds->ds_user_ptr = NULL; 996 } 997 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 998 dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 999 dsl_dataset_destroy_sync, ds, tag, 0); 1000 dsl_sync_task_create(dstg, dsl_dir_destroy_check, 1001 dsl_dir_destroy_sync, dd, FTAG, 0); 1002 err = dsl_sync_task_group_wait(dstg); 1003 dsl_sync_task_group_destroy(dstg); 1004 /* if it is successful, dsl_dir_destroy_sync will close the dd */ 1005 if (err) 1006 dsl_dir_close(dd, FTAG); 1007 out: 1008 dsl_dataset_disown(ds, tag); 1009 return (err); 1010 } 1011 1012 int 1013 dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost) 1014 { 1015 ASSERT(ds->ds_owner); 1016 1017 return (dsl_sync_task_do(ds->ds_dir->dd_pool, 1018 dsl_dataset_rollback_check, dsl_dataset_rollback_sync, 1019 ds, &ost, 0)); 1020 } 1021 1022 void * 1023 dsl_dataset_set_user_ptr(dsl_dataset_t *ds, 1024 void *p, dsl_dataset_evict_func_t func) 1025 { 1026 void *old; 1027 1028 mutex_enter(&ds->ds_lock); 1029 old = ds->ds_user_ptr; 1030 if (old == NULL) { 1031 ds->ds_user_ptr = p; 1032 ds->ds_user_evict_func = func; 1033 } 1034 mutex_exit(&ds->ds_lock); 1035 return (old); 1036 } 1037 1038 void * 1039 dsl_dataset_get_user_ptr(dsl_dataset_t *ds) 1040 { 1041 return (ds->ds_user_ptr); 1042 } 1043 1044 1045 blkptr_t * 1046 dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1047 { 1048 return (&ds->ds_phys->ds_bp); 1049 } 1050 1051 void 1052 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1053 { 1054 ASSERT(dmu_tx_is_syncing(tx)); 1055 /* If it's the meta-objset, set dp_meta_rootbp */ 1056 if (ds == NULL) { 1057 tx->tx_pool->dp_meta_rootbp = *bp; 1058 } else { 1059 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1060 ds->ds_phys->ds_bp = *bp; 1061 } 1062 } 1063 1064 spa_t * 1065 dsl_dataset_get_spa(dsl_dataset_t *ds) 1066 { 1067 return (ds->ds_dir->dd_pool->dp_spa); 1068 } 1069 1070 void 1071 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1072 { 1073 dsl_pool_t *dp; 1074 1075 if (ds == NULL) /* this is the meta-objset */ 1076 return; 1077 1078 ASSERT(ds->ds_user_ptr != NULL); 1079 1080 if (ds->ds_phys->ds_next_snap_obj != 0) 1081 panic("dirtying snapshot!"); 1082 1083 dp = ds->ds_dir->dd_pool; 1084 1085 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1086 /* up the hold count until we can be written out */ 1087 dmu_buf_add_ref(ds->ds_dbuf, ds); 1088 } 1089 } 1090 1091 /* 1092 * The unique space in the head dataset can be calculated by subtracting 1093 * the space used in the most recent snapshot, that is still being used 1094 * in this file system, from the space currently in use. To figure out 1095 * the space in the most recent snapshot still in use, we need to take 1096 * the total space used in the snapshot and subtract out the space that 1097 * has been freed up since the snapshot was taken. 1098 */ 1099 static void 1100 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 1101 { 1102 uint64_t mrs_used; 1103 uint64_t dlused, dlcomp, dluncomp; 1104 1105 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 1106 1107 if (ds->ds_phys->ds_prev_snap_obj != 0) 1108 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 1109 else 1110 mrs_used = 0; 1111 1112 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 1113 &dluncomp)); 1114 1115 ASSERT3U(dlused, <=, mrs_used); 1116 ds->ds_phys->ds_unique_bytes = 1117 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 1118 1119 if (!DS_UNIQUE_IS_ACCURATE(ds) && 1120 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 1121 SPA_VERSION_UNIQUE_ACCURATE) 1122 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1123 } 1124 1125 static uint64_t 1126 dsl_dataset_unique(dsl_dataset_t *ds) 1127 { 1128 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 1129 dsl_dataset_recalc_head_uniq(ds); 1130 1131 return (ds->ds_phys->ds_unique_bytes); 1132 } 1133 1134 struct killarg { 1135 int64_t *usedp; 1136 int64_t *compressedp; 1137 int64_t *uncompressedp; 1138 zio_t *zio; 1139 dmu_tx_t *tx; 1140 }; 1141 1142 static int 1143 kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1144 { 1145 struct killarg *ka = arg; 1146 blkptr_t *bp = &bc->bc_blkptr; 1147 1148 ASSERT3U(bc->bc_errno, ==, 0); 1149 1150 /* 1151 * Since this callback is not called concurrently, no lock is 1152 * needed on the accounting values. 1153 */ 1154 *ka->usedp += bp_get_dasize(spa, bp); 1155 *ka->compressedp += BP_GET_PSIZE(bp); 1156 *ka->uncompressedp += BP_GET_UCSIZE(bp); 1157 /* XXX check for EIO? */ 1158 (void) arc_free(ka->zio, spa, ka->tx->tx_txg, bp, NULL, NULL, 1159 ARC_NOWAIT); 1160 return (0); 1161 } 1162 1163 /* ARGSUSED */ 1164 static int 1165 dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx) 1166 { 1167 dsl_dataset_t *ds = arg1; 1168 dmu_objset_type_t *ost = arg2; 1169 1170 /* 1171 * We can only roll back to emptyness if it is a ZPL objset. 1172 */ 1173 if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0) 1174 return (EINVAL); 1175 1176 /* 1177 * This must not be a snapshot. 1178 */ 1179 if (ds->ds_phys->ds_next_snap_obj != 0) 1180 return (EINVAL); 1181 1182 /* 1183 * If we made changes this txg, traverse_dsl_dataset won't find 1184 * them. Try again. 1185 */ 1186 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1187 return (EAGAIN); 1188 1189 return (0); 1190 } 1191 1192 /* ARGSUSED */ 1193 static void 1194 dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1195 { 1196 dsl_dataset_t *ds = arg1; 1197 dmu_objset_type_t *ost = arg2; 1198 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1199 1200 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1201 1202 /* 1203 * Before the roll back destroy the zil. 1204 */ 1205 if (ds->ds_user_ptr != NULL) { 1206 zil_rollback_destroy( 1207 ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx); 1208 1209 /* 1210 * We need to make sure that the objset_impl_t is reopened after 1211 * we do the rollback, otherwise it will have the wrong 1212 * objset_phys_t. Normally this would happen when this 1213 * dataset-open is closed, thus causing the 1214 * dataset to be immediately evicted. But when doing "zfs recv 1215 * -F", we reopen the objset before that, so that there is no 1216 * window where the dataset is closed and inconsistent. 1217 */ 1218 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 1219 ds->ds_user_ptr = NULL; 1220 } 1221 1222 /* Zero out the deadlist. */ 1223 bplist_close(&ds->ds_deadlist); 1224 bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1225 ds->ds_phys->ds_deadlist_obj = 1226 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 1227 VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 1228 ds->ds_phys->ds_deadlist_obj)); 1229 1230 { 1231 /* Free blkptrs that we gave birth to */ 1232 zio_t *zio; 1233 int64_t used = 0, compressed = 0, uncompressed = 0; 1234 struct killarg ka; 1235 int64_t delta; 1236 1237 zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, 1238 ZIO_FLAG_MUSTSUCCEED); 1239 ka.usedp = &used; 1240 ka.compressedp = &compressed; 1241 ka.uncompressedp = &uncompressed; 1242 ka.zio = zio; 1243 ka.tx = tx; 1244 (void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1245 ADVANCE_POST, kill_blkptr, &ka); 1246 (void) zio_wait(zio); 1247 1248 /* only deduct space beyond any refreservation */ 1249 delta = parent_delta(ds, -used); 1250 dsl_dir_diduse_space(ds->ds_dir, 1251 delta, -compressed, -uncompressed, tx); 1252 } 1253 1254 if (ds->ds_prev) { 1255 /* Change our contents to that of the prev snapshot */ 1256 ASSERT3U(ds->ds_prev->ds_object, ==, 1257 ds->ds_phys->ds_prev_snap_obj); 1258 ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp; 1259 ds->ds_phys->ds_used_bytes = 1260 ds->ds_prev->ds_phys->ds_used_bytes; 1261 ds->ds_phys->ds_compressed_bytes = 1262 ds->ds_prev->ds_phys->ds_compressed_bytes; 1263 ds->ds_phys->ds_uncompressed_bytes = 1264 ds->ds_prev->ds_phys->ds_uncompressed_bytes; 1265 ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags; 1266 ds->ds_phys->ds_unique_bytes = 0; 1267 1268 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 1269 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1270 ds->ds_prev->ds_phys->ds_unique_bytes = 0; 1271 } 1272 } else { 1273 /* Zero out our contents, recreate objset */ 1274 bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t)); 1275 ds->ds_phys->ds_used_bytes = 0; 1276 ds->ds_phys->ds_compressed_bytes = 0; 1277 ds->ds_phys->ds_uncompressed_bytes = 0; 1278 ds->ds_phys->ds_flags = 0; 1279 ds->ds_phys->ds_unique_bytes = 0; 1280 (void) dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds, 1281 &ds->ds_phys->ds_bp, *ost, tx); 1282 } 1283 1284 spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa, 1285 tx, cr, "dataset = %llu", ds->ds_object); 1286 } 1287 1288 /* ARGSUSED */ 1289 static int 1290 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 1291 { 1292 dsl_dataset_t *ds = arg1; 1293 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1294 uint64_t count; 1295 int err; 1296 1297 /* 1298 * Can't delete a head dataset if there are snapshots of it. 1299 * (Except if the only snapshots are from the branch we cloned 1300 * from.) 1301 */ 1302 if (ds->ds_prev != NULL && 1303 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1304 return (EINVAL); 1305 1306 /* 1307 * This is really a dsl_dir thing, but check it here so that 1308 * we'll be less likely to leave this dataset inconsistent & 1309 * nearly destroyed. 1310 */ 1311 err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 1312 if (err) 1313 return (err); 1314 if (count != 0) 1315 return (EEXIST); 1316 1317 return (0); 1318 } 1319 1320 /* ARGSUSED */ 1321 static void 1322 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1323 { 1324 dsl_dataset_t *ds = arg1; 1325 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1326 1327 /* Mark it as inconsistent on-disk, in case we crash */ 1328 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1329 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 1330 1331 spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 1332 cr, "dataset = %llu", ds->ds_object); 1333 } 1334 1335 /* ARGSUSED */ 1336 int 1337 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 1338 { 1339 dsl_dataset_t *ds = arg1; 1340 1341 /* we have an owner hold, so noone else can destroy us */ 1342 ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 1343 1344 /* Can't delete a branch point. */ 1345 if (ds->ds_phys->ds_num_children > 1) 1346 return (EEXIST); 1347 1348 /* 1349 * Can't delete a head dataset if there are snapshots of it. 1350 * (Except if the only snapshots are from the branch we cloned 1351 * from.) 1352 */ 1353 if (ds->ds_prev != NULL && 1354 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1355 return (EINVAL); 1356 1357 /* 1358 * If we made changes this txg, traverse_dsl_dataset won't find 1359 * them. Try again. 1360 */ 1361 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1362 return (EAGAIN); 1363 1364 /* XXX we should do some i/o error checking... */ 1365 return (0); 1366 } 1367 1368 struct refsarg { 1369 kmutex_t lock; 1370 boolean_t gone; 1371 kcondvar_t cv; 1372 }; 1373 1374 /* ARGSUSED */ 1375 static void 1376 dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 1377 { 1378 struct refsarg *arg = argv; 1379 1380 mutex_enter(&arg->lock); 1381 arg->gone = TRUE; 1382 cv_signal(&arg->cv); 1383 mutex_exit(&arg->lock); 1384 } 1385 1386 static void 1387 dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 1388 { 1389 struct refsarg arg; 1390 1391 mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 1392 cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 1393 arg.gone = FALSE; 1394 (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 1395 dsl_dataset_refs_gone); 1396 dmu_buf_rele(ds->ds_dbuf, tag); 1397 mutex_enter(&arg.lock); 1398 while (!arg.gone) 1399 cv_wait(&arg.cv, &arg.lock); 1400 ASSERT(arg.gone); 1401 mutex_exit(&arg.lock); 1402 ds->ds_dbuf = NULL; 1403 ds->ds_phys = NULL; 1404 mutex_destroy(&arg.lock); 1405 cv_destroy(&arg.cv); 1406 } 1407 1408 void 1409 dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 1410 { 1411 dsl_dataset_t *ds = arg1; 1412 int64_t used = 0, compressed = 0, uncompressed = 0; 1413 zio_t *zio; 1414 int err; 1415 int after_branch_point = FALSE; 1416 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1417 objset_t *mos = dp->dp_meta_objset; 1418 dsl_dataset_t *ds_prev = NULL; 1419 uint64_t obj; 1420 1421 ASSERT(ds->ds_owner); 1422 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1); 1423 ASSERT(ds->ds_prev == NULL || 1424 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 1425 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 1426 1427 /* signal any waiters that this dataset is going away */ 1428 mutex_enter(&ds->ds_lock); 1429 ds->ds_owner = dsl_reaper; 1430 cv_broadcast(&ds->ds_exclusive_cv); 1431 mutex_exit(&ds->ds_lock); 1432 1433 /* Remove our reservation */ 1434 if (ds->ds_reserved != 0) { 1435 uint64_t val = 0; 1436 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 1437 ASSERT3U(ds->ds_reserved, ==, 0); 1438 } 1439 1440 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1441 1442 obj = ds->ds_object; 1443 1444 if (ds->ds_phys->ds_prev_snap_obj != 0) { 1445 if (ds->ds_prev) { 1446 ds_prev = ds->ds_prev; 1447 } else { 1448 VERIFY(0 == dsl_dataset_hold_obj(dp, 1449 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1450 } 1451 after_branch_point = 1452 (ds_prev->ds_phys->ds_next_snap_obj != obj); 1453 1454 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1455 if (after_branch_point && 1456 ds->ds_phys->ds_next_snap_obj == 0) { 1457 /* This clone is toast. */ 1458 ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1459 ds_prev->ds_phys->ds_num_children--; 1460 } else if (!after_branch_point) { 1461 ds_prev->ds_phys->ds_next_snap_obj = 1462 ds->ds_phys->ds_next_snap_obj; 1463 } 1464 } 1465 1466 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1467 1468 if (ds->ds_phys->ds_next_snap_obj != 0) { 1469 blkptr_t bp; 1470 dsl_dataset_t *ds_next; 1471 uint64_t itor = 0; 1472 uint64_t old_unique; 1473 1474 spa_scrub_restart(dp->dp_spa, tx->tx_txg); 1475 1476 VERIFY(0 == dsl_dataset_hold_obj(dp, 1477 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1478 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1479 1480 old_unique = dsl_dataset_unique(ds_next); 1481 1482 dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1483 ds_next->ds_phys->ds_prev_snap_obj = 1484 ds->ds_phys->ds_prev_snap_obj; 1485 ds_next->ds_phys->ds_prev_snap_txg = 1486 ds->ds_phys->ds_prev_snap_txg; 1487 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1488 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1489 1490 /* 1491 * Transfer to our deadlist (which will become next's 1492 * new deadlist) any entries from next's current 1493 * deadlist which were born before prev, and free the 1494 * other entries. 1495 * 1496 * XXX we're doing this long task with the config lock held 1497 */ 1498 while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1499 if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 1500 VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 1501 &bp, tx)); 1502 if (ds_prev && !after_branch_point && 1503 bp.blk_birth > 1504 ds_prev->ds_phys->ds_prev_snap_txg) { 1505 ds_prev->ds_phys->ds_unique_bytes += 1506 bp_get_dasize(dp->dp_spa, &bp); 1507 } 1508 } else { 1509 used += bp_get_dasize(dp->dp_spa, &bp); 1510 compressed += BP_GET_PSIZE(&bp); 1511 uncompressed += BP_GET_UCSIZE(&bp); 1512 /* XXX check return value? */ 1513 (void) arc_free(zio, dp->dp_spa, tx->tx_txg, 1514 &bp, NULL, NULL, ARC_NOWAIT); 1515 } 1516 } 1517 1518 /* free next's deadlist */ 1519 bplist_close(&ds_next->ds_deadlist); 1520 bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1521 1522 /* set next's deadlist to our deadlist */ 1523 bplist_close(&ds->ds_deadlist); 1524 ds_next->ds_phys->ds_deadlist_obj = 1525 ds->ds_phys->ds_deadlist_obj; 1526 VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 1527 ds_next->ds_phys->ds_deadlist_obj)); 1528 ds->ds_phys->ds_deadlist_obj = 0; 1529 1530 if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1531 /* 1532 * Update next's unique to include blocks which 1533 * were previously shared by only this snapshot 1534 * and it. Those blocks will be born after the 1535 * prev snap and before this snap, and will have 1536 * died after the next snap and before the one 1537 * after that (ie. be on the snap after next's 1538 * deadlist). 1539 * 1540 * XXX we're doing this long task with the 1541 * config lock held 1542 */ 1543 dsl_dataset_t *ds_after_next; 1544 1545 VERIFY(0 == dsl_dataset_hold_obj(dp, 1546 ds_next->ds_phys->ds_next_snap_obj, 1547 FTAG, &ds_after_next)); 1548 itor = 0; 1549 while (bplist_iterate(&ds_after_next->ds_deadlist, 1550 &itor, &bp) == 0) { 1551 if (bp.blk_birth > 1552 ds->ds_phys->ds_prev_snap_txg && 1553 bp.blk_birth <= 1554 ds->ds_phys->ds_creation_txg) { 1555 ds_next->ds_phys->ds_unique_bytes += 1556 bp_get_dasize(dp->dp_spa, &bp); 1557 } 1558 } 1559 1560 dsl_dataset_rele(ds_after_next, FTAG); 1561 ASSERT3P(ds_next->ds_prev, ==, NULL); 1562 } else { 1563 ASSERT3P(ds_next->ds_prev, ==, ds); 1564 dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 1565 ds_next->ds_prev = NULL; 1566 if (ds_prev) { 1567 VERIFY(0 == dsl_dataset_get_ref(dp, 1568 ds->ds_phys->ds_prev_snap_obj, 1569 ds_next, &ds_next->ds_prev)); 1570 } 1571 1572 dsl_dataset_recalc_head_uniq(ds_next); 1573 1574 /* 1575 * Reduce the amount of our unconsmed refreservation 1576 * being charged to our parent by the amount of 1577 * new unique data we have gained. 1578 */ 1579 if (old_unique < ds_next->ds_reserved) { 1580 int64_t mrsdelta; 1581 uint64_t new_unique = 1582 ds_next->ds_phys->ds_unique_bytes; 1583 1584 ASSERT(old_unique <= new_unique); 1585 mrsdelta = MIN(new_unique - old_unique, 1586 ds_next->ds_reserved - old_unique); 1587 dsl_dir_diduse_space(ds->ds_dir, -mrsdelta, 1588 0, 0, tx); 1589 } 1590 } 1591 dsl_dataset_rele(ds_next, FTAG); 1592 1593 /* 1594 * NB: unique_bytes might not be accurate for the head objset. 1595 * Before SPA_VERSION 9, we didn't update its value when we 1596 * deleted the most recent snapshot. 1597 */ 1598 ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 1599 } else { 1600 /* 1601 * There's no next snapshot, so this is a head dataset. 1602 * Destroy the deadlist. Unless it's a clone, the 1603 * deadlist should be empty. (If it's a clone, it's 1604 * safe to ignore the deadlist contents.) 1605 */ 1606 struct killarg ka; 1607 1608 ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1609 bplist_close(&ds->ds_deadlist); 1610 bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1611 ds->ds_phys->ds_deadlist_obj = 0; 1612 1613 /* 1614 * Free everything that we point to (that's born after 1615 * the previous snapshot, if we are a clone) 1616 * 1617 * XXX we're doing this long task with the config lock held 1618 */ 1619 ka.usedp = &used; 1620 ka.compressedp = &compressed; 1621 ka.uncompressedp = &uncompressed; 1622 ka.zio = zio; 1623 ka.tx = tx; 1624 err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1625 ADVANCE_POST, kill_blkptr, &ka); 1626 ASSERT3U(err, ==, 0); 1627 ASSERT(spa_version(dp->dp_spa) < 1628 SPA_VERSION_UNIQUE_ACCURATE || 1629 used == ds->ds_phys->ds_unique_bytes); 1630 } 1631 1632 err = zio_wait(zio); 1633 ASSERT3U(err, ==, 0); 1634 1635 dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx); 1636 1637 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 1638 /* Erase the link in the dir */ 1639 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1640 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 1641 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1642 err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1643 ASSERT(err == 0); 1644 } else { 1645 /* remove from snapshot namespace */ 1646 dsl_dataset_t *ds_head; 1647 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 1648 VERIFY(0 == dsl_dataset_hold_obj(dp, 1649 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 1650 VERIFY(0 == dsl_dataset_get_snapname(ds)); 1651 #ifdef ZFS_DEBUG 1652 { 1653 uint64_t val; 1654 1655 err = dsl_dataset_snap_lookup(ds_head, 1656 ds->ds_snapname, &val); 1657 ASSERT3U(err, ==, 0); 1658 ASSERT3U(val, ==, obj); 1659 } 1660 #endif 1661 err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1662 ASSERT(err == 0); 1663 dsl_dataset_rele(ds_head, FTAG); 1664 } 1665 1666 if (ds_prev && ds->ds_prev != ds_prev) 1667 dsl_dataset_rele(ds_prev, FTAG); 1668 1669 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 1670 spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 1671 cr, "dataset = %llu", ds->ds_object); 1672 1673 dsl_dir_close(ds->ds_dir, ds); 1674 ds->ds_dir = NULL; 1675 dsl_dataset_drain_refs(ds, tag); 1676 VERIFY(0 == dmu_object_free(mos, obj, tx)); 1677 } 1678 1679 static int 1680 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1681 { 1682 uint64_t asize; 1683 1684 if (!dmu_tx_is_syncing(tx)) 1685 return (0); 1686 1687 /* 1688 * If there's an fs-only reservation, any blocks that might become 1689 * owned by the snapshot dataset must be accommodated by space 1690 * outside of the reservation. 1691 */ 1692 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 1693 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 1694 return (ENOSPC); 1695 1696 /* 1697 * Propogate any reserved space for this snapshot to other 1698 * snapshot checks in this sync group. 1699 */ 1700 if (asize > 0) 1701 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 1702 1703 return (0); 1704 } 1705 1706 /* ARGSUSED */ 1707 int 1708 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 1709 { 1710 dsl_dataset_t *ds = arg1; 1711 const char *snapname = arg2; 1712 int err; 1713 uint64_t value; 1714 1715 /* 1716 * We don't allow multiple snapshots of the same txg. If there 1717 * is already one, try again. 1718 */ 1719 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 1720 return (EAGAIN); 1721 1722 /* 1723 * Check for conflicting name snapshot name. 1724 */ 1725 err = dsl_dataset_snap_lookup(ds, snapname, &value); 1726 if (err == 0) 1727 return (EEXIST); 1728 if (err != ENOENT) 1729 return (err); 1730 1731 /* 1732 * Check that the dataset's name is not too long. Name consists 1733 * of the dataset's length + 1 for the @-sign + snapshot name's length 1734 */ 1735 if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 1736 return (ENAMETOOLONG); 1737 1738 err = dsl_dataset_snapshot_reserve_space(ds, tx); 1739 if (err) 1740 return (err); 1741 1742 ds->ds_trysnap_txg = tx->tx_txg; 1743 return (0); 1744 } 1745 1746 void 1747 dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1748 { 1749 dsl_dataset_t *ds = arg1; 1750 const char *snapname = arg2; 1751 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1752 dmu_buf_t *dbuf; 1753 dsl_dataset_phys_t *dsphys; 1754 uint64_t dsobj; 1755 objset_t *mos = dp->dp_meta_objset; 1756 int err; 1757 1758 spa_scrub_restart(dp->dp_spa, tx->tx_txg); 1759 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1760 1761 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1762 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 1763 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1764 dmu_buf_will_dirty(dbuf, tx); 1765 dsphys = dbuf->db_data; 1766 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 1767 dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1768 dsphys->ds_fsid_guid = unique_create(); 1769 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1770 sizeof (dsphys->ds_guid)); 1771 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1772 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1773 dsphys->ds_next_snap_obj = ds->ds_object; 1774 dsphys->ds_num_children = 1; 1775 dsphys->ds_creation_time = gethrestime_sec(); 1776 dsphys->ds_creation_txg = tx->tx_txg; 1777 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1778 dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1779 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1780 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 1781 dsphys->ds_flags = ds->ds_phys->ds_flags; 1782 dsphys->ds_bp = ds->ds_phys->ds_bp; 1783 dmu_buf_rele(dbuf, FTAG); 1784 1785 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 1786 if (ds->ds_prev) { 1787 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1788 ds->ds_object || 1789 ds->ds_prev->ds_phys->ds_num_children > 1); 1790 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 1791 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1792 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1793 ds->ds_prev->ds_phys->ds_creation_txg); 1794 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1795 } 1796 } 1797 1798 /* 1799 * If we have a reference-reservation on this dataset, we will 1800 * need to increase the amount of refreservation being charged 1801 * since our unique space is going to zero. 1802 */ 1803 if (ds->ds_reserved) { 1804 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 1805 dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx); 1806 } 1807 1808 bplist_close(&ds->ds_deadlist); 1809 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1810 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1811 ds->ds_phys->ds_prev_snap_obj = dsobj; 1812 ds->ds_phys->ds_prev_snap_txg = tx->tx_txg; 1813 ds->ds_phys->ds_unique_bytes = 0; 1814 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1815 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1816 ds->ds_phys->ds_deadlist_obj = 1817 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 1818 VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 1819 ds->ds_phys->ds_deadlist_obj)); 1820 1821 dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1822 err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1823 snapname, 8, 1, &dsobj, tx); 1824 ASSERT(err == 0); 1825 1826 if (ds->ds_prev) 1827 dsl_dataset_drop_ref(ds->ds_prev, ds); 1828 VERIFY(0 == dsl_dataset_get_ref(dp, 1829 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 1830 1831 spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 1832 "dataset = %llu", dsobj); 1833 } 1834 1835 void 1836 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1837 { 1838 ASSERT(dmu_tx_is_syncing(tx)); 1839 ASSERT(ds->ds_user_ptr != NULL); 1840 ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1841 1842 /* 1843 * in case we had to change ds_fsid_guid when we opened it, 1844 * sync it out now. 1845 */ 1846 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1847 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 1848 1849 dsl_dir_dirty(ds->ds_dir, tx); 1850 dmu_objset_sync(ds->ds_user_ptr, zio, tx); 1851 } 1852 1853 void 1854 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1855 { 1856 uint64_t refd, avail, uobjs, aobjs; 1857 1858 dsl_dir_stats(ds->ds_dir, nv); 1859 1860 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1861 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1862 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1863 1864 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1865 ds->ds_phys->ds_creation_time); 1866 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1867 ds->ds_phys->ds_creation_txg); 1868 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1869 ds->ds_quota); 1870 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1871 ds->ds_reserved); 1872 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1873 ds->ds_phys->ds_guid); 1874 1875 if (ds->ds_phys->ds_next_snap_obj) { 1876 /* 1877 * This is a snapshot; override the dd's space used with 1878 * our unique space and compression ratio. 1879 */ 1880 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1881 ds->ds_phys->ds_unique_bytes); 1882 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 1883 ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 1884 (ds->ds_phys->ds_uncompressed_bytes * 100 / 1885 ds->ds_phys->ds_compressed_bytes)); 1886 } 1887 } 1888 1889 void 1890 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1891 { 1892 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1893 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 1894 stat->dds_guid = ds->ds_phys->ds_guid; 1895 if (ds->ds_phys->ds_next_snap_obj) { 1896 stat->dds_is_snapshot = B_TRUE; 1897 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1898 } 1899 1900 /* clone origin is really a dsl_dir thing... */ 1901 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 1902 if (ds->ds_dir->dd_phys->dd_origin_obj) { 1903 dsl_dataset_t *ods; 1904 1905 VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 1906 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 1907 dsl_dataset_name(ods, stat->dds_origin); 1908 dsl_dataset_drop_ref(ods, FTAG); 1909 } 1910 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 1911 } 1912 1913 uint64_t 1914 dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1915 { 1916 return (ds->ds_fsid_guid); 1917 } 1918 1919 void 1920 dsl_dataset_space(dsl_dataset_t *ds, 1921 uint64_t *refdbytesp, uint64_t *availbytesp, 1922 uint64_t *usedobjsp, uint64_t *availobjsp) 1923 { 1924 *refdbytesp = ds->ds_phys->ds_used_bytes; 1925 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1926 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 1927 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 1928 if (ds->ds_quota != 0) { 1929 /* 1930 * Adjust available bytes according to refquota 1931 */ 1932 if (*refdbytesp < ds->ds_quota) 1933 *availbytesp = MIN(*availbytesp, 1934 ds->ds_quota - *refdbytesp); 1935 else 1936 *availbytesp = 0; 1937 } 1938 *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 1939 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1940 } 1941 1942 boolean_t 1943 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 1944 { 1945 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1946 1947 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 1948 dsl_pool_sync_context(dp)); 1949 if (ds->ds_prev == NULL) 1950 return (B_FALSE); 1951 if (ds->ds_phys->ds_bp.blk_birth > 1952 ds->ds_prev->ds_phys->ds_creation_txg) 1953 return (B_TRUE); 1954 return (B_FALSE); 1955 } 1956 1957 /* ARGSUSED */ 1958 static int 1959 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1960 { 1961 dsl_dataset_t *ds = arg1; 1962 char *newsnapname = arg2; 1963 dsl_dir_t *dd = ds->ds_dir; 1964 dsl_dataset_t *hds; 1965 uint64_t val; 1966 int err; 1967 1968 err = dsl_dataset_hold_obj(dd->dd_pool, 1969 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 1970 if (err) 1971 return (err); 1972 1973 /* new name better not be in use */ 1974 err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 1975 dsl_dataset_rele(hds, FTAG); 1976 1977 if (err == 0) 1978 err = EEXIST; 1979 else if (err == ENOENT) 1980 err = 0; 1981 1982 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 1983 if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 1984 err = ENAMETOOLONG; 1985 1986 return (err); 1987 } 1988 1989 static void 1990 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 1991 cred_t *cr, dmu_tx_t *tx) 1992 { 1993 dsl_dataset_t *ds = arg1; 1994 const char *newsnapname = arg2; 1995 dsl_dir_t *dd = ds->ds_dir; 1996 objset_t *mos = dd->dd_pool->dp_meta_objset; 1997 dsl_dataset_t *hds; 1998 int err; 1999 2000 ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2001 2002 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 2003 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2004 2005 VERIFY(0 == dsl_dataset_get_snapname(ds)); 2006 err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2007 ASSERT3U(err, ==, 0); 2008 mutex_enter(&ds->ds_lock); 2009 (void) strcpy(ds->ds_snapname, newsnapname); 2010 mutex_exit(&ds->ds_lock); 2011 err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 2012 ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2013 ASSERT3U(err, ==, 0); 2014 2015 spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 2016 cr, "dataset = %llu", ds->ds_object); 2017 dsl_dataset_rele(hds, FTAG); 2018 } 2019 2020 struct renamesnaparg { 2021 dsl_sync_task_group_t *dstg; 2022 char failed[MAXPATHLEN]; 2023 char *oldsnap; 2024 char *newsnap; 2025 }; 2026 2027 static int 2028 dsl_snapshot_rename_one(char *name, void *arg) 2029 { 2030 struct renamesnaparg *ra = arg; 2031 dsl_dataset_t *ds = NULL; 2032 char *cp; 2033 int err; 2034 2035 cp = name + strlen(name); 2036 *cp = '@'; 2037 (void) strcpy(cp + 1, ra->oldsnap); 2038 2039 /* 2040 * For recursive snapshot renames the parent won't be changing 2041 * so we just pass name for both the to/from argument. 2042 */ 2043 if (err = zfs_secpolicy_rename_perms(name, name, CRED())) { 2044 (void) strcpy(ra->failed, name); 2045 return (err); 2046 } 2047 2048 #ifdef _KERNEL 2049 /* 2050 * For all filesystems undergoing rename, we'll need to unmount it. 2051 */ 2052 (void) zfs_unmount_snap(name, NULL); 2053 #endif 2054 err = dsl_dataset_hold(name, ra->dstg, &ds); 2055 *cp = '\0'; 2056 if (err == ENOENT) { 2057 return (0); 2058 } else if (err) { 2059 (void) strcpy(ra->failed, name); 2060 return (err); 2061 } 2062 2063 dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 2064 dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 2065 2066 return (0); 2067 } 2068 2069 static int 2070 dsl_recursive_rename(char *oldname, const char *newname) 2071 { 2072 int err; 2073 struct renamesnaparg *ra; 2074 dsl_sync_task_t *dst; 2075 spa_t *spa; 2076 char *cp, *fsname = spa_strdup(oldname); 2077 int len = strlen(oldname); 2078 2079 /* truncate the snapshot name to get the fsname */ 2080 cp = strchr(fsname, '@'); 2081 *cp = '\0'; 2082 2083 err = spa_open(fsname, &spa, FTAG); 2084 if (err) { 2085 kmem_free(fsname, len + 1); 2086 return (err); 2087 } 2088 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 2089 ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 2090 2091 ra->oldsnap = strchr(oldname, '@') + 1; 2092 ra->newsnap = strchr(newname, '@') + 1; 2093 *ra->failed = '\0'; 2094 2095 err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 2096 DS_FIND_CHILDREN); 2097 kmem_free(fsname, len + 1); 2098 2099 if (err == 0) { 2100 err = dsl_sync_task_group_wait(ra->dstg); 2101 } 2102 2103 for (dst = list_head(&ra->dstg->dstg_tasks); dst; 2104 dst = list_next(&ra->dstg->dstg_tasks, dst)) { 2105 dsl_dataset_t *ds = dst->dst_arg1; 2106 if (dst->dst_err) { 2107 dsl_dir_name(ds->ds_dir, ra->failed); 2108 (void) strcat(ra->failed, "@"); 2109 (void) strcat(ra->failed, ra->newsnap); 2110 } 2111 dsl_dataset_rele(ds, ra->dstg); 2112 } 2113 2114 if (err) 2115 (void) strcpy(oldname, ra->failed); 2116 2117 dsl_sync_task_group_destroy(ra->dstg); 2118 kmem_free(ra, sizeof (struct renamesnaparg)); 2119 spa_close(spa, FTAG); 2120 return (err); 2121 } 2122 2123 static int 2124 dsl_valid_rename(char *oldname, void *arg) 2125 { 2126 int delta = *(int *)arg; 2127 2128 if (strlen(oldname) + delta >= MAXNAMELEN) 2129 return (ENAMETOOLONG); 2130 2131 return (0); 2132 } 2133 2134 #pragma weak dmu_objset_rename = dsl_dataset_rename 2135 int 2136 dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2137 { 2138 dsl_dir_t *dd; 2139 dsl_dataset_t *ds; 2140 const char *tail; 2141 int err; 2142 2143 err = dsl_dir_open(oldname, FTAG, &dd, &tail); 2144 if (err) 2145 return (err); 2146 if (tail == NULL) { 2147 int delta = strlen(newname) - strlen(oldname); 2148 2149 /* if we're growing, validate child size lengths */ 2150 if (delta > 0) 2151 err = dmu_objset_find(oldname, dsl_valid_rename, 2152 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 2153 2154 if (!err) 2155 err = dsl_dir_rename(dd, newname); 2156 dsl_dir_close(dd, FTAG); 2157 return (err); 2158 } 2159 if (tail[0] != '@') { 2160 /* the name ended in a nonexistant component */ 2161 dsl_dir_close(dd, FTAG); 2162 return (ENOENT); 2163 } 2164 2165 dsl_dir_close(dd, FTAG); 2166 2167 /* new name must be snapshot in same filesystem */ 2168 tail = strchr(newname, '@'); 2169 if (tail == NULL) 2170 return (EINVAL); 2171 tail++; 2172 if (strncmp(oldname, newname, tail - newname) != 0) 2173 return (EXDEV); 2174 2175 if (recursive) { 2176 err = dsl_recursive_rename(oldname, newname); 2177 } else { 2178 err = dsl_dataset_hold(oldname, FTAG, &ds); 2179 if (err) 2180 return (err); 2181 2182 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2183 dsl_dataset_snapshot_rename_check, 2184 dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 2185 2186 dsl_dataset_rele(ds, FTAG); 2187 } 2188 2189 return (err); 2190 } 2191 2192 struct promotedsarg { 2193 list_node_t link; 2194 dsl_dataset_t *ds; 2195 }; 2196 2197 struct promotearg { 2198 list_t snap_list; 2199 dsl_dataset_t *clone_origin, *old_head; 2200 uint64_t used, comp, uncomp, unique; 2201 uint64_t newnext_obj; 2202 }; 2203 2204 /* ARGSUSED */ 2205 static int 2206 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 2207 { 2208 dsl_dataset_t *hds = arg1; 2209 struct promotearg *pa = arg2; 2210 struct promotedsarg *snap = list_head(&pa->snap_list); 2211 dsl_pool_t *dp = hds->ds_dir->dd_pool; 2212 dsl_dataset_t *origin_ds = snap->ds; 2213 dsl_dataset_t *newnext_ds; 2214 char *name; 2215 uint64_t itor = 0; 2216 blkptr_t bp; 2217 int err; 2218 2219 /* Check that it is a clone */ 2220 if (hds->ds_dir->dd_phys->dd_origin_obj == 0) 2221 return (EINVAL); 2222 2223 /* Since this is so expensive, don't do the preliminary check */ 2224 if (!dmu_tx_is_syncing(tx)) 2225 return (0); 2226 2227 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 2228 return (EXDEV); 2229 2230 /* find origin's new next ds */ 2231 newnext_ds = hds; 2232 while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) { 2233 dsl_dataset_t *prev; 2234 2235 err = dsl_dataset_hold_obj(dp, 2236 newnext_ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 2237 if (newnext_ds != hds) 2238 dsl_dataset_rele(newnext_ds, FTAG); 2239 if (err) 2240 return (err); 2241 newnext_ds = prev; 2242 } 2243 pa->newnext_obj = newnext_ds->ds_object; 2244 2245 /* compute origin's new unique space */ 2246 pa->unique = 0; 2247 while ((err = bplist_iterate(&newnext_ds->ds_deadlist, 2248 &itor, &bp)) == 0) { 2249 if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg) 2250 pa->unique += bp_get_dasize(dp->dp_spa, &bp); 2251 } 2252 if (newnext_ds != hds) 2253 dsl_dataset_rele(newnext_ds, FTAG); 2254 if (err != ENOENT) 2255 return (err); 2256 2257 name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2258 2259 /* 2260 * Walk the snapshots that we are moving 2261 * 2262 * Compute space to transfer. Each snapshot gave birth to: 2263 * (my used) - (prev's used) + (deadlist's used) 2264 * So a sequence would look like: 2265 * uN - u(N-1) + dN + ... + u1 - u0 + d1 + u0 - 0 + d0 2266 * Which simplifies to: 2267 * uN + dN + ... + d1 + d0 2268 * Note however, if we stop before we reach the ORIGIN we get: 2269 * uN + dN + ... + dM - uM-1 2270 */ 2271 pa->used = origin_ds->ds_phys->ds_used_bytes; 2272 pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 2273 pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 2274 do { 2275 uint64_t val, dlused, dlcomp, dluncomp; 2276 dsl_dataset_t *ds = snap->ds; 2277 2278 /* Check that the snapshot name does not conflict */ 2279 dsl_dataset_name(ds, name); 2280 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2281 if (err == 0) 2282 err = EEXIST; 2283 if (err != ENOENT) 2284 break; 2285 err = 0; 2286 2287 /* The very first snapshot does not have a deadlist */ 2288 if (ds->ds_phys->ds_prev_snap_obj != 0) { 2289 if (err = bplist_space(&ds->ds_deadlist, 2290 &dlused, &dlcomp, &dluncomp)) 2291 break; 2292 pa->used += dlused; 2293 pa->comp += dlcomp; 2294 pa->uncomp += dluncomp; 2295 } 2296 } while (snap = list_next(&pa->snap_list, snap)); 2297 2298 /* 2299 * If we are a clone of a clone then we never reached ORIGIN, 2300 * so we need to subtract out the clone origin's used space. 2301 */ 2302 if (pa->clone_origin) { 2303 pa->used -= pa->clone_origin->ds_phys->ds_used_bytes; 2304 pa->comp -= pa->clone_origin->ds_phys->ds_compressed_bytes; 2305 pa->uncomp -= pa->clone_origin->ds_phys->ds_uncompressed_bytes; 2306 } 2307 2308 kmem_free(name, MAXPATHLEN); 2309 2310 /* Check that there is enough space here */ 2311 if (err == 0) { 2312 dsl_dir_t *odd = origin_ds->ds_dir; 2313 err = dsl_dir_transfer_possible(odd, hds->ds_dir, pa->used); 2314 } 2315 2316 return (err); 2317 } 2318 2319 static void 2320 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 2321 { 2322 dsl_dataset_t *hds = arg1; 2323 struct promotearg *pa = arg2; 2324 struct promotedsarg *snap = list_head(&pa->snap_list); 2325 dsl_dataset_t *origin_ds = snap->ds; 2326 dsl_dir_t *dd = hds->ds_dir; 2327 dsl_pool_t *dp = hds->ds_dir->dd_pool; 2328 dsl_dir_t *odd = NULL; 2329 char *name; 2330 2331 ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 2332 2333 /* 2334 * We need to explicitly open odd, since origin_ds's dd will be 2335 * changing. 2336 */ 2337 VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 2338 NULL, FTAG, &odd)); 2339 2340 /* change origin's next snap */ 2341 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2342 origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj; 2343 2344 /* change origin */ 2345 dmu_buf_will_dirty(dd->dd_dbuf, tx); 2346 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2347 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 2348 dmu_buf_will_dirty(odd->dd_dbuf, tx); 2349 odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 2350 2351 /* move snapshots to this dir */ 2352 name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2353 do { 2354 dsl_dataset_t *ds = snap->ds; 2355 2356 /* move snap name entry */ 2357 dsl_dataset_name(ds, name); 2358 VERIFY(0 == dsl_dataset_snap_remove(pa->old_head, 2359 ds->ds_snapname, tx)); 2360 VERIFY(0 == zap_add(dp->dp_meta_objset, 2361 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 2362 8, 1, &ds->ds_object, tx)); 2363 2364 /* change containing dsl_dir */ 2365 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2366 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 2367 ds->ds_phys->ds_dir_obj = dd->dd_object; 2368 ASSERT3P(ds->ds_dir, ==, odd); 2369 dsl_dir_close(ds->ds_dir, ds); 2370 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 2371 NULL, ds, &ds->ds_dir)); 2372 2373 ASSERT3U(dsl_prop_numcb(ds), ==, 0); 2374 } while (snap = list_next(&pa->snap_list, snap)); 2375 2376 /* change space accounting */ 2377 dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx); 2378 dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx); 2379 origin_ds->ds_phys->ds_unique_bytes = pa->unique; 2380 2381 /* log history record */ 2382 spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 2383 cr, "dataset = %llu", hds->ds_object); 2384 2385 dsl_dir_close(odd, FTAG); 2386 kmem_free(name, MAXPATHLEN); 2387 } 2388 2389 int 2390 dsl_dataset_promote(const char *name) 2391 { 2392 dsl_dataset_t *ds; 2393 dsl_dir_t *dd; 2394 dsl_pool_t *dp; 2395 dmu_object_info_t doi; 2396 struct promotearg pa; 2397 struct promotedsarg *snap; 2398 uint64_t snap_obj; 2399 uint64_t last_snap = 0; 2400 int err; 2401 2402 err = dsl_dataset_hold(name, FTAG, &ds); 2403 if (err) 2404 return (err); 2405 dd = ds->ds_dir; 2406 dp = dd->dd_pool; 2407 2408 err = dmu_object_info(dp->dp_meta_objset, 2409 ds->ds_phys->ds_snapnames_zapobj, &doi); 2410 if (err) { 2411 dsl_dataset_rele(ds, FTAG); 2412 return (err); 2413 } 2414 2415 /* 2416 * We are going to inherit all the snapshots taken before our 2417 * origin (i.e., our new origin will be our parent's origin). 2418 * Take ownership of them so that we can rename them into our 2419 * namespace. 2420 */ 2421 pa.clone_origin = NULL; 2422 list_create(&pa.snap_list, 2423 sizeof (struct promotedsarg), offsetof(struct promotedsarg, link)); 2424 rw_enter(&dp->dp_config_rwlock, RW_READER); 2425 ASSERT(dd->dd_phys->dd_origin_obj != 0); 2426 snap_obj = dd->dd_phys->dd_origin_obj; 2427 while (snap_obj) { 2428 snap = kmem_alloc(sizeof (struct promotedsarg), KM_SLEEP); 2429 err = dsl_dataset_own_obj(dp, snap_obj, 0, FTAG, &snap->ds); 2430 if (err == ENOENT) { 2431 /* lost race with snapshot destroy */ 2432 struct promotedsarg *last = list_tail(&pa.snap_list); 2433 ASSERT(snap_obj != last->ds->ds_phys->ds_prev_snap_obj); 2434 snap_obj = last->ds->ds_phys->ds_prev_snap_obj; 2435 kmem_free(snap, sizeof (struct promotedsarg)); 2436 continue; 2437 } else if (err) { 2438 kmem_free(snap, sizeof (struct promotedsarg)); 2439 rw_exit(&dp->dp_config_rwlock); 2440 goto out; 2441 } 2442 /* 2443 * We could be a clone of a clone. If we reach our 2444 * parent's branch point, we're done. 2445 */ 2446 if (last_snap && 2447 snap->ds->ds_phys->ds_next_snap_obj != last_snap) { 2448 pa.clone_origin = snap->ds; 2449 kmem_free(snap, sizeof (struct promotedsarg)); 2450 snap_obj = 0; 2451 } else { 2452 list_insert_tail(&pa.snap_list, snap); 2453 last_snap = snap_obj; 2454 snap_obj = snap->ds->ds_phys->ds_prev_snap_obj; 2455 } 2456 } 2457 snap = list_head(&pa.snap_list); 2458 ASSERT(snap != NULL); 2459 err = dsl_dataset_hold_obj(dp, 2460 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &pa.old_head); 2461 rw_exit(&dp->dp_config_rwlock); 2462 2463 if (err) 2464 goto out; 2465 2466 /* 2467 * Add in 128x the snapnames zapobj size, since we will be moving 2468 * a bunch of snapnames to the promoted ds, and dirtying their 2469 * bonus buffers. 2470 */ 2471 err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 2472 dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks); 2473 2474 dsl_dataset_rele(pa.old_head, FTAG); 2475 out: 2476 while ((snap = list_tail(&pa.snap_list)) != NULL) { 2477 list_remove(&pa.snap_list, snap); 2478 dsl_dataset_disown(snap->ds, FTAG); 2479 kmem_free(snap, sizeof (struct promotedsarg)); 2480 } 2481 list_destroy(&pa.snap_list); 2482 if (pa.clone_origin) 2483 dsl_dataset_disown(pa.clone_origin, FTAG); 2484 dsl_dataset_rele(ds, FTAG); 2485 return (err); 2486 } 2487 2488 struct cloneswaparg { 2489 dsl_dataset_t *cds; /* clone dataset */ 2490 dsl_dataset_t *ohds; /* origin's head dataset */ 2491 boolean_t force; 2492 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 2493 }; 2494 2495 /* ARGSUSED */ 2496 static int 2497 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 2498 { 2499 struct cloneswaparg *csa = arg1; 2500 2501 /* they should both be heads */ 2502 if (dsl_dataset_is_snapshot(csa->cds) || 2503 dsl_dataset_is_snapshot(csa->ohds)) 2504 return (EINVAL); 2505 2506 /* the branch point should be just before them */ 2507 if (csa->cds->ds_prev != csa->ohds->ds_prev) 2508 return (EINVAL); 2509 2510 /* cds should be the clone */ 2511 if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj != 2512 csa->ohds->ds_object) 2513 return (EINVAL); 2514 2515 /* the clone should be a child of the origin */ 2516 if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 2517 return (EINVAL); 2518 2519 /* ohds shouldn't be modified unless 'force' */ 2520 if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 2521 return (ETXTBSY); 2522 2523 /* adjust amount of any unconsumed refreservation */ 2524 csa->unused_refres_delta = 2525 (int64_t)MIN(csa->ohds->ds_reserved, 2526 csa->ohds->ds_phys->ds_unique_bytes) - 2527 (int64_t)MIN(csa->ohds->ds_reserved, 2528 csa->cds->ds_phys->ds_unique_bytes); 2529 2530 if (csa->unused_refres_delta > 0 && 2531 csa->unused_refres_delta > 2532 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 2533 return (ENOSPC); 2534 2535 return (0); 2536 } 2537 2538 /* ARGSUSED */ 2539 static void 2540 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 2541 { 2542 struct cloneswaparg *csa = arg1; 2543 dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 2544 uint64_t itor = 0; 2545 blkptr_t bp; 2546 uint64_t unique = 0; 2547 int err; 2548 2549 ASSERT(csa->cds->ds_reserved == 0); 2550 ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota); 2551 2552 dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 2553 dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 2554 dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx); 2555 2556 if (csa->cds->ds_user_ptr != NULL) { 2557 csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr); 2558 csa->cds->ds_user_ptr = NULL; 2559 } 2560 2561 if (csa->ohds->ds_user_ptr != NULL) { 2562 csa->ohds->ds_user_evict_func(csa->ohds, 2563 csa->ohds->ds_user_ptr); 2564 csa->ohds->ds_user_ptr = NULL; 2565 } 2566 2567 /* compute unique space */ 2568 while ((err = bplist_iterate(&csa->cds->ds_deadlist, 2569 &itor, &bp)) == 0) { 2570 if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg) 2571 unique += bp_get_dasize(dp->dp_spa, &bp); 2572 } 2573 VERIFY(err == ENOENT); 2574 2575 /* reset origin's unique bytes */ 2576 csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique; 2577 2578 /* swap blkptrs */ 2579 { 2580 blkptr_t tmp; 2581 tmp = csa->ohds->ds_phys->ds_bp; 2582 csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 2583 csa->cds->ds_phys->ds_bp = tmp; 2584 } 2585 2586 /* set dd_*_bytes */ 2587 { 2588 int64_t dused, dcomp, duncomp; 2589 uint64_t cdl_used, cdl_comp, cdl_uncomp; 2590 uint64_t odl_used, odl_comp, odl_uncomp; 2591 2592 VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 2593 &cdl_comp, &cdl_uncomp)); 2594 VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 2595 &odl_comp, &odl_uncomp)); 2596 dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 2597 (csa->ohds->ds_phys->ds_used_bytes + odl_used); 2598 dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 2599 (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 2600 duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 2601 cdl_uncomp - 2602 (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2603 2604 dsl_dir_diduse_space(csa->ohds->ds_dir, 2605 dused, dcomp, duncomp, tx); 2606 dsl_dir_diduse_space(csa->cds->ds_dir, 2607 -dused, -dcomp, -duncomp, tx); 2608 } 2609 2610 #define SWITCH64(x, y) \ 2611 { \ 2612 uint64_t __tmp = (x); \ 2613 (x) = (y); \ 2614 (y) = __tmp; \ 2615 } 2616 2617 /* swap ds_*_bytes */ 2618 SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 2619 csa->cds->ds_phys->ds_used_bytes); 2620 SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 2621 csa->cds->ds_phys->ds_compressed_bytes); 2622 SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 2623 csa->cds->ds_phys->ds_uncompressed_bytes); 2624 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 2625 csa->cds->ds_phys->ds_unique_bytes); 2626 2627 /* apply any parent delta for change in unconsumed refreservation */ 2628 dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta, 2629 0, 0, tx); 2630 2631 /* swap deadlists */ 2632 bplist_close(&csa->cds->ds_deadlist); 2633 bplist_close(&csa->ohds->ds_deadlist); 2634 SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 2635 csa->cds->ds_phys->ds_deadlist_obj); 2636 VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 2637 csa->cds->ds_phys->ds_deadlist_obj)); 2638 VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 2639 csa->ohds->ds_phys->ds_deadlist_obj)); 2640 } 2641 2642 /* 2643 * Swap 'clone' with its origin head file system. Used at the end 2644 * of "online recv" to swizzle the file system to the new version. 2645 */ 2646 int 2647 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 2648 boolean_t force) 2649 { 2650 struct cloneswaparg csa; 2651 int error; 2652 2653 ASSERT(clone->ds_owner); 2654 ASSERT(origin_head->ds_owner); 2655 retry: 2656 /* Need exclusive access for the swap */ 2657 rw_enter(&clone->ds_rwlock, RW_WRITER); 2658 if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 2659 rw_exit(&clone->ds_rwlock); 2660 rw_enter(&origin_head->ds_rwlock, RW_WRITER); 2661 if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 2662 rw_exit(&origin_head->ds_rwlock); 2663 goto retry; 2664 } 2665 } 2666 csa.cds = clone; 2667 csa.ohds = origin_head; 2668 csa.force = force; 2669 error = dsl_sync_task_do(clone->ds_dir->dd_pool, 2670 dsl_dataset_clone_swap_check, 2671 dsl_dataset_clone_swap_sync, &csa, NULL, 9); 2672 return (error); 2673 } 2674 2675 /* 2676 * Given a pool name and a dataset object number in that pool, 2677 * return the name of that dataset. 2678 */ 2679 int 2680 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2681 { 2682 spa_t *spa; 2683 dsl_pool_t *dp; 2684 dsl_dataset_t *ds; 2685 int error; 2686 2687 if ((error = spa_open(pname, &spa, FTAG)) != 0) 2688 return (error); 2689 dp = spa_get_dsl(spa); 2690 rw_enter(&dp->dp_config_rwlock, RW_READER); 2691 if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 2692 dsl_dataset_name(ds, buf); 2693 dsl_dataset_rele(ds, FTAG); 2694 } 2695 rw_exit(&dp->dp_config_rwlock); 2696 spa_close(spa, FTAG); 2697 2698 return (error); 2699 } 2700 2701 int 2702 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 2703 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2704 { 2705 int error = 0; 2706 2707 ASSERT3S(asize, >, 0); 2708 2709 /* 2710 * *ref_rsrv is the portion of asize that will come from any 2711 * unconsumed refreservation space. 2712 */ 2713 *ref_rsrv = 0; 2714 2715 mutex_enter(&ds->ds_lock); 2716 /* 2717 * Make a space adjustment for reserved bytes. 2718 */ 2719 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 2720 ASSERT3U(*used, >=, 2721 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 2722 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 2723 *ref_rsrv = 2724 asize - MIN(asize, parent_delta(ds, asize + inflight)); 2725 } 2726 2727 if (!check_quota || ds->ds_quota == 0) { 2728 mutex_exit(&ds->ds_lock); 2729 return (0); 2730 } 2731 /* 2732 * If they are requesting more space, and our current estimate 2733 * is over quota, they get to try again unless the actual 2734 * on-disk is over quota and there are no pending changes (which 2735 * may free up space for us). 2736 */ 2737 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 2738 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 2739 error = ERESTART; 2740 else 2741 error = EDQUOT; 2742 } 2743 mutex_exit(&ds->ds_lock); 2744 2745 return (error); 2746 } 2747 2748 /* ARGSUSED */ 2749 static int 2750 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 2751 { 2752 dsl_dataset_t *ds = arg1; 2753 uint64_t *quotap = arg2; 2754 uint64_t new_quota = *quotap; 2755 2756 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 2757 return (ENOTSUP); 2758 2759 if (new_quota == 0) 2760 return (0); 2761 2762 if (new_quota < ds->ds_phys->ds_used_bytes || 2763 new_quota < ds->ds_reserved) 2764 return (ENOSPC); 2765 2766 return (0); 2767 } 2768 2769 /* ARGSUSED */ 2770 void 2771 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 2772 { 2773 dsl_dataset_t *ds = arg1; 2774 uint64_t *quotap = arg2; 2775 uint64_t new_quota = *quotap; 2776 2777 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2778 2779 ds->ds_quota = new_quota; 2780 2781 dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 2782 2783 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 2784 tx, cr, "%lld dataset = %llu ", 2785 (longlong_t)new_quota, ds->ds_object); 2786 } 2787 2788 int 2789 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 2790 { 2791 dsl_dataset_t *ds; 2792 int err; 2793 2794 err = dsl_dataset_hold(dsname, FTAG, &ds); 2795 if (err) 2796 return (err); 2797 2798 if (quota != ds->ds_quota) { 2799 /* 2800 * If someone removes a file, then tries to set the quota, we 2801 * want to make sure the file freeing takes effect. 2802 */ 2803 txg_wait_open(ds->ds_dir->dd_pool, 0); 2804 2805 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2806 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 2807 ds, "a, 0); 2808 } 2809 dsl_dataset_rele(ds, FTAG); 2810 return (err); 2811 } 2812 2813 static int 2814 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 2815 { 2816 dsl_dataset_t *ds = arg1; 2817 uint64_t *reservationp = arg2; 2818 uint64_t new_reservation = *reservationp; 2819 int64_t delta; 2820 uint64_t unique; 2821 2822 if (new_reservation > INT64_MAX) 2823 return (EOVERFLOW); 2824 2825 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 2826 SPA_VERSION_REFRESERVATION) 2827 return (ENOTSUP); 2828 2829 if (dsl_dataset_is_snapshot(ds)) 2830 return (EINVAL); 2831 2832 /* 2833 * If we are doing the preliminary check in open context, the 2834 * space estimates may be inaccurate. 2835 */ 2836 if (!dmu_tx_is_syncing(tx)) 2837 return (0); 2838 2839 mutex_enter(&ds->ds_lock); 2840 unique = dsl_dataset_unique(ds); 2841 delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved); 2842 mutex_exit(&ds->ds_lock); 2843 2844 if (delta > 0 && 2845 delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 2846 return (ENOSPC); 2847 if (delta > 0 && ds->ds_quota > 0 && 2848 new_reservation > ds->ds_quota) 2849 return (ENOSPC); 2850 2851 return (0); 2852 } 2853 2854 /* ARGSUSED */ 2855 static void 2856 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 2857 dmu_tx_t *tx) 2858 { 2859 dsl_dataset_t *ds = arg1; 2860 uint64_t *reservationp = arg2; 2861 uint64_t new_reservation = *reservationp; 2862 uint64_t unique; 2863 int64_t delta; 2864 2865 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2866 2867 mutex_enter(&ds->ds_lock); 2868 unique = dsl_dataset_unique(ds); 2869 delta = MAX(0, (int64_t)(new_reservation - unique)) - 2870 MAX(0, (int64_t)(ds->ds_reserved - unique)); 2871 ds->ds_reserved = new_reservation; 2872 mutex_exit(&ds->ds_lock); 2873 2874 dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation", 2875 new_reservation, cr, tx); 2876 2877 dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx); 2878 2879 spa_history_internal_log(LOG_DS_REFRESERV, 2880 ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 2881 (longlong_t)new_reservation, 2882 ds->ds_dir->dd_phys->dd_head_dataset_obj); 2883 } 2884 2885 int 2886 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 2887 { 2888 dsl_dataset_t *ds; 2889 int err; 2890 2891 err = dsl_dataset_hold(dsname, FTAG, &ds); 2892 if (err) 2893 return (err); 2894 2895 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2896 dsl_dataset_set_reservation_check, 2897 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 2898 dsl_dataset_rele(ds, FTAG); 2899 return (err); 2900 } 2901