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