usr/src/uts/common/fs/zfs/vdev_label.c
author ahrens
Fri, 10 Mar 2006 16:27:46 -0800
changeset 1596 2e2377ccbf85
parent 1544 938876158511
child 1601 438b928f80c7
permissions -rw-r--r--
6395371 ASSERT in dmu_tx_count_free: blkid + i < dn->dn_phys->dn_nblkptr 6396359 infinite loop due to dangling dbufs (hang on unmount)
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
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
     5
 * Common Development and Distribution License (the "License").
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
    22
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
 * Virtual Device Labels
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
 * ---------------------
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
 * The vdev label serves several distinct purposes:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
 *	1. Uniquely identify this device as part of a ZFS pool and confirm its
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
 *	   identity within the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
 * 	2. Verify that all the devices given in a configuration are present
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
 *         within the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
 * 	3. Determine the uberblock for the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
 * 	4. In case of an import operation, determine the configuration of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
 *         toplevel vdev of which it is a part.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
 * 	5. If an import operation cannot find all the devices in the pool,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
 *         provide enough information to the administrator to determine which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 *         devices are missing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
 * It is important to note that while the kernel is responsible for writing the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
 * label, it only consumes the information in the first three cases.  The
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
 * latter information is only consumed in userland when determining the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
 * configuration to import a pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
 * Label Organization
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
 * ------------------
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
 * Before describing the contents of the label, it's important to understand how
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
 * the labels are written and updated with respect to the uberblock.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
 * When the pool configuration is altered, either because it was newly created
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
 * or a device was added, we want to update all the labels such that we can deal
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
 * with fatal failure at any point.  To this end, each disk has two labels which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
 * are updated before and after the uberblock is synced.  Assuming we have
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
 * labels and an uberblock with the following transacation groups:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
 *              L1          UB          L2
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
 *           +------+    +------+    +------+
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
 *           |      |    |      |    |      |
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
 *           | t10  |    | t10  |    | t10  |
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
 *           |      |    |      |    |      |
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
 *           +------+    +------+    +------+
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
 * In this stable state, the labels and the uberblock were all updated within
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
 * the same transaction group (10).  Each label is mirrored and checksummed, so
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
 * that we can detect when we fail partway through writing the label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
 * In order to identify which labels are valid, the labels are written in the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
 * following manner:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
 * 	1. For each vdev, update 'L1' to the new label
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
 * 	2. Update the uberblock
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
 * 	3. For each vdev, update 'L2' to the new label
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
 * Given arbitrary failure, we can determine the correct label to use based on
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
 * the transaction group.  If we fail after updating L1 but before updating the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
 * UB, we will notice that L1's transaction group is greater than the uberblock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
 * so L2 must be valid.  If we fail after writing the uberblock but before
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
 * writing L2, we will notice that L2's transaction group is less than L1, and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
 * therefore L1 is valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
 * Another added complexity is that not every label is updated when the config
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
 * is synced.  If we add a single device, we do not want to have to re-write
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
 * every label for every device in the pool.  This means that both L1 and L2 may
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
 * be older than the pool uberblock, because the necessary information is stored
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
 * on another vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
 * On-disk Format
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
 * --------------
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
 * The vdev label consists of two distinct parts, and is wrapped within the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
 * vdev_label_t structure.  The label includes 8k of padding to permit legacy
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
 * VTOC disk labels, but is otherwise ignored.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
 * The first half of the label is a packed nvlist which contains pool wide
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
 * properties, per-vdev properties, and configuration information.  It is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
 * described in more detail below.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
 * The latter half of the label consists of a redundant array of uberblocks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
 * These uberblocks are updated whenever a transaction group is committed,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
 * or when the configuration is updated.  When a pool is loaded, we scan each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
 * vdev for the 'best' uberblock.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
 * Configuration Information
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
 * -------------------------
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
 * The nvlist describing the pool and vdev contains the following elements:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
 * 	version		ZFS on-disk version
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
 * 	name		Pool name
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
 * 	state		Pool state
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
 * 	txg		Transaction group in which this label was written
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
 * 	pool_guid	Unique identifier for this pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
 * 	vdev_tree	An nvlist describing vdev tree.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
 * Each leaf device label also contains the following:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
 * 	top_guid	Unique ID for top-level vdev in which this is contained
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
 * 	guid		Unique ID for the leaf vdev
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
 * The 'vs' configuration follows the format described in 'spa_config.c'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
#include <sys/spa_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
#include <sys/zap.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
#include <sys/vdev.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
#include <sys/vdev_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
#include <sys/uberblock_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
#include <sys/metaslab.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
#include <sys/zio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
#include <sys/fs/zfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
 * Basic routines to read and write from a vdev label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
 * Used throughout the rest of this file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
vdev_label_offset(uint64_t psize, int l, uint64_t offset)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
	uint64_t size, zio_done_func_t *done, void *private)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
	ASSERT(vd->vdev_children == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
	zio_nowait(zio_read_phys(zio, vd,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
	    vdev_label_offset(vd->vdev_psize, l, offset),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   168
	    ZIO_PRIORITY_SYNC_READ,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   169
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
	uint64_t size, zio_done_func_t *done, void *private)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
	ASSERT(vd->vdev_children == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   177
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   178
	zio_nowait(zio_write_phys(zio, vd,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   179
	    vdev_label_offset(vd->vdev_psize, l, offset),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   180
	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   181
	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   182
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
 * Generate the nvlist representing this vdev's config.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
nvlist_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
vdev_config_generate(vdev_t *vd, int getstats)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
	nvlist_t *nv = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   192
	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
	    vd->vdev_ops->vdev_op_type) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	if (vd->vdev_path != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
		    vd->vdev_path) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
	if (vd->vdev_devid != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
		    vd->vdev_devid) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 789
diff changeset
   207
	if (vd->vdev_wholedisk != -1ULL)
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 789
diff changeset
   208
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 789
diff changeset
   209
		    vd->vdev_wholedisk) == 0);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 789
diff changeset
   210
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   211
	if (vd->vdev_not_present)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   212
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   213
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
	if (vd == vd->vdev_top) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
		    vd->vdev_ms_array) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
		    vd->vdev_ms_shift) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
		    vd->vdev_ashift) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
		    vd->vdev_asize) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
	if (vd->vdev_dtl.smo_object != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
		    vd->vdev_dtl.smo_object) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
	if (getstats) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
		vdev_stat_t vs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
		vdev_get_stats(vd, &vs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
	if (!vd->vdev_ops->vdev_op_leaf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
		nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
		int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
		    KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
		for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
			child[c] = vdev_config_generate(vd->vdev_child[c],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
			    getstats);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
		    child, vd->vdev_children) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
		for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
			nvlist_free(child[c]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   254
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   255
	} else {
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   256
		if (!vd->vdev_tmpoffline) {
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   257
		    if (vd->vdev_offline)
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   258
			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   259
				B_TRUE) == 0);
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   260
		    else
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   261
			(void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE,
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   262
				DATA_TYPE_UINT64);
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1171
diff changeset
   263
		}
789
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
	return (nv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
nvlist_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
vdev_label_read_config(vdev_t *vd)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	nvlist_t *config = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
	vdev_phys_t *vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
	int l;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
	if (vdev_is_dead(vd))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
	vp = zio_buf_alloc(sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
	for (l = 0; l < VDEV_LABELS; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   284
		zio = zio_root(vd->vdev_spa, NULL, NULL, ZIO_FLAG_CANFAIL |
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   285
		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
		vdev_label_read(zio, vd, l, vp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
		    offsetof(vdev_label_t, vl_vdev_phys),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
		    sizeof (vdev_phys_t), NULL, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
		if (zio_wait(zio) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   293
		    &config, 0) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
		if (config != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
			nvlist_free(config);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
			config = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
	zio_buf_free(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
	return (config);
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
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
vdev_label_init(vdev_t *vd, uint64_t crtxg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
	spa_t *spa = vd->vdev_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
	nvlist_t *label;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
	vdev_phys_t *vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
	vdev_boot_header_t *vb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
	uberblock_phys_t *ubphys;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
	int l, c, n;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
	char *buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
	size_t buflen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
	for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
		if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
	if (!vd->vdev_ops->vdev_op_leaf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
	 * Make sure each leaf device is writable, and zero its initial content.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
	 * Along the way, also make sure that no leaf is already in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
	 * Note that it's important to do this sequentially, not in parallel,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
	 * so that we catch cases of multiple use of the same leaf vdev in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
	 * the vdev we're creating -- e.g. mirroring a disk with itself.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
	if (vdev_is_dead(vd))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
		return (EIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
	 * Check whether this device is already in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
	 * Ignore the check if crtxg == 0, which we use for device removal.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   342
	if (crtxg != 0 &&
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   343
	    (label = vdev_label_read_config(vd)) != NULL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   344
		uint64_t state, pool_guid, device_guid, txg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
		uint64_t mycrtxg = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
		(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
		    &mycrtxg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   350
		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
		    &state) == 0 && state == POOL_STATE_ACTIVE &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
		    &pool_guid) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
		    &device_guid) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
		    spa_guid_exists(pool_guid, device_guid) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
		    &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
			dprintf("vdev %s in use, pool_state %d\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
			    vdev_description(vd), state);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
			nvlist_free(label);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
			return (EBUSY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
		nvlist_free(label);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
	}
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
	 * The device isn't in use, so initialize its label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
	vp = zio_buf_alloc(sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
	bzero(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
	 * Generate a label describing the pool and our top-level vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
	 * We mark it as being from txg 0 to indicate that it's not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
	 * really part of an active pool just yet.  The labels will
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
	 * be written again with a meaningful txg by spa_sync().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
	label = spa_config_generate(spa, vd, 0ULL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
	 * Add our creation time.  This allows us to detect multiple vdev
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
	 * uses as described above, and automatically expires if we fail.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
	VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
	buf = vp->vp_nvlist;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
	buflen = sizeof (vp->vp_nvlist);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   390
	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
		nvlist_free(label);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
		zio_buf_free(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
	 * Initialize boot block header.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
	bzero(vb, sizeof (vdev_boot_header_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
	vb->vb_magic = VDEV_BOOT_MAGIC;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
	vb->vb_version = VDEV_BOOT_VERSION;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
	vb->vb_offset = VDEV_BOOT_OFFSET;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
	vb->vb_size = VDEV_BOOT_SIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
	 * Initialize uberblock template.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
	bzero(ubphys, sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
	ubphys->ubp_uberblock = spa->spa_uberblock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	ubphys->ubp_uberblock.ub_txg = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
	 * Write everything in parallel.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
	zio = zio_root(spa, NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	for (l = 0; l < VDEV_LABELS; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
		vdev_label_write(zio, vd, l, vp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
		    offsetof(vdev_label_t, vl_vdev_phys),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
		    sizeof (vdev_phys_t), NULL, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
		vdev_label_write(zio, vd, l, vb,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
		    offsetof(vdev_label_t, vl_boot_header),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
		    sizeof (vdev_boot_header_t), NULL, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
			vdev_label_write(zio, vd, l, ubphys,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
			    offsetof(vdev_label_t, vl_uberblock[n]),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
			    sizeof (uberblock_phys_t), NULL, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	error = zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
	nvlist_free(label);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
	zio_buf_free(vb, sizeof (vdev_boot_header_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
	zio_buf_free(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
 * ==========================================================================
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
 * uberblock load/sync
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
 * ==========================================================================
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
 * Consider the following situation: txg is safely synced to disk.  We've
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
 * written the first uberblock for txg + 1, and then we lose power.  When we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
 * come back up, we fail to see the uberblock for txg + 1 because, say,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
 * it was on a mirrored device and the replica to which we wrote txg + 1
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
 * is now offline.  If we then make some changes and sync txg + 1, and then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
 * the missing replica comes back, then for a new seconds we'll have two
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
 * conflicting uberblocks on disk with the same txg.  The solution is simple:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
 * among uberblocks with equal txg, choose the one with the latest timestamp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
	if (ub1->ub_txg < ub2->ub_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
	if (ub1->ub_txg > ub2->ub_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
	if (ub1->ub_timestamp < ub2->ub_timestamp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
	if (ub1->ub_timestamp > ub2->ub_timestamp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
vdev_uberblock_load_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
	uberblock_phys_t *ubphys = zio->io_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	uberblock_t *ub = &ubphys->ubp_uberblock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
	uberblock_t *ubbest = zio->io_private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	spa_t *spa = zio->io_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   491
	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
		mutex_enter(&spa->spa_uberblock_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
		if (vdev_uberblock_compare(ub, ubbest) > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
			*ubbest = *ub;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
		mutex_exit(&spa->spa_uberblock_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
	zio_buf_free(zio->io_data, zio->io_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
	int l, c, n;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
	for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
	if (!vd->vdev_ops->vdev_op_leaf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
	if (vdev_is_dead(vd))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
	for (l = 0; l < VDEV_LABELS; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
			vdev_label_read(zio, vd, l,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
			    zio_buf_alloc(sizeof (uberblock_phys_t)),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
			    offsetof(vdev_label_t, vl_uberblock[n]),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
			    sizeof (uberblock_phys_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
			    vdev_uberblock_load_done, ubbest);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
}
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
 * Write the uberblock to both labels of all leaves of the specified vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
vdev_uberblock_sync_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
	uint64_t *good_writes = zio->io_root->io_private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
	if (zio->io_error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
		atomic_add_64(good_writes, 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
	uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
	int l, c, n;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
	for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
		vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
	if (!vd->vdev_ops->vdev_op_leaf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
	if (vdev_is_dead(vd))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
	n = txg & (VDEV_UBERBLOCKS - 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
	ASSERT(ubphys->ubp_uberblock.ub_txg == txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	for (l = 0; l < VDEV_LABELS; l++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
		vdev_label_write(zio, vd, l, ubphys,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
		    offsetof(vdev_label_t, vl_uberblock[n]),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
		    sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
	uberblock_phys_t *ubphys;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
	uint64_t *good_writes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
	bzero(ubphys, sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
	ubphys->ubp_uberblock = *ub;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
	zio = zio_root(spa, NULL, good_writes,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
	vdev_uberblock_sync(zio, ubphys, uvd, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
	error = zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
	if (error && *good_writes != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
		dprintf("partial success: good_writes = %llu\n", *good_writes);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
	 * It's possible to have no good writes and no error if every vdev is in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
	 * the CANT_OPEN state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
	if (*good_writes == 0 && error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
		error = EIO;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	kmem_free(good_writes, sizeof (uint64_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
 * Sync out an individual vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
vdev_sync_label_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
	uint64_t *good_writes = zio->io_root->io_private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
	if (zio->io_error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
		atomic_add_64(good_writes, 1);
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 void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
	nvlist_t *label;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
	vdev_phys_t *vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
	char *buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
	size_t buflen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
	for (c = 0; c < vd->vdev_children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
	if (!vd->vdev_ops->vdev_op_leaf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
	if (vdev_is_dead(vd))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
	 * Generate a label describing the top-level config to which we belong.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
	label = spa_config_generate(vd->vdev_spa, vd, txg, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
	vp = zio_buf_alloc(sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
	bzero(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
	buf = vp->vp_nvlist;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
	buflen = sizeof (vp->vp_nvlist);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   645
	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
		vdev_label_write(zio, vd, l, vp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
		    vdev_sync_label_done, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
	zio_buf_free(vp, sizeof (vdev_phys_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
	nvlist_free(label);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
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
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
	uint64_t *good_writes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
	ASSERT(vd == vd->vdev_top);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
	zio = zio_root(vd->vdev_spa, NULL, good_writes,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
	 * Recursively kick off writes to all labels.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	vdev_sync_label(zio, vd, l, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
	error = zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	if (error && *good_writes != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
		dprintf("partial success: good_writes = %llu\n", *good_writes);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	if (*good_writes == 0 && error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
		error = ENODEV;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
	kmem_free(good_writes, sizeof (uint64_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
 * Sync the entire vdev configuration.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
 * The order of operations is carefully crafted to ensure that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
 * if the system panics or loses power at any time, the state on disk
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
 * is still transactionally consistent.  The in-line comments below
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
 * describe the failure semantics at each stage.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
 * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
 * at any time, you can just call it again, and it will resume its work.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
spa_sync_labels(spa_t *spa, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
	uberblock_t *ub = &spa->spa_uberblock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
	vdev_t *rvd = spa->spa_root_vdev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
	vdev_t *vd, *uvd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
	int c, l, error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
	ASSERT(ub->ub_txg <= txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	 * If this isn't a resync due to I/O errors, and nothing changed
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
	 * in this transaction group, and the vdev configuration hasn't changed,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
	 * and this isn't an explicit sync-all, then there's nothing to do.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
	    list_is_empty(&spa->spa_dirty_list)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
		dprintf("nothing to sync in %s in txg %llu\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
		    spa_name(spa), txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	if (txg > spa_freeze_txg(spa))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
	 * Flush the write cache of every disk that's been written to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
	 * in this transaction group.  This ensures that all blocks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
	 * written in this txg will be committed to stable storage
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
	 * before any uberblock that references them.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
	zio = zio_root(spa, NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
		    NULL, NULL, ZIO_PRIORITY_NOW,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
	(void) zio_wait(zio);
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
	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
	 * system dies in the middle of this process, that's OK: all of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
	 * even labels that made it to disk will be newer than any uberblock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
	 * and will therefore be considered invalid.  The odd labels (L1, L3),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
	 * which have not yet been touched, will still be valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
	    vd = list_next(&spa->spa_dirty_list, vd)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
		for (l = 0; l < VDEV_LABELS; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
			if (l & 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
				return (error);
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
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
	 * Flush the new labels to disk.  This ensures that all even-label
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
	 * updates are committed to stable storage before the uberblock update.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
	zio = zio_root(spa, NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
	    vd = list_next(&spa->spa_dirty_list, vd)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
		    NULL, NULL, ZIO_PRIORITY_NOW,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	(void) zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
	 * Otherwise, pick one top-level vdev at random.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
	if (!list_is_empty(&spa->spa_dirty_list))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
		uvd = rvd;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
		uvd = rvd->vdev_child[spa_get_random(rvd->vdev_children)];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
	 * Sync the uberblocks.  If the system dies in the middle of this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
	 * step, there are two cases to consider, and the on-disk state
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
	 * is consistent either way:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
	 * (1)	If none of the new uberblocks made it to disk, then the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
	 *	previous uberblock will be the newest, and the odd labels
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
	 *	(which had not yet been touched) will be valid with respect
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
	 *	to that uberblock.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
	 * (2)	If one or more new uberblocks made it to disk, then they
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
	 *	will be the newest, and the even labels (which had all
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
	 *	been successfully committed) will be valid with respect
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
	 *	to the new uberblocks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
	 * Flush the uberblocks to disk.  This ensures that the odd labels
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	 * are no longer needed (because the new uberblocks and the even
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
	 * labels are safely on disk), so it is safe to overwrite them.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
	    NULL, NULL, ZIO_PRIORITY_NOW,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
	 * Sync out odd labels for every dirty vdev.  If the system dies
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	 * in the middle of this process, the even labels and the new
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
	 * uberblocks will suffice to open the pool.  The next time
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
	 * the pool is opened, the first thing we'll do -- before any
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	 * user data is modified -- is mark every vdev dirty so that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
	 * all labels will be brought up to date.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
	    vd = list_next(&spa->spa_dirty_list, vd)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
		for (l = 0; l < VDEV_LABELS; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
			if ((l & 1) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
				return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   831
	 * Flush the new labels to disk.  This ensures that all odd-label
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
	 * updates are committed to stable storage before the next
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
	 * transaction group begins.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
	zio = zio_root(spa, NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
	    vd = list_next(&spa->spa_dirty_list, vd)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   839
		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
		    NULL, NULL, ZIO_PRIORITY_NOW,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	(void) zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
	 * Clear the dirty list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
	while (!list_is_empty(&spa->spa_dirty_list))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
		vdev_config_clean(list_head(&spa->spa_dirty_list));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
#ifdef DEBUG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
	for (c = 0; c < rvd->vdev_children; c++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
		ASSERT(rvd->vdev_child[c]->vdev_is_dirty == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
}