178f1710Matthew Ahrens/*
278f1710Matthew Ahrens * CDDL HEADER START
378f1710Matthew Ahrens *
478f1710Matthew Ahrens * This file and its contents are supplied under the terms of the
578f1710Matthew Ahrens * Common Development and Distribution License ("CDDL"), version 1.0.
678f1710Matthew Ahrens * You may only use this file in accordance with the terms of version
778f1710Matthew Ahrens * 1.0 of the CDDL.
878f1710Matthew Ahrens *
978f1710Matthew Ahrens * A full copy of the text of the CDDL should have accompanied this
1078f1710Matthew Ahrens * source.  A copy of the CDDL is also available via the Internet at
1178f1710Matthew Ahrens * http://www.illumos.org/license/CDDL.
1278f1710Matthew Ahrens *
1378f1710Matthew Ahrens * CDDL HEADER END
1478f1710Matthew Ahrens */
151c17160Kevin Crowe
1678f1710Matthew Ahrens/*
177d46dc6Matthew Ahrens * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
181c17160Kevin Crowe * Copyright 2017 Nexenta Systems, Inc.
1978f1710Matthew Ahrens */
2078f1710Matthew Ahrens
2178f1710Matthew Ahrens#include <sys/zfs_context.h>
2278f1710Matthew Ahrens#include <sys/dsl_dataset.h>
2378f1710Matthew Ahrens#include <sys/dsl_dir.h>
2478f1710Matthew Ahrens#include <sys/dsl_prop.h>
2578f1710Matthew Ahrens#include <sys/dsl_synctask.h>
2678f1710Matthew Ahrens#include <sys/dmu_impl.h>
2778f1710Matthew Ahrens#include <sys/dmu_tx.h>
2878f1710Matthew Ahrens#include <sys/arc.h>
2978f1710Matthew Ahrens#include <sys/zap.h>
3078f1710Matthew Ahrens#include <sys/zfeature.h>
3178f1710Matthew Ahrens#include <sys/spa.h>
3278f1710Matthew Ahrens#include <sys/dsl_bookmark.h>
3378f1710Matthew Ahrens#include <zfs_namecheck.h>
3478f1710Matthew Ahrens
3578f1710Matthew Ahrensstatic int
3678f1710Matthew Ahrensdsl_bookmark_hold_ds(dsl_pool_t *dp, const char *fullname,
3778f1710Matthew Ahrens    dsl_dataset_t **dsp, void *tag, char **shortnamep)
3878f1710Matthew Ahrens{
399adfa60Matthew Ahrens	char buf[ZFS_MAX_DATASET_NAME_LEN];
4078f1710Matthew Ahrens	char *hashp;
4178f1710Matthew Ahrens
429adfa60Matthew Ahrens	if (strlen(fullname) >= ZFS_MAX_DATASET_NAME_LEN)
4378f1710Matthew Ahrens		return (SET_ERROR(ENAMETOOLONG));
4478f1710Matthew Ahrens	hashp = strchr(fullname, '#');
4578f1710Matthew Ahrens	if (hashp == NULL)
4678f1710Matthew Ahrens		return (SET_ERROR(EINVAL));
4778f1710Matthew Ahrens
4878f1710Matthew Ahrens	*shortnamep = hashp + 1;
4978f1710Matthew Ahrens	if (zfs_component_namecheck(*shortnamep, NULL, NULL))
5078f1710Matthew Ahrens		return (SET_ERROR(EINVAL));
5178f1710Matthew Ahrens	(void) strlcpy(buf, fullname, hashp - fullname + 1);
5278f1710Matthew Ahrens	return (dsl_dataset_hold(dp, buf, tag, dsp));
5378f1710Matthew Ahrens}
5478f1710Matthew Ahrens
5578f1710Matthew Ahrens/*
5678f1710Matthew Ahrens * Returns ESRCH if bookmark is not found.
5778f1710Matthew Ahrens */
5878f1710Matthew Ahrensstatic int
5978f1710Matthew Ahrensdsl_dataset_bmark_lookup(dsl_dataset_t *ds, const char *shortname,
6078f1710Matthew Ahrens    zfs_bookmark_phys_t *bmark_phys)
6178f1710Matthew Ahrens{
6278f1710Matthew Ahrens	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
6378f1710Matthew Ahrens	uint64_t bmark_zapobj = ds->ds_bookmarks;
641c17160Kevin Crowe	matchtype_t mt = 0;
6578f1710Matthew Ahrens	int err;
6678f1710Matthew Ahrens
6778f1710Matthew Ahrens	if (bmark_zapobj == 0)
6878f1710Matthew Ahrens		return (SET_ERROR(ESRCH));
6978f1710Matthew Ahrens
70c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
711c17160Kevin Crowe		mt = MT_NORMALIZE;
7278f1710Matthew Ahrens
73eb63303Tom Caputi	/*
74eb63303Tom Caputi	 * Zero out the bookmark in case the one stored on disk
75eb63303Tom Caputi	 * is in an older, shorter format.
76eb63303Tom Caputi	 */
77eb63303Tom Caputi	bzero(bmark_phys, sizeof (*bmark_phys));
78eb63303Tom Caputi
7978f1710Matthew Ahrens	err = zap_lookup_norm(mos, bmark_zapobj, shortname, sizeof (uint64_t),
8078f1710Matthew Ahrens	    sizeof (*bmark_phys) / sizeof (uint64_t), bmark_phys, mt,
8178f1710Matthew Ahrens	    NULL, 0, NULL);
8278f1710Matthew Ahrens
8378f1710Matthew Ahrens	return (err == ENOENT ? ESRCH : err);
8478f1710Matthew Ahrens}
8578f1710Matthew Ahrens
8678f1710Matthew Ahrens/*
8778f1710Matthew Ahrens * If later_ds is non-NULL, this will return EXDEV if the the specified bookmark
8878f1710Matthew Ahrens * does not represents an earlier point in later_ds's timeline.
8978f1710Matthew Ahrens *
9078f1710Matthew Ahrens * Returns ENOENT if the dataset containing the bookmark does not exist.
9178f1710Matthew Ahrens * Returns ESRCH if the dataset exists but the bookmark was not found in it.
9278f1710Matthew Ahrens */
9378f1710Matthew Ahrensint
9478f1710Matthew Ahrensdsl_bookmark_lookup(dsl_pool_t *dp, const char *fullname,
9578f1710Matthew Ahrens    dsl_dataset_t *later_ds, zfs_bookmark_phys_t *bmp)
9678f1710Matthew Ahrens{
9778f1710Matthew Ahrens	char *shortname;
9878f1710Matthew Ahrens	dsl_dataset_t *ds;
9978f1710Matthew Ahrens	int error;
10078f1710Matthew Ahrens
10178f1710Matthew Ahrens	error = dsl_bookmark_hold_ds(dp, fullname, &ds, FTAG, &shortname);
10278f1710Matthew Ahrens	if (error != 0)
10378f1710Matthew Ahrens		return (error);
10478f1710Matthew Ahrens
10578f1710Matthew Ahrens	error = dsl_dataset_bmark_lookup(ds, shortname, bmp);
10678f1710Matthew Ahrens	if (error == 0 && later_ds != NULL) {
10778f1710Matthew Ahrens		if (!dsl_dataset_is_before(later_ds, ds, bmp->zbm_creation_txg))
10878f1710Matthew Ahrens			error = SET_ERROR(EXDEV);
10978f1710Matthew Ahrens	}
11078f1710Matthew Ahrens	dsl_dataset_rele(ds, FTAG);
11178f1710Matthew Ahrens	return (error);
11278f1710Matthew Ahrens}
11378f1710Matthew Ahrens
11478f1710Matthew Ahrenstypedef struct dsl_bookmark_create_arg {
11578f1710Matthew Ahrens	nvlist_t *dbca_bmarks;
11678f1710Matthew Ahrens	nvlist_t *dbca_errors;
11778f1710Matthew Ahrens} dsl_bookmark_create_arg_t;
11878f1710Matthew Ahrens
11978f1710Matthew Ahrensstatic int
12078f1710Matthew Ahrensdsl_bookmark_create_check_impl(dsl_dataset_t *snapds, const char *bookmark_name,
12178f1710Matthew Ahrens    dmu_tx_t *tx)
12278f1710Matthew Ahrens{
12378f1710Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
12478f1710Matthew Ahrens	dsl_dataset_t *bmark_fs;
12578f1710Matthew Ahrens	char *shortname;
12678f1710Matthew Ahrens	int error;
12778f1710Matthew Ahrens	zfs_bookmark_phys_t bmark_phys;
12878f1710Matthew Ahrens
129bc9014eJustin Gibbs	if (!snapds->ds_is_snapshot)
13078f1710Matthew Ahrens		return (SET_ERROR(EINVAL));
13178f1710Matthew Ahrens
13278f1710Matthew Ahrens	error = dsl_bookmark_hold_ds(dp, bookmark_name,
13378f1710Matthew Ahrens	    &bmark_fs, FTAG, &shortname);
13478f1710Matthew Ahrens	if (error != 0)
13578f1710Matthew Ahrens		return (error);
13678f1710Matthew Ahrens
13778f1710Matthew Ahrens	if (!dsl_dataset_is_before(bmark_fs, snapds, 0)) {
13878f1710Matthew Ahrens		dsl_dataset_rele(bmark_fs, FTAG);
13978f1710Matthew Ahrens		return (SET_ERROR(EINVAL));
14078f1710Matthew Ahrens	}
14178f1710Matthew Ahrens
14278f1710Matthew Ahrens	error = dsl_dataset_bmark_lookup(bmark_fs, shortname,
14378f1710Matthew Ahrens	    &bmark_phys);
14478f1710Matthew Ahrens	dsl_dataset_rele(bmark_fs, FTAG);
14578f1710Matthew Ahrens	if (error == 0)
14678f1710Matthew Ahrens		return (SET_ERROR(EEXIST));
14778f1710Matthew Ahrens	if (error == ESRCH)
14878f1710Matthew Ahrens		return (0);
14978f1710Matthew Ahrens	return (error);
15078f1710Matthew Ahrens}
15178f1710Matthew Ahrens
15278f1710Matthew Ahrensstatic int
15378f1710Matthew Ahrensdsl_bookmark_create_check(void *arg, dmu_tx_t *tx)
15478f1710Matthew Ahrens{
15578f1710Matthew Ahrens	dsl_bookmark_create_arg_t *dbca = arg;
15678f1710Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
15778f1710Matthew Ahrens	int rv = 0;
15878f1710Matthew Ahrens
15978f1710Matthew Ahrens	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
16078f1710Matthew Ahrens		return (SET_ERROR(ENOTSUP));
16178f1710Matthew Ahrens
16278f1710Matthew Ahrens	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
16378f1710Matthew Ahrens	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
16478f1710Matthew Ahrens		dsl_dataset_t *snapds;
16578f1710Matthew Ahrens		int error;
16678f1710Matthew Ahrens
16778f1710Matthew Ahrens		/* note: validity of nvlist checked by ioctl layer */
16878f1710Matthew Ahrens		error = dsl_dataset_hold(dp, fnvpair_value_string(pair),
16978f1710Matthew Ahrens		    FTAG, &snapds);
17078f1710Matthew Ahrens		if (error == 0) {
17178f1710Matthew Ahrens			error = dsl_bookmark_create_check_impl(snapds,
17278f1710Matthew Ahrens			    nvpair_name(pair), tx);
17378f1710Matthew Ahrens			dsl_dataset_rele(snapds, FTAG);
17478f1710Matthew Ahrens		}
17578f1710Matthew Ahrens		if (error != 0) {
17678f1710Matthew Ahrens			fnvlist_add_int32(dbca->dbca_errors,
17778f1710Matthew Ahrens			    nvpair_name(pair), error);
17878f1710Matthew Ahrens			rv = error;
17978f1710Matthew Ahrens		}
18078f1710Matthew Ahrens	}
18178f1710Matthew Ahrens
18278f1710Matthew Ahrens	return (rv);
18378f1710Matthew Ahrens}
18478f1710Matthew Ahrens
18578f1710Matthew Ahrensstatic void
18678f1710Matthew Ahrensdsl_bookmark_create_sync(void *arg, dmu_tx_t *tx)
18778f1710Matthew Ahrens{
18878f1710Matthew Ahrens	dsl_bookmark_create_arg_t *dbca = arg;
18978f1710Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
19078f1710Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
19178f1710Matthew Ahrens
19278f1710Matthew Ahrens	ASSERT(spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS));
19378f1710Matthew Ahrens
19478f1710Matthew Ahrens	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
19578f1710Matthew Ahrens	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
19678f1710Matthew Ahrens		dsl_dataset_t *snapds, *bmark_fs;
197eb63303Tom Caputi		zfs_bookmark_phys_t bmark_phys = { 0 };
19878f1710Matthew Ahrens		char *shortname;
199eb63303Tom Caputi		uint32_t bmark_len = BOOKMARK_PHYS_SIZE_V1;
20078f1710Matthew Ahrens
20178f1710Matthew Ahrens		VERIFY0(dsl_dataset_hold(dp, fnvpair_value_string(pair),
20278f1710Matthew Ahrens		    FTAG, &snapds));
20378f1710Matthew Ahrens		VERIFY0(dsl_bookmark_hold_ds(dp, nvpair_name(pair),
20478f1710Matthew Ahrens		    &bmark_fs, FTAG, &shortname));
20578f1710Matthew Ahrens		if (bmark_fs->ds_bookmarks == 0) {
20678f1710Matthew Ahrens			bmark_fs->ds_bookmarks =
20778f1710Matthew Ahrens			    zap_create_norm(mos, U8_TEXTPREP_TOUPPER,
20878f1710Matthew Ahrens			    DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
20978f1710Matthew Ahrens			spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
21078f1710Matthew Ahrens
21178f1710Matthew Ahrens			dsl_dataset_zapify(bmark_fs, tx);
21278f1710Matthew Ahrens			VERIFY0(zap_add(mos, bmark_fs->ds_object,
21378f1710Matthew Ahrens			    DS_FIELD_BOOKMARK_NAMES,
21478f1710Matthew Ahrens			    sizeof (bmark_fs->ds_bookmarks), 1,
21578f1710Matthew Ahrens			    &bmark_fs->ds_bookmarks, tx));
21678f1710Matthew Ahrens		}
21778f1710Matthew Ahrens
218c137962Justin T. Gibbs		bmark_phys.zbm_guid = dsl_dataset_phys(snapds)->ds_guid;
219c137962Justin T. Gibbs		bmark_phys.zbm_creation_txg =
220c137962Justin T. Gibbs		    dsl_dataset_phys(snapds)->ds_creation_txg;
22178f1710Matthew Ahrens		bmark_phys.zbm_creation_time =
222c137962Justin T. Gibbs		    dsl_dataset_phys(snapds)->ds_creation_time;
22378f1710Matthew Ahrens
224eb63303Tom Caputi		/*
225eb63303Tom Caputi		 * If the dataset is encrypted create a larger bookmark to
226eb63303Tom Caputi		 * accommodate the IVset guid. The IVset guid was added
227eb63303Tom Caputi		 * after the encryption feature to prevent a problem with
228eb63303Tom Caputi		 * raw sends. If we encounter an encrypted dataset without
229eb63303Tom Caputi		 * an IVset guid we fall back to a normal bookmark.
230eb63303Tom Caputi		 */
231eb63303Tom Caputi		if (snapds->ds_dir->dd_crypto_obj != 0 &&
232eb63303Tom Caputi		    spa_feature_is_enabled(dp->dp_spa,
233eb63303Tom Caputi		    SPA_FEATURE_BOOKMARK_V2)) {
234eb63303Tom Caputi			int err = zap_lookup(mos, snapds->ds_object,
235eb63303Tom Caputi			    DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
236eb63303Tom Caputi			    &bmark_phys.zbm_ivset_guid);
237eb63303Tom Caputi			if (err == 0) {
238eb63303Tom Caputi				bmark_len = BOOKMARK_PHYS_SIZE_V2;
239eb63303Tom Caputi				spa_feature_incr(dp->dp_spa,
240eb63303Tom Caputi				    SPA_FEATURE_BOOKMARK_V2, tx);
241eb63303Tom Caputi			}
242eb63303Tom Caputi		}
243eb63303Tom Caputi
24478f1710Matthew Ahrens		VERIFY0(zap_add(mos, bmark_fs->ds_bookmarks,
24578f1710Matthew Ahrens		    shortname, sizeof (uint64_t),
246eb63303Tom Caputi		    bmark_len / sizeof (uint64_t), &bmark_phys, tx));
24778f1710Matthew Ahrens
24878f1710Matthew Ahrens		spa_history_log_internal_ds(bmark_fs, "bookmark", tx,
24978f1710Matthew Ahrens		    "name=%s creation_txg=%llu target_snap=%llu",
25078f1710Matthew Ahrens		    shortname,
25178f1710Matthew Ahrens		    (longlong_t)bmark_phys.zbm_creation_txg,
25278f1710Matthew Ahrens		    (longlong_t)snapds->ds_object);
25378f1710Matthew Ahrens
25478f1710Matthew Ahrens		dsl_dataset_rele(bmark_fs, FTAG);
25578f1710Matthew Ahrens		dsl_dataset_rele(snapds, FTAG);
25678f1710Matthew Ahrens	}
25778f1710Matthew Ahrens}
25878f1710Matthew Ahrens
25978f1710Matthew Ahrens/*
26078f1710Matthew Ahrens * The bookmarks must all be in the same pool.
26178f1710Matthew Ahrens */
26278f1710Matthew Ahrensint
26378f1710Matthew Ahrensdsl_bookmark_create(nvlist_t *bmarks, nvlist_t *errors)
26478f1710Matthew Ahrens{
26578f1710Matthew Ahrens	nvpair_t *pair;
26678f1710Matthew Ahrens	dsl_bookmark_create_arg_t dbca;
26778f1710Matthew Ahrens
26878f1710Matthew Ahrens	pair = nvlist_next_nvpair(bmarks, NULL);
26978f1710Matthew Ahrens	if (pair == NULL)
27078f1710Matthew Ahrens		return (0);
27178f1710Matthew Ahrens
27278f1710Matthew Ahrens	dbca.dbca_bmarks = bmarks;
27378f1710Matthew Ahrens	dbca.dbca_errors = errors;
27478f1710Matthew Ahrens
27578f1710Matthew Ahrens	return (dsl_sync_task(nvpair_name(pair), dsl_bookmark_create_check,
2767d46dc6Matthew Ahrens	    dsl_bookmark_create_sync, &dbca,
2777d46dc6Matthew Ahrens	    fnvlist_num_pairs(bmarks), ZFS_SPACE_CHECK_NORMAL));
27878f1710Matthew Ahrens}
27978f1710Matthew Ahrens
28078f1710Matthew Ahrensint
28178f1710Matthew Ahrensdsl_get_bookmarks_impl(dsl_dataset_t *ds, nvlist_t *props, nvlist_t *outnvl)
28278f1710Matthew Ahrens{
28378f1710Matthew Ahrens	int err = 0;
28478f1710Matthew Ahrens	zap_cursor_t zc;
28578f1710Matthew Ahrens	zap_attribute_t attr;
28678f1710Matthew Ahrens	dsl_pool_t *dp = ds->ds_dir->dd_pool;
28778f1710Matthew Ahrens
28878f1710Matthew Ahrens	uint64_t bmark_zapobj = ds->ds_bookmarks;
28978f1710Matthew Ahrens	if (bmark_zapobj == 0)
29078f1710Matthew Ahrens		return (0);
29178f1710Matthew Ahrens
29278f1710Matthew Ahrens	for (zap_cursor_init(&zc, dp->dp_meta_objset, bmark_zapobj);
29378f1710Matthew Ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
29478f1710Matthew Ahrens	    zap_cursor_advance(&zc)) {
29578f1710Matthew Ahrens		char *bmark_name = attr.za_name;
296eb63303Tom Caputi		zfs_bookmark_phys_t bmark_phys = { 0 };
29778f1710Matthew Ahrens
29878f1710Matthew Ahrens		err = dsl_dataset_bmark_lookup(ds, bmark_name, &bmark_phys);
29978f1710Matthew Ahrens		ASSERT3U(err, !=, ENOENT);
30078f1710Matthew Ahrens		if (err != 0)
30178f1710Matthew Ahrens			break;
30278f1710Matthew Ahrens
30378f1710Matthew Ahrens		nvlist_t *out_props = fnvlist_alloc();
30478f1710Matthew Ahrens		if (nvlist_exists(props,
30578f1710Matthew Ahrens		    zfs_prop_to_name(ZFS_PROP_GUID))) {
30678f1710Matthew Ahrens			dsl_prop_nvlist_add_uint64(out_props,
30778f1710Matthew Ahrens			    ZFS_PROP_GUID, bmark_phys.zbm_guid);
30878f1710Matthew Ahrens		}
30978f1710Matthew Ahrens		if (nvlist_exists(props,
31078f1710Matthew Ahrens		    zfs_prop_to_name(ZFS_PROP_CREATETXG))) {
31178f1710Matthew Ahrens			dsl_prop_nvlist_add_uint64(out_props,
31278f1710Matthew Ahrens			    ZFS_PROP_CREATETXG, bmark_phys.zbm_creation_txg);
31378f1710Matthew Ahrens		}
31478f1710Matthew Ahrens		if (nvlist_exists(props,
31578f1710Matthew Ahrens		    zfs_prop_to_name(ZFS_PROP_CREATION))) {
31678f1710Matthew Ahrens			dsl_prop_nvlist_add_uint64(out_props,
31778f1710Matthew Ahrens			    ZFS_PROP_CREATION, bmark_phys.zbm_creation_time);
31878f1710Matthew Ahrens		}
319eb63303Tom Caputi		if (nvlist_exists(props,
320eb63303Tom Caputi		    zfs_prop_to_name(ZFS_PROP_IVSET_GUID))) {
321eb63303Tom Caputi			dsl_prop_nvlist_add_uint64(out_props,
322eb63303Tom Caputi			    ZFS_PROP_IVSET_GUID, bmark_phys.zbm_ivset_guid);
323eb63303Tom Caputi		}
32478f1710Matthew Ahrens
32578f1710Matthew Ahrens		fnvlist_add_nvlist(outnvl, bmark_name, out_props);
32678f1710Matthew Ahrens		fnvlist_free(out_props);
32778f1710Matthew Ahrens	}
32878f1710Matthew Ahrens	zap_cursor_fini(&zc);
32978f1710Matthew Ahrens	return (err);
33078f1710Matthew Ahrens}
33178f1710Matthew Ahrens
33278f1710Matthew Ahrens/*
33378f1710Matthew Ahrens * Retrieve the bookmarks that exist in the specified dataset, and the
33478f1710Matthew Ahrens * requested properties of each bookmark.
33578f1710Matthew Ahrens *
33678f1710Matthew Ahrens * The "props" nvlist specifies which properties are requested.
33778f1710Matthew Ahrens * See lzc_get_bookmarks() for the list of valid properties.
33878f1710Matthew Ahrens */
33978f1710Matthew Ahrensint
34078f1710Matthew Ahrensdsl_get_bookmarks(const char *dsname, nvlist_t *props, nvlist_t *outnvl)
34178f1710Matthew Ahrens{
34278f1710Matthew Ahrens	dsl_pool_t *dp;
34378f1710Matthew Ahrens	dsl_dataset_t *ds;
34478f1710Matthew Ahrens	int err;
34578f1710Matthew Ahrens
34678f1710Matthew Ahrens	err = dsl_pool_hold(dsname, FTAG, &dp);
34778f1710Matthew Ahrens	if (err != 0)
34878f1710Matthew Ahrens		return (err);
34978f1710Matthew Ahrens	err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
35078f1710Matthew Ahrens	if (err != 0) {
35178f1710Matthew Ahrens		dsl_pool_rele(dp, FTAG);
35278f1710Matthew Ahrens		return (err);
35378f1710Matthew Ahrens	}
35478f1710Matthew Ahrens
35578f1710Matthew Ahrens	err = dsl_get_bookmarks_impl(ds, props, outnvl);
35678f1710Matthew Ahrens
35778f1710Matthew Ahrens	dsl_dataset_rele(ds, FTAG);
35878f1710Matthew Ahrens	dsl_pool_rele(dp, FTAG);
35978f1710Matthew Ahrens	return (err);
36078f1710Matthew Ahrens}
36178f1710Matthew Ahrens
36278f1710Matthew Ahrenstypedef struct dsl_bookmark_destroy_arg {
36378f1710Matthew Ahrens	nvlist_t *dbda_bmarks;
36478f1710Matthew Ahrens	nvlist_t *dbda_success;
36578f1710Matthew Ahrens	nvlist_t *dbda_errors;
36678f1710Matthew Ahrens} dsl_bookmark_destroy_arg_t;
36778f1710Matthew Ahrens
36878f1710Matthew Ahrensstatic int
36978f1710Matthew Ahrensdsl_dataset_bookmark_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
37078f1710Matthew Ahrens{
371eb63303Tom Caputi	int err;
37278f1710Matthew Ahrens	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
37378f1710Matthew Ahrens	uint64_t bmark_zapobj = ds->ds_bookmarks;
3741c17160Kevin Crowe	matchtype_t mt = 0;
375eb63303Tom Caputi	uint64_t int_size, num_ints;
37678f1710Matthew Ahrens
377c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
3781c17160Kevin Crowe		mt = MT_NORMALIZE;
37978f1710Matthew Ahrens
380eb63303Tom Caputi	err = zap_length(mos, bmark_zapobj, name, &int_size, &num_ints);
381eb63303Tom Caputi	if (err != 0)
382eb63303Tom Caputi		return (err);
383eb63303Tom Caputi
384eb63303Tom Caputi	ASSERT3U(int_size, ==, sizeof (uint64_t));
385eb63303Tom Caputi
386eb63303Tom Caputi	if (num_ints * int_size > BOOKMARK_PHYS_SIZE_V1) {
387eb63303Tom Caputi		spa_feature_decr(dmu_objset_spa(mos),
388eb63303Tom Caputi		    SPA_FEATURE_BOOKMARK_V2, tx);
389eb63303Tom Caputi	}
390eb63303Tom Caputi
39178f1710Matthew Ahrens	return (zap_remove_norm(mos, bmark_zapobj, name, mt, tx));
39278f1710Matthew Ahrens}
39378f1710Matthew Ahrens
39478f1710Matthew Ahrensstatic int
39578f1710Matthew Ahrensdsl_bookmark_destroy_check(void *arg, dmu_tx_t *tx)
39678f1710Matthew Ahrens{
39778f1710Matthew Ahrens	dsl_bookmark_destroy_arg_t *dbda = arg;
39878f1710Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
39978f1710Matthew Ahrens	int rv = 0;
40078f1710Matthew Ahrens
40142418f9Matthew Ahrens	ASSERT(nvlist_empty(dbda->dbda_success));
40242418f9Matthew Ahrens	ASSERT(nvlist_empty(dbda->dbda_errors));
40342418f9Matthew Ahrens
40478f1710Matthew Ahrens	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
40578f1710Matthew Ahrens		return (0);
40678f1710Matthew Ahrens
40778f1710Matthew Ahrens	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_bmarks, NULL);
40878f1710Matthew Ahrens	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_bmarks, pair)) {
40978f1710Matthew Ahrens		const char *fullname = nvpair_name(pair);
41078f1710Matthew Ahrens		dsl_dataset_t *ds;
41178f1710Matthew Ahrens		zfs_bookmark_phys_t bm;
41278f1710Matthew Ahrens		int error;
41378f1710Matthew Ahrens		char *shortname;
41478f1710Matthew Ahrens
41578f1710Matthew Ahrens		error = dsl_bookmark_hold_ds(dp, fullname, &ds,
41678f1710Matthew Ahrens		    FTAG, &shortname);
41778f1710Matthew Ahrens		if (error == ENOENT) {
41878f1710Matthew Ahrens			/* ignore it; the bookmark is "already destroyed" */
41978f1710Matthew Ahrens			continue;
42078f1710Matthew Ahrens		}
42178f1710Matthew Ahrens		if (error == 0) {
42278f1710Matthew Ahrens			error = dsl_dataset_bmark_lookup(ds, shortname, &bm);
42378f1710Matthew Ahrens			dsl_dataset_rele(ds, FTAG);
42478f1710Matthew Ahrens			if (error == ESRCH) {
42578f1710Matthew Ahrens				/*
42678f1710Matthew Ahrens				 * ignore it; the bookmark is
42778f1710Matthew Ahrens				 * "already destroyed"
42878f1710Matthew Ahrens				 */
42978f1710Matthew Ahrens				continue;
43078f1710Matthew Ahrens			}
43178f1710Matthew Ahrens		}
43278f1710Matthew Ahrens		if (error == 0) {
43342418f9Matthew Ahrens			if (dmu_tx_is_syncing(tx)) {
43442418f9Matthew Ahrens				fnvlist_add_boolean(dbda->dbda_success,
43542418f9Matthew Ahrens				    fullname);
43642418f9Matthew Ahrens			}
43778f1710Matthew Ahrens		} else {
43878f1710Matthew Ahrens			fnvlist_add_int32(dbda->dbda_errors, fullname, error);
43978f1710Matthew Ahrens			rv = error;
44078f1710Matthew Ahrens		}
44178f1710Matthew Ahrens	}
44278f1710Matthew Ahrens	return (rv);
44378f1710Matthew Ahrens}
44478f1710Matthew Ahrens
44578f1710Matthew Ahrensstatic void
44678f1710Matthew Ahrensdsl_bookmark_destroy_sync(void *arg, dmu_tx_t *tx)
44778f1710Matthew Ahrens{
44878f1710Matthew Ahrens	dsl_bookmark_destroy_arg_t *dbda = arg;
44978f1710Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
45078f1710Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
45178f1710Matthew Ahrens
45278f1710Matthew Ahrens	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_success, NULL);
45378f1710Matthew Ahrens	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_success, pair)) {
45478f1710Matthew Ahrens		dsl_dataset_t *ds;
45578f1710Matthew Ahrens		char *shortname;
45678f1710Matthew Ahrens		uint64_t zap_cnt;
45778f1710Matthew Ahrens
45878f1710Matthew Ahrens		VERIFY0(dsl_bookmark_hold_ds(dp, nvpair_name(pair),
45978f1710Matthew Ahrens		    &ds, FTAG, &shortname));
46078f1710Matthew Ahrens		VERIFY0(dsl_dataset_bookmark_remove(ds, shortname, tx));
46178f1710Matthew Ahrens
46278f1710Matthew Ahrens		/*
46378f1710Matthew Ahrens		 * If all of this dataset's bookmarks have been destroyed,
46478f1710Matthew Ahrens		 * free the zap object and decrement the feature's use count.
46578f1710Matthew Ahrens		 */
46678f1710Matthew Ahrens		VERIFY0(zap_count(mos, ds->ds_bookmarks,
46778f1710Matthew Ahrens		    &zap_cnt));
46878f1710Matthew Ahrens		if (zap_cnt == 0) {
46978f1710Matthew Ahrens			dmu_buf_will_dirty(ds->ds_dbuf, tx);
47078f1710Matthew Ahrens			VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
47178f1710Matthew Ahrens			ds->ds_bookmarks = 0;
47278f1710Matthew Ahrens			spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
47378f1710Matthew Ahrens			VERIFY0(zap_remove(mos, ds->ds_object,
47478f1710Matthew Ahrens			    DS_FIELD_BOOKMARK_NAMES, tx));
47578f1710Matthew Ahrens		}
47678f1710Matthew Ahrens
47778f1710Matthew Ahrens		spa_history_log_internal_ds(ds, "remove bookmark", tx,
47878f1710Matthew Ahrens		    "name=%s", shortname);
47978f1710Matthew Ahrens
48078f1710Matthew Ahrens		dsl_dataset_rele(ds, FTAG);
48178f1710Matthew Ahrens	}
48278f1710Matthew Ahrens}
48378f1710Matthew Ahrens
48478f1710Matthew Ahrens/*
48578f1710Matthew Ahrens * The bookmarks must all be in the same pool.
48678f1710Matthew Ahrens */
48778f1710Matthew Ahrensint
48878f1710Matthew Ahrensdsl_bookmark_destroy(nvlist_t *bmarks, nvlist_t *errors)
48978f1710Matthew Ahrens{
49078f1710Matthew Ahrens	int rv;
49178f1710Matthew Ahrens	dsl_bookmark_destroy_arg_t dbda;
49278f1710Matthew Ahrens	nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
49378f1710Matthew Ahrens	if (pair == NULL)
49478f1710Matthew Ahrens		return (0);
49578f1710Matthew Ahrens
49678f1710Matthew Ahrens	dbda.dbda_bmarks = bmarks;
49778f1710Matthew Ahrens	dbda.dbda_errors = errors;
49878f1710Matthew Ahrens	dbda.dbda_success = fnvlist_alloc();
49978f1710Matthew Ahrens
50078f1710Matthew Ahrens	rv = dsl_sync_task(nvpair_name(pair), dsl_bookmark_destroy_check,
5017d46dc6Matthew Ahrens	    dsl_bookmark_destroy_sync, &dbda, fnvlist_num_pairs(bmarks),
5027d46dc6Matthew Ahrens	    ZFS_SPACE_CHECK_RESERVED);
50378f1710Matthew Ahrens	fnvlist_free(dbda.dbda_success);
50478f1710Matthew Ahrens	return (rv);
50578f1710Matthew Ahrens}
506