xref: /illumos-gate/usr/src/cmd/zpool/zpool_vdev.c (revision 8654d0253136055bd4cc2423d87378e8a37f2eb5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2346657f8dSmmusante  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Functions to convert between a list of vdevs and an nvlist representing the
31fa9e4066Sahrens  * configuration.  Each entry in the list can be one of:
32fa9e4066Sahrens  *
33fa9e4066Sahrens  * 	Device vdevs
34fa9e4066Sahrens  * 		disk=(path=..., devid=...)
35fa9e4066Sahrens  * 		file=(path=...)
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * 	Group vdevs
3899653d4eSeschrock  * 		raidz[1|2]=(...)
39fa9e4066Sahrens  * 		mirror=(...)
40fa9e4066Sahrens  *
4199653d4eSeschrock  * 	Hot spares
4299653d4eSeschrock  *
43fa9e4066Sahrens  * While the underlying implementation supports it, group vdevs cannot contain
44fa9e4066Sahrens  * other group vdevs.  All userland verification of devices is contained within
45fa9e4066Sahrens  * this file.  If successful, the nvlist returned can be passed directly to the
46fa9e4066Sahrens  * kernel; we've done as much verification as possible in userland.
47fa9e4066Sahrens  *
4899653d4eSeschrock  * Hot spares are a special case, and passed down as an array of disk vdevs, at
4999653d4eSeschrock  * the same level as the root of the vdev tree.
5099653d4eSeschrock  *
518488aeb5Staylor  * The only function exported by this file is 'make_root_vdev'.  The
528488aeb5Staylor  * function performs several passes:
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * 	1. Construct the vdev specification.  Performs syntax validation and
55fa9e4066Sahrens  *         makes sure each device is valid.
56fa9e4066Sahrens  * 	2. Check for devices in use.  Using libdiskmgt, makes sure that no
57fa9e4066Sahrens  *         devices are also in use.  Some can be overridden using the 'force'
58fa9e4066Sahrens  *         flag, others cannot.
59fa9e4066Sahrens  * 	3. Check for replication errors if the 'force' flag is not specified.
60fa9e4066Sahrens  *         validates that the replication level is consistent across the
61fa9e4066Sahrens  *         entire pool.
628488aeb5Staylor  * 	4. Call libzfs to label any whole disks with an EFI label.
63fa9e4066Sahrens  */
64fa9e4066Sahrens 
65fa9e4066Sahrens #include <assert.h>
66fa9e4066Sahrens #include <devid.h>
67fa9e4066Sahrens #include <errno.h>
68fa9e4066Sahrens #include <fcntl.h>
69fa9e4066Sahrens #include <libdiskmgt.h>
70fa9e4066Sahrens #include <libintl.h>
71fa9e4066Sahrens #include <libnvpair.h>
72fa9e4066Sahrens #include <stdio.h>
73fa9e4066Sahrens #include <string.h>
74fa9e4066Sahrens #include <unistd.h>
75fa9e4066Sahrens #include <sys/efi_partition.h>
76fa9e4066Sahrens #include <sys/stat.h>
77fa9e4066Sahrens #include <sys/vtoc.h>
78fa9e4066Sahrens #include <sys/mntent.h>
79fa9e4066Sahrens 
80fa9e4066Sahrens #include "zpool_util.h"
81fa9e4066Sahrens 
82fa9e4066Sahrens #define	DISK_ROOT	"/dev/dsk"
83fa9e4066Sahrens #define	RDISK_ROOT	"/dev/rdsk"
84fa9e4066Sahrens #define	BACKUP_SLICE	"s2"
85fa9e4066Sahrens 
86fa9e4066Sahrens /*
87fa9e4066Sahrens  * For any given vdev specification, we can have multiple errors.  The
88fa9e4066Sahrens  * vdev_error() function keeps track of whether we have seen an error yet, and
89fa9e4066Sahrens  * prints out a header if its the first error we've seen.
90fa9e4066Sahrens  */
9199653d4eSeschrock boolean_t error_seen;
9299653d4eSeschrock boolean_t is_force;
93fa9e4066Sahrens 
9499653d4eSeschrock /*PRINTFLIKE1*/
9599653d4eSeschrock static void
96fa9e4066Sahrens vdev_error(const char *fmt, ...)
97fa9e4066Sahrens {
98fa9e4066Sahrens 	va_list ap;
99fa9e4066Sahrens 
100fa9e4066Sahrens 	if (!error_seen) {
101fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid vdev specification\n"));
102fa9e4066Sahrens 		if (!is_force)
103fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-f' to override "
104fa9e4066Sahrens 			    "the following errors:\n"));
105fa9e4066Sahrens 		else
106fa9e4066Sahrens 			(void) fprintf(stderr, gettext("the following errors "
107fa9e4066Sahrens 			    "must be manually repaired:\n"));
10899653d4eSeschrock 		error_seen = B_TRUE;
109fa9e4066Sahrens 	}
110fa9e4066Sahrens 
111fa9e4066Sahrens 	va_start(ap, fmt);
112fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
113fa9e4066Sahrens 	va_end(ap);
114fa9e4066Sahrens }
115fa9e4066Sahrens 
11646a2abf2Seschrock static void
11746a2abf2Seschrock libdiskmgt_error(int error)
118fa9e4066Sahrens {
119ea8dc4b6Seschrock 	/*
12099653d4eSeschrock 	 * ENXIO/ENODEV is a valid error message if the device doesn't live in
121ea8dc4b6Seschrock 	 * /dev/dsk.  Don't bother printing an error message in this case.
122ea8dc4b6Seschrock 	 */
12399653d4eSeschrock 	if (error == ENXIO || error == ENODEV)
124ea8dc4b6Seschrock 		return;
125ea8dc4b6Seschrock 
12646a2abf2Seschrock 	(void) fprintf(stderr, gettext("warning: device in use checking "
12746a2abf2Seschrock 	    "failed: %s\n"), strerror(error));
128fa9e4066Sahrens }
129fa9e4066Sahrens 
130fa9e4066Sahrens /*
13146a2abf2Seschrock  * Validate a device, passing the bulk of the work off to libdiskmgt.
132fa9e4066Sahrens  */
1338488aeb5Staylor static int
13499653d4eSeschrock check_slice(const char *path, int force, boolean_t wholedisk, boolean_t isspare)
135fa9e4066Sahrens {
13646a2abf2Seschrock 	char *msg;
13746a2abf2Seschrock 	int error = 0;
138fa9e4066Sahrens 
13946657f8dSmmusante 	if (dm_inuse((char *)path, &msg, isspare ? DM_WHO_ZPOOL_SPARE :
14046657f8dSmmusante 	    (force ? DM_WHO_ZPOOL_FORCE : DM_WHO_ZPOOL), &error) || error) {
14146a2abf2Seschrock 		if (error != 0) {
14246a2abf2Seschrock 			libdiskmgt_error(error);
14346a2abf2Seschrock 			return (0);
14446657f8dSmmusante 		} else {
14546a2abf2Seschrock 			vdev_error("%s", msg);
14646a2abf2Seschrock 			free(msg);
147181c2f42Smmusante 			return (-1);
148fa9e4066Sahrens 		}
149fa9e4066Sahrens 	}
150fa9e4066Sahrens 
151fa9e4066Sahrens 	/*
15246a2abf2Seschrock 	 * If we're given a whole disk, ignore overlapping slices since we're
15346a2abf2Seschrock 	 * about to label it anyway.
154fa9e4066Sahrens 	 */
15546a2abf2Seschrock 	error = 0;
15646a2abf2Seschrock 	if (!wholedisk && !force &&
15746a2abf2Seschrock 	    (dm_isoverlapping((char *)path, &msg, &error) || error)) {
158181c2f42Smmusante 		if (error == 0) {
159181c2f42Smmusante 			/* dm_isoverlapping returned -1 */
160181c2f42Smmusante 			vdev_error(gettext("%s overlaps with %s\n"), path, msg);
161181c2f42Smmusante 			free(msg);
162181c2f42Smmusante 			return (-1);
163181c2f42Smmusante 		} else if (error != ENODEV) {
164181c2f42Smmusante 			/* libdiskmgt's devcache only handles physical drives */
16546a2abf2Seschrock 			libdiskmgt_error(error);
16646a2abf2Seschrock 			return (0);
167fa9e4066Sahrens 		}
16846a2abf2Seschrock 	}
169fa9e4066Sahrens 
170181c2f42Smmusante 	return (0);
171fa9e4066Sahrens }
172fa9e4066Sahrens 
1738488aeb5Staylor 
174fa9e4066Sahrens /*
175fa9e4066Sahrens  * Validate a whole disk.  Iterate over all slices on the disk and make sure
176fa9e4066Sahrens  * that none is in use by calling check_slice().
177fa9e4066Sahrens  */
1788488aeb5Staylor static int
17999653d4eSeschrock check_disk(const char *name, dm_descriptor_t disk, int force, int isspare)
180fa9e4066Sahrens {
181fa9e4066Sahrens 	dm_descriptor_t *drive, *media, *slice;
182fa9e4066Sahrens 	int err = 0;
183fa9e4066Sahrens 	int i;
184fa9e4066Sahrens 	int ret;
185fa9e4066Sahrens 
186fa9e4066Sahrens 	/*
187fa9e4066Sahrens 	 * Get the drive associated with this disk.  This should never fail,
188fa9e4066Sahrens 	 * because we already have an alias handle open for the device.
189fa9e4066Sahrens 	 */
190fa9e4066Sahrens 	if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
19146a2abf2Seschrock 	    &err)) == NULL || *drive == NULL) {
19246a2abf2Seschrock 		if (err)
19346a2abf2Seschrock 			libdiskmgt_error(err);
19446a2abf2Seschrock 		return (0);
19546a2abf2Seschrock 	}
196fa9e4066Sahrens 
197fa9e4066Sahrens 	if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
19846a2abf2Seschrock 	    &err)) == NULL) {
19946a2abf2Seschrock 		dm_free_descriptors(drive);
20046a2abf2Seschrock 		if (err)
20146a2abf2Seschrock 			libdiskmgt_error(err);
20246a2abf2Seschrock 		return (0);
20346a2abf2Seschrock 	}
204fa9e4066Sahrens 
205fa9e4066Sahrens 	dm_free_descriptors(drive);
206fa9e4066Sahrens 
207fa9e4066Sahrens 	/*
208fa9e4066Sahrens 	 * It is possible that the user has specified a removable media drive,
209fa9e4066Sahrens 	 * and the media is not present.
210fa9e4066Sahrens 	 */
211fa9e4066Sahrens 	if (*media == NULL) {
212fa9e4066Sahrens 		dm_free_descriptors(media);
21346a2abf2Seschrock 		vdev_error(gettext("'%s' has no media in drive\n"), name);
214fa9e4066Sahrens 		return (-1);
215fa9e4066Sahrens 	}
216fa9e4066Sahrens 
217fa9e4066Sahrens 	if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
21846a2abf2Seschrock 	    &err)) == NULL) {
21946a2abf2Seschrock 		dm_free_descriptors(media);
22046a2abf2Seschrock 		if (err)
22146a2abf2Seschrock 			libdiskmgt_error(err);
22246a2abf2Seschrock 		return (0);
22346a2abf2Seschrock 	}
224fa9e4066Sahrens 
225fa9e4066Sahrens 	dm_free_descriptors(media);
226fa9e4066Sahrens 
227fa9e4066Sahrens 	ret = 0;
228fa9e4066Sahrens 
229fa9e4066Sahrens 	/*
230fa9e4066Sahrens 	 * Iterate over all slices and report any errors.  We don't care about
231fa9e4066Sahrens 	 * overlapping slices because we are using the whole disk.
232fa9e4066Sahrens 	 */
233fa9e4066Sahrens 	for (i = 0; slice[i] != NULL; i++) {
23499653d4eSeschrock 		char *name = dm_get_name(slice[i], &err);
23599653d4eSeschrock 
23699653d4eSeschrock 		if (check_slice(name, force, B_TRUE, isspare) != 0)
237fa9e4066Sahrens 			ret = -1;
23899653d4eSeschrock 
23999653d4eSeschrock 		dm_free_name(name);
240fa9e4066Sahrens 	}
241fa9e4066Sahrens 
242fa9e4066Sahrens 	dm_free_descriptors(slice);
243fa9e4066Sahrens 	return (ret);
244fa9e4066Sahrens }
245fa9e4066Sahrens 
246fa9e4066Sahrens /*
24746a2abf2Seschrock  * Validate a device.
248fa9e4066Sahrens  */
2498488aeb5Staylor static int
25099653d4eSeschrock check_device(const char *path, boolean_t force, boolean_t isspare)
251fa9e4066Sahrens {
252fa9e4066Sahrens 	dm_descriptor_t desc;
253fa9e4066Sahrens 	int err;
25446a2abf2Seschrock 	char *dev;
255fa9e4066Sahrens 
256fa9e4066Sahrens 	/*
257fa9e4066Sahrens 	 * For whole disks, libdiskmgt does not include the leading dev path.
258fa9e4066Sahrens 	 */
259fa9e4066Sahrens 	dev = strrchr(path, '/');
260fa9e4066Sahrens 	assert(dev != NULL);
261fa9e4066Sahrens 	dev++;
26246a2abf2Seschrock 	if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) {
26399653d4eSeschrock 		err = check_disk(path, desc, force, isspare);
26446a2abf2Seschrock 		dm_free_descriptor(desc);
26546a2abf2Seschrock 		return (err);
266fa9e4066Sahrens 	}
267fa9e4066Sahrens 
26899653d4eSeschrock 	return (check_slice(path, force, B_FALSE, isspare));
269fa9e4066Sahrens }
270fa9e4066Sahrens 
271fa9e4066Sahrens /*
272fa9e4066Sahrens  * Check that a file is valid.  All we can do in this case is check that it's
273181c2f42Smmusante  * not in use by another pool, and not in use by swap.
274fa9e4066Sahrens  */
2758488aeb5Staylor static int
27699653d4eSeschrock check_file(const char *file, boolean_t force, boolean_t isspare)
277fa9e4066Sahrens {
27846a2abf2Seschrock 	char  *name;
279fa9e4066Sahrens 	int fd;
280fa9e4066Sahrens 	int ret = 0;
281181c2f42Smmusante 	int err;
28246a2abf2Seschrock 	pool_state_t state;
28399653d4eSeschrock 	boolean_t inuse;
284fa9e4066Sahrens 
285181c2f42Smmusante 	if (dm_inuse_swap(file, &err)) {
286181c2f42Smmusante 		if (err)
287181c2f42Smmusante 			libdiskmgt_error(err);
288181c2f42Smmusante 		else
289181c2f42Smmusante 			vdev_error(gettext("%s is currently used by swap. "
290181c2f42Smmusante 			    "Please see swap(1M).\n"), file);
291181c2f42Smmusante 		return (-1);
292181c2f42Smmusante 	}
293181c2f42Smmusante 
294fa9e4066Sahrens 	if ((fd = open(file, O_RDONLY)) < 0)
295fa9e4066Sahrens 		return (0);
296fa9e4066Sahrens 
29799653d4eSeschrock 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
29846a2abf2Seschrock 		const char *desc;
29946a2abf2Seschrock 
30046a2abf2Seschrock 		switch (state) {
30146a2abf2Seschrock 		case POOL_STATE_ACTIVE:
30246a2abf2Seschrock 			desc = gettext("active");
30346a2abf2Seschrock 			break;
30446a2abf2Seschrock 
30546a2abf2Seschrock 		case POOL_STATE_EXPORTED:
30646a2abf2Seschrock 			desc = gettext("exported");
30746a2abf2Seschrock 			break;
30846a2abf2Seschrock 
30946a2abf2Seschrock 		case POOL_STATE_POTENTIALLY_ACTIVE:
31046a2abf2Seschrock 			desc = gettext("potentially active");
31146a2abf2Seschrock 			break;
31246a2abf2Seschrock 
31346a2abf2Seschrock 		default:
31446a2abf2Seschrock 			desc = gettext("unknown");
31546a2abf2Seschrock 			break;
31646a2abf2Seschrock 		}
31746a2abf2Seschrock 
31899653d4eSeschrock 		/*
31999653d4eSeschrock 		 * Allow hot spares to be shared between pools.
32099653d4eSeschrock 		 */
32199653d4eSeschrock 		if (state == POOL_STATE_SPARE && isspare)
32299653d4eSeschrock 			return (0);
32399653d4eSeschrock 
32499653d4eSeschrock 		if (state == POOL_STATE_ACTIVE ||
32599653d4eSeschrock 		    state == POOL_STATE_SPARE || !force) {
32699653d4eSeschrock 			switch (state) {
32799653d4eSeschrock 			case POOL_STATE_SPARE:
32899653d4eSeschrock 				vdev_error(gettext("%s is reserved as a hot "
32999653d4eSeschrock 				    "spare for pool %s\n"), file, name);
33099653d4eSeschrock 				break;
33199653d4eSeschrock 			default:
33299653d4eSeschrock 				vdev_error(gettext("%s is part of %s pool "
33399653d4eSeschrock 				    "'%s'\n"), file, desc, name);
33499653d4eSeschrock 				break;
33599653d4eSeschrock 			}
336fa9e4066Sahrens 			ret = -1;
337fa9e4066Sahrens 		}
338fa9e4066Sahrens 
339fa9e4066Sahrens 		free(name);
340fa9e4066Sahrens 	}
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) close(fd);
343fa9e4066Sahrens 	return (ret);
344fa9e4066Sahrens }
345fa9e4066Sahrens 
3468488aeb5Staylor 
3478488aeb5Staylor /*
3488488aeb5Staylor  * By "whole disk" we mean an entire physical disk (something we can
3498488aeb5Staylor  * label, toggle the write cache on, etc.) as opposed to the full
3508488aeb5Staylor  * capacity of a pseudo-device such as lofi or did.  We act as if we
3518488aeb5Staylor  * are labeling the disk, which should be a pretty good test of whether
3528488aeb5Staylor  * it's a viable device or not.  Returns B_TRUE if it is and B_FALSE if
3538488aeb5Staylor  * it isn't.
3548488aeb5Staylor  */
35599653d4eSeschrock static boolean_t
3568488aeb5Staylor is_whole_disk(const char *arg)
357fa9e4066Sahrens {
3588488aeb5Staylor 	struct dk_gpt *label;
3598488aeb5Staylor 	int	fd;
3608488aeb5Staylor 	char	path[MAXPATHLEN];
361fa9e4066Sahrens 
3628488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s%s%s",
3638488aeb5Staylor 	    RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
3648488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
3658488aeb5Staylor 		return (B_FALSE);
3668488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
3678488aeb5Staylor 		(void) close(fd);
3688488aeb5Staylor 		return (B_FALSE);
3698488aeb5Staylor 	}
3708488aeb5Staylor 	efi_free(label);
3718488aeb5Staylor 	(void) close(fd);
3728488aeb5Staylor 	return (B_TRUE);
373fa9e4066Sahrens }
374fa9e4066Sahrens 
375fa9e4066Sahrens /*
376fa9e4066Sahrens  * Create a leaf vdev.  Determine if this is a file or a device.  If it's a
377fa9e4066Sahrens  * device, fill in the device id to make a complete nvlist.  Valid forms for a
378fa9e4066Sahrens  * leaf vdev are:
379fa9e4066Sahrens  *
380fa9e4066Sahrens  * 	/dev/dsk/xxx	Complete disk path
381fa9e4066Sahrens  * 	/xxx		Full path to file
382fa9e4066Sahrens  * 	xxx		Shorthand for /dev/dsk/xxx
383fa9e4066Sahrens  */
3848488aeb5Staylor static nvlist_t *
385*8654d025Sperrin make_leaf_vdev(const char *arg, uint64_t is_log)
386fa9e4066Sahrens {
387fa9e4066Sahrens 	char path[MAXPATHLEN];
388fa9e4066Sahrens 	struct stat64 statbuf;
389fa9e4066Sahrens 	nvlist_t *vdev = NULL;
390fa9e4066Sahrens 	char *type = NULL;
39199653d4eSeschrock 	boolean_t wholedisk = B_FALSE;
392fa9e4066Sahrens 
393fa9e4066Sahrens 	/*
394fa9e4066Sahrens 	 * Determine what type of vdev this is, and put the full path into
395fa9e4066Sahrens 	 * 'path'.  We detect whether this is a device of file afterwards by
396fa9e4066Sahrens 	 * checking the st_mode of the file.
397fa9e4066Sahrens 	 */
398fa9e4066Sahrens 	if (arg[0] == '/') {
399fa9e4066Sahrens 		/*
400fa9e4066Sahrens 		 * Complete device or file path.  Exact type is determined by
401fa9e4066Sahrens 		 * examining the file descriptor afterwards.
402fa9e4066Sahrens 		 */
4038488aeb5Staylor 		wholedisk = is_whole_disk(arg);
4048488aeb5Staylor 		if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
405fa9e4066Sahrens 			(void) fprintf(stderr,
406fa9e4066Sahrens 			    gettext("cannot open '%s': %s\n"),
407fa9e4066Sahrens 			    arg, strerror(errno));
408fa9e4066Sahrens 			return (NULL);
409fa9e4066Sahrens 		}
410fa9e4066Sahrens 
411fa9e4066Sahrens 		(void) strlcpy(path, arg, sizeof (path));
412fa9e4066Sahrens 	} else {
413fa9e4066Sahrens 		/*
414fa9e4066Sahrens 		 * This may be a short path for a device, or it could be total
415fa9e4066Sahrens 		 * gibberish.  Check to see if it's a known device in
416fa9e4066Sahrens 		 * /dev/dsk/.  As part of this check, see if we've been given a
417fa9e4066Sahrens 		 * an entire disk (minus the slice number).
418fa9e4066Sahrens 		 */
419fa9e4066Sahrens 		(void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT,
420fa9e4066Sahrens 		    arg);
4218488aeb5Staylor 		wholedisk = is_whole_disk(path);
4228488aeb5Staylor 		if (!wholedisk && (stat64(path, &statbuf) != 0)) {
423fa9e4066Sahrens 			/*
424fa9e4066Sahrens 			 * If we got ENOENT, then the user gave us
425fa9e4066Sahrens 			 * gibberish, so try to direct them with a
426fa9e4066Sahrens 			 * reasonable error message.  Otherwise,
427fa9e4066Sahrens 			 * regurgitate strerror() since it's the best we
428fa9e4066Sahrens 			 * can do.
429fa9e4066Sahrens 			 */
430fa9e4066Sahrens 			if (errno == ENOENT) {
431fa9e4066Sahrens 				(void) fprintf(stderr,
432fa9e4066Sahrens 				    gettext("cannot open '%s': no such "
433fa9e4066Sahrens 				    "device in %s\n"), arg, DISK_ROOT);
434fa9e4066Sahrens 				(void) fprintf(stderr,
435fa9e4066Sahrens 				    gettext("must be a full path or "
436fa9e4066Sahrens 				    "shorthand device name\n"));
437fa9e4066Sahrens 				return (NULL);
438fa9e4066Sahrens 			} else {
439fa9e4066Sahrens 				(void) fprintf(stderr,
440fa9e4066Sahrens 				    gettext("cannot open '%s': %s\n"),
441fa9e4066Sahrens 				    path, strerror(errno));
442fa9e4066Sahrens 				return (NULL);
443fa9e4066Sahrens 			}
444fa9e4066Sahrens 		}
445fa9e4066Sahrens 	}
446fa9e4066Sahrens 
447fa9e4066Sahrens 	/*
448fa9e4066Sahrens 	 * Determine whether this is a device or a file.
449fa9e4066Sahrens 	 */
4508488aeb5Staylor 	if (wholedisk || S_ISBLK(statbuf.st_mode)) {
451fa9e4066Sahrens 		type = VDEV_TYPE_DISK;
452fa9e4066Sahrens 	} else if (S_ISREG(statbuf.st_mode)) {
453fa9e4066Sahrens 		type = VDEV_TYPE_FILE;
454fa9e4066Sahrens 	} else {
455fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot use '%s': must be a "
456fa9e4066Sahrens 		    "block device or regular file\n"), path);
457fa9e4066Sahrens 		return (NULL);
458fa9e4066Sahrens 	}
459fa9e4066Sahrens 
460fa9e4066Sahrens 	/*
461fa9e4066Sahrens 	 * Finally, we have the complete device or file, and we know that it is
462fa9e4066Sahrens 	 * acceptable to use.  Construct the nvlist to describe this vdev.  All
463fa9e4066Sahrens 	 * vdevs have a 'path' element, and devices also have a 'devid' element.
464fa9e4066Sahrens 	 */
465fa9e4066Sahrens 	verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
466fa9e4066Sahrens 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
467fa9e4066Sahrens 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
468*8654d025Sperrin 	verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
469afefbcddSeschrock 	if (strcmp(type, VDEV_TYPE_DISK) == 0)
470afefbcddSeschrock 		verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
471afefbcddSeschrock 		    (uint64_t)wholedisk) == 0);
472fa9e4066Sahrens 
473fa9e4066Sahrens 	/*
474fa9e4066Sahrens 	 * For a whole disk, defer getting its devid until after labeling it.
475fa9e4066Sahrens 	 */
476fa9e4066Sahrens 	if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
477fa9e4066Sahrens 		/*
478fa9e4066Sahrens 		 * Get the devid for the device.
479fa9e4066Sahrens 		 */
480fa9e4066Sahrens 		int fd;
481fa9e4066Sahrens 		ddi_devid_t devid;
482fa9e4066Sahrens 		char *minor = NULL, *devid_str = NULL;
483fa9e4066Sahrens 
484fa9e4066Sahrens 		if ((fd = open(path, O_RDONLY)) < 0) {
485fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot open '%s': "
486fa9e4066Sahrens 			    "%s\n"), path, strerror(errno));
487fa9e4066Sahrens 			nvlist_free(vdev);
488fa9e4066Sahrens 			return (NULL);
489fa9e4066Sahrens 		}
490fa9e4066Sahrens 
491fa9e4066Sahrens 		if (devid_get(fd, &devid) == 0) {
492fa9e4066Sahrens 			if (devid_get_minor_name(fd, &minor) == 0 &&
493fa9e4066Sahrens 			    (devid_str = devid_str_encode(devid, minor)) !=
494fa9e4066Sahrens 			    NULL) {
495fa9e4066Sahrens 				verify(nvlist_add_string(vdev,
496fa9e4066Sahrens 				    ZPOOL_CONFIG_DEVID, devid_str) == 0);
497fa9e4066Sahrens 			}
498fa9e4066Sahrens 			if (devid_str != NULL)
499fa9e4066Sahrens 				devid_str_free(devid_str);
500fa9e4066Sahrens 			if (minor != NULL)
501fa9e4066Sahrens 				devid_str_free(minor);
502fa9e4066Sahrens 			devid_free(devid);
503fa9e4066Sahrens 		}
504fa9e4066Sahrens 
505fa9e4066Sahrens 		(void) close(fd);
506fa9e4066Sahrens 	}
507fa9e4066Sahrens 
508fa9e4066Sahrens 	return (vdev);
509fa9e4066Sahrens }
510fa9e4066Sahrens 
511fa9e4066Sahrens /*
512fa9e4066Sahrens  * Go through and verify the replication level of the pool is consistent.
513fa9e4066Sahrens  * Performs the following checks:
514fa9e4066Sahrens  *
515fa9e4066Sahrens  * 	For the new spec, verifies that devices in mirrors and raidz are the
516fa9e4066Sahrens  * 	same size.
517fa9e4066Sahrens  *
518fa9e4066Sahrens  * 	If the current configuration already has inconsistent replication
519fa9e4066Sahrens  * 	levels, ignore any other potential problems in the new spec.
520fa9e4066Sahrens  *
521fa9e4066Sahrens  * 	Otherwise, make sure that the current spec (if there is one) and the new
522fa9e4066Sahrens  * 	spec have consistent replication levels.
523fa9e4066Sahrens  */
524fa9e4066Sahrens typedef struct replication_level {
52599653d4eSeschrock 	char *zprl_type;
52699653d4eSeschrock 	uint64_t zprl_children;
52799653d4eSeschrock 	uint64_t zprl_parity;
528fa9e4066Sahrens } replication_level_t;
529fa9e4066Sahrens 
5308488aeb5Staylor #define	ZPOOL_FUZZ	(16 * 1024 * 1024)
5318488aeb5Staylor 
532fa9e4066Sahrens /*
533fa9e4066Sahrens  * Given a list of toplevel vdevs, return the current replication level.  If
534fa9e4066Sahrens  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
535fa9e4066Sahrens  * an error message will be displayed for each self-inconsistent vdev.
536fa9e4066Sahrens  */
5378488aeb5Staylor static replication_level_t *
53899653d4eSeschrock get_replication(nvlist_t *nvroot, boolean_t fatal)
539fa9e4066Sahrens {
540fa9e4066Sahrens 	nvlist_t **top;
541fa9e4066Sahrens 	uint_t t, toplevels;
542fa9e4066Sahrens 	nvlist_t **child;
543fa9e4066Sahrens 	uint_t c, children;
544fa9e4066Sahrens 	nvlist_t *nv;
545fa9e4066Sahrens 	char *type;
546fa9e4066Sahrens 	replication_level_t lastrep, rep, *ret;
54799653d4eSeschrock 	boolean_t dontreport;
548*8654d025Sperrin 	uint64_t is_log;
549fa9e4066Sahrens 
550fa9e4066Sahrens 	ret = safe_malloc(sizeof (replication_level_t));
551fa9e4066Sahrens 
552fa9e4066Sahrens 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
553fa9e4066Sahrens 	    &top, &toplevels) == 0);
554fa9e4066Sahrens 
55599653d4eSeschrock 	lastrep.zprl_type = NULL;
556fa9e4066Sahrens 	for (t = 0; t < toplevels; t++) {
557*8654d025Sperrin 		uint64_t is_log = B_FALSE;
558*8654d025Sperrin 
559fa9e4066Sahrens 		nv = top[t];
560fa9e4066Sahrens 
561*8654d025Sperrin 		/*
562*8654d025Sperrin 		 * For separate logs we ignore the top level vdev replication
563*8654d025Sperrin 		 * constraints.
564*8654d025Sperrin 		 */
565*8654d025Sperrin 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
566*8654d025Sperrin 		if (is_log)
567*8654d025Sperrin 			continue;
568*8654d025Sperrin 
569*8654d025Sperrin 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
570*8654d025Sperrin 		    &type) == 0);
571fa9e4066Sahrens 		if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
572fa9e4066Sahrens 		    &child, &children) != 0) {
573fa9e4066Sahrens 			/*
574fa9e4066Sahrens 			 * This is a 'file' or 'disk' vdev.
575fa9e4066Sahrens 			 */
57699653d4eSeschrock 			rep.zprl_type = type;
57799653d4eSeschrock 			rep.zprl_children = 1;
57899653d4eSeschrock 			rep.zprl_parity = 0;
579fa9e4066Sahrens 		} else {
580fa9e4066Sahrens 			uint64_t vdev_size;
581fa9e4066Sahrens 
582fa9e4066Sahrens 			/*
583fa9e4066Sahrens 			 * This is a mirror or RAID-Z vdev.  Go through and make
584fa9e4066Sahrens 			 * sure the contents are all the same (files vs. disks),
585fa9e4066Sahrens 			 * keeping track of the number of elements in the
586fa9e4066Sahrens 			 * process.
587fa9e4066Sahrens 			 *
588fa9e4066Sahrens 			 * We also check that the size of each vdev (if it can
589fa9e4066Sahrens 			 * be determined) is the same.
590fa9e4066Sahrens 			 */
59199653d4eSeschrock 			rep.zprl_type = type;
59299653d4eSeschrock 			rep.zprl_children = 0;
59399653d4eSeschrock 
59499653d4eSeschrock 			if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
59599653d4eSeschrock 				verify(nvlist_lookup_uint64(nv,
59699653d4eSeschrock 				    ZPOOL_CONFIG_NPARITY,
59799653d4eSeschrock 				    &rep.zprl_parity) == 0);
59899653d4eSeschrock 				assert(rep.zprl_parity != 0);
59999653d4eSeschrock 			} else {
60099653d4eSeschrock 				rep.zprl_parity = 0;
60199653d4eSeschrock 			}
602fa9e4066Sahrens 
603fa9e4066Sahrens 			/*
604*8654d025Sperrin 			 * The 'dontreport' variable indicates that we've
605fa9e4066Sahrens 			 * already reported an error for this spec, so don't
606fa9e4066Sahrens 			 * bother doing it again.
607fa9e4066Sahrens 			 */
608fa9e4066Sahrens 			type = NULL;
609fa9e4066Sahrens 			dontreport = 0;
610fa9e4066Sahrens 			vdev_size = -1ULL;
611fa9e4066Sahrens 			for (c = 0; c < children; c++) {
612fa9e4066Sahrens 				nvlist_t *cnv = child[c];
613fa9e4066Sahrens 				char *path;
614fa9e4066Sahrens 				struct stat64 statbuf;
615fa9e4066Sahrens 				uint64_t size = -1ULL;
616fa9e4066Sahrens 				char *childtype;
617fa9e4066Sahrens 				int fd, err;
618fa9e4066Sahrens 
61999653d4eSeschrock 				rep.zprl_children++;
620fa9e4066Sahrens 
621fa9e4066Sahrens 				verify(nvlist_lookup_string(cnv,
622fa9e4066Sahrens 				    ZPOOL_CONFIG_TYPE, &childtype) == 0);
62394de1d4cSeschrock 
62494de1d4cSeschrock 				/*
625*8654d025Sperrin 				 * If this is a replacing or spare vdev, then
62694de1d4cSeschrock 				 * get the real first child of the vdev.
62794de1d4cSeschrock 				 */
62894de1d4cSeschrock 				if (strcmp(childtype,
62994de1d4cSeschrock 				    VDEV_TYPE_REPLACING) == 0 ||
63094de1d4cSeschrock 				    strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
63194de1d4cSeschrock 					nvlist_t **rchild;
63294de1d4cSeschrock 					uint_t rchildren;
63394de1d4cSeschrock 
63494de1d4cSeschrock 					verify(nvlist_lookup_nvlist_array(cnv,
63594de1d4cSeschrock 					    ZPOOL_CONFIG_CHILDREN, &rchild,
63694de1d4cSeschrock 					    &rchildren) == 0);
63794de1d4cSeschrock 					assert(rchildren == 2);
63894de1d4cSeschrock 					cnv = rchild[0];
63994de1d4cSeschrock 
64094de1d4cSeschrock 					verify(nvlist_lookup_string(cnv,
64194de1d4cSeschrock 					    ZPOOL_CONFIG_TYPE,
64294de1d4cSeschrock 					    &childtype) == 0);
64394de1d4cSeschrock 				}
64494de1d4cSeschrock 
645fa9e4066Sahrens 				verify(nvlist_lookup_string(cnv,
646fa9e4066Sahrens 				    ZPOOL_CONFIG_PATH, &path) == 0);
647fa9e4066Sahrens 
648fa9e4066Sahrens 				/*
649fa9e4066Sahrens 				 * If we have a raidz/mirror that combines disks
650fa9e4066Sahrens 				 * with files, report it as an error.
651fa9e4066Sahrens 				 */
652fa9e4066Sahrens 				if (!dontreport && type != NULL &&
653fa9e4066Sahrens 				    strcmp(type, childtype) != 0) {
654fa9e4066Sahrens 					if (ret != NULL)
655fa9e4066Sahrens 						free(ret);
656fa9e4066Sahrens 					ret = NULL;
657fa9e4066Sahrens 					if (fatal)
658fa9e4066Sahrens 						vdev_error(gettext(
659fa9e4066Sahrens 						    "mismatched replication "
660fa9e4066Sahrens 						    "level: %s contains both "
661fa9e4066Sahrens 						    "files and devices\n"),
66299653d4eSeschrock 						    rep.zprl_type);
663fa9e4066Sahrens 					else
664fa9e4066Sahrens 						return (NULL);
66599653d4eSeschrock 					dontreport = B_TRUE;
666fa9e4066Sahrens 				}
667fa9e4066Sahrens 
668fa9e4066Sahrens 				/*
669fa9e4066Sahrens 				 * According to stat(2), the value of 'st_size'
670fa9e4066Sahrens 				 * is undefined for block devices and character
671fa9e4066Sahrens 				 * devices.  But there is no effective way to
672fa9e4066Sahrens 				 * determine the real size in userland.
673fa9e4066Sahrens 				 *
674fa9e4066Sahrens 				 * Instead, we'll take advantage of an
675fa9e4066Sahrens 				 * implementation detail of spec_size().  If the
676fa9e4066Sahrens 				 * device is currently open, then we (should)
677fa9e4066Sahrens 				 * return a valid size.
678fa9e4066Sahrens 				 *
679fa9e4066Sahrens 				 * If we still don't get a valid size (indicated
680fa9e4066Sahrens 				 * by a size of 0 or MAXOFFSET_T), then ignore
681fa9e4066Sahrens 				 * this device altogether.
682fa9e4066Sahrens 				 */
683fa9e4066Sahrens 				if ((fd = open(path, O_RDONLY)) >= 0) {
684fa9e4066Sahrens 					err = fstat64(fd, &statbuf);
685fa9e4066Sahrens 					(void) close(fd);
686fa9e4066Sahrens 				} else {
687fa9e4066Sahrens 					err = stat64(path, &statbuf);
688fa9e4066Sahrens 				}
689fa9e4066Sahrens 
690fa9e4066Sahrens 				if (err != 0 ||
691fa9e4066Sahrens 				    statbuf.st_size == 0 ||
692fa9e4066Sahrens 				    statbuf.st_size == MAXOFFSET_T)
693fa9e4066Sahrens 					continue;
694fa9e4066Sahrens 
695fa9e4066Sahrens 				size = statbuf.st_size;
696fa9e4066Sahrens 
697fa9e4066Sahrens 				/*
6988488aeb5Staylor 				 * Also make sure that devices and
6998488aeb5Staylor 				 * slices have a consistent size.  If
7008488aeb5Staylor 				 * they differ by a significant amount
7018488aeb5Staylor 				 * (~16MB) then report an error.
702fa9e4066Sahrens 				 */
7038488aeb5Staylor 				if (!dontreport &&
7048488aeb5Staylor 				    (vdev_size != -1ULL &&
7058488aeb5Staylor 				    (labs(size - vdev_size) >
7068488aeb5Staylor 				    ZPOOL_FUZZ))) {
707fa9e4066Sahrens 					if (ret != NULL)
708fa9e4066Sahrens 						free(ret);
709fa9e4066Sahrens 					ret = NULL;
710fa9e4066Sahrens 					if (fatal)
711fa9e4066Sahrens 						vdev_error(gettext(
712fa9e4066Sahrens 						    "%s contains devices of "
713fa9e4066Sahrens 						    "different sizes\n"),
71499653d4eSeschrock 						    rep.zprl_type);
715fa9e4066Sahrens 					else
716fa9e4066Sahrens 						return (NULL);
71799653d4eSeschrock 					dontreport = B_TRUE;
718fa9e4066Sahrens 				}
719fa9e4066Sahrens 
720fa9e4066Sahrens 				type = childtype;
721fa9e4066Sahrens 				vdev_size = size;
722fa9e4066Sahrens 			}
723fa9e4066Sahrens 		}
724fa9e4066Sahrens 
725fa9e4066Sahrens 		/*
726fa9e4066Sahrens 		 * At this point, we have the replication of the last toplevel
727fa9e4066Sahrens 		 * vdev in 'rep'.  Compare it to 'lastrep' to see if its
728fa9e4066Sahrens 		 * different.
729fa9e4066Sahrens 		 */
73099653d4eSeschrock 		if (lastrep.zprl_type != NULL) {
73199653d4eSeschrock 			if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
732fa9e4066Sahrens 				if (ret != NULL)
733fa9e4066Sahrens 					free(ret);
734fa9e4066Sahrens 				ret = NULL;
735fa9e4066Sahrens 				if (fatal)
736fa9e4066Sahrens 					vdev_error(gettext(
73799653d4eSeschrock 					    "mismatched replication level: "
73899653d4eSeschrock 					    "both %s and %s vdevs are "
739fa9e4066Sahrens 					    "present\n"),
74099653d4eSeschrock 					    lastrep.zprl_type, rep.zprl_type);
741fa9e4066Sahrens 				else
742fa9e4066Sahrens 					return (NULL);
74399653d4eSeschrock 			} else if (lastrep.zprl_parity != rep.zprl_parity) {
744fa9e4066Sahrens 				if (ret)
745fa9e4066Sahrens 					free(ret);
746fa9e4066Sahrens 				ret = NULL;
747fa9e4066Sahrens 				if (fatal)
748fa9e4066Sahrens 					vdev_error(gettext(
74999653d4eSeschrock 					    "mismatched replication level: "
75099653d4eSeschrock 					    "both %llu and %llu device parity "
75199653d4eSeschrock 					    "%s vdevs are present\n"),
75299653d4eSeschrock 					    lastrep.zprl_parity,
75399653d4eSeschrock 					    rep.zprl_parity,
75499653d4eSeschrock 					    rep.zprl_type);
75599653d4eSeschrock 				else
75699653d4eSeschrock 					return (NULL);
75799653d4eSeschrock 			} else if (lastrep.zprl_children != rep.zprl_children) {
75899653d4eSeschrock 				if (ret)
75999653d4eSeschrock 					free(ret);
76099653d4eSeschrock 				ret = NULL;
76199653d4eSeschrock 				if (fatal)
76299653d4eSeschrock 					vdev_error(gettext(
76399653d4eSeschrock 					    "mismatched replication level: "
76499653d4eSeschrock 					    "both %llu-way and %llu-way %s "
765fa9e4066Sahrens 					    "vdevs are present\n"),
76699653d4eSeschrock 					    lastrep.zprl_children,
76799653d4eSeschrock 					    rep.zprl_children,
76899653d4eSeschrock 					    rep.zprl_type);
769fa9e4066Sahrens 				else
770fa9e4066Sahrens 					return (NULL);
771fa9e4066Sahrens 			}
772fa9e4066Sahrens 		}
773fa9e4066Sahrens 		lastrep = rep;
774fa9e4066Sahrens 	}
775fa9e4066Sahrens 
77699653d4eSeschrock 	if (ret != NULL)
77799653d4eSeschrock 		*ret = rep;
778fa9e4066Sahrens 
779fa9e4066Sahrens 	return (ret);
780fa9e4066Sahrens }
781fa9e4066Sahrens 
782fa9e4066Sahrens /*
783fa9e4066Sahrens  * Check the replication level of the vdev spec against the current pool.  Calls
784fa9e4066Sahrens  * get_replication() to make sure the new spec is self-consistent.  If the pool
785fa9e4066Sahrens  * has a consistent replication level, then we ignore any errors.  Otherwise,
786fa9e4066Sahrens  * report any difference between the two.
787fa9e4066Sahrens  */
7888488aeb5Staylor static int
789fa9e4066Sahrens check_replication(nvlist_t *config, nvlist_t *newroot)
790fa9e4066Sahrens {
7918488aeb5Staylor 	nvlist_t **child;
7928488aeb5Staylor 	uint_t	children;
793fa9e4066Sahrens 	replication_level_t *current = NULL, *new;
794fa9e4066Sahrens 	int ret;
795fa9e4066Sahrens 
796fa9e4066Sahrens 	/*
797fa9e4066Sahrens 	 * If we have a current pool configuration, check to see if it's
798fa9e4066Sahrens 	 * self-consistent.  If not, simply return success.
799fa9e4066Sahrens 	 */
800fa9e4066Sahrens 	if (config != NULL) {
801fa9e4066Sahrens 		nvlist_t *nvroot;
802fa9e4066Sahrens 
803fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
804fa9e4066Sahrens 		    &nvroot) == 0);
80599653d4eSeschrock 		if ((current = get_replication(nvroot, B_FALSE)) == NULL)
806fa9e4066Sahrens 			return (0);
807fa9e4066Sahrens 	}
8088488aeb5Staylor 	/*
8098488aeb5Staylor 	 * for spares there may be no children, and therefore no
8108488aeb5Staylor 	 * replication level to check
8118488aeb5Staylor 	 */
8128488aeb5Staylor 	if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
8138488aeb5Staylor 	    &child, &children) != 0) || (children == 0)) {
8148488aeb5Staylor 		free(current);
8158488aeb5Staylor 		return (0);
8168488aeb5Staylor 	}
817fa9e4066Sahrens 
818*8654d025Sperrin 	/*
819*8654d025Sperrin 	 * If all we have is logs then there's no replication level to check.
820*8654d025Sperrin 	 */
821*8654d025Sperrin 	if (num_logs(newroot) == children) {
822*8654d025Sperrin 		free(current);
823*8654d025Sperrin 		return (0);
824*8654d025Sperrin 	}
825*8654d025Sperrin 
826fa9e4066Sahrens 	/*
827fa9e4066Sahrens 	 * Get the replication level of the new vdev spec, reporting any
828fa9e4066Sahrens 	 * inconsistencies found.
829fa9e4066Sahrens 	 */
83099653d4eSeschrock 	if ((new = get_replication(newroot, B_TRUE)) == NULL) {
831fa9e4066Sahrens 		free(current);
832fa9e4066Sahrens 		return (-1);
833fa9e4066Sahrens 	}
834fa9e4066Sahrens 
835fa9e4066Sahrens 	/*
836fa9e4066Sahrens 	 * Check to see if the new vdev spec matches the replication level of
837fa9e4066Sahrens 	 * the current pool.
838fa9e4066Sahrens 	 */
839fa9e4066Sahrens 	ret = 0;
840fa9e4066Sahrens 	if (current != NULL) {
84199653d4eSeschrock 		if (strcmp(current->zprl_type, new->zprl_type) != 0) {
842fa9e4066Sahrens 			vdev_error(gettext(
84399653d4eSeschrock 			    "mismatched replication level: pool uses %s "
84499653d4eSeschrock 			    "and new vdev is %s\n"),
84599653d4eSeschrock 			    current->zprl_type, new->zprl_type);
84699653d4eSeschrock 			ret = -1;
84799653d4eSeschrock 		} else if (current->zprl_parity != new->zprl_parity) {
84899653d4eSeschrock 			vdev_error(gettext(
84999653d4eSeschrock 			    "mismatched replication level: pool uses %llu "
85099653d4eSeschrock 			    "device parity and new vdev uses %llu\n"),
85199653d4eSeschrock 			    current->zprl_parity, new->zprl_parity);
85299653d4eSeschrock 			ret = -1;
85399653d4eSeschrock 		} else if (current->zprl_children != new->zprl_children) {
85499653d4eSeschrock 			vdev_error(gettext(
85599653d4eSeschrock 			    "mismatched replication level: pool uses %llu-way "
85699653d4eSeschrock 			    "%s and new vdev uses %llu-way %s\n"),
85799653d4eSeschrock 			    current->zprl_children, current->zprl_type,
85899653d4eSeschrock 			    new->zprl_children, new->zprl_type);
859fa9e4066Sahrens 			ret = -1;
860fa9e4066Sahrens 		}
861fa9e4066Sahrens 	}
862fa9e4066Sahrens 
863fa9e4066Sahrens 	free(new);
864fa9e4066Sahrens 	if (current != NULL)
865fa9e4066Sahrens 		free(current);
866fa9e4066Sahrens 
867fa9e4066Sahrens 	return (ret);
868fa9e4066Sahrens }
869fa9e4066Sahrens 
870fa9e4066Sahrens /*
871fa9e4066Sahrens  * Go through and find any whole disks in the vdev specification, labelling them
872fa9e4066Sahrens  * as appropriate.  When constructing the vdev spec, we were unable to open this
873fa9e4066Sahrens  * device in order to provide a devid.  Now that we have labelled the disk and
874fa9e4066Sahrens  * know that slice 0 is valid, we can construct the devid now.
875fa9e4066Sahrens  *
8768488aeb5Staylor  * If the disk was already labeled with an EFI label, we will have gotten the
877fa9e4066Sahrens  * devid already (because we were able to open the whole disk).  Otherwise, we
878fa9e4066Sahrens  * need to get the devid after we label the disk.
879fa9e4066Sahrens  */
8808488aeb5Staylor static int
8818488aeb5Staylor make_disks(zpool_handle_t *zhp, nvlist_t *nv)
882fa9e4066Sahrens {
883fa9e4066Sahrens 	nvlist_t **child;
884fa9e4066Sahrens 	uint_t c, children;
885fa9e4066Sahrens 	char *type, *path, *diskname;
886fa9e4066Sahrens 	char buf[MAXPATHLEN];
887afefbcddSeschrock 	uint64_t wholedisk;
888fa9e4066Sahrens 	int fd;
889fa9e4066Sahrens 	int ret;
890fa9e4066Sahrens 	ddi_devid_t devid;
891fa9e4066Sahrens 	char *minor = NULL, *devid_str = NULL;
892fa9e4066Sahrens 
893fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
894fa9e4066Sahrens 
895fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
896fa9e4066Sahrens 	    &child, &children) != 0) {
897fa9e4066Sahrens 
898fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_DISK) != 0)
899fa9e4066Sahrens 			return (0);
900fa9e4066Sahrens 
901fa9e4066Sahrens 		/*
902fa9e4066Sahrens 		 * We have a disk device.  Get the path to the device
9038488aeb5Staylor 		 * and see if it's a whole disk by appending the backup
904fa9e4066Sahrens 		 * slice and stat()ing the device.
905fa9e4066Sahrens 		 */
906fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
907afefbcddSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
908afefbcddSeschrock 		    &wholedisk) != 0 || !wholedisk)
909fa9e4066Sahrens 			return (0);
910fa9e4066Sahrens 
911fa9e4066Sahrens 		diskname = strrchr(path, '/');
912fa9e4066Sahrens 		assert(diskname != NULL);
913fa9e4066Sahrens 		diskname++;
9148488aeb5Staylor 		if (zpool_label_disk(g_zfs, zhp, diskname) == -1)
915fa9e4066Sahrens 			return (-1);
916fa9e4066Sahrens 
917fa9e4066Sahrens 		/*
918fa9e4066Sahrens 		 * Fill in the devid, now that we've labeled the disk.
919fa9e4066Sahrens 		 */
920fa9e4066Sahrens 		(void) snprintf(buf, sizeof (buf), "%ss0", path);
921fa9e4066Sahrens 		if ((fd = open(buf, O_RDONLY)) < 0) {
922fa9e4066Sahrens 			(void) fprintf(stderr,
923fa9e4066Sahrens 			    gettext("cannot open '%s': %s\n"),
924fa9e4066Sahrens 			    buf, strerror(errno));
925fa9e4066Sahrens 			return (-1);
926fa9e4066Sahrens 		}
927fa9e4066Sahrens 
928fa9e4066Sahrens 		if (devid_get(fd, &devid) == 0) {
929fa9e4066Sahrens 			if (devid_get_minor_name(fd, &minor) == 0 &&
930fa9e4066Sahrens 			    (devid_str = devid_str_encode(devid, minor)) !=
931fa9e4066Sahrens 			    NULL) {
932fa9e4066Sahrens 				verify(nvlist_add_string(nv,
933fa9e4066Sahrens 				    ZPOOL_CONFIG_DEVID, devid_str) == 0);
934fa9e4066Sahrens 			}
935fa9e4066Sahrens 			if (devid_str != NULL)
936fa9e4066Sahrens 				devid_str_free(devid_str);
937fa9e4066Sahrens 			if (minor != NULL)
938fa9e4066Sahrens 				devid_str_free(minor);
939fa9e4066Sahrens 			devid_free(devid);
940fa9e4066Sahrens 		}
941fa9e4066Sahrens 
942afefbcddSeschrock 		/*
943afefbcddSeschrock 		 * Update the path to refer to the 's0' slice.  The presence of
944afefbcddSeschrock 		 * the 'whole_disk' field indicates to the CLI that we should
945afefbcddSeschrock 		 * chop off the slice number when displaying the device in
946afefbcddSeschrock 		 * future output.
947afefbcddSeschrock 		 */
948afefbcddSeschrock 		verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
949afefbcddSeschrock 
950fa9e4066Sahrens 		(void) close(fd);
951fa9e4066Sahrens 
952fa9e4066Sahrens 		return (0);
953fa9e4066Sahrens 	}
954fa9e4066Sahrens 
955fa9e4066Sahrens 	for (c = 0; c < children; c++)
9568488aeb5Staylor 		if ((ret = make_disks(zhp, child[c])) != 0)
957fa9e4066Sahrens 			return (ret);
958fa9e4066Sahrens 
95999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
96099653d4eSeschrock 	    &child, &children) == 0)
96199653d4eSeschrock 		for (c = 0; c < children; c++)
9628488aeb5Staylor 			if ((ret = make_disks(zhp, child[c])) != 0)
96399653d4eSeschrock 				return (ret);
96499653d4eSeschrock 
965fa9e4066Sahrens 	return (0);
966fa9e4066Sahrens }
967fa9e4066Sahrens 
96899653d4eSeschrock /*
96999653d4eSeschrock  * Determine if the given path is a hot spare within the given configuration.
97099653d4eSeschrock  */
97199653d4eSeschrock static boolean_t
97299653d4eSeschrock is_spare(nvlist_t *config, const char *path)
97399653d4eSeschrock {
97499653d4eSeschrock 	int fd;
97599653d4eSeschrock 	pool_state_t state;
9763ccfa83cSahrens 	char *name = NULL;
97799653d4eSeschrock 	nvlist_t *label;
97899653d4eSeschrock 	uint64_t guid, spareguid;
97999653d4eSeschrock 	nvlist_t *nvroot;
98099653d4eSeschrock 	nvlist_t **spares;
98199653d4eSeschrock 	uint_t i, nspares;
98299653d4eSeschrock 	boolean_t inuse;
98399653d4eSeschrock 
98499653d4eSeschrock 	if ((fd = open(path, O_RDONLY)) < 0)
98599653d4eSeschrock 		return (B_FALSE);
98699653d4eSeschrock 
98799653d4eSeschrock 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
98899653d4eSeschrock 	    !inuse ||
98999653d4eSeschrock 	    state != POOL_STATE_SPARE ||
99099653d4eSeschrock 	    zpool_read_label(fd, &label) != 0) {
9913ccfa83cSahrens 		free(name);
99299653d4eSeschrock 		(void) close(fd);
99399653d4eSeschrock 		return (B_FALSE);
99499653d4eSeschrock 	}
9953ccfa83cSahrens 	free(name);
99699653d4eSeschrock 
99799653d4eSeschrock 	(void) close(fd);
99899653d4eSeschrock 	verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
99999653d4eSeschrock 	nvlist_free(label);
100099653d4eSeschrock 
100199653d4eSeschrock 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
100299653d4eSeschrock 	    &nvroot) == 0);
100399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
100499653d4eSeschrock 	    &spares, &nspares) == 0) {
100599653d4eSeschrock 		for (i = 0; i < nspares; i++) {
100699653d4eSeschrock 			verify(nvlist_lookup_uint64(spares[i],
100799653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &spareguid) == 0);
100899653d4eSeschrock 			if (spareguid == guid)
100999653d4eSeschrock 				return (B_TRUE);
101099653d4eSeschrock 		}
101199653d4eSeschrock 	}
101299653d4eSeschrock 
101399653d4eSeschrock 	return (B_FALSE);
101499653d4eSeschrock }
101599653d4eSeschrock 
1016fa9e4066Sahrens /*
1017fa9e4066Sahrens  * Go through and find any devices that are in use.  We rely on libdiskmgt for
1018fa9e4066Sahrens  * the majority of this task.
1019fa9e4066Sahrens  */
10208488aeb5Staylor static int
102199653d4eSeschrock check_in_use(nvlist_t *config, nvlist_t *nv, int force, int isreplacing,
102299653d4eSeschrock     int isspare)
1023fa9e4066Sahrens {
1024fa9e4066Sahrens 	nvlist_t **child;
1025fa9e4066Sahrens 	uint_t c, children;
1026fa9e4066Sahrens 	char *type, *path;
1027fa9e4066Sahrens 	int ret;
102899653d4eSeschrock 	char buf[MAXPATHLEN];
102999653d4eSeschrock 	uint64_t wholedisk;
1030fa9e4066Sahrens 
1031fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1034fa9e4066Sahrens 	    &child, &children) != 0) {
1035fa9e4066Sahrens 
1036fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1037fa9e4066Sahrens 
103899653d4eSeschrock 		/*
103999653d4eSeschrock 		 * As a generic check, we look to see if this is a replace of a
104099653d4eSeschrock 		 * hot spare within the same pool.  If so, we allow it
104199653d4eSeschrock 		 * regardless of what libdiskmgt or zpool_in_use() says.
104299653d4eSeschrock 		 */
104399653d4eSeschrock 		if (isreplacing) {
104499653d4eSeschrock 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
104599653d4eSeschrock 			    &wholedisk) == 0 && wholedisk)
104699653d4eSeschrock 				(void) snprintf(buf, sizeof (buf), "%ss0",
104799653d4eSeschrock 				    path);
104899653d4eSeschrock 			else
104999653d4eSeschrock 				(void) strlcpy(buf, path, sizeof (buf));
105099653d4eSeschrock 			if (is_spare(config, buf))
105199653d4eSeschrock 				return (0);
105299653d4eSeschrock 		}
105399653d4eSeschrock 
1054fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
105599653d4eSeschrock 			ret = check_device(path, force, isspare);
1056fa9e4066Sahrens 
1057fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_FILE) == 0)
105899653d4eSeschrock 			ret = check_file(path, force, isspare);
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 		return (ret);
1061fa9e4066Sahrens 	}
1062fa9e4066Sahrens 
1063fa9e4066Sahrens 	for (c = 0; c < children; c++)
106499653d4eSeschrock 		if ((ret = check_in_use(config, child[c], force,
106599653d4eSeschrock 		    isreplacing, B_FALSE)) != 0)
1066fa9e4066Sahrens 			return (ret);
1067fa9e4066Sahrens 
106899653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
106999653d4eSeschrock 	    &child, &children) == 0)
107099653d4eSeschrock 		for (c = 0; c < children; c++)
107199653d4eSeschrock 			if ((ret = check_in_use(config, child[c], force,
107299653d4eSeschrock 			    isreplacing, B_TRUE)) != 0)
107399653d4eSeschrock 				return (ret);
1074fa9e4066Sahrens 	return (0);
1075fa9e4066Sahrens }
1076fa9e4066Sahrens 
10778488aeb5Staylor static const char *
107899653d4eSeschrock is_grouping(const char *type, int *mindev)
107999653d4eSeschrock {
108099653d4eSeschrock 	if (strcmp(type, "raidz") == 0 || strcmp(type, "raidz1") == 0) {
108199653d4eSeschrock 		if (mindev != NULL)
108299653d4eSeschrock 			*mindev = 2;
108399653d4eSeschrock 		return (VDEV_TYPE_RAIDZ);
108499653d4eSeschrock 	}
108599653d4eSeschrock 
108699653d4eSeschrock 	if (strcmp(type, "raidz2") == 0) {
108799653d4eSeschrock 		if (mindev != NULL)
108899653d4eSeschrock 			*mindev = 3;
108999653d4eSeschrock 		return (VDEV_TYPE_RAIDZ);
109099653d4eSeschrock 	}
109199653d4eSeschrock 
109299653d4eSeschrock 	if (strcmp(type, "mirror") == 0) {
109399653d4eSeschrock 		if (mindev != NULL)
109499653d4eSeschrock 			*mindev = 2;
109599653d4eSeschrock 		return (VDEV_TYPE_MIRROR);
109699653d4eSeschrock 	}
109799653d4eSeschrock 
109899653d4eSeschrock 	if (strcmp(type, "spare") == 0) {
109999653d4eSeschrock 		if (mindev != NULL)
110099653d4eSeschrock 			*mindev = 1;
110199653d4eSeschrock 		return (VDEV_TYPE_SPARE);
110299653d4eSeschrock 	}
110399653d4eSeschrock 
1104*8654d025Sperrin 	if (strcmp(type, "log") == 0) {
1105*8654d025Sperrin 		if (mindev != NULL)
1106*8654d025Sperrin 			*mindev = 1;
1107*8654d025Sperrin 		return (VDEV_TYPE_LOG);
1108*8654d025Sperrin 	}
1109*8654d025Sperrin 
111099653d4eSeschrock 	return (NULL);
111199653d4eSeschrock }
111299653d4eSeschrock 
1113fa9e4066Sahrens /*
1114fa9e4066Sahrens  * Construct a syntactically valid vdev specification,
1115fa9e4066Sahrens  * and ensure that all devices and files exist and can be opened.
1116fa9e4066Sahrens  * Note: we don't bother freeing anything in the error paths
1117fa9e4066Sahrens  * because the program is just going to exit anyway.
1118fa9e4066Sahrens  */
1119fa9e4066Sahrens nvlist_t *
1120fa9e4066Sahrens construct_spec(int argc, char **argv)
1121fa9e4066Sahrens {
112299653d4eSeschrock 	nvlist_t *nvroot, *nv, **top, **spares;
1123*8654d025Sperrin 	int t, toplevels, mindev, nspares, nlogs;
112499653d4eSeschrock 	const char *type;
1125*8654d025Sperrin 	uint64_t is_log;
1126*8654d025Sperrin 	boolean_t seen_logs;
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	top = NULL;
1129fa9e4066Sahrens 	toplevels = 0;
113099653d4eSeschrock 	spares = NULL;
113199653d4eSeschrock 	nspares = 0;
1132*8654d025Sperrin 	nlogs = 0;
1133*8654d025Sperrin 	is_log = B_FALSE;
1134*8654d025Sperrin 	seen_logs = B_FALSE;
1135fa9e4066Sahrens 
1136fa9e4066Sahrens 	while (argc > 0) {
1137fa9e4066Sahrens 		nv = NULL;
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 		/*
1140fa9e4066Sahrens 		 * If it's a mirror or raidz, the subsequent arguments are
1141fa9e4066Sahrens 		 * its leaves -- until we encounter the next mirror or raidz.
1142fa9e4066Sahrens 		 */
114399653d4eSeschrock 		if ((type = is_grouping(argv[0], &mindev)) != NULL) {
1144fa9e4066Sahrens 			nvlist_t **child = NULL;
114599653d4eSeschrock 			int c, children = 0;
114699653d4eSeschrock 
1147*8654d025Sperrin 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1148*8654d025Sperrin 				if (spares != NULL) {
1149*8654d025Sperrin 					(void) fprintf(stderr,
1150*8654d025Sperrin 					    gettext("invalid vdev "
1151*8654d025Sperrin 					    "specification: 'spare' can be "
1152*8654d025Sperrin 					    "specified only once\n"));
1153*8654d025Sperrin 					return (NULL);
1154*8654d025Sperrin 				}
1155*8654d025Sperrin 				is_log = B_FALSE;
1156*8654d025Sperrin 			}
1157*8654d025Sperrin 
1158*8654d025Sperrin 			if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1159*8654d025Sperrin 				if (seen_logs) {
1160*8654d025Sperrin 					(void) fprintf(stderr,
1161*8654d025Sperrin 					    gettext("invalid vdev "
1162*8654d025Sperrin 					    "specification: 'log' can be "
1163*8654d025Sperrin 					    "specified only once\n"));
1164*8654d025Sperrin 					return (NULL);
1165*8654d025Sperrin 				}
1166*8654d025Sperrin 				seen_logs = B_TRUE;
1167*8654d025Sperrin 				is_log = B_TRUE;
1168*8654d025Sperrin 				argc--;
1169*8654d025Sperrin 				argv++;
1170*8654d025Sperrin 				/*
1171*8654d025Sperrin 				 * A log is not a real grouping device.
1172*8654d025Sperrin 				 * We just set is_log and continue.
1173*8654d025Sperrin 				 */
1174*8654d025Sperrin 				continue;
1175*8654d025Sperrin 			}
1176*8654d025Sperrin 
1177*8654d025Sperrin 			if (is_log) {
1178*8654d025Sperrin 				if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1179*8654d025Sperrin 					(void) fprintf(stderr,
1180*8654d025Sperrin 					    gettext("invalid vdev "
1181*8654d025Sperrin 					    "specification: unsupported 'log' "
1182*8654d025Sperrin 					    "device: %s\n"), type);
1183*8654d025Sperrin 					return (NULL);
1184*8654d025Sperrin 				}
1185*8654d025Sperrin 				nlogs++;
118699653d4eSeschrock 			}
1187fa9e4066Sahrens 
1188fa9e4066Sahrens 			for (c = 1; c < argc; c++) {
118999653d4eSeschrock 				if (is_grouping(argv[c], NULL) != NULL)
1190fa9e4066Sahrens 					break;
1191fa9e4066Sahrens 				children++;
1192fa9e4066Sahrens 				child = realloc(child,
1193fa9e4066Sahrens 				    children * sizeof (nvlist_t *));
1194fa9e4066Sahrens 				if (child == NULL)
11955ad82045Snd 					zpool_no_memory();
1196*8654d025Sperrin 				if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1197*8654d025Sperrin 				    == NULL)
1198fa9e4066Sahrens 					return (NULL);
1199fa9e4066Sahrens 				child[children - 1] = nv;
1200fa9e4066Sahrens 			}
1201fa9e4066Sahrens 
120299653d4eSeschrock 			if (children < mindev) {
120399653d4eSeschrock 				(void) fprintf(stderr, gettext("invalid vdev "
120499653d4eSeschrock 				    "specification: %s requires at least %d "
120599653d4eSeschrock 				    "devices\n"), argv[0], mindev);
1206fa9e4066Sahrens 				return (NULL);
1207fa9e4066Sahrens 			}
1208fa9e4066Sahrens 
120999653d4eSeschrock 			argc -= c;
121099653d4eSeschrock 			argv += c;
121199653d4eSeschrock 
121299653d4eSeschrock 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
121399653d4eSeschrock 				spares = child;
121499653d4eSeschrock 				nspares = children;
121599653d4eSeschrock 				continue;
121699653d4eSeschrock 			} else {
121799653d4eSeschrock 				verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
121899653d4eSeschrock 				    0) == 0);
121999653d4eSeschrock 				verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
122099653d4eSeschrock 				    type) == 0);
1221*8654d025Sperrin 				verify(nvlist_add_uint64(nv,
1222*8654d025Sperrin 				    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
122399653d4eSeschrock 				if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
122499653d4eSeschrock 					verify(nvlist_add_uint64(nv,
122599653d4eSeschrock 					    ZPOOL_CONFIG_NPARITY,
122699653d4eSeschrock 					    mindev - 1) == 0);
122799653d4eSeschrock 				}
122899653d4eSeschrock 				verify(nvlist_add_nvlist_array(nv,
122999653d4eSeschrock 				    ZPOOL_CONFIG_CHILDREN, child,
123099653d4eSeschrock 				    children) == 0);
1231fa9e4066Sahrens 
123299653d4eSeschrock 				for (c = 0; c < children; c++)
123399653d4eSeschrock 					nvlist_free(child[c]);
123499653d4eSeschrock 				free(child);
123599653d4eSeschrock 			}
1236fa9e4066Sahrens 		} else {
1237fa9e4066Sahrens 			/*
1238fa9e4066Sahrens 			 * We have a device.  Pass off to make_leaf_vdev() to
1239fa9e4066Sahrens 			 * construct the appropriate nvlist describing the vdev.
1240fa9e4066Sahrens 			 */
1241*8654d025Sperrin 			if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1242fa9e4066Sahrens 				return (NULL);
1243*8654d025Sperrin 			if (is_log)
1244*8654d025Sperrin 				nlogs++;
1245fa9e4066Sahrens 			argc--;
1246fa9e4066Sahrens 			argv++;
1247fa9e4066Sahrens 		}
1248fa9e4066Sahrens 
1249fa9e4066Sahrens 		toplevels++;
1250fa9e4066Sahrens 		top = realloc(top, toplevels * sizeof (nvlist_t *));
1251fa9e4066Sahrens 		if (top == NULL)
12525ad82045Snd 			zpool_no_memory();
1253fa9e4066Sahrens 		top[toplevels - 1] = nv;
1254fa9e4066Sahrens 	}
1255fa9e4066Sahrens 
125699653d4eSeschrock 	if (toplevels == 0 && nspares == 0) {
125799653d4eSeschrock 		(void) fprintf(stderr, gettext("invalid vdev "
125899653d4eSeschrock 		    "specification: at least one toplevel vdev must be "
125999653d4eSeschrock 		    "specified\n"));
126099653d4eSeschrock 		return (NULL);
126199653d4eSeschrock 	}
126299653d4eSeschrock 
1263*8654d025Sperrin 	if (seen_logs && nlogs == 0) {
1264*8654d025Sperrin 		(void) fprintf(stderr, gettext("invalid vdev specification: "
1265*8654d025Sperrin 		    "log requires at least 1 device\n"));
1266*8654d025Sperrin 		return (NULL);
1267*8654d025Sperrin 	}
1268*8654d025Sperrin 
1269fa9e4066Sahrens 	/*
1270fa9e4066Sahrens 	 * Finally, create nvroot and add all top-level vdevs to it.
1271fa9e4066Sahrens 	 */
1272fa9e4066Sahrens 	verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1273fa9e4066Sahrens 	verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1274fa9e4066Sahrens 	    VDEV_TYPE_ROOT) == 0);
1275fa9e4066Sahrens 	verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1276fa9e4066Sahrens 	    top, toplevels) == 0);
127799653d4eSeschrock 	if (nspares != 0)
127899653d4eSeschrock 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
127999653d4eSeschrock 		    spares, nspares) == 0);
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	for (t = 0; t < toplevels; t++)
1282fa9e4066Sahrens 		nvlist_free(top[t]);
128399653d4eSeschrock 	for (t = 0; t < nspares; t++)
128499653d4eSeschrock 		nvlist_free(spares[t]);
128599653d4eSeschrock 	if (spares)
128699653d4eSeschrock 		free(spares);
1287fa9e4066Sahrens 	free(top);
1288fa9e4066Sahrens 
1289fa9e4066Sahrens 	return (nvroot);
1290fa9e4066Sahrens }
1291fa9e4066Sahrens 
12928488aeb5Staylor 
1293fa9e4066Sahrens /*
1294fa9e4066Sahrens  * Get and validate the contents of the given vdev specification.  This ensures
1295fa9e4066Sahrens  * that the nvlist returned is well-formed, that all the devices exist, and that
1296fa9e4066Sahrens  * they are not currently in use by any other known consumer.  The 'poolconfig'
1297fa9e4066Sahrens  * parameter is the current configuration of the pool when adding devices
1298fa9e4066Sahrens  * existing pool, and is used to perform additional checks, such as changing the
1299fa9e4066Sahrens  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1300fa9e4066Sahrens  * new pool.  The 'force' flag controls whether devices should be forcefully
1301fa9e4066Sahrens  * added, even if they appear in use.
1302fa9e4066Sahrens  */
1303fa9e4066Sahrens nvlist_t *
13048488aeb5Staylor make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
130599653d4eSeschrock     boolean_t isreplacing, int argc, char **argv)
1306fa9e4066Sahrens {
1307fa9e4066Sahrens 	nvlist_t *newroot;
13088488aeb5Staylor 	nvlist_t *poolconfig = NULL;
1309fa9e4066Sahrens 	is_force = force;
1310fa9e4066Sahrens 
1311fa9e4066Sahrens 	/*
1312fa9e4066Sahrens 	 * Construct the vdev specification.  If this is successful, we know
1313fa9e4066Sahrens 	 * that we have a valid specification, and that all devices can be
1314fa9e4066Sahrens 	 * opened.
1315fa9e4066Sahrens 	 */
1316fa9e4066Sahrens 	if ((newroot = construct_spec(argc, argv)) == NULL)
1317fa9e4066Sahrens 		return (NULL);
1318fa9e4066Sahrens 
13198488aeb5Staylor 	if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
13208488aeb5Staylor 		return (NULL);
13218488aeb5Staylor 
1322fa9e4066Sahrens 	/*
1323fa9e4066Sahrens 	 * Validate each device to make sure that its not shared with another
1324fa9e4066Sahrens 	 * subsystem.  We do this even if 'force' is set, because there are some
1325fa9e4066Sahrens 	 * uses (such as a dedicated dump device) that even '-f' cannot
1326fa9e4066Sahrens 	 * override.
1327fa9e4066Sahrens 	 */
132899653d4eSeschrock 	if (check_in_use(poolconfig, newroot, force, isreplacing,
132999653d4eSeschrock 	    B_FALSE) != 0) {
1330fa9e4066Sahrens 		nvlist_free(newroot);
1331fa9e4066Sahrens 		return (NULL);
1332fa9e4066Sahrens 	}
1333fa9e4066Sahrens 
1334fa9e4066Sahrens 	/*
1335fa9e4066Sahrens 	 * Check the replication level of the given vdevs and report any errors
1336fa9e4066Sahrens 	 * found.  We include the existing pool spec, if any, as we need to
1337fa9e4066Sahrens 	 * catch changes against the existing replication level.
1338fa9e4066Sahrens 	 */
1339fa9e4066Sahrens 	if (check_rep && check_replication(poolconfig, newroot) != 0) {
1340fa9e4066Sahrens 		nvlist_free(newroot);
1341fa9e4066Sahrens 		return (NULL);
1342fa9e4066Sahrens 	}
1343fa9e4066Sahrens 
1344fa9e4066Sahrens 	/*
1345fa9e4066Sahrens 	 * Run through the vdev specification and label any whole disks found.
1346fa9e4066Sahrens 	 */
13478488aeb5Staylor 	if (make_disks(zhp, newroot) != 0) {
1348fa9e4066Sahrens 		nvlist_free(newroot);
1349fa9e4066Sahrens 		return (NULL);
1350fa9e4066Sahrens 	}
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	return (newroot);
1353fa9e4066Sahrens }
1354