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