1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2016, 2017 Intel Corporation.
26  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
27  */
28 
29 /*
30  * Functions to convert between a list of vdevs and an nvlist representing the
31  * configuration.  Each entry in the list can be one of:
32  *
33  * 	Device vdevs
34  * 		disk=(path=..., devid=...)
35  * 		file=(path=...)
36  *
37  * 	Group vdevs
38  * 		raidz[1|2]=(...)
39  * 		mirror=(...)
40  *
41  * 	Hot spares
42  *
43  * While the underlying implementation supports it, group vdevs cannot contain
44  * other group vdevs.  All userland verification of devices is contained within
45  * this file.  If successful, the nvlist returned can be passed directly to the
46  * kernel; we've done as much verification as possible in userland.
47  *
48  * Hot spares are a special case, and passed down as an array of disk vdevs, at
49  * the same level as the root of the vdev tree.
50  *
51  * The only function exported by this file is 'make_root_vdev'.  The
52  * function performs several passes:
53  *
54  * 	1. Construct the vdev specification.  Performs syntax validation and
55  *         makes sure each device is valid.
56  * 	2. Check for devices in use.  Using libblkid to make sure that no
57  *         devices are also in use.  Some can be overridden using the 'force'
58  *         flag, others cannot.
59  * 	3. Check for replication errors if the 'force' flag is not specified.
60  *         validates that the replication level is consistent across the
61  *         entire pool.
62  * 	4. Call libzfs to label any whole disks with an EFI label.
63  */
64 
65 #include <assert.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <libintl.h>
70 #include <libnvpair.h>
71 #include <libzutil.h>
72 #include <limits.h>
73 #include <sys/spa.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include "zpool_util.h"
78 #include <sys/zfs_context.h>
79 #include <sys/stat.h>
80 
81 /*
82  * For any given vdev specification, we can have multiple errors.  The
83  * vdev_error() function keeps track of whether we have seen an error yet, and
84  * prints out a header if its the first error we've seen.
85  */
86 boolean_t error_seen;
87 boolean_t is_force;
88 
89 
90 
91 
92 /*PRINTFLIKE1*/
93 void
94 vdev_error(const char *fmt, ...)
95 {
96 	va_list ap;
97 
98 	if (!error_seen) {
99 		(void) fprintf(stderr, gettext("invalid vdev specification\n"));
100 		if (!is_force)
101 			(void) fprintf(stderr, gettext("use '-f' to override "
102 			    "the following errors:\n"));
103 		else
104 			(void) fprintf(stderr, gettext("the following errors "
105 			    "must be manually repaired:\n"));
106 		error_seen = B_TRUE;
107 	}
108 
109 	va_start(ap, fmt);
110 	(void) vfprintf(stderr, fmt, ap);
111 	va_end(ap);
112 }
113 
114 /*
115  * Check that a file is valid.  All we can do in this case is check that it's
116  * not in use by another pool, and not in use by swap.
117  */
118 int
119 check_file(const char *file, boolean_t force, boolean_t isspare)
120 {
121 	char  *name;
122 	int fd;
123 	int ret = 0;
124 	pool_state_t state;
125 	boolean_t inuse;
126 
127 	if ((fd = open(file, O_RDONLY)) < 0)
128 		return (0);
129 
130 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
131 		const char *desc;
132 
133 		switch (state) {
134 		case POOL_STATE_ACTIVE:
135 			desc = gettext("active");
136 			break;
137 
138 		case POOL_STATE_EXPORTED:
139 			desc = gettext("exported");
140 			break;
141 
142 		case POOL_STATE_POTENTIALLY_ACTIVE:
143 			desc = gettext("potentially active");
144 			break;
145 
146 		default:
147 			desc = gettext("unknown");
148 			break;
149 		}
150 
151 		/*
152 		 * Allow hot spares to be shared between pools.
153 		 */
154 		if (state == POOL_STATE_SPARE && isspare) {
155 			free(name);
156 			(void) close(fd);
157 			return (0);
158 		}
159 
160 		if (state == POOL_STATE_ACTIVE ||
161 		    state == POOL_STATE_SPARE || !force) {
162 			switch (state) {
163 			case POOL_STATE_SPARE:
164 				vdev_error(gettext("%s is reserved as a hot "
165 				    "spare for pool %s\n"), file, name);
166 				break;
167 			default:
168 				vdev_error(gettext("%s is part of %s pool "
169 				    "'%s'\n"), file, desc, name);
170 				break;
171 			}
172 			ret = -1;
173 		}
174 
175 		free(name);
176 	}
177 
178 	(void) close(fd);
179 	return (ret);
180 }
181 
182 /*
183  * This may be a shorthand device path or it could be total gibberish.
184  * Check to see if it is a known device available in zfs_vdev_paths.
185  * As part of this check, see if we've been given an entire disk
186  * (minus the slice number).
187  */
188 static int
189 is_shorthand_path(const char *arg, char *path, size_t path_size,
190     struct stat64 *statbuf, boolean_t *wholedisk)
191 {
192 	int error;
193 
194 	error = zfs_resolve_shortname(arg, path, path_size);
195 	if (error == 0) {
196 		*wholedisk = zfs_dev_is_whole_disk(path);
197 		if (*wholedisk || (stat64(path, statbuf) == 0))
198 			return (0);
199 	}
200 
201 	strlcpy(path, arg, path_size);
202 	memset(statbuf, 0, sizeof (*statbuf));
203 	*wholedisk = B_FALSE;
204 
205 	return (error);
206 }
207 
208 /*
209  * Determine if the given path is a hot spare within the given configuration.
210  * If no configuration is given we rely solely on the label.
211  */
212 static boolean_t
213 is_spare(nvlist_t *config, const char *path)
214 {
215 	int fd;
216 	pool_state_t state;
217 	char *name = NULL;
218 	nvlist_t *label;
219 	uint64_t guid, spareguid;
220 	nvlist_t *nvroot;
221 	nvlist_t **spares;
222 	uint_t i, nspares;
223 	boolean_t inuse;
224 
225 	if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
226 		return (B_FALSE);
227 
228 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
229 	    !inuse ||
230 	    state != POOL_STATE_SPARE ||
231 	    zpool_read_label(fd, &label, NULL) != 0) {
232 		free(name);
233 		(void) close(fd);
234 		return (B_FALSE);
235 	}
236 	free(name);
237 	(void) close(fd);
238 
239 	if (config == NULL) {
240 		nvlist_free(label);
241 		return (B_TRUE);
242 	}
243 
244 	verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
245 	nvlist_free(label);
246 
247 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
248 	    &nvroot) == 0);
249 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
250 	    &spares, &nspares) == 0) {
251 		for (i = 0; i < nspares; i++) {
252 			verify(nvlist_lookup_uint64(spares[i],
253 			    ZPOOL_CONFIG_GUID, &spareguid) == 0);
254 			if (spareguid == guid)
255 				return (B_TRUE);
256 		}
257 	}
258 
259 	return (B_FALSE);
260 }
261 
262 /*
263  * Create a leaf vdev.  Determine if this is a file or a device.  If it's a
264  * device, fill in the device id to make a complete nvlist.  Valid forms for a
265  * leaf vdev are:
266  *
267  *	/dev/xxx	Complete disk path
268  *	/xxx		Full path to file
269  *	xxx		Shorthand for <zfs_vdev_paths>/xxx
270  */
271 static nvlist_t *
272 make_leaf_vdev(nvlist_t *props, const char *arg, uint64_t is_log)
273 {
274 	char path[MAXPATHLEN];
275 	struct stat64 statbuf;
276 	nvlist_t *vdev = NULL;
277 	char *type = NULL;
278 	boolean_t wholedisk = B_FALSE;
279 	uint64_t ashift = 0;
280 	int err;
281 
282 	/*
283 	 * Determine what type of vdev this is, and put the full path into
284 	 * 'path'.  We detect whether this is a device of file afterwards by
285 	 * checking the st_mode of the file.
286 	 */
287 	if (arg[0] == '/') {
288 		/*
289 		 * Complete device or file path.  Exact type is determined by
290 		 * examining the file descriptor afterwards.  Symbolic links
291 		 * are resolved to their real paths to determine whole disk
292 		 * and S_ISBLK/S_ISREG type checks.  However, we are careful
293 		 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
294 		 * can leverage udev's persistent device labels.
295 		 */
296 		if (realpath(arg, path) == NULL) {
297 			(void) fprintf(stderr,
298 			    gettext("cannot resolve path '%s'\n"), arg);
299 			return (NULL);
300 		}
301 
302 		wholedisk = zfs_dev_is_whole_disk(path);
303 		if (!wholedisk && (stat64(path, &statbuf) != 0)) {
304 			(void) fprintf(stderr,
305 			    gettext("cannot open '%s': %s\n"),
306 			    path, strerror(errno));
307 			return (NULL);
308 		}
309 
310 		/* After whole disk check restore original passed path */
311 		strlcpy(path, arg, sizeof (path));
312 	} else {
313 		err = is_shorthand_path(arg, path, sizeof (path),
314 		    &statbuf, &wholedisk);
315 		if (err != 0) {
316 			/*
317 			 * If we got ENOENT, then the user gave us
318 			 * gibberish, so try to direct them with a
319 			 * reasonable error message.  Otherwise,
320 			 * regurgitate strerror() since it's the best we
321 			 * can do.
322 			 */
323 			if (err == ENOENT) {
324 				(void) fprintf(stderr,
325 				    gettext("cannot open '%s': no such "
326 				    "device in %s\n"), arg, DISK_ROOT);
327 				(void) fprintf(stderr,
328 				    gettext("must be a full path or "
329 				    "shorthand device name\n"));
330 				return (NULL);
331 			} else {
332 				(void) fprintf(stderr,
333 				    gettext("cannot open '%s': %s\n"),
334 				    path, strerror(errno));
335 				return (NULL);
336 			}
337 		}
338 	}
339 
340 	/*
341 	 * Determine whether this is a device or a file.
342 	 */
343 	if (wholedisk || S_ISBLK(statbuf.st_mode)) {
344 		type = VDEV_TYPE_DISK;
345 	} else if (S_ISREG(statbuf.st_mode)) {
346 		type = VDEV_TYPE_FILE;
347 	} else {
348 		(void) fprintf(stderr, gettext("cannot use '%s': must be a "
349 		    "block device or regular file\n"), path);
350 		return (NULL);
351 	}
352 
353 	/*
354 	 * Finally, we have the complete device or file, and we know that it is
355 	 * acceptable to use.  Construct the nvlist to describe this vdev.  All
356 	 * vdevs have a 'path' element, and devices also have a 'devid' element.
357 	 */
358 	verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
359 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
360 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
361 	verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
362 	if (is_log)
363 		verify(nvlist_add_string(vdev, ZPOOL_CONFIG_ALLOCATION_BIAS,
364 		    VDEV_ALLOC_BIAS_LOG) == 0);
365 	if (strcmp(type, VDEV_TYPE_DISK) == 0)
366 		verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
367 		    (uint64_t)wholedisk) == 0);
368 
369 	/*
370 	 * Override defaults if custom properties are provided.
371 	 */
372 	if (props != NULL) {
373 		char *value = NULL;
374 
375 		if (nvlist_lookup_string(props,
376 		    zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0) {
377 			if (zfs_nicestrtonum(NULL, value, &ashift) != 0) {
378 				(void) fprintf(stderr,
379 				    gettext("ashift must be a number.\n"));
380 				return (NULL);
381 			}
382 			if (ashift != 0 &&
383 			    (ashift < ASHIFT_MIN || ashift > ASHIFT_MAX)) {
384 				(void) fprintf(stderr,
385 				    gettext("invalid 'ashift=%" PRIu64 "' "
386 				    "property: only values between %" PRId32 " "
387 				    "and %" PRId32 " are allowed.\n"),
388 				    ashift, ASHIFT_MIN, ASHIFT_MAX);
389 				return (NULL);
390 			}
391 		}
392 	}
393 
394 	/*
395 	 * If the device is known to incorrectly report its physical sector
396 	 * size explicitly provide the known correct value.
397 	 */
398 	if (ashift == 0) {
399 		int sector_size;
400 
401 		if (check_sector_size_database(path, &sector_size) == B_TRUE)
402 			ashift = highbit64(sector_size) - 1;
403 	}
404 
405 	if (ashift > 0)
406 		(void) nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
407 
408 	return (vdev);
409 }
410 
411 /*
412  * Go through and verify the replication level of the pool is consistent.
413  * Performs the following checks:
414  *
415  * 	For the new spec, verifies that devices in mirrors and raidz are the
416  * 	same size.
417  *
418  * 	If the current configuration already has inconsistent replication
419  * 	levels, ignore any other potential problems in the new spec.
420  *
421  * 	Otherwise, make sure that the current spec (if there is one) and the new
422  * 	spec have consistent replication levels.
423  *
424  *	If there is no current spec (create), make sure new spec has at least
425  *	one general purpose vdev.
426  */
427 typedef struct replication_level {
428 	char *zprl_type;
429 	uint64_t zprl_children;
430 	uint64_t zprl_parity;
431 } replication_level_t;
432 
433 #define	ZPOOL_FUZZ	(16 * 1024 * 1024)
434 
435 static boolean_t
436 is_raidz_mirror(replication_level_t *a, replication_level_t *b,
437     replication_level_t **raidz, replication_level_t **mirror)
438 {
439 	if (strcmp(a->zprl_type, "raidz") == 0 &&
440 	    strcmp(b->zprl_type, "mirror") == 0) {
441 		*raidz = a;
442 		*mirror = b;
443 		return (B_TRUE);
444 	}
445 	return (B_FALSE);
446 }
447 
448 /*
449  * Given a list of toplevel vdevs, return the current replication level.  If
450  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
451  * an error message will be displayed for each self-inconsistent vdev.
452  */
453 static replication_level_t *
454 get_replication(nvlist_t *nvroot, boolean_t fatal)
455 {
456 	nvlist_t **top;
457 	uint_t t, toplevels;
458 	nvlist_t **child;
459 	uint_t c, children;
460 	nvlist_t *nv;
461 	char *type;
462 	replication_level_t lastrep = {0};
463 	replication_level_t rep;
464 	replication_level_t *ret;
465 	replication_level_t *raidz, *mirror;
466 	boolean_t dontreport;
467 
468 	ret = safe_malloc(sizeof (replication_level_t));
469 
470 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
471 	    &top, &toplevels) == 0);
472 
473 	for (t = 0; t < toplevels; t++) {
474 		uint64_t is_log = B_FALSE;
475 
476 		nv = top[t];
477 
478 		/*
479 		 * For separate logs we ignore the top level vdev replication
480 		 * constraints.
481 		 */
482 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
483 		if (is_log)
484 			continue;
485 
486 		/* Ignore holes introduced by removing aux devices */
487 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
488 		if (strcmp(type, VDEV_TYPE_HOLE) == 0)
489 			continue;
490 
491 		if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
492 		    &child, &children) != 0) {
493 			/*
494 			 * This is a 'file' or 'disk' vdev.
495 			 */
496 			rep.zprl_type = type;
497 			rep.zprl_children = 1;
498 			rep.zprl_parity = 0;
499 		} else {
500 			int64_t vdev_size;
501 
502 			/*
503 			 * This is a mirror or RAID-Z vdev.  Go through and make
504 			 * sure the contents are all the same (files vs. disks),
505 			 * keeping track of the number of elements in the
506 			 * process.
507 			 *
508 			 * We also check that the size of each vdev (if it can
509 			 * be determined) is the same.
510 			 */
511 			rep.zprl_type = type;
512 			rep.zprl_children = 0;
513 
514 			if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
515 				verify(nvlist_lookup_uint64(nv,
516 				    ZPOOL_CONFIG_NPARITY,
517 				    &rep.zprl_parity) == 0);
518 				assert(rep.zprl_parity != 0);
519 			} else {
520 				rep.zprl_parity = 0;
521 			}
522 
523 			/*
524 			 * The 'dontreport' variable indicates that we've
525 			 * already reported an error for this spec, so don't
526 			 * bother doing it again.
527 			 */
528 			type = NULL;
529 			dontreport = 0;
530 			vdev_size = -1LL;
531 			for (c = 0; c < children; c++) {
532 				nvlist_t *cnv = child[c];
533 				char *path;
534 				struct stat64 statbuf;
535 				int64_t size = -1LL;
536 				char *childtype;
537 				int fd, err;
538 
539 				rep.zprl_children++;
540 
541 				verify(nvlist_lookup_string(cnv,
542 				    ZPOOL_CONFIG_TYPE, &childtype) == 0);
543 
544 				/*
545 				 * If this is a replacing or spare vdev, then
546 				 * get the real first child of the vdev: do this
547 				 * in a loop because replacing and spare vdevs
548 				 * can be nested.
549 				 */
550 				while (strcmp(childtype,
551 				    VDEV_TYPE_REPLACING) == 0 ||
552 				    strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
553 					nvlist_t **rchild;
554 					uint_t rchildren;
555 
556 					verify(nvlist_lookup_nvlist_array(cnv,
557 					    ZPOOL_CONFIG_CHILDREN, &rchild,
558 					    &rchildren) == 0);
559 					assert(rchildren == 2);
560 					cnv = rchild[0];
561 
562 					verify(nvlist_lookup_string(cnv,
563 					    ZPOOL_CONFIG_TYPE,
564 					    &childtype) == 0);
565 				}
566 
567 				verify(nvlist_lookup_string(cnv,
568 				    ZPOOL_CONFIG_PATH, &path) == 0);
569 
570 				/*
571 				 * If we have a raidz/mirror that combines disks
572 				 * with files, report it as an error.
573 				 */
574 				if (!dontreport && type != NULL &&
575 				    strcmp(type, childtype) != 0) {
576 					if (ret != NULL)
577 						free(ret);
578 					ret = NULL;
579 					if (fatal)
580 						vdev_error(gettext(
581 						    "mismatched replication "
582 						    "level: %s contains both "
583 						    "files and devices\n"),
584 						    rep.zprl_type);
585 					else
586 						return (NULL);
587 					dontreport = B_TRUE;
588 				}
589 
590 				/*
591 				 * According to stat(2), the value of 'st_size'
592 				 * is undefined for block devices and character
593 				 * devices.  But there is no effective way to
594 				 * determine the real size in userland.
595 				 *
596 				 * Instead, we'll take advantage of an
597 				 * implementation detail of spec_size().  If the
598 				 * device is currently open, then we (should)
599 				 * return a valid size.
600 				 *
601 				 * If we still don't get a valid size (indicated
602 				 * by a size of 0 or MAXOFFSET_T), then ignore
603 				 * this device altogether.
604 				 */
605 				if ((fd = open(path, O_RDONLY)) >= 0) {
606 					err = fstat64_blk(fd, &statbuf);
607 					(void) close(fd);
608 				} else {
609 					err = stat64(path, &statbuf);
610 				}
611 
612 				if (err != 0 ||
613 				    statbuf.st_size == 0 ||
614 				    statbuf.st_size == MAXOFFSET_T)
615 					continue;
616 
617 				size = statbuf.st_size;
618 
619 				/*
620 				 * Also make sure that devices and
621 				 * slices have a consistent size.  If
622 				 * they differ by a significant amount
623 				 * (~16MB) then report an error.
624 				 */
625 				if (!dontreport &&
626 				    (vdev_size != -1LL &&
627 				    (llabs(size - vdev_size) >
628 				    ZPOOL_FUZZ))) {
629 					if (ret != NULL)
630 						free(ret);
631 					ret = NULL;
632 					if (fatal)
633 						vdev_error(gettext(
634 						    "%s contains devices of "
635 						    "different sizes\n"),
636 						    rep.zprl_type);
637 					else
638 						return (NULL);
639 					dontreport = B_TRUE;
640 				}
641 
642 				type = childtype;
643 				vdev_size = size;
644 			}
645 		}
646 
647 		/*
648 		 * At this point, we have the replication of the last toplevel
649 		 * vdev in 'rep'.  Compare it to 'lastrep' to see if it is
650 		 * different.
651 		 */
652 		if (lastrep.zprl_type != NULL) {
653 			if (is_raidz_mirror(&lastrep, &rep, &raidz, &mirror) ||
654 			    is_raidz_mirror(&rep, &lastrep, &raidz, &mirror)) {
655 				/*
656 				 * Accepted raidz and mirror when they can
657 				 * handle the same number of disk failures.
658 				 */
659 				if (raidz->zprl_parity !=
660 				    mirror->zprl_children - 1) {
661 					if (ret != NULL)
662 						free(ret);
663 					ret = NULL;
664 					if (fatal)
665 						vdev_error(gettext(
666 						    "mismatched replication "
667 						    "level: "
668 						    "%s and %s vdevs with "
669 						    "different redundancy, "
670 						    "%llu vs. %llu (%llu-way) "
671 						    "are present\n"),
672 						    raidz->zprl_type,
673 						    mirror->zprl_type,
674 						    raidz->zprl_parity,
675 						    mirror->zprl_children - 1,
676 						    mirror->zprl_children);
677 					else
678 						return (NULL);
679 				}
680 			} else if (strcmp(lastrep.zprl_type, rep.zprl_type) !=
681 			    0) {
682 				if (ret != NULL)
683 					free(ret);
684 				ret = NULL;
685 				if (fatal)
686 					vdev_error(gettext(
687 					    "mismatched replication level: "
688 					    "both %s and %s vdevs are "
689 					    "present\n"),
690 					    lastrep.zprl_type, rep.zprl_type);
691 				else
692 					return (NULL);
693 			} else if (lastrep.zprl_parity != rep.zprl_parity) {
694 				if (ret)
695 					free(ret);
696 				ret = NULL;
697 				if (fatal)
698 					vdev_error(gettext(
699 					    "mismatched replication level: "
700 					    "both %llu and %llu device parity "
701 					    "%s vdevs are present\n"),
702 					    lastrep.zprl_parity,
703 					    rep.zprl_parity,
704 					    rep.zprl_type);
705 				else
706 					return (NULL);
707 			} else if (lastrep.zprl_children != rep.zprl_children) {
708 				if (ret)
709 					free(ret);
710 				ret = NULL;
711 				if (fatal)
712 					vdev_error(gettext(
713 					    "mismatched replication level: "
714 					    "both %llu-way and %llu-way %s "
715 					    "vdevs are present\n"),
716 					    lastrep.zprl_children,
717 					    rep.zprl_children,
718 					    rep.zprl_type);
719 				else
720 					return (NULL);
721 			}
722 		}
723 		lastrep = rep;
724 	}
725 
726 	if (ret != NULL)
727 		*ret = rep;
728 
729 	return (ret);
730 }
731 
732 /*
733  * Check the replication level of the vdev spec against the current pool.  Calls
734  * get_replication() to make sure the new spec is self-consistent.  If the pool
735  * has a consistent replication level, then we ignore any errors.  Otherwise,
736  * report any difference between the two.
737  */
738 static int
739 check_replication(nvlist_t *config, nvlist_t *newroot)
740 {
741 	nvlist_t **child;
742 	uint_t	children;
743 	replication_level_t *current = NULL, *new;
744 	replication_level_t *raidz, *mirror;
745 	int ret;
746 
747 	/*
748 	 * If we have a current pool configuration, check to see if it's
749 	 * self-consistent.  If not, simply return success.
750 	 */
751 	if (config != NULL) {
752 		nvlist_t *nvroot;
753 
754 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
755 		    &nvroot) == 0);
756 		if ((current = get_replication(nvroot, B_FALSE)) == NULL)
757 			return (0);
758 	}
759 	/*
760 	 * for spares there may be no children, and therefore no
761 	 * replication level to check
762 	 */
763 	if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
764 	    &child, &children) != 0) || (children == 0)) {
765 		free(current);
766 		return (0);
767 	}
768 
769 	/*
770 	 * If all we have is logs then there's no replication level to check.
771 	 */
772 	if (num_logs(newroot) == children) {
773 		free(current);
774 		return (0);
775 	}
776 
777 	/*
778 	 * Get the replication level of the new vdev spec, reporting any
779 	 * inconsistencies found.
780 	 */
781 	if ((new = get_replication(newroot, B_TRUE)) == NULL) {
782 		free(current);
783 		return (-1);
784 	}
785 
786 	/*
787 	 * Check to see if the new vdev spec matches the replication level of
788 	 * the current pool.
789 	 */
790 	ret = 0;
791 	if (current != NULL) {
792 		if (is_raidz_mirror(current, new, &raidz, &mirror) ||
793 		    is_raidz_mirror(new, current, &raidz, &mirror)) {
794 			if (raidz->zprl_parity != mirror->zprl_children - 1) {
795 				vdev_error(gettext(
796 				    "mismatched replication level: pool and "
797 				    "new vdev with different redundancy, %s "
798 				    "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
799 				    raidz->zprl_type,
800 				    mirror->zprl_type,
801 				    raidz->zprl_parity,
802 				    mirror->zprl_children - 1,
803 				    mirror->zprl_children);
804 				ret = -1;
805 			}
806 		} else if (strcmp(current->zprl_type, new->zprl_type) != 0) {
807 			vdev_error(gettext(
808 			    "mismatched replication level: pool uses %s "
809 			    "and new vdev is %s\n"),
810 			    current->zprl_type, new->zprl_type);
811 			ret = -1;
812 		} else if (current->zprl_parity != new->zprl_parity) {
813 			vdev_error(gettext(
814 			    "mismatched replication level: pool uses %llu "
815 			    "device parity and new vdev uses %llu\n"),
816 			    current->zprl_parity, new->zprl_parity);
817 			ret = -1;
818 		} else if (current->zprl_children != new->zprl_children) {
819 			vdev_error(gettext(
820 			    "mismatched replication level: pool uses %llu-way "
821 			    "%s and new vdev uses %llu-way %s\n"),
822 			    current->zprl_children, current->zprl_type,
823 			    new->zprl_children, new->zprl_type);
824 			ret = -1;
825 		}
826 	}
827 
828 	free(new);
829 	if (current != NULL)
830 		free(current);
831 
832 	return (ret);
833 }
834 
835 static int
836 zero_label(char *path)
837 {
838 	const int size = 4096;
839 	char buf[size];
840 	int err, fd;
841 
842 	if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
843 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
844 		    path, strerror(errno));
845 		return (-1);
846 	}
847 
848 	memset(buf, 0, size);
849 	err = write(fd, buf, size);
850 	(void) fdatasync(fd);
851 	(void) close(fd);
852 
853 	if (err == -1) {
854 		(void) fprintf(stderr, gettext("cannot zero first %d bytes "
855 		    "of '%s': %s\n"), size, path, strerror(errno));
856 		return (-1);
857 	}
858 
859 	if (err != size) {
860 		(void) fprintf(stderr, gettext("could only zero %d/%d bytes "
861 		    "of '%s'\n"), err, size, path);
862 		return (-1);
863 	}
864 
865 	return (0);
866 }
867 
868 /*
869  * Go through and find any whole disks in the vdev specification, labelling them
870  * as appropriate.  When constructing the vdev spec, we were unable to open this
871  * device in order to provide a devid.  Now that we have labelled the disk and
872  * know that slice 0 is valid, we can construct the devid now.
873  *
874  * If the disk was already labeled with an EFI label, we will have gotten the
875  * devid already (because we were able to open the whole disk).  Otherwise, we
876  * need to get the devid after we label the disk.
877  */
878 static int
879 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
880 {
881 	nvlist_t **child;
882 	uint_t c, children;
883 	char *type, *path;
884 	char devpath[MAXPATHLEN];
885 	char udevpath[MAXPATHLEN];
886 	uint64_t wholedisk;
887 	struct stat64 statbuf;
888 	int is_exclusive = 0;
889 	int fd;
890 	int ret;
891 
892 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
893 
894 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
895 	    &child, &children) != 0) {
896 
897 		if (strcmp(type, VDEV_TYPE_DISK) != 0)
898 			return (0);
899 
900 		/*
901 		 * We have a disk device.  If this is a whole disk write
902 		 * out the efi partition table, otherwise write zero's to
903 		 * the first 4k of the partition.  This is to ensure that
904 		 * libblkid will not misidentify the partition due to a
905 		 * magic value left by the previous filesystem.
906 		 */
907 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
908 		verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
909 		    &wholedisk));
910 
911 		if (!wholedisk) {
912 			/*
913 			 * Update device id string for mpath nodes (Linux only)
914 			 */
915 			if (is_mpath_whole_disk(path))
916 				update_vdev_config_dev_strs(nv);
917 
918 			if (!is_spare(NULL, path))
919 				(void) zero_label(path);
920 			return (0);
921 		}
922 
923 		if (realpath(path, devpath) == NULL) {
924 			ret = errno;
925 			(void) fprintf(stderr,
926 			    gettext("cannot resolve path '%s'\n"), path);
927 			return (ret);
928 		}
929 
930 		/*
931 		 * Remove any previously existing symlink from a udev path to
932 		 * the device before labeling the disk.  This ensures that
933 		 * only newly created links are used.  Otherwise there is a
934 		 * window between when udev deletes and recreates the link
935 		 * during which access attempts will fail with ENOENT.
936 		 */
937 		strlcpy(udevpath, path, MAXPATHLEN);
938 		(void) zfs_append_partition(udevpath, MAXPATHLEN);
939 
940 		fd = open(devpath, O_RDWR|O_EXCL);
941 		if (fd == -1) {
942 			if (errno == EBUSY)
943 				is_exclusive = 1;
944 #ifdef __FreeBSD__
945 			if (errno == EPERM)
946 				is_exclusive = 1;
947 #endif
948 		} else {
949 			(void) close(fd);
950 		}
951 
952 		/*
953 		 * If the partition exists, contains a valid spare label,
954 		 * and is opened exclusively there is no need to partition
955 		 * it.  Hot spares have already been partitioned and are
956 		 * held open exclusively by the kernel as a safety measure.
957 		 *
958 		 * If the provided path is for a /dev/disk/ device its
959 		 * symbolic link will be removed, partition table created,
960 		 * and then block until udev creates the new link.
961 		 */
962 		if (!is_exclusive && !is_spare(NULL, udevpath)) {
963 			char *devnode = strrchr(devpath, '/') + 1;
964 
965 			ret = strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT));
966 			if (ret == 0) {
967 				ret = lstat64(udevpath, &statbuf);
968 				if (ret == 0 && S_ISLNK(statbuf.st_mode))
969 					(void) unlink(udevpath);
970 			}
971 
972 			/*
973 			 * When labeling a pool the raw device node name
974 			 * is provided as it appears under /dev/.
975 			 */
976 			if (zpool_label_disk(g_zfs, zhp, devnode) == -1)
977 				return (-1);
978 
979 			/*
980 			 * Wait for udev to signal the device is available
981 			 * by the provided path.
982 			 */
983 			ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
984 			if (ret) {
985 				(void) fprintf(stderr,
986 				    gettext("missing link: %s was "
987 				    "partitioned but %s is missing\n"),
988 				    devnode, udevpath);
989 				return (ret);
990 			}
991 
992 			ret = zero_label(udevpath);
993 			if (ret)
994 				return (ret);
995 		}
996 
997 		/*
998 		 * Update the path to refer to the partition.  The presence of
999 		 * the 'whole_disk' field indicates to the CLI that we should
1000 		 * chop off the partition number when displaying the device in
1001 		 * future output.
1002 		 */
1003 		verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1004 
1005 		/*
1006 		 * Update device id strings for whole disks (Linux only)
1007 		 */
1008 		update_vdev_config_dev_strs(nv);
1009 
1010 		return (0);
1011 	}
1012 
1013 	for (c = 0; c < children; c++)
1014 		if ((ret = make_disks(zhp, child[c])) != 0)
1015 			return (ret);
1016 
1017 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1018 	    &child, &children) == 0)
1019 		for (c = 0; c < children; c++)
1020 			if ((ret = make_disks(zhp, child[c])) != 0)
1021 				return (ret);
1022 
1023 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1024 	    &child, &children) == 0)
1025 		for (c = 0; c < children; c++)
1026 			if ((ret = make_disks(zhp, child[c])) != 0)
1027 				return (ret);
1028 
1029 	return (0);
1030 }
1031 
1032 /*
1033  * Go through and find any devices that are in use.  We rely on libdiskmgt for
1034  * the majority of this task.
1035  */
1036 static boolean_t
1037 is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1038     boolean_t replacing, boolean_t isspare)
1039 {
1040 	nvlist_t **child;
1041 	uint_t c, children;
1042 	char *type, *path;
1043 	int ret = 0;
1044 	char buf[MAXPATHLEN];
1045 	uint64_t wholedisk = B_FALSE;
1046 	boolean_t anyinuse = B_FALSE;
1047 
1048 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1049 
1050 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1051 	    &child, &children) != 0) {
1052 
1053 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1054 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1055 			verify(!nvlist_lookup_uint64(nv,
1056 			    ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1057 
1058 		/*
1059 		 * As a generic check, we look to see if this is a replace of a
1060 		 * hot spare within the same pool.  If so, we allow it
1061 		 * regardless of what libblkid or zpool_in_use() says.
1062 		 */
1063 		if (replacing) {
1064 			(void) strlcpy(buf, path, sizeof (buf));
1065 			if (wholedisk) {
1066 				ret = zfs_append_partition(buf,  sizeof (buf));
1067 				if (ret == -1)
1068 					return (-1);
1069 			}
1070 
1071 			if (is_spare(config, buf))
1072 				return (B_FALSE);
1073 		}
1074 
1075 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1076 			ret = check_device(path, force, isspare, wholedisk);
1077 
1078 		else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1079 			ret = check_file(path, force, isspare);
1080 
1081 		return (ret != 0);
1082 	}
1083 
1084 	for (c = 0; c < children; c++)
1085 		if (is_device_in_use(config, child[c], force, replacing,
1086 		    B_FALSE))
1087 			anyinuse = B_TRUE;
1088 
1089 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1090 	    &child, &children) == 0)
1091 		for (c = 0; c < children; c++)
1092 			if (is_device_in_use(config, child[c], force, replacing,
1093 			    B_TRUE))
1094 				anyinuse = B_TRUE;
1095 
1096 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1097 	    &child, &children) == 0)
1098 		for (c = 0; c < children; c++)
1099 			if (is_device_in_use(config, child[c], force, replacing,
1100 			    B_FALSE))
1101 				anyinuse = B_TRUE;
1102 
1103 	return (anyinuse);
1104 }
1105 
1106 static const char *
1107 is_grouping(const char *type, int *mindev, int *maxdev)
1108 {
1109 	if (strncmp(type, "raidz", 5) == 0) {
1110 		const char *p = type + 5;
1111 		char *end;
1112 		long nparity;
1113 
1114 		if (*p == '\0') {
1115 			nparity = 1;
1116 		} else if (*p == '0') {
1117 			return (NULL); /* no zero prefixes allowed */
1118 		} else {
1119 			errno = 0;
1120 			nparity = strtol(p, &end, 10);
1121 			if (errno != 0 || nparity < 1 || nparity >= 255 ||
1122 			    *end != '\0')
1123 				return (NULL);
1124 		}
1125 
1126 		if (mindev != NULL)
1127 			*mindev = nparity + 1;
1128 		if (maxdev != NULL)
1129 			*maxdev = 255;
1130 		return (VDEV_TYPE_RAIDZ);
1131 	}
1132 
1133 	if (maxdev != NULL)
1134 		*maxdev = INT_MAX;
1135 
1136 	if (strcmp(type, "mirror") == 0) {
1137 		if (mindev != NULL)
1138 			*mindev = 2;
1139 		return (VDEV_TYPE_MIRROR);
1140 	}
1141 
1142 	if (strcmp(type, "spare") == 0) {
1143 		if (mindev != NULL)
1144 			*mindev = 1;
1145 		return (VDEV_TYPE_SPARE);
1146 	}
1147 
1148 	if (strcmp(type, "log") == 0) {
1149 		if (mindev != NULL)
1150 			*mindev = 1;
1151 		return (VDEV_TYPE_LOG);
1152 	}
1153 
1154 	if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
1155 	    strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1156 		if (mindev != NULL)
1157 			*mindev = 1;
1158 		return (type);
1159 	}
1160 
1161 	if (strcmp(type, "cache") == 0) {
1162 		if (mindev != NULL)
1163 			*mindev = 1;
1164 		return (VDEV_TYPE_L2CACHE);
1165 	}
1166 
1167 	return (NULL);
1168 }
1169 
1170 /*
1171  * Construct a syntactically valid vdev specification,
1172  * and ensure that all devices and files exist and can be opened.
1173  * Note: we don't bother freeing anything in the error paths
1174  * because the program is just going to exit anyway.
1175  */
1176 static nvlist_t *
1177 construct_spec(nvlist_t *props, int argc, char **argv)
1178 {
1179 	nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1180 	int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1181 	const char *type;
1182 	uint64_t is_log, is_special, is_dedup;
1183 	boolean_t seen_logs;
1184 
1185 	top = NULL;
1186 	toplevels = 0;
1187 	spares = NULL;
1188 	l2cache = NULL;
1189 	nspares = 0;
1190 	nlogs = 0;
1191 	nl2cache = 0;
1192 	is_log = is_special = is_dedup = B_FALSE;
1193 	seen_logs = B_FALSE;
1194 	nvroot = NULL;
1195 
1196 	while (argc > 0) {
1197 		nv = NULL;
1198 
1199 		/*
1200 		 * If it's a mirror or raidz, the subsequent arguments are
1201 		 * its leaves -- until we encounter the next mirror or raidz.
1202 		 */
1203 		if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
1204 			nvlist_t **child = NULL;
1205 			int c, children = 0;
1206 
1207 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1208 				if (spares != NULL) {
1209 					(void) fprintf(stderr,
1210 					    gettext("invalid vdev "
1211 					    "specification: 'spare' can be "
1212 					    "specified only once\n"));
1213 					goto spec_out;
1214 				}
1215 				is_log = is_special = is_dedup = B_FALSE;
1216 			}
1217 
1218 			if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1219 				if (seen_logs) {
1220 					(void) fprintf(stderr,
1221 					    gettext("invalid vdev "
1222 					    "specification: 'log' can be "
1223 					    "specified only once\n"));
1224 					goto spec_out;
1225 				}
1226 				seen_logs = B_TRUE;
1227 				is_log = B_TRUE;
1228 				is_special = B_FALSE;
1229 				is_dedup = B_FALSE;
1230 				argc--;
1231 				argv++;
1232 				/*
1233 				 * A log is not a real grouping device.
1234 				 * We just set is_log and continue.
1235 				 */
1236 				continue;
1237 			}
1238 
1239 			if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1240 				is_special = B_TRUE;
1241 				is_log = B_FALSE;
1242 				is_dedup = B_FALSE;
1243 				argc--;
1244 				argv++;
1245 				continue;
1246 			}
1247 
1248 			if (strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1249 				is_dedup = B_TRUE;
1250 				is_log = B_FALSE;
1251 				is_special = B_FALSE;
1252 				argc--;
1253 				argv++;
1254 				continue;
1255 			}
1256 
1257 			if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1258 				if (l2cache != NULL) {
1259 					(void) fprintf(stderr,
1260 					    gettext("invalid vdev "
1261 					    "specification: 'cache' can be "
1262 					    "specified only once\n"));
1263 					goto spec_out;
1264 				}
1265 				is_log = is_special = is_dedup = B_FALSE;
1266 			}
1267 
1268 			if (is_log || is_special || is_dedup) {
1269 				if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1270 					(void) fprintf(stderr,
1271 					    gettext("invalid vdev "
1272 					    "specification: unsupported '%s' "
1273 					    "device: %s\n"), is_log ? "log" :
1274 					    "special", type);
1275 					goto spec_out;
1276 				}
1277 				nlogs++;
1278 			}
1279 
1280 			for (c = 1; c < argc; c++) {
1281 				if (is_grouping(argv[c], NULL, NULL) != NULL)
1282 					break;
1283 				children++;
1284 				child = realloc(child,
1285 				    children * sizeof (nvlist_t *));
1286 				if (child == NULL)
1287 					zpool_no_memory();
1288 				if ((nv = make_leaf_vdev(props, argv[c],
1289 				    B_FALSE)) == NULL) {
1290 					for (c = 0; c < children - 1; c++)
1291 						nvlist_free(child[c]);
1292 					free(child);
1293 					goto spec_out;
1294 				}
1295 
1296 				child[children - 1] = nv;
1297 			}
1298 
1299 			if (children < mindev) {
1300 				(void) fprintf(stderr, gettext("invalid vdev "
1301 				    "specification: %s requires at least %d "
1302 				    "devices\n"), argv[0], mindev);
1303 				for (c = 0; c < children; c++)
1304 					nvlist_free(child[c]);
1305 				free(child);
1306 				goto spec_out;
1307 			}
1308 
1309 			if (children > maxdev) {
1310 				(void) fprintf(stderr, gettext("invalid vdev "
1311 				    "specification: %s supports no more than "
1312 				    "%d devices\n"), argv[0], maxdev);
1313 				for (c = 0; c < children; c++)
1314 					nvlist_free(child[c]);
1315 				free(child);
1316 				goto spec_out;
1317 			}
1318 
1319 			argc -= c;
1320 			argv += c;
1321 
1322 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1323 				spares = child;
1324 				nspares = children;
1325 				continue;
1326 			} else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1327 				l2cache = child;
1328 				nl2cache = children;
1329 				continue;
1330 			} else {
1331 				/* create a top-level vdev with children */
1332 				verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1333 				    0) == 0);
1334 				verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1335 				    type) == 0);
1336 				verify(nvlist_add_uint64(nv,
1337 				    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1338 				if (is_log)
1339 					verify(nvlist_add_string(nv,
1340 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1341 					    VDEV_ALLOC_BIAS_LOG) == 0);
1342 				if (is_special) {
1343 					verify(nvlist_add_string(nv,
1344 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1345 					    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1346 				}
1347 				if (is_dedup) {
1348 					verify(nvlist_add_string(nv,
1349 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1350 					    VDEV_ALLOC_BIAS_DEDUP) == 0);
1351 				}
1352 				if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1353 					verify(nvlist_add_uint64(nv,
1354 					    ZPOOL_CONFIG_NPARITY,
1355 					    mindev - 1) == 0);
1356 				}
1357 				verify(nvlist_add_nvlist_array(nv,
1358 				    ZPOOL_CONFIG_CHILDREN, child,
1359 				    children) == 0);
1360 
1361 				for (c = 0; c < children; c++)
1362 					nvlist_free(child[c]);
1363 				free(child);
1364 			}
1365 		} else {
1366 			/*
1367 			 * We have a device.  Pass off to make_leaf_vdev() to
1368 			 * construct the appropriate nvlist describing the vdev.
1369 			 */
1370 			if ((nv = make_leaf_vdev(props, argv[0],
1371 			    is_log)) == NULL)
1372 				goto spec_out;
1373 
1374 			if (is_log)
1375 				nlogs++;
1376 			if (is_special) {
1377 				verify(nvlist_add_string(nv,
1378 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1379 				    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1380 			}
1381 			if (is_dedup) {
1382 				verify(nvlist_add_string(nv,
1383 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1384 				    VDEV_ALLOC_BIAS_DEDUP) == 0);
1385 			}
1386 			argc--;
1387 			argv++;
1388 		}
1389 
1390 		toplevels++;
1391 		top = realloc(top, toplevels * sizeof (nvlist_t *));
1392 		if (top == NULL)
1393 			zpool_no_memory();
1394 		top[toplevels - 1] = nv;
1395 	}
1396 
1397 	if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1398 		(void) fprintf(stderr, gettext("invalid vdev "
1399 		    "specification: at least one toplevel vdev must be "
1400 		    "specified\n"));
1401 		goto spec_out;
1402 	}
1403 
1404 	if (seen_logs && nlogs == 0) {
1405 		(void) fprintf(stderr, gettext("invalid vdev specification: "
1406 		    "log requires at least 1 device\n"));
1407 		goto spec_out;
1408 	}
1409 
1410 	/*
1411 	 * Finally, create nvroot and add all top-level vdevs to it.
1412 	 */
1413 	verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1414 	verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1415 	    VDEV_TYPE_ROOT) == 0);
1416 	verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1417 	    top, toplevels) == 0);
1418 	if (nspares != 0)
1419 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1420 		    spares, nspares) == 0);
1421 	if (nl2cache != 0)
1422 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1423 		    l2cache, nl2cache) == 0);
1424 
1425 spec_out:
1426 	for (t = 0; t < toplevels; t++)
1427 		nvlist_free(top[t]);
1428 	for (t = 0; t < nspares; t++)
1429 		nvlist_free(spares[t]);
1430 	for (t = 0; t < nl2cache; t++)
1431 		nvlist_free(l2cache[t]);
1432 
1433 	free(spares);
1434 	free(l2cache);
1435 	free(top);
1436 
1437 	return (nvroot);
1438 }
1439 
1440 nvlist_t *
1441 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1442     splitflags_t flags, int argc, char **argv)
1443 {
1444 	nvlist_t *newroot = NULL, **child;
1445 	uint_t c, children;
1446 
1447 	if (argc > 0) {
1448 		if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1449 			(void) fprintf(stderr, gettext("Unable to build a "
1450 			    "pool from the specified devices\n"));
1451 			return (NULL);
1452 		}
1453 
1454 		if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1455 			nvlist_free(newroot);
1456 			return (NULL);
1457 		}
1458 
1459 		/* avoid any tricks in the spec */
1460 		verify(nvlist_lookup_nvlist_array(newroot,
1461 		    ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1462 		for (c = 0; c < children; c++) {
1463 			char *path;
1464 			const char *type;
1465 			int min, max;
1466 
1467 			verify(nvlist_lookup_string(child[c],
1468 			    ZPOOL_CONFIG_PATH, &path) == 0);
1469 			if ((type = is_grouping(path, &min, &max)) != NULL) {
1470 				(void) fprintf(stderr, gettext("Cannot use "
1471 				    "'%s' as a device for splitting\n"), type);
1472 				nvlist_free(newroot);
1473 				return (NULL);
1474 			}
1475 		}
1476 	}
1477 
1478 	if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1479 		nvlist_free(newroot);
1480 		return (NULL);
1481 	}
1482 
1483 	return (newroot);
1484 }
1485 
1486 static int
1487 num_normal_vdevs(nvlist_t *nvroot)
1488 {
1489 	nvlist_t **top;
1490 	uint_t t, toplevels, normal = 0;
1491 
1492 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1493 	    &top, &toplevels) == 0);
1494 
1495 	for (t = 0; t < toplevels; t++) {
1496 		uint64_t log = B_FALSE;
1497 
1498 		(void) nvlist_lookup_uint64(top[t], ZPOOL_CONFIG_IS_LOG, &log);
1499 		if (log)
1500 			continue;
1501 		if (nvlist_exists(top[t], ZPOOL_CONFIG_ALLOCATION_BIAS))
1502 			continue;
1503 
1504 		normal++;
1505 	}
1506 
1507 	return (normal);
1508 }
1509 
1510 /*
1511  * Get and validate the contents of the given vdev specification.  This ensures
1512  * that the nvlist returned is well-formed, that all the devices exist, and that
1513  * they are not currently in use by any other known consumer.  The 'poolconfig'
1514  * parameter is the current configuration of the pool when adding devices
1515  * existing pool, and is used to perform additional checks, such as changing the
1516  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1517  * new pool.  The 'force' flag controls whether devices should be forcefully
1518  * added, even if they appear in use.
1519  */
1520 nvlist_t *
1521 make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1522     boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1523 {
1524 	nvlist_t *newroot;
1525 	nvlist_t *poolconfig = NULL;
1526 	is_force = force;
1527 
1528 	/*
1529 	 * Construct the vdev specification.  If this is successful, we know
1530 	 * that we have a valid specification, and that all devices can be
1531 	 * opened.
1532 	 */
1533 	if ((newroot = construct_spec(props, argc, argv)) == NULL)
1534 		return (NULL);
1535 
1536 	if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
1537 		nvlist_free(newroot);
1538 		return (NULL);
1539 	}
1540 
1541 	/*
1542 	 * Validate each device to make sure that it's not shared with another
1543 	 * subsystem.  We do this even if 'force' is set, because there are some
1544 	 * uses (such as a dedicated dump device) that even '-f' cannot
1545 	 * override.
1546 	 */
1547 	if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1548 		nvlist_free(newroot);
1549 		return (NULL);
1550 	}
1551 
1552 	/*
1553 	 * Check the replication level of the given vdevs and report any errors
1554 	 * found.  We include the existing pool spec, if any, as we need to
1555 	 * catch changes against the existing replication level.
1556 	 */
1557 	if (check_rep && check_replication(poolconfig, newroot) != 0) {
1558 		nvlist_free(newroot);
1559 		return (NULL);
1560 	}
1561 
1562 	/*
1563 	 * On pool create the new vdev spec must have one normal vdev.
1564 	 */
1565 	if (poolconfig == NULL && num_normal_vdevs(newroot) == 0) {
1566 		vdev_error(gettext("at least one general top-level vdev must "
1567 		    "be specified\n"));
1568 		nvlist_free(newroot);
1569 		return (NULL);
1570 	}
1571 
1572 	/*
1573 	 * Run through the vdev specification and label any whole disks found.
1574 	 */
1575 	if (!dryrun && make_disks(zhp, newroot) != 0) {
1576 		nvlist_free(newroot);
1577 		return (NULL);
1578 	}
1579 
1580 	return (newroot);
1581 }
1582