usr/src/lib/libzfs/common/libzfs_pool.c
author mmusante
Wed, 26 Mar 2008 09:44:41 -0700
changeset 6289 9a83171c70d6
parent 5621 cd0984d5b1c1
child 6423 437422a29d3a
permissions -rw-r--r--
6603209 Multiple -o options to 'zfs mount' ignore all but the last. 6617720 zpool_label_disk() doesn't correctly indicate failure 6676119 zfs unmount by pathname will unmount /var/run even when /var is set canmount=noauto
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: 1354
diff changeset
     5
 * Common Development and Distribution License (the "License").
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1354
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
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
    21
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
    23
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
789
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
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    29
#include <alloca.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <assert.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <ctype.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <devid.h>
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    34
#include <dirent.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <fcntl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <libintl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <stdlib.h>
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    39
#include <strings.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <unistd.h>
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
    41
#include <sys/efi_partition.h>
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
    42
#include <sys/vtoc.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <sys/zfs_ioctl.h>
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
    44
#include <sys/zio.h>
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
    45
#include <strings.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include "zfs_namecheck.h"
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
    48
#include "zfs_prop.h"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
#include "libzfs_impl.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    51
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    52
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    53
 * ====================================================================
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    54
 *   zpool property functions
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    55
 * ====================================================================
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    56
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    57
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    58
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    59
zpool_get_all_props(zpool_handle_t *zhp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    60
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    61
	zfs_cmd_t zc = { 0 };
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    62
	libzfs_handle_t *hdl = zhp->zpool_hdl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    63
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    64
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    65
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    66
	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    67
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    68
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    69
	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    70
		if (errno == ENOMEM) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    71
			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    72
				zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    73
				return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    74
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    75
		} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    76
			zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    77
			return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    78
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    79
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    80
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    81
	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    82
		zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    83
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    84
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    85
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    86
	zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    87
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    88
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    89
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    90
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    91
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    92
zpool_props_refresh(zpool_handle_t *zhp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    93
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    94
	nvlist_t *old_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    95
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    96
	old_props = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    97
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    98
	if (zpool_get_all_props(zhp) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    99
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   100
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   101
	nvlist_free(old_props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   102
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   103
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   104
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   105
static char *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   106
zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   107
    zprop_source_t *src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   108
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   109
	nvlist_t *nv, *nvl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   110
	uint64_t ival;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   111
	char *value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   112
	zprop_source_t source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   113
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   114
	nvl = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   115
	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   116
		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   117
		source = ival;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   118
		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   119
	} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   120
		source = ZPROP_SRC_DEFAULT;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   121
		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   122
			value = "-";
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   123
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   124
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   125
	if (src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   126
		*src = source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   127
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   128
	return (value);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   129
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   130
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   131
uint64_t
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   132
zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   133
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   134
	nvlist_t *nv, *nvl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   135
	uint64_t value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   136
	zprop_source_t source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   137
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   138
	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   139
		return (zpool_prop_default_numeric(prop));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   140
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   141
	nvl = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   142
	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   143
		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   144
		source = value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   145
		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   146
	} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   147
		source = ZPROP_SRC_DEFAULT;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   148
		value = zpool_prop_default_numeric(prop);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   149
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   150
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   151
	if (src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   152
		*src = source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   153
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   154
	return (value);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   155
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   156
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   157
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   158
 * Map VDEV STATE to printed strings.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   159
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   160
char *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   161
zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   162
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   163
	switch (state) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   164
	case VDEV_STATE_CLOSED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   165
	case VDEV_STATE_OFFLINE:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   166
		return (gettext("OFFLINE"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   167
	case VDEV_STATE_REMOVED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   168
		return (gettext("REMOVED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   169
	case VDEV_STATE_CANT_OPEN:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   170
		if (aux == VDEV_AUX_CORRUPT_DATA)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   171
			return (gettext("FAULTED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   172
		else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   173
			return (gettext("UNAVAIL"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   174
	case VDEV_STATE_FAULTED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   175
		return (gettext("FAULTED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   176
	case VDEV_STATE_DEGRADED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   177
		return (gettext("DEGRADED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   178
	case VDEV_STATE_HEALTHY:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   179
		return (gettext("ONLINE"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   180
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   181
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   182
	return (gettext("UNKNOWN"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   183
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   184
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   185
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   186
 * Get a zpool property value for 'prop' and return the value in
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   187
 * a pre-allocated buffer.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   188
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   189
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   190
zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   191
    zprop_source_t *srctype)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   192
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   193
	uint64_t intval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   194
	const char *strval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   195
	zprop_source_t src = ZPROP_SRC_NONE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   196
	nvlist_t *nvroot;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   197
	vdev_stat_t *vs;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   198
	uint_t vsc;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   199
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   200
	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   201
		if (prop == ZPOOL_PROP_NAME)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   202
			(void) strlcpy(buf, zpool_get_name(zhp), len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   203
		else if (prop == ZPOOL_PROP_HEALTH)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   204
			(void) strlcpy(buf, "FAULTED", len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   205
		else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   206
			(void) strlcpy(buf, "-", len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   207
		return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   208
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   209
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   210
	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   211
	    prop != ZPOOL_PROP_NAME)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   212
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   213
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   214
	switch (zpool_prop_get_type(prop)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   215
	case PROP_TYPE_STRING:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   216
		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   217
		    len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   218
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   219
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   220
	case PROP_TYPE_NUMBER:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   221
		intval = zpool_get_prop_int(zhp, prop, &src);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   222
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   223
		switch (prop) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   224
		case ZPOOL_PROP_SIZE:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   225
		case ZPOOL_PROP_USED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   226
		case ZPOOL_PROP_AVAILABLE:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   227
			(void) zfs_nicenum(intval, buf, len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   228
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   229
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   230
		case ZPOOL_PROP_CAPACITY:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   231
			(void) snprintf(buf, len, "%llu%%",
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   232
			    (u_longlong_t)intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   233
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   234
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   235
		case ZPOOL_PROP_HEALTH:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   236
			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   237
			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   238
			verify(nvlist_lookup_uint64_array(nvroot,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   239
			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   240
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   241
			(void) strlcpy(buf, zpool_state_to_name(intval,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   242
			    vs->vs_aux), len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   243
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   244
		default:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   245
			(void) snprintf(buf, len, "%llu", intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   246
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   247
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   248
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   249
	case PROP_TYPE_INDEX:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   250
		intval = zpool_get_prop_int(zhp, prop, &src);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   251
		if (zpool_prop_index_to_string(prop, intval, &strval)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   252
		    != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   253
			return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   254
		(void) strlcpy(buf, strval, len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   255
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   256
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   257
	default:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   258
		abort();
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   259
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   260
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   261
	if (srctype)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   262
		*srctype = src;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   263
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   264
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   265
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   266
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   267
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   268
 * Check if the bootfs name has the same pool name as it is set to.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   269
 * Assuming bootfs is a valid dataset name.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   270
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   271
static boolean_t
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   272
bootfs_name_valid(const char *pool, char *bootfs)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   273
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   274
	int len = strlen(pool);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   275
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   276
	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   277
		return (B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   278
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   279
	if (strncmp(pool, bootfs, len) == 0 &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   280
	    (bootfs[len] == '/' || bootfs[len] == '\0'))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   281
		return (B_TRUE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   282
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   283
	return (B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   284
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   285
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   286
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   287
 * Given an nvlist of zpool properties to be set, validate that they are
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   288
 * correct, and parse any numeric properties (index, boolean, etc) if they are
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   289
 * specified as strings.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   290
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   291
static nvlist_t *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   292
zpool_validate_properties(libzfs_handle_t *hdl, const char *poolname,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   293
    nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   294
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   295
	nvpair_t *elem;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   296
	nvlist_t *retprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   297
	zpool_prop_t prop;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   298
	char *strval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   299
	uint64_t intval;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   300
	char *slash;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   301
	struct stat64 statbuf;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   302
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   303
	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   304
		(void) no_memory(hdl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   305
		return (NULL);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   306
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   307
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   308
	elem = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   309
	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   310
		const char *propname = nvpair_name(elem);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   311
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   312
		/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   313
		 * Make sure this property is valid and applies to this type.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   314
		 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   315
		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   316
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   317
			    "invalid property '%s'"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   318
			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   319
			goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   320
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   321
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   322
		if (zpool_prop_readonly(prop)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   323
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   324
			    "is readonly"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   325
			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   326
			goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   327
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   328
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   329
		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   330
		    &strval, &intval, errbuf) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   331
			goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   332
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   333
		/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   334
		 * Perform additional checking for specific properties.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   335
		 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   336
		switch (prop) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   337
		case ZPOOL_PROP_VERSION:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   338
			if (intval < version || intval > SPA_VERSION) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   339
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   340
				    "property '%s' number %d is invalid."),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   341
				    propname, intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   342
				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   343
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   344
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   345
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   346
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   347
		case ZPOOL_PROP_BOOTFS:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   348
			if (create_or_import) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   349
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   350
				    "property '%s' cannot be set at creation "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   351
				    "or import time"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   352
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   353
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   354
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   355
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   356
			if (version < SPA_VERSION_BOOTFS) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   357
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   358
				    "pool must be upgraded to support "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   359
				    "'%s' property"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   360
				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   361
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   362
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   363
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   364
			/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   365
			 * bootfs property value has to be a dataset name and
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   366
			 * the dataset has to be in the same pool as it sets to.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   367
			 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   368
			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   369
			    strval)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   370
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   371
				    "is an invalid name"), strval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   372
				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   373
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   374
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   375
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   376
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   377
		case ZPOOL_PROP_ALTROOT:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   378
			if (!create_or_import) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   379
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   380
				    "property '%s' can only be set during pool "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   381
				    "creation or import"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   382
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   383
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   384
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   385
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   386
			if (strval[0] != '/') {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   387
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   388
				    "bad alternate root '%s'"), strval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   389
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   390
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   391
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   392
			break;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   393
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   394
		case ZPOOL_PROP_CACHEFILE:
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   395
			if (strval[0] == '\0')
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   396
				break;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   397
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   398
			if (strcmp(strval, "none") == 0)
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   399
				break;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   400
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   401
			if (strval[0] != '/') {
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   402
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   403
				    "property '%s' must be empty, an "
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   404
				    "absolute path, or 'none'"), propname);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   405
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   406
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   407
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   408
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   409
			slash = strrchr(strval, '/');
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   410
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   411
			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   412
			    strcmp(slash, "/..") == 0) {
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   413
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   414
				    "'%s' is not a valid file"), strval);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   415
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   416
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   417
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   418
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   419
			*slash = '\0';
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   420
5621
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   421
			if (strval[0] != '\0' &&
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   422
			    (stat64(strval, &statbuf) != 0 ||
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   423
			    !S_ISDIR(statbuf.st_mode))) {
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   424
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   425
				    "'%s' is not a valid directory"),
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   426
				    strval);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   427
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   428
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   429
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   430
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   431
			*slash = '/';
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   432
			break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   433
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   434
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   435
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   436
	return (retprops);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   437
error:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   438
	nvlist_free(retprops);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   439
	return (NULL);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   440
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   441
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   442
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   443
 * Set zpool property : propname=propval.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   444
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   445
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   446
zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   447
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   448
	zfs_cmd_t zc = { 0 };
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   449
	int ret = -1;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   450
	char errbuf[1024];
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   451
	nvlist_t *nvl = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   452
	nvlist_t *realprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   453
	uint64_t version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   454
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   455
	(void) snprintf(errbuf, sizeof (errbuf),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   456
	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   457
	    zhp->zpool_name);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   458
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   459
	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   460
		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   461
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   462
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   463
		return (no_memory(zhp->zpool_hdl));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   464
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   465
	if (nvlist_add_string(nvl, propname, propval) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   466
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   467
		return (no_memory(zhp->zpool_hdl));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   468
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   469
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   470
	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   471
	if ((realprops = zpool_validate_properties(zhp->zpool_hdl,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   472
	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   473
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   474
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   475
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   476
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   477
	nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   478
	nvl = realprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   479
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   480
	/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   481
	 * Execute the corresponding ioctl() to set this property.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   482
	 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   483
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   484
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   485
	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   486
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   487
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   488
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   489
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   490
	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   491
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   492
	zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   493
	nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   494
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   495
	if (ret)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   496
		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   497
	else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   498
		(void) zpool_props_refresh(zhp);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   499
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   500
	return (ret);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   501
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   502
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   503
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   504
zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   505
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   506
	libzfs_handle_t *hdl = zhp->zpool_hdl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   507
	zprop_list_t *entry;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   508
	char buf[ZFS_MAXPROPLEN];
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   509
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   510
	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   511
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   512
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   513
	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   514
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   515
		if (entry->pl_fixed)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   516
			continue;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   517
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   518
		if (entry->pl_prop != ZPROP_INVAL &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   519
		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   520
		    NULL) == 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   521
			if (strlen(buf) > entry->pl_width)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   522
				entry->pl_width = strlen(buf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   523
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   524
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   525
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   526
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   527
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   528
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   529
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
 * Validate the given pool name, optionally putting an extended error message in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
 * 'buf'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   534
static boolean_t
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   535
zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
	namecheck_err_t why;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
	char what;
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   539
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   541
	ret = pool_namecheck(pool, &why, &what);
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   542
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   543
	/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   544
	 * The rules for reserved pool names were extended at a later point.
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   545
	 * But we need to support users with existing pools that may now be
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   546
	 * invalid.  So we only check for this expanded set of names during a
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   547
	 * create (or import), and only in userland.
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   548
	 */
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   549
	if (ret == 0 && !isopen &&
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   550
	    (strncmp(pool, "mirror", 6) == 0 ||
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   551
	    strncmp(pool, "raidz", 5) == 0 ||
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   552
	    strncmp(pool, "spare", 5) == 0 ||
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   553
	    strcmp(pool, "log") == 0)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   554
		zfs_error_aux(hdl,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   555
		    dgettext(TEXT_DOMAIN, "name is reserved"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   556
		return (B_FALSE);
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   557
	}
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   558
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   559
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   560
	if (ret != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   561
		if (hdl != NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
			switch (why) {
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   563
			case NAME_ERR_TOOLONG:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   564
				zfs_error_aux(hdl,
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   565
				    dgettext(TEXT_DOMAIN, "name is too long"));
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   566
				break;
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   567
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
			case NAME_ERR_INVALCHAR:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   569
				zfs_error_aux(hdl,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
				    dgettext(TEXT_DOMAIN, "invalid character "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
				    "'%c' in pool name"), what);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
			case NAME_ERR_NOLETTER:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   575
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   576
				    "name must begin with a letter"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
			case NAME_ERR_RESERVED:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   580
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   581
				    "name is reserved"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
			case NAME_ERR_DISKLIKE:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   585
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   586
				    "pool name is reserved"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
				break;
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   588
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   589
			case NAME_ERR_LEADING_SLASH:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   590
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   591
				    "leading slash in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   592
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   593
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   594
			case NAME_ERR_EMPTY_COMPONENT:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   595
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   596
				    "empty component in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   597
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   598
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   599
			case NAME_ERR_TRAILING_SLASH:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   600
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   601
				    "trailing slash in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   602
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   603
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   604
			case NAME_ERR_MULTIPLE_AT:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   605
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   606
				    "multiple '@' delimiters in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   607
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   608
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
		}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   611
		return (B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   614
	return (B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
 * Open a handle to the given pool, even if the pool is currently in the FAULTED
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
 * state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
zpool_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   622
zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
	zpool_handle_t *zhp;
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   625
	boolean_t missing;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
	 * Make sure the pool name is valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   630
	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
   631
		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   632
		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   633
		    pool);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   637
	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   638
		return (NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   640
	zhp->zpool_hdl = hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   643
	if (zpool_refresh_stats(zhp, &missing) != 0) {
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   644
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   645
		return (NULL);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   646
	}
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   647
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   648
	if (missing) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   649
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
   650
		(void) zfs_error_fmt(hdl, EZFS_NOENT,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   651
		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   652
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   653
		return (NULL);
789
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
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
 * Like the above, but silent on error.  Used when iterating over pools (because
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
 * the configuration cache may be out of date).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
 */
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   663
int
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   664
zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
	zpool_handle_t *zhp;
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   667
	boolean_t missing;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   669
	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   670
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   672
	zhp->zpool_hdl = hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   675
	if (zpool_refresh_stats(zhp, &missing) != 0) {
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   676
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   677
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   680
	if (missing) {
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   681
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   682
		*ret = NULL;
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   683
		return (0);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   684
	}
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   685
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   686
	*ret = zhp;
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   687
	return (0);
789
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
 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
 * state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
zpool_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   695
zpool_open(libzfs_handle_t *hdl, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   699
	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
   703
		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   704
		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
}
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
 * Close the handle.  Simply frees the memory associated with the handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
zpool_close(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
	if (zhp->zpool_config)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
		nvlist_free(zhp->zpool_config);
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 789
diff changeset
   720
	if (zhp->zpool_old_config)
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 789
diff changeset
   721
		nvlist_free(zhp->zpool_old_config);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   722
	if (zhp->zpool_props)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   723
		nvlist_free(zhp->zpool_props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	free(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
 * Return the name of the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
zpool_get_name(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
	return (zhp->zpool_name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
 * Return the state of the pool (ACTIVE or UNAVAILABLE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
zpool_get_state(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
	return (zhp->zpool_state);
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
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
 * Create the named pool, using the provided vdev list.  It is assumed
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
 * that the consumer has already validated the contents of the nvlist, so we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
 * don't have to worry about error semantics.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   752
zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   753
    nvlist_t *props)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   756
	char msg[1024];
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   757
	char *altroot;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   758
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   759
	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   760
	    "cannot create '%s'"), pool);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   762
	if (!zpool_name_valid(hdl, B_FALSE, pool))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   763
		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   764
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   765
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   766
		return (-1);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   767
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   768
	if (props && (props = zpool_validate_properties(hdl, pool, props,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   769
	    SPA_VERSION_1, B_TRUE, msg)) == NULL)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   770
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   771
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   772
	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   773
		nvlist_free(props);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   774
		return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   775
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
   779
	if (zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc) != 0) {
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   780
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   781
		zcmd_free_nvlists(&zc);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   782
		nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   783
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
		case EBUSY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
			 * This can happen if the user has specified the same
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
			 * device multiple times.  We can't reliably detect this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
			 * until we try to add it and see we already have a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
			 * label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   792
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   793
			    "one or more vdevs refer to the same device"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   794
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
			/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   798
			 * This occurs when one of the devices is below
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
			 * device was the problem device since there's no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
			 * reliable way to determine device size from userland.
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
				char buf[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   808
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   809
				    "one or more devices is less than the "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   810
				    "minimum size (%s)"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   812
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
		case ENOSPC:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   815
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   816
			    "one or more devices is out of space"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   817
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   819
		case ENOTBLK:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   820
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   821
			    "cache device must be a disk or disk slice"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   822
			return (zfs_error(hdl, EZFS_BADDEV, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   823
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   825
			return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
		}
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
	 * If this is an alternate root pool, then we automatically set the
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   831
	 * mountpoint of the root dataset to be '/'.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
	 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   833
	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   834
	    &altroot) == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
		zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   837
		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   838
		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   839
		    "/") == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   844
	zcmd_free_nvlists(&zc);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   845
	nvlist_free(props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
 * Destroy the given pool.  It is up to the caller to ensure that there are no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
 * datasets left in the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
zpool_destroy(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	zfs_handle_t *zfp = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   858
	libzfs_handle_t *hdl = zhp->zpool_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   859
	char msg[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   862
	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   863
	    ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   866
	if (zpool_remove_zvol_links(zhp) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
   871
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   872
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   873
		    "cannot destroy '%s'"), zhp->zpool_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   875
		if (errno == EROFS) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   876
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   877
			    "one or more devices is read only"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   878
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   879
		} else {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   880
			(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
		if (zfp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
			zfs_close(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
	if (zfp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
		remove_mountpoint(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
		zfs_close(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   893
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
 * Add the given vdevs to the pool.  The caller must have already performed the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
 * necessary verification to ensure that the vdev specification is well-formed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   903
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   904
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   905
	libzfs_handle_t *hdl = zhp->zpool_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   906
	char msg[1024];
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   907
	nvlist_t **spares, **l2cache;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   908
	uint_t nspares, nl2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   909
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   910
	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   911
	    "cannot add to '%s'"), zhp->zpool_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   912
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   913
	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   914
	    SPA_VERSION_SPARES &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   915
	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   916
	    &spares, &nspares) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   917
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   918
		    "upgraded to add hot spares"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   919
		return (zfs_error(hdl, EZFS_BADVERSION, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   920
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   922
	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   923
	    SPA_VERSION_L2CACHE &&
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   924
	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   925
	    &l2cache, &nl2cache) == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   926
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   927
		    "upgraded to add cache devices"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   928
		return (zfs_error(hdl, EZFS_BADVERSION, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   929
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   930
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   931
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   932
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
   935
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
		case EBUSY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
			 * This can happen if the user has specified the same
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
			 * device multiple times.  We can't reliably detect this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
			 * until we try to add it and see we already have a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
			 * label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   944
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   945
			    "one or more vdevs refer to the same device"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   946
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   947
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   950
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
			 * This occurrs when one of the devices is below
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
			 * device was the problem device since there's no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
			 * reliable way to determine device size from userland.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   955
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
			{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
				char buf[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   961
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   962
				    "device is less than the minimum "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   963
				    "size (%s)"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   965
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   966
			break;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   967
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   968
		case ENOTSUP:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   969
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   970
			    "pool must be upgraded to add these vdevs"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   971
			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   974
		case EDOM:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   975
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   976
			    "root pool can not have multiple vdevs"
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   977
			    " or separate logs"));
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   978
			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   979
			break;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   980
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   981
		case ENOTBLK:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   982
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   983
			    "cache device must be a disk or disk slice"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   984
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   985
			break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
   986
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   988
			(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   991
		ret = -1;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   992
	} else {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   993
		ret = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   996
	zcmd_free_nvlists(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   998
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
 * Exports the pool from the system.  The caller must ensure that there are no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1003
 * mounted datasets in the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1004
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1005
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1006
zpool_export(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
	if (zpool_remove_zvol_links(zhp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1015
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0)
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  1016
		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1017
		    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1018
		    zhp->zpool_name));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1023
 * zpool_import() is a contracted interface. Should be kept the same
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1024
 * if possible.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1025
 *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1026
 * Applications should use zpool_import_props() to import a pool with
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1027
 * new properties value to be set.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1030
zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1031
    char *altroot)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1032
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1033
	nvlist_t *props = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1034
	int ret;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1035
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1036
	if (altroot != NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1037
		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1038
			return (zfs_error_fmt(hdl, EZFS_NOMEM,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1039
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1040
			    newname));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1041
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1042
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1043
		if (nvlist_add_string(props,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1044
		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1045
			nvlist_free(props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1046
			return (zfs_error_fmt(hdl, EZFS_NOMEM,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1047
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1048
			    newname));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1049
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1050
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1051
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1052
	ret = zpool_import_props(hdl, config, newname, props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1053
	if (props)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1054
		nvlist_free(props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1055
	return (ret);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1056
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1057
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1058
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1059
 * Import the given pool using the known configuration and a list of
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1060
 * properties to be set. The configuration should have come from
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1061
 * zpool_find_import(). The 'newname' parameters control whether the pool
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1062
 * is imported with a different name.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1063
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1064
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1065
zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1066
    nvlist_t *props)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1068
	zfs_cmd_t zc = { 0 };
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
	char *thename;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
	char *origname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
	int ret;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1072
	char errbuf[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
	    &origname) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1077
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1078
	    "cannot import pool '%s'"), origname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1079
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
	if (newname != NULL) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1081
		if (!zpool_name_valid(hdl, B_FALSE, newname))
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  1082
			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1083
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1084
			    newname));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1085
		thename = (char *)newname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
		thename = origname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1090
	if (props) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1091
		uint64_t version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1092
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1093
		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1094
		    &version) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1095
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1096
		if ((props = zpool_validate_properties(hdl, origname,
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1097
		    props, version, B_TRUE, errbuf)) == NULL) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1098
			return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1099
		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1100
			nvlist_free(props);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1101
			return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1102
		}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1103
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1108
	    &zc.zc_guid) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1110
	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1111
		nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1112
		return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1113
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
	ret = 0;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1116
	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1117
		char desc[1024];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1118
		if (newname == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1119
			(void) snprintf(desc, sizeof (desc),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1120
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1121
			    thename);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
			(void) snprintf(desc, sizeof (desc),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1124
			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1125
			    origname, thename);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1126
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1127
		switch (errno) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1128
		case ENOTSUP:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1129
			/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1130
			 * Unsupported version.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1131
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1132
			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1133
			break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1134
2174
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1135
		case EINVAL:
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1136
			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1137
			break;
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1138
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1140
			(void) zpool_standard_error(hdl, errno, desc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
		ret = -1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
		zpool_handle_t *zhp;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1146
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
		 * This should never fail, but play it safe anyway.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
		 */
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1150
		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1151
			ret = -1;
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1152
		} else if (zhp != NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
			ret = zpool_create_zvol_links(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
			zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
		}
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1156
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1159
	zcmd_free_nvlists(&zc);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1160
	nvlist_free(props);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1161
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
 * Scrub the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1167
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1170
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1171
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1172
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1173
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
	zc.zc_cookie = type;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1178
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
	(void) snprintf(msg, sizeof (msg),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1184
	if (errno == EBUSY)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1185
		return (zfs_error(hdl, EZFS_RESILVERING, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1186
	else
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1187
		return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1190
/*
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1191
 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1192
 * spare; but FALSE if its an INUSE spare.
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1193
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1194
static nvlist_t *
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1195
vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1196
    boolean_t *avail_spare, boolean_t *l2cache)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1197
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1198
	uint_t c, children;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1199
	nvlist_t **child;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1200
	uint64_t theguid, present;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1201
	char *path;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1202
	uint64_t wholedisk = 0;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1203
	nvlist_t *ret;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1204
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1205
	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1206
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1207
	if (search == NULL &&
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1208
	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1209
		/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1210
		 * If the device has never been present since import, the only
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1211
		 * reliable way to match the vdev is by GUID.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1212
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1213
		if (theguid == guid)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1214
			return (nv);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1215
	} else if (search != NULL &&
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1216
	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1217
		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1218
		    &wholedisk);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1219
		if (wholedisk) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1220
			/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1221
			 * For whole disks, the internal path has 's0', but the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1222
			 * path passed in by the user doesn't.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1223
			 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1224
			if (strlen(search) == strlen(path) - 2 &&
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1225
			    strncmp(search, path, strlen(search)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1226
				return (nv);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1227
		} else if (strcmp(search, path) == 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1228
			return (nv);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1229
		}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1230
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1231
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1232
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1233
	    &child, &children) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1234
		return (NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1235
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1236
	for (c = 0; c < children; c++)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1237
		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1238
		    avail_spare, l2cache)) != NULL)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1239
			return (ret);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1240
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1241
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1242
	    &child, &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1243
		for (c = 0; c < children; c++) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1244
			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1245
			    avail_spare, l2cache)) != NULL) {
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1246
				*avail_spare = B_TRUE;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1247
				return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1248
			}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1249
		}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1250
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1251
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1252
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1253
	    &child, &children) == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1254
		for (c = 0; c < children; c++) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1255
			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1256
			    avail_spare, l2cache)) != NULL) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1257
				*l2cache = B_TRUE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1258
				return (ret);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1259
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1260
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1261
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1262
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1263
	return (NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1264
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1265
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1266
nvlist_t *
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1267
zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1268
    boolean_t *l2cache)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1269
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1270
	char buf[MAXPATHLEN];
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1271
	const char *search;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1272
	char *end;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1273
	nvlist_t *nvroot;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1274
	uint64_t guid;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1275
1613
aca102a242d3 6396628 zpool offline does not recognize c0d0 patten recently
eschrock
parents: 1544
diff changeset
  1276
	guid = strtoull(path, &end, 10);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1277
	if (guid != 0 && *end == '\0') {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1278
		search = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1279
	} else if (path[0] != '/') {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1280
		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1281
		search = buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1282
	} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1283
		search = path;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1284
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1285
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1286
	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1287
	    &nvroot) == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1288
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1289
	*avail_spare = B_FALSE;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1290
	*l2cache = B_FALSE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1291
	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1292
	    l2cache));
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1293
}
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1294
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1295
/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1296
 * Returns TRUE if the given guid corresponds to the given type.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1297
 * This is used to check for hot spares (INUSE or not), and level 2 cache
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1298
 * devices.
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1299
 */
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1300
static boolean_t
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1301
is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1302
{
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1303
	uint64_t target_guid;
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1304
	nvlist_t *nvroot;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1305
	nvlist_t **list;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1306
	uint_t count;
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1307
	int i;
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1308
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1309
	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1310
	    &nvroot) == 0);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1311
	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1312
		for (i = 0; i < count; i++) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1313
			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1314
			    &target_guid) == 0);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1315
			if (guid == target_guid)
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1316
				return (B_TRUE);
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1317
		}
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1318
	}
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1319
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1320
	return (B_FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1321
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1322
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1323
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1324
 * Bring the specified vdev online.   The 'flags' parameter is a set of the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1325
 * ZFS_ONLINE_* flags.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
int
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1328
zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1329
    vdev_state_t *newstate)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1331
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1333
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1334
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1335
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1336
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1337
	(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1338
	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1339
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1340
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1341
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1342
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1344
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1345
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1346
	if (avail_spare ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1347
	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1348
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1349
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1350
	if (l2cache ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1351
	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_L2CACHE) == B_TRUE)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1352
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1353
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1354
	zc.zc_cookie = VDEV_STATE_ONLINE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1355
	zc.zc_obj = flags;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1356
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1357
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1358
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1359
		return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1360
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1361
	*newstate = zc.zc_cookie;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1362
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1364
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1365
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1366
 * Take the specified vdev offline
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1367
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1368
int
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1369
zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1370
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1371
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1372
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1373
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1374
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1375
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1376
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1377
	(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1378
	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1379
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1380
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1381
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1382
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1383
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1384
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1385
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1386
	if (avail_spare ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1387
	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1388
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1389
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1390
	if (l2cache ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1391
	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_L2CACHE) == B_TRUE)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1392
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1393
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1394
	zc.zc_cookie = VDEV_STATE_OFFLINE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1395
	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1354
diff changeset
  1396
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1397
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1398
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1399
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1400
	switch (errno) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1401
	case EBUSY:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
		 * There are no other replicas of this device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1405
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1406
		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1407
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1408
	default:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1409
		return (zpool_standard_error(hdl, errno, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1410
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1411
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1412
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1413
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1414
 * Mark the given vdev faulted.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1415
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1416
int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1417
zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1418
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1419
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1420
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1421
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1422
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1423
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1424
	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1425
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1426
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1427
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1428
	zc.zc_cookie = VDEV_STATE_FAULTED;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1429
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1430
	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1431
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1432
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1433
	switch (errno) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1434
	case EBUSY:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1435
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1436
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1437
		 * There are no other replicas of this device.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1438
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1439
		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1440
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1441
	default:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1442
		return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1443
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1444
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1445
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1446
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1447
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1448
 * Mark the given vdev degraded.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1449
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1450
int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1451
zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1452
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1453
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1454
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1455
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1456
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1457
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1458
	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1459
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1460
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1461
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1462
	zc.zc_cookie = VDEV_STATE_DEGRADED;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1463
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1464
	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1465
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1466
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1467
	return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1468
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1469
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1470
/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1471
 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1472
 * a hot spare.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1473
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1474
static boolean_t
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1475
is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1476
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1477
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1478
	uint_t c, children;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1479
	char *type;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1480
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1481
	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1482
	    &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1483
		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1484
		    &type) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1485
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1486
		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1487
		    children == 2 && child[which] == tgt)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1488
			return (B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1489
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1490
		for (c = 0; c < children; c++)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1491
			if (is_replacing_spare(child[c], tgt, which))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1492
				return (B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1494
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1495
	return (B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
 * Attach new_disk (fully described by nvroot) to old_disk.
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1500
 * If 'replacing' is specified, the new disk will replace the old one.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1501
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1502
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1503
zpool_vdev_attach(zpool_handle_t *zhp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1504
    const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1505
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1506
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1507
	char msg[1024];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1508
	int ret;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1509
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1510
	boolean_t avail_spare, l2cache;
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1511
	uint64_t val, is_log;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1512
	char *path;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1513
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1514
	uint_t children;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1515
	nvlist_t *config_root;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1516
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1517
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1518
	if (replacing)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1519
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1520
		    "cannot replace %s with %s"), old_disk, new_disk);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1521
	else
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1522
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1523
		    "cannot attach %s to %s"), new_disk, old_disk);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1524
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1526
	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1527
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1528
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1529
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1530
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1531
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1532
	if (l2cache)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1533
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1534
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1535
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1536
	zc.zc_cookie = replacing;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1537
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1538
	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1539
	    &child, &children) != 0 || children != 1) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1540
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1541
		    "new device must be a single disk"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1542
		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1543
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1544
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1545
	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1546
	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1547
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1548
	/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1549
	 * If the target is a hot spare that has been swapped in, we can only
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1550
	 * replace it with another hot spare.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1551
	 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1552
	if (replacing &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1553
	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1554
	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1555
	    (zpool_find_vdev(zhp, path, &avail_spare, &l2cache) == NULL ||
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1556
	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1557
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1558
		    "can only be replaced by another hot spare"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1559
		return (zfs_error(hdl, EZFS_BADTARGET, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1560
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1561
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1562
	/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1563
	 * If we are attempting to replace a spare, it canot be applied to an
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1564
	 * already spared device.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1565
	 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1566
	if (replacing &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1567
	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1568
	    zpool_find_vdev(zhp, path, &avail_spare, &l2cache) != NULL &&
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1569
	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1570
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1571
		    "device has already been replaced with a spare"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1572
		return (zfs_error(hdl, EZFS_BADTARGET, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1573
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1575
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1576
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1577
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1578
	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1579
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1580
	zcmd_free_nvlists(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1582
	if (ret == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1583
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1584
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1585
	switch (errno) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1586
	case ENOTSUP:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1587
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1588
		 * Can't attach to or replace this type of vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1589
		 */
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1590
		if (replacing) {
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1591
			is_log = B_FALSE;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1592
			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1593
			    &is_log);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1594
			if (is_log)
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1595
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1596
				    "cannot replace a log with a spare"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1597
			else
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1598
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1599
				    "cannot replace a replacing device"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1600
		} else {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1601
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1602
			    "can only attach to mirrors and top-level "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1603
			    "disks"));
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1604
		}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1605
		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1608
	case EINVAL:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1609
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1610
		 * The new device must be a single disk.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1611
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1612
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1613
		    "new device must be a single disk"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1614
		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1617
	case EBUSY:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1618
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1619
		    new_disk);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1620
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1623
	case EOVERFLOW:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1624
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
		 * The new device is too small.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1626
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1627
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1628
		    "device is too small"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1629
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1630
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1631
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1632
	case EDOM:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1633
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1634
		 * The new device has a different alignment requirement.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1636
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1637
		    "devices have different sector alignment"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1638
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1641
	case ENAMETOOLONG:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1642
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1643
		 * The resulting top-level vdev spec won't fit in the label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1644
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1645
		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1646
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1647
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1648
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1649
		(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1650
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1651
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1652
	return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1653
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1654
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1655
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1656
 * Detach the specified device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1657
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1660
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1662
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1663
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1664
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1665
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1666
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1667
	(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1668
	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1669
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1670
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1671
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1672
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1673
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1674
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1675
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1676
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1677
	if (l2cache)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1678
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1679
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1680
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1681
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1682
	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
	switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1687
	case ENOTSUP:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1688
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
		 * Can't detach from this type of vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1690
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1691
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1692
		    "applicable to mirror and replacing vdevs"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1693
		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1694
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1695
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1696
	case EBUSY:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
		 * There are no other replicas of this device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1699
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1700
		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1701
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1702
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1703
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1704
		(void) zpool_standard_error(hdl, errno, msg);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1705
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1706
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1707
	return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1708
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1709
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1710
/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1711
 * Remove the given device.  Currently, this is supported only for hot spares
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1712
 * and level 2 cache devices.
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1713
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1714
int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1715
zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1716
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1717
	zfs_cmd_t zc = { 0 };
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1718
	char msg[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1719
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1720
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1721
	libzfs_handle_t *hdl = zhp->zpool_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1722
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1723
	(void) snprintf(msg, sizeof (msg),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1724
	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1725
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1726
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1727
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1728
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1729
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1730
	if (!avail_spare && !l2cache) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1731
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1732
		    "only inactive hot spares or cache devices "
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1733
		    "can be removed"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1734
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1735
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1736
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1737
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1738
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1739
	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1740
		return (0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1741
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1742
	return (zpool_standard_error(hdl, errno, msg));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1743
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1744
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1745
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1746
 * Clear the errors for the pool, or the particular device if specified.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1747
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1748
int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1749
zpool_clear(zpool_handle_t *zhp, const char *path)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1750
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1751
	zfs_cmd_t zc = { 0 };
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1752
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1753
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1754
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1755
	libzfs_handle_t *hdl = zhp->zpool_hdl;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1756
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1757
	if (path)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1758
		(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1759
		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1760
		    path);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1761
	else
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1762
		(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1763
		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1764
		    zhp->zpool_name);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1765
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1766
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1767
	if (path) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1768
		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1769
		    &l2cache)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1770
			return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1771
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1772
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1773
		 * Don't allow error clearing for hot spares.  Do allow
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1774
		 * error clearing for l2cache devices.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1775
		 */
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1776
		if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1777
			return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1778
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1779
		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1780
		    &zc.zc_guid) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1781
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1782
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1783
	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1784
		return (0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1785
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1786
	return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1787
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1789
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1790
 * Similar to zpool_clear(), but takes a GUID (used by fmd).
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1791
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1792
int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1793
zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1794
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1795
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1796
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1797
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1798
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1799
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1800
	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1801
	    guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1802
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1803
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1804
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1805
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1806
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1807
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1808
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1809
	return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1810
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1811
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  1812
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1813
 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1814
 * hierarchy.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1815
 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1816
int
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1817
zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1818
    void *data)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1820
	libzfs_handle_t *hdl = zhp->zpool_hdl;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1821
	char (*paths)[MAXPATHLEN];
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1822
	size_t size = 4;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1823
	int curr, fd, base, ret = 0;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1824
	DIR *dirp;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1825
	struct dirent *dp;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1826
	struct stat st;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1827
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1828
	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1829
		return (errno == ENOENT ? 0 : -1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1830
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1831
	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1832
		int err = errno;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1833
		(void) close(base);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1834
		return (err == ENOENT ? 0 : -1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1835
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1837
	/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1838
	 * Oddly this wasn't a directory -- ignore that failure since we
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1839
	 * know there are no links lower in the (non-existant) hierarchy.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
	 */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1841
	if (!S_ISDIR(st.st_mode)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1842
		(void) close(base);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1843
		return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1844
	}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1845
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1846
	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1847
		(void) close(base);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1848
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1850
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1851
	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1852
	curr = 0;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1853
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1854
	while (curr >= 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1855
		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1856
			goto err;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1857
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1858
		if (S_ISDIR(st.st_mode)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1859
			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1860
				goto err;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1861
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1862
			if ((dirp = fdopendir(fd)) == NULL) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1863
				(void) close(fd);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1864
				goto err;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1865
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1866
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1867
			while ((dp = readdir(dirp)) != NULL) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1868
				if (dp->d_name[0] == '.')
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1869
					continue;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1870
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1871
				if (curr + 1 == size) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1872
					paths = zfs_realloc(hdl, paths,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1873
					    size * sizeof (paths[0]),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1874
					    size * 2 * sizeof (paths[0]));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1875
					if (paths == NULL) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1876
						(void) closedir(dirp);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1877
						(void) close(fd);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1878
						goto err;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1879
					}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1880
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1881
					size *= 2;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1882
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1883
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1884
				(void) strlcpy(paths[curr + 1], paths[curr],
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1885
				    sizeof (paths[curr + 1]));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1886
				(void) strlcat(paths[curr], "/",
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1887
				    sizeof (paths[curr]));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1888
				(void) strlcat(paths[curr], dp->d_name,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1889
				    sizeof (paths[curr]));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1890
				curr++;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1891
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1892
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1893
			(void) closedir(dirp);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1894
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1895
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1896
			if ((ret = cb(paths[curr], data)) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1897
				break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1898
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1899
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1900
		curr--;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1901
	}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1902
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1903
	free(paths);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1904
	(void) close(base);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1905
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1906
	return (ret);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1907
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1908
err:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1909
	free(paths);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1910
	(void) close(base);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1911
	return (-1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1912
}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1913
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1914
typedef struct zvol_cb {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1915
	zpool_handle_t *zcb_pool;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1916
	boolean_t zcb_create;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1917
} zvol_cb_t;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1918
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1919
/*ARGSUSED*/
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1920
static int
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1921
do_zvol_create(zfs_handle_t *zhp, void *data)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1922
{
4657
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1923
	int ret = 0;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1924
4657
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1925
	if (ZFS_IS_VOLUME(zhp)) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1926
		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
4657
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1927
		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1928
	}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1929
4657
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1930
	if (ret == 0)
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1931
		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1932
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
	zfs_close(zhp);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1934
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1936
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
 * Iterate over all zvols in the pool and make any necessary minor nodes.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1941
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1942
zpool_create_zvol_links(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1944
	zfs_handle_t *zfp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1945
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1946
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1947
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1948
	 * If the pool is unavailable, just return success.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1950
	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1951
	    zhp->zpool_name)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1952
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1953
4657
59d80d18fea6 6580497 zfs volinit shouldn't slowly iterate over all snapshots
ahrens
parents: 4577
diff changeset
  1954
	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
	zfs_close(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1960
static int
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1961
do_zvol_remove(const char *dataset, void *data)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1962
{
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1963
	zpool_handle_t *zhp = data;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1964
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1965
	return (zvol_remove_link(zhp->zpool_hdl, dataset));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1966
}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1967
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1969
 * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1970
 * by examining the /dev links so that a corrupted pool doesn't impede this
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1971
 * operation.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1974
zpool_remove_zvol_links(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1976
	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
}
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1978
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1979
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1980
 * Convert from a devid string to a path.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1981
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1982
static char *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1983
devid_to_path(char *devid_str)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1984
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1985
	ddi_devid_t devid;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1986
	char *minor;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1987
	char *path;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1988
	devid_nmlist_t *list = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1989
	int ret;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1990
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1991
	if (devid_str_decode(devid_str, &devid, &minor) != 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1992
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1993
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1994
	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1995
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1996
	devid_str_free(minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1997
	devid_free(devid);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1998
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  1999
	if (ret != 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2000
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2001
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2002
	if ((path = strdup(list[0].devname)) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2003
		return (NULL);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2004
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2005
	devid_free_nmlist(list);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2006
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2007
	return (path);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2008
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2009
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2010
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2011
 * Convert from a path to a devid string.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2012
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2013
static char *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2014
path_to_devid(const char *path)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2015
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2016
	int fd;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2017
	ddi_devid_t devid;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2018
	char *minor, *ret;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2019
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2020
	if ((fd = open(path, O_RDONLY)) < 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2021
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2022
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2023
	minor = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2024
	ret = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2025
	if (devid_get(fd, &devid) == 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2026
		if (devid_get_minor_name(fd, &minor) == 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2027
			ret = devid_str_encode(devid, minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2028
		if (minor != NULL)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2029
			devid_str_free(minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2030
		devid_free(devid);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2031
	}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2032
	(void) close(fd);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2033
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2034
	return (ret);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2035
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2036
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2037
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2038
 * Issue the necessary ioctl() to update the stored path value for the vdev.  We
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2039
 * ignore any failure here, since a common case is for an unprivileged user to
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2040
 * type 'zpool status', and we'll display the correct information anyway.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2041
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2042
static void
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2043
set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2044
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2045
	zfs_cmd_t zc = { 0 };
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2046
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2047
	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2048
	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2049
	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2050
	    &zc.zc_guid) == 0);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2051
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2052
	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2053
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2054
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2055
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2056
 * Given a vdev, return the name to display in iostat.  If the vdev has a path,
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2057
 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2058
 * We also check if this is a whole disk, in which case we strip off the
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2059
 * trailing 's0' slice name.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2060
 *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2061
 * This routine is also responsible for identifying when disks have been
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2062
 * reconfigured in a new location.  The kernel will have opened the device by
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2063
 * devid, but the path will still refer to the old location.  To catch this, we
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2064
 * first do a path -> devid translation (which is fast for the common case).  If
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2065
 * the devid matches, we're done.  If not, we do a reverse devid -> path
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2066
 * translation and issue the appropriate ioctl() to update the path of the vdev.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2067
 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2068
 * of these checks.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2069
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2070
char *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2071
zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2072
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2073
	char *path, *devid;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2074
	uint64_t value;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2075
	char buf[64];
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2076
	vdev_stat_t *vs;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2077
	uint_t vsc;
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2078
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2079
	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2080
	    &value) == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2081
		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2082
		    &value) == 0);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2083
		(void) snprintf(buf, sizeof (buf), "%llu",
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2084
		    (u_longlong_t)value);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2085
		path = buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2086
	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2087
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2088
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2089
		 * If the device is dead (faulted, offline, etc) then don't
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2090
		 * bother opening it.  Otherwise we may be forcing the user to
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2091
		 * open a misbehaving device, which can have undesirable
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2092
		 * effects.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2093
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2094
		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2095
		    (uint64_t **)&vs, &vsc) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2096
		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2097
		    zhp != NULL &&
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2098
		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2099
			/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2100
			 * Determine if the current path is correct.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2101
			 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2102
			char *newdevid = path_to_devid(path);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2103
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2104
			if (newdevid == NULL ||
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2105
			    strcmp(devid, newdevid) != 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2106
				char *newpath;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2107
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2108
				if ((newpath = devid_to_path(devid)) != NULL) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2109
					/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2110
					 * Update the path appropriately.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2111
					 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2112
					set_path(zhp, nv, newpath);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2113
					if (nvlist_add_string(nv,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2114
					    ZPOOL_CONFIG_PATH, newpath) == 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2115
						verify(nvlist_lookup_string(nv,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2116
						    ZPOOL_CONFIG_PATH,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2117
						    &path) == 0);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2118
					free(newpath);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2119
				}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2120
			}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2121
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2122
			if (newdevid)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2123
				devid_str_free(newdevid);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2124
		}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2125
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2126
		if (strncmp(path, "/dev/dsk/", 9) == 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2127
			path += 9;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2128
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2129
		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2130
		    &value) == 0 && value) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2131
			char *tmp = zfs_strdup(hdl, path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2132
			if (tmp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2133
				return (NULL);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2134
			tmp[strlen(path) - 2] = '\0';
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2135
			return (tmp);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2136
		}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2137
	} else {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2138
		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2139
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2140
		/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2141
		 * If it's a raidz device, we need to stick in the parity level.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2142
		 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2143
		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2144
			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2145
			    &value) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2146
			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2147
			    (u_longlong_t)value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2148
			path = buf;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2149
		}
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2150
	}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2151
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2152
	return (zfs_strdup(hdl, path));
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2153
}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2154
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2155
static int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2156
zbookmark_compare(const void *a, const void *b)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2157
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2158
	return (memcmp(a, b, sizeof (zbookmark_t)));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2159
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2160
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2161
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2162
 * Retrieve the persistent error log, uniquify the members, and return to the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2163
 * caller.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2164
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2165
int
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2166
zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2167
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2168
	zfs_cmd_t zc = { 0 };
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2169
	uint64_t count;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2170
	zbookmark_t *zb = NULL;
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2171
	int i;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2172
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2173
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2174
	 * Retrieve the raw error list from the kernel.  If the number of errors
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2175
	 * has increased, allocate more space and continue until we get the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2176
	 * entire list.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2177
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2178
	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2179
	    &count) == 0);
4820
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4715
diff changeset
  2180
	if (count == 0)
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4715
diff changeset
  2181
		return (0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2182
	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2183
	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2184
		return (-1);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2185
	zc.zc_nvlist_dst_size = count;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2186
	(void) strcpy(zc.zc_name, zhp->zpool_name);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2187
	for (;;) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2188
		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2189
		    &zc) != 0) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2190
			free((void *)(uintptr_t)zc.zc_nvlist_dst);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2191
			if (errno == ENOMEM) {
3823
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2192
				count = zc.zc_nvlist_dst_size;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2193
				if ((zc.zc_nvlist_dst = (uintptr_t)
3823
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2194
				    zfs_alloc(zhp->zpool_hdl, count *
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2195
				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2196
					return (-1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2197
			} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2198
				return (-1);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2199
			}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2200
		} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2201
			break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2202
		}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2203
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2204
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2205
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2206
	 * Sort the resulting bookmarks.  This is a little confusing due to the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2207
	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2208
	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2209
	 * _not_ copied as part of the process.  So we point the start of our
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2210
	 * array appropriate and decrement the total number of elements.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2211
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2212
	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2213
	    zc.zc_nvlist_dst_size;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2214
	count -= zc.zc_nvlist_dst_size;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2215
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2216
	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2217
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2218
	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2219
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2220
	/*
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2221
	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2222
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2223
	for (i = 0; i < count; i++) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2224
		nvlist_t *nv;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2225
3700
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3444
diff changeset
  2226
		/* ignoring zb_blkid and zb_level for now */
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3444
diff changeset
  2227
		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3444
diff changeset
  2228
		    zb[i-1].zb_object == zb[i].zb_object)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2229
			continue;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2230
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2231
		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2232
			goto nomem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2233
		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2234
		    zb[i].zb_objset) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2235
			nvlist_free(nv);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2236
			goto nomem;
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2237
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2238
		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2239
		    zb[i].zb_object) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2240
			nvlist_free(nv);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2241
			goto nomem;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2242
		}
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2243
		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2244
			nvlist_free(nv);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2245
			goto nomem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2246
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2247
		nvlist_free(nv);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2248
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2249
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2250
	free((void *)(uintptr_t)zc.zc_nvlist_dst);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2251
	return (0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2252
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2253
nomem:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2254
	free((void *)(uintptr_t)zc.zc_nvlist_dst);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2255
	return (no_memory(zhp->zpool_hdl));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2256
}
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2257
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2258
/*
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2259
 * Upgrade a ZFS pool to the latest on-disk version.
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2260
 */
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2261
int
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2262
zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2263
{
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2264
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2265
	libzfs_handle_t *hdl = zhp->zpool_hdl;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2266
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2267
	(void) strcpy(zc.zc_name, zhp->zpool_name);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2268
	zc.zc_cookie = new_version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2269
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2270
	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2271
		return (zpool_standard_error_fmt(hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2272
		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2273
		    zhp->zpool_name));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2274
	return (0);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2275
}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2276
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2277
void
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2278
zpool_set_history_str(const char *subcommand, int argc, char **argv,
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2279
    char *history_str)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2280
{
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2281
	int i;
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2282
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2283
	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2284
	for (i = 1; i < argc; i++) {
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2285
		if (strlen(history_str) + 1 + strlen(argv[i]) >
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2286
		    HIS_MAX_RECORD_LEN)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2287
			break;
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2288
		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2289
		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2290
	}
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2291
}
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2292
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2293
/*
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2294
 * Stage command history for logging.
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2295
 */
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2296
int
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2297
zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2298
{
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2299
	if (history_str == NULL)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2300
		return (EINVAL);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2301
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2302
	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2303
		return (EINVAL);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2304
4715
e8d212dda064 6535695 Panic: shpp->sh_eof == shpp->sh_pool_create_len, file: ../../common/fs/zfs/spa_history.c, line: 235
ek110237
parents: 4657
diff changeset
  2305
	if (hdl->libzfs_log_str != NULL)
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2306
		free(hdl->libzfs_log_str);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2307
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2308
	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2309
		return (no_memory(hdl));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2310
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2311
	return (0);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2312
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2313
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2314
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2315
 * Perform ioctl to get some command history of a pool.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2316
 *
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2317
 * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2318
 * logical offset of the history buffer to start reading from.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2319
 *
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2320
 * Upon return, 'off' is the next logical offset to read from and
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2321
 * 'len' is the actual amount of bytes read into 'buf'.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2322
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2323
static int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2324
get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2325
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2326
	zfs_cmd_t zc = { 0 };
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2327
	libzfs_handle_t *hdl = zhp->zpool_hdl;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2328
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2329
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2330
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2331
	zc.zc_history = (uint64_t)(uintptr_t)buf;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2332
	zc.zc_history_len = *len;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2333
	zc.zc_history_offset = *off;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2334
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2335
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2336
		switch (errno) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2337
		case EPERM:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2338
			return (zfs_error_fmt(hdl, EZFS_PERM,
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2339
			    dgettext(TEXT_DOMAIN,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2340
			    "cannot show history for pool '%s'"),
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2341
			    zhp->zpool_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2342
		case ENOENT:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2343
			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2344
			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2345
			    "'%s'"), zhp->zpool_name));
3863
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2346
		case ENOTSUP:
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2347
			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2348
			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2349
			    "'%s', pool must be upgraded"), zhp->zpool_name));
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2350
		default:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2351
			return (zpool_standard_error_fmt(hdl, errno,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2352
			    dgettext(TEXT_DOMAIN,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2353
			    "cannot get history for '%s'"), zhp->zpool_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2354
		}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2355
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2356
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2357
	*len = zc.zc_history_len;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2358
	*off = zc.zc_history_offset;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2359
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2360
	return (0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2361
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2362
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2363
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2364
 * Process the buffer of nvlists, unpacking and storing each nvlist record
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2365
 * into 'records'.  'leftover' is set to the number of bytes that weren't
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2366
 * processed as there wasn't a complete record.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2367
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2368
static int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2369
zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2370
    nvlist_t ***records, uint_t *numrecords)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2371
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2372
	uint64_t reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2373
	nvlist_t *nv;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2374
	int i;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2375
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2376
	while (bytes_read > sizeof (reclen)) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2377
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2378
		/* get length of packed record (stored as little endian) */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2379
		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2380
			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2381
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2382
		if (bytes_read < sizeof (reclen) + reclen)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2383
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2384
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2385
		/* unpack record */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2386
		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2387
			return (ENOMEM);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2388
		bytes_read -= sizeof (reclen) + reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2389
		buf += sizeof (reclen) + reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2390
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2391
		/* add record to nvlist array */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2392
		(*numrecords)++;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2393
		if (ISP2(*numrecords + 1)) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2394
			*records = realloc(*records,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2395
			    *numrecords * 2 * sizeof (nvlist_t *));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2396
		}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2397
		(*records)[*numrecords - 1] = nv;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2398
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2399
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2400
	*leftover = bytes_read;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2401
	return (0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2402
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2403
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2404
#define	HIS_BUF_LEN	(128*1024)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2405
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2406
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2407
 * Retrieve the command history of a pool.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2408
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2409
int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2410
zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2411
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2412
	char buf[HIS_BUF_LEN];
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2413
	uint64_t off = 0;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2414
	nvlist_t **records = NULL;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2415
	uint_t numrecords = 0;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2416
	int err, i;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2417
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2418
	do {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2419
		uint64_t bytes_read = sizeof (buf);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2420
		uint64_t leftover;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2421
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2422
		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2423
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2424
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2425
		/* if nothing else was read in, we're at EOF, just return */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2426
		if (!bytes_read)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2427
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2428
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2429
		if ((err = zpool_history_unpack(buf, bytes_read,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2430
		    &leftover, &records, &numrecords)) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2431
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2432
		off -= leftover;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2433
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2434
		/* CONSTCOND */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2435
	} while (1);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2436
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2437
	if (!err) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2438
		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2439
		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2440
		    records, numrecords) == 0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2441
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2442
	for (i = 0; i < numrecords; i++)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2443
		nvlist_free(records[i]);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2444
	free(records);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2445
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2446
	return (err);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2447
}
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2448
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2449
void
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2450
zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2451
    char *pathname, size_t len)
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2452
{
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2453
	zfs_cmd_t zc = { 0 };
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2454
	boolean_t mounted = B_FALSE;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2455
	char *mntpnt = NULL;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2456
	char dsname[MAXNAMELEN];
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2457
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2458
	if (dsobj == 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2459
		/* special case for the MOS */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2460
		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2461
		return;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2462
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2463
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2464
	/* get the dataset's name */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2465
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2466
	zc.zc_obj = dsobj;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2467
	if (ioctl(zhp->zpool_hdl->libzfs_fd,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2468
	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2469
		/* just write out a path of two object numbers */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2470
		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2471
		    dsobj, obj);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2472
		return;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2473
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2474
	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2475
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2476
	/* find out if the dataset is mounted */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2477
	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2478
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2479
	/* get the corrupted object's path */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2480
	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2481
	zc.zc_obj = obj;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2482
	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2483
	    &zc) == 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2484
		if (mounted) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2485
			(void) snprintf(pathname, len, "%s%s", mntpnt,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2486
			    zc.zc_value);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2487
		} else {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2488
			(void) snprintf(pathname, len, "%s:%s",
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2489
			    dsname, zc.zc_value);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2490
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2491
	} else {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2492
		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2493
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2494
	free(mntpnt);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2495
}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  2496
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2497
#define	RDISK_ROOT	"/dev/rdsk"
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2498
#define	BACKUP_SLICE	"s2"
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2499
/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2500
 * Don't start the slice at the default block of 34; many storage
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2501
 * devices will use a stripe width of 128k, so start there instead.
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2502
 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2503
#define	NEW_START_BLOCK	256
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2504
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2505
/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2506
 * determine where a partition starts on a disk in the current
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2507
 * configuration
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2508
 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2509
static diskaddr_t
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2510
find_start_block(nvlist_t *config)
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2511
{
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2512
	nvlist_t **child;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2513
	uint_t c, children;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2514
	char *path;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2515
	diskaddr_t sb = MAXOFFSET_T;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2516
	int fd;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2517
	char diskname[MAXPATHLEN];
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2518
	uint64_t wholedisk;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2519
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2520
	if (nvlist_lookup_nvlist_array(config,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2521
	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2522
		if (nvlist_lookup_uint64(config,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2523
		    ZPOOL_CONFIG_WHOLE_DISK,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2524
		    &wholedisk) != 0 || !wholedisk) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2525
			return (MAXOFFSET_T);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2526
		}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2527
		if (nvlist_lookup_string(config,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2528
		    ZPOOL_CONFIG_PATH, &path) != 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2529
			return (MAXOFFSET_T);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2530
		}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2531
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2532
		(void) snprintf(diskname, sizeof (diskname), "%s%s",
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2533
		    RDISK_ROOT, strrchr(path, '/'));
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2534
		if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2535
			struct dk_gpt *vtoc;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2536
			if (efi_alloc_and_read(fd, &vtoc) >= 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2537
				sb = vtoc->efi_parts[0].p_start;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2538
				efi_free(vtoc);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2539
			}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2540
			(void) close(fd);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2541
		}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2542
		return (sb);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2543
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2544
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2545
	for (c = 0; c < children; c++) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2546
		sb = find_start_block(child[c]);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2547
		if (sb != MAXOFFSET_T) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2548
			return (sb);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2549
		}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2550
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2551
	return (MAXOFFSET_T);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2552
}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2553
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2554
/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2555
 * Label an individual disk.  The name provided is the short name,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2556
 * stripped of any leading /dev path.
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2557
 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2558
int
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2559
zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2560
{
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2561
	char path[MAXPATHLEN];
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2562
	struct dk_gpt *vtoc;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2563
	int fd;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2564
	size_t resv = EFI_MIN_RESV_SIZE;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2565
	uint64_t slice_size;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2566
	diskaddr_t start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2567
	char errbuf[1024];
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2568
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2569
	/* prepare an error message just in case */
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2570
	(void) snprintf(errbuf, sizeof (errbuf),
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2571
	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2572
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2573
	if (zhp) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2574
		nvlist_t *nvroot;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2575
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2576
		verify(nvlist_lookup_nvlist(zhp->zpool_config,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2577
		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2578
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2579
		if (zhp->zpool_start_block == 0)
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2580
			start_block = find_start_block(nvroot);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2581
		else
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2582
			start_block = zhp->zpool_start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2583
		zhp->zpool_start_block = start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2584
	} else {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2585
		/* new pool */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2586
		start_block = NEW_START_BLOCK;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2587
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2588
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2589
	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2590
	    BACKUP_SLICE);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2591
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2592
	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2593
		/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2594
		 * This shouldn't happen.  We've long since verified that this
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2595
		 * is a valid device.
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2596
		 */
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2597
		zfs_error_aux(hdl,
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2598
		    dgettext(TEXT_DOMAIN, "unable to open device"));
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2599
		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2600
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2601
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2602
	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2603
		/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2604
		 * The only way this can fail is if we run out of memory, or we
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2605
		 * were unable to read the disk's capacity
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2606
		 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2607
		if (errno == ENOMEM)
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2608
			(void) no_memory(hdl);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2609
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2610
		(void) close(fd);
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2611
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2612
		    "unable to read disk capacity"), name);
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2613
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2614
		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2615
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2616
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2617
	slice_size = vtoc->efi_last_u_lba + 1;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2618
	slice_size -= EFI_MIN_RESV_SIZE;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2619
	if (start_block == MAXOFFSET_T)
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2620
		start_block = NEW_START_BLOCK;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2621
	slice_size -= start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2622
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2623
	vtoc->efi_parts[0].p_start = start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2624
	vtoc->efi_parts[0].p_size = slice_size;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2625
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2626
	/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2627
	 * Why we use V_USR: V_BACKUP confuses users, and is considered
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2628
	 * disposable by some EFI utilities (since EFI doesn't have a backup
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2629
	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2630
	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2631
	 * etc. were all pretty specific.  V_USR is as close to reality as we
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2632
	 * can get, in the absence of V_OTHER.
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2633
	 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2634
	vtoc->efi_parts[0].p_tag = V_USR;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2635
	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2636
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2637
	vtoc->efi_parts[8].p_start = slice_size + start_block;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2638
	vtoc->efi_parts[8].p_size = resv;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2639
	vtoc->efi_parts[8].p_tag = V_RESERVED;
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2640
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2641
	if (efi_write(fd, vtoc) != 0) {
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2642
		/*
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2643
		 * Some block drivers (like pcata) may not support EFI
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2644
		 * GPT labels.  Print out a helpful error message dir-
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2645
		 * ecting the user to manually label the disk and give
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2646
		 * a specific slice.
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2647
		 */
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2648
		(void) close(fd);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2649
		efi_free(vtoc);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2650
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2651
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  2652
		    "try using fdisk(1M) and then provide a specific slice"));
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2653
		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2654
	}
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2655
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2656
	(void) close(fd);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2657
	efi_free(vtoc);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2658
	return (0);
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 3912
diff changeset
  2659
}