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