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