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