dsl_dataset.c revision 276081
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Portions Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org> 24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25 * Copyright (c) 2014, Joyent, Inc. All rights reserved. 26 * Copyright (c) 2014 RackTop Systems. 27 */ 28 29#include <sys/dmu_objset.h> 30#include <sys/dsl_dataset.h> 31#include <sys/dsl_dir.h> 32#include <sys/dsl_prop.h> 33#include <sys/dsl_synctask.h> 34#include <sys/dmu_traverse.h> 35#include <sys/dmu_impl.h> 36#include <sys/dmu_tx.h> 37#include <sys/arc.h> 38#include <sys/zio.h> 39#include <sys/zap.h> 40#include <sys/zfeature.h> 41#include <sys/unique.h> 42#include <sys/zfs_context.h> 43#include <sys/zfs_ioctl.h> 44#include <sys/spa.h> 45#include <sys/zfs_znode.h> 46#include <sys/zfs_onexit.h> 47#include <sys/zvol.h> 48#include <sys/dsl_scan.h> 49#include <sys/dsl_deadlist.h> 50#include <sys/dsl_destroy.h> 51#include <sys/dsl_userhold.h> 52#include <sys/dsl_bookmark.h> 53 54SYSCTL_DECL(_vfs_zfs); 55 56/* 57 * The SPA supports block sizes up to 16MB. However, very large blocks 58 * can have an impact on i/o latency (e.g. tying up a spinning disk for 59 * ~300ms), and also potentially on the memory allocator. Therefore, 60 * we do not allow the recordsize to be set larger than zfs_max_recordsize 61 * (default 1MB). Larger blocks can be created by changing this tunable, 62 * and pools with larger blocks can always be imported and used, regardless 63 * of this setting. 64 */ 65int zfs_max_recordsize = 1 * 1024 * 1024; 66SYSCTL_INT(_vfs_zfs, OID_AUTO, max_recordsize, CTLFLAG_RWTUN, 67 &zfs_max_recordsize, 0, 68 "Maximum block size. Expect dragons when tuning this."); 69 70#define SWITCH64(x, y) \ 71 { \ 72 uint64_t __tmp = (x); \ 73 (x) = (y); \ 74 (y) = __tmp; \ 75 } 76 77#define DS_REF_MAX (1ULL << 62) 78 79/* 80 * Figure out how much of this delta should be propogated to the dsl_dir 81 * layer. If there's a refreservation, that space has already been 82 * partially accounted for in our ancestors. 83 */ 84static int64_t 85parent_delta(dsl_dataset_t *ds, int64_t delta) 86{ 87 uint64_t old_bytes, new_bytes; 88 89 if (ds->ds_reserved == 0) 90 return (delta); 91 92 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 93 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 94 95 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 96 return (new_bytes - old_bytes); 97} 98 99void 100dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 101{ 102 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 103 int compressed = BP_GET_PSIZE(bp); 104 int uncompressed = BP_GET_UCSIZE(bp); 105 int64_t delta; 106 107 dprintf_bp(bp, "ds=%p", ds); 108 109 ASSERT(dmu_tx_is_syncing(tx)); 110 /* It could have been compressed away to nothing */ 111 if (BP_IS_HOLE(bp)) 112 return; 113 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 114 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 115 if (ds == NULL) { 116 dsl_pool_mos_diduse_space(tx->tx_pool, 117 used, compressed, uncompressed); 118 return; 119 } 120 121 dmu_buf_will_dirty(ds->ds_dbuf, tx); 122 mutex_enter(&ds->ds_lock); 123 delta = parent_delta(ds, used); 124 ds->ds_phys->ds_referenced_bytes += used; 125 ds->ds_phys->ds_compressed_bytes += compressed; 126 ds->ds_phys->ds_uncompressed_bytes += uncompressed; 127 ds->ds_phys->ds_unique_bytes += used; 128 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) 129 ds->ds_need_large_blocks = B_TRUE; 130 mutex_exit(&ds->ds_lock); 131 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 132 compressed, uncompressed, tx); 133 dsl_dir_transfer_space(ds->ds_dir, used - delta, 134 DD_USED_REFRSRV, DD_USED_HEAD, tx); 135} 136 137int 138dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 139 boolean_t async) 140{ 141 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 142 int compressed = BP_GET_PSIZE(bp); 143 int uncompressed = BP_GET_UCSIZE(bp); 144 145 if (BP_IS_HOLE(bp)) 146 return (0); 147 148 ASSERT(dmu_tx_is_syncing(tx)); 149 ASSERT(bp->blk_birth <= tx->tx_txg); 150 151 if (ds == NULL) { 152 dsl_free(tx->tx_pool, tx->tx_txg, bp); 153 dsl_pool_mos_diduse_space(tx->tx_pool, 154 -used, -compressed, -uncompressed); 155 return (used); 156 } 157 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 158 159 ASSERT(!dsl_dataset_is_snapshot(ds)); 160 dmu_buf_will_dirty(ds->ds_dbuf, tx); 161 162 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 163 int64_t delta; 164 165 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 166 dsl_free(tx->tx_pool, tx->tx_txg, bp); 167 168 mutex_enter(&ds->ds_lock); 169 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 170 !DS_UNIQUE_IS_ACCURATE(ds)); 171 delta = parent_delta(ds, -used); 172 ds->ds_phys->ds_unique_bytes -= used; 173 mutex_exit(&ds->ds_lock); 174 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 175 delta, -compressed, -uncompressed, tx); 176 dsl_dir_transfer_space(ds->ds_dir, -used - delta, 177 DD_USED_REFRSRV, DD_USED_HEAD, tx); 178 } else { 179 dprintf_bp(bp, "putting on dead list: %s", ""); 180 if (async) { 181 /* 182 * We are here as part of zio's write done callback, 183 * which means we're a zio interrupt thread. We can't 184 * call dsl_deadlist_insert() now because it may block 185 * waiting for I/O. Instead, put bp on the deferred 186 * queue and let dsl_pool_sync() finish the job. 187 */ 188 bplist_append(&ds->ds_pending_deadlist, bp); 189 } else { 190 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 191 } 192 ASSERT3U(ds->ds_prev->ds_object, ==, 193 ds->ds_phys->ds_prev_snap_obj); 194 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 195 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 196 if (ds->ds_prev->ds_phys->ds_next_snap_obj == 197 ds->ds_object && bp->blk_birth > 198 ds->ds_prev->ds_phys->ds_prev_snap_txg) { 199 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 200 mutex_enter(&ds->ds_prev->ds_lock); 201 ds->ds_prev->ds_phys->ds_unique_bytes += used; 202 mutex_exit(&ds->ds_prev->ds_lock); 203 } 204 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 205 dsl_dir_transfer_space(ds->ds_dir, used, 206 DD_USED_HEAD, DD_USED_SNAP, tx); 207 } 208 } 209 mutex_enter(&ds->ds_lock); 210 ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used); 211 ds->ds_phys->ds_referenced_bytes -= used; 212 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 213 ds->ds_phys->ds_compressed_bytes -= compressed; 214 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 215 ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 216 mutex_exit(&ds->ds_lock); 217 218 return (used); 219} 220 221uint64_t 222dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 223{ 224 uint64_t trysnap = 0; 225 226 if (ds == NULL) 227 return (0); 228 /* 229 * The snapshot creation could fail, but that would cause an 230 * incorrect FALSE return, which would only result in an 231 * overestimation of the amount of space that an operation would 232 * consume, which is OK. 233 * 234 * There's also a small window where we could miss a pending 235 * snapshot, because we could set the sync task in the quiescing 236 * phase. So this should only be used as a guess. 237 */ 238 if (ds->ds_trysnap_txg > 239 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 240 trysnap = ds->ds_trysnap_txg; 241 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 242} 243 244boolean_t 245dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 246 uint64_t blk_birth) 247{ 248 if (blk_birth <= dsl_dataset_prev_snap_txg(ds) || 249 (bp != NULL && BP_IS_HOLE(bp))) 250 return (B_FALSE); 251 252 ddt_prefetch(dsl_dataset_get_spa(ds), bp); 253 254 return (B_TRUE); 255} 256 257/* ARGSUSED */ 258static void 259dsl_dataset_evict(dmu_buf_t *db, void *dsv) 260{ 261 dsl_dataset_t *ds = dsv; 262 263 ASSERT(ds->ds_owner == NULL); 264 265 unique_remove(ds->ds_fsid_guid); 266 267 if (ds->ds_objset != NULL) 268 dmu_objset_evict(ds->ds_objset); 269 270 if (ds->ds_prev) { 271 dsl_dataset_rele(ds->ds_prev, ds); 272 ds->ds_prev = NULL; 273 } 274 275 bplist_destroy(&ds->ds_pending_deadlist); 276 if (ds->ds_phys->ds_deadlist_obj != 0) 277 dsl_deadlist_close(&ds->ds_deadlist); 278 if (ds->ds_dir) 279 dsl_dir_rele(ds->ds_dir, ds); 280 281 ASSERT(!list_link_active(&ds->ds_synced_link)); 282 283 if (mutex_owned(&ds->ds_lock)) 284 mutex_exit(&ds->ds_lock); 285 mutex_destroy(&ds->ds_lock); 286 if (mutex_owned(&ds->ds_opening_lock)) 287 mutex_exit(&ds->ds_opening_lock); 288 mutex_destroy(&ds->ds_opening_lock); 289 mutex_destroy(&ds->ds_sendstream_lock); 290 refcount_destroy(&ds->ds_longholds); 291 292 kmem_free(ds, sizeof (dsl_dataset_t)); 293} 294 295int 296dsl_dataset_get_snapname(dsl_dataset_t *ds) 297{ 298 dsl_dataset_phys_t *headphys; 299 int err; 300 dmu_buf_t *headdbuf; 301 dsl_pool_t *dp = ds->ds_dir->dd_pool; 302 objset_t *mos = dp->dp_meta_objset; 303 304 if (ds->ds_snapname[0]) 305 return (0); 306 if (ds->ds_phys->ds_next_snap_obj == 0) 307 return (0); 308 309 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 310 FTAG, &headdbuf); 311 if (err != 0) 312 return (err); 313 headphys = headdbuf->db_data; 314 err = zap_value_search(dp->dp_meta_objset, 315 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 316 dmu_buf_rele(headdbuf, FTAG); 317 return (err); 318} 319 320int 321dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 322{ 323 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 324 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 325 matchtype_t mt; 326 int err; 327 328 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 329 mt = MT_FIRST; 330 else 331 mt = MT_EXACT; 332 333 err = zap_lookup_norm(mos, snapobj, name, 8, 1, 334 value, mt, NULL, 0, NULL); 335 if (err == ENOTSUP && mt == MT_FIRST) 336 err = zap_lookup(mos, snapobj, name, 8, 1, value); 337 return (err); 338} 339 340int 341dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 342 boolean_t adj_cnt) 343{ 344 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 345 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 346 matchtype_t mt; 347 int err; 348 349 dsl_dir_snap_cmtime_update(ds->ds_dir); 350 351 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 352 mt = MT_FIRST; 353 else 354 mt = MT_EXACT; 355 356 err = zap_remove_norm(mos, snapobj, name, mt, tx); 357 if (err == ENOTSUP && mt == MT_FIRST) 358 err = zap_remove(mos, snapobj, name, tx); 359 360 if (err == 0 && adj_cnt) 361 dsl_fs_ss_count_adjust(ds->ds_dir, -1, 362 DD_FIELD_SNAPSHOT_COUNT, tx); 363 364 return (err); 365} 366 367int 368dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 369 dsl_dataset_t **dsp) 370{ 371 objset_t *mos = dp->dp_meta_objset; 372 dmu_buf_t *dbuf; 373 dsl_dataset_t *ds; 374 int err; 375 dmu_object_info_t doi; 376 377 ASSERT(dsl_pool_config_held(dp)); 378 379 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 380 if (err != 0) 381 return (err); 382 383 /* Make sure dsobj has the correct object type. */ 384 dmu_object_info_from_db(dbuf, &doi); 385 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 386 dmu_buf_rele(dbuf, tag); 387 return (SET_ERROR(EINVAL)); 388 } 389 390 ds = dmu_buf_get_user(dbuf); 391 if (ds == NULL) { 392 dsl_dataset_t *winner = NULL; 393 394 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 395 ds->ds_dbuf = dbuf; 396 ds->ds_object = dsobj; 397 ds->ds_phys = dbuf->db_data; 398 399 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 400 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 401 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 402 refcount_create(&ds->ds_longholds); 403 404 bplist_create(&ds->ds_pending_deadlist); 405 dsl_deadlist_open(&ds->ds_deadlist, 406 mos, ds->ds_phys->ds_deadlist_obj); 407 408 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 409 offsetof(dmu_sendarg_t, dsa_link)); 410 411 if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 412 int zaperr = zap_contains(mos, dsobj, DS_FIELD_LARGE_BLOCKS); 413 if (zaperr != ENOENT) { 414 VERIFY0(zaperr); 415 ds->ds_large_blocks = B_TRUE; 416 } 417 } 418 419 if (err == 0) { 420 err = dsl_dir_hold_obj(dp, 421 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 422 } 423 if (err != 0) { 424 mutex_destroy(&ds->ds_lock); 425 mutex_destroy(&ds->ds_opening_lock); 426 mutex_destroy(&ds->ds_sendstream_lock); 427 refcount_destroy(&ds->ds_longholds); 428 bplist_destroy(&ds->ds_pending_deadlist); 429 dsl_deadlist_close(&ds->ds_deadlist); 430 kmem_free(ds, sizeof (dsl_dataset_t)); 431 dmu_buf_rele(dbuf, tag); 432 return (err); 433 } 434 435 if (!dsl_dataset_is_snapshot(ds)) { 436 ds->ds_snapname[0] = '\0'; 437 if (ds->ds_phys->ds_prev_snap_obj != 0) { 438 err = dsl_dataset_hold_obj(dp, 439 ds->ds_phys->ds_prev_snap_obj, 440 ds, &ds->ds_prev); 441 } 442 if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 443 int zaperr = zap_lookup(mos, ds->ds_object, 444 DS_FIELD_BOOKMARK_NAMES, 445 sizeof (ds->ds_bookmarks), 1, 446 &ds->ds_bookmarks); 447 if (zaperr != ENOENT) 448 VERIFY0(zaperr); 449 } 450 } else { 451 if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 452 err = dsl_dataset_get_snapname(ds); 453 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 454 err = zap_count( 455 ds->ds_dir->dd_pool->dp_meta_objset, 456 ds->ds_phys->ds_userrefs_obj, 457 &ds->ds_userrefs); 458 } 459 } 460 461 if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 462 err = dsl_prop_get_int_ds(ds, 463 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 464 &ds->ds_reserved); 465 if (err == 0) { 466 err = dsl_prop_get_int_ds(ds, 467 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 468 &ds->ds_quota); 469 } 470 } else { 471 ds->ds_reserved = ds->ds_quota = 0; 472 } 473 474 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds, 475 &ds->ds_phys, dsl_dataset_evict)) != NULL) { 476 bplist_destroy(&ds->ds_pending_deadlist); 477 dsl_deadlist_close(&ds->ds_deadlist); 478 if (ds->ds_prev) 479 dsl_dataset_rele(ds->ds_prev, ds); 480 dsl_dir_rele(ds->ds_dir, ds); 481 mutex_destroy(&ds->ds_lock); 482 mutex_destroy(&ds->ds_opening_lock); 483 mutex_destroy(&ds->ds_sendstream_lock); 484 refcount_destroy(&ds->ds_longholds); 485 kmem_free(ds, sizeof (dsl_dataset_t)); 486 if (err != 0) { 487 dmu_buf_rele(dbuf, tag); 488 return (err); 489 } 490 ds = winner; 491 } else { 492 ds->ds_fsid_guid = 493 unique_insert(ds->ds_phys->ds_fsid_guid); 494 } 495 } 496 ASSERT3P(ds->ds_dbuf, ==, dbuf); 497 ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 498 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 499 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 500 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 501 *dsp = ds; 502 return (0); 503} 504 505int 506dsl_dataset_hold(dsl_pool_t *dp, const char *name, 507 void *tag, dsl_dataset_t **dsp) 508{ 509 dsl_dir_t *dd; 510 const char *snapname; 511 uint64_t obj; 512 int err = 0; 513 514 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 515 if (err != 0) 516 return (err); 517 518 ASSERT(dsl_pool_config_held(dp)); 519 obj = dd->dd_phys->dd_head_dataset_obj; 520 if (obj != 0) 521 err = dsl_dataset_hold_obj(dp, obj, tag, dsp); 522 else 523 err = SET_ERROR(ENOENT); 524 525 /* we may be looking for a snapshot */ 526 if (err == 0 && snapname != NULL) { 527 dsl_dataset_t *ds; 528 529 if (*snapname++ != '@') { 530 dsl_dataset_rele(*dsp, tag); 531 dsl_dir_rele(dd, FTAG); 532 return (SET_ERROR(ENOENT)); 533 } 534 535 dprintf("looking for snapshot '%s'\n", snapname); 536 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 537 if (err == 0) 538 err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 539 dsl_dataset_rele(*dsp, tag); 540 541 if (err == 0) { 542 mutex_enter(&ds->ds_lock); 543 if (ds->ds_snapname[0] == 0) 544 (void) strlcpy(ds->ds_snapname, snapname, 545 sizeof (ds->ds_snapname)); 546 mutex_exit(&ds->ds_lock); 547 *dsp = ds; 548 } 549 } 550 551 dsl_dir_rele(dd, FTAG); 552 return (err); 553} 554 555int 556dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 557 void *tag, dsl_dataset_t **dsp) 558{ 559 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 560 if (err != 0) 561 return (err); 562 if (!dsl_dataset_tryown(*dsp, tag)) { 563 dsl_dataset_rele(*dsp, tag); 564 *dsp = NULL; 565 return (SET_ERROR(EBUSY)); 566 } 567 return (0); 568} 569 570int 571dsl_dataset_own(dsl_pool_t *dp, const char *name, 572 void *tag, dsl_dataset_t **dsp) 573{ 574 int err = dsl_dataset_hold(dp, name, tag, dsp); 575 if (err != 0) 576 return (err); 577 if (!dsl_dataset_tryown(*dsp, tag)) { 578 dsl_dataset_rele(*dsp, tag); 579 return (SET_ERROR(EBUSY)); 580 } 581 return (0); 582} 583 584/* 585 * See the comment above dsl_pool_hold() for details. In summary, a long 586 * hold is used to prevent destruction of a dataset while the pool hold 587 * is dropped, allowing other concurrent operations (e.g. spa_sync()). 588 * 589 * The dataset and pool must be held when this function is called. After it 590 * is called, the pool hold may be released while the dataset is still held 591 * and accessed. 592 */ 593void 594dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 595{ 596 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 597 (void) refcount_add(&ds->ds_longholds, tag); 598} 599 600void 601dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 602{ 603 (void) refcount_remove(&ds->ds_longholds, tag); 604} 605 606/* Return B_TRUE if there are any long holds on this dataset. */ 607boolean_t 608dsl_dataset_long_held(dsl_dataset_t *ds) 609{ 610 return (!refcount_is_zero(&ds->ds_longholds)); 611} 612 613void 614dsl_dataset_name(dsl_dataset_t *ds, char *name) 615{ 616 if (ds == NULL) { 617 (void) strcpy(name, "mos"); 618 } else { 619 dsl_dir_name(ds->ds_dir, name); 620 VERIFY0(dsl_dataset_get_snapname(ds)); 621 if (ds->ds_snapname[0]) { 622 (void) strcat(name, "@"); 623 /* 624 * We use a "recursive" mutex so that we 625 * can call dprintf_ds() with ds_lock held. 626 */ 627 if (!MUTEX_HELD(&ds->ds_lock)) { 628 mutex_enter(&ds->ds_lock); 629 (void) strcat(name, ds->ds_snapname); 630 mutex_exit(&ds->ds_lock); 631 } else { 632 (void) strcat(name, ds->ds_snapname); 633 } 634 } 635 } 636} 637 638void 639dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 640{ 641 dmu_buf_rele(ds->ds_dbuf, tag); 642} 643 644void 645dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 646{ 647 ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL); 648 649 mutex_enter(&ds->ds_lock); 650 ds->ds_owner = NULL; 651 mutex_exit(&ds->ds_lock); 652 dsl_dataset_long_rele(ds, tag); 653 if (ds->ds_dbuf != NULL) 654 dsl_dataset_rele(ds, tag); 655 else 656 dsl_dataset_evict(NULL, ds); 657} 658 659boolean_t 660dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 661{ 662 boolean_t gotit = FALSE; 663 664 mutex_enter(&ds->ds_lock); 665 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 666 ds->ds_owner = tag; 667 dsl_dataset_long_hold(ds, tag); 668 gotit = TRUE; 669 } 670 mutex_exit(&ds->ds_lock); 671 return (gotit); 672} 673 674uint64_t 675dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 676 uint64_t flags, dmu_tx_t *tx) 677{ 678 dsl_pool_t *dp = dd->dd_pool; 679 dmu_buf_t *dbuf; 680 dsl_dataset_phys_t *dsphys; 681 uint64_t dsobj; 682 objset_t *mos = dp->dp_meta_objset; 683 684 if (origin == NULL) 685 origin = dp->dp_origin_snap; 686 687 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 688 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 689 ASSERT(dmu_tx_is_syncing(tx)); 690 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 691 692 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 693 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 694 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 695 dmu_buf_will_dirty(dbuf, tx); 696 dsphys = dbuf->db_data; 697 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 698 dsphys->ds_dir_obj = dd->dd_object; 699 dsphys->ds_flags = flags; 700 dsphys->ds_fsid_guid = unique_create(); 701 do { 702 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 703 sizeof (dsphys->ds_guid)); 704 } while (dsphys->ds_guid == 0); 705 dsphys->ds_snapnames_zapobj = 706 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 707 DMU_OT_NONE, 0, tx); 708 dsphys->ds_creation_time = gethrestime_sec(); 709 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 710 711 if (origin == NULL) { 712 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 713 } else { 714 dsl_dataset_t *ohds; /* head of the origin snapshot */ 715 716 dsphys->ds_prev_snap_obj = origin->ds_object; 717 dsphys->ds_prev_snap_txg = 718 origin->ds_phys->ds_creation_txg; 719 dsphys->ds_referenced_bytes = 720 origin->ds_phys->ds_referenced_bytes; 721 dsphys->ds_compressed_bytes = 722 origin->ds_phys->ds_compressed_bytes; 723 dsphys->ds_uncompressed_bytes = 724 origin->ds_phys->ds_uncompressed_bytes; 725 dsphys->ds_bp = origin->ds_phys->ds_bp; 726 727 /* 728 * Inherit flags that describe the dataset's contents 729 * (INCONSISTENT) or properties (Case Insensitive). 730 */ 731 dsphys->ds_flags |= origin->ds_phys->ds_flags & 732 (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 733 734 if (origin->ds_large_blocks) 735 dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx); 736 737 dmu_buf_will_dirty(origin->ds_dbuf, tx); 738 origin->ds_phys->ds_num_children++; 739 740 VERIFY0(dsl_dataset_hold_obj(dp, 741 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds)); 742 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 743 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 744 dsl_dataset_rele(ohds, FTAG); 745 746 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 747 if (origin->ds_phys->ds_next_clones_obj == 0) { 748 origin->ds_phys->ds_next_clones_obj = 749 zap_create(mos, 750 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 751 } 752 VERIFY0(zap_add_int(mos, 753 origin->ds_phys->ds_next_clones_obj, dsobj, tx)); 754 } 755 756 dmu_buf_will_dirty(dd->dd_dbuf, tx); 757 dd->dd_phys->dd_origin_obj = origin->ds_object; 758 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 759 if (origin->ds_dir->dd_phys->dd_clones == 0) { 760 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 761 origin->ds_dir->dd_phys->dd_clones = 762 zap_create(mos, 763 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 764 } 765 VERIFY0(zap_add_int(mos, 766 origin->ds_dir->dd_phys->dd_clones, dsobj, tx)); 767 } 768 } 769 770 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 771 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 772 773 dmu_buf_rele(dbuf, FTAG); 774 775 dmu_buf_will_dirty(dd->dd_dbuf, tx); 776 dd->dd_phys->dd_head_dataset_obj = dsobj; 777 778 return (dsobj); 779} 780 781static void 782dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 783{ 784 objset_t *os; 785 786 VERIFY0(dmu_objset_from_ds(ds, &os)); 787 bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 788 dsl_dataset_dirty(ds, tx); 789} 790 791uint64_t 792dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 793 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 794{ 795 dsl_pool_t *dp = pdd->dd_pool; 796 uint64_t dsobj, ddobj; 797 dsl_dir_t *dd; 798 799 ASSERT(dmu_tx_is_syncing(tx)); 800 ASSERT(lastname[0] != '@'); 801 802 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 803 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 804 805 dsobj = dsl_dataset_create_sync_dd(dd, origin, 806 flags & ~DS_CREATE_FLAG_NODIRTY, tx); 807 808 dsl_deleg_set_create_perms(dd, tx, cr); 809 810 /* 811 * Since we're creating a new node we know it's a leaf, so we can 812 * initialize the counts if the limit feature is active. 813 */ 814 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 815 uint64_t cnt = 0; 816 objset_t *os = dd->dd_pool->dp_meta_objset; 817 818 dsl_dir_zapify(dd, tx); 819 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 820 sizeof (cnt), 1, &cnt, tx)); 821 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 822 sizeof (cnt), 1, &cnt, tx)); 823 } 824 825 dsl_dir_rele(dd, FTAG); 826 827 /* 828 * If we are creating a clone, make sure we zero out any stale 829 * data from the origin snapshots zil header. 830 */ 831 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 832 dsl_dataset_t *ds; 833 834 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 835 dsl_dataset_zero_zil(ds, tx); 836 dsl_dataset_rele(ds, FTAG); 837 } 838 839 return (dsobj); 840} 841 842#ifdef __FreeBSD__ 843/* FreeBSD ioctl compat begin */ 844struct destroyarg { 845 nvlist_t *nvl; 846 const char *snapname; 847}; 848 849static int 850dsl_check_snap_cb(const char *name, void *arg) 851{ 852 struct destroyarg *da = arg; 853 dsl_dataset_t *ds; 854 char *dsname; 855 856 dsname = kmem_asprintf("%s@%s", name, da->snapname); 857 fnvlist_add_boolean(da->nvl, dsname); 858 kmem_free(dsname, strlen(dsname) + 1); 859 860 return (0); 861} 862 863int 864dmu_get_recursive_snaps_nvl(char *fsname, const char *snapname, 865 nvlist_t *snaps) 866{ 867 struct destroyarg *da; 868 int err; 869 870 da = kmem_zalloc(sizeof (struct destroyarg), KM_SLEEP); 871 da->nvl = snaps; 872 da->snapname = snapname; 873 err = dmu_objset_find(fsname, dsl_check_snap_cb, da, 874 DS_FIND_CHILDREN); 875 kmem_free(da, sizeof (struct destroyarg)); 876 877 return (err); 878} 879/* FreeBSD ioctl compat end */ 880#endif /* __FreeBSD__ */ 881 882/* 883 * The unique space in the head dataset can be calculated by subtracting 884 * the space used in the most recent snapshot, that is still being used 885 * in this file system, from the space currently in use. To figure out 886 * the space in the most recent snapshot still in use, we need to take 887 * the total space used in the snapshot and subtract out the space that 888 * has been freed up since the snapshot was taken. 889 */ 890void 891dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 892{ 893 uint64_t mrs_used; 894 uint64_t dlused, dlcomp, dluncomp; 895 896 ASSERT(!dsl_dataset_is_snapshot(ds)); 897 898 if (ds->ds_phys->ds_prev_snap_obj != 0) 899 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes; 900 else 901 mrs_used = 0; 902 903 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 904 905 ASSERT3U(dlused, <=, mrs_used); 906 ds->ds_phys->ds_unique_bytes = 907 ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused); 908 909 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 910 SPA_VERSION_UNIQUE_ACCURATE) 911 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 912} 913 914void 915dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 916 dmu_tx_t *tx) 917{ 918 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 919 uint64_t count; 920 int err; 921 922 ASSERT(ds->ds_phys->ds_num_children >= 2); 923 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 924 /* 925 * The err should not be ENOENT, but a bug in a previous version 926 * of the code could cause upgrade_clones_cb() to not set 927 * ds_next_snap_obj when it should, leading to a missing entry. 928 * If we knew that the pool was created after 929 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 930 * ENOENT. However, at least we can check that we don't have 931 * too many entries in the next_clones_obj even after failing to 932 * remove this one. 933 */ 934 if (err != ENOENT) 935 VERIFY0(err); 936 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 937 &count)); 938 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 939} 940 941 942blkptr_t * 943dsl_dataset_get_blkptr(dsl_dataset_t *ds) 944{ 945 return (&ds->ds_phys->ds_bp); 946} 947 948void 949dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 950{ 951 ASSERT(dmu_tx_is_syncing(tx)); 952 /* If it's the meta-objset, set dp_meta_rootbp */ 953 if (ds == NULL) { 954 tx->tx_pool->dp_meta_rootbp = *bp; 955 } else { 956 dmu_buf_will_dirty(ds->ds_dbuf, tx); 957 ds->ds_phys->ds_bp = *bp; 958 } 959} 960 961spa_t * 962dsl_dataset_get_spa(dsl_dataset_t *ds) 963{ 964 return (ds->ds_dir->dd_pool->dp_spa); 965} 966 967void 968dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 969{ 970 dsl_pool_t *dp; 971 972 if (ds == NULL) /* this is the meta-objset */ 973 return; 974 975 ASSERT(ds->ds_objset != NULL); 976 977 if (ds->ds_phys->ds_next_snap_obj != 0) 978 panic("dirtying snapshot!"); 979 980 dp = ds->ds_dir->dd_pool; 981 982 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 983 /* up the hold count until we can be written out */ 984 dmu_buf_add_ref(ds->ds_dbuf, ds); 985 } 986} 987 988boolean_t 989dsl_dataset_is_dirty(dsl_dataset_t *ds) 990{ 991 for (int t = 0; t < TXG_SIZE; t++) { 992 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 993 ds, t)) 994 return (B_TRUE); 995 } 996 return (B_FALSE); 997} 998 999static int 1000dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1001{ 1002 uint64_t asize; 1003 1004 if (!dmu_tx_is_syncing(tx)) 1005 return (0); 1006 1007 /* 1008 * If there's an fs-only reservation, any blocks that might become 1009 * owned by the snapshot dataset must be accommodated by space 1010 * outside of the reservation. 1011 */ 1012 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1013 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 1014 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1015 return (SET_ERROR(ENOSPC)); 1016 1017 /* 1018 * Propagate any reserved space for this snapshot to other 1019 * snapshot checks in this sync group. 1020 */ 1021 if (asize > 0) 1022 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 1023 1024 return (0); 1025} 1026 1027typedef struct dsl_dataset_snapshot_arg { 1028 nvlist_t *ddsa_snaps; 1029 nvlist_t *ddsa_props; 1030 nvlist_t *ddsa_errors; 1031 cred_t *ddsa_cr; 1032} dsl_dataset_snapshot_arg_t; 1033 1034int 1035dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1036 dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 1037{ 1038 int error; 1039 uint64_t value; 1040 1041 ds->ds_trysnap_txg = tx->tx_txg; 1042 1043 if (!dmu_tx_is_syncing(tx)) 1044 return (0); 1045 1046 /* 1047 * We don't allow multiple snapshots of the same txg. If there 1048 * is already one, try again. 1049 */ 1050 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 1051 return (SET_ERROR(EAGAIN)); 1052 1053 /* 1054 * Check for conflicting snapshot name. 1055 */ 1056 error = dsl_dataset_snap_lookup(ds, snapname, &value); 1057 if (error == 0) 1058 return (SET_ERROR(EEXIST)); 1059 if (error != ENOENT) 1060 return (error); 1061 1062 /* 1063 * We don't allow taking snapshots of inconsistent datasets, such as 1064 * those into which we are currently receiving. However, if we are 1065 * creating this snapshot as part of a receive, this check will be 1066 * executed atomically with respect to the completion of the receive 1067 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1068 * case we ignore this, knowing it will be fixed up for us shortly in 1069 * dmu_recv_end_sync(). 1070 */ 1071 if (!recv && DS_IS_INCONSISTENT(ds)) 1072 return (SET_ERROR(EBUSY)); 1073 1074 /* 1075 * Skip the check for temporary snapshots or if we have already checked 1076 * the counts in dsl_dataset_snapshot_check. This means we really only 1077 * check the count here when we're receiving a stream. 1078 */ 1079 if (cnt != 0 && cr != NULL) { 1080 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1081 ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1082 if (error != 0) 1083 return (error); 1084 } 1085 1086 error = dsl_dataset_snapshot_reserve_space(ds, tx); 1087 if (error != 0) 1088 return (error); 1089 1090 return (0); 1091} 1092 1093static int 1094dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1095{ 1096 dsl_dataset_snapshot_arg_t *ddsa = arg; 1097 dsl_pool_t *dp = dmu_tx_pool(tx); 1098 nvpair_t *pair; 1099 int rv = 0; 1100 1101 /* 1102 * Pre-compute how many total new snapshots will be created for each 1103 * level in the tree and below. This is needed for validating the 1104 * snapshot limit when either taking a recursive snapshot or when 1105 * taking multiple snapshots. 1106 * 1107 * The problem is that the counts are not actually adjusted when 1108 * we are checking, only when we finally sync. For a single snapshot, 1109 * this is easy, the count will increase by 1 at each node up the tree, 1110 * but its more complicated for the recursive/multiple snapshot case. 1111 * 1112 * The dsl_fs_ss_limit_check function does recursively check the count 1113 * at each level up the tree but since it is validating each snapshot 1114 * independently we need to be sure that we are validating the complete 1115 * count for the entire set of snapshots. We do this by rolling up the 1116 * counts for each component of the name into an nvlist and then 1117 * checking each of those cases with the aggregated count. 1118 * 1119 * This approach properly handles not only the recursive snapshot 1120 * case (where we get all of those on the ddsa_snaps list) but also 1121 * the sibling case (e.g. snapshot a/b and a/c so that we will also 1122 * validate the limit on 'a' using a count of 2). 1123 * 1124 * We validate the snapshot names in the third loop and only report 1125 * name errors once. 1126 */ 1127 if (dmu_tx_is_syncing(tx)) { 1128 nvlist_t *cnt_track = NULL; 1129 cnt_track = fnvlist_alloc(); 1130 1131 /* Rollup aggregated counts into the cnt_track list */ 1132 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1133 pair != NULL; 1134 pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1135 char *pdelim; 1136 uint64_t val; 1137 char nm[MAXPATHLEN]; 1138 1139 (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1140 pdelim = strchr(nm, '@'); 1141 if (pdelim == NULL) 1142 continue; 1143 *pdelim = '\0'; 1144 1145 do { 1146 if (nvlist_lookup_uint64(cnt_track, nm, 1147 &val) == 0) { 1148 /* update existing entry */ 1149 fnvlist_add_uint64(cnt_track, nm, 1150 val + 1); 1151 } else { 1152 /* add to list */ 1153 fnvlist_add_uint64(cnt_track, nm, 1); 1154 } 1155 1156 pdelim = strrchr(nm, '/'); 1157 if (pdelim != NULL) 1158 *pdelim = '\0'; 1159 } while (pdelim != NULL); 1160 } 1161 1162 /* Check aggregated counts at each level */ 1163 for (pair = nvlist_next_nvpair(cnt_track, NULL); 1164 pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1165 int error = 0; 1166 char *name; 1167 uint64_t cnt = 0; 1168 dsl_dataset_t *ds; 1169 1170 name = nvpair_name(pair); 1171 cnt = fnvpair_value_uint64(pair); 1172 ASSERT(cnt > 0); 1173 1174 error = dsl_dataset_hold(dp, name, FTAG, &ds); 1175 if (error == 0) { 1176 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1177 ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1178 ddsa->ddsa_cr); 1179 dsl_dataset_rele(ds, FTAG); 1180 } 1181 1182 if (error != 0) { 1183 if (ddsa->ddsa_errors != NULL) 1184 fnvlist_add_int32(ddsa->ddsa_errors, 1185 name, error); 1186 rv = error; 1187 /* only report one error for this check */ 1188 break; 1189 } 1190 } 1191 nvlist_free(cnt_track); 1192 } 1193 1194 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1195 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1196 int error = 0; 1197 dsl_dataset_t *ds; 1198 char *name, *atp; 1199 char dsname[MAXNAMELEN]; 1200 1201 name = nvpair_name(pair); 1202 if (strlen(name) >= MAXNAMELEN) 1203 error = SET_ERROR(ENAMETOOLONG); 1204 if (error == 0) { 1205 atp = strchr(name, '@'); 1206 if (atp == NULL) 1207 error = SET_ERROR(EINVAL); 1208 if (error == 0) 1209 (void) strlcpy(dsname, name, atp - name + 1); 1210 } 1211 if (error == 0) 1212 error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 1213 if (error == 0) { 1214 /* passing 0/NULL skips dsl_fs_ss_limit_check */ 1215 error = dsl_dataset_snapshot_check_impl(ds, 1216 atp + 1, tx, B_FALSE, 0, NULL); 1217 dsl_dataset_rele(ds, FTAG); 1218 } 1219 1220 if (error != 0) { 1221 if (ddsa->ddsa_errors != NULL) { 1222 fnvlist_add_int32(ddsa->ddsa_errors, 1223 name, error); 1224 } 1225 rv = error; 1226 } 1227 } 1228 1229 return (rv); 1230} 1231 1232void 1233dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 1234 dmu_tx_t *tx) 1235{ 1236 static zil_header_t zero_zil; 1237 1238 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1239 dmu_buf_t *dbuf; 1240 dsl_dataset_phys_t *dsphys; 1241 uint64_t dsobj, crtxg; 1242 objset_t *mos = dp->dp_meta_objset; 1243 objset_t *os; 1244 1245 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1246 1247 /* 1248 * If we are on an old pool, the zil must not be active, in which 1249 * case it will be zeroed. Usually zil_suspend() accomplishes this. 1250 */ 1251 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 1252 dmu_objset_from_ds(ds, &os) != 0 || 1253 bcmp(&os->os_phys->os_zil_header, &zero_zil, 1254 sizeof (zero_zil)) == 0); 1255 1256 dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1257 1258 /* 1259 * The origin's ds_creation_txg has to be < TXG_INITIAL 1260 */ 1261 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1262 crtxg = 1; 1263 else 1264 crtxg = tx->tx_txg; 1265 1266 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1267 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 1268 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1269 dmu_buf_will_dirty(dbuf, tx); 1270 dsphys = dbuf->db_data; 1271 bzero(dsphys, sizeof (dsl_dataset_phys_t)); 1272 dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1273 dsphys->ds_fsid_guid = unique_create(); 1274 do { 1275 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1276 sizeof (dsphys->ds_guid)); 1277 } while (dsphys->ds_guid == 0); 1278 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1279 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1280 dsphys->ds_next_snap_obj = ds->ds_object; 1281 dsphys->ds_num_children = 1; 1282 dsphys->ds_creation_time = gethrestime_sec(); 1283 dsphys->ds_creation_txg = crtxg; 1284 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1285 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes; 1286 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1287 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 1288 dsphys->ds_flags = ds->ds_phys->ds_flags; 1289 dsphys->ds_bp = ds->ds_phys->ds_bp; 1290 dmu_buf_rele(dbuf, FTAG); 1291 1292 if (ds->ds_large_blocks) 1293 dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx); 1294 1295 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 1296 if (ds->ds_prev) { 1297 uint64_t next_clones_obj = 1298 ds->ds_prev->ds_phys->ds_next_clones_obj; 1299 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1300 ds->ds_object || 1301 ds->ds_prev->ds_phys->ds_num_children > 1); 1302 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 1303 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1304 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1305 ds->ds_prev->ds_phys->ds_creation_txg); 1306 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1307 } else if (next_clones_obj != 0) { 1308 dsl_dataset_remove_from_next_clones(ds->ds_prev, 1309 dsphys->ds_next_snap_obj, tx); 1310 VERIFY0(zap_add_int(mos, 1311 next_clones_obj, dsobj, tx)); 1312 } 1313 } 1314 1315 /* 1316 * If we have a reference-reservation on this dataset, we will 1317 * need to increase the amount of refreservation being charged 1318 * since our unique space is going to zero. 1319 */ 1320 if (ds->ds_reserved) { 1321 int64_t delta; 1322 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1323 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 1324 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 1325 delta, 0, 0, tx); 1326 } 1327 1328 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1329 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist, 1330 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx); 1331 dsl_deadlist_close(&ds->ds_deadlist); 1332 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 1333 dsl_deadlist_add_key(&ds->ds_deadlist, 1334 ds->ds_phys->ds_prev_snap_txg, tx); 1335 1336 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1337 ds->ds_phys->ds_prev_snap_obj = dsobj; 1338 ds->ds_phys->ds_prev_snap_txg = crtxg; 1339 ds->ds_phys->ds_unique_bytes = 0; 1340 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1341 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1342 1343 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1344 snapname, 8, 1, &dsobj, tx)); 1345 1346 if (ds->ds_prev) 1347 dsl_dataset_rele(ds->ds_prev, ds); 1348 VERIFY0(dsl_dataset_hold_obj(dp, 1349 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 1350 1351 dsl_scan_ds_snapshotted(ds, tx); 1352 1353 dsl_dir_snap_cmtime_update(ds->ds_dir); 1354 1355 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1356} 1357 1358static void 1359dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1360{ 1361 dsl_dataset_snapshot_arg_t *ddsa = arg; 1362 dsl_pool_t *dp = dmu_tx_pool(tx); 1363 nvpair_t *pair; 1364 1365 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1366 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1367 dsl_dataset_t *ds; 1368 char *name, *atp; 1369 char dsname[MAXNAMELEN]; 1370 1371 name = nvpair_name(pair); 1372 atp = strchr(name, '@'); 1373 (void) strlcpy(dsname, name, atp - name + 1); 1374 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 1375 1376 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 1377 if (ddsa->ddsa_props != NULL) { 1378 dsl_props_set_sync_impl(ds->ds_prev, 1379 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 1380 } 1381 dsl_dataset_rele(ds, FTAG); 1382 } 1383} 1384 1385/* 1386 * The snapshots must all be in the same pool. 1387 * All-or-nothing: if there are any failures, nothing will be modified. 1388 */ 1389int 1390dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 1391{ 1392 dsl_dataset_snapshot_arg_t ddsa; 1393 nvpair_t *pair; 1394 boolean_t needsuspend; 1395 int error; 1396 spa_t *spa; 1397 char *firstname; 1398 nvlist_t *suspended = NULL; 1399 1400 pair = nvlist_next_nvpair(snaps, NULL); 1401 if (pair == NULL) 1402 return (0); 1403 firstname = nvpair_name(pair); 1404 1405 error = spa_open(firstname, &spa, FTAG); 1406 if (error != 0) 1407 return (error); 1408 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 1409 spa_close(spa, FTAG); 1410 1411 if (needsuspend) { 1412 suspended = fnvlist_alloc(); 1413 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 1414 pair = nvlist_next_nvpair(snaps, pair)) { 1415 char fsname[MAXNAMELEN]; 1416 char *snapname = nvpair_name(pair); 1417 char *atp; 1418 void *cookie; 1419 1420 atp = strchr(snapname, '@'); 1421 if (atp == NULL) { 1422 error = SET_ERROR(EINVAL); 1423 break; 1424 } 1425 (void) strlcpy(fsname, snapname, atp - snapname + 1); 1426 1427 error = zil_suspend(fsname, &cookie); 1428 if (error != 0) 1429 break; 1430 fnvlist_add_uint64(suspended, fsname, 1431 (uintptr_t)cookie); 1432 } 1433 } 1434 1435 ddsa.ddsa_snaps = snaps; 1436 ddsa.ddsa_props = props; 1437 ddsa.ddsa_errors = errors; 1438 ddsa.ddsa_cr = CRED(); 1439 1440 if (error == 0) { 1441 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 1442 dsl_dataset_snapshot_sync, &ddsa, 1443 fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 1444 } 1445 1446 if (suspended != NULL) { 1447 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 1448 pair = nvlist_next_nvpair(suspended, pair)) { 1449 zil_resume((void *)(uintptr_t) 1450 fnvpair_value_uint64(pair)); 1451 } 1452 fnvlist_free(suspended); 1453 } 1454 1455#ifdef __FreeBSD__ 1456#ifdef _KERNEL 1457 if (error == 0) { 1458 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 1459 pair = nvlist_next_nvpair(snaps, pair)) { 1460 char *snapname = nvpair_name(pair); 1461 zvol_create_minors(snapname); 1462 } 1463 } 1464#endif 1465#endif 1466 return (error); 1467} 1468 1469typedef struct dsl_dataset_snapshot_tmp_arg { 1470 const char *ddsta_fsname; 1471 const char *ddsta_snapname; 1472 minor_t ddsta_cleanup_minor; 1473 const char *ddsta_htag; 1474} dsl_dataset_snapshot_tmp_arg_t; 1475 1476static int 1477dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 1478{ 1479 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 1480 dsl_pool_t *dp = dmu_tx_pool(tx); 1481 dsl_dataset_t *ds; 1482 int error; 1483 1484 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 1485 if (error != 0) 1486 return (error); 1487 1488 /* NULL cred means no limit check for tmp snapshot */ 1489 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1490 tx, B_FALSE, 0, NULL); 1491 if (error != 0) { 1492 dsl_dataset_rele(ds, FTAG); 1493 return (error); 1494 } 1495 1496 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 1497 dsl_dataset_rele(ds, FTAG); 1498 return (SET_ERROR(ENOTSUP)); 1499 } 1500 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 1501 B_TRUE, tx); 1502 if (error != 0) { 1503 dsl_dataset_rele(ds, FTAG); 1504 return (error); 1505 } 1506 1507 dsl_dataset_rele(ds, FTAG); 1508 return (0); 1509} 1510 1511static void 1512dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 1513{ 1514 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 1515 dsl_pool_t *dp = dmu_tx_pool(tx); 1516 dsl_dataset_t *ds; 1517 1518 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 1519 1520 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 1521 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 1522 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 1523 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 1524 1525 dsl_dataset_rele(ds, FTAG); 1526} 1527 1528int 1529dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 1530 minor_t cleanup_minor, const char *htag) 1531{ 1532 dsl_dataset_snapshot_tmp_arg_t ddsta; 1533 int error; 1534 spa_t *spa; 1535 boolean_t needsuspend; 1536 void *cookie; 1537 1538 ddsta.ddsta_fsname = fsname; 1539 ddsta.ddsta_snapname = snapname; 1540 ddsta.ddsta_cleanup_minor = cleanup_minor; 1541 ddsta.ddsta_htag = htag; 1542 1543 error = spa_open(fsname, &spa, FTAG); 1544 if (error != 0) 1545 return (error); 1546 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 1547 spa_close(spa, FTAG); 1548 1549 if (needsuspend) { 1550 error = zil_suspend(fsname, &cookie); 1551 if (error != 0) 1552 return (error); 1553 } 1554 1555 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 1556 dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 1557 1558 if (needsuspend) 1559 zil_resume(cookie); 1560 return (error); 1561} 1562 1563 1564void 1565dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1566{ 1567 ASSERT(dmu_tx_is_syncing(tx)); 1568 ASSERT(ds->ds_objset != NULL); 1569 ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1570 1571 /* 1572 * in case we had to change ds_fsid_guid when we opened it, 1573 * sync it out now. 1574 */ 1575 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1576 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 1577 1578 dmu_objset_sync(ds->ds_objset, zio, tx); 1579 1580 if (ds->ds_need_large_blocks && !ds->ds_large_blocks) { 1581 dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx); 1582 ds->ds_large_blocks = B_TRUE; 1583 } 1584} 1585 1586static void 1587get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 1588{ 1589 uint64_t count = 0; 1590 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1591 zap_cursor_t zc; 1592 zap_attribute_t za; 1593 nvlist_t *propval = fnvlist_alloc(); 1594 nvlist_t *val = fnvlist_alloc(); 1595 1596 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 1597 1598 /* 1599 * There may be missing entries in ds_next_clones_obj 1600 * due to a bug in a previous version of the code. 1601 * Only trust it if it has the right number of entries. 1602 */ 1603 if (ds->ds_phys->ds_next_clones_obj != 0) { 1604 VERIFY0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 1605 &count)); 1606 } 1607 if (count != ds->ds_phys->ds_num_children - 1) 1608 goto fail; 1609 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj); 1610 zap_cursor_retrieve(&zc, &za) == 0; 1611 zap_cursor_advance(&zc)) { 1612 dsl_dataset_t *clone; 1613 char buf[ZFS_MAXNAMELEN]; 1614 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 1615 za.za_first_integer, FTAG, &clone)); 1616 dsl_dir_name(clone->ds_dir, buf); 1617 fnvlist_add_boolean(val, buf); 1618 dsl_dataset_rele(clone, FTAG); 1619 } 1620 zap_cursor_fini(&zc); 1621 fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 1622 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 1623fail: 1624 nvlist_free(val); 1625 nvlist_free(propval); 1626} 1627 1628void 1629dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1630{ 1631 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1632 uint64_t refd, avail, uobjs, aobjs, ratio; 1633 1634 ASSERT(dsl_pool_config_held(dp)); 1635 1636 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 1637 (ds->ds_phys->ds_uncompressed_bytes * 100 / 1638 ds->ds_phys->ds_compressed_bytes); 1639 1640 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 1641 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 1642 ds->ds_phys->ds_uncompressed_bytes); 1643 1644 if (dsl_dataset_is_snapshot(ds)) { 1645 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 1646 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1647 ds->ds_phys->ds_unique_bytes); 1648 get_clones_stat(ds, nv); 1649 } else { 1650 if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 1651 char buf[MAXNAMELEN]; 1652 dsl_dataset_name(ds->ds_prev, buf); 1653 dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf); 1654 } 1655 1656 dsl_dir_stats(ds->ds_dir, nv); 1657 } 1658 1659 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1660 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1661 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1662 1663 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1664 ds->ds_phys->ds_creation_time); 1665 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1666 ds->ds_phys->ds_creation_txg); 1667 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1668 ds->ds_quota); 1669 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1670 ds->ds_reserved); 1671 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1672 ds->ds_phys->ds_guid); 1673 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 1674 ds->ds_phys->ds_unique_bytes); 1675 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 1676 ds->ds_object); 1677 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 1678 ds->ds_userrefs); 1679 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1680 DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1681 1682 if (ds->ds_phys->ds_prev_snap_obj != 0) { 1683 uint64_t written, comp, uncomp; 1684 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1685 dsl_dataset_t *prev; 1686 1687 int err = dsl_dataset_hold_obj(dp, 1688 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 1689 if (err == 0) { 1690 err = dsl_dataset_space_written(prev, ds, &written, 1691 &comp, &uncomp); 1692 dsl_dataset_rele(prev, FTAG); 1693 if (err == 0) { 1694 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 1695 written); 1696 } 1697 } 1698 } 1699} 1700 1701void 1702dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1703{ 1704 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1705 ASSERT(dsl_pool_config_held(dp)); 1706 1707 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1708 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 1709 stat->dds_guid = ds->ds_phys->ds_guid; 1710 stat->dds_origin[0] = '\0'; 1711 if (dsl_dataset_is_snapshot(ds)) { 1712 stat->dds_is_snapshot = B_TRUE; 1713 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1714 } else { 1715 stat->dds_is_snapshot = B_FALSE; 1716 stat->dds_num_clones = 0; 1717 1718 if (dsl_dir_is_clone(ds->ds_dir)) { 1719 dsl_dataset_t *ods; 1720 1721 VERIFY0(dsl_dataset_hold_obj(dp, 1722 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 1723 dsl_dataset_name(ods, stat->dds_origin); 1724 dsl_dataset_rele(ods, FTAG); 1725 } 1726 } 1727} 1728 1729uint64_t 1730dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1731{ 1732 return (ds->ds_fsid_guid); 1733} 1734 1735void 1736dsl_dataset_space(dsl_dataset_t *ds, 1737 uint64_t *refdbytesp, uint64_t *availbytesp, 1738 uint64_t *usedobjsp, uint64_t *availobjsp) 1739{ 1740 *refdbytesp = ds->ds_phys->ds_referenced_bytes; 1741 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1742 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 1743 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 1744 if (ds->ds_quota != 0) { 1745 /* 1746 * Adjust available bytes according to refquota 1747 */ 1748 if (*refdbytesp < ds->ds_quota) 1749 *availbytesp = MIN(*availbytesp, 1750 ds->ds_quota - *refdbytesp); 1751 else 1752 *availbytesp = 0; 1753 } 1754 *usedobjsp = BP_GET_FILL(&ds->ds_phys->ds_bp); 1755 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1756} 1757 1758boolean_t 1759dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1760{ 1761 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1762 1763 ASSERT(dsl_pool_config_held(dp)); 1764 if (snap == NULL) 1765 return (B_FALSE); 1766 if (ds->ds_phys->ds_bp.blk_birth > 1767 snap->ds_phys->ds_creation_txg) { 1768 objset_t *os, *os_snap; 1769 /* 1770 * It may be that only the ZIL differs, because it was 1771 * reset in the head. Don't count that as being 1772 * modified. 1773 */ 1774 if (dmu_objset_from_ds(ds, &os) != 0) 1775 return (B_TRUE); 1776 if (dmu_objset_from_ds(snap, &os_snap) != 0) 1777 return (B_TRUE); 1778 return (bcmp(&os->os_phys->os_meta_dnode, 1779 &os_snap->os_phys->os_meta_dnode, 1780 sizeof (os->os_phys->os_meta_dnode)) != 0); 1781 } 1782 return (B_FALSE); 1783} 1784 1785typedef struct dsl_dataset_rename_snapshot_arg { 1786 const char *ddrsa_fsname; 1787 const char *ddrsa_oldsnapname; 1788 const char *ddrsa_newsnapname; 1789 boolean_t ddrsa_recursive; 1790 dmu_tx_t *ddrsa_tx; 1791} dsl_dataset_rename_snapshot_arg_t; 1792 1793/* ARGSUSED */ 1794static int 1795dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 1796 dsl_dataset_t *hds, void *arg) 1797{ 1798 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 1799 int error; 1800 uint64_t val; 1801 1802 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 1803 if (error != 0) { 1804 /* ignore nonexistent snapshots */ 1805 return (error == ENOENT ? 0 : error); 1806 } 1807 1808 /* new name should not exist */ 1809 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 1810 if (error == 0) 1811 error = SET_ERROR(EEXIST); 1812 else if (error == ENOENT) 1813 error = 0; 1814 1815 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 1816 if (dsl_dir_namelen(hds->ds_dir) + 1 + 1817 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1818 error = SET_ERROR(ENAMETOOLONG); 1819 1820 return (error); 1821} 1822 1823static int 1824dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 1825{ 1826 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 1827 dsl_pool_t *dp = dmu_tx_pool(tx); 1828 dsl_dataset_t *hds; 1829 int error; 1830 1831 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 1832 if (error != 0) 1833 return (error); 1834 1835 if (ddrsa->ddrsa_recursive) { 1836 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 1837 dsl_dataset_rename_snapshot_check_impl, ddrsa, 1838 DS_FIND_CHILDREN); 1839 } else { 1840 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 1841 } 1842 dsl_dataset_rele(hds, FTAG); 1843 return (error); 1844} 1845 1846static int 1847dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 1848 dsl_dataset_t *hds, void *arg) 1849{ 1850#ifdef __FreeBSD__ 1851#ifdef _KERNEL 1852 char *oldname, *newname; 1853#endif 1854#endif 1855 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 1856 dsl_dataset_t *ds; 1857 uint64_t val; 1858 dmu_tx_t *tx = ddrsa->ddrsa_tx; 1859 int error; 1860 1861 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 1862 ASSERT(error == 0 || error == ENOENT); 1863 if (error == ENOENT) { 1864 /* ignore nonexistent snapshots */ 1865 return (0); 1866 } 1867 1868 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 1869 1870 /* log before we change the name */ 1871 spa_history_log_internal_ds(ds, "rename", tx, 1872 "-> @%s", ddrsa->ddrsa_newsnapname); 1873 1874 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 1875 B_FALSE)); 1876 mutex_enter(&ds->ds_lock); 1877 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 1878 mutex_exit(&ds->ds_lock); 1879 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj, 1880 ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1881 1882#ifdef __FreeBSD__ 1883#ifdef _KERNEL 1884 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1885 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1886 snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname, 1887 ddrsa->ddrsa_oldsnapname); 1888 snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname, 1889 ddrsa->ddrsa_newsnapname); 1890 zfsvfs_update_fromname(oldname, newname); 1891 zvol_rename_minors(oldname, newname); 1892 kmem_free(newname, MAXPATHLEN); 1893 kmem_free(oldname, MAXPATHLEN); 1894#endif 1895#endif 1896 dsl_dataset_rele(ds, FTAG); 1897 1898 return (0); 1899} 1900 1901static void 1902dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1903{ 1904 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 1905 dsl_pool_t *dp = dmu_tx_pool(tx); 1906 dsl_dataset_t *hds; 1907 1908 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 1909 ddrsa->ddrsa_tx = tx; 1910 if (ddrsa->ddrsa_recursive) { 1911 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 1912 dsl_dataset_rename_snapshot_sync_impl, ddrsa, 1913 DS_FIND_CHILDREN)); 1914 } else { 1915 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1916 } 1917 dsl_dataset_rele(hds, FTAG); 1918} 1919 1920int 1921dsl_dataset_rename_snapshot(const char *fsname, 1922 const char *oldsnapname, const char *newsnapname, boolean_t recursive) 1923{ 1924 dsl_dataset_rename_snapshot_arg_t ddrsa; 1925 1926 ddrsa.ddrsa_fsname = fsname; 1927 ddrsa.ddrsa_oldsnapname = oldsnapname; 1928 ddrsa.ddrsa_newsnapname = newsnapname; 1929 ddrsa.ddrsa_recursive = recursive; 1930 1931 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 1932 dsl_dataset_rename_snapshot_sync, &ddrsa, 1933 1, ZFS_SPACE_CHECK_RESERVED)); 1934} 1935 1936/* 1937 * If we're doing an ownership handoff, we need to make sure that there is 1938 * only one long hold on the dataset. We're not allowed to change anything here 1939 * so we don't permanently release the long hold or regular hold here. We want 1940 * to do this only when syncing to avoid the dataset unexpectedly going away 1941 * when we release the long hold. 1942 */ 1943static int 1944dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 1945{ 1946 boolean_t held; 1947 1948 if (!dmu_tx_is_syncing(tx)) 1949 return (0); 1950 1951 if (owner != NULL) { 1952 VERIFY3P(ds->ds_owner, ==, owner); 1953 dsl_dataset_long_rele(ds, owner); 1954 } 1955 1956 held = dsl_dataset_long_held(ds); 1957 1958 if (owner != NULL) 1959 dsl_dataset_long_hold(ds, owner); 1960 1961 if (held) 1962 return (SET_ERROR(EBUSY)); 1963 1964 return (0); 1965} 1966 1967typedef struct dsl_dataset_rollback_arg { 1968 const char *ddra_fsname; 1969 void *ddra_owner; 1970 nvlist_t *ddra_result; 1971} dsl_dataset_rollback_arg_t; 1972 1973static int 1974dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1975{ 1976 dsl_dataset_rollback_arg_t *ddra = arg; 1977 dsl_pool_t *dp = dmu_tx_pool(tx); 1978 dsl_dataset_t *ds; 1979 int64_t unused_refres_delta; 1980 int error; 1981 1982 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 1983 if (error != 0) 1984 return (error); 1985 1986 /* must not be a snapshot */ 1987 if (dsl_dataset_is_snapshot(ds)) { 1988 dsl_dataset_rele(ds, FTAG); 1989 return (SET_ERROR(EINVAL)); 1990 } 1991 1992 /* must have a most recent snapshot */ 1993 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 1994 dsl_dataset_rele(ds, FTAG); 1995 return (SET_ERROR(EINVAL)); 1996 } 1997 1998 /* must not have any bookmarks after the most recent snapshot */ 1999 nvlist_t *proprequest = fnvlist_alloc(); 2000 fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 2001 nvlist_t *bookmarks = fnvlist_alloc(); 2002 error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 2003 fnvlist_free(proprequest); 2004 if (error != 0) 2005 return (error); 2006 for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 2007 pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 2008 nvlist_t *valuenv = 2009 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 2010 zfs_prop_to_name(ZFS_PROP_CREATETXG)); 2011 uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 2012 if (createtxg > ds->ds_phys->ds_prev_snap_txg) { 2013 fnvlist_free(bookmarks); 2014 dsl_dataset_rele(ds, FTAG); 2015 return (SET_ERROR(EEXIST)); 2016 } 2017 } 2018 fnvlist_free(bookmarks); 2019 2020 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 2021 if (error != 0) { 2022 dsl_dataset_rele(ds, FTAG); 2023 return (error); 2024 } 2025 2026 /* 2027 * Check if the snap we are rolling back to uses more than 2028 * the refquota. 2029 */ 2030 if (ds->ds_quota != 0 && 2031 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) { 2032 dsl_dataset_rele(ds, FTAG); 2033 return (SET_ERROR(EDQUOT)); 2034 } 2035 2036 /* 2037 * When we do the clone swap, we will temporarily use more space 2038 * due to the refreservation (the head will no longer have any 2039 * unique space, so the entire amount of the refreservation will need 2040 * to be free). We will immediately destroy the clone, freeing 2041 * this space, but the freeing happens over many txg's. 2042 */ 2043 unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 2044 ds->ds_phys->ds_unique_bytes); 2045 2046 if (unused_refres_delta > 0 && 2047 unused_refres_delta > 2048 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 2049 dsl_dataset_rele(ds, FTAG); 2050 return (SET_ERROR(ENOSPC)); 2051 } 2052 2053 dsl_dataset_rele(ds, FTAG); 2054 return (0); 2055} 2056 2057static void 2058dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 2059{ 2060 dsl_dataset_rollback_arg_t *ddra = arg; 2061 dsl_pool_t *dp = dmu_tx_pool(tx); 2062 dsl_dataset_t *ds, *clone; 2063 uint64_t cloneobj; 2064 char namebuf[ZFS_MAXNAMELEN]; 2065 2066 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 2067 2068 dsl_dataset_name(ds->ds_prev, namebuf); 2069 fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2070 2071 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 2072 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 2073 2074 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 2075 2076 dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 2077 dsl_dataset_zero_zil(ds, tx); 2078 2079 dsl_destroy_head_sync_impl(clone, tx); 2080 2081 dsl_dataset_rele(clone, FTAG); 2082 dsl_dataset_rele(ds, FTAG); 2083} 2084 2085/* 2086 * Rolls back the given filesystem or volume to the most recent snapshot. 2087 * The name of the most recent snapshot will be returned under key "target" 2088 * in the result nvlist. 2089 * 2090 * If owner != NULL: 2091 * - The existing dataset MUST be owned by the specified owner at entry 2092 * - Upon return, dataset will still be held by the same owner, whether we 2093 * succeed or not. 2094 * 2095 * This mode is required any time the existing filesystem is mounted. See 2096 * notes above zfs_suspend_fs() for further details. 2097 */ 2098int 2099dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 2100{ 2101 dsl_dataset_rollback_arg_t ddra; 2102 2103 ddra.ddra_fsname = fsname; 2104 ddra.ddra_owner = owner; 2105 ddra.ddra_result = result; 2106 2107 return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 2108 dsl_dataset_rollback_sync, &ddra, 2109 1, ZFS_SPACE_CHECK_RESERVED)); 2110} 2111 2112struct promotenode { 2113 list_node_t link; 2114 dsl_dataset_t *ds; 2115}; 2116 2117typedef struct dsl_dataset_promote_arg { 2118 const char *ddpa_clonename; 2119 dsl_dataset_t *ddpa_clone; 2120 list_t shared_snaps, origin_snaps, clone_snaps; 2121 dsl_dataset_t *origin_origin; /* origin of the origin */ 2122 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2123 char *err_ds; 2124 cred_t *cr; 2125} dsl_dataset_promote_arg_t; 2126 2127static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 2128static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 2129 void *tag); 2130static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 2131 2132static int 2133dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 2134{ 2135 dsl_dataset_promote_arg_t *ddpa = arg; 2136 dsl_pool_t *dp = dmu_tx_pool(tx); 2137 dsl_dataset_t *hds; 2138 struct promotenode *snap; 2139 dsl_dataset_t *origin_ds; 2140 int err; 2141 uint64_t unused; 2142 uint64_t ss_mv_cnt; 2143 2144 err = promote_hold(ddpa, dp, FTAG); 2145 if (err != 0) 2146 return (err); 2147 2148 hds = ddpa->ddpa_clone; 2149 2150 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) { 2151 promote_rele(ddpa, FTAG); 2152 return (SET_ERROR(EXDEV)); 2153 } 2154 2155 /* 2156 * Compute and check the amount of space to transfer. Since this is 2157 * so expensive, don't do the preliminary check. 2158 */ 2159 if (!dmu_tx_is_syncing(tx)) { 2160 promote_rele(ddpa, FTAG); 2161 return (0); 2162 } 2163 2164 snap = list_head(&ddpa->shared_snaps); 2165 origin_ds = snap->ds; 2166 2167 /* compute origin's new unique space */ 2168 snap = list_tail(&ddpa->clone_snaps); 2169 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2170 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2171 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 2172 &ddpa->unique, &unused, &unused); 2173 2174 /* 2175 * Walk the snapshots that we are moving 2176 * 2177 * Compute space to transfer. Consider the incremental changes 2178 * to used by each snapshot: 2179 * (my used) = (prev's used) + (blocks born) - (blocks killed) 2180 * So each snapshot gave birth to: 2181 * (blocks born) = (my used) - (prev's used) + (blocks killed) 2182 * So a sequence would look like: 2183 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2184 * Which simplifies to: 2185 * uN + kN + kN-1 + ... + k1 + k0 2186 * Note however, if we stop before we reach the ORIGIN we get: 2187 * uN + kN + kN-1 + ... + kM - uM-1 2188 */ 2189 ss_mv_cnt = 0; 2190 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes; 2191 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes; 2192 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 2193 for (snap = list_head(&ddpa->shared_snaps); snap; 2194 snap = list_next(&ddpa->shared_snaps, snap)) { 2195 uint64_t val, dlused, dlcomp, dluncomp; 2196 dsl_dataset_t *ds = snap->ds; 2197 2198 ss_mv_cnt++; 2199 2200 /* 2201 * If there are long holds, we won't be able to evict 2202 * the objset. 2203 */ 2204 if (dsl_dataset_long_held(ds)) { 2205 err = SET_ERROR(EBUSY); 2206 goto out; 2207 } 2208 2209 /* Check that the snapshot name does not conflict */ 2210 VERIFY0(dsl_dataset_get_snapname(ds)); 2211 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2212 if (err == 0) { 2213 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 2214 err = SET_ERROR(EEXIST); 2215 goto out; 2216 } 2217 if (err != ENOENT) 2218 goto out; 2219 2220 /* The very first snapshot does not have a deadlist */ 2221 if (ds->ds_phys->ds_prev_snap_obj == 0) 2222 continue; 2223 2224 dsl_deadlist_space(&ds->ds_deadlist, 2225 &dlused, &dlcomp, &dluncomp); 2226 ddpa->used += dlused; 2227 ddpa->comp += dlcomp; 2228 ddpa->uncomp += dluncomp; 2229 } 2230 2231 /* 2232 * If we are a clone of a clone then we never reached ORIGIN, 2233 * so we need to subtract out the clone origin's used space. 2234 */ 2235 if (ddpa->origin_origin) { 2236 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes; 2237 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes; 2238 ddpa->uncomp -= 2239 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes; 2240 } 2241 2242 /* Check that there is enough space and limit headroom here */ 2243 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2244 0, ss_mv_cnt, ddpa->used, ddpa->cr); 2245 if (err != 0) 2246 goto out; 2247 2248 /* 2249 * Compute the amounts of space that will be used by snapshots 2250 * after the promotion (for both origin and clone). For each, 2251 * it is the amount of space that will be on all of their 2252 * deadlists (that was not born before their new origin). 2253 */ 2254 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2255 uint64_t space; 2256 2257 /* 2258 * Note, typically this will not be a clone of a clone, 2259 * so dd_origin_txg will be < TXG_INITIAL, so 2260 * these snaplist_space() -> dsl_deadlist_space_range() 2261 * calls will be fast because they do not have to 2262 * iterate over all bps. 2263 */ 2264 snap = list_head(&ddpa->origin_snaps); 2265 err = snaplist_space(&ddpa->shared_snaps, 2266 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 2267 if (err != 0) 2268 goto out; 2269 2270 err = snaplist_space(&ddpa->clone_snaps, 2271 snap->ds->ds_dir->dd_origin_txg, &space); 2272 if (err != 0) 2273 goto out; 2274 ddpa->cloneusedsnap += space; 2275 } 2276 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 2277 err = snaplist_space(&ddpa->origin_snaps, 2278 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap); 2279 if (err != 0) 2280 goto out; 2281 } 2282 2283out: 2284 promote_rele(ddpa, FTAG); 2285 return (err); 2286} 2287 2288static void 2289dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 2290{ 2291 dsl_dataset_promote_arg_t *ddpa = arg; 2292 dsl_pool_t *dp = dmu_tx_pool(tx); 2293 dsl_dataset_t *hds; 2294 struct promotenode *snap; 2295 dsl_dataset_t *origin_ds; 2296 dsl_dataset_t *origin_head; 2297 dsl_dir_t *dd; 2298 dsl_dir_t *odd = NULL; 2299 uint64_t oldnext_obj; 2300 int64_t delta; 2301#if defined(__FreeBSD__) && defined(_KERNEL) 2302 char *oldname, *newname; 2303#endif 2304 2305 VERIFY0(promote_hold(ddpa, dp, FTAG)); 2306 hds = ddpa->ddpa_clone; 2307 2308 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE); 2309 2310 snap = list_head(&ddpa->shared_snaps); 2311 origin_ds = snap->ds; 2312 dd = hds->ds_dir; 2313 2314 snap = list_head(&ddpa->origin_snaps); 2315 origin_head = snap->ds; 2316 2317 /* 2318 * We need to explicitly open odd, since origin_ds's dd will be 2319 * changing. 2320 */ 2321 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 2322 NULL, FTAG, &odd)); 2323 2324 /* change origin's next snap */ 2325 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2326 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 2327 snap = list_tail(&ddpa->clone_snaps); 2328 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 2329 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 2330 2331 /* change the origin's next clone */ 2332 if (origin_ds->ds_phys->ds_next_clones_obj) { 2333 dsl_dataset_remove_from_next_clones(origin_ds, 2334 snap->ds->ds_object, tx); 2335 VERIFY0(zap_add_int(dp->dp_meta_objset, 2336 origin_ds->ds_phys->ds_next_clones_obj, 2337 oldnext_obj, tx)); 2338 } 2339 2340 /* change origin */ 2341 dmu_buf_will_dirty(dd->dd_dbuf, tx); 2342 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2343 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 2344 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2345 dmu_buf_will_dirty(odd->dd_dbuf, tx); 2346 odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 2347 origin_head->ds_dir->dd_origin_txg = 2348 origin_ds->ds_phys->ds_creation_txg; 2349 2350 /* change dd_clone entries */ 2351 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2352 VERIFY0(zap_remove_int(dp->dp_meta_objset, 2353 odd->dd_phys->dd_clones, hds->ds_object, tx)); 2354 VERIFY0(zap_add_int(dp->dp_meta_objset, 2355 ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2356 hds->ds_object, tx)); 2357 2358 VERIFY0(zap_remove_int(dp->dp_meta_objset, 2359 ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2360 origin_head->ds_object, tx)); 2361 if (dd->dd_phys->dd_clones == 0) { 2362 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset, 2363 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 2364 } 2365 VERIFY0(zap_add_int(dp->dp_meta_objset, 2366 dd->dd_phys->dd_clones, origin_head->ds_object, tx)); 2367 } 2368 2369#if defined(__FreeBSD__) && defined(_KERNEL) 2370 /* Take the spa_namespace_lock early so zvol renames don't deadlock. */ 2371 mutex_enter(&spa_namespace_lock); 2372 2373 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2374 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2375#endif 2376 2377 /* move snapshots to this dir */ 2378 for (snap = list_head(&ddpa->shared_snaps); snap; 2379 snap = list_next(&ddpa->shared_snaps, snap)) { 2380 dsl_dataset_t *ds = snap->ds; 2381 2382 /* 2383 * Property callbacks are registered to a particular 2384 * dsl_dir. Since ours is changing, evict the objset 2385 * so that they will be unregistered from the old dsl_dir. 2386 */ 2387 if (ds->ds_objset) { 2388 dmu_objset_evict(ds->ds_objset); 2389 ds->ds_objset = NULL; 2390 } 2391 2392 /* move snap name entry */ 2393 VERIFY0(dsl_dataset_get_snapname(ds)); 2394 VERIFY0(dsl_dataset_snap_remove(origin_head, 2395 ds->ds_snapname, tx, B_TRUE)); 2396 VERIFY0(zap_add(dp->dp_meta_objset, 2397 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 2398 8, 1, &ds->ds_object, tx)); 2399 dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2400 DD_FIELD_SNAPSHOT_COUNT, tx); 2401 2402 /* change containing dsl_dir */ 2403 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2404 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 2405 ds->ds_phys->ds_dir_obj = dd->dd_object; 2406 ASSERT3P(ds->ds_dir, ==, odd); 2407 dsl_dir_rele(ds->ds_dir, ds); 2408 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 2409 NULL, ds, &ds->ds_dir)); 2410 2411#if defined(__FreeBSD__) && defined(_KERNEL) 2412 dsl_dataset_name(ds, newname); 2413 zfsvfs_update_fromname(oldname, newname); 2414 zvol_rename_minors(oldname, newname); 2415#endif 2416 2417 /* move any clone references */ 2418 if (ds->ds_phys->ds_next_clones_obj && 2419 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2420 zap_cursor_t zc; 2421 zap_attribute_t za; 2422 2423 for (zap_cursor_init(&zc, dp->dp_meta_objset, 2424 ds->ds_phys->ds_next_clones_obj); 2425 zap_cursor_retrieve(&zc, &za) == 0; 2426 zap_cursor_advance(&zc)) { 2427 dsl_dataset_t *cnds; 2428 uint64_t o; 2429 2430 if (za.za_first_integer == oldnext_obj) { 2431 /* 2432 * We've already moved the 2433 * origin's reference. 2434 */ 2435 continue; 2436 } 2437 2438 VERIFY0(dsl_dataset_hold_obj(dp, 2439 za.za_first_integer, FTAG, &cnds)); 2440 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj; 2441 2442 VERIFY0(zap_remove_int(dp->dp_meta_objset, 2443 odd->dd_phys->dd_clones, o, tx)); 2444 VERIFY0(zap_add_int(dp->dp_meta_objset, 2445 dd->dd_phys->dd_clones, o, tx)); 2446 dsl_dataset_rele(cnds, FTAG); 2447 } 2448 zap_cursor_fini(&zc); 2449 } 2450 2451 ASSERT(!dsl_prop_hascb(ds)); 2452 } 2453 2454#if defined(__FreeBSD__) && defined(_KERNEL) 2455 mutex_exit(&spa_namespace_lock); 2456 2457 kmem_free(newname, MAXPATHLEN); 2458 kmem_free(oldname, MAXPATHLEN); 2459#endif 2460 /* 2461 * Change space accounting. 2462 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 2463 * both be valid, or both be 0 (resulting in delta == 0). This 2464 * is true for each of {clone,origin} independently. 2465 */ 2466 2467 delta = ddpa->cloneusedsnap - 2468 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2469 ASSERT3S(delta, >=, 0); 2470 ASSERT3U(ddpa->used, >=, delta); 2471 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 2472 dsl_dir_diduse_space(dd, DD_USED_HEAD, 2473 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 2474 2475 delta = ddpa->originusedsnap - 2476 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 2477 ASSERT3S(delta, <=, 0); 2478 ASSERT3U(ddpa->used, >=, -delta); 2479 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 2480 dsl_dir_diduse_space(odd, DD_USED_HEAD, 2481 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 2482 2483 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique; 2484 2485 /* log history record */ 2486 spa_history_log_internal_ds(hds, "promote", tx, ""); 2487 2488 dsl_dir_rele(odd, FTAG); 2489 promote_rele(ddpa, FTAG); 2490} 2491 2492/* 2493 * Make a list of dsl_dataset_t's for the snapshots between first_obj 2494 * (exclusive) and last_obj (inclusive). The list will be in reverse 2495 * order (last_obj will be the list_head()). If first_obj == 0, do all 2496 * snapshots back to this dataset's origin. 2497 */ 2498static int 2499snaplist_make(dsl_pool_t *dp, 2500 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2501{ 2502 uint64_t obj = last_obj; 2503 2504 list_create(l, sizeof (struct promotenode), 2505 offsetof(struct promotenode, link)); 2506 2507 while (obj != first_obj) { 2508 dsl_dataset_t *ds; 2509 struct promotenode *snap; 2510 int err; 2511 2512 err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 2513 ASSERT(err != ENOENT); 2514 if (err != 0) 2515 return (err); 2516 2517 if (first_obj == 0) 2518 first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 2519 2520 snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 2521 snap->ds = ds; 2522 list_insert_tail(l, snap); 2523 obj = ds->ds_phys->ds_prev_snap_obj; 2524 } 2525 2526 return (0); 2527} 2528 2529static int 2530snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2531{ 2532 struct promotenode *snap; 2533 2534 *spacep = 0; 2535 for (snap = list_head(l); snap; snap = list_next(l, snap)) { 2536 uint64_t used, comp, uncomp; 2537 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2538 mintxg, UINT64_MAX, &used, &comp, &uncomp); 2539 *spacep += used; 2540 } 2541 return (0); 2542} 2543 2544static void 2545snaplist_destroy(list_t *l, void *tag) 2546{ 2547 struct promotenode *snap; 2548 2549 if (l == NULL || !list_link_active(&l->list_head)) 2550 return; 2551 2552 while ((snap = list_tail(l)) != NULL) { 2553 list_remove(l, snap); 2554 dsl_dataset_rele(snap->ds, tag); 2555 kmem_free(snap, sizeof (*snap)); 2556 } 2557 list_destroy(l); 2558} 2559 2560static int 2561promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2562{ 2563 int error; 2564 dsl_dir_t *dd; 2565 struct promotenode *snap; 2566 2567 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 2568 &ddpa->ddpa_clone); 2569 if (error != 0) 2570 return (error); 2571 dd = ddpa->ddpa_clone->ds_dir; 2572 2573 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) || 2574 !dsl_dir_is_clone(dd)) { 2575 dsl_dataset_rele(ddpa->ddpa_clone, tag); 2576 return (SET_ERROR(EINVAL)); 2577 } 2578 2579 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj, 2580 &ddpa->shared_snaps, tag); 2581 if (error != 0) 2582 goto out; 2583 2584 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 2585 &ddpa->clone_snaps, tag); 2586 if (error != 0) 2587 goto out; 2588 2589 snap = list_head(&ddpa->shared_snaps); 2590 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 2591 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj, 2592 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, 2593 &ddpa->origin_snaps, tag); 2594 if (error != 0) 2595 goto out; 2596 2597 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) { 2598 error = dsl_dataset_hold_obj(dp, 2599 snap->ds->ds_dir->dd_phys->dd_origin_obj, 2600 tag, &ddpa->origin_origin); 2601 if (error != 0) 2602 goto out; 2603 } 2604out: 2605 if (error != 0) 2606 promote_rele(ddpa, tag); 2607 return (error); 2608} 2609 2610static void 2611promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2612{ 2613 snaplist_destroy(&ddpa->shared_snaps, tag); 2614 snaplist_destroy(&ddpa->clone_snaps, tag); 2615 snaplist_destroy(&ddpa->origin_snaps, tag); 2616 if (ddpa->origin_origin != NULL) 2617 dsl_dataset_rele(ddpa->origin_origin, tag); 2618 dsl_dataset_rele(ddpa->ddpa_clone, tag); 2619} 2620 2621/* 2622 * Promote a clone. 2623 * 2624 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 2625 * in with the name. (It must be at least MAXNAMELEN bytes long.) 2626 */ 2627int 2628dsl_dataset_promote(const char *name, char *conflsnap) 2629{ 2630 dsl_dataset_promote_arg_t ddpa = { 0 }; 2631 uint64_t numsnaps; 2632 int error; 2633 objset_t *os; 2634 2635 /* 2636 * We will modify space proportional to the number of 2637 * snapshots. Compute numsnaps. 2638 */ 2639 error = dmu_objset_hold(name, FTAG, &os); 2640 if (error != 0) 2641 return (error); 2642 error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 2643 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps); 2644 dmu_objset_rele(os, FTAG); 2645 if (error != 0) 2646 return (error); 2647 2648 ddpa.ddpa_clonename = name; 2649 ddpa.err_ds = conflsnap; 2650 ddpa.cr = CRED(); 2651 2652 return (dsl_sync_task(name, dsl_dataset_promote_check, 2653 dsl_dataset_promote_sync, &ddpa, 2654 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED)); 2655} 2656 2657int 2658dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 2659 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2660{ 2661 int64_t unused_refres_delta; 2662 2663 /* they should both be heads */ 2664 if (dsl_dataset_is_snapshot(clone) || 2665 dsl_dataset_is_snapshot(origin_head)) 2666 return (SET_ERROR(EINVAL)); 2667 2668 /* if we are not forcing, the branch point should be just before them */ 2669 if (!force && clone->ds_prev != origin_head->ds_prev) 2670 return (SET_ERROR(EINVAL)); 2671 2672 /* clone should be the clone (unless they are unrelated) */ 2673 if (clone->ds_prev != NULL && 2674 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 2675 origin_head->ds_dir != clone->ds_prev->ds_dir) 2676 return (SET_ERROR(EINVAL)); 2677 2678 /* the clone should be a child of the origin */ 2679 if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2680 return (SET_ERROR(EINVAL)); 2681 2682 /* origin_head shouldn't be modified unless 'force' */ 2683 if (!force && 2684 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2685 return (SET_ERROR(ETXTBSY)); 2686 2687 /* origin_head should have no long holds (e.g. is not mounted) */ 2688 if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2689 return (SET_ERROR(EBUSY)); 2690 2691 /* check amount of any unconsumed refreservation */ 2692 unused_refres_delta = 2693 (int64_t)MIN(origin_head->ds_reserved, 2694 origin_head->ds_phys->ds_unique_bytes) - 2695 (int64_t)MIN(origin_head->ds_reserved, 2696 clone->ds_phys->ds_unique_bytes); 2697 2698 if (unused_refres_delta > 0 && 2699 unused_refres_delta > 2700 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2701 return (SET_ERROR(ENOSPC)); 2702 2703 /* clone can't be over the head's refquota */ 2704 if (origin_head->ds_quota != 0 && 2705 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota) 2706 return (SET_ERROR(EDQUOT)); 2707 2708 return (0); 2709} 2710 2711void 2712dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 2713 dsl_dataset_t *origin_head, dmu_tx_t *tx) 2714{ 2715 dsl_pool_t *dp = dmu_tx_pool(tx); 2716 int64_t unused_refres_delta; 2717 2718 ASSERT(clone->ds_reserved == 0); 2719 ASSERT(origin_head->ds_quota == 0 || 2720 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota); 2721 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2722 2723 dmu_buf_will_dirty(clone->ds_dbuf, tx); 2724 dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2725 2726 if (clone->ds_objset != NULL) { 2727 dmu_objset_evict(clone->ds_objset); 2728 clone->ds_objset = NULL; 2729 } 2730 2731 if (origin_head->ds_objset != NULL) { 2732 dmu_objset_evict(origin_head->ds_objset); 2733 origin_head->ds_objset = NULL; 2734 } 2735 2736 unused_refres_delta = 2737 (int64_t)MIN(origin_head->ds_reserved, 2738 origin_head->ds_phys->ds_unique_bytes) - 2739 (int64_t)MIN(origin_head->ds_reserved, 2740 clone->ds_phys->ds_unique_bytes); 2741 2742 /* 2743 * Reset origin's unique bytes, if it exists. 2744 */ 2745 if (clone->ds_prev) { 2746 dsl_dataset_t *origin = clone->ds_prev; 2747 uint64_t comp, uncomp; 2748 2749 dmu_buf_will_dirty(origin->ds_dbuf, tx); 2750 dsl_deadlist_space_range(&clone->ds_deadlist, 2751 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 2752 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp); 2753 } 2754 2755 /* swap blkptrs */ 2756 { 2757 blkptr_t tmp; 2758 tmp = origin_head->ds_phys->ds_bp; 2759 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp; 2760 clone->ds_phys->ds_bp = tmp; 2761 } 2762 2763 /* set dd_*_bytes */ 2764 { 2765 int64_t dused, dcomp, duncomp; 2766 uint64_t cdl_used, cdl_comp, cdl_uncomp; 2767 uint64_t odl_used, odl_comp, odl_uncomp; 2768 2769 ASSERT3U(clone->ds_dir->dd_phys-> 2770 dd_used_breakdown[DD_USED_SNAP], ==, 0); 2771 2772 dsl_deadlist_space(&clone->ds_deadlist, 2773 &cdl_used, &cdl_comp, &cdl_uncomp); 2774 dsl_deadlist_space(&origin_head->ds_deadlist, 2775 &odl_used, &odl_comp, &odl_uncomp); 2776 2777 dused = clone->ds_phys->ds_referenced_bytes + cdl_used - 2778 (origin_head->ds_phys->ds_referenced_bytes + odl_used); 2779 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp - 2780 (origin_head->ds_phys->ds_compressed_bytes + odl_comp); 2781 duncomp = clone->ds_phys->ds_uncompressed_bytes + 2782 cdl_uncomp - 2783 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2784 2785 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 2786 dused, dcomp, duncomp, tx); 2787 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 2788 -dused, -dcomp, -duncomp, tx); 2789 2790 /* 2791 * The difference in the space used by snapshots is the 2792 * difference in snapshot space due to the head's 2793 * deadlist (since that's the only thing that's 2794 * changing that affects the snapused). 2795 */ 2796 dsl_deadlist_space_range(&clone->ds_deadlist, 2797 origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 2798 &cdl_used, &cdl_comp, &cdl_uncomp); 2799 dsl_deadlist_space_range(&origin_head->ds_deadlist, 2800 origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 2801 &odl_used, &odl_comp, &odl_uncomp); 2802 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 2803 DD_USED_HEAD, DD_USED_SNAP, tx); 2804 } 2805 2806 /* swap ds_*_bytes */ 2807 SWITCH64(origin_head->ds_phys->ds_referenced_bytes, 2808 clone->ds_phys->ds_referenced_bytes); 2809 SWITCH64(origin_head->ds_phys->ds_compressed_bytes, 2810 clone->ds_phys->ds_compressed_bytes); 2811 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes, 2812 clone->ds_phys->ds_uncompressed_bytes); 2813 SWITCH64(origin_head->ds_phys->ds_unique_bytes, 2814 clone->ds_phys->ds_unique_bytes); 2815 2816 /* apply any parent delta for change in unconsumed refreservation */ 2817 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 2818 unused_refres_delta, 0, 0, tx); 2819 2820 /* 2821 * Swap deadlists. 2822 */ 2823 dsl_deadlist_close(&clone->ds_deadlist); 2824 dsl_deadlist_close(&origin_head->ds_deadlist); 2825 SWITCH64(origin_head->ds_phys->ds_deadlist_obj, 2826 clone->ds_phys->ds_deadlist_obj); 2827 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 2828 clone->ds_phys->ds_deadlist_obj); 2829 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 2830 origin_head->ds_phys->ds_deadlist_obj); 2831 2832 dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2833 2834 spa_history_log_internal_ds(clone, "clone swap", tx, 2835 "parent=%s", origin_head->ds_dir->dd_myname); 2836} 2837 2838/* 2839 * Given a pool name and a dataset object number in that pool, 2840 * return the name of that dataset. 2841 */ 2842int 2843dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2844{ 2845 dsl_pool_t *dp; 2846 dsl_dataset_t *ds; 2847 int error; 2848 2849 error = dsl_pool_hold(pname, FTAG, &dp); 2850 if (error != 0) 2851 return (error); 2852 2853 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 2854 if (error == 0) { 2855 dsl_dataset_name(ds, buf); 2856 dsl_dataset_rele(ds, FTAG); 2857 } 2858 dsl_pool_rele(dp, FTAG); 2859 2860 return (error); 2861} 2862 2863int 2864dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 2865 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2866{ 2867 int error = 0; 2868 2869 ASSERT3S(asize, >, 0); 2870 2871 /* 2872 * *ref_rsrv is the portion of asize that will come from any 2873 * unconsumed refreservation space. 2874 */ 2875 *ref_rsrv = 0; 2876 2877 mutex_enter(&ds->ds_lock); 2878 /* 2879 * Make a space adjustment for reserved bytes. 2880 */ 2881 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 2882 ASSERT3U(*used, >=, 2883 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 2884 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 2885 *ref_rsrv = 2886 asize - MIN(asize, parent_delta(ds, asize + inflight)); 2887 } 2888 2889 if (!check_quota || ds->ds_quota == 0) { 2890 mutex_exit(&ds->ds_lock); 2891 return (0); 2892 } 2893 /* 2894 * If they are requesting more space, and our current estimate 2895 * is over quota, they get to try again unless the actual 2896 * on-disk is over quota and there are no pending changes (which 2897 * may free up space for us). 2898 */ 2899 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) { 2900 if (inflight > 0 || 2901 ds->ds_phys->ds_referenced_bytes < ds->ds_quota) 2902 error = SET_ERROR(ERESTART); 2903 else 2904 error = SET_ERROR(EDQUOT); 2905 } 2906 mutex_exit(&ds->ds_lock); 2907 2908 return (error); 2909} 2910 2911typedef struct dsl_dataset_set_qr_arg { 2912 const char *ddsqra_name; 2913 zprop_source_t ddsqra_source; 2914 uint64_t ddsqra_value; 2915} dsl_dataset_set_qr_arg_t; 2916 2917 2918/* ARGSUSED */ 2919static int 2920dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2921{ 2922 dsl_dataset_set_qr_arg_t *ddsqra = arg; 2923 dsl_pool_t *dp = dmu_tx_pool(tx); 2924 dsl_dataset_t *ds; 2925 int error; 2926 uint64_t newval; 2927 2928 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2929 return (SET_ERROR(ENOTSUP)); 2930 2931 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 2932 if (error != 0) 2933 return (error); 2934 2935 if (dsl_dataset_is_snapshot(ds)) { 2936 dsl_dataset_rele(ds, FTAG); 2937 return (SET_ERROR(EINVAL)); 2938 } 2939 2940 error = dsl_prop_predict(ds->ds_dir, 2941 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 2942 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 2943 if (error != 0) { 2944 dsl_dataset_rele(ds, FTAG); 2945 return (error); 2946 } 2947 2948 if (newval == 0) { 2949 dsl_dataset_rele(ds, FTAG); 2950 return (0); 2951 } 2952 2953 if (newval < ds->ds_phys->ds_referenced_bytes || 2954 newval < ds->ds_reserved) { 2955 dsl_dataset_rele(ds, FTAG); 2956 return (SET_ERROR(ENOSPC)); 2957 } 2958 2959 dsl_dataset_rele(ds, FTAG); 2960 return (0); 2961} 2962 2963static void 2964dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 2965{ 2966 dsl_dataset_set_qr_arg_t *ddsqra = arg; 2967 dsl_pool_t *dp = dmu_tx_pool(tx); 2968 dsl_dataset_t *ds; 2969 uint64_t newval; 2970 2971 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 2972 2973 dsl_prop_set_sync_impl(ds, 2974 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 2975 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 2976 &ddsqra->ddsqra_value, tx); 2977 2978 VERIFY0(dsl_prop_get_int_ds(ds, 2979 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 2980 2981 if (ds->ds_quota != newval) { 2982 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2983 ds->ds_quota = newval; 2984 } 2985 dsl_dataset_rele(ds, FTAG); 2986} 2987 2988int 2989dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 2990 uint64_t refquota) 2991{ 2992 dsl_dataset_set_qr_arg_t ddsqra; 2993 2994 ddsqra.ddsqra_name = dsname; 2995 ddsqra.ddsqra_source = source; 2996 ddsqra.ddsqra_value = refquota; 2997 2998 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 2999 dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 3000} 3001 3002static int 3003dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 3004{ 3005 dsl_dataset_set_qr_arg_t *ddsqra = arg; 3006 dsl_pool_t *dp = dmu_tx_pool(tx); 3007 dsl_dataset_t *ds; 3008 int error; 3009 uint64_t newval, unique; 3010 3011 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 3012 return (SET_ERROR(ENOTSUP)); 3013 3014 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 3015 if (error != 0) 3016 return (error); 3017 3018 if (dsl_dataset_is_snapshot(ds)) { 3019 dsl_dataset_rele(ds, FTAG); 3020 return (SET_ERROR(EINVAL)); 3021 } 3022 3023 error = dsl_prop_predict(ds->ds_dir, 3024 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 3025 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 3026 if (error != 0) { 3027 dsl_dataset_rele(ds, FTAG); 3028 return (error); 3029 } 3030 3031 /* 3032 * If we are doing the preliminary check in open context, the 3033 * space estimates may be inaccurate. 3034 */ 3035 if (!dmu_tx_is_syncing(tx)) { 3036 dsl_dataset_rele(ds, FTAG); 3037 return (0); 3038 } 3039 3040 mutex_enter(&ds->ds_lock); 3041 if (!DS_UNIQUE_IS_ACCURATE(ds)) 3042 dsl_dataset_recalc_head_uniq(ds); 3043 unique = ds->ds_phys->ds_unique_bytes; 3044 mutex_exit(&ds->ds_lock); 3045 3046 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 3047 uint64_t delta = MAX(unique, newval) - 3048 MAX(unique, ds->ds_reserved); 3049 3050 if (delta > 3051 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 3052 (ds->ds_quota > 0 && newval > ds->ds_quota)) { 3053 dsl_dataset_rele(ds, FTAG); 3054 return (SET_ERROR(ENOSPC)); 3055 } 3056 } 3057 3058 dsl_dataset_rele(ds, FTAG); 3059 return (0); 3060} 3061 3062void 3063dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 3064 zprop_source_t source, uint64_t value, dmu_tx_t *tx) 3065{ 3066 uint64_t newval; 3067 uint64_t unique; 3068 int64_t delta; 3069 3070 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 3071 source, sizeof (value), 1, &value, tx); 3072 3073 VERIFY0(dsl_prop_get_int_ds(ds, 3074 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3075 3076 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3077 mutex_enter(&ds->ds_dir->dd_lock); 3078 mutex_enter(&ds->ds_lock); 3079 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3080 unique = ds->ds_phys->ds_unique_bytes; 3081 delta = MAX(0, (int64_t)(newval - unique)) - 3082 MAX(0, (int64_t)(ds->ds_reserved - unique)); 3083 ds->ds_reserved = newval; 3084 mutex_exit(&ds->ds_lock); 3085 3086 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 3087 mutex_exit(&ds->ds_dir->dd_lock); 3088} 3089 3090static void 3091dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3092{ 3093 dsl_dataset_set_qr_arg_t *ddsqra = arg; 3094 dsl_pool_t *dp = dmu_tx_pool(tx); 3095 dsl_dataset_t *ds; 3096 3097 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 3098 dsl_dataset_set_refreservation_sync_impl(ds, 3099 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3100 dsl_dataset_rele(ds, FTAG); 3101} 3102 3103int 3104dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 3105 uint64_t refreservation) 3106{ 3107 dsl_dataset_set_qr_arg_t ddsqra; 3108 3109 ddsqra.ddsqra_name = dsname; 3110 ddsqra.ddsqra_source = source; 3111 ddsqra.ddsqra_value = refreservation; 3112 3113 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 3114 dsl_dataset_set_refreservation_sync, &ddsqra, 3115 0, ZFS_SPACE_CHECK_NONE)); 3116} 3117 3118/* 3119 * Return (in *usedp) the amount of space written in new that is not 3120 * present in oldsnap. New may be a snapshot or the head. Old must be 3121 * a snapshot before new, in new's filesystem (or its origin). If not then 3122 * fail and return EINVAL. 3123 * 3124 * The written space is calculated by considering two components: First, we 3125 * ignore any freed space, and calculate the written as new's used space 3126 * minus old's used space. Next, we add in the amount of space that was freed 3127 * between the two snapshots, thus reducing new's used space relative to old's. 3128 * Specifically, this is the space that was born before old->ds_creation_txg, 3129 * and freed before new (ie. on new's deadlist or a previous deadlist). 3130 * 3131 * space freed [---------------------] 3132 * snapshots ---O-------O--------O-------O------ 3133 * oldsnap new 3134 */ 3135int 3136dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 3137 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 3138{ 3139 int err = 0; 3140 uint64_t snapobj; 3141 dsl_pool_t *dp = new->ds_dir->dd_pool; 3142 3143 ASSERT(dsl_pool_config_held(dp)); 3144 3145 *usedp = 0; 3146 *usedp += new->ds_phys->ds_referenced_bytes; 3147 *usedp -= oldsnap->ds_phys->ds_referenced_bytes; 3148 3149 *compp = 0; 3150 *compp += new->ds_phys->ds_compressed_bytes; 3151 *compp -= oldsnap->ds_phys->ds_compressed_bytes; 3152 3153 *uncompp = 0; 3154 *uncompp += new->ds_phys->ds_uncompressed_bytes; 3155 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes; 3156 3157 snapobj = new->ds_object; 3158 while (snapobj != oldsnap->ds_object) { 3159 dsl_dataset_t *snap; 3160 uint64_t used, comp, uncomp; 3161 3162 if (snapobj == new->ds_object) { 3163 snap = new; 3164 } else { 3165 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3166 if (err != 0) 3167 break; 3168 } 3169 3170 if (snap->ds_phys->ds_prev_snap_txg == 3171 oldsnap->ds_phys->ds_creation_txg) { 3172 /* 3173 * The blocks in the deadlist can not be born after 3174 * ds_prev_snap_txg, so get the whole deadlist space, 3175 * which is more efficient (especially for old-format 3176 * deadlists). Unfortunately the deadlist code 3177 * doesn't have enough information to make this 3178 * optimization itself. 3179 */ 3180 dsl_deadlist_space(&snap->ds_deadlist, 3181 &used, &comp, &uncomp); 3182 } else { 3183 dsl_deadlist_space_range(&snap->ds_deadlist, 3184 0, oldsnap->ds_phys->ds_creation_txg, 3185 &used, &comp, &uncomp); 3186 } 3187 *usedp += used; 3188 *compp += comp; 3189 *uncompp += uncomp; 3190 3191 /* 3192 * If we get to the beginning of the chain of snapshots 3193 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 3194 * was not a snapshot of/before new. 3195 */ 3196 snapobj = snap->ds_phys->ds_prev_snap_obj; 3197 if (snap != new) 3198 dsl_dataset_rele(snap, FTAG); 3199 if (snapobj == 0) { 3200 err = SET_ERROR(EINVAL); 3201 break; 3202 } 3203 3204 } 3205 return (err); 3206} 3207 3208/* 3209 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 3210 * lastsnap, and all snapshots in between are deleted. 3211 * 3212 * blocks that would be freed [---------------------------] 3213 * snapshots ---O-------O--------O-------O--------O 3214 * firstsnap lastsnap 3215 * 3216 * This is the set of blocks that were born after the snap before firstsnap, 3217 * (birth > firstsnap->prev_snap_txg) and died before the snap after the 3218 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 3219 * We calculate this by iterating over the relevant deadlists (from the snap 3220 * after lastsnap, backward to the snap after firstsnap), summing up the 3221 * space on the deadlist that was born after the snap before firstsnap. 3222 */ 3223int 3224dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 3225 dsl_dataset_t *lastsnap, 3226 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 3227{ 3228 int err = 0; 3229 uint64_t snapobj; 3230 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 3231 3232 ASSERT(dsl_dataset_is_snapshot(firstsnap)); 3233 ASSERT(dsl_dataset_is_snapshot(lastsnap)); 3234 3235 /* 3236 * Check that the snapshots are in the same dsl_dir, and firstsnap 3237 * is before lastsnap. 3238 */ 3239 if (firstsnap->ds_dir != lastsnap->ds_dir || 3240 firstsnap->ds_phys->ds_creation_txg > 3241 lastsnap->ds_phys->ds_creation_txg) 3242 return (SET_ERROR(EINVAL)); 3243 3244 *usedp = *compp = *uncompp = 0; 3245 3246 snapobj = lastsnap->ds_phys->ds_next_snap_obj; 3247 while (snapobj != firstsnap->ds_object) { 3248 dsl_dataset_t *ds; 3249 uint64_t used, comp, uncomp; 3250 3251 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 3252 if (err != 0) 3253 break; 3254 3255 dsl_deadlist_space_range(&ds->ds_deadlist, 3256 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX, 3257 &used, &comp, &uncomp); 3258 *usedp += used; 3259 *compp += comp; 3260 *uncompp += uncomp; 3261 3262 snapobj = ds->ds_phys->ds_prev_snap_obj; 3263 ASSERT3U(snapobj, !=, 0); 3264 dsl_dataset_rele(ds, FTAG); 3265 } 3266 return (err); 3267} 3268 3269static int 3270dsl_dataset_activate_large_blocks_check(void *arg, dmu_tx_t *tx) 3271{ 3272 const char *dsname = arg; 3273 dsl_dataset_t *ds; 3274 dsl_pool_t *dp = dmu_tx_pool(tx); 3275 int error = 0; 3276 3277 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS)) 3278 return (SET_ERROR(ENOTSUP)); 3279 3280 ASSERT(spa_feature_is_enabled(dp->dp_spa, 3281 SPA_FEATURE_EXTENSIBLE_DATASET)); 3282 3283 error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 3284 if (error != 0) 3285 return (error); 3286 3287 if (ds->ds_large_blocks) 3288 error = EALREADY; 3289 dsl_dataset_rele(ds, FTAG); 3290 3291 return (error); 3292} 3293 3294void 3295dsl_dataset_activate_large_blocks_sync_impl(uint64_t dsobj, dmu_tx_t *tx) 3296{ 3297 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 3298 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 3299 uint64_t zero = 0; 3300 3301 spa_feature_incr(spa, SPA_FEATURE_LARGE_BLOCKS, tx); 3302 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 3303 3304 VERIFY0(zap_add(mos, dsobj, DS_FIELD_LARGE_BLOCKS, 3305 sizeof (zero), 1, &zero, tx)); 3306} 3307 3308static void 3309dsl_dataset_activate_large_blocks_sync(void *arg, dmu_tx_t *tx) 3310{ 3311 const char *dsname = arg; 3312 dsl_dataset_t *ds; 3313 3314 VERIFY0(dsl_dataset_hold(dmu_tx_pool(tx), dsname, FTAG, &ds)); 3315 3316 dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx); 3317 ASSERT(!ds->ds_large_blocks); 3318 ds->ds_large_blocks = B_TRUE; 3319 dsl_dataset_rele(ds, FTAG); 3320} 3321 3322int 3323dsl_dataset_activate_large_blocks(const char *dsname) 3324{ 3325 int error; 3326 3327 error = dsl_sync_task(dsname, 3328 dsl_dataset_activate_large_blocks_check, 3329 dsl_dataset_activate_large_blocks_sync, (void *)dsname, 3330 1, ZFS_SPACE_CHECK_RESERVED); 3331 3332 /* 3333 * EALREADY indicates that this dataset already supports large blocks. 3334 */ 3335 if (error == EALREADY) 3336 error = 0; 3337 return (error); 3338} 3339 3340/* 3341 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 3342 * For example, they could both be snapshots of the same filesystem, and 3343 * 'earlier' is before 'later'. Or 'earlier' could be the origin of 3344 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 3345 * filesystem. Or 'earlier' could be the origin's origin. 3346 * 3347 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 3348 */ 3349boolean_t 3350dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 3351 uint64_t earlier_txg) 3352{ 3353 dsl_pool_t *dp = later->ds_dir->dd_pool; 3354 int error; 3355 boolean_t ret; 3356 3357 ASSERT(dsl_pool_config_held(dp)); 3358 ASSERT(dsl_dataset_is_snapshot(earlier) || earlier_txg != 0); 3359 3360 if (earlier_txg == 0) 3361 earlier_txg = earlier->ds_phys->ds_creation_txg; 3362 3363 if (dsl_dataset_is_snapshot(later) && 3364 earlier_txg >= later->ds_phys->ds_creation_txg) 3365 return (B_FALSE); 3366 3367 if (later->ds_dir == earlier->ds_dir) 3368 return (B_TRUE); 3369 if (!dsl_dir_is_clone(later->ds_dir)) 3370 return (B_FALSE); 3371 3372 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object) 3373 return (B_TRUE); 3374 dsl_dataset_t *origin; 3375 error = dsl_dataset_hold_obj(dp, 3376 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin); 3377 if (error != 0) 3378 return (B_FALSE); 3379 ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 3380 dsl_dataset_rele(origin, FTAG); 3381 return (ret); 3382} 3383 3384 3385void 3386dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 3387{ 3388 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3389 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 3390} 3391