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