usr/src/lib/libzfs/common/libzfs_pool.c
author Tim Haley <Tim.Haley@Sun.COM>
Mon, 09 Nov 2009 23:34:30 -0700
changeset 11026 e8e10df16a8f
parent 10956 0c81acaaf614
child 11422 42768837421d
permissions -rw-r--r--
6899159 injection isn't trashing pools 6899160 pool recovery info sometimes not appearing
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
/*
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
    23
 * Copyright 2009 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
#include <ctype.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <devid.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <fcntl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <libintl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <stdlib.h>
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    34
#include <strings.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#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
    36
#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
    37
#include <sys/vtoc.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/zfs_ioctl.h>
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
    39
#include <dlfcn.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include "zfs_namecheck.h"
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
    42
#include "zfs_prop.h"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include "libzfs_impl.h"
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
    44
#include "zfs_comutil.h"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
10685
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    46
const char *hist_event_table[LOG_END] = {
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    47
	"invalid event",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    48
	"pool create",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    49
	"vdev add",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    50
	"pool remove",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    51
	"pool destroy",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    52
	"pool export",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    53
	"pool import",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    54
	"vdev attach",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    55
	"vdev replace",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    56
	"vdev detach",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    57
	"vdev online",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    58
	"vdev offline",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    59
	"vdev upgrade",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    60
	"pool clear",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    61
	"pool scrub",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    62
	"pool property set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    63
	"create",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    64
	"clone",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    65
	"destroy",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    66
	"destroy_begin_sync",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    67
	"inherit",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    68
	"property set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    69
	"quota set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    70
	"permission update",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    71
	"permission remove",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    72
	"permission who remove",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    73
	"promote",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    74
	"receive",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    75
	"rename",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    76
	"reservation set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    77
	"replay_inc_sync",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    78
	"replay_full_sync",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    79
	"rollback",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    80
	"snapshot",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    81
	"filesystem version upgrade",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    82
	"refquota set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    83
	"refreservation set",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    84
	"pool scrub done",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    85
	"user hold",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    86
	"user release",
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    87
};
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
    88
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
    89
static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
    90
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    91
#if defined(__i386) || defined(__amd64)
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    92
#define	BOOTCMD	"installgrub(1M)"
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    93
#else
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    94
#define	BOOTCMD	"installboot(1M)"
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    95
#endif
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
    96
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
    97
#define	DISK_ROOT	"/dev/dsk"
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
    98
#define	RDISK_ROOT	"/dev/rdsk"
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
    99
#define	BACKUP_SLICE	"s2"
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   100
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   101
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   102
 * ====================================================================
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   103
 *   zpool property functions
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
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   106
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   107
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   108
zpool_get_all_props(zpool_handle_t *zhp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   109
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   110
	zfs_cmd_t zc = { 0 };
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   111
	libzfs_handle_t *hdl = zhp->zpool_hdl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   112
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   113
	(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
   114
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   115
	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   116
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   117
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   118
	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
   119
		if (errno == ENOMEM) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   120
			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   121
				zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   122
				return (-1);
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
		} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   125
			zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   126
			return (-1);
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
	}
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
	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   131
		zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   132
		return (-1);
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
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   135
	zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   136
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   137
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   138
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   139
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   140
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   141
zpool_props_refresh(zpool_handle_t *zhp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   142
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   143
	nvlist_t *old_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   144
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   145
	old_props = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   146
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   147
	if (zpool_get_all_props(zhp) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   148
		return (-1);
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
	nvlist_free(old_props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   151
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   152
}
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
static char *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   155
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
   156
    zprop_source_t *src)
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
	nvlist_t *nv, *nvl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   159
	uint64_t ival;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   160
	char *value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   161
	zprop_source_t source;
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
	nvl = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   164
	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
   165
		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   166
		source = ival;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   167
		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   168
	} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   169
		source = ZPROP_SRC_DEFAULT;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   170
		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   171
			value = "-";
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   172
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   173
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   174
	if (src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   175
		*src = source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   176
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   177
	return (value);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   178
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   179
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   180
uint64_t
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   181
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
   182
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   183
	nvlist_t *nv, *nvl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   184
	uint64_t value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   185
	zprop_source_t source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   186
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   187
	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   188
		/*
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   189
		 * zpool_get_all_props() has most likely failed because
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   190
		 * the pool is faulted, but if all we need is the top level
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   191
		 * vdev's guid then get it from the zhp config nvlist.
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   192
		 */
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   193
		if ((prop == ZPOOL_PROP_GUID) &&
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   194
		    (nvlist_lookup_nvlist(zhp->zpool_config,
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   195
		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   196
		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   197
		    == 0)) {
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   198
			return (value);
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   199
		}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   200
		return (zpool_prop_default_numeric(prop));
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   201
	}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   202
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   203
	nvl = zhp->zpool_props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   204
	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
   205
		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   206
		source = value;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   207
		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   208
	} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   209
		source = ZPROP_SRC_DEFAULT;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   210
		value = zpool_prop_default_numeric(prop);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   211
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   212
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   213
	if (src)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   214
		*src = source;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   215
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   216
	return (value);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   217
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   218
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
 * Map VDEV STATE to printed strings.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   221
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   222
char *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   223
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
   224
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   225
	switch (state) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   226
	case VDEV_STATE_CLOSED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   227
	case VDEV_STATE_OFFLINE:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   228
		return (gettext("OFFLINE"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   229
	case VDEV_STATE_REMOVED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   230
		return (gettext("REMOVED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   231
	case VDEV_STATE_CANT_OPEN:
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 7214
diff changeset
   232
		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   233
			return (gettext("FAULTED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   234
		else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   235
			return (gettext("UNAVAIL"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   236
	case VDEV_STATE_FAULTED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   237
		return (gettext("FAULTED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   238
	case VDEV_STATE_DEGRADED:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   239
		return (gettext("DEGRADED"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   240
	case VDEV_STATE_HEALTHY:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   241
		return (gettext("ONLINE"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   242
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   243
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   244
	return (gettext("UNKNOWN"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   245
}
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
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   248
 * 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
   249
 * a pre-allocated buffer.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   250
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   251
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   252
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
   253
    zprop_source_t *srctype)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   254
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   255
	uint64_t intval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   256
	const char *strval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   257
	zprop_source_t src = ZPROP_SRC_NONE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   258
	nvlist_t *nvroot;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   259
	vdev_stat_t *vs;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   260
	uint_t vsc;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   261
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   262
	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   263
		switch (prop) {
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   264
		case ZPOOL_PROP_NAME:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   265
			(void) strlcpy(buf, zpool_get_name(zhp), len);
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   266
			break;
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   267
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   268
		case ZPOOL_PROP_HEALTH:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   269
			(void) strlcpy(buf, "FAULTED", len);
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   270
			break;
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   271
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   272
		case ZPOOL_PROP_GUID:
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   273
			intval = zpool_get_prop_int(zhp, prop, &src);
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   274
			(void) snprintf(buf, len, "%llu", intval);
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   275
			break;
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   276
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   277
		case ZPOOL_PROP_ALTROOT:
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   278
		case ZPOOL_PROP_CACHEFILE:
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   279
			if (zhp->zpool_props != NULL ||
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   280
			    zpool_get_all_props(zhp) == 0) {
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   281
				(void) strlcpy(buf,
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   282
				    zpool_get_prop_string(zhp, prop, &src),
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   283
				    len);
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   284
				if (srctype != NULL)
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   285
					*srctype = src;
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   286
				return (0);
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   287
			}
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   288
			/* FALLTHROUGH */
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   289
		default:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   290
			(void) strlcpy(buf, "-", len);
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   291
			break;
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   292
		}
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   293
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   294
		if (srctype != NULL)
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8211
diff changeset
   295
			*srctype = src;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   296
		return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   297
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   298
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   299
	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   300
	    prop != ZPOOL_PROP_NAME)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   301
		return (-1);
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
	switch (zpool_prop_get_type(prop)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   304
	case PROP_TYPE_STRING:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   305
		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   306
		    len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   307
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   308
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   309
	case PROP_TYPE_NUMBER:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   310
		intval = zpool_get_prop_int(zhp, prop, &src);
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
		switch (prop) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   313
		case ZPOOL_PROP_SIZE:
10956
0c81acaaf614 6897693 deduplication can only go so far
George Wilson <George.Wilson@Sun.COM>
parents: 10922
diff changeset
   314
		case ZPOOL_PROP_ALLOCATED:
0c81acaaf614 6897693 deduplication can only go so far
George Wilson <George.Wilson@Sun.COM>
parents: 10922
diff changeset
   315
		case ZPOOL_PROP_FREE:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   316
			(void) zfs_nicenum(intval, buf, len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   317
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   318
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   319
		case ZPOOL_PROP_CAPACITY:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   320
			(void) snprintf(buf, len, "%llu%%",
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   321
			    (u_longlong_t)intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   322
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   323
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   324
		case ZPOOL_PROP_DEDUPRATIO:
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   325
			(void) snprintf(buf, len, "%llu.%02llux",
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   326
			    (u_longlong_t)(intval / 100),
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   327
			    (u_longlong_t)(intval % 100));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   328
			break;
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10921
diff changeset
   329
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   330
		case ZPOOL_PROP_HEALTH:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   331
			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   332
			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   333
			verify(nvlist_lookup_uint64_array(nvroot,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   334
			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
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
			(void) strlcpy(buf, zpool_state_to_name(intval,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   337
			    vs->vs_aux), len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   338
			break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   339
		default:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   340
			(void) snprintf(buf, len, "%llu", intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   341
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   342
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   343
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   344
	case PROP_TYPE_INDEX:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   345
		intval = zpool_get_prop_int(zhp, prop, &src);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   346
		if (zpool_prop_index_to_string(prop, intval, &strval)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   347
		    != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   348
			return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   349
		(void) strlcpy(buf, strval, len);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   350
		break;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   351
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   352
	default:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   353
		abort();
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 (srctype)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   357
		*srctype = src;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   358
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   359
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   360
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   361
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
 * 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
   364
 * Assuming bootfs is a valid dataset name.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   365
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   366
static boolean_t
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   367
bootfs_name_valid(const char *pool, char *bootfs)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   368
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   369
	int len = strlen(pool);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   370
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7294
diff changeset
   371
	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   372
		return (B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   373
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   374
	if (strncmp(pool, bootfs, len) == 0 &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   375
	    (bootfs[len] == '/' || bootfs[len] == '\0'))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   376
		return (B_TRUE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   377
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   378
	return (B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   379
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   380
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   381
/*
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   382
 * Inspect the configuration to determine if any of the devices contain
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   383
 * an EFI label.
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   384
 */
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   385
static boolean_t
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   386
pool_uses_efi(nvlist_t *config)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   387
{
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   388
	nvlist_t **child;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   389
	uint_t c, children;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   390
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   391
	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   392
	    &child, &children) != 0)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   393
		return (read_efi_label(config, NULL) >= 0);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   394
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   395
	for (c = 0; c < children; c++) {
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   396
		if (pool_uses_efi(child[c]))
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   397
			return (B_TRUE);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   398
	}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   399
	return (B_FALSE);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   400
}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   401
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   402
static boolean_t
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   403
pool_is_bootable(zpool_handle_t *zhp)
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   404
{
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   405
	char bootfs[ZPOOL_MAXNAMELEN];
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   406
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   407
	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   408
	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   409
	    sizeof (bootfs)) != 0);
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   410
}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   411
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
   412
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   413
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   414
 * 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
   415
 * 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
   416
 * specified as strings.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   417
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   418
static nvlist_t *
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   419
zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   420
    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
   421
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   422
	nvpair_t *elem;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   423
	nvlist_t *retprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   424
	zpool_prop_t prop;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   425
	char *strval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   426
	uint64_t intval;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   427
	char *slash;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   428
	struct stat64 statbuf;
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   429
	zpool_handle_t *zhp;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   430
	nvlist_t *nvroot;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   431
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   432
	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   433
		(void) no_memory(hdl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   434
		return (NULL);
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
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   437
	elem = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   438
	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   439
		const char *propname = nvpair_name(elem);
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
		 * Make sure this property is valid and applies to this type.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   443
		 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   444
		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   445
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   446
			    "invalid property '%s'"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   447
			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   448
			goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   449
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   450
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   451
		if (zpool_prop_readonly(prop)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   452
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   453
			    "is readonly"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   454
			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   455
			goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   456
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   457
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   458
		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   459
		    &strval, &intval, errbuf) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   460
			goto error;
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
		/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   463
		 * Perform additional checking for specific properties.
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
		switch (prop) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   466
		case ZPOOL_PROP_VERSION:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   467
			if (intval < version || intval > SPA_VERSION) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   468
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   469
				    "property '%s' number %d is invalid."),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   470
				    propname, intval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   471
				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   472
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   473
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   474
			break;
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
		case ZPOOL_PROP_BOOTFS:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   477
			if (create_or_import) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   478
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   479
				    "property '%s' cannot be set at creation "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   480
				    "or import time"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   481
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   482
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   483
			}
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 (version < SPA_VERSION_BOOTFS) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   486
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   487
				    "pool must be upgraded to support "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   488
				    "'%s' property"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   489
				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   490
				goto error;
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
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   493
			/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   494
			 * bootfs property value has to be a dataset name and
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   495
			 * 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
   496
			 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   497
			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   498
			    strval)) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   499
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   500
				    "is an invalid name"), strval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   501
				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   502
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   503
			}
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   504
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   505
			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   506
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   507
				    "could not open pool '%s'"), poolname);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   508
				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   509
				goto error;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   510
			}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   511
			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   512
			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   513
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   514
			/*
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   515
			 * bootfs property cannot be set on a disk which has
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   516
			 * been EFI labeled.
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   517
			 */
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   518
			if (pool_uses_efi(nvroot)) {
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   519
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   520
				    "property '%s' not supported on "
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   521
				    "EFI labeled devices"), propname);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   522
				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   523
				zpool_close(zhp);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   524
				goto error;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   525
			}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
   526
			zpool_close(zhp);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   527
			break;
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
		case ZPOOL_PROP_ALTROOT:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   530
			if (!create_or_import) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   531
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   532
				    "property '%s' can only be set during pool "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   533
				    "creation or import"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   534
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   535
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   536
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   537
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   538
			if (strval[0] != '/') {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   539
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   540
				    "bad alternate root '%s'"), strval);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   541
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   542
				goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   543
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   544
			break;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   545
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   546
		case ZPOOL_PROP_CACHEFILE:
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   547
			if (strval[0] == '\0')
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   548
				break;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   549
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   550
			if (strcmp(strval, "none") == 0)
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   551
				break;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   552
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   553
			if (strval[0] != '/') {
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   554
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   555
				    "property '%s' must be empty, an "
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   556
				    "absolute path, or 'none'"), propname);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   557
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   558
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   559
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   560
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   561
			slash = strrchr(strval, '/');
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   562
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   563
			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   564
			    strcmp(slash, "/..") == 0) {
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   565
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   566
				    "'%s' is not a valid file"), strval);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   567
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   568
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   569
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   570
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   571
			*slash = '\0';
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   572
5621
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   573
			if (strval[0] != '\0' &&
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   574
			    (stat64(strval, &statbuf) != 0 ||
cd0984d5b1c1 6627006 setting cachefile to default value confuses ZFS
eschrock
parents: 5450
diff changeset
   575
			    !S_ISDIR(statbuf.st_mode))) {
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   576
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   577
				    "'%s' is not a valid directory"),
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   578
				    strval);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   579
				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   580
				goto error;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   581
			}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   582
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   583
			*slash = '/';
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5320
diff changeset
   584
			break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   585
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   586
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   587
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   588
	return (retprops);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   589
error:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   590
	nvlist_free(retprops);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   591
	return (NULL);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   592
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   593
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   594
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   595
 * Set zpool property : propname=propval.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   596
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   597
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   598
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
   599
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   600
	zfs_cmd_t zc = { 0 };
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   601
	int ret = -1;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   602
	char errbuf[1024];
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   603
	nvlist_t *nvl = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   604
	nvlist_t *realprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   605
	uint64_t version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   606
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   607
	(void) snprintf(errbuf, sizeof (errbuf),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   608
	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   609
	    zhp->zpool_name);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   610
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   611
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   612
		return (no_memory(zhp->zpool_hdl));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   613
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   614
	if (nvlist_add_string(nvl, propname, propval) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   615
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   616
		return (no_memory(zhp->zpool_hdl));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   617
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   618
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   619
	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   620
	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   621
	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   622
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   623
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   624
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   625
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   626
	nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   627
	nvl = realprops;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   628
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   629
	/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   630
	 * Execute the corresponding ioctl() to set this property.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   631
	 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   632
	(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
   633
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   634
	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   635
		nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   636
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   637
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   638
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   639
	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
   640
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   641
	zcmd_free_nvlists(&zc);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   642
	nvlist_free(nvl);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   643
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   644
	if (ret)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   645
		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   646
	else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   647
		(void) zpool_props_refresh(zhp);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   648
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   649
	return (ret);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   650
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   651
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   652
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   653
zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   654
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   655
	libzfs_handle_t *hdl = zhp->zpool_hdl;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   656
	zprop_list_t *entry;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   657
	char buf[ZFS_MAXPROPLEN];
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   658
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   659
	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   660
		return (-1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   661
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   662
	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   663
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   664
		if (entry->pl_fixed)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   665
			continue;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   666
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   667
		if (entry->pl_prop != ZPROP_INVAL &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   668
		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   669
		    NULL) == 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   670
			if (strlen(buf) > entry->pl_width)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   671
				entry->pl_width = strlen(buf);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   672
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   673
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   674
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   675
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   676
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   677
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   678
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
/*
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   680
 * Don't start the slice at the default block of 34; many storage
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   681
 * devices will use a stripe width of 128k, so start there instead.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   682
 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   683
#define	NEW_START_BLOCK	256
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   684
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
   685
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
 * Validate the given pool name, optionally putting an extended error message in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
 * 'buf'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
 */
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
   689
boolean_t
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   690
zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	namecheck_err_t why;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
	char what;
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   694
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   696
	ret = pool_namecheck(pool, &why, &what);
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   697
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   698
	/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   699
	 * 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
   700
	 * 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
   701
	 * 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
   702
	 * 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
   703
	 */
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   704
	if (ret == 0 && !isopen &&
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   705
	    (strncmp(pool, "mirror", 6) == 0 ||
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   706
	    strncmp(pool, "raidz", 5) == 0 ||
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   707
	    strncmp(pool, "spare", 5) == 0 ||
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   708
	    strcmp(pool, "log") == 0)) {
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
   709
		if (hdl != NULL)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
   710
			zfs_error_aux(hdl,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
   711
			    dgettext(TEXT_DOMAIN, "name is reserved"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   712
		return (B_FALSE);
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   713
	}
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   714
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   715
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1760
diff changeset
   716
	if (ret != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   717
		if (hdl != NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
			switch (why) {
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   719
			case NAME_ERR_TOOLONG:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   720
				zfs_error_aux(hdl,
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   721
				    dgettext(TEXT_DOMAIN, "name is too long"));
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   722
				break;
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 952
diff changeset
   723
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
			case NAME_ERR_INVALCHAR:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   725
				zfs_error_aux(hdl,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
				    dgettext(TEXT_DOMAIN, "invalid character "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
				    "'%c' in pool name"), what);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
			case NAME_ERR_NOLETTER:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   731
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   732
				    "name must begin with a letter"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
			case NAME_ERR_RESERVED:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   736
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   737
				    "name is reserved"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
			case NAME_ERR_DISKLIKE:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   741
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   742
				    "pool name is reserved"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
				break;
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   744
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   745
			case NAME_ERR_LEADING_SLASH:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   746
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   747
				    "leading slash in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   748
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   749
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   750
			case NAME_ERR_EMPTY_COMPONENT:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   751
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   752
				    "empty component in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   753
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   754
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   755
			case NAME_ERR_TRAILING_SLASH:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   756
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   757
				    "trailing slash in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   758
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   759
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   760
			case NAME_ERR_MULTIPLE_AT:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   761
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   762
				    "multiple '@' delimiters in name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   763
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
   764
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
		}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   767
		return (B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   770
	return (B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
 * 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
   775
 * state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
zpool_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   778
zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
	zpool_handle_t *zhp;
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   781
	boolean_t missing;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
	 * Make sure the pool name is valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   786
	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
   787
		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   788
		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   789
		    pool);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   793
	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   794
		return (NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   796
	zhp->zpool_hdl = hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   799
	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
   800
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   801
		return (NULL);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   802
	}
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   803
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   804
	if (missing) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   805
		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
   806
		(void) zfs_error_fmt(hdl, EZFS_NOENT,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   807
		    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
   808
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   809
		return (NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
 * Like the above, but silent on error.  Used when iterating over pools (because
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
 * the configuration cache may be out of date).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
 */
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   819
int
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   820
zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
	zpool_handle_t *zhp;
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   823
	boolean_t missing;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   825
	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
   826
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   828
	zhp->zpool_hdl = hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   831
	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
   832
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   833
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   836
	if (missing) {
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   837
		zpool_close(zhp);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   838
		*ret = NULL;
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   839
		return (0);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   840
	}
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   841
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   842
	*ret = zhp;
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   843
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
 * state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
zpool_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   851
zpool_open(libzfs_handle_t *hdl, const char *pool)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   855
	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
	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
   859
		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   860
		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
 * Close the handle.  Simply frees the memory associated with the handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
zpool_close(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
	if (zhp->zpool_config)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
		nvlist_free(zhp->zpool_config);
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 789
diff changeset
   876
	if (zhp->zpool_old_config)
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 789
diff changeset
   877
		nvlist_free(zhp->zpool_old_config);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   878
	if (zhp->zpool_props)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
   879
		nvlist_free(zhp->zpool_props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
	free(zhp);
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
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
 * Return the name of the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
zpool_get_name(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
	return (zhp->zpool_name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
}
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
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
 * Return the state of the pool (ACTIVE or UNAVAILABLE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
zpool_get_state(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
	return (zhp->zpool_state);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   903
 * Create the named pool, using the provided vdev list.  It is assumed
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   904
 * that the consumer has already validated the contents of the nvlist, so we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   905
 * don't have to worry about error semantics.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   906
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   907
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   908
zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   909
    nvlist_t *props, nvlist_t *fsprops)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   910
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   911
	zfs_cmd_t zc = { 0 };
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   912
	nvlist_t *zc_fsprops = NULL;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   913
	nvlist_t *zc_props = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   914
	char msg[1024];
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   915
	char *altroot;
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   916
	int ret = -1;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   917
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   918
	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   919
	    "cannot create '%s'"), pool);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   920
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   921
	if (!zpool_name_valid(hdl, B_FALSE, pool))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   922
		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   923
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   924
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   925
		return (-1);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   926
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   927
	if (props) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   928
		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   929
		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   930
			goto create_failed;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   931
		}
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
   932
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   934
	if (fsprops) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   935
		uint64_t zoned;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   936
		char *zonestr;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   937
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   938
		zoned = ((nvlist_lookup_string(fsprops,
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   939
		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   940
		    strcmp(zonestr, "on") == 0);
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   941
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   942
		if ((zc_fsprops = zfs_valid_proplist(hdl,
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   943
		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   944
			goto create_failed;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   945
		}
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   946
		if (!zc_props &&
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   947
		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   948
			goto create_failed;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   949
		}
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   950
		if (nvlist_add_nvlist(zc_props,
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   951
		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   952
			goto create_failed;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   953
		}
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   954
	}
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   955
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   956
	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   957
		goto create_failed;
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   958
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   961
	if ((ret = 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
   962
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
   963
		zcmd_free_nvlists(&zc);
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   964
		nvlist_free(zc_props);
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
   965
		nvlist_free(zc_fsprops);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   966
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   968
		case EBUSY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   970
			 * This can happen if the user has specified the same
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   971
			 * device multiple times.  We can't reliably detect this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
			 * until we try to add it and see we already have a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
			 * label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   975
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   976
			    "one or more vdevs refer to the same device"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   977
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
			/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   981
			 * This occurs when one of the devices is below
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
			 * device was the problem device since there's no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
			 * reliable way to determine device size from userland.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
			{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
				char buf[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   991
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   992
				    "one or more devices is less than the "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   993
				    "minimum size (%s)"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   995
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
		case ENOSPC:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   998
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
   999
			    "one or more devices is out of space"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1000
			return (zfs_error(hdl, EZFS_BADDEV, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1002
		case ENOTBLK:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1003
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1004
			    "cache device must be a disk or disk slice"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1005
			return (zfs_error(hdl, EZFS_BADDEV, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1006
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1008
			return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
	 * 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
  1014
	 * mountpoint of the root dataset to be '/'.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
	 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1016
	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
  1017
	    &altroot) == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
		zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1020
		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1021
		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1022
		    "/") == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
  1027
create_failed:
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1028
	zcmd_free_nvlists(&zc);
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
  1029
	nvlist_free(zc_props);
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
  1030
	nvlist_free(zc_fsprops);
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
  1031
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
 * 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
  1036
 * datasets left in the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
zpool_destroy(zpool_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
	zfs_handle_t *zfp = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1043
	libzfs_handle_t *hdl = zhp->zpool_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1044
	char msg[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1045
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1047
	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1048
	    ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1052
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1053
	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
  1054
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1055
		    "cannot destroy '%s'"), zhp->zpool_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1057
		if (errno == EROFS) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1058
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1059
			    "one or more devices is read only"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1060
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1061
		} else {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1062
			(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
		if (zfp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
			zfs_close(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
	if (zfp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
		remove_mountpoint(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
		zfs_close(zfp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1077
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1078
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
 * Add the given vdevs to the pool.  The caller must have already performed the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
 * necessary verification to ensure that the vdev specification is well-formed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1085
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1086
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1087
	libzfs_handle_t *hdl = zhp->zpool_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1088
	char msg[1024];
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1089
	nvlist_t **spares, **l2cache;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1090
	uint_t nspares, nl2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1091
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1092
	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1093
	    "cannot add to '%s'"), zhp->zpool_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1094
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1095
	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1096
	    SPA_VERSION_SPARES &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1097
	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1098
	    &spares, &nspares) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1099
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1100
		    "upgraded to add hot spares"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1101
		return (zfs_error(hdl, EZFS_BADVERSION, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1102
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1104
	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1105
	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1106
		uint64_t s;
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1107
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1108
		for (s = 0; s < nspares; s++) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1109
			char *path;
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1110
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1111
			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1112
			    &path) == 0 && pool_uses_efi(spares[s])) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1113
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1114
				    "device '%s' contains an EFI label and "
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1115
				    "cannot be used on root pools."),
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1116
				    zpool_vdev_name(hdl, NULL, spares[s],
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1117
				    B_FALSE));
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1118
				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1119
			}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1120
		}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1121
	}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  1122
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1123
	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1124
	    SPA_VERSION_L2CACHE &&
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1125
	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1126
	    &l2cache, &nl2cache) == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1127
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1128
		    "upgraded to add cache devices"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1129
		return (zfs_error(hdl, EZFS_BADVERSION, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1130
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1131
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1132
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1133
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1136
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
		case EBUSY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
			 * This can happen if the user has specified the same
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
			 * device multiple times.  We can't reliably detect this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
			 * until we try to add it and see we already have a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
			 * label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1145
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1146
			    "one or more vdevs refer to the same device"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1147
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
			 * This occurrs when one of the devices is below
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
			 * device was the problem device since there's no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
			 * reliable way to determine device size from userland.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
			{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
				char buf[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1162
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1163
				    "device is less than the minimum "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1164
				    "size (%s)"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1166
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1167
			break;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1168
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1169
		case ENOTSUP:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1170
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1171
			    "pool must be upgraded to add these vdevs"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1172
			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1173
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  1175
		case EDOM:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  1176
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1177
			    "root pool can not have multiple vdevs"
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1178
			    " or separate logs"));
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  1179
			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  1180
			break;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  1181
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1182
		case ENOTBLK:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1183
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1184
			    "cache device must be a disk or disk slice"));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1185
			(void) zfs_error(hdl, EZFS_BADDEV, msg);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1186
			break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1187
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1189
			(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1192
		ret = -1;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1193
	} else {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1194
		ret = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1197
	zcmd_free_nvlists(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1199
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
 * Exports the pool from the system.  The caller must ensure that there are no
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
 * mounted datasets in the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
int
8211
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1207
zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
	zfs_cmd_t zc = { 0 };
7214
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1210
	char msg[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
7214
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1212
	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1213
	    "cannot export '%s'"), zhp->zpool_name);
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1214
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1215
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7214
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1216
	zc.zc_cookie = force;
8211
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1217
	zc.zc_guid = hardforce;
7214
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1218
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1219
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1220
		switch (errno) {
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1221
		case EXDEV:
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1222
			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1223
			    "use '-f' to override the following errors:\n"
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1224
			    "'%s' has an active shared spare which could be"
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1225
			    " used by other pools once '%s' is exported."),
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1226
			    zhp->zpool_name, zhp->zpool_name);
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1227
			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1228
			    msg));
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1229
		default:
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1230
			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1231
			    msg));
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1232
		}
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1233
	}
04c540040a32 6721908 A hot spare "in use" in an exported zpool, is stolen when a disk fails in an imported pool.
lling
parents: 7184
diff changeset
  1234
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1235
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
8211
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1238
int
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1239
zpool_export(zpool_handle_t *zhp, boolean_t force)
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1240
{
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1241
	return (zpool_export_common(zhp, force, B_FALSE));
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1242
}
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1243
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1244
int
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1245
zpool_export_force(zpool_handle_t *zhp)
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1246
{
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1247
	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1248
}
32722be6ad3b 6775357 ZFS should have a way to export a pool without touching anything
George Wilson <George.Wilson@Sun.COM>
parents: 8084
diff changeset
  1249
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1250
static void
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1251
zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1252
    nvlist_t *rbi)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1253
{
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1254
	uint64_t rewindto;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1255
	int64_t loss = -1;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1256
	struct tm t;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1257
	char timestr[128];
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1258
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1259
	if (!hdl->libzfs_printerr || rbi == NULL)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1260
		return;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1261
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1262
	if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1263
		return;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1264
	(void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1265
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1266
	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1267
	    strftime(timestr, 128, 0, &t) != 0) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1268
		if (dryrun) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1269
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1270
			    "Would be able to return %s "
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1271
			    "to its state as of %s.\n"),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1272
			    name, timestr);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1273
		} else {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1274
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1275
			    "Pool %s returned to its state as of %s.\n"),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1276
			    name, timestr);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1277
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1278
		if (loss > 120) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1279
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1280
			    "%s approximately %lld "),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1281
			    dryrun ? "Would discard" : "Discarded",
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1282
			    (loss + 30) / 60);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1283
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1284
			    "minutes of transactions.\n"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1285
		} else if (loss > 0) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1286
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1287
			    "%s approximately %lld "),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1288
			    dryrun ? "Would discard" : "Discarded", loss);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1289
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1290
			    "seconds of transactions.\n"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1291
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1292
	}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1293
}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1294
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1295
void
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1296
zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1297
    nvlist_t *config)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1298
{
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1299
	int64_t loss = -1;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1300
	uint64_t edata = UINT64_MAX;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1301
	uint64_t rewindto;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1302
	struct tm t;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1303
	char timestr[128];
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1304
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1305
	if (!hdl->libzfs_printerr)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1306
		return;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1307
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1308
	if (reason >= 0)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1309
		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1310
	else
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1311
		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1312
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1313
	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1314
	if (nvlist_lookup_uint64(config,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1315
	    ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1316
		goto no_info;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1317
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1318
	(void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1319
	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1320
	    &edata);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1321
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1322
	(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1323
	    "Recovery is possible, but will result in some data loss.\n"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1324
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1325
	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1326
	    strftime(timestr, 128, 0, &t) != 0) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1327
		(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1328
		    "\tReturning the pool to its state as of %s\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1329
		    "\tshould correct the problem.  "),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1330
		    timestr);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1331
	} else {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1332
		(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1333
		    "\tReverting the pool to an earlier state "
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1334
		    "should correct the problem.\n\t"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1335
	}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1336
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1337
	if (loss > 120) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1338
		(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1339
		    "Approximately %lld minutes of data\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1340
		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1341
	} else if (loss > 0) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1342
		(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1343
		    "Approximately %lld seconds of data\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1344
		    "\tmust be discarded, irreversibly.  "), loss);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1345
	}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1346
	if (edata != 0 && edata != UINT64_MAX) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1347
		if (edata == 1) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1348
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1349
			    "After rewind, at least\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1350
			    "\tone persistent user-data error will remain.  "));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1351
		} else {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1352
			(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1353
			    "After rewind, several\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1354
			    "\tpersistent user-data errors will remain.  "));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1355
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1356
	}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1357
	(void) printf(dgettext(TEXT_DOMAIN,
11026
e8e10df16a8f 6899159 injection isn't trashing pools
Tim Haley <Tim.Haley@Sun.COM>
parents: 10956
diff changeset
  1358
	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
e8e10df16a8f 6899159 injection isn't trashing pools
Tim Haley <Tim.Haley@Sun.COM>
parents: 10956
diff changeset
  1359
	    reason >= 0 ? "clear" : "import", name);
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1360
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1361
	(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1362
	    "A scrub of the pool\n"
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1363
	    "\tis strongly recommended after recovery.\n"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1364
	return;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1365
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1366
no_info:
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1367
	(void) printf(dgettext(TEXT_DOMAIN,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1368
	    "Destroy and re-create the pool from\n\ta backup source.\n"));
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1369
}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1370
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1371
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1372
 * zpool_import() is a contracted interface. Should be kept the same
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1373
 * if possible.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1374
 *
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1375
 * Applications should use zpool_import_props() to import a pool with
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1376
 * new properties value to be set.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1377
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1378
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1379
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
  1380
    char *altroot)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1381
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1382
	nvlist_t *props = NULL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1383
	int ret;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1384
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1385
	if (altroot != NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1386
		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1387
			return (zfs_error_fmt(hdl, EZFS_NOMEM,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1388
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1389
			    newname));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1390
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1391
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1392
		if (nvlist_add_string(props,
8084
b811cc60d650 6769612 zpool_import() will continue to write to cachefile even if altroot is set
George Wilson <George.Wilson@Sun.COM>
parents: 7965
diff changeset
  1393
		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
b811cc60d650 6769612 zpool_import() will continue to write to cachefile even if altroot is set
George Wilson <George.Wilson@Sun.COM>
parents: 7965
diff changeset
  1394
		    nvlist_add_string(props,
b811cc60d650 6769612 zpool_import() will continue to write to cachefile even if altroot is set
George Wilson <George.Wilson@Sun.COM>
parents: 7965
diff changeset
  1395
		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1396
			nvlist_free(props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1397
			return (zfs_error_fmt(hdl, EZFS_NOMEM,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1398
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1399
			    newname));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1400
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1401
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1402
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6423
diff changeset
  1403
	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1404
	if (props)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1405
		nvlist_free(props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1406
	return (ret);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1407
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1408
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1409
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1410
 * 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
  1411
 * properties to be set. The configuration should have come from
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1412
 * zpool_find_import(). The 'newname' parameters control whether the pool
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1413
 * is imported with a different name.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1414
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1415
int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1416
zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6423
diff changeset
  1417
    nvlist_t *props, boolean_t importfaulted)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1418
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1419
	zfs_cmd_t zc = { 0 };
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1420
	zpool_rewind_policy_t policy;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1421
	nvlist_t *nvi = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1422
	char *thename;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1423
	char *origname;
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1424
	uint64_t returned_size;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1425
	int ret;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1426
	char errbuf[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1427
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1428
	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1429
	    &origname) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1430
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1431
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1432
	    "cannot import pool '%s'"), origname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1433
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1434
	if (newname != NULL) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1435
		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
  1436
			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1437
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1438
			    newname));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1439
		thename = (char *)newname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1440
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
		thename = origname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1442
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1443
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1444
	if (props) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1445
		uint64_t version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1446
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1447
		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1448
		    &version) == 0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1449
7184
9508660f9c27 PSARC 2008/223 Pool root file system properties via zpool(1M)
timh
parents: 7042
diff changeset
  1450
		if ((props = zpool_valid_proplist(hdl, origname,
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1451
		    props, version, B_TRUE, errbuf)) == NULL) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1452
			return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1453
		} 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
  1454
			nvlist_free(props);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1455
			return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1456
		}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1457
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1458
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1459
	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1460
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1461
	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1462
	    &zc.zc_guid) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1463
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1464
	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1465
		nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1466
		return (-1);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1467
	}
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1468
	returned_size =  zc.zc_nvlist_conf_size + 512;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1469
	if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1470
		nvlist_free(props);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1471
		return (-1);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1472
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1473
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6423
diff changeset
  1474
	zc.zc_cookie = (uint64_t)importfaulted;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1475
	ret = 0;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1476
	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
		char desc[1024];
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1478
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1479
		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1480
		zpool_get_rewind_policy(config, &policy);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1481
		/*
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1482
		 * Dry-run failed, but we print out what success
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1483
		 * looks like if we found a best txg
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1484
		 */
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1485
		if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1486
			zpool_rewind_exclaim(hdl, newname ? origname : thename,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1487
			    B_TRUE, nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1488
			nvlist_free(nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1489
			return (-1);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1490
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1491
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1492
		if (newname == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
			(void) snprintf(desc, sizeof (desc),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1495
			    thename);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
			(void) snprintf(desc, sizeof (desc),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
			    origname, thename);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1500
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1501
		switch (errno) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1502
		case ENOTSUP:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1503
			/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1504
			 * Unsupported version.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1505
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1506
			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1507
			break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1508
2174
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1509
		case EINVAL:
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1510
			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1511
			break;
73de7a781492 6433717 offline devices should not be marked persistently unavailble
eschrock
parents: 2142
diff changeset
  1512
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1513
		default:
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1514
			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1515
			(void) zpool_standard_error(hdl, errno, desc);
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1516
			zpool_explain_recover(hdl,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1517
			    newname ? origname : thename, -errno, nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1518
			nvlist_free(nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1519
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1520
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1521
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1522
		ret = -1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1523
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1524
		zpool_handle_t *zhp;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1525
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
		 * This should never fail, but play it safe anyway.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
		 */
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9816
diff changeset
  1529
		if (zpool_open_silent(hdl, thename, &zhp) != 0)
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1530
			ret = -1;
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9816
diff changeset
  1531
		else if (zhp != NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1532
			zpool_close(zhp);
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1533
		(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1534
		zpool_get_rewind_policy(config, &policy);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1535
		if (policy.zrp_request &
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1536
		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1537
			zpool_rewind_exclaim(hdl, newname ? origname : thename,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1538
			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1539
			    nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1540
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1541
		nvlist_free(nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  1542
		return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1543
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  1545
	zcmd_free_nvlists(&zc);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1546
	nvlist_free(props);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  1547
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1551
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1552
 * Scrub the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1553
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1555
zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1556
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1559
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
	zc.zc_cookie = type;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1563
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1564
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1566
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1567
	(void) snprintf(msg, sizeof (msg),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1570
	if (errno == EBUSY)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1571
		return (zfs_error(hdl, EZFS_RESILVERING, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1572
	else
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1573
		return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1575
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1576
/*
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1577
 * Find a vdev that matches the search criteria specified. We use the
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1578
 * the nvpair name to determine how we should look for the device.
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1579
 * '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
  1580
 * spare; but FALSE if its an INUSE spare.
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1581
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1582
static nvlist_t *
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1583
vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1584
    boolean_t *l2cache, boolean_t *log)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1585
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1586
	uint_t c, children;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1587
	nvlist_t **child;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1588
	nvlist_t *ret;
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1589
	uint64_t is_log;
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1590
	char *srchkey;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1591
	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1592
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1593
	/* Nothing to look for */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1594
	if (search == NULL || pair == NULL)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1595
		return (NULL);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1596
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1597
	/* Obtain the key we will use to search */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1598
	srchkey = nvpair_name(pair);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1599
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1600
	switch (nvpair_type(pair)) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1601
	case DATA_TYPE_UINT64: {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1602
		uint64_t srchval, theguid, present;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1603
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1604
		verify(nvpair_value_uint64(pair, &srchval) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1605
		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1606
			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1607
			    &present) == 0) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1608
				/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1609
				 * If the device has never been present since
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1610
				 * import, the only reliable way to match the
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1611
				 * vdev is by GUID.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1612
				 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1613
				verify(nvlist_lookup_uint64(nv,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1614
				    ZPOOL_CONFIG_GUID, &theguid) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1615
				if (theguid == srchval)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1616
					return (nv);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1617
			}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1618
		}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1619
		break;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1620
	}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1621
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1622
	case DATA_TYPE_STRING: {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1623
		char *srchval, *val;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1624
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1625
		verify(nvpair_value_string(pair, &srchval) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1626
		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1627
			break;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1628
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1629
		/*
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1630
		 * Search for the requested value. We special case the search
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1631
		 * for ZPOOL_CONFIG_PATH when it's a wholedisk and when
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1632
		 * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1633
		 * Otherwise, all other searches are simple string compares.
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1634
		 */
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1635
		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1636
			uint64_t wholedisk = 0;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1637
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1638
			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1639
			    &wholedisk);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1640
			if (wholedisk) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1641
				/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1642
				 * For whole disks, the internal path has 's0',
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1643
				 * but the path passed in by the user doesn't.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1644
				 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1645
				if (strlen(srchval) == strlen(val) - 2 &&
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1646
				    strncmp(srchval, val, strlen(srchval)) == 0)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1647
					return (nv);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1648
				break;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1649
			}
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1650
		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1651
			char *type, *idx, *end, *p;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1652
			uint64_t id, vdev_id;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1653
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1654
			/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1655
			 * Determine our vdev type, keeping in mind
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1656
			 * that the srchval is composed of a type and
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1657
			 * vdev id pair (i.e. mirror-4).
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1658
			 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1659
			if ((type = strdup(srchval)) == NULL)
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1660
				return (NULL);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1661
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1662
			if ((p = strrchr(type, '-')) == NULL) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1663
				free(type);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1664
				break;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1665
			}
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1666
			idx = p + 1;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1667
			*p = '\0';
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1668
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1669
			/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1670
			 * If the types don't match then keep looking.
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1671
			 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1672
			if (strncmp(val, type, strlen(val)) != 0) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1673
				free(type);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1674
				break;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1675
			}
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1676
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1677
			verify(strncmp(type, VDEV_TYPE_RAIDZ,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1678
			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1679
			    strncmp(type, VDEV_TYPE_MIRROR,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1680
			    strlen(VDEV_TYPE_MIRROR)) == 0);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1681
			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1682
			    &id) == 0);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1683
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1684
			errno = 0;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1685
			vdev_id = strtoull(idx, &end, 10);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1686
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1687
			free(type);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1688
			if (errno != 0)
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1689
				return (NULL);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1690
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1691
			/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1692
			 * Now verify that we have the correct vdev id.
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1693
			 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1694
			if (vdev_id == id)
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1695
				return (nv);
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1696
		}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1697
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1698
		/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1699
		 * Common case
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1700
		 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1701
		if (strcmp(srchval, val) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1702
			return (nv);
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1703
		break;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1704
	}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1705
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1706
	default:
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1707
		break;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1708
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1709
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1710
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1711
	    &child, &children) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1712
		return (NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1713
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1714
	for (c = 0; c < children; c++) {
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1715
		if ((ret = vdev_to_nvlist_iter(child[c], search,
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1716
		    avail_spare, l2cache, NULL)) != NULL) {
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1717
			/*
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1718
			 * The 'is_log' value is only set for the toplevel
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1719
			 * vdev, not the leaf vdevs.  So we always lookup the
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1720
			 * log device from the root of the vdev tree (where
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1721
			 * 'log' is non-NULL).
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1722
			 */
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1723
			if (log != NULL &&
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1724
			    nvlist_lookup_uint64(child[c],
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1725
			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1726
			    is_log) {
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1727
				*log = B_TRUE;
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1728
			}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1729
			return (ret);
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1730
		}
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1731
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1732
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1733
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1734
	    &child, &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1735
		for (c = 0; c < children; c++) {
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1736
			if ((ret = vdev_to_nvlist_iter(child[c], search,
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1737
			    avail_spare, l2cache, NULL)) != NULL) {
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1738
				*avail_spare = B_TRUE;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1739
				return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1740
			}
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
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1743
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1744
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1745
	    &child, &children) == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1746
		for (c = 0; c < children; c++) {
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1747
			if ((ret = vdev_to_nvlist_iter(child[c], search,
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1748
			    avail_spare, l2cache, NULL)) != NULL) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1749
				*l2cache = B_TRUE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1750
				return (ret);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1751
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1752
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1753
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1754
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1755
	return (NULL);
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
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1758
/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1759
 * Given a physical path (minus the "/devices" prefix), find the
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1760
 * associated vdev.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1761
 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1762
nvlist_t *
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1763
zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1764
    boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1765
{
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1766
	nvlist_t *search, *nvroot, *ret;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1767
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1768
	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1769
	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1770
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1771
	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1772
	    &nvroot) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1773
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1774
	*avail_spare = B_FALSE;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1775
	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1776
	nvlist_free(search);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1777
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1778
	return (ret);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1779
}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1780
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1781
/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1782
 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1783
 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1784
boolean_t
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1785
zpool_vdev_is_interior(const char *name)
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1786
{
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1787
	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1788
	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1789
		return (B_TRUE);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1790
	return (B_FALSE);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1791
}
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1792
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  1793
nvlist_t *
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1794
zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1795
    boolean_t *l2cache, boolean_t *log)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1796
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1797
	char buf[MAXPATHLEN];
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1798
	char *end;
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1799
	nvlist_t *nvroot, *search, *ret;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1800
	uint64_t guid;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1801
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1802
	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1803
1613
aca102a242d3 6396628 zpool offline does not recognize c0d0 patten recently
eschrock
parents: 1544
diff changeset
  1804
	guid = strtoull(path, &end, 10);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1805
	if (guid != 0 && *end == '\0') {
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1806
		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1807
	} else if (zpool_vdev_is_interior(path)) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  1808
		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1809
	} else if (path[0] != '/') {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1810
		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1811
		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1812
	} else {
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1813
		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1814
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1815
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1816
	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1817
	    &nvroot) == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1818
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1819
	*avail_spare = B_FALSE;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  1820
	*l2cache = B_FALSE;
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1821
	if (log != NULL)
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  1822
		*log = B_FALSE;
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1823
	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1824
	nvlist_free(search);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1825
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1826
	return (ret);
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1827
}
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1828
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1829
static int
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1830
vdev_online(nvlist_t *nv)
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1831
{
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1832
	uint64_t ival;
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1833
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1834
	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1835
	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1836
	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1837
		return (0);
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1838
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1839
	return (1);
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1840
}
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1841
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1842
/*
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1843
 * Helper function for zpool_get_physpaths().
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1844
 */
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1845
static int
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1846
vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1847
    size_t *bytes_written)
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1848
{
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1849
	size_t bytes_left, pos, rsz;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1850
	char *tmppath;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1851
	const char *format;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1852
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1853
	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1854
	    &tmppath) != 0)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1855
		return (EZFS_NODEVICE);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1856
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1857
	pos = *bytes_written;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1858
	bytes_left = physpath_size - pos;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1859
	format = (pos == 0) ? "%s" : " %s";
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1860
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1861
	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1862
	*bytes_written += rsz;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1863
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1864
	if (rsz >= bytes_left) {
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1865
		/* if physpath was not copied properly, clear it */
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1866
		if (bytes_left != 0) {
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1867
			physpath[pos] = 0;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1868
		}
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1869
		return (EZFS_NOSPC);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1870
	}
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1871
	return (0);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1872
}
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1873
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1874
static int
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1875
vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1876
    size_t *rsz, boolean_t is_spare)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1877
{
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1878
	char *type;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1879
	int ret;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1880
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1881
	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1882
		return (EZFS_INVALCONFIG);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1883
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1884
	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1885
		/*
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1886
		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1887
		 * For a spare vdev, we only want to boot from the active
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1888
		 * spare device.
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1889
		 */
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1890
		if (is_spare) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1891
			uint64_t spare = 0;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1892
			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1893
			    &spare);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1894
			if (!spare)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1895
				return (EZFS_INVALCONFIG);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1896
		}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1897
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1898
		if (vdev_online(nv)) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1899
			if ((ret = vdev_get_one_physpath(nv, physpath,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1900
			    phypath_size, rsz)) != 0)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1901
				return (ret);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1902
		}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1903
	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1904
	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1905
	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1906
		nvlist_t **child;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1907
		uint_t count;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1908
		int i, ret;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1909
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1910
		if (nvlist_lookup_nvlist_array(nv,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1911
		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1912
			return (EZFS_INVALCONFIG);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1913
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1914
		for (i = 0; i < count; i++) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1915
			ret = vdev_get_physpaths(child[i], physpath,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1916
			    phypath_size, rsz, is_spare);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1917
			if (ret == EZFS_NOSPC)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1918
				return (ret);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1919
		}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1920
	}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1921
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1922
	return (EZFS_POOL_INVALARG);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1923
}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1924
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1925
/*
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1926
 * Get phys_path for a root pool config.
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1927
 * Return 0 on success; non-zero on failure.
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1928
 */
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1929
static int
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1930
zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1931
{
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1932
	size_t rsz;
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1933
	nvlist_t *vdev_root;
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1934
	nvlist_t **child;
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1935
	uint_t count;
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1936
	char *type;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1937
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1938
	rsz = 0;
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1939
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1940
	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1941
	    &vdev_root) != 0)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1942
		return (EZFS_INVALCONFIG);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1943
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1944
	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1945
	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1946
	    &child, &count) != 0)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1947
		return (EZFS_INVALCONFIG);
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1948
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1949
	/*
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1950
	 * root pool can not have EFI labeled disks and can only have
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1951
	 * a single top-level vdev.
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1952
	 */
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1953
	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1954
	    pool_uses_efi(vdev_root))
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1955
		return (EZFS_POOL_INVALARG);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1956
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1957
	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  1958
	    B_FALSE);
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1959
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1960
	/* No online devices */
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1961
	if (rsz == 0)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1962
		return (EZFS_NODEVICE);
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1963
7656
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1964
	return (0);
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1965
}
2621e50fdf4a PSARC 2008/382 Fast Reboot
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 7326
diff changeset
  1966
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  1967
/*
9160
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1968
 * Get phys_path for a root pool
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1969
 * Return 0 on success; non-zero on failure.
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1970
 */
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1971
int
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1972
zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1973
{
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1974
	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1975
	    phypath_size));
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1976
}
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1977
1517e6edbc6f PSARC/2008/760 Boot configuration Service
Sherry Moore <Sherry.Moore@Sun.COM>
parents: 8525
diff changeset
  1978
/*
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1979
 * If the device has being dynamically expanded then we need to relabel
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1980
 * the disk to use the new unallocated space.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1981
 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1982
static int
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1983
zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1984
{
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1985
	char path[MAXPATHLEN];
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1986
	char errbuf[1024];
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1987
	int fd, error;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1988
	int (*_efi_use_whole_disk)(int);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1989
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1990
	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1991
	    "efi_use_whole_disk")) == NULL)
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1992
		return (-1);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1993
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1994
	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1995
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1996
	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1997
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1998
		    "relabel '%s': unable to open device"), name);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  1999
		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2000
	}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2001
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2002
	/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2003
	 * It's possible that we might encounter an error if the device
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2004
	 * does not have any unallocated space left. If so, we simply
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2005
	 * ignore that error and continue on.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2006
	 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2007
	error = _efi_use_whole_disk(fd);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2008
	(void) close(fd);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2009
	if (error && error != VT_ENOSPC) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2010
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2011
		    "relabel '%s': unable to read disk capacity"), name);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2012
		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2013
	}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2014
	return (0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2015
}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2016
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2017
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2018
 * Bring the specified vdev online.   The 'flags' parameter is a set of the
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2019
 * ZFS_ONLINE_* flags.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2021
int
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2022
zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2023
    vdev_state_t *newstate)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2024
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2025
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2026
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2027
	nvlist_t *tgt;
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2028
	boolean_t avail_spare, l2cache, islog;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2029
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2030
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2031
	if (flags & ZFS_ONLINE_EXPAND) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2032
		(void) snprintf(msg, sizeof (msg),
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2033
		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2034
	} else {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2035
		(void) snprintf(msg, sizeof (msg),
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2036
		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2037
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2038
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2039
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2040
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2041
	    &islog)) == NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2042
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  2044
	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
  2045
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2046
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2047
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2048
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2049
	if (flags & ZFS_ONLINE_EXPAND ||
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2050
	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2051
		char *pathname = NULL;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2052
		uint64_t wholedisk = 0;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2053
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2054
		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2055
		    &wholedisk);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2056
		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2057
		    &pathname) == 0);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2058
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2059
		/*
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2060
		 * XXX - L2ARC 1.0 devices can't support expansion.
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2061
		 */
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2062
		if (l2cache) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2063
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2064
			    "cannot expand cache devices"));
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2065
			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2066
		}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2067
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2068
		if (wholedisk) {
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2069
			pathname += strlen(DISK_ROOT) + 1;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2070
			(void) zpool_relabel_disk(zhp->zpool_hdl, pathname);
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2071
		}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2072
	}
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9790
diff changeset
  2073
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2074
	zc.zc_cookie = VDEV_STATE_ONLINE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2075
	zc.zc_obj = flags;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2076
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2077
	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
  2078
		return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2079
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2080
	*newstate = zc.zc_cookie;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2081
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2082
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2083
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2084
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2085
 * Take the specified vdev offline
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2086
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
int
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2088
zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2092
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2093
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2094
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2096
	(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2097
	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2098
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2100
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2101
	    NULL)) == NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2102
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2103
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  2104
	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
  2105
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2106
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2107
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2108
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2109
	zc.zc_cookie = VDEV_STATE_OFFLINE;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2110
	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1354
diff changeset
  2111
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2112
	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2113
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2115
	switch (errno) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2116
	case EBUSY:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2117
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2118
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2119
		 * There are no other replicas of this device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2120
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2121
		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2122
9701
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2123
	case EEXIST:
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2124
		/*
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2125
		 * The log device has unplayed logs
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2126
		 */
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2127
		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
cc5b64682e64 6803605 should be able to offline log devices
George Wilson <George.Wilson@Sun.COM>
parents: 9160
diff changeset
  2128
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2129
	default:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2130
		return (zpool_standard_error(hdl, errno, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2131
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2132
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2134
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2135
 * Mark the given vdev faulted.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2136
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2137
int
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2138
zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2139
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2140
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2141
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2142
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2143
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2144
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2145
	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2146
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2147
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2148
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2149
	zc.zc_cookie = VDEV_STATE_FAULTED;
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2150
	zc.zc_obj = aux;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2151
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2152
	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
  2153
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2154
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2155
	switch (errno) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2156
	case EBUSY:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2157
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2158
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2159
		 * There are no other replicas of this device.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2160
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2161
		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2162
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2163
	default:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2164
		return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2165
	}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2166
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2167
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2168
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2169
/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2170
 * Mark the given vdev degraded.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2171
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2172
int
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2173
zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2174
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2175
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2176
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2177
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2178
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2179
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2180
	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2181
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2182
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2183
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2184
	zc.zc_cookie = VDEV_STATE_DEGRADED;
10817
7dfde45252f0 6889826 ZFS retire agent should be able to repair pools
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10685
diff changeset
  2185
	zc.zc_obj = aux;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2186
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2187
	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
  2188
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2189
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2190
	return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2191
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2192
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2193
/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2194
 * 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
  2195
 * a hot spare.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2196
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2197
static boolean_t
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2198
is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2199
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2200
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2201
	uint_t c, children;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2202
	char *type;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2203
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2204
	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2205
	    &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2206
		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2207
		    &type) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2208
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2209
		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2210
		    children == 2 && child[which] == tgt)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2211
			return (B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2212
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2213
		for (c = 0; c < children; c++)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2214
			if (is_replacing_spare(child[c], tgt, which))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2215
				return (B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2216
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2217
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2218
	return (B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2219
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2220
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2221
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2222
 * Attach new_disk (fully described by nvroot) to old_disk.
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2223
 * If 'replacing' is specified, the new disk will replace the old one.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2224
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2225
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2226
zpool_vdev_attach(zpool_handle_t *zhp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2227
    const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2228
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2229
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2230
	char msg[1024];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2231
	int ret;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2232
	nvlist_t *tgt;
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2233
	boolean_t avail_spare, l2cache, islog;
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2234
	uint64_t val;
7041
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2235
	char *path, *newname;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2236
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2237
	uint_t children;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2238
	nvlist_t *config_root;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2239
	libzfs_handle_t *hdl = zhp->zpool_hdl;
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2240
	boolean_t rootpool = pool_is_bootable(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2241
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2242
	if (replacing)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2243
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2244
		    "cannot replace %s with %s"), old_disk, new_disk);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2245
	else
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2246
		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2247
		    "cannot attach %s to %s"), new_disk, old_disk);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2248
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2249
	/*
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2250
	 * If this is a root pool, make sure that we're not attaching an
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2251
	 * EFI labeled device.
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2252
	 */
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2253
	if (rootpool && pool_uses_efi(nvroot)) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2254
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2255
		    "EFI labeled devices are not supported on root pools."));
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2256
		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2257
	}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2258
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2259
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2260
	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2261
	    &islog)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2262
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2263
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  2264
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2265
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2266
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2267
	if (l2cache)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2268
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2269
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2270
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2271
	zc.zc_cookie = replacing;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2272
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2273
	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2274
	    &child, &children) != 0 || children != 1) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2275
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2276
		    "new device must be a single disk"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2277
		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2278
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2279
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2280
	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2281
	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2282
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2283
	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
7041
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2284
		return (-1);
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2285
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2286
	/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2287
	 * 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
  2288
	 * replace it with another hot spare.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2289
	 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2290
	if (replacing &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2291
	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2292
	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2293
	    NULL) == NULL || !avail_spare) &&
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2294
	    is_replacing_spare(config_root, tgt, 1)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2295
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2296
		    "can only be replaced by another hot spare"));
7041
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2297
		free(newname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2298
		return (zfs_error(hdl, EZFS_BADTARGET, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2299
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2300
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2301
	/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2302
	 * 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
  2303
	 * already spared device.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2304
	 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2305
	if (replacing &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2306
	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2307
	    zpool_find_vdev(zhp, newname, &avail_spare,
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2308
	    &l2cache, NULL) != NULL && avail_spare &&
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2309
	    is_replacing_spare(config_root, tgt, 0)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2310
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2311
		    "device has already been replaced with a spare"));
7041
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2312
		free(newname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2313
		return (zfs_error(hdl, EZFS_BADTARGET, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2314
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
7041
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2316
	free(newname);
b4c5fe87fad8 6721901 uninitialized variable in zfs_fm_recv() can confuse diagnosis
eschrock
parents: 6643
diff changeset
  2317
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2318
	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2319
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2320
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2321
	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2322
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2323
	zcmd_free_nvlists(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2324
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2325
	if (ret == 0) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2326
		if (rootpool) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2327
			/*
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2328
			 * XXX - This should be removed once we can
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2329
			 * automatically install the bootblocks on the
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2330
			 * newly attached disk.
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2331
			 */
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2332
			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2333
			    "be sure to invoke %s to make '%s' bootable.\n"),
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2334
			    BOOTCMD, new_disk);
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2335
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2336
			/*
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2337
			 * XXX need a better way to prevent user from
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2338
			 * booting up a half-baked vdev.
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2339
			 */
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2340
			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2341
			    "sure to wait until resilver is done "
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 9701
diff changeset
  2342
			    "before rebooting.\n"));
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2343
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2344
		return (0);
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  2345
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2346
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2347
	switch (errno) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2348
	case ENOTSUP:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
		 * Can't attach to or replace this type of vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2351
		 */
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2352
		if (replacing) {
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2353
			if (islog)
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2354
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2355
				    "cannot replace a log with a spare"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2356
			else
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2357
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2358
				    "cannot replace a replacing device"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2359
		} else {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2360
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2361
			    "can only attach to mirrors and top-level "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2362
			    "disks"));
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2363
		}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2364
		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2365
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2366
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2367
	case EINVAL:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2368
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2369
		 * The new device must be a single disk.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2370
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2371
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2372
		    "new device must be a single disk"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2373
		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2375
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2376
	case EBUSY:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2377
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2378
		    new_disk);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2379
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2382
	case EOVERFLOW:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2383
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
		 * The new device is too small.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2385
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2386
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2387
		    "device is too small"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2388
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2391
	case EDOM:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2392
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2393
		 * The new device has a different alignment requirement.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2394
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2395
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2396
		    "devices have different sector alignment"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2397
		(void) zfs_error(hdl, EZFS_BADDEV, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2398
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2400
	case ENAMETOOLONG:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2401
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2402
		 * The resulting top-level vdev spec won't fit in the label.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2403
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2404
		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2405
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2406
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2407
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2408
		(void) zpool_standard_error(hdl, errno, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2409
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2410
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2411
	return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2412
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2413
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2414
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2415
 * Detach the specified device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2416
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2417
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2418
zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2419
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2420
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2421
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2422
	nvlist_t *tgt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2423
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2424
	libzfs_handle_t *hdl = zhp->zpool_hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2425
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2426
	(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2427
	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2428
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2429
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2430
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2431
	    NULL)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2432
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  2434
	if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2435
		return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2436
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2437
	if (l2cache)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2438
		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2439
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2440
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2441
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2442
	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2443
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2444
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2445
	switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2446
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2447
	case ENOTSUP:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2448
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2449
		 * Can't detach from this type of vdev.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2450
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2451
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2452
		    "applicable to mirror and replacing vdevs"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2453
		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2454
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2455
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2456
	case EBUSY:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2457
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2458
		 * There are no other replicas of this device.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2460
		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2461
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2462
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2463
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2464
		(void) zpool_standard_error(hdl, errno, msg);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2465
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2466
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2467
	return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2468
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2469
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2470
/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2471
 * Remove the given device.  Currently, this is supported only for hot spares
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2472
 * and level 2 cache devices.
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2473
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2474
int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2475
zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2476
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2477
	zfs_cmd_t zc = { 0 };
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2478
	char msg[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2479
	nvlist_t *tgt;
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2480
	boolean_t avail_spare, l2cache, islog;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2481
	libzfs_handle_t *hdl = zhp->zpool_hdl;
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2482
	uint64_t version;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2483
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2484
	(void) snprintf(msg, sizeof (msg),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2485
	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2486
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2487
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2488
	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2489
	    &islog)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2490
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2491
	/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2492
	 * XXX - this should just go away.
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2493
	 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2494
	if (!avail_spare && !l2cache && !islog) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2495
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2496
		    "only inactive hot spares, cache, top-level, "
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2497
		    "or log devices can be removed"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2498
		return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2499
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2500
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2501
	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2502
	if (islog && version < SPA_VERSION_HOLES) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2503
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2504
		    "pool must be upgrade to support log removal"));
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2505
		return (zfs_error(hdl, EZFS_BADVERSION, msg));
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2506
	}
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2507
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2508
	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2509
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2510
	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2511
		return (0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2512
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2513
	return (zpool_standard_error(hdl, errno, msg));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2514
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2515
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2516
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2517
 * Clear the errors for the pool, or the particular device if specified.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2518
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2519
int
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2520
zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2521
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2522
	zfs_cmd_t zc = { 0 };
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2523
	char msg[1024];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2524
	nvlist_t *tgt;
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2525
	zpool_rewind_policy_t policy;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2526
	boolean_t avail_spare, l2cache;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2527
	libzfs_handle_t *hdl = zhp->zpool_hdl;
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2528
	nvlist_t *nvi = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2529
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2530
	if (path)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2531
		(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2532
		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2533
		    path);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2534
	else
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2535
		(void) snprintf(msg, sizeof (msg),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2536
		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2537
		    zhp->zpool_name);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2538
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2539
	(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
  2540
	if (path) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2541
		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
7326
a3c803100a09 6713886 hot spares of mirrored log devices should not be allowed
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 7300
diff changeset
  2542
		    &l2cache, NULL)) == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2543
			return (zfs_error(hdl, EZFS_NODEVICE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2544
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2545
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2546
		 * Don't allow error clearing for hot spares.  Do allow
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2547
		 * error clearing for l2cache devices.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5363
diff changeset
  2548
		 */
2468
b5038d012f66 6433208 should not be able to offline/online a spare
ek110237
parents: 2174
diff changeset
  2549
		if (avail_spare)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2550
			return (zfs_error(hdl, EZFS_ISSPARE, msg));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2551
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2552
		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2553
		    &zc.zc_guid) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2554
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2555
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2556
	zpool_get_rewind_policy(rewindnvl, &policy);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2557
	zc.zc_cookie = policy.zrp_request;
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2558
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2559
	if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2560
		return (-1);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2561
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2562
	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0)
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2563
		return (-1);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2564
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2565
	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 ||
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2566
	    ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2567
	    errno != EPERM && errno != EACCES)) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2568
		if (policy.zrp_request &
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2569
		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2570
			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2571
			zpool_rewind_exclaim(hdl, zc.zc_name,
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2572
			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2573
			    nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2574
			nvlist_free(nvi);
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2575
		}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2576
		zcmd_free_nvlists(&zc);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2577
		return (0);
10921
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2578
	}
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2579
8aac17999e4d PSARC 2009/479 zpool recovery support
Tim Haley <Tim.Haley@Sun.COM>
parents: 10817
diff changeset
  2580
	zcmd_free_nvlists(&zc);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2581
	return (zpool_standard_error(hdl, errno, msg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2582
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2583
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2584
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2585
 * Similar to zpool_clear(), but takes a GUID (used by fmd).
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2586
 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2587
int
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2588
zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2589
{
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2590
	zfs_cmd_t zc = { 0 };
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2591
	char msg[1024];
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2592
	libzfs_handle_t *hdl = zhp->zpool_hdl;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2593
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2594
	(void) snprintf(msg, sizeof (msg),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2595
	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2596
	    guid);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2597
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2598
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2599
	zc.zc_guid = guid;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2600
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2601
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2602
		return (0);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2603
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2604
	return (zpool_standard_error(hdl, errno, msg));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2605
}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2606
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2607
/*
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2608
 * Convert from a devid string to a path.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2609
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2610
static char *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2611
devid_to_path(char *devid_str)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2612
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2613
	ddi_devid_t devid;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2614
	char *minor;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2615
	char *path;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2616
	devid_nmlist_t *list = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2617
	int ret;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2618
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2619
	if (devid_str_decode(devid_str, &devid, &minor) != 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2620
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2621
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2622
	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2623
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2624
	devid_str_free(minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2625
	devid_free(devid);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2626
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2627
	if (ret != 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2628
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2629
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2630
	if ((path = strdup(list[0].devname)) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2631
		return (NULL);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2632
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2633
	devid_free_nmlist(list);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2634
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2635
	return (path);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2636
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2637
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2638
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2639
 * Convert from a path to a devid string.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2640
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2641
static char *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2642
path_to_devid(const char *path)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2643
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2644
	int fd;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2645
	ddi_devid_t devid;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2646
	char *minor, *ret;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2647
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2648
	if ((fd = open(path, O_RDONLY)) < 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2649
		return (NULL);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2650
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2651
	minor = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2652
	ret = NULL;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2653
	if (devid_get(fd, &devid) == 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2654
		if (devid_get_minor_name(fd, &minor) == 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2655
			ret = devid_str_encode(devid, minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2656
		if (minor != NULL)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2657
			devid_str_free(minor);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2658
		devid_free(devid);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2659
	}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2660
	(void) close(fd);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2661
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2662
	return (ret);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2663
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2664
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2665
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2666
 * 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
  2667
 * 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
  2668
 * type 'zpool status', and we'll display the correct information anyway.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2669
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2670
static void
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2671
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
  2672
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2673
	zfs_cmd_t zc = { 0 };
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2674
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2675
	(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
  2676
	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2677
	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2678
	    &zc.zc_guid) == 0);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2679
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2680
	(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
  2681
}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2682
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2683
/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2684
 * 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
  2685
 * 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
  2686
 * 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
  2687
 * trailing 's0' slice name.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2688
 *
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2689
 * This routine is also responsible for identifying when disks have been
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2690
 * 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
  2691
 * 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
  2692
 * 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
  2693
 * 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
  2694
 * 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
  2695
 * 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
  2696
 * of these checks.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2697
 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2698
char *
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2699
zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2700
    boolean_t verbose)
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2701
{
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2702
	char *path, *devid;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2703
	uint64_t value;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2704
	char buf[64];
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2705
	vdev_stat_t *vs;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2706
	uint_t vsc;
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2707
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2708
	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2709
	    &value) == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2710
		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2711
		    &value) == 0);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2712
		(void) snprintf(buf, sizeof (buf), "%llu",
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2713
		    (u_longlong_t)value);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2714
		path = buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2715
	} 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
  2716
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2717
		/*
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2718
		 * If the device is dead (faulted, offline, etc) then don't
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2719
		 * bother opening it.  Otherwise we may be forcing the user to
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2720
		 * open a misbehaving device, which can have undesirable
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2721
		 * effects.
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2722
		 */
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2723
		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2724
		    (uint64_t **)&vs, &vsc) != 0 ||
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2725
		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4295
diff changeset
  2726
		    zhp != NULL &&
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2727
		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2728
			/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2729
			 * Determine if the current path is correct.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2730
			 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2731
			char *newdevid = path_to_devid(path);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2732
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2733
			if (newdevid == NULL ||
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2734
			    strcmp(devid, newdevid) != 0) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2735
				char *newpath;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2736
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2737
				if ((newpath = devid_to_path(devid)) != NULL) {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2738
					/*
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2739
					 * Update the path appropriately.
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2740
					 */
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2741
					set_path(zhp, nv, newpath);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2742
					if (nvlist_add_string(nv,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2743
					    ZPOOL_CONFIG_PATH, newpath) == 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2744
						verify(nvlist_lookup_string(nv,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2745
						    ZPOOL_CONFIG_PATH,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2746
						    &path) == 0);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2747
					free(newpath);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2748
				}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2749
			}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2750
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2751
			if (newdevid)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2752
				devid_str_free(newdevid);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2753
		}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2754
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2755
		if (strncmp(path, "/dev/dsk/", 9) == 0)
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2756
			path += 9;
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2757
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2758
		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2759
		    &value) == 0 && value) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2760
			char *tmp = zfs_strdup(hdl, path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2761
			if (tmp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2762
				return (NULL);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2763
			tmp[strlen(path) - 2] = '\0';
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2764
			return (tmp);
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2765
		}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2766
	} else {
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2767
		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2768
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2769
		/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2770
		 * 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
  2771
		 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2772
		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2773
			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2774
			    &value) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2775
			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2776
			    (u_longlong_t)value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2777
			path = buf;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2778
		}
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2779
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2780
		/*
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2781
		 * We identify each top-level vdev by using a <type-id>
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2782
		 * naming convention.
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2783
		 */
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2784
		if (verbose) {
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2785
			uint64_t id;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2786
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2787
			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2788
			    &id) == 0);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2789
			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2790
			    (u_longlong_t)id);
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2791
			path = buf;
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  2792
		}
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2793
	}
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2794
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2795
	return (zfs_strdup(hdl, path));
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1003
diff changeset
  2796
}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2797
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2798
static int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2799
zbookmark_compare(const void *a, const void *b)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2800
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2801
	return (memcmp(a, b, sizeof (zbookmark_t)));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2802
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2803
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2804
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2805
 * Retrieve the persistent error log, uniquify the members, and return to the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2806
 * caller.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2807
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2808
int
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2809
zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2810
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2811
	zfs_cmd_t zc = { 0 };
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2812
	uint64_t count;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2813
	zbookmark_t *zb = NULL;
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2814
	int i;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2815
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2816
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2817
	 * Retrieve the raw error list from the kernel.  If the number of errors
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2818
	 * has increased, allocate more space and continue until we get the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2819
	 * entire list.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2820
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2821
	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2822
	    &count) == 0);
4820
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4715
diff changeset
  2823
	if (count == 0)
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4715
diff changeset
  2824
		return (0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2825
	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
  2826
	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2827
		return (-1);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2828
	zc.zc_nvlist_dst_size = count;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2829
	(void) strcpy(zc.zc_name, zhp->zpool_name);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2830
	for (;;) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2831
		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2832
		    &zc) != 0) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2833
			free((void *)(uintptr_t)zc.zc_nvlist_dst);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2834
			if (errno == ENOMEM) {
3823
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2835
				count = zc.zc_nvlist_dst_size;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2836
				if ((zc.zc_nvlist_dst = (uintptr_t)
3823
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2837
				    zfs_alloc(zhp->zpool_hdl, count *
2cbdd0a48584 6494119 zpool status command exits with abort
vb160487
parents: 3741
diff changeset
  2838
				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2839
					return (-1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2840
			} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2841
				return (-1);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2842
			}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2843
		} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2844
			break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2845
		}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2846
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2847
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2848
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2849
	 * Sort the resulting bookmarks.  This is a little confusing due to the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2850
	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2851
	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2852
	 * _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
  2853
	 * array appropriate and decrement the total number of elements.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2854
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2855
	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2856
	    zc.zc_nvlist_dst_size;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2857
	count -= zc.zc_nvlist_dst_size;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2858
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2859
	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2860
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2861
	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2862
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2863
	/*
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2864
	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2865
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2866
	for (i = 0; i < count; i++) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2867
		nvlist_t *nv;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2868
3700
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3444
diff changeset
  2869
		/* 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
  2870
		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
  2871
		    zb[i-1].zb_object == zb[i].zb_object)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2872
			continue;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2873
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2874
		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
  2875
			goto nomem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2876
		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2877
		    zb[i].zb_objset) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2878
			nvlist_free(nv);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2879
			goto nomem;
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2880
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2881
		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2882
		    zb[i].zb_object) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2883
			nvlist_free(nv);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2884
			goto nomem;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2885
		}
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2886
		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2887
			nvlist_free(nv);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2888
			goto nomem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2889
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  2890
		nvlist_free(nv);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2891
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2892
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2893
	free((void *)(uintptr_t)zc.zc_nvlist_dst);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2894
	return (0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2895
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2896
nomem:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2468
diff changeset
  2897
	free((void *)(uintptr_t)zc.zc_nvlist_dst);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2898
	return (no_memory(zhp->zpool_hdl));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2899
}
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2900
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2901
/*
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2902
 * Upgrade a ZFS pool to the latest on-disk version.
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2903
 */
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2904
int
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2905
zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2906
{
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2907
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2908
	libzfs_handle_t *hdl = zhp->zpool_hdl;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2909
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2910
	(void) strcpy(zc.zc_name, zhp->zpool_name);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2911
	zc.zc_cookie = new_version;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2912
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2913
	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
  2914
		return (zpool_standard_error_fmt(hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2915
		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1773
diff changeset
  2916
		    zhp->zpool_name));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2917
	return (0);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1635
diff changeset
  2918
}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2919
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2920
void
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2921
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
  2922
    char *history_str)
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2923
{
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2924
	int i;
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2925
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2926
	(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
  2927
	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
  2928
		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
  2929
		    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
  2930
			break;
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2931
		(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
  2932
		(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
  2933
	}
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2934
}
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2935
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2936
/*
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2937
 * Stage command history for logging.
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2938
 */
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2939
int
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2940
zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2941
{
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2942
	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
  2943
		return (EINVAL);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2944
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2945
	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
  2946
		return (EINVAL);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2947
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
  2948
	if (hdl->libzfs_log_str != NULL)
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2949
		free(hdl->libzfs_log_str);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2950
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2951
	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
  2952
		return (no_memory(hdl));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  2953
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4820
diff changeset
  2954
	return (0);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2955
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2956
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2957
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2958
 * Perform ioctl to get some command history of a pool.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2959
 *
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2960
 * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2961
 * logical offset of the history buffer to start reading from.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2962
 *
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2963
 * Upon return, 'off' is the next logical offset to read from and
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2964
 * 'len' is the actual amount of bytes read into 'buf'.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2965
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2966
static int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2967
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
  2968
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2969
	zfs_cmd_t zc = { 0 };
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2970
	libzfs_handle_t *hdl = zhp->zpool_hdl;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2971
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2972
	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2973
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2974
	zc.zc_history = (uint64_t)(uintptr_t)buf;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2975
	zc.zc_history_len = *len;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2976
	zc.zc_history_offset = *off;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2977
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2978
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2979
		switch (errno) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2980
		case EPERM:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2981
			return (zfs_error_fmt(hdl, EZFS_PERM,
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2982
			    dgettext(TEXT_DOMAIN,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2983
			    "cannot show history for pool '%s'"),
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2984
			    zhp->zpool_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2985
		case ENOENT:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2986
			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2987
			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2988
			    "'%s'"), zhp->zpool_name));
3863
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2989
		case ENOTSUP:
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2990
			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2991
			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3823
diff changeset
  2992
			    "'%s', pool must be upgraded"), zhp->zpool_name));
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2993
		default:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3126
diff changeset
  2994
			return (zpool_standard_error_fmt(hdl, errno,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2995
			    dgettext(TEXT_DOMAIN,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2996
			    "cannot get history for '%s'"), zhp->zpool_name));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2997
		}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2998
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2999
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3000
	*len = zc.zc_history_len;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3001
	*off = zc.zc_history_offset;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3002
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3003
	return (0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3004
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3005
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3006
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3007
 * Process the buffer of nvlists, unpacking and storing each nvlist record
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3008
 * into 'records'.  'leftover' is set to the number of bytes that weren't
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3009
 * processed as there wasn't a complete record.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3010
 */
10685
931790026ac6 6846163 ZFS continues to use faulted logzilla, bringing system to a crawl
George Wilson <George.Wilson@Sun.COM>
parents: 10594
diff changeset
  3011
int
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3012
zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3013
    nvlist_t ***records, uint_t *numrecords)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3014
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3015
	uint64_t reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3016
	nvlist_t *nv;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3017
	int i;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3018
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3019
	while (bytes_read > sizeof (reclen)) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3020
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3021
		/* get length of packed record (stored as little endian) */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3022
		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3023
			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3024
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3025
		if (bytes_read < sizeof (reclen) + reclen)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3026
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3027
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3028
		/* unpack record */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3029
		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3030
			return (ENOMEM);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3031
		bytes_read -= sizeof (reclen) + reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3032
		buf += sizeof (reclen) + reclen;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3033
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3034
		/* add record to nvlist array */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3035
		(*numrecords)++;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3036
		if (ISP2(*numrecords + 1)) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3037
			*records = realloc(*records,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3038
			    *numrecords * 2 * sizeof (nvlist_t *));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3039
		}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3040
		(*records)[*numrecords - 1] = nv;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3041
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3042
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3043
	*leftover = bytes_read;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3044
	return (0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3045
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3046
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3047
#define	HIS_BUF_LEN	(128*1024)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3048
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3049
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3050
 * Retrieve the command history of a pool.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3051
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3052
int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3053
zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3054
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3055
	char buf[HIS_BUF_LEN];
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3056
	uint64_t off = 0;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3057
	nvlist_t **records = NULL;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3058
	uint_t numrecords = 0;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3059
	int err, i;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3060
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3061
	do {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3062
		uint64_t bytes_read = sizeof (buf);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3063
		uint64_t leftover;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3064
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3065
		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3066
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3067
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3068
		/* if nothing else was read in, we're at EOF, just return */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3069
		if (!bytes_read)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3070
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3071
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3072
		if ((err = zpool_history_unpack(buf, bytes_read,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3073
		    &leftover, &records, &numrecords)) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3074
			break;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3075
		off -= leftover;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3076
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3077
		/* CONSTCOND */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3078
	} while (1);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3079
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3080
	if (!err) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3081
		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3082
		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3083
		    records, numrecords) == 0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3084
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3085
	for (i = 0; i < numrecords; i++)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3086
		nvlist_free(records[i]);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3087
	free(records);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3088
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3089
	return (err);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3090
}
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3091
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3092
void
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3093
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
  3094
    char *pathname, size_t len)
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3095
{
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3096
	zfs_cmd_t zc = { 0 };
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3097
	boolean_t mounted = B_FALSE;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3098
	char *mntpnt = NULL;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3099
	char dsname[MAXNAMELEN];
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3100
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3101
	if (dsobj == 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3102
		/* special case for the MOS */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3103
		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3104
		return;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3105
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3106
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3107
	/* get the dataset's name */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3108
	(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
  3109
	zc.zc_obj = dsobj;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3110
	if (ioctl(zhp->zpool_hdl->libzfs_fd,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3111
	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3112
		/* 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
  3113
		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3114
		    dsobj, obj);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3115
		return;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3116
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3117
	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3118
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3119
	/* find out if the dataset is mounted */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3120
	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3121
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3122
	/* get the corrupted object's path */
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3123
	(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
  3124
	zc.zc_obj = obj;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3125
	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
  3126
	    &zc) == 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3127
		if (mounted) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3128
			(void) snprintf(pathname, len, "%s%s", mntpnt,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3129
			    zc.zc_value);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3130
		} else {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3131
			(void) snprintf(pathname, len, "%s:%s",
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3132
			    dsname, zc.zc_value);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3133
		}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3134
	} else {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3135
		(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
  3136
	}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3137
	free(mntpnt);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3377
diff changeset
  3138
}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3863
diff changeset
  3139
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
  3140
/*
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3141
 * Read the EFI label from the config, if a label does not exist then
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3142
 * pass back the error to the caller. If the caller has passed a non-NULL
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3143
 * diskaddr argument then we set it to the starting address of the EFI
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3144
 * partition.
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3145
 */
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3146
static int
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3147
read_efi_label(nvlist_t *config, diskaddr_t *sb)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3148
{
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3149
	char *path;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3150
	int fd;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3151
	char diskname[MAXPATHLEN];
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3152
	int err = -1;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3153
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3154
	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3155
		return (err);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3156
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3157
	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3158
	    strrchr(path, '/'));
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3159
	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3160
		struct dk_gpt *vtoc;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3161
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3162
		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3163
			if (sb != NULL)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3164
				*sb = vtoc->efi_parts[0].p_start;
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3165
			efi_free(vtoc);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3166
		}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3167
		(void) close(fd);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3168
	}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3169
	return (err);
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3170
}
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3171
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3172
/*
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
  3173
 * 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
  3174
 * 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
  3175
 */
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
  3176
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
  3177
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
  3178
{
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
  3179
	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
  3180
	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
  3181
	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
  3182
	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
  3183
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
  3184
	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
  3185
	    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
  3186
		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
  3187
		    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
  3188
		    &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
  3189
			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
  3190
		}
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3191
		if (read_efi_label(config, &sb) < 0)
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 7041
diff changeset
  3192
			sb = MAXOFFSET_T;
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
  3193
		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
  3194
	}
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
  3195
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
  3196
	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
  3197
		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
  3198
		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
  3199
			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
  3200
		}
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
  3201
	}
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
  3202
	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
  3203
}
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
  3204
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
  3205
/*
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
  3206
 * 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
  3207
 * 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
  3208
 */
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
  3209
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
  3210
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
  3211
{
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
  3212
	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
  3213
	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
  3214
	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
  3215
	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
  3216
	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
  3217
	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
  3218
	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
  3219
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  3220
	/* 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
  3221
	(void) snprintf(errbuf, sizeof (errbuf),
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  3222
	    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
  3223
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
  3224
	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
  3225
		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
  3226
7965
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3227
		if (pool_is_bootable(zhp)) {
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3228
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3229
			    "EFI labeled devices are not supported on root "
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3230
			    "pools."));
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3231
			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3232
		}
b795da521357 6740164 zpool attach can create an illegal root pool
George Wilson <George.Wilson@Sun.COM>
parents: 7656
diff changeset
  3233
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
  3234
		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
  3235
		    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
  3236
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
  3237
		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
  3238
			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
  3239
		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
  3240
			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
  3241
		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
  3242
	} 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
  3243
		/* 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
  3244
		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
  3245
	}
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
  3246
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
  3247
	(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
  3248
	    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
  3249
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
  3250
	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
  3251
		/*
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
  3252
		 * 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
  3253
		 * 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
  3254
		 */
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  3255
		zfs_error_aux(hdl,
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  3256
		    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
  3257
		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
  3258
	}
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
  3259
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
  3260
	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
  3261
		/*
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
  3262
		 * 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
  3263
		 * 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
  3264
		 */
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
  3265
		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
  3266
			(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
  3267
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
  3268
		(void) close(fd);
6289
9a83171c70d6 6603209 Multiple -o options to 'zfs mount' ignore all but the last.
mmusante
parents: 5621
diff changeset
  3269
		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
  3270
		    "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
  3271
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
  3272
		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
  3273
	}
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
  3274
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
  3275
	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
  3276
	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
  3277
	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
  3278
		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
  3279
	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
  3280
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
  3281
	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
  3282
	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
  3283
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
  3284
	/*
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
  3285
	 * 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
  3286
	 * 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
  3287
	 * 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
  3288
	 * 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
  3289
	 * 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
  3290
	 * 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
  3291
	 */
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
  3292
	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
  3293
	(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
  3294
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
  3295
	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
  3296
	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
  3297
	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
  3298
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
  3299
	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
  3300
		/*
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
  3301
		 * 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
  3302
		 * 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
  3303
		 * 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
  3304
		 * 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
  3305
		 */
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
  3306
		(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
  3307
		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
  3308
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
  3309
		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
  3310
		    "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
  3311
		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
  3312
	}
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
  3313
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
  3314
	(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
  3315
	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
  3316
	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
  3317
}
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3318
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3319
static boolean_t
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3320
supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3321
{
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3322
	char *type;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3323
	nvlist_t **child;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3324
	uint_t children, c;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3325
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3326
	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3327
	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3328
	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3329
	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
10594
986cb68d2347 6574286 removing a slog doesn't work
George Wilson <George.Wilson@Sun.COM>
parents: 10588
diff changeset
  3330
	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3331
	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3332
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3333
		    "vdev type '%s' is not supported"), type);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3334
		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3335
		return (B_FALSE);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3336
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3337
	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3338
	    &child, &children) == 0) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3339
		for (c = 0; c < children; c++) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3340
			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3341
				return (B_FALSE);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3342
		}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3343
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3344
	return (B_TRUE);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3345
}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3346
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3347
/*
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3348
 * check if this zvol is allowable for use as a dump device; zero if
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3349
 * it is, > 0 if it isn't, < 0 if it isn't a zvol
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3350
 */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3351
int
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3352
zvol_check_dump_config(char *arg)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3353
{
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3354
	zpool_handle_t *zhp = NULL;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3355
	nvlist_t *config, *nvroot;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3356
	char *p, *volname;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3357
	nvlist_t **top;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3358
	uint_t toplevels;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3359
	libzfs_handle_t *hdl;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3360
	char errbuf[1024];
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3361
	char poolname[ZPOOL_MAXNAMELEN];
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3362
	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3363
	int ret = 1;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3364
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3365
	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3366
		return (-1);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3367
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3368
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3369
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3370
	    "dump is not supported on device '%s'"), arg);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3371
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3372
	if ((hdl = libzfs_init()) == NULL)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3373
		return (1);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3374
	libzfs_print_on_error(hdl, B_TRUE);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3375
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3376
	volname = arg + pathlen;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3377
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3378
	/* check the configuration of the pool */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3379
	if ((p = strchr(volname, '/')) == NULL) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3380
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3381
		    "malformed dataset name"));
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3382
		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3383
		return (1);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3384
	} else if (p - volname >= ZFS_MAXNAMELEN) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3385
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3386
		    "dataset name is too long"));
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3387
		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3388
		return (1);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3389
	} else {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3390
		(void) strncpy(poolname, volname, p - volname);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3391
		poolname[p - volname] = '\0';
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3392
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3393
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3394
	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3395
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3396
		    "could not open pool '%s'"), poolname);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3397
		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3398
		goto out;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3399
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3400
	config = zpool_get_config(zhp, NULL);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3401
	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3402
	    &nvroot) != 0) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3403
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3404
		    "could not obtain vdev configuration for  '%s'"), poolname);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3405
		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3406
		goto out;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3407
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3408
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3409
	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3410
	    &top, &toplevels) == 0);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3411
	if (toplevels != 1) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3412
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3413
		    "'%s' has multiple top level vdevs"), poolname);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3414
		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3415
		goto out;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3416
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3417
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3418
	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3419
		goto out;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3420
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3421
	ret = 0;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3422
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3423
out:
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3424
	if (zhp)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3425
		zpool_close(zhp);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3426
	libzfs_fini(hdl);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3427
	return (ret);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 6289
diff changeset
  3428
}