]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/txg.c
Fix zfs_write_limit_max integer size mismatch on 32-bit systems
[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.
34dc7c2f
BB
23 */
24
34dc7c2f
BB
25#include <sys/zfs_context.h>
26#include <sys/txg_impl.h>
27#include <sys/dmu_impl.h>
428870ff 28#include <sys/dmu_tx.h>
34dc7c2f 29#include <sys/dsl_pool.h>
428870ff 30#include <sys/dsl_scan.h>
34dc7c2f
BB
31#include <sys/callb.h>
32
33/*
34 * Pool-wide transaction groups.
35 */
36
37static void txg_sync_thread(dsl_pool_t *dp);
38static void txg_quiesce_thread(dsl_pool_t *dp);
39
572e2857 40int zfs_txg_timeout = 5; /* max seconds worth of delta per txg */
34dc7c2f
BB
41
42/*
43 * Prepare the txg subsystem.
44 */
45void
46txg_init(dsl_pool_t *dp, uint64_t txg)
47{
48 tx_state_t *tx = &dp->dp_tx;
49 int c;
50 bzero(tx, sizeof (tx_state_t));
51
00b46022 52 tx->tx_cpu = vmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
34dc7c2f
BB
53
54 for (c = 0; c < max_ncpus; c++) {
55 int i;
56
57 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
58 for (i = 0; i < TXG_SIZE; i++) {
59 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
60 NULL);
428870ff
BB
61 list_create(&tx->tx_cpu[c].tc_callbacks[i],
62 sizeof (dmu_tx_callback_t),
63 offsetof(dmu_tx_callback_t, dcb_node));
34dc7c2f
BB
64 }
65 }
66
34dc7c2f
BB
67 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
68
fb5f0bc8
BB
69 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
70 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
71 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
72 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
73 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
74
34dc7c2f
BB
75 tx->tx_open_txg = txg;
76}
77
78/*
79 * Close down the txg subsystem.
80 */
81void
82txg_fini(dsl_pool_t *dp)
83{
84 tx_state_t *tx = &dp->dp_tx;
85 int c;
86
87 ASSERT(tx->tx_threads == 0);
88
34dc7c2f
BB
89 mutex_destroy(&tx->tx_sync_lock);
90
fb5f0bc8
BB
91 cv_destroy(&tx->tx_sync_more_cv);
92 cv_destroy(&tx->tx_sync_done_cv);
93 cv_destroy(&tx->tx_quiesce_more_cv);
94 cv_destroy(&tx->tx_quiesce_done_cv);
95 cv_destroy(&tx->tx_exit_cv);
96
34dc7c2f
BB
97 for (c = 0; c < max_ncpus; c++) {
98 int i;
99
100 mutex_destroy(&tx->tx_cpu[c].tc_lock);
428870ff 101 for (i = 0; i < TXG_SIZE; i++) {
34dc7c2f 102 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
428870ff
BB
103 list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
104 }
34dc7c2f
BB
105 }
106
428870ff
BB
107 if (tx->tx_commit_cb_taskq != NULL)
108 taskq_destroy(tx->tx_commit_cb_taskq);
109
00b46022 110 vmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
34dc7c2f
BB
111
112 bzero(tx, sizeof (tx_state_t));
113}
114
115/*
116 * Start syncing transaction groups.
117 */
118void
119txg_sync_start(dsl_pool_t *dp)
120{
121 tx_state_t *tx = &dp->dp_tx;
122
123 mutex_enter(&tx->tx_sync_lock);
124
125 dprintf("pool %p\n", dp);
126
127 ASSERT(tx->tx_threads == 0);
128
129 tx->tx_threads = 2;
130
131 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
132 dp, 0, &p0, TS_RUN, minclsyspri);
133
b128c09f
BB
134 /*
135 * The sync thread can need a larger-than-default stack size on
136 * 32-bit x86. This is due in part to nested pools and
137 * scrub_visitbp() recursion.
138 */
428870ff 139 tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
34dc7c2f
BB
140 dp, 0, &p0, TS_RUN, minclsyspri);
141
142 mutex_exit(&tx->tx_sync_lock);
143}
144
145static void
146txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
147{
148 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
149 mutex_enter(&tx->tx_sync_lock);
150}
151
152static void
153txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
154{
155 ASSERT(*tpp != NULL);
156 *tpp = NULL;
157 tx->tx_threads--;
158 cv_broadcast(&tx->tx_exit_cv);
159 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
160 thread_exit();
161}
162
163static void
164txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, uint64_t time)
165{
166 CALLB_CPR_SAFE_BEGIN(cpr);
167
168 if (time)
bfd214af 169 (void) cv_timedwait_interruptible(cv, &tx->tx_sync_lock,
428870ff 170 ddi_get_lbolt() + time);
34dc7c2f 171 else
bfd214af 172 cv_wait_interruptible(cv, &tx->tx_sync_lock);
34dc7c2f
BB
173
174 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
175}
176
177/*
178 * Stop syncing transaction groups.
179 */
180void
181txg_sync_stop(dsl_pool_t *dp)
182{
183 tx_state_t *tx = &dp->dp_tx;
184
185 dprintf("pool %p\n", dp);
186 /*
187 * Finish off any work in progress.
188 */
189 ASSERT(tx->tx_threads == 2);
428870ff
BB
190
191 /*
192 * We need to ensure that we've vacated the deferred space_maps.
193 */
194 txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
34dc7c2f
BB
195
196 /*
197 * Wake all sync threads and wait for them to die.
198 */
199 mutex_enter(&tx->tx_sync_lock);
200
201 ASSERT(tx->tx_threads == 2);
202
203 tx->tx_exiting = 1;
204
205 cv_broadcast(&tx->tx_quiesce_more_cv);
206 cv_broadcast(&tx->tx_quiesce_done_cv);
207 cv_broadcast(&tx->tx_sync_more_cv);
208
209 while (tx->tx_threads != 0)
210 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
211
212 tx->tx_exiting = 0;
213
214 mutex_exit(&tx->tx_sync_lock);
215}
216
217uint64_t
218txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
219{
220 tx_state_t *tx = &dp->dp_tx;
15a9e033 221 tx_cpu_t *tc;
34dc7c2f
BB
222 uint64_t txg;
223
15a9e033
PS
224 /*
225 * It appears the processor id is simply used as a "random"
226 * number to index into the array, and there isn't any other
227 * significance to the chosen tx_cpu. Because.. Why not use
228 * the current cpu to index into the array?
229 */
230 kpreempt_disable();
231 tc = &tx->tx_cpu[CPU_SEQID];
232 kpreempt_enable();
233
34dc7c2f
BB
234 mutex_enter(&tc->tc_lock);
235
236 txg = tx->tx_open_txg;
237 tc->tc_count[txg & TXG_MASK]++;
238
239 th->th_cpu = tc;
240 th->th_txg = txg;
241
242 return (txg);
243}
244
245void
246txg_rele_to_quiesce(txg_handle_t *th)
247{
248 tx_cpu_t *tc = th->th_cpu;
249
250 mutex_exit(&tc->tc_lock);
251}
252
428870ff
BB
253void
254txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
255{
256 tx_cpu_t *tc = th->th_cpu;
257 int g = th->th_txg & TXG_MASK;
258
259 mutex_enter(&tc->tc_lock);
260 list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
261 mutex_exit(&tc->tc_lock);
262}
263
34dc7c2f
BB
264void
265txg_rele_to_sync(txg_handle_t *th)
266{
267 tx_cpu_t *tc = th->th_cpu;
268 int g = th->th_txg & TXG_MASK;
269
270 mutex_enter(&tc->tc_lock);
271 ASSERT(tc->tc_count[g] != 0);
272 if (--tc->tc_count[g] == 0)
273 cv_broadcast(&tc->tc_cv[g]);
274 mutex_exit(&tc->tc_lock);
275
276 th->th_cpu = NULL; /* defensive */
277}
278
279static void
280txg_quiesce(dsl_pool_t *dp, uint64_t txg)
281{
282 tx_state_t *tx = &dp->dp_tx;
283 int g = txg & TXG_MASK;
284 int c;
285
286 /*
287 * Grab all tx_cpu locks so nobody else can get into this txg.
288 */
289 for (c = 0; c < max_ncpus; c++)
290 mutex_enter(&tx->tx_cpu[c].tc_lock);
291
292 ASSERT(txg == tx->tx_open_txg);
293 tx->tx_open_txg++;
294
295 /*
296 * Now that we've incremented tx_open_txg, we can let threads
297 * enter the next transaction group.
298 */
299 for (c = 0; c < max_ncpus; c++)
300 mutex_exit(&tx->tx_cpu[c].tc_lock);
301
302 /*
303 * Quiesce the transaction group by waiting for everyone to txg_exit().
304 */
305 for (c = 0; c < max_ncpus; c++) {
306 tx_cpu_t *tc = &tx->tx_cpu[c];
307 mutex_enter(&tc->tc_lock);
308 while (tc->tc_count[g] != 0)
309 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
310 mutex_exit(&tc->tc_lock);
311 }
312}
313
428870ff
BB
314static void
315txg_do_callbacks(list_t *cb_list)
316{
317 dmu_tx_do_callbacks(cb_list, 0);
318
319 list_destroy(cb_list);
320
321 kmem_free(cb_list, sizeof (list_t));
322}
323
324/*
325 * Dispatch the commit callbacks registered on this txg to worker threads.
326 */
327static void
328txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
329{
330 int c;
331 tx_state_t *tx = &dp->dp_tx;
332 list_t *cb_list;
333
334 for (c = 0; c < max_ncpus; c++) {
335 tx_cpu_t *tc = &tx->tx_cpu[c];
336 /* No need to lock tx_cpu_t at this point */
337
338 int g = txg & TXG_MASK;
339
340 if (list_is_empty(&tc->tc_callbacks[g]))
341 continue;
342
343 if (tx->tx_commit_cb_taskq == NULL) {
344 /*
345 * Commit callback taskq hasn't been created yet.
346 */
347 tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
090ff092
RC
348 100, minclsyspri, max_ncpus, INT_MAX,
349 TASKQ_THREADS_CPU_PCT | TASKQ_PREPOPULATE);
428870ff
BB
350 }
351
b8d06fca 352 cb_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE);
428870ff
BB
353 list_create(cb_list, sizeof (dmu_tx_callback_t),
354 offsetof(dmu_tx_callback_t, dcb_node));
355
090ff092 356 list_move_tail(cb_list, &tc->tc_callbacks[g]);
428870ff
BB
357
358 (void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
359 txg_do_callbacks, cb_list, TQ_SLEEP);
360 }
361}
362
54a179e7
RC
363/*
364 * Wait for pending commit callbacks of already-synced transactions to finish
365 * processing.
366 * Calling this function from within a commit callback will deadlock.
367 */
368void
369txg_wait_callbacks(dsl_pool_t *dp)
370{
371 tx_state_t *tx = &dp->dp_tx;
372
373 if (tx->tx_commit_cb_taskq != NULL)
374 taskq_wait(tx->tx_commit_cb_taskq);
375}
376
34dc7c2f
BB
377static void
378txg_sync_thread(dsl_pool_t *dp)
379{
428870ff 380 spa_t *spa = dp->dp_spa;
34dc7c2f
BB
381 tx_state_t *tx = &dp->dp_tx;
382 callb_cpr_t cpr;
b128c09f 383 uint64_t start, delta;
34dc7c2f 384
8630650a
BB
385#ifdef _KERNEL
386 /*
387 * Annotate this process with a flag that indicates that it is
388 * unsafe to use KM_SLEEP during memory allocations due to the
389 * potential for a deadlock. KM_PUSHPAGE should be used instead.
390 */
391 current->flags |= PF_NOFS;
392#endif /* _KERNEL */
393
34dc7c2f
BB
394 txg_thread_enter(tx, &cpr);
395
396 start = delta = 0;
34dc7c2f 397 for (;;) {
b128c09f
BB
398 uint64_t timer, timeout = zfs_txg_timeout * hz;
399 uint64_t txg;
34dc7c2f
BB
400
401 /*
428870ff 402 * We sync when we're scanning, there's someone waiting
b128c09f
BB
403 * on us, or the quiesce thread has handed off a txg to
404 * us, or we have reached our timeout.
34dc7c2f
BB
405 */
406 timer = (delta >= timeout ? 0 : timeout - delta);
428870ff 407 while (!dsl_scan_active(dp->dp_scan) &&
b128c09f 408 !tx->tx_exiting && timer > 0 &&
34dc7c2f
BB
409 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
410 tx->tx_quiesced_txg == 0) {
411 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
412 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
413 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
428870ff 414 delta = ddi_get_lbolt() - start;
34dc7c2f
BB
415 timer = (delta > timeout ? 0 : timeout - delta);
416 }
417
418 /*
419 * Wait until the quiesce thread hands off a txg to us,
420 * prompting it to do so if necessary.
421 */
422 while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
423 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
424 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
425 cv_broadcast(&tx->tx_quiesce_more_cv);
426 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
427 }
428
429 if (tx->tx_exiting)
430 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
431
34dc7c2f
BB
432 /*
433 * Consume the quiesced txg which has been handed off to
434 * us. This may cause the quiescing thread to now be
435 * able to quiesce another txg, so we must signal it.
436 */
437 txg = tx->tx_quiesced_txg;
438 tx->tx_quiesced_txg = 0;
439 tx->tx_syncing_txg = txg;
440 cv_broadcast(&tx->tx_quiesce_more_cv);
34dc7c2f
BB
441
442 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
443 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
444 mutex_exit(&tx->tx_sync_lock);
b128c09f 445
428870ff
BB
446 start = ddi_get_lbolt();
447 spa_sync(spa, txg);
448 delta = ddi_get_lbolt() - start;
34dc7c2f 449
34dc7c2f 450 mutex_enter(&tx->tx_sync_lock);
34dc7c2f
BB
451 tx->tx_synced_txg = txg;
452 tx->tx_syncing_txg = 0;
34dc7c2f 453 cv_broadcast(&tx->tx_sync_done_cv);
428870ff
BB
454
455 /*
456 * Dispatch commit callbacks to worker threads.
457 */
458 txg_dispatch_callbacks(dp, txg);
34dc7c2f
BB
459 }
460}
461
462static void
463txg_quiesce_thread(dsl_pool_t *dp)
464{
465 tx_state_t *tx = &dp->dp_tx;
466 callb_cpr_t cpr;
467
468 txg_thread_enter(tx, &cpr);
469
470 for (;;) {
471 uint64_t txg;
472
473 /*
474 * We quiesce when there's someone waiting on us.
475 * However, we can only have one txg in "quiescing" or
476 * "quiesced, waiting to sync" state. So we wait until
477 * the "quiesced, waiting to sync" txg has been consumed
478 * by the sync thread.
479 */
480 while (!tx->tx_exiting &&
481 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
482 tx->tx_quiesced_txg != 0))
483 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
484
485 if (tx->tx_exiting)
486 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
487
488 txg = tx->tx_open_txg;
489 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
490 txg, tx->tx_quiesce_txg_waiting,
491 tx->tx_sync_txg_waiting);
492 mutex_exit(&tx->tx_sync_lock);
493 txg_quiesce(dp, txg);
494 mutex_enter(&tx->tx_sync_lock);
495
496 /*
497 * Hand this txg off to the sync thread.
498 */
499 dprintf("quiesce done, handing off txg %llu\n", txg);
500 tx->tx_quiesced_txg = txg;
501 cv_broadcast(&tx->tx_sync_more_cv);
502 cv_broadcast(&tx->tx_quiesce_done_cv);
503 }
504}
505
506/*
507 * Delay this thread by 'ticks' if we are still in the open transaction
508 * group and there is already a waiting txg quiesing or quiesced. Abort
509 * the delay if this txg stalls or enters the quiesing state.
510 */
511void
512txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
513{
514 tx_state_t *tx = &dp->dp_tx;
cddafdcb 515 clock_t timeout = ddi_get_lbolt() + ticks;
34dc7c2f
BB
516
517 /* don't delay if this txg could transition to quiesing immediately */
518 if (tx->tx_open_txg > txg ||
519 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
520 return;
521
522 mutex_enter(&tx->tx_sync_lock);
523 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
524 mutex_exit(&tx->tx_sync_lock);
525 return;
526 }
527
428870ff 528 while (ddi_get_lbolt() < timeout &&
34dc7c2f
BB
529 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp))
530 (void) cv_timedwait(&tx->tx_quiesce_more_cv, &tx->tx_sync_lock,
531 timeout);
532
570827e1
BB
533 DMU_TX_STAT_BUMP(dmu_tx_delay);
534
34dc7c2f
BB
535 mutex_exit(&tx->tx_sync_lock);
536}
537
538void
539txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
540{
541 tx_state_t *tx = &dp->dp_tx;
542
543 mutex_enter(&tx->tx_sync_lock);
544 ASSERT(tx->tx_threads == 2);
545 if (txg == 0)
428870ff 546 txg = tx->tx_open_txg + TXG_DEFER_SIZE;
34dc7c2f
BB
547 if (tx->tx_sync_txg_waiting < txg)
548 tx->tx_sync_txg_waiting = txg;
549 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
550 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
551 while (tx->tx_synced_txg < txg) {
552 dprintf("broadcasting sync more "
553 "tx_synced=%llu waiting=%llu dp=%p\n",
554 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
555 cv_broadcast(&tx->tx_sync_more_cv);
556 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
557 }
558 mutex_exit(&tx->tx_sync_lock);
559}
560
561void
562txg_wait_open(dsl_pool_t *dp, uint64_t txg)
563{
564 tx_state_t *tx = &dp->dp_tx;
565
566 mutex_enter(&tx->tx_sync_lock);
567 ASSERT(tx->tx_threads == 2);
568 if (txg == 0)
569 txg = tx->tx_open_txg + 1;
570 if (tx->tx_quiesce_txg_waiting < txg)
571 tx->tx_quiesce_txg_waiting = txg;
572 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
573 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
574 while (tx->tx_open_txg < txg) {
575 cv_broadcast(&tx->tx_quiesce_more_cv);
576 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
577 }
578 mutex_exit(&tx->tx_sync_lock);
579}
580
b128c09f 581boolean_t
34dc7c2f
BB
582txg_stalled(dsl_pool_t *dp)
583{
584 tx_state_t *tx = &dp->dp_tx;
585 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
586}
587
b128c09f
BB
588boolean_t
589txg_sync_waiting(dsl_pool_t *dp)
590{
591 tx_state_t *tx = &dp->dp_tx;
592
593 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
594 tx->tx_quiesced_txg != 0);
595}
596
34dc7c2f
BB
597/*
598 * Per-txg object lists.
599 */
600void
601txg_list_create(txg_list_t *tl, size_t offset)
602{
603 int t;
604
605 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
606
607 tl->tl_offset = offset;
608
609 for (t = 0; t < TXG_SIZE; t++)
610 tl->tl_head[t] = NULL;
611}
612
613void
614txg_list_destroy(txg_list_t *tl)
615{
616 int t;
617
618 for (t = 0; t < TXG_SIZE; t++)
619 ASSERT(txg_list_empty(tl, t));
620
621 mutex_destroy(&tl->tl_lock);
622}
623
624int
625txg_list_empty(txg_list_t *tl, uint64_t txg)
626{
627 return (tl->tl_head[txg & TXG_MASK] == NULL);
628}
629
630/*
631 * Add an entry to the list.
632 * Returns 0 if it's a new entry, 1 if it's already there.
633 */
634int
635txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
636{
637 int t = txg & TXG_MASK;
638 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
639 int already_on_list;
640
641 mutex_enter(&tl->tl_lock);
642 already_on_list = tn->tn_member[t];
643 if (!already_on_list) {
644 tn->tn_member[t] = 1;
645 tn->tn_next[t] = tl->tl_head[t];
646 tl->tl_head[t] = tn;
647 }
648 mutex_exit(&tl->tl_lock);
649
650 return (already_on_list);
651}
652
428870ff
BB
653/*
654 * Add an entry to the end of the list (walks list to find end).
655 * Returns 0 if it's a new entry, 1 if it's already there.
656 */
657int
658txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
659{
660 int t = txg & TXG_MASK;
661 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
662 int already_on_list;
663
664 mutex_enter(&tl->tl_lock);
665 already_on_list = tn->tn_member[t];
666 if (!already_on_list) {
667 txg_node_t **tp;
668
669 for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
670 continue;
671
672 tn->tn_member[t] = 1;
673 tn->tn_next[t] = NULL;
674 *tp = tn;
675 }
676 mutex_exit(&tl->tl_lock);
677
678 return (already_on_list);
679}
680
34dc7c2f
BB
681/*
682 * Remove the head of the list and return it.
683 */
684void *
685txg_list_remove(txg_list_t *tl, uint64_t txg)
686{
687 int t = txg & TXG_MASK;
688 txg_node_t *tn;
689 void *p = NULL;
690
691 mutex_enter(&tl->tl_lock);
692 if ((tn = tl->tl_head[t]) != NULL) {
693 p = (char *)tn - tl->tl_offset;
694 tl->tl_head[t] = tn->tn_next[t];
695 tn->tn_next[t] = NULL;
696 tn->tn_member[t] = 0;
697 }
698 mutex_exit(&tl->tl_lock);
699
700 return (p);
701}
702
703/*
704 * Remove a specific item from the list and return it.
705 */
706void *
707txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
708{
709 int t = txg & TXG_MASK;
710 txg_node_t *tn, **tp;
711
712 mutex_enter(&tl->tl_lock);
713
714 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
715 if ((char *)tn - tl->tl_offset == p) {
716 *tp = tn->tn_next[t];
717 tn->tn_next[t] = NULL;
718 tn->tn_member[t] = 0;
719 mutex_exit(&tl->tl_lock);
720 return (p);
721 }
722 }
723
724 mutex_exit(&tl->tl_lock);
725
726 return (NULL);
727}
728
729int
730txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
731{
732 int t = txg & TXG_MASK;
733 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
734
735 return (tn->tn_member[t]);
736}
737
738/*
739 * Walk a txg list -- only safe if you know it's not changing.
740 */
741void *
742txg_list_head(txg_list_t *tl, uint64_t txg)
743{
744 int t = txg & TXG_MASK;
745 txg_node_t *tn = tl->tl_head[t];
746
747 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
748}
749
750void *
751txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
752{
753 int t = txg & TXG_MASK;
754 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
755
756 tn = tn->tn_next[t];
757
758 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
759}
c28b2279
BB
760
761#if defined(_KERNEL) && defined(HAVE_SPL)
762EXPORT_SYMBOL(txg_init);
763EXPORT_SYMBOL(txg_fini);
764EXPORT_SYMBOL(txg_sync_start);
765EXPORT_SYMBOL(txg_sync_stop);
766EXPORT_SYMBOL(txg_hold_open);
767EXPORT_SYMBOL(txg_rele_to_quiesce);
768EXPORT_SYMBOL(txg_rele_to_sync);
769EXPORT_SYMBOL(txg_register_callbacks);
770EXPORT_SYMBOL(txg_delay);
771EXPORT_SYMBOL(txg_wait_synced);
772EXPORT_SYMBOL(txg_wait_open);
54a179e7 773EXPORT_SYMBOL(txg_wait_callbacks);
c28b2279
BB
774EXPORT_SYMBOL(txg_stalled);
775EXPORT_SYMBOL(txg_sync_waiting);
776#endif