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