1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51472Sperrin * Common Development and Distribution License (the "License").
61472Sperrin * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
2212294SMark.Musante@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
2512294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
2612294SMark.Musante@Sun.COM
27789Sahrens #include <sys/zfs_context.h>
28789Sahrens #include <sys/spa.h>
29789Sahrens #include <sys/dmu.h>
30789Sahrens #include <sys/zap.h>
31789Sahrens #include <sys/arc.h>
32789Sahrens #include <sys/stat.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/zil.h>
35789Sahrens #include <sys/zil_impl.h>
36789Sahrens #include <sys/dsl_dataset.h>
37*12949SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h>
383668Sgw25295 #include <sys/dmu_tx.h>
3912296SLin.Ling@Sun.COM #include <sys/dsl_pool.h>
40789Sahrens
41789Sahrens /*
42789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls
43789Sahrens * that change the file system in memory with enough information
44789Sahrens * to be able to replay them. These are stored in memory until
45789Sahrens * either the DMU transaction group (txg) commits them to the stable pool
46789Sahrens * and they can be discarded, or they are flushed to the stable log
47789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous
48789Sahrens * requirement. In the event of a panic or power fail then those log
49789Sahrens * records (transactions) are replayed.
50789Sahrens *
51789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists
52789Sahrens * of 3 parts:
53789Sahrens *
54789Sahrens * - ZIL header
55789Sahrens * - ZIL blocks
56789Sahrens * - ZIL records
57789Sahrens *
58789Sahrens * A log record holds a system call transaction. Log blocks can
59789Sahrens * hold many log records and the blocks are chained together.
60789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next
61789Sahrens * ZIL block in the chain. The ZIL header points to the first
62789Sahrens * block in the chain. Note there is not a fixed place in the pool
63789Sahrens * to hold blocks. They are dynamically allocated and freed as
64789Sahrens * needed from the blocks available. Figure X shows the ZIL structure:
65789Sahrens */
66789Sahrens
67789Sahrens /*
682986Sek110237 * This global ZIL switch affects all pools
69789Sahrens */
7012294SMark.Musante@Sun.COM int zil_replay_disable = 0; /* disable intent logging replay */
712986Sek110237
722986Sek110237 /*
732986Sek110237 * Tunable parameter for debugging or performance analysis. Setting
742986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile
752986Sek110237 * out-of-order write cache is enabled.
762986Sek110237 */
772986Sek110237 boolean_t zfs_nocacheflush = B_FALSE;
78789Sahrens
79789Sahrens static kmem_cache_t *zil_lwb_cache;
80789Sahrens
8112771SNeil.Perrin@Sun.COM static void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
8210685SGeorge.Wilson@Sun.COM
8311670SNeil.Perrin@Sun.COM #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
8411670SNeil.Perrin@Sun.COM sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
8511670SNeil.Perrin@Sun.COM
8611670SNeil.Perrin@Sun.COM
8712699SNeil.Perrin@Sun.COM /*
8812699SNeil.Perrin@Sun.COM * ziltest is by and large an ugly hack, but very useful in
8912699SNeil.Perrin@Sun.COM * checking replay without tedious work.
9012699SNeil.Perrin@Sun.COM * When running ziltest we want to keep all itx's and so maintain
9112699SNeil.Perrin@Sun.COM * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
9212699SNeil.Perrin@Sun.COM * We subtract TXG_CONCURRENT_STATES to allow for common code.
9312699SNeil.Perrin@Sun.COM */
9412699SNeil.Perrin@Sun.COM #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
9512699SNeil.Perrin@Sun.COM
96789Sahrens static int
zil_bp_compare(const void * x1,const void * x2)9710922SJeff.Bonwick@Sun.COM zil_bp_compare(const void *x1, const void *x2)
98789Sahrens {
9910922SJeff.Bonwick@Sun.COM const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
10010922SJeff.Bonwick@Sun.COM const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
101789Sahrens
102789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
103789Sahrens return (-1);
104789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
105789Sahrens return (1);
106789Sahrens
107789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
108789Sahrens return (-1);
109789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
110789Sahrens return (1);
111789Sahrens
112789Sahrens return (0);
113789Sahrens }
114789Sahrens
115789Sahrens static void
zil_bp_tree_init(zilog_t * zilog)11610922SJeff.Bonwick@Sun.COM zil_bp_tree_init(zilog_t *zilog)
117789Sahrens {
11810922SJeff.Bonwick@Sun.COM avl_create(&zilog->zl_bp_tree, zil_bp_compare,
11910922SJeff.Bonwick@Sun.COM sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
120789Sahrens }
121789Sahrens
122789Sahrens static void
zil_bp_tree_fini(zilog_t * zilog)12310922SJeff.Bonwick@Sun.COM zil_bp_tree_fini(zilog_t *zilog)
124789Sahrens {
12510922SJeff.Bonwick@Sun.COM avl_tree_t *t = &zilog->zl_bp_tree;
12610922SJeff.Bonwick@Sun.COM zil_bp_node_t *zn;
127789Sahrens void *cookie = NULL;
128789Sahrens
129789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
13010922SJeff.Bonwick@Sun.COM kmem_free(zn, sizeof (zil_bp_node_t));
131789Sahrens
132789Sahrens avl_destroy(t);
133789Sahrens }
134789Sahrens
13510922SJeff.Bonwick@Sun.COM int
zil_bp_tree_add(zilog_t * zilog,const blkptr_t * bp)13610922SJeff.Bonwick@Sun.COM zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
137789Sahrens {
13810922SJeff.Bonwick@Sun.COM avl_tree_t *t = &zilog->zl_bp_tree;
13910922SJeff.Bonwick@Sun.COM const dva_t *dva = BP_IDENTITY(bp);
14010922SJeff.Bonwick@Sun.COM zil_bp_node_t *zn;
141789Sahrens avl_index_t where;
142789Sahrens
143789Sahrens if (avl_find(t, dva, &where) != NULL)
144789Sahrens return (EEXIST);
145789Sahrens
14610922SJeff.Bonwick@Sun.COM zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
147789Sahrens zn->zn_dva = *dva;
148789Sahrens avl_insert(t, zn, where);
149789Sahrens
150789Sahrens return (0);
151789Sahrens }
152789Sahrens
1531807Sbonwick static zil_header_t *
zil_header_in_syncing_context(zilog_t * zilog)1541807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1551807Sbonwick {
1561807Sbonwick return ((zil_header_t *)zilog->zl_header);
1571807Sbonwick }
1581807Sbonwick
1591807Sbonwick static void
zil_init_log_chain(zilog_t * zilog,blkptr_t * bp)1601807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1611807Sbonwick {
1621807Sbonwick zio_cksum_t *zc = &bp->blk_cksum;
1631807Sbonwick
1641807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1651807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1661807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1671807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1681807Sbonwick }
1691807Sbonwick
170789Sahrens /*
17110922SJeff.Bonwick@Sun.COM * Read a log block and make sure it's valid.
172789Sahrens */
173789Sahrens static int
zil_read_log_block(zilog_t * zilog,const blkptr_t * bp,blkptr_t * nbp,void * dst,char ** end)17411670SNeil.Perrin@Sun.COM zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
17511670SNeil.Perrin@Sun.COM char **end)
176789Sahrens {
17710922SJeff.Bonwick@Sun.COM enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
17810922SJeff.Bonwick@Sun.COM uint32_t aflags = ARC_WAIT;
17910922SJeff.Bonwick@Sun.COM arc_buf_t *abuf = NULL;
1801544Seschrock zbookmark_t zb;
181789Sahrens int error;
182789Sahrens
18310922SJeff.Bonwick@Sun.COM if (zilog->zl_header->zh_claim_txg == 0)
18410922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
1851807Sbonwick
18610922SJeff.Bonwick@Sun.COM if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
18710922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE;
1881807Sbonwick
18910922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
19010922SJeff.Bonwick@Sun.COM ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
19110922SJeff.Bonwick@Sun.COM
19212296SLin.Ling@Sun.COM error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
19310922SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
1941807Sbonwick
1951807Sbonwick if (error == 0) {
1961807Sbonwick zio_cksum_t cksum = bp->blk_cksum;
1971544Seschrock
1981807Sbonwick /*
1997522SNeil.Perrin@Sun.COM * Validate the checksummed log block.
2007522SNeil.Perrin@Sun.COM *
2011807Sbonwick * Sequence numbers should be... sequential. The checksum
2021807Sbonwick * verifier for the next block should be bp's checksum plus 1.
2037522SNeil.Perrin@Sun.COM *
2047522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used.
2051807Sbonwick */
2061807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++;
2071807Sbonwick
20811670SNeil.Perrin@Sun.COM if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
20911670SNeil.Perrin@Sun.COM zil_chain_t *zilc = abuf->b_data;
21011670SNeil.Perrin@Sun.COM char *lr = (char *)(zilc + 1);
21111670SNeil.Perrin@Sun.COM uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
21211670SNeil.Perrin@Sun.COM
21311670SNeil.Perrin@Sun.COM if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
21411670SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
21511670SNeil.Perrin@Sun.COM error = ECKSUM;
21611670SNeil.Perrin@Sun.COM } else {
21711670SNeil.Perrin@Sun.COM bcopy(lr, dst, len);
21811670SNeil.Perrin@Sun.COM *end = (char *)dst + len;
21911670SNeil.Perrin@Sun.COM *nbp = zilc->zc_next_blk;
22011670SNeil.Perrin@Sun.COM }
22111670SNeil.Perrin@Sun.COM } else {
22211670SNeil.Perrin@Sun.COM char *lr = abuf->b_data;
22311670SNeil.Perrin@Sun.COM uint64_t size = BP_GET_LSIZE(bp);
22411670SNeil.Perrin@Sun.COM zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
22511670SNeil.Perrin@Sun.COM
22611670SNeil.Perrin@Sun.COM if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
22711670SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
22811670SNeil.Perrin@Sun.COM (zilc->zc_nused > (size - sizeof (*zilc)))) {
22911670SNeil.Perrin@Sun.COM error = ECKSUM;
23011670SNeil.Perrin@Sun.COM } else {
23111670SNeil.Perrin@Sun.COM bcopy(lr, dst, zilc->zc_nused);
23211670SNeil.Perrin@Sun.COM *end = (char *)dst + zilc->zc_nused;
23311670SNeil.Perrin@Sun.COM *nbp = zilc->zc_next_blk;
23411670SNeil.Perrin@Sun.COM }
23511670SNeil.Perrin@Sun.COM }
2361807Sbonwick
23710922SJeff.Bonwick@Sun.COM VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
238789Sahrens }
239789Sahrens
24010922SJeff.Bonwick@Sun.COM return (error);
24110922SJeff.Bonwick@Sun.COM }
24210922SJeff.Bonwick@Sun.COM
24310922SJeff.Bonwick@Sun.COM /*
24410922SJeff.Bonwick@Sun.COM * Read a TX_WRITE log data block.
24510922SJeff.Bonwick@Sun.COM */
24610922SJeff.Bonwick@Sun.COM static int
zil_read_log_data(zilog_t * zilog,const lr_write_t * lr,void * wbuf)24710922SJeff.Bonwick@Sun.COM zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
24810922SJeff.Bonwick@Sun.COM {
24910922SJeff.Bonwick@Sun.COM enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
25010922SJeff.Bonwick@Sun.COM const blkptr_t *bp = &lr->lr_blkptr;
25110922SJeff.Bonwick@Sun.COM uint32_t aflags = ARC_WAIT;
25210922SJeff.Bonwick@Sun.COM arc_buf_t *abuf = NULL;
25310922SJeff.Bonwick@Sun.COM zbookmark_t zb;
25410922SJeff.Bonwick@Sun.COM int error;
25510922SJeff.Bonwick@Sun.COM
25610922SJeff.Bonwick@Sun.COM if (BP_IS_HOLE(bp)) {
25710922SJeff.Bonwick@Sun.COM if (wbuf != NULL)
25810922SJeff.Bonwick@Sun.COM bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
25910922SJeff.Bonwick@Sun.COM return (0);
26010922SJeff.Bonwick@Sun.COM }
26110922SJeff.Bonwick@Sun.COM
26210922SJeff.Bonwick@Sun.COM if (zilog->zl_header->zh_claim_txg == 0)
26310922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
26410922SJeff.Bonwick@Sun.COM
26510922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
26610922SJeff.Bonwick@Sun.COM ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
26710922SJeff.Bonwick@Sun.COM
26810922SJeff.Bonwick@Sun.COM error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
26910922SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
27010922SJeff.Bonwick@Sun.COM
27110922SJeff.Bonwick@Sun.COM if (error == 0) {
27210922SJeff.Bonwick@Sun.COM if (wbuf != NULL)
27310922SJeff.Bonwick@Sun.COM bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
27410922SJeff.Bonwick@Sun.COM (void) arc_buf_remove_ref(abuf, &abuf);
27510922SJeff.Bonwick@Sun.COM }
276789Sahrens
2771807Sbonwick return (error);
278789Sahrens }
279789Sahrens
280789Sahrens /*
281789Sahrens * Parse the intent log, and call parse_func for each valid record within.
282789Sahrens */
28310922SJeff.Bonwick@Sun.COM int
zil_parse(zilog_t * zilog,zil_parse_blk_func_t * parse_blk_func,zil_parse_lr_func_t * parse_lr_func,void * arg,uint64_t txg)284789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
285789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
286789Sahrens {
2871807Sbonwick const zil_header_t *zh = zilog->zl_header;
28810922SJeff.Bonwick@Sun.COM boolean_t claimed = !!zh->zh_claim_txg;
28910922SJeff.Bonwick@Sun.COM uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
29010922SJeff.Bonwick@Sun.COM uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
29110922SJeff.Bonwick@Sun.COM uint64_t max_blk_seq = 0;
29210922SJeff.Bonwick@Sun.COM uint64_t max_lr_seq = 0;
29310922SJeff.Bonwick@Sun.COM uint64_t blk_count = 0;
29410922SJeff.Bonwick@Sun.COM uint64_t lr_count = 0;
29510922SJeff.Bonwick@Sun.COM blkptr_t blk, next_blk;
296789Sahrens char *lrbuf, *lrp;
29710922SJeff.Bonwick@Sun.COM int error = 0;
298789Sahrens
29910922SJeff.Bonwick@Sun.COM /*
30010922SJeff.Bonwick@Sun.COM * Old logs didn't record the maximum zh_claim_lr_seq.
30110922SJeff.Bonwick@Sun.COM */
30210922SJeff.Bonwick@Sun.COM if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
30310922SJeff.Bonwick@Sun.COM claim_lr_seq = UINT64_MAX;
304789Sahrens
305789Sahrens /*
306789Sahrens * Starting at the block pointed to by zh_log we read the log chain.
307789Sahrens * For each block in the chain we strongly check that block to
308789Sahrens * ensure its validity. We stop when an invalid block is found.
309789Sahrens * For each block pointer in the chain we call parse_blk_func().
310789Sahrens * For each record in each valid block we call parse_lr_func().
3111807Sbonwick * If the log has been claimed, stop if we encounter a sequence
3121807Sbonwick * number greater than the highest claimed sequence number.
313789Sahrens */
31410922SJeff.Bonwick@Sun.COM lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
31510922SJeff.Bonwick@Sun.COM zil_bp_tree_init(zilog);
31610922SJeff.Bonwick@Sun.COM
31710922SJeff.Bonwick@Sun.COM for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
31810922SJeff.Bonwick@Sun.COM uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
31910922SJeff.Bonwick@Sun.COM int reclen;
32011670SNeil.Perrin@Sun.COM char *end;
3211807Sbonwick
32210922SJeff.Bonwick@Sun.COM if (blk_seq > claim_blk_seq)
32310922SJeff.Bonwick@Sun.COM break;
32410922SJeff.Bonwick@Sun.COM if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
32510922SJeff.Bonwick@Sun.COM break;
32611670SNeil.Perrin@Sun.COM ASSERT3U(max_blk_seq, <, blk_seq);
32710922SJeff.Bonwick@Sun.COM max_blk_seq = blk_seq;
32810922SJeff.Bonwick@Sun.COM blk_count++;
32910922SJeff.Bonwick@Sun.COM
33010922SJeff.Bonwick@Sun.COM if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
3311807Sbonwick break;
3321807Sbonwick
33311670SNeil.Perrin@Sun.COM error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
334789Sahrens if (error)
335789Sahrens break;
336789Sahrens
33711670SNeil.Perrin@Sun.COM for (lrp = lrbuf; lrp < end; lrp += reclen) {
338789Sahrens lr_t *lr = (lr_t *)lrp;
339789Sahrens reclen = lr->lrc_reclen;
340789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t));
34110922SJeff.Bonwick@Sun.COM if (lr->lrc_seq > claim_lr_seq)
34210922SJeff.Bonwick@Sun.COM goto done;
34310922SJeff.Bonwick@Sun.COM if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
34410922SJeff.Bonwick@Sun.COM goto done;
34511670SNeil.Perrin@Sun.COM ASSERT3U(max_lr_seq, <, lr->lrc_seq);
34610922SJeff.Bonwick@Sun.COM max_lr_seq = lr->lrc_seq;
34710922SJeff.Bonwick@Sun.COM lr_count++;
348789Sahrens }
349789Sahrens }
35010922SJeff.Bonwick@Sun.COM done:
35110922SJeff.Bonwick@Sun.COM zilog->zl_parse_error = error;
35210922SJeff.Bonwick@Sun.COM zilog->zl_parse_blk_seq = max_blk_seq;
35310922SJeff.Bonwick@Sun.COM zilog->zl_parse_lr_seq = max_lr_seq;
35410922SJeff.Bonwick@Sun.COM zilog->zl_parse_blk_count = blk_count;
35510922SJeff.Bonwick@Sun.COM zilog->zl_parse_lr_count = lr_count;
35610922SJeff.Bonwick@Sun.COM
35710922SJeff.Bonwick@Sun.COM ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
35810922SJeff.Bonwick@Sun.COM (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
35910922SJeff.Bonwick@Sun.COM
36010922SJeff.Bonwick@Sun.COM zil_bp_tree_fini(zilog);
36110922SJeff.Bonwick@Sun.COM zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
36210922SJeff.Bonwick@Sun.COM
36310922SJeff.Bonwick@Sun.COM return (error);
36410922SJeff.Bonwick@Sun.COM }
36510922SJeff.Bonwick@Sun.COM
36610922SJeff.Bonwick@Sun.COM static int
zil_claim_log_block(zilog_t * zilog,blkptr_t * bp,void * tx,uint64_t first_txg)36710922SJeff.Bonwick@Sun.COM zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
36810922SJeff.Bonwick@Sun.COM {
36910922SJeff.Bonwick@Sun.COM /*
37010922SJeff.Bonwick@Sun.COM * Claim log block if not already committed and not already claimed.
37110922SJeff.Bonwick@Sun.COM * If tx == NULL, just verify that the block is claimable.
37210922SJeff.Bonwick@Sun.COM */
37310922SJeff.Bonwick@Sun.COM if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0)
37410922SJeff.Bonwick@Sun.COM return (0);
3751807Sbonwick
37610922SJeff.Bonwick@Sun.COM return (zio_wait(zio_claim(NULL, zilog->zl_spa,
37710922SJeff.Bonwick@Sun.COM tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
37810922SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
37910922SJeff.Bonwick@Sun.COM }
38010922SJeff.Bonwick@Sun.COM
38110922SJeff.Bonwick@Sun.COM static int
zil_claim_log_record(zilog_t * zilog,lr_t * lrc,void * tx,uint64_t first_txg)38210922SJeff.Bonwick@Sun.COM zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
38310922SJeff.Bonwick@Sun.COM {
38410922SJeff.Bonwick@Sun.COM lr_write_t *lr = (lr_write_t *)lrc;
38510922SJeff.Bonwick@Sun.COM int error;
38610922SJeff.Bonwick@Sun.COM
38710922SJeff.Bonwick@Sun.COM if (lrc->lrc_txtype != TX_WRITE)
38810922SJeff.Bonwick@Sun.COM return (0);
38910922SJeff.Bonwick@Sun.COM
39010922SJeff.Bonwick@Sun.COM /*
39110922SJeff.Bonwick@Sun.COM * If the block is not readable, don't claim it. This can happen
39210922SJeff.Bonwick@Sun.COM * in normal operation when a log block is written to disk before
39310922SJeff.Bonwick@Sun.COM * some of the dmu_sync() blocks it points to. In this case, the
39410922SJeff.Bonwick@Sun.COM * transaction cannot have been committed to anyone (we would have
39510922SJeff.Bonwick@Sun.COM * waited for all writes to be stable first), so it is semantically
39610922SJeff.Bonwick@Sun.COM * correct to declare this the end of the log.
39710922SJeff.Bonwick@Sun.COM */
39810922SJeff.Bonwick@Sun.COM if (lr->lr_blkptr.blk_birth >= first_txg &&
39910922SJeff.Bonwick@Sun.COM (error = zil_read_log_data(zilog, lr, NULL)) != 0)
40010922SJeff.Bonwick@Sun.COM return (error);
40110922SJeff.Bonwick@Sun.COM return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
402789Sahrens }
403789Sahrens
404789Sahrens /* ARGSUSED */
40510922SJeff.Bonwick@Sun.COM static int
zil_free_log_block(zilog_t * zilog,blkptr_t * bp,void * tx,uint64_t claim_txg)40610922SJeff.Bonwick@Sun.COM zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
407789Sahrens {
40810922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
409789Sahrens
41010922SJeff.Bonwick@Sun.COM return (0);
411789Sahrens }
412789Sahrens
41310922SJeff.Bonwick@Sun.COM static int
zil_free_log_record(zilog_t * zilog,lr_t * lrc,void * tx,uint64_t claim_txg)414789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
415789Sahrens {
41610922SJeff.Bonwick@Sun.COM lr_write_t *lr = (lr_write_t *)lrc;
41710922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr;
41810922SJeff.Bonwick@Sun.COM
419789Sahrens /*
420789Sahrens * If we previously claimed it, we need to free it.
421789Sahrens */
42210922SJeff.Bonwick@Sun.COM if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
42310922SJeff.Bonwick@Sun.COM bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0)
42410922SJeff.Bonwick@Sun.COM zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
42510922SJeff.Bonwick@Sun.COM
42610922SJeff.Bonwick@Sun.COM return (0);
427789Sahrens }
428789Sahrens
42911670SNeil.Perrin@Sun.COM static lwb_t *
zil_alloc_lwb(zilog_t * zilog,blkptr_t * bp,uint64_t txg)43011670SNeil.Perrin@Sun.COM zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
43111670SNeil.Perrin@Sun.COM {
43211670SNeil.Perrin@Sun.COM lwb_t *lwb;
43311670SNeil.Perrin@Sun.COM
43411670SNeil.Perrin@Sun.COM lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
43511670SNeil.Perrin@Sun.COM lwb->lwb_zilog = zilog;
43611670SNeil.Perrin@Sun.COM lwb->lwb_blk = *bp;
43711670SNeil.Perrin@Sun.COM lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
43811670SNeil.Perrin@Sun.COM lwb->lwb_max_txg = txg;
43911670SNeil.Perrin@Sun.COM lwb->lwb_zio = NULL;
44011670SNeil.Perrin@Sun.COM lwb->lwb_tx = NULL;
44111670SNeil.Perrin@Sun.COM if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
44211670SNeil.Perrin@Sun.COM lwb->lwb_nused = sizeof (zil_chain_t);
44311670SNeil.Perrin@Sun.COM lwb->lwb_sz = BP_GET_LSIZE(bp);
44411670SNeil.Perrin@Sun.COM } else {
44511670SNeil.Perrin@Sun.COM lwb->lwb_nused = 0;
44611670SNeil.Perrin@Sun.COM lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
44711670SNeil.Perrin@Sun.COM }
44811670SNeil.Perrin@Sun.COM
44911670SNeil.Perrin@Sun.COM mutex_enter(&zilog->zl_lock);
45011670SNeil.Perrin@Sun.COM list_insert_tail(&zilog->zl_lwb_list, lwb);
45111670SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock);
45211670SNeil.Perrin@Sun.COM
45311670SNeil.Perrin@Sun.COM return (lwb);
45411670SNeil.Perrin@Sun.COM }
45511670SNeil.Perrin@Sun.COM
456789Sahrens /*
457789Sahrens * Create an on-disk intent log.
458789Sahrens */
45911670SNeil.Perrin@Sun.COM static lwb_t *
zil_create(zilog_t * zilog)460789Sahrens zil_create(zilog_t *zilog)
461789Sahrens {
4621807Sbonwick const zil_header_t *zh = zilog->zl_header;
46311670SNeil.Perrin@Sun.COM lwb_t *lwb = NULL;
4641807Sbonwick uint64_t txg = 0;
4651807Sbonwick dmu_tx_t *tx = NULL;
466789Sahrens blkptr_t blk;
4671807Sbonwick int error = 0;
468789Sahrens
469789Sahrens /*
4701807Sbonwick * Wait for any previous destroy to complete.
471789Sahrens */
4721807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
4731807Sbonwick
4741807Sbonwick ASSERT(zh->zh_claim_txg == 0);
4751807Sbonwick ASSERT(zh->zh_replay_seq == 0);
4761807Sbonwick
4771807Sbonwick blk = zh->zh_log;
478789Sahrens
479789Sahrens /*
48011670SNeil.Perrin@Sun.COM * Allocate an initial log block if:
48111670SNeil.Perrin@Sun.COM * - there isn't one already
48211670SNeil.Perrin@Sun.COM * - the existing block is the wrong endianess
483789Sahrens */
4848109SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
4851807Sbonwick tx = dmu_tx_create(zilog->zl_os);
48610922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
4871807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
4881807Sbonwick txg = dmu_tx_get_txg(tx);
4891807Sbonwick
4908109SNeil.Perrin@Sun.COM if (!BP_IS_HOLE(&blk)) {
49110922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, txg, &blk);
4928109SNeil.Perrin@Sun.COM BP_ZERO(&blk);
4938109SNeil.Perrin@Sun.COM }
4948109SNeil.Perrin@Sun.COM
49510922SJeff.Bonwick@Sun.COM error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
49610922SJeff.Bonwick@Sun.COM ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
4971807Sbonwick
4981807Sbonwick if (error == 0)
4991807Sbonwick zil_init_log_chain(zilog, &blk);
5001362Sperrin }
5011807Sbonwick
5021807Sbonwick /*
5031807Sbonwick * Allocate a log write buffer (lwb) for the first log block.
5041807Sbonwick */
50511670SNeil.Perrin@Sun.COM if (error == 0)
50611670SNeil.Perrin@Sun.COM lwb = zil_alloc_lwb(zilog, &blk, txg);
507789Sahrens
5081807Sbonwick /*
5091807Sbonwick * If we just allocated the first log block, commit our transaction
5101807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log.
5111807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.)
5121807Sbonwick */
5131807Sbonwick if (tx != NULL) {
5141807Sbonwick dmu_tx_commit(tx);
5151362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg);
5161807Sbonwick }
5171807Sbonwick
5181807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
51911670SNeil.Perrin@Sun.COM
52011670SNeil.Perrin@Sun.COM return (lwb);
521789Sahrens }
522789Sahrens
523789Sahrens /*
524789Sahrens * In one tx, free all log blocks and clear the log header.
5251807Sbonwick * If keep_first is set, then we're replaying a log with no content.
5261807Sbonwick * We want to keep the first block, however, so that the first
5271807Sbonwick * synchronous transaction doesn't require a txg_wait_synced()
5281807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either
5291807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy()
5301807Sbonwick * will wait for any in-progress destroys to complete.
531789Sahrens */
532789Sahrens void
zil_destroy(zilog_t * zilog,boolean_t keep_first)5331807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
534789Sahrens {
5351807Sbonwick const zil_header_t *zh = zilog->zl_header;
5361807Sbonwick lwb_t *lwb;
537789Sahrens dmu_tx_t *tx;
538789Sahrens uint64_t txg;
539789Sahrens
5401807Sbonwick /*
5411807Sbonwick * Wait for any previous destroy to complete.
5421807Sbonwick */
5431807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
544789Sahrens
54510922SJeff.Bonwick@Sun.COM zilog->zl_old_header = *zh; /* debugging aid */
54610922SJeff.Bonwick@Sun.COM
5471807Sbonwick if (BP_IS_HOLE(&zh->zh_log))
548789Sahrens return;
549789Sahrens
550789Sahrens tx = dmu_tx_create(zilog->zl_os);
55110922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
552789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
553789Sahrens txg = dmu_tx_get_txg(tx);
554789Sahrens
5551807Sbonwick mutex_enter(&zilog->zl_lock);
5561807Sbonwick
5571807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg);
558789Sahrens zilog->zl_destroy_txg = txg;
55910922SJeff.Bonwick@Sun.COM zilog->zl_keep_first = keep_first;
5601807Sbonwick
5611807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) {
5621807Sbonwick ASSERT(zh->zh_claim_txg == 0);
56310922SJeff.Bonwick@Sun.COM ASSERT(!keep_first);
5641807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
5651807Sbonwick list_remove(&zilog->zl_lwb_list, lwb);
5661807Sbonwick if (lwb->lwb_buf != NULL)
5671807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
56810922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
5691807Sbonwick kmem_cache_free(zil_lwb_cache, lwb);
5701807Sbonwick }
57110922SJeff.Bonwick@Sun.COM } else if (!keep_first) {
57210922SJeff.Bonwick@Sun.COM (void) zil_parse(zilog, zil_free_log_block,
57310922SJeff.Bonwick@Sun.COM zil_free_log_record, tx, zh->zh_claim_txg);
5741807Sbonwick }
5752638Sperrin mutex_exit(&zilog->zl_lock);
576789Sahrens
577789Sahrens dmu_tx_commit(tx);
578789Sahrens }
579789Sahrens
5802199Sahrens int
zil_claim(const char * osname,void * txarg)58111209SMatthew.Ahrens@Sun.COM zil_claim(const char *osname, void *txarg)
582789Sahrens {
583789Sahrens dmu_tx_t *tx = txarg;
584789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx);
585789Sahrens zilog_t *zilog;
586789Sahrens zil_header_t *zh;
587789Sahrens objset_t *os;
588789Sahrens int error;
589789Sahrens
59010298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os);
591789Sahrens if (error) {
5927294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname);
5932199Sahrens return (0);
594789Sahrens }
595789Sahrens
596789Sahrens zilog = dmu_objset_zil(os);
5971807Sbonwick zh = zil_header_in_syncing_context(zilog);
598789Sahrens
59910922SJeff.Bonwick@Sun.COM if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
6009701SGeorge.Wilson@Sun.COM if (!BP_IS_HOLE(&zh->zh_log))
60110922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
6029701SGeorge.Wilson@Sun.COM BP_ZERO(&zh->zh_log);
6039701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx);
60410921STim.Haley@Sun.COM dmu_objset_rele(os, FTAG);
60510921STim.Haley@Sun.COM return (0);
6069701SGeorge.Wilson@Sun.COM }
6079701SGeorge.Wilson@Sun.COM
608789Sahrens /*
6091807Sbonwick * Claim all log blocks if we haven't already done so, and remember
6101807Sbonwick * the highest claimed sequence number. This ensures that if we can
6111807Sbonwick * read only part of the log now (e.g. due to a missing device),
6121807Sbonwick * but we can read the entire log later, we will not try to replay
6131807Sbonwick * or destroy beyond the last block we successfully claimed.
614789Sahrens */
615789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg);
616789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
61710922SJeff.Bonwick@Sun.COM (void) zil_parse(zilog, zil_claim_log_block,
61810922SJeff.Bonwick@Sun.COM zil_claim_log_record, tx, first_txg);
619789Sahrens zh->zh_claim_txg = first_txg;
62010922SJeff.Bonwick@Sun.COM zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
62110922SJeff.Bonwick@Sun.COM zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
62210922SJeff.Bonwick@Sun.COM if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
62310922SJeff.Bonwick@Sun.COM zh->zh_flags |= ZIL_REPLAY_NEEDED;
62410922SJeff.Bonwick@Sun.COM zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
625789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx);
626789Sahrens }
6271807Sbonwick
628789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
62910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG);
6302199Sahrens return (0);
631789Sahrens }
632789Sahrens
6337294Sperrin /*
6347294Sperrin * Check the log by walking the log chain.
6357294Sperrin * Checksum errors are ok as they indicate the end of the chain.
6367294Sperrin * Any other error (no device or read failure) returns an error.
6377294Sperrin */
6387294Sperrin int
zil_check_log_chain(const char * osname,void * tx)63911209SMatthew.Ahrens@Sun.COM zil_check_log_chain(const char *osname, void *tx)
6407294Sperrin {
6417294Sperrin zilog_t *zilog;
6427294Sperrin objset_t *os;
643*12949SGeorge.Wilson@Sun.COM blkptr_t *bp;
6447294Sperrin int error;
6457294Sperrin
64610922SJeff.Bonwick@Sun.COM ASSERT(tx == NULL);
64710922SJeff.Bonwick@Sun.COM
64810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os);
6497294Sperrin if (error) {
6507294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname);
6517294Sperrin return (0);
6527294Sperrin }
6537294Sperrin
6547294Sperrin zilog = dmu_objset_zil(os);
655*12949SGeorge.Wilson@Sun.COM bp = (blkptr_t *)&zilog->zl_header->zh_log;
656*12949SGeorge.Wilson@Sun.COM
657*12949SGeorge.Wilson@Sun.COM /*
658*12949SGeorge.Wilson@Sun.COM * Check the first block and determine if it's on a log device
659*12949SGeorge.Wilson@Sun.COM * which may have been removed or faulted prior to loading this
660*12949SGeorge.Wilson@Sun.COM * pool. If so, there's no point in checking the rest of the log
661*12949SGeorge.Wilson@Sun.COM * as its content should have already been synced to the pool.
662*12949SGeorge.Wilson@Sun.COM */
663*12949SGeorge.Wilson@Sun.COM if (!BP_IS_HOLE(bp)) {
664*12949SGeorge.Wilson@Sun.COM vdev_t *vd;
665*12949SGeorge.Wilson@Sun.COM boolean_t valid = B_TRUE;
666*12949SGeorge.Wilson@Sun.COM
667*12949SGeorge.Wilson@Sun.COM spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
668*12949SGeorge.Wilson@Sun.COM vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
669*12949SGeorge.Wilson@Sun.COM if (vd->vdev_islog && vdev_is_dead(vd))
670*12949SGeorge.Wilson@Sun.COM valid = vdev_log_state_valid(vd);
671*12949SGeorge.Wilson@Sun.COM spa_config_exit(os->os_spa, SCL_STATE, FTAG);
672*12949SGeorge.Wilson@Sun.COM
673*12949SGeorge.Wilson@Sun.COM if (!valid) {
674*12949SGeorge.Wilson@Sun.COM dmu_objset_rele(os, FTAG);
675*12949SGeorge.Wilson@Sun.COM return (0);
676*12949SGeorge.Wilson@Sun.COM }
677*12949SGeorge.Wilson@Sun.COM }
6787294Sperrin
67910922SJeff.Bonwick@Sun.COM /*
68010922SJeff.Bonwick@Sun.COM * Because tx == NULL, zil_claim_log_block() will not actually claim
68110922SJeff.Bonwick@Sun.COM * any blocks, but just determine whether it is possible to do so.
68210922SJeff.Bonwick@Sun.COM * In addition to checking the log chain, zil_claim_log_block()
68310922SJeff.Bonwick@Sun.COM * will invoke zio_claim() with a done func of spa_claim_notify(),
68410922SJeff.Bonwick@Sun.COM * which will update spa_max_claim_txg. See spa_load() for details.
68510922SJeff.Bonwick@Sun.COM */
68610922SJeff.Bonwick@Sun.COM error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
68710922SJeff.Bonwick@Sun.COM zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
68810922SJeff.Bonwick@Sun.COM
68910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG);
69010922SJeff.Bonwick@Sun.COM
69110922SJeff.Bonwick@Sun.COM return ((error == ECKSUM || error == ENOENT) ? 0 : error);
6927294Sperrin }
6937294Sperrin
6945688Sbonwick static int
zil_vdev_compare(const void * x1,const void * x2)6955688Sbonwick zil_vdev_compare(const void *x1, const void *x2)
696789Sahrens {
69712699SNeil.Perrin@Sun.COM const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
69812699SNeil.Perrin@Sun.COM const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
6995688Sbonwick
7005688Sbonwick if (v1 < v2)
7015688Sbonwick return (-1);
7025688Sbonwick if (v1 > v2)
7035688Sbonwick return (1);
7045688Sbonwick
7055688Sbonwick return (0);
7065688Sbonwick }
7075688Sbonwick
7085688Sbonwick void
zil_add_block(zilog_t * zilog,const blkptr_t * bp)70910922SJeff.Bonwick@Sun.COM zil_add_block(zilog_t *zilog, const blkptr_t *bp)
7105688Sbonwick {
7115688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree;
7125688Sbonwick avl_index_t where;
7135688Sbonwick zil_vdev_node_t *zv, zvsearch;
7145688Sbonwick int ndvas = BP_GET_NDVAS(bp);
7155688Sbonwick int i;
716789Sahrens
7172986Sek110237 if (zfs_nocacheflush)
718789Sahrens return;
719789Sahrens
7205688Sbonwick ASSERT(zilog->zl_writer);
7215688Sbonwick
7225688Sbonwick /*
7235688Sbonwick * Even though we're zl_writer, we still need a lock because the
7245688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks
7255688Sbonwick * that will run concurrently.
7265688Sbonwick */
7275688Sbonwick mutex_enter(&zilog->zl_vdev_lock);
7285688Sbonwick for (i = 0; i < ndvas; i++) {
7295688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
7305688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) {
7315688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
7325688Sbonwick zv->zv_vdev = zvsearch.zv_vdev;
7335688Sbonwick avl_insert(t, zv, where);
7343063Sperrin }
7353063Sperrin }
7365688Sbonwick mutex_exit(&zilog->zl_vdev_lock);
7373063Sperrin }
7383063Sperrin
73912771SNeil.Perrin@Sun.COM static void
zil_flush_vdevs(zilog_t * zilog)7402638Sperrin zil_flush_vdevs(zilog_t *zilog)
741789Sahrens {
7423063Sperrin spa_t *spa = zilog->zl_spa;
7435688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree;
7445688Sbonwick void *cookie = NULL;
7455688Sbonwick zil_vdev_node_t *zv;
7465688Sbonwick zio_t *zio;
7473063Sperrin
7483063Sperrin ASSERT(zilog->zl_writer);
749789Sahrens
7505688Sbonwick /*
7515688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer,
7525688Sbonwick * and all zl_get_data() callbacks are done.
7535688Sbonwick */
7545688Sbonwick if (avl_numnodes(t) == 0)
7555688Sbonwick return;
7565688Sbonwick
7577754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7585688Sbonwick
7597754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
7605688Sbonwick
7615688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
7625688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
7635688Sbonwick if (vd != NULL)
7645688Sbonwick zio_flush(zio, vd);
7655688Sbonwick kmem_free(zv, sizeof (*zv));
7663063Sperrin }
767789Sahrens
768789Sahrens /*
769789Sahrens * Wait for all the flushes to complete. Not all devices actually
770789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
771789Sahrens */
7725688Sbonwick (void) zio_wait(zio);
7735688Sbonwick
7747754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG);
775789Sahrens }
776789Sahrens
777789Sahrens /*
778789Sahrens * Function called when a log block write completes
779789Sahrens */
780789Sahrens static void
zil_lwb_write_done(zio_t * zio)781789Sahrens zil_lwb_write_done(zio_t *zio)
782789Sahrens {
783789Sahrens lwb_t *lwb = zio->io_private;
784789Sahrens zilog_t *zilog = lwb->lwb_zilog;
78510922SJeff.Bonwick@Sun.COM dmu_tx_t *tx = lwb->lwb_tx;
786789Sahrens
7877754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
7887754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
7897754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
7907754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
7917754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp));
7927754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp));
7937754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0);
7947754SJeff.Bonwick@Sun.COM
795789Sahrens /*
7969493SNeil.Perrin@Sun.COM * Ensure the lwb buffer pointer is cleared before releasing
7979493SNeil.Perrin@Sun.COM * the txg. If we have had an allocation failure and
7989493SNeil.Perrin@Sun.COM * the txg is waiting to sync then we want want zil_sync()
7999493SNeil.Perrin@Sun.COM * to remove the lwb so that it's not picked up as the next new
8009493SNeil.Perrin@Sun.COM * one in zil_commit_writer(). zil_sync() will only remove
8019493SNeil.Perrin@Sun.COM * the lwb if lwb_buf is null.
802789Sahrens */
803789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
804789Sahrens mutex_enter(&zilog->zl_lock);
805789Sahrens lwb->lwb_buf = NULL;
80610922SJeff.Bonwick@Sun.COM lwb->lwb_tx = NULL;
80710922SJeff.Bonwick@Sun.COM mutex_exit(&zilog->zl_lock);
8089493SNeil.Perrin@Sun.COM
8099493SNeil.Perrin@Sun.COM /*
8109493SNeil.Perrin@Sun.COM * Now that we've written this log block, we have a stable pointer
8119493SNeil.Perrin@Sun.COM * to the next block in the chain, so it's OK to let the txg in
81210922SJeff.Bonwick@Sun.COM * which we allocated the next block sync.
8139493SNeil.Perrin@Sun.COM */
81410922SJeff.Bonwick@Sun.COM dmu_tx_commit(tx);
815789Sahrens }
816789Sahrens
817789Sahrens /*
8182237Smaybee * Initialize the io for a log block.
8192237Smaybee */
8202237Smaybee static void
zil_lwb_write_init(zilog_t * zilog,lwb_t * lwb)8212237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
8222237Smaybee {
8232237Smaybee zbookmark_t zb;
8242237Smaybee
82510922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
82610922SJeff.Bonwick@Sun.COM ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
82710922SJeff.Bonwick@Sun.COM lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
8282237Smaybee
8292638Sperrin if (zilog->zl_root_zio == NULL) {
8302638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
8312638Sperrin ZIO_FLAG_CANFAIL);
8322638Sperrin }
8333063Sperrin if (lwb->lwb_zio == NULL) {
8343063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
83511670SNeil.Perrin@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
8369701SGeorge.Wilson@Sun.COM zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
83710685SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
8383063Sperrin }
8392237Smaybee }
8402237Smaybee
8412237Smaybee /*
84211670SNeil.Perrin@Sun.COM * Define a limited set of intent log block sizes.
84311670SNeil.Perrin@Sun.COM * These must be a multiple of 4KB. Note only the amount used (again
84411670SNeil.Perrin@Sun.COM * aligned to 4KB) actually gets written. However, we can't always just
84511670SNeil.Perrin@Sun.COM * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
84611670SNeil.Perrin@Sun.COM */
84711670SNeil.Perrin@Sun.COM uint64_t zil_block_buckets[] = {
84811670SNeil.Perrin@Sun.COM 4096, /* non TX_WRITE */
84911670SNeil.Perrin@Sun.COM 8192+4096, /* data base */
85011670SNeil.Perrin@Sun.COM 32*1024 + 4096, /* NFS writes */
85111670SNeil.Perrin@Sun.COM UINT64_MAX
85211670SNeil.Perrin@Sun.COM };
85311670SNeil.Perrin@Sun.COM
85411670SNeil.Perrin@Sun.COM /*
85510879SNeil.Perrin@Sun.COM * Use the slog as long as the logbias is 'latency' and the current commit size
85610879SNeil.Perrin@Sun.COM * is less than the limit or the total list size is less than 2X the limit.
85710879SNeil.Perrin@Sun.COM * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
85810879SNeil.Perrin@Sun.COM */
85910879SNeil.Perrin@Sun.COM uint64_t zil_slog_limit = 1024 * 1024;
86010879SNeil.Perrin@Sun.COM #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
86110879SNeil.Perrin@Sun.COM (((zilog)->zl_cur_used < zil_slog_limit) || \
86210879SNeil.Perrin@Sun.COM ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
86310879SNeil.Perrin@Sun.COM
86410879SNeil.Perrin@Sun.COM /*
865789Sahrens * Start a log block write and advance to the next log block.
866789Sahrens * Calls are serialized.
867789Sahrens */
868789Sahrens static lwb_t *
zil_lwb_write_start(zilog_t * zilog,lwb_t * lwb)869789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
870789Sahrens {
87111670SNeil.Perrin@Sun.COM lwb_t *nlwb = NULL;
87211670SNeil.Perrin@Sun.COM zil_chain_t *zilc;
8731807Sbonwick spa_t *spa = zilog->zl_spa;
87411670SNeil.Perrin@Sun.COM blkptr_t *bp;
87510922SJeff.Bonwick@Sun.COM dmu_tx_t *tx;
876789Sahrens uint64_t txg;
87711813SNeil.Perrin@Sun.COM uint64_t zil_blksz, wsz;
87811670SNeil.Perrin@Sun.COM int i, error;
879789Sahrens
88011670SNeil.Perrin@Sun.COM if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
88111670SNeil.Perrin@Sun.COM zilc = (zil_chain_t *)lwb->lwb_buf;
88211670SNeil.Perrin@Sun.COM bp = &zilc->zc_next_blk;
88311670SNeil.Perrin@Sun.COM } else {
88411670SNeil.Perrin@Sun.COM zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
88511670SNeil.Perrin@Sun.COM bp = &zilc->zc_next_blk;
88611670SNeil.Perrin@Sun.COM }
88711670SNeil.Perrin@Sun.COM
88811670SNeil.Perrin@Sun.COM ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
889789Sahrens
890789Sahrens /*
891789Sahrens * Allocate the next block and save its address in this block
892789Sahrens * before writing it in order to establish the log chain.
893789Sahrens * Note that if the allocation of nlwb synced before we wrote
894789Sahrens * the block that points at it (lwb), we'd leak it if we crashed.
89510922SJeff.Bonwick@Sun.COM * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
89610922SJeff.Bonwick@Sun.COM * We dirty the dataset to ensure that zil_sync() will be called
89710922SJeff.Bonwick@Sun.COM * to clean up in the event of allocation failure or I/O failure.
898789Sahrens */
89910922SJeff.Bonwick@Sun.COM tx = dmu_tx_create(zilog->zl_os);
90010922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
90110922SJeff.Bonwick@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
90210922SJeff.Bonwick@Sun.COM txg = dmu_tx_get_txg(tx);
90310922SJeff.Bonwick@Sun.COM
90410922SJeff.Bonwick@Sun.COM lwb->lwb_tx = tx;
905789Sahrens
906789Sahrens /*
90711670SNeil.Perrin@Sun.COM * Log blocks are pre-allocated. Here we select the size of the next
90811670SNeil.Perrin@Sun.COM * block, based on size used in the last block.
90911670SNeil.Perrin@Sun.COM * - first find the smallest bucket that will fit the block from a
91011670SNeil.Perrin@Sun.COM * limited set of block sizes. This is because it's faster to write
91111670SNeil.Perrin@Sun.COM * blocks allocated from the same metaslab as they are adjacent or
91211670SNeil.Perrin@Sun.COM * close.
91311670SNeil.Perrin@Sun.COM * - next find the maximum from the new suggested size and an array of
91411670SNeil.Perrin@Sun.COM * previous sizes. This lessens a picket fence effect of wrongly
91511670SNeil.Perrin@Sun.COM * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
91611670SNeil.Perrin@Sun.COM * requests.
91711670SNeil.Perrin@Sun.COM *
91811670SNeil.Perrin@Sun.COM * Note we only write what is used, but we can't just allocate
91911670SNeil.Perrin@Sun.COM * the maximum block size because we can exhaust the available
92011670SNeil.Perrin@Sun.COM * pool log space.
921789Sahrens */
92211670SNeil.Perrin@Sun.COM zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
92311670SNeil.Perrin@Sun.COM for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
92411670SNeil.Perrin@Sun.COM continue;
92511670SNeil.Perrin@Sun.COM zil_blksz = zil_block_buckets[i];
92611670SNeil.Perrin@Sun.COM if (zil_blksz == UINT64_MAX)
92711670SNeil.Perrin@Sun.COM zil_blksz = SPA_MAXBLOCKSIZE;
92811670SNeil.Perrin@Sun.COM zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
92911670SNeil.Perrin@Sun.COM for (i = 0; i < ZIL_PREV_BLKS; i++)
93011670SNeil.Perrin@Sun.COM zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
93111670SNeil.Perrin@Sun.COM zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
932789Sahrens
9333063Sperrin BP_ZERO(bp);
9343063Sperrin /* pass the old blkptr in order to spread log blocks across devs */
93510922SJeff.Bonwick@Sun.COM error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
93610879SNeil.Perrin@Sun.COM USE_SLOG(zilog));
93711670SNeil.Perrin@Sun.COM if (!error) {
93811670SNeil.Perrin@Sun.COM ASSERT3U(bp->blk_birth, ==, txg);
93911670SNeil.Perrin@Sun.COM bp->blk_cksum = lwb->lwb_blk.blk_cksum;
94011670SNeil.Perrin@Sun.COM bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
9413668Sgw25295
9423668Sgw25295 /*
94311670SNeil.Perrin@Sun.COM * Allocate a new log write buffer (lwb).
9441544Seschrock */
94511670SNeil.Perrin@Sun.COM nlwb = zil_alloc_lwb(zilog, bp, txg);
94611670SNeil.Perrin@Sun.COM
94711670SNeil.Perrin@Sun.COM /* Record the block for later vdev flushing */
94811670SNeil.Perrin@Sun.COM zil_add_block(zilog, &lwb->lwb_blk);
949789Sahrens }
950789Sahrens
95111670SNeil.Perrin@Sun.COM if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
95211813SNeil.Perrin@Sun.COM /* For Slim ZIL only write what is used. */
95311813SNeil.Perrin@Sun.COM wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
95411813SNeil.Perrin@Sun.COM ASSERT3U(wsz, <=, lwb->lwb_sz);
95511813SNeil.Perrin@Sun.COM zio_shrink(lwb->lwb_zio, wsz);
95611670SNeil.Perrin@Sun.COM
95711813SNeil.Perrin@Sun.COM } else {
95811813SNeil.Perrin@Sun.COM wsz = lwb->lwb_sz;
95911813SNeil.Perrin@Sun.COM }
96011670SNeil.Perrin@Sun.COM
96111670SNeil.Perrin@Sun.COM zilc->zc_pad = 0;
96211670SNeil.Perrin@Sun.COM zilc->zc_nused = lwb->lwb_nused;
96311670SNeil.Perrin@Sun.COM zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
96411670SNeil.Perrin@Sun.COM
96511813SNeil.Perrin@Sun.COM /*
96611813SNeil.Perrin@Sun.COM * clear unused data for security
96711813SNeil.Perrin@Sun.COM */
96811813SNeil.Perrin@Sun.COM bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
96911813SNeil.Perrin@Sun.COM
97011670SNeil.Perrin@Sun.COM zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
971789Sahrens
972789Sahrens /*
97311670SNeil.Perrin@Sun.COM * If there was an allocation failure then nlwb will be null which
97411670SNeil.Perrin@Sun.COM * forces a txg_wait_synced().
975789Sahrens */
976789Sahrens return (nlwb);
977789Sahrens }
978789Sahrens
979789Sahrens static lwb_t *
zil_lwb_commit(zilog_t * zilog,itx_t * itx,lwb_t * lwb)980789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
981789Sahrens {
982789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */
98310922SJeff.Bonwick@Sun.COM lr_write_t *lrw = (lr_write_t *)lrc;
98410922SJeff.Bonwick@Sun.COM char *lr_buf;
985789Sahrens uint64_t txg = lrc->lrc_txg;
986789Sahrens uint64_t reclen = lrc->lrc_reclen;
98710922SJeff.Bonwick@Sun.COM uint64_t dlen = 0;
988789Sahrens
989789Sahrens if (lwb == NULL)
990789Sahrens return (NULL);
99110922SJeff.Bonwick@Sun.COM
992789Sahrens ASSERT(lwb->lwb_buf != NULL);
993789Sahrens
9942237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
9952237Smaybee dlen = P2ROUNDUP_TYPED(
99610922SJeff.Bonwick@Sun.COM lrw->lr_length, sizeof (uint64_t), uint64_t);
9971669Sperrin
9981669Sperrin zilog->zl_cur_used += (reclen + dlen);
9991669Sperrin
10003063Sperrin zil_lwb_write_init(zilog, lwb);
10013063Sperrin
10021669Sperrin /*
10031669Sperrin * If this record won't fit in the current log block, start a new one.
10041669Sperrin */
100511670SNeil.Perrin@Sun.COM if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
10061669Sperrin lwb = zil_lwb_write_start(zilog, lwb);
10072237Smaybee if (lwb == NULL)
10081669Sperrin return (NULL);
10093063Sperrin zil_lwb_write_init(zilog, lwb);
101011670SNeil.Perrin@Sun.COM ASSERT(LWB_EMPTY(lwb));
101111670SNeil.Perrin@Sun.COM if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
10121669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg);
1013789Sahrens return (lwb);
1014789Sahrens }
1015789Sahrens }
1016789Sahrens
101710922SJeff.Bonwick@Sun.COM lr_buf = lwb->lwb_buf + lwb->lwb_nused;
101810922SJeff.Bonwick@Sun.COM bcopy(lrc, lr_buf, reclen);
101910922SJeff.Bonwick@Sun.COM lrc = (lr_t *)lr_buf;
102010922SJeff.Bonwick@Sun.COM lrw = (lr_write_t *)lrc;
10212237Smaybee
10222237Smaybee /*
10232237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate.
10242237Smaybee */
10252237Smaybee if (lrc->lrc_txtype == TX_WRITE) {
10262237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa))
10272237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg);
10282237Smaybee if (itx->itx_wr_state != WR_COPIED) {
10292237Smaybee char *dbuf;
10302237Smaybee int error;
10312237Smaybee
10322237Smaybee if (dlen) {
10332237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY);
103410922SJeff.Bonwick@Sun.COM dbuf = lr_buf + reclen;
103510922SJeff.Bonwick@Sun.COM lrw->lr_common.lrc_reclen += dlen;
10362237Smaybee } else {
10372237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT);
10382237Smaybee dbuf = NULL;
10392237Smaybee }
10402237Smaybee error = zilog->zl_get_data(
104110922SJeff.Bonwick@Sun.COM itx->itx_private, lrw, dbuf, lwb->lwb_zio);
104210209SMark.Musante@Sun.COM if (error == EIO) {
104310209SMark.Musante@Sun.COM txg_wait_synced(zilog->zl_dmu_pool, txg);
104410209SMark.Musante@Sun.COM return (lwb);
104510209SMark.Musante@Sun.COM }
10462237Smaybee if (error) {
10472237Smaybee ASSERT(error == ENOENT || error == EEXIST ||
10482237Smaybee error == EALREADY);
10492237Smaybee return (lwb);
10502237Smaybee }
10512237Smaybee }
10521669Sperrin }
10532237Smaybee
105410922SJeff.Bonwick@Sun.COM /*
105510922SJeff.Bonwick@Sun.COM * We're actually making an entry, so update lrc_seq to be the
105610922SJeff.Bonwick@Sun.COM * log record sequence number. Note that this is generally not
105710922SJeff.Bonwick@Sun.COM * equal to the itx sequence number because not all transactions
105810922SJeff.Bonwick@Sun.COM * are synchronous, and sometimes spa_sync() gets there first.
105910922SJeff.Bonwick@Sun.COM */
106010922SJeff.Bonwick@Sun.COM lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
10612237Smaybee lwb->lwb_nused += reclen + dlen;
1062789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
106311670SNeil.Perrin@Sun.COM ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1064789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
1065789Sahrens
1066789Sahrens return (lwb);
1067789Sahrens }
1068789Sahrens
1069789Sahrens itx_t *
zil_itx_create(uint64_t txtype,size_t lrsize)10705331Samw zil_itx_create(uint64_t txtype, size_t lrsize)
1071789Sahrens {
1072789Sahrens itx_t *itx;
1073789Sahrens
10741842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1075789Sahrens
1076789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
1077789Sahrens itx->itx_lr.lrc_txtype = txtype;
1078789Sahrens itx->itx_lr.lrc_reclen = lrsize;
10796101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1080789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */
108112699SNeil.Perrin@Sun.COM itx->itx_sync = B_TRUE; /* default is synchronous */
1082789Sahrens
1083789Sahrens return (itx);
1084789Sahrens }
1085789Sahrens
108610922SJeff.Bonwick@Sun.COM void
zil_itx_destroy(itx_t * itx)108710922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx_t *itx)
108810922SJeff.Bonwick@Sun.COM {
108910922SJeff.Bonwick@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
109010922SJeff.Bonwick@Sun.COM }
109110922SJeff.Bonwick@Sun.COM
109212699SNeil.Perrin@Sun.COM /*
109312699SNeil.Perrin@Sun.COM * Free up the sync and async itxs. The itxs_t has already been detached
109412699SNeil.Perrin@Sun.COM * so no locks are needed.
109512699SNeil.Perrin@Sun.COM */
109612699SNeil.Perrin@Sun.COM static void
zil_itxg_clean(itxs_t * itxs)109712699SNeil.Perrin@Sun.COM zil_itxg_clean(itxs_t *itxs)
1098789Sahrens {
109912699SNeil.Perrin@Sun.COM itx_t *itx;
110012699SNeil.Perrin@Sun.COM list_t *list;
110112699SNeil.Perrin@Sun.COM avl_tree_t *t;
110212699SNeil.Perrin@Sun.COM void *cookie;
110312699SNeil.Perrin@Sun.COM itx_async_node_t *ian;
1104789Sahrens
110512699SNeil.Perrin@Sun.COM list = &itxs->i_sync_list;
110612699SNeil.Perrin@Sun.COM while ((itx = list_head(list)) != NULL) {
110712699SNeil.Perrin@Sun.COM list_remove(list, itx);
110812699SNeil.Perrin@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr) +
110912699SNeil.Perrin@Sun.COM itx->itx_lr.lrc_reclen);
111012699SNeil.Perrin@Sun.COM }
1111789Sahrens
111212699SNeil.Perrin@Sun.COM cookie = NULL;
111312699SNeil.Perrin@Sun.COM t = &itxs->i_async_tree;
111412699SNeil.Perrin@Sun.COM while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
111512699SNeil.Perrin@Sun.COM list = &ian->ia_list;
111612699SNeil.Perrin@Sun.COM while ((itx = list_head(list)) != NULL) {
111712699SNeil.Perrin@Sun.COM list_remove(list, itx);
111812699SNeil.Perrin@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr) +
111912699SNeil.Perrin@Sun.COM itx->itx_lr.lrc_reclen);
112012699SNeil.Perrin@Sun.COM }
112112699SNeil.Perrin@Sun.COM list_destroy(list);
112212699SNeil.Perrin@Sun.COM kmem_free(ian, sizeof (itx_async_node_t));
112312699SNeil.Perrin@Sun.COM }
112412699SNeil.Perrin@Sun.COM avl_destroy(t);
1125789Sahrens
112612699SNeil.Perrin@Sun.COM kmem_free(itxs, sizeof (itxs_t));
112712699SNeil.Perrin@Sun.COM }
112812699SNeil.Perrin@Sun.COM
112912699SNeil.Perrin@Sun.COM static int
zil_aitx_compare(const void * x1,const void * x2)113012699SNeil.Perrin@Sun.COM zil_aitx_compare(const void *x1, const void *x2)
113112699SNeil.Perrin@Sun.COM {
113212699SNeil.Perrin@Sun.COM const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
113312699SNeil.Perrin@Sun.COM const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
113412699SNeil.Perrin@Sun.COM
113512699SNeil.Perrin@Sun.COM if (o1 < o2)
113612699SNeil.Perrin@Sun.COM return (-1);
113712699SNeil.Perrin@Sun.COM if (o1 > o2)
113812699SNeil.Perrin@Sun.COM return (1);
113912699SNeil.Perrin@Sun.COM
114012699SNeil.Perrin@Sun.COM return (0);
1141789Sahrens }
1142789Sahrens
1143789Sahrens /*
114412699SNeil.Perrin@Sun.COM * Remove all async itx with the given oid.
1145789Sahrens */
114612771SNeil.Perrin@Sun.COM static void
zil_remove_async(zilog_t * zilog,uint64_t oid)114712699SNeil.Perrin@Sun.COM zil_remove_async(zilog_t *zilog, uint64_t oid)
1148789Sahrens {
114912699SNeil.Perrin@Sun.COM uint64_t otxg, txg;
115012699SNeil.Perrin@Sun.COM itx_async_node_t *ian;
115112699SNeil.Perrin@Sun.COM avl_tree_t *t;
115212699SNeil.Perrin@Sun.COM avl_index_t where;
11533778Sjohansen list_t clean_list;
1154789Sahrens itx_t *itx;
1155789Sahrens
115612699SNeil.Perrin@Sun.COM ASSERT(oid != 0);
11573778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
11583778Sjohansen
115912699SNeil.Perrin@Sun.COM if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
116012699SNeil.Perrin@Sun.COM otxg = ZILTEST_TXG;
116112699SNeil.Perrin@Sun.COM else
116212699SNeil.Perrin@Sun.COM otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
116312699SNeil.Perrin@Sun.COM
116412699SNeil.Perrin@Sun.COM for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
116512699SNeil.Perrin@Sun.COM itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
116612699SNeil.Perrin@Sun.COM
116712699SNeil.Perrin@Sun.COM mutex_enter(&itxg->itxg_lock);
116812699SNeil.Perrin@Sun.COM if (itxg->itxg_txg != txg) {
116912699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
117012699SNeil.Perrin@Sun.COM continue;
117112699SNeil.Perrin@Sun.COM }
117212699SNeil.Perrin@Sun.COM
117312699SNeil.Perrin@Sun.COM /*
117412699SNeil.Perrin@Sun.COM * Locate the object node and append its list.
117512699SNeil.Perrin@Sun.COM */
117612699SNeil.Perrin@Sun.COM t = &itxg->itxg_itxs->i_async_tree;
117712699SNeil.Perrin@Sun.COM ian = avl_find(t, &oid, &where);
117812699SNeil.Perrin@Sun.COM if (ian != NULL)
117912699SNeil.Perrin@Sun.COM list_move_tail(&clean_list, &ian->ia_list);
118012699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
11812638Sperrin }
118212699SNeil.Perrin@Sun.COM while ((itx = list_head(&clean_list)) != NULL) {
118312699SNeil.Perrin@Sun.COM list_remove(&clean_list, itx);
118412699SNeil.Perrin@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr) +
118512699SNeil.Perrin@Sun.COM itx->itx_lr.lrc_reclen);
118612699SNeil.Perrin@Sun.COM }
118712699SNeil.Perrin@Sun.COM list_destroy(&clean_list);
118812699SNeil.Perrin@Sun.COM }
118912699SNeil.Perrin@Sun.COM
119012699SNeil.Perrin@Sun.COM void
zil_itx_assign(zilog_t * zilog,itx_t * itx,dmu_tx_t * tx)119112699SNeil.Perrin@Sun.COM zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
119212699SNeil.Perrin@Sun.COM {
119312699SNeil.Perrin@Sun.COM uint64_t txg;
119412699SNeil.Perrin@Sun.COM itxg_t *itxg;
119512699SNeil.Perrin@Sun.COM itxs_t *itxs, *clean = NULL;
11963778Sjohansen
11973778Sjohansen /*
119812771SNeil.Perrin@Sun.COM * Object ids can be re-instantiated in the next txg so
119912699SNeil.Perrin@Sun.COM * remove any async transactions to avoid future leaks.
120012699SNeil.Perrin@Sun.COM * This can happen if a fsync occurs on the re-instantiated
120112699SNeil.Perrin@Sun.COM * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
120212699SNeil.Perrin@Sun.COM * the new file data and flushes a write record for the old object.
12033778Sjohansen */
120412699SNeil.Perrin@Sun.COM if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
120512700SNeil.Perrin@Sun.COM zil_remove_async(zilog, itx->itx_oid);
120612699SNeil.Perrin@Sun.COM
120712771SNeil.Perrin@Sun.COM /*
120812771SNeil.Perrin@Sun.COM * Ensure the data of a renamed file is committed before the rename.
120912771SNeil.Perrin@Sun.COM */
121012771SNeil.Perrin@Sun.COM if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
121112771SNeil.Perrin@Sun.COM zil_async_to_sync(zilog, itx->itx_oid);
121212771SNeil.Perrin@Sun.COM
121312699SNeil.Perrin@Sun.COM if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
121412699SNeil.Perrin@Sun.COM txg = ZILTEST_TXG;
121512699SNeil.Perrin@Sun.COM else
121612699SNeil.Perrin@Sun.COM txg = dmu_tx_get_txg(tx);
121712699SNeil.Perrin@Sun.COM
121812699SNeil.Perrin@Sun.COM itxg = &zilog->zl_itxg[txg & TXG_MASK];
121912699SNeil.Perrin@Sun.COM mutex_enter(&itxg->itxg_lock);
122012699SNeil.Perrin@Sun.COM itxs = itxg->itxg_itxs;
122112699SNeil.Perrin@Sun.COM if (itxg->itxg_txg != txg) {
122212699SNeil.Perrin@Sun.COM if (itxs != NULL) {
122312699SNeil.Perrin@Sun.COM /*
122412699SNeil.Perrin@Sun.COM * The zil_clean callback hasn't got around to cleaning
122512699SNeil.Perrin@Sun.COM * this itxg. Save the itxs for release below.
122612699SNeil.Perrin@Sun.COM * This should be rare.
122712699SNeil.Perrin@Sun.COM */
122812699SNeil.Perrin@Sun.COM atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
122912699SNeil.Perrin@Sun.COM itxg->itxg_sod = 0;
123012699SNeil.Perrin@Sun.COM clean = itxg->itxg_itxs;
123112699SNeil.Perrin@Sun.COM }
123212699SNeil.Perrin@Sun.COM ASSERT(itxg->itxg_sod == 0);
123312699SNeil.Perrin@Sun.COM itxg->itxg_txg = txg;
123412699SNeil.Perrin@Sun.COM itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP);
123512699SNeil.Perrin@Sun.COM
123612699SNeil.Perrin@Sun.COM list_create(&itxs->i_sync_list, sizeof (itx_t),
123712699SNeil.Perrin@Sun.COM offsetof(itx_t, itx_node));
123812699SNeil.Perrin@Sun.COM avl_create(&itxs->i_async_tree, zil_aitx_compare,
123912699SNeil.Perrin@Sun.COM sizeof (itx_async_node_t),
124012699SNeil.Perrin@Sun.COM offsetof(itx_async_node_t, ia_node));
12413778Sjohansen }
124212699SNeil.Perrin@Sun.COM if (itx->itx_sync) {
124312699SNeil.Perrin@Sun.COM list_insert_tail(&itxs->i_sync_list, itx);
124412699SNeil.Perrin@Sun.COM atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
124512699SNeil.Perrin@Sun.COM itxg->itxg_sod += itx->itx_sod;
124612699SNeil.Perrin@Sun.COM } else {
124712699SNeil.Perrin@Sun.COM avl_tree_t *t = &itxs->i_async_tree;
124812699SNeil.Perrin@Sun.COM uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
124912699SNeil.Perrin@Sun.COM itx_async_node_t *ian;
125012699SNeil.Perrin@Sun.COM avl_index_t where;
12513778Sjohansen
125212699SNeil.Perrin@Sun.COM ian = avl_find(t, &foid, &where);
125312699SNeil.Perrin@Sun.COM if (ian == NULL) {
125412699SNeil.Perrin@Sun.COM ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP);
125512699SNeil.Perrin@Sun.COM list_create(&ian->ia_list, sizeof (itx_t),
125612699SNeil.Perrin@Sun.COM offsetof(itx_t, itx_node));
125712699SNeil.Perrin@Sun.COM ian->ia_foid = foid;
125812699SNeil.Perrin@Sun.COM avl_insert(t, ian, where);
125912699SNeil.Perrin@Sun.COM }
126012699SNeil.Perrin@Sun.COM list_insert_tail(&ian->ia_list, itx);
1261789Sahrens }
126212699SNeil.Perrin@Sun.COM
126312699SNeil.Perrin@Sun.COM itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
126412699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
126512699SNeil.Perrin@Sun.COM
126612699SNeil.Perrin@Sun.COM /* Release the old itxs now we've dropped the lock */
126712699SNeil.Perrin@Sun.COM if (clean != NULL)
126812699SNeil.Perrin@Sun.COM zil_itxg_clean(clean);
1269789Sahrens }
1270789Sahrens
12712638Sperrin /*
12723063Sperrin * If there are any in-memory intent log transactions which have now been
12733063Sperrin * synced then start up a taskq to free them.
12742638Sperrin */
1275789Sahrens void
zil_clean(zilog_t * zilog,uint64_t synced_txg)127612699SNeil.Perrin@Sun.COM zil_clean(zilog_t *zilog, uint64_t synced_txg)
1277789Sahrens {
127812699SNeil.Perrin@Sun.COM itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
127912699SNeil.Perrin@Sun.COM itxs_t *clean_me;
128012699SNeil.Perrin@Sun.COM
128112699SNeil.Perrin@Sun.COM mutex_enter(&itxg->itxg_lock);
128212699SNeil.Perrin@Sun.COM if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
128312699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
128412699SNeil.Perrin@Sun.COM return;
128512699SNeil.Perrin@Sun.COM }
128612699SNeil.Perrin@Sun.COM ASSERT3U(itxg->itxg_txg, <=, synced_txg);
128712699SNeil.Perrin@Sun.COM ASSERT(itxg->itxg_txg != 0);
128812699SNeil.Perrin@Sun.COM ASSERT(zilog->zl_clean_taskq != NULL);
128912699SNeil.Perrin@Sun.COM atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
129012699SNeil.Perrin@Sun.COM itxg->itxg_sod = 0;
129112699SNeil.Perrin@Sun.COM clean_me = itxg->itxg_itxs;
129212699SNeil.Perrin@Sun.COM itxg->itxg_itxs = NULL;
129312699SNeil.Perrin@Sun.COM itxg->itxg_txg = 0;
129412699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
129512699SNeil.Perrin@Sun.COM /*
129612699SNeil.Perrin@Sun.COM * Preferably start a task queue to free up the old itxs but
129712699SNeil.Perrin@Sun.COM * if taskq_dispatch can't allocate resources to do that then
129812699SNeil.Perrin@Sun.COM * free it in-line. This should be rare. Note, using TQ_SLEEP
129912699SNeil.Perrin@Sun.COM * created a bad performance problem.
130012699SNeil.Perrin@Sun.COM */
130112699SNeil.Perrin@Sun.COM if (taskq_dispatch(zilog->zl_clean_taskq,
130212699SNeil.Perrin@Sun.COM (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == NULL)
130312699SNeil.Perrin@Sun.COM zil_itxg_clean(clean_me);
130412699SNeil.Perrin@Sun.COM }
130512699SNeil.Perrin@Sun.COM
130612699SNeil.Perrin@Sun.COM /*
130712699SNeil.Perrin@Sun.COM * Get the list of itxs to commit into zl_itx_commit_list.
130812699SNeil.Perrin@Sun.COM */
130912771SNeil.Perrin@Sun.COM static void
zil_get_commit_list(zilog_t * zilog)131012699SNeil.Perrin@Sun.COM zil_get_commit_list(zilog_t *zilog)
131112699SNeil.Perrin@Sun.COM {
131212699SNeil.Perrin@Sun.COM uint64_t otxg, txg;
131312699SNeil.Perrin@Sun.COM list_t *commit_list = &zilog->zl_itx_commit_list;
131412699SNeil.Perrin@Sun.COM uint64_t push_sod = 0;
131512699SNeil.Perrin@Sun.COM
131612699SNeil.Perrin@Sun.COM if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
131712699SNeil.Perrin@Sun.COM otxg = ZILTEST_TXG;
131812699SNeil.Perrin@Sun.COM else
131912699SNeil.Perrin@Sun.COM otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
132012699SNeil.Perrin@Sun.COM
132112699SNeil.Perrin@Sun.COM for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
132212699SNeil.Perrin@Sun.COM itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
132312699SNeil.Perrin@Sun.COM
132412699SNeil.Perrin@Sun.COM mutex_enter(&itxg->itxg_lock);
132512699SNeil.Perrin@Sun.COM if (itxg->itxg_txg != txg) {
132612699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
132712699SNeil.Perrin@Sun.COM continue;
132812699SNeil.Perrin@Sun.COM }
13293063Sperrin
133012699SNeil.Perrin@Sun.COM list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
133112699SNeil.Perrin@Sun.COM push_sod += itxg->itxg_sod;
133212699SNeil.Perrin@Sun.COM itxg->itxg_sod = 0;
133312699SNeil.Perrin@Sun.COM
133412699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
13353063Sperrin }
133612699SNeil.Perrin@Sun.COM atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
133712699SNeil.Perrin@Sun.COM }
133812699SNeil.Perrin@Sun.COM
133912699SNeil.Perrin@Sun.COM /*
134012699SNeil.Perrin@Sun.COM * Move the async itxs for a specified object to commit into sync lists.
134112699SNeil.Perrin@Sun.COM */
134212771SNeil.Perrin@Sun.COM static void
zil_async_to_sync(zilog_t * zilog,uint64_t foid)134312699SNeil.Perrin@Sun.COM zil_async_to_sync(zilog_t *zilog, uint64_t foid)
134412699SNeil.Perrin@Sun.COM {
134512699SNeil.Perrin@Sun.COM uint64_t otxg, txg;
134612699SNeil.Perrin@Sun.COM itx_async_node_t *ian;
134712699SNeil.Perrin@Sun.COM avl_tree_t *t;
134812699SNeil.Perrin@Sun.COM avl_index_t where;
134912699SNeil.Perrin@Sun.COM
135012699SNeil.Perrin@Sun.COM if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
135112699SNeil.Perrin@Sun.COM otxg = ZILTEST_TXG;
135212699SNeil.Perrin@Sun.COM else
135312699SNeil.Perrin@Sun.COM otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
135412699SNeil.Perrin@Sun.COM
135512699SNeil.Perrin@Sun.COM for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
135612699SNeil.Perrin@Sun.COM itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
135712699SNeil.Perrin@Sun.COM
135812699SNeil.Perrin@Sun.COM mutex_enter(&itxg->itxg_lock);
135912699SNeil.Perrin@Sun.COM if (itxg->itxg_txg != txg) {
136012699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
136112699SNeil.Perrin@Sun.COM continue;
136212699SNeil.Perrin@Sun.COM }
136312699SNeil.Perrin@Sun.COM
136412699SNeil.Perrin@Sun.COM /*
136512699SNeil.Perrin@Sun.COM * If a foid is specified then find that node and append its
136612699SNeil.Perrin@Sun.COM * list. Otherwise walk the tree appending all the lists
136712699SNeil.Perrin@Sun.COM * to the sync list. We add to the end rather than the
136812699SNeil.Perrin@Sun.COM * beginning to ensure the create has happened.
136912699SNeil.Perrin@Sun.COM */
137012699SNeil.Perrin@Sun.COM t = &itxg->itxg_itxs->i_async_tree;
137112699SNeil.Perrin@Sun.COM if (foid != 0) {
137212699SNeil.Perrin@Sun.COM ian = avl_find(t, &foid, &where);
137312699SNeil.Perrin@Sun.COM if (ian != NULL) {
137412699SNeil.Perrin@Sun.COM list_move_tail(&itxg->itxg_itxs->i_sync_list,
137512699SNeil.Perrin@Sun.COM &ian->ia_list);
137612699SNeil.Perrin@Sun.COM }
137712699SNeil.Perrin@Sun.COM } else {
137812699SNeil.Perrin@Sun.COM void *cookie = NULL;
137912699SNeil.Perrin@Sun.COM
138012699SNeil.Perrin@Sun.COM while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
138112699SNeil.Perrin@Sun.COM list_move_tail(&itxg->itxg_itxs->i_sync_list,
138212699SNeil.Perrin@Sun.COM &ian->ia_list);
138312699SNeil.Perrin@Sun.COM list_destroy(&ian->ia_list);
138412699SNeil.Perrin@Sun.COM kmem_free(ian, sizeof (itx_async_node_t));
138512699SNeil.Perrin@Sun.COM }
138612699SNeil.Perrin@Sun.COM }
138712699SNeil.Perrin@Sun.COM mutex_exit(&itxg->itxg_lock);
138812699SNeil.Perrin@Sun.COM }
1389789Sahrens }
1390789Sahrens
13917754SJeff.Bonwick@Sun.COM static void
zil_commit_writer(zilog_t * zilog)139212699SNeil.Perrin@Sun.COM zil_commit_writer(zilog_t *zilog)
1393789Sahrens {
1394789Sahrens uint64_t txg;
139512699SNeil.Perrin@Sun.COM itx_t *itx;
1396789Sahrens lwb_t *lwb;
139712699SNeil.Perrin@Sun.COM spa_t *spa = zilog->zl_spa;
139810922SJeff.Bonwick@Sun.COM int error = 0;
1399789Sahrens
14007754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL);
140112699SNeil.Perrin@Sun.COM
140212699SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock);
140312699SNeil.Perrin@Sun.COM
140412699SNeil.Perrin@Sun.COM zil_get_commit_list(zilog);
140512699SNeil.Perrin@Sun.COM
140612699SNeil.Perrin@Sun.COM /*
140712699SNeil.Perrin@Sun.COM * Return if there's nothing to commit before we dirty the fs by
140812699SNeil.Perrin@Sun.COM * calling zil_create().
140912699SNeil.Perrin@Sun.COM */
141012699SNeil.Perrin@Sun.COM if (list_head(&zilog->zl_itx_commit_list) == NULL) {
141112699SNeil.Perrin@Sun.COM mutex_enter(&zilog->zl_lock);
141212699SNeil.Perrin@Sun.COM return;
141312699SNeil.Perrin@Sun.COM }
1414789Sahrens
1415789Sahrens if (zilog->zl_suspend) {
1416789Sahrens lwb = NULL;
1417789Sahrens } else {
1418789Sahrens lwb = list_tail(&zilog->zl_lwb_list);
141912699SNeil.Perrin@Sun.COM if (lwb == NULL)
142011670SNeil.Perrin@Sun.COM lwb = zil_create(zilog);
1421789Sahrens }
142210922SJeff.Bonwick@Sun.COM
142312699SNeil.Perrin@Sun.COM DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
142412699SNeil.Perrin@Sun.COM while (itx = list_head(&zilog->zl_itx_commit_list)) {
1425789Sahrens txg = itx->itx_lr.lrc_txg;
1426789Sahrens ASSERT(txg);
1427789Sahrens
142812699SNeil.Perrin@Sun.COM if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
1429789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb);
143012699SNeil.Perrin@Sun.COM list_remove(&zilog->zl_itx_commit_list, itx);
143112699SNeil.Perrin@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr)
143212699SNeil.Perrin@Sun.COM + itx->itx_lr.lrc_reclen);
1433789Sahrens }
14342638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1435789Sahrens
1436789Sahrens /* write the last block out */
14373063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL)
1438789Sahrens lwb = zil_lwb_write_start(zilog, lwb);
1439789Sahrens
14401141Sperrin zilog->zl_cur_used = 0;
14411141Sperrin
14422638Sperrin /*
14432638Sperrin * Wait if necessary for the log blocks to be on stable storage.
14442638Sperrin */
14452638Sperrin if (zilog->zl_root_zio) {
144610922SJeff.Bonwick@Sun.COM error = zio_wait(zilog->zl_root_zio);
14477754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL;
14485688Sbonwick zil_flush_vdevs(zilog);
1449789Sahrens }
14501141Sperrin
145110922SJeff.Bonwick@Sun.COM if (error || lwb == NULL)
1452789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0);
14533063Sperrin
14543063Sperrin mutex_enter(&zilog->zl_lock);
145510922SJeff.Bonwick@Sun.COM
145610922SJeff.Bonwick@Sun.COM /*
145710922SJeff.Bonwick@Sun.COM * Remember the highest committed log sequence number for ztest.
145810922SJeff.Bonwick@Sun.COM * We only update this value when all the log writes succeeded,
145910922SJeff.Bonwick@Sun.COM * because ztest wants to ASSERT that it got the whole log chain.
146010922SJeff.Bonwick@Sun.COM */
146110922SJeff.Bonwick@Sun.COM if (error == 0 && lwb != NULL)
146210922SJeff.Bonwick@Sun.COM zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
14632638Sperrin }
14642638Sperrin
14652638Sperrin /*
146612699SNeil.Perrin@Sun.COM * Commit zfs transactions to stable storage.
14672638Sperrin * If foid is 0 push out all transactions, otherwise push only those
146812699SNeil.Perrin@Sun.COM * for that object or might reference that object.
146912699SNeil.Perrin@Sun.COM *
147012699SNeil.Perrin@Sun.COM * itxs are committed in batches. In a heavily stressed zil there will be
147112699SNeil.Perrin@Sun.COM * a commit writer thread who is writing out a bunch of itxs to the log
147212699SNeil.Perrin@Sun.COM * for a set of committing threads (cthreads) in the same batch as the writer.
147312699SNeil.Perrin@Sun.COM * Those cthreads are all waiting on the same cv for that batch.
147412699SNeil.Perrin@Sun.COM *
147512699SNeil.Perrin@Sun.COM * There will also be a different and growing batch of threads that are
147612699SNeil.Perrin@Sun.COM * waiting to commit (qthreads). When the committing batch completes
147712699SNeil.Perrin@Sun.COM * a transition occurs such that the cthreads exit and the qthreads become
147812699SNeil.Perrin@Sun.COM * cthreads. One of the new cthreads becomes the writer thread for the
147912699SNeil.Perrin@Sun.COM * batch. Any new threads arriving become new qthreads.
148012699SNeil.Perrin@Sun.COM *
148112699SNeil.Perrin@Sun.COM * Only 2 condition variables are needed and there's no transition
148212699SNeil.Perrin@Sun.COM * between the two cvs needed. They just flip-flop between qthreads
148312699SNeil.Perrin@Sun.COM * and cthreads.
148412699SNeil.Perrin@Sun.COM *
148512699SNeil.Perrin@Sun.COM * Using this scheme we can efficiently wakeup up only those threads
148612699SNeil.Perrin@Sun.COM * that have been committed.
14872638Sperrin */
14882638Sperrin void
zil_commit(zilog_t * zilog,uint64_t foid)148912699SNeil.Perrin@Sun.COM zil_commit(zilog_t *zilog, uint64_t foid)
14902638Sperrin {
149112699SNeil.Perrin@Sun.COM uint64_t mybatch;
149212699SNeil.Perrin@Sun.COM
149312699SNeil.Perrin@Sun.COM if (zilog->zl_sync == ZFS_SYNC_DISABLED)
14942638Sperrin return;
14952638Sperrin
149612699SNeil.Perrin@Sun.COM /* move the async itxs for the foid to the sync queues */
149712699SNeil.Perrin@Sun.COM zil_async_to_sync(zilog, foid);
149812699SNeil.Perrin@Sun.COM
14992638Sperrin mutex_enter(&zilog->zl_lock);
150012699SNeil.Perrin@Sun.COM mybatch = zilog->zl_next_batch;
15013063Sperrin while (zilog->zl_writer) {
150212699SNeil.Perrin@Sun.COM cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
150312699SNeil.Perrin@Sun.COM if (mybatch <= zilog->zl_com_batch) {
15043063Sperrin mutex_exit(&zilog->zl_lock);
15053063Sperrin return;
15063063Sperrin }
15073063Sperrin }
150810922SJeff.Bonwick@Sun.COM
150912699SNeil.Perrin@Sun.COM zilog->zl_next_batch++;
151012699SNeil.Perrin@Sun.COM zilog->zl_writer = B_TRUE;
151112699SNeil.Perrin@Sun.COM zil_commit_writer(zilog);
151212699SNeil.Perrin@Sun.COM zilog->zl_com_batch = mybatch;
151312699SNeil.Perrin@Sun.COM zilog->zl_writer = B_FALSE;
151412699SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock);
151510922SJeff.Bonwick@Sun.COM
151612699SNeil.Perrin@Sun.COM /* wake up one thread to become the next writer */
151712699SNeil.Perrin@Sun.COM cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
151810922SJeff.Bonwick@Sun.COM
151912699SNeil.Perrin@Sun.COM /* wake up all threads waiting for this batch to be committed */
152012699SNeil.Perrin@Sun.COM cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
152110922SJeff.Bonwick@Sun.COM }
152210922SJeff.Bonwick@Sun.COM
152310922SJeff.Bonwick@Sun.COM /*
1524789Sahrens * Called in syncing context to free committed log blocks and update log header.
1525789Sahrens */
1526789Sahrens void
zil_sync(zilog_t * zilog,dmu_tx_t * tx)1527789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1528789Sahrens {
15291807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog);
1530789Sahrens uint64_t txg = dmu_tx_get_txg(tx);
1531789Sahrens spa_t *spa = zilog->zl_spa;
153210922SJeff.Bonwick@Sun.COM uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1533789Sahrens lwb_t *lwb;
1534789Sahrens
15359396SMatthew.Ahrens@Sun.COM /*
15369396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try
15379396SMatthew.Ahrens@Sun.COM * to destroy it twice.
15389396SMatthew.Ahrens@Sun.COM */
15399396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1)
15409396SMatthew.Ahrens@Sun.COM return;
15419396SMatthew.Ahrens@Sun.COM
15421807Sbonwick mutex_enter(&zilog->zl_lock);
15431807Sbonwick
1544789Sahrens ASSERT(zilog->zl_stop_sync == 0);
1545789Sahrens
154610922SJeff.Bonwick@Sun.COM if (*replayed_seq != 0) {
154710922SJeff.Bonwick@Sun.COM ASSERT(zh->zh_replay_seq < *replayed_seq);
154810922SJeff.Bonwick@Sun.COM zh->zh_replay_seq = *replayed_seq;
154910922SJeff.Bonwick@Sun.COM *replayed_seq = 0;
155010922SJeff.Bonwick@Sun.COM }
1551789Sahrens
1552789Sahrens if (zilog->zl_destroy_txg == txg) {
15531807Sbonwick blkptr_t blk = zh->zh_log;
15541807Sbonwick
15551807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
15561807Sbonwick
15571807Sbonwick bzero(zh, sizeof (zil_header_t));
15588227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
15591807Sbonwick
15601807Sbonwick if (zilog->zl_keep_first) {
15611807Sbonwick /*
15621807Sbonwick * If this block was part of log chain that couldn't
15631807Sbonwick * be claimed because a device was missing during
15641807Sbonwick * zil_claim(), but that device later returns,
15651807Sbonwick * then this block could erroneously appear valid.
15661807Sbonwick * To guard against this, assign a new GUID to the new
15671807Sbonwick * log chain so it doesn't matter what blk points to.
15681807Sbonwick */
15691807Sbonwick zil_init_log_chain(zilog, &blk);
15701807Sbonwick zh->zh_log = blk;
15711807Sbonwick }
1572789Sahrens }
1573789Sahrens
15749701SGeorge.Wilson@Sun.COM while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
15752638Sperrin zh->zh_log = lwb->lwb_blk;
1576789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1577789Sahrens break;
1578789Sahrens list_remove(&zilog->zl_lwb_list, lwb);
157910922SJeff.Bonwick@Sun.COM zio_free_zil(spa, txg, &lwb->lwb_blk);
1580789Sahrens kmem_cache_free(zil_lwb_cache, lwb);
15813668Sgw25295
15823668Sgw25295 /*
15833668Sgw25295 * If we don't have anything left in the lwb list then
15843668Sgw25295 * we've had an allocation failure and we need to zero
15853668Sgw25295 * out the zil_header blkptr so that we don't end
15863668Sgw25295 * up freeing the same block twice.
15873668Sgw25295 */
15883668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL)
15893668Sgw25295 BP_ZERO(&zh->zh_log);
1590789Sahrens }
1591789Sahrens mutex_exit(&zilog->zl_lock);
1592789Sahrens }
1593789Sahrens
1594789Sahrens void
zil_init(void)1595789Sahrens zil_init(void)
1596789Sahrens {
1597789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
15982856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1599789Sahrens }
1600789Sahrens
1601789Sahrens void
zil_fini(void)1602789Sahrens zil_fini(void)
1603789Sahrens {
1604789Sahrens kmem_cache_destroy(zil_lwb_cache);
1605789Sahrens }
1606789Sahrens
160710310SNeil.Perrin@Sun.COM void
zil_set_sync(zilog_t * zilog,uint64_t sync)160812294SMark.Musante@Sun.COM zil_set_sync(zilog_t *zilog, uint64_t sync)
160912294SMark.Musante@Sun.COM {
161012294SMark.Musante@Sun.COM zilog->zl_sync = sync;
161112294SMark.Musante@Sun.COM }
161212294SMark.Musante@Sun.COM
161312294SMark.Musante@Sun.COM void
zil_set_logbias(zilog_t * zilog,uint64_t logbias)161410310SNeil.Perrin@Sun.COM zil_set_logbias(zilog_t *zilog, uint64_t logbias)
161510310SNeil.Perrin@Sun.COM {
161610310SNeil.Perrin@Sun.COM zilog->zl_logbias = logbias;
161710310SNeil.Perrin@Sun.COM }
161810310SNeil.Perrin@Sun.COM
1619789Sahrens zilog_t *
zil_alloc(objset_t * os,zil_header_t * zh_phys)1620789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1621789Sahrens {
1622789Sahrens zilog_t *zilog;
1623789Sahrens
1624789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1625789Sahrens
1626789Sahrens zilog->zl_header = zh_phys;
1627789Sahrens zilog->zl_os = os;
1628789Sahrens zilog->zl_spa = dmu_objset_spa(os);
1629789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os);
16301807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1;
163110310SNeil.Perrin@Sun.COM zilog->zl_logbias = dmu_objset_logbias(os);
163212294SMark.Musante@Sun.COM zilog->zl_sync = dmu_objset_syncprop(os);
163312699SNeil.Perrin@Sun.COM zilog->zl_next_batch = 1;
1634789Sahrens
16352856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
16362856Snd150628
163712699SNeil.Perrin@Sun.COM for (int i = 0; i < TXG_SIZE; i++) {
163812699SNeil.Perrin@Sun.COM mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
163912699SNeil.Perrin@Sun.COM MUTEX_DEFAULT, NULL);
164012699SNeil.Perrin@Sun.COM }
1641789Sahrens
1642789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1643789Sahrens offsetof(lwb_t, lwb_node));
1644789Sahrens
164512699SNeil.Perrin@Sun.COM list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
164612699SNeil.Perrin@Sun.COM offsetof(itx_t, itx_node));
164712699SNeil.Perrin@Sun.COM
16485688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
16495688Sbonwick
16505688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
16515688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1652789Sahrens
16535913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
16545913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
165512699SNeil.Perrin@Sun.COM cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
165612699SNeil.Perrin@Sun.COM cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
16575913Sperrin
1658789Sahrens return (zilog);
1659789Sahrens }
1660789Sahrens
1661789Sahrens void
zil_free(zilog_t * zilog)1662789Sahrens zil_free(zilog_t *zilog)
1663789Sahrens {
166412699SNeil.Perrin@Sun.COM lwb_t *head_lwb;
1665789Sahrens
1666789Sahrens zilog->zl_stop_sync = 1;
1667789Sahrens
166812699SNeil.Perrin@Sun.COM /*
166912699SNeil.Perrin@Sun.COM * After zil_close() there should only be one lwb with a buffer.
167012699SNeil.Perrin@Sun.COM */
167112699SNeil.Perrin@Sun.COM head_lwb = list_head(&zilog->zl_lwb_list);
167212699SNeil.Perrin@Sun.COM if (head_lwb) {
167312699SNeil.Perrin@Sun.COM ASSERT(head_lwb == list_tail(&zilog->zl_lwb_list));
167412699SNeil.Perrin@Sun.COM list_remove(&zilog->zl_lwb_list, head_lwb);
167512915SNeil.Perrin@Sun.COM zio_buf_free(head_lwb->lwb_buf, head_lwb->lwb_sz);
167612699SNeil.Perrin@Sun.COM kmem_cache_free(zil_lwb_cache, head_lwb);
1677789Sahrens }
1678789Sahrens list_destroy(&zilog->zl_lwb_list);
1679789Sahrens
16805688Sbonwick avl_destroy(&zilog->zl_vdev_tree);
16815688Sbonwick mutex_destroy(&zilog->zl_vdev_lock);
1682789Sahrens
168312699SNeil.Perrin@Sun.COM ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
168412699SNeil.Perrin@Sun.COM list_destroy(&zilog->zl_itx_commit_list);
168512699SNeil.Perrin@Sun.COM
168612699SNeil.Perrin@Sun.COM for (int i = 0; i < TXG_SIZE; i++) {
168712699SNeil.Perrin@Sun.COM /*
168812699SNeil.Perrin@Sun.COM * It's possible for an itx to be generated that doesn't dirty
168912699SNeil.Perrin@Sun.COM * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
169012699SNeil.Perrin@Sun.COM * callback to remove the entry. We remove those here.
169112699SNeil.Perrin@Sun.COM *
169212699SNeil.Perrin@Sun.COM * Also free up the ziltest itxs.
169312699SNeil.Perrin@Sun.COM */
169412699SNeil.Perrin@Sun.COM if (zilog->zl_itxg[i].itxg_itxs)
169512699SNeil.Perrin@Sun.COM zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
169612699SNeil.Perrin@Sun.COM mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
169712699SNeil.Perrin@Sun.COM }
169812699SNeil.Perrin@Sun.COM
16992856Snd150628 mutex_destroy(&zilog->zl_lock);
1700789Sahrens
17015913Sperrin cv_destroy(&zilog->zl_cv_writer);
17025913Sperrin cv_destroy(&zilog->zl_cv_suspend);
170312699SNeil.Perrin@Sun.COM cv_destroy(&zilog->zl_cv_batch[0]);
170412699SNeil.Perrin@Sun.COM cv_destroy(&zilog->zl_cv_batch[1]);
17055913Sperrin
1706789Sahrens kmem_free(zilog, sizeof (zilog_t));
1707789Sahrens }
1708789Sahrens
1709789Sahrens /*
1710789Sahrens * Open an intent log.
1711789Sahrens */
1712789Sahrens zilog_t *
zil_open(objset_t * os,zil_get_data_t * get_data)1713789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1714789Sahrens {
1715789Sahrens zilog_t *zilog = dmu_objset_zil(os);
1716789Sahrens
1717789Sahrens zilog->zl_get_data = get_data;
1718789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1719789Sahrens 2, 2, TASKQ_PREPOPULATE);
1720789Sahrens
1721789Sahrens return (zilog);
1722789Sahrens }
1723789Sahrens
1724789Sahrens /*
1725789Sahrens * Close an intent log.
1726789Sahrens */
1727789Sahrens void
zil_close(zilog_t * zilog)1728789Sahrens zil_close(zilog_t *zilog)
1729789Sahrens {
173012699SNeil.Perrin@Sun.COM lwb_t *tail_lwb;
173112699SNeil.Perrin@Sun.COM uint64_t txg = 0;
173212699SNeil.Perrin@Sun.COM
173312699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); /* commit all itx */
173412699SNeil.Perrin@Sun.COM
17351807Sbonwick /*
173612699SNeil.Perrin@Sun.COM * The lwb_max_txg for the stubby lwb will reflect the last activity
173712699SNeil.Perrin@Sun.COM * for the zil. After a txg_wait_synced() on the txg we know all the
173812699SNeil.Perrin@Sun.COM * callbacks have occurred that may clean the zil. Only then can we
173912699SNeil.Perrin@Sun.COM * destroy the zl_clean_taskq.
17401807Sbonwick */
174112699SNeil.Perrin@Sun.COM mutex_enter(&zilog->zl_lock);
174212699SNeil.Perrin@Sun.COM tail_lwb = list_tail(&zilog->zl_lwb_list);
174312699SNeil.Perrin@Sun.COM if (tail_lwb != NULL)
174412699SNeil.Perrin@Sun.COM txg = tail_lwb->lwb_max_txg;
174512699SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock);
174612699SNeil.Perrin@Sun.COM if (txg)
17471807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg);
17481807Sbonwick
1749789Sahrens taskq_destroy(zilog->zl_clean_taskq);
1750789Sahrens zilog->zl_clean_taskq = NULL;
1751789Sahrens zilog->zl_get_data = NULL;
1752789Sahrens }
1753789Sahrens
1754789Sahrens /*
1755789Sahrens * Suspend an intent log. While in suspended mode, we still honor
1756789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it.
1757789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot
1758789Sahrens * contains all the data it's supposed to, and has an empty intent log.
1759789Sahrens */
1760789Sahrens int
zil_suspend(zilog_t * zilog)1761789Sahrens zil_suspend(zilog_t *zilog)
1762789Sahrens {
17631807Sbonwick const zil_header_t *zh = zilog->zl_header;
1764789Sahrens
1765789Sahrens mutex_enter(&zilog->zl_lock);
17668989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */
1767789Sahrens mutex_exit(&zilog->zl_lock);
1768789Sahrens return (EBUSY);
1769789Sahrens }
17701807Sbonwick if (zilog->zl_suspend++ != 0) {
17711807Sbonwick /*
17721807Sbonwick * Someone else already began a suspend.
17731807Sbonwick * Just wait for them to finish.
17741807Sbonwick */
17751807Sbonwick while (zilog->zl_suspending)
17761807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
17771807Sbonwick mutex_exit(&zilog->zl_lock);
17781807Sbonwick return (0);
17791807Sbonwick }
17801807Sbonwick zilog->zl_suspending = B_TRUE;
1781789Sahrens mutex_exit(&zilog->zl_lock);
1782789Sahrens
178312699SNeil.Perrin@Sun.COM zil_commit(zilog, 0);
1784789Sahrens
17851807Sbonwick zil_destroy(zilog, B_FALSE);
17861807Sbonwick
17871807Sbonwick mutex_enter(&zilog->zl_lock);
17881807Sbonwick zilog->zl_suspending = B_FALSE;
17891807Sbonwick cv_broadcast(&zilog->zl_cv_suspend);
17901807Sbonwick mutex_exit(&zilog->zl_lock);
1791789Sahrens
1792789Sahrens return (0);
1793789Sahrens }
1794789Sahrens
1795789Sahrens void
zil_resume(zilog_t * zilog)1796789Sahrens zil_resume(zilog_t *zilog)
1797789Sahrens {
1798789Sahrens mutex_enter(&zilog->zl_lock);
1799789Sahrens ASSERT(zilog->zl_suspend != 0);
1800789Sahrens zilog->zl_suspend--;
1801789Sahrens mutex_exit(&zilog->zl_lock);
1802789Sahrens }
1803789Sahrens
1804789Sahrens typedef struct zil_replay_arg {
1805789Sahrens zil_replay_func_t **zr_replay;
1806789Sahrens void *zr_arg;
1807789Sahrens boolean_t zr_byteswap;
180810922SJeff.Bonwick@Sun.COM char *zr_lr;
1809789Sahrens } zil_replay_arg_t;
1810789Sahrens
181110922SJeff.Bonwick@Sun.COM static int
zil_replay_error(zilog_t * zilog,lr_t * lr,int error)181210922SJeff.Bonwick@Sun.COM zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
181310922SJeff.Bonwick@Sun.COM {
181410922SJeff.Bonwick@Sun.COM char name[MAXNAMELEN];
181510922SJeff.Bonwick@Sun.COM
181610922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq--; /* didn't actually replay this one */
181710922SJeff.Bonwick@Sun.COM
181810922SJeff.Bonwick@Sun.COM dmu_objset_name(zilog->zl_os, name);
181910922SJeff.Bonwick@Sun.COM
182010922SJeff.Bonwick@Sun.COM cmn_err(CE_WARN, "ZFS replay transaction error %d, "
182110922SJeff.Bonwick@Sun.COM "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
182210922SJeff.Bonwick@Sun.COM (u_longlong_t)lr->lrc_seq,
182310922SJeff.Bonwick@Sun.COM (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
182410922SJeff.Bonwick@Sun.COM (lr->lrc_txtype & TX_CI) ? "CI" : "");
182510922SJeff.Bonwick@Sun.COM
182610922SJeff.Bonwick@Sun.COM return (error);
182710922SJeff.Bonwick@Sun.COM }
182810922SJeff.Bonwick@Sun.COM
182910922SJeff.Bonwick@Sun.COM static int
zil_replay_log_record(zilog_t * zilog,lr_t * lr,void * zra,uint64_t claim_txg)1830789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1831789Sahrens {
1832789Sahrens zil_replay_arg_t *zr = zra;
18331807Sbonwick const zil_header_t *zh = zilog->zl_header;
1834789Sahrens uint64_t reclen = lr->lrc_reclen;
1835789Sahrens uint64_t txtype = lr->lrc_txtype;
183610922SJeff.Bonwick@Sun.COM int error = 0;
1837789Sahrens
183810922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq;
183910922SJeff.Bonwick@Sun.COM
184010922SJeff.Bonwick@Sun.COM if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
184110922SJeff.Bonwick@Sun.COM return (0);
1842789Sahrens
1843789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */
184410922SJeff.Bonwick@Sun.COM return (0);
1845789Sahrens
18465331Samw /* Strip case-insensitive bit, still present in log record */
18475331Samw txtype &= ~TX_CI;
18485331Samw
184910922SJeff.Bonwick@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE)
185010922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, EINVAL));
185110922SJeff.Bonwick@Sun.COM
185210922SJeff.Bonwick@Sun.COM /*
185310922SJeff.Bonwick@Sun.COM * If this record type can be logged out of order, the object
185410922SJeff.Bonwick@Sun.COM * (lr_foid) may no longer exist. That's legitimate, not an error.
185510922SJeff.Bonwick@Sun.COM */
185610922SJeff.Bonwick@Sun.COM if (TX_OOO(txtype)) {
185710922SJeff.Bonwick@Sun.COM error = dmu_object_info(zilog->zl_os,
185810922SJeff.Bonwick@Sun.COM ((lr_ooo_t *)lr)->lr_foid, NULL);
185910922SJeff.Bonwick@Sun.COM if (error == ENOENT || error == EEXIST)
186010922SJeff.Bonwick@Sun.COM return (0);
18618227SNeil.Perrin@Sun.COM }
18628227SNeil.Perrin@Sun.COM
1863789Sahrens /*
1864789Sahrens * Make a copy of the data so we can revise and extend it.
1865789Sahrens */
186610922SJeff.Bonwick@Sun.COM bcopy(lr, zr->zr_lr, reclen);
186710922SJeff.Bonwick@Sun.COM
186810922SJeff.Bonwick@Sun.COM /*
186910922SJeff.Bonwick@Sun.COM * If this is a TX_WRITE with a blkptr, suck in the data.
187010922SJeff.Bonwick@Sun.COM */
187110922SJeff.Bonwick@Sun.COM if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
187210922SJeff.Bonwick@Sun.COM error = zil_read_log_data(zilog, (lr_write_t *)lr,
187310922SJeff.Bonwick@Sun.COM zr->zr_lr + reclen);
187410922SJeff.Bonwick@Sun.COM if (error)
187510922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, error));
187610922SJeff.Bonwick@Sun.COM }
1877789Sahrens
1878789Sahrens /*
1879789Sahrens * The log block containing this lr may have been byteswapped
1880789Sahrens * so that we can easily examine common fields like lrc_txtype.
188110922SJeff.Bonwick@Sun.COM * However, the log is a mix of different record types, and only the
1882789Sahrens * replay vectors know how to byteswap their records. Therefore, if
1883789Sahrens * the lr was byteswapped, undo it before invoking the replay vector.
1884789Sahrens */
1885789Sahrens if (zr->zr_byteswap)
188610922SJeff.Bonwick@Sun.COM byteswap_uint64_array(zr->zr_lr, reclen);
1887789Sahrens
1888789Sahrens /*
1889789Sahrens * We must now do two things atomically: replay this log record,
18908227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that
18918227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number
18928227SNeil.Perrin@Sun.COM * is updated if we are in replay mode.
1893789Sahrens */
189410922SJeff.Bonwick@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
189510922SJeff.Bonwick@Sun.COM if (error) {
18963063Sperrin /*
18973063Sperrin * The DMU's dnode layer doesn't see removes until the txg
18983063Sperrin * commits, so a subsequent claim can spuriously fail with
18998227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out
190010922SJeff.Bonwick@Sun.COM * any removes then retry the transaction. Note that we
190110922SJeff.Bonwick@Sun.COM * specify B_FALSE for byteswap now, so we don't do it twice.
19023063Sperrin */
190310922SJeff.Bonwick@Sun.COM txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
190410922SJeff.Bonwick@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
190510922SJeff.Bonwick@Sun.COM if (error)
190610922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, error));
1907789Sahrens }
190810922SJeff.Bonwick@Sun.COM return (0);
19093063Sperrin }
1910789Sahrens
19113063Sperrin /* ARGSUSED */
191210922SJeff.Bonwick@Sun.COM static int
zil_incr_blks(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)19133063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
19143063Sperrin {
19153063Sperrin zilog->zl_replay_blks++;
191610922SJeff.Bonwick@Sun.COM
191710922SJeff.Bonwick@Sun.COM return (0);
1918789Sahrens }
1919789Sahrens
1920789Sahrens /*
19211362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it.
1922789Sahrens */
1923789Sahrens void
zil_replay(objset_t * os,void * arg,zil_replay_func_t * replay_func[TX_MAX_TYPE])19248227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1925789Sahrens {
1926789Sahrens zilog_t *zilog = dmu_objset_zil(os);
19271807Sbonwick const zil_header_t *zh = zilog->zl_header;
19281807Sbonwick zil_replay_arg_t zr;
19291362Sperrin
19308989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
19311807Sbonwick zil_destroy(zilog, B_TRUE);
19321362Sperrin return;
19331362Sperrin }
1934789Sahrens
1935789Sahrens zr.zr_replay = replay_func;
1936789Sahrens zr.zr_arg = arg;
19371807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
193810922SJeff.Bonwick@Sun.COM zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1939789Sahrens
1940789Sahrens /*
1941789Sahrens * Wait for in-progress removes to sync before starting replay.
1942789Sahrens */
1943789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0);
1944789Sahrens
19458227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE;
194611066Srafael.vanoni@sun.com zilog->zl_replay_time = ddi_get_lbolt();
19473063Sperrin ASSERT(zilog->zl_replay_blks == 0);
19483063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
19491807Sbonwick zh->zh_claim_txg);
195010922SJeff.Bonwick@Sun.COM kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
1951789Sahrens
19521807Sbonwick zil_destroy(zilog, B_FALSE);
19535712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
19548227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE;
1955789Sahrens }
19561646Sperrin
195710922SJeff.Bonwick@Sun.COM boolean_t
zil_replaying(zilog_t * zilog,dmu_tx_t * tx)195810922SJeff.Bonwick@Sun.COM zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
19591646Sperrin {
196012294SMark.Musante@Sun.COM if (zilog->zl_sync == ZFS_SYNC_DISABLED)
196110922SJeff.Bonwick@Sun.COM return (B_TRUE);
19621646Sperrin
196310922SJeff.Bonwick@Sun.COM if (zilog->zl_replay) {
196410922SJeff.Bonwick@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
196510922SJeff.Bonwick@Sun.COM zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
196610922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq;
196710922SJeff.Bonwick@Sun.COM return (B_TRUE);
19682638Sperrin }
19692638Sperrin
197010922SJeff.Bonwick@Sun.COM return (B_FALSE);
19711646Sperrin }
19729701SGeorge.Wilson@Sun.COM
19739701SGeorge.Wilson@Sun.COM /* ARGSUSED */
19749701SGeorge.Wilson@Sun.COM int
zil_vdev_offline(const char * osname,void * arg)197511209SMatthew.Ahrens@Sun.COM zil_vdev_offline(const char *osname, void *arg)
19769701SGeorge.Wilson@Sun.COM {
19779701SGeorge.Wilson@Sun.COM objset_t *os;
19789701SGeorge.Wilson@Sun.COM zilog_t *zilog;
19799701SGeorge.Wilson@Sun.COM int error;
19809701SGeorge.Wilson@Sun.COM
198110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os);
19829701SGeorge.Wilson@Sun.COM if (error)
19839701SGeorge.Wilson@Sun.COM return (error);
19849701SGeorge.Wilson@Sun.COM
19859701SGeorge.Wilson@Sun.COM zilog = dmu_objset_zil(os);
19869701SGeorge.Wilson@Sun.COM if (zil_suspend(zilog) != 0)
19879701SGeorge.Wilson@Sun.COM error = EEXIST;
19889701SGeorge.Wilson@Sun.COM else
19899701SGeorge.Wilson@Sun.COM zil_resume(zilog);
199010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG);
19919701SGeorge.Wilson@Sun.COM return (error);
19929701SGeorge.Wilson@Sun.COM }
1993