]>
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. |
3e31d2b0 | 23 | * Copyright (c) 2011 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
428870ff BB |
26 | /* Portions Copyright 2010 Robert Milkowski */ |
27 | ||
34dc7c2f BB |
28 | #include <sys/zfs_context.h> |
29 | #include <sys/spa.h> | |
30 | #include <sys/dmu.h> | |
31 | #include <sys/zap.h> | |
32 | #include <sys/arc.h> | |
33 | #include <sys/stat.h> | |
34 | #include <sys/resource.h> | |
35 | #include <sys/zil.h> | |
36 | #include <sys/zil_impl.h> | |
37 | #include <sys/dsl_dataset.h> | |
572e2857 | 38 | #include <sys/vdev_impl.h> |
34dc7c2f | 39 | #include <sys/dmu_tx.h> |
428870ff | 40 | #include <sys/dsl_pool.h> |
34dc7c2f BB |
41 | |
42 | /* | |
43 | * The zfs intent log (ZIL) saves transaction records of system calls | |
44 | * that change the file system in memory with enough information | |
45 | * to be able to replay them. These are stored in memory until | |
46 | * either the DMU transaction group (txg) commits them to the stable pool | |
47 | * and they can be discarded, or they are flushed to the stable log | |
48 | * (also in the pool) due to a fsync, O_DSYNC or other synchronous | |
49 | * requirement. In the event of a panic or power fail then those log | |
50 | * records (transactions) are replayed. | |
51 | * | |
52 | * There is one ZIL per file system. Its on-disk (pool) format consists | |
53 | * of 3 parts: | |
54 | * | |
55 | * - ZIL header | |
56 | * - ZIL blocks | |
57 | * - ZIL records | |
58 | * | |
59 | * A log record holds a system call transaction. Log blocks can | |
60 | * hold many log records and the blocks are chained together. | |
61 | * Each ZIL block contains a block pointer (blkptr_t) to the next | |
62 | * ZIL block in the chain. The ZIL header points to the first | |
63 | * block in the chain. Note there is not a fixed place in the pool | |
64 | * to hold blocks. They are dynamically allocated and freed as | |
65 | * needed from the blocks available. Figure X shows the ZIL structure: | |
66 | */ | |
67 | ||
b6ad9671 ED |
68 | /* |
69 | * See zil.h for more information about these fields. | |
70 | */ | |
71 | zil_stats_t zil_stats = { | |
72 | { "zil_commit_count", KSTAT_DATA_UINT64 }, | |
73 | { "zil_commit_writer_count", KSTAT_DATA_UINT64 }, | |
74 | { "zil_itx_count", KSTAT_DATA_UINT64 }, | |
75 | { "zil_itx_indirect_count", KSTAT_DATA_UINT64 }, | |
76 | { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64 }, | |
77 | { "zil_itx_copied_count", KSTAT_DATA_UINT64 }, | |
78 | { "zil_itx_copied_bytes", KSTAT_DATA_UINT64 }, | |
79 | { "zil_itx_needcopy_count", KSTAT_DATA_UINT64 }, | |
80 | { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64 }, | |
81 | { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64 }, | |
82 | { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64 }, | |
83 | { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64 }, | |
84 | { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64 }, | |
85 | }; | |
86 | ||
87 | static kstat_t *zil_ksp; | |
88 | ||
34dc7c2f BB |
89 | /* |
90 | * This global ZIL switch affects all pools | |
91 | */ | |
428870ff | 92 | int zil_replay_disable = 0; /* disable intent logging replay */ |
34dc7c2f BB |
93 | |
94 | /* | |
95 | * Tunable parameter for debugging or performance analysis. Setting | |
96 | * zfs_nocacheflush will cause corruption on power loss if a volatile | |
97 | * out-of-order write cache is enabled. | |
98 | */ | |
c409e464 | 99 | int zfs_nocacheflush = 0; |
34dc7c2f BB |
100 | |
101 | static kmem_cache_t *zil_lwb_cache; | |
102 | ||
572e2857 | 103 | static void zil_async_to_sync(zilog_t *zilog, uint64_t foid); |
428870ff BB |
104 | |
105 | #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ | |
106 | sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) | |
107 | ||
108 | ||
572e2857 BB |
109 | /* |
110 | * ziltest is by and large an ugly hack, but very useful in | |
111 | * checking replay without tedious work. | |
112 | * When running ziltest we want to keep all itx's and so maintain | |
113 | * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG | |
114 | * We subtract TXG_CONCURRENT_STATES to allow for common code. | |
115 | */ | |
116 | #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES) | |
117 | ||
34dc7c2f | 118 | static int |
428870ff | 119 | zil_bp_compare(const void *x1, const void *x2) |
34dc7c2f | 120 | { |
428870ff BB |
121 | const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; |
122 | const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; | |
34dc7c2f BB |
123 | |
124 | if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) | |
125 | return (-1); | |
126 | if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) | |
127 | return (1); | |
128 | ||
129 | if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) | |
130 | return (-1); | |
131 | if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) | |
132 | return (1); | |
133 | ||
134 | return (0); | |
135 | } | |
136 | ||
137 | static void | |
428870ff | 138 | zil_bp_tree_init(zilog_t *zilog) |
34dc7c2f | 139 | { |
428870ff BB |
140 | avl_create(&zilog->zl_bp_tree, zil_bp_compare, |
141 | sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); | |
34dc7c2f BB |
142 | } |
143 | ||
144 | static void | |
428870ff | 145 | zil_bp_tree_fini(zilog_t *zilog) |
34dc7c2f | 146 | { |
428870ff BB |
147 | avl_tree_t *t = &zilog->zl_bp_tree; |
148 | zil_bp_node_t *zn; | |
34dc7c2f BB |
149 | void *cookie = NULL; |
150 | ||
151 | while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) | |
428870ff | 152 | kmem_free(zn, sizeof (zil_bp_node_t)); |
34dc7c2f BB |
153 | |
154 | avl_destroy(t); | |
155 | } | |
156 | ||
428870ff BB |
157 | int |
158 | zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) | |
34dc7c2f | 159 | { |
428870ff BB |
160 | avl_tree_t *t = &zilog->zl_bp_tree; |
161 | const dva_t *dva = BP_IDENTITY(bp); | |
162 | zil_bp_node_t *zn; | |
34dc7c2f BB |
163 | avl_index_t where; |
164 | ||
165 | if (avl_find(t, dva, &where) != NULL) | |
166 | return (EEXIST); | |
167 | ||
428870ff | 168 | zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); |
34dc7c2f BB |
169 | zn->zn_dva = *dva; |
170 | avl_insert(t, zn, where); | |
171 | ||
172 | return (0); | |
173 | } | |
174 | ||
175 | static zil_header_t * | |
176 | zil_header_in_syncing_context(zilog_t *zilog) | |
177 | { | |
178 | return ((zil_header_t *)zilog->zl_header); | |
179 | } | |
180 | ||
181 | static void | |
182 | zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) | |
183 | { | |
184 | zio_cksum_t *zc = &bp->blk_cksum; | |
185 | ||
186 | zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); | |
187 | zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); | |
188 | zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); | |
189 | zc->zc_word[ZIL_ZC_SEQ] = 1ULL; | |
190 | } | |
191 | ||
192 | /* | |
428870ff | 193 | * Read a log block and make sure it's valid. |
34dc7c2f BB |
194 | */ |
195 | static int | |
428870ff BB |
196 | zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, |
197 | char **end) | |
34dc7c2f | 198 | { |
428870ff | 199 | enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; |
34dc7c2f | 200 | uint32_t aflags = ARC_WAIT; |
428870ff BB |
201 | arc_buf_t *abuf = NULL; |
202 | zbookmark_t zb; | |
34dc7c2f BB |
203 | int error; |
204 | ||
428870ff BB |
205 | if (zilog->zl_header->zh_claim_txg == 0) |
206 | zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; | |
34dc7c2f | 207 | |
428870ff BB |
208 | if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) |
209 | zio_flags |= ZIO_FLAG_SPECULATIVE; | |
34dc7c2f | 210 | |
428870ff BB |
211 | SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], |
212 | ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); | |
213 | ||
214 | error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, | |
215 | ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); | |
34dc7c2f BB |
216 | |
217 | if (error == 0) { | |
34dc7c2f BB |
218 | zio_cksum_t cksum = bp->blk_cksum; |
219 | ||
220 | /* | |
b128c09f BB |
221 | * Validate the checksummed log block. |
222 | * | |
34dc7c2f BB |
223 | * Sequence numbers should be... sequential. The checksum |
224 | * verifier for the next block should be bp's checksum plus 1. | |
b128c09f BB |
225 | * |
226 | * Also check the log chain linkage and size used. | |
34dc7c2f BB |
227 | */ |
228 | cksum.zc_word[ZIL_ZC_SEQ]++; | |
229 | ||
428870ff BB |
230 | if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { |
231 | zil_chain_t *zilc = abuf->b_data; | |
232 | char *lr = (char *)(zilc + 1); | |
233 | uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); | |
34dc7c2f | 234 | |
428870ff BB |
235 | if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, |
236 | sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { | |
237 | error = ECKSUM; | |
238 | } else { | |
239 | bcopy(lr, dst, len); | |
240 | *end = (char *)dst + len; | |
241 | *nbp = zilc->zc_next_blk; | |
242 | } | |
243 | } else { | |
244 | char *lr = abuf->b_data; | |
245 | uint64_t size = BP_GET_LSIZE(bp); | |
246 | zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; | |
247 | ||
248 | if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, | |
249 | sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || | |
250 | (zilc->zc_nused > (size - sizeof (*zilc)))) { | |
251 | error = ECKSUM; | |
252 | } else { | |
253 | bcopy(lr, dst, zilc->zc_nused); | |
254 | *end = (char *)dst + zilc->zc_nused; | |
255 | *nbp = zilc->zc_next_blk; | |
256 | } | |
34dc7c2f | 257 | } |
428870ff BB |
258 | |
259 | VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); | |
260 | } | |
261 | ||
262 | return (error); | |
263 | } | |
264 | ||
265 | /* | |
266 | * Read a TX_WRITE log data block. | |
267 | */ | |
268 | static int | |
269 | zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) | |
270 | { | |
271 | enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; | |
272 | const blkptr_t *bp = &lr->lr_blkptr; | |
273 | uint32_t aflags = ARC_WAIT; | |
274 | arc_buf_t *abuf = NULL; | |
275 | zbookmark_t zb; | |
276 | int error; | |
277 | ||
278 | if (BP_IS_HOLE(bp)) { | |
279 | if (wbuf != NULL) | |
280 | bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); | |
281 | return (0); | |
34dc7c2f BB |
282 | } |
283 | ||
428870ff BB |
284 | if (zilog->zl_header->zh_claim_txg == 0) |
285 | zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; | |
286 | ||
287 | SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, | |
288 | ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); | |
289 | ||
290 | error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, | |
291 | ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); | |
292 | ||
293 | if (error == 0) { | |
294 | if (wbuf != NULL) | |
295 | bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); | |
296 | (void) arc_buf_remove_ref(abuf, &abuf); | |
297 | } | |
34dc7c2f BB |
298 | |
299 | return (error); | |
300 | } | |
301 | ||
302 | /* | |
303 | * Parse the intent log, and call parse_func for each valid record within. | |
34dc7c2f | 304 | */ |
428870ff | 305 | int |
34dc7c2f BB |
306 | zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, |
307 | zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) | |
308 | { | |
309 | const zil_header_t *zh = zilog->zl_header; | |
428870ff BB |
310 | boolean_t claimed = !!zh->zh_claim_txg; |
311 | uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; | |
312 | uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; | |
313 | uint64_t max_blk_seq = 0; | |
314 | uint64_t max_lr_seq = 0; | |
315 | uint64_t blk_count = 0; | |
316 | uint64_t lr_count = 0; | |
317 | blkptr_t blk, next_blk; | |
34dc7c2f | 318 | char *lrbuf, *lrp; |
428870ff | 319 | int error = 0; |
34dc7c2f | 320 | |
d4ed6673 BB |
321 | bzero(&next_blk, sizeof(blkptr_t)); |
322 | ||
428870ff BB |
323 | /* |
324 | * Old logs didn't record the maximum zh_claim_lr_seq. | |
325 | */ | |
326 | if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) | |
327 | claim_lr_seq = UINT64_MAX; | |
34dc7c2f BB |
328 | |
329 | /* | |
330 | * Starting at the block pointed to by zh_log we read the log chain. | |
331 | * For each block in the chain we strongly check that block to | |
332 | * ensure its validity. We stop when an invalid block is found. | |
333 | * For each block pointer in the chain we call parse_blk_func(). | |
334 | * For each record in each valid block we call parse_lr_func(). | |
335 | * If the log has been claimed, stop if we encounter a sequence | |
336 | * number greater than the highest claimed sequence number. | |
337 | */ | |
428870ff BB |
338 | lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); |
339 | zil_bp_tree_init(zilog); | |
34dc7c2f | 340 | |
428870ff BB |
341 | for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { |
342 | uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; | |
343 | int reclen; | |
d4ed6673 | 344 | char *end = NULL; |
34dc7c2f | 345 | |
428870ff BB |
346 | if (blk_seq > claim_blk_seq) |
347 | break; | |
348 | if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) | |
349 | break; | |
350 | ASSERT3U(max_blk_seq, <, blk_seq); | |
351 | max_blk_seq = blk_seq; | |
352 | blk_count++; | |
34dc7c2f | 353 | |
428870ff BB |
354 | if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) |
355 | break; | |
34dc7c2f | 356 | |
428870ff | 357 | error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); |
34dc7c2f BB |
358 | if (error) |
359 | break; | |
360 | ||
428870ff | 361 | for (lrp = lrbuf; lrp < end; lrp += reclen) { |
34dc7c2f BB |
362 | lr_t *lr = (lr_t *)lrp; |
363 | reclen = lr->lrc_reclen; | |
364 | ASSERT3U(reclen, >=, sizeof (lr_t)); | |
428870ff BB |
365 | if (lr->lrc_seq > claim_lr_seq) |
366 | goto done; | |
367 | if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) | |
368 | goto done; | |
369 | ASSERT3U(max_lr_seq, <, lr->lrc_seq); | |
370 | max_lr_seq = lr->lrc_seq; | |
371 | lr_count++; | |
34dc7c2f | 372 | } |
34dc7c2f | 373 | } |
428870ff BB |
374 | done: |
375 | zilog->zl_parse_error = error; | |
376 | zilog->zl_parse_blk_seq = max_blk_seq; | |
377 | zilog->zl_parse_lr_seq = max_lr_seq; | |
378 | zilog->zl_parse_blk_count = blk_count; | |
379 | zilog->zl_parse_lr_count = lr_count; | |
380 | ||
381 | ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || | |
382 | (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); | |
383 | ||
384 | zil_bp_tree_fini(zilog); | |
385 | zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); | |
34dc7c2f | 386 | |
428870ff | 387 | return (error); |
34dc7c2f BB |
388 | } |
389 | ||
428870ff | 390 | static int |
34dc7c2f BB |
391 | zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) |
392 | { | |
34dc7c2f BB |
393 | /* |
394 | * Claim log block if not already committed and not already claimed. | |
428870ff | 395 | * If tx == NULL, just verify that the block is claimable. |
34dc7c2f | 396 | */ |
428870ff BB |
397 | if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0) |
398 | return (0); | |
399 | ||
400 | return (zio_wait(zio_claim(NULL, zilog->zl_spa, | |
401 | tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, | |
402 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); | |
34dc7c2f BB |
403 | } |
404 | ||
428870ff | 405 | static int |
34dc7c2f BB |
406 | zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) |
407 | { | |
428870ff BB |
408 | lr_write_t *lr = (lr_write_t *)lrc; |
409 | int error; | |
410 | ||
411 | if (lrc->lrc_txtype != TX_WRITE) | |
412 | return (0); | |
413 | ||
414 | /* | |
415 | * If the block is not readable, don't claim it. This can happen | |
416 | * in normal operation when a log block is written to disk before | |
417 | * some of the dmu_sync() blocks it points to. In this case, the | |
418 | * transaction cannot have been committed to anyone (we would have | |
419 | * waited for all writes to be stable first), so it is semantically | |
420 | * correct to declare this the end of the log. | |
421 | */ | |
422 | if (lr->lr_blkptr.blk_birth >= first_txg && | |
423 | (error = zil_read_log_data(zilog, lr, NULL)) != 0) | |
424 | return (error); | |
425 | return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); | |
34dc7c2f BB |
426 | } |
427 | ||
428 | /* ARGSUSED */ | |
428870ff | 429 | static int |
34dc7c2f BB |
430 | zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) |
431 | { | |
428870ff BB |
432 | zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); |
433 | ||
434 | return (0); | |
34dc7c2f BB |
435 | } |
436 | ||
428870ff | 437 | static int |
34dc7c2f BB |
438 | zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) |
439 | { | |
428870ff BB |
440 | lr_write_t *lr = (lr_write_t *)lrc; |
441 | blkptr_t *bp = &lr->lr_blkptr; | |
442 | ||
34dc7c2f BB |
443 | /* |
444 | * If we previously claimed it, we need to free it. | |
445 | */ | |
428870ff BB |
446 | if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && |
447 | bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0) | |
448 | zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); | |
449 | ||
450 | return (0); | |
451 | } | |
452 | ||
453 | static lwb_t * | |
454 | zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg) | |
455 | { | |
456 | lwb_t *lwb; | |
457 | ||
458 | lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); | |
459 | lwb->lwb_zilog = zilog; | |
460 | lwb->lwb_blk = *bp; | |
461 | lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); | |
462 | lwb->lwb_max_txg = txg; | |
463 | lwb->lwb_zio = NULL; | |
464 | lwb->lwb_tx = NULL; | |
465 | if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { | |
466 | lwb->lwb_nused = sizeof (zil_chain_t); | |
467 | lwb->lwb_sz = BP_GET_LSIZE(bp); | |
468 | } else { | |
469 | lwb->lwb_nused = 0; | |
470 | lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); | |
34dc7c2f | 471 | } |
428870ff BB |
472 | |
473 | mutex_enter(&zilog->zl_lock); | |
474 | list_insert_tail(&zilog->zl_lwb_list, lwb); | |
475 | mutex_exit(&zilog->zl_lock); | |
476 | ||
477 | return (lwb); | |
34dc7c2f BB |
478 | } |
479 | ||
480 | /* | |
481 | * Create an on-disk intent log. | |
482 | */ | |
428870ff | 483 | static lwb_t * |
34dc7c2f BB |
484 | zil_create(zilog_t *zilog) |
485 | { | |
486 | const zil_header_t *zh = zilog->zl_header; | |
428870ff | 487 | lwb_t *lwb = NULL; |
34dc7c2f BB |
488 | uint64_t txg = 0; |
489 | dmu_tx_t *tx = NULL; | |
490 | blkptr_t blk; | |
491 | int error = 0; | |
492 | ||
493 | /* | |
494 | * Wait for any previous destroy to complete. | |
495 | */ | |
496 | txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); | |
497 | ||
498 | ASSERT(zh->zh_claim_txg == 0); | |
499 | ASSERT(zh->zh_replay_seq == 0); | |
500 | ||
501 | blk = zh->zh_log; | |
502 | ||
503 | /* | |
428870ff BB |
504 | * Allocate an initial log block if: |
505 | * - there isn't one already | |
506 | * - the existing block is the wrong endianess | |
34dc7c2f | 507 | */ |
fb5f0bc8 | 508 | if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { |
34dc7c2f | 509 | tx = dmu_tx_create(zilog->zl_os); |
428870ff | 510 | VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); |
34dc7c2f BB |
511 | dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); |
512 | txg = dmu_tx_get_txg(tx); | |
513 | ||
fb5f0bc8 | 514 | if (!BP_IS_HOLE(&blk)) { |
428870ff | 515 | zio_free_zil(zilog->zl_spa, txg, &blk); |
fb5f0bc8 BB |
516 | BP_ZERO(&blk); |
517 | } | |
518 | ||
428870ff BB |
519 | error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, |
520 | ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); | |
34dc7c2f BB |
521 | |
522 | if (error == 0) | |
523 | zil_init_log_chain(zilog, &blk); | |
524 | } | |
525 | ||
526 | /* | |
527 | * Allocate a log write buffer (lwb) for the first log block. | |
528 | */ | |
428870ff BB |
529 | if (error == 0) |
530 | lwb = zil_alloc_lwb(zilog, &blk, txg); | |
34dc7c2f BB |
531 | |
532 | /* | |
533 | * If we just allocated the first log block, commit our transaction | |
534 | * and wait for zil_sync() to stuff the block poiner into zh_log. | |
535 | * (zh is part of the MOS, so we cannot modify it in open context.) | |
536 | */ | |
537 | if (tx != NULL) { | |
538 | dmu_tx_commit(tx); | |
539 | txg_wait_synced(zilog->zl_dmu_pool, txg); | |
540 | } | |
541 | ||
542 | ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); | |
428870ff BB |
543 | |
544 | return (lwb); | |
34dc7c2f BB |
545 | } |
546 | ||
547 | /* | |
548 | * In one tx, free all log blocks and clear the log header. | |
549 | * If keep_first is set, then we're replaying a log with no content. | |
550 | * We want to keep the first block, however, so that the first | |
551 | * synchronous transaction doesn't require a txg_wait_synced() | |
552 | * in zil_create(). We don't need to txg_wait_synced() here either | |
553 | * when keep_first is set, because both zil_create() and zil_destroy() | |
554 | * will wait for any in-progress destroys to complete. | |
555 | */ | |
556 | void | |
557 | zil_destroy(zilog_t *zilog, boolean_t keep_first) | |
558 | { | |
559 | const zil_header_t *zh = zilog->zl_header; | |
560 | lwb_t *lwb; | |
561 | dmu_tx_t *tx; | |
562 | uint64_t txg; | |
563 | ||
564 | /* | |
565 | * Wait for any previous destroy to complete. | |
566 | */ | |
567 | txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); | |
568 | ||
428870ff BB |
569 | zilog->zl_old_header = *zh; /* debugging aid */ |
570 | ||
34dc7c2f BB |
571 | if (BP_IS_HOLE(&zh->zh_log)) |
572 | return; | |
573 | ||
574 | tx = dmu_tx_create(zilog->zl_os); | |
428870ff | 575 | VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); |
34dc7c2f BB |
576 | dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); |
577 | txg = dmu_tx_get_txg(tx); | |
578 | ||
579 | mutex_enter(&zilog->zl_lock); | |
580 | ||
34dc7c2f BB |
581 | ASSERT3U(zilog->zl_destroy_txg, <, txg); |
582 | zilog->zl_destroy_txg = txg; | |
583 | zilog->zl_keep_first = keep_first; | |
584 | ||
585 | if (!list_is_empty(&zilog->zl_lwb_list)) { | |
586 | ASSERT(zh->zh_claim_txg == 0); | |
3e31d2b0 | 587 | VERIFY(!keep_first); |
34dc7c2f BB |
588 | while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { |
589 | list_remove(&zilog->zl_lwb_list, lwb); | |
590 | if (lwb->lwb_buf != NULL) | |
591 | zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); | |
428870ff | 592 | zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); |
34dc7c2f BB |
593 | kmem_cache_free(zil_lwb_cache, lwb); |
594 | } | |
428870ff BB |
595 | } else if (!keep_first) { |
596 | (void) zil_parse(zilog, zil_free_log_block, | |
597 | zil_free_log_record, tx, zh->zh_claim_txg); | |
34dc7c2f BB |
598 | } |
599 | mutex_exit(&zilog->zl_lock); | |
600 | ||
601 | dmu_tx_commit(tx); | |
602 | } | |
603 | ||
34dc7c2f | 604 | int |
428870ff | 605 | zil_claim(const char *osname, void *txarg) |
34dc7c2f BB |
606 | { |
607 | dmu_tx_t *tx = txarg; | |
608 | uint64_t first_txg = dmu_tx_get_txg(tx); | |
609 | zilog_t *zilog; | |
610 | zil_header_t *zh; | |
611 | objset_t *os; | |
612 | int error; | |
613 | ||
428870ff | 614 | error = dmu_objset_hold(osname, FTAG, &os); |
34dc7c2f | 615 | if (error) { |
b128c09f | 616 | cmn_err(CE_WARN, "can't open objset for %s", osname); |
34dc7c2f BB |
617 | return (0); |
618 | } | |
619 | ||
620 | zilog = dmu_objset_zil(os); | |
621 | zh = zil_header_in_syncing_context(zilog); | |
622 | ||
428870ff | 623 | if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { |
9babb374 | 624 | if (!BP_IS_HOLE(&zh->zh_log)) |
428870ff | 625 | zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); |
9babb374 BB |
626 | BP_ZERO(&zh->zh_log); |
627 | dsl_dataset_dirty(dmu_objset_ds(os), tx); | |
428870ff BB |
628 | dmu_objset_rele(os, FTAG); |
629 | return (0); | |
9babb374 BB |
630 | } |
631 | ||
34dc7c2f BB |
632 | /* |
633 | * Claim all log blocks if we haven't already done so, and remember | |
634 | * the highest claimed sequence number. This ensures that if we can | |
635 | * read only part of the log now (e.g. due to a missing device), | |
636 | * but we can read the entire log later, we will not try to replay | |
637 | * or destroy beyond the last block we successfully claimed. | |
638 | */ | |
639 | ASSERT3U(zh->zh_claim_txg, <=, first_txg); | |
640 | if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { | |
428870ff | 641 | (void) zil_parse(zilog, zil_claim_log_block, |
34dc7c2f | 642 | zil_claim_log_record, tx, first_txg); |
428870ff BB |
643 | zh->zh_claim_txg = first_txg; |
644 | zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; | |
645 | zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; | |
646 | if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) | |
647 | zh->zh_flags |= ZIL_REPLAY_NEEDED; | |
648 | zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; | |
34dc7c2f BB |
649 | dsl_dataset_dirty(dmu_objset_ds(os), tx); |
650 | } | |
651 | ||
652 | ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); | |
428870ff | 653 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
654 | return (0); |
655 | } | |
656 | ||
b128c09f BB |
657 | /* |
658 | * Check the log by walking the log chain. | |
659 | * Checksum errors are ok as they indicate the end of the chain. | |
660 | * Any other error (no device or read failure) returns an error. | |
661 | */ | |
b128c09f | 662 | int |
428870ff | 663 | zil_check_log_chain(const char *osname, void *tx) |
b128c09f BB |
664 | { |
665 | zilog_t *zilog; | |
b128c09f | 666 | objset_t *os; |
572e2857 | 667 | blkptr_t *bp; |
b128c09f BB |
668 | int error; |
669 | ||
428870ff BB |
670 | ASSERT(tx == NULL); |
671 | ||
672 | error = dmu_objset_hold(osname, FTAG, &os); | |
b128c09f BB |
673 | if (error) { |
674 | cmn_err(CE_WARN, "can't open objset for %s", osname); | |
675 | return (0); | |
676 | } | |
677 | ||
678 | zilog = dmu_objset_zil(os); | |
572e2857 BB |
679 | bp = (blkptr_t *)&zilog->zl_header->zh_log; |
680 | ||
681 | /* | |
682 | * Check the first block and determine if it's on a log device | |
683 | * which may have been removed or faulted prior to loading this | |
684 | * pool. If so, there's no point in checking the rest of the log | |
685 | * as its content should have already been synced to the pool. | |
686 | */ | |
687 | if (!BP_IS_HOLE(bp)) { | |
688 | vdev_t *vd; | |
689 | boolean_t valid = B_TRUE; | |
690 | ||
691 | spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); | |
692 | vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); | |
693 | if (vd->vdev_islog && vdev_is_dead(vd)) | |
694 | valid = vdev_log_state_valid(vd); | |
695 | spa_config_exit(os->os_spa, SCL_STATE, FTAG); | |
696 | ||
697 | if (!valid) { | |
698 | dmu_objset_rele(os, FTAG); | |
699 | return (0); | |
700 | } | |
701 | } | |
b128c09f | 702 | |
428870ff BB |
703 | /* |
704 | * Because tx == NULL, zil_claim_log_block() will not actually claim | |
705 | * any blocks, but just determine whether it is possible to do so. | |
706 | * In addition to checking the log chain, zil_claim_log_block() | |
707 | * will invoke zio_claim() with a done func of spa_claim_notify(), | |
708 | * which will update spa_max_claim_txg. See spa_load() for details. | |
709 | */ | |
710 | error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, | |
711 | zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); | |
712 | ||
713 | dmu_objset_rele(os, FTAG); | |
714 | ||
715 | return ((error == ECKSUM || error == ENOENT) ? 0 : error); | |
b128c09f BB |
716 | } |
717 | ||
34dc7c2f BB |
718 | static int |
719 | zil_vdev_compare(const void *x1, const void *x2) | |
720 | { | |
572e2857 BB |
721 | const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; |
722 | const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; | |
34dc7c2f BB |
723 | |
724 | if (v1 < v2) | |
725 | return (-1); | |
726 | if (v1 > v2) | |
727 | return (1); | |
728 | ||
729 | return (0); | |
730 | } | |
731 | ||
732 | void | |
428870ff | 733 | zil_add_block(zilog_t *zilog, const blkptr_t *bp) |
34dc7c2f BB |
734 | { |
735 | avl_tree_t *t = &zilog->zl_vdev_tree; | |
736 | avl_index_t where; | |
737 | zil_vdev_node_t *zv, zvsearch; | |
738 | int ndvas = BP_GET_NDVAS(bp); | |
739 | int i; | |
740 | ||
741 | if (zfs_nocacheflush) | |
742 | return; | |
743 | ||
744 | ASSERT(zilog->zl_writer); | |
745 | ||
746 | /* | |
747 | * Even though we're zl_writer, we still need a lock because the | |
748 | * zl_get_data() callbacks may have dmu_sync() done callbacks | |
749 | * that will run concurrently. | |
750 | */ | |
751 | mutex_enter(&zilog->zl_vdev_lock); | |
752 | for (i = 0; i < ndvas; i++) { | |
753 | zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); | |
754 | if (avl_find(t, &zvsearch, &where) == NULL) { | |
755 | zv = kmem_alloc(sizeof (*zv), KM_SLEEP); | |
756 | zv->zv_vdev = zvsearch.zv_vdev; | |
757 | avl_insert(t, zv, where); | |
758 | } | |
759 | } | |
760 | mutex_exit(&zilog->zl_vdev_lock); | |
761 | } | |
762 | ||
572e2857 | 763 | static void |
34dc7c2f BB |
764 | zil_flush_vdevs(zilog_t *zilog) |
765 | { | |
766 | spa_t *spa = zilog->zl_spa; | |
767 | avl_tree_t *t = &zilog->zl_vdev_tree; | |
768 | void *cookie = NULL; | |
769 | zil_vdev_node_t *zv; | |
770 | zio_t *zio; | |
771 | ||
772 | ASSERT(zilog->zl_writer); | |
773 | ||
774 | /* | |
775 | * We don't need zl_vdev_lock here because we're the zl_writer, | |
776 | * and all zl_get_data() callbacks are done. | |
777 | */ | |
778 | if (avl_numnodes(t) == 0) | |
779 | return; | |
780 | ||
b128c09f | 781 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
34dc7c2f | 782 | |
b128c09f | 783 | zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); |
34dc7c2f BB |
784 | |
785 | while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { | |
786 | vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); | |
787 | if (vd != NULL) | |
788 | zio_flush(zio, vd); | |
789 | kmem_free(zv, sizeof (*zv)); | |
790 | } | |
791 | ||
792 | /* | |
793 | * Wait for all the flushes to complete. Not all devices actually | |
794 | * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. | |
795 | */ | |
796 | (void) zio_wait(zio); | |
797 | ||
b128c09f | 798 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f BB |
799 | } |
800 | ||
801 | /* | |
802 | * Function called when a log block write completes | |
803 | */ | |
804 | static void | |
805 | zil_lwb_write_done(zio_t *zio) | |
806 | { | |
807 | lwb_t *lwb = zio->io_private; | |
808 | zilog_t *zilog = lwb->lwb_zilog; | |
428870ff | 809 | dmu_tx_t *tx = lwb->lwb_tx; |
34dc7c2f | 810 | |
b128c09f | 811 | ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); |
b128c09f BB |
812 | ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); |
813 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
814 | ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); | |
815 | ASSERT(!BP_IS_GANG(zio->io_bp)); | |
816 | ASSERT(!BP_IS_HOLE(zio->io_bp)); | |
817 | ASSERT(zio->io_bp->blk_fill == 0); | |
818 | ||
34dc7c2f | 819 | /* |
9babb374 BB |
820 | * Ensure the lwb buffer pointer is cleared before releasing |
821 | * the txg. If we have had an allocation failure and | |
822 | * the txg is waiting to sync then we want want zil_sync() | |
823 | * to remove the lwb so that it's not picked up as the next new | |
824 | * one in zil_commit_writer(). zil_sync() will only remove | |
825 | * the lwb if lwb_buf is null. | |
34dc7c2f | 826 | */ |
34dc7c2f BB |
827 | zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); |
828 | mutex_enter(&zilog->zl_lock); | |
829 | lwb->lwb_buf = NULL; | |
428870ff BB |
830 | lwb->lwb_tx = NULL; |
831 | mutex_exit(&zilog->zl_lock); | |
9babb374 BB |
832 | |
833 | /* | |
834 | * Now that we've written this log block, we have a stable pointer | |
835 | * to the next block in the chain, so it's OK to let the txg in | |
428870ff | 836 | * which we allocated the next block sync. |
9babb374 | 837 | */ |
428870ff | 838 | dmu_tx_commit(tx); |
34dc7c2f BB |
839 | } |
840 | ||
841 | /* | |
842 | * Initialize the io for a log block. | |
34dc7c2f BB |
843 | */ |
844 | static void | |
845 | zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) | |
846 | { | |
847 | zbookmark_t zb; | |
848 | ||
428870ff BB |
849 | SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], |
850 | ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, | |
851 | lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); | |
34dc7c2f BB |
852 | |
853 | if (zilog->zl_root_zio == NULL) { | |
854 | zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, | |
855 | ZIO_FLAG_CANFAIL); | |
856 | } | |
857 | if (lwb->lwb_zio == NULL) { | |
858 | lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, | |
428870ff | 859 | 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk), |
9babb374 | 860 | zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, |
428870ff | 861 | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); |
34dc7c2f BB |
862 | } |
863 | } | |
864 | ||
428870ff BB |
865 | /* |
866 | * Define a limited set of intent log block sizes. | |
867 | * These must be a multiple of 4KB. Note only the amount used (again | |
868 | * aligned to 4KB) actually gets written. However, we can't always just | |
869 | * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted. | |
870 | */ | |
871 | uint64_t zil_block_buckets[] = { | |
872 | 4096, /* non TX_WRITE */ | |
873 | 8192+4096, /* data base */ | |
874 | 32*1024 + 4096, /* NFS writes */ | |
875 | UINT64_MAX | |
876 | }; | |
877 | ||
878 | /* | |
879 | * Use the slog as long as the logbias is 'latency' and the current commit size | |
880 | * is less than the limit or the total list size is less than 2X the limit. | |
881 | * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. | |
882 | */ | |
ee191e80 | 883 | unsigned long zil_slog_limit = 1024 * 1024; |
428870ff BB |
884 | #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ |
885 | (((zilog)->zl_cur_used < zil_slog_limit) || \ | |
886 | ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) | |
887 | ||
34dc7c2f BB |
888 | /* |
889 | * Start a log block write and advance to the next log block. | |
890 | * Calls are serialized. | |
891 | */ | |
892 | static lwb_t * | |
893 | zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) | |
894 | { | |
428870ff BB |
895 | lwb_t *nlwb = NULL; |
896 | zil_chain_t *zilc; | |
34dc7c2f | 897 | spa_t *spa = zilog->zl_spa; |
428870ff BB |
898 | blkptr_t *bp; |
899 | dmu_tx_t *tx; | |
34dc7c2f | 900 | uint64_t txg; |
428870ff BB |
901 | uint64_t zil_blksz, wsz; |
902 | int i, error; | |
b6ad9671 | 903 | boolean_t use_slog; |
428870ff BB |
904 | |
905 | if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { | |
906 | zilc = (zil_chain_t *)lwb->lwb_buf; | |
907 | bp = &zilc->zc_next_blk; | |
908 | } else { | |
909 | zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); | |
910 | bp = &zilc->zc_next_blk; | |
911 | } | |
34dc7c2f | 912 | |
428870ff | 913 | ASSERT(lwb->lwb_nused <= lwb->lwb_sz); |
34dc7c2f BB |
914 | |
915 | /* | |
916 | * Allocate the next block and save its address in this block | |
917 | * before writing it in order to establish the log chain. | |
918 | * Note that if the allocation of nlwb synced before we wrote | |
919 | * the block that points at it (lwb), we'd leak it if we crashed. | |
428870ff BB |
920 | * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). |
921 | * We dirty the dataset to ensure that zil_sync() will be called | |
922 | * to clean up in the event of allocation failure or I/O failure. | |
34dc7c2f | 923 | */ |
428870ff BB |
924 | tx = dmu_tx_create(zilog->zl_os); |
925 | VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); | |
926 | dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); | |
927 | txg = dmu_tx_get_txg(tx); | |
928 | ||
929 | lwb->lwb_tx = tx; | |
34dc7c2f BB |
930 | |
931 | /* | |
428870ff BB |
932 | * Log blocks are pre-allocated. Here we select the size of the next |
933 | * block, based on size used in the last block. | |
934 | * - first find the smallest bucket that will fit the block from a | |
935 | * limited set of block sizes. This is because it's faster to write | |
936 | * blocks allocated from the same metaslab as they are adjacent or | |
937 | * close. | |
938 | * - next find the maximum from the new suggested size and an array of | |
939 | * previous sizes. This lessens a picket fence effect of wrongly | |
940 | * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k | |
941 | * requests. | |
942 | * | |
943 | * Note we only write what is used, but we can't just allocate | |
944 | * the maximum block size because we can exhaust the available | |
945 | * pool log space. | |
34dc7c2f | 946 | */ |
428870ff BB |
947 | zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); |
948 | for (i = 0; zil_blksz > zil_block_buckets[i]; i++) | |
949 | continue; | |
950 | zil_blksz = zil_block_buckets[i]; | |
951 | if (zil_blksz == UINT64_MAX) | |
952 | zil_blksz = SPA_MAXBLOCKSIZE; | |
953 | zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; | |
954 | for (i = 0; i < ZIL_PREV_BLKS; i++) | |
955 | zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); | |
956 | zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); | |
34dc7c2f BB |
957 | |
958 | BP_ZERO(bp); | |
959 | /* pass the old blkptr in order to spread log blocks across devs */ | |
b6ad9671 | 960 | use_slog = USE_SLOG(zilog); |
428870ff | 961 | error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, |
b6ad9671 ED |
962 | use_slog); |
963 | if (use_slog) | |
964 | { | |
965 | ZIL_STAT_BUMP(zil_itx_metaslab_slog_count); | |
966 | ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused); | |
967 | } | |
968 | else | |
969 | { | |
970 | ZIL_STAT_BUMP(zil_itx_metaslab_normal_count); | |
971 | ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused); | |
972 | } | |
428870ff BB |
973 | if (!error) { |
974 | ASSERT3U(bp->blk_birth, ==, txg); | |
975 | bp->blk_cksum = lwb->lwb_blk.blk_cksum; | |
976 | bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; | |
34dc7c2f BB |
977 | |
978 | /* | |
428870ff | 979 | * Allocate a new log write buffer (lwb). |
34dc7c2f | 980 | */ |
428870ff | 981 | nlwb = zil_alloc_lwb(zilog, bp, txg); |
34dc7c2f | 982 | |
428870ff BB |
983 | /* Record the block for later vdev flushing */ |
984 | zil_add_block(zilog, &lwb->lwb_blk); | |
34dc7c2f BB |
985 | } |
986 | ||
428870ff BB |
987 | if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { |
988 | /* For Slim ZIL only write what is used. */ | |
989 | wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); | |
990 | ASSERT3U(wsz, <=, lwb->lwb_sz); | |
991 | zio_shrink(lwb->lwb_zio, wsz); | |
34dc7c2f | 992 | |
428870ff BB |
993 | } else { |
994 | wsz = lwb->lwb_sz; | |
995 | } | |
34dc7c2f | 996 | |
428870ff BB |
997 | zilc->zc_pad = 0; |
998 | zilc->zc_nused = lwb->lwb_nused; | |
999 | zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; | |
34dc7c2f BB |
1000 | |
1001 | /* | |
428870ff | 1002 | * clear unused data for security |
34dc7c2f | 1003 | */ |
428870ff | 1004 | bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); |
34dc7c2f | 1005 | |
428870ff | 1006 | zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */ |
34dc7c2f BB |
1007 | |
1008 | /* | |
428870ff BB |
1009 | * If there was an allocation failure then nlwb will be null which |
1010 | * forces a txg_wait_synced(). | |
34dc7c2f | 1011 | */ |
34dc7c2f BB |
1012 | return (nlwb); |
1013 | } | |
1014 | ||
1015 | static lwb_t * | |
1016 | zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) | |
1017 | { | |
1018 | lr_t *lrc = &itx->itx_lr; /* common log record */ | |
428870ff BB |
1019 | lr_write_t *lrw = (lr_write_t *)lrc; |
1020 | char *lr_buf; | |
34dc7c2f BB |
1021 | uint64_t txg = lrc->lrc_txg; |
1022 | uint64_t reclen = lrc->lrc_reclen; | |
428870ff | 1023 | uint64_t dlen = 0; |
34dc7c2f BB |
1024 | |
1025 | if (lwb == NULL) | |
1026 | return (NULL); | |
428870ff | 1027 | |
34dc7c2f BB |
1028 | ASSERT(lwb->lwb_buf != NULL); |
1029 | ||
1030 | if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) | |
1031 | dlen = P2ROUNDUP_TYPED( | |
428870ff | 1032 | lrw->lr_length, sizeof (uint64_t), uint64_t); |
34dc7c2f BB |
1033 | |
1034 | zilog->zl_cur_used += (reclen + dlen); | |
1035 | ||
1036 | zil_lwb_write_init(zilog, lwb); | |
1037 | ||
1038 | /* | |
1039 | * If this record won't fit in the current log block, start a new one. | |
1040 | */ | |
428870ff | 1041 | if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { |
34dc7c2f BB |
1042 | lwb = zil_lwb_write_start(zilog, lwb); |
1043 | if (lwb == NULL) | |
1044 | return (NULL); | |
1045 | zil_lwb_write_init(zilog, lwb); | |
428870ff BB |
1046 | ASSERT(LWB_EMPTY(lwb)); |
1047 | if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { | |
34dc7c2f BB |
1048 | txg_wait_synced(zilog->zl_dmu_pool, txg); |
1049 | return (lwb); | |
1050 | } | |
1051 | } | |
1052 | ||
428870ff BB |
1053 | lr_buf = lwb->lwb_buf + lwb->lwb_nused; |
1054 | bcopy(lrc, lr_buf, reclen); | |
1055 | lrc = (lr_t *)lr_buf; | |
1056 | lrw = (lr_write_t *)lrc; | |
34dc7c2f | 1057 | |
b6ad9671 ED |
1058 | ZIL_STAT_BUMP(zil_itx_count); |
1059 | ||
34dc7c2f BB |
1060 | /* |
1061 | * If it's a write, fetch the data or get its blkptr as appropriate. | |
1062 | */ | |
1063 | if (lrc->lrc_txtype == TX_WRITE) { | |
1064 | if (txg > spa_freeze_txg(zilog->zl_spa)) | |
1065 | txg_wait_synced(zilog->zl_dmu_pool, txg); | |
b6ad9671 ED |
1066 | if (itx->itx_wr_state == WR_COPIED) { |
1067 | ZIL_STAT_BUMP(zil_itx_copied_count); | |
1068 | ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length); | |
1069 | } else { | |
34dc7c2f BB |
1070 | char *dbuf; |
1071 | int error; | |
1072 | ||
34dc7c2f BB |
1073 | if (dlen) { |
1074 | ASSERT(itx->itx_wr_state == WR_NEED_COPY); | |
428870ff BB |
1075 | dbuf = lr_buf + reclen; |
1076 | lrw->lr_common.lrc_reclen += dlen; | |
b6ad9671 ED |
1077 | ZIL_STAT_BUMP(zil_itx_needcopy_count); |
1078 | ZIL_STAT_INCR(zil_itx_needcopy_bytes, lrw->lr_length); | |
34dc7c2f BB |
1079 | } else { |
1080 | ASSERT(itx->itx_wr_state == WR_INDIRECT); | |
1081 | dbuf = NULL; | |
b6ad9671 ED |
1082 | ZIL_STAT_BUMP(zil_itx_indirect_count); |
1083 | ZIL_STAT_INCR(zil_itx_indirect_bytes, lrw->lr_length); | |
34dc7c2f BB |
1084 | } |
1085 | error = zilog->zl_get_data( | |
428870ff | 1086 | itx->itx_private, lrw, dbuf, lwb->lwb_zio); |
45d1cae3 BB |
1087 | if (error == EIO) { |
1088 | txg_wait_synced(zilog->zl_dmu_pool, txg); | |
1089 | return (lwb); | |
1090 | } | |
34dc7c2f BB |
1091 | if (error) { |
1092 | ASSERT(error == ENOENT || error == EEXIST || | |
1093 | error == EALREADY); | |
1094 | return (lwb); | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | ||
428870ff BB |
1099 | /* |
1100 | * We're actually making an entry, so update lrc_seq to be the | |
1101 | * log record sequence number. Note that this is generally not | |
1102 | * equal to the itx sequence number because not all transactions | |
1103 | * are synchronous, and sometimes spa_sync() gets there first. | |
1104 | */ | |
1105 | lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ | |
34dc7c2f BB |
1106 | lwb->lwb_nused += reclen + dlen; |
1107 | lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); | |
428870ff | 1108 | ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); |
34dc7c2f BB |
1109 | ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); |
1110 | ||
1111 | return (lwb); | |
1112 | } | |
1113 | ||
1114 | itx_t * | |
1115 | zil_itx_create(uint64_t txtype, size_t lrsize) | |
1116 | { | |
1117 | itx_t *itx; | |
1118 | ||
1119 | lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); | |
1120 | ||
701b1f81 BB |
1121 | itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, |
1122 | KM_PUSHPAGE | KM_NODEBUG); | |
34dc7c2f BB |
1123 | itx->itx_lr.lrc_txtype = txtype; |
1124 | itx->itx_lr.lrc_reclen = lrsize; | |
1125 | itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ | |
1126 | itx->itx_lr.lrc_seq = 0; /* defensive */ | |
572e2857 | 1127 | itx->itx_sync = B_TRUE; /* default is synchronous */ |
34dc7c2f BB |
1128 | |
1129 | return (itx); | |
1130 | } | |
1131 | ||
428870ff BB |
1132 | void |
1133 | zil_itx_destroy(itx_t *itx) | |
1134 | { | |
1135 | kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); | |
1136 | } | |
1137 | ||
572e2857 BB |
1138 | /* |
1139 | * Free up the sync and async itxs. The itxs_t has already been detached | |
1140 | * so no locks are needed. | |
1141 | */ | |
1142 | static void | |
1143 | zil_itxg_clean(itxs_t *itxs) | |
34dc7c2f | 1144 | { |
572e2857 BB |
1145 | itx_t *itx; |
1146 | list_t *list; | |
1147 | avl_tree_t *t; | |
1148 | void *cookie; | |
1149 | itx_async_node_t *ian; | |
1150 | ||
1151 | list = &itxs->i_sync_list; | |
1152 | while ((itx = list_head(list)) != NULL) { | |
1153 | list_remove(list, itx); | |
1154 | kmem_free(itx, offsetof(itx_t, itx_lr) + | |
1155 | itx->itx_lr.lrc_reclen); | |
1156 | } | |
34dc7c2f | 1157 | |
572e2857 BB |
1158 | cookie = NULL; |
1159 | t = &itxs->i_async_tree; | |
1160 | while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { | |
1161 | list = &ian->ia_list; | |
1162 | while ((itx = list_head(list)) != NULL) { | |
1163 | list_remove(list, itx); | |
1164 | kmem_free(itx, offsetof(itx_t, itx_lr) + | |
1165 | itx->itx_lr.lrc_reclen); | |
1166 | } | |
1167 | list_destroy(list); | |
1168 | kmem_free(ian, sizeof (itx_async_node_t)); | |
1169 | } | |
1170 | avl_destroy(t); | |
34dc7c2f | 1171 | |
572e2857 BB |
1172 | kmem_free(itxs, sizeof (itxs_t)); |
1173 | } | |
34dc7c2f | 1174 | |
572e2857 BB |
1175 | static int |
1176 | zil_aitx_compare(const void *x1, const void *x2) | |
1177 | { | |
1178 | const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; | |
1179 | const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; | |
1180 | ||
1181 | if (o1 < o2) | |
1182 | return (-1); | |
1183 | if (o1 > o2) | |
1184 | return (1); | |
1185 | ||
1186 | return (0); | |
34dc7c2f BB |
1187 | } |
1188 | ||
1189 | /* | |
572e2857 | 1190 | * Remove all async itx with the given oid. |
34dc7c2f BB |
1191 | */ |
1192 | static void | |
572e2857 | 1193 | zil_remove_async(zilog_t *zilog, uint64_t oid) |
34dc7c2f | 1194 | { |
572e2857 BB |
1195 | uint64_t otxg, txg; |
1196 | itx_async_node_t *ian; | |
1197 | avl_tree_t *t; | |
1198 | avl_index_t where; | |
34dc7c2f BB |
1199 | list_t clean_list; |
1200 | itx_t *itx; | |
1201 | ||
572e2857 | 1202 | ASSERT(oid != 0); |
34dc7c2f BB |
1203 | list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); |
1204 | ||
572e2857 BB |
1205 | if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ |
1206 | otxg = ZILTEST_TXG; | |
1207 | else | |
1208 | otxg = spa_last_synced_txg(zilog->zl_spa) + 1; | |
34dc7c2f | 1209 | |
572e2857 BB |
1210 | for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { |
1211 | itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; | |
1212 | ||
1213 | mutex_enter(&itxg->itxg_lock); | |
1214 | if (itxg->itxg_txg != txg) { | |
1215 | mutex_exit(&itxg->itxg_lock); | |
1216 | continue; | |
1217 | } | |
34dc7c2f | 1218 | |
572e2857 BB |
1219 | /* |
1220 | * Locate the object node and append its list. | |
1221 | */ | |
1222 | t = &itxg->itxg_itxs->i_async_tree; | |
1223 | ian = avl_find(t, &oid, &where); | |
1224 | if (ian != NULL) | |
1225 | list_move_tail(&clean_list, &ian->ia_list); | |
1226 | mutex_exit(&itxg->itxg_lock); | |
1227 | } | |
34dc7c2f BB |
1228 | while ((itx = list_head(&clean_list)) != NULL) { |
1229 | list_remove(&clean_list, itx); | |
572e2857 BB |
1230 | kmem_free(itx, offsetof(itx_t, itx_lr) + |
1231 | itx->itx_lr.lrc_reclen); | |
34dc7c2f BB |
1232 | } |
1233 | list_destroy(&clean_list); | |
1234 | } | |
1235 | ||
572e2857 BB |
1236 | void |
1237 | zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) | |
1238 | { | |
1239 | uint64_t txg; | |
1240 | itxg_t *itxg; | |
1241 | itxs_t *itxs, *clean = NULL; | |
1242 | ||
1243 | /* | |
1244 | * Object ids can be re-instantiated in the next txg so | |
1245 | * remove any async transactions to avoid future leaks. | |
1246 | * This can happen if a fsync occurs on the re-instantiated | |
1247 | * object for a WR_INDIRECT or WR_NEED_COPY write, which gets | |
1248 | * the new file data and flushes a write record for the old object. | |
1249 | */ | |
1250 | if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) | |
1251 | zil_remove_async(zilog, itx->itx_oid); | |
1252 | ||
1253 | /* | |
1254 | * Ensure the data of a renamed file is committed before the rename. | |
1255 | */ | |
1256 | if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) | |
1257 | zil_async_to_sync(zilog, itx->itx_oid); | |
1258 | ||
1259 | if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) | |
1260 | txg = ZILTEST_TXG; | |
1261 | else | |
1262 | txg = dmu_tx_get_txg(tx); | |
1263 | ||
1264 | itxg = &zilog->zl_itxg[txg & TXG_MASK]; | |
1265 | mutex_enter(&itxg->itxg_lock); | |
1266 | itxs = itxg->itxg_itxs; | |
1267 | if (itxg->itxg_txg != txg) { | |
1268 | if (itxs != NULL) { | |
1269 | /* | |
1270 | * The zil_clean callback hasn't got around to cleaning | |
1271 | * this itxg. Save the itxs for release below. | |
1272 | * This should be rare. | |
1273 | */ | |
1274 | atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); | |
1275 | itxg->itxg_sod = 0; | |
1276 | clean = itxg->itxg_itxs; | |
1277 | } | |
1278 | ASSERT(itxg->itxg_sod == 0); | |
1279 | itxg->itxg_txg = txg; | |
1280 | itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); | |
1281 | ||
1282 | list_create(&itxs->i_sync_list, sizeof (itx_t), | |
1283 | offsetof(itx_t, itx_node)); | |
1284 | avl_create(&itxs->i_async_tree, zil_aitx_compare, | |
1285 | sizeof (itx_async_node_t), | |
1286 | offsetof(itx_async_node_t, ia_node)); | |
1287 | } | |
1288 | if (itx->itx_sync) { | |
1289 | list_insert_tail(&itxs->i_sync_list, itx); | |
1290 | atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod); | |
1291 | itxg->itxg_sod += itx->itx_sod; | |
1292 | } else { | |
1293 | avl_tree_t *t = &itxs->i_async_tree; | |
1294 | uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; | |
1295 | itx_async_node_t *ian; | |
1296 | avl_index_t where; | |
1297 | ||
1298 | ian = avl_find(t, &foid, &where); | |
1299 | if (ian == NULL) { | |
1300 | ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); | |
1301 | list_create(&ian->ia_list, sizeof (itx_t), | |
1302 | offsetof(itx_t, itx_node)); | |
1303 | ian->ia_foid = foid; | |
1304 | avl_insert(t, ian, where); | |
1305 | } | |
1306 | list_insert_tail(&ian->ia_list, itx); | |
1307 | } | |
1308 | ||
1309 | itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); | |
1310 | mutex_exit(&itxg->itxg_lock); | |
1311 | ||
1312 | /* Release the old itxs now we've dropped the lock */ | |
1313 | if (clean != NULL) | |
1314 | zil_itxg_clean(clean); | |
1315 | } | |
1316 | ||
34dc7c2f BB |
1317 | /* |
1318 | * If there are any in-memory intent log transactions which have now been | |
1319 | * synced then start up a taskq to free them. | |
1320 | */ | |
1321 | void | |
572e2857 | 1322 | zil_clean(zilog_t *zilog, uint64_t synced_txg) |
34dc7c2f | 1323 | { |
572e2857 BB |
1324 | itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; |
1325 | itxs_t *clean_me; | |
34dc7c2f | 1326 | |
572e2857 BB |
1327 | mutex_enter(&itxg->itxg_lock); |
1328 | if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { | |
1329 | mutex_exit(&itxg->itxg_lock); | |
1330 | return; | |
1331 | } | |
1332 | ASSERT3U(itxg->itxg_txg, <=, synced_txg); | |
1333 | ASSERT(itxg->itxg_txg != 0); | |
1334 | ASSERT(zilog->zl_clean_taskq != NULL); | |
1335 | atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); | |
1336 | itxg->itxg_sod = 0; | |
1337 | clean_me = itxg->itxg_itxs; | |
1338 | itxg->itxg_itxs = NULL; | |
1339 | itxg->itxg_txg = 0; | |
1340 | mutex_exit(&itxg->itxg_lock); | |
1341 | /* | |
1342 | * Preferably start a task queue to free up the old itxs but | |
1343 | * if taskq_dispatch can't allocate resources to do that then | |
1344 | * free it in-line. This should be rare. Note, using TQ_SLEEP | |
1345 | * created a bad performance problem. | |
1346 | */ | |
1347 | if (taskq_dispatch(zilog->zl_clean_taskq, | |
b8864a23 | 1348 | (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) |
572e2857 BB |
1349 | zil_itxg_clean(clean_me); |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Get the list of itxs to commit into zl_itx_commit_list. | |
1354 | */ | |
1355 | static void | |
1356 | zil_get_commit_list(zilog_t *zilog) | |
1357 | { | |
1358 | uint64_t otxg, txg; | |
1359 | list_t *commit_list = &zilog->zl_itx_commit_list; | |
1360 | uint64_t push_sod = 0; | |
1361 | ||
1362 | if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ | |
1363 | otxg = ZILTEST_TXG; | |
1364 | else | |
1365 | otxg = spa_last_synced_txg(zilog->zl_spa) + 1; | |
1366 | ||
1367 | for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { | |
1368 | itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; | |
1369 | ||
1370 | mutex_enter(&itxg->itxg_lock); | |
1371 | if (itxg->itxg_txg != txg) { | |
1372 | mutex_exit(&itxg->itxg_lock); | |
1373 | continue; | |
1374 | } | |
1375 | ||
1376 | list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); | |
1377 | push_sod += itxg->itxg_sod; | |
1378 | itxg->itxg_sod = 0; | |
1379 | ||
1380 | mutex_exit(&itxg->itxg_lock); | |
1381 | } | |
1382 | atomic_add_64(&zilog->zl_itx_list_sz, -push_sod); | |
1383 | } | |
1384 | ||
1385 | /* | |
1386 | * Move the async itxs for a specified object to commit into sync lists. | |
1387 | */ | |
1388 | static void | |
1389 | zil_async_to_sync(zilog_t *zilog, uint64_t foid) | |
1390 | { | |
1391 | uint64_t otxg, txg; | |
1392 | itx_async_node_t *ian; | |
1393 | avl_tree_t *t; | |
1394 | avl_index_t where; | |
1395 | ||
1396 | if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ | |
1397 | otxg = ZILTEST_TXG; | |
1398 | else | |
1399 | otxg = spa_last_synced_txg(zilog->zl_spa) + 1; | |
1400 | ||
1401 | for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { | |
1402 | itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; | |
1403 | ||
1404 | mutex_enter(&itxg->itxg_lock); | |
1405 | if (itxg->itxg_txg != txg) { | |
1406 | mutex_exit(&itxg->itxg_lock); | |
1407 | continue; | |
1408 | } | |
1409 | ||
1410 | /* | |
1411 | * If a foid is specified then find that node and append its | |
1412 | * list. Otherwise walk the tree appending all the lists | |
1413 | * to the sync list. We add to the end rather than the | |
1414 | * beginning to ensure the create has happened. | |
1415 | */ | |
1416 | t = &itxg->itxg_itxs->i_async_tree; | |
1417 | if (foid != 0) { | |
1418 | ian = avl_find(t, &foid, &where); | |
1419 | if (ian != NULL) { | |
1420 | list_move_tail(&itxg->itxg_itxs->i_sync_list, | |
1421 | &ian->ia_list); | |
1422 | } | |
1423 | } else { | |
1424 | void *cookie = NULL; | |
1425 | ||
1426 | while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { | |
1427 | list_move_tail(&itxg->itxg_itxs->i_sync_list, | |
1428 | &ian->ia_list); | |
1429 | list_destroy(&ian->ia_list); | |
1430 | kmem_free(ian, sizeof (itx_async_node_t)); | |
1431 | } | |
1432 | } | |
1433 | mutex_exit(&itxg->itxg_lock); | |
34dc7c2f | 1434 | } |
34dc7c2f BB |
1435 | } |
1436 | ||
b128c09f | 1437 | static void |
572e2857 | 1438 | zil_commit_writer(zilog_t *zilog) |
34dc7c2f BB |
1439 | { |
1440 | uint64_t txg; | |
572e2857 | 1441 | itx_t *itx; |
34dc7c2f | 1442 | lwb_t *lwb; |
572e2857 | 1443 | spa_t *spa = zilog->zl_spa; |
428870ff | 1444 | int error = 0; |
34dc7c2f | 1445 | |
b128c09f | 1446 | ASSERT(zilog->zl_root_zio == NULL); |
572e2857 BB |
1447 | |
1448 | mutex_exit(&zilog->zl_lock); | |
1449 | ||
1450 | zil_get_commit_list(zilog); | |
1451 | ||
1452 | /* | |
1453 | * Return if there's nothing to commit before we dirty the fs by | |
1454 | * calling zil_create(). | |
1455 | */ | |
1456 | if (list_head(&zilog->zl_itx_commit_list) == NULL) { | |
1457 | mutex_enter(&zilog->zl_lock); | |
1458 | return; | |
1459 | } | |
34dc7c2f BB |
1460 | |
1461 | if (zilog->zl_suspend) { | |
1462 | lwb = NULL; | |
1463 | } else { | |
1464 | lwb = list_tail(&zilog->zl_lwb_list); | |
572e2857 | 1465 | if (lwb == NULL) |
428870ff | 1466 | lwb = zil_create(zilog); |
34dc7c2f BB |
1467 | } |
1468 | ||
34dc7c2f | 1469 | DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); |
c65aa5b2 | 1470 | while ((itx = list_head(&zilog->zl_itx_commit_list))) { |
34dc7c2f BB |
1471 | txg = itx->itx_lr.lrc_txg; |
1472 | ASSERT(txg); | |
1473 | ||
572e2857 | 1474 | if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa)) |
34dc7c2f | 1475 | lwb = zil_lwb_commit(zilog, itx, lwb); |
572e2857 BB |
1476 | list_remove(&zilog->zl_itx_commit_list, itx); |
1477 | kmem_free(itx, offsetof(itx_t, itx_lr) | |
1478 | + itx->itx_lr.lrc_reclen); | |
34dc7c2f BB |
1479 | } |
1480 | DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); | |
34dc7c2f BB |
1481 | |
1482 | /* write the last block out */ | |
1483 | if (lwb != NULL && lwb->lwb_zio != NULL) | |
1484 | lwb = zil_lwb_write_start(zilog, lwb); | |
1485 | ||
34dc7c2f BB |
1486 | zilog->zl_cur_used = 0; |
1487 | ||
1488 | /* | |
1489 | * Wait if necessary for the log blocks to be on stable storage. | |
1490 | */ | |
1491 | if (zilog->zl_root_zio) { | |
428870ff | 1492 | error = zio_wait(zilog->zl_root_zio); |
b128c09f | 1493 | zilog->zl_root_zio = NULL; |
34dc7c2f BB |
1494 | zil_flush_vdevs(zilog); |
1495 | } | |
1496 | ||
428870ff | 1497 | if (error || lwb == NULL) |
34dc7c2f | 1498 | txg_wait_synced(zilog->zl_dmu_pool, 0); |
34dc7c2f BB |
1499 | |
1500 | mutex_enter(&zilog->zl_lock); | |
428870ff BB |
1501 | |
1502 | /* | |
1503 | * Remember the highest committed log sequence number for ztest. | |
1504 | * We only update this value when all the log writes succeeded, | |
1505 | * because ztest wants to ASSERT that it got the whole log chain. | |
1506 | */ | |
1507 | if (error == 0 && lwb != NULL) | |
1508 | zilog->zl_commit_lr_seq = zilog->zl_lr_seq; | |
34dc7c2f BB |
1509 | } |
1510 | ||
1511 | /* | |
572e2857 | 1512 | * Commit zfs transactions to stable storage. |
34dc7c2f | 1513 | * If foid is 0 push out all transactions, otherwise push only those |
572e2857 BB |
1514 | * for that object or might reference that object. |
1515 | * | |
1516 | * itxs are committed in batches. In a heavily stressed zil there will be | |
1517 | * a commit writer thread who is writing out a bunch of itxs to the log | |
1518 | * for a set of committing threads (cthreads) in the same batch as the writer. | |
1519 | * Those cthreads are all waiting on the same cv for that batch. | |
1520 | * | |
1521 | * There will also be a different and growing batch of threads that are | |
1522 | * waiting to commit (qthreads). When the committing batch completes | |
1523 | * a transition occurs such that the cthreads exit and the qthreads become | |
1524 | * cthreads. One of the new cthreads becomes the writer thread for the | |
1525 | * batch. Any new threads arriving become new qthreads. | |
1526 | * | |
1527 | * Only 2 condition variables are needed and there's no transition | |
1528 | * between the two cvs needed. They just flip-flop between qthreads | |
1529 | * and cthreads. | |
1530 | * | |
1531 | * Using this scheme we can efficiently wakeup up only those threads | |
1532 | * that have been committed. | |
34dc7c2f BB |
1533 | */ |
1534 | void | |
572e2857 | 1535 | zil_commit(zilog_t *zilog, uint64_t foid) |
34dc7c2f | 1536 | { |
572e2857 | 1537 | uint64_t mybatch; |
34dc7c2f | 1538 | |
572e2857 BB |
1539 | if (zilog->zl_sync == ZFS_SYNC_DISABLED) |
1540 | return; | |
34dc7c2f | 1541 | |
b6ad9671 ED |
1542 | ZIL_STAT_BUMP(zil_commit_count); |
1543 | ||
572e2857 BB |
1544 | /* move the async itxs for the foid to the sync queues */ |
1545 | zil_async_to_sync(zilog, foid); | |
34dc7c2f | 1546 | |
572e2857 BB |
1547 | mutex_enter(&zilog->zl_lock); |
1548 | mybatch = zilog->zl_next_batch; | |
34dc7c2f | 1549 | while (zilog->zl_writer) { |
572e2857 BB |
1550 | cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock); |
1551 | if (mybatch <= zilog->zl_com_batch) { | |
34dc7c2f BB |
1552 | mutex_exit(&zilog->zl_lock); |
1553 | return; | |
1554 | } | |
1555 | } | |
428870ff | 1556 | |
572e2857 BB |
1557 | zilog->zl_next_batch++; |
1558 | zilog->zl_writer = B_TRUE; | |
b6ad9671 | 1559 | ZIL_STAT_BUMP(zil_commit_writer_count); |
572e2857 BB |
1560 | zil_commit_writer(zilog); |
1561 | zilog->zl_com_batch = mybatch; | |
1562 | zilog->zl_writer = B_FALSE; | |
1563 | mutex_exit(&zilog->zl_lock); | |
428870ff | 1564 | |
572e2857 BB |
1565 | /* wake up one thread to become the next writer */ |
1566 | cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]); | |
428870ff | 1567 | |
572e2857 BB |
1568 | /* wake up all threads waiting for this batch to be committed */ |
1569 | cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]); | |
428870ff BB |
1570 | } |
1571 | ||
34dc7c2f BB |
1572 | /* |
1573 | * Called in syncing context to free committed log blocks and update log header. | |
1574 | */ | |
1575 | void | |
1576 | zil_sync(zilog_t *zilog, dmu_tx_t *tx) | |
1577 | { | |
1578 | zil_header_t *zh = zil_header_in_syncing_context(zilog); | |
1579 | uint64_t txg = dmu_tx_get_txg(tx); | |
1580 | spa_t *spa = zilog->zl_spa; | |
428870ff | 1581 | uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; |
34dc7c2f BB |
1582 | lwb_t *lwb; |
1583 | ||
9babb374 BB |
1584 | /* |
1585 | * We don't zero out zl_destroy_txg, so make sure we don't try | |
1586 | * to destroy it twice. | |
1587 | */ | |
1588 | if (spa_sync_pass(spa) != 1) | |
1589 | return; | |
1590 | ||
34dc7c2f BB |
1591 | mutex_enter(&zilog->zl_lock); |
1592 | ||
1593 | ASSERT(zilog->zl_stop_sync == 0); | |
1594 | ||
428870ff BB |
1595 | if (*replayed_seq != 0) { |
1596 | ASSERT(zh->zh_replay_seq < *replayed_seq); | |
1597 | zh->zh_replay_seq = *replayed_seq; | |
1598 | *replayed_seq = 0; | |
1599 | } | |
34dc7c2f BB |
1600 | |
1601 | if (zilog->zl_destroy_txg == txg) { | |
1602 | blkptr_t blk = zh->zh_log; | |
1603 | ||
1604 | ASSERT(list_head(&zilog->zl_lwb_list) == NULL); | |
34dc7c2f BB |
1605 | |
1606 | bzero(zh, sizeof (zil_header_t)); | |
fb5f0bc8 | 1607 | bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); |
34dc7c2f BB |
1608 | |
1609 | if (zilog->zl_keep_first) { | |
1610 | /* | |
1611 | * If this block was part of log chain that couldn't | |
1612 | * be claimed because a device was missing during | |
1613 | * zil_claim(), but that device later returns, | |
1614 | * then this block could erroneously appear valid. | |
1615 | * To guard against this, assign a new GUID to the new | |
1616 | * log chain so it doesn't matter what blk points to. | |
1617 | */ | |
1618 | zil_init_log_chain(zilog, &blk); | |
1619 | zh->zh_log = blk; | |
1620 | } | |
1621 | } | |
1622 | ||
9babb374 | 1623 | while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { |
34dc7c2f BB |
1624 | zh->zh_log = lwb->lwb_blk; |
1625 | if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) | |
1626 | break; | |
1627 | list_remove(&zilog->zl_lwb_list, lwb); | |
428870ff | 1628 | zio_free_zil(spa, txg, &lwb->lwb_blk); |
34dc7c2f BB |
1629 | kmem_cache_free(zil_lwb_cache, lwb); |
1630 | ||
1631 | /* | |
1632 | * If we don't have anything left in the lwb list then | |
1633 | * we've had an allocation failure and we need to zero | |
1634 | * out the zil_header blkptr so that we don't end | |
1635 | * up freeing the same block twice. | |
1636 | */ | |
1637 | if (list_head(&zilog->zl_lwb_list) == NULL) | |
1638 | BP_ZERO(&zh->zh_log); | |
1639 | } | |
1640 | mutex_exit(&zilog->zl_lock); | |
1641 | } | |
1642 | ||
1643 | void | |
1644 | zil_init(void) | |
1645 | { | |
1646 | zil_lwb_cache = kmem_cache_create("zil_lwb_cache", | |
1647 | sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); | |
b6ad9671 ED |
1648 | |
1649 | zil_ksp = kstat_create("zfs", 0, "zil", "misc", | |
1650 | KSTAT_TYPE_NAMED, sizeof(zil_stats) / sizeof(kstat_named_t), | |
1651 | KSTAT_FLAG_VIRTUAL); | |
1652 | ||
1653 | if (zil_ksp != NULL) { | |
1654 | zil_ksp->ks_data = &zil_stats; | |
1655 | kstat_install(zil_ksp); | |
1656 | } | |
34dc7c2f BB |
1657 | } |
1658 | ||
1659 | void | |
1660 | zil_fini(void) | |
1661 | { | |
1662 | kmem_cache_destroy(zil_lwb_cache); | |
b6ad9671 ED |
1663 | |
1664 | if (zil_ksp != NULL) { | |
1665 | kstat_delete(zil_ksp); | |
1666 | zil_ksp = NULL; | |
1667 | } | |
34dc7c2f BB |
1668 | } |
1669 | ||
428870ff BB |
1670 | void |
1671 | zil_set_sync(zilog_t *zilog, uint64_t sync) | |
1672 | { | |
1673 | zilog->zl_sync = sync; | |
1674 | } | |
1675 | ||
1676 | void | |
1677 | zil_set_logbias(zilog_t *zilog, uint64_t logbias) | |
1678 | { | |
1679 | zilog->zl_logbias = logbias; | |
1680 | } | |
1681 | ||
34dc7c2f BB |
1682 | zilog_t * |
1683 | zil_alloc(objset_t *os, zil_header_t *zh_phys) | |
1684 | { | |
1685 | zilog_t *zilog; | |
d6320ddb | 1686 | int i; |
34dc7c2f BB |
1687 | |
1688 | zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); | |
1689 | ||
1690 | zilog->zl_header = zh_phys; | |
1691 | zilog->zl_os = os; | |
1692 | zilog->zl_spa = dmu_objset_spa(os); | |
1693 | zilog->zl_dmu_pool = dmu_objset_pool(os); | |
1694 | zilog->zl_destroy_txg = TXG_INITIAL - 1; | |
428870ff BB |
1695 | zilog->zl_logbias = dmu_objset_logbias(os); |
1696 | zilog->zl_sync = dmu_objset_syncprop(os); | |
572e2857 | 1697 | zilog->zl_next_batch = 1; |
34dc7c2f BB |
1698 | |
1699 | mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); | |
1700 | ||
d6320ddb | 1701 | for (i = 0; i < TXG_SIZE; i++) { |
572e2857 BB |
1702 | mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, |
1703 | MUTEX_DEFAULT, NULL); | |
1704 | } | |
34dc7c2f BB |
1705 | |
1706 | list_create(&zilog->zl_lwb_list, sizeof (lwb_t), | |
1707 | offsetof(lwb_t, lwb_node)); | |
1708 | ||
572e2857 BB |
1709 | list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), |
1710 | offsetof(itx_t, itx_node)); | |
1711 | ||
34dc7c2f BB |
1712 | mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); |
1713 | ||
1714 | avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, | |
1715 | sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); | |
1716 | ||
1717 | cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); | |
1718 | cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); | |
572e2857 BB |
1719 | cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL); |
1720 | cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL); | |
34dc7c2f BB |
1721 | |
1722 | return (zilog); | |
1723 | } | |
1724 | ||
1725 | void | |
1726 | zil_free(zilog_t *zilog) | |
1727 | { | |
d6320ddb | 1728 | int i; |
34dc7c2f BB |
1729 | |
1730 | zilog->zl_stop_sync = 1; | |
1731 | ||
3e31d2b0 | 1732 | ASSERT(list_is_empty(&zilog->zl_lwb_list)); |
34dc7c2f BB |
1733 | list_destroy(&zilog->zl_lwb_list); |
1734 | ||
1735 | avl_destroy(&zilog->zl_vdev_tree); | |
1736 | mutex_destroy(&zilog->zl_vdev_lock); | |
1737 | ||
572e2857 BB |
1738 | ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); |
1739 | list_destroy(&zilog->zl_itx_commit_list); | |
1740 | ||
d6320ddb | 1741 | for (i = 0; i < TXG_SIZE; i++) { |
572e2857 BB |
1742 | /* |
1743 | * It's possible for an itx to be generated that doesn't dirty | |
1744 | * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() | |
1745 | * callback to remove the entry. We remove those here. | |
1746 | * | |
1747 | * Also free up the ziltest itxs. | |
1748 | */ | |
1749 | if (zilog->zl_itxg[i].itxg_itxs) | |
1750 | zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); | |
1751 | mutex_destroy(&zilog->zl_itxg[i].itxg_lock); | |
1752 | } | |
1753 | ||
34dc7c2f BB |
1754 | mutex_destroy(&zilog->zl_lock); |
1755 | ||
1756 | cv_destroy(&zilog->zl_cv_writer); | |
1757 | cv_destroy(&zilog->zl_cv_suspend); | |
572e2857 BB |
1758 | cv_destroy(&zilog->zl_cv_batch[0]); |
1759 | cv_destroy(&zilog->zl_cv_batch[1]); | |
34dc7c2f BB |
1760 | |
1761 | kmem_free(zilog, sizeof (zilog_t)); | |
1762 | } | |
1763 | ||
34dc7c2f BB |
1764 | /* |
1765 | * Open an intent log. | |
1766 | */ | |
1767 | zilog_t * | |
1768 | zil_open(objset_t *os, zil_get_data_t *get_data) | |
1769 | { | |
1770 | zilog_t *zilog = dmu_objset_zil(os); | |
1771 | ||
3e31d2b0 ES |
1772 | ASSERT(zilog->zl_clean_taskq == NULL); |
1773 | ASSERT(zilog->zl_get_data == NULL); | |
1774 | ASSERT(list_is_empty(&zilog->zl_lwb_list)); | |
1775 | ||
34dc7c2f BB |
1776 | zilog->zl_get_data = get_data; |
1777 | zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, | |
1778 | 2, 2, TASKQ_PREPOPULATE); | |
1779 | ||
1780 | return (zilog); | |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * Close an intent log. | |
1785 | */ | |
1786 | void | |
1787 | zil_close(zilog_t *zilog) | |
1788 | { | |
3e31d2b0 | 1789 | lwb_t *lwb; |
572e2857 BB |
1790 | uint64_t txg = 0; |
1791 | ||
1792 | zil_commit(zilog, 0); /* commit all itx */ | |
1793 | ||
34dc7c2f | 1794 | /* |
572e2857 BB |
1795 | * The lwb_max_txg for the stubby lwb will reflect the last activity |
1796 | * for the zil. After a txg_wait_synced() on the txg we know all the | |
1797 | * callbacks have occurred that may clean the zil. Only then can we | |
1798 | * destroy the zl_clean_taskq. | |
34dc7c2f | 1799 | */ |
572e2857 | 1800 | mutex_enter(&zilog->zl_lock); |
3e31d2b0 ES |
1801 | lwb = list_tail(&zilog->zl_lwb_list); |
1802 | if (lwb != NULL) | |
1803 | txg = lwb->lwb_max_txg; | |
572e2857 BB |
1804 | mutex_exit(&zilog->zl_lock); |
1805 | if (txg) | |
34dc7c2f | 1806 | txg_wait_synced(zilog->zl_dmu_pool, txg); |
34dc7c2f BB |
1807 | |
1808 | taskq_destroy(zilog->zl_clean_taskq); | |
1809 | zilog->zl_clean_taskq = NULL; | |
1810 | zilog->zl_get_data = NULL; | |
3e31d2b0 ES |
1811 | |
1812 | /* | |
1813 | * We should have only one LWB left on the list; remove it now. | |
1814 | */ | |
1815 | mutex_enter(&zilog->zl_lock); | |
1816 | lwb = list_head(&zilog->zl_lwb_list); | |
1817 | if (lwb != NULL) { | |
1818 | ASSERT(lwb == list_tail(&zilog->zl_lwb_list)); | |
1819 | list_remove(&zilog->zl_lwb_list, lwb); | |
1820 | zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); | |
1821 | kmem_cache_free(zil_lwb_cache, lwb); | |
1822 | } | |
1823 | mutex_exit(&zilog->zl_lock); | |
34dc7c2f BB |
1824 | } |
1825 | ||
1826 | /* | |
1827 | * Suspend an intent log. While in suspended mode, we still honor | |
1828 | * synchronous semantics, but we rely on txg_wait_synced() to do it. | |
1829 | * We suspend the log briefly when taking a snapshot so that the snapshot | |
1830 | * contains all the data it's supposed to, and has an empty intent log. | |
1831 | */ | |
1832 | int | |
1833 | zil_suspend(zilog_t *zilog) | |
1834 | { | |
1835 | const zil_header_t *zh = zilog->zl_header; | |
1836 | ||
1837 | mutex_enter(&zilog->zl_lock); | |
9babb374 | 1838 | if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ |
34dc7c2f BB |
1839 | mutex_exit(&zilog->zl_lock); |
1840 | return (EBUSY); | |
1841 | } | |
1842 | if (zilog->zl_suspend++ != 0) { | |
1843 | /* | |
1844 | * Someone else already began a suspend. | |
1845 | * Just wait for them to finish. | |
1846 | */ | |
1847 | while (zilog->zl_suspending) | |
1848 | cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); | |
34dc7c2f BB |
1849 | mutex_exit(&zilog->zl_lock); |
1850 | return (0); | |
1851 | } | |
1852 | zilog->zl_suspending = B_TRUE; | |
1853 | mutex_exit(&zilog->zl_lock); | |
1854 | ||
572e2857 | 1855 | zil_commit(zilog, 0); |
34dc7c2f BB |
1856 | |
1857 | zil_destroy(zilog, B_FALSE); | |
1858 | ||
1859 | mutex_enter(&zilog->zl_lock); | |
1860 | zilog->zl_suspending = B_FALSE; | |
1861 | cv_broadcast(&zilog->zl_cv_suspend); | |
1862 | mutex_exit(&zilog->zl_lock); | |
1863 | ||
1864 | return (0); | |
1865 | } | |
1866 | ||
1867 | void | |
1868 | zil_resume(zilog_t *zilog) | |
1869 | { | |
1870 | mutex_enter(&zilog->zl_lock); | |
1871 | ASSERT(zilog->zl_suspend != 0); | |
1872 | zilog->zl_suspend--; | |
1873 | mutex_exit(&zilog->zl_lock); | |
1874 | } | |
1875 | ||
1876 | typedef struct zil_replay_arg { | |
34dc7c2f BB |
1877 | zil_replay_func_t **zr_replay; |
1878 | void *zr_arg; | |
34dc7c2f | 1879 | boolean_t zr_byteswap; |
428870ff | 1880 | char *zr_lr; |
34dc7c2f BB |
1881 | } zil_replay_arg_t; |
1882 | ||
428870ff BB |
1883 | static int |
1884 | zil_replay_error(zilog_t *zilog, lr_t *lr, int error) | |
1885 | { | |
1886 | char name[MAXNAMELEN]; | |
1887 | ||
1888 | zilog->zl_replaying_seq--; /* didn't actually replay this one */ | |
1889 | ||
1890 | dmu_objset_name(zilog->zl_os, name); | |
1891 | ||
1892 | cmn_err(CE_WARN, "ZFS replay transaction error %d, " | |
1893 | "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, | |
1894 | (u_longlong_t)lr->lrc_seq, | |
1895 | (u_longlong_t)(lr->lrc_txtype & ~TX_CI), | |
1896 | (lr->lrc_txtype & TX_CI) ? "CI" : ""); | |
1897 | ||
1898 | return (error); | |
1899 | } | |
1900 | ||
1901 | static int | |
34dc7c2f BB |
1902 | zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) |
1903 | { | |
1904 | zil_replay_arg_t *zr = zra; | |
1905 | const zil_header_t *zh = zilog->zl_header; | |
1906 | uint64_t reclen = lr->lrc_reclen; | |
1907 | uint64_t txtype = lr->lrc_txtype; | |
428870ff | 1908 | int error = 0; |
34dc7c2f | 1909 | |
428870ff | 1910 | zilog->zl_replaying_seq = lr->lrc_seq; |
34dc7c2f BB |
1911 | |
1912 | if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ | |
428870ff BB |
1913 | return (0); |
1914 | ||
1915 | if (lr->lrc_txg < claim_txg) /* already committed */ | |
1916 | return (0); | |
34dc7c2f BB |
1917 | |
1918 | /* Strip case-insensitive bit, still present in log record */ | |
1919 | txtype &= ~TX_CI; | |
1920 | ||
428870ff BB |
1921 | if (txtype == 0 || txtype >= TX_MAX_TYPE) |
1922 | return (zil_replay_error(zilog, lr, EINVAL)); | |
1923 | ||
1924 | /* | |
1925 | * If this record type can be logged out of order, the object | |
1926 | * (lr_foid) may no longer exist. That's legitimate, not an error. | |
1927 | */ | |
1928 | if (TX_OOO(txtype)) { | |
1929 | error = dmu_object_info(zilog->zl_os, | |
1930 | ((lr_ooo_t *)lr)->lr_foid, NULL); | |
1931 | if (error == ENOENT || error == EEXIST) | |
1932 | return (0); | |
fb5f0bc8 BB |
1933 | } |
1934 | ||
34dc7c2f BB |
1935 | /* |
1936 | * Make a copy of the data so we can revise and extend it. | |
1937 | */ | |
428870ff BB |
1938 | bcopy(lr, zr->zr_lr, reclen); |
1939 | ||
1940 | /* | |
1941 | * If this is a TX_WRITE with a blkptr, suck in the data. | |
1942 | */ | |
1943 | if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { | |
1944 | error = zil_read_log_data(zilog, (lr_write_t *)lr, | |
1945 | zr->zr_lr + reclen); | |
1946 | if (error) | |
1947 | return (zil_replay_error(zilog, lr, error)); | |
1948 | } | |
34dc7c2f BB |
1949 | |
1950 | /* | |
1951 | * The log block containing this lr may have been byteswapped | |
1952 | * so that we can easily examine common fields like lrc_txtype. | |
428870ff | 1953 | * However, the log is a mix of different record types, and only the |
34dc7c2f BB |
1954 | * replay vectors know how to byteswap their records. Therefore, if |
1955 | * the lr was byteswapped, undo it before invoking the replay vector. | |
1956 | */ | |
1957 | if (zr->zr_byteswap) | |
428870ff | 1958 | byteswap_uint64_array(zr->zr_lr, reclen); |
34dc7c2f BB |
1959 | |
1960 | /* | |
1961 | * We must now do two things atomically: replay this log record, | |
fb5f0bc8 BB |
1962 | * and update the log header sequence number to reflect the fact that |
1963 | * we did so. At the end of each replay function the sequence number | |
1964 | * is updated if we are in replay mode. | |
34dc7c2f | 1965 | */ |
428870ff BB |
1966 | error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); |
1967 | if (error) { | |
34dc7c2f BB |
1968 | /* |
1969 | * The DMU's dnode layer doesn't see removes until the txg | |
1970 | * commits, so a subsequent claim can spuriously fail with | |
fb5f0bc8 | 1971 | * EEXIST. So if we receive any error we try syncing out |
428870ff BB |
1972 | * any removes then retry the transaction. Note that we |
1973 | * specify B_FALSE for byteswap now, so we don't do it twice. | |
34dc7c2f | 1974 | */ |
428870ff BB |
1975 | txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); |
1976 | error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); | |
1977 | if (error) | |
1978 | return (zil_replay_error(zilog, lr, error)); | |
34dc7c2f | 1979 | } |
428870ff | 1980 | return (0); |
34dc7c2f BB |
1981 | } |
1982 | ||
1983 | /* ARGSUSED */ | |
428870ff | 1984 | static int |
34dc7c2f BB |
1985 | zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) |
1986 | { | |
1987 | zilog->zl_replay_blks++; | |
428870ff BB |
1988 | |
1989 | return (0); | |
34dc7c2f BB |
1990 | } |
1991 | ||
1992 | /* | |
1993 | * If this dataset has a non-empty intent log, replay it and destroy it. | |
1994 | */ | |
1995 | void | |
fb5f0bc8 | 1996 | zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) |
34dc7c2f BB |
1997 | { |
1998 | zilog_t *zilog = dmu_objset_zil(os); | |
1999 | const zil_header_t *zh = zilog->zl_header; | |
2000 | zil_replay_arg_t zr; | |
2001 | ||
9babb374 | 2002 | if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { |
34dc7c2f BB |
2003 | zil_destroy(zilog, B_TRUE); |
2004 | return; | |
2005 | } | |
2006 | ||
34dc7c2f BB |
2007 | zr.zr_replay = replay_func; |
2008 | zr.zr_arg = arg; | |
34dc7c2f | 2009 | zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); |
00b46022 | 2010 | zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); |
34dc7c2f BB |
2011 | |
2012 | /* | |
2013 | * Wait for in-progress removes to sync before starting replay. | |
2014 | */ | |
2015 | txg_wait_synced(zilog->zl_dmu_pool, 0); | |
2016 | ||
fb5f0bc8 | 2017 | zilog->zl_replay = B_TRUE; |
428870ff | 2018 | zilog->zl_replay_time = ddi_get_lbolt(); |
34dc7c2f BB |
2019 | ASSERT(zilog->zl_replay_blks == 0); |
2020 | (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, | |
2021 | zh->zh_claim_txg); | |
00b46022 | 2022 | vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); |
34dc7c2f BB |
2023 | |
2024 | zil_destroy(zilog, B_FALSE); | |
2025 | txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); | |
fb5f0bc8 | 2026 | zilog->zl_replay = B_FALSE; |
34dc7c2f BB |
2027 | } |
2028 | ||
428870ff BB |
2029 | boolean_t |
2030 | zil_replaying(zilog_t *zilog, dmu_tx_t *tx) | |
34dc7c2f | 2031 | { |
428870ff BB |
2032 | if (zilog->zl_sync == ZFS_SYNC_DISABLED) |
2033 | return (B_TRUE); | |
34dc7c2f | 2034 | |
428870ff BB |
2035 | if (zilog->zl_replay) { |
2036 | dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); | |
2037 | zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = | |
2038 | zilog->zl_replaying_seq; | |
2039 | return (B_TRUE); | |
34dc7c2f BB |
2040 | } |
2041 | ||
428870ff | 2042 | return (B_FALSE); |
34dc7c2f | 2043 | } |
9babb374 BB |
2044 | |
2045 | /* ARGSUSED */ | |
2046 | int | |
428870ff | 2047 | zil_vdev_offline(const char *osname, void *arg) |
9babb374 BB |
2048 | { |
2049 | objset_t *os; | |
2050 | zilog_t *zilog; | |
2051 | int error; | |
2052 | ||
428870ff | 2053 | error = dmu_objset_hold(osname, FTAG, &os); |
9babb374 BB |
2054 | if (error) |
2055 | return (error); | |
2056 | ||
2057 | zilog = dmu_objset_zil(os); | |
2058 | if (zil_suspend(zilog) != 0) | |
2059 | error = EEXIST; | |
2060 | else | |
2061 | zil_resume(zilog); | |
428870ff | 2062 | dmu_objset_rele(os, FTAG); |
9babb374 BB |
2063 | return (error); |
2064 | } | |
c409e464 BB |
2065 | |
2066 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
2067 | module_param(zil_replay_disable, int, 0644); | |
2068 | MODULE_PARM_DESC(zil_replay_disable, "Disable intent logging replay"); | |
2069 | ||
2070 | module_param(zfs_nocacheflush, int, 0644); | |
2071 | MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes"); | |
ee191e80 ED |
2072 | |
2073 | module_param(zil_slog_limit, ulong, 0644); | |
2074 | MODULE_PARM_DESC(zil_slog_limit, "Max commit bytes to separate log device"); | |
c409e464 | 2075 | #endif |