xref: /freebsd/sys/contrib/openzfs/cmd/zpool/zpool_vdev.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy  * See the License for the specific language governing permissions
12eda14cbcSMatt Macy  * and limitations under the License.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  * CDDL HEADER END
21eda14cbcSMatt Macy  */
22eda14cbcSMatt Macy 
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2016, 2017 Intel Corporation.
27eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy  * Functions to convert between a list of vdevs and an nvlist representing the
32eda14cbcSMatt Macy  * configuration.  Each entry in the list can be one of:
33eda14cbcSMatt Macy  *
34eda14cbcSMatt Macy  * 	Device vdevs
35eda14cbcSMatt Macy  * 		disk=(path=..., devid=...)
36eda14cbcSMatt Macy  * 		file=(path=...)
37eda14cbcSMatt Macy  *
38eda14cbcSMatt Macy  * 	Group vdevs
39eda14cbcSMatt Macy  * 		raidz[1|2]=(...)
40eda14cbcSMatt Macy  * 		mirror=(...)
41eda14cbcSMatt Macy  *
42eda14cbcSMatt Macy  * 	Hot spares
43eda14cbcSMatt Macy  *
44eda14cbcSMatt Macy  * While the underlying implementation supports it, group vdevs cannot contain
45eda14cbcSMatt Macy  * other group vdevs.  All userland verification of devices is contained within
46eda14cbcSMatt Macy  * this file.  If successful, the nvlist returned can be passed directly to the
47eda14cbcSMatt Macy  * kernel; we've done as much verification as possible in userland.
48eda14cbcSMatt Macy  *
49eda14cbcSMatt Macy  * Hot spares are a special case, and passed down as an array of disk vdevs, at
50eda14cbcSMatt Macy  * the same level as the root of the vdev tree.
51eda14cbcSMatt Macy  *
52eda14cbcSMatt Macy  * The only function exported by this file is 'make_root_vdev'.  The
53eda14cbcSMatt Macy  * function performs several passes:
54eda14cbcSMatt Macy  *
55eda14cbcSMatt Macy  * 	1. Construct the vdev specification.  Performs syntax validation and
56eda14cbcSMatt Macy  *         makes sure each device is valid.
57eda14cbcSMatt Macy  * 	2. Check for devices in use.  Using libblkid to make sure that no
58eda14cbcSMatt Macy  *         devices are also in use.  Some can be overridden using the 'force'
59eda14cbcSMatt Macy  *         flag, others cannot.
60eda14cbcSMatt Macy  * 	3. Check for replication errors if the 'force' flag is not specified.
61eda14cbcSMatt Macy  *         validates that the replication level is consistent across the
62eda14cbcSMatt Macy  *         entire pool.
63eda14cbcSMatt Macy  * 	4. Call libzfs to label any whole disks with an EFI label.
64eda14cbcSMatt Macy  */
65eda14cbcSMatt Macy 
66eda14cbcSMatt Macy #include <assert.h>
67eda14cbcSMatt Macy #include <ctype.h>
68eda14cbcSMatt Macy #include <errno.h>
69eda14cbcSMatt Macy #include <fcntl.h>
70eda14cbcSMatt Macy #include <libintl.h>
71eda14cbcSMatt Macy #include <libnvpair.h>
72eda14cbcSMatt Macy #include <libzutil.h>
73eda14cbcSMatt Macy #include <limits.h>
74eda14cbcSMatt Macy #include <sys/spa.h>
75eda14cbcSMatt Macy #include <stdio.h>
76eda14cbcSMatt Macy #include <string.h>
77eda14cbcSMatt Macy #include <unistd.h>
78eda14cbcSMatt Macy #include "zpool_util.h"
79eda14cbcSMatt Macy #include <sys/zfs_context.h>
80eda14cbcSMatt Macy #include <sys/stat.h>
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy /*
83eda14cbcSMatt Macy  * For any given vdev specification, we can have multiple errors.  The
84eda14cbcSMatt Macy  * vdev_error() function keeps track of whether we have seen an error yet, and
85eda14cbcSMatt Macy  * prints out a header if its the first error we've seen.
86eda14cbcSMatt Macy  */
87eda14cbcSMatt Macy boolean_t error_seen;
88eda14cbcSMatt Macy boolean_t is_force;
89eda14cbcSMatt Macy 
90eda14cbcSMatt Macy void
vdev_error(const char * fmt,...)91eda14cbcSMatt Macy vdev_error(const char *fmt, ...)
92eda14cbcSMatt Macy {
93eda14cbcSMatt Macy 	va_list ap;
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 	if (!error_seen) {
96eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid vdev specification\n"));
97eda14cbcSMatt Macy 		if (!is_force)
98eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("use '-f' to override "
99eda14cbcSMatt Macy 			    "the following errors:\n"));
100eda14cbcSMatt Macy 		else
101eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("the following errors "
102eda14cbcSMatt Macy 			    "must be manually repaired:\n"));
103eda14cbcSMatt Macy 		error_seen = B_TRUE;
104eda14cbcSMatt Macy 	}
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy 	va_start(ap, fmt);
107eda14cbcSMatt Macy 	(void) vfprintf(stderr, fmt, ap);
108eda14cbcSMatt Macy 	va_end(ap);
109eda14cbcSMatt Macy }
110eda14cbcSMatt Macy 
111eda14cbcSMatt Macy /*
112eda14cbcSMatt Macy  * Check that a file is valid.  All we can do in this case is check that it's
113eda14cbcSMatt Macy  * not in use by another pool, and not in use by swap.
114eda14cbcSMatt Macy  */
115eda14cbcSMatt Macy int
check_file_generic(const char * file,boolean_t force,boolean_t isspare)1161f88aa09SMartin Matuska check_file_generic(const char *file, boolean_t force, boolean_t isspare)
117eda14cbcSMatt Macy {
118eda14cbcSMatt Macy 	char  *name;
119eda14cbcSMatt Macy 	int fd;
120eda14cbcSMatt Macy 	int ret = 0;
121eda14cbcSMatt Macy 	pool_state_t state;
122eda14cbcSMatt Macy 	boolean_t inuse;
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	if ((fd = open(file, O_RDONLY)) < 0)
125eda14cbcSMatt Macy 		return (0);
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
128eda14cbcSMatt Macy 		const char *desc;
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy 		switch (state) {
131eda14cbcSMatt Macy 		case POOL_STATE_ACTIVE:
132eda14cbcSMatt Macy 			desc = gettext("active");
133eda14cbcSMatt Macy 			break;
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy 		case POOL_STATE_EXPORTED:
136eda14cbcSMatt Macy 			desc = gettext("exported");
137eda14cbcSMatt Macy 			break;
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy 		case POOL_STATE_POTENTIALLY_ACTIVE:
140eda14cbcSMatt Macy 			desc = gettext("potentially active");
141eda14cbcSMatt Macy 			break;
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy 		default:
144eda14cbcSMatt Macy 			desc = gettext("unknown");
145eda14cbcSMatt Macy 			break;
146eda14cbcSMatt Macy 		}
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy 		/*
149eda14cbcSMatt Macy 		 * Allow hot spares to be shared between pools.
150eda14cbcSMatt Macy 		 */
151eda14cbcSMatt Macy 		if (state == POOL_STATE_SPARE && isspare) {
152eda14cbcSMatt Macy 			free(name);
153eda14cbcSMatt Macy 			(void) close(fd);
154eda14cbcSMatt Macy 			return (0);
155eda14cbcSMatt Macy 		}
156eda14cbcSMatt Macy 
157eda14cbcSMatt Macy 		if (state == POOL_STATE_ACTIVE ||
158eda14cbcSMatt Macy 		    state == POOL_STATE_SPARE || !force) {
159eda14cbcSMatt Macy 			switch (state) {
160eda14cbcSMatt Macy 			case POOL_STATE_SPARE:
161eda14cbcSMatt Macy 				vdev_error(gettext("%s is reserved as a hot "
162eda14cbcSMatt Macy 				    "spare for pool %s\n"), file, name);
163eda14cbcSMatt Macy 				break;
164eda14cbcSMatt Macy 			default:
165eda14cbcSMatt Macy 				vdev_error(gettext("%s is part of %s pool "
166eda14cbcSMatt Macy 				    "'%s'\n"), file, desc, name);
167eda14cbcSMatt Macy 				break;
168eda14cbcSMatt Macy 			}
169eda14cbcSMatt Macy 			ret = -1;
170eda14cbcSMatt Macy 		}
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 		free(name);
173eda14cbcSMatt Macy 	}
174eda14cbcSMatt Macy 
175eda14cbcSMatt Macy 	(void) close(fd);
176eda14cbcSMatt Macy 	return (ret);
177eda14cbcSMatt Macy }
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy /*
180eda14cbcSMatt Macy  * This may be a shorthand device path or it could be total gibberish.
181eda14cbcSMatt Macy  * Check to see if it is a known device available in zfs_vdev_paths.
182eda14cbcSMatt Macy  * As part of this check, see if we've been given an entire disk
183eda14cbcSMatt Macy  * (minus the slice number).
184eda14cbcSMatt Macy  */
185eda14cbcSMatt Macy static int
is_shorthand_path(const char * arg,char * path,size_t path_size,struct stat64 * statbuf,boolean_t * wholedisk)186eda14cbcSMatt Macy is_shorthand_path(const char *arg, char *path, size_t path_size,
187eda14cbcSMatt Macy     struct stat64 *statbuf, boolean_t *wholedisk)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy 	int error;
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy 	error = zfs_resolve_shortname(arg, path, path_size);
192eda14cbcSMatt Macy 	if (error == 0) {
193eda14cbcSMatt Macy 		*wholedisk = zfs_dev_is_whole_disk(path);
194eda14cbcSMatt Macy 		if (*wholedisk || (stat64(path, statbuf) == 0))
195eda14cbcSMatt Macy 			return (0);
196eda14cbcSMatt Macy 	}
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	strlcpy(path, arg, path_size);
199eda14cbcSMatt Macy 	memset(statbuf, 0, sizeof (*statbuf));
200eda14cbcSMatt Macy 	*wholedisk = B_FALSE;
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy 	return (error);
203eda14cbcSMatt Macy }
204eda14cbcSMatt Macy 
205eda14cbcSMatt Macy /*
206eda14cbcSMatt Macy  * Determine if the given path is a hot spare within the given configuration.
207eda14cbcSMatt Macy  * If no configuration is given we rely solely on the label.
208eda14cbcSMatt Macy  */
209eda14cbcSMatt Macy static boolean_t
is_spare(nvlist_t * config,const char * path)210eda14cbcSMatt Macy is_spare(nvlist_t *config, const char *path)
211eda14cbcSMatt Macy {
212eda14cbcSMatt Macy 	int fd;
213eda14cbcSMatt Macy 	pool_state_t state;
214eda14cbcSMatt Macy 	char *name = NULL;
215eda14cbcSMatt Macy 	nvlist_t *label;
216eda14cbcSMatt Macy 	uint64_t guid, spareguid;
217eda14cbcSMatt Macy 	nvlist_t *nvroot;
218eda14cbcSMatt Macy 	nvlist_t **spares;
219eda14cbcSMatt Macy 	uint_t i, nspares;
220eda14cbcSMatt Macy 	boolean_t inuse;
221eda14cbcSMatt Macy 
2227877fdebSMatt Macy 	if (zpool_is_draid_spare(path))
2237877fdebSMatt Macy 		return (B_TRUE);
2247877fdebSMatt Macy 
225eda14cbcSMatt Macy 	if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
226eda14cbcSMatt Macy 		return (B_FALSE);
227eda14cbcSMatt Macy 
228eda14cbcSMatt Macy 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
229eda14cbcSMatt Macy 	    !inuse ||
230eda14cbcSMatt Macy 	    state != POOL_STATE_SPARE ||
231eda14cbcSMatt Macy 	    zpool_read_label(fd, &label, NULL) != 0) {
232eda14cbcSMatt Macy 		free(name);
233eda14cbcSMatt Macy 		(void) close(fd);
234eda14cbcSMatt Macy 		return (B_FALSE);
235eda14cbcSMatt Macy 	}
236eda14cbcSMatt Macy 	free(name);
237eda14cbcSMatt Macy 	(void) close(fd);
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 	if (config == NULL) {
240eda14cbcSMatt Macy 		nvlist_free(label);
241eda14cbcSMatt Macy 		return (B_TRUE);
242eda14cbcSMatt Macy 	}
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
245eda14cbcSMatt Macy 	nvlist_free(label);
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
248eda14cbcSMatt Macy 	    &nvroot) == 0);
249eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
250eda14cbcSMatt Macy 	    &spares, &nspares) == 0) {
251eda14cbcSMatt Macy 		for (i = 0; i < nspares; i++) {
252eda14cbcSMatt Macy 			verify(nvlist_lookup_uint64(spares[i],
253eda14cbcSMatt Macy 			    ZPOOL_CONFIG_GUID, &spareguid) == 0);
254eda14cbcSMatt Macy 			if (spareguid == guid)
255eda14cbcSMatt Macy 				return (B_TRUE);
256eda14cbcSMatt Macy 		}
257eda14cbcSMatt Macy 	}
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	return (B_FALSE);
260eda14cbcSMatt Macy }
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy /*
263eda14cbcSMatt Macy  * Create a leaf vdev.  Determine if this is a file or a device.  If it's a
264eda14cbcSMatt Macy  * device, fill in the device id to make a complete nvlist.  Valid forms for a
265eda14cbcSMatt Macy  * leaf vdev are:
266eda14cbcSMatt Macy  *
267eda14cbcSMatt Macy  *	/dev/xxx	Complete disk path
268eda14cbcSMatt Macy  *	/xxx		Full path to file
269eda14cbcSMatt Macy  *	xxx		Shorthand for <zfs_vdev_paths>/xxx
2707877fdebSMatt Macy  *	draid*		Virtual dRAID spare
271eda14cbcSMatt Macy  */
272eda14cbcSMatt Macy static nvlist_t *
make_leaf_vdev(nvlist_t * props,const char * arg,boolean_t is_primary)2737877fdebSMatt Macy make_leaf_vdev(nvlist_t *props, const char *arg, boolean_t is_primary)
274eda14cbcSMatt Macy {
275eda14cbcSMatt Macy 	char path[MAXPATHLEN];
276eda14cbcSMatt Macy 	struct stat64 statbuf;
277eda14cbcSMatt Macy 	nvlist_t *vdev = NULL;
278a0b956f5SMartin Matuska 	const char *type = NULL;
279eda14cbcSMatt Macy 	boolean_t wholedisk = B_FALSE;
280eda14cbcSMatt Macy 	uint64_t ashift = 0;
281eda14cbcSMatt Macy 	int err;
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	/*
284eda14cbcSMatt Macy 	 * Determine what type of vdev this is, and put the full path into
285eda14cbcSMatt Macy 	 * 'path'.  We detect whether this is a device of file afterwards by
286eda14cbcSMatt Macy 	 * checking the st_mode of the file.
287eda14cbcSMatt Macy 	 */
288eda14cbcSMatt Macy 	if (arg[0] == '/') {
289eda14cbcSMatt Macy 		/*
290eda14cbcSMatt Macy 		 * Complete device or file path.  Exact type is determined by
291eda14cbcSMatt Macy 		 * examining the file descriptor afterwards.  Symbolic links
292eda14cbcSMatt Macy 		 * are resolved to their real paths to determine whole disk
293eda14cbcSMatt Macy 		 * and S_ISBLK/S_ISREG type checks.  However, we are careful
294eda14cbcSMatt Macy 		 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
295eda14cbcSMatt Macy 		 * can leverage udev's persistent device labels.
296eda14cbcSMatt Macy 		 */
297eda14cbcSMatt Macy 		if (realpath(arg, path) == NULL) {
298eda14cbcSMatt Macy 			(void) fprintf(stderr,
299eda14cbcSMatt Macy 			    gettext("cannot resolve path '%s'\n"), arg);
300eda14cbcSMatt Macy 			return (NULL);
301eda14cbcSMatt Macy 		}
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 		wholedisk = zfs_dev_is_whole_disk(path);
304eda14cbcSMatt Macy 		if (!wholedisk && (stat64(path, &statbuf) != 0)) {
305eda14cbcSMatt Macy 			(void) fprintf(stderr,
306eda14cbcSMatt Macy 			    gettext("cannot open '%s': %s\n"),
307eda14cbcSMatt Macy 			    path, strerror(errno));
308eda14cbcSMatt Macy 			return (NULL);
309eda14cbcSMatt Macy 		}
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy 		/* After whole disk check restore original passed path */
312eda14cbcSMatt Macy 		strlcpy(path, arg, sizeof (path));
3137877fdebSMatt Macy 	} else if (zpool_is_draid_spare(arg)) {
3147877fdebSMatt Macy 		if (!is_primary) {
3157877fdebSMatt Macy 			(void) fprintf(stderr,
3167877fdebSMatt Macy 			    gettext("cannot open '%s': dRAID spares can only "
3177877fdebSMatt Macy 			    "be used to replace primary vdevs\n"), arg);
3187877fdebSMatt Macy 			return (NULL);
3197877fdebSMatt Macy 		}
3207877fdebSMatt Macy 
3217877fdebSMatt Macy 		wholedisk = B_TRUE;
3227877fdebSMatt Macy 		strlcpy(path, arg, sizeof (path));
3237877fdebSMatt Macy 		type = VDEV_TYPE_DRAID_SPARE;
324eda14cbcSMatt Macy 	} else {
325eda14cbcSMatt Macy 		err = is_shorthand_path(arg, path, sizeof (path),
326eda14cbcSMatt Macy 		    &statbuf, &wholedisk);
327eda14cbcSMatt Macy 		if (err != 0) {
328eda14cbcSMatt Macy 			/*
329eda14cbcSMatt Macy 			 * If we got ENOENT, then the user gave us
330eda14cbcSMatt Macy 			 * gibberish, so try to direct them with a
331eda14cbcSMatt Macy 			 * reasonable error message.  Otherwise,
332eda14cbcSMatt Macy 			 * regurgitate strerror() since it's the best we
333eda14cbcSMatt Macy 			 * can do.
334eda14cbcSMatt Macy 			 */
335eda14cbcSMatt Macy 			if (err == ENOENT) {
336eda14cbcSMatt Macy 				(void) fprintf(stderr,
337eda14cbcSMatt Macy 				    gettext("cannot open '%s': no such "
338eda14cbcSMatt Macy 				    "device in %s\n"), arg, DISK_ROOT);
339eda14cbcSMatt Macy 				(void) fprintf(stderr,
340eda14cbcSMatt Macy 				    gettext("must be a full path or "
341eda14cbcSMatt Macy 				    "shorthand device name\n"));
342eda14cbcSMatt Macy 				return (NULL);
343eda14cbcSMatt Macy 			} else {
344eda14cbcSMatt Macy 				(void) fprintf(stderr,
345eda14cbcSMatt Macy 				    gettext("cannot open '%s': %s\n"),
346eda14cbcSMatt Macy 				    path, strerror(errno));
347eda14cbcSMatt Macy 				return (NULL);
348eda14cbcSMatt Macy 			}
349eda14cbcSMatt Macy 		}
350eda14cbcSMatt Macy 	}
351eda14cbcSMatt Macy 
3527877fdebSMatt Macy 	if (type == NULL) {
353eda14cbcSMatt Macy 		/*
354eda14cbcSMatt Macy 		 * Determine whether this is a device or a file.
355eda14cbcSMatt Macy 		 */
356eda14cbcSMatt Macy 		if (wholedisk || S_ISBLK(statbuf.st_mode)) {
357eda14cbcSMatt Macy 			type = VDEV_TYPE_DISK;
358eda14cbcSMatt Macy 		} else if (S_ISREG(statbuf.st_mode)) {
359eda14cbcSMatt Macy 			type = VDEV_TYPE_FILE;
360eda14cbcSMatt Macy 		} else {
3617877fdebSMatt Macy 			fprintf(stderr, gettext("cannot use '%s': must "
3627877fdebSMatt Macy 			    "be a block device or regular file\n"), path);
363eda14cbcSMatt Macy 			return (NULL);
364eda14cbcSMatt Macy 		}
3657877fdebSMatt Macy 	}
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 	/*
368eda14cbcSMatt Macy 	 * Finally, we have the complete device or file, and we know that it is
369eda14cbcSMatt Macy 	 * acceptable to use.  Construct the nvlist to describe this vdev.  All
370eda14cbcSMatt Macy 	 * vdevs have a 'path' element, and devices also have a 'devid' element.
371eda14cbcSMatt Macy 	 */
372eda14cbcSMatt Macy 	verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
373eda14cbcSMatt Macy 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
374eda14cbcSMatt Macy 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
3757877fdebSMatt Macy 
37614c2e0a0SMartin Matuska 	/* Lookup and add the enclosure sysfs path (if exists) */
37714c2e0a0SMartin Matuska 	update_vdev_config_dev_sysfs_path(vdev, path,
37814c2e0a0SMartin Matuska 	    ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
37914c2e0a0SMartin Matuska 
380eda14cbcSMatt Macy 	if (strcmp(type, VDEV_TYPE_DISK) == 0)
381eda14cbcSMatt Macy 		verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
382eda14cbcSMatt Macy 		    (uint64_t)wholedisk) == 0);
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 	/*
385eda14cbcSMatt Macy 	 * Override defaults if custom properties are provided.
386eda14cbcSMatt Macy 	 */
387eda14cbcSMatt Macy 	if (props != NULL) {
3882a58b312SMartin Matuska 		const char *value = NULL;
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 		if (nvlist_lookup_string(props,
391eda14cbcSMatt Macy 		    zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0) {
392eda14cbcSMatt Macy 			if (zfs_nicestrtonum(NULL, value, &ashift) != 0) {
393eda14cbcSMatt Macy 				(void) fprintf(stderr,
394eda14cbcSMatt Macy 				    gettext("ashift must be a number.\n"));
395eda14cbcSMatt Macy 				return (NULL);
396eda14cbcSMatt Macy 			}
397eda14cbcSMatt Macy 			if (ashift != 0 &&
398eda14cbcSMatt Macy 			    (ashift < ASHIFT_MIN || ashift > ASHIFT_MAX)) {
399eda14cbcSMatt Macy 				(void) fprintf(stderr,
400eda14cbcSMatt Macy 				    gettext("invalid 'ashift=%" PRIu64 "' "
401eda14cbcSMatt Macy 				    "property: only values between %" PRId32 " "
402eda14cbcSMatt Macy 				    "and %" PRId32 " are allowed.\n"),
403eda14cbcSMatt Macy 				    ashift, ASHIFT_MIN, ASHIFT_MAX);
404eda14cbcSMatt Macy 				return (NULL);
405eda14cbcSMatt Macy 			}
406eda14cbcSMatt Macy 		}
407eda14cbcSMatt Macy 	}
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy 	/*
410eda14cbcSMatt Macy 	 * If the device is known to incorrectly report its physical sector
411eda14cbcSMatt Macy 	 * size explicitly provide the known correct value.
412eda14cbcSMatt Macy 	 */
413eda14cbcSMatt Macy 	if (ashift == 0) {
414eda14cbcSMatt Macy 		int sector_size;
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 		if (check_sector_size_database(path, &sector_size) == B_TRUE)
417eda14cbcSMatt Macy 			ashift = highbit64(sector_size) - 1;
418eda14cbcSMatt Macy 	}
419eda14cbcSMatt Macy 
420eda14cbcSMatt Macy 	if (ashift > 0)
421eda14cbcSMatt Macy 		(void) nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 	return (vdev);
424eda14cbcSMatt Macy }
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy /*
427eda14cbcSMatt Macy  * Go through and verify the replication level of the pool is consistent.
428eda14cbcSMatt Macy  * Performs the following checks:
429eda14cbcSMatt Macy  *
430eda14cbcSMatt Macy  * 	For the new spec, verifies that devices in mirrors and raidz are the
431eda14cbcSMatt Macy  * 	same size.
432eda14cbcSMatt Macy  *
433eda14cbcSMatt Macy  * 	If the current configuration already has inconsistent replication
434eda14cbcSMatt Macy  * 	levels, ignore any other potential problems in the new spec.
435eda14cbcSMatt Macy  *
436eda14cbcSMatt Macy  * 	Otherwise, make sure that the current spec (if there is one) and the new
437eda14cbcSMatt Macy  * 	spec have consistent replication levels.
438eda14cbcSMatt Macy  *
439eda14cbcSMatt Macy  *	If there is no current spec (create), make sure new spec has at least
440eda14cbcSMatt Macy  *	one general purpose vdev.
441eda14cbcSMatt Macy  */
442eda14cbcSMatt Macy typedef struct replication_level {
4432a58b312SMartin Matuska 	const char *zprl_type;
444eda14cbcSMatt Macy 	uint64_t zprl_children;
445eda14cbcSMatt Macy 	uint64_t zprl_parity;
446eda14cbcSMatt Macy } replication_level_t;
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy #define	ZPOOL_FUZZ	(16 * 1024 * 1024)
449eda14cbcSMatt Macy 
4507877fdebSMatt Macy /*
4517877fdebSMatt Macy  * N.B. For the purposes of comparing replication levels dRAID can be
45216038816SMartin Matuska  * considered functionally equivalent to raidz.
4537877fdebSMatt Macy  */
454eda14cbcSMatt Macy static boolean_t
is_raidz_mirror(replication_level_t * a,replication_level_t * b,replication_level_t ** raidz,replication_level_t ** mirror)455eda14cbcSMatt Macy is_raidz_mirror(replication_level_t *a, replication_level_t *b,
456eda14cbcSMatt Macy     replication_level_t **raidz, replication_level_t **mirror)
457eda14cbcSMatt Macy {
4587877fdebSMatt Macy 	if ((strcmp(a->zprl_type, "raidz") == 0 ||
4597877fdebSMatt Macy 	    strcmp(a->zprl_type, "draid") == 0) &&
460eda14cbcSMatt Macy 	    strcmp(b->zprl_type, "mirror") == 0) {
461eda14cbcSMatt Macy 		*raidz = a;
462eda14cbcSMatt Macy 		*mirror = b;
463eda14cbcSMatt Macy 		return (B_TRUE);
464eda14cbcSMatt Macy 	}
465eda14cbcSMatt Macy 	return (B_FALSE);
466eda14cbcSMatt Macy }
467eda14cbcSMatt Macy 
468eda14cbcSMatt Macy /*
4697877fdebSMatt Macy  * Comparison for determining if dRAID and raidz where passed in either order.
4707877fdebSMatt Macy  */
4717877fdebSMatt Macy static boolean_t
is_raidz_draid(replication_level_t * a,replication_level_t * b)4727877fdebSMatt Macy is_raidz_draid(replication_level_t *a, replication_level_t *b)
4737877fdebSMatt Macy {
4747877fdebSMatt Macy 	if ((strcmp(a->zprl_type, "raidz") == 0 ||
4757877fdebSMatt Macy 	    strcmp(a->zprl_type, "draid") == 0) &&
4767877fdebSMatt Macy 	    (strcmp(b->zprl_type, "raidz") == 0 ||
4777877fdebSMatt Macy 	    strcmp(b->zprl_type, "draid") == 0)) {
4787877fdebSMatt Macy 		return (B_TRUE);
4797877fdebSMatt Macy 	}
4807877fdebSMatt Macy 
4817877fdebSMatt Macy 	return (B_FALSE);
4827877fdebSMatt Macy }
4837877fdebSMatt Macy 
4847877fdebSMatt Macy /*
485eda14cbcSMatt Macy  * Given a list of toplevel vdevs, return the current replication level.  If
486eda14cbcSMatt Macy  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
487eda14cbcSMatt Macy  * an error message will be displayed for each self-inconsistent vdev.
488eda14cbcSMatt Macy  */
489eda14cbcSMatt Macy static replication_level_t *
get_replication(nvlist_t * nvroot,boolean_t fatal)490eda14cbcSMatt Macy get_replication(nvlist_t *nvroot, boolean_t fatal)
491eda14cbcSMatt Macy {
492eda14cbcSMatt Macy 	nvlist_t **top;
493eda14cbcSMatt Macy 	uint_t t, toplevels;
494eda14cbcSMatt Macy 	nvlist_t **child;
495eda14cbcSMatt Macy 	uint_t c, children;
496eda14cbcSMatt Macy 	nvlist_t *nv;
4972a58b312SMartin Matuska 	const char *type;
498eda14cbcSMatt Macy 	replication_level_t lastrep = {0};
499eda14cbcSMatt Macy 	replication_level_t rep;
500eda14cbcSMatt Macy 	replication_level_t *ret;
501eda14cbcSMatt Macy 	replication_level_t *raidz, *mirror;
502eda14cbcSMatt Macy 	boolean_t dontreport;
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 	ret = safe_malloc(sizeof (replication_level_t));
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
507eda14cbcSMatt Macy 	    &top, &toplevels) == 0);
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy 	for (t = 0; t < toplevels; t++) {
510eda14cbcSMatt Macy 		uint64_t is_log = B_FALSE;
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 		nv = top[t];
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 		/*
515eda14cbcSMatt Macy 		 * For separate logs we ignore the top level vdev replication
516eda14cbcSMatt Macy 		 * constraints.
517eda14cbcSMatt Macy 		 */
518eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
519eda14cbcSMatt Macy 		if (is_log)
520eda14cbcSMatt Macy 			continue;
521eda14cbcSMatt Macy 
522271171e0SMartin Matuska 		/*
523271171e0SMartin Matuska 		 * Ignore holes introduced by removing aux devices, along
524271171e0SMartin Matuska 		 * with indirect vdevs introduced by previously removed
525271171e0SMartin Matuska 		 * vdevs.
526271171e0SMartin Matuska 		 */
527eda14cbcSMatt Macy 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
528271171e0SMartin Matuska 		if (strcmp(type, VDEV_TYPE_HOLE) == 0 ||
529271171e0SMartin Matuska 		    strcmp(type, VDEV_TYPE_INDIRECT) == 0)
530eda14cbcSMatt Macy 			continue;
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
533eda14cbcSMatt Macy 		    &child, &children) != 0) {
534eda14cbcSMatt Macy 			/*
535eda14cbcSMatt Macy 			 * This is a 'file' or 'disk' vdev.
536eda14cbcSMatt Macy 			 */
537eda14cbcSMatt Macy 			rep.zprl_type = type;
538eda14cbcSMatt Macy 			rep.zprl_children = 1;
539eda14cbcSMatt Macy 			rep.zprl_parity = 0;
540eda14cbcSMatt Macy 		} else {
541eda14cbcSMatt Macy 			int64_t vdev_size;
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 			/*
544eda14cbcSMatt Macy 			 * This is a mirror or RAID-Z vdev.  Go through and make
545eda14cbcSMatt Macy 			 * sure the contents are all the same (files vs. disks),
546eda14cbcSMatt Macy 			 * keeping track of the number of elements in the
547eda14cbcSMatt Macy 			 * process.
548eda14cbcSMatt Macy 			 *
549eda14cbcSMatt Macy 			 * We also check that the size of each vdev (if it can
550eda14cbcSMatt Macy 			 * be determined) is the same.
551eda14cbcSMatt Macy 			 */
552eda14cbcSMatt Macy 			rep.zprl_type = type;
553eda14cbcSMatt Macy 			rep.zprl_children = 0;
554eda14cbcSMatt Macy 
5557877fdebSMatt Macy 			if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
5567877fdebSMatt Macy 			    strcmp(type, VDEV_TYPE_DRAID) == 0) {
557eda14cbcSMatt Macy 				verify(nvlist_lookup_uint64(nv,
558eda14cbcSMatt Macy 				    ZPOOL_CONFIG_NPARITY,
559eda14cbcSMatt Macy 				    &rep.zprl_parity) == 0);
560eda14cbcSMatt Macy 				assert(rep.zprl_parity != 0);
561eda14cbcSMatt Macy 			} else {
562eda14cbcSMatt Macy 				rep.zprl_parity = 0;
563eda14cbcSMatt Macy 			}
564eda14cbcSMatt Macy 
565eda14cbcSMatt Macy 			/*
566eda14cbcSMatt Macy 			 * The 'dontreport' variable indicates that we've
567eda14cbcSMatt Macy 			 * already reported an error for this spec, so don't
568eda14cbcSMatt Macy 			 * bother doing it again.
569eda14cbcSMatt Macy 			 */
570eda14cbcSMatt Macy 			type = NULL;
571eda14cbcSMatt Macy 			dontreport = 0;
572eda14cbcSMatt Macy 			vdev_size = -1LL;
573eda14cbcSMatt Macy 			for (c = 0; c < children; c++) {
574eda14cbcSMatt Macy 				nvlist_t *cnv = child[c];
5752a58b312SMartin Matuska 				const char *path;
576eda14cbcSMatt Macy 				struct stat64 statbuf;
577eda14cbcSMatt Macy 				int64_t size = -1LL;
5782a58b312SMartin Matuska 				const char *childtype;
579eda14cbcSMatt Macy 				int fd, err;
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy 				rep.zprl_children++;
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 				verify(nvlist_lookup_string(cnv,
584eda14cbcSMatt Macy 				    ZPOOL_CONFIG_TYPE, &childtype) == 0);
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy 				/*
587eda14cbcSMatt Macy 				 * If this is a replacing or spare vdev, then
588eda14cbcSMatt Macy 				 * get the real first child of the vdev: do this
589eda14cbcSMatt Macy 				 * in a loop because replacing and spare vdevs
590eda14cbcSMatt Macy 				 * can be nested.
591eda14cbcSMatt Macy 				 */
592eda14cbcSMatt Macy 				while (strcmp(childtype,
593eda14cbcSMatt Macy 				    VDEV_TYPE_REPLACING) == 0 ||
594eda14cbcSMatt Macy 				    strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
595eda14cbcSMatt Macy 					nvlist_t **rchild;
596eda14cbcSMatt Macy 					uint_t rchildren;
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy 					verify(nvlist_lookup_nvlist_array(cnv,
599eda14cbcSMatt Macy 					    ZPOOL_CONFIG_CHILDREN, &rchild,
600eda14cbcSMatt Macy 					    &rchildren) == 0);
601eda14cbcSMatt Macy 					assert(rchildren == 2);
602eda14cbcSMatt Macy 					cnv = rchild[0];
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 					verify(nvlist_lookup_string(cnv,
605eda14cbcSMatt Macy 					    ZPOOL_CONFIG_TYPE,
606eda14cbcSMatt Macy 					    &childtype) == 0);
607eda14cbcSMatt Macy 				}
608eda14cbcSMatt Macy 
609eda14cbcSMatt Macy 				verify(nvlist_lookup_string(cnv,
610eda14cbcSMatt Macy 				    ZPOOL_CONFIG_PATH, &path) == 0);
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 				/*
613eda14cbcSMatt Macy 				 * If we have a raidz/mirror that combines disks
614eda14cbcSMatt Macy 				 * with files, report it as an error.
615eda14cbcSMatt Macy 				 */
616eda14cbcSMatt Macy 				if (!dontreport && type != NULL &&
617eda14cbcSMatt Macy 				    strcmp(type, childtype) != 0) {
618eda14cbcSMatt Macy 					if (ret != NULL)
619eda14cbcSMatt Macy 						free(ret);
620eda14cbcSMatt Macy 					ret = NULL;
621eda14cbcSMatt Macy 					if (fatal)
622eda14cbcSMatt Macy 						vdev_error(gettext(
623eda14cbcSMatt Macy 						    "mismatched replication "
624eda14cbcSMatt Macy 						    "level: %s contains both "
625eda14cbcSMatt Macy 						    "files and devices\n"),
626eda14cbcSMatt Macy 						    rep.zprl_type);
627eda14cbcSMatt Macy 					else
628eda14cbcSMatt Macy 						return (NULL);
629eda14cbcSMatt Macy 					dontreport = B_TRUE;
630eda14cbcSMatt Macy 				}
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy 				/*
633eda14cbcSMatt Macy 				 * According to stat(2), the value of 'st_size'
634eda14cbcSMatt Macy 				 * is undefined for block devices and character
635eda14cbcSMatt Macy 				 * devices.  But there is no effective way to
636eda14cbcSMatt Macy 				 * determine the real size in userland.
637eda14cbcSMatt Macy 				 *
638eda14cbcSMatt Macy 				 * Instead, we'll take advantage of an
639eda14cbcSMatt Macy 				 * implementation detail of spec_size().  If the
640eda14cbcSMatt Macy 				 * device is currently open, then we (should)
641eda14cbcSMatt Macy 				 * return a valid size.
642eda14cbcSMatt Macy 				 *
643eda14cbcSMatt Macy 				 * If we still don't get a valid size (indicated
644eda14cbcSMatt Macy 				 * by a size of 0 or MAXOFFSET_T), then ignore
645eda14cbcSMatt Macy 				 * this device altogether.
646eda14cbcSMatt Macy 				 */
647eda14cbcSMatt Macy 				if ((fd = open(path, O_RDONLY)) >= 0) {
648eda14cbcSMatt Macy 					err = fstat64_blk(fd, &statbuf);
649eda14cbcSMatt Macy 					(void) close(fd);
650eda14cbcSMatt Macy 				} else {
651eda14cbcSMatt Macy 					err = stat64(path, &statbuf);
652eda14cbcSMatt Macy 				}
653eda14cbcSMatt Macy 
654eda14cbcSMatt Macy 				if (err != 0 ||
655eda14cbcSMatt Macy 				    statbuf.st_size == 0 ||
656eda14cbcSMatt Macy 				    statbuf.st_size == MAXOFFSET_T)
657eda14cbcSMatt Macy 					continue;
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy 				size = statbuf.st_size;
660eda14cbcSMatt Macy 
661eda14cbcSMatt Macy 				/*
662eda14cbcSMatt Macy 				 * Also make sure that devices and
663eda14cbcSMatt Macy 				 * slices have a consistent size.  If
664eda14cbcSMatt Macy 				 * they differ by a significant amount
665eda14cbcSMatt Macy 				 * (~16MB) then report an error.
666eda14cbcSMatt Macy 				 */
667eda14cbcSMatt Macy 				if (!dontreport &&
668eda14cbcSMatt Macy 				    (vdev_size != -1LL &&
669eda14cbcSMatt Macy 				    (llabs(size - vdev_size) >
670eda14cbcSMatt Macy 				    ZPOOL_FUZZ))) {
671eda14cbcSMatt Macy 					if (ret != NULL)
672eda14cbcSMatt Macy 						free(ret);
673eda14cbcSMatt Macy 					ret = NULL;
674eda14cbcSMatt Macy 					if (fatal)
675eda14cbcSMatt Macy 						vdev_error(gettext(
676eda14cbcSMatt Macy 						    "%s contains devices of "
677eda14cbcSMatt Macy 						    "different sizes\n"),
678eda14cbcSMatt Macy 						    rep.zprl_type);
679eda14cbcSMatt Macy 					else
680eda14cbcSMatt Macy 						return (NULL);
681eda14cbcSMatt Macy 					dontreport = B_TRUE;
682eda14cbcSMatt Macy 				}
683eda14cbcSMatt Macy 
684eda14cbcSMatt Macy 				type = childtype;
685eda14cbcSMatt Macy 				vdev_size = size;
686eda14cbcSMatt Macy 			}
687eda14cbcSMatt Macy 		}
688eda14cbcSMatt Macy 
689eda14cbcSMatt Macy 		/*
690eda14cbcSMatt Macy 		 * At this point, we have the replication of the last toplevel
691eda14cbcSMatt Macy 		 * vdev in 'rep'.  Compare it to 'lastrep' to see if it is
692eda14cbcSMatt Macy 		 * different.
693eda14cbcSMatt Macy 		 */
694eda14cbcSMatt Macy 		if (lastrep.zprl_type != NULL) {
695eda14cbcSMatt Macy 			if (is_raidz_mirror(&lastrep, &rep, &raidz, &mirror) ||
696eda14cbcSMatt Macy 			    is_raidz_mirror(&rep, &lastrep, &raidz, &mirror)) {
697eda14cbcSMatt Macy 				/*
698eda14cbcSMatt Macy 				 * Accepted raidz and mirror when they can
699eda14cbcSMatt Macy 				 * handle the same number of disk failures.
700eda14cbcSMatt Macy 				 */
701eda14cbcSMatt Macy 				if (raidz->zprl_parity !=
702eda14cbcSMatt Macy 				    mirror->zprl_children - 1) {
703eda14cbcSMatt Macy 					if (ret != NULL)
704eda14cbcSMatt Macy 						free(ret);
705eda14cbcSMatt Macy 					ret = NULL;
706eda14cbcSMatt Macy 					if (fatal)
707eda14cbcSMatt Macy 						vdev_error(gettext(
708eda14cbcSMatt Macy 						    "mismatched replication "
709eda14cbcSMatt Macy 						    "level: "
710eda14cbcSMatt Macy 						    "%s and %s vdevs with "
711eda14cbcSMatt Macy 						    "different redundancy, "
712eda14cbcSMatt Macy 						    "%llu vs. %llu (%llu-way) "
713eda14cbcSMatt Macy 						    "are present\n"),
714eda14cbcSMatt Macy 						    raidz->zprl_type,
715eda14cbcSMatt Macy 						    mirror->zprl_type,
7161f88aa09SMartin Matuska 						    (u_longlong_t)
717eda14cbcSMatt Macy 						    raidz->zprl_parity,
7181f88aa09SMartin Matuska 						    (u_longlong_t)
719eda14cbcSMatt Macy 						    mirror->zprl_children - 1,
7201f88aa09SMartin Matuska 						    (u_longlong_t)
721eda14cbcSMatt Macy 						    mirror->zprl_children);
722eda14cbcSMatt Macy 					else
723eda14cbcSMatt Macy 						return (NULL);
724eda14cbcSMatt Macy 				}
7257877fdebSMatt Macy 			} else if (is_raidz_draid(&lastrep, &rep)) {
7267877fdebSMatt Macy 				/*
7277877fdebSMatt Macy 				 * Accepted raidz and draid when they can
7287877fdebSMatt Macy 				 * handle the same number of disk failures.
7297877fdebSMatt Macy 				 */
7307877fdebSMatt Macy 				if (lastrep.zprl_parity != rep.zprl_parity) {
7317877fdebSMatt Macy 					if (ret != NULL)
7327877fdebSMatt Macy 						free(ret);
7337877fdebSMatt Macy 					ret = NULL;
7347877fdebSMatt Macy 					if (fatal)
7357877fdebSMatt Macy 						vdev_error(gettext(
7367877fdebSMatt Macy 						    "mismatched replication "
7377877fdebSMatt Macy 						    "level: %s and %s vdevs "
7387877fdebSMatt Macy 						    "with different "
7397877fdebSMatt Macy 						    "redundancy, %llu vs. "
7407877fdebSMatt Macy 						    "%llu are present\n"),
7417877fdebSMatt Macy 						    lastrep.zprl_type,
7427877fdebSMatt Macy 						    rep.zprl_type,
7431f88aa09SMartin Matuska 						    (u_longlong_t)
7447877fdebSMatt Macy 						    lastrep.zprl_parity,
7451f88aa09SMartin Matuska 						    (u_longlong_t)
7467877fdebSMatt Macy 						    rep.zprl_parity);
7477877fdebSMatt Macy 					else
7487877fdebSMatt Macy 						return (NULL);
7497877fdebSMatt Macy 				}
750eda14cbcSMatt Macy 			} else if (strcmp(lastrep.zprl_type, rep.zprl_type) !=
751eda14cbcSMatt Macy 			    0) {
752eda14cbcSMatt Macy 				if (ret != NULL)
753eda14cbcSMatt Macy 					free(ret);
754eda14cbcSMatt Macy 				ret = NULL;
755eda14cbcSMatt Macy 				if (fatal)
756eda14cbcSMatt Macy 					vdev_error(gettext(
757eda14cbcSMatt Macy 					    "mismatched replication level: "
758eda14cbcSMatt Macy 					    "both %s and %s vdevs are "
759eda14cbcSMatt Macy 					    "present\n"),
760eda14cbcSMatt Macy 					    lastrep.zprl_type, rep.zprl_type);
761eda14cbcSMatt Macy 				else
762eda14cbcSMatt Macy 					return (NULL);
763eda14cbcSMatt Macy 			} else if (lastrep.zprl_parity != rep.zprl_parity) {
764eda14cbcSMatt Macy 				if (ret)
765eda14cbcSMatt Macy 					free(ret);
766eda14cbcSMatt Macy 				ret = NULL;
767eda14cbcSMatt Macy 				if (fatal)
768eda14cbcSMatt Macy 					vdev_error(gettext(
769eda14cbcSMatt Macy 					    "mismatched replication level: "
770eda14cbcSMatt Macy 					    "both %llu and %llu device parity "
771eda14cbcSMatt Macy 					    "%s vdevs are present\n"),
7721f88aa09SMartin Matuska 					    (u_longlong_t)
773eda14cbcSMatt Macy 					    lastrep.zprl_parity,
7741f88aa09SMartin Matuska 					    (u_longlong_t)rep.zprl_parity,
775eda14cbcSMatt Macy 					    rep.zprl_type);
776eda14cbcSMatt Macy 				else
777eda14cbcSMatt Macy 					return (NULL);
778eda14cbcSMatt Macy 			} else if (lastrep.zprl_children != rep.zprl_children) {
779eda14cbcSMatt Macy 				if (ret)
780eda14cbcSMatt Macy 					free(ret);
781eda14cbcSMatt Macy 				ret = NULL;
782eda14cbcSMatt Macy 				if (fatal)
783eda14cbcSMatt Macy 					vdev_error(gettext(
784eda14cbcSMatt Macy 					    "mismatched replication level: "
785eda14cbcSMatt Macy 					    "both %llu-way and %llu-way %s "
786eda14cbcSMatt Macy 					    "vdevs are present\n"),
7871f88aa09SMartin Matuska 					    (u_longlong_t)
788eda14cbcSMatt Macy 					    lastrep.zprl_children,
7891f88aa09SMartin Matuska 					    (u_longlong_t)
790eda14cbcSMatt Macy 					    rep.zprl_children,
791eda14cbcSMatt Macy 					    rep.zprl_type);
792eda14cbcSMatt Macy 				else
793eda14cbcSMatt Macy 					return (NULL);
794eda14cbcSMatt Macy 			}
795eda14cbcSMatt Macy 		}
796eda14cbcSMatt Macy 		lastrep = rep;
797eda14cbcSMatt Macy 	}
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 	if (ret != NULL)
800eda14cbcSMatt Macy 		*ret = rep;
801eda14cbcSMatt Macy 
802eda14cbcSMatt Macy 	return (ret);
803eda14cbcSMatt Macy }
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy /*
806eda14cbcSMatt Macy  * Check the replication level of the vdev spec against the current pool.  Calls
807eda14cbcSMatt Macy  * get_replication() to make sure the new spec is self-consistent.  If the pool
808eda14cbcSMatt Macy  * has a consistent replication level, then we ignore any errors.  Otherwise,
809eda14cbcSMatt Macy  * report any difference between the two.
810eda14cbcSMatt Macy  */
811eda14cbcSMatt Macy static int
check_replication(nvlist_t * config,nvlist_t * newroot)812eda14cbcSMatt Macy check_replication(nvlist_t *config, nvlist_t *newroot)
813eda14cbcSMatt Macy {
814eda14cbcSMatt Macy 	nvlist_t **child;
815eda14cbcSMatt Macy 	uint_t	children;
816eda14cbcSMatt Macy 	replication_level_t *current = NULL, *new;
817eda14cbcSMatt Macy 	replication_level_t *raidz, *mirror;
818eda14cbcSMatt Macy 	int ret;
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	/*
821eda14cbcSMatt Macy 	 * If we have a current pool configuration, check to see if it's
822eda14cbcSMatt Macy 	 * self-consistent.  If not, simply return success.
823eda14cbcSMatt Macy 	 */
824eda14cbcSMatt Macy 	if (config != NULL) {
825eda14cbcSMatt Macy 		nvlist_t *nvroot;
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
828eda14cbcSMatt Macy 		    &nvroot) == 0);
829eda14cbcSMatt Macy 		if ((current = get_replication(nvroot, B_FALSE)) == NULL)
830eda14cbcSMatt Macy 			return (0);
831eda14cbcSMatt Macy 	}
832eda14cbcSMatt Macy 	/*
833eda14cbcSMatt Macy 	 * for spares there may be no children, and therefore no
834eda14cbcSMatt Macy 	 * replication level to check
835eda14cbcSMatt Macy 	 */
836eda14cbcSMatt Macy 	if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
837eda14cbcSMatt Macy 	    &child, &children) != 0) || (children == 0)) {
838eda14cbcSMatt Macy 		free(current);
839eda14cbcSMatt Macy 		return (0);
840eda14cbcSMatt Macy 	}
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy 	/*
843eda14cbcSMatt Macy 	 * If all we have is logs then there's no replication level to check.
844eda14cbcSMatt Macy 	 */
845eda14cbcSMatt Macy 	if (num_logs(newroot) == children) {
846eda14cbcSMatt Macy 		free(current);
847eda14cbcSMatt Macy 		return (0);
848eda14cbcSMatt Macy 	}
849eda14cbcSMatt Macy 
850eda14cbcSMatt Macy 	/*
851eda14cbcSMatt Macy 	 * Get the replication level of the new vdev spec, reporting any
852eda14cbcSMatt Macy 	 * inconsistencies found.
853eda14cbcSMatt Macy 	 */
854eda14cbcSMatt Macy 	if ((new = get_replication(newroot, B_TRUE)) == NULL) {
855eda14cbcSMatt Macy 		free(current);
856eda14cbcSMatt Macy 		return (-1);
857eda14cbcSMatt Macy 	}
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy 	/*
860eda14cbcSMatt Macy 	 * Check to see if the new vdev spec matches the replication level of
861eda14cbcSMatt Macy 	 * the current pool.
862eda14cbcSMatt Macy 	 */
863eda14cbcSMatt Macy 	ret = 0;
864eda14cbcSMatt Macy 	if (current != NULL) {
865eda14cbcSMatt Macy 		if (is_raidz_mirror(current, new, &raidz, &mirror) ||
866eda14cbcSMatt Macy 		    is_raidz_mirror(new, current, &raidz, &mirror)) {
867eda14cbcSMatt Macy 			if (raidz->zprl_parity != mirror->zprl_children - 1) {
868eda14cbcSMatt Macy 				vdev_error(gettext(
869eda14cbcSMatt Macy 				    "mismatched replication level: pool and "
870eda14cbcSMatt Macy 				    "new vdev with different redundancy, %s "
871eda14cbcSMatt Macy 				    "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
872eda14cbcSMatt Macy 				    raidz->zprl_type,
873eda14cbcSMatt Macy 				    mirror->zprl_type,
8741f88aa09SMartin Matuska 				    (u_longlong_t)raidz->zprl_parity,
8751f88aa09SMartin Matuska 				    (u_longlong_t)mirror->zprl_children - 1,
8761f88aa09SMartin Matuska 				    (u_longlong_t)mirror->zprl_children);
877eda14cbcSMatt Macy 				ret = -1;
878eda14cbcSMatt Macy 			}
879eda14cbcSMatt Macy 		} else if (strcmp(current->zprl_type, new->zprl_type) != 0) {
880eda14cbcSMatt Macy 			vdev_error(gettext(
881eda14cbcSMatt Macy 			    "mismatched replication level: pool uses %s "
882eda14cbcSMatt Macy 			    "and new vdev is %s\n"),
883eda14cbcSMatt Macy 			    current->zprl_type, new->zprl_type);
884eda14cbcSMatt Macy 			ret = -1;
885eda14cbcSMatt Macy 		} else if (current->zprl_parity != new->zprl_parity) {
886eda14cbcSMatt Macy 			vdev_error(gettext(
887eda14cbcSMatt Macy 			    "mismatched replication level: pool uses %llu "
888eda14cbcSMatt Macy 			    "device parity and new vdev uses %llu\n"),
8891f88aa09SMartin Matuska 			    (u_longlong_t)current->zprl_parity,
8901f88aa09SMartin Matuska 			    (u_longlong_t)new->zprl_parity);
891eda14cbcSMatt Macy 			ret = -1;
892eda14cbcSMatt Macy 		} else if (current->zprl_children != new->zprl_children) {
893eda14cbcSMatt Macy 			vdev_error(gettext(
894eda14cbcSMatt Macy 			    "mismatched replication level: pool uses %llu-way "
895eda14cbcSMatt Macy 			    "%s and new vdev uses %llu-way %s\n"),
8961f88aa09SMartin Matuska 			    (u_longlong_t)current->zprl_children,
8971f88aa09SMartin Matuska 			    current->zprl_type,
8981f88aa09SMartin Matuska 			    (u_longlong_t)new->zprl_children,
8991f88aa09SMartin Matuska 			    new->zprl_type);
900eda14cbcSMatt Macy 			ret = -1;
901eda14cbcSMatt Macy 		}
902eda14cbcSMatt Macy 	}
903eda14cbcSMatt Macy 
904eda14cbcSMatt Macy 	free(new);
905eda14cbcSMatt Macy 	if (current != NULL)
906eda14cbcSMatt Macy 		free(current);
907eda14cbcSMatt Macy 
908eda14cbcSMatt Macy 	return (ret);
909eda14cbcSMatt Macy }
910eda14cbcSMatt Macy 
911eda14cbcSMatt Macy static int
zero_label(const char * path)9122a58b312SMartin Matuska zero_label(const char *path)
913eda14cbcSMatt Macy {
914eda14cbcSMatt Macy 	const int size = 4096;
915eda14cbcSMatt Macy 	char buf[size];
916eda14cbcSMatt Macy 	int err, fd;
917eda14cbcSMatt Macy 
918eda14cbcSMatt Macy 	if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
919eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
920eda14cbcSMatt Macy 		    path, strerror(errno));
921eda14cbcSMatt Macy 		return (-1);
922eda14cbcSMatt Macy 	}
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy 	memset(buf, 0, size);
925eda14cbcSMatt Macy 	err = write(fd, buf, size);
926eda14cbcSMatt Macy 	(void) fdatasync(fd);
927eda14cbcSMatt Macy 	(void) close(fd);
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	if (err == -1) {
930eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("cannot zero first %d bytes "
931eda14cbcSMatt Macy 		    "of '%s': %s\n"), size, path, strerror(errno));
932eda14cbcSMatt Macy 		return (-1);
933eda14cbcSMatt Macy 	}
934eda14cbcSMatt Macy 
935eda14cbcSMatt Macy 	if (err != size) {
936eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("could only zero %d/%d bytes "
937eda14cbcSMatt Macy 		    "of '%s'\n"), err, size, path);
938eda14cbcSMatt Macy 		return (-1);
939eda14cbcSMatt Macy 	}
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 	return (0);
942eda14cbcSMatt Macy }
943eda14cbcSMatt Macy 
944abcdc1b9SMartin Matuska static void
lines_to_stderr(char * lines[],int lines_cnt)945abcdc1b9SMartin Matuska lines_to_stderr(char *lines[], int lines_cnt)
946abcdc1b9SMartin Matuska {
947abcdc1b9SMartin Matuska 	int i;
948abcdc1b9SMartin Matuska 	for (i = 0; i < lines_cnt; i++) {
949abcdc1b9SMartin Matuska 		fprintf(stderr, "%s\n", lines[i]);
950abcdc1b9SMartin Matuska 	}
951abcdc1b9SMartin Matuska }
952abcdc1b9SMartin Matuska 
953eda14cbcSMatt Macy /*
954eda14cbcSMatt Macy  * Go through and find any whole disks in the vdev specification, labelling them
955eda14cbcSMatt Macy  * as appropriate.  When constructing the vdev spec, we were unable to open this
956eda14cbcSMatt Macy  * device in order to provide a devid.  Now that we have labelled the disk and
957eda14cbcSMatt Macy  * know that slice 0 is valid, we can construct the devid now.
958eda14cbcSMatt Macy  *
959eda14cbcSMatt Macy  * If the disk was already labeled with an EFI label, we will have gotten the
960eda14cbcSMatt Macy  * devid already (because we were able to open the whole disk).  Otherwise, we
961eda14cbcSMatt Macy  * need to get the devid after we label the disk.
962eda14cbcSMatt Macy  */
963eda14cbcSMatt Macy static int
make_disks(zpool_handle_t * zhp,nvlist_t * nv,boolean_t replacing)964abcdc1b9SMartin Matuska make_disks(zpool_handle_t *zhp, nvlist_t *nv, boolean_t replacing)
965eda14cbcSMatt Macy {
966eda14cbcSMatt Macy 	nvlist_t **child;
967eda14cbcSMatt Macy 	uint_t c, children;
9682a58b312SMartin Matuska 	const char *type, *path;
969eda14cbcSMatt Macy 	char devpath[MAXPATHLEN];
970eda14cbcSMatt Macy 	char udevpath[MAXPATHLEN];
971eda14cbcSMatt Macy 	uint64_t wholedisk;
972eda14cbcSMatt Macy 	struct stat64 statbuf;
973eda14cbcSMatt Macy 	int is_exclusive = 0;
974eda14cbcSMatt Macy 	int fd;
975eda14cbcSMatt Macy 	int ret;
976eda14cbcSMatt Macy 
977eda14cbcSMatt Macy 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
978eda14cbcSMatt Macy 
979eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
980eda14cbcSMatt Macy 	    &child, &children) != 0) {
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 		if (strcmp(type, VDEV_TYPE_DISK) != 0)
983eda14cbcSMatt Macy 			return (0);
984eda14cbcSMatt Macy 
985eda14cbcSMatt Macy 		/*
986eda14cbcSMatt Macy 		 * We have a disk device.  If this is a whole disk write
987eda14cbcSMatt Macy 		 * out the efi partition table, otherwise write zero's to
988eda14cbcSMatt Macy 		 * the first 4k of the partition.  This is to ensure that
989eda14cbcSMatt Macy 		 * libblkid will not misidentify the partition due to a
990eda14cbcSMatt Macy 		 * magic value left by the previous filesystem.
991eda14cbcSMatt Macy 		 */
992eda14cbcSMatt Macy 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
993eda14cbcSMatt Macy 		verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
994eda14cbcSMatt Macy 		    &wholedisk));
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy 		if (!wholedisk) {
997eda14cbcSMatt Macy 			/*
998eda14cbcSMatt Macy 			 * Update device id string for mpath nodes (Linux only)
999eda14cbcSMatt Macy 			 */
1000eda14cbcSMatt Macy 			if (is_mpath_whole_disk(path))
1001eda14cbcSMatt Macy 				update_vdev_config_dev_strs(nv);
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy 			if (!is_spare(NULL, path))
1004eda14cbcSMatt Macy 				(void) zero_label(path);
1005eda14cbcSMatt Macy 			return (0);
1006eda14cbcSMatt Macy 		}
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 		if (realpath(path, devpath) == NULL) {
1009eda14cbcSMatt Macy 			ret = errno;
1010eda14cbcSMatt Macy 			(void) fprintf(stderr,
1011eda14cbcSMatt Macy 			    gettext("cannot resolve path '%s'\n"), path);
1012eda14cbcSMatt Macy 			return (ret);
1013eda14cbcSMatt Macy 		}
1014eda14cbcSMatt Macy 
1015eda14cbcSMatt Macy 		/*
1016eda14cbcSMatt Macy 		 * Remove any previously existing symlink from a udev path to
1017eda14cbcSMatt Macy 		 * the device before labeling the disk.  This ensures that
1018eda14cbcSMatt Macy 		 * only newly created links are used.  Otherwise there is a
1019eda14cbcSMatt Macy 		 * window between when udev deletes and recreates the link
1020eda14cbcSMatt Macy 		 * during which access attempts will fail with ENOENT.
1021eda14cbcSMatt Macy 		 */
1022eda14cbcSMatt Macy 		strlcpy(udevpath, path, MAXPATHLEN);
1023eda14cbcSMatt Macy 		(void) zfs_append_partition(udevpath, MAXPATHLEN);
1024eda14cbcSMatt Macy 
1025eda14cbcSMatt Macy 		fd = open(devpath, O_RDWR|O_EXCL);
1026eda14cbcSMatt Macy 		if (fd == -1) {
1027eda14cbcSMatt Macy 			if (errno == EBUSY)
1028eda14cbcSMatt Macy 				is_exclusive = 1;
1029eda14cbcSMatt Macy #ifdef __FreeBSD__
1030eda14cbcSMatt Macy 			if (errno == EPERM)
1031eda14cbcSMatt Macy 				is_exclusive = 1;
1032eda14cbcSMatt Macy #endif
1033eda14cbcSMatt Macy 		} else {
1034eda14cbcSMatt Macy 			(void) close(fd);
1035eda14cbcSMatt Macy 		}
1036eda14cbcSMatt Macy 
1037eda14cbcSMatt Macy 		/*
1038eda14cbcSMatt Macy 		 * If the partition exists, contains a valid spare label,
1039eda14cbcSMatt Macy 		 * and is opened exclusively there is no need to partition
1040eda14cbcSMatt Macy 		 * it.  Hot spares have already been partitioned and are
1041eda14cbcSMatt Macy 		 * held open exclusively by the kernel as a safety measure.
1042eda14cbcSMatt Macy 		 *
1043eda14cbcSMatt Macy 		 * If the provided path is for a /dev/disk/ device its
1044eda14cbcSMatt Macy 		 * symbolic link will be removed, partition table created,
1045eda14cbcSMatt Macy 		 * and then block until udev creates the new link.
1046eda14cbcSMatt Macy 		 */
1047eda14cbcSMatt Macy 		if (!is_exclusive && !is_spare(NULL, udevpath)) {
1048eda14cbcSMatt Macy 			char *devnode = strrchr(devpath, '/') + 1;
1049abcdc1b9SMartin Matuska 			char **lines = NULL;
1050abcdc1b9SMartin Matuska 			int lines_cnt = 0;
1051eda14cbcSMatt Macy 
1052eda14cbcSMatt Macy 			ret = strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT));
1053eda14cbcSMatt Macy 			if (ret == 0) {
1054eda14cbcSMatt Macy 				ret = lstat64(udevpath, &statbuf);
1055eda14cbcSMatt Macy 				if (ret == 0 && S_ISLNK(statbuf.st_mode))
1056eda14cbcSMatt Macy 					(void) unlink(udevpath);
1057eda14cbcSMatt Macy 			}
1058eda14cbcSMatt Macy 
1059eda14cbcSMatt Macy 			/*
1060eda14cbcSMatt Macy 			 * When labeling a pool the raw device node name
1061eda14cbcSMatt Macy 			 * is provided as it appears under /dev/.
1062abcdc1b9SMartin Matuska 			 *
1063abcdc1b9SMartin Matuska 			 * Note that 'zhp' will be NULL when we're creating a
1064abcdc1b9SMartin Matuska 			 * pool.
1065eda14cbcSMatt Macy 			 */
1066abcdc1b9SMartin Matuska 			if (zpool_prepare_and_label_disk(g_zfs, zhp, devnode,
1067abcdc1b9SMartin Matuska 			    nv, zhp == NULL ? "create" :
1068abcdc1b9SMartin Matuska 			    replacing ? "replace" : "add", &lines,
1069abcdc1b9SMartin Matuska 			    &lines_cnt) != 0) {
1070abcdc1b9SMartin Matuska 				(void) fprintf(stderr,
1071abcdc1b9SMartin Matuska 				    gettext(
1072abcdc1b9SMartin Matuska 				    "Error preparing/labeling disk.\n"));
1073abcdc1b9SMartin Matuska 				if (lines_cnt > 0) {
1074abcdc1b9SMartin Matuska 					(void) fprintf(stderr,
1075abcdc1b9SMartin Matuska 					gettext("zfs_prepare_disk output:\n"));
1076abcdc1b9SMartin Matuska 					lines_to_stderr(lines, lines_cnt);
1077abcdc1b9SMartin Matuska 				}
1078abcdc1b9SMartin Matuska 
1079abcdc1b9SMartin Matuska 				libzfs_free_str_array(lines, lines_cnt);
1080eda14cbcSMatt Macy 				return (-1);
1081abcdc1b9SMartin Matuska 			}
1082abcdc1b9SMartin Matuska 			libzfs_free_str_array(lines, lines_cnt);
1083eda14cbcSMatt Macy 
1084eda14cbcSMatt Macy 			/*
1085eda14cbcSMatt Macy 			 * Wait for udev to signal the device is available
1086eda14cbcSMatt Macy 			 * by the provided path.
1087eda14cbcSMatt Macy 			 */
1088eda14cbcSMatt Macy 			ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
1089eda14cbcSMatt Macy 			if (ret) {
1090eda14cbcSMatt Macy 				(void) fprintf(stderr,
1091eda14cbcSMatt Macy 				    gettext("missing link: %s was "
1092eda14cbcSMatt Macy 				    "partitioned but %s is missing\n"),
1093eda14cbcSMatt Macy 				    devnode, udevpath);
1094eda14cbcSMatt Macy 				return (ret);
1095eda14cbcSMatt Macy 			}
1096eda14cbcSMatt Macy 
1097eda14cbcSMatt Macy 			ret = zero_label(udevpath);
1098eda14cbcSMatt Macy 			if (ret)
1099eda14cbcSMatt Macy 				return (ret);
1100eda14cbcSMatt Macy 		}
1101eda14cbcSMatt Macy 
1102eda14cbcSMatt Macy 		/*
1103eda14cbcSMatt Macy 		 * Update the path to refer to the partition.  The presence of
1104eda14cbcSMatt Macy 		 * the 'whole_disk' field indicates to the CLI that we should
1105eda14cbcSMatt Macy 		 * chop off the partition number when displaying the device in
1106eda14cbcSMatt Macy 		 * future output.
1107eda14cbcSMatt Macy 		 */
1108eda14cbcSMatt Macy 		verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1109eda14cbcSMatt Macy 
1110eda14cbcSMatt Macy 		/*
1111eda14cbcSMatt Macy 		 * Update device id strings for whole disks (Linux only)
1112eda14cbcSMatt Macy 		 */
1113eda14cbcSMatt Macy 		update_vdev_config_dev_strs(nv);
1114eda14cbcSMatt Macy 
1115eda14cbcSMatt Macy 		return (0);
1116eda14cbcSMatt Macy 	}
1117eda14cbcSMatt Macy 
1118eda14cbcSMatt Macy 	for (c = 0; c < children; c++)
1119abcdc1b9SMartin Matuska 		if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1120eda14cbcSMatt Macy 			return (ret);
1121eda14cbcSMatt Macy 
1122eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1123eda14cbcSMatt Macy 	    &child, &children) == 0)
1124eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
1125abcdc1b9SMartin Matuska 			if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1126eda14cbcSMatt Macy 				return (ret);
1127eda14cbcSMatt Macy 
1128eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1129eda14cbcSMatt Macy 	    &child, &children) == 0)
1130eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
1131abcdc1b9SMartin Matuska 			if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1132eda14cbcSMatt Macy 				return (ret);
1133eda14cbcSMatt Macy 
1134eda14cbcSMatt Macy 	return (0);
1135eda14cbcSMatt Macy }
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy /*
1138eda14cbcSMatt Macy  * Go through and find any devices that are in use.  We rely on libdiskmgt for
1139eda14cbcSMatt Macy  * the majority of this task.
1140eda14cbcSMatt Macy  */
1141eda14cbcSMatt Macy static boolean_t
is_device_in_use(nvlist_t * config,nvlist_t * nv,boolean_t force,boolean_t replacing,boolean_t isspare)1142eda14cbcSMatt Macy is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1143eda14cbcSMatt Macy     boolean_t replacing, boolean_t isspare)
1144eda14cbcSMatt Macy {
1145eda14cbcSMatt Macy 	nvlist_t **child;
1146eda14cbcSMatt Macy 	uint_t c, children;
11472a58b312SMartin Matuska 	const char *type, *path;
1148eda14cbcSMatt Macy 	int ret = 0;
1149eda14cbcSMatt Macy 	char buf[MAXPATHLEN];
1150eda14cbcSMatt Macy 	uint64_t wholedisk = B_FALSE;
1151eda14cbcSMatt Macy 	boolean_t anyinuse = B_FALSE;
1152eda14cbcSMatt Macy 
1153eda14cbcSMatt Macy 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1154eda14cbcSMatt Macy 
1155eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1156eda14cbcSMatt Macy 	    &child, &children) != 0) {
1157eda14cbcSMatt Macy 
1158eda14cbcSMatt Macy 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1159eda14cbcSMatt Macy 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1160eda14cbcSMatt Macy 			verify(!nvlist_lookup_uint64(nv,
1161eda14cbcSMatt Macy 			    ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 		/*
1164eda14cbcSMatt Macy 		 * As a generic check, we look to see if this is a replace of a
1165eda14cbcSMatt Macy 		 * hot spare within the same pool.  If so, we allow it
1166eda14cbcSMatt Macy 		 * regardless of what libblkid or zpool_in_use() says.
1167eda14cbcSMatt Macy 		 */
1168eda14cbcSMatt Macy 		if (replacing) {
1169eda14cbcSMatt Macy 			(void) strlcpy(buf, path, sizeof (buf));
1170eda14cbcSMatt Macy 			if (wholedisk) {
1171eda14cbcSMatt Macy 				ret = zfs_append_partition(buf,  sizeof (buf));
1172eda14cbcSMatt Macy 				if (ret == -1)
1173eda14cbcSMatt Macy 					return (-1);
1174eda14cbcSMatt Macy 			}
1175eda14cbcSMatt Macy 
1176eda14cbcSMatt Macy 			if (is_spare(config, buf))
1177eda14cbcSMatt Macy 				return (B_FALSE);
1178eda14cbcSMatt Macy 		}
1179eda14cbcSMatt Macy 
1180eda14cbcSMatt Macy 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1181eda14cbcSMatt Macy 			ret = check_device(path, force, isspare, wholedisk);
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 		else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1184eda14cbcSMatt Macy 			ret = check_file(path, force, isspare);
1185eda14cbcSMatt Macy 
1186eda14cbcSMatt Macy 		return (ret != 0);
1187eda14cbcSMatt Macy 	}
1188eda14cbcSMatt Macy 
1189eda14cbcSMatt Macy 	for (c = 0; c < children; c++)
1190eda14cbcSMatt Macy 		if (is_device_in_use(config, child[c], force, replacing,
1191eda14cbcSMatt Macy 		    B_FALSE))
1192eda14cbcSMatt Macy 			anyinuse = B_TRUE;
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1195eda14cbcSMatt Macy 	    &child, &children) == 0)
1196eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
1197eda14cbcSMatt Macy 			if (is_device_in_use(config, child[c], force, replacing,
1198eda14cbcSMatt Macy 			    B_TRUE))
1199eda14cbcSMatt Macy 				anyinuse = B_TRUE;
1200eda14cbcSMatt Macy 
1201eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1202eda14cbcSMatt Macy 	    &child, &children) == 0)
1203eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
1204eda14cbcSMatt Macy 			if (is_device_in_use(config, child[c], force, replacing,
1205eda14cbcSMatt Macy 			    B_FALSE))
1206eda14cbcSMatt Macy 				anyinuse = B_TRUE;
1207eda14cbcSMatt Macy 
1208eda14cbcSMatt Macy 	return (anyinuse);
1209eda14cbcSMatt Macy }
1210eda14cbcSMatt Macy 
12117877fdebSMatt Macy /*
12127877fdebSMatt Macy  * Returns the parity level extracted from a raidz or draid type.
12137877fdebSMatt Macy  * If the parity cannot be determined zero is returned.
12147877fdebSMatt Macy  */
12157877fdebSMatt Macy static int
get_parity(const char * type)12167877fdebSMatt Macy get_parity(const char *type)
12177877fdebSMatt Macy {
12187877fdebSMatt Macy 	long parity = 0;
12197877fdebSMatt Macy 	const char *p;
12207877fdebSMatt Macy 
12217877fdebSMatt Macy 	if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0) {
12227877fdebSMatt Macy 		p = type + strlen(VDEV_TYPE_RAIDZ);
12237877fdebSMatt Macy 
12247877fdebSMatt Macy 		if (*p == '\0') {
12257877fdebSMatt Macy 			/* when unspecified default to single parity */
12267877fdebSMatt Macy 			return (1);
12277877fdebSMatt Macy 		} else if (*p == '0') {
12287877fdebSMatt Macy 			/* no zero prefixes allowed */
12297877fdebSMatt Macy 			return (0);
12307877fdebSMatt Macy 		} else {
12317877fdebSMatt Macy 			/* 0-3, no suffixes allowed */
12327877fdebSMatt Macy 			char *end;
12337877fdebSMatt Macy 			errno = 0;
12347877fdebSMatt Macy 			parity = strtol(p, &end, 10);
12357877fdebSMatt Macy 			if (errno != 0 || *end != '\0' ||
12367877fdebSMatt Macy 			    parity < 1 || parity > VDEV_RAIDZ_MAXPARITY) {
12377877fdebSMatt Macy 				return (0);
12387877fdebSMatt Macy 			}
12397877fdebSMatt Macy 		}
12407877fdebSMatt Macy 	} else if (strncmp(type, VDEV_TYPE_DRAID,
12417877fdebSMatt Macy 	    strlen(VDEV_TYPE_DRAID)) == 0) {
12427877fdebSMatt Macy 		p = type + strlen(VDEV_TYPE_DRAID);
12437877fdebSMatt Macy 
12447877fdebSMatt Macy 		if (*p == '\0' || *p == ':') {
12457877fdebSMatt Macy 			/* when unspecified default to single parity */
12467877fdebSMatt Macy 			return (1);
12477877fdebSMatt Macy 		} else if (*p == '0') {
12487877fdebSMatt Macy 			/* no zero prefixes allowed */
12497877fdebSMatt Macy 			return (0);
12507877fdebSMatt Macy 		} else {
12517877fdebSMatt Macy 			/* 0-3, allowed suffixes: '\0' or ':' */
12527877fdebSMatt Macy 			char *end;
12537877fdebSMatt Macy 			errno = 0;
12547877fdebSMatt Macy 			parity = strtol(p, &end, 10);
12557877fdebSMatt Macy 			if (errno != 0 ||
12567877fdebSMatt Macy 			    parity < 1 || parity > VDEV_DRAID_MAXPARITY ||
12577877fdebSMatt Macy 			    (*end != '\0' && *end != ':')) {
12587877fdebSMatt Macy 				return (0);
12597877fdebSMatt Macy 			}
12607877fdebSMatt Macy 		}
12617877fdebSMatt Macy 	}
12627877fdebSMatt Macy 
12637877fdebSMatt Macy 	return ((int)parity);
12647877fdebSMatt Macy }
12657877fdebSMatt Macy 
12667877fdebSMatt Macy /*
12677877fdebSMatt Macy  * Assign the minimum and maximum number of devices allowed for
12687877fdebSMatt Macy  * the specified type.  On error NULL is returned, otherwise the
12697877fdebSMatt Macy  * type prefix is returned (raidz, mirror, etc).
12707877fdebSMatt Macy  */
1271eda14cbcSMatt Macy static const char *
is_grouping(const char * type,int * mindev,int * maxdev)1272eda14cbcSMatt Macy is_grouping(const char *type, int *mindev, int *maxdev)
1273eda14cbcSMatt Macy {
12747877fdebSMatt Macy 	int nparity;
1275eda14cbcSMatt Macy 
12767877fdebSMatt Macy 	if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
12777877fdebSMatt Macy 	    strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0) {
12787877fdebSMatt Macy 		nparity = get_parity(type);
12797877fdebSMatt Macy 		if (nparity == 0)
1280eda14cbcSMatt Macy 			return (NULL);
1281eda14cbcSMatt Macy 		if (mindev != NULL)
1282eda14cbcSMatt Macy 			*mindev = nparity + 1;
1283eda14cbcSMatt Macy 		if (maxdev != NULL)
1284eda14cbcSMatt Macy 			*maxdev = 255;
12857877fdebSMatt Macy 
12867877fdebSMatt Macy 		if (strncmp(type, VDEV_TYPE_RAIDZ,
12877877fdebSMatt Macy 		    strlen(VDEV_TYPE_RAIDZ)) == 0) {
1288eda14cbcSMatt Macy 			return (VDEV_TYPE_RAIDZ);
12897877fdebSMatt Macy 		} else {
12907877fdebSMatt Macy 			return (VDEV_TYPE_DRAID);
12917877fdebSMatt Macy 		}
1292eda14cbcSMatt Macy 	}
1293eda14cbcSMatt Macy 
1294eda14cbcSMatt Macy 	if (maxdev != NULL)
1295eda14cbcSMatt Macy 		*maxdev = INT_MAX;
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy 	if (strcmp(type, "mirror") == 0) {
1298eda14cbcSMatt Macy 		if (mindev != NULL)
1299eda14cbcSMatt Macy 			*mindev = 2;
1300eda14cbcSMatt Macy 		return (VDEV_TYPE_MIRROR);
1301eda14cbcSMatt Macy 	}
1302eda14cbcSMatt Macy 
1303eda14cbcSMatt Macy 	if (strcmp(type, "spare") == 0) {
1304eda14cbcSMatt Macy 		if (mindev != NULL)
1305eda14cbcSMatt Macy 			*mindev = 1;
1306eda14cbcSMatt Macy 		return (VDEV_TYPE_SPARE);
1307eda14cbcSMatt Macy 	}
1308eda14cbcSMatt Macy 
1309eda14cbcSMatt Macy 	if (strcmp(type, "log") == 0) {
1310eda14cbcSMatt Macy 		if (mindev != NULL)
1311eda14cbcSMatt Macy 			*mindev = 1;
1312eda14cbcSMatt Macy 		return (VDEV_TYPE_LOG);
1313eda14cbcSMatt Macy 	}
1314eda14cbcSMatt Macy 
1315eda14cbcSMatt Macy 	if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
1316eda14cbcSMatt Macy 	    strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1317eda14cbcSMatt Macy 		if (mindev != NULL)
1318eda14cbcSMatt Macy 			*mindev = 1;
1319eda14cbcSMatt Macy 		return (type);
1320eda14cbcSMatt Macy 	}
1321eda14cbcSMatt Macy 
1322eda14cbcSMatt Macy 	if (strcmp(type, "cache") == 0) {
1323eda14cbcSMatt Macy 		if (mindev != NULL)
1324eda14cbcSMatt Macy 			*mindev = 1;
1325eda14cbcSMatt Macy 		return (VDEV_TYPE_L2CACHE);
1326eda14cbcSMatt Macy 	}
1327eda14cbcSMatt Macy 
1328eda14cbcSMatt Macy 	return (NULL);
1329eda14cbcSMatt Macy }
1330eda14cbcSMatt Macy 
1331eda14cbcSMatt Macy /*
13327877fdebSMatt Macy  * Extract the configuration parameters encoded in the dRAID type and
13337877fdebSMatt Macy  * use them to generate a dRAID configuration.  The expected format is:
13347877fdebSMatt Macy  *
13357877fdebSMatt Macy  * draid[<parity>][:<data><d|D>][:<children><c|C>][:<spares><s|S>]
13367877fdebSMatt Macy  *
13377877fdebSMatt Macy  * The intent is to be able to generate a good configuration when no
13387877fdebSMatt Macy  * additional information is provided.  The only mandatory component
13397877fdebSMatt Macy  * of the 'type' is the 'draid' prefix.  If a value is not provided
13407877fdebSMatt Macy  * then reasonable defaults are used.  The optional components may
13417877fdebSMatt Macy  * appear in any order but the d/s/c suffix is required.
13427877fdebSMatt Macy  *
13437877fdebSMatt Macy  * Valid inputs:
13447877fdebSMatt Macy  * - data:     number of data devices per group (1-255)
13457877fdebSMatt Macy  * - parity:   number of parity blocks per group (1-3)
13467877fdebSMatt Macy  * - spares:   number of distributed spare (0-100)
13477877fdebSMatt Macy  * - children: total number of devices (1-255)
13487877fdebSMatt Macy  *
13497877fdebSMatt Macy  * Examples:
13507877fdebSMatt Macy  * - zpool create tank draid <devices...>
13517877fdebSMatt Macy  * - zpool create tank draid2:8d:51c:2s <devices...>
13527877fdebSMatt Macy  */
13537877fdebSMatt Macy static int
draid_config_by_type(nvlist_t * nv,const char * type,uint64_t children)13547877fdebSMatt Macy draid_config_by_type(nvlist_t *nv, const char *type, uint64_t children)
13557877fdebSMatt Macy {
13567877fdebSMatt Macy 	uint64_t nparity = 1;
13577877fdebSMatt Macy 	uint64_t nspares = 0;
13587877fdebSMatt Macy 	uint64_t ndata = UINT64_MAX;
13597877fdebSMatt Macy 	uint64_t ngroups = 1;
13607877fdebSMatt Macy 	long value;
13617877fdebSMatt Macy 
13627877fdebSMatt Macy 	if (strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) != 0)
13637877fdebSMatt Macy 		return (EINVAL);
13647877fdebSMatt Macy 
13657877fdebSMatt Macy 	nparity = (uint64_t)get_parity(type);
13662a58b312SMartin Matuska 	if (nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
13672a58b312SMartin Matuska 		fprintf(stderr,
13682a58b312SMartin Matuska 		    gettext("invalid dRAID parity level %llu; must be "
13692a58b312SMartin Matuska 		    "between 1 and %d\n"), (u_longlong_t)nparity,
13702a58b312SMartin Matuska 		    VDEV_DRAID_MAXPARITY);
13717877fdebSMatt Macy 		return (EINVAL);
13722a58b312SMartin Matuska 	}
13737877fdebSMatt Macy 
13747877fdebSMatt Macy 	char *p = (char *)type;
13757877fdebSMatt Macy 	while ((p = strchr(p, ':')) != NULL) {
13767877fdebSMatt Macy 		char *end;
13777877fdebSMatt Macy 
13787877fdebSMatt Macy 		p = p + 1;
13797877fdebSMatt Macy 		errno = 0;
13807877fdebSMatt Macy 
13817877fdebSMatt Macy 		if (!isdigit(p[0])) {
13827877fdebSMatt Macy 			(void) fprintf(stderr, gettext("invalid dRAID "
13837877fdebSMatt Macy 			    "syntax; expected [:<number><c|d|s>] not '%s'\n"),
13847877fdebSMatt Macy 			    type);
13857877fdebSMatt Macy 			return (EINVAL);
13867877fdebSMatt Macy 		}
13877877fdebSMatt Macy 
13887877fdebSMatt Macy 		/* Expected non-zero value with c/d/s suffix */
13897877fdebSMatt Macy 		value = strtol(p, &end, 10);
13907877fdebSMatt Macy 		char suffix = tolower(*end);
13917877fdebSMatt Macy 		if (errno != 0 ||
13927877fdebSMatt Macy 		    (suffix != 'c' && suffix != 'd' && suffix != 's')) {
13937877fdebSMatt Macy 			(void) fprintf(stderr, gettext("invalid dRAID "
13947877fdebSMatt Macy 			    "syntax; expected [:<number><c|d|s>] not '%s'\n"),
13957877fdebSMatt Macy 			    type);
13967877fdebSMatt Macy 			return (EINVAL);
13977877fdebSMatt Macy 		}
13987877fdebSMatt Macy 
13997877fdebSMatt Macy 		if (suffix == 'c') {
14007877fdebSMatt Macy 			if ((uint64_t)value != children) {
14017877fdebSMatt Macy 				fprintf(stderr,
14027877fdebSMatt Macy 				    gettext("invalid number of dRAID children; "
14037877fdebSMatt Macy 				    "%llu required but %llu provided\n"),
14047877fdebSMatt Macy 				    (u_longlong_t)value,
14057877fdebSMatt Macy 				    (u_longlong_t)children);
14067877fdebSMatt Macy 				return (EINVAL);
14077877fdebSMatt Macy 			}
14087877fdebSMatt Macy 		} else if (suffix == 'd') {
14097877fdebSMatt Macy 			ndata = (uint64_t)value;
14107877fdebSMatt Macy 		} else if (suffix == 's') {
14117877fdebSMatt Macy 			nspares = (uint64_t)value;
14127877fdebSMatt Macy 		} else {
14137877fdebSMatt Macy 			verify(0); /* Unreachable */
14147877fdebSMatt Macy 		}
14157877fdebSMatt Macy 	}
14167877fdebSMatt Macy 
14177877fdebSMatt Macy 	/*
14187877fdebSMatt Macy 	 * When a specific number of data disks is not provided limit a
14197877fdebSMatt Macy 	 * redundancy group to 8 data disks.  This value was selected to
14207877fdebSMatt Macy 	 * provide a reasonable tradeoff between capacity and performance.
14217877fdebSMatt Macy 	 */
14227877fdebSMatt Macy 	if (ndata == UINT64_MAX) {
14237877fdebSMatt Macy 		if (children > nspares + nparity) {
14247877fdebSMatt Macy 			ndata = MIN(children - nspares - nparity, 8);
14257877fdebSMatt Macy 		} else {
14267877fdebSMatt Macy 			fprintf(stderr, gettext("request number of "
14277877fdebSMatt Macy 			    "distributed spares %llu and parity level %llu\n"
14287877fdebSMatt Macy 			    "leaves no disks available for data\n"),
14297877fdebSMatt Macy 			    (u_longlong_t)nspares, (u_longlong_t)nparity);
14307877fdebSMatt Macy 			return (EINVAL);
14317877fdebSMatt Macy 		}
14327877fdebSMatt Macy 	}
14337877fdebSMatt Macy 
14347877fdebSMatt Macy 	/* Verify the maximum allowed group size is never exceeded. */
14357877fdebSMatt Macy 	if (ndata == 0 || (ndata + nparity > children - nspares)) {
14367877fdebSMatt Macy 		fprintf(stderr, gettext("requested number of dRAID data "
14377877fdebSMatt Macy 		    "disks per group %llu is too high,\nat most %llu disks "
14387877fdebSMatt Macy 		    "are available for data\n"), (u_longlong_t)ndata,
14397877fdebSMatt Macy 		    (u_longlong_t)(children - nspares - nparity));
14407877fdebSMatt Macy 		return (EINVAL);
14417877fdebSMatt Macy 	}
14427877fdebSMatt Macy 
14437877fdebSMatt Macy 	/*
14447877fdebSMatt Macy 	 * Verify the requested number of spares can be satisfied.
14457877fdebSMatt Macy 	 * An arbitrary limit of 100 distributed spares is applied.
14467877fdebSMatt Macy 	 */
14477877fdebSMatt Macy 	if (nspares > 100 || nspares > (children - (ndata + nparity))) {
14487877fdebSMatt Macy 		fprintf(stderr,
14497877fdebSMatt Macy 		    gettext("invalid number of dRAID spares %llu; additional "
14507877fdebSMatt Macy 		    "disks would be required\n"), (u_longlong_t)nspares);
14517877fdebSMatt Macy 		return (EINVAL);
14527877fdebSMatt Macy 	}
14537877fdebSMatt Macy 
14547877fdebSMatt Macy 	/* Verify the requested number children is sufficient. */
14557877fdebSMatt Macy 	if (children < (ndata + nparity + nspares)) {
14567877fdebSMatt Macy 		fprintf(stderr, gettext("%llu disks were provided, but at "
14577877fdebSMatt Macy 		    "least %llu disks are required for this config\n"),
14587877fdebSMatt Macy 		    (u_longlong_t)children,
14597877fdebSMatt Macy 		    (u_longlong_t)(ndata + nparity + nspares));
14607877fdebSMatt Macy 	}
14617877fdebSMatt Macy 
14627877fdebSMatt Macy 	if (children > VDEV_DRAID_MAX_CHILDREN) {
14637877fdebSMatt Macy 		fprintf(stderr, gettext("%llu disks were provided, but "
14647877fdebSMatt Macy 		    "dRAID only supports up to %u disks"),
14657877fdebSMatt Macy 		    (u_longlong_t)children, VDEV_DRAID_MAX_CHILDREN);
14667877fdebSMatt Macy 	}
14677877fdebSMatt Macy 
14687877fdebSMatt Macy 	/*
14697877fdebSMatt Macy 	 * Calculate the minimum number of groups required to fill a slice.
14707877fdebSMatt Macy 	 * This is the LCM of the stripe width (ndata + nparity) and the
14717877fdebSMatt Macy 	 * number of data drives (children - nspares).
14727877fdebSMatt Macy 	 */
14737877fdebSMatt Macy 	while (ngroups * (ndata + nparity) % (children - nspares) != 0)
14747877fdebSMatt Macy 		ngroups++;
14757877fdebSMatt Macy 
14767877fdebSMatt Macy 	/* Store the basic dRAID configuration. */
14777877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, nparity);
14787877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, ndata);
14797877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, nspares);
14807877fdebSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, ngroups);
14817877fdebSMatt Macy 
14827877fdebSMatt Macy 	return (0);
14837877fdebSMatt Macy }
14847877fdebSMatt Macy 
14857877fdebSMatt Macy /*
1486eda14cbcSMatt Macy  * Construct a syntactically valid vdev specification,
1487eda14cbcSMatt Macy  * and ensure that all devices and files exist and can be opened.
1488eda14cbcSMatt Macy  * Note: we don't bother freeing anything in the error paths
1489eda14cbcSMatt Macy  * because the program is just going to exit anyway.
1490eda14cbcSMatt Macy  */
1491eda14cbcSMatt Macy static nvlist_t *
construct_spec(nvlist_t * props,int argc,char ** argv)1492eda14cbcSMatt Macy construct_spec(nvlist_t *props, int argc, char **argv)
1493eda14cbcSMatt Macy {
1494eda14cbcSMatt Macy 	nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1495eda14cbcSMatt Macy 	int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
14967877fdebSMatt Macy 	const char *type, *fulltype;
14977877fdebSMatt Macy 	boolean_t is_log, is_special, is_dedup, is_spare;
1498eda14cbcSMatt Macy 	boolean_t seen_logs;
1499eda14cbcSMatt Macy 
1500eda14cbcSMatt Macy 	top = NULL;
1501eda14cbcSMatt Macy 	toplevels = 0;
1502eda14cbcSMatt Macy 	spares = NULL;
1503eda14cbcSMatt Macy 	l2cache = NULL;
1504eda14cbcSMatt Macy 	nspares = 0;
1505eda14cbcSMatt Macy 	nlogs = 0;
1506eda14cbcSMatt Macy 	nl2cache = 0;
15077877fdebSMatt Macy 	is_log = is_special = is_dedup = is_spare = B_FALSE;
1508eda14cbcSMatt Macy 	seen_logs = B_FALSE;
1509eda14cbcSMatt Macy 	nvroot = NULL;
1510eda14cbcSMatt Macy 
1511eda14cbcSMatt Macy 	while (argc > 0) {
15127877fdebSMatt Macy 		fulltype = argv[0];
1513eda14cbcSMatt Macy 		nv = NULL;
1514eda14cbcSMatt Macy 
1515eda14cbcSMatt Macy 		/*
15167877fdebSMatt Macy 		 * If it's a mirror, raidz, or draid the subsequent arguments
15177877fdebSMatt Macy 		 * are its leaves -- until we encounter the next mirror,
15187877fdebSMatt Macy 		 * raidz or draid.
1519eda14cbcSMatt Macy 		 */
15207877fdebSMatt Macy 		if ((type = is_grouping(fulltype, &mindev, &maxdev)) != NULL) {
1521eda14cbcSMatt Macy 			nvlist_t **child = NULL;
1522eda14cbcSMatt Macy 			int c, children = 0;
1523eda14cbcSMatt Macy 
1524eda14cbcSMatt Macy 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1525eda14cbcSMatt Macy 				if (spares != NULL) {
1526eda14cbcSMatt Macy 					(void) fprintf(stderr,
1527eda14cbcSMatt Macy 					    gettext("invalid vdev "
1528eda14cbcSMatt Macy 					    "specification: 'spare' can be "
1529eda14cbcSMatt Macy 					    "specified only once\n"));
1530eda14cbcSMatt Macy 					goto spec_out;
1531eda14cbcSMatt Macy 				}
15327877fdebSMatt Macy 				is_spare = B_TRUE;
1533eda14cbcSMatt Macy 				is_log = is_special = is_dedup = B_FALSE;
1534eda14cbcSMatt Macy 			}
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy 			if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1537eda14cbcSMatt Macy 				if (seen_logs) {
1538eda14cbcSMatt Macy 					(void) fprintf(stderr,
1539eda14cbcSMatt Macy 					    gettext("invalid vdev "
1540eda14cbcSMatt Macy 					    "specification: 'log' can be "
1541eda14cbcSMatt Macy 					    "specified only once\n"));
1542eda14cbcSMatt Macy 					goto spec_out;
1543eda14cbcSMatt Macy 				}
1544eda14cbcSMatt Macy 				seen_logs = B_TRUE;
1545eda14cbcSMatt Macy 				is_log = B_TRUE;
15467877fdebSMatt Macy 				is_special = is_dedup = is_spare = B_FALSE;
1547eda14cbcSMatt Macy 				argc--;
1548eda14cbcSMatt Macy 				argv++;
1549eda14cbcSMatt Macy 				/*
1550eda14cbcSMatt Macy 				 * A log is not a real grouping device.
1551eda14cbcSMatt Macy 				 * We just set is_log and continue.
1552eda14cbcSMatt Macy 				 */
1553eda14cbcSMatt Macy 				continue;
1554eda14cbcSMatt Macy 			}
1555eda14cbcSMatt Macy 
1556eda14cbcSMatt Macy 			if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1557eda14cbcSMatt Macy 				is_special = B_TRUE;
15587877fdebSMatt Macy 				is_log = is_dedup = is_spare = B_FALSE;
1559eda14cbcSMatt Macy 				argc--;
1560eda14cbcSMatt Macy 				argv++;
1561eda14cbcSMatt Macy 				continue;
1562eda14cbcSMatt Macy 			}
1563eda14cbcSMatt Macy 
1564eda14cbcSMatt Macy 			if (strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1565eda14cbcSMatt Macy 				is_dedup = B_TRUE;
15667877fdebSMatt Macy 				is_log = is_special = is_spare = B_FALSE;
1567eda14cbcSMatt Macy 				argc--;
1568eda14cbcSMatt Macy 				argv++;
1569eda14cbcSMatt Macy 				continue;
1570eda14cbcSMatt Macy 			}
1571eda14cbcSMatt Macy 
1572eda14cbcSMatt Macy 			if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1573eda14cbcSMatt Macy 				if (l2cache != NULL) {
1574eda14cbcSMatt Macy 					(void) fprintf(stderr,
1575eda14cbcSMatt Macy 					    gettext("invalid vdev "
1576eda14cbcSMatt Macy 					    "specification: 'cache' can be "
1577eda14cbcSMatt Macy 					    "specified only once\n"));
1578eda14cbcSMatt Macy 					goto spec_out;
1579eda14cbcSMatt Macy 				}
15807877fdebSMatt Macy 				is_log = is_special = B_FALSE;
15817877fdebSMatt Macy 				is_dedup = is_spare = B_FALSE;
1582eda14cbcSMatt Macy 			}
1583eda14cbcSMatt Macy 
1584eda14cbcSMatt Macy 			if (is_log || is_special || is_dedup) {
1585eda14cbcSMatt Macy 				if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1586eda14cbcSMatt Macy 					(void) fprintf(stderr,
1587eda14cbcSMatt Macy 					    gettext("invalid vdev "
1588eda14cbcSMatt Macy 					    "specification: unsupported '%s' "
1589eda14cbcSMatt Macy 					    "device: %s\n"), is_log ? "log" :
1590eda14cbcSMatt Macy 					    "special", type);
1591eda14cbcSMatt Macy 					goto spec_out;
1592eda14cbcSMatt Macy 				}
1593eda14cbcSMatt Macy 				nlogs++;
1594eda14cbcSMatt Macy 			}
1595eda14cbcSMatt Macy 
1596eda14cbcSMatt Macy 			for (c = 1; c < argc; c++) {
1597eda14cbcSMatt Macy 				if (is_grouping(argv[c], NULL, NULL) != NULL)
1598eda14cbcSMatt Macy 					break;
15997877fdebSMatt Macy 
1600eda14cbcSMatt Macy 				children++;
1601eda14cbcSMatt Macy 				child = realloc(child,
1602eda14cbcSMatt Macy 				    children * sizeof (nvlist_t *));
1603eda14cbcSMatt Macy 				if (child == NULL)
1604eda14cbcSMatt Macy 					zpool_no_memory();
1605eda14cbcSMatt Macy 				if ((nv = make_leaf_vdev(props, argv[c],
16067877fdebSMatt Macy 				    !(is_log || is_special || is_dedup ||
16077877fdebSMatt Macy 				    is_spare))) == NULL) {
1608eda14cbcSMatt Macy 					for (c = 0; c < children - 1; c++)
1609eda14cbcSMatt Macy 						nvlist_free(child[c]);
1610eda14cbcSMatt Macy 					free(child);
1611eda14cbcSMatt Macy 					goto spec_out;
1612eda14cbcSMatt Macy 				}
1613eda14cbcSMatt Macy 
1614eda14cbcSMatt Macy 				child[children - 1] = nv;
1615eda14cbcSMatt Macy 			}
1616eda14cbcSMatt Macy 
1617eda14cbcSMatt Macy 			if (children < mindev) {
1618eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid vdev "
1619eda14cbcSMatt Macy 				    "specification: %s requires at least %d "
1620eda14cbcSMatt Macy 				    "devices\n"), argv[0], mindev);
1621eda14cbcSMatt Macy 				for (c = 0; c < children; c++)
1622eda14cbcSMatt Macy 					nvlist_free(child[c]);
1623eda14cbcSMatt Macy 				free(child);
1624eda14cbcSMatt Macy 				goto spec_out;
1625eda14cbcSMatt Macy 			}
1626eda14cbcSMatt Macy 
1627eda14cbcSMatt Macy 			if (children > maxdev) {
1628eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("invalid vdev "
1629eda14cbcSMatt Macy 				    "specification: %s supports no more than "
1630eda14cbcSMatt Macy 				    "%d devices\n"), argv[0], maxdev);
1631eda14cbcSMatt Macy 				for (c = 0; c < children; c++)
1632eda14cbcSMatt Macy 					nvlist_free(child[c]);
1633eda14cbcSMatt Macy 				free(child);
1634eda14cbcSMatt Macy 				goto spec_out;
1635eda14cbcSMatt Macy 			}
1636eda14cbcSMatt Macy 
1637eda14cbcSMatt Macy 			argc -= c;
1638eda14cbcSMatt Macy 			argv += c;
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1641eda14cbcSMatt Macy 				spares = child;
1642eda14cbcSMatt Macy 				nspares = children;
1643eda14cbcSMatt Macy 				continue;
1644eda14cbcSMatt Macy 			} else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1645eda14cbcSMatt Macy 				l2cache = child;
1646eda14cbcSMatt Macy 				nl2cache = children;
1647eda14cbcSMatt Macy 				continue;
1648eda14cbcSMatt Macy 			} else {
1649eda14cbcSMatt Macy 				/* create a top-level vdev with children */
1650eda14cbcSMatt Macy 				verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1651eda14cbcSMatt Macy 				    0) == 0);
1652eda14cbcSMatt Macy 				verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1653eda14cbcSMatt Macy 				    type) == 0);
1654eda14cbcSMatt Macy 				verify(nvlist_add_uint64(nv,
1655eda14cbcSMatt Macy 				    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
16567877fdebSMatt Macy 				if (is_log) {
1657eda14cbcSMatt Macy 					verify(nvlist_add_string(nv,
1658eda14cbcSMatt Macy 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1659eda14cbcSMatt Macy 					    VDEV_ALLOC_BIAS_LOG) == 0);
16607877fdebSMatt Macy 				}
1661eda14cbcSMatt Macy 				if (is_special) {
1662eda14cbcSMatt Macy 					verify(nvlist_add_string(nv,
1663eda14cbcSMatt Macy 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1664eda14cbcSMatt Macy 					    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1665eda14cbcSMatt Macy 				}
1666eda14cbcSMatt Macy 				if (is_dedup) {
1667eda14cbcSMatt Macy 					verify(nvlist_add_string(nv,
1668eda14cbcSMatt Macy 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1669eda14cbcSMatt Macy 					    VDEV_ALLOC_BIAS_DEDUP) == 0);
1670eda14cbcSMatt Macy 				}
1671eda14cbcSMatt Macy 				if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1672eda14cbcSMatt Macy 					verify(nvlist_add_uint64(nv,
1673eda14cbcSMatt Macy 					    ZPOOL_CONFIG_NPARITY,
1674eda14cbcSMatt Macy 					    mindev - 1) == 0);
1675eda14cbcSMatt Macy 				}
16767877fdebSMatt Macy 				if (strcmp(type, VDEV_TYPE_DRAID) == 0) {
16777877fdebSMatt Macy 					if (draid_config_by_type(nv,
16787877fdebSMatt Macy 					    fulltype, children) != 0) {
16797877fdebSMatt Macy 						for (c = 0; c < children; c++)
16807877fdebSMatt Macy 							nvlist_free(child[c]);
16817877fdebSMatt Macy 						free(child);
16827877fdebSMatt Macy 						goto spec_out;
16837877fdebSMatt Macy 					}
16847877fdebSMatt Macy 				}
1685eda14cbcSMatt Macy 				verify(nvlist_add_nvlist_array(nv,
1686681ce946SMartin Matuska 				    ZPOOL_CONFIG_CHILDREN,
1687681ce946SMartin Matuska 				    (const nvlist_t **)child, children) == 0);
1688eda14cbcSMatt Macy 
1689eda14cbcSMatt Macy 				for (c = 0; c < children; c++)
1690eda14cbcSMatt Macy 					nvlist_free(child[c]);
1691eda14cbcSMatt Macy 				free(child);
1692eda14cbcSMatt Macy 			}
1693eda14cbcSMatt Macy 		} else {
1694eda14cbcSMatt Macy 			/*
1695eda14cbcSMatt Macy 			 * We have a device.  Pass off to make_leaf_vdev() to
1696eda14cbcSMatt Macy 			 * construct the appropriate nvlist describing the vdev.
1697eda14cbcSMatt Macy 			 */
16987877fdebSMatt Macy 			if ((nv = make_leaf_vdev(props, argv[0], !(is_log ||
16997877fdebSMatt Macy 			    is_special || is_dedup || is_spare))) == NULL)
1700eda14cbcSMatt Macy 				goto spec_out;
1701eda14cbcSMatt Macy 
17027877fdebSMatt Macy 			verify(nvlist_add_uint64(nv,
17037877fdebSMatt Macy 			    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
17047877fdebSMatt Macy 			if (is_log) {
17057877fdebSMatt Macy 				verify(nvlist_add_string(nv,
17067877fdebSMatt Macy 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
17077877fdebSMatt Macy 				    VDEV_ALLOC_BIAS_LOG) == 0);
1708eda14cbcSMatt Macy 				nlogs++;
17097877fdebSMatt Macy 			}
17107877fdebSMatt Macy 
1711eda14cbcSMatt Macy 			if (is_special) {
1712eda14cbcSMatt Macy 				verify(nvlist_add_string(nv,
1713eda14cbcSMatt Macy 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1714eda14cbcSMatt Macy 				    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1715eda14cbcSMatt Macy 			}
1716eda14cbcSMatt Macy 			if (is_dedup) {
1717eda14cbcSMatt Macy 				verify(nvlist_add_string(nv,
1718eda14cbcSMatt Macy 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1719eda14cbcSMatt Macy 				    VDEV_ALLOC_BIAS_DEDUP) == 0);
1720eda14cbcSMatt Macy 			}
1721eda14cbcSMatt Macy 			argc--;
1722eda14cbcSMatt Macy 			argv++;
1723eda14cbcSMatt Macy 		}
1724eda14cbcSMatt Macy 
1725eda14cbcSMatt Macy 		toplevels++;
1726eda14cbcSMatt Macy 		top = realloc(top, toplevels * sizeof (nvlist_t *));
1727eda14cbcSMatt Macy 		if (top == NULL)
1728eda14cbcSMatt Macy 			zpool_no_memory();
1729eda14cbcSMatt Macy 		top[toplevels - 1] = nv;
1730eda14cbcSMatt Macy 	}
1731eda14cbcSMatt Macy 
1732eda14cbcSMatt Macy 	if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1733eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid vdev "
1734eda14cbcSMatt Macy 		    "specification: at least one toplevel vdev must be "
1735eda14cbcSMatt Macy 		    "specified\n"));
1736eda14cbcSMatt Macy 		goto spec_out;
1737eda14cbcSMatt Macy 	}
1738eda14cbcSMatt Macy 
1739eda14cbcSMatt Macy 	if (seen_logs && nlogs == 0) {
1740eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("invalid vdev specification: "
1741eda14cbcSMatt Macy 		    "log requires at least 1 device\n"));
1742eda14cbcSMatt Macy 		goto spec_out;
1743eda14cbcSMatt Macy 	}
1744eda14cbcSMatt Macy 
1745eda14cbcSMatt Macy 	/*
1746eda14cbcSMatt Macy 	 * Finally, create nvroot and add all top-level vdevs to it.
1747eda14cbcSMatt Macy 	 */
1748eda14cbcSMatt Macy 	verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1749eda14cbcSMatt Macy 	verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1750eda14cbcSMatt Macy 	    VDEV_TYPE_ROOT) == 0);
1751eda14cbcSMatt Macy 	verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1752681ce946SMartin Matuska 	    (const nvlist_t **)top, toplevels) == 0);
1753eda14cbcSMatt Macy 	if (nspares != 0)
1754eda14cbcSMatt Macy 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1755681ce946SMartin Matuska 		    (const nvlist_t **)spares, nspares) == 0);
1756eda14cbcSMatt Macy 	if (nl2cache != 0)
1757eda14cbcSMatt Macy 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1758681ce946SMartin Matuska 		    (const nvlist_t **)l2cache, nl2cache) == 0);
1759eda14cbcSMatt Macy 
1760eda14cbcSMatt Macy spec_out:
1761eda14cbcSMatt Macy 	for (t = 0; t < toplevels; t++)
1762eda14cbcSMatt Macy 		nvlist_free(top[t]);
1763eda14cbcSMatt Macy 	for (t = 0; t < nspares; t++)
1764eda14cbcSMatt Macy 		nvlist_free(spares[t]);
1765eda14cbcSMatt Macy 	for (t = 0; t < nl2cache; t++)
1766eda14cbcSMatt Macy 		nvlist_free(l2cache[t]);
1767eda14cbcSMatt Macy 
1768eda14cbcSMatt Macy 	free(spares);
1769eda14cbcSMatt Macy 	free(l2cache);
1770eda14cbcSMatt Macy 	free(top);
1771eda14cbcSMatt Macy 
1772eda14cbcSMatt Macy 	return (nvroot);
1773eda14cbcSMatt Macy }
1774eda14cbcSMatt Macy 
1775eda14cbcSMatt Macy nvlist_t *
split_mirror_vdev(zpool_handle_t * zhp,char * newname,nvlist_t * props,splitflags_t flags,int argc,char ** argv)1776eda14cbcSMatt Macy split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1777eda14cbcSMatt Macy     splitflags_t flags, int argc, char **argv)
1778eda14cbcSMatt Macy {
1779eda14cbcSMatt Macy 	nvlist_t *newroot = NULL, **child;
1780eda14cbcSMatt Macy 	uint_t c, children;
1781eda14cbcSMatt Macy 
1782eda14cbcSMatt Macy 	if (argc > 0) {
1783eda14cbcSMatt Macy 		if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1784eda14cbcSMatt Macy 			(void) fprintf(stderr, gettext("Unable to build a "
1785eda14cbcSMatt Macy 			    "pool from the specified devices\n"));
1786eda14cbcSMatt Macy 			return (NULL);
1787eda14cbcSMatt Macy 		}
1788eda14cbcSMatt Macy 
1789abcdc1b9SMartin Matuska 		if (!flags.dryrun && make_disks(zhp, newroot, B_FALSE) != 0) {
1790eda14cbcSMatt Macy 			nvlist_free(newroot);
1791eda14cbcSMatt Macy 			return (NULL);
1792eda14cbcSMatt Macy 		}
1793eda14cbcSMatt Macy 
1794eda14cbcSMatt Macy 		/* avoid any tricks in the spec */
1795eda14cbcSMatt Macy 		verify(nvlist_lookup_nvlist_array(newroot,
1796eda14cbcSMatt Macy 		    ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1797eda14cbcSMatt Macy 		for (c = 0; c < children; c++) {
17982a58b312SMartin Matuska 			const char *path;
1799eda14cbcSMatt Macy 			const char *type;
1800eda14cbcSMatt Macy 			int min, max;
1801eda14cbcSMatt Macy 
1802eda14cbcSMatt Macy 			verify(nvlist_lookup_string(child[c],
1803eda14cbcSMatt Macy 			    ZPOOL_CONFIG_PATH, &path) == 0);
1804eda14cbcSMatt Macy 			if ((type = is_grouping(path, &min, &max)) != NULL) {
1805eda14cbcSMatt Macy 				(void) fprintf(stderr, gettext("Cannot use "
1806eda14cbcSMatt Macy 				    "'%s' as a device for splitting\n"), type);
1807eda14cbcSMatt Macy 				nvlist_free(newroot);
1808eda14cbcSMatt Macy 				return (NULL);
1809eda14cbcSMatt Macy 			}
1810eda14cbcSMatt Macy 		}
1811eda14cbcSMatt Macy 	}
1812eda14cbcSMatt Macy 
1813eda14cbcSMatt Macy 	if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1814eda14cbcSMatt Macy 		nvlist_free(newroot);
1815eda14cbcSMatt Macy 		return (NULL);
1816eda14cbcSMatt Macy 	}
1817eda14cbcSMatt Macy 
1818eda14cbcSMatt Macy 	return (newroot);
1819eda14cbcSMatt Macy }
1820eda14cbcSMatt Macy 
1821eda14cbcSMatt Macy static int
num_normal_vdevs(nvlist_t * nvroot)1822eda14cbcSMatt Macy num_normal_vdevs(nvlist_t *nvroot)
1823eda14cbcSMatt Macy {
1824eda14cbcSMatt Macy 	nvlist_t **top;
1825eda14cbcSMatt Macy 	uint_t t, toplevels, normal = 0;
1826eda14cbcSMatt Macy 
1827eda14cbcSMatt Macy 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1828eda14cbcSMatt Macy 	    &top, &toplevels) == 0);
1829eda14cbcSMatt Macy 
1830eda14cbcSMatt Macy 	for (t = 0; t < toplevels; t++) {
1831eda14cbcSMatt Macy 		uint64_t log = B_FALSE;
1832eda14cbcSMatt Macy 
1833eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(top[t], ZPOOL_CONFIG_IS_LOG, &log);
1834eda14cbcSMatt Macy 		if (log)
1835eda14cbcSMatt Macy 			continue;
1836eda14cbcSMatt Macy 		if (nvlist_exists(top[t], ZPOOL_CONFIG_ALLOCATION_BIAS))
1837eda14cbcSMatt Macy 			continue;
1838eda14cbcSMatt Macy 
1839eda14cbcSMatt Macy 		normal++;
1840eda14cbcSMatt Macy 	}
1841eda14cbcSMatt Macy 
1842eda14cbcSMatt Macy 	return (normal);
1843eda14cbcSMatt Macy }
1844eda14cbcSMatt Macy 
1845eda14cbcSMatt Macy /*
1846eda14cbcSMatt Macy  * Get and validate the contents of the given vdev specification.  This ensures
1847eda14cbcSMatt Macy  * that the nvlist returned is well-formed, that all the devices exist, and that
1848eda14cbcSMatt Macy  * they are not currently in use by any other known consumer.  The 'poolconfig'
1849eda14cbcSMatt Macy  * parameter is the current configuration of the pool when adding devices
1850eda14cbcSMatt Macy  * existing pool, and is used to perform additional checks, such as changing the
1851eda14cbcSMatt Macy  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1852eda14cbcSMatt Macy  * new pool.  The 'force' flag controls whether devices should be forcefully
1853eda14cbcSMatt Macy  * added, even if they appear in use.
1854eda14cbcSMatt Macy  */
1855eda14cbcSMatt Macy nvlist_t *
make_root_vdev(zpool_handle_t * zhp,nvlist_t * props,int force,int check_rep,boolean_t replacing,boolean_t dryrun,int argc,char ** argv)1856eda14cbcSMatt Macy make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1857eda14cbcSMatt Macy     boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1858eda14cbcSMatt Macy {
1859eda14cbcSMatt Macy 	nvlist_t *newroot;
1860eda14cbcSMatt Macy 	nvlist_t *poolconfig = NULL;
1861eda14cbcSMatt Macy 	is_force = force;
1862eda14cbcSMatt Macy 
1863eda14cbcSMatt Macy 	/*
1864eda14cbcSMatt Macy 	 * Construct the vdev specification.  If this is successful, we know
1865eda14cbcSMatt Macy 	 * that we have a valid specification, and that all devices can be
1866eda14cbcSMatt Macy 	 * opened.
1867eda14cbcSMatt Macy 	 */
1868eda14cbcSMatt Macy 	if ((newroot = construct_spec(props, argc, argv)) == NULL)
1869eda14cbcSMatt Macy 		return (NULL);
1870eda14cbcSMatt Macy 
1871eda14cbcSMatt Macy 	if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
1872eda14cbcSMatt Macy 		nvlist_free(newroot);
1873eda14cbcSMatt Macy 		return (NULL);
1874eda14cbcSMatt Macy 	}
1875eda14cbcSMatt Macy 
1876eda14cbcSMatt Macy 	/*
1877eda14cbcSMatt Macy 	 * Validate each device to make sure that it's not shared with another
1878eda14cbcSMatt Macy 	 * subsystem.  We do this even if 'force' is set, because there are some
1879eda14cbcSMatt Macy 	 * uses (such as a dedicated dump device) that even '-f' cannot
1880eda14cbcSMatt Macy 	 * override.
1881eda14cbcSMatt Macy 	 */
1882eda14cbcSMatt Macy 	if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1883eda14cbcSMatt Macy 		nvlist_free(newroot);
1884eda14cbcSMatt Macy 		return (NULL);
1885eda14cbcSMatt Macy 	}
1886eda14cbcSMatt Macy 
1887eda14cbcSMatt Macy 	/*
1888eda14cbcSMatt Macy 	 * Check the replication level of the given vdevs and report any errors
1889eda14cbcSMatt Macy 	 * found.  We include the existing pool spec, if any, as we need to
1890eda14cbcSMatt Macy 	 * catch changes against the existing replication level.
1891eda14cbcSMatt Macy 	 */
1892eda14cbcSMatt Macy 	if (check_rep && check_replication(poolconfig, newroot) != 0) {
1893eda14cbcSMatt Macy 		nvlist_free(newroot);
1894eda14cbcSMatt Macy 		return (NULL);
1895eda14cbcSMatt Macy 	}
1896eda14cbcSMatt Macy 
1897eda14cbcSMatt Macy 	/*
1898eda14cbcSMatt Macy 	 * On pool create the new vdev spec must have one normal vdev.
1899eda14cbcSMatt Macy 	 */
1900eda14cbcSMatt Macy 	if (poolconfig == NULL && num_normal_vdevs(newroot) == 0) {
1901eda14cbcSMatt Macy 		vdev_error(gettext("at least one general top-level vdev must "
1902eda14cbcSMatt Macy 		    "be specified\n"));
1903eda14cbcSMatt Macy 		nvlist_free(newroot);
1904eda14cbcSMatt Macy 		return (NULL);
1905eda14cbcSMatt Macy 	}
1906eda14cbcSMatt Macy 
1907eda14cbcSMatt Macy 	/*
1908eda14cbcSMatt Macy 	 * Run through the vdev specification and label any whole disks found.
1909eda14cbcSMatt Macy 	 */
1910abcdc1b9SMartin Matuska 	if (!dryrun && make_disks(zhp, newroot, replacing) != 0) {
1911eda14cbcSMatt Macy 		nvlist_free(newroot);
1912eda14cbcSMatt Macy 		return (NULL);
1913eda14cbcSMatt Macy 	}
1914eda14cbcSMatt Macy 
1915eda14cbcSMatt Macy 	return (newroot);
1916eda14cbcSMatt Macy }
1917