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