]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/txg.c
Fix dnode_hold_impl() soft lockup
[mirror_zfs.git] / module / zfs / txg.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
29809a6c 23 * Portions Copyright 2011 Martin Matuska
4747a7d3 24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/zfs_context.h>
28#include <sys/txg_impl.h>
29#include <sys/dmu_impl.h>
0b1401ee 30#include <sys/spa_impl.h>
428870ff 31#include <sys/dmu_tx.h>
34dc7c2f 32#include <sys/dsl_pool.h>
428870ff 33#include <sys/dsl_scan.h>
4747a7d3 34#include <sys/zil.h>
34dc7c2f 35#include <sys/callb.h>
49ee64e5 36#include <sys/trace_txg.h>
34dc7c2f
BB
37
38/*
89103a26
AL
39 * ZFS Transaction Groups
40 * ----------------------
41 *
42 * ZFS transaction groups are, as the name implies, groups of transactions
43 * that act on persistent state. ZFS asserts consistency at the granularity of
44 * these transaction groups. Each successive transaction group (txg) is
45 * assigned a 64-bit consecutive identifier. There are three active
46 * transaction group states: open, quiescing, or syncing. At any given time,
47 * there may be an active txg associated with each state; each active txg may
48 * either be processing, or blocked waiting to enter the next state. There may
49 * be up to three active txgs, and there is always a txg in the open state
50 * (though it may be blocked waiting to enter the quiescing state). In broad
e8b96c60 51 * strokes, transactions -- operations that change in-memory structures -- are
89103a26
AL
52 * accepted into the txg in the open state, and are completed while the txg is
53 * in the open or quiescing states. The accumulated changes are written to
54 * disk in the syncing state.
55 *
56 * Open
57 *
58 * When a new txg becomes active, it first enters the open state. New
e8b96c60 59 * transactions -- updates to in-memory structures -- are assigned to the
89103a26
AL
60 * currently open txg. There is always a txg in the open state so that ZFS can
61 * accept new changes (though the txg may refuse new changes if it has hit
62 * some limit). ZFS advances the open txg to the next state for a variety of
63 * reasons such as it hitting a time or size threshold, or the execution of an
64 * administrative action that must be completed in the syncing state.
65 *
66 * Quiescing
67 *
68 * After a txg exits the open state, it enters the quiescing state. The
69 * quiescing state is intended to provide a buffer between accepting new
70 * transactions in the open state and writing them out to stable storage in
71 * the syncing state. While quiescing, transactions can continue their
72 * operation without delaying either of the other states. Typically, a txg is
73 * in the quiescing state very briefly since the operations are bounded by
74 * software latencies rather than, say, slower I/O latencies. After all
75 * transactions complete, the txg is ready to enter the next state.
76 *
77 * Syncing
78 *
79 * In the syncing state, the in-memory state built up during the open and (to
80 * a lesser degree) the quiescing states is written to stable storage. The
81 * process of writing out modified data can, in turn modify more data. For
82 * example when we write new blocks, we need to allocate space for them; those
83 * allocations modify metadata (space maps)... which themselves must be
84 * written to stable storage. During the sync state, ZFS iterates, writing out
85 * data until it converges and all in-memory changes have been written out.
86 * The first such pass is the largest as it encompasses all the modified user
87 * data (as opposed to filesystem metadata). Subsequent passes typically have
88 * far less data to write as they consist exclusively of filesystem metadata.
89 *
90 * To ensure convergence, after a certain number of passes ZFS begins
91 * overwriting locations on stable storage that had been allocated earlier in
92 * the syncing state (and subsequently freed). ZFS usually allocates new
93 * blocks to optimize for large, continuous, writes. For the syncing state to
94 * converge however it must complete a pass where no new blocks are allocated
95 * since each allocation requires a modification of persistent metadata.
96 * Further, to hasten convergence, after a prescribed number of passes, ZFS
97 * also defers frees, and stops compressing.
98 *
99 * In addition to writing out user data, we must also execute synctasks during
100 * the syncing context. A synctask is the mechanism by which some
101 * administrative activities work such as creating and destroying snapshots or
102 * datasets. Note that when a synctask is initiated it enters the open txg,
103 * and ZFS then pushes that txg as quickly as possible to completion of the
104 * syncing state in order to reduce the latency of the administrative
105 * activity. To complete the syncing state, ZFS writes out a new uberblock,
106 * the root of the tree of blocks that comprise all state stored on the ZFS
107 * pool. Finally, if there is a quiesced txg waiting, we signal that it can
108 * now transition to the syncing state.
34dc7c2f
BB
109 */
110
867959b5
BB
111static void txg_sync_thread(void *arg);
112static void txg_quiesce_thread(void *arg);
34dc7c2f 113
572e2857 114int zfs_txg_timeout = 5; /* max seconds worth of delta per txg */
34dc7c2f
BB
115
116/*
117 * Prepare the txg subsystem.
118 */
119void
120txg_init(dsl_pool_t *dp, uint64_t txg)
121{
122 tx_state_t *tx = &dp->dp_tx;
123 int c;
124 bzero(tx, sizeof (tx_state_t));
125
00b46022 126 tx->tx_cpu = vmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
34dc7c2f
BB
127
128 for (c = 0; c < max_ncpus; c++) {
129 int i;
130
131 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
448d7aaa 132 mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_NOLOCKDEP,
2696dfaf 133 NULL);
34dc7c2f
BB
134 for (i = 0; i < TXG_SIZE; i++) {
135 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
136 NULL);
428870ff
BB
137 list_create(&tx->tx_cpu[c].tc_callbacks[i],
138 sizeof (dmu_tx_callback_t),
139 offsetof(dmu_tx_callback_t, dcb_node));
34dc7c2f
BB
140 }
141 }
142
34dc7c2f
BB
143 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
144
fb5f0bc8
BB
145 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
146 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
147 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
148 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
149 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
150
34dc7c2f
BB
151 tx->tx_open_txg = txg;
152}
153
154/*
155 * Close down the txg subsystem.
156 */
157void
158txg_fini(dsl_pool_t *dp)
159{
160 tx_state_t *tx = &dp->dp_tx;
161 int c;
162
1ce23dca 163 ASSERT0(tx->tx_threads);
34dc7c2f 164
34dc7c2f
BB
165 mutex_destroy(&tx->tx_sync_lock);
166
fb5f0bc8
BB
167 cv_destroy(&tx->tx_sync_more_cv);
168 cv_destroy(&tx->tx_sync_done_cv);
169 cv_destroy(&tx->tx_quiesce_more_cv);
170 cv_destroy(&tx->tx_quiesce_done_cv);
171 cv_destroy(&tx->tx_exit_cv);
172
34dc7c2f
BB
173 for (c = 0; c < max_ncpus; c++) {
174 int i;
175
2696dfaf 176 mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
34dc7c2f 177 mutex_destroy(&tx->tx_cpu[c].tc_lock);
428870ff 178 for (i = 0; i < TXG_SIZE; i++) {
34dc7c2f 179 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
428870ff
BB
180 list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
181 }
34dc7c2f
BB
182 }
183
428870ff
BB
184 if (tx->tx_commit_cb_taskq != NULL)
185 taskq_destroy(tx->tx_commit_cb_taskq);
186
00b46022 187 vmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
34dc7c2f
BB
188
189 bzero(tx, sizeof (tx_state_t));
190}
191
192/*
193 * Start syncing transaction groups.
194 */
195void
196txg_sync_start(dsl_pool_t *dp)
197{
198 tx_state_t *tx = &dp->dp_tx;
199
200 mutex_enter(&tx->tx_sync_lock);
201
202 dprintf("pool %p\n", dp);
203
1ce23dca 204 ASSERT0(tx->tx_threads);
34dc7c2f
BB
205
206 tx->tx_threads = 2;
207
208 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
1229323d 209 dp, 0, &p0, TS_RUN, defclsyspri);
34dc7c2f 210
b128c09f
BB
211 /*
212 * The sync thread can need a larger-than-default stack size on
213 * 32-bit x86. This is due in part to nested pools and
214 * scrub_visitbp() recursion.
215 */
89666a8e 216 tx->tx_sync_thread = thread_create(NULL, 0, txg_sync_thread,
1229323d 217 dp, 0, &p0, TS_RUN, defclsyspri);
34dc7c2f
BB
218
219 mutex_exit(&tx->tx_sync_lock);
220}
221
222static void
223txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
224{
225 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
226 mutex_enter(&tx->tx_sync_lock);
227}
228
229static void
230txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
231{
232 ASSERT(*tpp != NULL);
233 *tpp = NULL;
234 tx->tx_threads--;
235 cv_broadcast(&tx->tx_exit_cv);
236 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
237 thread_exit();
238}
239
240static void
63fd3c6c 241txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
34dc7c2f
BB
242{
243 CALLB_CPR_SAFE_BEGIN(cpr);
244
245 if (time)
b64ccd6c 246 (void) cv_timedwait_sig(cv, &tx->tx_sync_lock,
428870ff 247 ddi_get_lbolt() + time);
34dc7c2f 248 else
b64ccd6c 249 cv_wait_sig(cv, &tx->tx_sync_lock);
34dc7c2f
BB
250
251 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
252}
253
254/*
255 * Stop syncing transaction groups.
256 */
257void
258txg_sync_stop(dsl_pool_t *dp)
259{
260 tx_state_t *tx = &dp->dp_tx;
261
262 dprintf("pool %p\n", dp);
263 /*
264 * Finish off any work in progress.
265 */
1ce23dca 266 ASSERT3U(tx->tx_threads, ==, 2);
428870ff
BB
267
268 /*
269 * We need to ensure that we've vacated the deferred space_maps.
270 */
271 txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
34dc7c2f
BB
272
273 /*
274 * Wake all sync threads and wait for them to die.
275 */
276 mutex_enter(&tx->tx_sync_lock);
277
1ce23dca 278 ASSERT3U(tx->tx_threads, ==, 2);
34dc7c2f
BB
279
280 tx->tx_exiting = 1;
281
282 cv_broadcast(&tx->tx_quiesce_more_cv);
283 cv_broadcast(&tx->tx_quiesce_done_cv);
284 cv_broadcast(&tx->tx_sync_more_cv);
285
286 while (tx->tx_threads != 0)
287 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
288
289 tx->tx_exiting = 0;
290
291 mutex_exit(&tx->tx_sync_lock);
292}
293
294uint64_t
295txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
296{
297 tx_state_t *tx = &dp->dp_tx;
15a9e033 298 tx_cpu_t *tc;
34dc7c2f
BB
299 uint64_t txg;
300
15a9e033
PS
301 /*
302 * It appears the processor id is simply used as a "random"
303 * number to index into the array, and there isn't any other
304 * significance to the chosen tx_cpu. Because.. Why not use
305 * the current cpu to index into the array?
306 */
307 kpreempt_disable();
308 tc = &tx->tx_cpu[CPU_SEQID];
309 kpreempt_enable();
310
2696dfaf 311 mutex_enter(&tc->tc_open_lock);
34dc7c2f 312 txg = tx->tx_open_txg;
2696dfaf
GW
313
314 mutex_enter(&tc->tc_lock);
34dc7c2f 315 tc->tc_count[txg & TXG_MASK]++;
2696dfaf 316 mutex_exit(&tc->tc_lock);
34dc7c2f
BB
317
318 th->th_cpu = tc;
319 th->th_txg = txg;
320
321 return (txg);
322}
323
324void
325txg_rele_to_quiesce(txg_handle_t *th)
326{
327 tx_cpu_t *tc = th->th_cpu;
328
2696dfaf
GW
329 ASSERT(!MUTEX_HELD(&tc->tc_lock));
330 mutex_exit(&tc->tc_open_lock);
34dc7c2f
BB
331}
332
428870ff
BB
333void
334txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
335{
336 tx_cpu_t *tc = th->th_cpu;
337 int g = th->th_txg & TXG_MASK;
338
339 mutex_enter(&tc->tc_lock);
340 list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
341 mutex_exit(&tc->tc_lock);
342}
343
34dc7c2f
BB
344void
345txg_rele_to_sync(txg_handle_t *th)
346{
347 tx_cpu_t *tc = th->th_cpu;
348 int g = th->th_txg & TXG_MASK;
349
350 mutex_enter(&tc->tc_lock);
351 ASSERT(tc->tc_count[g] != 0);
352 if (--tc->tc_count[g] == 0)
353 cv_broadcast(&tc->tc_cv[g]);
354 mutex_exit(&tc->tc_lock);
355
356 th->th_cpu = NULL; /* defensive */
357}
358
e49f1e20
WA
359/*
360 * Blocks until all transactions in the group are committed.
361 *
362 * On return, the transaction group has reached a stable state in which it can
363 * then be passed off to the syncing context.
364 */
34dc7c2f
BB
365static void
366txg_quiesce(dsl_pool_t *dp, uint64_t txg)
367{
368 tx_state_t *tx = &dp->dp_tx;
f26b4b3c 369 uint64_t tx_open_time;
34dc7c2f
BB
370 int g = txg & TXG_MASK;
371 int c;
372
373 /*
2696dfaf 374 * Grab all tc_open_locks so nobody else can get into this txg.
34dc7c2f
BB
375 */
376 for (c = 0; c < max_ncpus; c++)
2696dfaf 377 mutex_enter(&tx->tx_cpu[c].tc_open_lock);
34dc7c2f
BB
378
379 ASSERT(txg == tx->tx_open_txg);
380 tx->tx_open_txg++;
f26b4b3c 381 tx->tx_open_time = tx_open_time = gethrtime();
0b1401ee 382
63fd3c6c
AL
383 DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
384 DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
385
57f5a200
BB
386 /*
387 * Now that we've incremented tx_open_txg, we can let threads
388 * enter the next transaction group.
389 */
390 for (c = 0; c < max_ncpus; c++)
2696dfaf 391 mutex_exit(&tx->tx_cpu[c].tc_open_lock);
57f5a200 392
f26b4b3c
RY
393 spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_OPEN, tx_open_time);
394 spa_txg_history_add(dp->dp_spa, txg + 1, tx_open_time);
395
34dc7c2f
BB
396 /*
397 * Quiesce the transaction group by waiting for everyone to txg_exit().
398 */
399 for (c = 0; c < max_ncpus; c++) {
400 tx_cpu_t *tc = &tx->tx_cpu[c];
401 mutex_enter(&tc->tc_lock);
402 while (tc->tc_count[g] != 0)
403 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
404 mutex_exit(&tc->tc_lock);
405 }
0b1401ee
BB
406
407 spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_QUIESCED, gethrtime());
34dc7c2f
BB
408}
409
428870ff
BB
410static void
411txg_do_callbacks(list_t *cb_list)
412{
413 dmu_tx_do_callbacks(cb_list, 0);
414
415 list_destroy(cb_list);
416
417 kmem_free(cb_list, sizeof (list_t));
418}
419
420/*
421 * Dispatch the commit callbacks registered on this txg to worker threads.
e49f1e20
WA
422 *
423 * If no callbacks are registered for a given TXG, nothing happens.
424 * This function creates a taskq for the associated pool, if needed.
428870ff
BB
425 */
426static void
427txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
428{
429 int c;
430 tx_state_t *tx = &dp->dp_tx;
431 list_t *cb_list;
432
433 for (c = 0; c < max_ncpus; c++) {
434 tx_cpu_t *tc = &tx->tx_cpu[c];
e49f1e20
WA
435 /*
436 * No need to lock tx_cpu_t at this point, since this can
437 * only be called once a txg has been synced.
438 */
428870ff
BB
439
440 int g = txg & TXG_MASK;
441
442 if (list_is_empty(&tc->tc_callbacks[g]))
443 continue;
444
445 if (tx->tx_commit_cb_taskq == NULL) {
446 /*
447 * Commit callback taskq hasn't been created yet.
448 */
449 tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
1229323d 450 max_ncpus, defclsyspri, max_ncpus, max_ncpus * 2,
aa9af22c 451 TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
428870ff
BB
452 }
453
79c76d5b 454 cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
428870ff
BB
455 list_create(cb_list, sizeof (dmu_tx_callback_t),
456 offsetof(dmu_tx_callback_t, dcb_node));
457
090ff092 458 list_move_tail(cb_list, &tc->tc_callbacks[g]);
428870ff
BB
459
460 (void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
461 txg_do_callbacks, cb_list, TQ_SLEEP);
462 }
463}
464
54a179e7
RC
465/*
466 * Wait for pending commit callbacks of already-synced transactions to finish
467 * processing.
468 * Calling this function from within a commit callback will deadlock.
469 */
470void
471txg_wait_callbacks(dsl_pool_t *dp)
472{
473 tx_state_t *tx = &dp->dp_tx;
474
475 if (tx->tx_commit_cb_taskq != NULL)
c5528b9b 476 taskq_wait_outstanding(tx->tx_commit_cb_taskq, 0);
54a179e7
RC
477}
478
e48afbc4
SD
479static boolean_t
480txg_is_syncing(dsl_pool_t *dp)
481{
482 tx_state_t *tx = &dp->dp_tx;
483 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
484 return (tx->tx_syncing_txg != 0);
485}
486
487static boolean_t
488txg_is_quiescing(dsl_pool_t *dp)
489{
490 tx_state_t *tx = &dp->dp_tx;
491 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
492 return (tx->tx_quiescing_txg != 0);
493}
494
495static boolean_t
496txg_has_quiesced_to_sync(dsl_pool_t *dp)
497{
498 tx_state_t *tx = &dp->dp_tx;
499 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
500 return (tx->tx_quiesced_txg != 0);
501}
502
34dc7c2f 503static void
c25b8f99 504txg_sync_thread(void *arg)
34dc7c2f 505{
867959b5 506 dsl_pool_t *dp = arg;
428870ff 507 spa_t *spa = dp->dp_spa;
34dc7c2f
BB
508 tx_state_t *tx = &dp->dp_tx;
509 callb_cpr_t cpr;
0b75bdb3 510 clock_t start, delta;
34dc7c2f 511
92119cc2 512 (void) spl_fstrans_mark();
34dc7c2f
BB
513 txg_thread_enter(tx, &cpr);
514
515 start = delta = 0;
34dc7c2f 516 for (;;) {
baf67d15
BB
517 clock_t timeout = zfs_txg_timeout * hz;
518 clock_t timer;
b128c09f 519 uint64_t txg;
dfbe2675
MA
520 uint64_t dirty_min_bytes =
521 zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100;
87d98efe 522
34dc7c2f 523 /*
428870ff 524 * We sync when we're scanning, there's someone waiting
b128c09f
BB
525 * on us, or the quiesce thread has handed off a txg to
526 * us, or we have reached our timeout.
34dc7c2f
BB
527 */
528 timer = (delta >= timeout ? 0 : timeout - delta);
428870ff 529 while (!dsl_scan_active(dp->dp_scan) &&
b128c09f 530 !tx->tx_exiting && timer > 0 &&
34dc7c2f 531 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
e48afbc4 532 !txg_has_quiesced_to_sync(dp) &&
dfbe2675 533 dp->dp_dirty_total < dirty_min_bytes) {
34dc7c2f
BB
534 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
535 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
536 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
428870ff 537 delta = ddi_get_lbolt() - start;
34dc7c2f
BB
538 timer = (delta > timeout ? 0 : timeout - delta);
539 }
540
541 /*
542 * Wait until the quiesce thread hands off a txg to us,
543 * prompting it to do so if necessary.
544 */
e48afbc4 545 while (!tx->tx_exiting && !txg_has_quiesced_to_sync(dp)) {
34dc7c2f
BB
546 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
547 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
548 cv_broadcast(&tx->tx_quiesce_more_cv);
549 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
550 }
551
baf67d15 552 if (tx->tx_exiting)
34dc7c2f
BB
553 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
554
34dc7c2f
BB
555 /*
556 * Consume the quiesced txg which has been handed off to
557 * us. This may cause the quiescing thread to now be
558 * able to quiesce another txg, so we must signal it.
559 */
e48afbc4 560 ASSERT(tx->tx_quiesced_txg != 0);
34dc7c2f
BB
561 txg = tx->tx_quiesced_txg;
562 tx->tx_quiesced_txg = 0;
563 tx->tx_syncing_txg = txg;
63fd3c6c 564 DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
34dc7c2f 565 cv_broadcast(&tx->tx_quiesce_more_cv);
34dc7c2f
BB
566
567 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
568 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
569 mutex_exit(&tx->tx_sync_lock);
b128c09f 570
a783dd96 571 txg_stat_t *ts = spa_txg_history_init_io(spa, txg, dp);
428870ff
BB
572 start = ddi_get_lbolt();
573 spa_sync(spa, txg);
574 delta = ddi_get_lbolt() - start;
a783dd96 575 spa_txg_history_fini_io(spa, ts);
34dc7c2f 576
34dc7c2f 577 mutex_enter(&tx->tx_sync_lock);
34dc7c2f
BB
578 tx->tx_synced_txg = txg;
579 tx->tx_syncing_txg = 0;
63fd3c6c 580 DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
34dc7c2f 581 cv_broadcast(&tx->tx_sync_done_cv);
428870ff
BB
582
583 /*
584 * Dispatch commit callbacks to worker threads.
585 */
586 txg_dispatch_callbacks(dp, txg);
34dc7c2f
BB
587 }
588}
589
590static void
c25b8f99 591txg_quiesce_thread(void *arg)
34dc7c2f 592{
867959b5 593 dsl_pool_t *dp = arg;
34dc7c2f
BB
594 tx_state_t *tx = &dp->dp_tx;
595 callb_cpr_t cpr;
596
597 txg_thread_enter(tx, &cpr);
598
599 for (;;) {
600 uint64_t txg;
601
602 /*
603 * We quiesce when there's someone waiting on us.
604 * However, we can only have one txg in "quiescing" or
605 * "quiesced, waiting to sync" state. So we wait until
606 * the "quiesced, waiting to sync" txg has been consumed
607 * by the sync thread.
608 */
609 while (!tx->tx_exiting &&
610 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
e48afbc4 611 txg_has_quiesced_to_sync(dp)))
34dc7c2f
BB
612 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
613
614 if (tx->tx_exiting)
615 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
616
617 txg = tx->tx_open_txg;
618 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
619 txg, tx->tx_quiesce_txg_waiting,
620 tx->tx_sync_txg_waiting);
e48afbc4
SD
621 tx->tx_quiescing_txg = txg;
622
34dc7c2f
BB
623 mutex_exit(&tx->tx_sync_lock);
624 txg_quiesce(dp, txg);
625 mutex_enter(&tx->tx_sync_lock);
626
627 /*
628 * Hand this txg off to the sync thread.
629 */
630 dprintf("quiesce done, handing off txg %llu\n", txg);
e48afbc4 631 tx->tx_quiescing_txg = 0;
34dc7c2f 632 tx->tx_quiesced_txg = txg;
63fd3c6c 633 DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
34dc7c2f
BB
634 cv_broadcast(&tx->tx_sync_more_cv);
635 cv_broadcast(&tx->tx_quiesce_done_cv);
636 }
637}
638
639/*
63fd3c6c
AL
640 * Delay this thread by delay nanoseconds if we are still in the open
641 * transaction group and there is already a waiting txg quiesing or quiesced.
642 * Abort the delay if this txg stalls or enters the quiesing state.
34dc7c2f
BB
643 */
644void
63fd3c6c 645txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
34dc7c2f
BB
646{
647 tx_state_t *tx = &dp->dp_tx;
63fd3c6c 648 hrtime_t start = gethrtime();
34dc7c2f 649
d3cc8b15 650 /* don't delay if this txg could transition to quiescing immediately */
34dc7c2f
BB
651 if (tx->tx_open_txg > txg ||
652 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
653 return;
654
655 mutex_enter(&tx->tx_sync_lock);
656 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
657 mutex_exit(&tx->tx_sync_lock);
658 return;
659 }
660
63fd3c6c
AL
661 while (gethrtime() - start < delay &&
662 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
663 (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
664 &tx->tx_sync_lock, delay, resolution, 0);
665 }
34dc7c2f 666
570827e1
BB
667 DMU_TX_STAT_BUMP(dmu_tx_delay);
668
34dc7c2f
BB
669 mutex_exit(&tx->tx_sync_lock);
670}
671
672void
673txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
674{
675 tx_state_t *tx = &dp->dp_tx;
676
13fe0198
MA
677 ASSERT(!dsl_pool_config_held(dp));
678
34dc7c2f 679 mutex_enter(&tx->tx_sync_lock);
1ce23dca 680 ASSERT3U(tx->tx_threads, ==, 2);
34dc7c2f 681 if (txg == 0)
428870ff 682 txg = tx->tx_open_txg + TXG_DEFER_SIZE;
34dc7c2f
BB
683 if (tx->tx_sync_txg_waiting < txg)
684 tx->tx_sync_txg_waiting = txg;
685 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
686 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
687 while (tx->tx_synced_txg < txg) {
688 dprintf("broadcasting sync more "
689 "tx_synced=%llu waiting=%llu dp=%p\n",
690 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
691 cv_broadcast(&tx->tx_sync_more_cv);
692 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
693 }
694 mutex_exit(&tx->tx_sync_lock);
695}
696
697void
698txg_wait_open(dsl_pool_t *dp, uint64_t txg)
699{
700 tx_state_t *tx = &dp->dp_tx;
701
13fe0198
MA
702 ASSERT(!dsl_pool_config_held(dp));
703
34dc7c2f 704 mutex_enter(&tx->tx_sync_lock);
1ce23dca 705 ASSERT3U(tx->tx_threads, ==, 2);
34dc7c2f
BB
706 if (txg == 0)
707 txg = tx->tx_open_txg + 1;
708 if (tx->tx_quiesce_txg_waiting < txg)
709 tx->tx_quiesce_txg_waiting = txg;
710 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
711 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
712 while (tx->tx_open_txg < txg) {
713 cv_broadcast(&tx->tx_quiesce_more_cv);
714 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
715 }
716 mutex_exit(&tx->tx_sync_lock);
717}
718
e8b96c60
MA
719/*
720 * If there isn't a txg syncing or in the pipeline, push another txg through
721 * the pipeline by queiscing the open txg.
722 */
723void
724txg_kick(dsl_pool_t *dp)
725{
726 tx_state_t *tx = &dp->dp_tx;
727
728 ASSERT(!dsl_pool_config_held(dp));
729
730 mutex_enter(&tx->tx_sync_lock);
e48afbc4
SD
731 if (!txg_is_syncing(dp) &&
732 !txg_is_quiescing(dp) &&
e8b96c60
MA
733 tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
734 tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
735 tx->tx_quiesced_txg <= tx->tx_synced_txg) {
736 tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
737 cv_broadcast(&tx->tx_quiesce_more_cv);
738 }
739 mutex_exit(&tx->tx_sync_lock);
740}
741
b128c09f 742boolean_t
34dc7c2f
BB
743txg_stalled(dsl_pool_t *dp)
744{
745 tx_state_t *tx = &dp->dp_tx;
746 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
747}
748
b128c09f
BB
749boolean_t
750txg_sync_waiting(dsl_pool_t *dp)
751{
752 tx_state_t *tx = &dp->dp_tx;
753
754 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
755 tx->tx_quiesced_txg != 0);
756}
757
4747a7d3
MA
758/*
759 * Verify that this txg is active (open, quiescing, syncing). Non-active
760 * txg's should not be manipulated.
761 */
8c4fb36a 762#ifdef ZFS_DEBUG
4747a7d3
MA
763void
764txg_verify(spa_t *spa, uint64_t txg)
765{
766 ASSERTV(dsl_pool_t *dp = spa_get_dsl(spa));
767 if (txg <= TXG_INITIAL || txg == ZILTEST_TXG)
768 return;
769 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
770 ASSERT3U(txg, >=, dp->dp_tx.tx_synced_txg);
771 ASSERT3U(txg, >=, dp->dp_tx.tx_open_txg - TXG_CONCURRENT_STATES);
772}
8c4fb36a 773#endif
4747a7d3 774
34dc7c2f
BB
775/*
776 * Per-txg object lists.
777 */
778void
4747a7d3 779txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset)
34dc7c2f
BB
780{
781 int t;
782
783 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
784
785 tl->tl_offset = offset;
4747a7d3 786 tl->tl_spa = spa;
34dc7c2f
BB
787
788 for (t = 0; t < TXG_SIZE; t++)
789 tl->tl_head[t] = NULL;
790}
791
8c4fb36a
TC
792static boolean_t
793txg_list_empty_impl(txg_list_t *tl, uint64_t txg)
794{
795 ASSERT(MUTEX_HELD(&tl->tl_lock));
796 TXG_VERIFY(tl->tl_spa, txg);
797 return (tl->tl_head[txg & TXG_MASK] == NULL);
798}
799
800boolean_t
801txg_list_empty(txg_list_t *tl, uint64_t txg)
802{
803 mutex_enter(&tl->tl_lock);
804 boolean_t ret = txg_list_empty_impl(tl, txg);
805 mutex_exit(&tl->tl_lock);
806
807 return (ret);
808}
809
34dc7c2f
BB
810void
811txg_list_destroy(txg_list_t *tl)
812{
813 int t;
814
8c4fb36a 815 mutex_enter(&tl->tl_lock);
34dc7c2f 816 for (t = 0; t < TXG_SIZE; t++)
8c4fb36a
TC
817 ASSERT(txg_list_empty_impl(tl, t));
818 mutex_exit(&tl->tl_lock);
34dc7c2f
BB
819
820 mutex_destroy(&tl->tl_lock);
821}
822
acbad6ff
AR
823/*
824 * Returns true if all txg lists are empty.
825 *
826 * Warning: this is inherently racy (an item could be added immediately
8c4fb36a 827 * after this function returns).
acbad6ff
AR
828 */
829boolean_t
830txg_all_lists_empty(txg_list_t *tl)
831{
8c4fb36a 832 mutex_enter(&tl->tl_lock);
1c27024e 833 for (int i = 0; i < TXG_SIZE; i++) {
8c4fb36a
TC
834 if (!txg_list_empty_impl(tl, i)) {
835 mutex_exit(&tl->tl_lock);
acbad6ff
AR
836 return (B_FALSE);
837 }
838 }
8c4fb36a 839 mutex_exit(&tl->tl_lock);
acbad6ff
AR
840 return (B_TRUE);
841}
842
34dc7c2f 843/*
13fe0198
MA
844 * Add an entry to the list (unless it's already on the list).
845 * Returns B_TRUE if it was actually added.
34dc7c2f 846 */
13fe0198 847boolean_t
34dc7c2f
BB
848txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
849{
850 int t = txg & TXG_MASK;
851 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
13fe0198 852 boolean_t add;
34dc7c2f 853
8c4fb36a 854 TXG_VERIFY(tl->tl_spa, txg);
34dc7c2f 855 mutex_enter(&tl->tl_lock);
13fe0198
MA
856 add = (tn->tn_member[t] == 0);
857 if (add) {
34dc7c2f
BB
858 tn->tn_member[t] = 1;
859 tn->tn_next[t] = tl->tl_head[t];
860 tl->tl_head[t] = tn;
861 }
862 mutex_exit(&tl->tl_lock);
863
13fe0198 864 return (add);
34dc7c2f
BB
865}
866
428870ff 867/*
13fe0198
MA
868 * Add an entry to the end of the list, unless it's already on the list.
869 * (walks list to find end)
870 * Returns B_TRUE if it was actually added.
428870ff 871 */
13fe0198 872boolean_t
428870ff
BB
873txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
874{
875 int t = txg & TXG_MASK;
876 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
13fe0198 877 boolean_t add;
428870ff 878
8c4fb36a 879 TXG_VERIFY(tl->tl_spa, txg);
428870ff 880 mutex_enter(&tl->tl_lock);
13fe0198
MA
881 add = (tn->tn_member[t] == 0);
882 if (add) {
428870ff
BB
883 txg_node_t **tp;
884
885 for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
886 continue;
887
888 tn->tn_member[t] = 1;
889 tn->tn_next[t] = NULL;
890 *tp = tn;
891 }
892 mutex_exit(&tl->tl_lock);
893
13fe0198 894 return (add);
428870ff
BB
895}
896
34dc7c2f
BB
897/*
898 * Remove the head of the list and return it.
899 */
900void *
901txg_list_remove(txg_list_t *tl, uint64_t txg)
902{
903 int t = txg & TXG_MASK;
904 txg_node_t *tn;
905 void *p = NULL;
906
8c4fb36a 907 TXG_VERIFY(tl->tl_spa, txg);
34dc7c2f
BB
908 mutex_enter(&tl->tl_lock);
909 if ((tn = tl->tl_head[t]) != NULL) {
a1d477c2
MA
910 ASSERT(tn->tn_member[t]);
911 ASSERT(tn->tn_next[t] == NULL || tn->tn_next[t]->tn_member[t]);
34dc7c2f
BB
912 p = (char *)tn - tl->tl_offset;
913 tl->tl_head[t] = tn->tn_next[t];
914 tn->tn_next[t] = NULL;
915 tn->tn_member[t] = 0;
916 }
917 mutex_exit(&tl->tl_lock);
918
919 return (p);
920}
921
922/*
923 * Remove a specific item from the list and return it.
924 */
925void *
926txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
927{
928 int t = txg & TXG_MASK;
929 txg_node_t *tn, **tp;
930
8c4fb36a 931 TXG_VERIFY(tl->tl_spa, txg);
34dc7c2f
BB
932 mutex_enter(&tl->tl_lock);
933
934 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
935 if ((char *)tn - tl->tl_offset == p) {
936 *tp = tn->tn_next[t];
937 tn->tn_next[t] = NULL;
938 tn->tn_member[t] = 0;
939 mutex_exit(&tl->tl_lock);
940 return (p);
941 }
942 }
943
944 mutex_exit(&tl->tl_lock);
945
946 return (NULL);
947}
948
13fe0198 949boolean_t
34dc7c2f
BB
950txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
951{
952 int t = txg & TXG_MASK;
953 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
954
8c4fb36a 955 TXG_VERIFY(tl->tl_spa, txg);
13fe0198 956 return (tn->tn_member[t] != 0);
34dc7c2f
BB
957}
958
959/*
8c4fb36a 960 * Walk a txg list
34dc7c2f
BB
961 */
962void *
963txg_list_head(txg_list_t *tl, uint64_t txg)
964{
965 int t = txg & TXG_MASK;
8c4fb36a
TC
966 txg_node_t *tn;
967
968 mutex_enter(&tl->tl_lock);
969 tn = tl->tl_head[t];
970 mutex_exit(&tl->tl_lock);
34dc7c2f 971
8c4fb36a 972 TXG_VERIFY(tl->tl_spa, txg);
34dc7c2f
BB
973 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
974}
975
976void *
977txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
978{
979 int t = txg & TXG_MASK;
980 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
981
8c4fb36a
TC
982 TXG_VERIFY(tl->tl_spa, txg);
983
984 mutex_enter(&tl->tl_lock);
34dc7c2f 985 tn = tn->tn_next[t];
8c4fb36a 986 mutex_exit(&tl->tl_lock);
34dc7c2f
BB
987
988 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
989}
c28b2279 990
93ce2b4c 991#if defined(_KERNEL)
c28b2279
BB
992EXPORT_SYMBOL(txg_init);
993EXPORT_SYMBOL(txg_fini);
994EXPORT_SYMBOL(txg_sync_start);
995EXPORT_SYMBOL(txg_sync_stop);
996EXPORT_SYMBOL(txg_hold_open);
997EXPORT_SYMBOL(txg_rele_to_quiesce);
998EXPORT_SYMBOL(txg_rele_to_sync);
999EXPORT_SYMBOL(txg_register_callbacks);
1000EXPORT_SYMBOL(txg_delay);
1001EXPORT_SYMBOL(txg_wait_synced);
1002EXPORT_SYMBOL(txg_wait_open);
54a179e7 1003EXPORT_SYMBOL(txg_wait_callbacks);
c28b2279
BB
1004EXPORT_SYMBOL(txg_stalled);
1005EXPORT_SYMBOL(txg_sync_waiting);
87d98efe
BB
1006
1007module_param(zfs_txg_timeout, int, 0644);
1008MODULE_PARM_DESC(zfs_txg_timeout, "Max seconds worth of delta per txg");
c28b2279 1009#endif