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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
27 #include <sys/txg_impl.h>
28 #include <sys/dmu_impl.h>
29 #include <sys/dsl_pool.h>
30 #include <sys/callb.h>
33 * Pool-wide transaction groups.
36 static void txg_sync_thread(void *arg);
37 static void txg_quiesce_thread(void *arg);
39 int zfs_txg_timeout = 30; /* max seconds worth of delta per txg */
40 extern int zfs_txg_synctime;
42 SYSCTL_DECL(_vfs_zfs);
43 SYSCTL_NODE(_vfs_zfs, OID_AUTO, txg, CTLFLAG_RW, 0, "ZFS TXG");
44 TUNABLE_INT("vfs.zfs.txg.timeout", &zfs_txg_timeout);
45 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, timeout, CTLFLAG_RDTUN, &zfs_txg_timeout, 0,
46 "Maximum seconds worth of delta per txg");
47 TUNABLE_INT("vfs.zfs.txg.synctime", &zfs_txg_synctime);
48 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, synctime, CTLFLAG_RDTUN, &zfs_txg_synctime,
49 0, "Target seconds to sync a txg");
52 * Prepare the txg subsystem.
55 txg_init(dsl_pool_t *dp, uint64_t txg)
57 tx_state_t *tx = &dp->dp_tx;
59 bzero(tx, sizeof (tx_state_t));
61 tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
63 for (c = 0; c < max_ncpus; c++) {
66 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
67 for (i = 0; i < TXG_SIZE; i++) {
68 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
73 rw_init(&tx->tx_suspend, NULL, RW_DEFAULT, NULL);
74 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
75 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
76 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
77 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
78 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
79 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
81 tx->tx_open_txg = txg;
85 * Close down the txg subsystem.
88 txg_fini(dsl_pool_t *dp)
90 tx_state_t *tx = &dp->dp_tx;
93 ASSERT(tx->tx_threads == 0);
95 cv_destroy(&tx->tx_exit_cv);
96 cv_destroy(&tx->tx_quiesce_done_cv);
97 cv_destroy(&tx->tx_quiesce_more_cv);
98 cv_destroy(&tx->tx_sync_done_cv);
99 cv_destroy(&tx->tx_sync_more_cv);
100 rw_destroy(&tx->tx_suspend);
101 mutex_destroy(&tx->tx_sync_lock);
103 for (c = 0; c < max_ncpus; c++) {
106 mutex_destroy(&tx->tx_cpu[c].tc_lock);
107 for (i = 0; i < TXG_SIZE; i++)
108 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
111 kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
113 bzero(tx, sizeof (tx_state_t));
117 * Start syncing transaction groups.
120 txg_sync_start(dsl_pool_t *dp)
122 tx_state_t *tx = &dp->dp_tx;
124 mutex_enter(&tx->tx_sync_lock);
126 dprintf("pool %p\n", dp);
128 ASSERT(tx->tx_threads == 0);
132 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
133 dp, 0, &p0, TS_RUN, minclsyspri);
136 * The sync thread can need a larger-than-default stack size on
137 * 32-bit x86. This is due in part to nested pools and
138 * scrub_visitbp() recursion.
140 tx->tx_sync_thread = thread_create(NULL, 12<<10, txg_sync_thread,
141 dp, 0, &p0, TS_RUN, minclsyspri);
143 mutex_exit(&tx->tx_sync_lock);
147 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
149 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
150 mutex_enter(&tx->tx_sync_lock);
154 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
156 ASSERT(*tpp != NULL);
159 cv_broadcast(&tx->tx_exit_cv);
160 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
165 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, uint64_t time)
167 CALLB_CPR_SAFE_BEGIN(cpr);
170 (void) cv_timedwait(cv, &tx->tx_sync_lock, time);
172 cv_wait(cv, &tx->tx_sync_lock);
174 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
178 * Stop syncing transaction groups.
181 txg_sync_stop(dsl_pool_t *dp)
183 tx_state_t *tx = &dp->dp_tx;
185 dprintf("pool %p\n", dp);
187 * Finish off any work in progress.
189 ASSERT(tx->tx_threads == 2);
190 txg_wait_synced(dp, 0);
193 * Wake all sync threads and wait for them to die.
195 mutex_enter(&tx->tx_sync_lock);
197 ASSERT(tx->tx_threads == 2);
201 cv_broadcast(&tx->tx_quiesce_more_cv);
202 cv_broadcast(&tx->tx_quiesce_done_cv);
203 cv_broadcast(&tx->tx_sync_more_cv);
205 while (tx->tx_threads != 0)
206 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
210 mutex_exit(&tx->tx_sync_lock);
214 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
216 tx_state_t *tx = &dp->dp_tx;
217 tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
220 mutex_enter(&tc->tc_lock);
222 txg = tx->tx_open_txg;
223 tc->tc_count[txg & TXG_MASK]++;
232 txg_rele_to_quiesce(txg_handle_t *th)
234 tx_cpu_t *tc = th->th_cpu;
236 mutex_exit(&tc->tc_lock);
240 txg_rele_to_sync(txg_handle_t *th)
242 tx_cpu_t *tc = th->th_cpu;
243 int g = th->th_txg & TXG_MASK;
245 mutex_enter(&tc->tc_lock);
246 ASSERT(tc->tc_count[g] != 0);
247 if (--tc->tc_count[g] == 0)
248 cv_broadcast(&tc->tc_cv[g]);
249 mutex_exit(&tc->tc_lock);
251 th->th_cpu = NULL; /* defensive */
255 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
257 tx_state_t *tx = &dp->dp_tx;
258 int g = txg & TXG_MASK;
262 * Grab all tx_cpu locks so nobody else can get into this txg.
264 for (c = 0; c < max_ncpus; c++)
265 mutex_enter(&tx->tx_cpu[c].tc_lock);
267 ASSERT(txg == tx->tx_open_txg);
271 * Now that we've incremented tx_open_txg, we can let threads
272 * enter the next transaction group.
274 for (c = 0; c < max_ncpus; c++)
275 mutex_exit(&tx->tx_cpu[c].tc_lock);
278 * Quiesce the transaction group by waiting for everyone to txg_exit().
280 for (c = 0; c < max_ncpus; c++) {
281 tx_cpu_t *tc = &tx->tx_cpu[c];
282 mutex_enter(&tc->tc_lock);
283 while (tc->tc_count[g] != 0)
284 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
285 mutex_exit(&tc->tc_lock);
290 txg_sync_thread(void *arg)
292 dsl_pool_t *dp = arg;
293 tx_state_t *tx = &dp->dp_tx;
295 uint64_t start, delta;
297 txg_thread_enter(tx, &cpr);
301 uint64_t timer, timeout = zfs_txg_timeout * hz;
305 * We sync when there's someone waiting on us, or the
306 * quiesce thread has handed off a txg to us, or we have
307 * reached our timeout.
309 timer = (delta >= timeout ? 0 : timeout - delta);
310 while (!tx->tx_exiting && timer > 0 &&
311 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
312 tx->tx_quiesced_txg == 0) {
313 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
314 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
315 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
316 delta = LBOLT - start;
317 timer = (delta > timeout ? 0 : timeout - delta);
321 * Wait until the quiesce thread hands off a txg to us,
322 * prompting it to do so if necessary.
324 while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
325 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
326 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
327 cv_broadcast(&tx->tx_quiesce_more_cv);
328 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
332 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
334 rw_enter(&tx->tx_suspend, RW_WRITER);
337 * Consume the quiesced txg which has been handed off to
338 * us. This may cause the quiescing thread to now be
339 * able to quiesce another txg, so we must signal it.
341 txg = tx->tx_quiesced_txg;
342 tx->tx_quiesced_txg = 0;
343 tx->tx_syncing_txg = txg;
344 cv_broadcast(&tx->tx_quiesce_more_cv);
345 rw_exit(&tx->tx_suspend);
347 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
348 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
349 mutex_exit(&tx->tx_sync_lock);
352 spa_sync(dp->dp_spa, txg);
353 delta = LBOLT - start;
355 mutex_enter(&tx->tx_sync_lock);
356 rw_enter(&tx->tx_suspend, RW_WRITER);
357 tx->tx_synced_txg = txg;
358 tx->tx_syncing_txg = 0;
359 rw_exit(&tx->tx_suspend);
360 cv_broadcast(&tx->tx_sync_done_cv);
365 txg_quiesce_thread(void *arg)
367 dsl_pool_t *dp = arg;
368 tx_state_t *tx = &dp->dp_tx;
371 txg_thread_enter(tx, &cpr);
377 * We quiesce when there's someone waiting on us.
378 * However, we can only have one txg in "quiescing" or
379 * "quiesced, waiting to sync" state. So we wait until
380 * the "quiesced, waiting to sync" txg has been consumed
381 * by the sync thread.
383 while (!tx->tx_exiting &&
384 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
385 tx->tx_quiesced_txg != 0))
386 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
389 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
391 txg = tx->tx_open_txg;
392 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
393 txg, tx->tx_quiesce_txg_waiting,
394 tx->tx_sync_txg_waiting);
395 mutex_exit(&tx->tx_sync_lock);
396 txg_quiesce(dp, txg);
397 mutex_enter(&tx->tx_sync_lock);
400 * Hand this txg off to the sync thread.
402 dprintf("quiesce done, handing off txg %llu\n", txg);
403 tx->tx_quiesced_txg = txg;
404 cv_broadcast(&tx->tx_sync_more_cv);
405 cv_broadcast(&tx->tx_quiesce_done_cv);
410 * Delay this thread by 'ticks' if we are still in the open transaction
411 * group and there is already a waiting txg quiesing or quiesced. Abort
412 * the delay if this txg stalls or enters the quiesing state.
415 txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
417 tx_state_t *tx = &dp->dp_tx;
418 int timeout = LBOLT + ticks;
420 /* don't delay if this txg could transition to quiesing immediately */
421 if (tx->tx_open_txg > txg ||
422 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
425 mutex_enter(&tx->tx_sync_lock);
426 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
427 mutex_exit(&tx->tx_sync_lock);
431 while (LBOLT < timeout &&
432 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp))
433 (void) cv_timedwait(&tx->tx_quiesce_more_cv, &tx->tx_sync_lock,
436 mutex_exit(&tx->tx_sync_lock);
440 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
442 tx_state_t *tx = &dp->dp_tx;
444 mutex_enter(&tx->tx_sync_lock);
445 ASSERT(tx->tx_threads == 2);
447 txg = tx->tx_open_txg;
448 if (tx->tx_sync_txg_waiting < txg)
449 tx->tx_sync_txg_waiting = txg;
450 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
451 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
452 while (tx->tx_synced_txg < txg) {
453 dprintf("broadcasting sync more "
454 "tx_synced=%llu waiting=%llu dp=%p\n",
455 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
456 cv_broadcast(&tx->tx_sync_more_cv);
457 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
459 mutex_exit(&tx->tx_sync_lock);
463 txg_wait_open(dsl_pool_t *dp, uint64_t txg)
465 tx_state_t *tx = &dp->dp_tx;
467 mutex_enter(&tx->tx_sync_lock);
468 ASSERT(tx->tx_threads == 2);
470 txg = tx->tx_open_txg + 1;
471 if (tx->tx_quiesce_txg_waiting < txg)
472 tx->tx_quiesce_txg_waiting = txg;
473 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
474 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
475 while (tx->tx_open_txg < txg) {
476 cv_broadcast(&tx->tx_quiesce_more_cv);
477 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
479 mutex_exit(&tx->tx_sync_lock);
483 txg_stalled(dsl_pool_t *dp)
485 tx_state_t *tx = &dp->dp_tx;
486 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
490 txg_sync_waiting(dsl_pool_t *dp)
492 tx_state_t *tx = &dp->dp_tx;
494 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
495 tx->tx_quiesced_txg != 0);
499 txg_suspend(dsl_pool_t *dp)
501 tx_state_t *tx = &dp->dp_tx;
502 /* XXX some code paths suspend when they are already suspended! */
503 rw_enter(&tx->tx_suspend, RW_READER);
507 txg_resume(dsl_pool_t *dp)
509 tx_state_t *tx = &dp->dp_tx;
510 rw_exit(&tx->tx_suspend);
514 * Per-txg object lists.
517 txg_list_create(txg_list_t *tl, size_t offset)
521 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
523 tl->tl_offset = offset;
525 for (t = 0; t < TXG_SIZE; t++)
526 tl->tl_head[t] = NULL;
530 txg_list_destroy(txg_list_t *tl)
534 for (t = 0; t < TXG_SIZE; t++)
535 ASSERT(txg_list_empty(tl, t));
537 mutex_destroy(&tl->tl_lock);
541 txg_list_empty(txg_list_t *tl, uint64_t txg)
543 return (tl->tl_head[txg & TXG_MASK] == NULL);
547 * Add an entry to the list.
548 * Returns 0 if it's a new entry, 1 if it's already there.
551 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
553 int t = txg & TXG_MASK;
554 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
557 mutex_enter(&tl->tl_lock);
558 already_on_list = tn->tn_member[t];
559 if (!already_on_list) {
560 tn->tn_member[t] = 1;
561 tn->tn_next[t] = tl->tl_head[t];
564 mutex_exit(&tl->tl_lock);
566 return (already_on_list);
570 * Remove the head of the list and return it.
573 txg_list_remove(txg_list_t *tl, uint64_t txg)
575 int t = txg & TXG_MASK;
579 mutex_enter(&tl->tl_lock);
580 if ((tn = tl->tl_head[t]) != NULL) {
581 p = (char *)tn - tl->tl_offset;
582 tl->tl_head[t] = tn->tn_next[t];
583 tn->tn_next[t] = NULL;
584 tn->tn_member[t] = 0;
586 mutex_exit(&tl->tl_lock);
592 * Remove a specific item from the list and return it.
595 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
597 int t = txg & TXG_MASK;
598 txg_node_t *tn, **tp;
600 mutex_enter(&tl->tl_lock);
602 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
603 if ((char *)tn - tl->tl_offset == p) {
604 *tp = tn->tn_next[t];
605 tn->tn_next[t] = NULL;
606 tn->tn_member[t] = 0;
607 mutex_exit(&tl->tl_lock);
612 mutex_exit(&tl->tl_lock);
618 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
620 int t = txg & TXG_MASK;
621 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
623 return (tn->tn_member[t]);
627 * Walk a txg list -- only safe if you know it's not changing.
630 txg_list_head(txg_list_t *tl, uint64_t txg)
632 int t = txg & TXG_MASK;
633 txg_node_t *tn = tl->tl_head[t];
635 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
639 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
641 int t = txg & TXG_MASK;
642 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
646 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);