4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
17 * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
18 * Copyright 2017 Nexenta Systems, Inc.
19 * Copyright 2019, 2020 by Christian Schwarz. All rights reserved.
22 #include <sys/zfs_context.h>
23 #include <sys/dsl_dataset.h>
24 #include <sys/dsl_dir.h>
25 #include <sys/dsl_prop.h>
26 #include <sys/dsl_synctask.h>
27 #include <sys/dsl_destroy.h>
28 #include <sys/dmu_impl.h>
29 #include <sys/dmu_tx.h>
32 #include <sys/zfeature.h>
34 #include <sys/dsl_bookmark.h>
35 #include <zfs_namecheck.h>
36 #include <sys/dmu_send.h>
39 dsl_bookmark_hold_ds(dsl_pool_t
*dp
, const char *fullname
,
40 dsl_dataset_t
**dsp
, void *tag
, char **shortnamep
)
42 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
45 if (strlen(fullname
) >= ZFS_MAX_DATASET_NAME_LEN
)
46 return (SET_ERROR(ENAMETOOLONG
));
47 hashp
= strchr(fullname
, '#');
49 return (SET_ERROR(EINVAL
));
51 *shortnamep
= hashp
+ 1;
52 if (zfs_component_namecheck(*shortnamep
, NULL
, NULL
))
53 return (SET_ERROR(EINVAL
));
54 (void) strlcpy(buf
, fullname
, hashp
- fullname
+ 1);
55 return (dsl_dataset_hold(dp
, buf
, tag
, dsp
));
59 * When reading BOOKMARK_V1 bookmarks, the BOOKMARK_V2 fields are guaranteed
62 * Returns ESRCH if bookmark is not found.
63 * Note, we need to use the ZAP rather than the AVL to look up bookmarks
64 * by name, because only the ZAP honors the casesensitivity setting.
67 dsl_bookmark_lookup_impl(dsl_dataset_t
*ds
, const char *shortname
,
68 zfs_bookmark_phys_t
*bmark_phys
)
70 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
71 uint64_t bmark_zapobj
= ds
->ds_bookmarks_obj
;
75 if (bmark_zapobj
== 0)
76 return (SET_ERROR(ESRCH
));
78 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
82 * Zero out the bookmark in case the one stored on disk
83 * is in an older, shorter format.
85 bzero(bmark_phys
, sizeof (*bmark_phys
));
87 err
= zap_lookup_norm(mos
, bmark_zapobj
, shortname
, sizeof (uint64_t),
88 sizeof (*bmark_phys
) / sizeof (uint64_t), bmark_phys
, mt
, NULL
, 0,
91 return (err
== ENOENT
? SET_ERROR(ESRCH
) : err
);
95 * If later_ds is non-NULL, this will return EXDEV if the specified bookmark
96 * does not represents an earlier point in later_ds's timeline. However,
97 * bmp will still be filled in if we return EXDEV.
99 * Returns ENOENT if the dataset containing the bookmark does not exist.
100 * Returns ESRCH if the dataset exists but the bookmark was not found in it.
103 dsl_bookmark_lookup(dsl_pool_t
*dp
, const char *fullname
,
104 dsl_dataset_t
*later_ds
, zfs_bookmark_phys_t
*bmp
)
110 error
= dsl_bookmark_hold_ds(dp
, fullname
, &ds
, FTAG
, &shortname
);
114 error
= dsl_bookmark_lookup_impl(ds
, shortname
, bmp
);
115 if (error
== 0 && later_ds
!= NULL
) {
116 if (!dsl_dataset_is_before(later_ds
, ds
, bmp
->zbm_creation_txg
))
117 error
= SET_ERROR(EXDEV
);
119 dsl_dataset_rele(ds
, FTAG
);
125 * - bmark is a full dataset path of a bookmark (bookmark_namecheck)
126 * - source is a full path of a snapshot or bookmark
127 * ({bookmark,snapshot}_namecheck)
129 * Returns 0 if valid, -1 otherwise.
132 dsl_bookmark_create_nvl_validate_pair(const char *bmark
, const char *source
)
134 if (bookmark_namecheck(bmark
, NULL
, NULL
) != 0)
137 int is_bmark
, is_snap
;
138 is_bmark
= bookmark_namecheck(source
, NULL
, NULL
) == 0;
139 is_snap
= snapshot_namecheck(source
, NULL
, NULL
) == 0;
140 if (!is_bmark
&& !is_snap
)
147 * Check that the given nvlist corresponds to the following schema:
148 * { newbookmark -> source, ... }
150 * - each pair passes dsl_bookmark_create_nvl_validate_pair
151 * - all newbookmarks are in the same pool
152 * - all newbookmarks have unique names
154 * Note that this function is only validates above schema. Callers must ensure
155 * that the bookmarks can be created, e.g. that sources exist.
157 * Returns 0 if the nvlist adheres to above schema.
158 * Returns -1 if it doesn't.
161 dsl_bookmark_create_nvl_validate(nvlist_t
*bmarks
)
167 for (nvpair_t
*pair
= nvlist_next_nvpair(bmarks
, NULL
);
168 pair
!= NULL
; pair
= nvlist_next_nvpair(bmarks
, pair
)) {
170 char *bmark
= nvpair_name(pair
);
173 /* list structure: values must be snapshots XOR bookmarks */
174 if (nvpair_value_string(pair
, &source
) != 0)
176 if (dsl_bookmark_create_nvl_validate_pair(bmark
, source
) != 0)
179 /* same pool check */
181 char *cp
= strpbrk(bmark
, "/#");
185 first_len
= cp
- bmark
;
187 if (strncmp(first
, bmark
, first_len
) != 0)
189 switch (*(bmark
+ first_len
)) {
190 case '/': /* fallthrough */
197 /* unique newbookmark names; todo: O(n^2) */
198 for (nvpair_t
*pair2
= nvlist_next_nvpair(bmarks
, pair
);
199 pair2
!= NULL
; pair2
= nvlist_next_nvpair(bmarks
, pair2
)) {
200 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
209 * expects that newbm and source have been validated using
210 * dsl_bookmark_create_nvl_validate_pair
213 dsl_bookmark_create_check_impl(dsl_pool_t
*dp
,
214 const char *newbm
, const char *source
)
216 ASSERT0(dsl_bookmark_create_nvl_validate_pair(newbm
, source
));
217 /* defer source namecheck until we know it's a snapshot or bookmark */
220 dsl_dataset_t
*newbm_ds
;
222 zfs_bookmark_phys_t bmark_phys
;
224 error
= dsl_bookmark_hold_ds(dp
, newbm
, &newbm_ds
, FTAG
, &newbm_short
);
228 /* Verify that the new bookmark does not already exist */
229 error
= dsl_bookmark_lookup_impl(newbm_ds
, newbm_short
, &bmark_phys
);
232 /* happy path: new bmark doesn't exist, proceed after switch */
236 error
= SET_ERROR(EEXIST
);
239 /* dsl_bookmark_lookup_impl already did SET_ERROR */
243 /* error is retval of the following if-cascade */
244 if (strchr(source
, '@') != NULL
) {
245 dsl_dataset_t
*source_snap_ds
;
246 ASSERT3S(snapshot_namecheck(source
, NULL
, NULL
), ==, 0);
247 error
= dsl_dataset_hold(dp
, source
, FTAG
, &source_snap_ds
);
249 VERIFY(source_snap_ds
->ds_is_snapshot
);
251 * Verify that source snapshot is an earlier point in
252 * newbm_ds's timeline (source may be newbm_ds's origin)
254 if (!dsl_dataset_is_before(newbm_ds
, source_snap_ds
, 0))
256 ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
);
257 dsl_dataset_rele(source_snap_ds
, FTAG
);
259 } else if (strchr(source
, '#') != NULL
) {
260 zfs_bookmark_phys_t source_phys
;
261 ASSERT3S(bookmark_namecheck(source
, NULL
, NULL
), ==, 0);
263 * Source must exists and be an earlier point in newbm_ds's
264 * timeline (newbm_ds's origin may be a snap of source's ds)
266 error
= dsl_bookmark_lookup(dp
, source
, newbm_ds
, &source_phys
);
269 break; /* happy path */
271 error
= SET_ERROR(ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR
);
274 /* dsl_bookmark_lookup already did SET_ERROR */
279 * dsl_bookmark_create_nvl_validate validates that source is
280 * either snapshot or bookmark
282 panic("unreachable code: %s", source
);
286 dsl_dataset_rele(newbm_ds
, FTAG
);
291 dsl_bookmark_create_check(void *arg
, dmu_tx_t
*tx
)
293 dsl_bookmark_create_arg_t
*dbca
= arg
;
296 ASSERT3P(dbca
, !=, NULL
);
297 ASSERT3P(dbca
->dbca_bmarks
, !=, NULL
);
298 /* dbca->dbca_errors is allowed to be NULL */
300 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
302 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
))
303 return (SET_ERROR(ENOTSUP
));
305 if (dsl_bookmark_create_nvl_validate(dbca
->dbca_bmarks
) != 0)
306 rv
= schema_err
= SET_ERROR(EINVAL
);
308 for (nvpair_t
*pair
= nvlist_next_nvpair(dbca
->dbca_bmarks
, NULL
);
309 pair
!= NULL
; pair
= nvlist_next_nvpair(dbca
->dbca_bmarks
, pair
)) {
310 char *new = nvpair_name(pair
);
312 int error
= schema_err
;
314 char *source
= fnvpair_value_string(pair
);
315 error
= dsl_bookmark_create_check_impl(dp
, new, source
);
317 error
= SET_ERROR(error
);
322 if (dbca
->dbca_errors
!= NULL
)
323 fnvlist_add_int32(dbca
->dbca_errors
,
331 static dsl_bookmark_node_t
*
332 dsl_bookmark_node_alloc(char *shortname
)
334 dsl_bookmark_node_t
*dbn
= kmem_alloc(sizeof (*dbn
), KM_SLEEP
);
335 dbn
->dbn_name
= spa_strdup(shortname
);
336 dbn
->dbn_dirty
= B_FALSE
;
337 mutex_init(&dbn
->dbn_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
342 * Set the fields in the zfs_bookmark_phys_t based on the specified snapshot.
345 dsl_bookmark_set_phys(zfs_bookmark_phys_t
*zbm
, dsl_dataset_t
*snap
)
347 spa_t
*spa
= dsl_dataset_get_spa(snap
);
348 objset_t
*mos
= spa_get_dsl(spa
)->dp_meta_objset
;
349 dsl_dataset_phys_t
*dsp
= dsl_dataset_phys(snap
);
350 zbm
->zbm_guid
= dsp
->ds_guid
;
351 zbm
->zbm_creation_txg
= dsp
->ds_creation_txg
;
352 zbm
->zbm_creation_time
= dsp
->ds_creation_time
;
353 zbm
->zbm_redaction_obj
= 0;
356 * If the dataset is encrypted create a larger bookmark to
357 * accommodate the IVset guid. The IVset guid was added
358 * after the encryption feature to prevent a problem with
359 * raw sends. If we encounter an encrypted dataset without
360 * an IVset guid we fall back to a normal bookmark.
362 if (snap
->ds_dir
->dd_crypto_obj
!= 0 &&
363 spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
364 (void) zap_lookup(mos
, snap
->ds_object
,
365 DS_FIELD_IVSET_GUID
, sizeof (uint64_t), 1,
366 &zbm
->zbm_ivset_guid
);
369 if (spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_WRITTEN
)) {
370 zbm
->zbm_flags
= ZBM_FLAG_SNAPSHOT_EXISTS
| ZBM_FLAG_HAS_FBN
;
371 zbm
->zbm_referenced_bytes_refd
= dsp
->ds_referenced_bytes
;
372 zbm
->zbm_compressed_bytes_refd
= dsp
->ds_compressed_bytes
;
373 zbm
->zbm_uncompressed_bytes_refd
= dsp
->ds_uncompressed_bytes
;
375 dsl_dataset_t
*nextds
;
376 VERIFY0(dsl_dataset_hold_obj(snap
->ds_dir
->dd_pool
,
377 dsp
->ds_next_snap_obj
, FTAG
, &nextds
));
378 dsl_deadlist_space(&nextds
->ds_deadlist
,
379 &zbm
->zbm_referenced_freed_before_next_snap
,
380 &zbm
->zbm_compressed_freed_before_next_snap
,
381 &zbm
->zbm_uncompressed_freed_before_next_snap
);
382 dsl_dataset_rele(nextds
, FTAG
);
384 bzero(&zbm
->zbm_flags
,
385 sizeof (zfs_bookmark_phys_t
) -
386 offsetof(zfs_bookmark_phys_t
, zbm_flags
));
391 * Add dsl_bookmark_node_t `dbn` to the given dataset and increment appropriate
392 * SPA feature counters.
395 dsl_bookmark_node_add(dsl_dataset_t
*hds
, dsl_bookmark_node_t
*dbn
,
398 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
399 objset_t
*mos
= dp
->dp_meta_objset
;
401 if (hds
->ds_bookmarks_obj
== 0) {
402 hds
->ds_bookmarks_obj
= zap_create_norm(mos
,
403 U8_TEXTPREP_TOUPPER
, DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0,
405 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
407 dsl_dataset_zapify(hds
, tx
);
408 VERIFY0(zap_add(mos
, hds
->ds_object
,
409 DS_FIELD_BOOKMARK_NAMES
,
410 sizeof (hds
->ds_bookmarks_obj
), 1,
411 &hds
->ds_bookmarks_obj
, tx
));
414 avl_add(&hds
->ds_bookmarks
, dbn
);
417 * To maintain backwards compatibility with software that doesn't
418 * understand SPA_FEATURE_BOOKMARK_V2, we need to use the smallest
419 * possible bookmark size.
421 uint64_t bookmark_phys_size
= BOOKMARK_PHYS_SIZE_V1
;
422 if (spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_BOOKMARK_V2
) &&
423 (dbn
->dbn_phys
.zbm_ivset_guid
!= 0 || dbn
->dbn_phys
.zbm_flags
&
424 ZBM_FLAG_HAS_FBN
|| dbn
->dbn_phys
.zbm_redaction_obj
!= 0)) {
425 bookmark_phys_size
= BOOKMARK_PHYS_SIZE_V2
;
426 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_BOOKMARK_V2
, tx
);
429 __attribute__((unused
)) zfs_bookmark_phys_t zero_phys
= { 0 };
430 ASSERT0(bcmp(((char *)&dbn
->dbn_phys
) + bookmark_phys_size
,
431 &zero_phys
, sizeof (zfs_bookmark_phys_t
) - bookmark_phys_size
));
433 VERIFY0(zap_add(mos
, hds
->ds_bookmarks_obj
, dbn
->dbn_name
,
434 sizeof (uint64_t), bookmark_phys_size
/ sizeof (uint64_t),
435 &dbn
->dbn_phys
, tx
));
439 * If redaction_list is non-null, we create a redacted bookmark and redaction
440 * list, and store the object number of the redaction list in redact_obj.
443 dsl_bookmark_create_sync_impl_snap(const char *bookmark
, const char *snapshot
,
444 dmu_tx_t
*tx
, uint64_t num_redact_snaps
, uint64_t *redact_snaps
, void *tag
,
445 redaction_list_t
**redaction_list
)
447 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
448 objset_t
*mos
= dp
->dp_meta_objset
;
449 dsl_dataset_t
*snapds
, *bmark_fs
;
451 boolean_t bookmark_redacted
;
452 uint64_t *dsredactsnaps
;
455 VERIFY0(dsl_dataset_hold(dp
, snapshot
, FTAG
, &snapds
));
456 VERIFY0(dsl_bookmark_hold_ds(dp
, bookmark
, &bmark_fs
, FTAG
,
459 dsl_bookmark_node_t
*dbn
= dsl_bookmark_node_alloc(shortname
);
460 dsl_bookmark_set_phys(&dbn
->dbn_phys
, snapds
);
462 bookmark_redacted
= dsl_dataset_get_uint64_array_feature(snapds
,
463 SPA_FEATURE_REDACTED_DATASETS
, &dsnumsnaps
, &dsredactsnaps
);
464 if (redaction_list
!= NULL
|| bookmark_redacted
) {
465 redaction_list_t
*local_rl
;
466 if (bookmark_redacted
) {
467 redact_snaps
= dsredactsnaps
;
468 num_redact_snaps
= dsnumsnaps
;
470 dbn
->dbn_phys
.zbm_redaction_obj
= dmu_object_alloc(mos
,
471 DMU_OTN_UINT64_METADATA
, SPA_OLD_MAXBLOCKSIZE
,
472 DMU_OTN_UINT64_METADATA
, sizeof (redaction_list_phys_t
) +
473 num_redact_snaps
* sizeof (uint64_t), tx
);
474 spa_feature_incr(dp
->dp_spa
,
475 SPA_FEATURE_REDACTION_BOOKMARKS
, tx
);
477 VERIFY0(dsl_redaction_list_hold_obj(dp
,
478 dbn
->dbn_phys
.zbm_redaction_obj
, tag
, &local_rl
));
479 dsl_redaction_list_long_hold(dp
, local_rl
, tag
);
481 ASSERT3U((local_rl
)->rl_dbuf
->db_size
, >=,
482 sizeof (redaction_list_phys_t
) + num_redact_snaps
*
484 dmu_buf_will_dirty(local_rl
->rl_dbuf
, tx
);
485 bcopy(redact_snaps
, local_rl
->rl_phys
->rlp_snaps
,
486 sizeof (uint64_t) * num_redact_snaps
);
487 local_rl
->rl_phys
->rlp_num_snaps
= num_redact_snaps
;
488 if (bookmark_redacted
) {
489 ASSERT3P(redaction_list
, ==, NULL
);
490 local_rl
->rl_phys
->rlp_last_blkid
= UINT64_MAX
;
491 local_rl
->rl_phys
->rlp_last_object
= UINT64_MAX
;
492 dsl_redaction_list_long_rele(local_rl
, tag
);
493 dsl_redaction_list_rele(local_rl
, tag
);
495 *redaction_list
= local_rl
;
499 if (dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
) {
500 spa_feature_incr(dp
->dp_spa
,
501 SPA_FEATURE_BOOKMARK_WRITTEN
, tx
);
504 dsl_bookmark_node_add(bmark_fs
, dbn
, tx
);
506 spa_history_log_internal_ds(bmark_fs
, "bookmark", tx
,
507 "name=%s creation_txg=%llu target_snap=%llu redact_obj=%llu",
508 shortname
, (longlong_t
)dbn
->dbn_phys
.zbm_creation_txg
,
509 (longlong_t
)snapds
->ds_object
,
510 (longlong_t
)dbn
->dbn_phys
.zbm_redaction_obj
);
512 dsl_dataset_rele(bmark_fs
, FTAG
);
513 dsl_dataset_rele(snapds
, FTAG
);
518 dsl_bookmark_create_sync_impl_book(
519 const char *new_name
, const char *source_name
, dmu_tx_t
*tx
)
521 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
522 dsl_dataset_t
*bmark_fs_source
, *bmark_fs_new
;
523 char *source_shortname
, *new_shortname
;
524 zfs_bookmark_phys_t source_phys
;
526 VERIFY0(dsl_bookmark_hold_ds(dp
, source_name
, &bmark_fs_source
, FTAG
,
528 VERIFY0(dsl_bookmark_hold_ds(dp
, new_name
, &bmark_fs_new
, FTAG
,
532 * create a copy of the source bookmark by copying most of its members
534 * Caveat: bookmarking a redaction bookmark yields a normal bookmark
535 * -----------------------------------------------------------------
537 * - The zbm_redaction_obj would be referred to by both source and new
538 * bookmark, but would be destroyed once either source or new is
539 * destroyed, resulting in use-after-free of the referred object.
540 * - User expectation when issuing the `zfs bookmark` command is that
541 * a normal bookmark of the source is created
543 * Design Alternatives For Full Redaction Bookmark Copying:
544 * - reference-count the redaction object => would require on-disk
545 * format change for existing redaction objects
546 * - Copy the redaction object => cannot be done in syncing context
547 * because the redaction object might be too large
550 VERIFY0(dsl_bookmark_lookup_impl(bmark_fs_source
, source_shortname
,
552 dsl_bookmark_node_t
*new_dbn
= dsl_bookmark_node_alloc(new_shortname
);
554 memcpy(&new_dbn
->dbn_phys
, &source_phys
, sizeof (source_phys
));
555 new_dbn
->dbn_phys
.zbm_redaction_obj
= 0;
557 /* update feature counters */
558 if (new_dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
) {
559 spa_feature_incr(dp
->dp_spa
,
560 SPA_FEATURE_BOOKMARK_WRITTEN
, tx
);
562 /* no need for redaction bookmark counter; nulled zbm_redaction_obj */
563 /* dsl_bookmark_node_add bumps bookmarks and v2-bookmarks counter */
568 * Note that dsl_bookmark_lookup_impl guarantees that, if source is a
569 * v1 bookmark, the v2-only fields are zeroed.
570 * And dsl_bookmark_node_add writes back a v1-sized bookmark if
571 * v2 bookmarks are disabled and/or v2-only fields are zeroed.
572 * => bookmark copying works on pre-bookmark-v2 pools
574 dsl_bookmark_node_add(bmark_fs_new
, new_dbn
, tx
);
576 spa_history_log_internal_ds(bmark_fs_source
, "bookmark", tx
,
577 "name=%s creation_txg=%llu source_guid=%llu",
578 new_shortname
, (longlong_t
)new_dbn
->dbn_phys
.zbm_creation_txg
,
579 (longlong_t
)source_phys
.zbm_guid
);
581 dsl_dataset_rele(bmark_fs_source
, FTAG
);
582 dsl_dataset_rele(bmark_fs_new
, FTAG
);
586 dsl_bookmark_create_sync(void *arg
, dmu_tx_t
*tx
)
588 dsl_bookmark_create_arg_t
*dbca
= arg
;
590 ASSERT(spa_feature_is_enabled(dmu_tx_pool(tx
)->dp_spa
,
591 SPA_FEATURE_BOOKMARKS
));
593 for (nvpair_t
*pair
= nvlist_next_nvpair(dbca
->dbca_bmarks
, NULL
);
594 pair
!= NULL
; pair
= nvlist_next_nvpair(dbca
->dbca_bmarks
, pair
)) {
596 char *new = nvpair_name(pair
);
597 char *source
= fnvpair_value_string(pair
);
599 if (strchr(source
, '@') != NULL
) {
600 dsl_bookmark_create_sync_impl_snap(new, source
, tx
,
601 0, NULL
, NULL
, NULL
);
602 } else if (strchr(source
, '#') != NULL
) {
603 dsl_bookmark_create_sync_impl_book(new, source
, tx
);
605 panic("unreachable code");
612 * The bookmarks must all be in the same pool.
615 dsl_bookmark_create(nvlist_t
*bmarks
, nvlist_t
*errors
)
618 dsl_bookmark_create_arg_t dbca
;
620 pair
= nvlist_next_nvpair(bmarks
, NULL
);
624 dbca
.dbca_bmarks
= bmarks
;
625 dbca
.dbca_errors
= errors
;
627 return (dsl_sync_task(nvpair_name(pair
), dsl_bookmark_create_check
,
628 dsl_bookmark_create_sync
, &dbca
,
629 fnvlist_num_pairs(bmarks
), ZFS_SPACE_CHECK_NORMAL
));
633 dsl_bookmark_create_redacted_check(void *arg
, dmu_tx_t
*tx
)
635 dsl_bookmark_create_redacted_arg_t
*dbcra
= arg
;
636 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
639 if (!spa_feature_is_enabled(dp
->dp_spa
,
640 SPA_FEATURE_REDACTION_BOOKMARKS
))
641 return (SET_ERROR(ENOTSUP
));
643 * If the list of redact snaps will not fit in the bonus buffer with
644 * the furthest reached object and offset, fail.
646 if (dbcra
->dbcra_numsnaps
> (dmu_bonus_max() -
647 sizeof (redaction_list_phys_t
)) / sizeof (uint64_t))
648 return (SET_ERROR(E2BIG
));
650 if (dsl_bookmark_create_nvl_validate_pair(
651 dbcra
->dbcra_bmark
, dbcra
->dbcra_snap
) != 0)
652 return (SET_ERROR(EINVAL
));
654 rv
= dsl_bookmark_create_check_impl(dp
,
655 dbcra
->dbcra_bmark
, dbcra
->dbcra_snap
);
660 dsl_bookmark_create_redacted_sync(void *arg
, dmu_tx_t
*tx
)
662 dsl_bookmark_create_redacted_arg_t
*dbcra
= arg
;
663 dsl_bookmark_create_sync_impl_snap(dbcra
->dbcra_bmark
,
664 dbcra
->dbcra_snap
, tx
, dbcra
->dbcra_numsnaps
, dbcra
->dbcra_snaps
,
665 dbcra
->dbcra_tag
, dbcra
->dbcra_rl
);
669 dsl_bookmark_create_redacted(const char *bookmark
, const char *snapshot
,
670 uint64_t numsnaps
, uint64_t *snapguids
, void *tag
, redaction_list_t
**rl
)
672 dsl_bookmark_create_redacted_arg_t dbcra
;
674 dbcra
.dbcra_bmark
= bookmark
;
675 dbcra
.dbcra_snap
= snapshot
;
677 dbcra
.dbcra_numsnaps
= numsnaps
;
678 dbcra
.dbcra_snaps
= snapguids
;
679 dbcra
.dbcra_tag
= tag
;
681 return (dsl_sync_task(bookmark
, dsl_bookmark_create_redacted_check
,
682 dsl_bookmark_create_redacted_sync
, &dbcra
, 5,
683 ZFS_SPACE_CHECK_NORMAL
));
687 * Retrieve the list of properties given in the 'props' nvlist for a bookmark.
688 * If 'props' is NULL, retrieves all properties.
691 dsl_bookmark_fetch_props(dsl_pool_t
*dp
, zfs_bookmark_phys_t
*bmark_phys
,
692 nvlist_t
*props
, nvlist_t
*out_props
)
694 ASSERT3P(dp
, !=, NULL
);
695 ASSERT3P(bmark_phys
, !=, NULL
);
696 ASSERT3P(out_props
, !=, NULL
);
697 ASSERT(RRW_LOCK_HELD(&dp
->dp_config_rwlock
));
699 if (props
== NULL
|| nvlist_exists(props
,
700 zfs_prop_to_name(ZFS_PROP_GUID
))) {
701 dsl_prop_nvlist_add_uint64(out_props
,
702 ZFS_PROP_GUID
, bmark_phys
->zbm_guid
);
704 if (props
== NULL
|| nvlist_exists(props
,
705 zfs_prop_to_name(ZFS_PROP_CREATETXG
))) {
706 dsl_prop_nvlist_add_uint64(out_props
,
707 ZFS_PROP_CREATETXG
, bmark_phys
->zbm_creation_txg
);
709 if (props
== NULL
|| nvlist_exists(props
,
710 zfs_prop_to_name(ZFS_PROP_CREATION
))) {
711 dsl_prop_nvlist_add_uint64(out_props
,
712 ZFS_PROP_CREATION
, bmark_phys
->zbm_creation_time
);
714 if (props
== NULL
|| nvlist_exists(props
,
715 zfs_prop_to_name(ZFS_PROP_IVSET_GUID
))) {
716 dsl_prop_nvlist_add_uint64(out_props
,
717 ZFS_PROP_IVSET_GUID
, bmark_phys
->zbm_ivset_guid
);
719 if (bmark_phys
->zbm_flags
& ZBM_FLAG_HAS_FBN
) {
720 if (props
== NULL
|| nvlist_exists(props
,
721 zfs_prop_to_name(ZFS_PROP_REFERENCED
))) {
722 dsl_prop_nvlist_add_uint64(out_props
,
724 bmark_phys
->zbm_referenced_bytes_refd
);
726 if (props
== NULL
|| nvlist_exists(props
,
727 zfs_prop_to_name(ZFS_PROP_LOGICALREFERENCED
))) {
728 dsl_prop_nvlist_add_uint64(out_props
,
729 ZFS_PROP_LOGICALREFERENCED
,
730 bmark_phys
->zbm_uncompressed_bytes_refd
);
732 if (props
== NULL
|| nvlist_exists(props
,
733 zfs_prop_to_name(ZFS_PROP_REFRATIO
))) {
735 bmark_phys
->zbm_compressed_bytes_refd
== 0 ? 100 :
736 bmark_phys
->zbm_uncompressed_bytes_refd
* 100 /
737 bmark_phys
->zbm_compressed_bytes_refd
;
738 dsl_prop_nvlist_add_uint64(out_props
,
739 ZFS_PROP_REFRATIO
, ratio
);
743 if ((props
== NULL
|| nvlist_exists(props
, "redact_snaps") ||
744 nvlist_exists(props
, "redact_complete")) &&
745 bmark_phys
->zbm_redaction_obj
!= 0) {
746 redaction_list_t
*rl
;
747 int err
= dsl_redaction_list_hold_obj(dp
,
748 bmark_phys
->zbm_redaction_obj
, FTAG
, &rl
);
750 if (nvlist_exists(props
, "redact_snaps")) {
752 nvl
= fnvlist_alloc();
753 fnvlist_add_uint64_array(nvl
, ZPROP_VALUE
,
754 rl
->rl_phys
->rlp_snaps
,
755 rl
->rl_phys
->rlp_num_snaps
);
756 fnvlist_add_nvlist(out_props
, "redact_snaps",
760 if (nvlist_exists(props
, "redact_complete")) {
762 nvl
= fnvlist_alloc();
763 fnvlist_add_boolean_value(nvl
, ZPROP_VALUE
,
764 rl
->rl_phys
->rlp_last_blkid
== UINT64_MAX
&&
765 rl
->rl_phys
->rlp_last_object
== UINT64_MAX
);
766 fnvlist_add_nvlist(out_props
, "redact_complete",
770 dsl_redaction_list_rele(rl
, FTAG
);
776 dsl_get_bookmarks_impl(dsl_dataset_t
*ds
, nvlist_t
*props
, nvlist_t
*outnvl
)
778 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
780 ASSERT(dsl_pool_config_held(dp
));
782 if (dsl_dataset_is_snapshot(ds
))
783 return (SET_ERROR(EINVAL
));
785 for (dsl_bookmark_node_t
*dbn
= avl_first(&ds
->ds_bookmarks
);
786 dbn
!= NULL
; dbn
= AVL_NEXT(&ds
->ds_bookmarks
, dbn
)) {
787 nvlist_t
*out_props
= fnvlist_alloc();
789 dsl_bookmark_fetch_props(dp
, &dbn
->dbn_phys
, props
, out_props
);
791 fnvlist_add_nvlist(outnvl
, dbn
->dbn_name
, out_props
);
792 fnvlist_free(out_props
);
798 * Comparison func for ds_bookmarks AVL tree. We sort the bookmarks by
799 * their TXG, then by their FBN-ness. The "FBN-ness" component ensures
800 * that all bookmarks at the same TXG that HAS_FBN are adjacent, which
801 * dsl_bookmark_destroy_sync_impl() depends on. Note that there may be
802 * multiple bookmarks at the same TXG (with the same FBN-ness). In this
803 * case we differentiate them by an arbitrary metric (in this case,
807 dsl_bookmark_compare(const void *l
, const void *r
)
809 const dsl_bookmark_node_t
*ldbn
= l
;
810 const dsl_bookmark_node_t
*rdbn
= r
;
812 int64_t cmp
= TREE_CMP(ldbn
->dbn_phys
.zbm_creation_txg
,
813 rdbn
->dbn_phys
.zbm_creation_txg
);
816 cmp
= TREE_CMP((ldbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
),
817 (rdbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
));
820 cmp
= strcmp(ldbn
->dbn_name
, rdbn
->dbn_name
);
821 return (TREE_ISIGN(cmp
));
825 * Cache this (head) dataset's bookmarks in the ds_bookmarks AVL tree.
828 dsl_bookmark_init_ds(dsl_dataset_t
*ds
)
830 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
831 objset_t
*mos
= dp
->dp_meta_objset
;
833 ASSERT(!ds
->ds_is_snapshot
);
835 avl_create(&ds
->ds_bookmarks
, dsl_bookmark_compare
,
836 sizeof (dsl_bookmark_node_t
),
837 offsetof(dsl_bookmark_node_t
, dbn_node
));
839 if (!dsl_dataset_is_zapified(ds
))
842 int zaperr
= zap_lookup(mos
, ds
->ds_object
, DS_FIELD_BOOKMARK_NAMES
,
843 sizeof (ds
->ds_bookmarks_obj
), 1, &ds
->ds_bookmarks_obj
);
844 if (zaperr
== ENOENT
)
849 if (ds
->ds_bookmarks_obj
== 0)
854 zap_attribute_t attr
;
856 for (zap_cursor_init(&zc
, mos
, ds
->ds_bookmarks_obj
);
857 (err
= zap_cursor_retrieve(&zc
, &attr
)) == 0;
858 zap_cursor_advance(&zc
)) {
859 dsl_bookmark_node_t
*dbn
=
860 dsl_bookmark_node_alloc(attr
.za_name
);
862 err
= dsl_bookmark_lookup_impl(ds
,
863 dbn
->dbn_name
, &dbn
->dbn_phys
);
864 ASSERT3U(err
, !=, ENOENT
);
866 kmem_free(dbn
, sizeof (*dbn
));
869 avl_add(&ds
->ds_bookmarks
, dbn
);
871 zap_cursor_fini(&zc
);
878 dsl_bookmark_fini_ds(dsl_dataset_t
*ds
)
881 dsl_bookmark_node_t
*dbn
;
883 if (ds
->ds_is_snapshot
)
886 while ((dbn
= avl_destroy_nodes(&ds
->ds_bookmarks
, &cookie
)) != NULL
) {
887 spa_strfree(dbn
->dbn_name
);
888 mutex_destroy(&dbn
->dbn_lock
);
889 kmem_free(dbn
, sizeof (*dbn
));
891 avl_destroy(&ds
->ds_bookmarks
);
895 * Retrieve the bookmarks that exist in the specified dataset, and the
896 * requested properties of each bookmark.
898 * The "props" nvlist specifies which properties are requested.
899 * See lzc_get_bookmarks() for the list of valid properties.
902 dsl_get_bookmarks(const char *dsname
, nvlist_t
*props
, nvlist_t
*outnvl
)
908 err
= dsl_pool_hold(dsname
, FTAG
, &dp
);
911 err
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
913 dsl_pool_rele(dp
, FTAG
);
917 err
= dsl_get_bookmarks_impl(ds
, props
, outnvl
);
919 dsl_dataset_rele(ds
, FTAG
);
920 dsl_pool_rele(dp
, FTAG
);
925 * Retrieve all properties for a single bookmark in the given dataset.
928 dsl_get_bookmark_props(const char *dsname
, const char *bmname
, nvlist_t
*props
)
932 zfs_bookmark_phys_t bmark_phys
= { 0 };
935 err
= dsl_pool_hold(dsname
, FTAG
, &dp
);
938 err
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
940 dsl_pool_rele(dp
, FTAG
);
944 err
= dsl_bookmark_lookup_impl(ds
, bmname
, &bmark_phys
);
948 dsl_bookmark_fetch_props(dp
, &bmark_phys
, NULL
, props
);
950 dsl_dataset_rele(ds
, FTAG
);
951 dsl_pool_rele(dp
, FTAG
);
955 typedef struct dsl_bookmark_destroy_arg
{
956 nvlist_t
*dbda_bmarks
;
957 nvlist_t
*dbda_success
;
958 nvlist_t
*dbda_errors
;
959 } dsl_bookmark_destroy_arg_t
;
962 dsl_bookmark_destroy_sync_impl(dsl_dataset_t
*ds
, const char *name
,
965 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
966 uint64_t bmark_zapobj
= ds
->ds_bookmarks_obj
;
968 uint64_t int_size
, num_ints
;
970 * 'search' must be zeroed so that dbn_flags (which is used in
971 * dsl_bookmark_compare()) will be zeroed even if the on-disk
972 * (in ZAP) bookmark is shorter than offsetof(dbn_flags).
974 dsl_bookmark_node_t search
= { 0 };
975 char realname
[ZFS_MAX_DATASET_NAME_LEN
];
978 * Find the real name of this bookmark, which may be different
979 * from the given name if the dataset is case-insensitive. Then
980 * use the real name to find the node in the ds_bookmarks AVL tree.
983 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
986 VERIFY0(zap_length(mos
, bmark_zapobj
, name
, &int_size
, &num_ints
));
988 ASSERT3U(int_size
, ==, sizeof (uint64_t));
990 if (num_ints
* int_size
> BOOKMARK_PHYS_SIZE_V1
) {
991 spa_feature_decr(dmu_objset_spa(mos
),
992 SPA_FEATURE_BOOKMARK_V2
, tx
);
994 VERIFY0(zap_lookup_norm(mos
, bmark_zapobj
, name
, sizeof (uint64_t),
995 num_ints
, &search
.dbn_phys
, mt
, realname
, sizeof (realname
), NULL
));
997 search
.dbn_name
= realname
;
998 dsl_bookmark_node_t
*dbn
= avl_find(&ds
->ds_bookmarks
, &search
, NULL
);
1001 if (dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
) {
1003 * If this bookmark HAS_FBN, and it is before the most
1004 * recent snapshot, then its TXG is a key in the head's
1005 * deadlist (and all clones' heads' deadlists). If this is
1006 * the last thing keeping the key (i.e. there are no more
1007 * bookmarks with HAS_FBN at this TXG, and there is no
1008 * snapshot at this TXG), then remove the key.
1010 * Note that this algorithm depends on ds_bookmarks being
1011 * sorted such that all bookmarks at the same TXG with
1012 * HAS_FBN are adjacent (with no non-HAS_FBN bookmarks
1013 * at the same TXG in between them). If this were not
1014 * the case, we would need to examine *all* bookmarks
1015 * at this TXG, rather than just the adjacent ones.
1018 dsl_bookmark_node_t
*dbn_prev
=
1019 AVL_PREV(&ds
->ds_bookmarks
, dbn
);
1020 dsl_bookmark_node_t
*dbn_next
=
1021 AVL_NEXT(&ds
->ds_bookmarks
, dbn
);
1023 boolean_t more_bookmarks_at_this_txg
=
1024 (dbn_prev
!= NULL
&& dbn_prev
->dbn_phys
.zbm_creation_txg
==
1025 dbn
->dbn_phys
.zbm_creation_txg
&&
1026 (dbn_prev
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
)) ||
1027 (dbn_next
!= NULL
&& dbn_next
->dbn_phys
.zbm_creation_txg
==
1028 dbn
->dbn_phys
.zbm_creation_txg
&&
1029 (dbn_next
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
));
1031 if (!(dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_SNAPSHOT_EXISTS
) &&
1032 !more_bookmarks_at_this_txg
&&
1033 dbn
->dbn_phys
.zbm_creation_txg
<
1034 dsl_dataset_phys(ds
)->ds_prev_snap_txg
) {
1035 dsl_dir_remove_clones_key(ds
->ds_dir
,
1036 dbn
->dbn_phys
.zbm_creation_txg
, tx
);
1037 dsl_deadlist_remove_key(&ds
->ds_deadlist
,
1038 dbn
->dbn_phys
.zbm_creation_txg
, tx
);
1041 spa_feature_decr(dmu_objset_spa(mos
),
1042 SPA_FEATURE_BOOKMARK_WRITTEN
, tx
);
1045 if (dbn
->dbn_phys
.zbm_redaction_obj
!= 0) {
1046 VERIFY0(dmu_object_free(mos
,
1047 dbn
->dbn_phys
.zbm_redaction_obj
, tx
));
1048 spa_feature_decr(dmu_objset_spa(mos
),
1049 SPA_FEATURE_REDACTION_BOOKMARKS
, tx
);
1052 avl_remove(&ds
->ds_bookmarks
, dbn
);
1053 spa_strfree(dbn
->dbn_name
);
1054 mutex_destroy(&dbn
->dbn_lock
);
1055 kmem_free(dbn
, sizeof (*dbn
));
1057 VERIFY0(zap_remove_norm(mos
, bmark_zapobj
, name
, mt
, tx
));
1061 dsl_bookmark_destroy_check(void *arg
, dmu_tx_t
*tx
)
1063 dsl_bookmark_destroy_arg_t
*dbda
= arg
;
1064 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1067 ASSERT(nvlist_empty(dbda
->dbda_success
));
1068 ASSERT(nvlist_empty(dbda
->dbda_errors
));
1070 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
))
1073 for (nvpair_t
*pair
= nvlist_next_nvpair(dbda
->dbda_bmarks
, NULL
);
1074 pair
!= NULL
; pair
= nvlist_next_nvpair(dbda
->dbda_bmarks
, pair
)) {
1075 const char *fullname
= nvpair_name(pair
);
1077 zfs_bookmark_phys_t bm
;
1081 error
= dsl_bookmark_hold_ds(dp
, fullname
, &ds
,
1083 if (error
== ENOENT
) {
1084 /* ignore it; the bookmark is "already destroyed" */
1088 error
= dsl_bookmark_lookup_impl(ds
, shortname
, &bm
);
1089 dsl_dataset_rele(ds
, FTAG
);
1090 if (error
== ESRCH
) {
1092 * ignore it; the bookmark is
1093 * "already destroyed"
1097 if (error
== 0 && bm
.zbm_redaction_obj
!= 0) {
1098 redaction_list_t
*rl
= NULL
;
1099 error
= dsl_redaction_list_hold_obj(tx
->tx_pool
,
1100 bm
.zbm_redaction_obj
, FTAG
, &rl
);
1101 if (error
== ENOENT
) {
1103 } else if (error
== 0 &&
1104 dsl_redaction_list_long_held(rl
)) {
1105 error
= SET_ERROR(EBUSY
);
1108 dsl_redaction_list_rele(rl
, FTAG
);
1113 if (dmu_tx_is_syncing(tx
)) {
1114 fnvlist_add_boolean(dbda
->dbda_success
,
1118 fnvlist_add_int32(dbda
->dbda_errors
, fullname
, error
);
1126 dsl_bookmark_destroy_sync(void *arg
, dmu_tx_t
*tx
)
1128 dsl_bookmark_destroy_arg_t
*dbda
= arg
;
1129 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1130 objset_t
*mos
= dp
->dp_meta_objset
;
1132 for (nvpair_t
*pair
= nvlist_next_nvpair(dbda
->dbda_success
, NULL
);
1133 pair
!= NULL
; pair
= nvlist_next_nvpair(dbda
->dbda_success
, pair
)) {
1138 VERIFY0(dsl_bookmark_hold_ds(dp
, nvpair_name(pair
),
1139 &ds
, FTAG
, &shortname
));
1140 dsl_bookmark_destroy_sync_impl(ds
, shortname
, tx
);
1143 * If all of this dataset's bookmarks have been destroyed,
1144 * free the zap object and decrement the feature's use count.
1146 VERIFY0(zap_count(mos
, ds
->ds_bookmarks_obj
, &zap_cnt
));
1148 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1149 VERIFY0(zap_destroy(mos
, ds
->ds_bookmarks_obj
, tx
));
1150 ds
->ds_bookmarks_obj
= 0;
1151 spa_feature_decr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
1152 VERIFY0(zap_remove(mos
, ds
->ds_object
,
1153 DS_FIELD_BOOKMARK_NAMES
, tx
));
1156 spa_history_log_internal_ds(ds
, "remove bookmark", tx
,
1157 "name=%s", shortname
);
1159 dsl_dataset_rele(ds
, FTAG
);
1164 * The bookmarks must all be in the same pool.
1167 dsl_bookmark_destroy(nvlist_t
*bmarks
, nvlist_t
*errors
)
1170 dsl_bookmark_destroy_arg_t dbda
;
1171 nvpair_t
*pair
= nvlist_next_nvpair(bmarks
, NULL
);
1175 dbda
.dbda_bmarks
= bmarks
;
1176 dbda
.dbda_errors
= errors
;
1177 dbda
.dbda_success
= fnvlist_alloc();
1179 rv
= dsl_sync_task(nvpair_name(pair
), dsl_bookmark_destroy_check
,
1180 dsl_bookmark_destroy_sync
, &dbda
, fnvlist_num_pairs(bmarks
),
1181 ZFS_SPACE_CHECK_RESERVED
);
1182 fnvlist_free(dbda
.dbda_success
);
1186 /* Return B_TRUE if there are any long holds on this dataset. */
1188 dsl_redaction_list_long_held(redaction_list_t
*rl
)
1190 return (!zfs_refcount_is_zero(&rl
->rl_longholds
));
1194 dsl_redaction_list_long_hold(dsl_pool_t
*dp
, redaction_list_t
*rl
, void *tag
)
1196 ASSERT(dsl_pool_config_held(dp
));
1197 (void) zfs_refcount_add(&rl
->rl_longholds
, tag
);
1201 dsl_redaction_list_long_rele(redaction_list_t
*rl
, void *tag
)
1203 (void) zfs_refcount_remove(&rl
->rl_longholds
, tag
);
1208 redaction_list_evict_sync(void *rlu
)
1210 redaction_list_t
*rl
= rlu
;
1211 zfs_refcount_destroy(&rl
->rl_longholds
);
1213 kmem_free(rl
, sizeof (redaction_list_t
));
1217 dsl_redaction_list_rele(redaction_list_t
*rl
, void *tag
)
1219 dmu_buf_rele(rl
->rl_dbuf
, tag
);
1223 dsl_redaction_list_hold_obj(dsl_pool_t
*dp
, uint64_t rlobj
, void *tag
,
1224 redaction_list_t
**rlp
)
1226 objset_t
*mos
= dp
->dp_meta_objset
;
1228 redaction_list_t
*rl
;
1231 ASSERT(dsl_pool_config_held(dp
));
1233 err
= dmu_bonus_hold(mos
, rlobj
, tag
, &dbuf
);
1237 rl
= dmu_buf_get_user(dbuf
);
1239 redaction_list_t
*winner
= NULL
;
1241 rl
= kmem_zalloc(sizeof (redaction_list_t
), KM_SLEEP
);
1243 rl
->rl_object
= rlobj
;
1244 rl
->rl_phys
= dbuf
->db_data
;
1245 rl
->rl_mos
= dp
->dp_meta_objset
;
1246 zfs_refcount_create(&rl
->rl_longholds
);
1247 dmu_buf_init_user(&rl
->rl_dbu
, redaction_list_evict_sync
, NULL
,
1249 if ((winner
= dmu_buf_set_user_ie(dbuf
, &rl
->rl_dbu
)) != NULL
) {
1250 kmem_free(rl
, sizeof (*rl
));
1259 * Snapshot ds is being destroyed.
1261 * Adjust the "freed_before_next" of any bookmarks between this snap
1262 * and the previous snapshot, because their "next snapshot" is changing.
1264 * If there are any bookmarks with HAS_FBN at this snapshot, remove
1265 * their HAS_SNAP flag (note: there can be at most one snapshot of
1266 * each filesystem at a given txg), and return B_TRUE. In this case
1267 * the caller can not remove the key in the deadlist at this TXG, because
1268 * the HAS_FBN bookmarks require the key be there.
1270 * Returns B_FALSE if there are no bookmarks with HAS_FBN at this
1271 * snapshot's TXG. In this case the caller can remove the key in the
1272 * deadlist at this TXG.
1275 dsl_bookmark_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1277 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1279 dsl_dataset_t
*head
, *next
;
1280 VERIFY0(dsl_dataset_hold_obj(dp
,
1281 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &head
));
1282 VERIFY0(dsl_dataset_hold_obj(dp
,
1283 dsl_dataset_phys(ds
)->ds_next_snap_obj
, FTAG
, &next
));
1286 * Find the first bookmark that HAS_FBN at or after the
1287 * previous snapshot.
1289 dsl_bookmark_node_t search
= { 0 };
1291 search
.dbn_phys
.zbm_creation_txg
=
1292 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
1293 search
.dbn_phys
.zbm_flags
= ZBM_FLAG_HAS_FBN
;
1295 * The empty-string name can't be in the AVL, and it compares
1296 * before any entries with this TXG.
1298 search
.dbn_name
= "";
1299 VERIFY3P(avl_find(&head
->ds_bookmarks
, &search
, &idx
), ==, NULL
);
1300 dsl_bookmark_node_t
*dbn
=
1301 avl_nearest(&head
->ds_bookmarks
, idx
, AVL_AFTER
);
1304 * Iterate over all bookmarks that are at or after the previous
1305 * snapshot, and before this (being deleted) snapshot. Adjust
1306 * their FBN based on their new next snapshot.
1308 for (; dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
<
1309 dsl_dataset_phys(ds
)->ds_creation_txg
;
1310 dbn
= AVL_NEXT(&head
->ds_bookmarks
, dbn
)) {
1311 if (!(dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
))
1314 * Increase our FBN by the amount of space that was live
1315 * (referenced) at the time of this bookmark (i.e.
1316 * birth <= zbm_creation_txg), and killed between this
1317 * (being deleted) snapshot and the next snapshot (i.e.
1318 * on the next snapshot's deadlist). (Space killed before
1319 * this are already on our FBN.)
1321 uint64_t referenced
, compressed
, uncompressed
;
1322 dsl_deadlist_space_range(&next
->ds_deadlist
,
1323 0, dbn
->dbn_phys
.zbm_creation_txg
,
1324 &referenced
, &compressed
, &uncompressed
);
1325 dbn
->dbn_phys
.zbm_referenced_freed_before_next_snap
+=
1327 dbn
->dbn_phys
.zbm_compressed_freed_before_next_snap
+=
1329 dbn
->dbn_phys
.zbm_uncompressed_freed_before_next_snap
+=
1331 VERIFY0(zap_update(dp
->dp_meta_objset
, head
->ds_bookmarks_obj
,
1332 dbn
->dbn_name
, sizeof (uint64_t),
1333 sizeof (zfs_bookmark_phys_t
) / sizeof (uint64_t),
1334 &dbn
->dbn_phys
, tx
));
1336 dsl_dataset_rele(next
, FTAG
);
1339 * There may be several bookmarks at this txg (the TXG of the
1340 * snapshot being deleted). We need to clear the SNAPSHOT_EXISTS
1341 * flag on all of them, and return TRUE if there is at least 1
1342 * bookmark here with HAS_FBN (thus preventing the deadlist
1343 * key from being removed).
1345 boolean_t rv
= B_FALSE
;
1346 for (; dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
==
1347 dsl_dataset_phys(ds
)->ds_creation_txg
;
1348 dbn
= AVL_NEXT(&head
->ds_bookmarks
, dbn
)) {
1349 if (!(dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
)) {
1350 ASSERT(!(dbn
->dbn_phys
.zbm_flags
&
1351 ZBM_FLAG_SNAPSHOT_EXISTS
));
1354 ASSERT(dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_SNAPSHOT_EXISTS
);
1355 dbn
->dbn_phys
.zbm_flags
&= ~ZBM_FLAG_SNAPSHOT_EXISTS
;
1356 VERIFY0(zap_update(dp
->dp_meta_objset
, head
->ds_bookmarks_obj
,
1357 dbn
->dbn_name
, sizeof (uint64_t),
1358 sizeof (zfs_bookmark_phys_t
) / sizeof (uint64_t),
1359 &dbn
->dbn_phys
, tx
));
1362 dsl_dataset_rele(head
, FTAG
);
1367 * A snapshot is being created of this (head) dataset.
1369 * We don't keep keys in the deadlist for the most recent snapshot, or any
1370 * bookmarks at or after it, because there can't be any blocks on the
1371 * deadlist in this range. Now that the most recent snapshot is after
1372 * all bookmarks, we need to add these keys. Note that the caller always
1373 * adds a key at the previous snapshot, so we only add keys for bookmarks
1377 dsl_bookmark_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1379 uint64_t last_key_added
= UINT64_MAX
;
1380 for (dsl_bookmark_node_t
*dbn
= avl_last(&ds
->ds_bookmarks
);
1381 dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
>
1382 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
1383 dbn
= AVL_PREV(&ds
->ds_bookmarks
, dbn
)) {
1384 uint64_t creation_txg
= dbn
->dbn_phys
.zbm_creation_txg
;
1385 ASSERT3U(creation_txg
, <=, last_key_added
);
1387 * Note, there may be multiple bookmarks at this TXG,
1388 * and we only want to add the key for this TXG once.
1389 * The ds_bookmarks AVL is sorted by TXG, so we will visit
1390 * these bookmarks in sequence.
1392 if ((dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
) &&
1393 creation_txg
!= last_key_added
) {
1394 dsl_deadlist_add_key(&ds
->ds_deadlist
,
1396 last_key_added
= creation_txg
;
1402 * The next snapshot of the origin dataset has changed, due to
1403 * promote or clone swap. If there are any bookmarks at this dataset,
1404 * we need to update their zbm_*_freed_before_next_snap to reflect this.
1405 * The head dataset has the relevant bookmarks in ds_bookmarks.
1408 dsl_bookmark_next_changed(dsl_dataset_t
*head
, dsl_dataset_t
*origin
,
1411 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1414 * Find the first bookmark that HAS_FBN at the origin snapshot.
1416 dsl_bookmark_node_t search
= { 0 };
1418 search
.dbn_phys
.zbm_creation_txg
=
1419 dsl_dataset_phys(origin
)->ds_creation_txg
;
1420 search
.dbn_phys
.zbm_flags
= ZBM_FLAG_HAS_FBN
;
1422 * The empty-string name can't be in the AVL, and it compares
1423 * before any entries with this TXG.
1425 search
.dbn_name
= "";
1426 VERIFY3P(avl_find(&head
->ds_bookmarks
, &search
, &idx
), ==, NULL
);
1427 dsl_bookmark_node_t
*dbn
=
1428 avl_nearest(&head
->ds_bookmarks
, idx
, AVL_AFTER
);
1431 * Iterate over all bookmarks that are at the origin txg.
1432 * Adjust their FBN based on their new next snapshot.
1434 for (; dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
==
1435 dsl_dataset_phys(origin
)->ds_creation_txg
&&
1436 (dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
);
1437 dbn
= AVL_NEXT(&head
->ds_bookmarks
, dbn
)) {
1440 * Bookmark is at the origin, therefore its
1441 * "next dataset" is changing, so we need
1442 * to reset its FBN by recomputing it in
1443 * dsl_bookmark_set_phys().
1445 ASSERT3U(dbn
->dbn_phys
.zbm_guid
, ==,
1446 dsl_dataset_phys(origin
)->ds_guid
);
1447 ASSERT3U(dbn
->dbn_phys
.zbm_referenced_bytes_refd
, ==,
1448 dsl_dataset_phys(origin
)->ds_referenced_bytes
);
1449 ASSERT(dbn
->dbn_phys
.zbm_flags
&
1450 ZBM_FLAG_SNAPSHOT_EXISTS
);
1452 * Save and restore the zbm_redaction_obj, which
1453 * is zeroed by dsl_bookmark_set_phys().
1455 uint64_t redaction_obj
=
1456 dbn
->dbn_phys
.zbm_redaction_obj
;
1457 dsl_bookmark_set_phys(&dbn
->dbn_phys
, origin
);
1458 dbn
->dbn_phys
.zbm_redaction_obj
= redaction_obj
;
1460 VERIFY0(zap_update(dp
->dp_meta_objset
, head
->ds_bookmarks_obj
,
1461 dbn
->dbn_name
, sizeof (uint64_t),
1462 sizeof (zfs_bookmark_phys_t
) / sizeof (uint64_t),
1463 &dbn
->dbn_phys
, tx
));
1468 * This block is no longer referenced by this (head) dataset.
1470 * Adjust the FBN of any bookmarks that reference this block, whose "next"
1471 * is the head dataset.
1475 dsl_bookmark_block_killed(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1478 * Iterate over bookmarks whose "next" is the head dataset.
1480 for (dsl_bookmark_node_t
*dbn
= avl_last(&ds
->ds_bookmarks
);
1481 dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
>=
1482 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
1483 dbn
= AVL_PREV(&ds
->ds_bookmarks
, dbn
)) {
1485 * If the block was live (referenced) at the time of this
1486 * bookmark, add its space to the bookmark's FBN.
1488 if (bp
->blk_birth
<= dbn
->dbn_phys
.zbm_creation_txg
&&
1489 (dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
)) {
1490 mutex_enter(&dbn
->dbn_lock
);
1491 dbn
->dbn_phys
.zbm_referenced_freed_before_next_snap
+=
1492 bp_get_dsize_sync(dsl_dataset_get_spa(ds
), bp
);
1493 dbn
->dbn_phys
.zbm_compressed_freed_before_next_snap
+=
1495 dbn
->dbn_phys
.zbm_uncompressed_freed_before_next_snap
+=
1498 * Changing the ZAP object here would be too
1499 * expensive. Also, we may be called from the zio
1500 * interrupt thread, which can't block on i/o.
1501 * Therefore, we mark this bookmark as dirty and
1502 * modify the ZAP once per txg, in
1503 * dsl_bookmark_sync_done().
1505 dbn
->dbn_dirty
= B_TRUE
;
1506 mutex_exit(&dbn
->dbn_lock
);
1512 dsl_bookmark_sync_done(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1514 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1516 if (dsl_dataset_is_snapshot(ds
))
1520 * We only dirty bookmarks that are at or after the most recent
1521 * snapshot. We can't create snapshots between
1522 * dsl_bookmark_block_killed() and dsl_bookmark_sync_done(), so we
1523 * don't need to look at any bookmarks before ds_prev_snap_txg.
1525 for (dsl_bookmark_node_t
*dbn
= avl_last(&ds
->ds_bookmarks
);
1526 dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
>=
1527 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
1528 dbn
= AVL_PREV(&ds
->ds_bookmarks
, dbn
)) {
1529 if (dbn
->dbn_dirty
) {
1531 * We only dirty nodes with HAS_FBN, therefore
1532 * we can always use the current bookmark struct size.
1534 ASSERT(dbn
->dbn_phys
.zbm_flags
& ZBM_FLAG_HAS_FBN
);
1535 VERIFY0(zap_update(dp
->dp_meta_objset
,
1536 ds
->ds_bookmarks_obj
,
1537 dbn
->dbn_name
, sizeof (uint64_t),
1538 sizeof (zfs_bookmark_phys_t
) / sizeof (uint64_t),
1539 &dbn
->dbn_phys
, tx
));
1540 dbn
->dbn_dirty
= B_FALSE
;
1544 for (dsl_bookmark_node_t
*dbn
= avl_first(&ds
->ds_bookmarks
);
1545 dbn
!= NULL
; dbn
= AVL_NEXT(&ds
->ds_bookmarks
, dbn
)) {
1546 ASSERT(!dbn
->dbn_dirty
);
1552 * Return the TXG of the most recent bookmark (or 0 if there are no bookmarks).
1555 dsl_bookmark_latest_txg(dsl_dataset_t
*ds
)
1557 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1558 dsl_bookmark_node_t
*dbn
= avl_last(&ds
->ds_bookmarks
);
1561 return (dbn
->dbn_phys
.zbm_creation_txg
);
1565 * Compare the redact_block_phys_t to the bookmark. If the last block in the
1566 * redact_block_phys_t is before the bookmark, return -1. If the first block in
1567 * the redact_block_phys_t is after the bookmark, return 1. Otherwise, the
1568 * bookmark is inside the range of the redact_block_phys_t, and we return 0.
1571 redact_block_zb_compare(redact_block_phys_t
*first
,
1572 zbookmark_phys_t
*second
)
1575 * If the block_phys is for a previous object, or the last block in the
1576 * block_phys is strictly before the block in the bookmark, the
1577 * block_phys is earlier.
1579 if (first
->rbp_object
< second
->zb_object
||
1580 (first
->rbp_object
== second
->zb_object
&&
1581 first
->rbp_blkid
+ (redact_block_get_count(first
) - 1) <
1582 second
->zb_blkid
)) {
1587 * If the bookmark is for a previous object, or the block in the
1588 * bookmark is strictly before the first block in the block_phys, the
1589 * bookmark is earlier.
1591 if (first
->rbp_object
> second
->zb_object
||
1592 (first
->rbp_object
== second
->zb_object
&&
1593 first
->rbp_blkid
> second
->zb_blkid
)) {
1601 * Traverse the redaction list in the provided object, and call the callback for
1602 * each entry we find. Don't call the callback for any records before resume.
1605 dsl_redaction_list_traverse(redaction_list_t
*rl
, zbookmark_phys_t
*resume
,
1606 rl_traverse_callback_t cb
, void *arg
)
1608 objset_t
*mos
= rl
->rl_mos
;
1611 if (rl
->rl_phys
->rlp_last_object
!= UINT64_MAX
||
1612 rl
->rl_phys
->rlp_last_blkid
!= UINT64_MAX
) {
1614 * When we finish a send, we update the last object and offset
1615 * to UINT64_MAX. If a send fails partway through, the last
1616 * object and offset will have some other value, indicating how
1617 * far the send got. The redaction list must be complete before
1618 * it can be traversed, so return EINVAL if the last object and
1619 * blkid are not set to UINT64_MAX.
1621 return (SET_ERROR(EINVAL
));
1625 * This allows us to skip the binary search and resume checking logic
1626 * below, if we're not resuming a redacted send.
1628 if (ZB_IS_ZERO(resume
))
1632 * Binary search for the point to resume from.
1634 uint64_t maxidx
= rl
->rl_phys
->rlp_num_entries
- 1;
1635 uint64_t minidx
= 0;
1636 while (resume
!= NULL
&& maxidx
> minidx
) {
1637 redact_block_phys_t rbp
= { 0 };
1638 ASSERT3U(maxidx
, >, minidx
);
1639 uint64_t mididx
= minidx
+ ((maxidx
- minidx
) / 2);
1640 err
= dmu_read(mos
, rl
->rl_object
, mididx
* sizeof (rbp
),
1641 sizeof (rbp
), &rbp
, DMU_READ_NO_PREFETCH
);
1645 int cmp
= redact_block_zb_compare(&rbp
, resume
);
1650 } else if (cmp
> 0) {
1652 (mididx
== minidx
? minidx
: mididx
- 1);
1654 minidx
= mididx
+ 1;
1658 unsigned int bufsize
= SPA_OLD_MAXBLOCKSIZE
;
1659 redact_block_phys_t
*buf
= zio_data_buf_alloc(bufsize
);
1661 unsigned int entries_per_buf
= bufsize
/ sizeof (redact_block_phys_t
);
1662 uint64_t start_block
= minidx
/ entries_per_buf
;
1663 err
= dmu_read(mos
, rl
->rl_object
, start_block
* bufsize
, bufsize
, buf
,
1666 for (uint64_t curidx
= minidx
;
1667 err
== 0 && curidx
< rl
->rl_phys
->rlp_num_entries
;
1670 * We read in the redaction list one block at a time. Once we
1671 * finish with all the entries in a given block, we read in a
1672 * new one. The predictive prefetcher will take care of any
1673 * prefetching, and this code shouldn't be the bottleneck, so we
1674 * don't need to do manual prefetching.
1676 if (curidx
% entries_per_buf
== 0) {
1677 err
= dmu_read(mos
, rl
->rl_object
, curidx
*
1678 sizeof (*buf
), bufsize
, buf
,
1683 redact_block_phys_t
*rb
= &buf
[curidx
% entries_per_buf
];
1685 * If resume is non-null, we should either not send the data, or
1686 * null out resume so we don't have to keep doing these
1689 if (resume
!= NULL
) {
1691 * It is possible that after the binary search we got
1692 * a record before the resume point. There's two cases
1693 * where this can occur. If the record is the last
1694 * redaction record, and the resume point is after the
1695 * end of the redacted data, curidx will be the last
1696 * redaction record. In that case, the loop will end
1697 * after this iteration. The second case is if the
1698 * resume point is between two redaction records, the
1699 * binary search can return either the record before
1700 * or after the resume point. In that case, the next
1701 * iteration will be greater than the resume point.
1703 if (redact_block_zb_compare(rb
, resume
) < 0) {
1704 ASSERT3U(curidx
, ==, minidx
);
1708 * If the place to resume is in the middle of
1709 * the range described by this
1710 * redact_block_phys, then modify the
1711 * redact_block_phys in memory so we generate
1712 * the right records.
1714 if (resume
->zb_object
== rb
->rbp_object
&&
1715 resume
->zb_blkid
> rb
->rbp_blkid
) {
1716 uint64_t diff
= resume
->zb_blkid
-
1718 rb
->rbp_blkid
= resume
->zb_blkid
;
1719 redact_block_set_count(rb
,
1720 redact_block_get_count(rb
) - diff
);
1726 if (cb(rb
, arg
) != 0) {
1732 zio_data_buf_free(buf
, bufsize
);