Path: blob/main/sys/contrib/openzfs/module/zfs/dsl_bookmark.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*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.20*/2122#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>30#include <sys/arc.h>31#include <sys/zap.h>32#include <sys/zfeature.h>33#include <sys/spa.h>34#include <sys/dsl_bookmark.h>35#include <zfs_namecheck.h>36#include <sys/dmu_send.h>37#include <sys/dbuf.h>3839static int40dsl_bookmark_hold_ds(dsl_pool_t *dp, const char *fullname,41dsl_dataset_t **dsp, const void *tag, char **shortnamep)42{43char buf[ZFS_MAX_DATASET_NAME_LEN];44char *hashp;4546if (strlen(fullname) >= ZFS_MAX_DATASET_NAME_LEN)47return (SET_ERROR(ENAMETOOLONG));48hashp = strchr(fullname, '#');49if (hashp == NULL)50return (SET_ERROR(EINVAL));5152*shortnamep = hashp + 1;53if (zfs_component_namecheck(*shortnamep, NULL, NULL))54return (SET_ERROR(EINVAL));55(void) strlcpy(buf, fullname, hashp - fullname + 1);56return (dsl_dataset_hold(dp, buf, tag, dsp));57}5859/*60* When reading BOOKMARK_V1 bookmarks, the BOOKMARK_V2 fields are guaranteed61* to be zeroed.62*63* Returns ESRCH if bookmark is not found.64* Note, we need to use the ZAP rather than the AVL to look up bookmarks65* by name, because only the ZAP honors the casesensitivity setting.66*/67int68dsl_bookmark_lookup_impl(dsl_dataset_t *ds, const char *shortname,69zfs_bookmark_phys_t *bmark_phys)70{71objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;72uint64_t bmark_zapobj = ds->ds_bookmarks_obj;73matchtype_t mt = 0;74int err;7576if (bmark_zapobj == 0)77return (SET_ERROR(ESRCH));7879if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)80mt = MT_NORMALIZE;8182/*83* Zero out the bookmark in case the one stored on disk84* is in an older, shorter format.85*/86memset(bmark_phys, 0, sizeof (*bmark_phys));8788err = zap_lookup_norm(mos, bmark_zapobj, shortname, sizeof (uint64_t),89sizeof (*bmark_phys) / sizeof (uint64_t), bmark_phys, mt, NULL, 0,90NULL);9192return (err == ENOENT ? SET_ERROR(ESRCH) : err);93}9495/*96* If later_ds is non-NULL, this will return EXDEV if the specified bookmark97* does not represents an earlier point in later_ds's timeline. However,98* bmp will still be filled in if we return EXDEV.99*100* Returns ENOENT if the dataset containing the bookmark does not exist.101* Returns ESRCH if the dataset exists but the bookmark was not found in it.102*/103int104dsl_bookmark_lookup(dsl_pool_t *dp, const char *fullname,105dsl_dataset_t *later_ds, zfs_bookmark_phys_t *bmp)106{107char *shortname;108dsl_dataset_t *ds;109int error;110111error = dsl_bookmark_hold_ds(dp, fullname, &ds, FTAG, &shortname);112if (error != 0)113return (error);114115error = dsl_bookmark_lookup_impl(ds, shortname, bmp);116if (error == 0 && later_ds != NULL) {117if (!dsl_dataset_is_before(later_ds, ds, bmp->zbm_creation_txg))118error = SET_ERROR(EXDEV);119}120dsl_dataset_rele(ds, FTAG);121return (error);122}123124/*125* Validates that126* - bmark is a full dataset path of a bookmark (bookmark_namecheck)127* - source is a full path of a snapshot or bookmark128* ({bookmark,snapshot}_namecheck)129*130* Returns 0 if valid, -1 otherwise.131*/132static int133dsl_bookmark_create_nvl_validate_pair(const char *bmark, const char *source)134{135if (bookmark_namecheck(bmark, NULL, NULL) != 0)136return (-1);137138int is_bmark, is_snap;139is_bmark = bookmark_namecheck(source, NULL, NULL) == 0;140is_snap = snapshot_namecheck(source, NULL, NULL) == 0;141if (!is_bmark && !is_snap)142return (-1);143144return (0);145}146147/*148* Check that the given nvlist corresponds to the following schema:149* { newbookmark -> source, ... }150* where151* - each pair passes dsl_bookmark_create_nvl_validate_pair152* - all newbookmarks are in the same pool153* - all newbookmarks have unique names154*155* Note that this function is only validates above schema. Callers must ensure156* that the bookmarks can be created, e.g. that sources exist.157*158* Returns 0 if the nvlist adheres to above schema.159* Returns -1 if it doesn't.160*/161int162dsl_bookmark_create_nvl_validate(nvlist_t *bmarks)163{164const char *first = NULL;165size_t first_len = 0;166167for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);168pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {169170const char *bmark = nvpair_name(pair);171const char *source;172173/* list structure: values must be snapshots XOR bookmarks */174if (nvpair_value_string(pair, &source) != 0)175return (-1);176if (dsl_bookmark_create_nvl_validate_pair(bmark, source) != 0)177return (-1);178179/* same pool check */180if (first == NULL) {181const char *cp = strpbrk(bmark, "/#");182if (cp == NULL)183return (-1);184first = bmark;185first_len = cp - bmark;186}187if (strncmp(first, bmark, first_len) != 0)188return (-1);189switch (*(bmark + first_len)) {190case '/': /* fallthrough */191case '#':192break;193default:194return (-1);195}196197/* unique newbookmark names; todo: O(n^2) */198for (nvpair_t *pair2 = nvlist_next_nvpair(bmarks, pair);199pair2 != NULL; pair2 = nvlist_next_nvpair(bmarks, pair2)) {200if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)201return (-1);202}203204}205return (0);206}207208/*209* expects that newbm and source have been validated using210* dsl_bookmark_create_nvl_validate_pair211*/212static int213dsl_bookmark_create_check_impl(dsl_pool_t *dp,214const char *newbm, const char *source)215{216ASSERT0(dsl_bookmark_create_nvl_validate_pair(newbm, source));217/* defer source namecheck until we know it's a snapshot or bookmark */218219int error;220dsl_dataset_t *newbm_ds;221char *newbm_short;222zfs_bookmark_phys_t bmark_phys;223224error = dsl_bookmark_hold_ds(dp, newbm, &newbm_ds, FTAG, &newbm_short);225if (error != 0)226return (error);227228/* Verify that the new bookmark does not already exist */229error = dsl_bookmark_lookup_impl(newbm_ds, newbm_short, &bmark_phys);230switch (error) {231case ESRCH:232/* happy path: new bmark doesn't exist, proceed after switch */233break;234case 0:235error = SET_ERROR(EEXIST);236goto eholdnewbmds;237default:238/* dsl_bookmark_lookup_impl already did SET_ERROR */239goto eholdnewbmds;240}241242/* error is retval of the following if-cascade */243if (strchr(source, '@') != NULL) {244dsl_dataset_t *source_snap_ds;245ASSERT0(snapshot_namecheck(source, NULL, NULL));246error = dsl_dataset_hold(dp, source, FTAG, &source_snap_ds);247if (error == 0) {248VERIFY(source_snap_ds->ds_is_snapshot);249/*250* Verify that source snapshot is an earlier point in251* newbm_ds's timeline (source may be newbm_ds's origin)252*/253if (!dsl_dataset_is_before(newbm_ds, source_snap_ds, 0))254error = SET_ERROR(255ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);256dsl_dataset_rele(source_snap_ds, FTAG);257}258} else if (strchr(source, '#') != NULL) {259zfs_bookmark_phys_t source_phys;260ASSERT0(bookmark_namecheck(source, NULL, NULL));261/*262* Source must exists and be an earlier point in newbm_ds's263* timeline (newbm_ds's origin may be a snap of source's ds)264*/265error = dsl_bookmark_lookup(dp, source, newbm_ds, &source_phys);266switch (error) {267case 0:268break; /* happy path */269case EXDEV:270error = SET_ERROR(ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);271break;272default:273/* dsl_bookmark_lookup already did SET_ERROR */274break;275}276} else {277/*278* dsl_bookmark_create_nvl_validate validates that source is279* either snapshot or bookmark280*/281panic("unreachable code: %s", source);282}283284eholdnewbmds:285dsl_dataset_rele(newbm_ds, FTAG);286return (error);287}288289int290dsl_bookmark_create_check(void *arg, dmu_tx_t *tx)291{292dsl_bookmark_create_arg_t *dbca = arg;293int rv = 0;294int schema_err = 0;295ASSERT3P(dbca, !=, NULL);296ASSERT3P(dbca->dbca_bmarks, !=, NULL);297/* dbca->dbca_errors is allowed to be NULL */298299dsl_pool_t *dp = dmu_tx_pool(tx);300301if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))302return (SET_ERROR(ENOTSUP));303304if (dsl_bookmark_create_nvl_validate(dbca->dbca_bmarks) != 0)305rv = schema_err = SET_ERROR(EINVAL);306307for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);308pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {309const char *new = nvpair_name(pair);310311int error = schema_err;312if (error == 0) {313const char *source = fnvpair_value_string(pair);314error = dsl_bookmark_create_check_impl(dp, new, source);315if (error != 0)316error = SET_ERROR(error);317}318319if (error != 0) {320rv = error;321if (dbca->dbca_errors != NULL)322fnvlist_add_int32(dbca->dbca_errors,323new, error);324}325}326327return (rv);328}329330static dsl_bookmark_node_t *331dsl_bookmark_node_alloc(char *shortname)332{333dsl_bookmark_node_t *dbn = kmem_alloc(sizeof (*dbn), KM_SLEEP);334dbn->dbn_name = spa_strdup(shortname);335dbn->dbn_dirty = B_FALSE;336mutex_init(&dbn->dbn_lock, NULL, MUTEX_DEFAULT, NULL);337return (dbn);338}339340/*341* Set the fields in the zfs_bookmark_phys_t based on the specified snapshot.342*/343static void344dsl_bookmark_set_phys(zfs_bookmark_phys_t *zbm, dsl_dataset_t *snap)345{346spa_t *spa = dsl_dataset_get_spa(snap);347objset_t *mos = spa_get_dsl(spa)->dp_meta_objset;348dsl_dataset_phys_t *dsp = dsl_dataset_phys(snap);349350memset(zbm, 0, sizeof (zfs_bookmark_phys_t));351zbm->zbm_guid = dsp->ds_guid;352zbm->zbm_creation_txg = dsp->ds_creation_txg;353zbm->zbm_creation_time = dsp->ds_creation_time;354zbm->zbm_redaction_obj = 0;355356/*357* If the dataset is encrypted create a larger bookmark to358* accommodate the IVset guid. The IVset guid was added359* after the encryption feature to prevent a problem with360* raw sends. If we encounter an encrypted dataset without361* an IVset guid we fall back to a normal bookmark.362*/363if (snap->ds_dir->dd_crypto_obj != 0 &&364spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) {365(void) zap_lookup(mos, snap->ds_object,366DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,367&zbm->zbm_ivset_guid);368}369370if (spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_WRITTEN)) {371zbm->zbm_flags = ZBM_FLAG_SNAPSHOT_EXISTS | ZBM_FLAG_HAS_FBN;372zbm->zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;373zbm->zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;374zbm->zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;375376dsl_dataset_t *nextds;377VERIFY0(dsl_dataset_hold_obj(snap->ds_dir->dd_pool,378dsp->ds_next_snap_obj, FTAG, &nextds));379dsl_deadlist_space(&nextds->ds_deadlist,380&zbm->zbm_referenced_freed_before_next_snap,381&zbm->zbm_compressed_freed_before_next_snap,382&zbm->zbm_uncompressed_freed_before_next_snap);383dsl_dataset_rele(nextds, FTAG);384}385}386387/*388* Add dsl_bookmark_node_t `dbn` to the given dataset and increment appropriate389* SPA feature counters.390*/391void392dsl_bookmark_node_add(dsl_dataset_t *hds, dsl_bookmark_node_t *dbn,393dmu_tx_t *tx)394{395dsl_pool_t *dp = dmu_tx_pool(tx);396objset_t *mos = dp->dp_meta_objset;397398if (hds->ds_bookmarks_obj == 0) {399hds->ds_bookmarks_obj = zap_create_norm(mos,400U8_TEXTPREP_TOUPPER, DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0,401tx);402spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);403404dsl_dataset_zapify(hds, tx);405VERIFY0(zap_add(mos, hds->ds_object,406DS_FIELD_BOOKMARK_NAMES,407sizeof (hds->ds_bookmarks_obj), 1,408&hds->ds_bookmarks_obj, tx));409}410411avl_add(&hds->ds_bookmarks, dbn);412413/*414* To maintain backwards compatibility with software that doesn't415* understand SPA_FEATURE_BOOKMARK_V2, we need to use the smallest416* possible bookmark size.417*/418uint64_t bookmark_phys_size = BOOKMARK_PHYS_SIZE_V1;419if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2) &&420(dbn->dbn_phys.zbm_ivset_guid != 0 || dbn->dbn_phys.zbm_flags &421ZBM_FLAG_HAS_FBN || dbn->dbn_phys.zbm_redaction_obj != 0)) {422bookmark_phys_size = BOOKMARK_PHYS_SIZE_V2;423spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2, tx);424}425426zfs_bookmark_phys_t zero_phys = { 0 };427ASSERT0(memcmp(((char *)&dbn->dbn_phys) + bookmark_phys_size,428&zero_phys, sizeof (zfs_bookmark_phys_t) - bookmark_phys_size));429430VERIFY0(zap_add(mos, hds->ds_bookmarks_obj, dbn->dbn_name,431sizeof (uint64_t), bookmark_phys_size / sizeof (uint64_t),432&dbn->dbn_phys, tx));433}434435/*436* If redaction_list is non-null, we create a redacted bookmark and redaction437* list, and store the object number of the redaction list in redact_obj.438*/439static void440dsl_bookmark_create_sync_impl_snap(const char *bookmark, const char *snapshot,441dmu_tx_t *tx, uint64_t num_redact_snaps, uint64_t *redact_snaps,442const void *tag, redaction_list_t **redaction_list)443{444dsl_pool_t *dp = dmu_tx_pool(tx);445objset_t *mos = dp->dp_meta_objset;446dsl_dataset_t *snapds, *bmark_fs;447char *shortname;448boolean_t bookmark_redacted;449uint64_t *dsredactsnaps;450uint64_t dsnumsnaps;451452VERIFY0(dsl_dataset_hold(dp, snapshot, FTAG, &snapds));453VERIFY0(dsl_bookmark_hold_ds(dp, bookmark, &bmark_fs, FTAG,454&shortname));455456dsl_bookmark_node_t *dbn = dsl_bookmark_node_alloc(shortname);457dsl_bookmark_set_phys(&dbn->dbn_phys, snapds);458459bookmark_redacted = dsl_dataset_get_uint64_array_feature(snapds,460SPA_FEATURE_REDACTED_DATASETS, &dsnumsnaps, &dsredactsnaps);461if (redaction_list != NULL || bookmark_redacted) {462redaction_list_t *local_rl;463boolean_t spill = B_FALSE;464if (bookmark_redacted) {465redact_snaps = dsredactsnaps;466num_redact_snaps = dsnumsnaps;467}468int bonuslen = sizeof (redaction_list_phys_t) +469num_redact_snaps * sizeof (uint64_t);470if (bonuslen > dmu_bonus_max())471spill = B_TRUE;472dbn->dbn_phys.zbm_redaction_obj = dmu_object_alloc(mos,473DMU_OTN_UINT64_METADATA, SPA_OLD_MAXBLOCKSIZE,474DMU_OTN_UINT64_METADATA, spill ? 0 : bonuslen, tx);475spa_feature_incr(dp->dp_spa,476SPA_FEATURE_REDACTION_BOOKMARKS, tx);477if (spill) {478spa_feature_incr(dp->dp_spa,479SPA_FEATURE_REDACTION_LIST_SPILL, tx);480}481482VERIFY0(dsl_redaction_list_hold_obj(dp,483dbn->dbn_phys.zbm_redaction_obj, tag, &local_rl));484dsl_redaction_list_long_hold(dp, local_rl, tag);485486if (!spill) {487ASSERT3U(local_rl->rl_bonus->db_size, >=, bonuslen);488dmu_buf_will_dirty(local_rl->rl_bonus, tx);489} else {490dmu_buf_t *db;491VERIFY0(dmu_spill_hold_by_bonus(local_rl->rl_bonus,492DB_RF_MUST_SUCCEED, FTAG, &db));493dmu_buf_will_fill(db, tx, B_FALSE);494VERIFY0(dbuf_spill_set_blksz(db, P2ROUNDUP(bonuslen,495SPA_MINBLOCKSIZE), tx));496local_rl->rl_phys = db->db_data;497local_rl->rl_dbuf = db;498}499memcpy(local_rl->rl_phys->rlp_snaps, redact_snaps,500sizeof (uint64_t) * num_redact_snaps);501local_rl->rl_phys->rlp_num_snaps = num_redact_snaps;502if (bookmark_redacted) {503ASSERT0P(redaction_list);504local_rl->rl_phys->rlp_last_blkid = UINT64_MAX;505local_rl->rl_phys->rlp_last_object = UINT64_MAX;506dsl_redaction_list_long_rele(local_rl, tag);507dsl_redaction_list_rele(local_rl, tag);508} else {509*redaction_list = local_rl;510}511}512513if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {514spa_feature_incr(dp->dp_spa,515SPA_FEATURE_BOOKMARK_WRITTEN, tx);516}517518dsl_bookmark_node_add(bmark_fs, dbn, tx);519520spa_history_log_internal_ds(bmark_fs, "bookmark", tx,521"name=%s creation_txg=%llu target_snap=%llu redact_obj=%llu",522shortname, (longlong_t)dbn->dbn_phys.zbm_creation_txg,523(longlong_t)snapds->ds_object,524(longlong_t)dbn->dbn_phys.zbm_redaction_obj);525526dsl_dataset_rele(bmark_fs, FTAG);527dsl_dataset_rele(snapds, FTAG);528}529530531static void532dsl_bookmark_create_sync_impl_book(533const char *new_name, const char *source_name, dmu_tx_t *tx)534{535dsl_pool_t *dp = dmu_tx_pool(tx);536dsl_dataset_t *bmark_fs_source, *bmark_fs_new;537char *source_shortname, *new_shortname;538zfs_bookmark_phys_t source_phys;539540VERIFY0(dsl_bookmark_hold_ds(dp, source_name, &bmark_fs_source, FTAG,541&source_shortname));542VERIFY0(dsl_bookmark_hold_ds(dp, new_name, &bmark_fs_new, FTAG,543&new_shortname));544545/*546* create a copy of the source bookmark by copying most of its members547*548* Caveat: bookmarking a redaction bookmark yields a normal bookmark549* -----------------------------------------------------------------550* Reasoning:551* - The zbm_redaction_obj would be referred to by both source and new552* bookmark, but would be destroyed once either source or new is553* destroyed, resulting in use-after-free of the referred object.554* - User expectation when issuing the `zfs bookmark` command is that555* a normal bookmark of the source is created556*557* Design Alternatives For Full Redaction Bookmark Copying:558* - reference-count the redaction object => would require on-disk559* format change for existing redaction objects560* - Copy the redaction object => cannot be done in syncing context561* because the redaction object might be too large562*/563564VERIFY0(dsl_bookmark_lookup_impl(bmark_fs_source, source_shortname,565&source_phys));566dsl_bookmark_node_t *new_dbn = dsl_bookmark_node_alloc(new_shortname);567568memcpy(&new_dbn->dbn_phys, &source_phys, sizeof (source_phys));569new_dbn->dbn_phys.zbm_redaction_obj = 0;570571/* update feature counters */572if (new_dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {573spa_feature_incr(dp->dp_spa,574SPA_FEATURE_BOOKMARK_WRITTEN, tx);575}576/* no need for redaction bookmark counter; nulled zbm_redaction_obj */577/* dsl_bookmark_node_add bumps bookmarks and v2-bookmarks counter */578579/*580* write new bookmark581*582* Note that dsl_bookmark_lookup_impl guarantees that, if source is a583* v1 bookmark, the v2-only fields are zeroed.584* And dsl_bookmark_node_add writes back a v1-sized bookmark if585* v2 bookmarks are disabled and/or v2-only fields are zeroed.586* => bookmark copying works on pre-bookmark-v2 pools587*/588dsl_bookmark_node_add(bmark_fs_new, new_dbn, tx);589590spa_history_log_internal_ds(bmark_fs_source, "bookmark", tx,591"name=%s creation_txg=%llu source_guid=%llu",592new_shortname, (longlong_t)new_dbn->dbn_phys.zbm_creation_txg,593(longlong_t)source_phys.zbm_guid);594595dsl_dataset_rele(bmark_fs_source, FTAG);596dsl_dataset_rele(bmark_fs_new, FTAG);597}598599void600dsl_bookmark_create_sync(void *arg, dmu_tx_t *tx)601{602dsl_bookmark_create_arg_t *dbca = arg;603604ASSERT(spa_feature_is_enabled(dmu_tx_pool(tx)->dp_spa,605SPA_FEATURE_BOOKMARKS));606607for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);608pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {609610const char *new = nvpair_name(pair);611const char *source = fnvpair_value_string(pair);612613if (strchr(source, '@') != NULL) {614dsl_bookmark_create_sync_impl_snap(new, source, tx,6150, NULL, NULL, NULL);616} else if (strchr(source, '#') != NULL) {617dsl_bookmark_create_sync_impl_book(new, source, tx);618} else {619panic("unreachable code");620}621622}623}624625/*626* The bookmarks must all be in the same pool.627*/628int629dsl_bookmark_create(nvlist_t *bmarks, nvlist_t *errors)630{631nvpair_t *pair;632dsl_bookmark_create_arg_t dbca;633634pair = nvlist_next_nvpair(bmarks, NULL);635if (pair == NULL)636return (0);637638dbca.dbca_bmarks = bmarks;639dbca.dbca_errors = errors;640641return (dsl_sync_task(nvpair_name(pair), dsl_bookmark_create_check,642dsl_bookmark_create_sync, &dbca,643fnvlist_num_pairs(bmarks), ZFS_SPACE_CHECK_NORMAL));644}645646static int647dsl_bookmark_create_redacted_check(void *arg, dmu_tx_t *tx)648{649dsl_bookmark_create_redacted_arg_t *dbcra = arg;650dsl_pool_t *dp = dmu_tx_pool(tx);651int rv = 0;652653if (!spa_feature_is_enabled(dp->dp_spa,654SPA_FEATURE_REDACTION_BOOKMARKS))655return (SET_ERROR(ENOTSUP));656/*657* If the list of redact snaps will not fit in the bonus buffer (or658* spill block, with the REDACTION_LIST_SPILL feature) with the659* furthest reached object and offset, fail.660*/661uint64_t snaplimit = ((spa_feature_is_enabled(dp->dp_spa,662SPA_FEATURE_REDACTION_LIST_SPILL) ? spa_maxblocksize(dp->dp_spa) :663dmu_bonus_max()) -664sizeof (redaction_list_phys_t)) / sizeof (uint64_t);665if (dbcra->dbcra_numsnaps > snaplimit)666return (SET_ERROR(E2BIG));667668if (dsl_bookmark_create_nvl_validate_pair(669dbcra->dbcra_bmark, dbcra->dbcra_snap) != 0)670return (SET_ERROR(EINVAL));671672rv = dsl_bookmark_create_check_impl(dp,673dbcra->dbcra_bmark, dbcra->dbcra_snap);674return (rv);675}676677static void678dsl_bookmark_create_redacted_sync(void *arg, dmu_tx_t *tx)679{680dsl_bookmark_create_redacted_arg_t *dbcra = arg;681dsl_bookmark_create_sync_impl_snap(dbcra->dbcra_bmark,682dbcra->dbcra_snap, tx, dbcra->dbcra_numsnaps, dbcra->dbcra_snaps,683dbcra->dbcra_tag, dbcra->dbcra_rl);684}685686int687dsl_bookmark_create_redacted(const char *bookmark, const char *snapshot,688uint64_t numsnaps, uint64_t *snapguids, const void *tag,689redaction_list_t **rl)690{691dsl_bookmark_create_redacted_arg_t dbcra;692693dbcra.dbcra_bmark = bookmark;694dbcra.dbcra_snap = snapshot;695dbcra.dbcra_rl = rl;696dbcra.dbcra_numsnaps = numsnaps;697dbcra.dbcra_snaps = snapguids;698dbcra.dbcra_tag = tag;699700return (dsl_sync_task(bookmark, dsl_bookmark_create_redacted_check,701dsl_bookmark_create_redacted_sync, &dbcra, 5,702ZFS_SPACE_CHECK_NORMAL));703}704705/*706* Retrieve the list of properties given in the 'props' nvlist for a bookmark.707* If 'props' is NULL, retrieves all properties.708*/709static void710dsl_bookmark_fetch_props(dsl_pool_t *dp, zfs_bookmark_phys_t *bmark_phys,711nvlist_t *props, nvlist_t *out_props)712{713ASSERT3P(dp, !=, NULL);714ASSERT3P(bmark_phys, !=, NULL);715ASSERT3P(out_props, !=, NULL);716ASSERT(RRW_LOCK_HELD(&dp->dp_config_rwlock));717718if (props == NULL || nvlist_exists(props,719zfs_prop_to_name(ZFS_PROP_GUID))) {720dsl_prop_nvlist_add_uint64(out_props,721ZFS_PROP_GUID, bmark_phys->zbm_guid);722}723if (props == NULL || nvlist_exists(props,724zfs_prop_to_name(ZFS_PROP_CREATETXG))) {725dsl_prop_nvlist_add_uint64(out_props,726ZFS_PROP_CREATETXG, bmark_phys->zbm_creation_txg);727}728if (props == NULL || nvlist_exists(props,729zfs_prop_to_name(ZFS_PROP_CREATION))) {730dsl_prop_nvlist_add_uint64(out_props,731ZFS_PROP_CREATION, bmark_phys->zbm_creation_time);732}733if (props == NULL || nvlist_exists(props,734zfs_prop_to_name(ZFS_PROP_IVSET_GUID))) {735dsl_prop_nvlist_add_uint64(out_props,736ZFS_PROP_IVSET_GUID, bmark_phys->zbm_ivset_guid);737}738if (bmark_phys->zbm_flags & ZBM_FLAG_HAS_FBN) {739if (props == NULL || nvlist_exists(props,740zfs_prop_to_name(ZFS_PROP_REFERENCED))) {741dsl_prop_nvlist_add_uint64(out_props,742ZFS_PROP_REFERENCED,743bmark_phys->zbm_referenced_bytes_refd);744}745if (props == NULL || nvlist_exists(props,746zfs_prop_to_name(ZFS_PROP_LOGICALREFERENCED))) {747dsl_prop_nvlist_add_uint64(out_props,748ZFS_PROP_LOGICALREFERENCED,749bmark_phys->zbm_uncompressed_bytes_refd);750}751if (props == NULL || nvlist_exists(props,752zfs_prop_to_name(ZFS_PROP_REFRATIO))) {753uint64_t ratio =754bmark_phys->zbm_compressed_bytes_refd == 0 ? 100 :755bmark_phys->zbm_uncompressed_bytes_refd * 100 /756bmark_phys->zbm_compressed_bytes_refd;757dsl_prop_nvlist_add_uint64(out_props,758ZFS_PROP_REFRATIO, ratio);759}760}761762if ((props == NULL || nvlist_exists(props, "redact_snaps") ||763nvlist_exists(props, "redact_complete")) &&764bmark_phys->zbm_redaction_obj != 0) {765redaction_list_t *rl;766int err = dsl_redaction_list_hold_obj(dp,767bmark_phys->zbm_redaction_obj, FTAG, &rl);768if (err == 0) {769if (nvlist_exists(props, "redact_snaps")) {770nvlist_t *nvl;771nvl = fnvlist_alloc();772fnvlist_add_uint64_array(nvl, ZPROP_VALUE,773rl->rl_phys->rlp_snaps,774rl->rl_phys->rlp_num_snaps);775fnvlist_add_nvlist(out_props, "redact_snaps",776nvl);777nvlist_free(nvl);778}779if (nvlist_exists(props, "redact_complete")) {780nvlist_t *nvl;781nvl = fnvlist_alloc();782fnvlist_add_boolean_value(nvl, ZPROP_VALUE,783rl->rl_phys->rlp_last_blkid == UINT64_MAX &&784rl->rl_phys->rlp_last_object == UINT64_MAX);785fnvlist_add_nvlist(out_props, "redact_complete",786nvl);787nvlist_free(nvl);788}789dsl_redaction_list_rele(rl, FTAG);790}791}792}793794int795dsl_get_bookmarks_impl(dsl_dataset_t *ds, nvlist_t *props, nvlist_t *outnvl)796{797dsl_pool_t *dp = ds->ds_dir->dd_pool;798799ASSERT(dsl_pool_config_held(dp));800801if (dsl_dataset_is_snapshot(ds))802return (SET_ERROR(EINVAL));803804for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);805dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {806nvlist_t *out_props = fnvlist_alloc();807808dsl_bookmark_fetch_props(dp, &dbn->dbn_phys, props, out_props);809810fnvlist_add_nvlist(outnvl, dbn->dbn_name, out_props);811fnvlist_free(out_props);812}813return (0);814}815816/*817* Comparison func for ds_bookmarks AVL tree. We sort the bookmarks by818* their TXG, then by their FBN-ness. The "FBN-ness" component ensures819* that all bookmarks at the same TXG that HAS_FBN are adjacent, which820* dsl_bookmark_destroy_sync_impl() depends on. Note that there may be821* multiple bookmarks at the same TXG (with the same FBN-ness). In this822* case we differentiate them by an arbitrary metric (in this case,823* their names).824*/825static int826dsl_bookmark_compare(const void *l, const void *r)827{828const dsl_bookmark_node_t *ldbn = l;829const dsl_bookmark_node_t *rdbn = r;830831int64_t cmp = TREE_CMP(ldbn->dbn_phys.zbm_creation_txg,832rdbn->dbn_phys.zbm_creation_txg);833if (likely(cmp))834return (cmp);835cmp = TREE_CMP((ldbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN),836(rdbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));837if (likely(cmp))838return (cmp);839cmp = strcmp(ldbn->dbn_name, rdbn->dbn_name);840return (TREE_ISIGN(cmp));841}842843/*844* Cache this (head) dataset's bookmarks in the ds_bookmarks AVL tree.845*/846int847dsl_bookmark_init_ds(dsl_dataset_t *ds)848{849dsl_pool_t *dp = ds->ds_dir->dd_pool;850objset_t *mos = dp->dp_meta_objset;851852ASSERT(!ds->ds_is_snapshot);853854avl_create(&ds->ds_bookmarks, dsl_bookmark_compare,855sizeof (dsl_bookmark_node_t),856offsetof(dsl_bookmark_node_t, dbn_node));857858if (!dsl_dataset_is_zapified(ds))859return (0);860861int zaperr = zap_lookup(mos, ds->ds_object, DS_FIELD_BOOKMARK_NAMES,862sizeof (ds->ds_bookmarks_obj), 1, &ds->ds_bookmarks_obj);863if (zaperr == ENOENT)864return (0);865if (zaperr != 0)866return (zaperr);867868if (ds->ds_bookmarks_obj == 0)869return (0);870871int err = 0;872zap_cursor_t zc;873zap_attribute_t *attr;874875attr = zap_attribute_alloc();876for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);877(err = zap_cursor_retrieve(&zc, attr)) == 0;878zap_cursor_advance(&zc)) {879dsl_bookmark_node_t *dbn =880dsl_bookmark_node_alloc(attr->za_name);881882err = dsl_bookmark_lookup_impl(ds,883dbn->dbn_name, &dbn->dbn_phys);884ASSERT3U(err, !=, ENOENT);885if (err != 0) {886kmem_free(dbn, sizeof (*dbn));887break;888}889avl_add(&ds->ds_bookmarks, dbn);890}891zap_cursor_fini(&zc);892zap_attribute_free(attr);893if (err == ENOENT)894err = 0;895return (err);896}897898void899dsl_bookmark_fini_ds(dsl_dataset_t *ds)900{901void *cookie = NULL;902dsl_bookmark_node_t *dbn;903904if (ds->ds_is_snapshot)905return;906907while ((dbn = avl_destroy_nodes(&ds->ds_bookmarks, &cookie)) != NULL) {908spa_strfree(dbn->dbn_name);909mutex_destroy(&dbn->dbn_lock);910kmem_free(dbn, sizeof (*dbn));911}912avl_destroy(&ds->ds_bookmarks);913}914915/*916* Retrieve the bookmarks that exist in the specified dataset, and the917* requested properties of each bookmark.918*919* The "props" nvlist specifies which properties are requested.920* See lzc_get_bookmarks() for the list of valid properties.921*/922int923dsl_get_bookmarks(const char *dsname, nvlist_t *props, nvlist_t *outnvl)924{925dsl_pool_t *dp;926dsl_dataset_t *ds;927int err;928929err = dsl_pool_hold(dsname, FTAG, &dp);930if (err != 0)931return (err);932err = dsl_dataset_hold(dp, dsname, FTAG, &ds);933if (err != 0) {934dsl_pool_rele(dp, FTAG);935return (err);936}937938err = dsl_get_bookmarks_impl(ds, props, outnvl);939940dsl_dataset_rele(ds, FTAG);941dsl_pool_rele(dp, FTAG);942return (err);943}944945/*946* Retrieve all properties for a single bookmark in the given dataset.947*/948int949dsl_get_bookmark_props(const char *dsname, const char *bmname, nvlist_t *props)950{951dsl_pool_t *dp;952dsl_dataset_t *ds;953zfs_bookmark_phys_t bmark_phys = { 0 };954int err;955956err = dsl_pool_hold(dsname, FTAG, &dp);957if (err != 0)958return (err);959err = dsl_dataset_hold(dp, dsname, FTAG, &ds);960if (err != 0) {961dsl_pool_rele(dp, FTAG);962return (err);963}964965err = dsl_bookmark_lookup_impl(ds, bmname, &bmark_phys);966if (err != 0)967goto out;968969dsl_bookmark_fetch_props(dp, &bmark_phys, NULL, props);970out:971dsl_dataset_rele(ds, FTAG);972dsl_pool_rele(dp, FTAG);973return (err);974}975976typedef struct dsl_bookmark_destroy_arg {977nvlist_t *dbda_bmarks;978nvlist_t *dbda_success;979nvlist_t *dbda_errors;980} dsl_bookmark_destroy_arg_t;981982static void983dsl_bookmark_destroy_sync_impl(dsl_dataset_t *ds, const char *name,984dmu_tx_t *tx)985{986objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;987uint64_t bmark_zapobj = ds->ds_bookmarks_obj;988matchtype_t mt = 0;989uint64_t int_size, num_ints;990/*991* 'search' must be zeroed so that dbn_flags (which is used in992* dsl_bookmark_compare()) will be zeroed even if the on-disk993* (in ZAP) bookmark is shorter than offsetof(dbn_flags).994*/995dsl_bookmark_node_t search = { 0 };996char realname[ZFS_MAX_DATASET_NAME_LEN];997998/*999* Find the real name of this bookmark, which may be different1000* from the given name if the dataset is case-insensitive. Then1001* use the real name to find the node in the ds_bookmarks AVL tree.1002*/10031004if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)1005mt = MT_NORMALIZE;10061007VERIFY0(zap_length(mos, bmark_zapobj, name, &int_size, &num_ints));10081009ASSERT3U(int_size, ==, sizeof (uint64_t));10101011if (num_ints * int_size > BOOKMARK_PHYS_SIZE_V1) {1012spa_feature_decr(dmu_objset_spa(mos),1013SPA_FEATURE_BOOKMARK_V2, tx);1014}1015VERIFY0(zap_lookup_norm(mos, bmark_zapobj, name, sizeof (uint64_t),1016num_ints, &search.dbn_phys, mt, realname, sizeof (realname), NULL));10171018search.dbn_name = realname;1019dsl_bookmark_node_t *dbn = avl_find(&ds->ds_bookmarks, &search, NULL);1020ASSERT(dbn != NULL);10211022if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {1023/*1024* If this bookmark HAS_FBN, and it is before the most1025* recent snapshot, then its TXG is a key in the head's1026* deadlist (and all clones' heads' deadlists). If this is1027* the last thing keeping the key (i.e. there are no more1028* bookmarks with HAS_FBN at this TXG, and there is no1029* snapshot at this TXG), then remove the key.1030*1031* Note that this algorithm depends on ds_bookmarks being1032* sorted such that all bookmarks at the same TXG with1033* HAS_FBN are adjacent (with no non-HAS_FBN bookmarks1034* at the same TXG in between them). If this were not1035* the case, we would need to examine *all* bookmarks1036* at this TXG, rather than just the adjacent ones.1037*/10381039dsl_bookmark_node_t *dbn_prev =1040AVL_PREV(&ds->ds_bookmarks, dbn);1041dsl_bookmark_node_t *dbn_next =1042AVL_NEXT(&ds->ds_bookmarks, dbn);10431044boolean_t more_bookmarks_at_this_txg =1045(dbn_prev != NULL && dbn_prev->dbn_phys.zbm_creation_txg ==1046dbn->dbn_phys.zbm_creation_txg &&1047(dbn_prev->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) ||1048(dbn_next != NULL && dbn_next->dbn_phys.zbm_creation_txg ==1049dbn->dbn_phys.zbm_creation_txg &&1050(dbn_next->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));10511052if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS) &&1053!more_bookmarks_at_this_txg &&1054dbn->dbn_phys.zbm_creation_txg <1055dsl_dataset_phys(ds)->ds_prev_snap_txg) {1056dsl_dir_remove_clones_key(ds->ds_dir,1057dbn->dbn_phys.zbm_creation_txg, tx);1058dsl_deadlist_remove_key(&ds->ds_deadlist,1059dbn->dbn_phys.zbm_creation_txg, tx);1060}10611062spa_feature_decr(dmu_objset_spa(mos),1063SPA_FEATURE_BOOKMARK_WRITTEN, tx);1064}10651066if (dbn->dbn_phys.zbm_redaction_obj != 0) {1067dnode_t *rl;1068VERIFY0(dnode_hold(mos,1069dbn->dbn_phys.zbm_redaction_obj, FTAG, &rl));1070if (rl->dn_have_spill) {1071spa_feature_decr(dmu_objset_spa(mos),1072SPA_FEATURE_REDACTION_LIST_SPILL, tx);1073}1074dnode_rele(rl, FTAG);1075VERIFY0(dmu_object_free(mos,1076dbn->dbn_phys.zbm_redaction_obj, tx));1077spa_feature_decr(dmu_objset_spa(mos),1078SPA_FEATURE_REDACTION_BOOKMARKS, tx);1079}10801081avl_remove(&ds->ds_bookmarks, dbn);1082spa_strfree(dbn->dbn_name);1083mutex_destroy(&dbn->dbn_lock);1084kmem_free(dbn, sizeof (*dbn));10851086VERIFY0(zap_remove_norm(mos, bmark_zapobj, name, mt, tx));1087}10881089static int1090dsl_bookmark_destroy_check(void *arg, dmu_tx_t *tx)1091{1092dsl_bookmark_destroy_arg_t *dbda = arg;1093dsl_pool_t *dp = dmu_tx_pool(tx);1094int rv = 0;10951096ASSERT(nvlist_empty(dbda->dbda_success));1097ASSERT(nvlist_empty(dbda->dbda_errors));10981099if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))1100return (0);11011102for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_bmarks, NULL);1103pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_bmarks, pair)) {1104const char *fullname = nvpair_name(pair);1105dsl_dataset_t *ds;1106zfs_bookmark_phys_t bm;1107int error;1108char *shortname;11091110error = dsl_bookmark_hold_ds(dp, fullname, &ds,1111FTAG, &shortname);1112if (error == ENOENT) {1113/* ignore it; the bookmark is "already destroyed" */1114continue;1115}1116if (error == 0) {1117error = dsl_bookmark_lookup_impl(ds, shortname, &bm);1118dsl_dataset_rele(ds, FTAG);1119if (error == ESRCH) {1120/*1121* ignore it; the bookmark is1122* "already destroyed"1123*/1124continue;1125}1126if (error == 0 && bm.zbm_redaction_obj != 0) {1127redaction_list_t *rl = NULL;1128error = dsl_redaction_list_hold_obj(tx->tx_pool,1129bm.zbm_redaction_obj, FTAG, &rl);1130if (error == ENOENT) {1131error = 0;1132} else if (error == 0 &&1133dsl_redaction_list_long_held(rl)) {1134error = SET_ERROR(EBUSY);1135}1136if (rl != NULL) {1137dsl_redaction_list_rele(rl, FTAG);1138}1139}1140}1141if (error == 0) {1142if (dmu_tx_is_syncing(tx)) {1143fnvlist_add_boolean(dbda->dbda_success,1144fullname);1145}1146} else {1147fnvlist_add_int32(dbda->dbda_errors, fullname, error);1148rv = error;1149}1150}1151return (rv);1152}11531154static void1155dsl_bookmark_destroy_sync(void *arg, dmu_tx_t *tx)1156{1157dsl_bookmark_destroy_arg_t *dbda = arg;1158dsl_pool_t *dp = dmu_tx_pool(tx);1159objset_t *mos = dp->dp_meta_objset;11601161for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_success, NULL);1162pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_success, pair)) {1163dsl_dataset_t *ds;1164char *shortname;1165uint64_t zap_cnt;11661167VERIFY0(dsl_bookmark_hold_ds(dp, nvpair_name(pair),1168&ds, FTAG, &shortname));1169dsl_bookmark_destroy_sync_impl(ds, shortname, tx);11701171/*1172* If all of this dataset's bookmarks have been destroyed,1173* free the zap object and decrement the feature's use count.1174*/1175VERIFY0(zap_count(mos, ds->ds_bookmarks_obj, &zap_cnt));1176if (zap_cnt == 0) {1177dmu_buf_will_dirty(ds->ds_dbuf, tx);1178VERIFY0(zap_destroy(mos, ds->ds_bookmarks_obj, tx));1179ds->ds_bookmarks_obj = 0;1180spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);1181VERIFY0(zap_remove(mos, ds->ds_object,1182DS_FIELD_BOOKMARK_NAMES, tx));1183}11841185spa_history_log_internal_ds(ds, "remove bookmark", tx,1186"name=%s", shortname);11871188dsl_dataset_rele(ds, FTAG);1189}1190}11911192/*1193* The bookmarks must all be in the same pool.1194*/1195int1196dsl_bookmark_destroy(nvlist_t *bmarks, nvlist_t *errors)1197{1198int rv;1199dsl_bookmark_destroy_arg_t dbda;1200nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);1201if (pair == NULL)1202return (0);12031204dbda.dbda_bmarks = bmarks;1205dbda.dbda_errors = errors;1206dbda.dbda_success = fnvlist_alloc();12071208rv = dsl_sync_task(nvpair_name(pair), dsl_bookmark_destroy_check,1209dsl_bookmark_destroy_sync, &dbda, fnvlist_num_pairs(bmarks),1210ZFS_SPACE_CHECK_RESERVED);1211fnvlist_free(dbda.dbda_success);1212return (rv);1213}12141215/* Return B_TRUE if there are any long holds on this dataset. */1216boolean_t1217dsl_redaction_list_long_held(redaction_list_t *rl)1218{1219return (!zfs_refcount_is_zero(&rl->rl_longholds));1220}12211222void1223dsl_redaction_list_long_hold(dsl_pool_t *dp, redaction_list_t *rl,1224const void *tag)1225{1226ASSERT(dsl_pool_config_held(dp));1227(void) zfs_refcount_add(&rl->rl_longholds, tag);1228}12291230void1231dsl_redaction_list_long_rele(redaction_list_t *rl, const void *tag)1232{1233(void) zfs_refcount_remove(&rl->rl_longholds, tag);1234}12351236static void1237redaction_list_evict_sync(void *rlu)1238{1239redaction_list_t *rl = rlu;1240zfs_refcount_destroy(&rl->rl_longholds);12411242kmem_free(rl, sizeof (redaction_list_t));1243}12441245void1246dsl_redaction_list_rele(redaction_list_t *rl, const void *tag)1247{1248if (rl->rl_bonus != rl->rl_dbuf)1249dmu_buf_rele(rl->rl_dbuf, tag);1250dmu_buf_rele(rl->rl_bonus, tag);1251}12521253int1254dsl_redaction_list_hold_obj(dsl_pool_t *dp, uint64_t rlobj, const void *tag,1255redaction_list_t **rlp)1256{1257objset_t *mos = dp->dp_meta_objset;1258dmu_buf_t *dbuf, *spill_dbuf;1259redaction_list_t *rl;1260int err;12611262ASSERT(dsl_pool_config_held(dp));12631264err = dmu_bonus_hold(mos, rlobj, tag, &dbuf);1265if (err != 0)1266return (err);12671268rl = dmu_buf_get_user(dbuf);1269if (rl == NULL) {1270redaction_list_t *winner = NULL;12711272rl = kmem_zalloc(sizeof (redaction_list_t), KM_SLEEP);1273rl->rl_bonus = dbuf;1274if (dmu_spill_hold_existing(dbuf, tag, &spill_dbuf) == 0) {1275rl->rl_dbuf = spill_dbuf;1276} else {1277rl->rl_dbuf = dbuf;1278}1279rl->rl_object = rlobj;1280rl->rl_phys = rl->rl_dbuf->db_data;1281rl->rl_mos = dp->dp_meta_objset;1282zfs_refcount_create(&rl->rl_longholds);1283dmu_buf_init_user(&rl->rl_dbu, redaction_list_evict_sync, NULL,1284&rl->rl_bonus);1285if ((winner = dmu_buf_set_user_ie(dbuf, &rl->rl_dbu)) != NULL) {1286kmem_free(rl, sizeof (*rl));1287rl = winner;1288}1289}1290*rlp = rl;1291return (0);1292}12931294/*1295* Snapshot ds is being destroyed.1296*1297* Adjust the "freed_before_next" of any bookmarks between this snap1298* and the previous snapshot, because their "next snapshot" is changing.1299*1300* If there are any bookmarks with HAS_FBN at this snapshot, remove1301* their HAS_SNAP flag (note: there can be at most one snapshot of1302* each filesystem at a given txg), and return B_TRUE. In this case1303* the caller can not remove the key in the deadlist at this TXG, because1304* the HAS_FBN bookmarks require the key be there.1305*1306* Returns B_FALSE if there are no bookmarks with HAS_FBN at this1307* snapshot's TXG. In this case the caller can remove the key in the1308* deadlist at this TXG.1309*/1310boolean_t1311dsl_bookmark_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)1312{1313dsl_pool_t *dp = ds->ds_dir->dd_pool;13141315dsl_dataset_t *head, *next;1316VERIFY0(dsl_dataset_hold_obj(dp,1317dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &head));1318VERIFY0(dsl_dataset_hold_obj(dp,1319dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &next));13201321/*1322* Find the first bookmark that HAS_FBN at or after the1323* previous snapshot.1324*/1325dsl_bookmark_node_t search = { 0 };1326avl_index_t idx;1327search.dbn_phys.zbm_creation_txg =1328dsl_dataset_phys(ds)->ds_prev_snap_txg;1329search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;1330/*1331* The empty-string name can't be in the AVL, and it compares1332* before any entries with this TXG.1333*/1334search.dbn_name = (char *)"";1335VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);1336dsl_bookmark_node_t *dbn =1337avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);13381339/*1340* Iterate over all bookmarks that are at or after the previous1341* snapshot, and before this (being deleted) snapshot. Adjust1342* their FBN based on their new next snapshot.1343*/1344for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg <1345dsl_dataset_phys(ds)->ds_creation_txg;1346dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {1347if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN))1348continue;1349/*1350* Increase our FBN by the amount of space that was live1351* (referenced) at the time of this bookmark (i.e.1352* birth <= zbm_creation_txg), and killed between this1353* (being deleted) snapshot and the next snapshot (i.e.1354* on the next snapshot's deadlist). (Space killed before1355* this are already on our FBN.)1356*/1357uint64_t referenced, compressed, uncompressed;1358dsl_deadlist_space_range(&next->ds_deadlist,13590, dbn->dbn_phys.zbm_creation_txg,1360&referenced, &compressed, &uncompressed);1361dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=1362referenced;1363dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=1364compressed;1365dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=1366uncompressed;1367VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,1368dbn->dbn_name, sizeof (uint64_t),1369sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),1370&dbn->dbn_phys, tx));1371}1372dsl_dataset_rele(next, FTAG);13731374/*1375* There may be several bookmarks at this txg (the TXG of the1376* snapshot being deleted). We need to clear the SNAPSHOT_EXISTS1377* flag on all of them, and return TRUE if there is at least 11378* bookmark here with HAS_FBN (thus preventing the deadlist1379* key from being removed).1380*/1381boolean_t rv = B_FALSE;1382for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==1383dsl_dataset_phys(ds)->ds_creation_txg;1384dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {1385if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {1386ASSERT(!(dbn->dbn_phys.zbm_flags &1387ZBM_FLAG_SNAPSHOT_EXISTS));1388continue;1389}1390ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS);1391dbn->dbn_phys.zbm_flags &= ~ZBM_FLAG_SNAPSHOT_EXISTS;1392VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,1393dbn->dbn_name, sizeof (uint64_t),1394sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),1395&dbn->dbn_phys, tx));1396rv = B_TRUE;1397}1398dsl_dataset_rele(head, FTAG);1399return (rv);1400}14011402/*1403* A snapshot is being created of this (head) dataset.1404*1405* We don't keep keys in the deadlist for the most recent snapshot, or any1406* bookmarks at or after it, because there can't be any blocks on the1407* deadlist in this range. Now that the most recent snapshot is after1408* all bookmarks, we need to add these keys. Note that the caller always1409* adds a key at the previous snapshot, so we only add keys for bookmarks1410* after that.1411*/1412void1413dsl_bookmark_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)1414{1415uint64_t last_key_added = UINT64_MAX;1416for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);1417dbn != NULL && dbn->dbn_phys.zbm_creation_txg >1418dsl_dataset_phys(ds)->ds_prev_snap_txg;1419dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {1420uint64_t creation_txg = dbn->dbn_phys.zbm_creation_txg;1421ASSERT3U(creation_txg, <=, last_key_added);1422/*1423* Note, there may be multiple bookmarks at this TXG,1424* and we only want to add the key for this TXG once.1425* The ds_bookmarks AVL is sorted by TXG, so we will visit1426* these bookmarks in sequence.1427*/1428if ((dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) &&1429creation_txg != last_key_added) {1430dsl_deadlist_add_key(&ds->ds_deadlist,1431creation_txg, tx);1432last_key_added = creation_txg;1433}1434}1435}14361437/*1438* The next snapshot of the origin dataset has changed, due to1439* promote or clone swap. If there are any bookmarks at this dataset,1440* we need to update their zbm_*_freed_before_next_snap to reflect this.1441* The head dataset has the relevant bookmarks in ds_bookmarks.1442*/1443void1444dsl_bookmark_next_changed(dsl_dataset_t *head, dsl_dataset_t *origin,1445dmu_tx_t *tx)1446{1447dsl_pool_t *dp = dmu_tx_pool(tx);14481449/*1450* Find the first bookmark that HAS_FBN at the origin snapshot.1451*/1452dsl_bookmark_node_t search = { 0 };1453avl_index_t idx;1454search.dbn_phys.zbm_creation_txg =1455dsl_dataset_phys(origin)->ds_creation_txg;1456search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;1457/*1458* The empty-string name can't be in the AVL, and it compares1459* before any entries with this TXG.1460*/1461search.dbn_name = (char *)"";1462VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);1463dsl_bookmark_node_t *dbn =1464avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);14651466/*1467* Iterate over all bookmarks that are at the origin txg.1468* Adjust their FBN based on their new next snapshot.1469*/1470for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==1471dsl_dataset_phys(origin)->ds_creation_txg &&1472(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);1473dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {14741475/*1476* Bookmark is at the origin, therefore its1477* "next dataset" is changing, so we need1478* to reset its FBN by recomputing it in1479* dsl_bookmark_set_phys().1480*/1481ASSERT3U(dbn->dbn_phys.zbm_guid, ==,1482dsl_dataset_phys(origin)->ds_guid);1483ASSERT3U(dbn->dbn_phys.zbm_referenced_bytes_refd, ==,1484dsl_dataset_phys(origin)->ds_referenced_bytes);1485ASSERT(dbn->dbn_phys.zbm_flags &1486ZBM_FLAG_SNAPSHOT_EXISTS);1487/*1488* Save and restore the zbm_redaction_obj, which1489* is zeroed by dsl_bookmark_set_phys().1490*/1491uint64_t redaction_obj =1492dbn->dbn_phys.zbm_redaction_obj;1493dsl_bookmark_set_phys(&dbn->dbn_phys, origin);1494dbn->dbn_phys.zbm_redaction_obj = redaction_obj;14951496VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,1497dbn->dbn_name, sizeof (uint64_t),1498sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),1499&dbn->dbn_phys, tx));1500}1501}15021503/*1504* This block is no longer referenced by this (head) dataset.1505*1506* Adjust the FBN of any bookmarks that reference this block, whose "next"1507* is the head dataset.1508*/1509void1510dsl_bookmark_block_killed(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)1511{1512(void) tx;15131514/*1515* Iterate over bookmarks whose "next" is the head dataset.1516*/1517for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);1518dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=1519dsl_dataset_phys(ds)->ds_prev_snap_txg;1520dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {1521/*1522* If the block was live (referenced) at the time of this1523* bookmark, add its space to the bookmark's FBN.1524*/1525if (BP_GET_BIRTH(bp) <=1526dbn->dbn_phys.zbm_creation_txg &&1527(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {1528mutex_enter(&dbn->dbn_lock);1529dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=1530bp_get_dsize_sync(dsl_dataset_get_spa(ds), bp);1531dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=1532BP_GET_PSIZE(bp);1533dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=1534BP_GET_UCSIZE(bp);1535/*1536* Changing the ZAP object here would be too1537* expensive. Also, we may be called from the zio1538* interrupt thread, which can't block on i/o.1539* Therefore, we mark this bookmark as dirty and1540* modify the ZAP once per txg, in1541* dsl_bookmark_sync_done().1542*/1543dbn->dbn_dirty = B_TRUE;1544mutex_exit(&dbn->dbn_lock);1545}1546}1547}15481549void1550dsl_bookmark_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)1551{1552dsl_pool_t *dp = dmu_tx_pool(tx);15531554if (dsl_dataset_is_snapshot(ds))1555return;15561557/*1558* We only dirty bookmarks that are at or after the most recent1559* snapshot. We can't create snapshots between1560* dsl_bookmark_block_killed() and dsl_bookmark_sync_done(), so we1561* don't need to look at any bookmarks before ds_prev_snap_txg.1562*/1563for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);1564dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=1565dsl_dataset_phys(ds)->ds_prev_snap_txg;1566dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {1567if (dbn->dbn_dirty) {1568/*1569* We only dirty nodes with HAS_FBN, therefore1570* we can always use the current bookmark struct size.1571*/1572ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);1573VERIFY0(zap_update(dp->dp_meta_objset,1574ds->ds_bookmarks_obj,1575dbn->dbn_name, sizeof (uint64_t),1576sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),1577&dbn->dbn_phys, tx));1578dbn->dbn_dirty = B_FALSE;1579}1580}1581#ifdef ZFS_DEBUG1582for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);1583dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {1584ASSERT(!dbn->dbn_dirty);1585}1586#endif1587}15881589/*1590* Return the TXG of the most recent bookmark (or 0 if there are no bookmarks).1591*/1592uint64_t1593dsl_bookmark_latest_txg(dsl_dataset_t *ds)1594{1595ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));1596dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);1597if (dbn == NULL)1598return (0);1599return (dbn->dbn_phys.zbm_creation_txg);1600}16011602/*1603* Compare the redact_block_phys_t to the bookmark. If the last block in the1604* redact_block_phys_t is before the bookmark, return -1. If the first block in1605* the redact_block_phys_t is after the bookmark, return 1. Otherwise, the1606* bookmark is inside the range of the redact_block_phys_t, and we return 0.1607*/1608static int1609redact_block_zb_compare(redact_block_phys_t *first,1610zbookmark_phys_t *second)1611{1612/*1613* If the block_phys is for a previous object, or the last block in the1614* block_phys is strictly before the block in the bookmark, the1615* block_phys is earlier.1616*/1617if (first->rbp_object < second->zb_object ||1618(first->rbp_object == second->zb_object &&1619first->rbp_blkid + (redact_block_get_count(first) - 1) <1620second->zb_blkid)) {1621return (-1);1622}16231624/*1625* If the bookmark is for a previous object, or the block in the1626* bookmark is strictly before the first block in the block_phys, the1627* bookmark is earlier.1628*/1629if (first->rbp_object > second->zb_object ||1630(first->rbp_object == second->zb_object &&1631first->rbp_blkid > second->zb_blkid)) {1632return (1);1633}16341635return (0);1636}16371638/*1639* Traverse the redaction list in the provided object, and call the callback for1640* each entry we find. Don't call the callback for any records before resume.1641*/1642int1643dsl_redaction_list_traverse(redaction_list_t *rl, zbookmark_phys_t *resume,1644rl_traverse_callback_t cb, void *arg)1645{1646objset_t *mos = rl->rl_mos;1647int err = 0;16481649if (rl->rl_phys->rlp_last_object != UINT64_MAX ||1650rl->rl_phys->rlp_last_blkid != UINT64_MAX) {1651/*1652* When we finish a send, we update the last object and offset1653* to UINT64_MAX. If a send fails partway through, the last1654* object and offset will have some other value, indicating how1655* far the send got. The redaction list must be complete before1656* it can be traversed, so return EINVAL if the last object and1657* blkid are not set to UINT64_MAX.1658*/1659return (SET_ERROR(EINVAL));1660}16611662/*1663* This allows us to skip the binary search and resume checking logic1664* below, if we're not resuming a redacted send.1665*/1666if (ZB_IS_ZERO(resume))1667resume = NULL;16681669/*1670* Binary search for the point to resume from.1671*/1672uint64_t maxidx = rl->rl_phys->rlp_num_entries - 1;1673uint64_t minidx = 0;1674while (resume != NULL && maxidx > minidx) {1675redact_block_phys_t rbp = { 0 };1676ASSERT3U(maxidx, >, minidx);1677uint64_t mididx = minidx + ((maxidx - minidx) / 2);1678err = dmu_read(mos, rl->rl_object, mididx * sizeof (rbp),1679sizeof (rbp), &rbp, DMU_READ_NO_PREFETCH);1680if (err != 0)1681break;16821683int cmp = redact_block_zb_compare(&rbp, resume);16841685if (cmp == 0) {1686minidx = mididx;1687break;1688} else if (cmp > 0) {1689maxidx =1690(mididx == minidx ? minidx : mididx - 1);1691} else {1692minidx = mididx + 1;1693}1694}16951696unsigned int bufsize = SPA_OLD_MAXBLOCKSIZE;1697redact_block_phys_t *buf = zio_data_buf_alloc(bufsize);16981699unsigned int entries_per_buf = bufsize / sizeof (redact_block_phys_t);1700uint64_t start_block = minidx / entries_per_buf;1701err = dmu_read(mos, rl->rl_object, start_block * bufsize, bufsize, buf,1702DMU_READ_PREFETCH);17031704for (uint64_t curidx = minidx;1705err == 0 && curidx < rl->rl_phys->rlp_num_entries;1706curidx++) {1707/*1708* We read in the redaction list one block at a time. Once we1709* finish with all the entries in a given block, we read in a1710* new one. The predictive prefetcher will take care of any1711* prefetching, and this code shouldn't be the bottleneck, so we1712* don't need to do manual prefetching.1713*/1714if (curidx % entries_per_buf == 0) {1715err = dmu_read(mos, rl->rl_object, curidx *1716sizeof (*buf), bufsize, buf,1717DMU_READ_PREFETCH);1718if (err != 0)1719break;1720}1721redact_block_phys_t *rb = &buf[curidx % entries_per_buf];1722/*1723* If resume is non-null, we should either not send the data, or1724* null out resume so we don't have to keep doing these1725* comparisons.1726*/1727if (resume != NULL) {1728/*1729* It is possible that after the binary search we got1730* a record before the resume point. There's two cases1731* where this can occur. If the record is the last1732* redaction record, and the resume point is after the1733* end of the redacted data, curidx will be the last1734* redaction record. In that case, the loop will end1735* after this iteration. The second case is if the1736* resume point is between two redaction records, the1737* binary search can return either the record before1738* or after the resume point. In that case, the next1739* iteration will be greater than the resume point.1740*/1741if (redact_block_zb_compare(rb, resume) < 0) {1742ASSERT3U(curidx, ==, minidx);1743continue;1744} else {1745/*1746* If the place to resume is in the middle of1747* the range described by this1748* redact_block_phys, then modify the1749* redact_block_phys in memory so we generate1750* the right records.1751*/1752if (resume->zb_object == rb->rbp_object &&1753resume->zb_blkid > rb->rbp_blkid) {1754uint64_t diff = resume->zb_blkid -1755rb->rbp_blkid;1756rb->rbp_blkid = resume->zb_blkid;1757redact_block_set_count(rb,1758redact_block_get_count(rb) - diff);1759}1760resume = NULL;1761}1762}17631764if (cb(rb, arg) != 0) {1765err = EINTR;1766break;1767}1768}17691770zio_data_buf_free(buf, bufsize);1771return (err);1772}177317741775