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