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);
1207 redaction_list_evict_sync(void *rlu)
1209 redaction_list_t *rl = rlu;
1210 zfs_refcount_destroy(&rl->rl_longholds);
1212 kmem_free(rl, sizeof (redaction_list_t));
1216 dsl_redaction_list_rele(redaction_list_t *rl, void *tag)
1218 dmu_buf_rele(rl->rl_dbuf, tag);
1222 dsl_redaction_list_hold_obj(dsl_pool_t *dp, uint64_t rlobj, void *tag,
1223 redaction_list_t **rlp)
1225 objset_t *mos = dp->dp_meta_objset;
1227 redaction_list_t *rl;
1230 ASSERT(dsl_pool_config_held(dp));
1232 err = dmu_bonus_hold(mos, rlobj, tag, &dbuf);
1236 rl = dmu_buf_get_user(dbuf);
1238 redaction_list_t *winner = NULL;
1240 rl = kmem_zalloc(sizeof (redaction_list_t), KM_SLEEP);
1242 rl->rl_object = rlobj;
1243 rl->rl_phys = dbuf->db_data;
1244 rl->rl_mos = dp->dp_meta_objset;
1245 zfs_refcount_create(&rl->rl_longholds);
1246 dmu_buf_init_user(&rl->rl_dbu, redaction_list_evict_sync, NULL,
1248 if ((winner = dmu_buf_set_user_ie(dbuf, &rl->rl_dbu)) != NULL) {
1249 kmem_free(rl, sizeof (*rl));
1258 * Snapshot ds is being destroyed.
1260 * Adjust the "freed_before_next" of any bookmarks between this snap
1261 * and the previous snapshot, because their "next snapshot" is changing.
1263 * If there are any bookmarks with HAS_FBN at this snapshot, remove
1264 * their HAS_SNAP flag (note: there can be at most one snapshot of
1265 * each filesystem at a given txg), and return B_TRUE. In this case
1266 * the caller can not remove the key in the deadlist at this TXG, because
1267 * the HAS_FBN bookmarks require the key be there.
1269 * Returns B_FALSE if there are no bookmarks with HAS_FBN at this
1270 * snapshot's TXG. In this case the caller can remove the key in the
1271 * deadlist at this TXG.
1274 dsl_bookmark_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
1276 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1278 dsl_dataset_t *head, *next;
1279 VERIFY0(dsl_dataset_hold_obj(dp,
1280 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &head));
1281 VERIFY0(dsl_dataset_hold_obj(dp,
1282 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &next));
1285 * Find the first bookmark that HAS_FBN at or after the
1286 * previous snapshot.
1288 dsl_bookmark_node_t search = { 0 };
1290 search.dbn_phys.zbm_creation_txg =
1291 dsl_dataset_phys(ds)->ds_prev_snap_txg;
1292 search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1294 * The empty-string name can't be in the AVL, and it compares
1295 * before any entries with this TXG.
1297 search.dbn_name = "";
1298 VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1299 dsl_bookmark_node_t *dbn =
1300 avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1303 * Iterate over all bookmarks that are at or after the previous
1304 * snapshot, and before this (being deleted) snapshot. Adjust
1305 * their FBN based on their new next snapshot.
1307 for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg <
1308 dsl_dataset_phys(ds)->ds_creation_txg;
1309 dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1310 if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN))
1313 * Increase our FBN by the amount of space that was live
1314 * (referenced) at the time of this bookmark (i.e.
1315 * birth <= zbm_creation_txg), and killed between this
1316 * (being deleted) snapshot and the next snapshot (i.e.
1317 * on the next snapshot's deadlist). (Space killed before
1318 * this are already on our FBN.)
1320 uint64_t referenced, compressed, uncompressed;
1321 dsl_deadlist_space_range(&next->ds_deadlist,
1322 0, dbn->dbn_phys.zbm_creation_txg,
1323 &referenced, &compressed, &uncompressed);
1324 dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=
1326 dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=
1328 dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=
1330 VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1331 dbn->dbn_name, sizeof (uint64_t),
1332 sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1333 &dbn->dbn_phys, tx));
1335 dsl_dataset_rele(next, FTAG);
1338 * There may be several bookmarks at this txg (the TXG of the
1339 * snapshot being deleted). We need to clear the SNAPSHOT_EXISTS
1340 * flag on all of them, and return TRUE if there is at least 1
1341 * bookmark here with HAS_FBN (thus preventing the deadlist
1342 * key from being removed).
1344 boolean_t rv = B_FALSE;
1345 for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1346 dsl_dataset_phys(ds)->ds_creation_txg;
1347 dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1348 if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {
1349 ASSERT(!(dbn->dbn_phys.zbm_flags &
1350 ZBM_FLAG_SNAPSHOT_EXISTS));
1353 ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS);
1354 dbn->dbn_phys.zbm_flags &= ~ZBM_FLAG_SNAPSHOT_EXISTS;
1355 VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1356 dbn->dbn_name, sizeof (uint64_t),
1357 sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1358 &dbn->dbn_phys, tx));
1361 dsl_dataset_rele(head, FTAG);
1366 * A snapshot is being created of this (head) dataset.
1368 * We don't keep keys in the deadlist for the most recent snapshot, or any
1369 * bookmarks at or after it, because there can't be any blocks on the
1370 * deadlist in this range. Now that the most recent snapshot is after
1371 * all bookmarks, we need to add these keys. Note that the caller always
1372 * adds a key at the previous snapshot, so we only add keys for bookmarks
1376 dsl_bookmark_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
1378 uint64_t last_key_added = UINT64_MAX;
1379 for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1380 dbn != NULL && dbn->dbn_phys.zbm_creation_txg >
1381 dsl_dataset_phys(ds)->ds_prev_snap_txg;
1382 dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1383 uint64_t creation_txg = dbn->dbn_phys.zbm_creation_txg;
1384 ASSERT3U(creation_txg, <=, last_key_added);
1386 * Note, there may be multiple bookmarks at this TXG,
1387 * and we only want to add the key for this TXG once.
1388 * The ds_bookmarks AVL is sorted by TXG, so we will visit
1389 * these bookmarks in sequence.
1391 if ((dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) &&
1392 creation_txg != last_key_added) {
1393 dsl_deadlist_add_key(&ds->ds_deadlist,
1395 last_key_added = creation_txg;
1401 * The next snapshot of the origin dataset has changed, due to
1402 * promote or clone swap. If there are any bookmarks at this dataset,
1403 * we need to update their zbm_*_freed_before_next_snap to reflect this.
1404 * The head dataset has the relevant bookmarks in ds_bookmarks.
1407 dsl_bookmark_next_changed(dsl_dataset_t *head, dsl_dataset_t *origin,
1410 dsl_pool_t *dp = dmu_tx_pool(tx);
1413 * Find the first bookmark that HAS_FBN at the origin snapshot.
1415 dsl_bookmark_node_t search = { 0 };
1417 search.dbn_phys.zbm_creation_txg =
1418 dsl_dataset_phys(origin)->ds_creation_txg;
1419 search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1421 * The empty-string name can't be in the AVL, and it compares
1422 * before any entries with this TXG.
1424 search.dbn_name = "";
1425 VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1426 dsl_bookmark_node_t *dbn =
1427 avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1430 * Iterate over all bookmarks that are at the origin txg.
1431 * Adjust their FBN based on their new next snapshot.
1433 for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1434 dsl_dataset_phys(origin)->ds_creation_txg &&
1435 (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);
1436 dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1439 * Bookmark is at the origin, therefore its
1440 * "next dataset" is changing, so we need
1441 * to reset its FBN by recomputing it in
1442 * dsl_bookmark_set_phys().
1444 ASSERT3U(dbn->dbn_phys.zbm_guid, ==,
1445 dsl_dataset_phys(origin)->ds_guid);
1446 ASSERT3U(dbn->dbn_phys.zbm_referenced_bytes_refd, ==,
1447 dsl_dataset_phys(origin)->ds_referenced_bytes);
1448 ASSERT(dbn->dbn_phys.zbm_flags &
1449 ZBM_FLAG_SNAPSHOT_EXISTS);
1451 * Save and restore the zbm_redaction_obj, which
1452 * is zeroed by dsl_bookmark_set_phys().
1454 uint64_t redaction_obj =
1455 dbn->dbn_phys.zbm_redaction_obj;
1456 dsl_bookmark_set_phys(&dbn->dbn_phys, origin);
1457 dbn->dbn_phys.zbm_redaction_obj = redaction_obj;
1459 VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1460 dbn->dbn_name, sizeof (uint64_t),
1461 sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1462 &dbn->dbn_phys, tx));
1467 * This block is no longer referenced by this (head) dataset.
1469 * Adjust the FBN of any bookmarks that reference this block, whose "next"
1470 * is the head dataset.
1473 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);