usr/src/uts/common/fs/zfs/zil.c
author eschrock
Mon, 30 Jan 2006 21:34:28 -0800
changeset 1354 81359ee1ee63
parent 1141 ca4bb9237a10
child 1362 2b3ee2467364
permissions -rw-r--r--
6362672 import gets confused about overlapping slices 6364582 need to fixup paths if they've changed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     5
 * Common Development and Distribution License, Version 1.0 only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     6
 * (the "License").  You may not use this file except in compliance
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 * with the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/zap.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/arc.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/resource.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/zil.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/zil_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/dsl_dataset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/vdev.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
 * The zfs intent log (ZIL) saves transaction records of system calls
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
 * that change the file system in memory with enough information
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
 * to be able to replay them. These are stored in memory until
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
 * either the DMU transaction group (txg) commits them to the stable pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
 * and they can be discarded, or they are flushed to the stable log
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 * (also in the pool) due to a fsync, O_DSYNC or other synchronous
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
 * requirement. In the event of a panic or power fail then those log
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
 * records (transactions) are replayed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
 * There is one ZIL per file system. Its on-disk (pool) format consists
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
 * of 3 parts:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
 * 	- ZIL header
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
 * 	- ZIL blocks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
 * 	- ZIL records
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
 * A log record holds a system call transaction. Log blocks can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
 * hold many log records and the blocks are chained together.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
 * Each ZIL block contains a block pointer (blkptr_t) to the next
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
 * ZIL block in the chain. The ZIL header points to the first
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
 * block in the chain. Note there is not a fixed place in the pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
 * to hold blocks. They are dynamically allocated and freed as
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
 * needed from the blocks available. Figure X shows the ZIL structure:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
 * These global ZIL switches affect all pools
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
int zil_disable = 0;	/* disable intent logging */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
int zil_always = 0;	/* make every transaction synchronous */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
int zil_purge = 0;	/* at pool open, just throw everything away */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
int zil_noflush = 0;	/* don't flush write cache buffers on disks */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
static kmem_cache_t *zil_lwb_cache;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
zil_dva_compare(const void *x1, const void *x2)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
	const dva_t *dva1 = x1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
	const dva_t *dva2 = x2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
zil_dva_tree_init(avl_tree_t *t)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
	    offsetof(zil_dva_node_t, zn_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
zil_dva_tree_fini(avl_tree_t *t)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	zil_dva_node_t *zn;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
	void *cookie = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
		kmem_free(zn, sizeof (zil_dva_node_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
	avl_destroy(t);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
	zil_dva_node_t *zn;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
	avl_index_t where;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
	if (avl_find(t, dva, &where) != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
		return (EEXIST);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
	zn->zn_dva = *dva;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
	avl_insert(t, zn, where);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
 * Read a log block, make sure it's valid, and byteswap it if necessary.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
zil_read_log_block(zilog_t *zilog, blkptr_t *bp, char *buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
	uint64_t blksz = BP_GET_LSIZE(bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
	zil_trailer_t *ztp = (zil_trailer_t *)(buf + blksz) - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
	zio_cksum_t cksum;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	error = zio_wait(zio_read(NULL, zilog->zl_spa, bp, buf, blksz,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
	    NULL, NULL, ZIO_PRIORITY_SYNC_READ,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
		dprintf_bp(bp, "zilog %p bp %p read failed, error %d: ",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
		    zilog, bp, error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
	if (BP_SHOULD_BYTESWAP(bp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
		byteswap_uint64_array(buf, blksz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
	 * Sequence numbers should be... sequential.  The checksum verifier for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
	 * the next block should be: <logid[0], logid[1], objset id, seq + 1>.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
	cksum = bp->blk_cksum;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
	cksum.zc_word[3]++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
	if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
		dprintf_bp(bp, "zilog %p bp %p stale pointer: ", zilog, bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
		return (ESTALE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
	if (BP_IS_HOLE(&ztp->zit_next_blk)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
		dprintf_bp(bp, "zilog %p bp %p hole: ", zilog, bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
		return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
	if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
		dprintf("zilog %p bp %p nused exceeds blksz\n", zilog, bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
		return (EOVERFLOW);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
	dprintf_bp(bp, "zilog %p bp %p good block: ", zilog, bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   177
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   178
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   179
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   180
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   181
 * Parse the intent log, and call parse_func for each valid record within.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   182
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
    zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
	blkptr_t blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
	char *lrbuf, *lrp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
	zil_trailer_t *ztp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
	int reclen, error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
	blk = zilog->zl_header->zh_log;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
	if (BP_IS_HOLE(&blk))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
	 * Starting at the block pointed to by zh_log we read the log chain.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
	 * For each block in the chain we strongly check that block to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	 * ensure its validity.  We stop when an invalid block is found.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
	 * For each block pointer in the chain we call parse_blk_func().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
	 * For each record in each valid block we call parse_lr_func().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
	zil_dva_tree_init(&zilog->zl_dva_tree);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
		error = zil_read_log_block(zilog, &blk, lrbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
		if (parse_blk_func != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
			parse_blk_func(zilog, &blk, arg, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
		blk = ztp->zit_next_blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
		if (parse_lr_func == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
			lr_t *lr = (lr_t *)lrp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
			reclen = lr->lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
			ASSERT3U(reclen, >=, sizeof (lr_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
			parse_lr_func(zilog, lr, arg, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
	zil_dva_tree_fini(&zilog->zl_dva_tree);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
	spa_t *spa = zilog->zl_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
	dprintf_bp(bp, "first_txg %llu: ", first_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
	 * Claim log block if not already committed and not already claimed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
	if (bp->blk_birth >= first_txg &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
		ASSERT(err == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
	if (lrc->lrc_txtype == TX_WRITE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
		lr_write_t *lr = (lr_write_t *)lrc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	 * If we previously claimed it, we need to free it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
		lr_write_t *lr = (lr_write_t *)lrc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
		blkptr_t *bp = &lr->lr_blkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
		if (bp->blk_birth >= claim_txg &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
			(void) arc_free(NULL, zilog->zl_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
 * Create an on-disk intent log.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
zil_create(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
	lwb_t *lwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
	uint64_t txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
	dmu_tx_t *tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
	blkptr_t blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
	ASSERT(zilog->zl_header->zh_claim_txg == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
	ASSERT(zilog->zl_header->zh_replay_seq == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
	 * Initialize the log header block.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
	tx = dmu_tx_create(zilog->zl_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
	(void) dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
	txg = dmu_tx_get_txg(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
	 * Allocate the first log block and assign its checksum verifier.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
	error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
	    ZIL_MIN_BLKSZ, &blk, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
	if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
		ZIO_SET_CHECKSUM(&blk.blk_cksum,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
		    spa_get_random(-1ULL), spa_get_random(-1ULL),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
		    dmu_objset_id(zilog->zl_os), 1ULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
		 * Allocate a log write buffer (lwb) for the first log block.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
		lwb->lwb_zilog = zilog;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
		lwb->lwb_blk = blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
		lwb->lwb_nused = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
		lwb->lwb_max_txg = txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
		lwb->lwb_seq = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
		lwb->lwb_state = UNWRITTEN;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
		mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
		list_insert_tail(&zilog->zl_lwb_list, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
	txg_wait_synced(zilog->zl_dmu_pool, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
 * In one tx, free all log blocks and clear the log header.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
zil_destroy(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
	dmu_tx_t *tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
	uint64_t txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
	mutex_enter(&zilog->zl_destroy_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
	if (BP_IS_HOLE(&zilog->zl_header->zh_log)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
		mutex_exit(&zilog->zl_destroy_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
	tx = dmu_tx_create(zilog->zl_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
	(void) dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
	txg = dmu_tx_get_txg(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
	zil_parse(zilog, zil_free_log_block, zil_free_log_record, tx,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
	    zilog->zl_header->zh_claim_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
	zilog->zl_destroy_txg = txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
	txg_wait_synced(zilog->zl_dmu_pool, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
	mutex_exit(&zilog->zl_destroy_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
zil_claim(char *osname, void *txarg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
	dmu_tx_t *tx = txarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
	uint64_t first_txg = dmu_tx_get_txg(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
	zilog_t *zilog;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
	zil_header_t *zh;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
	objset_t *os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
		cmn_err(CE_WARN, "can't process intent log for %s", osname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
	zilog = dmu_objset_zil(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
	zh = zilog->zl_header;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
	 * Claim all log blocks if we haven't already done so.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
		zh->zh_claim_txg = first_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
		zil_parse(zilog, zil_claim_log_block, zil_claim_log_record,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
		    tx, first_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
		dsl_dataset_dirty(dmu_objset_ds(os), tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
	dmu_objset_close(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
	zil_vdev_t *zv;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	if (zil_noflush)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
	ASSERT(MUTEX_HELD(&zilog->zl_lock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
	zv->vdev = vdev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	zv->seq = seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
	list_insert_tail(&zilog->zl_vdev_list, zv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
	vdev_t *vd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	zil_vdev_t *zv, *zv2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
	spa_t *spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
	uint64_t vdev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
	if (zil_noflush)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
	ASSERT(MUTEX_HELD(&zilog->zl_lock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	spa = zilog->zl_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	zio = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
	    zv->seq <= seq) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
		vdev = zv->vdev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		list_remove(&zilog->zl_vdev_list, zv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
		kmem_free(zv, sizeof (zil_vdev_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
		 * remove all chained entries <= seq with same vdev
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
		zv = list_head(&zilog->zl_vdev_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
		while (zv && zv->seq <= seq) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
			zv2 = list_next(&zilog->zl_vdev_list, zv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
			if (zv->vdev == vdev) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
				list_remove(&zilog->zl_vdev_list, zv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
				kmem_free(zv, sizeof (zil_vdev_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
			zv = zv2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
		/* flush the write cache for this vdev */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
		if (zio == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
		vd = vdev_lookup_top(spa, vdev);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
		ASSERT(vd);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
		    NULL, NULL, ZIO_PRIORITY_NOW,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
		mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
	 * Wait for all the flushes to complete.  Not all devices actually
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
	 */
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   468
	if (zio != NULL) {
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   469
		mutex_exit(&zilog->zl_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
		(void) zio_wait(zio);
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   471
		mutex_enter(&zilog->zl_lock);
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   472
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
 * Function called when a log block write completes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
zil_lwb_write_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
	lwb_t *prev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
	lwb_t *lwb = zio->io_private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
	zilog_t *zilog = lwb->lwb_zilog;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
	uint64_t max_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	 * Now that we've written this log block, we have a stable pointer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
	 * to the next block in the chain, so it's OK to let the txg in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	 * which we allocated the next block sync.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
	txg_rele_to_sync(&lwb->lwb_txgh);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
	lwb->lwb_buf = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	if (zio->io_error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
		zilog->zl_log_error = B_TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
		cv_broadcast(&zilog->zl_cv_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
	prev = list_prev(&zilog->zl_lwb_list, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
	if (prev && prev->lwb_state != SEQ_COMPLETE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
		/* There's an unwritten buffer in the chain before this one */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
		lwb->lwb_state = SEQ_INCOMPLETE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
	max_seq = lwb->lwb_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
	lwb->lwb_state = SEQ_COMPLETE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
	 * We must also follow up the chain for already written buffers
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
	 * to see if we can set zl_ss_seq even higher.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
		if (lwb->lwb_state != SEQ_INCOMPLETE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
		lwb->lwb_state = SEQ_COMPLETE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
		/* lwb_seq will be zero if we've written an empty buffer */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
		if (lwb->lwb_seq) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
			ASSERT3U(max_seq, <, lwb->lwb_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
			max_seq = lwb->lwb_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
	cv_broadcast(&zilog->zl_cv_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
 * Start a log block write and advance to the next log block.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
 * Calls are serialized.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
static lwb_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
	lwb_t *nlwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
	uint64_t txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
	uint64_t zil_blksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
	 * Allocate the next block and save its address in this block
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
	 * before writing it in order to establish the log chain.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
	 * Note that if the allocation of nlwb synced before we wrote
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
	 * the block that points at it (lwb), we'd leak it if we crashed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
	txg_rele_to_quiesce(&lwb->lwb_txgh);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	/*
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   558
	 * Pick a ZIL blocksize. We request a size that is the
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   559
	 * maximum of the previous used size, the current used size and
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   560
	 * the amount waiting in the queue.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
	 */
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   562
	zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used);
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   563
	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   564
	zil_blksz = P2ROUNDUP(zil_blksz, ZIL_MIN_BLKSZ);
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   565
	if (zil_blksz > ZIL_MAX_BLKSZ)
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   566
		zil_blksz = ZIL_MAX_BLKSZ;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
	error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
	    zil_blksz, &ztp->zit_next_blk, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
		txg_rele_to_sync(&lwb->lwb_txgh);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
	ASSERT3U(ztp->zit_next_blk.blk_birth, ==, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
	ztp->zit_nused = lwb->lwb_nused;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
	ztp->zit_next_blk.blk_cksum = lwb->lwb_blk.blk_cksum;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
	ztp->zit_next_blk.blk_cksum.zc_word[3]++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
	 * Allocate a new log write buffer (lwb).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
	nlwb->lwb_zilog = zilog;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
	nlwb->lwb_blk = ztp->zit_next_blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
	nlwb->lwb_nused = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
	nlwb->lwb_max_txg = txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
	nlwb->lwb_seq = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
	nlwb->lwb_state = UNWRITTEN;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
	 * Put new lwb at the end of the log chain,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
	 * and record the vdev for later flushing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
	list_insert_tail(&zilog->zl_lwb_list, nlwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
	    lwb->lwb_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
	 * write the old log block
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
	zio_nowait(zio_rewrite(NULL, zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
	    &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
	return (nlwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
static lwb_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
	lr_t *lrc = &itx->itx_lr; /* common log record */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
	uint64_t seq = lrc->lrc_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
	uint64_t txg = lrc->lrc_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
	uint64_t reclen = lrc->lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
	if (lwb == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
	ASSERT(lwb->lwb_buf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
	 * If it's a write, fetch the data or get its blkptr as appropriate.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
	if (lrc->lrc_txtype == TX_WRITE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
		lr_write_t *lr = (lr_write_t *)lrc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
		if (txg > spa_freeze_txg(zilog->zl_spa))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
			txg_wait_synced(zilog->zl_dmu_pool, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
		if (!itx->itx_data_copied &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
		    (error = zilog->zl_get_data(itx->itx_private, lr)) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
			if (error != ENOENT && error != EALREADY) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
				txg_wait_synced(zilog->zl_dmu_pool, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
				mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
				zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
				zil_add_vdev(zilog,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
				    DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
				    seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
				mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
				return (lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
			mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
			zil_add_vdev(zilog,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
			    DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))), seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
			mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
			return (lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   657
	zilog->zl_cur_used += reclen;
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   658
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
	 * If this record won't fit in the current log block, start a new one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
	if (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
		lwb = zil_lwb_write_start(zilog, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
		if (lwb == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
			return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
		if (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
			txg_wait_synced(zilog->zl_dmu_pool, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
			mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
			mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
			return (lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
	lwb->lwb_nused += reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
	ASSERT3U(lwb->lwb_seq, <, seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	lwb->lwb_seq = seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
	return (lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
itx_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
zil_itx_create(int txtype, size_t lrsize)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
	itx_t *itx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
	lrsize = P2ROUNDUP(lrsize, sizeof (uint64_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
	itx->itx_lr.lrc_txtype = txtype;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
	itx->itx_lr.lrc_reclen = lrsize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
	itx->itx_lr.lrc_seq = 0;	/* defensive */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
	return (itx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
	uint64_t seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
	ASSERT(itx->itx_lr.lrc_seq == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
	list_insert_tail(&zilog->zl_itx_list, itx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
	return (seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
 * Free up all in-memory intent log transactions that have now been synced.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
zil_itx_clean(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
	uint64_t max_seq = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
	itx_t *itx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
		list_remove(&zilog->zl_itx_list, itx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
		max_seq = itx->itx_lr.lrc_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
		kmem_free(itx, offsetof(itx_t, itx_lr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
		    + itx->itx_lr.lrc_reclen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
	if (max_seq > zilog->zl_ss_seq) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
		zilog->zl_ss_seq = max_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
		cv_broadcast(&zilog->zl_cv_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
zil_clean(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
	 * Check for any log blocks that can be freed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
	 * Log blocks are only freed when the log block allocation and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
	 * log records contained within are both known to be committed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
	if (list_head(&zilog->zl_itx_list) != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
		(void) taskq_dispatch(zilog->zl_clean_taskq,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
 * Push zfs transactions to stable storage up to the supplied sequence number.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
	uint64_t txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
	uint64_t max_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
	uint64_t reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
	itx_t *itx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
	lwb_t *lwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
	spa_t *spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	if (zilog == NULL || seq == 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
	spa = zilog->zl_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
			cv_signal(&zilog->zl_cv_write);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
			mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
			return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
	zilog->zl_writer = B_TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
	max_seq = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
	if (zilog->zl_suspend) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
		lwb = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
		lwb = list_tail(&zilog->zl_lwb_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
		if (lwb == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
			mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
			zil_create(zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
			mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
			lwb = list_tail(&zilog->zl_lwb_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	 * Loop through in-memory log transactions filling log blocks,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
	 * until we reach the given sequence number and there's no more
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	 * room in the write buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
		itx = list_head(&zilog->zl_itx_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
		if (itx == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
		reclen = itx->itx_lr.lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
		if ((itx->itx_lr.lrc_seq > seq) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
		    ZIL_BLK_DATA_SZ(lwb))))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
		list_remove(&zilog->zl_itx_list, itx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
		txg = itx->itx_lr.lrc_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
		ASSERT(txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   831
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
		if (txg > spa_last_synced_txg(spa) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
		    txg > spa_freeze_txg(spa))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
			lwb = zil_lwb_commit(zilog, itx, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
			max_seq = itx->itx_lr.lrc_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
		kmem_free(itx, offsetof(itx_t, itx_lr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
		    + itx->itx_lr.lrc_reclen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   839
		mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
		zilog->zl_itx_list_sz -= reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	/* write the last block out */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
	if (lwb != NULL && lwb->lwb_nused != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
		lwb = zil_lwb_write_start(zilog, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   849
	zilog->zl_prev_used = zilog->zl_cur_used;
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   850
	zilog->zl_cur_used = 0;
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   851
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
	if (max_seq > zilog->zl_ss_seq) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
		zilog->zl_ss_seq = max_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
		cv_broadcast(&zilog->zl_cv_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
	 * Wait if necessary for our seq to be committed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
	if (lwb) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
		while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
		zil_flush_vdevs(zilog, seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	}
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   865
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	if (zilog->zl_log_error || lwb == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
		zilog->zl_log_error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
		max_seq = zilog->zl_itx_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
		txg_wait_synced(zilog->zl_dmu_pool, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
		mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
		cv_broadcast(&zilog->zl_cv_seq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
	}
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   875
	/* wake up others waiting to start a write */
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   876
	zilog->zl_writer = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
	mutex_exit(&zilog->zl_lock);
1141
ca4bb9237a10 6354547 sticky log buf size
perrin
parents: 789
diff changeset
   878
	cv_signal(&zilog->zl_cv_write);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
 * Called in syncing context to free committed log blocks and update log header.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
zil_sync(zilog_t *zilog, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
	uint64_t txg = dmu_tx_get_txg(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
	spa_t *spa = zilog->zl_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
	lwb_t *lwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
	ASSERT(zilog->zl_stop_sync == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   893
	zilog->zl_header->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
	if (zilog->zl_destroy_txg == txg) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
		bzero(zilog->zl_header, sizeof (zil_header_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
		zilog->zl_destroy_txg = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   903
		lwb = list_head(&zilog->zl_lwb_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   904
		if (lwb == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   905
			mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   906
			return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   907
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   908
		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   909
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   910
		list_remove(&zilog->zl_lwb_list, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   911
		zio_free_blk(spa, &lwb->lwb_blk, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   912
		kmem_cache_free(zil_lwb_cache, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   914
	zilog->zl_header->zh_log = lwb->lwb_blk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   916
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   917
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   918
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   919
zil_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   920
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   922
	    sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   923
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   925
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
zil_fini(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
	kmem_cache_destroy(zil_lwb_cache);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
zilog_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
zil_alloc(objset_t *os, zil_header_t *zh_phys)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
	zilog_t *zilog;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
	zilog->zl_header = zh_phys;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
	zilog->zl_os = os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
	zilog->zl_spa = dmu_objset_spa(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
	zilog->zl_dmu_pool = dmu_objset_pool(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
	list_create(&zilog->zl_itx_list, sizeof (itx_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
	    offsetof(itx_t, itx_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   945
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   946
	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   947
	    offsetof(lwb_t, lwb_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   950
	    offsetof(zil_vdev_t, vdev_seq_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
	return (zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   955
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
zil_free(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
	lwb_t *lwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
	zil_vdev_t *zv;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
	zilog->zl_stop_sync = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
		list_remove(&zilog->zl_lwb_list, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   965
		if (lwb->lwb_buf != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   966
			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
		kmem_cache_free(zil_lwb_cache, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   968
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
	list_destroy(&zilog->zl_lwb_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   970
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   971
	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
		list_remove(&zilog->zl_vdev_list, zv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
		kmem_free(zv, sizeof (zil_vdev_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
	list_destroy(&zilog->zl_vdev_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   977
	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
	list_destroy(&zilog->zl_itx_list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
	kmem_free(zilog, sizeof (zilog_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
 * Open an intent log.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
zilog_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
zil_open(objset_t *os, zil_get_data_t *get_data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
	zilog_t *zilog = dmu_objset_zil(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   991
	zilog->zl_get_data = get_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
	    2, 2, TASKQ_PREPOPULATE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
	return (zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   998
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
 * Close an intent log.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
zil_close(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1003
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1004
	txg_wait_synced(zilog->zl_dmu_pool, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1005
	taskq_destroy(zilog->zl_clean_taskq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1006
	zilog->zl_clean_taskq = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
	zilog->zl_get_data = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
	zil_itx_clean(zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
 * Suspend an intent log.  While in suspended mode, we still honor
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
 * synchronous semantics, but we rely on txg_wait_synced() to do it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1016
 * We suspend the log briefly when taking a snapshot so that the snapshot
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1017
 * contains all the data it's supposed to, and has an empty intent log.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
zil_suspend(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
	lwb_t *lwb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
	if (zilog->zl_header->zh_claim_txg != 0) {	/* unplayed log */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
		mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
		return (EBUSY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
	zilog->zl_suspend++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1031
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
	zil_commit(zilog, UINT64_MAX, FSYNC);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
		if (lwb->lwb_buf != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
			 * Wait for the buffer if it's in the process of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
			 * being written.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
			if ((lwb->lwb_seq != 0) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
			    (lwb->lwb_state != SEQ_COMPLETE)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1043
				cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1044
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1045
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1047
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1048
		list_remove(&zilog->zl_lwb_list, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
		kmem_cache_free(zil_lwb_cache, lwb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1052
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1053
	zil_destroy(zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1057
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1058
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1059
zil_resume(zilog_t *zilog)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1060
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1061
	mutex_enter(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
	ASSERT(zilog->zl_suspend != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
	zilog->zl_suspend--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
	mutex_exit(&zilog->zl_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
typedef struct zil_replay_arg {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
	objset_t	*zr_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
	zil_replay_func_t **zr_replay;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
	void		*zr_arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
	void		(*zr_rm_sync)(void *arg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
	uint64_t	*zr_txgp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
	boolean_t	zr_byteswap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
	char		*zr_lrbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
} zil_replay_arg_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1077
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1078
zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
	zil_replay_arg_t *zr = zra;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
	zil_header_t *zh = zilog->zl_header;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
	uint64_t reclen = lr->lrc_reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
	uint64_t txtype = lr->lrc_txtype;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
	int pass, error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1085
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
	if (zilog->zl_stop_replay)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
	if (lr->lrc_txg < claim_txg)		/* already committed */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1090
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1092
	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1094
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1096
	 * Make a copy of the data so we can revise and extend it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
	bcopy(lr, zr->zr_lrbuf, reclen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1099
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1100
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
	 * The log block containing this lr may have been byteswapped
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
	 * so that we can easily examine common fields like lrc_txtype.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
	 * However, the log is a mix of different data types, and only the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
	 * replay vectors know how to byteswap their records.  Therefore, if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
	 * the lr was byteswapped, undo it before invoking the replay vector.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
	if (zr->zr_byteswap)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1108
		byteswap_uint64_array(zr->zr_lrbuf, reclen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1110
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1111
	 * If this is a TX_WRITE with a blkptr, suck in the data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1112
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
		lr_write_t *lrw = (lr_write_t *)lr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
		blkptr_t *wbp = &lrw->lr_blkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1116
		uint64_t wlen = lrw->lr_length;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1117
		char *wbuf = zr->zr_lrbuf + reclen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1118
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1119
		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1120
			bzero(wbuf, wlen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1121
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
			 * A subsequent write may have overwritten this block,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1124
			 * in which case wbp may have been been freed and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1125
			 * reallocated, and our read of wbp may fail with a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1126
			 * checksum error.  We can safely ignore this because
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1127
			 * the later write will provide the correct data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1128
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1129
			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1130
			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1131
			    ZIO_PRIORITY_SYNC_READ,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1132
			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1133
			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
	 * We must now do two things atomically: replay this log record,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
	 * and update the log header to reflect the fact that we did so.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
	 * We use the DMU's ability to assign into a specific txg to do this.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
		uint64_t replay_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
		dmu_tx_t *replay_tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1146
		replay_tx = dmu_tx_create(zr->zr_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
		error = dmu_tx_assign(replay_tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
			dmu_tx_abort(replay_tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
		replay_txg = dmu_tx_get_txg(replay_tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
			error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
			 * On the first pass, arrange for the replay vector
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
			 * to fail its dmu_tx_assign().  That's the only way
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
			 * to ensure that those code paths remain well tested.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
			*zr->zr_txgp = replay_txg - (pass == 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
			    zr->zr_byteswap);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
			*zr->zr_txgp = TXG_NOWAIT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1167
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
		if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1170
			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1171
			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1172
			    lr->lrc_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1173
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
		dmu_tx_commit(replay_tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
		if (error != ERESTART)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1178
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
		if (pass != 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
			txg_wait_open(spa_get_dsl(zilog->zl_spa),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
			    replay_txg + 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
		dprintf("pass %d, retrying\n", pass);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1187
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
		dmu_objset_name(zr->zr_os, name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
		    "dataset %s, seq 0x%llx, txtype %llu\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
		    error, name,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
		zilog->zl_stop_replay = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
		kmem_free(name, MAXNAMELEN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
	 * The DMU's dnode layer doesn't see removes until the txg commits,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
	 * so a subsequent claim can spuriously fail with EEXIST.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
	 * To prevent this, if we might have removed an object,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
	 * wait for the delete thread to delete it, and then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
	 * wait for the transaction group to sync.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
		if (zr->zr_rm_sync != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
			zr->zr_rm_sync(zr->zr_arg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1210
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1213
 * If this dataset has an intent log, replay it and destroy it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1214
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1215
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
zil_replay(objset_t *os, void *arg, uint64_t *txgp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1217
	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1218
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1219
	zilog_t *zilog = dmu_objset_zil(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1220
	zil_replay_arg_t zr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1221
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1222
	zr.zr_os = os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1223
	zr.zr_replay = replay_func;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1224
	zr.zr_arg = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1225
	zr.zr_rm_sync = rm_sync;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1226
	zr.zr_txgp = txgp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1227
	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zilog->zl_header->zh_log);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1228
	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1229
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1230
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1231
	 * Wait for in-progress removes to sync before starting replay.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
	if (rm_sync != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1234
		rm_sync(arg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1235
	txg_wait_synced(zilog->zl_dmu_pool, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
	zilog->zl_stop_replay = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
	zil_parse(zilog, NULL, zil_replay_log_record, &zr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1239
	    zilog->zl_header->zh_claim_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1240
	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
	zil_destroy(zilog);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
}