1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 26 * Copyright (c) 2014, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2014 RackTop Systems. 28 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 29 * Copyright (c) 2016 Actifio, Inc. All rights reserved. 30 * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved. 31 * Copyright 2017 Nexenta Systems, Inc. 32 * Copyright (c) 2019, Klara Inc. 33 * Copyright (c) 2019, Allan Jude 34 * Copyright (c) 2020 The FreeBSD Foundation [1] 35 * Copyright (c) 2025, Rob Norris <robn@despairlabs.com> 36 * 37 * [1] Portions of this software were developed by Allan Jude 38 * under sponsorship from the FreeBSD Foundation. 39 */ 40 41 #include <sys/dmu_objset.h> 42 #include <sys/dsl_dataset.h> 43 #include <sys/dsl_dir.h> 44 #include <sys/dsl_prop.h> 45 #include <sys/dsl_synctask.h> 46 #include <sys/dmu_traverse.h> 47 #include <sys/dmu_impl.h> 48 #include <sys/dmu_tx.h> 49 #include <sys/arc.h> 50 #include <sys/zio.h> 51 #include <sys/zap.h> 52 #include <sys/zfeature.h> 53 #include <sys/unique.h> 54 #include <sys/zfs_context.h> 55 #include <sys/zfs_ioctl.h> 56 #include <sys/spa.h> 57 #include <sys/spa_impl.h> 58 #include <sys/vdev.h> 59 #include <sys/zfs_znode.h> 60 #include <sys/zfs_onexit.h> 61 #include <sys/zvol.h> 62 #include <sys/dsl_scan.h> 63 #include <sys/dsl_deadlist.h> 64 #include <sys/dsl_destroy.h> 65 #include <sys/dsl_userhold.h> 66 #include <sys/dsl_bookmark.h> 67 #include <sys/policy.h> 68 #include <sys/dmu_send.h> 69 #include <sys/dmu_recv.h> 70 #include <sys/zio_compress.h> 71 #include <zfs_fletcher.h> 72 #include <sys/zio_checksum.h> 73 #include <sys/brt.h> 74 75 /* 76 * The SPA supports block sizes up to 16MB. However, very large blocks 77 * can have an impact on i/o latency (e.g. tying up a spinning disk for 78 * ~300ms), and also potentially on the memory allocator. Therefore, 79 * we did not allow the recordsize to be set larger than zfs_max_recordsize 80 * (former default: 1MB). Larger blocks could be created by changing this 81 * tunable, and pools with larger blocks could always be imported and used, 82 * regardless of this setting. 83 * 84 * We do, however, still limit it by default to 1M on x86_32, because Linux's 85 * 3/1 memory split doesn't leave much room for 16M chunks. 86 */ 87 #ifdef _ILP32 88 uint_t zfs_max_recordsize = 1 * 1024 * 1024; 89 #else 90 uint_t zfs_max_recordsize = 16 * 1024 * 1024; 91 #endif 92 static int zfs_allow_redacted_dataset_mount = 0; 93 94 int zfs_snapshot_history_enabled = 1; 95 96 #define SWITCH64(x, y) \ 97 { \ 98 uint64_t __tmp = (x); \ 99 (x) = (y); \ 100 (y) = __tmp; \ 101 } 102 103 #define DS_REF_MAX (1ULL << 62) 104 105 static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, 106 uint64_t obj, dmu_tx_t *tx); 107 static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, 108 dmu_tx_t *tx); 109 110 static void unload_zfeature(dsl_dataset_t *ds, spa_feature_t f); 111 112 extern uint_t spa_asize_inflation; 113 114 static zil_header_t zero_zil; 115 116 /* 117 * Figure out how much of this delta should be propagated to the dsl_dir 118 * layer. If there's a refreservation, that space has already been 119 * partially accounted for in our ancestors. 120 */ 121 static int64_t 122 parent_delta(dsl_dataset_t *ds, int64_t delta) 123 { 124 dsl_dataset_phys_t *ds_phys; 125 uint64_t old_bytes, new_bytes; 126 127 if (ds->ds_reserved == 0) 128 return (delta); 129 130 ds_phys = dsl_dataset_phys(ds); 131 old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved); 132 new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 133 134 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 135 return (new_bytes - old_bytes); 136 } 137 138 void 139 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 140 { 141 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 142 int used = bp_get_dsize_sync(spa, bp); 143 int compressed = BP_GET_PSIZE(bp); 144 int uncompressed = BP_GET_UCSIZE(bp); 145 int64_t delta; 146 spa_feature_t f; 147 148 dprintf_bp(bp, "ds=%p", ds); 149 150 ASSERT(dmu_tx_is_syncing(tx)); 151 /* It could have been compressed away to nothing */ 152 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) 153 return; 154 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 155 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 156 if (ds == NULL) { 157 dsl_pool_mos_diduse_space(tx->tx_pool, 158 used, compressed, uncompressed); 159 return; 160 } 161 162 ASSERT3U(BP_GET_LOGICAL_BIRTH(bp), >, 163 dsl_dataset_phys(ds)->ds_prev_snap_txg); 164 dmu_buf_will_dirty(ds->ds_dbuf, tx); 165 mutex_enter(&ds->ds_lock); 166 delta = parent_delta(ds, used); 167 dsl_dataset_phys(ds)->ds_referenced_bytes += used; 168 dsl_dataset_phys(ds)->ds_compressed_bytes += compressed; 169 dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed; 170 dsl_dataset_phys(ds)->ds_unique_bytes += used; 171 172 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) { 173 ds->ds_feature_activation[SPA_FEATURE_LARGE_BLOCKS] = 174 (void *)B_TRUE; 175 } 176 177 178 f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp)); 179 if (f != SPA_FEATURE_NONE) { 180 ASSERT3S(spa_feature_table[f].fi_type, ==, 181 ZFEATURE_TYPE_BOOLEAN); 182 ds->ds_feature_activation[f] = (void *)B_TRUE; 183 } 184 185 f = zio_compress_to_feature(BP_GET_COMPRESS(bp)); 186 if (f != SPA_FEATURE_NONE) { 187 ASSERT3S(spa_feature_table[f].fi_type, ==, 188 ZFEATURE_TYPE_BOOLEAN); 189 ds->ds_feature_activation[f] = (void *)B_TRUE; 190 } 191 192 /* 193 * Track block for livelist, but ignore embedded blocks because 194 * they do not need to be freed. 195 */ 196 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) && 197 BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg && 198 !(BP_IS_EMBEDDED(bp))) { 199 ASSERT(dsl_dir_is_clone(ds->ds_dir)); 200 ASSERT(spa_feature_is_enabled(spa, 201 SPA_FEATURE_LIVELIST)); 202 bplist_append(&ds->ds_dir->dd_pending_allocs, bp); 203 } 204 205 mutex_exit(&ds->ds_lock); 206 dsl_dir_diduse_transfer_space(ds->ds_dir, delta, 207 compressed, uncompressed, used, 208 DD_USED_REFRSRV, DD_USED_HEAD, tx); 209 } 210 211 /* 212 * Called when the specified segment has been remapped, and is thus no 213 * longer referenced in the head dataset. The vdev must be indirect. 214 * 215 * If the segment is referenced by a snapshot, put it on the remap deadlist. 216 * Otherwise, add this segment to the obsolete spacemap. 217 */ 218 void 219 dsl_dataset_block_remapped(dsl_dataset_t *ds, uint64_t vdev, uint64_t offset, 220 uint64_t size, uint64_t birth, dmu_tx_t *tx) 221 { 222 spa_t *spa = ds->ds_dir->dd_pool->dp_spa; 223 224 ASSERT(dmu_tx_is_syncing(tx)); 225 ASSERT(birth <= tx->tx_txg); 226 ASSERT(!ds->ds_is_snapshot); 227 228 if (birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 229 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx); 230 } else { 231 blkptr_t fakebp; 232 dva_t *dva = &fakebp.blk_dva[0]; 233 234 ASSERT(ds != NULL); 235 236 mutex_enter(&ds->ds_remap_deadlist_lock); 237 if (!dsl_dataset_remap_deadlist_exists(ds)) { 238 dsl_dataset_create_remap_deadlist(ds, tx); 239 } 240 mutex_exit(&ds->ds_remap_deadlist_lock); 241 242 BP_ZERO(&fakebp); 243 BP_SET_LOGICAL_BIRTH(&fakebp, birth); 244 DVA_SET_VDEV(dva, vdev); 245 DVA_SET_OFFSET(dva, offset); 246 DVA_SET_ASIZE(dva, size); 247 dsl_deadlist_insert(&ds->ds_remap_deadlist, &fakebp, B_FALSE, 248 tx); 249 } 250 } 251 252 int 253 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 254 boolean_t async) 255 { 256 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 257 258 int used = bp_get_dsize_sync(spa, bp); 259 int compressed = BP_GET_PSIZE(bp); 260 int uncompressed = BP_GET_UCSIZE(bp); 261 262 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) 263 return (0); 264 265 ASSERT(dmu_tx_is_syncing(tx)); 266 ASSERT(BP_GET_LOGICAL_BIRTH(bp) <= tx->tx_txg); 267 268 if (ds == NULL) { 269 dsl_free(tx->tx_pool, tx->tx_txg, bp); 270 dsl_pool_mos_diduse_space(tx->tx_pool, 271 -used, -compressed, -uncompressed); 272 return (used); 273 } 274 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 275 276 ASSERT(!ds->ds_is_snapshot); 277 dmu_buf_will_dirty(ds->ds_dbuf, tx); 278 279 /* 280 * Track block for livelist, but ignore embedded blocks because 281 * they do not need to be freed. 282 */ 283 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) && 284 BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg && 285 !(BP_IS_EMBEDDED(bp))) { 286 ASSERT(dsl_dir_is_clone(ds->ds_dir)); 287 ASSERT(spa_feature_is_enabled(spa, 288 SPA_FEATURE_LIVELIST)); 289 bplist_append(&ds->ds_dir->dd_pending_frees, bp); 290 } 291 292 if (BP_GET_LOGICAL_BIRTH(bp) > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 293 int64_t delta; 294 295 /* 296 * Put blocks that would create IO on the pool's deadlist for 297 * dsl_process_async_destroys() to find. This is to prevent 298 * zio_free() from creating a ZIO_TYPE_FREE IO for them, which 299 * are very heavy and can lead to out-of-memory conditions if 300 * something tries to free millions of blocks on the same txg. 301 */ 302 boolean_t defer = spa_version(spa) >= SPA_VERSION_DEADLISTS && 303 (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) || 304 brt_maybe_exists(spa, bp)); 305 306 if (defer) { 307 dprintf_bp(bp, "putting on free list: %s", ""); 308 bpobj_enqueue(&ds->ds_dir->dd_pool->dp_free_bpobj, 309 bp, B_FALSE, tx); 310 } else { 311 dprintf_bp(bp, "freeing ds=%llu", 312 (u_longlong_t)ds->ds_object); 313 dsl_free(tx->tx_pool, tx->tx_txg, bp); 314 } 315 316 mutex_enter(&ds->ds_lock); 317 ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used || 318 !DS_UNIQUE_IS_ACCURATE(ds)); 319 delta = parent_delta(ds, -used); 320 dsl_dataset_phys(ds)->ds_unique_bytes -= used; 321 mutex_exit(&ds->ds_lock); 322 323 dsl_dir_diduse_transfer_space(ds->ds_dir, 324 delta, -compressed, -uncompressed, -used, 325 DD_USED_REFRSRV, DD_USED_HEAD, tx); 326 327 if (defer) 328 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, 329 DD_USED_HEAD, used, compressed, uncompressed, tx); 330 } else { 331 dprintf_bp(bp, "putting on dead list: %s", ""); 332 if (async) { 333 /* 334 * We are here as part of zio's write done callback, 335 * which means we're a zio interrupt thread. We can't 336 * call dsl_deadlist_insert() now because it may block 337 * waiting for I/O. Instead, put bp on the deferred 338 * queue and let dsl_pool_sync() finish the job. 339 */ 340 bplist_append(&ds->ds_pending_deadlist, bp); 341 } else { 342 dsl_deadlist_insert(&ds->ds_deadlist, bp, B_FALSE, tx); 343 } 344 ASSERT3U(ds->ds_prev->ds_object, ==, 345 dsl_dataset_phys(ds)->ds_prev_snap_obj); 346 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0); 347 /* if (logical birth > prev prev snap txg) prev unique += bs */ 348 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 349 ds->ds_object && BP_GET_LOGICAL_BIRTH(bp) > 350 dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) { 351 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 352 mutex_enter(&ds->ds_prev->ds_lock); 353 dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used; 354 mutex_exit(&ds->ds_prev->ds_lock); 355 } 356 if (BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg) { 357 dsl_dir_transfer_space(ds->ds_dir, used, 358 DD_USED_HEAD, DD_USED_SNAP, tx); 359 } 360 } 361 362 dsl_bookmark_block_killed(ds, bp, tx); 363 364 mutex_enter(&ds->ds_lock); 365 ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used); 366 dsl_dataset_phys(ds)->ds_referenced_bytes -= used; 367 ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed); 368 dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed; 369 ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed); 370 dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed; 371 mutex_exit(&ds->ds_lock); 372 373 return (used); 374 } 375 376 struct feature_type_uint64_array_arg { 377 uint64_t length; 378 uint64_t *array; 379 }; 380 381 static void 382 unload_zfeature(dsl_dataset_t *ds, spa_feature_t f) 383 { 384 switch (spa_feature_table[f].fi_type) { 385 case ZFEATURE_TYPE_BOOLEAN: 386 break; 387 case ZFEATURE_TYPE_UINT64_ARRAY: 388 { 389 struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f]; 390 kmem_free(ftuaa->array, ftuaa->length * sizeof (uint64_t)); 391 kmem_free(ftuaa, sizeof (*ftuaa)); 392 break; 393 } 394 default: 395 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type); 396 } 397 } 398 399 static int 400 load_zfeature(objset_t *mos, dsl_dataset_t *ds, spa_feature_t f) 401 { 402 int err = 0; 403 switch (spa_feature_table[f].fi_type) { 404 case ZFEATURE_TYPE_BOOLEAN: 405 err = zap_contains(mos, ds->ds_object, 406 spa_feature_table[f].fi_guid); 407 if (err == 0) { 408 ds->ds_feature[f] = (void *)B_TRUE; 409 } else { 410 ASSERT3U(err, ==, ENOENT); 411 err = 0; 412 } 413 break; 414 case ZFEATURE_TYPE_UINT64_ARRAY: 415 { 416 uint64_t int_size, num_int; 417 uint64_t *data; 418 err = zap_length(mos, ds->ds_object, 419 spa_feature_table[f].fi_guid, &int_size, &num_int); 420 if (err != 0) { 421 ASSERT3U(err, ==, ENOENT); 422 err = 0; 423 break; 424 } 425 ASSERT3U(int_size, ==, sizeof (uint64_t)); 426 data = kmem_alloc(int_size * num_int, KM_SLEEP); 427 VERIFY0(zap_lookup(mos, ds->ds_object, 428 spa_feature_table[f].fi_guid, int_size, num_int, data)); 429 struct feature_type_uint64_array_arg *ftuaa = 430 kmem_alloc(sizeof (*ftuaa), KM_SLEEP); 431 ftuaa->length = num_int; 432 ftuaa->array = data; 433 ds->ds_feature[f] = ftuaa; 434 break; 435 } 436 default: 437 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type); 438 } 439 return (err); 440 } 441 442 /* 443 * We have to release the fsid synchronously or we risk that a subsequent 444 * mount of the same dataset will fail to unique_insert the fsid. This 445 * failure would manifest itself as the fsid of this dataset changing 446 * between mounts which makes NFS clients quite unhappy. 447 */ 448 static void 449 dsl_dataset_evict_sync(void *dbu) 450 { 451 dsl_dataset_t *ds = dbu; 452 453 ASSERT(ds->ds_owner == NULL); 454 455 unique_remove(ds->ds_fsid_guid); 456 } 457 458 static void 459 dsl_dataset_evict_async(void *dbu) 460 { 461 dsl_dataset_t *ds = dbu; 462 463 ASSERT(ds->ds_owner == NULL); 464 465 ds->ds_dbuf = NULL; 466 467 if (ds->ds_objset != NULL) 468 dmu_objset_evict(ds->ds_objset); 469 470 if (ds->ds_prev) { 471 dsl_dataset_rele(ds->ds_prev, ds); 472 ds->ds_prev = NULL; 473 } 474 475 dsl_bookmark_fini_ds(ds); 476 477 bplist_destroy(&ds->ds_pending_deadlist); 478 if (dsl_deadlist_is_open(&ds->ds_deadlist)) 479 dsl_deadlist_close(&ds->ds_deadlist); 480 if (dsl_deadlist_is_open(&ds->ds_remap_deadlist)) 481 dsl_deadlist_close(&ds->ds_remap_deadlist); 482 if (ds->ds_dir) 483 dsl_dir_async_rele(ds->ds_dir, ds); 484 485 ASSERT(!list_link_active(&ds->ds_synced_link)); 486 487 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 488 if (dsl_dataset_feature_is_active(ds, f)) 489 unload_zfeature(ds, f); 490 } 491 492 list_destroy(&ds->ds_prop_cbs); 493 mutex_destroy(&ds->ds_lock); 494 mutex_destroy(&ds->ds_opening_lock); 495 mutex_destroy(&ds->ds_sendstream_lock); 496 mutex_destroy(&ds->ds_remap_deadlist_lock); 497 zfs_refcount_destroy(&ds->ds_longholds); 498 rrw_destroy(&ds->ds_bp_rwlock); 499 500 kmem_free(ds, sizeof (dsl_dataset_t)); 501 } 502 503 int 504 dsl_dataset_get_snapname(dsl_dataset_t *ds) 505 { 506 dsl_dataset_phys_t *headphys; 507 int err; 508 dmu_buf_t *headdbuf; 509 dsl_pool_t *dp = ds->ds_dir->dd_pool; 510 objset_t *mos = dp->dp_meta_objset; 511 512 if (ds->ds_snapname[0]) 513 return (0); 514 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 515 return (0); 516 517 err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 518 FTAG, &headdbuf); 519 if (err != 0) 520 return (err); 521 headphys = headdbuf->db_data; 522 err = zap_value_search(dp->dp_meta_objset, 523 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname, 524 sizeof (ds->ds_snapname)); 525 if (err != 0 && zfs_recover == B_TRUE) { 526 err = 0; 527 (void) snprintf(ds->ds_snapname, sizeof (ds->ds_snapname), 528 "SNAPOBJ=%llu-ERR=%d", 529 (unsigned long long)ds->ds_object, err); 530 } 531 dmu_buf_rele(headdbuf, FTAG); 532 return (err); 533 } 534 535 int 536 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 537 { 538 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 539 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 540 matchtype_t mt = 0; 541 int err; 542 543 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 544 mt = MT_NORMALIZE; 545 546 err = zap_lookup_norm(mos, snapobj, name, 8, 1, 547 value, mt, NULL, 0, NULL); 548 if (err == ENOTSUP && (mt & MT_NORMALIZE)) 549 err = zap_lookup(mos, snapobj, name, 8, 1, value); 550 return (err); 551 } 552 553 int 554 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 555 boolean_t adj_cnt) 556 { 557 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 558 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 559 matchtype_t mt = 0; 560 int err; 561 562 dsl_dir_snap_cmtime_update(ds->ds_dir, tx); 563 564 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 565 mt = MT_NORMALIZE; 566 567 err = zap_remove_norm(mos, snapobj, name, mt, tx); 568 if (err == ENOTSUP && (mt & MT_NORMALIZE)) 569 err = zap_remove(mos, snapobj, name, tx); 570 571 if (err == 0 && adj_cnt) 572 dsl_fs_ss_count_adjust(ds->ds_dir, -1, 573 DD_FIELD_SNAPSHOT_COUNT, tx); 574 575 return (err); 576 } 577 578 boolean_t 579 dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, const void *tag) 580 { 581 dmu_buf_t *dbuf = ds->ds_dbuf; 582 boolean_t result = B_FALSE; 583 584 if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset, 585 ds->ds_object, DMU_BONUS_BLKID, tag)) { 586 587 if (ds == dmu_buf_get_user(dbuf)) 588 result = B_TRUE; 589 else 590 dmu_buf_rele(dbuf, tag); 591 } 592 593 return (result); 594 } 595 596 int 597 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, const void *tag, 598 dsl_dataset_t **dsp) 599 { 600 objset_t *mos = dp->dp_meta_objset; 601 dmu_buf_t *dbuf; 602 dsl_dataset_t *ds; 603 int err; 604 dmu_object_info_t doi; 605 606 ASSERT(dsl_pool_config_held(dp)); 607 608 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 609 if (err != 0) 610 return (err); 611 612 /* Make sure dsobj has the correct object type. */ 613 dmu_object_info_from_db(dbuf, &doi); 614 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 615 dmu_buf_rele(dbuf, tag); 616 return (SET_ERROR(EINVAL)); 617 } 618 619 ds = dmu_buf_get_user(dbuf); 620 if (ds == NULL) { 621 dsl_dataset_t *winner = NULL; 622 623 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 624 ds->ds_dbuf = dbuf; 625 ds->ds_object = dsobj; 626 ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0; 627 list_link_init(&ds->ds_synced_link); 628 629 err = dsl_dir_hold_obj(dp, dsl_dataset_phys(ds)->ds_dir_obj, 630 NULL, ds, &ds->ds_dir); 631 if (err != 0) { 632 kmem_free(ds, sizeof (dsl_dataset_t)); 633 dmu_buf_rele(dbuf, tag); 634 return (err); 635 } 636 637 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 638 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 639 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 640 mutex_init(&ds->ds_remap_deadlist_lock, 641 NULL, MUTEX_DEFAULT, NULL); 642 rrw_init(&ds->ds_bp_rwlock, B_FALSE); 643 zfs_refcount_create(&ds->ds_longholds); 644 645 bplist_create(&ds->ds_pending_deadlist); 646 647 list_create(&ds->ds_sendstreams, sizeof (dmu_sendstatus_t), 648 offsetof(dmu_sendstatus_t, dss_link)); 649 650 list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t), 651 offsetof(dsl_prop_cb_record_t, cbr_ds_node)); 652 653 if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 654 spa_feature_t f; 655 656 for (f = 0; f < SPA_FEATURES; f++) { 657 if (!(spa_feature_table[f].fi_flags & 658 ZFEATURE_FLAG_PER_DATASET)) 659 continue; 660 err = load_zfeature(mos, ds, f); 661 } 662 } 663 664 if (!ds->ds_is_snapshot) { 665 ds->ds_snapname[0] = '\0'; 666 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 667 err = dsl_dataset_hold_obj(dp, 668 dsl_dataset_phys(ds)->ds_prev_snap_obj, 669 ds, &ds->ds_prev); 670 } 671 if (err != 0) 672 goto after_dsl_bookmark_fini; 673 err = dsl_bookmark_init_ds(ds); 674 } else { 675 if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 676 err = dsl_dataset_get_snapname(ds); 677 if (err == 0 && 678 dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 679 err = zap_count( 680 ds->ds_dir->dd_pool->dp_meta_objset, 681 dsl_dataset_phys(ds)->ds_userrefs_obj, 682 &ds->ds_userrefs); 683 } 684 } 685 686 if (err == 0 && !ds->ds_is_snapshot) { 687 err = dsl_prop_get_int_ds(ds, 688 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 689 &ds->ds_reserved); 690 if (err == 0) { 691 err = dsl_prop_get_int_ds(ds, 692 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 693 &ds->ds_quota); 694 } 695 } else { 696 ds->ds_reserved = ds->ds_quota = 0; 697 } 698 699 if (err == 0 && ds->ds_dir->dd_crypto_obj != 0 && 700 ds->ds_is_snapshot && 701 zap_contains(mos, dsobj, DS_FIELD_IVSET_GUID) != 0) { 702 dp->dp_spa->spa_errata = 703 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; 704 } 705 706 if (err == 0) { 707 err = dsl_deadlist_open(&ds->ds_deadlist, 708 mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 709 } 710 if (err == 0) { 711 uint64_t remap_deadlist_obj = 712 dsl_dataset_get_remap_deadlist_object(ds); 713 if (remap_deadlist_obj != 0) { 714 err = dsl_deadlist_open(&ds->ds_remap_deadlist, 715 mos, remap_deadlist_obj); 716 } 717 } 718 719 dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync, 720 dsl_dataset_evict_async, &ds->ds_dbuf); 721 if (err == 0) 722 winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu); 723 724 if (err != 0 || winner != NULL) { 725 if (dsl_deadlist_is_open(&ds->ds_deadlist)) 726 dsl_deadlist_close(&ds->ds_deadlist); 727 if (dsl_deadlist_is_open(&ds->ds_remap_deadlist)) 728 dsl_deadlist_close(&ds->ds_remap_deadlist); 729 dsl_bookmark_fini_ds(ds); 730 after_dsl_bookmark_fini: 731 if (ds->ds_prev) 732 dsl_dataset_rele(ds->ds_prev, ds); 733 dsl_dir_rele(ds->ds_dir, ds); 734 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 735 if (dsl_dataset_feature_is_active(ds, f)) 736 unload_zfeature(ds, f); 737 } 738 739 list_destroy(&ds->ds_prop_cbs); 740 list_destroy(&ds->ds_sendstreams); 741 bplist_destroy(&ds->ds_pending_deadlist); 742 mutex_destroy(&ds->ds_lock); 743 mutex_destroy(&ds->ds_opening_lock); 744 mutex_destroy(&ds->ds_sendstream_lock); 745 mutex_destroy(&ds->ds_remap_deadlist_lock); 746 zfs_refcount_destroy(&ds->ds_longholds); 747 rrw_destroy(&ds->ds_bp_rwlock); 748 kmem_free(ds, sizeof (dsl_dataset_t)); 749 if (err != 0) { 750 dmu_buf_rele(dbuf, tag); 751 return (err); 752 } 753 ds = winner; 754 } else { 755 ds->ds_fsid_guid = 756 unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 757 if (ds->ds_fsid_guid != 758 dsl_dataset_phys(ds)->ds_fsid_guid) { 759 zfs_dbgmsg("ds_fsid_guid changed from " 760 "%llx to %llx for pool %s dataset id %llu", 761 (long long) 762 dsl_dataset_phys(ds)->ds_fsid_guid, 763 (long long)ds->ds_fsid_guid, 764 spa_name(dp->dp_spa), 765 (u_longlong_t)dsobj); 766 } 767 } 768 } 769 770 ASSERT3P(ds->ds_dbuf, ==, dbuf); 771 ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 772 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 773 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 774 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 775 *dsp = ds; 776 777 return (0); 778 } 779 780 int 781 dsl_dataset_create_key_mapping(dsl_dataset_t *ds) 782 { 783 dsl_dir_t *dd = ds->ds_dir; 784 785 if (dd->dd_crypto_obj == 0) 786 return (0); 787 788 return (spa_keystore_create_mapping(dd->dd_pool->dp_spa, 789 ds, ds, &ds->ds_key_mapping)); 790 } 791 792 int 793 dsl_dataset_hold_obj_flags(dsl_pool_t *dp, uint64_t dsobj, 794 ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp) 795 { 796 int err; 797 798 err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 799 if (err != 0) 800 return (err); 801 802 ASSERT3P(*dsp, !=, NULL); 803 804 if (flags & DS_HOLD_FLAG_DECRYPT) { 805 err = dsl_dataset_create_key_mapping(*dsp); 806 if (err != 0) 807 dsl_dataset_rele(*dsp, tag); 808 } 809 810 return (err); 811 } 812 813 int 814 dsl_dataset_hold_flags(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags, 815 const void *tag, dsl_dataset_t **dsp) 816 { 817 dsl_dir_t *dd; 818 const char *snapname; 819 uint64_t obj; 820 int err = 0; 821 dsl_dataset_t *ds; 822 823 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 824 if (err != 0) 825 return (err); 826 827 ASSERT(dsl_pool_config_held(dp)); 828 obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 829 if (obj != 0) 830 err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, &ds); 831 else 832 err = SET_ERROR(ENOENT); 833 834 /* we may be looking for a snapshot */ 835 if (err == 0 && snapname != NULL) { 836 dsl_dataset_t *snap_ds; 837 838 if (*snapname++ != '@') { 839 dsl_dataset_rele_flags(ds, flags, tag); 840 dsl_dir_rele(dd, FTAG); 841 return (SET_ERROR(ENOENT)); 842 } 843 844 dprintf("looking for snapshot '%s'\n", snapname); 845 err = dsl_dataset_snap_lookup(ds, snapname, &obj); 846 if (err == 0) { 847 err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, 848 &snap_ds); 849 } 850 dsl_dataset_rele_flags(ds, flags, tag); 851 852 if (err == 0) { 853 mutex_enter(&snap_ds->ds_lock); 854 if (snap_ds->ds_snapname[0] == 0) 855 (void) strlcpy(snap_ds->ds_snapname, snapname, 856 sizeof (snap_ds->ds_snapname)); 857 mutex_exit(&snap_ds->ds_lock); 858 ds = snap_ds; 859 } 860 } 861 if (err == 0) 862 *dsp = ds; 863 dsl_dir_rele(dd, FTAG); 864 return (err); 865 } 866 867 int 868 dsl_dataset_hold(dsl_pool_t *dp, const char *name, const void *tag, 869 dsl_dataset_t **dsp) 870 { 871 return (dsl_dataset_hold_flags(dp, name, 0, tag, dsp)); 872 } 873 874 static int 875 dsl_dataset_own_obj_impl(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags, 876 const void *tag, boolean_t override, dsl_dataset_t **dsp) 877 { 878 int err = dsl_dataset_hold_obj_flags(dp, dsobj, flags, tag, dsp); 879 if (err != 0) 880 return (err); 881 if (!dsl_dataset_tryown(*dsp, tag, override)) { 882 dsl_dataset_rele_flags(*dsp, flags, tag); 883 *dsp = NULL; 884 return (SET_ERROR(EBUSY)); 885 } 886 return (0); 887 } 888 889 890 int 891 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags, 892 const void *tag, dsl_dataset_t **dsp) 893 { 894 return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_FALSE, dsp)); 895 } 896 897 int 898 dsl_dataset_own_obj_force(dsl_pool_t *dp, uint64_t dsobj, 899 ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp) 900 { 901 return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_TRUE, dsp)); 902 } 903 904 static int 905 dsl_dataset_own_impl(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags, 906 const void *tag, boolean_t override, dsl_dataset_t **dsp) 907 { 908 int err = dsl_dataset_hold_flags(dp, name, flags, tag, dsp); 909 if (err != 0) 910 return (err); 911 if (!dsl_dataset_tryown(*dsp, tag, override)) { 912 dsl_dataset_rele_flags(*dsp, flags, tag); 913 return (SET_ERROR(EBUSY)); 914 } 915 return (0); 916 } 917 918 int 919 dsl_dataset_own_force(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags, 920 const void *tag, dsl_dataset_t **dsp) 921 { 922 return (dsl_dataset_own_impl(dp, name, flags, tag, B_TRUE, dsp)); 923 } 924 925 int 926 dsl_dataset_own(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags, 927 const void *tag, dsl_dataset_t **dsp) 928 { 929 return (dsl_dataset_own_impl(dp, name, flags, tag, B_FALSE, dsp)); 930 } 931 932 /* 933 * See the comment above dsl_pool_hold() for details. In summary, a long 934 * hold is used to prevent destruction of a dataset while the pool hold 935 * is dropped, allowing other concurrent operations (e.g. spa_sync()). 936 * 937 * The dataset and pool must be held when this function is called. After it 938 * is called, the pool hold may be released while the dataset is still held 939 * and accessed. 940 */ 941 void 942 dsl_dataset_long_hold(dsl_dataset_t *ds, const void *tag) 943 { 944 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 945 (void) zfs_refcount_add(&ds->ds_longholds, tag); 946 } 947 948 void 949 dsl_dataset_long_rele(dsl_dataset_t *ds, const void *tag) 950 { 951 (void) zfs_refcount_remove(&ds->ds_longholds, tag); 952 } 953 954 /* Return B_TRUE if there are any long holds on this dataset. */ 955 boolean_t 956 dsl_dataset_long_held(dsl_dataset_t *ds) 957 { 958 return (!zfs_refcount_is_zero(&ds->ds_longholds)); 959 } 960 961 void 962 dsl_dataset_name(dsl_dataset_t *ds, char *name) 963 { 964 if (ds == NULL) { 965 (void) strlcpy(name, "mos", ZFS_MAX_DATASET_NAME_LEN); 966 } else { 967 dsl_dir_name(ds->ds_dir, name); 968 VERIFY0(dsl_dataset_get_snapname(ds)); 969 if (ds->ds_snapname[0]) { 970 VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN), 971 <, ZFS_MAX_DATASET_NAME_LEN); 972 /* 973 * We use a "recursive" mutex so that we 974 * can call dprintf_ds() with ds_lock held. 975 */ 976 if (!MUTEX_HELD(&ds->ds_lock)) { 977 mutex_enter(&ds->ds_lock); 978 VERIFY3U(strlcat(name, ds->ds_snapname, 979 ZFS_MAX_DATASET_NAME_LEN), <, 980 ZFS_MAX_DATASET_NAME_LEN); 981 mutex_exit(&ds->ds_lock); 982 } else { 983 VERIFY3U(strlcat(name, ds->ds_snapname, 984 ZFS_MAX_DATASET_NAME_LEN), <, 985 ZFS_MAX_DATASET_NAME_LEN); 986 } 987 } 988 } 989 } 990 991 int 992 dsl_dataset_namelen(dsl_dataset_t *ds) 993 { 994 VERIFY0(dsl_dataset_get_snapname(ds)); 995 mutex_enter(&ds->ds_lock); 996 int len = strlen(ds->ds_snapname); 997 mutex_exit(&ds->ds_lock); 998 /* add '@' if ds is a snap */ 999 if (len > 0) 1000 len++; 1001 len += dsl_dir_namelen(ds->ds_dir); 1002 return (len); 1003 } 1004 1005 void 1006 dsl_dataset_rele(dsl_dataset_t *ds, const void *tag) 1007 { 1008 dmu_buf_rele(ds->ds_dbuf, tag); 1009 } 1010 1011 void 1012 dsl_dataset_remove_key_mapping(dsl_dataset_t *ds) 1013 { 1014 dsl_dir_t *dd = ds->ds_dir; 1015 1016 if (dd == NULL || dd->dd_crypto_obj == 0) 1017 return; 1018 1019 (void) spa_keystore_remove_mapping(dd->dd_pool->dp_spa, 1020 ds->ds_object, ds); 1021 } 1022 1023 void 1024 dsl_dataset_rele_flags(dsl_dataset_t *ds, ds_hold_flags_t flags, 1025 const void *tag) 1026 { 1027 if (flags & DS_HOLD_FLAG_DECRYPT) 1028 dsl_dataset_remove_key_mapping(ds); 1029 1030 dsl_dataset_rele(ds, tag); 1031 } 1032 1033 void 1034 dsl_dataset_disown(dsl_dataset_t *ds, ds_hold_flags_t flags, const void *tag) 1035 { 1036 ASSERT3P(ds->ds_owner, ==, tag); 1037 ASSERT(ds->ds_dbuf != NULL); 1038 1039 mutex_enter(&ds->ds_lock); 1040 ds->ds_owner = NULL; 1041 mutex_exit(&ds->ds_lock); 1042 dsl_dataset_long_rele(ds, tag); 1043 dsl_dataset_rele_flags(ds, flags, tag); 1044 } 1045 1046 boolean_t 1047 dsl_dataset_tryown(dsl_dataset_t *ds, const void *tag, boolean_t override) 1048 { 1049 boolean_t gotit = FALSE; 1050 1051 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 1052 mutex_enter(&ds->ds_lock); 1053 if (ds->ds_owner == NULL && (override || !(DS_IS_INCONSISTENT(ds) || 1054 (dsl_dataset_feature_is_active(ds, 1055 SPA_FEATURE_REDACTED_DATASETS) && 1056 !zfs_allow_redacted_dataset_mount)))) { 1057 ds->ds_owner = tag; 1058 dsl_dataset_long_hold(ds, tag); 1059 gotit = TRUE; 1060 } 1061 mutex_exit(&ds->ds_lock); 1062 return (gotit); 1063 } 1064 1065 boolean_t 1066 dsl_dataset_has_owner(dsl_dataset_t *ds) 1067 { 1068 boolean_t rv; 1069 mutex_enter(&ds->ds_lock); 1070 rv = (ds->ds_owner != NULL); 1071 mutex_exit(&ds->ds_lock); 1072 return (rv); 1073 } 1074 1075 static boolean_t 1076 zfeature_active(spa_feature_t f, void *arg) 1077 { 1078 switch (spa_feature_table[f].fi_type) { 1079 case ZFEATURE_TYPE_BOOLEAN: { 1080 boolean_t val = (boolean_t)(uintptr_t)arg; 1081 ASSERT(val == B_FALSE || val == B_TRUE); 1082 return (val); 1083 } 1084 case ZFEATURE_TYPE_UINT64_ARRAY: 1085 /* 1086 * In this case, arg is a uint64_t array. The feature is active 1087 * if the array is non-null. 1088 */ 1089 return (arg != NULL); 1090 default: 1091 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type); 1092 return (B_FALSE); 1093 } 1094 } 1095 1096 boolean_t 1097 dsl_dataset_feature_is_active(dsl_dataset_t *ds, spa_feature_t f) 1098 { 1099 return (zfeature_active(f, ds->ds_feature[f])); 1100 } 1101 1102 /* 1103 * The buffers passed out by this function are references to internal buffers; 1104 * they should not be freed by callers of this function, and they should not be 1105 * used after the dataset has been released. 1106 */ 1107 boolean_t 1108 dsl_dataset_get_uint64_array_feature(dsl_dataset_t *ds, spa_feature_t f, 1109 uint64_t *outlength, uint64_t **outp) 1110 { 1111 VERIFY(spa_feature_table[f].fi_type & ZFEATURE_TYPE_UINT64_ARRAY); 1112 if (!dsl_dataset_feature_is_active(ds, f)) { 1113 return (B_FALSE); 1114 } 1115 struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f]; 1116 *outp = ftuaa->array; 1117 *outlength = ftuaa->length; 1118 return (B_TRUE); 1119 } 1120 1121 void 1122 dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, void *arg, 1123 dmu_tx_t *tx) 1124 { 1125 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1126 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 1127 uint64_t zero = 0; 1128 1129 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 1130 1131 spa_feature_incr(spa, f, tx); 1132 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 1133 1134 switch (spa_feature_table[f].fi_type) { 1135 case ZFEATURE_TYPE_BOOLEAN: 1136 ASSERT3S((boolean_t)(uintptr_t)arg, ==, B_TRUE); 1137 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 1138 sizeof (zero), 1, &zero, tx)); 1139 break; 1140 case ZFEATURE_TYPE_UINT64_ARRAY: 1141 { 1142 struct feature_type_uint64_array_arg *ftuaa = arg; 1143 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 1144 sizeof (uint64_t), ftuaa->length, ftuaa->array, tx)); 1145 break; 1146 } 1147 default: 1148 panic("Invalid zfeature type %d", spa_feature_table[f].fi_type); 1149 } 1150 } 1151 1152 static void 1153 dsl_dataset_deactivate_feature_impl(dsl_dataset_t *ds, spa_feature_t f, 1154 dmu_tx_t *tx) 1155 { 1156 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1157 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 1158 uint64_t dsobj = ds->ds_object; 1159 1160 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 1161 1162 VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx)); 1163 spa_feature_decr(spa, f, tx); 1164 ds->ds_feature[f] = NULL; 1165 } 1166 1167 void 1168 dsl_dataset_deactivate_feature(dsl_dataset_t *ds, spa_feature_t f, dmu_tx_t *tx) 1169 { 1170 unload_zfeature(ds, f); 1171 dsl_dataset_deactivate_feature_impl(ds, f, tx); 1172 } 1173 1174 uint64_t 1175 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 1176 dsl_crypto_params_t *dcp, uint64_t flags, dmu_tx_t *tx) 1177 { 1178 dsl_pool_t *dp = dd->dd_pool; 1179 dmu_buf_t *dbuf; 1180 dsl_dataset_phys_t *dsphys; 1181 uint64_t dsobj; 1182 objset_t *mos = dp->dp_meta_objset; 1183 1184 if (origin == NULL) 1185 origin = dp->dp_origin_snap; 1186 1187 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 1188 ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 1189 ASSERT(dmu_tx_is_syncing(tx)); 1190 ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 1191 1192 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1193 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 1194 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1195 dmu_buf_will_dirty(dbuf, tx); 1196 dsphys = dbuf->db_data; 1197 memset(dsphys, 0, sizeof (dsl_dataset_phys_t)); 1198 dsphys->ds_dir_obj = dd->dd_object; 1199 dsphys->ds_flags = flags; 1200 dsphys->ds_fsid_guid = unique_create(); 1201 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1202 sizeof (dsphys->ds_guid)); 1203 dsphys->ds_snapnames_zapobj = 1204 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 1205 DMU_OT_NONE, 0, tx); 1206 dsphys->ds_creation_time = gethrestime_sec(); 1207 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 1208 1209 if (origin == NULL) { 1210 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 1211 } else { 1212 dsl_dataset_t *ohds; /* head of the origin snapshot */ 1213 1214 dsphys->ds_prev_snap_obj = origin->ds_object; 1215 dsphys->ds_prev_snap_txg = 1216 dsl_dataset_phys(origin)->ds_creation_txg; 1217 dsphys->ds_referenced_bytes = 1218 dsl_dataset_phys(origin)->ds_referenced_bytes; 1219 dsphys->ds_compressed_bytes = 1220 dsl_dataset_phys(origin)->ds_compressed_bytes; 1221 dsphys->ds_uncompressed_bytes = 1222 dsl_dataset_phys(origin)->ds_uncompressed_bytes; 1223 rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG); 1224 dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 1225 rrw_exit(&origin->ds_bp_rwlock, FTAG); 1226 1227 /* 1228 * Inherit flags that describe the dataset's contents 1229 * (INCONSISTENT) or properties (Case Insensitive). 1230 */ 1231 dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 1232 (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 1233 1234 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1235 if (zfeature_active(f, origin->ds_feature[f])) { 1236 dsl_dataset_activate_feature(dsobj, f, 1237 origin->ds_feature[f], tx); 1238 } 1239 } 1240 1241 dmu_buf_will_dirty(origin->ds_dbuf, tx); 1242 dsl_dataset_phys(origin)->ds_num_children++; 1243 1244 VERIFY0(dsl_dataset_hold_obj(dp, 1245 dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 1246 FTAG, &ohds)); 1247 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 1248 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 1249 dsl_dataset_rele(ohds, FTAG); 1250 1251 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 1252 if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 1253 dsl_dataset_phys(origin)->ds_next_clones_obj = 1254 zap_create(mos, 1255 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 1256 } 1257 VERIFY0(zap_add_int(mos, 1258 dsl_dataset_phys(origin)->ds_next_clones_obj, 1259 dsobj, tx)); 1260 } 1261 1262 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1263 dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 1264 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 1265 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 1266 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 1267 dsl_dir_phys(origin->ds_dir)->dd_clones = 1268 zap_create(mos, 1269 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 1270 } 1271 VERIFY0(zap_add_int(mos, 1272 dsl_dir_phys(origin->ds_dir)->dd_clones, 1273 dsobj, tx)); 1274 } 1275 } 1276 1277 /* handle encryption */ 1278 dsl_dataset_create_crypt_sync(dsobj, dd, origin, dcp, tx); 1279 1280 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1281 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1282 1283 dmu_buf_rele(dbuf, FTAG); 1284 1285 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1286 dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 1287 1288 return (dsobj); 1289 } 1290 1291 static void 1292 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 1293 { 1294 objset_t *os; 1295 1296 VERIFY0(dmu_objset_from_ds(ds, &os)); 1297 if (memcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) { 1298 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1299 zio_t *zio; 1300 1301 memset(&os->os_zil_header, 0, sizeof (os->os_zil_header)); 1302 if (os->os_encrypted) 1303 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE; 1304 1305 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1306 dsl_dataset_sync(ds, zio, tx); 1307 VERIFY0(zio_wait(zio)); 1308 dsl_dataset_sync_done(ds, tx); 1309 } 1310 } 1311 1312 uint64_t 1313 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 1314 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, 1315 dsl_crypto_params_t *dcp, dmu_tx_t *tx) 1316 { 1317 dsl_pool_t *dp = pdd->dd_pool; 1318 uint64_t dsobj, ddobj; 1319 dsl_dir_t *dd; 1320 1321 ASSERT(dmu_tx_is_syncing(tx)); 1322 ASSERT(lastname[0] != '@'); 1323 /* 1324 * Filesystems will eventually have their origin set to dp_origin_snap, 1325 * but that's taken care of in dsl_dataset_create_sync_dd. When 1326 * creating a filesystem, this function is called with origin equal to 1327 * NULL. 1328 */ 1329 if (origin != NULL) 1330 ASSERT3P(origin, !=, dp->dp_origin_snap); 1331 1332 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 1333 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 1334 1335 dsobj = dsl_dataset_create_sync_dd(dd, origin, dcp, 1336 flags & ~DS_CREATE_FLAG_NODIRTY, tx); 1337 1338 dsl_deleg_set_create_perms(dd, tx, cr); 1339 1340 /* 1341 * If we are creating a clone and the livelist feature is enabled, 1342 * add the entry DD_FIELD_LIVELIST to ZAP. 1343 */ 1344 if (origin != NULL && 1345 spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LIVELIST)) { 1346 objset_t *mos = dd->dd_pool->dp_meta_objset; 1347 dsl_dir_zapify(dd, tx); 1348 uint64_t obj = dsl_deadlist_alloc(mos, tx); 1349 VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_LIVELIST, 1350 sizeof (uint64_t), 1, &obj, tx)); 1351 spa_feature_incr(dp->dp_spa, SPA_FEATURE_LIVELIST, tx); 1352 } 1353 1354 /* 1355 * Since we're creating a new node we know it's a leaf, so we can 1356 * initialize the counts if the limit feature is active. 1357 */ 1358 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 1359 uint64_t cnt = 0; 1360 objset_t *os = dd->dd_pool->dp_meta_objset; 1361 1362 dsl_dir_zapify(dd, tx); 1363 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 1364 sizeof (cnt), 1, &cnt, tx)); 1365 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 1366 sizeof (cnt), 1, &cnt, tx)); 1367 } 1368 1369 dsl_dir_rele(dd, FTAG); 1370 1371 /* 1372 * If we are creating a clone, make sure we zero out any stale 1373 * data from the origin snapshots zil header. 1374 */ 1375 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 1376 dsl_dataset_t *ds; 1377 1378 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 1379 dsl_dataset_zero_zil(ds, tx); 1380 dsl_dataset_rele(ds, FTAG); 1381 } 1382 1383 return (dsobj); 1384 } 1385 1386 /* 1387 * The unique space in the head dataset can be calculated by subtracting 1388 * the space used in the most recent snapshot, that is still being used 1389 * in this file system, from the space currently in use. To figure out 1390 * the space in the most recent snapshot still in use, we need to take 1391 * the total space used in the snapshot and subtract out the space that 1392 * has been freed up since the snapshot was taken. 1393 */ 1394 void 1395 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 1396 { 1397 uint64_t mrs_used; 1398 uint64_t dlused, dlcomp, dluncomp; 1399 1400 ASSERT(!ds->ds_is_snapshot); 1401 1402 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 1403 mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 1404 else 1405 mrs_used = 0; 1406 1407 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 1408 1409 ASSERT3U(dlused, <=, mrs_used); 1410 dsl_dataset_phys(ds)->ds_unique_bytes = 1411 dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 1412 1413 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 1414 SPA_VERSION_UNIQUE_ACCURATE) 1415 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1416 } 1417 1418 void 1419 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 1420 dmu_tx_t *tx) 1421 { 1422 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1423 uint64_t count __maybe_unused; 1424 int err; 1425 1426 ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 1427 err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 1428 obj, tx); 1429 /* 1430 * The err should not be ENOENT, but a bug in a previous version 1431 * of the code could cause upgrade_clones_cb() to not set 1432 * ds_next_snap_obj when it should, leading to a missing entry. 1433 * If we knew that the pool was created after 1434 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 1435 * ENOENT. However, at least we can check that we don't have 1436 * too many entries in the next_clones_obj even after failing to 1437 * remove this one. 1438 */ 1439 if (err != ENOENT) 1440 VERIFY0(err); 1441 ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 1442 &count)); 1443 ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 1444 } 1445 1446 1447 blkptr_t * 1448 dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1449 { 1450 return (&dsl_dataset_phys(ds)->ds_bp); 1451 } 1452 1453 spa_t * 1454 dsl_dataset_get_spa(dsl_dataset_t *ds) 1455 { 1456 return (ds->ds_dir->dd_pool->dp_spa); 1457 } 1458 1459 void 1460 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1461 { 1462 dsl_pool_t *dp; 1463 1464 if (ds == NULL) /* this is the meta-objset */ 1465 return; 1466 1467 ASSERT(ds->ds_objset != NULL); 1468 1469 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 1470 panic("dirtying snapshot!"); 1471 1472 /* Must not dirty a dataset in the same txg where it got snapshotted. */ 1473 ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg); 1474 1475 dp = ds->ds_dir->dd_pool; 1476 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 1477 objset_t *os = ds->ds_objset; 1478 1479 /* up the hold count until we can be written out */ 1480 dmu_buf_add_ref(ds->ds_dbuf, ds); 1481 1482 /* if this dataset is encrypted, grab a reference to the DCK */ 1483 if (ds->ds_dir->dd_crypto_obj != 0 && 1484 !os->os_raw_receive && 1485 !os->os_next_write_raw[tx->tx_txg & TXG_MASK]) { 1486 ASSERT3P(ds->ds_key_mapping, !=, NULL); 1487 key_mapping_add_ref(ds->ds_key_mapping, ds); 1488 } 1489 } 1490 } 1491 1492 static int 1493 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1494 { 1495 uint64_t asize; 1496 1497 if (!dmu_tx_is_syncing(tx)) 1498 return (0); 1499 1500 /* 1501 * If there's an fs-only reservation, any blocks that might become 1502 * owned by the snapshot dataset must be accommodated by space 1503 * outside of the reservation. 1504 */ 1505 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1506 asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 1507 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1508 return (SET_ERROR(ENOSPC)); 1509 1510 /* 1511 * Propagate any reserved space for this snapshot to other 1512 * snapshot checks in this sync group. 1513 */ 1514 if (asize > 0) 1515 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 1516 1517 return (0); 1518 } 1519 1520 int 1521 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1522 dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 1523 { 1524 int error; 1525 uint64_t value; 1526 1527 ds->ds_trysnap_txg = tx->tx_txg; 1528 1529 if (!dmu_tx_is_syncing(tx)) 1530 return (0); 1531 1532 /* 1533 * We don't allow multiple snapshots of the same txg. If there 1534 * is already one, try again. 1535 */ 1536 if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1537 return (SET_ERROR(EAGAIN)); 1538 1539 /* 1540 * Check for conflicting snapshot name. 1541 */ 1542 error = dsl_dataset_snap_lookup(ds, snapname, &value); 1543 if (error == 0) 1544 return (SET_ERROR(EEXIST)); 1545 if (error != ENOENT) 1546 return (error); 1547 1548 /* 1549 * We don't allow taking snapshots of inconsistent datasets, such as 1550 * those into which we are currently receiving. However, if we are 1551 * creating this snapshot as part of a receive, this check will be 1552 * executed atomically with respect to the completion of the receive 1553 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1554 * case we ignore this, knowing it will be fixed up for us shortly in 1555 * dmu_recv_end_sync(). 1556 */ 1557 if (!recv && DS_IS_INCONSISTENT(ds)) 1558 return (SET_ERROR(EBUSY)); 1559 1560 /* 1561 * Skip the check for temporary snapshots or if we have already checked 1562 * the counts in dsl_dataset_snapshot_check. This means we really only 1563 * check the count here when we're receiving a stream. 1564 */ 1565 if (cnt != 0 && cr != NULL) { 1566 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1567 ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1568 if (error != 0) 1569 return (error); 1570 } 1571 1572 error = dsl_dataset_snapshot_reserve_space(ds, tx); 1573 if (error != 0) 1574 return (error); 1575 1576 return (0); 1577 } 1578 1579 int 1580 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1581 { 1582 dsl_dataset_snapshot_arg_t *ddsa = arg; 1583 dsl_pool_t *dp = dmu_tx_pool(tx); 1584 nvpair_t *pair; 1585 int rv = 0; 1586 1587 /* 1588 * Pre-compute how many total new snapshots will be created for each 1589 * level in the tree and below. This is needed for validating the 1590 * snapshot limit when either taking a recursive snapshot or when 1591 * taking multiple snapshots. 1592 * 1593 * The problem is that the counts are not actually adjusted when 1594 * we are checking, only when we finally sync. For a single snapshot, 1595 * this is easy, the count will increase by 1 at each node up the tree, 1596 * but its more complicated for the recursive/multiple snapshot case. 1597 * 1598 * The dsl_fs_ss_limit_check function does recursively check the count 1599 * at each level up the tree but since it is validating each snapshot 1600 * independently we need to be sure that we are validating the complete 1601 * count for the entire set of snapshots. We do this by rolling up the 1602 * counts for each component of the name into an nvlist and then 1603 * checking each of those cases with the aggregated count. 1604 * 1605 * This approach properly handles not only the recursive snapshot 1606 * case (where we get all of those on the ddsa_snaps list) but also 1607 * the sibling case (e.g. snapshot a/b and a/c so that we will also 1608 * validate the limit on 'a' using a count of 2). 1609 * 1610 * We validate the snapshot names in the third loop and only report 1611 * name errors once. 1612 */ 1613 if (dmu_tx_is_syncing(tx)) { 1614 char *nm; 1615 nvlist_t *cnt_track = NULL; 1616 cnt_track = fnvlist_alloc(); 1617 1618 nm = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1619 1620 /* Rollup aggregated counts into the cnt_track list */ 1621 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1622 pair != NULL; 1623 pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1624 char *pdelim; 1625 uint64_t val; 1626 1627 (void) strlcpy(nm, nvpair_name(pair), MAXPATHLEN); 1628 pdelim = strchr(nm, '@'); 1629 if (pdelim == NULL) 1630 continue; 1631 *pdelim = '\0'; 1632 1633 do { 1634 if (nvlist_lookup_uint64(cnt_track, nm, 1635 &val) == 0) { 1636 /* update existing entry */ 1637 fnvlist_add_uint64(cnt_track, nm, 1638 val + 1); 1639 } else { 1640 /* add to list */ 1641 fnvlist_add_uint64(cnt_track, nm, 1); 1642 } 1643 1644 pdelim = strrchr(nm, '/'); 1645 if (pdelim != NULL) 1646 *pdelim = '\0'; 1647 } while (pdelim != NULL); 1648 } 1649 1650 kmem_free(nm, MAXPATHLEN); 1651 1652 /* Check aggregated counts at each level */ 1653 for (pair = nvlist_next_nvpair(cnt_track, NULL); 1654 pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1655 int error = 0; 1656 const char *name; 1657 uint64_t cnt = 0; 1658 dsl_dataset_t *ds; 1659 1660 name = nvpair_name(pair); 1661 cnt = fnvpair_value_uint64(pair); 1662 ASSERT(cnt > 0); 1663 1664 error = dsl_dataset_hold(dp, name, FTAG, &ds); 1665 if (error == 0) { 1666 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1667 ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1668 ddsa->ddsa_cr); 1669 dsl_dataset_rele(ds, FTAG); 1670 } 1671 1672 if (error != 0) { 1673 if (ddsa->ddsa_errors != NULL) 1674 fnvlist_add_int32(ddsa->ddsa_errors, 1675 name, error); 1676 rv = error; 1677 /* only report one error for this check */ 1678 break; 1679 } 1680 } 1681 nvlist_free(cnt_track); 1682 } 1683 1684 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1685 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1686 int error = 0; 1687 dsl_dataset_t *ds; 1688 const char *name, *atp = NULL; 1689 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 1690 1691 name = nvpair_name(pair); 1692 if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN) 1693 error = SET_ERROR(ENAMETOOLONG); 1694 if (error == 0) { 1695 atp = strchr(name, '@'); 1696 if (atp == NULL) 1697 error = SET_ERROR(EINVAL); 1698 if (error == 0) 1699 (void) strlcpy(dsname, name, atp - name + 1); 1700 } 1701 if (error == 0) 1702 error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 1703 if (error == 0) { 1704 /* passing 0/NULL skips dsl_fs_ss_limit_check */ 1705 error = dsl_dataset_snapshot_check_impl(ds, 1706 atp + 1, tx, B_FALSE, 0, NULL); 1707 dsl_dataset_rele(ds, FTAG); 1708 } 1709 1710 if (error != 0) { 1711 if (ddsa->ddsa_errors != NULL) { 1712 fnvlist_add_int32(ddsa->ddsa_errors, 1713 name, error); 1714 } 1715 rv = error; 1716 } 1717 } 1718 1719 return (rv); 1720 } 1721 1722 void 1723 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 1724 dmu_tx_t *tx) 1725 { 1726 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1727 dmu_buf_t *dbuf; 1728 dsl_dataset_phys_t *dsphys; 1729 uint64_t dsobj, crtxg; 1730 objset_t *mos = dp->dp_meta_objset; 1731 objset_t *os __maybe_unused; 1732 1733 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1734 1735 /* 1736 * If we are on an old pool, the zil must not be active, in which 1737 * case it will be zeroed. Usually zil_suspend() accomplishes this. 1738 */ 1739 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 1740 dmu_objset_from_ds(ds, &os) != 0 || 1741 memcmp(&os->os_phys->os_zil_header, &zero_zil, 1742 sizeof (zero_zil)) == 0); 1743 1744 /* Should not snapshot a dirty dataset. */ 1745 ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 1746 ds, tx->tx_txg)); 1747 1748 dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1749 1750 /* 1751 * The origin's ds_creation_txg has to be < TXG_INITIAL 1752 */ 1753 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1754 crtxg = 1; 1755 else 1756 crtxg = tx->tx_txg; 1757 1758 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1759 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 1760 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1761 dmu_buf_will_dirty(dbuf, tx); 1762 dsphys = dbuf->db_data; 1763 memset(dsphys, 0, sizeof (dsl_dataset_phys_t)); 1764 dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1765 dsphys->ds_fsid_guid = unique_create(); 1766 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1767 sizeof (dsphys->ds_guid)); 1768 dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1769 dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1770 dsphys->ds_next_snap_obj = ds->ds_object; 1771 dsphys->ds_num_children = 1; 1772 dsphys->ds_creation_time = gethrestime_sec(); 1773 dsphys->ds_creation_txg = crtxg; 1774 dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1775 dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1776 dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1777 dsphys->ds_uncompressed_bytes = 1778 dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1779 dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1780 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1781 dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1782 rrw_exit(&ds->ds_bp_rwlock, FTAG); 1783 dmu_buf_rele(dbuf, FTAG); 1784 1785 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1786 if (zfeature_active(f, ds->ds_feature[f])) { 1787 dsl_dataset_activate_feature(dsobj, f, 1788 ds->ds_feature[f], tx); 1789 } 1790 } 1791 1792 ASSERT3U(ds->ds_prev != 0, ==, 1793 dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 1794 if (ds->ds_prev) { 1795 uint64_t next_clones_obj = 1796 dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1797 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1798 ds->ds_object || 1799 dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1800 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1801 ds->ds_object) { 1802 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1803 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1804 dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1805 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1806 } else if (next_clones_obj != 0) { 1807 dsl_dataset_remove_from_next_clones(ds->ds_prev, 1808 dsphys->ds_next_snap_obj, tx); 1809 VERIFY0(zap_add_int(mos, 1810 next_clones_obj, dsobj, tx)); 1811 } 1812 } 1813 1814 /* 1815 * If we have a reference-reservation on this dataset, we will 1816 * need to increase the amount of refreservation being charged 1817 * since our unique space is going to zero. 1818 */ 1819 if (ds->ds_reserved) { 1820 int64_t delta; 1821 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1822 delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1823 ds->ds_reserved); 1824 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 1825 delta, 0, 0, tx); 1826 } 1827 1828 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1829 dsl_dataset_phys(ds)->ds_deadlist_obj = 1830 dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1831 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1832 dsl_deadlist_close(&ds->ds_deadlist); 1833 VERIFY0(dsl_deadlist_open(&ds->ds_deadlist, mos, 1834 dsl_dataset_phys(ds)->ds_deadlist_obj)); 1835 dsl_deadlist_add_key(&ds->ds_deadlist, 1836 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1837 dsl_bookmark_snapshotted(ds, tx); 1838 1839 if (dsl_dataset_remap_deadlist_exists(ds)) { 1840 uint64_t remap_deadlist_obj = 1841 dsl_dataset_get_remap_deadlist_object(ds); 1842 /* 1843 * Move the remap_deadlist to the snapshot. The head 1844 * will create a new remap deadlist on demand, from 1845 * dsl_dataset_block_remapped(). 1846 */ 1847 dsl_dataset_unset_remap_deadlist_object(ds, tx); 1848 dsl_deadlist_close(&ds->ds_remap_deadlist); 1849 1850 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 1851 VERIFY0(zap_add(mos, dsobj, DS_FIELD_REMAP_DEADLIST, 1852 sizeof (remap_deadlist_obj), 1, &remap_deadlist_obj, tx)); 1853 } 1854 1855 /* 1856 * Create a ivset guid for this snapshot if the dataset is 1857 * encrypted. This may be overridden by a raw receive. A 1858 * previous implementation of this code did not have this 1859 * field as part of the on-disk format for ZFS encryption 1860 * (see errata #4). As part of the remediation for this 1861 * issue, we ask the user to enable the bookmark_v2 feature 1862 * which is now a dependency of the encryption feature. We 1863 * use this as a heuristic to determine when the user has 1864 * elected to correct any datasets created with the old code. 1865 * As a result, we only do this step if the bookmark_v2 1866 * feature is enabled, which limits the number of states a 1867 * given pool / dataset can be in with regards to terms of 1868 * correcting the issue. 1869 */ 1870 if (ds->ds_dir->dd_crypto_obj != 0 && 1871 spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2)) { 1872 uint64_t ivset_guid = unique_create(); 1873 1874 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 1875 VERIFY0(zap_add(mos, dsobj, DS_FIELD_IVSET_GUID, 1876 sizeof (ivset_guid), 1, &ivset_guid, tx)); 1877 } 1878 1879 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1880 dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1881 dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1882 dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1883 1884 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1885 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1886 1887 VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 1888 snapname, 8, 1, &dsobj, tx)); 1889 1890 if (ds->ds_prev) 1891 dsl_dataset_rele(ds->ds_prev, ds); 1892 VERIFY0(dsl_dataset_hold_obj(dp, 1893 dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1894 1895 dsl_scan_ds_snapshotted(ds, tx); 1896 1897 dsl_dir_snap_cmtime_update(ds->ds_dir, tx); 1898 1899 if (zfs_snapshot_history_enabled) 1900 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, " "); 1901 } 1902 1903 void 1904 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1905 { 1906 dsl_dataset_snapshot_arg_t *ddsa = arg; 1907 dsl_pool_t *dp = dmu_tx_pool(tx); 1908 nvpair_t *pair; 1909 1910 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1911 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1912 dsl_dataset_t *ds; 1913 const char *name, *atp; 1914 char dsname[ZFS_MAX_DATASET_NAME_LEN]; 1915 1916 name = nvpair_name(pair); 1917 atp = strchr(name, '@'); 1918 (void) strlcpy(dsname, name, atp - name + 1); 1919 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 1920 1921 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 1922 if (ddsa->ddsa_props != NULL) { 1923 dsl_props_set_sync_impl(ds->ds_prev, 1924 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 1925 } 1926 dsl_dataset_rele(ds, FTAG); 1927 } 1928 } 1929 1930 /* 1931 * The snapshots must all be in the same pool. 1932 * All-or-nothing: if there are any failures, nothing will be modified. 1933 */ 1934 int 1935 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 1936 { 1937 dsl_dataset_snapshot_arg_t ddsa; 1938 nvpair_t *pair; 1939 boolean_t needsuspend; 1940 int error; 1941 spa_t *spa; 1942 const char *firstname; 1943 nvlist_t *suspended = NULL; 1944 1945 pair = nvlist_next_nvpair(snaps, NULL); 1946 if (pair == NULL) 1947 return (0); 1948 firstname = nvpair_name(pair); 1949 1950 error = spa_open(firstname, &spa, FTAG); 1951 if (error != 0) 1952 return (error); 1953 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 1954 spa_close(spa, FTAG); 1955 1956 if (needsuspend) { 1957 suspended = fnvlist_alloc(); 1958 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 1959 pair = nvlist_next_nvpair(snaps, pair)) { 1960 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 1961 const char *snapname = nvpair_name(pair); 1962 const char *atp; 1963 void *cookie; 1964 1965 atp = strchr(snapname, '@'); 1966 if (atp == NULL) { 1967 error = SET_ERROR(EINVAL); 1968 break; 1969 } 1970 (void) strlcpy(fsname, snapname, atp - snapname + 1); 1971 1972 error = zil_suspend(fsname, &cookie); 1973 if (error != 0) 1974 break; 1975 fnvlist_add_uint64(suspended, fsname, 1976 (uintptr_t)cookie); 1977 } 1978 } 1979 1980 cred_t *cr = CRED(); 1981 crhold(cr); 1982 1983 ddsa.ddsa_snaps = snaps; 1984 ddsa.ddsa_props = props; 1985 ddsa.ddsa_errors = errors; 1986 ddsa.ddsa_cr = cr; 1987 1988 if (error == 0) { 1989 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 1990 dsl_dataset_snapshot_sync, &ddsa, 1991 fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 1992 } 1993 1994 crfree(cr); 1995 1996 if (suspended != NULL) { 1997 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 1998 pair = nvlist_next_nvpair(suspended, pair)) { 1999 zil_resume((void *)(uintptr_t) 2000 fnvpair_value_uint64(pair)); 2001 } 2002 fnvlist_free(suspended); 2003 } 2004 2005 if (error == 0) { 2006 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 2007 pair = nvlist_next_nvpair(snaps, pair)) { 2008 zvol_create_minor(nvpair_name(pair)); 2009 } 2010 } 2011 2012 return (error); 2013 } 2014 2015 typedef struct dsl_dataset_snapshot_tmp_arg { 2016 const char *ddsta_fsname; 2017 const char *ddsta_snapname; 2018 minor_t ddsta_cleanup_minor; 2019 const char *ddsta_htag; 2020 } dsl_dataset_snapshot_tmp_arg_t; 2021 2022 static int 2023 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 2024 { 2025 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 2026 dsl_pool_t *dp = dmu_tx_pool(tx); 2027 dsl_dataset_t *ds; 2028 int error; 2029 2030 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 2031 if (error != 0) 2032 return (error); 2033 2034 /* NULL cred means no limit check for tmp snapshot */ 2035 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 2036 tx, B_FALSE, 0, NULL); 2037 if (error != 0) { 2038 dsl_dataset_rele(ds, FTAG); 2039 return (error); 2040 } 2041 2042 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 2043 dsl_dataset_rele(ds, FTAG); 2044 return (SET_ERROR(ENOTSUP)); 2045 } 2046 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 2047 B_TRUE, tx); 2048 if (error != 0) { 2049 dsl_dataset_rele(ds, FTAG); 2050 return (error); 2051 } 2052 2053 dsl_dataset_rele(ds, FTAG); 2054 return (0); 2055 } 2056 2057 static void 2058 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 2059 { 2060 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 2061 dsl_pool_t *dp = dmu_tx_pool(tx); 2062 dsl_dataset_t *ds = NULL; 2063 2064 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 2065 2066 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 2067 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 2068 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 2069 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 2070 2071 dsl_dataset_rele(ds, FTAG); 2072 } 2073 2074 int 2075 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 2076 minor_t cleanup_minor, const char *htag) 2077 { 2078 dsl_dataset_snapshot_tmp_arg_t ddsta; 2079 int error; 2080 spa_t *spa; 2081 boolean_t needsuspend; 2082 void *cookie; 2083 2084 ddsta.ddsta_fsname = fsname; 2085 ddsta.ddsta_snapname = snapname; 2086 ddsta.ddsta_cleanup_minor = cleanup_minor; 2087 ddsta.ddsta_htag = htag; 2088 2089 error = spa_open(fsname, &spa, FTAG); 2090 if (error != 0) 2091 return (error); 2092 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 2093 spa_close(spa, FTAG); 2094 2095 if (needsuspend) { 2096 error = zil_suspend(fsname, &cookie); 2097 if (error != 0) 2098 return (error); 2099 } 2100 2101 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 2102 dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 2103 2104 if (needsuspend) 2105 zil_resume(cookie); 2106 return (error); 2107 } 2108 2109 /* Nonblocking dataset sync. Assumes dataset:objset is always 1:1 */ 2110 void 2111 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *rio, dmu_tx_t *tx) 2112 { 2113 ASSERT(dmu_tx_is_syncing(tx)); 2114 ASSERT(ds->ds_objset != NULL); 2115 ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 2116 2117 /* 2118 * in case we had to change ds_fsid_guid when we opened it, 2119 * sync it out now. 2120 */ 2121 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2122 dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 2123 2124 if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) { 2125 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 2126 ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1, 2127 &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx)); 2128 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 2129 ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1, 2130 &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx)); 2131 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 2132 ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1, 2133 &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx)); 2134 ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0; 2135 ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0; 2136 ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0; 2137 } 2138 2139 dmu_objset_sync(ds->ds_objset, rio, tx); 2140 } 2141 2142 /* 2143 * Check if the percentage of blocks shared between the clone and the 2144 * snapshot (as opposed to those that are clone only) is below a certain 2145 * threshold 2146 */ 2147 static boolean_t 2148 dsl_livelist_should_disable(dsl_dataset_t *ds) 2149 { 2150 uint64_t used, referenced; 2151 int percent_shared; 2152 2153 used = dsl_dir_get_usedds(ds->ds_dir); 2154 referenced = dsl_get_referenced(ds); 2155 if (referenced == 0) 2156 return (B_FALSE); 2157 percent_shared = (100 * (referenced - used)) / referenced; 2158 if (percent_shared <= zfs_livelist_min_percent_shared) 2159 return (B_TRUE); 2160 return (B_FALSE); 2161 } 2162 2163 /* 2164 * Check if it is possible to combine two livelist entries into one. 2165 * This is the case if the combined number of 'live' blkptrs (ALLOCs that 2166 * don't have a matching FREE) is under the maximum sublist size. 2167 * We check this by subtracting twice the total number of frees from the total 2168 * number of blkptrs. FREEs are counted twice because each FREE blkptr 2169 * will cancel out an ALLOC blkptr when the livelist is processed. 2170 */ 2171 static boolean_t 2172 dsl_livelist_should_condense(dsl_deadlist_entry_t *first, 2173 dsl_deadlist_entry_t *next) 2174 { 2175 uint64_t total_free = first->dle_bpobj.bpo_phys->bpo_num_freed + 2176 next->dle_bpobj.bpo_phys->bpo_num_freed; 2177 uint64_t total_entries = first->dle_bpobj.bpo_phys->bpo_num_blkptrs + 2178 next->dle_bpobj.bpo_phys->bpo_num_blkptrs; 2179 if ((total_entries - (2 * total_free)) < zfs_livelist_max_entries) 2180 return (B_TRUE); 2181 return (B_FALSE); 2182 } 2183 2184 typedef struct try_condense_arg { 2185 spa_t *spa; 2186 dsl_dataset_t *ds; 2187 } try_condense_arg_t; 2188 2189 /* 2190 * Iterate over the livelist entries, searching for a pair to condense. 2191 * A nonzero return value means stop, 0 means keep looking. 2192 */ 2193 static int 2194 dsl_livelist_try_condense(void *arg, dsl_deadlist_entry_t *first) 2195 { 2196 try_condense_arg_t *tca = arg; 2197 spa_t *spa = tca->spa; 2198 dsl_dataset_t *ds = tca->ds; 2199 dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; 2200 dsl_deadlist_entry_t *next; 2201 2202 /* The condense thread has not yet been created at import */ 2203 if (spa->spa_livelist_condense_zthr == NULL) 2204 return (1); 2205 2206 /* A condense is already in progress */ 2207 if (spa->spa_to_condense.ds != NULL) 2208 return (1); 2209 2210 next = AVL_NEXT(&ll->dl_tree, &first->dle_node); 2211 /* The livelist has only one entry - don't condense it */ 2212 if (next == NULL) 2213 return (1); 2214 2215 /* Next is the newest entry - don't condense it */ 2216 if (AVL_NEXT(&ll->dl_tree, &next->dle_node) == NULL) 2217 return (1); 2218 2219 /* This pair is not ready to condense but keep looking */ 2220 if (!dsl_livelist_should_condense(first, next)) 2221 return (0); 2222 2223 /* 2224 * Add a ref to prevent the dataset from being evicted while 2225 * the condense zthr or synctask are running. Ref will be 2226 * released at the end of the condense synctask 2227 */ 2228 dmu_buf_add_ref(ds->ds_dbuf, spa); 2229 2230 spa->spa_to_condense.ds = ds; 2231 spa->spa_to_condense.first = first; 2232 spa->spa_to_condense.next = next; 2233 spa->spa_to_condense.syncing = B_FALSE; 2234 spa->spa_to_condense.cancelled = B_FALSE; 2235 2236 zthr_wakeup(spa->spa_livelist_condense_zthr); 2237 return (1); 2238 } 2239 2240 static void 2241 dsl_flush_pending_livelist(dsl_dataset_t *ds, dmu_tx_t *tx) 2242 { 2243 dsl_dir_t *dd = ds->ds_dir; 2244 spa_t *spa = ds->ds_dir->dd_pool->dp_spa; 2245 dsl_deadlist_entry_t *last = dsl_deadlist_last(&dd->dd_livelist); 2246 2247 /* Check if we need to add a new sub-livelist */ 2248 if (last == NULL) { 2249 /* The livelist is empty */ 2250 dsl_deadlist_add_key(&dd->dd_livelist, 2251 tx->tx_txg - 1, tx); 2252 } else if (spa_sync_pass(spa) == 1) { 2253 /* 2254 * Check if the newest entry is full. If it is, make a new one. 2255 * We only do this once per sync because we could overfill a 2256 * sublist in one sync pass and don't want to add another entry 2257 * for a txg that is already represented. This ensures that 2258 * blkptrs born in the same txg are stored in the same sublist. 2259 */ 2260 bpobj_t bpobj = last->dle_bpobj; 2261 uint64_t all = bpobj.bpo_phys->bpo_num_blkptrs; 2262 uint64_t free = bpobj.bpo_phys->bpo_num_freed; 2263 uint64_t alloc = all - free; 2264 if (alloc > zfs_livelist_max_entries) { 2265 dsl_deadlist_add_key(&dd->dd_livelist, 2266 tx->tx_txg - 1, tx); 2267 } 2268 } 2269 2270 /* Insert each entry into the on-disk livelist */ 2271 bplist_iterate(&dd->dd_pending_allocs, 2272 dsl_deadlist_insert_alloc_cb, &dd->dd_livelist, tx); 2273 bplist_iterate(&dd->dd_pending_frees, 2274 dsl_deadlist_insert_free_cb, &dd->dd_livelist, tx); 2275 2276 /* Attempt to condense every pair of adjacent entries */ 2277 try_condense_arg_t arg = { 2278 .spa = spa, 2279 .ds = ds 2280 }; 2281 dsl_deadlist_iterate(&dd->dd_livelist, dsl_livelist_try_condense, 2282 &arg); 2283 } 2284 2285 void 2286 dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx) 2287 { 2288 objset_t *os = ds->ds_objset; 2289 2290 bplist_iterate(&ds->ds_pending_deadlist, 2291 dsl_deadlist_insert_alloc_cb, &ds->ds_deadlist, tx); 2292 2293 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) { 2294 dsl_flush_pending_livelist(ds, tx); 2295 if (dsl_livelist_should_disable(ds)) { 2296 dsl_dir_remove_livelist(ds->ds_dir, tx, B_TRUE); 2297 } 2298 } 2299 2300 dsl_bookmark_sync_done(ds, tx); 2301 2302 multilist_destroy(&os->os_synced_dnodes); 2303 2304 if (os->os_encrypted) 2305 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE; 2306 else 2307 ASSERT0(os->os_next_write_raw[tx->tx_txg & TXG_MASK]); 2308 2309 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2310 if (zfeature_active(f, 2311 ds->ds_feature_activation[f])) { 2312 if (zfeature_active(f, ds->ds_feature[f])) 2313 continue; 2314 dsl_dataset_activate_feature(ds->ds_object, f, 2315 ds->ds_feature_activation[f], tx); 2316 ds->ds_feature[f] = ds->ds_feature_activation[f]; 2317 } 2318 } 2319 2320 ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx))); 2321 } 2322 2323 int 2324 get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val) 2325 { 2326 uint64_t count = 0; 2327 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 2328 zap_cursor_t zc; 2329 zap_attribute_t *za; 2330 2331 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 2332 2333 /* 2334 * There may be missing entries in ds_next_clones_obj 2335 * due to a bug in a previous version of the code. 2336 * Only trust it if it has the right number of entries. 2337 */ 2338 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 2339 VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 2340 &count)); 2341 } 2342 if (count != dsl_dataset_phys(ds)->ds_num_children - 1) { 2343 return (SET_ERROR(ENOENT)); 2344 } 2345 2346 za = zap_attribute_alloc(); 2347 for (zap_cursor_init(&zc, mos, 2348 dsl_dataset_phys(ds)->ds_next_clones_obj); 2349 zap_cursor_retrieve(&zc, za) == 0; 2350 zap_cursor_advance(&zc)) { 2351 dsl_dataset_t *clone; 2352 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2353 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 2354 za->za_first_integer, FTAG, &clone)); 2355 dsl_dir_name(clone->ds_dir, buf); 2356 fnvlist_add_boolean(val, buf); 2357 dsl_dataset_rele(clone, FTAG); 2358 } 2359 zap_cursor_fini(&zc); 2360 zap_attribute_free(za); 2361 return (0); 2362 } 2363 2364 void 2365 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 2366 { 2367 nvlist_t *propval = fnvlist_alloc(); 2368 nvlist_t *val = fnvlist_alloc(); 2369 2370 if (get_clones_stat_impl(ds, val) == 0) { 2371 fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 2372 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), 2373 propval); 2374 } 2375 2376 nvlist_free(val); 2377 nvlist_free(propval); 2378 } 2379 2380 static char * 2381 get_receive_resume_token_impl(dsl_dataset_t *ds) 2382 { 2383 if (!dsl_dataset_has_resume_receive_state(ds)) 2384 return (NULL); 2385 2386 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2387 char *str; 2388 void *packed; 2389 uint8_t *compressed; 2390 uint64_t val; 2391 nvlist_t *token_nv = fnvlist_alloc(); 2392 size_t packed_size, compressed_size; 2393 2394 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2395 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) { 2396 fnvlist_add_uint64(token_nv, "fromguid", val); 2397 } 2398 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2399 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) { 2400 fnvlist_add_uint64(token_nv, "object", val); 2401 } 2402 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2403 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) { 2404 fnvlist_add_uint64(token_nv, "offset", val); 2405 } 2406 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2407 DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) { 2408 fnvlist_add_uint64(token_nv, "bytes", val); 2409 } 2410 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2411 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) { 2412 fnvlist_add_uint64(token_nv, "toguid", val); 2413 } 2414 char buf[MAXNAMELEN]; 2415 if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 2416 DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) { 2417 fnvlist_add_string(token_nv, "toname", buf); 2418 } 2419 if (zap_contains(dp->dp_meta_objset, ds->ds_object, 2420 DS_FIELD_RESUME_LARGEBLOCK) == 0) { 2421 fnvlist_add_boolean(token_nv, "largeblockok"); 2422 } 2423 if (zap_contains(dp->dp_meta_objset, ds->ds_object, 2424 DS_FIELD_RESUME_EMBEDOK) == 0) { 2425 fnvlist_add_boolean(token_nv, "embedok"); 2426 } 2427 if (zap_contains(dp->dp_meta_objset, ds->ds_object, 2428 DS_FIELD_RESUME_COMPRESSOK) == 0) { 2429 fnvlist_add_boolean(token_nv, "compressok"); 2430 } 2431 if (zap_contains(dp->dp_meta_objset, ds->ds_object, 2432 DS_FIELD_RESUME_RAWOK) == 0) { 2433 fnvlist_add_boolean(token_nv, "rawok"); 2434 } 2435 if (dsl_dataset_feature_is_active(ds, 2436 SPA_FEATURE_REDACTED_DATASETS)) { 2437 uint64_t num_redact_snaps = 0; 2438 uint64_t *redact_snaps = NULL; 2439 VERIFY3B(dsl_dataset_get_uint64_array_feature(ds, 2440 SPA_FEATURE_REDACTED_DATASETS, &num_redact_snaps, 2441 &redact_snaps), ==, B_TRUE); 2442 fnvlist_add_uint64_array(token_nv, "redact_snaps", 2443 redact_snaps, num_redact_snaps); 2444 } 2445 if (zap_contains(dp->dp_meta_objset, ds->ds_object, 2446 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS) == 0) { 2447 uint64_t num_redact_snaps = 0, int_size = 0; 2448 uint64_t *redact_snaps = NULL; 2449 VERIFY0(zap_length(dp->dp_meta_objset, ds->ds_object, 2450 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, &int_size, 2451 &num_redact_snaps)); 2452 ASSERT3U(int_size, ==, sizeof (uint64_t)); 2453 2454 redact_snaps = kmem_alloc(int_size * num_redact_snaps, 2455 KM_SLEEP); 2456 VERIFY0(zap_lookup(dp->dp_meta_objset, ds->ds_object, 2457 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, int_size, 2458 num_redact_snaps, redact_snaps)); 2459 fnvlist_add_uint64_array(token_nv, "book_redact_snaps", 2460 redact_snaps, num_redact_snaps); 2461 kmem_free(redact_snaps, int_size * num_redact_snaps); 2462 } 2463 packed = fnvlist_pack(token_nv, &packed_size); 2464 fnvlist_free(token_nv); 2465 compressed = kmem_alloc(packed_size, KM_SLEEP); 2466 2467 /* Call compress function directly to avoid hole detection. */ 2468 abd_t pabd, cabd; 2469 abd_get_from_buf_struct(&pabd, packed, packed_size); 2470 abd_get_from_buf_struct(&cabd, compressed, packed_size); 2471 compressed_size = zfs_gzip_compress(&pabd, &cabd, 2472 packed_size, packed_size, 6); 2473 abd_free(&cabd); 2474 abd_free(&pabd); 2475 2476 zio_cksum_t cksum; 2477 fletcher_4_native_varsize(compressed, compressed_size, &cksum); 2478 2479 size_t alloc_size = compressed_size * 2 + 1; 2480 str = kmem_alloc(alloc_size, KM_SLEEP); 2481 for (int i = 0; i < compressed_size; i++) { 2482 size_t offset = i * 2; 2483 (void) snprintf(str + offset, alloc_size - offset, 2484 "%02x", compressed[i]); 2485 } 2486 str[compressed_size * 2] = '\0'; 2487 char *propval = kmem_asprintf("%u-%llx-%llx-%s", 2488 ZFS_SEND_RESUME_TOKEN_VERSION, 2489 (longlong_t)cksum.zc_word[0], 2490 (longlong_t)packed_size, str); 2491 kmem_free(packed, packed_size); 2492 kmem_free(str, alloc_size); 2493 kmem_free(compressed, packed_size); 2494 return (propval); 2495 } 2496 2497 /* 2498 * Returns a string that represents the receive resume state token. It should 2499 * be freed with strfree(). NULL is returned if no resume state is present. 2500 */ 2501 char * 2502 get_receive_resume_token(dsl_dataset_t *ds) 2503 { 2504 /* 2505 * A failed "newfs" (e.g. full) resumable receive leaves 2506 * the stats set on this dataset. Check here for the prop. 2507 */ 2508 char *token = get_receive_resume_token_impl(ds); 2509 if (token != NULL) 2510 return (token); 2511 /* 2512 * A failed incremental resumable receive leaves the 2513 * stats set on our child named "%recv". Check the child 2514 * for the prop. 2515 */ 2516 /* 6 extra bytes for /%recv */ 2517 char name[ZFS_MAX_DATASET_NAME_LEN + 6]; 2518 dsl_dataset_t *recv_ds; 2519 dsl_dataset_name(ds, name); 2520 if (strlcat(name, "/", sizeof (name)) < sizeof (name) && 2521 strlcat(name, recv_clone_name, sizeof (name)) < sizeof (name) && 2522 dsl_dataset_hold(ds->ds_dir->dd_pool, name, FTAG, &recv_ds) == 0) { 2523 token = get_receive_resume_token_impl(recv_ds); 2524 dsl_dataset_rele(recv_ds, FTAG); 2525 } 2526 return (token); 2527 } 2528 2529 uint64_t 2530 dsl_get_refratio(dsl_dataset_t *ds) 2531 { 2532 uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 2533 (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 2534 dsl_dataset_phys(ds)->ds_compressed_bytes); 2535 return (ratio); 2536 } 2537 2538 uint64_t 2539 dsl_get_logicalreferenced(dsl_dataset_t *ds) 2540 { 2541 return (dsl_dataset_phys(ds)->ds_uncompressed_bytes); 2542 } 2543 2544 uint64_t 2545 dsl_get_compressratio(dsl_dataset_t *ds) 2546 { 2547 if (ds->ds_is_snapshot) { 2548 return (dsl_get_refratio(ds)); 2549 } else { 2550 dsl_dir_t *dd = ds->ds_dir; 2551 mutex_enter(&dd->dd_lock); 2552 uint64_t val = dsl_dir_get_compressratio(dd); 2553 mutex_exit(&dd->dd_lock); 2554 return (val); 2555 } 2556 } 2557 2558 uint64_t 2559 dsl_get_used(dsl_dataset_t *ds) 2560 { 2561 if (ds->ds_is_snapshot) { 2562 return (dsl_dataset_phys(ds)->ds_unique_bytes); 2563 } else { 2564 dsl_dir_t *dd = ds->ds_dir; 2565 mutex_enter(&dd->dd_lock); 2566 uint64_t val = dsl_dir_get_used(dd); 2567 mutex_exit(&dd->dd_lock); 2568 return (val); 2569 } 2570 } 2571 2572 uint64_t 2573 dsl_get_creation(dsl_dataset_t *ds) 2574 { 2575 return (dsl_dataset_phys(ds)->ds_creation_time); 2576 } 2577 2578 uint64_t 2579 dsl_get_creationtxg(dsl_dataset_t *ds) 2580 { 2581 return (dsl_dataset_phys(ds)->ds_creation_txg); 2582 } 2583 2584 uint64_t 2585 dsl_get_refquota(dsl_dataset_t *ds) 2586 { 2587 return (ds->ds_quota); 2588 } 2589 2590 uint64_t 2591 dsl_get_refreservation(dsl_dataset_t *ds) 2592 { 2593 return (ds->ds_reserved); 2594 } 2595 2596 uint64_t 2597 dsl_get_guid(dsl_dataset_t *ds) 2598 { 2599 return (dsl_dataset_phys(ds)->ds_guid); 2600 } 2601 2602 uint64_t 2603 dsl_get_unique(dsl_dataset_t *ds) 2604 { 2605 return (dsl_dataset_phys(ds)->ds_unique_bytes); 2606 } 2607 2608 uint64_t 2609 dsl_get_objsetid(dsl_dataset_t *ds) 2610 { 2611 return (ds->ds_object); 2612 } 2613 2614 uint64_t 2615 dsl_get_userrefs(dsl_dataset_t *ds) 2616 { 2617 return (ds->ds_userrefs); 2618 } 2619 2620 uint64_t 2621 dsl_get_defer_destroy(dsl_dataset_t *ds) 2622 { 2623 return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2624 } 2625 2626 uint64_t 2627 dsl_get_referenced(dsl_dataset_t *ds) 2628 { 2629 return (dsl_dataset_phys(ds)->ds_referenced_bytes); 2630 } 2631 2632 uint64_t 2633 dsl_get_numclones(dsl_dataset_t *ds) 2634 { 2635 ASSERT(ds->ds_is_snapshot); 2636 return (dsl_dataset_phys(ds)->ds_num_children - 1); 2637 } 2638 2639 uint64_t 2640 dsl_get_inconsistent(dsl_dataset_t *ds) 2641 { 2642 return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ? 2643 1 : 0); 2644 } 2645 2646 uint64_t 2647 dsl_get_redacted(dsl_dataset_t *ds) 2648 { 2649 return (dsl_dataset_feature_is_active(ds, 2650 SPA_FEATURE_REDACTED_DATASETS)); 2651 } 2652 2653 uint64_t 2654 dsl_get_available(dsl_dataset_t *ds) 2655 { 2656 uint64_t refdbytes = dsl_get_referenced(ds); 2657 uint64_t availbytes = dsl_dir_space_available(ds->ds_dir, 2658 NULL, 0, TRUE); 2659 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 2660 availbytes += 2661 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 2662 } 2663 if (ds->ds_quota != 0) { 2664 /* 2665 * Adjust available bytes according to refquota 2666 */ 2667 if (refdbytes < ds->ds_quota) { 2668 availbytes = MIN(availbytes, 2669 ds->ds_quota - refdbytes); 2670 } else { 2671 availbytes = 0; 2672 } 2673 } 2674 return (availbytes); 2675 } 2676 2677 int 2678 dsl_get_written(dsl_dataset_t *ds, uint64_t *written) 2679 { 2680 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2681 dsl_dataset_t *prev; 2682 int err = dsl_dataset_hold_obj(dp, 2683 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 2684 if (err == 0) { 2685 uint64_t comp, uncomp; 2686 err = dsl_dataset_space_written(prev, ds, written, 2687 &comp, &uncomp); 2688 dsl_dataset_rele(prev, FTAG); 2689 } 2690 return (err); 2691 } 2692 2693 /* 2694 * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN. 2695 */ 2696 int 2697 dsl_get_prev_snap(dsl_dataset_t *ds, char *snap) 2698 { 2699 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2700 if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 2701 dsl_dataset_name(ds->ds_prev, snap); 2702 return (0); 2703 } else { 2704 return (SET_ERROR(ENOENT)); 2705 } 2706 } 2707 2708 void 2709 dsl_get_redact_snaps(dsl_dataset_t *ds, nvlist_t *propval) 2710 { 2711 uint64_t nsnaps; 2712 uint64_t *snaps; 2713 if (dsl_dataset_get_uint64_array_feature(ds, 2714 SPA_FEATURE_REDACTED_DATASETS, &nsnaps, &snaps)) { 2715 fnvlist_add_uint64_array(propval, ZPROP_VALUE, snaps, 2716 nsnaps); 2717 } 2718 } 2719 2720 /* 2721 * Returns the mountpoint property and source for the given dataset in the value 2722 * and source buffers. The value buffer must be at least as large as MAXPATHLEN 2723 * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN. 2724 * Returns 0 on success and an error on failure. 2725 */ 2726 int 2727 dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value, 2728 char *source) 2729 { 2730 int error; 2731 dsl_pool_t *dp = ds->ds_dir->dd_pool; 2732 2733 /* Retrieve the mountpoint value stored in the zap object */ 2734 error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1, 2735 ZAP_MAXVALUELEN, value, source); 2736 if (error != 0) { 2737 return (error); 2738 } 2739 2740 /* 2741 * Process the dsname and source to find the full mountpoint string. 2742 * Can be skipped for 'legacy' or 'none'. 2743 */ 2744 if (value[0] == '/') { 2745 char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 2746 char *root = buf; 2747 const char *relpath; 2748 2749 /* 2750 * If we inherit the mountpoint, even from a dataset 2751 * with a received value, the source will be the path of 2752 * the dataset we inherit from. If source is 2753 * ZPROP_SOURCE_VAL_RECVD, the received value is not 2754 * inherited. 2755 */ 2756 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2757 relpath = ""; 2758 } else { 2759 ASSERT0(strncmp(dsname, source, strlen(source))); 2760 relpath = dsname + strlen(source); 2761 if (relpath[0] == '/') 2762 relpath++; 2763 } 2764 2765 spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN); 2766 2767 /* 2768 * Special case an alternate root of '/'. This will 2769 * avoid having multiple leading slashes in the 2770 * mountpoint path. 2771 */ 2772 if (strcmp(root, "/") == 0) 2773 root++; 2774 2775 /* 2776 * If the mountpoint is '/' then skip over this 2777 * if we are obtaining either an alternate root or 2778 * an inherited mountpoint. 2779 */ 2780 char *mnt = value; 2781 if (value[1] == '\0' && (root[0] != '\0' || 2782 relpath[0] != '\0')) 2783 mnt = value + 1; 2784 2785 mnt = kmem_strdup(mnt); 2786 2787 if (relpath[0] == '\0') { 2788 (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s", 2789 root, mnt); 2790 } else { 2791 (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s", 2792 root, mnt, relpath[0] == '@' ? "" : "/", 2793 relpath); 2794 } 2795 kmem_free(buf, ZAP_MAXVALUELEN); 2796 kmem_strfree(mnt); 2797 } 2798 2799 return (0); 2800 } 2801 2802 void 2803 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2804 { 2805 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool; 2806 2807 ASSERT(dsl_pool_config_held(dp)); 2808 2809 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, 2810 dsl_get_refratio(ds)); 2811 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 2812 dsl_get_logicalreferenced(ds)); 2813 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 2814 dsl_get_compressratio(ds)); 2815 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 2816 dsl_get_used(ds)); 2817 2818 if (ds->ds_is_snapshot) { 2819 get_clones_stat(ds, nv); 2820 } else { 2821 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2822 if (dsl_get_prev_snap(ds, buf) == 0) 2823 dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, 2824 buf); 2825 dsl_dir_stats(ds->ds_dir, nv); 2826 } 2827 2828 nvlist_t *propval = fnvlist_alloc(); 2829 dsl_get_redact_snaps(ds, propval); 2830 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), 2831 propval); 2832 nvlist_free(propval); 2833 2834 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, 2835 dsl_get_available(ds)); 2836 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, 2837 dsl_get_referenced(ds)); 2838 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 2839 dsl_get_creation(ds)); 2840 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 2841 dsl_get_creationtxg(ds)); 2842 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 2843 dsl_get_refquota(ds)); 2844 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 2845 dsl_get_refreservation(ds)); 2846 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 2847 dsl_get_guid(ds)); 2848 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 2849 dsl_get_unique(ds)); 2850 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 2851 dsl_get_objsetid(ds)); 2852 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 2853 dsl_get_userrefs(ds)); 2854 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 2855 dsl_get_defer_destroy(ds)); 2856 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOTS_CHANGED, 2857 dsl_dir_snap_cmtime(ds->ds_dir).tv_sec); 2858 dsl_dataset_crypt_stats(ds, nv); 2859 2860 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 2861 uint64_t written; 2862 if (dsl_get_written(ds, &written) == 0) { 2863 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 2864 written); 2865 } 2866 } 2867 2868 if (!dsl_dataset_is_snapshot(ds)) { 2869 char *token = get_receive_resume_token(ds); 2870 if (token != NULL) { 2871 dsl_prop_nvlist_add_string(nv, 2872 ZFS_PROP_RECEIVE_RESUME_TOKEN, token); 2873 kmem_strfree(token); 2874 } 2875 } 2876 } 2877 2878 void 2879 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2880 { 2881 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool; 2882 ASSERT(dsl_pool_config_held(dp)); 2883 2884 stat->dds_creation_txg = dsl_get_creationtxg(ds); 2885 stat->dds_inconsistent = dsl_get_inconsistent(ds); 2886 stat->dds_guid = dsl_get_guid(ds); 2887 stat->dds_redacted = dsl_get_redacted(ds); 2888 stat->dds_origin[0] = '\0'; 2889 if (ds->ds_is_snapshot) { 2890 stat->dds_is_snapshot = B_TRUE; 2891 stat->dds_num_clones = dsl_get_numclones(ds); 2892 } else { 2893 stat->dds_is_snapshot = B_FALSE; 2894 stat->dds_num_clones = 0; 2895 2896 if (dsl_dir_is_clone(ds->ds_dir)) { 2897 dsl_dir_get_origin(ds->ds_dir, stat->dds_origin); 2898 } 2899 } 2900 } 2901 2902 uint64_t 2903 dsl_dataset_fsid_guid(dsl_dataset_t *ds) 2904 { 2905 return (ds->ds_fsid_guid); 2906 } 2907 2908 void 2909 dsl_dataset_space(dsl_dataset_t *ds, 2910 uint64_t *refdbytesp, uint64_t *availbytesp, 2911 uint64_t *usedobjsp, uint64_t *availobjsp) 2912 { 2913 *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 2914 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 2915 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 2916 *availbytesp += 2917 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 2918 if (ds->ds_quota != 0) { 2919 /* 2920 * Adjust available bytes according to refquota 2921 */ 2922 if (*refdbytesp < ds->ds_quota) 2923 *availbytesp = MIN(*availbytesp, 2924 ds->ds_quota - *refdbytesp); 2925 else 2926 *availbytesp = 0; 2927 } 2928 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 2929 *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 2930 rrw_exit(&ds->ds_bp_rwlock, FTAG); 2931 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2932 } 2933 2934 boolean_t 2935 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 2936 { 2937 dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool; 2938 uint64_t birth; 2939 2940 ASSERT(dsl_pool_config_held(dp)); 2941 if (snap == NULL) 2942 return (B_FALSE); 2943 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 2944 birth = BP_GET_LOGICAL_BIRTH(dsl_dataset_get_blkptr(ds)); 2945 rrw_exit(&ds->ds_bp_rwlock, FTAG); 2946 if (birth > dsl_dataset_phys(snap)->ds_creation_txg) { 2947 objset_t *os, *os_snap; 2948 /* 2949 * It may be that only the ZIL differs, because it was 2950 * reset in the head. Don't count that as being 2951 * modified. 2952 */ 2953 if (dmu_objset_from_ds(ds, &os) != 0) 2954 return (B_TRUE); 2955 if (dmu_objset_from_ds(snap, &os_snap) != 0) 2956 return (B_TRUE); 2957 return (memcmp(&os->os_phys->os_meta_dnode, 2958 &os_snap->os_phys->os_meta_dnode, 2959 sizeof (os->os_phys->os_meta_dnode)) != 0); 2960 } 2961 return (B_FALSE); 2962 } 2963 2964 static int 2965 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 2966 dsl_dataset_t *hds, void *arg) 2967 { 2968 (void) dp; 2969 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 2970 int error; 2971 uint64_t val; 2972 2973 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 2974 if (error != 0) { 2975 /* ignore nonexistent snapshots */ 2976 return (error == ENOENT ? 0 : error); 2977 } 2978 2979 /* new name should not exist */ 2980 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 2981 if (error == 0) 2982 error = SET_ERROR(EEXIST); 2983 else if (error == ENOENT) 2984 error = 0; 2985 2986 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 2987 if (dsl_dir_namelen(hds->ds_dir) + 1 + 2988 strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN) 2989 error = SET_ERROR(ENAMETOOLONG); 2990 2991 return (error); 2992 } 2993 2994 int 2995 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 2996 { 2997 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 2998 dsl_pool_t *dp = dmu_tx_pool(tx); 2999 dsl_dataset_t *hds; 3000 int error; 3001 3002 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 3003 if (error != 0) 3004 return (error); 3005 3006 if (ddrsa->ddrsa_recursive) { 3007 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 3008 dsl_dataset_rename_snapshot_check_impl, ddrsa, 3009 DS_FIND_CHILDREN); 3010 } else { 3011 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 3012 } 3013 dsl_dataset_rele(hds, FTAG); 3014 return (error); 3015 } 3016 3017 static int 3018 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 3019 dsl_dataset_t *hds, void *arg) 3020 { 3021 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 3022 dsl_dataset_t *ds; 3023 uint64_t val; 3024 dmu_tx_t *tx = ddrsa->ddrsa_tx; 3025 char *oldname, *newname; 3026 int error; 3027 3028 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 3029 ASSERT(error == 0 || error == ENOENT); 3030 if (error == ENOENT) { 3031 /* ignore nonexistent snapshots */ 3032 return (0); 3033 } 3034 3035 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 3036 3037 /* log before we change the name */ 3038 spa_history_log_internal_ds(ds, "rename", tx, 3039 "-> @%s", ddrsa->ddrsa_newsnapname); 3040 3041 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 3042 B_FALSE)); 3043 mutex_enter(&ds->ds_lock); 3044 (void) strlcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname, 3045 sizeof (ds->ds_snapname)); 3046 mutex_exit(&ds->ds_lock); 3047 VERIFY0(zap_add(dp->dp_meta_objset, 3048 dsl_dataset_phys(hds)->ds_snapnames_zapobj, 3049 ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 3050 3051 oldname = kmem_asprintf("%s@%s", ddrsa->ddrsa_fsname, 3052 ddrsa->ddrsa_oldsnapname); 3053 newname = kmem_asprintf("%s@%s", ddrsa->ddrsa_fsname, 3054 ddrsa->ddrsa_newsnapname); 3055 zvol_rename_minors(dp->dp_spa, oldname, newname, B_TRUE); 3056 kmem_strfree(oldname); 3057 kmem_strfree(newname); 3058 3059 dsl_dataset_rele(ds, FTAG); 3060 return (0); 3061 } 3062 3063 void 3064 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 3065 { 3066 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 3067 dsl_pool_t *dp = dmu_tx_pool(tx); 3068 dsl_dataset_t *hds = NULL; 3069 3070 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 3071 ddrsa->ddrsa_tx = tx; 3072 if (ddrsa->ddrsa_recursive) { 3073 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 3074 dsl_dataset_rename_snapshot_sync_impl, ddrsa, 3075 DS_FIND_CHILDREN)); 3076 } else { 3077 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 3078 } 3079 dsl_dataset_rele(hds, FTAG); 3080 } 3081 3082 int 3083 dsl_dataset_rename_snapshot(const char *fsname, 3084 const char *oldsnapname, const char *newsnapname, boolean_t recursive) 3085 { 3086 dsl_dataset_rename_snapshot_arg_t ddrsa; 3087 3088 ddrsa.ddrsa_fsname = fsname; 3089 ddrsa.ddrsa_oldsnapname = oldsnapname; 3090 ddrsa.ddrsa_newsnapname = newsnapname; 3091 ddrsa.ddrsa_recursive = recursive; 3092 3093 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 3094 dsl_dataset_rename_snapshot_sync, &ddrsa, 3095 1, ZFS_SPACE_CHECK_RESERVED)); 3096 } 3097 3098 /* 3099 * If we're doing an ownership handoff, we need to make sure that there is 3100 * only one long hold on the dataset. We're not allowed to change anything here 3101 * so we don't permanently release the long hold or regular hold here. We want 3102 * to do this only when syncing to avoid the dataset unexpectedly going away 3103 * when we release the long hold. 3104 */ 3105 static int 3106 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 3107 { 3108 boolean_t held = B_FALSE; 3109 3110 if (!dmu_tx_is_syncing(tx)) 3111 return (0); 3112 3113 dsl_dir_t *dd = ds->ds_dir; 3114 mutex_enter(&dd->dd_activity_lock); 3115 uint64_t holds = zfs_refcount_count(&ds->ds_longholds) - 3116 (owner != NULL ? 1 : 0); 3117 /* 3118 * The value of dd_activity_waiters can chance as soon as we drop the 3119 * lock, but we're fine with that; new waiters coming in or old 3120 * waiters leaving doesn't cause problems, since we're going to cancel 3121 * waiters later anyway. The goal of this check is to verify that no 3122 * non-waiters have long-holds, and all new long-holds will be 3123 * prevented because we're holding the pool config as writer. 3124 */ 3125 if (holds != dd->dd_activity_waiters) 3126 held = B_TRUE; 3127 mutex_exit(&dd->dd_activity_lock); 3128 3129 if (held) 3130 return (SET_ERROR(EBUSY)); 3131 3132 return (0); 3133 } 3134 3135 int 3136 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 3137 { 3138 dsl_dataset_rollback_arg_t *ddra = arg; 3139 dsl_pool_t *dp = dmu_tx_pool(tx); 3140 dsl_dataset_t *ds; 3141 int64_t unused_refres_delta; 3142 int error; 3143 3144 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 3145 if (error != 0) 3146 return (error); 3147 3148 /* must not be a snapshot */ 3149 if (ds->ds_is_snapshot) { 3150 dsl_dataset_rele(ds, FTAG); 3151 return (SET_ERROR(EINVAL)); 3152 } 3153 3154 /* must have a most recent snapshot */ 3155 if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 3156 dsl_dataset_rele(ds, FTAG); 3157 return (SET_ERROR(ESRCH)); 3158 } 3159 3160 /* 3161 * No rollback to a snapshot created in the current txg, because 3162 * the rollback may dirty the dataset and create blocks that are 3163 * not reachable from the rootbp while having a birth txg that 3164 * falls into the snapshot's range. 3165 */ 3166 if (dmu_tx_is_syncing(tx) && 3167 dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) { 3168 dsl_dataset_rele(ds, FTAG); 3169 return (SET_ERROR(EAGAIN)); 3170 } 3171 3172 /* 3173 * If the expected target snapshot is specified, then check that 3174 * the latest snapshot is it. 3175 */ 3176 if (ddra->ddra_tosnap != NULL) { 3177 dsl_dataset_t *snapds; 3178 3179 /* Check if the target snapshot exists at all. */ 3180 error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds); 3181 if (error != 0) { 3182 /* 3183 * ESRCH is used to signal that the target snapshot does 3184 * not exist, while ENOENT is used to report that 3185 * the rolled back dataset does not exist. 3186 * ESRCH is also used to cover other cases where the 3187 * target snapshot is not related to the dataset being 3188 * rolled back such as being in a different pool. 3189 */ 3190 if (error == ENOENT || error == EXDEV) 3191 error = SET_ERROR(ESRCH); 3192 dsl_dataset_rele(ds, FTAG); 3193 return (error); 3194 } 3195 ASSERT(snapds->ds_is_snapshot); 3196 3197 /* Check if the snapshot is the latest snapshot indeed. */ 3198 if (snapds != ds->ds_prev) { 3199 /* 3200 * Distinguish between the case where the only problem 3201 * is intervening snapshots (EEXIST) vs the snapshot 3202 * not being a valid target for rollback (ESRCH). 3203 */ 3204 if (snapds->ds_dir == ds->ds_dir || 3205 (dsl_dir_is_clone(ds->ds_dir) && 3206 dsl_dir_phys(ds->ds_dir)->dd_origin_obj == 3207 snapds->ds_object)) { 3208 error = SET_ERROR(EEXIST); 3209 } else { 3210 error = SET_ERROR(ESRCH); 3211 } 3212 dsl_dataset_rele(snapds, FTAG); 3213 dsl_dataset_rele(ds, FTAG); 3214 return (error); 3215 } 3216 dsl_dataset_rele(snapds, FTAG); 3217 } 3218 3219 /* must not have any bookmarks after the most recent snapshot */ 3220 if (dsl_bookmark_latest_txg(ds) > 3221 dsl_dataset_phys(ds)->ds_prev_snap_txg) { 3222 dsl_dataset_rele(ds, FTAG); 3223 return (SET_ERROR(EEXIST)); 3224 } 3225 3226 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 3227 if (error != 0) { 3228 dsl_dataset_rele(ds, FTAG); 3229 return (error); 3230 } 3231 3232 /* 3233 * Check if the snap we are rolling back to uses more than 3234 * the refquota. 3235 */ 3236 if (ds->ds_quota != 0 && 3237 dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 3238 dsl_dataset_rele(ds, FTAG); 3239 return (SET_ERROR(EDQUOT)); 3240 } 3241 3242 /* 3243 * When we do the clone swap, we will temporarily use more space 3244 * due to the refreservation (the head will no longer have any 3245 * unique space, so the entire amount of the refreservation will need 3246 * to be free). We will immediately destroy the clone, freeing 3247 * this space, but the freeing happens over many txg's. 3248 */ 3249 unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 3250 dsl_dataset_phys(ds)->ds_unique_bytes); 3251 3252 if (unused_refres_delta > 0 && 3253 unused_refres_delta > 3254 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 3255 dsl_dataset_rele(ds, FTAG); 3256 return (SET_ERROR(ENOSPC)); 3257 } 3258 3259 dsl_dataset_rele(ds, FTAG); 3260 return (0); 3261 } 3262 3263 void 3264 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 3265 { 3266 dsl_dataset_rollback_arg_t *ddra = arg; 3267 dsl_pool_t *dp = dmu_tx_pool(tx); 3268 dsl_dataset_t *ds, *clone; 3269 uint64_t cloneobj; 3270 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 3271 3272 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 3273 3274 dsl_dataset_name(ds->ds_prev, namebuf); 3275 fnvlist_add_string(ddra->ddra_result, "target", namebuf); 3276 3277 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 3278 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, NULL, tx); 3279 3280 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 3281 3282 dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 3283 dsl_dataset_zero_zil(ds, tx); 3284 3285 dsl_destroy_head_sync_impl(clone, tx); 3286 3287 dsl_dataset_rele(clone, FTAG); 3288 dsl_dataset_rele(ds, FTAG); 3289 } 3290 3291 /* 3292 * Rolls back the given filesystem or volume to the most recent snapshot. 3293 * The name of the most recent snapshot will be returned under key "target" 3294 * in the result nvlist. 3295 * 3296 * If owner != NULL: 3297 * - The existing dataset MUST be owned by the specified owner at entry 3298 * - Upon return, dataset will still be held by the same owner, whether we 3299 * succeed or not. 3300 * 3301 * This mode is required any time the existing filesystem is mounted. See 3302 * notes above zfs_suspend_fs() for further details. 3303 */ 3304 int 3305 dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner, 3306 nvlist_t *result) 3307 { 3308 dsl_dataset_rollback_arg_t ddra; 3309 3310 ddra.ddra_fsname = fsname; 3311 ddra.ddra_tosnap = tosnap; 3312 ddra.ddra_owner = owner; 3313 ddra.ddra_result = result; 3314 3315 return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 3316 dsl_dataset_rollback_sync, &ddra, 3317 1, ZFS_SPACE_CHECK_RESERVED)); 3318 } 3319 3320 struct promotenode { 3321 list_node_t link; 3322 dsl_dataset_t *ds; 3323 }; 3324 3325 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 3326 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 3327 const void *tag); 3328 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag); 3329 3330 int 3331 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 3332 { 3333 dsl_dataset_promote_arg_t *ddpa = arg; 3334 dsl_pool_t *dp = dmu_tx_pool(tx); 3335 dsl_dataset_t *hds; 3336 struct promotenode *snap; 3337 int err; 3338 uint64_t unused; 3339 uint64_t ss_mv_cnt; 3340 size_t max_snap_len; 3341 boolean_t conflicting_snaps; 3342 3343 err = promote_hold(ddpa, dp, FTAG); 3344 if (err != 0) 3345 return (err); 3346 3347 hds = ddpa->ddpa_clone; 3348 max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1; 3349 3350 if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 3351 promote_rele(ddpa, FTAG); 3352 return (SET_ERROR(EXDEV)); 3353 } 3354 3355 snap = list_head(&ddpa->shared_snaps); 3356 if (snap == NULL) { 3357 err = SET_ERROR(ENOENT); 3358 goto out; 3359 } 3360 dsl_dataset_t *const origin_ds = snap->ds; 3361 3362 /* 3363 * Encrypted clones share a DSL Crypto Key with their origin's dsl dir. 3364 * When doing a promote we must make sure the encryption root for 3365 * both the target and the target's origin does not change to avoid 3366 * needing to rewrap encryption keys 3367 */ 3368 err = dsl_dataset_promote_crypt_check(hds->ds_dir, origin_ds->ds_dir); 3369 if (err != 0) 3370 goto out; 3371 3372 /* 3373 * Compute and check the amount of space to transfer. Since this is 3374 * so expensive, don't do the preliminary check. 3375 */ 3376 if (!dmu_tx_is_syncing(tx)) { 3377 promote_rele(ddpa, FTAG); 3378 return (0); 3379 } 3380 3381 /* compute origin's new unique space */ 3382 snap = list_tail(&ddpa->clone_snaps); 3383 ASSERT(snap != NULL); 3384 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 3385 origin_ds->ds_object); 3386 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 3387 dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 3388 &ddpa->unique, &unused, &unused); 3389 3390 /* 3391 * Walk the snapshots that we are moving 3392 * 3393 * Compute space to transfer. Consider the incremental changes 3394 * to used by each snapshot: 3395 * (my used) = (prev's used) + (blocks born) - (blocks killed) 3396 * So each snapshot gave birth to: 3397 * (blocks born) = (my used) - (prev's used) + (blocks killed) 3398 * So a sequence would look like: 3399 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 3400 * Which simplifies to: 3401 * uN + kN + kN-1 + ... + k1 + k0 3402 * Note however, if we stop before we reach the ORIGIN we get: 3403 * uN + kN + kN-1 + ... + kM - uM-1 3404 */ 3405 conflicting_snaps = B_FALSE; 3406 ss_mv_cnt = 0; 3407 ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 3408 ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 3409 ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 3410 for (snap = list_head(&ddpa->shared_snaps); snap; 3411 snap = list_next(&ddpa->shared_snaps, snap)) { 3412 uint64_t val, dlused, dlcomp, dluncomp; 3413 dsl_dataset_t *ds = snap->ds; 3414 3415 ss_mv_cnt++; 3416 3417 /* 3418 * If there are long holds, we won't be able to evict 3419 * the objset. 3420 */ 3421 if (dsl_dataset_long_held(ds)) { 3422 err = SET_ERROR(EBUSY); 3423 goto out; 3424 } 3425 3426 /* Check that the snapshot name does not conflict */ 3427 VERIFY0(dsl_dataset_get_snapname(ds)); 3428 if (strlen(ds->ds_snapname) >= max_snap_len) { 3429 err = SET_ERROR(ENAMETOOLONG); 3430 goto out; 3431 } 3432 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 3433 if (err == 0) { 3434 fnvlist_add_boolean(ddpa->err_ds, 3435 snap->ds->ds_snapname); 3436 conflicting_snaps = B_TRUE; 3437 } else if (err != ENOENT) { 3438 goto out; 3439 } 3440 3441 /* The very first snapshot does not have a deadlist */ 3442 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 3443 continue; 3444 3445 dsl_deadlist_space(&ds->ds_deadlist, 3446 &dlused, &dlcomp, &dluncomp); 3447 ddpa->used += dlused; 3448 ddpa->comp += dlcomp; 3449 ddpa->uncomp += dluncomp; 3450 } 3451 3452 /* 3453 * Check that bookmarks that are being transferred don't have 3454 * name conflicts. 3455 */ 3456 for (dsl_bookmark_node_t *dbn = avl_first(&origin_ds->ds_bookmarks); 3457 dbn != NULL && dbn->dbn_phys.zbm_creation_txg <= 3458 dsl_dataset_phys(origin_ds)->ds_creation_txg; 3459 dbn = AVL_NEXT(&origin_ds->ds_bookmarks, dbn)) { 3460 if (strlen(dbn->dbn_name) >= max_snap_len) { 3461 err = SET_ERROR(ENAMETOOLONG); 3462 goto out; 3463 } 3464 zfs_bookmark_phys_t bm; 3465 err = dsl_bookmark_lookup_impl(ddpa->ddpa_clone, 3466 dbn->dbn_name, &bm); 3467 3468 if (err == 0) { 3469 fnvlist_add_boolean(ddpa->err_ds, dbn->dbn_name); 3470 conflicting_snaps = B_TRUE; 3471 } else if (err == ESRCH) { 3472 err = 0; 3473 } 3474 if (err != 0) { 3475 goto out; 3476 } 3477 } 3478 3479 /* 3480 * In order to return the full list of conflicting snapshots, we check 3481 * whether there was a conflict after traversing all of them. 3482 */ 3483 if (conflicting_snaps) { 3484 err = SET_ERROR(EEXIST); 3485 goto out; 3486 } 3487 3488 /* 3489 * If we are a clone of a clone then we never reached ORIGIN, 3490 * so we need to subtract out the clone origin's used space. 3491 */ 3492 if (ddpa->origin_origin) { 3493 ddpa->used -= 3494 dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 3495 ddpa->comp -= 3496 dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 3497 ddpa->uncomp -= 3498 dsl_dataset_phys(ddpa->origin_origin)-> 3499 ds_uncompressed_bytes; 3500 } 3501 3502 /* Check that there is enough space and limit headroom here */ 3503 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 3504 0, ss_mv_cnt, ddpa->used, ddpa->cr); 3505 if (err != 0) 3506 goto out; 3507 3508 /* 3509 * Compute the amounts of space that will be used by snapshots 3510 * after the promotion (for both origin and clone). For each, 3511 * it is the amount of space that will be on all of their 3512 * deadlists (that was not born before their new origin). 3513 */ 3514 if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 3515 uint64_t space; 3516 3517 /* 3518 * Note, typically this will not be a clone of a clone, 3519 * so dd_origin_txg will be < TXG_INITIAL, so 3520 * these snaplist_space() -> dsl_deadlist_space_range() 3521 * calls will be fast because they do not have to 3522 * iterate over all bps. 3523 */ 3524 snap = list_head(&ddpa->origin_snaps); 3525 if (snap == NULL) { 3526 err = SET_ERROR(ENOENT); 3527 goto out; 3528 } 3529 err = snaplist_space(&ddpa->shared_snaps, 3530 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 3531 if (err != 0) 3532 goto out; 3533 3534 err = snaplist_space(&ddpa->clone_snaps, 3535 snap->ds->ds_dir->dd_origin_txg, &space); 3536 if (err != 0) 3537 goto out; 3538 ddpa->cloneusedsnap += space; 3539 } 3540 if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 3541 DD_FLAG_USED_BREAKDOWN) { 3542 err = snaplist_space(&ddpa->origin_snaps, 3543 dsl_dataset_phys(origin_ds)->ds_creation_txg, 3544 &ddpa->originusedsnap); 3545 if (err != 0) 3546 goto out; 3547 } 3548 3549 out: 3550 promote_rele(ddpa, FTAG); 3551 return (err); 3552 } 3553 3554 void 3555 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 3556 { 3557 dsl_dataset_promote_arg_t *ddpa = arg; 3558 dsl_pool_t *dp = dmu_tx_pool(tx); 3559 dsl_dataset_t *hds; 3560 struct promotenode *snap; 3561 dsl_dataset_t *origin_ds; 3562 dsl_dataset_t *origin_head; 3563 dsl_dir_t *dd; 3564 dsl_dir_t *odd = NULL; 3565 uint64_t oldnext_obj; 3566 int64_t delta; 3567 3568 ASSERT(nvlist_empty(ddpa->err_ds)); 3569 3570 VERIFY0(promote_hold(ddpa, dp, FTAG)); 3571 hds = ddpa->ddpa_clone; 3572 3573 ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 3574 3575 snap = list_head(&ddpa->shared_snaps); 3576 origin_ds = snap->ds; 3577 dd = hds->ds_dir; 3578 3579 snap = list_head(&ddpa->origin_snaps); 3580 origin_head = snap->ds; 3581 3582 /* 3583 * We need to explicitly open odd, since origin_ds's dd will be 3584 * changing. 3585 */ 3586 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 3587 NULL, FTAG, &odd)); 3588 3589 dsl_dataset_promote_crypt_sync(hds->ds_dir, odd, tx); 3590 3591 /* change origin's next snap */ 3592 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 3593 oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 3594 snap = list_tail(&ddpa->clone_snaps); 3595 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 3596 origin_ds->ds_object); 3597 dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 3598 3599 /* change the origin's next clone */ 3600 if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 3601 dsl_dataset_remove_from_next_clones(origin_ds, 3602 snap->ds->ds_object, tx); 3603 VERIFY0(zap_add_int(dp->dp_meta_objset, 3604 dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 3605 oldnext_obj, tx)); 3606 } 3607 3608 /* change origin */ 3609 dmu_buf_will_dirty(dd->dd_dbuf, tx); 3610 ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 3611 dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 3612 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 3613 dmu_buf_will_dirty(odd->dd_dbuf, tx); 3614 dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 3615 origin_head->ds_dir->dd_origin_txg = 3616 dsl_dataset_phys(origin_ds)->ds_creation_txg; 3617 3618 /* change dd_clone entries */ 3619 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 3620 VERIFY0(zap_remove_int(dp->dp_meta_objset, 3621 dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 3622 VERIFY0(zap_add_int(dp->dp_meta_objset, 3623 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 3624 hds->ds_object, tx)); 3625 3626 VERIFY0(zap_remove_int(dp->dp_meta_objset, 3627 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 3628 origin_head->ds_object, tx)); 3629 if (dsl_dir_phys(dd)->dd_clones == 0) { 3630 dsl_dir_phys(dd)->dd_clones = 3631 zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 3632 DMU_OT_NONE, 0, tx); 3633 } 3634 VERIFY0(zap_add_int(dp->dp_meta_objset, 3635 dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 3636 } 3637 3638 /* 3639 * Move bookmarks to this dir. 3640 */ 3641 dsl_bookmark_node_t *dbn_next; 3642 for (dsl_bookmark_node_t *dbn = avl_first(&origin_head->ds_bookmarks); 3643 dbn != NULL && dbn->dbn_phys.zbm_creation_txg <= 3644 dsl_dataset_phys(origin_ds)->ds_creation_txg; 3645 dbn = dbn_next) { 3646 dbn_next = AVL_NEXT(&origin_head->ds_bookmarks, dbn); 3647 3648 avl_remove(&origin_head->ds_bookmarks, dbn); 3649 VERIFY0(zap_remove(dp->dp_meta_objset, 3650 origin_head->ds_bookmarks_obj, dbn->dbn_name, tx)); 3651 3652 dsl_bookmark_node_add(hds, dbn, tx); 3653 } 3654 3655 dsl_bookmark_next_changed(hds, origin_ds, tx); 3656 3657 /* move snapshots to this dir */ 3658 for (snap = list_head(&ddpa->shared_snaps); snap; 3659 snap = list_next(&ddpa->shared_snaps, snap)) { 3660 dsl_dataset_t *ds = snap->ds; 3661 3662 /* 3663 * Property callbacks are registered to a particular 3664 * dsl_dir. Since ours is changing, evict the objset 3665 * so that they will be unregistered from the old dsl_dir. 3666 */ 3667 if (ds->ds_objset) { 3668 dmu_objset_evict(ds->ds_objset); 3669 ds->ds_objset = NULL; 3670 } 3671 3672 /* move snap name entry */ 3673 VERIFY0(dsl_dataset_get_snapname(ds)); 3674 VERIFY0(dsl_dataset_snap_remove(origin_head, 3675 ds->ds_snapname, tx, B_TRUE)); 3676 VERIFY0(zap_add(dp->dp_meta_objset, 3677 dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 3678 8, 1, &ds->ds_object, tx)); 3679 dsl_fs_ss_count_adjust(hds->ds_dir, 1, 3680 DD_FIELD_SNAPSHOT_COUNT, tx); 3681 3682 /* change containing dsl_dir */ 3683 dmu_buf_will_dirty(ds->ds_dbuf, tx); 3684 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 3685 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 3686 ASSERT3P(ds->ds_dir, ==, odd); 3687 dsl_dir_rele(ds->ds_dir, ds); 3688 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 3689 NULL, ds, &ds->ds_dir)); 3690 3691 /* move any clone references */ 3692 if (dsl_dataset_phys(ds)->ds_next_clones_obj && 3693 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 3694 zap_cursor_t zc; 3695 zap_attribute_t *za = zap_attribute_alloc(); 3696 3697 for (zap_cursor_init(&zc, dp->dp_meta_objset, 3698 dsl_dataset_phys(ds)->ds_next_clones_obj); 3699 zap_cursor_retrieve(&zc, za) == 0; 3700 zap_cursor_advance(&zc)) { 3701 dsl_dataset_t *cnds; 3702 uint64_t o; 3703 3704 if (za->za_first_integer == oldnext_obj) { 3705 /* 3706 * We've already moved the 3707 * origin's reference. 3708 */ 3709 continue; 3710 } 3711 3712 VERIFY0(dsl_dataset_hold_obj(dp, 3713 za->za_first_integer, FTAG, &cnds)); 3714 o = dsl_dir_phys(cnds->ds_dir)-> 3715 dd_head_dataset_obj; 3716 3717 VERIFY0(zap_remove_int(dp->dp_meta_objset, 3718 dsl_dir_phys(odd)->dd_clones, o, tx)); 3719 VERIFY0(zap_add_int(dp->dp_meta_objset, 3720 dsl_dir_phys(dd)->dd_clones, o, tx)); 3721 dsl_dataset_rele(cnds, FTAG); 3722 } 3723 zap_cursor_fini(&zc); 3724 zap_attribute_free(za); 3725 } 3726 3727 ASSERT(!dsl_prop_hascb(ds)); 3728 } 3729 3730 /* 3731 * Change space accounting. 3732 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 3733 * both be valid, or both be 0 (resulting in delta == 0). This 3734 * is true for each of {clone,origin} independently. 3735 */ 3736 3737 delta = ddpa->cloneusedsnap - 3738 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 3739 ASSERT3S(delta, >=, 0); 3740 ASSERT3U(ddpa->used, >=, delta); 3741 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 3742 dsl_dir_diduse_space(dd, DD_USED_HEAD, 3743 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 3744 3745 delta = ddpa->originusedsnap - 3746 dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 3747 ASSERT3S(delta, <=, 0); 3748 ASSERT3U(ddpa->used, >=, -delta); 3749 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 3750 dsl_dir_diduse_space(odd, DD_USED_HEAD, 3751 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 3752 3753 dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 3754 3755 /* 3756 * Since livelists are specific to a clone's origin txg, they 3757 * are no longer accurate. Destroy the livelist from the clone being 3758 * promoted. If the origin dataset is a clone, destroy its livelist 3759 * as well. 3760 */ 3761 dsl_dir_remove_livelist(dd, tx, B_TRUE); 3762 dsl_dir_remove_livelist(odd, tx, B_TRUE); 3763 3764 /* log history record */ 3765 spa_history_log_internal_ds(hds, "promote", tx, " "); 3766 3767 dsl_dir_rele(odd, FTAG); 3768 3769 /* 3770 * Transfer common error blocks from old head to new head, before 3771 * calling promote_rele() on ddpa since we need to dereference 3772 * origin_head and hds. 3773 */ 3774 if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_HEAD_ERRLOG)) { 3775 uint64_t old_head = origin_head->ds_object; 3776 uint64_t new_head = hds->ds_object; 3777 spa_swap_errlog(dp->dp_spa, new_head, old_head, tx); 3778 } 3779 3780 promote_rele(ddpa, FTAG); 3781 } 3782 3783 /* 3784 * Make a list of dsl_dataset_t's for the snapshots between first_obj 3785 * (exclusive) and last_obj (inclusive). The list will be in reverse 3786 * order (last_obj will be the list_head()). If first_obj == 0, do all 3787 * snapshots back to this dataset's origin. 3788 */ 3789 static int 3790 snaplist_make(dsl_pool_t *dp, 3791 uint64_t first_obj, uint64_t last_obj, list_t *l, const void *tag) 3792 { 3793 uint64_t obj = last_obj; 3794 3795 list_create(l, sizeof (struct promotenode), 3796 offsetof(struct promotenode, link)); 3797 3798 while (obj != first_obj) { 3799 dsl_dataset_t *ds; 3800 struct promotenode *snap; 3801 int err; 3802 3803 err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 3804 ASSERT(err != ENOENT); 3805 if (err != 0) 3806 return (err); 3807 3808 if (first_obj == 0) 3809 first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 3810 3811 snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 3812 snap->ds = ds; 3813 list_insert_tail(l, snap); 3814 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 3815 } 3816 3817 return (0); 3818 } 3819 3820 static int 3821 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 3822 { 3823 struct promotenode *snap; 3824 3825 *spacep = 0; 3826 for (snap = list_head(l); snap; snap = list_next(l, snap)) { 3827 uint64_t used, comp, uncomp; 3828 dsl_deadlist_space_range(&snap->ds->ds_deadlist, 3829 mintxg, UINT64_MAX, &used, &comp, &uncomp); 3830 *spacep += used; 3831 } 3832 return (0); 3833 } 3834 3835 static void 3836 snaplist_destroy(list_t *l, const void *tag) 3837 { 3838 struct promotenode *snap; 3839 3840 if (l == NULL || !list_link_active(&l->list_head)) 3841 return; 3842 3843 while ((snap = list_remove_tail(l)) != NULL) { 3844 dsl_dataset_rele(snap->ds, tag); 3845 kmem_free(snap, sizeof (*snap)); 3846 } 3847 list_destroy(l); 3848 } 3849 3850 static int 3851 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, const void *tag) 3852 { 3853 int error; 3854 dsl_dir_t *dd; 3855 struct promotenode *snap; 3856 3857 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 3858 &ddpa->ddpa_clone); 3859 if (error != 0) 3860 return (error); 3861 dd = ddpa->ddpa_clone->ds_dir; 3862 3863 if (ddpa->ddpa_clone->ds_is_snapshot || 3864 !dsl_dir_is_clone(dd)) { 3865 dsl_dataset_rele(ddpa->ddpa_clone, tag); 3866 return (SET_ERROR(EINVAL)); 3867 } 3868 3869 error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 3870 &ddpa->shared_snaps, tag); 3871 if (error != 0) 3872 goto out; 3873 3874 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 3875 &ddpa->clone_snaps, tag); 3876 if (error != 0) 3877 goto out; 3878 3879 snap = list_head(&ddpa->shared_snaps); 3880 ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 3881 error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 3882 dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 3883 &ddpa->origin_snaps, tag); 3884 if (error != 0) 3885 goto out; 3886 3887 if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 3888 error = dsl_dataset_hold_obj(dp, 3889 dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 3890 tag, &ddpa->origin_origin); 3891 if (error != 0) 3892 goto out; 3893 } 3894 out: 3895 if (error != 0) 3896 promote_rele(ddpa, tag); 3897 return (error); 3898 } 3899 3900 static void 3901 promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag) 3902 { 3903 snaplist_destroy(&ddpa->shared_snaps, tag); 3904 snaplist_destroy(&ddpa->clone_snaps, tag); 3905 snaplist_destroy(&ddpa->origin_snaps, tag); 3906 if (ddpa->origin_origin != NULL) 3907 dsl_dataset_rele(ddpa->origin_origin, tag); 3908 dsl_dataset_rele(ddpa->ddpa_clone, tag); 3909 } 3910 3911 /* 3912 * Promote a clone. 3913 * 3914 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 3915 * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.) 3916 */ 3917 int 3918 dsl_dataset_promote(const char *name, char *conflsnap) 3919 { 3920 dsl_dataset_promote_arg_t ddpa = { 0 }; 3921 uint64_t numsnaps; 3922 int error; 3923 nvpair_t *snap_pair; 3924 objset_t *os; 3925 3926 /* 3927 * We will modify space proportional to the number of 3928 * snapshots. Compute numsnaps. 3929 */ 3930 error = dmu_objset_hold(name, FTAG, &os); 3931 if (error != 0) 3932 return (error); 3933 error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 3934 dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 3935 &numsnaps); 3936 dmu_objset_rele(os, FTAG); 3937 if (error != 0) 3938 return (error); 3939 3940 cred_t *cr = CRED(); 3941 crhold(cr); 3942 3943 ddpa.ddpa_clonename = name; 3944 ddpa.err_ds = fnvlist_alloc(); 3945 ddpa.cr = cr; 3946 3947 error = dsl_sync_task(name, dsl_dataset_promote_check, 3948 dsl_dataset_promote_sync, &ddpa, 3949 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED); 3950 3951 crfree(cr); 3952 3953 /* 3954 * Return the first conflicting snapshot found. 3955 */ 3956 snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL); 3957 if (snap_pair != NULL && conflsnap != NULL) 3958 (void) strlcpy(conflsnap, nvpair_name(snap_pair), 3959 ZFS_MAX_DATASET_NAME_LEN); 3960 3961 fnvlist_free(ddpa.err_ds); 3962 return (error); 3963 } 3964 3965 int 3966 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 3967 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 3968 { 3969 /* 3970 * "slack" factor for received datasets with refquota set on them. 3971 * See the bottom of this function for details on its use. 3972 */ 3973 uint64_t refquota_slack = (uint64_t)DMU_MAX_ACCESS * 3974 spa_asize_inflation; 3975 int64_t unused_refres_delta; 3976 3977 /* they should both be heads */ 3978 if (clone->ds_is_snapshot || 3979 origin_head->ds_is_snapshot) 3980 return (SET_ERROR(EINVAL)); 3981 3982 /* if we are not forcing, the branch point should be just before them */ 3983 if (!force && clone->ds_prev != origin_head->ds_prev) 3984 return (SET_ERROR(EINVAL)); 3985 3986 /* clone should be the clone (unless they are unrelated) */ 3987 if (clone->ds_prev != NULL && 3988 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 3989 origin_head->ds_dir != clone->ds_prev->ds_dir) 3990 return (SET_ERROR(EINVAL)); 3991 3992 /* the clone should be a child of the origin */ 3993 if (clone->ds_dir->dd_parent != origin_head->ds_dir) 3994 return (SET_ERROR(EINVAL)); 3995 3996 /* origin_head shouldn't be modified unless 'force' */ 3997 if (!force && 3998 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 3999 return (SET_ERROR(ETXTBSY)); 4000 4001 /* origin_head should have no long holds (e.g. is not mounted) */ 4002 if (dsl_dataset_handoff_check(origin_head, owner, tx)) 4003 return (SET_ERROR(EBUSY)); 4004 4005 /* check amount of any unconsumed refreservation */ 4006 unused_refres_delta = 4007 (int64_t)MIN(origin_head->ds_reserved, 4008 dsl_dataset_phys(origin_head)->ds_unique_bytes) - 4009 (int64_t)MIN(origin_head->ds_reserved, 4010 dsl_dataset_phys(clone)->ds_unique_bytes); 4011 4012 if (unused_refres_delta > 0 && 4013 unused_refres_delta > 4014 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 4015 return (SET_ERROR(ENOSPC)); 4016 4017 /* 4018 * The clone can't be too much over the head's refquota. 4019 * 4020 * To ensure that the entire refquota can be used, we allow one 4021 * transaction to exceed the refquota. Therefore, this check 4022 * needs to also allow for the space referenced to be more than the 4023 * refquota. The maximum amount of space that one transaction can use 4024 * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this 4025 * overage ensures that we are able to receive a filesystem that 4026 * exceeds the refquota on the source system. 4027 * 4028 * So that overage is the refquota_slack we use below. 4029 */ 4030 if (origin_head->ds_quota != 0 && 4031 dsl_dataset_phys(clone)->ds_referenced_bytes > 4032 origin_head->ds_quota + refquota_slack) 4033 return (SET_ERROR(EDQUOT)); 4034 4035 return (0); 4036 } 4037 4038 static void 4039 dsl_dataset_swap_remap_deadlists(dsl_dataset_t *clone, 4040 dsl_dataset_t *origin, dmu_tx_t *tx) 4041 { 4042 uint64_t clone_remap_dl_obj, origin_remap_dl_obj; 4043 dsl_pool_t *dp = dmu_tx_pool(tx); 4044 4045 ASSERT(dsl_pool_sync_context(dp)); 4046 4047 clone_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(clone); 4048 origin_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(origin); 4049 4050 if (clone_remap_dl_obj != 0) { 4051 dsl_deadlist_close(&clone->ds_remap_deadlist); 4052 dsl_dataset_unset_remap_deadlist_object(clone, tx); 4053 } 4054 if (origin_remap_dl_obj != 0) { 4055 dsl_deadlist_close(&origin->ds_remap_deadlist); 4056 dsl_dataset_unset_remap_deadlist_object(origin, tx); 4057 } 4058 4059 if (clone_remap_dl_obj != 0) { 4060 dsl_dataset_set_remap_deadlist_object(origin, 4061 clone_remap_dl_obj, tx); 4062 VERIFY0(dsl_deadlist_open(&origin->ds_remap_deadlist, 4063 dp->dp_meta_objset, clone_remap_dl_obj)); 4064 } 4065 if (origin_remap_dl_obj != 0) { 4066 dsl_dataset_set_remap_deadlist_object(clone, 4067 origin_remap_dl_obj, tx); 4068 VERIFY0(dsl_deadlist_open(&clone->ds_remap_deadlist, 4069 dp->dp_meta_objset, origin_remap_dl_obj)); 4070 } 4071 } 4072 4073 void 4074 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 4075 dsl_dataset_t *origin_head, dmu_tx_t *tx) 4076 { 4077 dsl_pool_t *dp = dmu_tx_pool(tx); 4078 int64_t unused_refres_delta; 4079 4080 ASSERT(clone->ds_reserved == 0); 4081 /* 4082 * NOTE: On DEBUG kernels there could be a race between this and 4083 * the check function if spa_asize_inflation is adjusted... 4084 */ 4085 ASSERT(origin_head->ds_quota == 0 || 4086 dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota + 4087 DMU_MAX_ACCESS * spa_asize_inflation); 4088 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 4089 4090 dsl_dir_cancel_waiters(origin_head->ds_dir); 4091 4092 /* 4093 * Swap per-dataset feature flags. 4094 */ 4095 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 4096 if (!(spa_feature_table[f].fi_flags & 4097 ZFEATURE_FLAG_PER_DATASET)) { 4098 ASSERT(!dsl_dataset_feature_is_active(clone, f)); 4099 ASSERT(!dsl_dataset_feature_is_active(origin_head, f)); 4100 continue; 4101 } 4102 4103 boolean_t clone_inuse = dsl_dataset_feature_is_active(clone, f); 4104 void *clone_feature = clone->ds_feature[f]; 4105 boolean_t origin_head_inuse = 4106 dsl_dataset_feature_is_active(origin_head, f); 4107 void *origin_head_feature = origin_head->ds_feature[f]; 4108 4109 if (clone_inuse) 4110 dsl_dataset_deactivate_feature_impl(clone, f, tx); 4111 if (origin_head_inuse) 4112 dsl_dataset_deactivate_feature_impl(origin_head, f, tx); 4113 4114 if (clone_inuse) { 4115 dsl_dataset_activate_feature(origin_head->ds_object, f, 4116 clone_feature, tx); 4117 origin_head->ds_feature[f] = clone_feature; 4118 } 4119 if (origin_head_inuse) { 4120 dsl_dataset_activate_feature(clone->ds_object, f, 4121 origin_head_feature, tx); 4122 clone->ds_feature[f] = origin_head_feature; 4123 } 4124 } 4125 4126 dmu_buf_will_dirty(clone->ds_dbuf, tx); 4127 dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 4128 4129 if (clone->ds_objset != NULL) { 4130 dmu_objset_evict(clone->ds_objset); 4131 clone->ds_objset = NULL; 4132 } 4133 4134 if (origin_head->ds_objset != NULL) { 4135 dmu_objset_evict(origin_head->ds_objset); 4136 origin_head->ds_objset = NULL; 4137 } 4138 4139 unused_refres_delta = 4140 (int64_t)MIN(origin_head->ds_reserved, 4141 dsl_dataset_phys(origin_head)->ds_unique_bytes) - 4142 (int64_t)MIN(origin_head->ds_reserved, 4143 dsl_dataset_phys(clone)->ds_unique_bytes); 4144 4145 /* 4146 * Reset origin's unique bytes. 4147 */ 4148 { 4149 dsl_dataset_t *origin = clone->ds_prev; 4150 uint64_t comp, uncomp; 4151 4152 dmu_buf_will_dirty(origin->ds_dbuf, tx); 4153 dsl_deadlist_space_range(&clone->ds_deadlist, 4154 dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 4155 &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 4156 } 4157 4158 /* swap blkptrs */ 4159 { 4160 rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG); 4161 rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG); 4162 blkptr_t tmp; 4163 tmp = dsl_dataset_phys(origin_head)->ds_bp; 4164 dsl_dataset_phys(origin_head)->ds_bp = 4165 dsl_dataset_phys(clone)->ds_bp; 4166 dsl_dataset_phys(clone)->ds_bp = tmp; 4167 rrw_exit(&origin_head->ds_bp_rwlock, FTAG); 4168 rrw_exit(&clone->ds_bp_rwlock, FTAG); 4169 } 4170 4171 /* set dd_*_bytes */ 4172 { 4173 int64_t dused, dcomp, duncomp; 4174 uint64_t cdl_used, cdl_comp, cdl_uncomp; 4175 uint64_t odl_used, odl_comp, odl_uncomp; 4176 4177 ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 4178 dd_used_breakdown[DD_USED_SNAP], ==, 0); 4179 4180 dsl_deadlist_space(&clone->ds_deadlist, 4181 &cdl_used, &cdl_comp, &cdl_uncomp); 4182 dsl_deadlist_space(&origin_head->ds_deadlist, 4183 &odl_used, &odl_comp, &odl_uncomp); 4184 4185 dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 4186 cdl_used - 4187 (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 4188 odl_used); 4189 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 4190 cdl_comp - 4191 (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 4192 odl_comp); 4193 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 4194 cdl_uncomp - 4195 (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 4196 odl_uncomp); 4197 4198 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 4199 dused, dcomp, duncomp, tx); 4200 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 4201 -dused, -dcomp, -duncomp, tx); 4202 4203 /* 4204 * The difference in the space used by snapshots is the 4205 * difference in snapshot space due to the head's 4206 * deadlist (since that's the only thing that's 4207 * changing that affects the snapused). 4208 */ 4209 dsl_deadlist_space_range(&clone->ds_deadlist, 4210 origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 4211 &cdl_used, &cdl_comp, &cdl_uncomp); 4212 dsl_deadlist_space_range(&origin_head->ds_deadlist, 4213 origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 4214 &odl_used, &odl_comp, &odl_uncomp); 4215 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 4216 DD_USED_HEAD, DD_USED_SNAP, tx); 4217 } 4218 4219 /* swap ds_*_bytes */ 4220 SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 4221 dsl_dataset_phys(clone)->ds_referenced_bytes); 4222 SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 4223 dsl_dataset_phys(clone)->ds_compressed_bytes); 4224 SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 4225 dsl_dataset_phys(clone)->ds_uncompressed_bytes); 4226 SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 4227 dsl_dataset_phys(clone)->ds_unique_bytes); 4228 4229 /* apply any parent delta for change in unconsumed refreservation */ 4230 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 4231 unused_refres_delta, 0, 0, tx); 4232 4233 /* 4234 * Swap deadlists. 4235 */ 4236 dsl_deadlist_close(&clone->ds_deadlist); 4237 dsl_deadlist_close(&origin_head->ds_deadlist); 4238 SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 4239 dsl_dataset_phys(clone)->ds_deadlist_obj); 4240 VERIFY0(dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 4241 dsl_dataset_phys(clone)->ds_deadlist_obj)); 4242 VERIFY0(dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 4243 dsl_dataset_phys(origin_head)->ds_deadlist_obj)); 4244 dsl_dataset_swap_remap_deadlists(clone, origin_head, tx); 4245 4246 /* 4247 * If there is a bookmark at the origin, its "next dataset" is 4248 * changing, so we need to reset its FBN. 4249 */ 4250 dsl_bookmark_next_changed(origin_head, origin_head->ds_prev, tx); 4251 4252 dsl_scan_ds_clone_swapped(origin_head, clone, tx); 4253 4254 /* 4255 * Destroy any livelists associated with the clone or the origin, 4256 * since after the swap the corresponding livelists are no longer 4257 * valid. 4258 */ 4259 dsl_dir_remove_livelist(clone->ds_dir, tx, B_TRUE); 4260 dsl_dir_remove_livelist(origin_head->ds_dir, tx, B_TRUE); 4261 4262 spa_history_log_internal_ds(clone, "clone swap", tx, 4263 "parent=%s", origin_head->ds_dir->dd_myname); 4264 } 4265 4266 /* 4267 * Given a pool name and a dataset object number in that pool, 4268 * return the name of that dataset. 4269 */ 4270 int 4271 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 4272 { 4273 dsl_pool_t *dp; 4274 dsl_dataset_t *ds; 4275 int error; 4276 4277 error = dsl_pool_hold(pname, FTAG, &dp); 4278 if (error != 0) 4279 return (error); 4280 4281 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 4282 if (error == 0) { 4283 dsl_dataset_name(ds, buf); 4284 dsl_dataset_rele(ds, FTAG); 4285 } 4286 dsl_pool_rele(dp, FTAG); 4287 4288 return (error); 4289 } 4290 4291 int 4292 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 4293 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 4294 { 4295 int error = 0; 4296 4297 ASSERT3S(asize, >, 0); 4298 4299 /* 4300 * *ref_rsrv is the portion of asize that will come from any 4301 * unconsumed refreservation space. 4302 */ 4303 *ref_rsrv = 0; 4304 4305 mutex_enter(&ds->ds_lock); 4306 /* 4307 * Make a space adjustment for reserved bytes. 4308 */ 4309 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 4310 ASSERT3U(*used, >=, 4311 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 4312 *used -= 4313 (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 4314 *ref_rsrv = 4315 asize - MIN(asize, parent_delta(ds, asize + inflight)); 4316 } 4317 4318 if (!check_quota || ds->ds_quota == 0) { 4319 mutex_exit(&ds->ds_lock); 4320 return (0); 4321 } 4322 /* 4323 * If they are requesting more space, and our current estimate 4324 * is over quota, they get to try again unless the actual 4325 * on-disk is over quota and there are no pending changes (which 4326 * may free up space for us). 4327 */ 4328 if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 4329 ds->ds_quota) { 4330 if (inflight > 0 || 4331 dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 4332 error = SET_ERROR(ERESTART); 4333 else 4334 error = SET_ERROR(EDQUOT); 4335 } 4336 mutex_exit(&ds->ds_lock); 4337 4338 return (error); 4339 } 4340 4341 typedef struct dsl_dataset_set_qr_arg { 4342 const char *ddsqra_name; 4343 zprop_source_t ddsqra_source; 4344 uint64_t ddsqra_value; 4345 } dsl_dataset_set_qr_arg_t; 4346 4347 4348 static int 4349 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 4350 { 4351 dsl_dataset_set_qr_arg_t *ddsqra = arg; 4352 dsl_pool_t *dp = dmu_tx_pool(tx); 4353 dsl_dataset_t *ds; 4354 int error; 4355 uint64_t newval; 4356 4357 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 4358 return (SET_ERROR(ENOTSUP)); 4359 4360 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 4361 if (error != 0) 4362 return (error); 4363 4364 if (ds->ds_is_snapshot) { 4365 dsl_dataset_rele(ds, FTAG); 4366 return (SET_ERROR(EINVAL)); 4367 } 4368 4369 error = dsl_prop_predict(ds->ds_dir, 4370 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4371 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 4372 if (error != 0) { 4373 dsl_dataset_rele(ds, FTAG); 4374 return (error); 4375 } 4376 4377 if (newval == 0) { 4378 dsl_dataset_rele(ds, FTAG); 4379 return (0); 4380 } 4381 4382 if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 4383 newval < ds->ds_reserved) { 4384 dsl_dataset_rele(ds, FTAG); 4385 return (SET_ERROR(ENOSPC)); 4386 } 4387 4388 dsl_dataset_rele(ds, FTAG); 4389 return (0); 4390 } 4391 4392 static void 4393 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 4394 { 4395 dsl_dataset_set_qr_arg_t *ddsqra = arg; 4396 dsl_pool_t *dp = dmu_tx_pool(tx); 4397 dsl_dataset_t *ds = NULL; 4398 uint64_t newval; 4399 4400 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 4401 4402 dsl_prop_set_sync_impl(ds, 4403 zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4404 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 4405 &ddsqra->ddsqra_value, tx); 4406 4407 VERIFY0(dsl_prop_get_int_ds(ds, 4408 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 4409 4410 if (ds->ds_quota != newval) { 4411 dmu_buf_will_dirty(ds->ds_dbuf, tx); 4412 ds->ds_quota = newval; 4413 } 4414 dsl_dataset_rele(ds, FTAG); 4415 } 4416 4417 int 4418 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 4419 uint64_t refquota) 4420 { 4421 dsl_dataset_set_qr_arg_t ddsqra; 4422 4423 ddsqra.ddsqra_name = dsname; 4424 ddsqra.ddsqra_source = source; 4425 ddsqra.ddsqra_value = refquota; 4426 4427 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 4428 dsl_dataset_set_refquota_sync, &ddsqra, 0, 4429 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 4430 } 4431 4432 static int 4433 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 4434 { 4435 dsl_dataset_set_qr_arg_t *ddsqra = arg; 4436 dsl_pool_t *dp = dmu_tx_pool(tx); 4437 dsl_dataset_t *ds; 4438 int error; 4439 uint64_t newval, unique; 4440 4441 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 4442 return (SET_ERROR(ENOTSUP)); 4443 4444 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 4445 if (error != 0) 4446 return (error); 4447 4448 if (ds->ds_is_snapshot) { 4449 dsl_dataset_rele(ds, FTAG); 4450 return (SET_ERROR(EINVAL)); 4451 } 4452 4453 error = dsl_prop_predict(ds->ds_dir, 4454 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4455 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 4456 if (error != 0) { 4457 dsl_dataset_rele(ds, FTAG); 4458 return (error); 4459 } 4460 4461 /* 4462 * If we are doing the preliminary check in open context, the 4463 * space estimates may be inaccurate. 4464 */ 4465 if (!dmu_tx_is_syncing(tx)) { 4466 dsl_dataset_rele(ds, FTAG); 4467 return (0); 4468 } 4469 4470 mutex_enter(&ds->ds_lock); 4471 if (!DS_UNIQUE_IS_ACCURATE(ds)) 4472 dsl_dataset_recalc_head_uniq(ds); 4473 unique = dsl_dataset_phys(ds)->ds_unique_bytes; 4474 mutex_exit(&ds->ds_lock); 4475 4476 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 4477 uint64_t delta = MAX(unique, newval) - 4478 MAX(unique, ds->ds_reserved); 4479 4480 if (delta > 4481 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 4482 (ds->ds_quota > 0 && newval > ds->ds_quota)) { 4483 dsl_dataset_rele(ds, FTAG); 4484 return (SET_ERROR(ENOSPC)); 4485 } 4486 } 4487 4488 dsl_dataset_rele(ds, FTAG); 4489 return (0); 4490 } 4491 4492 void 4493 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 4494 zprop_source_t source, uint64_t value, dmu_tx_t *tx) 4495 { 4496 uint64_t newval; 4497 uint64_t unique; 4498 int64_t delta; 4499 4500 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4501 source, sizeof (value), 1, &value, tx); 4502 4503 VERIFY0(dsl_prop_get_int_ds(ds, 4504 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 4505 4506 dmu_buf_will_dirty(ds->ds_dbuf, tx); 4507 mutex_enter(&ds->ds_dir->dd_lock); 4508 mutex_enter(&ds->ds_lock); 4509 ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 4510 unique = dsl_dataset_phys(ds)->ds_unique_bytes; 4511 delta = MAX(0, (int64_t)(newval - unique)) - 4512 MAX(0, (int64_t)(ds->ds_reserved - unique)); 4513 ds->ds_reserved = newval; 4514 mutex_exit(&ds->ds_lock); 4515 4516 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 4517 mutex_exit(&ds->ds_dir->dd_lock); 4518 } 4519 4520 static void 4521 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 4522 { 4523 dsl_dataset_set_qr_arg_t *ddsqra = arg; 4524 dsl_pool_t *dp = dmu_tx_pool(tx); 4525 dsl_dataset_t *ds = NULL; 4526 4527 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 4528 dsl_dataset_set_refreservation_sync_impl(ds, 4529 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 4530 dsl_dataset_rele(ds, FTAG); 4531 } 4532 4533 int 4534 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 4535 uint64_t refreservation) 4536 { 4537 dsl_dataset_set_qr_arg_t ddsqra; 4538 4539 ddsqra.ddsqra_name = dsname; 4540 ddsqra.ddsqra_source = source; 4541 ddsqra.ddsqra_value = refreservation; 4542 4543 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 4544 dsl_dataset_set_refreservation_sync, &ddsqra, 0, 4545 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 4546 } 4547 4548 typedef struct dsl_dataset_set_compression_arg { 4549 const char *ddsca_name; 4550 zprop_source_t ddsca_source; 4551 uint64_t ddsca_value; 4552 } dsl_dataset_set_compression_arg_t; 4553 4554 static int 4555 dsl_dataset_set_compression_check(void *arg, dmu_tx_t *tx) 4556 { 4557 dsl_dataset_set_compression_arg_t *ddsca = arg; 4558 dsl_pool_t *dp = dmu_tx_pool(tx); 4559 4560 uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value); 4561 spa_feature_t f = zio_compress_to_feature(compval); 4562 4563 if (f == SPA_FEATURE_NONE) 4564 return (SET_ERROR(EINVAL)); 4565 4566 if (!spa_feature_is_enabled(dp->dp_spa, f)) 4567 return (SET_ERROR(ENOTSUP)); 4568 4569 return (0); 4570 } 4571 4572 static void 4573 dsl_dataset_set_compression_sync(void *arg, dmu_tx_t *tx) 4574 { 4575 dsl_dataset_set_compression_arg_t *ddsca = arg; 4576 dsl_pool_t *dp = dmu_tx_pool(tx); 4577 dsl_dataset_t *ds = NULL; 4578 4579 uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value); 4580 spa_feature_t f = zio_compress_to_feature(compval); 4581 ASSERT3S(f, !=, SPA_FEATURE_NONE); 4582 ASSERT3S(spa_feature_table[f].fi_type, ==, ZFEATURE_TYPE_BOOLEAN); 4583 4584 VERIFY0(dsl_dataset_hold(dp, ddsca->ddsca_name, FTAG, &ds)); 4585 if (zfeature_active(f, ds->ds_feature[f]) != B_TRUE) { 4586 ds->ds_feature_activation[f] = (void *)B_TRUE; 4587 dsl_dataset_activate_feature(ds->ds_object, f, 4588 ds->ds_feature_activation[f], tx); 4589 ds->ds_feature[f] = ds->ds_feature_activation[f]; 4590 } 4591 dsl_dataset_rele(ds, FTAG); 4592 } 4593 4594 int 4595 dsl_dataset_set_compression(const char *dsname, zprop_source_t source, 4596 uint64_t compression) 4597 { 4598 dsl_dataset_set_compression_arg_t ddsca; 4599 4600 /* 4601 * The sync task is only required for zstd in order to activate 4602 * the feature flag when the property is first set. 4603 */ 4604 if (ZIO_COMPRESS_ALGO(compression) != ZIO_COMPRESS_ZSTD) 4605 return (0); 4606 4607 ddsca.ddsca_name = dsname; 4608 ddsca.ddsca_source = source; 4609 ddsca.ddsca_value = compression; 4610 4611 return (dsl_sync_task(dsname, dsl_dataset_set_compression_check, 4612 dsl_dataset_set_compression_sync, &ddsca, 0, 4613 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 4614 } 4615 4616 /* 4617 * Return (in *usedp) the amount of space referenced by "new" that was not 4618 * referenced at the time the bookmark corresponds to. "New" may be a 4619 * snapshot or a head. The bookmark must be before new, in 4620 * new's filesystem (or its origin) -- caller verifies this. 4621 * 4622 * The written space is calculated by considering two components: First, we 4623 * ignore any freed space, and calculate the written as new's used space 4624 * minus old's used space. Next, we add in the amount of space that was freed 4625 * between the two time points, thus reducing new's used space relative to 4626 * old's. Specifically, this is the space that was born before 4627 * zbm_creation_txg, and freed before new (ie. on new's deadlist or a 4628 * previous deadlist). 4629 * 4630 * space freed [---------------------] 4631 * snapshots ---O-------O--------O-------O------ 4632 * bookmark new 4633 * 4634 * Note, the bookmark's zbm_*_bytes_refd must be valid, but if the HAS_FBN 4635 * flag is not set, we will calculate the freed_before_next based on the 4636 * next snapshot's deadlist, rather than using zbm_*_freed_before_next_snap. 4637 */ 4638 static int 4639 dsl_dataset_space_written_impl(zfs_bookmark_phys_t *bmp, 4640 dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 4641 { 4642 int err = 0; 4643 dsl_pool_t *dp = new->ds_dir->dd_pool; 4644 4645 ASSERT(dsl_pool_config_held(dp)); 4646 if (dsl_dataset_is_snapshot(new)) { 4647 ASSERT3U(bmp->zbm_creation_txg, <, 4648 dsl_dataset_phys(new)->ds_creation_txg); 4649 } 4650 4651 *usedp = 0; 4652 *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 4653 *usedp -= bmp->zbm_referenced_bytes_refd; 4654 4655 *compp = 0; 4656 *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 4657 *compp -= bmp->zbm_compressed_bytes_refd; 4658 4659 *uncompp = 0; 4660 *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 4661 *uncompp -= bmp->zbm_uncompressed_bytes_refd; 4662 4663 dsl_dataset_t *snap = new; 4664 4665 while (dsl_dataset_phys(snap)->ds_prev_snap_txg > 4666 bmp->zbm_creation_txg) { 4667 uint64_t used, comp, uncomp; 4668 4669 dsl_deadlist_space_range(&snap->ds_deadlist, 4670 0, bmp->zbm_creation_txg, 4671 &used, &comp, &uncomp); 4672 *usedp += used; 4673 *compp += comp; 4674 *uncompp += uncomp; 4675 4676 uint64_t snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 4677 if (snap != new) 4678 dsl_dataset_rele(snap, FTAG); 4679 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 4680 if (err != 0) 4681 break; 4682 } 4683 4684 /* 4685 * We might not have the FBN if we are calculating written from 4686 * a snapshot (because we didn't know the correct "next" snapshot 4687 * until now). 4688 */ 4689 if (bmp->zbm_flags & ZBM_FLAG_HAS_FBN) { 4690 *usedp += bmp->zbm_referenced_freed_before_next_snap; 4691 *compp += bmp->zbm_compressed_freed_before_next_snap; 4692 *uncompp += bmp->zbm_uncompressed_freed_before_next_snap; 4693 } else { 4694 ASSERT3U(dsl_dataset_phys(snap)->ds_prev_snap_txg, ==, 4695 bmp->zbm_creation_txg); 4696 uint64_t used, comp, uncomp; 4697 dsl_deadlist_space(&snap->ds_deadlist, &used, &comp, &uncomp); 4698 *usedp += used; 4699 *compp += comp; 4700 *uncompp += uncomp; 4701 } 4702 if (snap != new) 4703 dsl_dataset_rele(snap, FTAG); 4704 return (err); 4705 } 4706 4707 /* 4708 * Return (in *usedp) the amount of space written in new that was not 4709 * present at the time the bookmark corresponds to. New may be a 4710 * snapshot or the head. Old must be a bookmark before new, in 4711 * new's filesystem (or its origin) -- caller verifies this. 4712 */ 4713 int 4714 dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t *bmp, 4715 dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 4716 { 4717 if (!(bmp->zbm_flags & ZBM_FLAG_HAS_FBN)) 4718 return (SET_ERROR(ENOTSUP)); 4719 return (dsl_dataset_space_written_impl(bmp, new, 4720 usedp, compp, uncompp)); 4721 } 4722 4723 /* 4724 * Return (in *usedp) the amount of space written in new that is not 4725 * present in oldsnap. New may be a snapshot or the head. Old must be 4726 * a snapshot before new, in new's filesystem (or its origin). If not then 4727 * fail and return EINVAL. 4728 */ 4729 int 4730 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 4731 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 4732 { 4733 if (!dsl_dataset_is_before(new, oldsnap, 0)) 4734 return (SET_ERROR(EINVAL)); 4735 4736 zfs_bookmark_phys_t zbm = { 0 }; 4737 dsl_dataset_phys_t *dsp = dsl_dataset_phys(oldsnap); 4738 zbm.zbm_guid = dsp->ds_guid; 4739 zbm.zbm_creation_txg = dsp->ds_creation_txg; 4740 zbm.zbm_creation_time = dsp->ds_creation_time; 4741 zbm.zbm_referenced_bytes_refd = dsp->ds_referenced_bytes; 4742 zbm.zbm_compressed_bytes_refd = dsp->ds_compressed_bytes; 4743 zbm.zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes; 4744 4745 /* 4746 * If oldsnap is the origin (or origin's origin, ...) of new, 4747 * we can't easily calculate the effective FBN. Therefore, 4748 * we do not set ZBM_FLAG_HAS_FBN, so that the _impl will calculate 4749 * it relative to the correct "next": the next snapshot towards "new", 4750 * rather than the next snapshot in oldsnap's dsl_dir. 4751 */ 4752 return (dsl_dataset_space_written_impl(&zbm, new, 4753 usedp, compp, uncompp)); 4754 } 4755 4756 /* 4757 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 4758 * lastsnap, and all snapshots in between are deleted. 4759 * 4760 * blocks that would be freed [---------------------------] 4761 * snapshots ---O-------O--------O-------O--------O 4762 * firstsnap lastsnap 4763 * 4764 * This is the set of blocks that were born after the snap before firstsnap, 4765 * (birth > firstsnap->prev_snap_txg) and died before the snap after the 4766 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 4767 * We calculate this by iterating over the relevant deadlists (from the snap 4768 * after lastsnap, backward to the snap after firstsnap), summing up the 4769 * space on the deadlist that was born after the snap before firstsnap. 4770 */ 4771 int 4772 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 4773 dsl_dataset_t *lastsnap, 4774 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 4775 { 4776 int err = 0; 4777 uint64_t snapobj; 4778 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 4779 4780 ASSERT(firstsnap->ds_is_snapshot); 4781 ASSERT(lastsnap->ds_is_snapshot); 4782 4783 /* 4784 * Check that the snapshots are in the same dsl_dir, and firstsnap 4785 * is before lastsnap. 4786 */ 4787 if (firstsnap->ds_dir != lastsnap->ds_dir || 4788 dsl_dataset_phys(firstsnap)->ds_creation_txg > 4789 dsl_dataset_phys(lastsnap)->ds_creation_txg) 4790 return (SET_ERROR(EINVAL)); 4791 4792 *usedp = *compp = *uncompp = 0; 4793 4794 snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 4795 while (snapobj != firstsnap->ds_object) { 4796 dsl_dataset_t *ds; 4797 uint64_t used, comp, uncomp; 4798 4799 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 4800 if (err != 0) 4801 break; 4802 4803 dsl_deadlist_space_range(&ds->ds_deadlist, 4804 dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 4805 &used, &comp, &uncomp); 4806 *usedp += used; 4807 *compp += comp; 4808 *uncompp += uncomp; 4809 4810 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 4811 ASSERT3U(snapobj, !=, 0); 4812 dsl_dataset_rele(ds, FTAG); 4813 } 4814 return (err); 4815 } 4816 4817 /* 4818 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 4819 * For example, they could both be snapshots of the same filesystem, and 4820 * 'earlier' is before 'later'. Or 'earlier' could be the origin of 4821 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 4822 * filesystem. Or 'earlier' could be the origin's origin. 4823 * 4824 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 4825 */ 4826 boolean_t 4827 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 4828 uint64_t earlier_txg) 4829 { 4830 dsl_pool_t *dp = later->ds_dir->dd_pool; 4831 int error; 4832 boolean_t ret; 4833 4834 ASSERT(dsl_pool_config_held(dp)); 4835 ASSERT(earlier->ds_is_snapshot || earlier_txg != 0); 4836 4837 if (earlier_txg == 0) 4838 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 4839 4840 if (later->ds_is_snapshot && 4841 earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 4842 return (B_FALSE); 4843 4844 if (later->ds_dir == earlier->ds_dir) 4845 return (B_TRUE); 4846 4847 /* 4848 * We check dd_origin_obj explicitly here rather than using 4849 * dsl_dir_is_clone() so that we will return TRUE if "earlier" 4850 * is $ORIGIN@$ORIGIN. dsl_dataset_space_written() depends on 4851 * this behavior. 4852 */ 4853 if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == 0) 4854 return (B_FALSE); 4855 4856 dsl_dataset_t *origin; 4857 error = dsl_dataset_hold_obj(dp, 4858 dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 4859 if (error != 0) 4860 return (B_FALSE); 4861 if (dsl_dataset_phys(origin)->ds_creation_txg == earlier_txg && 4862 origin->ds_dir == earlier->ds_dir) { 4863 dsl_dataset_rele(origin, FTAG); 4864 return (B_TRUE); 4865 } 4866 ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 4867 dsl_dataset_rele(origin, FTAG); 4868 return (ret); 4869 } 4870 4871 void 4872 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 4873 { 4874 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 4875 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 4876 } 4877 4878 boolean_t 4879 dsl_dataset_is_zapified(dsl_dataset_t *ds) 4880 { 4881 dmu_object_info_t doi; 4882 4883 dmu_object_info_from_db(ds->ds_dbuf, &doi); 4884 return (doi.doi_type == DMU_OTN_ZAP_METADATA); 4885 } 4886 4887 boolean_t 4888 dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds) 4889 { 4890 return (dsl_dataset_is_zapified(ds) && 4891 zap_contains(ds->ds_dir->dd_pool->dp_meta_objset, 4892 ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0); 4893 } 4894 4895 uint64_t 4896 dsl_dataset_get_remap_deadlist_object(dsl_dataset_t *ds) 4897 { 4898 uint64_t remap_deadlist_obj; 4899 int err; 4900 4901 if (!dsl_dataset_is_zapified(ds)) 4902 return (0); 4903 4904 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object, 4905 DS_FIELD_REMAP_DEADLIST, sizeof (remap_deadlist_obj), 1, 4906 &remap_deadlist_obj); 4907 4908 if (err != 0) { 4909 VERIFY3S(err, ==, ENOENT); 4910 return (0); 4911 } 4912 4913 ASSERT(remap_deadlist_obj != 0); 4914 return (remap_deadlist_obj); 4915 } 4916 4917 boolean_t 4918 dsl_dataset_remap_deadlist_exists(dsl_dataset_t *ds) 4919 { 4920 EQUIV(dsl_deadlist_is_open(&ds->ds_remap_deadlist), 4921 dsl_dataset_get_remap_deadlist_object(ds) != 0); 4922 return (dsl_deadlist_is_open(&ds->ds_remap_deadlist)); 4923 } 4924 4925 static void 4926 dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, uint64_t obj, 4927 dmu_tx_t *tx) 4928 { 4929 ASSERT(obj != 0); 4930 dsl_dataset_zapify(ds, tx); 4931 VERIFY0(zap_add(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object, 4932 DS_FIELD_REMAP_DEADLIST, sizeof (obj), 1, &obj, tx)); 4933 } 4934 4935 static void 4936 dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, dmu_tx_t *tx) 4937 { 4938 VERIFY0(zap_remove(ds->ds_dir->dd_pool->dp_meta_objset, 4939 ds->ds_object, DS_FIELD_REMAP_DEADLIST, tx)); 4940 } 4941 4942 void 4943 dsl_dataset_destroy_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx) 4944 { 4945 uint64_t remap_deadlist_object; 4946 spa_t *spa = ds->ds_dir->dd_pool->dp_spa; 4947 4948 ASSERT(dmu_tx_is_syncing(tx)); 4949 ASSERT(dsl_dataset_remap_deadlist_exists(ds)); 4950 4951 remap_deadlist_object = ds->ds_remap_deadlist.dl_object; 4952 dsl_deadlist_close(&ds->ds_remap_deadlist); 4953 dsl_deadlist_free(spa_meta_objset(spa), remap_deadlist_object, tx); 4954 dsl_dataset_unset_remap_deadlist_object(ds, tx); 4955 spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 4956 } 4957 4958 void 4959 dsl_dataset_create_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx) 4960 { 4961 uint64_t remap_deadlist_obj; 4962 spa_t *spa = ds->ds_dir->dd_pool->dp_spa; 4963 4964 ASSERT(dmu_tx_is_syncing(tx)); 4965 ASSERT(MUTEX_HELD(&ds->ds_remap_deadlist_lock)); 4966 /* 4967 * Currently we only create remap deadlists when there are indirect 4968 * vdevs with referenced mappings. 4969 */ 4970 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 4971 4972 remap_deadlist_obj = dsl_deadlist_clone( 4973 &ds->ds_deadlist, UINT64_MAX, 4974 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 4975 dsl_dataset_set_remap_deadlist_object(ds, 4976 remap_deadlist_obj, tx); 4977 VERIFY0(dsl_deadlist_open(&ds->ds_remap_deadlist, spa_meta_objset(spa), 4978 remap_deadlist_obj)); 4979 spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 4980 } 4981 4982 void 4983 dsl_dataset_activate_redaction(dsl_dataset_t *ds, uint64_t *redact_snaps, 4984 uint64_t num_redact_snaps, dmu_tx_t *tx) 4985 { 4986 uint64_t dsobj = ds->ds_object; 4987 struct feature_type_uint64_array_arg *ftuaa = 4988 kmem_zalloc(sizeof (*ftuaa), KM_SLEEP); 4989 ftuaa->length = (int64_t)num_redact_snaps; 4990 if (num_redact_snaps > 0) { 4991 ftuaa->array = kmem_alloc(num_redact_snaps * sizeof (uint64_t), 4992 KM_SLEEP); 4993 memcpy(ftuaa->array, redact_snaps, num_redact_snaps * 4994 sizeof (uint64_t)); 4995 } 4996 dsl_dataset_activate_feature(dsobj, SPA_FEATURE_REDACTED_DATASETS, 4997 ftuaa, tx); 4998 ds->ds_feature[SPA_FEATURE_REDACTED_DATASETS] = ftuaa; 4999 } 5000 5001 /* 5002 * Find and return (in *oldest_dsobj) the oldest snapshot of the dsobj 5003 * dataset whose birth time is >= min_txg. 5004 */ 5005 int 5006 dsl_dataset_oldest_snapshot(spa_t *spa, uint64_t head_ds, uint64_t min_txg, 5007 uint64_t *oldest_dsobj) 5008 { 5009 dsl_dataset_t *ds; 5010 dsl_pool_t *dp = spa->spa_dsl_pool; 5011 5012 int error = dsl_dataset_hold_obj(dp, head_ds, FTAG, &ds); 5013 if (error != 0) 5014 return (error); 5015 5016 uint64_t prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 5017 uint64_t prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 5018 5019 while (prev_obj != 0 && min_txg < prev_obj_txg) { 5020 dsl_dataset_rele(ds, FTAG); 5021 if ((error = dsl_dataset_hold_obj(dp, prev_obj, 5022 FTAG, &ds)) != 0) 5023 return (error); 5024 prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 5025 prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 5026 } 5027 *oldest_dsobj = ds->ds_object; 5028 dsl_dataset_rele(ds, FTAG); 5029 return (0); 5030 } 5031 5032 ZFS_MODULE_PARAM(zfs, zfs_, max_recordsize, UINT, ZMOD_RW, 5033 "Max allowed record size"); 5034 5035 ZFS_MODULE_PARAM(zfs, zfs_, allow_redacted_dataset_mount, INT, ZMOD_RW, 5036 "Allow mounting of redacted datasets"); 5037 5038 ZFS_MODULE_PARAM(zfs, zfs_, snapshot_history_enabled, INT, ZMOD_RW, 5039 "Include snapshot events in pool history/events"); 5040 5041 EXPORT_SYMBOL(dsl_dataset_hold); 5042 EXPORT_SYMBOL(dsl_dataset_hold_flags); 5043 EXPORT_SYMBOL(dsl_dataset_hold_obj); 5044 EXPORT_SYMBOL(dsl_dataset_hold_obj_flags); 5045 EXPORT_SYMBOL(dsl_dataset_own); 5046 EXPORT_SYMBOL(dsl_dataset_own_obj); 5047 EXPORT_SYMBOL(dsl_dataset_name); 5048 EXPORT_SYMBOL(dsl_dataset_rele); 5049 EXPORT_SYMBOL(dsl_dataset_rele_flags); 5050 EXPORT_SYMBOL(dsl_dataset_disown); 5051 EXPORT_SYMBOL(dsl_dataset_tryown); 5052 EXPORT_SYMBOL(dsl_dataset_create_sync); 5053 EXPORT_SYMBOL(dsl_dataset_create_sync_dd); 5054 EXPORT_SYMBOL(dsl_dataset_snapshot_check); 5055 EXPORT_SYMBOL(dsl_dataset_snapshot_sync); 5056 EXPORT_SYMBOL(dsl_dataset_promote); 5057 EXPORT_SYMBOL(dsl_dataset_user_hold); 5058 EXPORT_SYMBOL(dsl_dataset_user_release); 5059 EXPORT_SYMBOL(dsl_dataset_get_holds); 5060 EXPORT_SYMBOL(dsl_dataset_get_blkptr); 5061 EXPORT_SYMBOL(dsl_dataset_get_spa); 5062 EXPORT_SYMBOL(dsl_dataset_modified_since_snap); 5063 EXPORT_SYMBOL(dsl_dataset_space_written); 5064 EXPORT_SYMBOL(dsl_dataset_space_wouldfree); 5065 EXPORT_SYMBOL(dsl_dataset_sync); 5066 EXPORT_SYMBOL(dsl_dataset_block_born); 5067 EXPORT_SYMBOL(dsl_dataset_block_kill); 5068 EXPORT_SYMBOL(dsl_dataset_dirty); 5069 EXPORT_SYMBOL(dsl_dataset_stats); 5070 EXPORT_SYMBOL(dsl_dataset_fast_stat); 5071 EXPORT_SYMBOL(dsl_dataset_space); 5072 EXPORT_SYMBOL(dsl_dataset_fsid_guid); 5073 EXPORT_SYMBOL(dsl_dsobj_to_dsname); 5074 EXPORT_SYMBOL(dsl_dataset_check_quota); 5075 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl); 5076 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl); 5077