xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_redact.c (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2017, 2018 by Delphix. All rights reserved.
23eda14cbcSMatt Macy  */
24eda14cbcSMatt Macy 
25eda14cbcSMatt Macy #include <sys/zfs_context.h>
26eda14cbcSMatt Macy #include <sys/txg.h>
27eda14cbcSMatt Macy #include <sys/dmu_objset.h>
28eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
29eda14cbcSMatt Macy #include <sys/dmu_redact.h>
30eda14cbcSMatt Macy #include <sys/bqueue.h>
31eda14cbcSMatt Macy #include <sys/objlist.h>
32eda14cbcSMatt Macy #include <sys/dmu_tx.h>
33eda14cbcSMatt Macy #ifdef _KERNEL
34eda14cbcSMatt Macy #include <sys/zfs_vfsops.h>
35eda14cbcSMatt Macy #include <sys/zap.h>
36eda14cbcSMatt Macy #include <sys/zfs_znode.h>
37eda14cbcSMatt Macy #endif
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy /*
40eda14cbcSMatt Macy  * This controls the number of entries in the buffer the redaction_list_update
41eda14cbcSMatt Macy  * synctask uses to buffer writes to the redaction list.
42eda14cbcSMatt Macy  */
43e92ffd9bSMartin Matuska static const int redact_sync_bufsize = 1024;
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy /*
46eda14cbcSMatt Macy  * Controls how often to update the redaction list when creating a redaction
47eda14cbcSMatt Macy  * list.
48eda14cbcSMatt Macy  */
49e92ffd9bSMartin Matuska static const uint64_t redaction_list_update_interval_ns =
50e92ffd9bSMartin Matuska     1000 * 1000 * 1000ULL; /* 1s */
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy /*
53eda14cbcSMatt Macy  * This tunable controls the length of the queues that zfs redact worker threads
54eda14cbcSMatt Macy  * use to communicate.  If the dmu_redact_snap thread is blocking on these
55eda14cbcSMatt Macy  * queues, this variable may need to be increased.  If there is a significant
56eda14cbcSMatt Macy  * slowdown at the start of a redact operation as these threads consume all the
57eda14cbcSMatt Macy  * available IO resources, or the queues are consuming too much memory, this
58eda14cbcSMatt Macy  * variable may need to be decreased.
59eda14cbcSMatt Macy  */
60e92ffd9bSMartin Matuska static const int zfs_redact_queue_length = 1024 * 1024;
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy /*
63eda14cbcSMatt Macy  * These tunables control the fill fraction of the queues by zfs redact. The
64eda14cbcSMatt Macy  * fill fraction controls the frequency with which threads have to be
65eda14cbcSMatt Macy  * cv_signaled. If a lot of cpu time is being spent on cv_signal, then these
66eda14cbcSMatt Macy  * should be tuned down.  If the queues empty before the signalled thread can
67eda14cbcSMatt Macy  * catch up, then these should be tuned up.
68eda14cbcSMatt Macy  */
69e92ffd9bSMartin Matuska static const uint64_t zfs_redact_queue_ff = 20;
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy struct redact_record {
72eda14cbcSMatt Macy 	bqueue_node_t		ln;
73eda14cbcSMatt Macy 	boolean_t		eos_marker; /* Marks the end of the stream */
74eda14cbcSMatt Macy 	uint64_t		start_object;
75eda14cbcSMatt Macy 	uint64_t		start_blkid;
76eda14cbcSMatt Macy 	uint64_t		end_object;
77eda14cbcSMatt Macy 	uint64_t		end_blkid;
78eda14cbcSMatt Macy 	uint8_t			indblkshift;
79eda14cbcSMatt Macy 	uint32_t		datablksz;
80eda14cbcSMatt Macy };
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy struct redact_thread_arg {
83eda14cbcSMatt Macy 	bqueue_t	q;
84eda14cbcSMatt Macy 	objset_t	*os;		/* Objset to traverse */
85eda14cbcSMatt Macy 	dsl_dataset_t	*ds;		/* Dataset to traverse */
86eda14cbcSMatt Macy 	struct redact_record *current_record;
87eda14cbcSMatt Macy 	int		error_code;
88eda14cbcSMatt Macy 	boolean_t	cancel;
89eda14cbcSMatt Macy 	zbookmark_phys_t resume;
90eda14cbcSMatt Macy 	objlist_t	*deleted_objs;
91eda14cbcSMatt Macy 	uint64_t	*num_blocks_visited;
92eda14cbcSMatt Macy 	uint64_t	ignore_object;	/* ignore further callbacks on this */
93eda14cbcSMatt Macy 	uint64_t	txg; /* txg to traverse since */
94eda14cbcSMatt Macy };
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy /*
97eda14cbcSMatt Macy  * The redaction node is a wrapper around the redaction record that is used
98eda14cbcSMatt Macy  * by the redaction merging thread to sort the records and determine overlaps.
99eda14cbcSMatt Macy  *
100eda14cbcSMatt Macy  * It contains two nodes; one sorts the records by their start_zb, and the other
101eda14cbcSMatt Macy  * sorts the records by their end_zb.
102eda14cbcSMatt Macy  */
103eda14cbcSMatt Macy struct redact_node {
104eda14cbcSMatt Macy 	avl_node_t			avl_node_start;
105eda14cbcSMatt Macy 	avl_node_t			avl_node_end;
106eda14cbcSMatt Macy 	struct redact_record		*record;
107eda14cbcSMatt Macy 	struct redact_thread_arg	*rt_arg;
108eda14cbcSMatt Macy 	uint32_t			thread_num;
109eda14cbcSMatt Macy };
110eda14cbcSMatt Macy 
111eda14cbcSMatt Macy struct merge_data {
112eda14cbcSMatt Macy 	list_t				md_redact_block_pending;
113eda14cbcSMatt Macy 	redact_block_phys_t		md_coalesce_block;
114eda14cbcSMatt Macy 	uint64_t			md_last_time;
115eda14cbcSMatt Macy 	redact_block_phys_t		md_furthest[TXG_SIZE];
116eda14cbcSMatt Macy 	/* Lists of struct redact_block_list_node. */
117eda14cbcSMatt Macy 	list_t				md_blocks[TXG_SIZE];
118eda14cbcSMatt Macy 	boolean_t			md_synctask_txg[TXG_SIZE];
119eda14cbcSMatt Macy 	uint64_t			md_latest_synctask_txg;
120eda14cbcSMatt Macy 	redaction_list_t		*md_redaction_list;
121eda14cbcSMatt Macy };
122eda14cbcSMatt Macy 
123eda14cbcSMatt Macy /*
124eda14cbcSMatt Macy  * A wrapper around struct redact_block so it can be stored in a list_t.
125eda14cbcSMatt Macy  */
126eda14cbcSMatt Macy struct redact_block_list_node {
127eda14cbcSMatt Macy 	redact_block_phys_t	block;
128eda14cbcSMatt Macy 	list_node_t		node;
129eda14cbcSMatt Macy };
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy /*
132eda14cbcSMatt Macy  * We've found a new redaction candidate.  In order to improve performance, we
133eda14cbcSMatt Macy  * coalesce these blocks when they're adjacent to each other.  This function
134eda14cbcSMatt Macy  * handles that.  If the new candidate block range is immediately after the
135eda14cbcSMatt Macy  * range we're building, coalesce it into the range we're building.  Otherwise,
136eda14cbcSMatt Macy  * put the record we're building on the queue, and update the build pointer to
137eda14cbcSMatt Macy  * point to the new record.
138eda14cbcSMatt Macy  */
139eda14cbcSMatt Macy static void
140eda14cbcSMatt Macy record_merge_enqueue(bqueue_t *q, struct redact_record **build,
141eda14cbcSMatt Macy     struct redact_record *new)
142eda14cbcSMatt Macy {
143eda14cbcSMatt Macy 	if (new->eos_marker) {
144eda14cbcSMatt Macy 		if (*build != NULL)
145*c7046f76SMartin Matuska 			bqueue_enqueue(q, *build, sizeof (**build));
146eda14cbcSMatt Macy 		bqueue_enqueue_flush(q, new, sizeof (*new));
147eda14cbcSMatt Macy 		return;
148eda14cbcSMatt Macy 	}
149eda14cbcSMatt Macy 	if (*build == NULL) {
150eda14cbcSMatt Macy 		*build = new;
151eda14cbcSMatt Macy 		return;
152eda14cbcSMatt Macy 	}
153eda14cbcSMatt Macy 	struct redact_record *curbuild = *build;
154eda14cbcSMatt Macy 	if ((curbuild->end_object == new->start_object &&
155eda14cbcSMatt Macy 	    curbuild->end_blkid + 1 == new->start_blkid &&
156eda14cbcSMatt Macy 	    curbuild->end_blkid != UINT64_MAX) ||
157eda14cbcSMatt Macy 	    (curbuild->end_object + 1 == new->start_object &&
158eda14cbcSMatt Macy 	    curbuild->end_blkid == UINT64_MAX && new->start_blkid == 0)) {
159eda14cbcSMatt Macy 		curbuild->end_object = new->end_object;
160eda14cbcSMatt Macy 		curbuild->end_blkid = new->end_blkid;
161eda14cbcSMatt Macy 		kmem_free(new, sizeof (*new));
162eda14cbcSMatt Macy 	} else {
163eda14cbcSMatt Macy 		bqueue_enqueue(q, curbuild, sizeof (*curbuild));
164eda14cbcSMatt Macy 		*build = new;
165eda14cbcSMatt Macy 	}
166eda14cbcSMatt Macy }
167eda14cbcSMatt Macy #ifdef _KERNEL
168eda14cbcSMatt Macy struct objnode {
169eda14cbcSMatt Macy 	avl_node_t node;
170eda14cbcSMatt Macy 	uint64_t obj;
171eda14cbcSMatt Macy };
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy static int
174eda14cbcSMatt Macy objnode_compare(const void *o1, const void *o2)
175eda14cbcSMatt Macy {
176eda14cbcSMatt Macy 	const struct objnode *obj1 = o1;
177eda14cbcSMatt Macy 	const struct objnode *obj2 = o2;
178eda14cbcSMatt Macy 	if (obj1->obj < obj2->obj)
179eda14cbcSMatt Macy 		return (-1);
180eda14cbcSMatt Macy 	if (obj1->obj > obj2->obj)
181eda14cbcSMatt Macy 		return (1);
182eda14cbcSMatt Macy 	return (0);
183eda14cbcSMatt Macy }
184eda14cbcSMatt Macy 
185eda14cbcSMatt Macy 
186eda14cbcSMatt Macy static objlist_t *
187eda14cbcSMatt Macy zfs_get_deleteq(objset_t *os)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy 	objlist_t *deleteq_objlist = objlist_create();
190eda14cbcSMatt Macy 	uint64_t deleteq_obj;
191eda14cbcSMatt Macy 	zap_cursor_t zc;
192eda14cbcSMatt Macy 	zap_attribute_t za;
193eda14cbcSMatt Macy 	dmu_object_info_t doi;
194eda14cbcSMatt Macy 
195eda14cbcSMatt Macy 	ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
196eda14cbcSMatt Macy 	VERIFY0(dmu_object_info(os, MASTER_NODE_OBJ, &doi));
197eda14cbcSMatt Macy 	ASSERT3U(doi.doi_type, ==, DMU_OT_MASTER_NODE);
198eda14cbcSMatt Macy 
199eda14cbcSMatt Macy 	VERIFY0(zap_lookup(os, MASTER_NODE_OBJ,
200eda14cbcSMatt Macy 	    ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy 	/*
203eda14cbcSMatt Macy 	 * In order to insert objects into the objlist, they must be in sorted
204eda14cbcSMatt Macy 	 * order. We don't know what order we'll get them out of the ZAP in, so
205eda14cbcSMatt Macy 	 * we insert them into and remove them from an avl_tree_t to sort them.
206eda14cbcSMatt Macy 	 */
207eda14cbcSMatt Macy 	avl_tree_t at;
208eda14cbcSMatt Macy 	avl_create(&at, objnode_compare, sizeof (struct objnode),
209eda14cbcSMatt Macy 	    offsetof(struct objnode, node));
210eda14cbcSMatt Macy 
211eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, os, deleteq_obj);
212eda14cbcSMatt Macy 	    zap_cursor_retrieve(&zc, &za) == 0; zap_cursor_advance(&zc)) {
213eda14cbcSMatt Macy 		struct objnode *obj = kmem_zalloc(sizeof (*obj), KM_SLEEP);
214eda14cbcSMatt Macy 		obj->obj = za.za_first_integer;
215eda14cbcSMatt Macy 		avl_add(&at, obj);
216eda14cbcSMatt Macy 	}
217eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	struct objnode *next, *found = avl_first(&at);
220eda14cbcSMatt Macy 	while (found != NULL) {
221eda14cbcSMatt Macy 		next = AVL_NEXT(&at, found);
222eda14cbcSMatt Macy 		objlist_insert(deleteq_objlist, found->obj);
223eda14cbcSMatt Macy 		found = next;
224eda14cbcSMatt Macy 	}
225eda14cbcSMatt Macy 
226eda14cbcSMatt Macy 	void *cookie = NULL;
227eda14cbcSMatt Macy 	while ((found = avl_destroy_nodes(&at, &cookie)) != NULL)
228eda14cbcSMatt Macy 		kmem_free(found, sizeof (*found));
229eda14cbcSMatt Macy 	avl_destroy(&at);
230eda14cbcSMatt Macy 	return (deleteq_objlist);
231eda14cbcSMatt Macy }
232eda14cbcSMatt Macy #endif
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy /*
235eda14cbcSMatt Macy  * This is the callback function to traverse_dataset for the redaction threads
236eda14cbcSMatt Macy  * for dmu_redact_snap.  This thread is responsible for creating redaction
237eda14cbcSMatt Macy  * records for all the data that is modified by the snapshots we're redacting
238eda14cbcSMatt Macy  * with respect to.  Redaction records represent ranges of data that have been
239eda14cbcSMatt Macy  * modified by one of the redaction snapshots, and are stored in the
240eda14cbcSMatt Macy  * redact_record struct. We need to create redaction records for three
241eda14cbcSMatt Macy  * cases:
242eda14cbcSMatt Macy  *
243eda14cbcSMatt Macy  * First, if there's a normal write, we need to create a redaction record for
244eda14cbcSMatt Macy  * that block.
245eda14cbcSMatt Macy  *
246eda14cbcSMatt Macy  * Second, if there's a hole, we need to create a redaction record that covers
247eda14cbcSMatt Macy  * the whole range of the hole.  If the hole is in the meta-dnode, it must cover
248eda14cbcSMatt Macy  * every block in all of the objects in the hole.
249eda14cbcSMatt Macy  *
250eda14cbcSMatt Macy  * Third, if there is a deleted object, we need to create a redaction record for
251eda14cbcSMatt Macy  * all of the blocks in that object.
252eda14cbcSMatt Macy  */
253eda14cbcSMatt Macy static int
254eda14cbcSMatt Macy redact_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
255eda14cbcSMatt Macy     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
256eda14cbcSMatt Macy {
257e92ffd9bSMartin Matuska 	(void) spa, (void) zilog;
258eda14cbcSMatt Macy 	struct redact_thread_arg *rta = arg;
259eda14cbcSMatt Macy 	struct redact_record *record;
260eda14cbcSMatt Macy 
261eda14cbcSMatt Macy 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
262eda14cbcSMatt Macy 	    zb->zb_object >= rta->resume.zb_object);
263eda14cbcSMatt Macy 
264eda14cbcSMatt Macy 	if (rta->cancel)
265eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	if (rta->ignore_object == zb->zb_object)
268eda14cbcSMatt Macy 		return (0);
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	/*
271eda14cbcSMatt Macy 	 * If we're visiting a dnode, we need to handle the case where the
272eda14cbcSMatt Macy 	 * object has been deleted.
273eda14cbcSMatt Macy 	 */
274eda14cbcSMatt Macy 	if (zb->zb_level == ZB_DNODE_LEVEL) {
275eda14cbcSMatt Macy 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy 		if (zb->zb_object == 0)
278eda14cbcSMatt Macy 			return (0);
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 		/*
281eda14cbcSMatt Macy 		 * If the object has been deleted, redact all of the blocks in
282eda14cbcSMatt Macy 		 * it.
283eda14cbcSMatt Macy 		 */
284eda14cbcSMatt Macy 		if (dnp->dn_type == DMU_OT_NONE ||
285eda14cbcSMatt Macy 		    objlist_exists(rta->deleted_objs, zb->zb_object)) {
286eda14cbcSMatt Macy 			rta->ignore_object = zb->zb_object;
287eda14cbcSMatt Macy 			record = kmem_zalloc(sizeof (struct redact_record),
288eda14cbcSMatt Macy 			    KM_SLEEP);
289eda14cbcSMatt Macy 
290eda14cbcSMatt Macy 			record->eos_marker = B_FALSE;
291eda14cbcSMatt Macy 			record->start_object = record->end_object =
292eda14cbcSMatt Macy 			    zb->zb_object;
293eda14cbcSMatt Macy 			record->start_blkid = 0;
294eda14cbcSMatt Macy 			record->end_blkid = UINT64_MAX;
295eda14cbcSMatt Macy 			record_merge_enqueue(&rta->q,
296eda14cbcSMatt Macy 			    &rta->current_record, record);
297eda14cbcSMatt Macy 		}
298eda14cbcSMatt Macy 		return (0);
299eda14cbcSMatt Macy 	} else if (zb->zb_level < 0) {
300eda14cbcSMatt Macy 		return (0);
301eda14cbcSMatt Macy 	} else if (zb->zb_level > 0 && !BP_IS_HOLE(bp)) {
302eda14cbcSMatt Macy 		/*
303eda14cbcSMatt Macy 		 * If this is an indirect block, but not a hole, it doesn't
304eda14cbcSMatt Macy 		 * provide any useful information for redaction, so ignore it.
305eda14cbcSMatt Macy 		 */
306eda14cbcSMatt Macy 		return (0);
307eda14cbcSMatt Macy 	}
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 	/*
310eda14cbcSMatt Macy 	 * At this point, there are two options left for the type of block we're
311eda14cbcSMatt Macy 	 * looking at.  Either this is a hole (which could be in the dnode or
312eda14cbcSMatt Macy 	 * the meta-dnode), or it's a level 0 block of some sort.  If it's a
313eda14cbcSMatt Macy 	 * hole, we create a redaction record that covers the whole range.  If
314eda14cbcSMatt Macy 	 * the hole is in a dnode, we need to redact all the blocks in that
315eda14cbcSMatt Macy 	 * hole.  If the hole is in the meta-dnode, we instead need to redact
316eda14cbcSMatt Macy 	 * all blocks in every object covered by that hole.  If it's a level 0
317eda14cbcSMatt Macy 	 * block, we only need to redact that single block.
318eda14cbcSMatt Macy 	 */
319eda14cbcSMatt Macy 	record = kmem_zalloc(sizeof (struct redact_record), KM_SLEEP);
320eda14cbcSMatt Macy 	record->eos_marker = B_FALSE;
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy 	record->start_object = record->end_object = zb->zb_object;
323eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp)) {
324eda14cbcSMatt Macy 		record->start_blkid = zb->zb_blkid *
325eda14cbcSMatt Macy 		    bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level);
326eda14cbcSMatt Macy 
327eda14cbcSMatt Macy 		record->end_blkid = ((zb->zb_blkid + 1) *
328eda14cbcSMatt Macy 		    bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level)) - 1;
329eda14cbcSMatt Macy 
330eda14cbcSMatt Macy 		if (zb->zb_object == DMU_META_DNODE_OBJECT) {
331eda14cbcSMatt Macy 			record->start_object = record->start_blkid *
332eda14cbcSMatt Macy 			    ((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) /
333eda14cbcSMatt Macy 			    sizeof (dnode_phys_t));
334eda14cbcSMatt Macy 			record->start_blkid = 0;
335eda14cbcSMatt Macy 			record->end_object = ((record->end_blkid +
336eda14cbcSMatt Macy 			    1) * ((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) /
337eda14cbcSMatt Macy 			    sizeof (dnode_phys_t))) - 1;
338eda14cbcSMatt Macy 			record->end_blkid = UINT64_MAX;
339eda14cbcSMatt Macy 		}
340eda14cbcSMatt Macy 	} else if (zb->zb_level != 0 ||
341eda14cbcSMatt Macy 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
342eda14cbcSMatt Macy 		kmem_free(record, sizeof (*record));
343eda14cbcSMatt Macy 		return (0);
344eda14cbcSMatt Macy 	} else {
345eda14cbcSMatt Macy 		record->start_blkid = record->end_blkid = zb->zb_blkid;
346eda14cbcSMatt Macy 	}
347eda14cbcSMatt Macy 	record->indblkshift = dnp->dn_indblkshift;
348eda14cbcSMatt Macy 	record->datablksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
349eda14cbcSMatt Macy 	record_merge_enqueue(&rta->q, &rta->current_record, record);
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	return (0);
352eda14cbcSMatt Macy }
353eda14cbcSMatt Macy 
354da5137abSMartin Matuska static __attribute__((noreturn)) void
355eda14cbcSMatt Macy redact_traverse_thread(void *arg)
356eda14cbcSMatt Macy {
357eda14cbcSMatt Macy 	struct redact_thread_arg *rt_arg = arg;
358eda14cbcSMatt Macy 	int err;
359eda14cbcSMatt Macy 	struct redact_record *data;
360eda14cbcSMatt Macy #ifdef _KERNEL
361eda14cbcSMatt Macy 	if (rt_arg->os->os_phys->os_type == DMU_OST_ZFS)
362eda14cbcSMatt Macy 		rt_arg->deleted_objs = zfs_get_deleteq(rt_arg->os);
363eda14cbcSMatt Macy 	else
364eda14cbcSMatt Macy 		rt_arg->deleted_objs = objlist_create();
365eda14cbcSMatt Macy #else
366eda14cbcSMatt Macy 	rt_arg->deleted_objs = objlist_create();
367eda14cbcSMatt Macy #endif
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	err = traverse_dataset_resume(rt_arg->ds, rt_arg->txg,
370eda14cbcSMatt Macy 	    &rt_arg->resume, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
371eda14cbcSMatt Macy 	    redact_cb, rt_arg);
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	if (err != EINTR)
374eda14cbcSMatt Macy 		rt_arg->error_code = err;
375eda14cbcSMatt Macy 	objlist_destroy(rt_arg->deleted_objs);
376eda14cbcSMatt Macy 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
377eda14cbcSMatt Macy 	data->eos_marker = B_TRUE;
378eda14cbcSMatt Macy 	record_merge_enqueue(&rt_arg->q, &rt_arg->current_record, data);
379eda14cbcSMatt Macy 	thread_exit();
380eda14cbcSMatt Macy }
381eda14cbcSMatt Macy 
382eda14cbcSMatt Macy static inline void
383eda14cbcSMatt Macy create_zbookmark_from_obj_off(zbookmark_phys_t *zb, uint64_t object,
384eda14cbcSMatt Macy     uint64_t blkid)
385eda14cbcSMatt Macy {
386eda14cbcSMatt Macy 	zb->zb_object = object;
387eda14cbcSMatt Macy 	zb->zb_level = 0;
388eda14cbcSMatt Macy 	zb->zb_blkid = blkid;
389eda14cbcSMatt Macy }
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy /*
392eda14cbcSMatt Macy  * This is a utility function that can do the comparison for the start or ends
393eda14cbcSMatt Macy  * of the ranges in a redact_record.
394eda14cbcSMatt Macy  */
395eda14cbcSMatt Macy static int
396eda14cbcSMatt Macy redact_range_compare(uint64_t obj1, uint64_t off1, uint32_t dbss1,
397eda14cbcSMatt Macy     uint64_t obj2, uint64_t off2, uint32_t dbss2)
398eda14cbcSMatt Macy {
399eda14cbcSMatt Macy 	zbookmark_phys_t z1, z2;
400eda14cbcSMatt Macy 	create_zbookmark_from_obj_off(&z1, obj1, off1);
401eda14cbcSMatt Macy 	create_zbookmark_from_obj_off(&z2, obj2, off2);
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 	return (zbookmark_compare(dbss1 >> SPA_MINBLOCKSHIFT, 0,
404eda14cbcSMatt Macy 	    dbss2 >> SPA_MINBLOCKSHIFT, 0, &z1, &z2));
405eda14cbcSMatt Macy }
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy /*
408eda14cbcSMatt Macy  * Compare two redaction records by their range's start location.  Also makes
409eda14cbcSMatt Macy  * eos records always compare last.  We use the thread number in the redact_node
410eda14cbcSMatt Macy  * to ensure that records do not compare equal (which is not allowed in our avl
411eda14cbcSMatt Macy  * trees).
412eda14cbcSMatt Macy  */
413eda14cbcSMatt Macy static int
414eda14cbcSMatt Macy redact_node_compare_start(const void *arg1, const void *arg2)
415eda14cbcSMatt Macy {
416eda14cbcSMatt Macy 	const struct redact_node *rn1 = arg1;
417eda14cbcSMatt Macy 	const struct redact_node *rn2 = arg2;
418eda14cbcSMatt Macy 	const struct redact_record *rr1 = rn1->record;
419eda14cbcSMatt Macy 	const struct redact_record *rr2 = rn2->record;
420eda14cbcSMatt Macy 	if (rr1->eos_marker)
421eda14cbcSMatt Macy 		return (1);
422eda14cbcSMatt Macy 	if (rr2->eos_marker)
423eda14cbcSMatt Macy 		return (-1);
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 	int cmp = redact_range_compare(rr1->start_object, rr1->start_blkid,
426eda14cbcSMatt Macy 	    rr1->datablksz, rr2->start_object, rr2->start_blkid,
427eda14cbcSMatt Macy 	    rr2->datablksz);
428eda14cbcSMatt Macy 	if (cmp == 0)
429eda14cbcSMatt Macy 		cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1);
430eda14cbcSMatt Macy 	return (cmp);
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy /*
434eda14cbcSMatt Macy  * Compare two redaction records by their range's end location.  Also makes
435eda14cbcSMatt Macy  * eos records always compare last.  We use the thread number in the redact_node
436eda14cbcSMatt Macy  * to ensure that records do not compare equal (which is not allowed in our avl
437eda14cbcSMatt Macy  * trees).
438eda14cbcSMatt Macy  */
439eda14cbcSMatt Macy static int
440eda14cbcSMatt Macy redact_node_compare_end(const void *arg1, const void *arg2)
441eda14cbcSMatt Macy {
442eda14cbcSMatt Macy 	const struct redact_node *rn1 = arg1;
443eda14cbcSMatt Macy 	const struct redact_node *rn2 = arg2;
444eda14cbcSMatt Macy 	const struct redact_record *srr1 = rn1->record;
445eda14cbcSMatt Macy 	const struct redact_record *srr2 = rn2->record;
446eda14cbcSMatt Macy 	if (srr1->eos_marker)
447eda14cbcSMatt Macy 		return (1);
448eda14cbcSMatt Macy 	if (srr2->eos_marker)
449eda14cbcSMatt Macy 		return (-1);
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	int cmp = redact_range_compare(srr1->end_object, srr1->end_blkid,
452eda14cbcSMatt Macy 	    srr1->datablksz, srr2->end_object, srr2->end_blkid,
453eda14cbcSMatt Macy 	    srr2->datablksz);
454eda14cbcSMatt Macy 	if (cmp == 0)
455eda14cbcSMatt Macy 		cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1);
456eda14cbcSMatt Macy 	return (cmp);
457eda14cbcSMatt Macy }
458eda14cbcSMatt Macy 
459eda14cbcSMatt Macy /*
460eda14cbcSMatt Macy  * Utility function that compares two redaction records to determine if any part
461eda14cbcSMatt Macy  * of the "from" record is before any part of the "to" record. Also causes End
462eda14cbcSMatt Macy  * of Stream redaction records to compare after all others, so that the
463eda14cbcSMatt Macy  * redaction merging logic can stay simple.
464eda14cbcSMatt Macy  */
465eda14cbcSMatt Macy static boolean_t
466eda14cbcSMatt Macy redact_record_before(const struct redact_record *from,
467eda14cbcSMatt Macy     const struct redact_record *to)
468eda14cbcSMatt Macy {
469eda14cbcSMatt Macy 	if (from->eos_marker == B_TRUE)
470eda14cbcSMatt Macy 		return (B_FALSE);
471eda14cbcSMatt Macy 	else if (to->eos_marker == B_TRUE)
472eda14cbcSMatt Macy 		return (B_TRUE);
473eda14cbcSMatt Macy 	return (redact_range_compare(from->start_object, from->start_blkid,
474eda14cbcSMatt Macy 	    from->datablksz, to->end_object, to->end_blkid,
475eda14cbcSMatt Macy 	    to->datablksz) <= 0);
476eda14cbcSMatt Macy }
477eda14cbcSMatt Macy 
478eda14cbcSMatt Macy /*
479eda14cbcSMatt Macy  * Pop a new redaction record off the queue, check that the records are in the
480eda14cbcSMatt Macy  * right order, and free the old data.
481eda14cbcSMatt Macy  */
482eda14cbcSMatt Macy static struct redact_record *
483eda14cbcSMatt Macy get_next_redact_record(bqueue_t *bq, struct redact_record *prev)
484eda14cbcSMatt Macy {
485eda14cbcSMatt Macy 	struct redact_record *next = bqueue_dequeue(bq);
486eda14cbcSMatt Macy 	ASSERT(redact_record_before(prev, next));
487eda14cbcSMatt Macy 	kmem_free(prev, sizeof (*prev));
488eda14cbcSMatt Macy 	return (next);
489eda14cbcSMatt Macy }
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy /*
492eda14cbcSMatt Macy  * Remove the given redaction node from both trees, pull a new redaction record
493eda14cbcSMatt Macy  * off the queue, free the old redaction record, update the redaction node, and
494eda14cbcSMatt Macy  * reinsert the node into the trees.
495eda14cbcSMatt Macy  */
496eda14cbcSMatt Macy static int
497eda14cbcSMatt Macy update_avl_trees(avl_tree_t *start_tree, avl_tree_t *end_tree,
498eda14cbcSMatt Macy     struct redact_node *redact_node)
499eda14cbcSMatt Macy {
500eda14cbcSMatt Macy 	avl_remove(start_tree, redact_node);
501eda14cbcSMatt Macy 	avl_remove(end_tree, redact_node);
502eda14cbcSMatt Macy 	redact_node->record = get_next_redact_record(&redact_node->rt_arg->q,
503eda14cbcSMatt Macy 	    redact_node->record);
504eda14cbcSMatt Macy 	avl_add(end_tree, redact_node);
505eda14cbcSMatt Macy 	avl_add(start_tree, redact_node);
506eda14cbcSMatt Macy 	return (redact_node->rt_arg->error_code);
507eda14cbcSMatt Macy }
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy /*
510eda14cbcSMatt Macy  * Synctask for updating redaction lists.  We first take this txg's list of
511eda14cbcSMatt Macy  * redacted blocks and append those to the redaction list.  We then update the
512eda14cbcSMatt Macy  * redaction list's bonus buffer.  We store the furthest blocks we visited and
513eda14cbcSMatt Macy  * the list of snapshots that we're redacting with respect to.  We need these so
514eda14cbcSMatt Macy  * that redacted sends and receives can be correctly resumed.
515eda14cbcSMatt Macy  */
516eda14cbcSMatt Macy static void
517eda14cbcSMatt Macy redaction_list_update_sync(void *arg, dmu_tx_t *tx)
518eda14cbcSMatt Macy {
519eda14cbcSMatt Macy 	struct merge_data *md = arg;
520eda14cbcSMatt Macy 	uint64_t txg = dmu_tx_get_txg(tx);
521eda14cbcSMatt Macy 	list_t *list = &md->md_blocks[txg & TXG_MASK];
522eda14cbcSMatt Macy 	redact_block_phys_t *furthest_visited =
523eda14cbcSMatt Macy 	    &md->md_furthest[txg & TXG_MASK];
524eda14cbcSMatt Macy 	objset_t *mos = tx->tx_pool->dp_meta_objset;
525eda14cbcSMatt Macy 	redaction_list_t *rl = md->md_redaction_list;
526eda14cbcSMatt Macy 	int bufsize = redact_sync_bufsize;
527eda14cbcSMatt Macy 	redact_block_phys_t *buf = kmem_alloc(bufsize * sizeof (*buf),
528eda14cbcSMatt Macy 	    KM_SLEEP);
529eda14cbcSMatt Macy 	int index = 0;
530eda14cbcSMatt Macy 
531eda14cbcSMatt Macy 	dmu_buf_will_dirty(rl->rl_dbuf, tx);
532eda14cbcSMatt Macy 
533eda14cbcSMatt Macy 	for (struct redact_block_list_node *rbln = list_remove_head(list);
534eda14cbcSMatt Macy 	    rbln != NULL; rbln = list_remove_head(list)) {
535eda14cbcSMatt Macy 		ASSERT3U(rbln->block.rbp_object, <=,
536eda14cbcSMatt Macy 		    furthest_visited->rbp_object);
537eda14cbcSMatt Macy 		ASSERT(rbln->block.rbp_object < furthest_visited->rbp_object ||
538eda14cbcSMatt Macy 		    rbln->block.rbp_blkid <= furthest_visited->rbp_blkid);
539eda14cbcSMatt Macy 		buf[index] = rbln->block;
540eda14cbcSMatt Macy 		index++;
541eda14cbcSMatt Macy 		if (index == bufsize) {
542eda14cbcSMatt Macy 			dmu_write(mos, rl->rl_object,
543eda14cbcSMatt Macy 			    rl->rl_phys->rlp_num_entries * sizeof (*buf),
544eda14cbcSMatt Macy 			    bufsize * sizeof (*buf), buf, tx);
545eda14cbcSMatt Macy 			rl->rl_phys->rlp_num_entries += bufsize;
546eda14cbcSMatt Macy 			index = 0;
547eda14cbcSMatt Macy 		}
548eda14cbcSMatt Macy 		kmem_free(rbln, sizeof (*rbln));
549eda14cbcSMatt Macy 	}
550eda14cbcSMatt Macy 	if (index > 0) {
551eda14cbcSMatt Macy 		dmu_write(mos, rl->rl_object, rl->rl_phys->rlp_num_entries *
552eda14cbcSMatt Macy 		    sizeof (*buf), index * sizeof (*buf), buf, tx);
553eda14cbcSMatt Macy 		rl->rl_phys->rlp_num_entries += index;
554eda14cbcSMatt Macy 	}
555eda14cbcSMatt Macy 	kmem_free(buf, bufsize * sizeof (*buf));
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy 	md->md_synctask_txg[txg & TXG_MASK] = B_FALSE;
558eda14cbcSMatt Macy 	rl->rl_phys->rlp_last_object = furthest_visited->rbp_object;
559eda14cbcSMatt Macy 	rl->rl_phys->rlp_last_blkid = furthest_visited->rbp_blkid;
560eda14cbcSMatt Macy }
561eda14cbcSMatt Macy 
562eda14cbcSMatt Macy static void
563eda14cbcSMatt Macy commit_rl_updates(objset_t *os, struct merge_data *md, uint64_t object,
564eda14cbcSMatt Macy     uint64_t blkid)
565eda14cbcSMatt Macy {
566eda14cbcSMatt Macy 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(os->os_spa)->dp_mos_dir);
567eda14cbcSMatt Macy 	dmu_tx_hold_space(tx, sizeof (struct redact_block_list_node));
568eda14cbcSMatt Macy 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
569eda14cbcSMatt Macy 	uint64_t txg = dmu_tx_get_txg(tx);
570eda14cbcSMatt Macy 	if (!md->md_synctask_txg[txg & TXG_MASK]) {
571eda14cbcSMatt Macy 		dsl_sync_task_nowait(dmu_tx_pool(tx),
5722c48331dSMatt Macy 		    redaction_list_update_sync, md, tx);
573eda14cbcSMatt Macy 		md->md_synctask_txg[txg & TXG_MASK] = B_TRUE;
574eda14cbcSMatt Macy 		md->md_latest_synctask_txg = txg;
575eda14cbcSMatt Macy 	}
576eda14cbcSMatt Macy 	md->md_furthest[txg & TXG_MASK].rbp_object = object;
577eda14cbcSMatt Macy 	md->md_furthest[txg & TXG_MASK].rbp_blkid = blkid;
578eda14cbcSMatt Macy 	list_move_tail(&md->md_blocks[txg & TXG_MASK],
579eda14cbcSMatt Macy 	    &md->md_redact_block_pending);
580eda14cbcSMatt Macy 	dmu_tx_commit(tx);
581eda14cbcSMatt Macy 	md->md_last_time = gethrtime();
582eda14cbcSMatt Macy }
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy /*
585eda14cbcSMatt Macy  * We want to store the list of blocks that we're redacting in the bookmark's
586eda14cbcSMatt Macy  * redaction list.  However, this list is stored in the MOS, which means it can
587eda14cbcSMatt Macy  * only be written to in syncing context.  To get around this, we create a
588eda14cbcSMatt Macy  * synctask that will write to the mos for us.  We tell it what to write by
589eda14cbcSMatt Macy  * a linked list for each current transaction group; every time we decide to
590eda14cbcSMatt Macy  * redact a block, we append it to the transaction group that is currently in
591eda14cbcSMatt Macy  * open context.  We also update some progress information that the synctask
592eda14cbcSMatt Macy  * will store to enable resumable redacted sends.
593eda14cbcSMatt Macy  */
594eda14cbcSMatt Macy static void
595eda14cbcSMatt Macy update_redaction_list(struct merge_data *md, objset_t *os,
596eda14cbcSMatt Macy     uint64_t object, uint64_t blkid, uint64_t endblkid, uint32_t blksz)
597eda14cbcSMatt Macy {
598eda14cbcSMatt Macy 	boolean_t enqueue = B_FALSE;
599eda14cbcSMatt Macy 	redact_block_phys_t cur = {0};
600eda14cbcSMatt Macy 	uint64_t count = endblkid - blkid + 1;
601eda14cbcSMatt Macy 	while (count > REDACT_BLOCK_MAX_COUNT) {
602eda14cbcSMatt Macy 		update_redaction_list(md, os, object, blkid,
603eda14cbcSMatt Macy 		    blkid + REDACT_BLOCK_MAX_COUNT - 1, blksz);
604eda14cbcSMatt Macy 		blkid += REDACT_BLOCK_MAX_COUNT;
605eda14cbcSMatt Macy 		count -= REDACT_BLOCK_MAX_COUNT;
606eda14cbcSMatt Macy 	}
607eda14cbcSMatt Macy 	redact_block_phys_t *coalesce = &md->md_coalesce_block;
608eda14cbcSMatt Macy 	boolean_t new;
609eda14cbcSMatt Macy 	if (coalesce->rbp_size_count == 0) {
610eda14cbcSMatt Macy 		new = B_TRUE;
611eda14cbcSMatt Macy 		enqueue = B_FALSE;
612eda14cbcSMatt Macy 	} else  {
613eda14cbcSMatt Macy 		uint64_t old_count = redact_block_get_count(coalesce);
614eda14cbcSMatt Macy 		if (coalesce->rbp_object == object &&
615eda14cbcSMatt Macy 		    coalesce->rbp_blkid + old_count == blkid &&
616eda14cbcSMatt Macy 		    old_count + count <= REDACT_BLOCK_MAX_COUNT) {
617eda14cbcSMatt Macy 			ASSERT3U(redact_block_get_size(coalesce), ==, blksz);
618eda14cbcSMatt Macy 			redact_block_set_count(coalesce, old_count + count);
619eda14cbcSMatt Macy 			new = B_FALSE;
620eda14cbcSMatt Macy 			enqueue = B_FALSE;
621eda14cbcSMatt Macy 		} else {
622eda14cbcSMatt Macy 			new = B_TRUE;
623eda14cbcSMatt Macy 			enqueue = B_TRUE;
624eda14cbcSMatt Macy 		}
625eda14cbcSMatt Macy 	}
626eda14cbcSMatt Macy 
627eda14cbcSMatt Macy 	if (new) {
628eda14cbcSMatt Macy 		cur = *coalesce;
629eda14cbcSMatt Macy 		coalesce->rbp_blkid = blkid;
630eda14cbcSMatt Macy 		coalesce->rbp_object = object;
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy 		redact_block_set_count(coalesce, count);
633eda14cbcSMatt Macy 		redact_block_set_size(coalesce, blksz);
634eda14cbcSMatt Macy 	}
635eda14cbcSMatt Macy 
636eda14cbcSMatt Macy 	if (enqueue && redact_block_get_size(&cur) != 0) {
637eda14cbcSMatt Macy 		struct redact_block_list_node *rbln =
638eda14cbcSMatt Macy 		    kmem_alloc(sizeof (struct redact_block_list_node),
639eda14cbcSMatt Macy 		    KM_SLEEP);
640eda14cbcSMatt Macy 		rbln->block = cur;
641eda14cbcSMatt Macy 		list_insert_tail(&md->md_redact_block_pending, rbln);
642eda14cbcSMatt Macy 	}
643eda14cbcSMatt Macy 
644eda14cbcSMatt Macy 	if (gethrtime() > md->md_last_time +
645eda14cbcSMatt Macy 	    redaction_list_update_interval_ns) {
646eda14cbcSMatt Macy 		commit_rl_updates(os, md, object, blkid);
647eda14cbcSMatt Macy 	}
648eda14cbcSMatt Macy }
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy /*
651eda14cbcSMatt Macy  * This thread merges all the redaction records provided by the worker threads,
652eda14cbcSMatt Macy  * and determines which blocks are redacted by all the snapshots.  The algorithm
653eda14cbcSMatt Macy  * for doing so is similar to performing a merge in mergesort with n sub-lists
654eda14cbcSMatt Macy  * instead of 2, with some added complexity due to the fact that the entries are
655eda14cbcSMatt Macy  * ranges, not just single blocks.  This algorithm relies on the fact that the
656eda14cbcSMatt Macy  * queues are sorted, which is ensured by the fact that traverse_dataset
657eda14cbcSMatt Macy  * traverses the dataset in a consistent order.  We pull one entry off the front
658eda14cbcSMatt Macy  * of the queues of each secure dataset traversal thread.  Then we repeat the
659eda14cbcSMatt Macy  * following: each record represents a range of blocks modified by one of the
660eda14cbcSMatt Macy  * redaction snapshots, and each block in that range may need to be redacted in
661eda14cbcSMatt Macy  * the send stream.  Find the record with the latest start of its range, and the
662eda14cbcSMatt Macy  * record with the earliest end of its range. If the last start is before the
663eda14cbcSMatt Macy  * first end, then we know that the blocks in the range [last_start, first_end]
664eda14cbcSMatt Macy  * are covered by all of the ranges at the front of the queues, which means
665eda14cbcSMatt Macy  * every thread redacts that whole range.  For example, let's say the ranges on
666eda14cbcSMatt Macy  * each queue look like this:
667eda14cbcSMatt Macy  *
668eda14cbcSMatt Macy  * Block Id   1  2  3  4  5  6  7  8  9 10 11
669eda14cbcSMatt Macy  * Thread 1 |    [====================]
670eda14cbcSMatt Macy  * Thread 2 |       [========]
671eda14cbcSMatt Macy  * Thread 3 |             [=================]
672eda14cbcSMatt Macy  *
673eda14cbcSMatt Macy  * Thread 3 has the last start (5), and the thread 2 has the last end (6).  All
674eda14cbcSMatt Macy  * three threads modified the range [5,6], so that data should not be sent over
675eda14cbcSMatt Macy  * the wire.  After we've determined whether or not to redact anything, we take
676eda14cbcSMatt Macy  * the record with the first end.  We discard that record, and pull a new one
677eda14cbcSMatt Macy  * off the front of the queue it came from.  In the above example, we would
678eda14cbcSMatt Macy  * discard Thread 2's record, and pull a new one.  Let's say the next record we
679eda14cbcSMatt Macy  * pulled from Thread 2 covered range [10,11].  The new layout would look like
680eda14cbcSMatt Macy  * this:
681eda14cbcSMatt Macy  *
682eda14cbcSMatt Macy  * Block Id   1  2  3  4  5  6  7  8  9 10 11
683eda14cbcSMatt Macy  * Thread 1 |    [====================]
684eda14cbcSMatt Macy  * Thread 2 |                            [==]
685eda14cbcSMatt Macy  * Thread 3 |             [=================]
686eda14cbcSMatt Macy  *
687eda14cbcSMatt Macy  * When we compare the last start (10, from Thread 2) and the first end (9, from
688eda14cbcSMatt Macy  * Thread 1), we see that the last start is greater than the first end.
689eda14cbcSMatt Macy  * Therefore, we do not redact anything from these records.  We'll iterate by
690eda14cbcSMatt Macy  * replacing the record from Thread 1.
691eda14cbcSMatt Macy  *
692eda14cbcSMatt Macy  * We iterate by replacing the record with the lowest end because we know
693eda14cbcSMatt Macy  * that the record with the lowest end has helped us as much as it can.  All the
694eda14cbcSMatt Macy  * ranges before it that we will ever redact have been redacted.  In addition,
695eda14cbcSMatt Macy  * by replacing the one with the lowest end, we guarantee we catch all ranges
696eda14cbcSMatt Macy  * that need to be redacted.  For example, if in the case above we had replaced
697eda14cbcSMatt Macy  * the record from Thread 1 instead, we might have ended up with the following:
698eda14cbcSMatt Macy  *
699eda14cbcSMatt Macy  * Block Id   1  2  3  4  5  6  7  8  9 10 11 12
700eda14cbcSMatt Macy  * Thread 1 |                               [==]
701eda14cbcSMatt Macy  * Thread 2 |       [========]
702eda14cbcSMatt Macy  * Thread 3 |             [=================]
703eda14cbcSMatt Macy  *
704eda14cbcSMatt Macy  * If the next record from Thread 2 had been [8,10], for example, we should have
705eda14cbcSMatt Macy  * redacted part of that range, but because we updated Thread 1's record, we
706eda14cbcSMatt Macy  * missed it.
707eda14cbcSMatt Macy  *
708eda14cbcSMatt Macy  * We implement this algorithm by using two trees.  The first sorts the
709eda14cbcSMatt Macy  * redaction records by their start_zb, and the second sorts them by their
710eda14cbcSMatt Macy  * end_zb.  We use these to find the record with the last start and the record
711eda14cbcSMatt Macy  * with the first end.  We create a record with that start and end, and send it
712eda14cbcSMatt Macy  * on.  The overall runtime of this implementation is O(n log m), where n is the
713eda14cbcSMatt Macy  * total number of redaction records from all the different redaction snapshots,
714eda14cbcSMatt Macy  * and m is the number of redaction snapshots.
715eda14cbcSMatt Macy  *
716eda14cbcSMatt Macy  * If we redact with respect to zero snapshots, we create a redaction
717eda14cbcSMatt Macy  * record with the start object and blkid to 0, and the end object and blkid to
718eda14cbcSMatt Macy  * UINT64_MAX.  This will result in us redacting every block.
719eda14cbcSMatt Macy  */
720eda14cbcSMatt Macy static int
721eda14cbcSMatt Macy perform_thread_merge(bqueue_t *q, uint32_t num_threads,
722eda14cbcSMatt Macy     struct redact_thread_arg *thread_args, boolean_t *cancel)
723eda14cbcSMatt Macy {
724eda14cbcSMatt Macy 	struct redact_node *redact_nodes = NULL;
725eda14cbcSMatt Macy 	avl_tree_t start_tree, end_tree;
726eda14cbcSMatt Macy 	struct redact_record *record;
727eda14cbcSMatt Macy 	struct redact_record *current_record = NULL;
728eda14cbcSMatt Macy 	int err = 0;
729eda14cbcSMatt Macy 	struct merge_data md = { {0} };
730eda14cbcSMatt Macy 	list_create(&md.md_redact_block_pending,
731eda14cbcSMatt Macy 	    sizeof (struct redact_block_list_node),
732eda14cbcSMatt Macy 	    offsetof(struct redact_block_list_node, node));
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy 	/*
735eda14cbcSMatt Macy 	 * If we're redacting with respect to zero snapshots, then no data is
736eda14cbcSMatt Macy 	 * permitted to be sent.  We enqueue a record that redacts all blocks,
737eda14cbcSMatt Macy 	 * and an eos marker.
738eda14cbcSMatt Macy 	 */
739eda14cbcSMatt Macy 	if (num_threads == 0) {
740eda14cbcSMatt Macy 		record = kmem_zalloc(sizeof (struct redact_record),
741eda14cbcSMatt Macy 		    KM_SLEEP);
742eda14cbcSMatt Macy 		// We can't redact object 0, so don't try.
743eda14cbcSMatt Macy 		record->start_object = 1;
744eda14cbcSMatt Macy 		record->start_blkid = 0;
745eda14cbcSMatt Macy 		record->end_object = record->end_blkid = UINT64_MAX;
746eda14cbcSMatt Macy 		bqueue_enqueue(q, record, sizeof (*record));
747eda14cbcSMatt Macy 		return (0);
748eda14cbcSMatt Macy 	}
749eda14cbcSMatt Macy 	if (num_threads > 0) {
750eda14cbcSMatt Macy 		redact_nodes = kmem_zalloc(num_threads *
751eda14cbcSMatt Macy 		    sizeof (*redact_nodes), KM_SLEEP);
752eda14cbcSMatt Macy 	}
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy 	avl_create(&start_tree, redact_node_compare_start,
755eda14cbcSMatt Macy 	    sizeof (struct redact_node),
756eda14cbcSMatt Macy 	    offsetof(struct redact_node, avl_node_start));
757eda14cbcSMatt Macy 	avl_create(&end_tree, redact_node_compare_end,
758eda14cbcSMatt Macy 	    sizeof (struct redact_node),
759eda14cbcSMatt Macy 	    offsetof(struct redact_node, avl_node_end));
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy 	for (int i = 0; i < num_threads; i++) {
762eda14cbcSMatt Macy 		struct redact_node *node = &redact_nodes[i];
763eda14cbcSMatt Macy 		struct redact_thread_arg *targ = &thread_args[i];
764eda14cbcSMatt Macy 		node->record = bqueue_dequeue(&targ->q);
765eda14cbcSMatt Macy 		node->rt_arg = targ;
766eda14cbcSMatt Macy 		node->thread_num = i;
767eda14cbcSMatt Macy 		avl_add(&start_tree, node);
768eda14cbcSMatt Macy 		avl_add(&end_tree, node);
769eda14cbcSMatt Macy 	}
770eda14cbcSMatt Macy 
771eda14cbcSMatt Macy 	/*
772eda14cbcSMatt Macy 	 * Once the first record in the end tree has returned EOS, every record
773eda14cbcSMatt Macy 	 * must be an EOS record, so we should stop.
774eda14cbcSMatt Macy 	 */
775eda14cbcSMatt Macy 	while (err == 0 && !((struct redact_node *)avl_first(&end_tree))->
776eda14cbcSMatt Macy 	    record->eos_marker) {
777eda14cbcSMatt Macy 		if (*cancel) {
778eda14cbcSMatt Macy 			err = EINTR;
779eda14cbcSMatt Macy 			break;
780eda14cbcSMatt Macy 		}
781eda14cbcSMatt Macy 		struct redact_node *last_start = avl_last(&start_tree);
782eda14cbcSMatt Macy 		struct redact_node *first_end = avl_first(&end_tree);
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 		/*
785eda14cbcSMatt Macy 		 * If the last start record is before the first end record,
786eda14cbcSMatt Macy 		 * then we have blocks that are redacted by all threads.
787eda14cbcSMatt Macy 		 * Therefore, we should redact them.  Copy the record, and send
788eda14cbcSMatt Macy 		 * it to the main thread.
789eda14cbcSMatt Macy 		 */
790eda14cbcSMatt Macy 		if (redact_record_before(last_start->record,
791eda14cbcSMatt Macy 		    first_end->record)) {
792eda14cbcSMatt Macy 			record = kmem_zalloc(sizeof (struct redact_record),
793eda14cbcSMatt Macy 			    KM_SLEEP);
794eda14cbcSMatt Macy 			*record = *first_end->record;
795eda14cbcSMatt Macy 			record->start_object = last_start->record->start_object;
796eda14cbcSMatt Macy 			record->start_blkid = last_start->record->start_blkid;
797eda14cbcSMatt Macy 			record_merge_enqueue(q, &current_record,
798eda14cbcSMatt Macy 			    record);
799eda14cbcSMatt Macy 		}
800eda14cbcSMatt Macy 		err = update_avl_trees(&start_tree, &end_tree, first_end);
801eda14cbcSMatt Macy 	}
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 	/*
804eda14cbcSMatt Macy 	 * We're done; if we were cancelled, we need to cancel our workers and
805eda14cbcSMatt Macy 	 * clear out their queues.  Either way, we need to remove every thread's
806eda14cbcSMatt Macy 	 * redact_node struct from the avl trees.
807eda14cbcSMatt Macy 	 */
808eda14cbcSMatt Macy 	for (int i = 0; i < num_threads; i++) {
809eda14cbcSMatt Macy 		if (err != 0) {
810eda14cbcSMatt Macy 			thread_args[i].cancel = B_TRUE;
811eda14cbcSMatt Macy 			while (!redact_nodes[i].record->eos_marker) {
812eda14cbcSMatt Macy 				(void) update_avl_trees(&start_tree, &end_tree,
813eda14cbcSMatt Macy 				    &redact_nodes[i]);
814eda14cbcSMatt Macy 			}
815eda14cbcSMatt Macy 		}
816eda14cbcSMatt Macy 		avl_remove(&start_tree, &redact_nodes[i]);
817eda14cbcSMatt Macy 		avl_remove(&end_tree, &redact_nodes[i]);
818eda14cbcSMatt Macy 		kmem_free(redact_nodes[i].record,
819eda14cbcSMatt Macy 		    sizeof (struct redact_record));
8203f9d360cSMartin Matuska 		bqueue_destroy(&thread_args[i].q);
821eda14cbcSMatt Macy 	}
822eda14cbcSMatt Macy 
823eda14cbcSMatt Macy 	avl_destroy(&start_tree);
824eda14cbcSMatt Macy 	avl_destroy(&end_tree);
825eda14cbcSMatt Macy 	kmem_free(redact_nodes, num_threads * sizeof (*redact_nodes));
826eda14cbcSMatt Macy 	if (current_record != NULL)
827*c7046f76SMartin Matuska 		bqueue_enqueue(q, current_record, sizeof (*current_record));
828eda14cbcSMatt Macy 	return (err);
829eda14cbcSMatt Macy }
830eda14cbcSMatt Macy 
831eda14cbcSMatt Macy struct redact_merge_thread_arg {
832eda14cbcSMatt Macy 	bqueue_t q;
833eda14cbcSMatt Macy 	spa_t *spa;
834eda14cbcSMatt Macy 	int numsnaps;
835eda14cbcSMatt Macy 	struct redact_thread_arg *thr_args;
836eda14cbcSMatt Macy 	boolean_t cancel;
837eda14cbcSMatt Macy 	int error_code;
838eda14cbcSMatt Macy };
839eda14cbcSMatt Macy 
840da5137abSMartin Matuska static __attribute__((noreturn)) void
841eda14cbcSMatt Macy redact_merge_thread(void *arg)
842eda14cbcSMatt Macy {
843eda14cbcSMatt Macy 	struct redact_merge_thread_arg *rmta = arg;
844eda14cbcSMatt Macy 	rmta->error_code = perform_thread_merge(&rmta->q,
845eda14cbcSMatt Macy 	    rmta->numsnaps, rmta->thr_args, &rmta->cancel);
846eda14cbcSMatt Macy 	struct redact_record *rec = kmem_zalloc(sizeof (*rec), KM_SLEEP);
847eda14cbcSMatt Macy 	rec->eos_marker = B_TRUE;
848eda14cbcSMatt Macy 	bqueue_enqueue_flush(&rmta->q, rec, 1);
849eda14cbcSMatt Macy 	thread_exit();
850eda14cbcSMatt Macy }
851eda14cbcSMatt Macy 
852eda14cbcSMatt Macy /*
853eda14cbcSMatt Macy  * Find the next object in or after the redaction range passed in, and hold
854eda14cbcSMatt Macy  * its dnode with the provided tag.  Also update *object to contain the new
855eda14cbcSMatt Macy  * object number.
856eda14cbcSMatt Macy  */
857eda14cbcSMatt Macy static int
858a0b956f5SMartin Matuska hold_next_object(objset_t *os, struct redact_record *rec, const void *tag,
859eda14cbcSMatt Macy     uint64_t *object, dnode_t **dn)
860eda14cbcSMatt Macy {
861eda14cbcSMatt Macy 	int err = 0;
862eda14cbcSMatt Macy 	if (*dn != NULL)
8637877fdebSMatt Macy 		dnode_rele(*dn, tag);
864eda14cbcSMatt Macy 	*dn = NULL;
865eda14cbcSMatt Macy 	if (*object < rec->start_object) {
866eda14cbcSMatt Macy 		*object = rec->start_object - 1;
867eda14cbcSMatt Macy 	}
868eda14cbcSMatt Macy 	err = dmu_object_next(os, object, B_FALSE, 0);
869eda14cbcSMatt Macy 	if (err != 0)
870eda14cbcSMatt Macy 		return (err);
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy 	err = dnode_hold(os, *object, tag, dn);
873eda14cbcSMatt Macy 	while (err == 0 && (*object < rec->start_object ||
874eda14cbcSMatt Macy 	    DMU_OT_IS_METADATA((*dn)->dn_type))) {
875eda14cbcSMatt Macy 		dnode_rele(*dn, tag);
876eda14cbcSMatt Macy 		*dn = NULL;
877eda14cbcSMatt Macy 		err = dmu_object_next(os, object, B_FALSE, 0);
878eda14cbcSMatt Macy 		if (err != 0)
879eda14cbcSMatt Macy 			break;
880eda14cbcSMatt Macy 		err = dnode_hold(os, *object, tag, dn);
881eda14cbcSMatt Macy 	}
882eda14cbcSMatt Macy 	return (err);
883eda14cbcSMatt Macy }
884eda14cbcSMatt Macy 
885eda14cbcSMatt Macy static int
886eda14cbcSMatt Macy perform_redaction(objset_t *os, redaction_list_t *rl,
887eda14cbcSMatt Macy     struct redact_merge_thread_arg *rmta)
888eda14cbcSMatt Macy {
889eda14cbcSMatt Macy 	int err = 0;
890eda14cbcSMatt Macy 	bqueue_t *q = &rmta->q;
891eda14cbcSMatt Macy 	struct redact_record *rec = NULL;
892eda14cbcSMatt Macy 	struct merge_data md = { {0} };
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 	list_create(&md.md_redact_block_pending,
895eda14cbcSMatt Macy 	    sizeof (struct redact_block_list_node),
896eda14cbcSMatt Macy 	    offsetof(struct redact_block_list_node, node));
897eda14cbcSMatt Macy 	md.md_redaction_list = rl;
898eda14cbcSMatt Macy 
899eda14cbcSMatt Macy 	for (int i = 0; i < TXG_SIZE; i++) {
900eda14cbcSMatt Macy 		list_create(&md.md_blocks[i],
901eda14cbcSMatt Macy 		    sizeof (struct redact_block_list_node),
902eda14cbcSMatt Macy 		    offsetof(struct redact_block_list_node, node));
903eda14cbcSMatt Macy 	}
904eda14cbcSMatt Macy 	dnode_t *dn = NULL;
905eda14cbcSMatt Macy 	uint64_t prev_obj = 0;
906eda14cbcSMatt Macy 	for (rec = bqueue_dequeue(q); !rec->eos_marker && err == 0;
907eda14cbcSMatt Macy 	    rec = get_next_redact_record(q, rec)) {
908eda14cbcSMatt Macy 		ASSERT3U(rec->start_object, !=, 0);
909eda14cbcSMatt Macy 		uint64_t object;
910eda14cbcSMatt Macy 		if (prev_obj != rec->start_object) {
911eda14cbcSMatt Macy 			object = rec->start_object - 1;
912eda14cbcSMatt Macy 			err = hold_next_object(os, rec, FTAG, &object, &dn);
913eda14cbcSMatt Macy 		} else {
914eda14cbcSMatt Macy 			object = prev_obj;
915eda14cbcSMatt Macy 		}
916eda14cbcSMatt Macy 		while (err == 0 && object <= rec->end_object) {
917eda14cbcSMatt Macy 			if (issig(JUSTLOOKING) && issig(FORREAL)) {
918eda14cbcSMatt Macy 				err = EINTR;
919eda14cbcSMatt Macy 				break;
920eda14cbcSMatt Macy 			}
921eda14cbcSMatt Macy 			/*
922eda14cbcSMatt Macy 			 * Part of the current object is contained somewhere in
923eda14cbcSMatt Macy 			 * the range covered by rec.
924eda14cbcSMatt Macy 			 */
925eda14cbcSMatt Macy 			uint64_t startblkid;
926eda14cbcSMatt Macy 			uint64_t endblkid;
927eda14cbcSMatt Macy 			uint64_t maxblkid = dn->dn_phys->dn_maxblkid;
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 			if (rec->start_object < object)
930eda14cbcSMatt Macy 				startblkid = 0;
931eda14cbcSMatt Macy 			else if (rec->start_blkid > maxblkid)
932eda14cbcSMatt Macy 				break;
933eda14cbcSMatt Macy 			else
934eda14cbcSMatt Macy 				startblkid = rec->start_blkid;
935eda14cbcSMatt Macy 
936eda14cbcSMatt Macy 			if (rec->end_object > object || rec->end_blkid >
937eda14cbcSMatt Macy 			    maxblkid) {
938eda14cbcSMatt Macy 				endblkid = maxblkid;
939eda14cbcSMatt Macy 			} else {
940eda14cbcSMatt Macy 				endblkid = rec->end_blkid;
941eda14cbcSMatt Macy 			}
942eda14cbcSMatt Macy 			update_redaction_list(&md, os, object, startblkid,
943eda14cbcSMatt Macy 			    endblkid, dn->dn_datablksz);
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 			if (object == rec->end_object)
946eda14cbcSMatt Macy 				break;
947eda14cbcSMatt Macy 			err = hold_next_object(os, rec, FTAG, &object, &dn);
948eda14cbcSMatt Macy 		}
949eda14cbcSMatt Macy 		if (err == ESRCH)
950eda14cbcSMatt Macy 			err = 0;
951eda14cbcSMatt Macy 		if (dn != NULL)
952eda14cbcSMatt Macy 			prev_obj = object;
953eda14cbcSMatt Macy 	}
954eda14cbcSMatt Macy 	if (err == 0 && dn != NULL)
955eda14cbcSMatt Macy 		dnode_rele(dn, FTAG);
956eda14cbcSMatt Macy 
957eda14cbcSMatt Macy 	if (err == ESRCH)
958eda14cbcSMatt Macy 		err = 0;
959eda14cbcSMatt Macy 	rmta->cancel = B_TRUE;
960eda14cbcSMatt Macy 	while (!rec->eos_marker)
961eda14cbcSMatt Macy 		rec = get_next_redact_record(q, rec);
962eda14cbcSMatt Macy 	kmem_free(rec, sizeof (*rec));
963eda14cbcSMatt Macy 
964eda14cbcSMatt Macy 	/*
965eda14cbcSMatt Macy 	 * There may be a block that's being coalesced, sync that out before we
966eda14cbcSMatt Macy 	 * return.
967eda14cbcSMatt Macy 	 */
968eda14cbcSMatt Macy 	if (err == 0 && md.md_coalesce_block.rbp_size_count != 0) {
969eda14cbcSMatt Macy 		struct redact_block_list_node *rbln =
970eda14cbcSMatt Macy 		    kmem_alloc(sizeof (struct redact_block_list_node),
971eda14cbcSMatt Macy 		    KM_SLEEP);
972eda14cbcSMatt Macy 		rbln->block = md.md_coalesce_block;
973eda14cbcSMatt Macy 		list_insert_tail(&md.md_redact_block_pending, rbln);
974eda14cbcSMatt Macy 	}
975eda14cbcSMatt Macy 	commit_rl_updates(os, &md, UINT64_MAX, UINT64_MAX);
976eda14cbcSMatt Macy 
977eda14cbcSMatt Macy 	/*
978eda14cbcSMatt Macy 	 * Wait for all the redaction info to sync out before we return, so that
979eda14cbcSMatt Macy 	 * anyone who attempts to resume this redaction will have all the data
980eda14cbcSMatt Macy 	 * they need.
981eda14cbcSMatt Macy 	 */
982eda14cbcSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(os->os_spa);
983eda14cbcSMatt Macy 	if (md.md_latest_synctask_txg != 0)
984eda14cbcSMatt Macy 		txg_wait_synced(dp, md.md_latest_synctask_txg);
985eda14cbcSMatt Macy 	for (int i = 0; i < TXG_SIZE; i++)
986eda14cbcSMatt Macy 		list_destroy(&md.md_blocks[i]);
987eda14cbcSMatt Macy 	return (err);
988eda14cbcSMatt Macy }
989eda14cbcSMatt Macy 
990eda14cbcSMatt Macy static boolean_t
991eda14cbcSMatt Macy redact_snaps_contains(uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
992eda14cbcSMatt Macy {
993eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps; i++) {
994eda14cbcSMatt Macy 		if (snaps[i] == guid)
995eda14cbcSMatt Macy 			return (B_TRUE);
996eda14cbcSMatt Macy 	}
997eda14cbcSMatt Macy 	return (B_FALSE);
998eda14cbcSMatt Macy }
999eda14cbcSMatt Macy 
1000eda14cbcSMatt Macy int
1001eda14cbcSMatt Macy dmu_redact_snap(const char *snapname, nvlist_t *redactnvl,
1002eda14cbcSMatt Macy     const char *redactbook)
1003eda14cbcSMatt Macy {
1004eda14cbcSMatt Macy 	int err = 0;
1005eda14cbcSMatt Macy 	dsl_pool_t *dp = NULL;
1006eda14cbcSMatt Macy 	dsl_dataset_t *ds = NULL;
1007eda14cbcSMatt Macy 	int numsnaps = 0;
1008eda14cbcSMatt Macy 	objset_t *os;
1009eda14cbcSMatt Macy 	struct redact_thread_arg *args = NULL;
1010eda14cbcSMatt Macy 	redaction_list_t *new_rl = NULL;
10112c48331dSMatt Macy 	char *newredactbook;
1012eda14cbcSMatt Macy 
1013eda14cbcSMatt Macy 	if ((err = dsl_pool_hold(snapname, FTAG, &dp)) != 0)
1014eda14cbcSMatt Macy 		return (err);
1015eda14cbcSMatt Macy 
10162c48331dSMatt Macy 	newredactbook = kmem_zalloc(sizeof (char) * ZFS_MAX_DATASET_NAME_LEN,
10172c48331dSMatt Macy 	    KM_SLEEP);
10182c48331dSMatt Macy 
1019eda14cbcSMatt Macy 	if ((err = dsl_dataset_hold_flags(dp, snapname, DS_HOLD_FLAG_DECRYPT,
1020eda14cbcSMatt Macy 	    FTAG, &ds)) != 0) {
1021eda14cbcSMatt Macy 		goto out;
1022eda14cbcSMatt Macy 	}
1023eda14cbcSMatt Macy 	dsl_dataset_long_hold(ds, FTAG);
1024eda14cbcSMatt Macy 	if (!ds->ds_is_snapshot || dmu_objset_from_ds(ds, &os) != 0) {
1025eda14cbcSMatt Macy 		err = EINVAL;
1026eda14cbcSMatt Macy 		goto out;
1027eda14cbcSMatt Macy 	}
1028eda14cbcSMatt Macy 	if (dsl_dataset_feature_is_active(ds, SPA_FEATURE_REDACTED_DATASETS)) {
1029eda14cbcSMatt Macy 		err = EALREADY;
1030eda14cbcSMatt Macy 		goto out;
1031eda14cbcSMatt Macy 	}
1032eda14cbcSMatt Macy 
1033eda14cbcSMatt Macy 	numsnaps = fnvlist_num_pairs(redactnvl);
1034eda14cbcSMatt Macy 	if (numsnaps > 0)
1035eda14cbcSMatt Macy 		args = kmem_zalloc(numsnaps * sizeof (*args), KM_SLEEP);
1036eda14cbcSMatt Macy 
1037eda14cbcSMatt Macy 	nvpair_t *pair = NULL;
1038eda14cbcSMatt Macy 	for (int i = 0; i < numsnaps; i++) {
1039eda14cbcSMatt Macy 		pair = nvlist_next_nvpair(redactnvl, pair);
1040eda14cbcSMatt Macy 		const char *name = nvpair_name(pair);
1041eda14cbcSMatt Macy 		struct redact_thread_arg *rta = &args[i];
1042eda14cbcSMatt Macy 		err = dsl_dataset_hold_flags(dp, name, DS_HOLD_FLAG_DECRYPT,
1043eda14cbcSMatt Macy 		    FTAG, &rta->ds);
1044eda14cbcSMatt Macy 		if (err != 0)
1045eda14cbcSMatt Macy 			break;
1046eda14cbcSMatt Macy 		/*
1047eda14cbcSMatt Macy 		 * We want to do the long hold before we can get any other
1048eda14cbcSMatt Macy 		 * errors, because the cleanup code will release the long
1049eda14cbcSMatt Macy 		 * hold if rta->ds is filled in.
1050eda14cbcSMatt Macy 		 */
1051eda14cbcSMatt Macy 		dsl_dataset_long_hold(rta->ds, FTAG);
1052eda14cbcSMatt Macy 
1053eda14cbcSMatt Macy 		err = dmu_objset_from_ds(rta->ds, &rta->os);
1054eda14cbcSMatt Macy 		if (err != 0)
1055eda14cbcSMatt Macy 			break;
1056eda14cbcSMatt Macy 		if (!dsl_dataset_is_before(rta->ds, ds, 0)) {
1057eda14cbcSMatt Macy 			err = EINVAL;
1058eda14cbcSMatt Macy 			break;
1059eda14cbcSMatt Macy 		}
1060eda14cbcSMatt Macy 		if (dsl_dataset_feature_is_active(rta->ds,
1061eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS)) {
1062eda14cbcSMatt Macy 			err = EALREADY;
1063eda14cbcSMatt Macy 			break;
1064eda14cbcSMatt Macy 
1065eda14cbcSMatt Macy 		}
1066eda14cbcSMatt Macy 	}
1067eda14cbcSMatt Macy 	if (err != 0)
1068eda14cbcSMatt Macy 		goto out;
1069180f8225SMatt Macy 	VERIFY3P(nvlist_next_nvpair(redactnvl, pair), ==, NULL);
1070eda14cbcSMatt Macy 
1071eda14cbcSMatt Macy 	boolean_t resuming = B_FALSE;
1072eda14cbcSMatt Macy 	zfs_bookmark_phys_t bookmark;
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy 	(void) strlcpy(newredactbook, snapname, ZFS_MAX_DATASET_NAME_LEN);
1075eda14cbcSMatt Macy 	char *c = strchr(newredactbook, '@');
1076eda14cbcSMatt Macy 	ASSERT3P(c, !=, NULL);
1077eda14cbcSMatt Macy 	int n = snprintf(c, ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook),
1078eda14cbcSMatt Macy 	    "#%s", redactbook);
1079eda14cbcSMatt Macy 	if (n >= ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook)) {
1080eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
10812c48331dSMatt Macy 		kmem_free(newredactbook,
10822c48331dSMatt Macy 		    sizeof (char) * ZFS_MAX_DATASET_NAME_LEN);
10832c48331dSMatt Macy 		if (args != NULL)
10842c48331dSMatt Macy 			kmem_free(args, numsnaps * sizeof (*args));
1085eda14cbcSMatt Macy 		return (SET_ERROR(ENAMETOOLONG));
1086eda14cbcSMatt Macy 	}
1087eda14cbcSMatt Macy 	err = dsl_bookmark_lookup(dp, newredactbook, NULL, &bookmark);
1088eda14cbcSMatt Macy 	if (err == 0) {
1089eda14cbcSMatt Macy 		resuming = B_TRUE;
1090eda14cbcSMatt Macy 		if (bookmark.zbm_redaction_obj == 0) {
1091eda14cbcSMatt Macy 			err = EEXIST;
1092eda14cbcSMatt Macy 			goto out;
1093eda14cbcSMatt Macy 		}
1094eda14cbcSMatt Macy 		err = dsl_redaction_list_hold_obj(dp,
1095eda14cbcSMatt Macy 		    bookmark.zbm_redaction_obj, FTAG, &new_rl);
1096eda14cbcSMatt Macy 		if (err != 0) {
1097eda14cbcSMatt Macy 			err = EIO;
1098eda14cbcSMatt Macy 			goto out;
1099eda14cbcSMatt Macy 		}
1100eda14cbcSMatt Macy 		dsl_redaction_list_long_hold(dp, new_rl, FTAG);
1101eda14cbcSMatt Macy 		if (new_rl->rl_phys->rlp_num_snaps != numsnaps) {
1102eda14cbcSMatt Macy 			err = ESRCH;
1103eda14cbcSMatt Macy 			goto out;
1104eda14cbcSMatt Macy 		}
1105eda14cbcSMatt Macy 		for (int i = 0; i < numsnaps; i++) {
1106eda14cbcSMatt Macy 			struct redact_thread_arg *rta = &args[i];
1107eda14cbcSMatt Macy 			if (!redact_snaps_contains(new_rl->rl_phys->rlp_snaps,
1108eda14cbcSMatt Macy 			    new_rl->rl_phys->rlp_num_snaps,
1109eda14cbcSMatt Macy 			    dsl_dataset_phys(rta->ds)->ds_guid)) {
1110eda14cbcSMatt Macy 				err = ESRCH;
1111eda14cbcSMatt Macy 				goto out;
1112eda14cbcSMatt Macy 			}
1113eda14cbcSMatt Macy 		}
1114eda14cbcSMatt Macy 		if (new_rl->rl_phys->rlp_last_blkid == UINT64_MAX &&
1115eda14cbcSMatt Macy 		    new_rl->rl_phys->rlp_last_object == UINT64_MAX) {
1116eda14cbcSMatt Macy 			err = EEXIST;
1117eda14cbcSMatt Macy 			goto out;
1118eda14cbcSMatt Macy 		}
1119eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
1120eda14cbcSMatt Macy 		dp = NULL;
1121eda14cbcSMatt Macy 	} else {
1122eda14cbcSMatt Macy 		uint64_t *guids = NULL;
1123eda14cbcSMatt Macy 		if (numsnaps > 0) {
1124eda14cbcSMatt Macy 			guids = kmem_zalloc(numsnaps * sizeof (uint64_t),
1125eda14cbcSMatt Macy 			    KM_SLEEP);
1126eda14cbcSMatt Macy 		}
1127eda14cbcSMatt Macy 		for (int i = 0; i < numsnaps; i++) {
1128eda14cbcSMatt Macy 			struct redact_thread_arg *rta = &args[i];
1129eda14cbcSMatt Macy 			guids[i] = dsl_dataset_phys(rta->ds)->ds_guid;
1130eda14cbcSMatt Macy 		}
1131eda14cbcSMatt Macy 
1132eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
1133eda14cbcSMatt Macy 		dp = NULL;
1134eda14cbcSMatt Macy 		err = dsl_bookmark_create_redacted(newredactbook, snapname,
1135eda14cbcSMatt Macy 		    numsnaps, guids, FTAG, &new_rl);
1136eda14cbcSMatt Macy 		kmem_free(guids, numsnaps * sizeof (uint64_t));
1137eda14cbcSMatt Macy 		if (err != 0) {
1138eda14cbcSMatt Macy 			goto out;
1139eda14cbcSMatt Macy 		}
1140eda14cbcSMatt Macy 	}
1141eda14cbcSMatt Macy 
1142eda14cbcSMatt Macy 	for (int i = 0; i < numsnaps; i++) {
1143eda14cbcSMatt Macy 		struct redact_thread_arg *rta = &args[i];
1144eda14cbcSMatt Macy 		(void) bqueue_init(&rta->q, zfs_redact_queue_ff,
1145eda14cbcSMatt Macy 		    zfs_redact_queue_length,
1146eda14cbcSMatt Macy 		    offsetof(struct redact_record, ln));
1147eda14cbcSMatt Macy 		if (resuming) {
1148eda14cbcSMatt Macy 			rta->resume.zb_blkid =
1149eda14cbcSMatt Macy 			    new_rl->rl_phys->rlp_last_blkid;
1150eda14cbcSMatt Macy 			rta->resume.zb_object =
1151eda14cbcSMatt Macy 			    new_rl->rl_phys->rlp_last_object;
1152eda14cbcSMatt Macy 		}
1153eda14cbcSMatt Macy 		rta->txg = dsl_dataset_phys(ds)->ds_creation_txg;
1154eda14cbcSMatt Macy 		(void) thread_create(NULL, 0, redact_traverse_thread, rta,
1155eda14cbcSMatt Macy 		    0, curproc, TS_RUN, minclsyspri);
1156eda14cbcSMatt Macy 	}
11572c48331dSMatt Macy 
11582c48331dSMatt Macy 	struct redact_merge_thread_arg *rmta;
11592c48331dSMatt Macy 	rmta = kmem_zalloc(sizeof (struct redact_merge_thread_arg), KM_SLEEP);
11602c48331dSMatt Macy 
11612c48331dSMatt Macy 	(void) bqueue_init(&rmta->q, zfs_redact_queue_ff,
1162eda14cbcSMatt Macy 	    zfs_redact_queue_length, offsetof(struct redact_record, ln));
11632c48331dSMatt Macy 	rmta->numsnaps = numsnaps;
11642c48331dSMatt Macy 	rmta->spa = os->os_spa;
11652c48331dSMatt Macy 	rmta->thr_args = args;
11662c48331dSMatt Macy 	(void) thread_create(NULL, 0, redact_merge_thread, rmta, 0, curproc,
1167eda14cbcSMatt Macy 	    TS_RUN, minclsyspri);
11682c48331dSMatt Macy 	err = perform_redaction(os, new_rl, rmta);
11693f9d360cSMartin Matuska 	bqueue_destroy(&rmta->q);
11702c48331dSMatt Macy 	kmem_free(rmta, sizeof (struct redact_merge_thread_arg));
11712c48331dSMatt Macy 
1172eda14cbcSMatt Macy out:
11732c48331dSMatt Macy 	kmem_free(newredactbook, sizeof (char) * ZFS_MAX_DATASET_NAME_LEN);
11742c48331dSMatt Macy 
1175eda14cbcSMatt Macy 	if (new_rl != NULL) {
1176eda14cbcSMatt Macy 		dsl_redaction_list_long_rele(new_rl, FTAG);
1177eda14cbcSMatt Macy 		dsl_redaction_list_rele(new_rl, FTAG);
1178eda14cbcSMatt Macy 	}
1179eda14cbcSMatt Macy 	for (int i = 0; i < numsnaps; i++) {
1180eda14cbcSMatt Macy 		struct redact_thread_arg *rta = &args[i];
1181eda14cbcSMatt Macy 		/*
1182eda14cbcSMatt Macy 		 * rta->ds may be NULL if we got an error while filling
1183eda14cbcSMatt Macy 		 * it in.
1184eda14cbcSMatt Macy 		 */
1185eda14cbcSMatt Macy 		if (rta->ds != NULL) {
1186eda14cbcSMatt Macy 			dsl_dataset_long_rele(rta->ds, FTAG);
1187eda14cbcSMatt Macy 			dsl_dataset_rele_flags(rta->ds,
1188eda14cbcSMatt Macy 			    DS_HOLD_FLAG_DECRYPT, FTAG);
1189eda14cbcSMatt Macy 		}
1190eda14cbcSMatt Macy 	}
1191eda14cbcSMatt Macy 
1192eda14cbcSMatt Macy 	if (args != NULL)
1193eda14cbcSMatt Macy 		kmem_free(args, numsnaps * sizeof (*args));
1194eda14cbcSMatt Macy 	if (dp != NULL)
1195eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
1196eda14cbcSMatt Macy 	if (ds != NULL) {
1197eda14cbcSMatt Macy 		dsl_dataset_long_rele(ds, FTAG);
1198eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1199eda14cbcSMatt Macy 	}
1200eda14cbcSMatt Macy 	return (SET_ERROR(err));
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy }
1203