xref: /freebsd/sys/contrib/openzfs/module/zfs/dsl_bookmark.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
6eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
7eda14cbcSMatt Macy  * 1.0 of the CDDL.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
10eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
11eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * CDDL HEADER END
14eda14cbcSMatt Macy  */
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
18eda14cbcSMatt Macy  * Copyright 2017 Nexenta Systems, Inc.
19eda14cbcSMatt Macy  * Copyright 2019, 2020 by Christian Schwarz. All rights reserved.
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy #include <sys/zfs_context.h>
23eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
24eda14cbcSMatt Macy #include <sys/dsl_dir.h>
25eda14cbcSMatt Macy #include <sys/dsl_prop.h>
26eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
27eda14cbcSMatt Macy #include <sys/dsl_destroy.h>
28eda14cbcSMatt Macy #include <sys/dmu_impl.h>
29eda14cbcSMatt Macy #include <sys/dmu_tx.h>
30eda14cbcSMatt Macy #include <sys/arc.h>
31eda14cbcSMatt Macy #include <sys/zap.h>
32eda14cbcSMatt Macy #include <sys/zfeature.h>
33eda14cbcSMatt Macy #include <sys/spa.h>
34eda14cbcSMatt Macy #include <sys/dsl_bookmark.h>
35eda14cbcSMatt Macy #include <zfs_namecheck.h>
36eda14cbcSMatt Macy #include <sys/dmu_send.h>
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy static int
39eda14cbcSMatt Macy dsl_bookmark_hold_ds(dsl_pool_t *dp, const char *fullname,
40a0b956f5SMartin Matuska     dsl_dataset_t **dsp, const void *tag, char **shortnamep)
41eda14cbcSMatt Macy {
42eda14cbcSMatt Macy 	char buf[ZFS_MAX_DATASET_NAME_LEN];
43eda14cbcSMatt Macy 	char *hashp;
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy 	if (strlen(fullname) >= ZFS_MAX_DATASET_NAME_LEN)
46eda14cbcSMatt Macy 		return (SET_ERROR(ENAMETOOLONG));
47eda14cbcSMatt Macy 	hashp = strchr(fullname, '#');
48eda14cbcSMatt Macy 	if (hashp == NULL)
49eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy 	*shortnamep = hashp + 1;
52eda14cbcSMatt Macy 	if (zfs_component_namecheck(*shortnamep, NULL, NULL))
53eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
54eda14cbcSMatt Macy 	(void) strlcpy(buf, fullname, hashp - fullname + 1);
55eda14cbcSMatt Macy 	return (dsl_dataset_hold(dp, buf, tag, dsp));
56eda14cbcSMatt Macy }
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy /*
59eda14cbcSMatt Macy  * When reading BOOKMARK_V1 bookmarks, the BOOKMARK_V2 fields are guaranteed
60eda14cbcSMatt Macy  * to be zeroed.
61eda14cbcSMatt Macy  *
62eda14cbcSMatt Macy  * Returns ESRCH if bookmark is not found.
63eda14cbcSMatt Macy  * Note, we need to use the ZAP rather than the AVL to look up bookmarks
64eda14cbcSMatt Macy  * by name, because only the ZAP honors the casesensitivity setting.
65eda14cbcSMatt Macy  */
66eda14cbcSMatt Macy int
67eda14cbcSMatt Macy dsl_bookmark_lookup_impl(dsl_dataset_t *ds, const char *shortname,
68eda14cbcSMatt Macy     zfs_bookmark_phys_t *bmark_phys)
69eda14cbcSMatt Macy {
70eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
71eda14cbcSMatt Macy 	uint64_t bmark_zapobj = ds->ds_bookmarks_obj;
72eda14cbcSMatt Macy 	matchtype_t mt = 0;
73eda14cbcSMatt Macy 	int err;
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy 	if (bmark_zapobj == 0)
76eda14cbcSMatt Macy 		return (SET_ERROR(ESRCH));
77eda14cbcSMatt Macy 
78eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
79eda14cbcSMatt Macy 		mt = MT_NORMALIZE;
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	/*
82eda14cbcSMatt Macy 	 * Zero out the bookmark in case the one stored on disk
83eda14cbcSMatt Macy 	 * is in an older, shorter format.
84eda14cbcSMatt Macy 	 */
85da5137abSMartin Matuska 	memset(bmark_phys, 0, sizeof (*bmark_phys));
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy 	err = zap_lookup_norm(mos, bmark_zapobj, shortname, sizeof (uint64_t),
88eda14cbcSMatt Macy 	    sizeof (*bmark_phys) / sizeof (uint64_t), bmark_phys, mt, NULL, 0,
89eda14cbcSMatt Macy 	    NULL);
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 	return (err == ENOENT ? SET_ERROR(ESRCH) : err);
92eda14cbcSMatt Macy }
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy /*
95eda14cbcSMatt Macy  * If later_ds is non-NULL, this will return EXDEV if the specified bookmark
96eda14cbcSMatt Macy  * does not represents an earlier point in later_ds's timeline.  However,
97eda14cbcSMatt Macy  * bmp will still be filled in if we return EXDEV.
98eda14cbcSMatt Macy  *
99eda14cbcSMatt Macy  * Returns ENOENT if the dataset containing the bookmark does not exist.
100eda14cbcSMatt Macy  * Returns ESRCH if the dataset exists but the bookmark was not found in it.
101eda14cbcSMatt Macy  */
102eda14cbcSMatt Macy int
103eda14cbcSMatt Macy dsl_bookmark_lookup(dsl_pool_t *dp, const char *fullname,
104eda14cbcSMatt Macy     dsl_dataset_t *later_ds, zfs_bookmark_phys_t *bmp)
105eda14cbcSMatt Macy {
106eda14cbcSMatt Macy 	char *shortname;
107eda14cbcSMatt Macy 	dsl_dataset_t *ds;
108eda14cbcSMatt Macy 	int error;
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 	error = dsl_bookmark_hold_ds(dp, fullname, &ds, FTAG, &shortname);
111eda14cbcSMatt Macy 	if (error != 0)
112eda14cbcSMatt Macy 		return (error);
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy 	error = dsl_bookmark_lookup_impl(ds, shortname, bmp);
115eda14cbcSMatt Macy 	if (error == 0 && later_ds != NULL) {
116eda14cbcSMatt Macy 		if (!dsl_dataset_is_before(later_ds, ds, bmp->zbm_creation_txg))
117eda14cbcSMatt Macy 			error = SET_ERROR(EXDEV);
118eda14cbcSMatt Macy 	}
119eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
120eda14cbcSMatt Macy 	return (error);
121eda14cbcSMatt Macy }
122eda14cbcSMatt Macy 
123eda14cbcSMatt Macy /*
124eda14cbcSMatt Macy  * Validates that
125eda14cbcSMatt Macy  * - bmark is a full dataset path of a bookmark (bookmark_namecheck)
126eda14cbcSMatt Macy  * - source is a full path of a snapshot or bookmark
127eda14cbcSMatt Macy  *   ({bookmark,snapshot}_namecheck)
128eda14cbcSMatt Macy  *
129eda14cbcSMatt Macy  * Returns 0 if valid, -1 otherwise.
130eda14cbcSMatt Macy  */
131eda14cbcSMatt Macy static int
132eda14cbcSMatt Macy dsl_bookmark_create_nvl_validate_pair(const char *bmark, const char *source)
133eda14cbcSMatt Macy {
134eda14cbcSMatt Macy 	if (bookmark_namecheck(bmark, NULL, NULL) != 0)
135eda14cbcSMatt Macy 		return (-1);
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy 	int is_bmark, is_snap;
138eda14cbcSMatt Macy 	is_bmark = bookmark_namecheck(source, NULL, NULL) == 0;
139eda14cbcSMatt Macy 	is_snap = snapshot_namecheck(source, NULL, NULL) == 0;
140eda14cbcSMatt Macy 	if (!is_bmark && !is_snap)
141eda14cbcSMatt Macy 		return (-1);
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy 	return (0);
144eda14cbcSMatt Macy }
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy /*
147eda14cbcSMatt Macy  * Check that the given nvlist corresponds to the following schema:
148eda14cbcSMatt Macy  *  { newbookmark -> source, ... }
149eda14cbcSMatt Macy  * where
150eda14cbcSMatt Macy  * - each pair passes dsl_bookmark_create_nvl_validate_pair
151eda14cbcSMatt Macy  * - all newbookmarks are in the same pool
152eda14cbcSMatt Macy  * - all newbookmarks have unique names
153eda14cbcSMatt Macy  *
154eda14cbcSMatt Macy  * Note that this function is only validates above schema. Callers must ensure
155eda14cbcSMatt Macy  * that the bookmarks can be created, e.g. that sources exist.
156eda14cbcSMatt Macy  *
157eda14cbcSMatt Macy  * Returns 0 if the nvlist adheres to above schema.
158eda14cbcSMatt Macy  * Returns -1 if it doesn't.
159eda14cbcSMatt Macy  */
160eda14cbcSMatt Macy int
161eda14cbcSMatt Macy dsl_bookmark_create_nvl_validate(nvlist_t *bmarks)
162eda14cbcSMatt Macy {
163*2a58b312SMartin Matuska 	const char *first = NULL;
164da5137abSMartin Matuska 	size_t first_len = 0;
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy 	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
167eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
168eda14cbcSMatt Macy 
169*2a58b312SMartin Matuska 		const char *bmark = nvpair_name(pair);
170*2a58b312SMartin Matuska 		const char *source;
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 		/* list structure: values must be snapshots XOR bookmarks */
173eda14cbcSMatt Macy 		if (nvpair_value_string(pair, &source) != 0)
174eda14cbcSMatt Macy 			return (-1);
175eda14cbcSMatt Macy 		if (dsl_bookmark_create_nvl_validate_pair(bmark, source) != 0)
176eda14cbcSMatt Macy 			return (-1);
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 		/* same pool check */
179eda14cbcSMatt Macy 		if (first == NULL) {
180*2a58b312SMartin Matuska 			const char *cp = strpbrk(bmark, "/#");
181eda14cbcSMatt Macy 			if (cp == NULL)
182eda14cbcSMatt Macy 				return (-1);
183eda14cbcSMatt Macy 			first = bmark;
184eda14cbcSMatt Macy 			first_len = cp - bmark;
185eda14cbcSMatt Macy 		}
186eda14cbcSMatt Macy 		if (strncmp(first, bmark, first_len) != 0)
187eda14cbcSMatt Macy 			return (-1);
188eda14cbcSMatt Macy 		switch (*(bmark + first_len)) {
189eda14cbcSMatt Macy 			case '/': /* fallthrough */
190eda14cbcSMatt Macy 			case '#':
191eda14cbcSMatt Macy 				break;
192eda14cbcSMatt Macy 			default:
193eda14cbcSMatt Macy 				return (-1);
194eda14cbcSMatt Macy 		}
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy 		/* unique newbookmark names; todo: O(n^2) */
197eda14cbcSMatt Macy 		for (nvpair_t *pair2 = nvlist_next_nvpair(bmarks, pair);
198eda14cbcSMatt Macy 		    pair2 != NULL; pair2 = nvlist_next_nvpair(bmarks, pair2)) {
199eda14cbcSMatt Macy 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
200eda14cbcSMatt Macy 				return (-1);
201eda14cbcSMatt Macy 		}
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 	}
204eda14cbcSMatt Macy 	return (0);
205eda14cbcSMatt Macy }
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy /*
208eda14cbcSMatt Macy  * expects that newbm and source have been validated using
209eda14cbcSMatt Macy  * dsl_bookmark_create_nvl_validate_pair
210eda14cbcSMatt Macy  */
211eda14cbcSMatt Macy static int
212eda14cbcSMatt Macy dsl_bookmark_create_check_impl(dsl_pool_t *dp,
213eda14cbcSMatt Macy     const char *newbm, const char *source)
214eda14cbcSMatt Macy {
215eda14cbcSMatt Macy 	ASSERT0(dsl_bookmark_create_nvl_validate_pair(newbm, source));
216eda14cbcSMatt Macy 	/* defer source namecheck until we know it's a snapshot or bookmark */
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy 	int error;
219eda14cbcSMatt Macy 	dsl_dataset_t *newbm_ds;
220eda14cbcSMatt Macy 	char *newbm_short;
221eda14cbcSMatt Macy 	zfs_bookmark_phys_t bmark_phys;
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	error = dsl_bookmark_hold_ds(dp, newbm, &newbm_ds, FTAG, &newbm_short);
224eda14cbcSMatt Macy 	if (error != 0)
225eda14cbcSMatt Macy 		return (error);
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy 	/* Verify that the new bookmark does not already exist */
228eda14cbcSMatt Macy 	error = dsl_bookmark_lookup_impl(newbm_ds, newbm_short, &bmark_phys);
229eda14cbcSMatt Macy 	switch (error) {
230eda14cbcSMatt Macy 	case ESRCH:
231eda14cbcSMatt Macy 		/* happy path: new bmark doesn't exist, proceed after switch */
232eda14cbcSMatt Macy 		break;
233eda14cbcSMatt Macy 	case 0:
234eda14cbcSMatt Macy 		error = SET_ERROR(EEXIST);
235eda14cbcSMatt Macy 		goto eholdnewbmds;
236eda14cbcSMatt Macy 	default:
23716038816SMartin Matuska 		/* dsl_bookmark_lookup_impl already did SET_ERROR */
238eda14cbcSMatt Macy 		goto eholdnewbmds;
239eda14cbcSMatt Macy 	}
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 	/* error is retval of the following if-cascade */
242eda14cbcSMatt Macy 	if (strchr(source, '@') != NULL) {
243eda14cbcSMatt Macy 		dsl_dataset_t *source_snap_ds;
244eda14cbcSMatt Macy 		ASSERT3S(snapshot_namecheck(source, NULL, NULL), ==, 0);
245eda14cbcSMatt Macy 		error = dsl_dataset_hold(dp, source, FTAG, &source_snap_ds);
246eda14cbcSMatt Macy 		if (error == 0) {
247eda14cbcSMatt Macy 			VERIFY(source_snap_ds->ds_is_snapshot);
248eda14cbcSMatt Macy 			/*
249eda14cbcSMatt Macy 			 * Verify that source snapshot is an earlier point in
250eda14cbcSMatt Macy 			 * newbm_ds's timeline (source may be newbm_ds's origin)
251eda14cbcSMatt Macy 			 */
252eda14cbcSMatt Macy 			if (!dsl_dataset_is_before(newbm_ds, source_snap_ds, 0))
253eda14cbcSMatt Macy 				error = SET_ERROR(
254eda14cbcSMatt Macy 				    ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);
255eda14cbcSMatt Macy 			dsl_dataset_rele(source_snap_ds, FTAG);
256eda14cbcSMatt Macy 		}
257eda14cbcSMatt Macy 	} else if (strchr(source, '#') != NULL) {
258eda14cbcSMatt Macy 		zfs_bookmark_phys_t source_phys;
259eda14cbcSMatt Macy 		ASSERT3S(bookmark_namecheck(source, NULL, NULL), ==, 0);
260eda14cbcSMatt Macy 		/*
261eda14cbcSMatt Macy 		 * Source must exists and be an earlier point in newbm_ds's
262eda14cbcSMatt Macy 		 * timeline (newbm_ds's origin may be a snap of source's ds)
263eda14cbcSMatt Macy 		 */
264eda14cbcSMatt Macy 		error = dsl_bookmark_lookup(dp, source, newbm_ds, &source_phys);
265eda14cbcSMatt Macy 		switch (error) {
266eda14cbcSMatt Macy 		case 0:
267eda14cbcSMatt Macy 			break; /* happy path */
268eda14cbcSMatt Macy 		case EXDEV:
269eda14cbcSMatt Macy 			error = SET_ERROR(ZFS_ERR_BOOKMARK_SOURCE_NOT_ANCESTOR);
270eda14cbcSMatt Macy 			break;
271eda14cbcSMatt Macy 		default:
27216038816SMartin Matuska 			/* dsl_bookmark_lookup already did SET_ERROR */
273eda14cbcSMatt Macy 			break;
274eda14cbcSMatt Macy 		}
275eda14cbcSMatt Macy 	} else {
276eda14cbcSMatt Macy 		/*
277eda14cbcSMatt Macy 		 * dsl_bookmark_create_nvl_validate validates that source is
278eda14cbcSMatt Macy 		 * either snapshot or bookmark
279eda14cbcSMatt Macy 		 */
280eda14cbcSMatt Macy 		panic("unreachable code: %s", source);
281eda14cbcSMatt Macy 	}
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy eholdnewbmds:
284eda14cbcSMatt Macy 	dsl_dataset_rele(newbm_ds, FTAG);
285eda14cbcSMatt Macy 	return (error);
286eda14cbcSMatt Macy }
287eda14cbcSMatt Macy 
288eda14cbcSMatt Macy int
289eda14cbcSMatt Macy dsl_bookmark_create_check(void *arg, dmu_tx_t *tx)
290eda14cbcSMatt Macy {
291eda14cbcSMatt Macy 	dsl_bookmark_create_arg_t *dbca = arg;
292eda14cbcSMatt Macy 	int rv = 0;
293eda14cbcSMatt Macy 	int schema_err = 0;
294eda14cbcSMatt Macy 	ASSERT3P(dbca, !=, NULL);
295eda14cbcSMatt Macy 	ASSERT3P(dbca->dbca_bmarks, !=, NULL);
296eda14cbcSMatt Macy 	/* dbca->dbca_errors is allowed to be NULL */
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
299eda14cbcSMatt Macy 
300eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
301eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	if (dsl_bookmark_create_nvl_validate(dbca->dbca_bmarks) != 0)
304eda14cbcSMatt Macy 		rv = schema_err = SET_ERROR(EINVAL);
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
307eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
308*2a58b312SMartin Matuska 		const char *new = nvpair_name(pair);
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 		int error = schema_err;
311eda14cbcSMatt Macy 		if (error == 0) {
312*2a58b312SMartin Matuska 			const char *source = fnvpair_value_string(pair);
313eda14cbcSMatt Macy 			error = dsl_bookmark_create_check_impl(dp, new, source);
314eda14cbcSMatt Macy 			if (error != 0)
315eda14cbcSMatt Macy 				error = SET_ERROR(error);
316eda14cbcSMatt Macy 		}
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 		if (error != 0) {
319eda14cbcSMatt Macy 			rv = error;
320eda14cbcSMatt Macy 			if (dbca->dbca_errors != NULL)
321eda14cbcSMatt Macy 				fnvlist_add_int32(dbca->dbca_errors,
322eda14cbcSMatt Macy 				    new, error);
323eda14cbcSMatt Macy 		}
324eda14cbcSMatt Macy 	}
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	return (rv);
327eda14cbcSMatt Macy }
328eda14cbcSMatt Macy 
329eda14cbcSMatt Macy static dsl_bookmark_node_t *
330eda14cbcSMatt Macy dsl_bookmark_node_alloc(char *shortname)
331eda14cbcSMatt Macy {
332eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn = kmem_alloc(sizeof (*dbn), KM_SLEEP);
333eda14cbcSMatt Macy 	dbn->dbn_name = spa_strdup(shortname);
334eda14cbcSMatt Macy 	dbn->dbn_dirty = B_FALSE;
335eda14cbcSMatt Macy 	mutex_init(&dbn->dbn_lock, NULL, MUTEX_DEFAULT, NULL);
336eda14cbcSMatt Macy 	return (dbn);
337eda14cbcSMatt Macy }
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy /*
340eda14cbcSMatt Macy  * Set the fields in the zfs_bookmark_phys_t based on the specified snapshot.
341eda14cbcSMatt Macy  */
342eda14cbcSMatt Macy static void
343eda14cbcSMatt Macy dsl_bookmark_set_phys(zfs_bookmark_phys_t *zbm, dsl_dataset_t *snap)
344eda14cbcSMatt Macy {
345eda14cbcSMatt Macy 	spa_t *spa = dsl_dataset_get_spa(snap);
346eda14cbcSMatt Macy 	objset_t *mos = spa_get_dsl(spa)->dp_meta_objset;
347eda14cbcSMatt Macy 	dsl_dataset_phys_t *dsp = dsl_dataset_phys(snap);
348a0b956f5SMartin Matuska 
349a0b956f5SMartin Matuska 	memset(zbm, 0, sizeof (zfs_bookmark_phys_t));
350eda14cbcSMatt Macy 	zbm->zbm_guid = dsp->ds_guid;
351eda14cbcSMatt Macy 	zbm->zbm_creation_txg = dsp->ds_creation_txg;
352eda14cbcSMatt Macy 	zbm->zbm_creation_time = dsp->ds_creation_time;
353eda14cbcSMatt Macy 	zbm->zbm_redaction_obj = 0;
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy 	/*
356eda14cbcSMatt Macy 	 * If the dataset is encrypted create a larger bookmark to
357eda14cbcSMatt Macy 	 * accommodate the IVset guid. The IVset guid was added
358eda14cbcSMatt Macy 	 * after the encryption feature to prevent a problem with
359eda14cbcSMatt Macy 	 * raw sends. If we encounter an encrypted dataset without
360eda14cbcSMatt Macy 	 * an IVset guid we fall back to a normal bookmark.
361eda14cbcSMatt Macy 	 */
362eda14cbcSMatt Macy 	if (snap->ds_dir->dd_crypto_obj != 0 &&
363eda14cbcSMatt Macy 	    spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) {
364eda14cbcSMatt Macy 		(void) zap_lookup(mos, snap->ds_object,
365eda14cbcSMatt Macy 		    DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
366eda14cbcSMatt Macy 		    &zbm->zbm_ivset_guid);
367eda14cbcSMatt Macy 	}
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	if (spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_WRITTEN)) {
370eda14cbcSMatt Macy 		zbm->zbm_flags = ZBM_FLAG_SNAPSHOT_EXISTS | ZBM_FLAG_HAS_FBN;
371eda14cbcSMatt Macy 		zbm->zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;
372eda14cbcSMatt Macy 		zbm->zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;
373eda14cbcSMatt Macy 		zbm->zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;
374eda14cbcSMatt Macy 
375eda14cbcSMatt Macy 		dsl_dataset_t *nextds;
376eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(snap->ds_dir->dd_pool,
377eda14cbcSMatt Macy 		    dsp->ds_next_snap_obj, FTAG, &nextds));
378eda14cbcSMatt Macy 		dsl_deadlist_space(&nextds->ds_deadlist,
379eda14cbcSMatt Macy 		    &zbm->zbm_referenced_freed_before_next_snap,
380eda14cbcSMatt Macy 		    &zbm->zbm_compressed_freed_before_next_snap,
381eda14cbcSMatt Macy 		    &zbm->zbm_uncompressed_freed_before_next_snap);
382eda14cbcSMatt Macy 		dsl_dataset_rele(nextds, FTAG);
383eda14cbcSMatt Macy 	}
384eda14cbcSMatt Macy }
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy /*
387eda14cbcSMatt Macy  * Add dsl_bookmark_node_t `dbn` to the given dataset and increment appropriate
388eda14cbcSMatt Macy  * SPA feature counters.
389eda14cbcSMatt Macy  */
390eda14cbcSMatt Macy void
391eda14cbcSMatt Macy dsl_bookmark_node_add(dsl_dataset_t *hds, dsl_bookmark_node_t *dbn,
392eda14cbcSMatt Macy     dmu_tx_t *tx)
393eda14cbcSMatt Macy {
394eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
395eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 	if (hds->ds_bookmarks_obj == 0) {
398eda14cbcSMatt Macy 		hds->ds_bookmarks_obj = zap_create_norm(mos,
399eda14cbcSMatt Macy 		    U8_TEXTPREP_TOUPPER, DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0,
400eda14cbcSMatt Macy 		    tx);
401eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 		dsl_dataset_zapify(hds, tx);
404eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, hds->ds_object,
405eda14cbcSMatt Macy 		    DS_FIELD_BOOKMARK_NAMES,
406eda14cbcSMatt Macy 		    sizeof (hds->ds_bookmarks_obj), 1,
407eda14cbcSMatt Macy 		    &hds->ds_bookmarks_obj, tx));
408eda14cbcSMatt Macy 	}
409eda14cbcSMatt Macy 
410eda14cbcSMatt Macy 	avl_add(&hds->ds_bookmarks, dbn);
411eda14cbcSMatt Macy 
412eda14cbcSMatt Macy 	/*
413eda14cbcSMatt Macy 	 * To maintain backwards compatibility with software that doesn't
414eda14cbcSMatt Macy 	 * understand SPA_FEATURE_BOOKMARK_V2, we need to use the smallest
415eda14cbcSMatt Macy 	 * possible bookmark size.
416eda14cbcSMatt Macy 	 */
417eda14cbcSMatt Macy 	uint64_t bookmark_phys_size = BOOKMARK_PHYS_SIZE_V1;
418eda14cbcSMatt Macy 	if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2) &&
419eda14cbcSMatt Macy 	    (dbn->dbn_phys.zbm_ivset_guid != 0 || dbn->dbn_phys.zbm_flags &
420eda14cbcSMatt Macy 	    ZBM_FLAG_HAS_FBN || dbn->dbn_phys.zbm_redaction_obj != 0)) {
421eda14cbcSMatt Macy 		bookmark_phys_size = BOOKMARK_PHYS_SIZE_V2;
422eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2, tx);
423eda14cbcSMatt Macy 	}
424eda14cbcSMatt Macy 
425da5137abSMartin Matuska 	zfs_bookmark_phys_t zero_phys = { 0 };
426da5137abSMartin Matuska 	ASSERT0(memcmp(((char *)&dbn->dbn_phys) + bookmark_phys_size,
427eda14cbcSMatt Macy 	    &zero_phys, sizeof (zfs_bookmark_phys_t) - bookmark_phys_size));
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 	VERIFY0(zap_add(mos, hds->ds_bookmarks_obj, dbn->dbn_name,
430eda14cbcSMatt Macy 	    sizeof (uint64_t), bookmark_phys_size / sizeof (uint64_t),
431eda14cbcSMatt Macy 	    &dbn->dbn_phys, tx));
432eda14cbcSMatt Macy }
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy /*
435eda14cbcSMatt Macy  * If redaction_list is non-null, we create a redacted bookmark and redaction
436eda14cbcSMatt Macy  * list, and store the object number of the redaction list in redact_obj.
437eda14cbcSMatt Macy  */
438eda14cbcSMatt Macy static void
439eda14cbcSMatt Macy dsl_bookmark_create_sync_impl_snap(const char *bookmark, const char *snapshot,
440a0b956f5SMartin Matuska     dmu_tx_t *tx, uint64_t num_redact_snaps, uint64_t *redact_snaps,
441a0b956f5SMartin Matuska     const void *tag, redaction_list_t **redaction_list)
442eda14cbcSMatt Macy {
443eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
444eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
445eda14cbcSMatt Macy 	dsl_dataset_t *snapds, *bmark_fs;
446eda14cbcSMatt Macy 	char *shortname;
447eda14cbcSMatt Macy 	boolean_t bookmark_redacted;
448eda14cbcSMatt Macy 	uint64_t *dsredactsnaps;
449eda14cbcSMatt Macy 	uint64_t dsnumsnaps;
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, snapshot, FTAG, &snapds));
452eda14cbcSMatt Macy 	VERIFY0(dsl_bookmark_hold_ds(dp, bookmark, &bmark_fs, FTAG,
453eda14cbcSMatt Macy 	    &shortname));
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn = dsl_bookmark_node_alloc(shortname);
456eda14cbcSMatt Macy 	dsl_bookmark_set_phys(&dbn->dbn_phys, snapds);
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy 	bookmark_redacted = dsl_dataset_get_uint64_array_feature(snapds,
459eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS, &dsnumsnaps, &dsredactsnaps);
460eda14cbcSMatt Macy 	if (redaction_list != NULL || bookmark_redacted) {
461eda14cbcSMatt Macy 		redaction_list_t *local_rl;
462eda14cbcSMatt Macy 		if (bookmark_redacted) {
463eda14cbcSMatt Macy 			redact_snaps = dsredactsnaps;
464eda14cbcSMatt Macy 			num_redact_snaps = dsnumsnaps;
465eda14cbcSMatt Macy 		}
466eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_redaction_obj = dmu_object_alloc(mos,
467eda14cbcSMatt Macy 		    DMU_OTN_UINT64_METADATA, SPA_OLD_MAXBLOCKSIZE,
468eda14cbcSMatt Macy 		    DMU_OTN_UINT64_METADATA, sizeof (redaction_list_phys_t) +
469eda14cbcSMatt Macy 		    num_redact_snaps * sizeof (uint64_t), tx);
470eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa,
471eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTION_BOOKMARKS, tx);
472eda14cbcSMatt Macy 
473eda14cbcSMatt Macy 		VERIFY0(dsl_redaction_list_hold_obj(dp,
474eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_redaction_obj, tag, &local_rl));
475eda14cbcSMatt Macy 		dsl_redaction_list_long_hold(dp, local_rl, tag);
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy 		ASSERT3U((local_rl)->rl_dbuf->db_size, >=,
478eda14cbcSMatt Macy 		    sizeof (redaction_list_phys_t) + num_redact_snaps *
479eda14cbcSMatt Macy 		    sizeof (uint64_t));
480eda14cbcSMatt Macy 		dmu_buf_will_dirty(local_rl->rl_dbuf, tx);
481da5137abSMartin Matuska 		memcpy(local_rl->rl_phys->rlp_snaps, redact_snaps,
482eda14cbcSMatt Macy 		    sizeof (uint64_t) * num_redact_snaps);
483eda14cbcSMatt Macy 		local_rl->rl_phys->rlp_num_snaps = num_redact_snaps;
484eda14cbcSMatt Macy 		if (bookmark_redacted) {
485eda14cbcSMatt Macy 			ASSERT3P(redaction_list, ==, NULL);
486eda14cbcSMatt Macy 			local_rl->rl_phys->rlp_last_blkid = UINT64_MAX;
487eda14cbcSMatt Macy 			local_rl->rl_phys->rlp_last_object = UINT64_MAX;
488eda14cbcSMatt Macy 			dsl_redaction_list_long_rele(local_rl, tag);
489eda14cbcSMatt Macy 			dsl_redaction_list_rele(local_rl, tag);
490eda14cbcSMatt Macy 		} else {
491eda14cbcSMatt Macy 			*redaction_list = local_rl;
492eda14cbcSMatt Macy 		}
493eda14cbcSMatt Macy 	}
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy 	if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
496eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa,
497eda14cbcSMatt Macy 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
498eda14cbcSMatt Macy 	}
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy 	dsl_bookmark_node_add(bmark_fs, dbn, tx);
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy 	spa_history_log_internal_ds(bmark_fs, "bookmark", tx,
503eda14cbcSMatt Macy 	    "name=%s creation_txg=%llu target_snap=%llu redact_obj=%llu",
504eda14cbcSMatt Macy 	    shortname, (longlong_t)dbn->dbn_phys.zbm_creation_txg,
505eda14cbcSMatt Macy 	    (longlong_t)snapds->ds_object,
506eda14cbcSMatt Macy 	    (longlong_t)dbn->dbn_phys.zbm_redaction_obj);
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy 	dsl_dataset_rele(bmark_fs, FTAG);
509eda14cbcSMatt Macy 	dsl_dataset_rele(snapds, FTAG);
510eda14cbcSMatt Macy }
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy static void
514eda14cbcSMatt Macy dsl_bookmark_create_sync_impl_book(
515eda14cbcSMatt Macy     const char *new_name, const char *source_name, dmu_tx_t *tx)
516eda14cbcSMatt Macy {
517eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
518eda14cbcSMatt Macy 	dsl_dataset_t *bmark_fs_source, *bmark_fs_new;
519eda14cbcSMatt Macy 	char *source_shortname, *new_shortname;
520eda14cbcSMatt Macy 	zfs_bookmark_phys_t source_phys;
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	VERIFY0(dsl_bookmark_hold_ds(dp, source_name, &bmark_fs_source, FTAG,
523eda14cbcSMatt Macy 	    &source_shortname));
524eda14cbcSMatt Macy 	VERIFY0(dsl_bookmark_hold_ds(dp, new_name, &bmark_fs_new, FTAG,
525eda14cbcSMatt Macy 	    &new_shortname));
526eda14cbcSMatt Macy 
527eda14cbcSMatt Macy 	/*
528eda14cbcSMatt Macy 	 * create a copy of the source bookmark by copying most of its members
529eda14cbcSMatt Macy 	 *
530eda14cbcSMatt Macy 	 * Caveat: bookmarking a redaction bookmark yields a normal bookmark
531eda14cbcSMatt Macy 	 * -----------------------------------------------------------------
532eda14cbcSMatt Macy 	 * Reasoning:
533eda14cbcSMatt Macy 	 * - The zbm_redaction_obj would be referred to by both source and new
534eda14cbcSMatt Macy 	 *   bookmark, but would be destroyed once either source or new is
53516038816SMartin Matuska 	 *   destroyed, resulting in use-after-free of the referred object.
536eda14cbcSMatt Macy 	 * - User expectation when issuing the `zfs bookmark` command is that
537eda14cbcSMatt Macy 	 *   a normal bookmark of the source is created
538eda14cbcSMatt Macy 	 *
539eda14cbcSMatt Macy 	 * Design Alternatives For Full Redaction Bookmark Copying:
540eda14cbcSMatt Macy 	 * - reference-count the redaction object => would require on-disk
541eda14cbcSMatt Macy 	 *   format change for existing redaction objects
542eda14cbcSMatt Macy 	 * - Copy the redaction object => cannot be done in syncing context
543eda14cbcSMatt Macy 	 *   because the redaction object might be too large
544eda14cbcSMatt Macy 	 */
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 	VERIFY0(dsl_bookmark_lookup_impl(bmark_fs_source, source_shortname,
547eda14cbcSMatt Macy 	    &source_phys));
548eda14cbcSMatt Macy 	dsl_bookmark_node_t *new_dbn = dsl_bookmark_node_alloc(new_shortname);
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy 	memcpy(&new_dbn->dbn_phys, &source_phys, sizeof (source_phys));
551eda14cbcSMatt Macy 	new_dbn->dbn_phys.zbm_redaction_obj = 0;
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy 	/* update feature counters */
554eda14cbcSMatt Macy 	if (new_dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
555eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa,
556eda14cbcSMatt Macy 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
557eda14cbcSMatt Macy 	}
558eda14cbcSMatt Macy 	/* no need for redaction bookmark counter; nulled zbm_redaction_obj */
559eda14cbcSMatt Macy 	/* dsl_bookmark_node_add bumps bookmarks and v2-bookmarks counter */
560eda14cbcSMatt Macy 
561eda14cbcSMatt Macy 	/*
562eda14cbcSMatt Macy 	 * write new bookmark
563eda14cbcSMatt Macy 	 *
564eda14cbcSMatt Macy 	 * Note that dsl_bookmark_lookup_impl guarantees that, if source is a
565eda14cbcSMatt Macy 	 * v1 bookmark, the v2-only fields are zeroed.
566eda14cbcSMatt Macy 	 * And dsl_bookmark_node_add writes back a v1-sized bookmark if
567eda14cbcSMatt Macy 	 * v2 bookmarks are disabled and/or v2-only fields are zeroed.
568eda14cbcSMatt Macy 	 * => bookmark copying works on pre-bookmark-v2 pools
569eda14cbcSMatt Macy 	 */
570eda14cbcSMatt Macy 	dsl_bookmark_node_add(bmark_fs_new, new_dbn, tx);
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 	spa_history_log_internal_ds(bmark_fs_source, "bookmark", tx,
573eda14cbcSMatt Macy 	    "name=%s creation_txg=%llu source_guid=%llu",
574eda14cbcSMatt Macy 	    new_shortname, (longlong_t)new_dbn->dbn_phys.zbm_creation_txg,
575eda14cbcSMatt Macy 	    (longlong_t)source_phys.zbm_guid);
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy 	dsl_dataset_rele(bmark_fs_source, FTAG);
578eda14cbcSMatt Macy 	dsl_dataset_rele(bmark_fs_new, FTAG);
579eda14cbcSMatt Macy }
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy void
582eda14cbcSMatt Macy dsl_bookmark_create_sync(void *arg, dmu_tx_t *tx)
583eda14cbcSMatt Macy {
584eda14cbcSMatt Macy 	dsl_bookmark_create_arg_t *dbca = arg;
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy 	ASSERT(spa_feature_is_enabled(dmu_tx_pool(tx)->dp_spa,
587eda14cbcSMatt Macy 	    SPA_FEATURE_BOOKMARKS));
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy 	for (nvpair_t *pair = nvlist_next_nvpair(dbca->dbca_bmarks, NULL);
590eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(dbca->dbca_bmarks, pair)) {
591eda14cbcSMatt Macy 
592*2a58b312SMartin Matuska 		const char *new = nvpair_name(pair);
593*2a58b312SMartin Matuska 		const char *source = fnvpair_value_string(pair);
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy 		if (strchr(source, '@') != NULL) {
596eda14cbcSMatt Macy 			dsl_bookmark_create_sync_impl_snap(new, source, tx,
597eda14cbcSMatt Macy 			    0, NULL, NULL, NULL);
598eda14cbcSMatt Macy 		} else if (strchr(source, '#') != NULL) {
599eda14cbcSMatt Macy 			dsl_bookmark_create_sync_impl_book(new, source, tx);
600eda14cbcSMatt Macy 		} else {
601eda14cbcSMatt Macy 			panic("unreachable code");
602eda14cbcSMatt Macy 		}
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 	}
605eda14cbcSMatt Macy }
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy /*
608eda14cbcSMatt Macy  * The bookmarks must all be in the same pool.
609eda14cbcSMatt Macy  */
610eda14cbcSMatt Macy int
611eda14cbcSMatt Macy dsl_bookmark_create(nvlist_t *bmarks, nvlist_t *errors)
612eda14cbcSMatt Macy {
613eda14cbcSMatt Macy 	nvpair_t *pair;
614eda14cbcSMatt Macy 	dsl_bookmark_create_arg_t dbca;
615eda14cbcSMatt Macy 
616eda14cbcSMatt Macy 	pair = nvlist_next_nvpair(bmarks, NULL);
617eda14cbcSMatt Macy 	if (pair == NULL)
618eda14cbcSMatt Macy 		return (0);
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 	dbca.dbca_bmarks = bmarks;
621eda14cbcSMatt Macy 	dbca.dbca_errors = errors;
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 	return (dsl_sync_task(nvpair_name(pair), dsl_bookmark_create_check,
624eda14cbcSMatt Macy 	    dsl_bookmark_create_sync, &dbca,
625eda14cbcSMatt Macy 	    fnvlist_num_pairs(bmarks), ZFS_SPACE_CHECK_NORMAL));
626eda14cbcSMatt Macy }
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy static int
629eda14cbcSMatt Macy dsl_bookmark_create_redacted_check(void *arg, dmu_tx_t *tx)
630eda14cbcSMatt Macy {
631eda14cbcSMatt Macy 	dsl_bookmark_create_redacted_arg_t *dbcra = arg;
632eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
633eda14cbcSMatt Macy 	int rv = 0;
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(dp->dp_spa,
636eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTION_BOOKMARKS))
637eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
638eda14cbcSMatt Macy 	/*
639eda14cbcSMatt Macy 	 * If the list of redact snaps will not fit in the bonus buffer with
640eda14cbcSMatt Macy 	 * the furthest reached object and offset, fail.
641eda14cbcSMatt Macy 	 */
642eda14cbcSMatt Macy 	if (dbcra->dbcra_numsnaps > (dmu_bonus_max() -
643eda14cbcSMatt Macy 	    sizeof (redaction_list_phys_t)) / sizeof (uint64_t))
644eda14cbcSMatt Macy 		return (SET_ERROR(E2BIG));
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy 	if (dsl_bookmark_create_nvl_validate_pair(
647eda14cbcSMatt Macy 	    dbcra->dbcra_bmark, dbcra->dbcra_snap) != 0)
648eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 	rv = dsl_bookmark_create_check_impl(dp,
651eda14cbcSMatt Macy 	    dbcra->dbcra_bmark, dbcra->dbcra_snap);
652eda14cbcSMatt Macy 	return (rv);
653eda14cbcSMatt Macy }
654eda14cbcSMatt Macy 
655eda14cbcSMatt Macy static void
656eda14cbcSMatt Macy dsl_bookmark_create_redacted_sync(void *arg, dmu_tx_t *tx)
657eda14cbcSMatt Macy {
658eda14cbcSMatt Macy 	dsl_bookmark_create_redacted_arg_t *dbcra = arg;
659eda14cbcSMatt Macy 	dsl_bookmark_create_sync_impl_snap(dbcra->dbcra_bmark,
660eda14cbcSMatt Macy 	    dbcra->dbcra_snap, tx, dbcra->dbcra_numsnaps, dbcra->dbcra_snaps,
661eda14cbcSMatt Macy 	    dbcra->dbcra_tag, dbcra->dbcra_rl);
662eda14cbcSMatt Macy }
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy int
665eda14cbcSMatt Macy dsl_bookmark_create_redacted(const char *bookmark, const char *snapshot,
666a0b956f5SMartin Matuska     uint64_t numsnaps, uint64_t *snapguids, const void *tag,
667a0b956f5SMartin Matuska     redaction_list_t **rl)
668eda14cbcSMatt Macy {
669eda14cbcSMatt Macy 	dsl_bookmark_create_redacted_arg_t dbcra;
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy 	dbcra.dbcra_bmark = bookmark;
672eda14cbcSMatt Macy 	dbcra.dbcra_snap = snapshot;
673eda14cbcSMatt Macy 	dbcra.dbcra_rl = rl;
674eda14cbcSMatt Macy 	dbcra.dbcra_numsnaps = numsnaps;
675eda14cbcSMatt Macy 	dbcra.dbcra_snaps = snapguids;
676eda14cbcSMatt Macy 	dbcra.dbcra_tag = tag;
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 	return (dsl_sync_task(bookmark, dsl_bookmark_create_redacted_check,
679eda14cbcSMatt Macy 	    dsl_bookmark_create_redacted_sync, &dbcra, 5,
680eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_NORMAL));
681eda14cbcSMatt Macy }
682eda14cbcSMatt Macy 
683eda14cbcSMatt Macy /*
684eda14cbcSMatt Macy  * Retrieve the list of properties given in the 'props' nvlist for a bookmark.
685eda14cbcSMatt Macy  * If 'props' is NULL, retrieves all properties.
686eda14cbcSMatt Macy  */
687eda14cbcSMatt Macy static void
688eda14cbcSMatt Macy dsl_bookmark_fetch_props(dsl_pool_t *dp, zfs_bookmark_phys_t *bmark_phys,
689eda14cbcSMatt Macy     nvlist_t *props, nvlist_t *out_props)
690eda14cbcSMatt Macy {
691eda14cbcSMatt Macy 	ASSERT3P(dp, !=, NULL);
692eda14cbcSMatt Macy 	ASSERT3P(bmark_phys, !=, NULL);
693eda14cbcSMatt Macy 	ASSERT3P(out_props, !=, NULL);
694eda14cbcSMatt Macy 	ASSERT(RRW_LOCK_HELD(&dp->dp_config_rwlock));
695eda14cbcSMatt Macy 
696eda14cbcSMatt Macy 	if (props == NULL || nvlist_exists(props,
697eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_GUID))) {
698eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(out_props,
699eda14cbcSMatt Macy 		    ZFS_PROP_GUID, bmark_phys->zbm_guid);
700eda14cbcSMatt Macy 	}
701eda14cbcSMatt Macy 	if (props == NULL || nvlist_exists(props,
702eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_CREATETXG))) {
703eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(out_props,
704eda14cbcSMatt Macy 		    ZFS_PROP_CREATETXG, bmark_phys->zbm_creation_txg);
705eda14cbcSMatt Macy 	}
706eda14cbcSMatt Macy 	if (props == NULL || nvlist_exists(props,
707eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_CREATION))) {
708eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(out_props,
709eda14cbcSMatt Macy 		    ZFS_PROP_CREATION, bmark_phys->zbm_creation_time);
710eda14cbcSMatt Macy 	}
711eda14cbcSMatt Macy 	if (props == NULL || nvlist_exists(props,
712eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_IVSET_GUID))) {
713eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(out_props,
714eda14cbcSMatt Macy 		    ZFS_PROP_IVSET_GUID, bmark_phys->zbm_ivset_guid);
715eda14cbcSMatt Macy 	}
716eda14cbcSMatt Macy 	if (bmark_phys->zbm_flags & ZBM_FLAG_HAS_FBN) {
717eda14cbcSMatt Macy 		if (props == NULL || nvlist_exists(props,
718eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REFERENCED))) {
719eda14cbcSMatt Macy 			dsl_prop_nvlist_add_uint64(out_props,
720eda14cbcSMatt Macy 			    ZFS_PROP_REFERENCED,
721eda14cbcSMatt Macy 			    bmark_phys->zbm_referenced_bytes_refd);
722eda14cbcSMatt Macy 		}
723eda14cbcSMatt Macy 		if (props == NULL || nvlist_exists(props,
724eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_LOGICALREFERENCED))) {
725eda14cbcSMatt Macy 			dsl_prop_nvlist_add_uint64(out_props,
726eda14cbcSMatt Macy 			    ZFS_PROP_LOGICALREFERENCED,
727eda14cbcSMatt Macy 			    bmark_phys->zbm_uncompressed_bytes_refd);
728eda14cbcSMatt Macy 		}
729eda14cbcSMatt Macy 		if (props == NULL || nvlist_exists(props,
730eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REFRATIO))) {
731eda14cbcSMatt Macy 			uint64_t ratio =
732eda14cbcSMatt Macy 			    bmark_phys->zbm_compressed_bytes_refd == 0 ? 100 :
733eda14cbcSMatt Macy 			    bmark_phys->zbm_uncompressed_bytes_refd * 100 /
734eda14cbcSMatt Macy 			    bmark_phys->zbm_compressed_bytes_refd;
735eda14cbcSMatt Macy 			dsl_prop_nvlist_add_uint64(out_props,
736eda14cbcSMatt Macy 			    ZFS_PROP_REFRATIO, ratio);
737eda14cbcSMatt Macy 		}
738eda14cbcSMatt Macy 	}
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy 	if ((props == NULL || nvlist_exists(props, "redact_snaps") ||
741eda14cbcSMatt Macy 	    nvlist_exists(props, "redact_complete")) &&
742eda14cbcSMatt Macy 	    bmark_phys->zbm_redaction_obj != 0) {
743eda14cbcSMatt Macy 		redaction_list_t *rl;
744eda14cbcSMatt Macy 		int err = dsl_redaction_list_hold_obj(dp,
745eda14cbcSMatt Macy 		    bmark_phys->zbm_redaction_obj, FTAG, &rl);
746eda14cbcSMatt Macy 		if (err == 0) {
747eda14cbcSMatt Macy 			if (nvlist_exists(props, "redact_snaps")) {
748eda14cbcSMatt Macy 				nvlist_t *nvl;
749eda14cbcSMatt Macy 				nvl = fnvlist_alloc();
750eda14cbcSMatt Macy 				fnvlist_add_uint64_array(nvl, ZPROP_VALUE,
751eda14cbcSMatt Macy 				    rl->rl_phys->rlp_snaps,
752eda14cbcSMatt Macy 				    rl->rl_phys->rlp_num_snaps);
753eda14cbcSMatt Macy 				fnvlist_add_nvlist(out_props, "redact_snaps",
754eda14cbcSMatt Macy 				    nvl);
755eda14cbcSMatt Macy 				nvlist_free(nvl);
756eda14cbcSMatt Macy 			}
757eda14cbcSMatt Macy 			if (nvlist_exists(props, "redact_complete")) {
758eda14cbcSMatt Macy 				nvlist_t *nvl;
759eda14cbcSMatt Macy 				nvl = fnvlist_alloc();
760eda14cbcSMatt Macy 				fnvlist_add_boolean_value(nvl, ZPROP_VALUE,
761eda14cbcSMatt Macy 				    rl->rl_phys->rlp_last_blkid == UINT64_MAX &&
762eda14cbcSMatt Macy 				    rl->rl_phys->rlp_last_object == UINT64_MAX);
763eda14cbcSMatt Macy 				fnvlist_add_nvlist(out_props, "redact_complete",
764eda14cbcSMatt Macy 				    nvl);
765eda14cbcSMatt Macy 				nvlist_free(nvl);
766eda14cbcSMatt Macy 			}
767eda14cbcSMatt Macy 			dsl_redaction_list_rele(rl, FTAG);
768eda14cbcSMatt Macy 		}
769eda14cbcSMatt Macy 	}
770eda14cbcSMatt Macy }
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy int
773eda14cbcSMatt Macy dsl_get_bookmarks_impl(dsl_dataset_t *ds, nvlist_t *props, nvlist_t *outnvl)
774eda14cbcSMatt Macy {
775eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
776eda14cbcSMatt Macy 
777eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
778eda14cbcSMatt Macy 
779eda14cbcSMatt Macy 	if (dsl_dataset_is_snapshot(ds))
780eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
781eda14cbcSMatt Macy 
782eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);
783eda14cbcSMatt Macy 	    dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {
784eda14cbcSMatt Macy 		nvlist_t *out_props = fnvlist_alloc();
785eda14cbcSMatt Macy 
786eda14cbcSMatt Macy 		dsl_bookmark_fetch_props(dp, &dbn->dbn_phys, props, out_props);
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 		fnvlist_add_nvlist(outnvl, dbn->dbn_name, out_props);
789eda14cbcSMatt Macy 		fnvlist_free(out_props);
790eda14cbcSMatt Macy 	}
791eda14cbcSMatt Macy 	return (0);
792eda14cbcSMatt Macy }
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy /*
795eda14cbcSMatt Macy  * Comparison func for ds_bookmarks AVL tree.  We sort the bookmarks by
796eda14cbcSMatt Macy  * their TXG, then by their FBN-ness.  The "FBN-ness" component ensures
797eda14cbcSMatt Macy  * that all bookmarks at the same TXG that HAS_FBN are adjacent, which
798eda14cbcSMatt Macy  * dsl_bookmark_destroy_sync_impl() depends on.  Note that there may be
799eda14cbcSMatt Macy  * multiple bookmarks at the same TXG (with the same FBN-ness).  In this
800eda14cbcSMatt Macy  * case we differentiate them by an arbitrary metric (in this case,
801eda14cbcSMatt Macy  * their names).
802eda14cbcSMatt Macy  */
803eda14cbcSMatt Macy static int
804eda14cbcSMatt Macy dsl_bookmark_compare(const void *l, const void *r)
805eda14cbcSMatt Macy {
806eda14cbcSMatt Macy 	const dsl_bookmark_node_t *ldbn = l;
807eda14cbcSMatt Macy 	const dsl_bookmark_node_t *rdbn = r;
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy 	int64_t cmp = TREE_CMP(ldbn->dbn_phys.zbm_creation_txg,
810eda14cbcSMatt Macy 	    rdbn->dbn_phys.zbm_creation_txg);
811eda14cbcSMatt Macy 	if (likely(cmp))
812eda14cbcSMatt Macy 		return (cmp);
813eda14cbcSMatt Macy 	cmp = TREE_CMP((ldbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN),
814eda14cbcSMatt Macy 	    (rdbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));
815eda14cbcSMatt Macy 	if (likely(cmp))
816eda14cbcSMatt Macy 		return (cmp);
817eda14cbcSMatt Macy 	cmp = strcmp(ldbn->dbn_name, rdbn->dbn_name);
818eda14cbcSMatt Macy 	return (TREE_ISIGN(cmp));
819eda14cbcSMatt Macy }
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy /*
822eda14cbcSMatt Macy  * Cache this (head) dataset's bookmarks in the ds_bookmarks AVL tree.
823eda14cbcSMatt Macy  */
824eda14cbcSMatt Macy int
825eda14cbcSMatt Macy dsl_bookmark_init_ds(dsl_dataset_t *ds)
826eda14cbcSMatt Macy {
827eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
828eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
829eda14cbcSMatt Macy 
830eda14cbcSMatt Macy 	ASSERT(!ds->ds_is_snapshot);
831eda14cbcSMatt Macy 
832eda14cbcSMatt Macy 	avl_create(&ds->ds_bookmarks, dsl_bookmark_compare,
833eda14cbcSMatt Macy 	    sizeof (dsl_bookmark_node_t),
834eda14cbcSMatt Macy 	    offsetof(dsl_bookmark_node_t, dbn_node));
835eda14cbcSMatt Macy 
836eda14cbcSMatt Macy 	if (!dsl_dataset_is_zapified(ds))
837eda14cbcSMatt Macy 		return (0);
838eda14cbcSMatt Macy 
839eda14cbcSMatt Macy 	int zaperr = zap_lookup(mos, ds->ds_object, DS_FIELD_BOOKMARK_NAMES,
840eda14cbcSMatt Macy 	    sizeof (ds->ds_bookmarks_obj), 1, &ds->ds_bookmarks_obj);
841eda14cbcSMatt Macy 	if (zaperr == ENOENT)
842eda14cbcSMatt Macy 		return (0);
843eda14cbcSMatt Macy 	if (zaperr != 0)
844eda14cbcSMatt Macy 		return (zaperr);
845eda14cbcSMatt Macy 
846eda14cbcSMatt Macy 	if (ds->ds_bookmarks_obj == 0)
847eda14cbcSMatt Macy 		return (0);
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 	int err = 0;
850eda14cbcSMatt Macy 	zap_cursor_t zc;
851eda14cbcSMatt Macy 	zap_attribute_t attr;
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj);
854eda14cbcSMatt Macy 	    (err = zap_cursor_retrieve(&zc, &attr)) == 0;
855eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
856eda14cbcSMatt Macy 		dsl_bookmark_node_t *dbn =
857eda14cbcSMatt Macy 		    dsl_bookmark_node_alloc(attr.za_name);
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy 		err = dsl_bookmark_lookup_impl(ds,
860eda14cbcSMatt Macy 		    dbn->dbn_name, &dbn->dbn_phys);
861eda14cbcSMatt Macy 		ASSERT3U(err, !=, ENOENT);
862eda14cbcSMatt Macy 		if (err != 0) {
863eda14cbcSMatt Macy 			kmem_free(dbn, sizeof (*dbn));
864eda14cbcSMatt Macy 			break;
865eda14cbcSMatt Macy 		}
866eda14cbcSMatt Macy 		avl_add(&ds->ds_bookmarks, dbn);
867eda14cbcSMatt Macy 	}
868eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
869eda14cbcSMatt Macy 	if (err == ENOENT)
870eda14cbcSMatt Macy 		err = 0;
871eda14cbcSMatt Macy 	return (err);
872eda14cbcSMatt Macy }
873eda14cbcSMatt Macy 
874eda14cbcSMatt Macy void
875eda14cbcSMatt Macy dsl_bookmark_fini_ds(dsl_dataset_t *ds)
876eda14cbcSMatt Macy {
877eda14cbcSMatt Macy 	void *cookie = NULL;
878eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn;
879eda14cbcSMatt Macy 
880eda14cbcSMatt Macy 	if (ds->ds_is_snapshot)
881eda14cbcSMatt Macy 		return;
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 	while ((dbn = avl_destroy_nodes(&ds->ds_bookmarks, &cookie)) != NULL) {
884eda14cbcSMatt Macy 		spa_strfree(dbn->dbn_name);
885eda14cbcSMatt Macy 		mutex_destroy(&dbn->dbn_lock);
886eda14cbcSMatt Macy 		kmem_free(dbn, sizeof (*dbn));
887eda14cbcSMatt Macy 	}
888eda14cbcSMatt Macy 	avl_destroy(&ds->ds_bookmarks);
889eda14cbcSMatt Macy }
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy /*
892eda14cbcSMatt Macy  * Retrieve the bookmarks that exist in the specified dataset, and the
893eda14cbcSMatt Macy  * requested properties of each bookmark.
894eda14cbcSMatt Macy  *
895eda14cbcSMatt Macy  * The "props" nvlist specifies which properties are requested.
896eda14cbcSMatt Macy  * See lzc_get_bookmarks() for the list of valid properties.
897eda14cbcSMatt Macy  */
898eda14cbcSMatt Macy int
899eda14cbcSMatt Macy dsl_get_bookmarks(const char *dsname, nvlist_t *props, nvlist_t *outnvl)
900eda14cbcSMatt Macy {
901eda14cbcSMatt Macy 	dsl_pool_t *dp;
902eda14cbcSMatt Macy 	dsl_dataset_t *ds;
903eda14cbcSMatt Macy 	int err;
904eda14cbcSMatt Macy 
905eda14cbcSMatt Macy 	err = dsl_pool_hold(dsname, FTAG, &dp);
906eda14cbcSMatt Macy 	if (err != 0)
907eda14cbcSMatt Macy 		return (err);
908eda14cbcSMatt Macy 	err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
909eda14cbcSMatt Macy 	if (err != 0) {
910eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
911eda14cbcSMatt Macy 		return (err);
912eda14cbcSMatt Macy 	}
913eda14cbcSMatt Macy 
914eda14cbcSMatt Macy 	err = dsl_get_bookmarks_impl(ds, props, outnvl);
915eda14cbcSMatt Macy 
916eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
917eda14cbcSMatt Macy 	dsl_pool_rele(dp, FTAG);
918eda14cbcSMatt Macy 	return (err);
919eda14cbcSMatt Macy }
920eda14cbcSMatt Macy 
921eda14cbcSMatt Macy /*
922eda14cbcSMatt Macy  * Retrieve all properties for a single bookmark in the given dataset.
923eda14cbcSMatt Macy  */
924eda14cbcSMatt Macy int
925eda14cbcSMatt Macy dsl_get_bookmark_props(const char *dsname, const char *bmname, nvlist_t *props)
926eda14cbcSMatt Macy {
927eda14cbcSMatt Macy 	dsl_pool_t *dp;
928eda14cbcSMatt Macy 	dsl_dataset_t *ds;
929eda14cbcSMatt Macy 	zfs_bookmark_phys_t bmark_phys = { 0 };
930eda14cbcSMatt Macy 	int err;
931eda14cbcSMatt Macy 
932eda14cbcSMatt Macy 	err = dsl_pool_hold(dsname, FTAG, &dp);
933eda14cbcSMatt Macy 	if (err != 0)
934eda14cbcSMatt Macy 		return (err);
935eda14cbcSMatt Macy 	err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
936eda14cbcSMatt Macy 	if (err != 0) {
937eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
938eda14cbcSMatt Macy 		return (err);
939eda14cbcSMatt Macy 	}
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 	err = dsl_bookmark_lookup_impl(ds, bmname, &bmark_phys);
942eda14cbcSMatt Macy 	if (err != 0)
943eda14cbcSMatt Macy 		goto out;
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 	dsl_bookmark_fetch_props(dp, &bmark_phys, NULL, props);
946eda14cbcSMatt Macy out:
947eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
948eda14cbcSMatt Macy 	dsl_pool_rele(dp, FTAG);
949eda14cbcSMatt Macy 	return (err);
950eda14cbcSMatt Macy }
951eda14cbcSMatt Macy 
952eda14cbcSMatt Macy typedef struct dsl_bookmark_destroy_arg {
953eda14cbcSMatt Macy 	nvlist_t *dbda_bmarks;
954eda14cbcSMatt Macy 	nvlist_t *dbda_success;
955eda14cbcSMatt Macy 	nvlist_t *dbda_errors;
956eda14cbcSMatt Macy } dsl_bookmark_destroy_arg_t;
957eda14cbcSMatt Macy 
958eda14cbcSMatt Macy static void
959eda14cbcSMatt Macy dsl_bookmark_destroy_sync_impl(dsl_dataset_t *ds, const char *name,
960eda14cbcSMatt Macy     dmu_tx_t *tx)
961eda14cbcSMatt Macy {
962eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
963eda14cbcSMatt Macy 	uint64_t bmark_zapobj = ds->ds_bookmarks_obj;
964eda14cbcSMatt Macy 	matchtype_t mt = 0;
965eda14cbcSMatt Macy 	uint64_t int_size, num_ints;
966eda14cbcSMatt Macy 	/*
967eda14cbcSMatt Macy 	 * 'search' must be zeroed so that dbn_flags (which is used in
968eda14cbcSMatt Macy 	 * dsl_bookmark_compare()) will be zeroed even if the on-disk
969eda14cbcSMatt Macy 	 * (in ZAP) bookmark is shorter than offsetof(dbn_flags).
970eda14cbcSMatt Macy 	 */
971eda14cbcSMatt Macy 	dsl_bookmark_node_t search = { 0 };
972eda14cbcSMatt Macy 	char realname[ZFS_MAX_DATASET_NAME_LEN];
973eda14cbcSMatt Macy 
974eda14cbcSMatt Macy 	/*
975eda14cbcSMatt Macy 	 * Find the real name of this bookmark, which may be different
976eda14cbcSMatt Macy 	 * from the given name if the dataset is case-insensitive.  Then
977eda14cbcSMatt Macy 	 * use the real name to find the node in the ds_bookmarks AVL tree.
978eda14cbcSMatt Macy 	 */
979eda14cbcSMatt Macy 
980eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
981eda14cbcSMatt Macy 		mt = MT_NORMALIZE;
982eda14cbcSMatt Macy 
983eda14cbcSMatt Macy 	VERIFY0(zap_length(mos, bmark_zapobj, name, &int_size, &num_ints));
984eda14cbcSMatt Macy 
985eda14cbcSMatt Macy 	ASSERT3U(int_size, ==, sizeof (uint64_t));
986eda14cbcSMatt Macy 
987eda14cbcSMatt Macy 	if (num_ints * int_size > BOOKMARK_PHYS_SIZE_V1) {
988eda14cbcSMatt Macy 		spa_feature_decr(dmu_objset_spa(mos),
989eda14cbcSMatt Macy 		    SPA_FEATURE_BOOKMARK_V2, tx);
990eda14cbcSMatt Macy 	}
991eda14cbcSMatt Macy 	VERIFY0(zap_lookup_norm(mos, bmark_zapobj, name, sizeof (uint64_t),
992eda14cbcSMatt Macy 	    num_ints, &search.dbn_phys, mt, realname, sizeof (realname), NULL));
993eda14cbcSMatt Macy 
994eda14cbcSMatt Macy 	search.dbn_name = realname;
995eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn = avl_find(&ds->ds_bookmarks, &search, NULL);
996eda14cbcSMatt Macy 	ASSERT(dbn != NULL);
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 	if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
999eda14cbcSMatt Macy 		/*
1000eda14cbcSMatt Macy 		 * If this bookmark HAS_FBN, and it is before the most
1001eda14cbcSMatt Macy 		 * recent snapshot, then its TXG is a key in the head's
1002eda14cbcSMatt Macy 		 * deadlist (and all clones' heads' deadlists).  If this is
1003eda14cbcSMatt Macy 		 * the last thing keeping the key (i.e. there are no more
1004eda14cbcSMatt Macy 		 * bookmarks with HAS_FBN at this TXG, and there is no
1005eda14cbcSMatt Macy 		 * snapshot at this TXG), then remove the key.
1006eda14cbcSMatt Macy 		 *
1007eda14cbcSMatt Macy 		 * Note that this algorithm depends on ds_bookmarks being
1008eda14cbcSMatt Macy 		 * sorted such that all bookmarks at the same TXG with
1009eda14cbcSMatt Macy 		 * HAS_FBN are adjacent (with no non-HAS_FBN bookmarks
1010eda14cbcSMatt Macy 		 * at the same TXG in between them).  If this were not
1011eda14cbcSMatt Macy 		 * the case, we would need to examine *all* bookmarks
1012eda14cbcSMatt Macy 		 * at this TXG, rather than just the adjacent ones.
1013eda14cbcSMatt Macy 		 */
1014eda14cbcSMatt Macy 
1015eda14cbcSMatt Macy 		dsl_bookmark_node_t *dbn_prev =
1016eda14cbcSMatt Macy 		    AVL_PREV(&ds->ds_bookmarks, dbn);
1017eda14cbcSMatt Macy 		dsl_bookmark_node_t *dbn_next =
1018eda14cbcSMatt Macy 		    AVL_NEXT(&ds->ds_bookmarks, dbn);
1019eda14cbcSMatt Macy 
1020eda14cbcSMatt Macy 		boolean_t more_bookmarks_at_this_txg =
1021eda14cbcSMatt Macy 		    (dbn_prev != NULL && dbn_prev->dbn_phys.zbm_creation_txg ==
1022eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_creation_txg &&
1023eda14cbcSMatt Macy 		    (dbn_prev->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) ||
1024eda14cbcSMatt Macy 		    (dbn_next != NULL && dbn_next->dbn_phys.zbm_creation_txg ==
1025eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_creation_txg &&
1026eda14cbcSMatt Macy 		    (dbn_next->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN));
1027eda14cbcSMatt Macy 
1028eda14cbcSMatt Macy 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS) &&
1029eda14cbcSMatt Macy 		    !more_bookmarks_at_this_txg &&
1030eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_creation_txg <
1031eda14cbcSMatt Macy 		    dsl_dataset_phys(ds)->ds_prev_snap_txg) {
1032eda14cbcSMatt Macy 			dsl_dir_remove_clones_key(ds->ds_dir,
1033eda14cbcSMatt Macy 			    dbn->dbn_phys.zbm_creation_txg, tx);
1034eda14cbcSMatt Macy 			dsl_deadlist_remove_key(&ds->ds_deadlist,
1035eda14cbcSMatt Macy 			    dbn->dbn_phys.zbm_creation_txg, tx);
1036eda14cbcSMatt Macy 		}
1037eda14cbcSMatt Macy 
1038eda14cbcSMatt Macy 		spa_feature_decr(dmu_objset_spa(mos),
1039eda14cbcSMatt Macy 		    SPA_FEATURE_BOOKMARK_WRITTEN, tx);
1040eda14cbcSMatt Macy 	}
1041eda14cbcSMatt Macy 
1042eda14cbcSMatt Macy 	if (dbn->dbn_phys.zbm_redaction_obj != 0) {
1043eda14cbcSMatt Macy 		VERIFY0(dmu_object_free(mos,
1044eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_redaction_obj, tx));
1045eda14cbcSMatt Macy 		spa_feature_decr(dmu_objset_spa(mos),
1046eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTION_BOOKMARKS, tx);
1047eda14cbcSMatt Macy 	}
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy 	avl_remove(&ds->ds_bookmarks, dbn);
1050eda14cbcSMatt Macy 	spa_strfree(dbn->dbn_name);
1051eda14cbcSMatt Macy 	mutex_destroy(&dbn->dbn_lock);
1052eda14cbcSMatt Macy 	kmem_free(dbn, sizeof (*dbn));
1053eda14cbcSMatt Macy 
1054eda14cbcSMatt Macy 	VERIFY0(zap_remove_norm(mos, bmark_zapobj, name, mt, tx));
1055eda14cbcSMatt Macy }
1056eda14cbcSMatt Macy 
1057eda14cbcSMatt Macy static int
1058eda14cbcSMatt Macy dsl_bookmark_destroy_check(void *arg, dmu_tx_t *tx)
1059eda14cbcSMatt Macy {
1060eda14cbcSMatt Macy 	dsl_bookmark_destroy_arg_t *dbda = arg;
1061eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1062eda14cbcSMatt Macy 	int rv = 0;
1063eda14cbcSMatt Macy 
1064eda14cbcSMatt Macy 	ASSERT(nvlist_empty(dbda->dbda_success));
1065eda14cbcSMatt Macy 	ASSERT(nvlist_empty(dbda->dbda_errors));
1066eda14cbcSMatt Macy 
1067eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARKS))
1068eda14cbcSMatt Macy 		return (0);
1069eda14cbcSMatt Macy 
1070eda14cbcSMatt Macy 	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_bmarks, NULL);
1071eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_bmarks, pair)) {
1072eda14cbcSMatt Macy 		const char *fullname = nvpair_name(pair);
1073eda14cbcSMatt Macy 		dsl_dataset_t *ds;
1074eda14cbcSMatt Macy 		zfs_bookmark_phys_t bm;
1075eda14cbcSMatt Macy 		int error;
1076eda14cbcSMatt Macy 		char *shortname;
1077eda14cbcSMatt Macy 
1078eda14cbcSMatt Macy 		error = dsl_bookmark_hold_ds(dp, fullname, &ds,
1079eda14cbcSMatt Macy 		    FTAG, &shortname);
1080eda14cbcSMatt Macy 		if (error == ENOENT) {
1081eda14cbcSMatt Macy 			/* ignore it; the bookmark is "already destroyed" */
1082eda14cbcSMatt Macy 			continue;
1083eda14cbcSMatt Macy 		}
1084eda14cbcSMatt Macy 		if (error == 0) {
1085eda14cbcSMatt Macy 			error = dsl_bookmark_lookup_impl(ds, shortname, &bm);
1086eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
1087eda14cbcSMatt Macy 			if (error == ESRCH) {
1088eda14cbcSMatt Macy 				/*
1089eda14cbcSMatt Macy 				 * ignore it; the bookmark is
1090eda14cbcSMatt Macy 				 * "already destroyed"
1091eda14cbcSMatt Macy 				 */
1092eda14cbcSMatt Macy 				continue;
1093eda14cbcSMatt Macy 			}
1094eda14cbcSMatt Macy 			if (error == 0 && bm.zbm_redaction_obj != 0) {
1095eda14cbcSMatt Macy 				redaction_list_t *rl = NULL;
1096eda14cbcSMatt Macy 				error = dsl_redaction_list_hold_obj(tx->tx_pool,
1097eda14cbcSMatt Macy 				    bm.zbm_redaction_obj, FTAG, &rl);
1098eda14cbcSMatt Macy 				if (error == ENOENT) {
1099eda14cbcSMatt Macy 					error = 0;
1100eda14cbcSMatt Macy 				} else if (error == 0 &&
1101eda14cbcSMatt Macy 				    dsl_redaction_list_long_held(rl)) {
1102eda14cbcSMatt Macy 					error = SET_ERROR(EBUSY);
1103eda14cbcSMatt Macy 				}
1104eda14cbcSMatt Macy 				if (rl != NULL) {
1105eda14cbcSMatt Macy 					dsl_redaction_list_rele(rl, FTAG);
1106eda14cbcSMatt Macy 				}
1107eda14cbcSMatt Macy 			}
1108eda14cbcSMatt Macy 		}
1109eda14cbcSMatt Macy 		if (error == 0) {
1110eda14cbcSMatt Macy 			if (dmu_tx_is_syncing(tx)) {
1111eda14cbcSMatt Macy 				fnvlist_add_boolean(dbda->dbda_success,
1112eda14cbcSMatt Macy 				    fullname);
1113eda14cbcSMatt Macy 			}
1114eda14cbcSMatt Macy 		} else {
1115eda14cbcSMatt Macy 			fnvlist_add_int32(dbda->dbda_errors, fullname, error);
1116eda14cbcSMatt Macy 			rv = error;
1117eda14cbcSMatt Macy 		}
1118eda14cbcSMatt Macy 	}
1119eda14cbcSMatt Macy 	return (rv);
1120eda14cbcSMatt Macy }
1121eda14cbcSMatt Macy 
1122eda14cbcSMatt Macy static void
1123eda14cbcSMatt Macy dsl_bookmark_destroy_sync(void *arg, dmu_tx_t *tx)
1124eda14cbcSMatt Macy {
1125eda14cbcSMatt Macy 	dsl_bookmark_destroy_arg_t *dbda = arg;
1126eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1127eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1128eda14cbcSMatt Macy 
1129eda14cbcSMatt Macy 	for (nvpair_t *pair = nvlist_next_nvpair(dbda->dbda_success, NULL);
1130eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(dbda->dbda_success, pair)) {
1131eda14cbcSMatt Macy 		dsl_dataset_t *ds;
1132eda14cbcSMatt Macy 		char *shortname;
1133eda14cbcSMatt Macy 		uint64_t zap_cnt;
1134eda14cbcSMatt Macy 
1135eda14cbcSMatt Macy 		VERIFY0(dsl_bookmark_hold_ds(dp, nvpair_name(pair),
1136eda14cbcSMatt Macy 		    &ds, FTAG, &shortname));
1137eda14cbcSMatt Macy 		dsl_bookmark_destroy_sync_impl(ds, shortname, tx);
1138eda14cbcSMatt Macy 
1139eda14cbcSMatt Macy 		/*
1140eda14cbcSMatt Macy 		 * If all of this dataset's bookmarks have been destroyed,
1141eda14cbcSMatt Macy 		 * free the zap object and decrement the feature's use count.
1142eda14cbcSMatt Macy 		 */
1143eda14cbcSMatt Macy 		VERIFY0(zap_count(mos, ds->ds_bookmarks_obj, &zap_cnt));
1144eda14cbcSMatt Macy 		if (zap_cnt == 0) {
1145eda14cbcSMatt Macy 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
1146eda14cbcSMatt Macy 			VERIFY0(zap_destroy(mos, ds->ds_bookmarks_obj, tx));
1147eda14cbcSMatt Macy 			ds->ds_bookmarks_obj = 0;
1148eda14cbcSMatt Macy 			spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
1149eda14cbcSMatt Macy 			VERIFY0(zap_remove(mos, ds->ds_object,
1150eda14cbcSMatt Macy 			    DS_FIELD_BOOKMARK_NAMES, tx));
1151eda14cbcSMatt Macy 		}
1152eda14cbcSMatt Macy 
1153eda14cbcSMatt Macy 		spa_history_log_internal_ds(ds, "remove bookmark", tx,
1154eda14cbcSMatt Macy 		    "name=%s", shortname);
1155eda14cbcSMatt Macy 
1156eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1157eda14cbcSMatt Macy 	}
1158eda14cbcSMatt Macy }
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy /*
1161eda14cbcSMatt Macy  * The bookmarks must all be in the same pool.
1162eda14cbcSMatt Macy  */
1163eda14cbcSMatt Macy int
1164eda14cbcSMatt Macy dsl_bookmark_destroy(nvlist_t *bmarks, nvlist_t *errors)
1165eda14cbcSMatt Macy {
1166eda14cbcSMatt Macy 	int rv;
1167eda14cbcSMatt Macy 	dsl_bookmark_destroy_arg_t dbda;
1168eda14cbcSMatt Macy 	nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
1169eda14cbcSMatt Macy 	if (pair == NULL)
1170eda14cbcSMatt Macy 		return (0);
1171eda14cbcSMatt Macy 
1172eda14cbcSMatt Macy 	dbda.dbda_bmarks = bmarks;
1173eda14cbcSMatt Macy 	dbda.dbda_errors = errors;
1174eda14cbcSMatt Macy 	dbda.dbda_success = fnvlist_alloc();
1175eda14cbcSMatt Macy 
1176eda14cbcSMatt Macy 	rv = dsl_sync_task(nvpair_name(pair), dsl_bookmark_destroy_check,
1177eda14cbcSMatt Macy 	    dsl_bookmark_destroy_sync, &dbda, fnvlist_num_pairs(bmarks),
1178eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_RESERVED);
1179eda14cbcSMatt Macy 	fnvlist_free(dbda.dbda_success);
1180eda14cbcSMatt Macy 	return (rv);
1181eda14cbcSMatt Macy }
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy /* Return B_TRUE if there are any long holds on this dataset. */
1184eda14cbcSMatt Macy boolean_t
1185eda14cbcSMatt Macy dsl_redaction_list_long_held(redaction_list_t *rl)
1186eda14cbcSMatt Macy {
1187eda14cbcSMatt Macy 	return (!zfs_refcount_is_zero(&rl->rl_longholds));
1188eda14cbcSMatt Macy }
1189eda14cbcSMatt Macy 
1190eda14cbcSMatt Macy void
1191a0b956f5SMartin Matuska dsl_redaction_list_long_hold(dsl_pool_t *dp, redaction_list_t *rl,
1192a0b956f5SMartin Matuska     const void *tag)
1193eda14cbcSMatt Macy {
1194eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
1195eda14cbcSMatt Macy 	(void) zfs_refcount_add(&rl->rl_longholds, tag);
1196eda14cbcSMatt Macy }
1197eda14cbcSMatt Macy 
1198eda14cbcSMatt Macy void
1199a0b956f5SMartin Matuska dsl_redaction_list_long_rele(redaction_list_t *rl, const void *tag)
1200eda14cbcSMatt Macy {
1201eda14cbcSMatt Macy 	(void) zfs_refcount_remove(&rl->rl_longholds, tag);
1202eda14cbcSMatt Macy }
1203eda14cbcSMatt Macy 
1204eda14cbcSMatt Macy static void
1205eda14cbcSMatt Macy redaction_list_evict_sync(void *rlu)
1206eda14cbcSMatt Macy {
1207eda14cbcSMatt Macy 	redaction_list_t *rl = rlu;
1208eda14cbcSMatt Macy 	zfs_refcount_destroy(&rl->rl_longholds);
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	kmem_free(rl, sizeof (redaction_list_t));
1211eda14cbcSMatt Macy }
1212eda14cbcSMatt Macy 
1213eda14cbcSMatt Macy void
1214a0b956f5SMartin Matuska dsl_redaction_list_rele(redaction_list_t *rl, const void *tag)
1215eda14cbcSMatt Macy {
1216eda14cbcSMatt Macy 	dmu_buf_rele(rl->rl_dbuf, tag);
1217eda14cbcSMatt Macy }
1218eda14cbcSMatt Macy 
1219eda14cbcSMatt Macy int
1220a0b956f5SMartin Matuska dsl_redaction_list_hold_obj(dsl_pool_t *dp, uint64_t rlobj, const void *tag,
1221eda14cbcSMatt Macy     redaction_list_t **rlp)
1222eda14cbcSMatt Macy {
1223eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1224eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
1225eda14cbcSMatt Macy 	redaction_list_t *rl;
1226eda14cbcSMatt Macy 	int err;
1227eda14cbcSMatt Macy 
1228eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
1229eda14cbcSMatt Macy 
1230eda14cbcSMatt Macy 	err = dmu_bonus_hold(mos, rlobj, tag, &dbuf);
1231eda14cbcSMatt Macy 	if (err != 0)
1232eda14cbcSMatt Macy 		return (err);
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy 	rl = dmu_buf_get_user(dbuf);
1235eda14cbcSMatt Macy 	if (rl == NULL) {
1236eda14cbcSMatt Macy 		redaction_list_t *winner = NULL;
1237eda14cbcSMatt Macy 
1238eda14cbcSMatt Macy 		rl = kmem_zalloc(sizeof (redaction_list_t), KM_SLEEP);
1239eda14cbcSMatt Macy 		rl->rl_dbuf = dbuf;
1240eda14cbcSMatt Macy 		rl->rl_object = rlobj;
1241eda14cbcSMatt Macy 		rl->rl_phys = dbuf->db_data;
1242eda14cbcSMatt Macy 		rl->rl_mos = dp->dp_meta_objset;
1243eda14cbcSMatt Macy 		zfs_refcount_create(&rl->rl_longholds);
1244eda14cbcSMatt Macy 		dmu_buf_init_user(&rl->rl_dbu, redaction_list_evict_sync, NULL,
1245eda14cbcSMatt Macy 		    &rl->rl_dbuf);
1246eda14cbcSMatt Macy 		if ((winner = dmu_buf_set_user_ie(dbuf, &rl->rl_dbu)) != NULL) {
1247eda14cbcSMatt Macy 			kmem_free(rl, sizeof (*rl));
1248eda14cbcSMatt Macy 			rl = winner;
1249eda14cbcSMatt Macy 		}
1250eda14cbcSMatt Macy 	}
1251eda14cbcSMatt Macy 	*rlp = rl;
1252eda14cbcSMatt Macy 	return (0);
1253eda14cbcSMatt Macy }
1254eda14cbcSMatt Macy 
1255eda14cbcSMatt Macy /*
1256eda14cbcSMatt Macy  * Snapshot ds is being destroyed.
1257eda14cbcSMatt Macy  *
1258eda14cbcSMatt Macy  * Adjust the "freed_before_next" of any bookmarks between this snap
1259eda14cbcSMatt Macy  * and the previous snapshot, because their "next snapshot" is changing.
1260eda14cbcSMatt Macy  *
1261eda14cbcSMatt Macy  * If there are any bookmarks with HAS_FBN at this snapshot, remove
1262eda14cbcSMatt Macy  * their HAS_SNAP flag (note: there can be at most one snapshot of
1263eda14cbcSMatt Macy  * each filesystem at a given txg), and return B_TRUE.  In this case
1264eda14cbcSMatt Macy  * the caller can not remove the key in the deadlist at this TXG, because
1265eda14cbcSMatt Macy  * the HAS_FBN bookmarks require the key be there.
1266eda14cbcSMatt Macy  *
1267eda14cbcSMatt Macy  * Returns B_FALSE if there are no bookmarks with HAS_FBN at this
1268eda14cbcSMatt Macy  * snapshot's TXG.  In this case the caller can remove the key in the
1269eda14cbcSMatt Macy  * deadlist at this TXG.
1270eda14cbcSMatt Macy  */
1271eda14cbcSMatt Macy boolean_t
1272eda14cbcSMatt Macy dsl_bookmark_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
1273eda14cbcSMatt Macy {
1274eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1275eda14cbcSMatt Macy 
1276eda14cbcSMatt Macy 	dsl_dataset_t *head, *next;
1277eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp,
1278eda14cbcSMatt Macy 	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &head));
1279eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp,
1280eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &next));
1281eda14cbcSMatt Macy 
1282eda14cbcSMatt Macy 	/*
1283eda14cbcSMatt Macy 	 * Find the first bookmark that HAS_FBN at or after the
1284eda14cbcSMatt Macy 	 * previous snapshot.
1285eda14cbcSMatt Macy 	 */
1286eda14cbcSMatt Macy 	dsl_bookmark_node_t search = { 0 };
1287eda14cbcSMatt Macy 	avl_index_t idx;
1288eda14cbcSMatt Macy 	search.dbn_phys.zbm_creation_txg =
1289eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1290eda14cbcSMatt Macy 	search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1291eda14cbcSMatt Macy 	/*
1292eda14cbcSMatt Macy 	 * The empty-string name can't be in the AVL, and it compares
1293eda14cbcSMatt Macy 	 * before any entries with this TXG.
1294eda14cbcSMatt Macy 	 */
1295a0b956f5SMartin Matuska 	search.dbn_name = (char *)"";
1296eda14cbcSMatt Macy 	VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1297eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn =
1298eda14cbcSMatt Macy 	    avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1299eda14cbcSMatt Macy 
1300eda14cbcSMatt Macy 	/*
1301eda14cbcSMatt Macy 	 * Iterate over all bookmarks that are at or after the previous
1302eda14cbcSMatt Macy 	 * snapshot, and before this (being deleted) snapshot.  Adjust
1303eda14cbcSMatt Macy 	 * their FBN based on their new next snapshot.
1304eda14cbcSMatt Macy 	 */
1305eda14cbcSMatt Macy 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg <
1306eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_creation_txg;
1307eda14cbcSMatt Macy 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1308eda14cbcSMatt Macy 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN))
1309eda14cbcSMatt Macy 			continue;
1310eda14cbcSMatt Macy 		/*
1311eda14cbcSMatt Macy 		 * Increase our FBN by the amount of space that was live
1312eda14cbcSMatt Macy 		 * (referenced) at the time of this bookmark (i.e.
1313eda14cbcSMatt Macy 		 * birth <= zbm_creation_txg), and killed between this
1314eda14cbcSMatt Macy 		 * (being deleted) snapshot and the next snapshot (i.e.
1315eda14cbcSMatt Macy 		 * on the next snapshot's deadlist).  (Space killed before
1316eda14cbcSMatt Macy 		 * this are already on our FBN.)
1317eda14cbcSMatt Macy 		 */
1318eda14cbcSMatt Macy 		uint64_t referenced, compressed, uncompressed;
1319eda14cbcSMatt Macy 		dsl_deadlist_space_range(&next->ds_deadlist,
1320eda14cbcSMatt Macy 		    0, dbn->dbn_phys.zbm_creation_txg,
1321eda14cbcSMatt Macy 		    &referenced, &compressed, &uncompressed);
1322eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=
1323eda14cbcSMatt Macy 		    referenced;
1324eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=
1325eda14cbcSMatt Macy 		    compressed;
1326eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=
1327eda14cbcSMatt Macy 		    uncompressed;
1328eda14cbcSMatt Macy 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1329eda14cbcSMatt Macy 		    dbn->dbn_name, sizeof (uint64_t),
1330eda14cbcSMatt Macy 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1331eda14cbcSMatt Macy 		    &dbn->dbn_phys, tx));
1332eda14cbcSMatt Macy 	}
1333eda14cbcSMatt Macy 	dsl_dataset_rele(next, FTAG);
1334eda14cbcSMatt Macy 
1335eda14cbcSMatt Macy 	/*
1336eda14cbcSMatt Macy 	 * There may be several bookmarks at this txg (the TXG of the
1337eda14cbcSMatt Macy 	 * snapshot being deleted).  We need to clear the SNAPSHOT_EXISTS
1338eda14cbcSMatt Macy 	 * flag on all of them, and return TRUE if there is at least 1
1339eda14cbcSMatt Macy 	 * bookmark here with HAS_FBN (thus preventing the deadlist
1340eda14cbcSMatt Macy 	 * key from being removed).
1341eda14cbcSMatt Macy 	 */
1342eda14cbcSMatt Macy 	boolean_t rv = B_FALSE;
1343eda14cbcSMatt Macy 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1344eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_creation_txg;
1345eda14cbcSMatt Macy 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1346eda14cbcSMatt Macy 		if (!(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {
1347eda14cbcSMatt Macy 			ASSERT(!(dbn->dbn_phys.zbm_flags &
1348eda14cbcSMatt Macy 			    ZBM_FLAG_SNAPSHOT_EXISTS));
1349eda14cbcSMatt Macy 			continue;
1350eda14cbcSMatt Macy 		}
1351eda14cbcSMatt Macy 		ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_SNAPSHOT_EXISTS);
1352eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_flags &= ~ZBM_FLAG_SNAPSHOT_EXISTS;
1353eda14cbcSMatt Macy 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1354eda14cbcSMatt Macy 		    dbn->dbn_name, sizeof (uint64_t),
1355eda14cbcSMatt Macy 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1356eda14cbcSMatt Macy 		    &dbn->dbn_phys, tx));
1357eda14cbcSMatt Macy 		rv = B_TRUE;
1358eda14cbcSMatt Macy 	}
1359eda14cbcSMatt Macy 	dsl_dataset_rele(head, FTAG);
1360eda14cbcSMatt Macy 	return (rv);
1361eda14cbcSMatt Macy }
1362eda14cbcSMatt Macy 
1363eda14cbcSMatt Macy /*
1364eda14cbcSMatt Macy  * A snapshot is being created of this (head) dataset.
1365eda14cbcSMatt Macy  *
1366eda14cbcSMatt Macy  * We don't keep keys in the deadlist for the most recent snapshot, or any
1367eda14cbcSMatt Macy  * bookmarks at or after it, because there can't be any blocks on the
1368eda14cbcSMatt Macy  * deadlist in this range.  Now that the most recent snapshot is after
1369eda14cbcSMatt Macy  * all bookmarks, we need to add these keys.  Note that the caller always
1370eda14cbcSMatt Macy  * adds a key at the previous snapshot, so we only add keys for bookmarks
1371eda14cbcSMatt Macy  * after that.
1372eda14cbcSMatt Macy  */
1373eda14cbcSMatt Macy void
1374eda14cbcSMatt Macy dsl_bookmark_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
1375eda14cbcSMatt Macy {
1376eda14cbcSMatt Macy 	uint64_t last_key_added = UINT64_MAX;
1377eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1378eda14cbcSMatt Macy 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >
1379eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1380eda14cbcSMatt Macy 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1381eda14cbcSMatt Macy 		uint64_t creation_txg = dbn->dbn_phys.zbm_creation_txg;
1382eda14cbcSMatt Macy 		ASSERT3U(creation_txg, <=, last_key_added);
1383eda14cbcSMatt Macy 		/*
1384eda14cbcSMatt Macy 		 * Note, there may be multiple bookmarks at this TXG,
1385eda14cbcSMatt Macy 		 * and we only want to add the key for this TXG once.
1386eda14cbcSMatt Macy 		 * The ds_bookmarks AVL is sorted by TXG, so we will visit
1387eda14cbcSMatt Macy 		 * these bookmarks in sequence.
1388eda14cbcSMatt Macy 		 */
1389eda14cbcSMatt Macy 		if ((dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) &&
1390eda14cbcSMatt Macy 		    creation_txg != last_key_added) {
1391eda14cbcSMatt Macy 			dsl_deadlist_add_key(&ds->ds_deadlist,
1392eda14cbcSMatt Macy 			    creation_txg, tx);
1393eda14cbcSMatt Macy 			last_key_added = creation_txg;
1394eda14cbcSMatt Macy 		}
1395eda14cbcSMatt Macy 	}
1396eda14cbcSMatt Macy }
1397eda14cbcSMatt Macy 
1398eda14cbcSMatt Macy /*
1399eda14cbcSMatt Macy  * The next snapshot of the origin dataset has changed, due to
1400eda14cbcSMatt Macy  * promote or clone swap.  If there are any bookmarks at this dataset,
1401eda14cbcSMatt Macy  * we need to update their zbm_*_freed_before_next_snap to reflect this.
1402eda14cbcSMatt Macy  * The head dataset has the relevant bookmarks in ds_bookmarks.
1403eda14cbcSMatt Macy  */
1404eda14cbcSMatt Macy void
1405eda14cbcSMatt Macy dsl_bookmark_next_changed(dsl_dataset_t *head, dsl_dataset_t *origin,
1406eda14cbcSMatt Macy     dmu_tx_t *tx)
1407eda14cbcSMatt Macy {
1408eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1409eda14cbcSMatt Macy 
1410eda14cbcSMatt Macy 	/*
1411eda14cbcSMatt Macy 	 * Find the first bookmark that HAS_FBN at the origin snapshot.
1412eda14cbcSMatt Macy 	 */
1413eda14cbcSMatt Macy 	dsl_bookmark_node_t search = { 0 };
1414eda14cbcSMatt Macy 	avl_index_t idx;
1415eda14cbcSMatt Macy 	search.dbn_phys.zbm_creation_txg =
1416eda14cbcSMatt Macy 	    dsl_dataset_phys(origin)->ds_creation_txg;
1417eda14cbcSMatt Macy 	search.dbn_phys.zbm_flags = ZBM_FLAG_HAS_FBN;
1418eda14cbcSMatt Macy 	/*
1419eda14cbcSMatt Macy 	 * The empty-string name can't be in the AVL, and it compares
1420eda14cbcSMatt Macy 	 * before any entries with this TXG.
1421eda14cbcSMatt Macy 	 */
1422a0b956f5SMartin Matuska 	search.dbn_name = (char *)"";
1423eda14cbcSMatt Macy 	VERIFY3P(avl_find(&head->ds_bookmarks, &search, &idx), ==, NULL);
1424eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn =
1425eda14cbcSMatt Macy 	    avl_nearest(&head->ds_bookmarks, idx, AVL_AFTER);
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy 	/*
1428eda14cbcSMatt Macy 	 * Iterate over all bookmarks that are at the origin txg.
1429eda14cbcSMatt Macy 	 * Adjust their FBN based on their new next snapshot.
1430eda14cbcSMatt Macy 	 */
1431eda14cbcSMatt Macy 	for (; dbn != NULL && dbn->dbn_phys.zbm_creation_txg ==
1432eda14cbcSMatt Macy 	    dsl_dataset_phys(origin)->ds_creation_txg &&
1433eda14cbcSMatt Macy 	    (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);
1434eda14cbcSMatt Macy 	    dbn = AVL_NEXT(&head->ds_bookmarks, dbn)) {
1435eda14cbcSMatt Macy 
1436eda14cbcSMatt Macy 		/*
1437eda14cbcSMatt Macy 		 * Bookmark is at the origin, therefore its
1438eda14cbcSMatt Macy 		 * "next dataset" is changing, so we need
1439eda14cbcSMatt Macy 		 * to reset its FBN by recomputing it in
1440eda14cbcSMatt Macy 		 * dsl_bookmark_set_phys().
1441eda14cbcSMatt Macy 		 */
1442eda14cbcSMatt Macy 		ASSERT3U(dbn->dbn_phys.zbm_guid, ==,
1443eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_guid);
1444eda14cbcSMatt Macy 		ASSERT3U(dbn->dbn_phys.zbm_referenced_bytes_refd, ==,
1445eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_referenced_bytes);
1446eda14cbcSMatt Macy 		ASSERT(dbn->dbn_phys.zbm_flags &
1447eda14cbcSMatt Macy 		    ZBM_FLAG_SNAPSHOT_EXISTS);
1448eda14cbcSMatt Macy 		/*
1449eda14cbcSMatt Macy 		 * Save and restore the zbm_redaction_obj, which
1450eda14cbcSMatt Macy 		 * is zeroed by dsl_bookmark_set_phys().
1451eda14cbcSMatt Macy 		 */
1452eda14cbcSMatt Macy 		uint64_t redaction_obj =
1453eda14cbcSMatt Macy 		    dbn->dbn_phys.zbm_redaction_obj;
1454eda14cbcSMatt Macy 		dsl_bookmark_set_phys(&dbn->dbn_phys, origin);
1455eda14cbcSMatt Macy 		dbn->dbn_phys.zbm_redaction_obj = redaction_obj;
1456eda14cbcSMatt Macy 
1457eda14cbcSMatt Macy 		VERIFY0(zap_update(dp->dp_meta_objset, head->ds_bookmarks_obj,
1458eda14cbcSMatt Macy 		    dbn->dbn_name, sizeof (uint64_t),
1459eda14cbcSMatt Macy 		    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1460eda14cbcSMatt Macy 		    &dbn->dbn_phys, tx));
1461eda14cbcSMatt Macy 	}
1462eda14cbcSMatt Macy }
1463eda14cbcSMatt Macy 
1464eda14cbcSMatt Macy /*
1465eda14cbcSMatt Macy  * This block is no longer referenced by this (head) dataset.
1466eda14cbcSMatt Macy  *
1467eda14cbcSMatt Macy  * Adjust the FBN of any bookmarks that reference this block, whose "next"
1468eda14cbcSMatt Macy  * is the head dataset.
1469eda14cbcSMatt Macy  */
1470eda14cbcSMatt Macy void
1471eda14cbcSMatt Macy dsl_bookmark_block_killed(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
1472eda14cbcSMatt Macy {
1473e92ffd9bSMartin Matuska 	(void) tx;
1474e92ffd9bSMartin Matuska 
1475eda14cbcSMatt Macy 	/*
1476eda14cbcSMatt Macy 	 * Iterate over bookmarks whose "next" is the head dataset.
1477eda14cbcSMatt Macy 	 */
1478eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1479eda14cbcSMatt Macy 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=
1480eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1481eda14cbcSMatt Macy 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1482eda14cbcSMatt Macy 		/*
1483eda14cbcSMatt Macy 		 * If the block was live (referenced) at the time of this
1484eda14cbcSMatt Macy 		 * bookmark, add its space to the bookmark's FBN.
1485eda14cbcSMatt Macy 		 */
1486eda14cbcSMatt Macy 		if (bp->blk_birth <= dbn->dbn_phys.zbm_creation_txg &&
1487eda14cbcSMatt Macy 		    (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN)) {
1488eda14cbcSMatt Macy 			mutex_enter(&dbn->dbn_lock);
1489eda14cbcSMatt Macy 			dbn->dbn_phys.zbm_referenced_freed_before_next_snap +=
1490eda14cbcSMatt Macy 			    bp_get_dsize_sync(dsl_dataset_get_spa(ds), bp);
1491eda14cbcSMatt Macy 			dbn->dbn_phys.zbm_compressed_freed_before_next_snap +=
1492eda14cbcSMatt Macy 			    BP_GET_PSIZE(bp);
1493eda14cbcSMatt Macy 			dbn->dbn_phys.zbm_uncompressed_freed_before_next_snap +=
1494eda14cbcSMatt Macy 			    BP_GET_UCSIZE(bp);
1495eda14cbcSMatt Macy 			/*
1496eda14cbcSMatt Macy 			 * Changing the ZAP object here would be too
1497eda14cbcSMatt Macy 			 * expensive.  Also, we may be called from the zio
1498eda14cbcSMatt Macy 			 * interrupt thread, which can't block on i/o.
1499eda14cbcSMatt Macy 			 * Therefore, we mark this bookmark as dirty and
1500eda14cbcSMatt Macy 			 * modify the ZAP once per txg, in
1501eda14cbcSMatt Macy 			 * dsl_bookmark_sync_done().
1502eda14cbcSMatt Macy 			 */
1503eda14cbcSMatt Macy 			dbn->dbn_dirty = B_TRUE;
1504eda14cbcSMatt Macy 			mutex_exit(&dbn->dbn_lock);
1505eda14cbcSMatt Macy 		}
1506eda14cbcSMatt Macy 	}
1507eda14cbcSMatt Macy }
1508eda14cbcSMatt Macy 
1509eda14cbcSMatt Macy void
1510eda14cbcSMatt Macy dsl_bookmark_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
1511eda14cbcSMatt Macy {
1512eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy 	if (dsl_dataset_is_snapshot(ds))
1515eda14cbcSMatt Macy 		return;
1516eda14cbcSMatt Macy 
1517eda14cbcSMatt Macy 	/*
1518eda14cbcSMatt Macy 	 * We only dirty bookmarks that are at or after the most recent
1519eda14cbcSMatt Macy 	 * snapshot.  We can't create snapshots between
1520eda14cbcSMatt Macy 	 * dsl_bookmark_block_killed() and dsl_bookmark_sync_done(), so we
1521eda14cbcSMatt Macy 	 * don't need to look at any bookmarks before ds_prev_snap_txg.
1522eda14cbcSMatt Macy 	 */
1523eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1524eda14cbcSMatt Macy 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg >=
1525eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
1526eda14cbcSMatt Macy 	    dbn = AVL_PREV(&ds->ds_bookmarks, dbn)) {
1527eda14cbcSMatt Macy 		if (dbn->dbn_dirty) {
1528eda14cbcSMatt Macy 			/*
1529eda14cbcSMatt Macy 			 * We only dirty nodes with HAS_FBN, therefore
1530eda14cbcSMatt Macy 			 * we can always use the current bookmark struct size.
1531eda14cbcSMatt Macy 			 */
1532eda14cbcSMatt Macy 			ASSERT(dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN);
1533eda14cbcSMatt Macy 			VERIFY0(zap_update(dp->dp_meta_objset,
1534eda14cbcSMatt Macy 			    ds->ds_bookmarks_obj,
1535eda14cbcSMatt Macy 			    dbn->dbn_name, sizeof (uint64_t),
1536eda14cbcSMatt Macy 			    sizeof (zfs_bookmark_phys_t) / sizeof (uint64_t),
1537eda14cbcSMatt Macy 			    &dbn->dbn_phys, tx));
1538eda14cbcSMatt Macy 			dbn->dbn_dirty = B_FALSE;
1539eda14cbcSMatt Macy 		}
1540eda14cbcSMatt Macy 	}
1541eda14cbcSMatt Macy #ifdef ZFS_DEBUG
1542eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_first(&ds->ds_bookmarks);
1543eda14cbcSMatt Macy 	    dbn != NULL; dbn = AVL_NEXT(&ds->ds_bookmarks, dbn)) {
1544eda14cbcSMatt Macy 		ASSERT(!dbn->dbn_dirty);
1545eda14cbcSMatt Macy 	}
1546eda14cbcSMatt Macy #endif
1547eda14cbcSMatt Macy }
1548eda14cbcSMatt Macy 
1549eda14cbcSMatt Macy /*
1550eda14cbcSMatt Macy  * Return the TXG of the most recent bookmark (or 0 if there are no bookmarks).
1551eda14cbcSMatt Macy  */
1552eda14cbcSMatt Macy uint64_t
1553eda14cbcSMatt Macy dsl_bookmark_latest_txg(dsl_dataset_t *ds)
1554eda14cbcSMatt Macy {
1555eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1556eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn = avl_last(&ds->ds_bookmarks);
1557eda14cbcSMatt Macy 	if (dbn == NULL)
1558eda14cbcSMatt Macy 		return (0);
1559eda14cbcSMatt Macy 	return (dbn->dbn_phys.zbm_creation_txg);
1560eda14cbcSMatt Macy }
1561eda14cbcSMatt Macy 
1562eda14cbcSMatt Macy /*
1563eda14cbcSMatt Macy  * Compare the redact_block_phys_t to the bookmark. If the last block in the
1564eda14cbcSMatt Macy  * redact_block_phys_t is before the bookmark, return -1.  If the first block in
1565eda14cbcSMatt Macy  * the redact_block_phys_t is after the bookmark, return 1.  Otherwise, the
1566eda14cbcSMatt Macy  * bookmark is inside the range of the redact_block_phys_t, and we return 0.
1567eda14cbcSMatt Macy  */
1568eda14cbcSMatt Macy static int
1569eda14cbcSMatt Macy redact_block_zb_compare(redact_block_phys_t *first,
1570eda14cbcSMatt Macy     zbookmark_phys_t *second)
1571eda14cbcSMatt Macy {
1572eda14cbcSMatt Macy 	/*
1573eda14cbcSMatt Macy 	 * If the block_phys is for a previous object, or the last block in the
1574eda14cbcSMatt Macy 	 * block_phys is strictly before the block in the bookmark, the
1575eda14cbcSMatt Macy 	 * block_phys is earlier.
1576eda14cbcSMatt Macy 	 */
1577eda14cbcSMatt Macy 	if (first->rbp_object < second->zb_object ||
1578eda14cbcSMatt Macy 	    (first->rbp_object == second->zb_object &&
1579eda14cbcSMatt Macy 	    first->rbp_blkid + (redact_block_get_count(first) - 1) <
1580eda14cbcSMatt Macy 	    second->zb_blkid)) {
1581eda14cbcSMatt Macy 		return (-1);
1582eda14cbcSMatt Macy 	}
1583eda14cbcSMatt Macy 
1584eda14cbcSMatt Macy 	/*
1585eda14cbcSMatt Macy 	 * If the bookmark is for a previous object, or the block in the
1586eda14cbcSMatt Macy 	 * bookmark is strictly before the first block in the block_phys, the
1587eda14cbcSMatt Macy 	 * bookmark is earlier.
1588eda14cbcSMatt Macy 	 */
1589eda14cbcSMatt Macy 	if (first->rbp_object > second->zb_object ||
1590eda14cbcSMatt Macy 	    (first->rbp_object == second->zb_object &&
1591eda14cbcSMatt Macy 	    first->rbp_blkid > second->zb_blkid)) {
1592eda14cbcSMatt Macy 		return (1);
1593eda14cbcSMatt Macy 	}
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy 	return (0);
1596eda14cbcSMatt Macy }
1597eda14cbcSMatt Macy 
1598eda14cbcSMatt Macy /*
1599eda14cbcSMatt Macy  * Traverse the redaction list in the provided object, and call the callback for
1600eda14cbcSMatt Macy  * each entry we find. Don't call the callback for any records before resume.
1601eda14cbcSMatt Macy  */
1602eda14cbcSMatt Macy int
1603eda14cbcSMatt Macy dsl_redaction_list_traverse(redaction_list_t *rl, zbookmark_phys_t *resume,
1604eda14cbcSMatt Macy     rl_traverse_callback_t cb, void *arg)
1605eda14cbcSMatt Macy {
1606eda14cbcSMatt Macy 	objset_t *mos = rl->rl_mos;
1607eda14cbcSMatt Macy 	int err = 0;
1608eda14cbcSMatt Macy 
1609eda14cbcSMatt Macy 	if (rl->rl_phys->rlp_last_object != UINT64_MAX ||
1610eda14cbcSMatt Macy 	    rl->rl_phys->rlp_last_blkid != UINT64_MAX) {
1611eda14cbcSMatt Macy 		/*
1612eda14cbcSMatt Macy 		 * When we finish a send, we update the last object and offset
1613eda14cbcSMatt Macy 		 * to UINT64_MAX.  If a send fails partway through, the last
1614eda14cbcSMatt Macy 		 * object and offset will have some other value, indicating how
1615eda14cbcSMatt Macy 		 * far the send got. The redaction list must be complete before
1616eda14cbcSMatt Macy 		 * it can be traversed, so return EINVAL if the last object and
1617eda14cbcSMatt Macy 		 * blkid are not set to UINT64_MAX.
1618eda14cbcSMatt Macy 		 */
1619eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1620eda14cbcSMatt Macy 	}
1621eda14cbcSMatt Macy 
1622eda14cbcSMatt Macy 	/*
16237877fdebSMatt Macy 	 * This allows us to skip the binary search and resume checking logic
16247877fdebSMatt Macy 	 * below, if we're not resuming a redacted send.
1625eda14cbcSMatt Macy 	 */
16267877fdebSMatt Macy 	if (ZB_IS_ZERO(resume))
16277877fdebSMatt Macy 		resume = NULL;
16287877fdebSMatt Macy 
16297877fdebSMatt Macy 	/*
16307877fdebSMatt Macy 	 * Binary search for the point to resume from.
16317877fdebSMatt Macy 	 */
16327877fdebSMatt Macy 	uint64_t maxidx = rl->rl_phys->rlp_num_entries - 1;
16337877fdebSMatt Macy 	uint64_t minidx = 0;
16347877fdebSMatt Macy 	while (resume != NULL && maxidx > minidx) {
16357877fdebSMatt Macy 		redact_block_phys_t rbp = { 0 };
16367877fdebSMatt Macy 		ASSERT3U(maxidx, >, minidx);
16377877fdebSMatt Macy 		uint64_t mididx = minidx + ((maxidx - minidx) / 2);
16387877fdebSMatt Macy 		err = dmu_read(mos, rl->rl_object, mididx * sizeof (rbp),
16397877fdebSMatt Macy 		    sizeof (rbp), &rbp, DMU_READ_NO_PREFETCH);
1640eda14cbcSMatt Macy 		if (err != 0)
1641eda14cbcSMatt Macy 			break;
1642eda14cbcSMatt Macy 
16437877fdebSMatt Macy 		int cmp = redact_block_zb_compare(&rbp, resume);
1644eda14cbcSMatt Macy 
16457877fdebSMatt Macy 		if (cmp == 0) {
16467877fdebSMatt Macy 			minidx = mididx;
1647eda14cbcSMatt Macy 			break;
16487877fdebSMatt Macy 		} else if (cmp > 0) {
16497877fdebSMatt Macy 			maxidx =
16507877fdebSMatt Macy 			    (mididx == minidx ? minidx : mididx - 1);
16517877fdebSMatt Macy 		} else {
16527877fdebSMatt Macy 			minidx = mididx + 1;
16537877fdebSMatt Macy 		}
1654eda14cbcSMatt Macy 	}
1655eda14cbcSMatt Macy 
16567877fdebSMatt Macy 	unsigned int bufsize = SPA_OLD_MAXBLOCKSIZE;
16577877fdebSMatt Macy 	redact_block_phys_t *buf = zio_data_buf_alloc(bufsize);
16587877fdebSMatt Macy 
16597877fdebSMatt Macy 	unsigned int entries_per_buf = bufsize / sizeof (redact_block_phys_t);
16607877fdebSMatt Macy 	uint64_t start_block = minidx / entries_per_buf;
16617877fdebSMatt Macy 	err = dmu_read(mos, rl->rl_object, start_block * bufsize, bufsize, buf,
16627877fdebSMatt Macy 	    DMU_READ_PREFETCH);
16637877fdebSMatt Macy 
16647877fdebSMatt Macy 	for (uint64_t curidx = minidx;
1665eda14cbcSMatt Macy 	    err == 0 && curidx < rl->rl_phys->rlp_num_entries;
1666eda14cbcSMatt Macy 	    curidx++) {
1667eda14cbcSMatt Macy 		/*
1668eda14cbcSMatt Macy 		 * We read in the redaction list one block at a time.  Once we
1669eda14cbcSMatt Macy 		 * finish with all the entries in a given block, we read in a
1670eda14cbcSMatt Macy 		 * new one.  The predictive prefetcher will take care of any
1671eda14cbcSMatt Macy 		 * prefetching, and this code shouldn't be the bottleneck, so we
1672eda14cbcSMatt Macy 		 * don't need to do manual prefetching.
1673eda14cbcSMatt Macy 		 */
16747877fdebSMatt Macy 		if (curidx % entries_per_buf == 0) {
1675eda14cbcSMatt Macy 			err = dmu_read(mos, rl->rl_object, curidx *
1676eda14cbcSMatt Macy 			    sizeof (*buf), bufsize, buf,
1677eda14cbcSMatt Macy 			    DMU_READ_PREFETCH);
1678eda14cbcSMatt Macy 			if (err != 0)
1679eda14cbcSMatt Macy 				break;
1680eda14cbcSMatt Macy 		}
16817877fdebSMatt Macy 		redact_block_phys_t *rb = &buf[curidx % entries_per_buf];
1682eda14cbcSMatt Macy 		/*
1683eda14cbcSMatt Macy 		 * If resume is non-null, we should either not send the data, or
1684eda14cbcSMatt Macy 		 * null out resume so we don't have to keep doing these
1685eda14cbcSMatt Macy 		 * comparisons.
1686eda14cbcSMatt Macy 		 */
1687eda14cbcSMatt Macy 		if (resume != NULL) {
16887877fdebSMatt Macy 			/*
16897877fdebSMatt Macy 			 * It is possible that after the binary search we got
16907877fdebSMatt Macy 			 * a record before the resume point. There's two cases
16917877fdebSMatt Macy 			 * where this can occur. If the record is the last
16927877fdebSMatt Macy 			 * redaction record, and the resume point is after the
16937877fdebSMatt Macy 			 * end of the redacted data, curidx will be the last
16947877fdebSMatt Macy 			 * redaction record. In that case, the loop will end
16957877fdebSMatt Macy 			 * after this iteration. The second case is if the
16967877fdebSMatt Macy 			 * resume point is between two redaction records, the
16977877fdebSMatt Macy 			 * binary search can return either the record before
16987877fdebSMatt Macy 			 * or after the resume point. In that case, the next
16997877fdebSMatt Macy 			 * iteration will be greater than the resume point.
17007877fdebSMatt Macy 			 */
1701eda14cbcSMatt Macy 			if (redact_block_zb_compare(rb, resume) < 0) {
17027877fdebSMatt Macy 				ASSERT3U(curidx, ==, minidx);
1703eda14cbcSMatt Macy 				continue;
1704eda14cbcSMatt Macy 			} else {
1705eda14cbcSMatt Macy 				/*
1706eda14cbcSMatt Macy 				 * If the place to resume is in the middle of
1707eda14cbcSMatt Macy 				 * the range described by this
1708eda14cbcSMatt Macy 				 * redact_block_phys, then modify the
1709eda14cbcSMatt Macy 				 * redact_block_phys in memory so we generate
1710eda14cbcSMatt Macy 				 * the right records.
1711eda14cbcSMatt Macy 				 */
1712eda14cbcSMatt Macy 				if (resume->zb_object == rb->rbp_object &&
1713eda14cbcSMatt Macy 				    resume->zb_blkid > rb->rbp_blkid) {
1714eda14cbcSMatt Macy 					uint64_t diff = resume->zb_blkid -
1715eda14cbcSMatt Macy 					    rb->rbp_blkid;
1716eda14cbcSMatt Macy 					rb->rbp_blkid = resume->zb_blkid;
1717eda14cbcSMatt Macy 					redact_block_set_count(rb,
1718eda14cbcSMatt Macy 					    redact_block_get_count(rb) - diff);
1719eda14cbcSMatt Macy 				}
1720eda14cbcSMatt Macy 				resume = NULL;
1721eda14cbcSMatt Macy 			}
1722eda14cbcSMatt Macy 		}
1723eda14cbcSMatt Macy 
17247877fdebSMatt Macy 		if (cb(rb, arg) != 0) {
17257877fdebSMatt Macy 			err = EINTR;
1726eda14cbcSMatt Macy 			break;
1727eda14cbcSMatt Macy 		}
17287877fdebSMatt Macy 	}
1729eda14cbcSMatt Macy 
1730eda14cbcSMatt Macy 	zio_data_buf_free(buf, bufsize);
1731eda14cbcSMatt Macy 	return (err);
1732eda14cbcSMatt Macy }
1733