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