]>
git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/txg.c
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(dsl_pool_t
*dp
);
37 static void txg_quiesce_thread(dsl_pool_t
*dp
);
39 int zfs_txg_timeout
= 30; /* max seconds worth of delta per txg */
42 * Prepare the txg subsystem.
45 txg_init(dsl_pool_t
*dp
, uint64_t txg
)
47 tx_state_t
*tx
= &dp
->dp_tx
;
49 bzero(tx
, sizeof (tx_state_t
));
51 tx
->tx_cpu
= kmem_zalloc(max_ncpus
* sizeof (tx_cpu_t
), KM_SLEEP
);
53 for (c
= 0; c
< max_ncpus
; c
++) {
56 mutex_init(&tx
->tx_cpu
[c
].tc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
57 for (i
= 0; i
< TXG_SIZE
; i
++) {
58 cv_init(&tx
->tx_cpu
[c
].tc_cv
[i
], NULL
, CV_DEFAULT
,
63 rw_init(&tx
->tx_suspend
, NULL
, RW_DEFAULT
, NULL
);
64 mutex_init(&tx
->tx_sync_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
66 tx
->tx_open_txg
= txg
;
70 * Close down the txg subsystem.
73 txg_fini(dsl_pool_t
*dp
)
75 tx_state_t
*tx
= &dp
->dp_tx
;
78 ASSERT(tx
->tx_threads
== 0);
80 rw_destroy(&tx
->tx_suspend
);
81 mutex_destroy(&tx
->tx_sync_lock
);
83 for (c
= 0; c
< max_ncpus
; c
++) {
86 mutex_destroy(&tx
->tx_cpu
[c
].tc_lock
);
87 for (i
= 0; i
< TXG_SIZE
; i
++)
88 cv_destroy(&tx
->tx_cpu
[c
].tc_cv
[i
]);
91 kmem_free(tx
->tx_cpu
, max_ncpus
* sizeof (tx_cpu_t
));
93 bzero(tx
, sizeof (tx_state_t
));
97 * Start syncing transaction groups.
100 txg_sync_start(dsl_pool_t
*dp
)
102 tx_state_t
*tx
= &dp
->dp_tx
;
104 mutex_enter(&tx
->tx_sync_lock
);
106 dprintf("pool %p\n", dp
);
108 ASSERT(tx
->tx_threads
== 0);
112 tx
->tx_quiesce_thread
= thread_create(NULL
, 0, txg_quiesce_thread
,
113 dp
, 0, &p0
, TS_RUN
, minclsyspri
);
116 * The sync thread can need a larger-than-default stack size on
117 * 32-bit x86. This is due in part to nested pools and
118 * scrub_visitbp() recursion.
120 tx
->tx_sync_thread
= thread_create(NULL
, 12<<10, txg_sync_thread
,
121 dp
, 0, &p0
, TS_RUN
, minclsyspri
);
123 mutex_exit(&tx
->tx_sync_lock
);
127 txg_thread_enter(tx_state_t
*tx
, callb_cpr_t
*cpr
)
129 CALLB_CPR_INIT(cpr
, &tx
->tx_sync_lock
, callb_generic_cpr
, FTAG
);
130 mutex_enter(&tx
->tx_sync_lock
);
134 txg_thread_exit(tx_state_t
*tx
, callb_cpr_t
*cpr
, kthread_t
**tpp
)
136 ASSERT(*tpp
!= NULL
);
139 cv_broadcast(&tx
->tx_exit_cv
);
140 CALLB_CPR_EXIT(cpr
); /* drops &tx->tx_sync_lock */
145 txg_thread_wait(tx_state_t
*tx
, callb_cpr_t
*cpr
, kcondvar_t
*cv
, uint64_t time
)
147 CALLB_CPR_SAFE_BEGIN(cpr
);
150 (void) cv_timedwait(cv
, &tx
->tx_sync_lock
, lbolt
+ time
);
152 cv_wait(cv
, &tx
->tx_sync_lock
);
154 CALLB_CPR_SAFE_END(cpr
, &tx
->tx_sync_lock
);
158 * Stop syncing transaction groups.
161 txg_sync_stop(dsl_pool_t
*dp
)
163 tx_state_t
*tx
= &dp
->dp_tx
;
165 dprintf("pool %p\n", dp
);
167 * Finish off any work in progress.
169 ASSERT(tx
->tx_threads
== 2);
170 txg_wait_synced(dp
, 0);
173 * Wake all sync threads and wait for them to die.
175 mutex_enter(&tx
->tx_sync_lock
);
177 ASSERT(tx
->tx_threads
== 2);
181 cv_broadcast(&tx
->tx_quiesce_more_cv
);
182 cv_broadcast(&tx
->tx_quiesce_done_cv
);
183 cv_broadcast(&tx
->tx_sync_more_cv
);
185 while (tx
->tx_threads
!= 0)
186 cv_wait(&tx
->tx_exit_cv
, &tx
->tx_sync_lock
);
190 mutex_exit(&tx
->tx_sync_lock
);
194 txg_hold_open(dsl_pool_t
*dp
, txg_handle_t
*th
)
196 tx_state_t
*tx
= &dp
->dp_tx
;
197 tx_cpu_t
*tc
= &tx
->tx_cpu
[CPU_SEQID
];
200 mutex_enter(&tc
->tc_lock
);
202 txg
= tx
->tx_open_txg
;
203 tc
->tc_count
[txg
& TXG_MASK
]++;
212 txg_rele_to_quiesce(txg_handle_t
*th
)
214 tx_cpu_t
*tc
= th
->th_cpu
;
216 mutex_exit(&tc
->tc_lock
);
220 txg_rele_to_sync(txg_handle_t
*th
)
222 tx_cpu_t
*tc
= th
->th_cpu
;
223 int g
= th
->th_txg
& TXG_MASK
;
225 mutex_enter(&tc
->tc_lock
);
226 ASSERT(tc
->tc_count
[g
] != 0);
227 if (--tc
->tc_count
[g
] == 0)
228 cv_broadcast(&tc
->tc_cv
[g
]);
229 mutex_exit(&tc
->tc_lock
);
231 th
->th_cpu
= NULL
; /* defensive */
235 txg_quiesce(dsl_pool_t
*dp
, uint64_t txg
)
237 tx_state_t
*tx
= &dp
->dp_tx
;
238 int g
= txg
& TXG_MASK
;
242 * Grab all tx_cpu locks so nobody else can get into this txg.
244 for (c
= 0; c
< max_ncpus
; c
++)
245 mutex_enter(&tx
->tx_cpu
[c
].tc_lock
);
247 ASSERT(txg
== tx
->tx_open_txg
);
251 * Now that we've incremented tx_open_txg, we can let threads
252 * enter the next transaction group.
254 for (c
= 0; c
< max_ncpus
; c
++)
255 mutex_exit(&tx
->tx_cpu
[c
].tc_lock
);
258 * Quiesce the transaction group by waiting for everyone to txg_exit().
260 for (c
= 0; c
< max_ncpus
; c
++) {
261 tx_cpu_t
*tc
= &tx
->tx_cpu
[c
];
262 mutex_enter(&tc
->tc_lock
);
263 while (tc
->tc_count
[g
] != 0)
264 cv_wait(&tc
->tc_cv
[g
], &tc
->tc_lock
);
265 mutex_exit(&tc
->tc_lock
);
270 txg_sync_thread(dsl_pool_t
*dp
)
272 tx_state_t
*tx
= &dp
->dp_tx
;
274 uint64_t start
, delta
;
276 txg_thread_enter(tx
, &cpr
);
280 uint64_t timer
, timeout
= zfs_txg_timeout
* hz
;
284 * We sync when we're scrubbing, there's someone waiting
285 * on us, or the quiesce thread has handed off a txg to
286 * us, or we have reached our timeout.
288 timer
= (delta
>= timeout
? 0 : timeout
- delta
);
289 while ((dp
->dp_scrub_func
== SCRUB_FUNC_NONE
||
290 spa_shutting_down(dp
->dp_spa
)) &&
291 !tx
->tx_exiting
&& timer
> 0 &&
292 tx
->tx_synced_txg
>= tx
->tx_sync_txg_waiting
&&
293 tx
->tx_quiesced_txg
== 0) {
294 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
295 tx
->tx_synced_txg
, tx
->tx_sync_txg_waiting
, dp
);
296 txg_thread_wait(tx
, &cpr
, &tx
->tx_sync_more_cv
, timer
);
297 delta
= lbolt
- start
;
298 timer
= (delta
> timeout
? 0 : timeout
- delta
);
302 * Wait until the quiesce thread hands off a txg to us,
303 * prompting it to do so if necessary.
305 while (!tx
->tx_exiting
&& tx
->tx_quiesced_txg
== 0) {
306 if (tx
->tx_quiesce_txg_waiting
< tx
->tx_open_txg
+1)
307 tx
->tx_quiesce_txg_waiting
= tx
->tx_open_txg
+1;
308 cv_broadcast(&tx
->tx_quiesce_more_cv
);
309 txg_thread_wait(tx
, &cpr
, &tx
->tx_quiesce_done_cv
, 0);
313 txg_thread_exit(tx
, &cpr
, &tx
->tx_sync_thread
);
315 rw_enter(&tx
->tx_suspend
, RW_WRITER
);
318 * Consume the quiesced txg which has been handed off to
319 * us. This may cause the quiescing thread to now be
320 * able to quiesce another txg, so we must signal it.
322 txg
= tx
->tx_quiesced_txg
;
323 tx
->tx_quiesced_txg
= 0;
324 tx
->tx_syncing_txg
= txg
;
325 cv_broadcast(&tx
->tx_quiesce_more_cv
);
326 rw_exit(&tx
->tx_suspend
);
328 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
329 txg
, tx
->tx_quiesce_txg_waiting
, tx
->tx_sync_txg_waiting
);
330 mutex_exit(&tx
->tx_sync_lock
);
333 spa_sync(dp
->dp_spa
, txg
);
334 delta
= lbolt
- start
;
336 mutex_enter(&tx
->tx_sync_lock
);
337 rw_enter(&tx
->tx_suspend
, RW_WRITER
);
338 tx
->tx_synced_txg
= txg
;
339 tx
->tx_syncing_txg
= 0;
340 rw_exit(&tx
->tx_suspend
);
341 cv_broadcast(&tx
->tx_sync_done_cv
);
346 txg_quiesce_thread(dsl_pool_t
*dp
)
348 tx_state_t
*tx
= &dp
->dp_tx
;
351 txg_thread_enter(tx
, &cpr
);
357 * We quiesce when there's someone waiting on us.
358 * However, we can only have one txg in "quiescing" or
359 * "quiesced, waiting to sync" state. So we wait until
360 * the "quiesced, waiting to sync" txg has been consumed
361 * by the sync thread.
363 while (!tx
->tx_exiting
&&
364 (tx
->tx_open_txg
>= tx
->tx_quiesce_txg_waiting
||
365 tx
->tx_quiesced_txg
!= 0))
366 txg_thread_wait(tx
, &cpr
, &tx
->tx_quiesce_more_cv
, 0);
369 txg_thread_exit(tx
, &cpr
, &tx
->tx_quiesce_thread
);
371 txg
= tx
->tx_open_txg
;
372 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
373 txg
, tx
->tx_quiesce_txg_waiting
,
374 tx
->tx_sync_txg_waiting
);
375 mutex_exit(&tx
->tx_sync_lock
);
376 txg_quiesce(dp
, txg
);
377 mutex_enter(&tx
->tx_sync_lock
);
380 * Hand this txg off to the sync thread.
382 dprintf("quiesce done, handing off txg %llu\n", txg
);
383 tx
->tx_quiesced_txg
= txg
;
384 cv_broadcast(&tx
->tx_sync_more_cv
);
385 cv_broadcast(&tx
->tx_quiesce_done_cv
);
390 * Delay this thread by 'ticks' if we are still in the open transaction
391 * group and there is already a waiting txg quiesing or quiesced. Abort
392 * the delay if this txg stalls or enters the quiesing state.
395 txg_delay(dsl_pool_t
*dp
, uint64_t txg
, int ticks
)
397 tx_state_t
*tx
= &dp
->dp_tx
;
398 int timeout
= lbolt
+ ticks
;
400 /* don't delay if this txg could transition to quiesing immediately */
401 if (tx
->tx_open_txg
> txg
||
402 tx
->tx_syncing_txg
== txg
-1 || tx
->tx_synced_txg
== txg
-1)
405 mutex_enter(&tx
->tx_sync_lock
);
406 if (tx
->tx_open_txg
> txg
|| tx
->tx_synced_txg
== txg
-1) {
407 mutex_exit(&tx
->tx_sync_lock
);
411 while (lbolt
< timeout
&&
412 tx
->tx_syncing_txg
< txg
-1 && !txg_stalled(dp
))
413 (void) cv_timedwait(&tx
->tx_quiesce_more_cv
, &tx
->tx_sync_lock
,
416 mutex_exit(&tx
->tx_sync_lock
);
420 txg_wait_synced(dsl_pool_t
*dp
, uint64_t txg
)
422 tx_state_t
*tx
= &dp
->dp_tx
;
424 mutex_enter(&tx
->tx_sync_lock
);
425 ASSERT(tx
->tx_threads
== 2);
427 txg
= tx
->tx_open_txg
;
428 if (tx
->tx_sync_txg_waiting
< txg
)
429 tx
->tx_sync_txg_waiting
= txg
;
430 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
431 txg
, tx
->tx_quiesce_txg_waiting
, tx
->tx_sync_txg_waiting
);
432 while (tx
->tx_synced_txg
< txg
) {
433 dprintf("broadcasting sync more "
434 "tx_synced=%llu waiting=%llu dp=%p\n",
435 tx
->tx_synced_txg
, tx
->tx_sync_txg_waiting
, dp
);
436 cv_broadcast(&tx
->tx_sync_more_cv
);
437 cv_wait(&tx
->tx_sync_done_cv
, &tx
->tx_sync_lock
);
439 mutex_exit(&tx
->tx_sync_lock
);
443 txg_wait_open(dsl_pool_t
*dp
, uint64_t txg
)
445 tx_state_t
*tx
= &dp
->dp_tx
;
447 mutex_enter(&tx
->tx_sync_lock
);
448 ASSERT(tx
->tx_threads
== 2);
450 txg
= tx
->tx_open_txg
+ 1;
451 if (tx
->tx_quiesce_txg_waiting
< txg
)
452 tx
->tx_quiesce_txg_waiting
= txg
;
453 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
454 txg
, tx
->tx_quiesce_txg_waiting
, tx
->tx_sync_txg_waiting
);
455 while (tx
->tx_open_txg
< txg
) {
456 cv_broadcast(&tx
->tx_quiesce_more_cv
);
457 cv_wait(&tx
->tx_quiesce_done_cv
, &tx
->tx_sync_lock
);
459 mutex_exit(&tx
->tx_sync_lock
);
463 txg_stalled(dsl_pool_t
*dp
)
465 tx_state_t
*tx
= &dp
->dp_tx
;
466 return (tx
->tx_quiesce_txg_waiting
> tx
->tx_open_txg
);
470 txg_sync_waiting(dsl_pool_t
*dp
)
472 tx_state_t
*tx
= &dp
->dp_tx
;
474 return (tx
->tx_syncing_txg
<= tx
->tx_sync_txg_waiting
||
475 tx
->tx_quiesced_txg
!= 0);
479 txg_suspend(dsl_pool_t
*dp
)
481 tx_state_t
*tx
= &dp
->dp_tx
;
482 /* XXX some code paths suspend when they are already suspended! */
483 rw_enter(&tx
->tx_suspend
, RW_READER
);
487 txg_resume(dsl_pool_t
*dp
)
489 tx_state_t
*tx
= &dp
->dp_tx
;
490 rw_exit(&tx
->tx_suspend
);
494 * Per-txg object lists.
497 txg_list_create(txg_list_t
*tl
, size_t offset
)
501 mutex_init(&tl
->tl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
503 tl
->tl_offset
= offset
;
505 for (t
= 0; t
< TXG_SIZE
; t
++)
506 tl
->tl_head
[t
] = NULL
;
510 txg_list_destroy(txg_list_t
*tl
)
514 for (t
= 0; t
< TXG_SIZE
; t
++)
515 ASSERT(txg_list_empty(tl
, t
));
517 mutex_destroy(&tl
->tl_lock
);
521 txg_list_empty(txg_list_t
*tl
, uint64_t txg
)
523 return (tl
->tl_head
[txg
& TXG_MASK
] == NULL
);
527 * Add an entry to the list.
528 * Returns 0 if it's a new entry, 1 if it's already there.
531 txg_list_add(txg_list_t
*tl
, void *p
, uint64_t txg
)
533 int t
= txg
& TXG_MASK
;
534 txg_node_t
*tn
= (txg_node_t
*)((char *)p
+ tl
->tl_offset
);
537 mutex_enter(&tl
->tl_lock
);
538 already_on_list
= tn
->tn_member
[t
];
539 if (!already_on_list
) {
540 tn
->tn_member
[t
] = 1;
541 tn
->tn_next
[t
] = tl
->tl_head
[t
];
544 mutex_exit(&tl
->tl_lock
);
546 return (already_on_list
);
550 * Remove the head of the list and return it.
553 txg_list_remove(txg_list_t
*tl
, uint64_t txg
)
555 int t
= txg
& TXG_MASK
;
559 mutex_enter(&tl
->tl_lock
);
560 if ((tn
= tl
->tl_head
[t
]) != NULL
) {
561 p
= (char *)tn
- tl
->tl_offset
;
562 tl
->tl_head
[t
] = tn
->tn_next
[t
];
563 tn
->tn_next
[t
] = NULL
;
564 tn
->tn_member
[t
] = 0;
566 mutex_exit(&tl
->tl_lock
);
572 * Remove a specific item from the list and return it.
575 txg_list_remove_this(txg_list_t
*tl
, void *p
, uint64_t txg
)
577 int t
= txg
& TXG_MASK
;
578 txg_node_t
*tn
, **tp
;
580 mutex_enter(&tl
->tl_lock
);
582 for (tp
= &tl
->tl_head
[t
]; (tn
= *tp
) != NULL
; tp
= &tn
->tn_next
[t
]) {
583 if ((char *)tn
- tl
->tl_offset
== p
) {
584 *tp
= tn
->tn_next
[t
];
585 tn
->tn_next
[t
] = NULL
;
586 tn
->tn_member
[t
] = 0;
587 mutex_exit(&tl
->tl_lock
);
592 mutex_exit(&tl
->tl_lock
);
598 txg_list_member(txg_list_t
*tl
, void *p
, uint64_t txg
)
600 int t
= txg
& TXG_MASK
;
601 txg_node_t
*tn
= (txg_node_t
*)((char *)p
+ tl
->tl_offset
);
603 return (tn
->tn_member
[t
]);
607 * Walk a txg list -- only safe if you know it's not changing.
610 txg_list_head(txg_list_t
*tl
, uint64_t txg
)
612 int t
= txg
& TXG_MASK
;
613 txg_node_t
*tn
= tl
->tl_head
[t
];
615 return (tn
== NULL
? NULL
: (char *)tn
- tl
->tl_offset
);
619 txg_list_next(txg_list_t
*tl
, void *p
, uint64_t txg
)
621 int t
= txg
& TXG_MASK
;
622 txg_node_t
*tn
= (txg_node_t
*)((char *)p
+ tl
->tl_offset
);
626 return (tn
== NULL
? NULL
: (char *)tn
- tl
->tl_offset
);