xref: /illumos-gate/usr/src/uts/common/fs/zfs/txg.c (revision b3d9f2e26021d3f55a281af30720589d303b9806)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
55ad82045Snd  * Common Development and Distribution License (the "License").
65ad82045Snd  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23383e7c74SXin Li  * Portions Copyright 2011 Martin Matuska
24adbbcffaSAdam H. Leventhal  * Copyright (c) 2013 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/txg_impl.h>
29fa9e4066Sahrens #include <sys/dmu_impl.h>
30d20e665cSRicardo M. Correia #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dsl_pool.h>
323f9d6ad7SLin Ling #include <sys/dsl_scan.h>
33fa9e4066Sahrens #include <sys/callb.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens /*
36adbbcffaSAdam H. Leventhal  * ZFS Transaction Groups
37adbbcffaSAdam H. Leventhal  * ----------------------
38adbbcffaSAdam H. Leventhal  *
39adbbcffaSAdam H. Leventhal  * ZFS transaction groups are, as the name implies, groups of transactions
40adbbcffaSAdam H. Leventhal  * that act on persistent state. ZFS asserts consistency at the granularity of
41adbbcffaSAdam H. Leventhal  * these transaction groups. Each successive transaction group (txg) is
42adbbcffaSAdam H. Leventhal  * assigned a 64-bit consecutive identifier. There are three active
43adbbcffaSAdam H. Leventhal  * transaction group states: open, quiescing, or syncing. At any given time,
44adbbcffaSAdam H. Leventhal  * there may be an active txg associated with each state; each active txg may
45adbbcffaSAdam H. Leventhal  * either be processing, or blocked waiting to enter the next state. There may
46adbbcffaSAdam H. Leventhal  * be up to three active txgs, and there is always a txg in the open state
47adbbcffaSAdam H. Leventhal  * (though it may be blocked waiting to enter the quiescing state). In broad
48adbbcffaSAdam H. Leventhal  * strokes, transactions — operations that change in-memory structures — are
49adbbcffaSAdam H. Leventhal  * accepted into the txg in the open state, and are completed while the txg is
50adbbcffaSAdam H. Leventhal  * in the open or quiescing states. The accumulated changes are written to
51adbbcffaSAdam H. Leventhal  * disk in the syncing state.
52adbbcffaSAdam H. Leventhal  *
53adbbcffaSAdam H. Leventhal  * Open
54adbbcffaSAdam H. Leventhal  *
55adbbcffaSAdam H. Leventhal  * When a new txg becomes active, it first enters the open state. New
56adbbcffaSAdam H. Leventhal  * transactions — updates to in-memory structures — are assigned to the
57adbbcffaSAdam H. Leventhal  * currently open txg. There is always a txg in the open state so that ZFS can
58adbbcffaSAdam H. Leventhal  * accept new changes (though the txg may refuse new changes if it has hit
59adbbcffaSAdam H. Leventhal  * some limit). ZFS advances the open txg to the next state for a variety of
60adbbcffaSAdam H. Leventhal  * reasons such as it hitting a time or size threshold, or the execution of an
61adbbcffaSAdam H. Leventhal  * administrative action that must be completed in the syncing state.
62adbbcffaSAdam H. Leventhal  *
63adbbcffaSAdam H. Leventhal  * Quiescing
64adbbcffaSAdam H. Leventhal  *
65adbbcffaSAdam H. Leventhal  * After a txg exits the open state, it enters the quiescing state. The
66adbbcffaSAdam H. Leventhal  * quiescing state is intended to provide a buffer between accepting new
67adbbcffaSAdam H. Leventhal  * transactions in the open state and writing them out to stable storage in
68adbbcffaSAdam H. Leventhal  * the syncing state. While quiescing, transactions can continue their
69adbbcffaSAdam H. Leventhal  * operation without delaying either of the other states. Typically, a txg is
70adbbcffaSAdam H. Leventhal  * in the quiescing state very briefly since the operations are bounded by
71adbbcffaSAdam H. Leventhal  * software latencies rather than, say, slower I/O latencies. After all
72adbbcffaSAdam H. Leventhal  * transactions complete, the txg is ready to enter the next state.
73adbbcffaSAdam H. Leventhal  *
74adbbcffaSAdam H. Leventhal  * Syncing
75adbbcffaSAdam H. Leventhal  *
76adbbcffaSAdam H. Leventhal  * In the syncing state, the in-memory state built up during the open and (to
77adbbcffaSAdam H. Leventhal  * a lesser degree) the quiescing states is written to stable storage. The
78adbbcffaSAdam H. Leventhal  * process of writing out modified data can, in turn modify more data. For
79adbbcffaSAdam H. Leventhal  * example when we write new blocks, we need to allocate space for them; those
80adbbcffaSAdam H. Leventhal  * allocations modify metadata (space maps)... which themselves must be
81adbbcffaSAdam H. Leventhal  * written to stable storage. During the sync state, ZFS iterates, writing out
82adbbcffaSAdam H. Leventhal  * data until it converges and all in-memory changes have been written out.
83adbbcffaSAdam H. Leventhal  * The first such pass is the largest as it encompasses all the modified user
84adbbcffaSAdam H. Leventhal  * data (as opposed to filesystem metadata). Subsequent passes typically have
85adbbcffaSAdam H. Leventhal  * far less data to write as they consist exclusively of filesystem metadata.
86adbbcffaSAdam H. Leventhal  *
87adbbcffaSAdam H. Leventhal  * To ensure convergence, after a certain number of passes ZFS begins
88adbbcffaSAdam H. Leventhal  * overwriting locations on stable storage that had been allocated earlier in
89adbbcffaSAdam H. Leventhal  * the syncing state (and subsequently freed). ZFS usually allocates new
90adbbcffaSAdam H. Leventhal  * blocks to optimize for large, continuous, writes. For the syncing state to
91adbbcffaSAdam H. Leventhal  * converge however it must complete a pass where no new blocks are allocated
92adbbcffaSAdam H. Leventhal  * since each allocation requires a modification of persistent metadata.
93adbbcffaSAdam H. Leventhal  * Further, to hasten convergence, after a prescribed number of passes, ZFS
94adbbcffaSAdam H. Leventhal  * also defers frees, and stops compressing.
95adbbcffaSAdam H. Leventhal  *
96adbbcffaSAdam H. Leventhal  * In addition to writing out user data, we must also execute synctasks during
97adbbcffaSAdam H. Leventhal  * the syncing context. A synctask is the mechanism by which some
98adbbcffaSAdam H. Leventhal  * administrative activities work such as creating and destroying snapshots or
99adbbcffaSAdam H. Leventhal  * datasets. Note that when a synctask is initiated it enters the open txg,
100adbbcffaSAdam H. Leventhal  * and ZFS then pushes that txg as quickly as possible to completion of the
101adbbcffaSAdam H. Leventhal  * syncing state in order to reduce the latency of the administrative
102adbbcffaSAdam H. Leventhal  * activity. To complete the syncing state, ZFS writes out a new uberblock,
103adbbcffaSAdam H. Leventhal  * the root of the tree of blocks that comprise all state stored on the ZFS
104adbbcffaSAdam H. Leventhal  * pool. Finally, if there is a quiesced txg waiting, we signal that it can
105adbbcffaSAdam H. Leventhal  * now transition to the syncing state.
106fa9e4066Sahrens  */
107fa9e4066Sahrens 
108fa9e4066Sahrens static void txg_sync_thread(dsl_pool_t *dp);
109fa9e4066Sahrens static void txg_quiesce_thread(dsl_pool_t *dp);
110fa9e4066Sahrens 
11144ecc532SGeorge Wilson int zfs_txg_timeout = 5;	/* max seconds worth of delta per txg */
112fa9e4066Sahrens 
113fa9e4066Sahrens /*
114fa9e4066Sahrens  * Prepare the txg subsystem.
115fa9e4066Sahrens  */
116fa9e4066Sahrens void
117fa9e4066Sahrens txg_init(dsl_pool_t *dp, uint64_t txg)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
1205ad82045Snd 	int c;
121fa9e4066Sahrens 	bzero(tx, sizeof (tx_state_t));
122fa9e4066Sahrens 
123fa9e4066Sahrens 	tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
124fa9e4066Sahrens 
1258f38d419Sek 	for (c = 0; c < max_ncpus; c++) {
1268f38d419Sek 		int i;
1278f38d419Sek 
1285ad82045Snd 		mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
1294a923759SGeorge Wilson 		mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_DEFAULT,
1304a923759SGeorge Wilson 		    NULL);
1318f38d419Sek 		for (i = 0; i < TXG_SIZE; i++) {
1328f38d419Sek 			cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
1338f38d419Sek 			    NULL);
134d20e665cSRicardo M. Correia 			list_create(&tx->tx_cpu[c].tc_callbacks[i],
135d20e665cSRicardo M. Correia 			    sizeof (dmu_tx_callback_t),
136d20e665cSRicardo M. Correia 			    offsetof(dmu_tx_callback_t, dcb_node));
1378f38d419Sek 		}
1388f38d419Sek 	}
1395ad82045Snd 
1405ad82045Snd 	mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
141fa9e4066Sahrens 
142b5e70f97SRicardo M. Correia 	cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
143b5e70f97SRicardo M. Correia 	cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
144b5e70f97SRicardo M. Correia 	cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
145b5e70f97SRicardo M. Correia 	cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
146b5e70f97SRicardo M. Correia 	cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
147b5e70f97SRicardo M. Correia 
148fa9e4066Sahrens 	tx->tx_open_txg = txg;
149fa9e4066Sahrens }
150fa9e4066Sahrens 
151fa9e4066Sahrens /*
152fa9e4066Sahrens  * Close down the txg subsystem.
153fa9e4066Sahrens  */
154fa9e4066Sahrens void
155fa9e4066Sahrens txg_fini(dsl_pool_t *dp)
156fa9e4066Sahrens {
157fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
1585ad82045Snd 	int c;
159fa9e4066Sahrens 
160fa9e4066Sahrens 	ASSERT(tx->tx_threads == 0);
161fa9e4066Sahrens 
1625ad82045Snd 	mutex_destroy(&tx->tx_sync_lock);
1635ad82045Snd 
164b5e70f97SRicardo M. Correia 	cv_destroy(&tx->tx_sync_more_cv);
165b5e70f97SRicardo M. Correia 	cv_destroy(&tx->tx_sync_done_cv);
166b5e70f97SRicardo M. Correia 	cv_destroy(&tx->tx_quiesce_more_cv);
167b5e70f97SRicardo M. Correia 	cv_destroy(&tx->tx_quiesce_done_cv);
168b5e70f97SRicardo M. Correia 	cv_destroy(&tx->tx_exit_cv);
169b5e70f97SRicardo M. Correia 
1708f38d419Sek 	for (c = 0; c < max_ncpus; c++) {
1718f38d419Sek 		int i;
1728f38d419Sek 
1734a923759SGeorge Wilson 		mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
1745ad82045Snd 		mutex_destroy(&tx->tx_cpu[c].tc_lock);
175d20e665cSRicardo M. Correia 		for (i = 0; i < TXG_SIZE; i++) {
1768f38d419Sek 			cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
177d20e665cSRicardo M. Correia 			list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
178d20e665cSRicardo M. Correia 		}
1798f38d419Sek 	}
180fa9e4066Sahrens 
181d20e665cSRicardo M. Correia 	if (tx->tx_commit_cb_taskq != NULL)
182d20e665cSRicardo M. Correia 		taskq_destroy(tx->tx_commit_cb_taskq);
183d20e665cSRicardo M. Correia 
184fa9e4066Sahrens 	kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
185fa9e4066Sahrens 
186fa9e4066Sahrens 	bzero(tx, sizeof (tx_state_t));
187fa9e4066Sahrens }
188fa9e4066Sahrens 
189fa9e4066Sahrens /*
190fa9e4066Sahrens  * Start syncing transaction groups.
191fa9e4066Sahrens  */
192fa9e4066Sahrens void
193fa9e4066Sahrens txg_sync_start(dsl_pool_t *dp)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	mutex_enter(&tx->tx_sync_lock);
198fa9e4066Sahrens 
199fa9e4066Sahrens 	dprintf("pool %p\n", dp);
200fa9e4066Sahrens 
201fa9e4066Sahrens 	ASSERT(tx->tx_threads == 0);
202fa9e4066Sahrens 
2031ab7f2deSmaybee 	tx->tx_threads = 2;
204fa9e4066Sahrens 
205fa9e4066Sahrens 	tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
206fa9e4066Sahrens 	    dp, 0, &p0, TS_RUN, minclsyspri);
207fa9e4066Sahrens 
208088f3894Sahrens 	/*
209088f3894Sahrens 	 * The sync thread can need a larger-than-default stack size on
210088f3894Sahrens 	 * 32-bit x86.  This is due in part to nested pools and
211088f3894Sahrens 	 * scrub_visitbp() recursion.
212088f3894Sahrens 	 */
2133f9d6ad7SLin Ling 	tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
214fa9e4066Sahrens 	    dp, 0, &p0, TS_RUN, minclsyspri);
215fa9e4066Sahrens 
216fa9e4066Sahrens 	mutex_exit(&tx->tx_sync_lock);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
219fa9e4066Sahrens static void
220fa9e4066Sahrens txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
221fa9e4066Sahrens {
222fa9e4066Sahrens 	CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
223fa9e4066Sahrens 	mutex_enter(&tx->tx_sync_lock);
224fa9e4066Sahrens }
225fa9e4066Sahrens 
226fa9e4066Sahrens static void
227fa9e4066Sahrens txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
228fa9e4066Sahrens {
229fa9e4066Sahrens 	ASSERT(*tpp != NULL);
230fa9e4066Sahrens 	*tpp = NULL;
231fa9e4066Sahrens 	tx->tx_threads--;
232fa9e4066Sahrens 	cv_broadcast(&tx->tx_exit_cv);
233fa9e4066Sahrens 	CALLB_CPR_EXIT(cpr);		/* drops &tx->tx_sync_lock */
234fa9e4066Sahrens 	thread_exit();
235fa9e4066Sahrens }
236fa9e4066Sahrens 
237fa9e4066Sahrens static void
2380689f76cSAdam Leventhal txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
239fa9e4066Sahrens {
240fa9e4066Sahrens 	CALLB_CPR_SAFE_BEGIN(cpr);
241fa9e4066Sahrens 
2421ab7f2deSmaybee 	if (time)
243d3d50737SRafael Vanoni 		(void) cv_timedwait(cv, &tx->tx_sync_lock,
244d3d50737SRafael Vanoni 		    ddi_get_lbolt() + time);
245fa9e4066Sahrens 	else
246fa9e4066Sahrens 		cv_wait(cv, &tx->tx_sync_lock);
247fa9e4066Sahrens 
248fa9e4066Sahrens 	CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
249fa9e4066Sahrens }
250fa9e4066Sahrens 
251fa9e4066Sahrens /*
252fa9e4066Sahrens  * Stop syncing transaction groups.
253fa9e4066Sahrens  */
254fa9e4066Sahrens void
255fa9e4066Sahrens txg_sync_stop(dsl_pool_t *dp)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
258fa9e4066Sahrens 
259fa9e4066Sahrens 	dprintf("pool %p\n", dp);
260fa9e4066Sahrens 	/*
261fa9e4066Sahrens 	 * Finish off any work in progress.
262fa9e4066Sahrens 	 */
2631ab7f2deSmaybee 	ASSERT(tx->tx_threads == 2);
264468c413aSTim Haley 
265468c413aSTim Haley 	/*
266468c413aSTim Haley 	 * We need to ensure that we've vacated the deferred space_maps.
267468c413aSTim Haley 	 */
268468c413aSTim Haley 	txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	/*
2711ab7f2deSmaybee 	 * Wake all sync threads and wait for them to die.
272fa9e4066Sahrens 	 */
273fa9e4066Sahrens 	mutex_enter(&tx->tx_sync_lock);
274fa9e4066Sahrens 
2751ab7f2deSmaybee 	ASSERT(tx->tx_threads == 2);
276fa9e4066Sahrens 
277fa9e4066Sahrens 	tx->tx_exiting = 1;
278fa9e4066Sahrens 
279fa9e4066Sahrens 	cv_broadcast(&tx->tx_quiesce_more_cv);
280fa9e4066Sahrens 	cv_broadcast(&tx->tx_quiesce_done_cv);
281fa9e4066Sahrens 	cv_broadcast(&tx->tx_sync_more_cv);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	while (tx->tx_threads != 0)
284fa9e4066Sahrens 		cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
285fa9e4066Sahrens 
286fa9e4066Sahrens 	tx->tx_exiting = 0;
287fa9e4066Sahrens 
288fa9e4066Sahrens 	mutex_exit(&tx->tx_sync_lock);
289fa9e4066Sahrens }
290fa9e4066Sahrens 
291fa9e4066Sahrens uint64_t
292fa9e4066Sahrens txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
293fa9e4066Sahrens {
294fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
295fa9e4066Sahrens 	tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
296fa9e4066Sahrens 	uint64_t txg;
297fa9e4066Sahrens 
2984a923759SGeorge Wilson 	mutex_enter(&tc->tc_open_lock);
299fa9e4066Sahrens 	txg = tx->tx_open_txg;
3004a923759SGeorge Wilson 
3014a923759SGeorge Wilson 	mutex_enter(&tc->tc_lock);
302fa9e4066Sahrens 	tc->tc_count[txg & TXG_MASK]++;
3034a923759SGeorge Wilson 	mutex_exit(&tc->tc_lock);
304fa9e4066Sahrens 
305fa9e4066Sahrens 	th->th_cpu = tc;
306fa9e4066Sahrens 	th->th_txg = txg;
307fa9e4066Sahrens 
308fa9e4066Sahrens 	return (txg);
309fa9e4066Sahrens }
310fa9e4066Sahrens 
311fa9e4066Sahrens void
312fa9e4066Sahrens txg_rele_to_quiesce(txg_handle_t *th)
313fa9e4066Sahrens {
314fa9e4066Sahrens 	tx_cpu_t *tc = th->th_cpu;
315fa9e4066Sahrens 
3164a923759SGeorge Wilson 	ASSERT(!MUTEX_HELD(&tc->tc_lock));
3174a923759SGeorge Wilson 	mutex_exit(&tc->tc_open_lock);
318fa9e4066Sahrens }
319fa9e4066Sahrens 
320d20e665cSRicardo M. Correia void
321d20e665cSRicardo M. Correia txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
322d20e665cSRicardo M. Correia {
323d20e665cSRicardo M. Correia 	tx_cpu_t *tc = th->th_cpu;
324d20e665cSRicardo M. Correia 	int g = th->th_txg & TXG_MASK;
325d20e665cSRicardo M. Correia 
326d20e665cSRicardo M. Correia 	mutex_enter(&tc->tc_lock);
327d20e665cSRicardo M. Correia 	list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
328d20e665cSRicardo M. Correia 	mutex_exit(&tc->tc_lock);
329d20e665cSRicardo M. Correia }
330d20e665cSRicardo M. Correia 
331fa9e4066Sahrens void
332fa9e4066Sahrens txg_rele_to_sync(txg_handle_t *th)
333fa9e4066Sahrens {
334fa9e4066Sahrens 	tx_cpu_t *tc = th->th_cpu;
335fa9e4066Sahrens 	int g = th->th_txg & TXG_MASK;
336fa9e4066Sahrens 
337fa9e4066Sahrens 	mutex_enter(&tc->tc_lock);
338fa9e4066Sahrens 	ASSERT(tc->tc_count[g] != 0);
339fa9e4066Sahrens 	if (--tc->tc_count[g] == 0)
340fa9e4066Sahrens 		cv_broadcast(&tc->tc_cv[g]);
341fa9e4066Sahrens 	mutex_exit(&tc->tc_lock);
342fa9e4066Sahrens 
343fa9e4066Sahrens 	th->th_cpu = NULL;	/* defensive */
344fa9e4066Sahrens }
345fa9e4066Sahrens 
3463e30c24aSWill Andrews /*
3473e30c24aSWill Andrews  * Blocks until all transactions in the group are committed.
3483e30c24aSWill Andrews  *
3493e30c24aSWill Andrews  * On return, the transaction group has reached a stable state in which it can
3503e30c24aSWill Andrews  * then be passed off to the syncing context.
3513e30c24aSWill Andrews  */
352fa9e4066Sahrens static void
353fa9e4066Sahrens txg_quiesce(dsl_pool_t *dp, uint64_t txg)
354fa9e4066Sahrens {
355fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
356fa9e4066Sahrens 	int g = txg & TXG_MASK;
357fa9e4066Sahrens 	int c;
358fa9e4066Sahrens 
359fa9e4066Sahrens 	/*
3604a923759SGeorge Wilson 	 * Grab all tc_open_locks so nobody else can get into this txg.
361fa9e4066Sahrens 	 */
362fa9e4066Sahrens 	for (c = 0; c < max_ncpus; c++)
3634a923759SGeorge Wilson 		mutex_enter(&tx->tx_cpu[c].tc_open_lock);
364fa9e4066Sahrens 
365fa9e4066Sahrens 	ASSERT(txg == tx->tx_open_txg);
366fa9e4066Sahrens 	tx->tx_open_txg++;
367fa9e4066Sahrens 
3680689f76cSAdam Leventhal 	DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
3690689f76cSAdam Leventhal 	DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
3700689f76cSAdam Leventhal 
371fa9e4066Sahrens 	/*
372fa9e4066Sahrens 	 * Now that we've incremented tx_open_txg, we can let threads
373fa9e4066Sahrens 	 * enter the next transaction group.
374fa9e4066Sahrens 	 */
375fa9e4066Sahrens 	for (c = 0; c < max_ncpus; c++)
3764a923759SGeorge Wilson 		mutex_exit(&tx->tx_cpu[c].tc_open_lock);
377fa9e4066Sahrens 
378fa9e4066Sahrens 	/*
379fa9e4066Sahrens 	 * Quiesce the transaction group by waiting for everyone to txg_exit().
380fa9e4066Sahrens 	 */
381fa9e4066Sahrens 	for (c = 0; c < max_ncpus; c++) {
382fa9e4066Sahrens 		tx_cpu_t *tc = &tx->tx_cpu[c];
383fa9e4066Sahrens 		mutex_enter(&tc->tc_lock);
384fa9e4066Sahrens 		while (tc->tc_count[g] != 0)
385fa9e4066Sahrens 			cv_wait(&tc->tc_cv[g], &tc->tc_lock);
386fa9e4066Sahrens 		mutex_exit(&tc->tc_lock);
387fa9e4066Sahrens 	}
388fa9e4066Sahrens }
389fa9e4066Sahrens 
390d20e665cSRicardo M. Correia static void
391d20e665cSRicardo M. Correia txg_do_callbacks(list_t *cb_list)
392d20e665cSRicardo M. Correia {
393d20e665cSRicardo M. Correia 	dmu_tx_do_callbacks(cb_list, 0);
394d20e665cSRicardo M. Correia 
395d20e665cSRicardo M. Correia 	list_destroy(cb_list);
396d20e665cSRicardo M. Correia 
397d20e665cSRicardo M. Correia 	kmem_free(cb_list, sizeof (list_t));
398d20e665cSRicardo M. Correia }
399d20e665cSRicardo M. Correia 
400d20e665cSRicardo M. Correia /*
401d20e665cSRicardo M. Correia  * Dispatch the commit callbacks registered on this txg to worker threads.
4023e30c24aSWill Andrews  *
4033e30c24aSWill Andrews  * If no callbacks are registered for a given TXG, nothing happens.
4043e30c24aSWill Andrews  * This function creates a taskq for the associated pool, if needed.
405d20e665cSRicardo M. Correia  */
406d20e665cSRicardo M. Correia static void
407d20e665cSRicardo M. Correia txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
408d20e665cSRicardo M. Correia {
409d20e665cSRicardo M. Correia 	int c;
410d20e665cSRicardo M. Correia 	tx_state_t *tx = &dp->dp_tx;
411d20e665cSRicardo M. Correia 	list_t *cb_list;
412d20e665cSRicardo M. Correia 
413d20e665cSRicardo M. Correia 	for (c = 0; c < max_ncpus; c++) {
414d20e665cSRicardo M. Correia 		tx_cpu_t *tc = &tx->tx_cpu[c];
4153e30c24aSWill Andrews 		/*
4163e30c24aSWill Andrews 		 * No need to lock tx_cpu_t at this point, since this can
4173e30c24aSWill Andrews 		 * only be called once a txg has been synced.
4183e30c24aSWill Andrews 		 */
419d20e665cSRicardo M. Correia 
420d20e665cSRicardo M. Correia 		int g = txg & TXG_MASK;
421d20e665cSRicardo M. Correia 
422d20e665cSRicardo M. Correia 		if (list_is_empty(&tc->tc_callbacks[g]))
423d20e665cSRicardo M. Correia 			continue;
424d20e665cSRicardo M. Correia 
425d20e665cSRicardo M. Correia 		if (tx->tx_commit_cb_taskq == NULL) {
426d20e665cSRicardo M. Correia 			/*
427d20e665cSRicardo M. Correia 			 * Commit callback taskq hasn't been created yet.
428d20e665cSRicardo M. Correia 			 */
429d20e665cSRicardo M. Correia 			tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
430d20e665cSRicardo M. Correia 			    max_ncpus, minclsyspri, max_ncpus, max_ncpus * 2,
431d20e665cSRicardo M. Correia 			    TASKQ_PREPOPULATE);
432d20e665cSRicardo M. Correia 		}
433d20e665cSRicardo M. Correia 
434d20e665cSRicardo M. Correia 		cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
435d20e665cSRicardo M. Correia 		list_create(cb_list, sizeof (dmu_tx_callback_t),
436d20e665cSRicardo M. Correia 		    offsetof(dmu_tx_callback_t, dcb_node));
437d20e665cSRicardo M. Correia 
438*b3d9f2e2SWill Andrews 		list_move_tail(cb_list, &tc->tc_callbacks[g]);
439d20e665cSRicardo M. Correia 
440d20e665cSRicardo M. Correia 		(void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
441d20e665cSRicardo M. Correia 		    txg_do_callbacks, cb_list, TQ_SLEEP);
442d20e665cSRicardo M. Correia 	}
443d20e665cSRicardo M. Correia }
444d20e665cSRicardo M. Correia 
445fa9e4066Sahrens static void
446fa9e4066Sahrens txg_sync_thread(dsl_pool_t *dp)
447fa9e4066Sahrens {
448b16da2e2SGeorge Wilson 	spa_t *spa = dp->dp_spa;
449fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
450fa9e4066Sahrens 	callb_cpr_t cpr;
45105715f94SMark Maybee 	uint64_t start, delta;
452fa9e4066Sahrens 
453fa9e4066Sahrens 	txg_thread_enter(tx, &cpr);
454fa9e4066Sahrens 
4551ab7f2deSmaybee 	start = delta = 0;
456fa9e4066Sahrens 	for (;;) {
45705715f94SMark Maybee 		uint64_t timer, timeout = zfs_txg_timeout * hz;
45805715f94SMark Maybee 		uint64_t txg;
459fa9e4066Sahrens 
460fa9e4066Sahrens 		/*
4613f9d6ad7SLin Ling 		 * We sync when we're scanning, there's someone waiting
46288b7b0f2SMatthew Ahrens 		 * on us, or the quiesce thread has handed off a txg to
46388b7b0f2SMatthew Ahrens 		 * us, or we have reached our timeout.
464fa9e4066Sahrens 		 */
4651ab7f2deSmaybee 		timer = (delta >= timeout ? 0 : timeout - delta);
466cde58dbcSMatthew Ahrens 		while (!dsl_scan_active(dp->dp_scan) &&
46788b7b0f2SMatthew Ahrens 		    !tx->tx_exiting && timer > 0 &&
468fa9e4066Sahrens 		    tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
469fa9e4066Sahrens 		    tx->tx_quiesced_txg == 0) {
470fa9e4066Sahrens 			dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
471fa9e4066Sahrens 			    tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
4721ab7f2deSmaybee 			txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
473d3d50737SRafael Vanoni 			delta = ddi_get_lbolt() - start;
4741ab7f2deSmaybee 			timer = (delta > timeout ? 0 : timeout - delta);
475fa9e4066Sahrens 		}
476fa9e4066Sahrens 
477fa9e4066Sahrens 		/*
478fa9e4066Sahrens 		 * Wait until the quiesce thread hands off a txg to us,
479fa9e4066Sahrens 		 * prompting it to do so if necessary.
480fa9e4066Sahrens 		 */
481fa9e4066Sahrens 		while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
482fa9e4066Sahrens 			if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
483fa9e4066Sahrens 				tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
484fa9e4066Sahrens 			cv_broadcast(&tx->tx_quiesce_more_cv);
485fa9e4066Sahrens 			txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
486fa9e4066Sahrens 		}
487fa9e4066Sahrens 
488fa9e4066Sahrens 		if (tx->tx_exiting)
489fa9e4066Sahrens 			txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
490fa9e4066Sahrens 
491fa9e4066Sahrens 		/*
492fa9e4066Sahrens 		 * Consume the quiesced txg which has been handed off to
493fa9e4066Sahrens 		 * us.  This may cause the quiescing thread to now be
494fa9e4066Sahrens 		 * able to quiesce another txg, so we must signal it.
495fa9e4066Sahrens 		 */
496fa9e4066Sahrens 		txg = tx->tx_quiesced_txg;
497fa9e4066Sahrens 		tx->tx_quiesced_txg = 0;
498fa9e4066Sahrens 		tx->tx_syncing_txg = txg;
4990689f76cSAdam Leventhal 		DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
500fa9e4066Sahrens 		cv_broadcast(&tx->tx_quiesce_more_cv);
501fa9e4066Sahrens 
502fa9e4066Sahrens 		dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
5038f38d419Sek 		    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
504fa9e4066Sahrens 		mutex_exit(&tx->tx_sync_lock);
50505715f94SMark Maybee 
506d3d50737SRafael Vanoni 		start = ddi_get_lbolt();
507b16da2e2SGeorge Wilson 		spa_sync(spa, txg);
508d3d50737SRafael Vanoni 		delta = ddi_get_lbolt() - start;
5091ab7f2deSmaybee 
510fa9e4066Sahrens 		mutex_enter(&tx->tx_sync_lock);
511fa9e4066Sahrens 		tx->tx_synced_txg = txg;
512fa9e4066Sahrens 		tx->tx_syncing_txg = 0;
5130689f76cSAdam Leventhal 		DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
514fa9e4066Sahrens 		cv_broadcast(&tx->tx_sync_done_cv);
515d20e665cSRicardo M. Correia 
516d20e665cSRicardo M. Correia 		/*
517d20e665cSRicardo M. Correia 		 * Dispatch commit callbacks to worker threads.
518d20e665cSRicardo M. Correia 		 */
519d20e665cSRicardo M. Correia 		txg_dispatch_callbacks(dp, txg);
520fa9e4066Sahrens 	}
521fa9e4066Sahrens }
522fa9e4066Sahrens 
523fa9e4066Sahrens static void
524fa9e4066Sahrens txg_quiesce_thread(dsl_pool_t *dp)
525fa9e4066Sahrens {
526fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
527fa9e4066Sahrens 	callb_cpr_t cpr;
528fa9e4066Sahrens 
529fa9e4066Sahrens 	txg_thread_enter(tx, &cpr);
530fa9e4066Sahrens 
531fa9e4066Sahrens 	for (;;) {
532fa9e4066Sahrens 		uint64_t txg;
533fa9e4066Sahrens 
534fa9e4066Sahrens 		/*
535fa9e4066Sahrens 		 * We quiesce when there's someone waiting on us.
536fa9e4066Sahrens 		 * However, we can only have one txg in "quiescing" or
537fa9e4066Sahrens 		 * "quiesced, waiting to sync" state.  So we wait until
538fa9e4066Sahrens 		 * the "quiesced, waiting to sync" txg has been consumed
539fa9e4066Sahrens 		 * by the sync thread.
540fa9e4066Sahrens 		 */
541fa9e4066Sahrens 		while (!tx->tx_exiting &&
542fa9e4066Sahrens 		    (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
543fa9e4066Sahrens 		    tx->tx_quiesced_txg != 0))
544fa9e4066Sahrens 			txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
545fa9e4066Sahrens 
546fa9e4066Sahrens 		if (tx->tx_exiting)
547fa9e4066Sahrens 			txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
548fa9e4066Sahrens 
549fa9e4066Sahrens 		txg = tx->tx_open_txg;
550fa9e4066Sahrens 		dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
551fa9e4066Sahrens 		    txg, tx->tx_quiesce_txg_waiting,
552fa9e4066Sahrens 		    tx->tx_sync_txg_waiting);
553fa9e4066Sahrens 		mutex_exit(&tx->tx_sync_lock);
554fa9e4066Sahrens 		txg_quiesce(dp, txg);
555fa9e4066Sahrens 		mutex_enter(&tx->tx_sync_lock);
556fa9e4066Sahrens 
557fa9e4066Sahrens 		/*
558fa9e4066Sahrens 		 * Hand this txg off to the sync thread.
559fa9e4066Sahrens 		 */
560fa9e4066Sahrens 		dprintf("quiesce done, handing off txg %llu\n", txg);
561fa9e4066Sahrens 		tx->tx_quiesced_txg = txg;
5620689f76cSAdam Leventhal 		DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
563fa9e4066Sahrens 		cv_broadcast(&tx->tx_sync_more_cv);
564fa9e4066Sahrens 		cv_broadcast(&tx->tx_quiesce_done_cv);
565fa9e4066Sahrens 	}
566fa9e4066Sahrens }
567fa9e4066Sahrens 
5681ab7f2deSmaybee /*
5690689f76cSAdam Leventhal  * Delay this thread by delay nanoseconds if we are still in the open
570f7170741SWill Andrews  * transaction group and there is already a waiting txg quiescing or quiesced.
571f7170741SWill Andrews  * Abort the delay if this txg stalls or enters the quiescing state.
5721ab7f2deSmaybee  */
5731ab7f2deSmaybee void
5740689f76cSAdam Leventhal txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
5751ab7f2deSmaybee {
5761ab7f2deSmaybee 	tx_state_t *tx = &dp->dp_tx;
5770689f76cSAdam Leventhal 	hrtime_t start = gethrtime();
5781ab7f2deSmaybee 
579f7170741SWill Andrews 	/* don't delay if this txg could transition to quiescing immediately */
5801ab7f2deSmaybee 	if (tx->tx_open_txg > txg ||
5811ab7f2deSmaybee 	    tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
5821ab7f2deSmaybee 		return;
5831ab7f2deSmaybee 
5841ab7f2deSmaybee 	mutex_enter(&tx->tx_sync_lock);
5851ab7f2deSmaybee 	if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
5861ab7f2deSmaybee 		mutex_exit(&tx->tx_sync_lock);
5871ab7f2deSmaybee 		return;
5881ab7f2deSmaybee 	}
5891ab7f2deSmaybee 
5900689f76cSAdam Leventhal 	while (gethrtime() - start < delay &&
5910689f76cSAdam Leventhal 	    tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
5920689f76cSAdam Leventhal 		(void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
5930689f76cSAdam Leventhal 		    &tx->tx_sync_lock, delay, resolution, 0);
5940689f76cSAdam Leventhal 	}
5951ab7f2deSmaybee 
5961ab7f2deSmaybee 	mutex_exit(&tx->tx_sync_lock);
5971ab7f2deSmaybee }
5981ab7f2deSmaybee 
599fa9e4066Sahrens void
600fa9e4066Sahrens txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
601fa9e4066Sahrens {
602fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
603fa9e4066Sahrens 
6043b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dp));
6053b2aab18SMatthew Ahrens 
606fa9e4066Sahrens 	mutex_enter(&tx->tx_sync_lock);
6071ab7f2deSmaybee 	ASSERT(tx->tx_threads == 2);
608fa9e4066Sahrens 	if (txg == 0)
609b24ab676SJeff Bonwick 		txg = tx->tx_open_txg + TXG_DEFER_SIZE;
610fa9e4066Sahrens 	if (tx->tx_sync_txg_waiting < txg)
611fa9e4066Sahrens 		tx->tx_sync_txg_waiting = txg;
612fa9e4066Sahrens 	dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
613fa9e4066Sahrens 	    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
614fa9e4066Sahrens 	while (tx->tx_synced_txg < txg) {
615fa9e4066Sahrens 		dprintf("broadcasting sync more "
616fa9e4066Sahrens 		    "tx_synced=%llu waiting=%llu dp=%p\n",
617fa9e4066Sahrens 		    tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
618fa9e4066Sahrens 		cv_broadcast(&tx->tx_sync_more_cv);
619fa9e4066Sahrens 		cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
620fa9e4066Sahrens 	}
621fa9e4066Sahrens 	mutex_exit(&tx->tx_sync_lock);
622fa9e4066Sahrens }
623fa9e4066Sahrens 
624fa9e4066Sahrens void
625fa9e4066Sahrens txg_wait_open(dsl_pool_t *dp, uint64_t txg)
626fa9e4066Sahrens {
627fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
628fa9e4066Sahrens 
6293b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dp));
6303b2aab18SMatthew Ahrens 
631fa9e4066Sahrens 	mutex_enter(&tx->tx_sync_lock);
6321ab7f2deSmaybee 	ASSERT(tx->tx_threads == 2);
633fa9e4066Sahrens 	if (txg == 0)
634fa9e4066Sahrens 		txg = tx->tx_open_txg + 1;
635fa9e4066Sahrens 	if (tx->tx_quiesce_txg_waiting < txg)
636fa9e4066Sahrens 		tx->tx_quiesce_txg_waiting = txg;
637fa9e4066Sahrens 	dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
638fa9e4066Sahrens 	    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
639fa9e4066Sahrens 	while (tx->tx_open_txg < txg) {
640fa9e4066Sahrens 		cv_broadcast(&tx->tx_quiesce_more_cv);
641fa9e4066Sahrens 		cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
642fa9e4066Sahrens 	}
643fa9e4066Sahrens 	mutex_exit(&tx->tx_sync_lock);
644fa9e4066Sahrens }
645fa9e4066Sahrens 
646088f3894Sahrens boolean_t
647fa9e4066Sahrens txg_stalled(dsl_pool_t *dp)
648fa9e4066Sahrens {
649fa9e4066Sahrens 	tx_state_t *tx = &dp->dp_tx;
650fa9e4066Sahrens 	return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
651fa9e4066Sahrens }
652fa9e4066Sahrens 
653088f3894Sahrens boolean_t
654088f3894Sahrens txg_sync_waiting(dsl_pool_t *dp)
655088f3894Sahrens {
656088f3894Sahrens 	tx_state_t *tx = &dp->dp_tx;
657088f3894Sahrens 
658088f3894Sahrens 	return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
659088f3894Sahrens 	    tx->tx_quiesced_txg != 0);
660088f3894Sahrens }
661088f3894Sahrens 
662fa9e4066Sahrens /*
663fa9e4066Sahrens  * Per-txg object lists.
664fa9e4066Sahrens  */
665fa9e4066Sahrens void
666fa9e4066Sahrens txg_list_create(txg_list_t *tl, size_t offset)
667fa9e4066Sahrens {
668fa9e4066Sahrens 	int t;
669fa9e4066Sahrens 
670fa9e4066Sahrens 	mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
671fa9e4066Sahrens 
672fa9e4066Sahrens 	tl->tl_offset = offset;
673fa9e4066Sahrens 
674fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++)
675fa9e4066Sahrens 		tl->tl_head[t] = NULL;
676fa9e4066Sahrens }
677fa9e4066Sahrens 
678fa9e4066Sahrens void
679fa9e4066Sahrens txg_list_destroy(txg_list_t *tl)
680fa9e4066Sahrens {
681fa9e4066Sahrens 	int t;
682fa9e4066Sahrens 
683fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++)
684fa9e4066Sahrens 		ASSERT(txg_list_empty(tl, t));
685fa9e4066Sahrens 
686fa9e4066Sahrens 	mutex_destroy(&tl->tl_lock);
687fa9e4066Sahrens }
688fa9e4066Sahrens 
689ce636f8bSMatthew Ahrens boolean_t
690fa9e4066Sahrens txg_list_empty(txg_list_t *tl, uint64_t txg)
691fa9e4066Sahrens {
692fa9e4066Sahrens 	return (tl->tl_head[txg & TXG_MASK] == NULL);
693fa9e4066Sahrens }
694fa9e4066Sahrens 
695fa9e4066Sahrens /*
6963b2aab18SMatthew Ahrens  * Add an entry to the list (unless it's already on the list).
6973b2aab18SMatthew Ahrens  * Returns B_TRUE if it was actually added.
698fa9e4066Sahrens  */
6993b2aab18SMatthew Ahrens boolean_t
700fa9e4066Sahrens txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
701fa9e4066Sahrens {
702fa9e4066Sahrens 	int t = txg & TXG_MASK;
703fa9e4066Sahrens 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
7043b2aab18SMatthew Ahrens 	boolean_t add;
705fa9e4066Sahrens 
706fa9e4066Sahrens 	mutex_enter(&tl->tl_lock);
7073b2aab18SMatthew Ahrens 	add = (tn->tn_member[t] == 0);
7083b2aab18SMatthew Ahrens 	if (add) {
709fa9e4066Sahrens 		tn->tn_member[t] = 1;
710fa9e4066Sahrens 		tn->tn_next[t] = tl->tl_head[t];
711fa9e4066Sahrens 		tl->tl_head[t] = tn;
712fa9e4066Sahrens 	}
713fa9e4066Sahrens 	mutex_exit(&tl->tl_lock);
714fa9e4066Sahrens 
7153b2aab18SMatthew Ahrens 	return (add);
716fa9e4066Sahrens }
717fa9e4066Sahrens 
718495807d7SMatthew Ahrens /*
7193b2aab18SMatthew Ahrens  * Add an entry to the end of the list, unless it's already on the list.
7203b2aab18SMatthew Ahrens  * (walks list to find end)
7213b2aab18SMatthew Ahrens  * Returns B_TRUE if it was actually added.
722495807d7SMatthew Ahrens  */
7233b2aab18SMatthew Ahrens boolean_t
724495807d7SMatthew Ahrens txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
725495807d7SMatthew Ahrens {
726495807d7SMatthew Ahrens 	int t = txg & TXG_MASK;
727495807d7SMatthew Ahrens 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
7283b2aab18SMatthew Ahrens 	boolean_t add;
729495807d7SMatthew Ahrens 
730495807d7SMatthew Ahrens 	mutex_enter(&tl->tl_lock);
7313b2aab18SMatthew Ahrens 	add = (tn->tn_member[t] == 0);
7323b2aab18SMatthew Ahrens 	if (add) {
733495807d7SMatthew Ahrens 		txg_node_t **tp;
734495807d7SMatthew Ahrens 
735495807d7SMatthew Ahrens 		for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
736495807d7SMatthew Ahrens 			continue;
737495807d7SMatthew Ahrens 
738495807d7SMatthew Ahrens 		tn->tn_member[t] = 1;
739495807d7SMatthew Ahrens 		tn->tn_next[t] = NULL;
740495807d7SMatthew Ahrens 		*tp = tn;
741495807d7SMatthew Ahrens 	}
742495807d7SMatthew Ahrens 	mutex_exit(&tl->tl_lock);
743495807d7SMatthew Ahrens 
7443b2aab18SMatthew Ahrens 	return (add);
745495807d7SMatthew Ahrens }
746495807d7SMatthew Ahrens 
747fa9e4066Sahrens /*
748fa9e4066Sahrens  * Remove the head of the list and return it.
749fa9e4066Sahrens  */
750fa9e4066Sahrens void *
751fa9e4066Sahrens txg_list_remove(txg_list_t *tl, uint64_t txg)
752fa9e4066Sahrens {
753fa9e4066Sahrens 	int t = txg & TXG_MASK;
754fa9e4066Sahrens 	txg_node_t *tn;
755fa9e4066Sahrens 	void *p = NULL;
756fa9e4066Sahrens 
757fa9e4066Sahrens 	mutex_enter(&tl->tl_lock);
758fa9e4066Sahrens 	if ((tn = tl->tl_head[t]) != NULL) {
759fa9e4066Sahrens 		p = (char *)tn - tl->tl_offset;
760fa9e4066Sahrens 		tl->tl_head[t] = tn->tn_next[t];
761fa9e4066Sahrens 		tn->tn_next[t] = NULL;
762fa9e4066Sahrens 		tn->tn_member[t] = 0;
763fa9e4066Sahrens 	}
764fa9e4066Sahrens 	mutex_exit(&tl->tl_lock);
765fa9e4066Sahrens 
766fa9e4066Sahrens 	return (p);
767fa9e4066Sahrens }
768fa9e4066Sahrens 
769fa9e4066Sahrens /*
770fa9e4066Sahrens  * Remove a specific item from the list and return it.
771fa9e4066Sahrens  */
772fa9e4066Sahrens void *
773fa9e4066Sahrens txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
774fa9e4066Sahrens {
775fa9e4066Sahrens 	int t = txg & TXG_MASK;
776fa9e4066Sahrens 	txg_node_t *tn, **tp;
777fa9e4066Sahrens 
778fa9e4066Sahrens 	mutex_enter(&tl->tl_lock);
779fa9e4066Sahrens 
780fa9e4066Sahrens 	for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
781fa9e4066Sahrens 		if ((char *)tn - tl->tl_offset == p) {
782fa9e4066Sahrens 			*tp = tn->tn_next[t];
783fa9e4066Sahrens 			tn->tn_next[t] = NULL;
784fa9e4066Sahrens 			tn->tn_member[t] = 0;
785fa9e4066Sahrens 			mutex_exit(&tl->tl_lock);
786fa9e4066Sahrens 			return (p);
787fa9e4066Sahrens 		}
788fa9e4066Sahrens 	}
789fa9e4066Sahrens 
790fa9e4066Sahrens 	mutex_exit(&tl->tl_lock);
791fa9e4066Sahrens 
792fa9e4066Sahrens 	return (NULL);
793fa9e4066Sahrens }
794fa9e4066Sahrens 
7953b2aab18SMatthew Ahrens boolean_t
796fa9e4066Sahrens txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
797fa9e4066Sahrens {
798fa9e4066Sahrens 	int t = txg & TXG_MASK;
799fa9e4066Sahrens 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
800fa9e4066Sahrens 
8013b2aab18SMatthew Ahrens 	return (tn->tn_member[t] != 0);
802fa9e4066Sahrens }
803fa9e4066Sahrens 
804fa9e4066Sahrens /*
805fa9e4066Sahrens  * Walk a txg list -- only safe if you know it's not changing.
806fa9e4066Sahrens  */
807fa9e4066Sahrens void *
808fa9e4066Sahrens txg_list_head(txg_list_t *tl, uint64_t txg)
809fa9e4066Sahrens {
810fa9e4066Sahrens 	int t = txg & TXG_MASK;
811fa9e4066Sahrens 	txg_node_t *tn = tl->tl_head[t];
812fa9e4066Sahrens 
813fa9e4066Sahrens 	return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
814fa9e4066Sahrens }
815fa9e4066Sahrens 
816fa9e4066Sahrens void *
817fa9e4066Sahrens txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
818fa9e4066Sahrens {
819fa9e4066Sahrens 	int t = txg & TXG_MASK;
820fa9e4066Sahrens 	txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
821fa9e4066Sahrens 
822fa9e4066Sahrens 	tn = tn->tn_next[t];
823fa9e4066Sahrens 
824fa9e4066Sahrens 	return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
825fa9e4066Sahrens }
826