xref: /trueos/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/txg.c (revision 031a36aa28612b41a2f8e8bb5bee7bef905bf21c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org>
24  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
25  */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/txg_impl.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dsl_scan.h>
33 #include <sys/callb.h>
34 
35 /*
36  * ZFS Transaction Groups
37  * ----------------------
38  *
39  * ZFS transaction groups are, as the name implies, groups of transactions
40  * that act on persistent state. ZFS asserts consistency at the granularity of
41  * these transaction groups. Each successive transaction group (txg) is
42  * assigned a 64-bit consecutive identifier. There are three active
43  * transaction group states: open, quiescing, or syncing. At any given time,
44  * there may be an active txg associated with each state; each active txg may
45  * either be processing, or blocked waiting to enter the next state. There may
46  * be up to three active txgs, and there is always a txg in the open state
47  * (though it may be blocked waiting to enter the quiescing state). In broad
48  * strokes, transactions -- operations that change in-memory structures -- are
49  * accepted into the txg in the open state, and are completed while the txg is
50  * in the open or quiescing states. The accumulated changes are written to
51  * disk in the syncing state.
52  *
53  * Open
54  *
55  * When a new txg becomes active, it first enters the open state. New
56  * transactions -- updates to in-memory structures -- are assigned to the
57  * currently open txg. There is always a txg in the open state so that ZFS can
58  * accept new changes (though the txg may refuse new changes if it has hit
59  * some limit). ZFS advances the open txg to the next state for a variety of
60  * reasons such as it hitting a time or size threshold, or the execution of an
61  * administrative action that must be completed in the syncing state.
62  *
63  * Quiescing
64  *
65  * After a txg exits the open state, it enters the quiescing state. The
66  * quiescing state is intended to provide a buffer between accepting new
67  * transactions in the open state and writing them out to stable storage in
68  * the syncing state. While quiescing, transactions can continue their
69  * operation without delaying either of the other states. Typically, a txg is
70  * in the quiescing state very briefly since the operations are bounded by
71  * software latencies rather than, say, slower I/O latencies. After all
72  * transactions complete, the txg is ready to enter the next state.
73  *
74  * Syncing
75  *
76  * In the syncing state, the in-memory state built up during the open and (to
77  * a lesser degree) the quiescing states is written to stable storage. The
78  * process of writing out modified data can, in turn modify more data. For
79  * example when we write new blocks, we need to allocate space for them; those
80  * allocations modify metadata (space maps)... which themselves must be
81  * written to stable storage. During the sync state, ZFS iterates, writing out
82  * data until it converges and all in-memory changes have been written out.
83  * The first such pass is the largest as it encompasses all the modified user
84  * data (as opposed to filesystem metadata). Subsequent passes typically have
85  * far less data to write as they consist exclusively of filesystem metadata.
86  *
87  * To ensure convergence, after a certain number of passes ZFS begins
88  * overwriting locations on stable storage that had been allocated earlier in
89  * the syncing state (and subsequently freed). ZFS usually allocates new
90  * blocks to optimize for large, continuous, writes. For the syncing state to
91  * converge however it must complete a pass where no new blocks are allocated
92  * since each allocation requires a modification of persistent metadata.
93  * Further, to hasten convergence, after a prescribed number of passes, ZFS
94  * also defers frees, and stops compressing.
95  *
96  * In addition to writing out user data, we must also execute synctasks during
97  * the syncing context. A synctask is the mechanism by which some
98  * administrative activities work such as creating and destroying snapshots or
99  * datasets. Note that when a synctask is initiated it enters the open txg,
100  * and ZFS then pushes that txg as quickly as possible to completion of the
101  * syncing state in order to reduce the latency of the administrative
102  * activity. To complete the syncing state, ZFS writes out a new uberblock,
103  * the root of the tree of blocks that comprise all state stored on the ZFS
104  * pool. Finally, if there is a quiesced txg waiting, we signal that it can
105  * now transition to the syncing state.
106  */
107 
108 static void txg_sync_thread(void *arg);
109 static void txg_quiesce_thread(void *arg);
110 
111 int zfs_txg_timeout = 5;	/* max seconds worth of delta per txg */
112 
113 SYSCTL_DECL(_vfs_zfs);
114 SYSCTL_NODE(_vfs_zfs, OID_AUTO, txg, CTLFLAG_RW, 0, "ZFS TXG");
115 TUNABLE_INT("vfs.zfs.txg.timeout", &zfs_txg_timeout);
116 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, timeout, CTLFLAG_RW, &zfs_txg_timeout, 0,
117     "Maximum seconds worth of delta per txg");
118 
119 /*
120  * Prepare the txg subsystem.
121  */
122 void
txg_init(dsl_pool_t * dp,uint64_t txg)123 txg_init(dsl_pool_t *dp, uint64_t txg)
124 {
125 	tx_state_t *tx = &dp->dp_tx;
126 	int c;
127 	bzero(tx, sizeof (tx_state_t));
128 
129 	tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
130 
131 	for (c = 0; c < max_ncpus; c++) {
132 		int i;
133 
134 		mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
135 		mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_DEFAULT,
136 		    NULL);
137 		for (i = 0; i < TXG_SIZE; i++) {
138 			cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
139 			    NULL);
140 			list_create(&tx->tx_cpu[c].tc_callbacks[i],
141 			    sizeof (dmu_tx_callback_t),
142 			    offsetof(dmu_tx_callback_t, dcb_node));
143 		}
144 	}
145 
146 	mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
147 
148 	cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
149 	cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
150 	cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
151 	cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
152 	cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
153 
154 	tx->tx_open_txg = txg;
155 }
156 
157 /*
158  * Close down the txg subsystem.
159  */
160 void
txg_fini(dsl_pool_t * dp)161 txg_fini(dsl_pool_t *dp)
162 {
163 	tx_state_t *tx = &dp->dp_tx;
164 	int c;
165 
166 	ASSERT(tx->tx_threads == 0);
167 
168 	mutex_destroy(&tx->tx_sync_lock);
169 
170 	cv_destroy(&tx->tx_sync_more_cv);
171 	cv_destroy(&tx->tx_sync_done_cv);
172 	cv_destroy(&tx->tx_quiesce_more_cv);
173 	cv_destroy(&tx->tx_quiesce_done_cv);
174 	cv_destroy(&tx->tx_exit_cv);
175 
176 	for (c = 0; c < max_ncpus; c++) {
177 		int i;
178 
179 		mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
180 		mutex_destroy(&tx->tx_cpu[c].tc_lock);
181 		for (i = 0; i < TXG_SIZE; i++) {
182 			cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
183 			list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
184 		}
185 	}
186 
187 	if (tx->tx_commit_cb_taskq != NULL)
188 		taskq_destroy(tx->tx_commit_cb_taskq);
189 
190 	kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
191 
192 	bzero(tx, sizeof (tx_state_t));
193 }
194 
195 /*
196  * Start syncing transaction groups.
197  */
198 void
txg_sync_start(dsl_pool_t * dp)199 txg_sync_start(dsl_pool_t *dp)
200 {
201 	tx_state_t *tx = &dp->dp_tx;
202 
203 	mutex_enter(&tx->tx_sync_lock);
204 
205 	dprintf("pool %p\n", dp);
206 
207 	ASSERT(tx->tx_threads == 0);
208 
209 	tx->tx_threads = 2;
210 
211 	tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
212 	    dp, 0, &p0, TS_RUN, minclsyspri);
213 
214 	/*
215 	 * The sync thread can need a larger-than-default stack size on
216 	 * 32-bit x86.  This is due in part to nested pools and
217 	 * scrub_visitbp() recursion.
218 	 */
219 	tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
220 	    dp, 0, &p0, TS_RUN, minclsyspri);
221 
222 	mutex_exit(&tx->tx_sync_lock);
223 }
224 
225 static void
txg_thread_enter(tx_state_t * tx,callb_cpr_t * cpr)226 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
227 {
228 	CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
229 	mutex_enter(&tx->tx_sync_lock);
230 }
231 
232 static void
txg_thread_exit(tx_state_t * tx,callb_cpr_t * cpr,kthread_t ** tpp)233 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
234 {
235 	ASSERT(*tpp != NULL);
236 	*tpp = NULL;
237 	tx->tx_threads--;
238 	cv_broadcast(&tx->tx_exit_cv);
239 	CALLB_CPR_EXIT(cpr);		/* drops &tx->tx_sync_lock */
240 	thread_exit();
241 }
242 
243 static void
txg_thread_wait(tx_state_t * tx,callb_cpr_t * cpr,kcondvar_t * cv,clock_t time)244 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
245 {
246 	CALLB_CPR_SAFE_BEGIN(cpr);
247 
248 	if (time)
249 		(void) cv_timedwait(cv, &tx->tx_sync_lock, time);
250 	else
251 		cv_wait(cv, &tx->tx_sync_lock);
252 
253 	CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
254 }
255 
256 /*
257  * Stop syncing transaction groups.
258  */
259 void
txg_sync_stop(dsl_pool_t * dp)260 txg_sync_stop(dsl_pool_t *dp)
261 {
262 	tx_state_t *tx = &dp->dp_tx;
263 
264 	dprintf("pool %p\n", dp);
265 	/*
266 	 * Finish off any work in progress.
267 	 */
268 	ASSERT(tx->tx_threads == 2);
269 
270 	/*
271 	 * We need to ensure that we've vacated the deferred space_maps.
272 	 */
273 	txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
274 
275 	/*
276 	 * Wake all sync threads and wait for them to die.
277 	 */
278 	mutex_enter(&tx->tx_sync_lock);
279 
280 	ASSERT(tx->tx_threads == 2);
281 
282 	tx->tx_exiting = 1;
283 
284 	cv_broadcast(&tx->tx_quiesce_more_cv);
285 	cv_broadcast(&tx->tx_quiesce_done_cv);
286 	cv_broadcast(&tx->tx_sync_more_cv);
287 
288 	while (tx->tx_threads != 0)
289 		cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
290 
291 	tx->tx_exiting = 0;
292 
293 	mutex_exit(&tx->tx_sync_lock);
294 }
295 
296 uint64_t
txg_hold_open(dsl_pool_t * dp,txg_handle_t * th)297 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
298 {
299 	tx_state_t *tx = &dp->dp_tx;
300 	tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
301 	uint64_t txg;
302 
303 	mutex_enter(&tc->tc_open_lock);
304 	txg = tx->tx_open_txg;
305 
306 	mutex_enter(&tc->tc_lock);
307 	tc->tc_count[txg & TXG_MASK]++;
308 	mutex_exit(&tc->tc_lock);
309 
310 	th->th_cpu = tc;
311 	th->th_txg = txg;
312 
313 	return (txg);
314 }
315 
316 void
txg_rele_to_quiesce(txg_handle_t * th)317 txg_rele_to_quiesce(txg_handle_t *th)
318 {
319 	tx_cpu_t *tc = th->th_cpu;
320 
321 	ASSERT(!MUTEX_HELD(&tc->tc_lock));
322 	mutex_exit(&tc->tc_open_lock);
323 }
324 
325 void
txg_register_callbacks(txg_handle_t * th,list_t * tx_callbacks)326 txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
327 {
328 	tx_cpu_t *tc = th->th_cpu;
329 	int g = th->th_txg & TXG_MASK;
330 
331 	mutex_enter(&tc->tc_lock);
332 	list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
333 	mutex_exit(&tc->tc_lock);
334 }
335 
336 void
txg_rele_to_sync(txg_handle_t * th)337 txg_rele_to_sync(txg_handle_t *th)
338 {
339 	tx_cpu_t *tc = th->th_cpu;
340 	int g = th->th_txg & TXG_MASK;
341 
342 	mutex_enter(&tc->tc_lock);
343 	ASSERT(tc->tc_count[g] != 0);
344 	if (--tc->tc_count[g] == 0)
345 		cv_broadcast(&tc->tc_cv[g]);
346 	mutex_exit(&tc->tc_lock);
347 
348 	th->th_cpu = NULL;	/* defensive */
349 }
350 
351 /*
352  * Blocks until all transactions in the group are committed.
353  *
354  * On return, the transaction group has reached a stable state in which it can
355  * then be passed off to the syncing context.
356  */
357 /*
358  * XXX: Ideally this function should be static, but in order to force compiler
359  * to not inline it and make visible from Dtrace dynamic tracking we expose
360  * it forcefully.
361  */
362 /*
363  * Blocks until all transactions in the group are committed.
364  *
365  * On return, the transaction group has reached a stable state in which it can
366  * then be passed off to the syncing context.
367  */
368 void
txg_quiesce(dsl_pool_t * dp,uint64_t txg)369 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
370 {
371 	tx_state_t *tx = &dp->dp_tx;
372 	int g = txg & TXG_MASK;
373 	int c;
374 
375 	/*
376 	 * Grab all tc_open_locks so nobody else can get into this txg.
377 	 */
378 	for (c = 0; c < max_ncpus; c++)
379 		mutex_enter(&tx->tx_cpu[c].tc_open_lock);
380 
381 	ASSERT(txg == tx->tx_open_txg);
382 	tx->tx_open_txg++;
383 	tx->tx_open_time = gethrtime();
384 
385 	DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
386 	DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
387 
388 	DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
389 	DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
390 
391 	/*
392 	 * Now that we've incremented tx_open_txg, we can let threads
393 	 * enter the next transaction group.
394 	 */
395 	for (c = 0; c < max_ncpus; c++)
396 		mutex_exit(&tx->tx_cpu[c].tc_open_lock);
397 
398 	/*
399 	 * Quiesce the transaction group by waiting for everyone to txg_exit().
400 	 */
401 	for (c = 0; c < max_ncpus; c++) {
402 		tx_cpu_t *tc = &tx->tx_cpu[c];
403 		mutex_enter(&tc->tc_lock);
404 		while (tc->tc_count[g] != 0)
405 			cv_wait(&tc->tc_cv[g], &tc->tc_lock);
406 		mutex_exit(&tc->tc_lock);
407 	}
408 }
409 
410 static void
txg_do_callbacks(void * arg)411 txg_do_callbacks(void *arg)
412 {
413 	list_t *cb_list = arg;
414 
415 	dmu_tx_do_callbacks(cb_list, 0);
416 
417 	list_destroy(cb_list);
418 
419 	kmem_free(cb_list, sizeof (list_t));
420 }
421 
422 /*
423  * Dispatch the commit callbacks registered on this txg to worker threads.
424  *
425  * If no callbacks are registered for a given TXG, nothing happens.
426  * This function creates a taskq for the associated pool, if needed.
427  */
428 static void
txg_dispatch_callbacks(dsl_pool_t * dp,uint64_t txg)429 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
430 {
431 	int c;
432 	tx_state_t *tx = &dp->dp_tx;
433 	list_t *cb_list;
434 
435 	for (c = 0; c < max_ncpus; c++) {
436 		tx_cpu_t *tc = &tx->tx_cpu[c];
437 		/*
438 		 * No need to lock tx_cpu_t at this point, since this can
439 		 * only be called once a txg has been synced.
440 		 */
441 
442 		int g = txg & TXG_MASK;
443 
444 		if (list_is_empty(&tc->tc_callbacks[g]))
445 			continue;
446 
447 		if (tx->tx_commit_cb_taskq == NULL) {
448 			/*
449 			 * Commit callback taskq hasn't been created yet.
450 			 */
451 			tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
452 			    max_ncpus, minclsyspri, max_ncpus, max_ncpus * 2,
453 			    TASKQ_PREPOPULATE);
454 		}
455 
456 		cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
457 		list_create(cb_list, sizeof (dmu_tx_callback_t),
458 		    offsetof(dmu_tx_callback_t, dcb_node));
459 
460 		list_move_tail(cb_list, &tc->tc_callbacks[g]);
461 
462 		(void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
463 		    txg_do_callbacks, cb_list, TQ_SLEEP);
464 	}
465 }
466 
467 static void
txg_sync_thread(void * arg)468 txg_sync_thread(void *arg)
469 {
470 	dsl_pool_t *dp = arg;
471 	spa_t *spa = dp->dp_spa;
472 	tx_state_t *tx = &dp->dp_tx;
473 	callb_cpr_t cpr;
474 	uint64_t start, delta;
475 
476 	txg_thread_enter(tx, &cpr);
477 
478 	start = delta = 0;
479 	for (;;) {
480 		uint64_t timeout = zfs_txg_timeout * hz;
481 		uint64_t timer;
482 		uint64_t txg;
483 
484 		/*
485 		 * We sync when we're scanning, there's someone waiting
486 		 * on us, or the quiesce thread has handed off a txg to
487 		 * us, or we have reached our timeout.
488 		 */
489 		timer = (delta >= timeout ? 0 : timeout - delta);
490 		while (!dsl_scan_active(dp->dp_scan) &&
491 		    !tx->tx_exiting && timer > 0 &&
492 		    tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
493 		    tx->tx_quiesced_txg == 0 &&
494 		    dp->dp_dirty_total < zfs_dirty_data_sync) {
495 			dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
496 			    tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
497 			txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
498 			delta = ddi_get_lbolt() - start;
499 			timer = (delta > timeout ? 0 : timeout - delta);
500 		}
501 
502 		/*
503 		 * Wait until the quiesce thread hands off a txg to us,
504 		 * prompting it to do so if necessary.
505 		 */
506 		while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
507 			if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
508 				tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
509 			cv_broadcast(&tx->tx_quiesce_more_cv);
510 			txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
511 		}
512 
513 		if (tx->tx_exiting)
514 			txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
515 
516 		/*
517 		 * Consume the quiesced txg which has been handed off to
518 		 * us.  This may cause the quiescing thread to now be
519 		 * able to quiesce another txg, so we must signal it.
520 		 */
521 		txg = tx->tx_quiesced_txg;
522 		tx->tx_quiesced_txg = 0;
523 		tx->tx_syncing_txg = txg;
524 		DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
525 		cv_broadcast(&tx->tx_quiesce_more_cv);
526 
527 		dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
528 		    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
529 		mutex_exit(&tx->tx_sync_lock);
530 
531 		start = ddi_get_lbolt();
532 		spa_sync(spa, txg);
533 		delta = ddi_get_lbolt() - start;
534 
535 		mutex_enter(&tx->tx_sync_lock);
536 		tx->tx_synced_txg = txg;
537 		tx->tx_syncing_txg = 0;
538 		DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
539 		cv_broadcast(&tx->tx_sync_done_cv);
540 
541 		/*
542 		 * Dispatch commit callbacks to worker threads.
543 		 */
544 		txg_dispatch_callbacks(dp, txg);
545 	}
546 }
547 
548 static void
txg_quiesce_thread(void * arg)549 txg_quiesce_thread(void *arg)
550 {
551 	dsl_pool_t *dp = arg;
552 	tx_state_t *tx = &dp->dp_tx;
553 	callb_cpr_t cpr;
554 
555 	txg_thread_enter(tx, &cpr);
556 
557 	for (;;) {
558 		uint64_t txg;
559 
560 		/*
561 		 * We quiesce when there's someone waiting on us.
562 		 * However, we can only have one txg in "quiescing" or
563 		 * "quiesced, waiting to sync" state.  So we wait until
564 		 * the "quiesced, waiting to sync" txg has been consumed
565 		 * by the sync thread.
566 		 */
567 		while (!tx->tx_exiting &&
568 		    (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
569 		    tx->tx_quiesced_txg != 0))
570 			txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
571 
572 		if (tx->tx_exiting)
573 			txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
574 
575 		txg = tx->tx_open_txg;
576 		dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
577 		    txg, tx->tx_quiesce_txg_waiting,
578 		    tx->tx_sync_txg_waiting);
579 		mutex_exit(&tx->tx_sync_lock);
580 		txg_quiesce(dp, txg);
581 		mutex_enter(&tx->tx_sync_lock);
582 
583 		/*
584 		 * Hand this txg off to the sync thread.
585 		 */
586 		dprintf("quiesce done, handing off txg %llu\n", txg);
587 		tx->tx_quiesced_txg = txg;
588 		DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
589 		cv_broadcast(&tx->tx_sync_more_cv);
590 		cv_broadcast(&tx->tx_quiesce_done_cv);
591 	}
592 }
593 
594 /*
595  * Delay this thread by delay nanoseconds if we are still in the open
596  * transaction group and there is already a waiting txg quiesing or quiesced.
597  * Abort the delay if this txg stalls or enters the quiesing state.
598  */
599 void
txg_delay(dsl_pool_t * dp,uint64_t txg,hrtime_t delay,hrtime_t resolution)600 txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
601 {
602 	tx_state_t *tx = &dp->dp_tx;
603 	hrtime_t start = gethrtime();
604 
605 	/* don't delay if this txg could transition to quiescing immediately */
606 	if (tx->tx_open_txg > txg ||
607 	    tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
608 		return;
609 
610 	mutex_enter(&tx->tx_sync_lock);
611 	if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
612 		mutex_exit(&tx->tx_sync_lock);
613 		return;
614 	}
615 
616 	while (gethrtime() - start < delay &&
617 	    tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
618 		(void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
619 		    &tx->tx_sync_lock, delay, resolution, 0);
620 	}
621 
622 	mutex_exit(&tx->tx_sync_lock);
623 }
624 
625 void
txg_wait_synced(dsl_pool_t * dp,uint64_t txg)626 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
627 {
628 	tx_state_t *tx = &dp->dp_tx;
629 
630 	ASSERT(!dsl_pool_config_held(dp));
631 
632 	mutex_enter(&tx->tx_sync_lock);
633 	ASSERT(tx->tx_threads == 2);
634 	if (txg == 0)
635 		txg = tx->tx_open_txg + TXG_DEFER_SIZE;
636 	if (tx->tx_sync_txg_waiting < txg)
637 		tx->tx_sync_txg_waiting = txg;
638 	dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
639 	    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
640 	while (tx->tx_synced_txg < txg) {
641 		dprintf("broadcasting sync more "
642 		    "tx_synced=%llu waiting=%llu dp=%p\n",
643 		    tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
644 		cv_broadcast(&tx->tx_sync_more_cv);
645 		cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
646 	}
647 	mutex_exit(&tx->tx_sync_lock);
648 }
649 
650 void
txg_wait_open(dsl_pool_t * dp,uint64_t txg)651 txg_wait_open(dsl_pool_t *dp, uint64_t txg)
652 {
653 	tx_state_t *tx = &dp->dp_tx;
654 
655 	ASSERT(!dsl_pool_config_held(dp));
656 
657 	mutex_enter(&tx->tx_sync_lock);
658 	ASSERT(tx->tx_threads == 2);
659 	if (txg == 0)
660 		txg = tx->tx_open_txg + 1;
661 	if (tx->tx_quiesce_txg_waiting < txg)
662 		tx->tx_quiesce_txg_waiting = txg;
663 	dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
664 	    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
665 	while (tx->tx_open_txg < txg) {
666 		cv_broadcast(&tx->tx_quiesce_more_cv);
667 		cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
668 	}
669 	mutex_exit(&tx->tx_sync_lock);
670 }
671 
672 /*
673  * If there isn't a txg syncing or in the pipeline, push another txg through
674  * the pipeline by queiscing the open txg.
675  */
676 void
txg_kick(dsl_pool_t * dp)677 txg_kick(dsl_pool_t *dp)
678 {
679 	tx_state_t *tx = &dp->dp_tx;
680 
681 	ASSERT(!dsl_pool_config_held(dp));
682 
683 	mutex_enter(&tx->tx_sync_lock);
684 	if (tx->tx_syncing_txg == 0 &&
685 	    tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
686 	    tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
687 	    tx->tx_quiesced_txg <= tx->tx_synced_txg) {
688 		tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
689 		cv_broadcast(&tx->tx_quiesce_more_cv);
690 	}
691 	mutex_exit(&tx->tx_sync_lock);
692 }
693 
694 boolean_t
txg_stalled(dsl_pool_t * dp)695 txg_stalled(dsl_pool_t *dp)
696 {
697 	tx_state_t *tx = &dp->dp_tx;
698 	return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
699 }
700 
701 boolean_t
txg_sync_waiting(dsl_pool_t * dp)702 txg_sync_waiting(dsl_pool_t *dp)
703 {
704 	tx_state_t *tx = &dp->dp_tx;
705 
706 	return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
707 	    tx->tx_quiesced_txg != 0);
708 }
709 
710 /*
711  * Per-txg object lists.
712  */
713 void
txg_list_create(txg_list_t * tl,size_t offset)714 txg_list_create(txg_list_t *tl, size_t offset)
715 {
716 	int t;
717 
718 	mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
719 
720 	tl->tl_offset = offset;
721 
722 	for (t = 0; t < TXG_SIZE; t++)
723 		tl->tl_head[t] = NULL;
724 }
725 
726 void
txg_list_destroy(txg_list_t * tl)727 txg_list_destroy(txg_list_t *tl)
728 {
729 	int t;
730 
731 	for (t = 0; t < TXG_SIZE; t++)
732 		ASSERT(txg_list_empty(tl, t));
733 
734 	mutex_destroy(&tl->tl_lock);
735 }
736 
737 boolean_t
txg_list_empty(txg_list_t * tl,uint64_t txg)738 txg_list_empty(txg_list_t *tl, uint64_t txg)
739 {
740 	return (tl->tl_head[txg & TXG_MASK] == NULL);
741 }
742 
743 /*
744  * Returns true if all txg lists are empty.
745  *
746  * Warning: this is inherently racy (an item could be added immediately after this
747  * function returns). We don't bother with the lock because it wouldn't change the
748  * semantics.
749  */
750 boolean_t
txg_all_lists_empty(txg_list_t * tl)751 txg_all_lists_empty(txg_list_t *tl)
752 {
753 	for (int i = 0; i < TXG_SIZE; i++) {
754 		if (!txg_list_empty(tl, i)) {
755 			return (B_FALSE);
756 		}
757 	}
758 	return (B_TRUE);
759 }
760 
761 /*
762  * Add an entry to the list (unless it's already on the list).
763  * Returns B_TRUE if it was actually added.
764  */
765 boolean_t
txg_list_add(txg_list_t * tl,void * p,uint64_t txg)766 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
767 {
768 	int t = txg & TXG_MASK;
769 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
770 	boolean_t add;
771 
772 	mutex_enter(&tl->tl_lock);
773 	add = (tn->tn_member[t] == 0);
774 	if (add) {
775 		tn->tn_member[t] = 1;
776 		tn->tn_next[t] = tl->tl_head[t];
777 		tl->tl_head[t] = tn;
778 	}
779 	mutex_exit(&tl->tl_lock);
780 
781 	return (add);
782 }
783 
784 /*
785  * Add an entry to the end of the list, unless it's already on the list.
786  * (walks list to find end)
787  * Returns B_TRUE if it was actually added.
788  */
789 boolean_t
txg_list_add_tail(txg_list_t * tl,void * p,uint64_t txg)790 txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
791 {
792 	int t = txg & TXG_MASK;
793 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
794 	boolean_t add;
795 
796 	mutex_enter(&tl->tl_lock);
797 	add = (tn->tn_member[t] == 0);
798 	if (add) {
799 		txg_node_t **tp;
800 
801 		for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
802 			continue;
803 
804 		tn->tn_member[t] = 1;
805 		tn->tn_next[t] = NULL;
806 		*tp = tn;
807 	}
808 	mutex_exit(&tl->tl_lock);
809 
810 	return (add);
811 }
812 
813 /*
814  * Remove the head of the list and return it.
815  */
816 void *
txg_list_remove(txg_list_t * tl,uint64_t txg)817 txg_list_remove(txg_list_t *tl, uint64_t txg)
818 {
819 	int t = txg & TXG_MASK;
820 	txg_node_t *tn;
821 	void *p = NULL;
822 
823 	mutex_enter(&tl->tl_lock);
824 	if ((tn = tl->tl_head[t]) != NULL) {
825 		p = (char *)tn - tl->tl_offset;
826 		tl->tl_head[t] = tn->tn_next[t];
827 		tn->tn_next[t] = NULL;
828 		tn->tn_member[t] = 0;
829 	}
830 	mutex_exit(&tl->tl_lock);
831 
832 	return (p);
833 }
834 
835 /*
836  * Remove a specific item from the list and return it.
837  */
838 void *
txg_list_remove_this(txg_list_t * tl,void * p,uint64_t txg)839 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
840 {
841 	int t = txg & TXG_MASK;
842 	txg_node_t *tn, **tp;
843 
844 	mutex_enter(&tl->tl_lock);
845 
846 	for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
847 		if ((char *)tn - tl->tl_offset == p) {
848 			*tp = tn->tn_next[t];
849 			tn->tn_next[t] = NULL;
850 			tn->tn_member[t] = 0;
851 			mutex_exit(&tl->tl_lock);
852 			return (p);
853 		}
854 	}
855 
856 	mutex_exit(&tl->tl_lock);
857 
858 	return (NULL);
859 }
860 
861 boolean_t
txg_list_member(txg_list_t * tl,void * p,uint64_t txg)862 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
863 {
864 	int t = txg & TXG_MASK;
865 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
866 
867 	return (tn->tn_member[t] != 0);
868 }
869 
870 /*
871  * Walk a txg list -- only safe if you know it's not changing.
872  */
873 void *
txg_list_head(txg_list_t * tl,uint64_t txg)874 txg_list_head(txg_list_t *tl, uint64_t txg)
875 {
876 	int t = txg & TXG_MASK;
877 	txg_node_t *tn = tl->tl_head[t];
878 
879 	return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
880 }
881 
882 void *
txg_list_next(txg_list_t * tl,void * p,uint64_t txg)883 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
884 {
885 	int t = txg & TXG_MASK;
886 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
887 
888 	tn = tn->tn_next[t];
889 
890 	return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
891 }
892